Added choice of simulation model for the pattern tessellation simulation. Refactoring

This commit is contained in:
iasonmanolas 2022-07-12 13:09:25 +03:00
parent 2c462d9ebf
commit a0ed388c56
1 changed files with 60 additions and 36 deletions

View File

@ -1,4 +1,5 @@
#include "reducedmodelevaluator.hpp"
#include "chronoseulersimulationmodel.hpp"
#include "hexagonremesher.hpp"
#include "reducedmodel.hpp"
#include "reducedmodeloptimizer.hpp"
@ -212,8 +213,8 @@ void ReducedModelEvaluator::printResultsVertically(const Results &evaluationResu
csvFile &csvOutput)
{
#ifdef POLYSCOPE_DEFINED
csvOutput << "drm2Reduced"
<< "norm_drm2Reduced";
csvOutput << "pattern2Reduced"
<< "norm_pattern2Reduced";
#else
csvOutput << "Job Label"
<< "drm2Reduced"
@ -378,7 +379,9 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
std::shared_ptr<SimulationMesh> pTiledFullPattern_simulationMesh;
pTiledFullPattern_simulationMesh = std::make_shared<SimulationMesh>(*pTiledFullPattern);
//NOTE: Those should be derived from the optimization results instead of hardcoding them
pTiledFullPattern_simulationMesh->setBeamCrossSection(CrossSectionType{0.002, 0.002});
constexpr double beamWidth = 0.001;
constexpr double beamHeight = 0.001;
pTiledFullPattern_simulationMesh->setBeamCrossSection(CrossSectionType{beamWidth, beamHeight});
if (optimizationResult.fullPatternYoungsModulus == 0) {
std::cerr << "Full pattern's young modulus not found." << std::endl;
std::terminate();
@ -415,10 +418,20 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
drmSimulationSettings.shouldCreatePlots = true;
#endif
constexpr bool shouldRerunFullPatternSimulation = false;
enum PatternSimulationModelTag { DRM, Chronos };
const PatternSimulationModelTag simulationModelTag{DRM};
const std::string simulationModelLabel = [&]() {
switch (simulationModelTag) {
case DRM:
return DRMSimulationModel::label;
case Chronos:
return ChronosEulerSimulationModel::label;
}
}();
// for (int jobIndex = 0; jobIndex < scenariosTestSetLabels.size(); jobIndex++) {
std::for_each(
//#ifndef POLYSCOPE_DEFINED
// std::execution::par_unseq,
std::execution::par_unseq,
//#endif
scenariosTestSetLabels.begin(),
scenariosTestSetLabels.end(),
@ -452,7 +465,7 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
// polyscope::show();
const std::filesystem::path surfaceFolderPath
= std::filesystem::path(fullPatternTessellatedResultsDirectoryPath)
.append(pTileIntoSurface->getLabel());
.append(simulationModelLabel + "_" + pTileIntoSurface->getLabel());
const std::string scenarioLabel = pJob_tiledFullPattern->getLabel();
const std::filesystem::path scenarioDirectoryPath
= std::filesystem::path(surfaceFolderPath).append(scenarioLabel);
@ -476,23 +489,24 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
return patternLabel;
}
}();
const auto fullResultsFolderPath = std::filesystem::path(scenarioDirectoryPath)
.append(patternLabel)
.append("Results");
if (shouldRerunFullPatternSimulation && std::filesystem::exists(fullResultsFolderPath)) {
std::filesystem::remove_all(fullResultsFolderPath);
const auto tilledPatternResultsFolderPath = std::filesystem::path(scenarioDirectoryPath)
.append(patternLabel)
.append("Results");
if (shouldRerunFullPatternSimulation
&& std::filesystem::exists(tilledPatternResultsFolderPath)) {
std::filesystem::remove_all(tilledPatternResultsFolderPath);
}
const std::filesystem::path fullPatternJobFolderPath = std::filesystem::path(
scenarioDirectoryPath)
.append(patternLabel)
.append("SimulationJob");
SimulationResults simulationResults_tiledFullPattern_drm;
if (std::filesystem::exists(fullResultsFolderPath)) {
SimulationResults simulationResults_tiledPattern;
if (std::filesystem::exists(tilledPatternResultsFolderPath)) {
//Load full pattern results
assert(std::filesystem::exists(fullPatternJobFolderPath));
simulationResults_tiledFullPattern_drm.load(fullResultsFolderPath,
fullPatternJobFolderPath);
simulationResults_tiledPattern.load(tilledPatternResultsFolderPath,
fullPatternJobFolderPath);
//#ifdef POLYSCOPE_DEFINED
// std::array<double, 3> resultsColor({28.0, 99.0, 227.0});
// simulationResults_tiledFullPattern_drm.registerForDrawing(resultsColor);
@ -518,27 +532,38 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
// simulationResults_tiledFullPattern_drm.unregister();
//#endif
simulationResults_tiledFullPattern_drm.converged = true;
simulationResults_tiledPattern.converged = true;
} else {
std::cout << "Drm results not found in:" << fullResultsFolderPath << std::endl;
std::cout << "Tilled pattern simulation results not found in:"
<< tilledPatternResultsFolderPath << std::endl;
//Full
std::cout << "Executing:" << jobLabel << std::endl;
DRMSimulationModel drmSimulationModel;
simulationResults_tiledFullPattern_drm
= drmSimulationModel.executeSimulation(pJob_tiledFullPattern,
drmSimulationSettings);
simulationResults_tiledFullPattern_drm.setLabelPrefix("DRM");
switch (simulationModelTag) {
case DRM: {
DRMSimulationModel drmSimulationModel;
simulationResults_tiledPattern = drmSimulationModel
.executeSimulation(pJob_tiledFullPattern,
drmSimulationSettings);
break;
}
case Chronos: {
ChronosEulerSimulationModel chronosSimulationModel;
simulationResults_tiledPattern = chronosSimulationModel.executeSimulation(
pJob_tiledFullPattern);
break;
}
}
}
if (!simulationResults_tiledFullPattern_drm.converged) {
if (!simulationResults_tiledPattern.converged) {
std::cerr << "Full pattern simulation failed." << std::endl;
std::cerr << "Not saving results" << std::endl;
// continue;
return;
}
std::filesystem::create_directories(fullResultsFolderPath);
std::filesystem::create_directories(tilledPatternResultsFolderPath);
const std::filesystem::path drmResultsOutputPath
= std::filesystem::path(scenarioDirectoryPath).append(patternLabel);
simulationResults_tiledFullPattern_drm.save(drmResultsOutputPath);
simulationResults_tiledPattern.save(drmResultsOutputPath);
LinearSimulationModel linearSimulationModel;
SimulationResults simulationResults_tiledReducedPattern
@ -549,22 +574,21 @@ ReducedModelEvaluator::Results ReducedModelEvaluator::evaluateReducedModel(
// polyscope::show();
//compute the full2reduced distance
const double distance_fullDrmToReduced
= simulationResults_tiledFullPattern_drm
.computeDistance(simulationResults_tiledReducedPattern, fullToReducedViMap);
double distance_fullSumOfAllVerts = 0;
const double distance_patternToReduced = simulationResults_tiledPattern.computeDistance(
simulationResults_tiledReducedPattern, fullToReducedViMap);
double distance_patternSumOfAllVerts = 0;
for (std::pair<size_t, size_t> fullToReducedPair : fullToReducedViMap) {
distance_fullSumOfAllVerts += simulationResults_tiledFullPattern_drm
.displacements[fullToReducedPair.first]
.getTranslation()
.norm();
distance_patternSumOfAllVerts += simulationResults_tiledPattern
.displacements[fullToReducedPair.first]
.getTranslation()
.norm();
}
const double distance_normalizedFullDrmToReduced = distance_fullDrmToReduced
/ distance_fullSumOfAllVerts;
const double distance_normalizedPatternToReduced = distance_patternToReduced
/ distance_patternSumOfAllVerts;
const int jobIndex = &jobLabel - &scenariosTestSetLabels[0];
evaluationResults.distances_drm2reduced[jobIndex] = distance_fullDrmToReduced;
evaluationResults.distances_drm2reduced[jobIndex] = distance_patternToReduced;
evaluationResults.distances_normalizedDrm2reduced[jobIndex]
= distance_normalizedFullDrmToReduced;
= distance_normalizedPatternToReduced;
});
return evaluationResults;