Refactoring

This commit is contained in:
iasonmanolas 2022-01-28 20:06:56 +02:00
parent 068626f299
commit 366727ced6
3 changed files with 36 additions and 26 deletions

View File

@ -61,7 +61,8 @@ struct GlobalOptimizationVariables
std::vector<double> xMax; std::vector<double> xMax;
std::vector<double> scenarioWeights; std::vector<double> scenarioWeights;
std::vector<ReducedModelOptimization::Settings::ObjectiveWeights> objectiveWeights; std::vector<ReducedModelOptimization::Settings::ObjectiveWeights> objectiveWeights;
} global; };
GlobalOptimizationVariables global;
double ReducedModelOptimizer::computeDisplacementError( double ReducedModelOptimizer::computeDisplacementError(
const std::vector<Vector6d> &fullPatternDisplacements, const std::vector<Vector6d> &fullPatternDisplacements,
@ -308,7 +309,7 @@ double ReducedModelOptimizer::objective(const std::vector<double> &x)
}); });
// double totalError = std::accumulate(simulationErrorsPerScenario.begin(), // double totalError = std::accumulate(simulationErrorsPerScenario.begin(),
// simulationErrorsPerScenario.end(), // simulationErrorsPerScenario.end(),
// 0); // 0.0);
const double totalError = std::reduce(std::execution::par_unseq, const double totalError = std::reduce(std::execution::par_unseq,
simulationErrorsPerScenario.begin(), simulationErrorsPerScenario.begin(),
simulationErrorsPerScenario.end()); simulationErrorsPerScenario.end());
@ -524,8 +525,9 @@ void ReducedModelOptimizer::computeMaps(PatternGeometry &fullPattern,
m_fullPatternOppositeInterfaceViPairs); m_fullPatternOppositeInterfaceViPairs);
} }
ReducedModelOptimizer::ReducedModelOptimizer(const std::vector<size_t> &numberOfNodesPerSlot) ReducedModelOptimizer::ReducedModelOptimizer()
{ {
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
FlatPatternTopology::constructNodeToSlotMap(numberOfNodesPerSlot, nodeToSlot); FlatPatternTopology::constructNodeToSlotMap(numberOfNodesPerSlot, nodeToSlot);
FlatPatternTopology::constructSlotToNodeMap(nodeToSlot, slotToNode); FlatPatternTopology::constructSlotToNodeMap(nodeToSlot, slotToNode);
interfaceNodeIndex = numberOfNodesPerSlot[0] + numberOfNodesPerSlot[3]; interfaceNodeIndex = numberOfNodesPerSlot[0] + numberOfNodesPerSlot[3];

View File

@ -42,7 +42,6 @@ class ReducedModelOptimizer
std::vector<bool> scenarioIsSymmetrical; std::vector<bool> scenarioIsSymmetrical;
int fullPatternNumberOfEdges; int fullPatternNumberOfEdges;
constexpr static double youngsModulus{1 * 1e9}; constexpr static double youngsModulus{1 * 1e9};
std::filesystem::path intermediateResultsDirectoryPath;
std::string fullPatternLabel; std::string fullPatternLabel;
public: public:
@ -82,7 +81,7 @@ public:
int interfaceNodeIndex; int interfaceNodeIndex;
double operator()(const Eigen::VectorXd &x, Eigen::VectorXd &) const; double operator()(const Eigen::VectorXd &x, Eigen::VectorXd &) const;
ReducedModelOptimizer(const std::vector<size_t> &numberOfNodesPerSlot); ReducedModelOptimizer();
static void computeReducedModelSimulationJob( static void computeReducedModelSimulationJob(
const SimulationJob &simulationJobOfFullModel, const SimulationJob &simulationJobOfFullModel,
const std::unordered_map<size_t, size_t> &fullToReducedMap, const std::unordered_map<size_t, size_t> &fullToReducedMap,
@ -304,4 +303,7 @@ inline std::function<void(const double &newJ,
inline std::function<void(const std::vector<double> &x, std::shared_ptr<SimulationMesh> &m)> inline std::function<void(const std::vector<double> &x, std::shared_ptr<SimulationMesh> &m)>
ReducedModelOptimizer::function_updateReducedPattern; ReducedModelOptimizer::function_updateReducedPattern;
struct GlobalOptimizationVariables;
extern GlobalOptimizationVariables global;
#endif // REDUCEDMODELOPTIMIZER_HPP #endif // REDUCEDMODELOPTIMIZER_HPP

View File

@ -126,6 +126,8 @@ struct Settings
double solverAccuracy{1e-3}; double solverAccuracy{1e-3};
double translationEpsilon{3e-3}; double translationEpsilon{3e-3};
double angularDistanceEpsilon{vcg::math::ToRad(0.0)}; double angularDistanceEpsilon{vcg::math::ToRad(0.0)};
double targetBaseTriangleSize{0.03};
std::filesystem::path intermediateResultsDirectoryPath;
struct ObjectiveWeights struct ObjectiveWeights
{ {
double translational{1.2}; double translational{1.2};
@ -206,6 +208,7 @@ struct Settings
json[GET_VARIABLE_NAME(translationEpsilon)] = translationEpsilon; json[GET_VARIABLE_NAME(translationEpsilon)] = translationEpsilon;
json[GET_VARIABLE_NAME(angularDistanceEpsilon)] = vcg::math::ToDeg( json[GET_VARIABLE_NAME(angularDistanceEpsilon)] = vcg::math::ToDeg(
angularDistanceEpsilon); angularDistanceEpsilon);
json[GET_VARIABLE_NAME(targetBaseTriangleSize)] = targetBaseTriangleSize;
std::filesystem::path jsonFilePath( std::filesystem::path jsonFilePath(
std::filesystem::path(saveToPath).append(defaultFilename)); std::filesystem::path(saveToPath).append(defaultFilename));
@ -214,20 +217,17 @@ struct Settings
jsonFile.close(); jsonFile.close();
} }
bool load(const std::filesystem::path &jsonFilepath) bool load(const std::filesystem::path &jsonFilePath)
{ {
assert(std::filesystem::is_directory(loadFromPath)); if (!std::filesystem::exists(jsonFilePath)) {
//Load optimal X
nlohmann::json json;
std::filesystem::path jsonFilepath(
std::filesystem::path(loadFromPath).append(JsonKeys::filename));
if (!std::filesystem::exists(jsonFilepath)) {
std::cerr << "Optimization settings could not be loaded because input filepath does " std::cerr << "Optimization settings could not be loaded because input filepath does "
"not exist:" "not exist:"
<< jsonFilepath << std::endl; << jsonFilePath << std::endl;
assert(false);
return false; return false;
} }
std::ifstream ifs(jsonFilepath); std::ifstream ifs(jsonFilePath);
nlohmann::json json;
ifs >> json; ifs >> json;
if (json.contains(GET_VARIABLE_NAME(optimizationStrategy))) { if (json.contains(GET_VARIABLE_NAME(optimizationStrategy))) {
@ -288,6 +288,11 @@ struct Settings
static_cast<double>(json[GET_VARIABLE_NAME(angularDistanceEpsilon)])); static_cast<double>(json[GET_VARIABLE_NAME(angularDistanceEpsilon)]));
} }
if (json.contains(GET_VARIABLE_NAME(targetBaseTriangleSize))) {
targetBaseTriangleSize = static_cast<double>(
json[GET_VARIABLE_NAME(targetBaseTriangleSize)]);
}
// perBaseScenarioObjectiveWeights = json.at(JsonKeys::ObjectiveWeights); // perBaseScenarioObjectiveWeights = json.at(JsonKeys::ObjectiveWeights);
// objectiveWeights.translational = json.at(JsonKeys::ObjectiveWeights); // objectiveWeights.translational = json.at(JsonKeys::ObjectiveWeights);
// objectiveWeights.rotational = 2 - objectiveWeights.translational; // objectiveWeights.rotational = 2 - objectiveWeights.translational;
@ -603,6 +608,7 @@ struct Settings
std::filesystem::path jsonFilepath( std::filesystem::path jsonFilepath(
std::filesystem::path(loadFromPath).append(JsonKeys::filename)); std::filesystem::path(loadFromPath).append(JsonKeys::filename));
if (!std::filesystem::exists(jsonFilepath)) { if (!std::filesystem::exists(jsonFilepath)) {
std::cerr << "Input path does not exist:" << loadFromPath << std::endl;
return false; return false;
} }
//Load optimal X //Load optimal X
@ -670,7 +676,7 @@ struct Settings
continue; continue;
} }
// Load full pattern files // Load full pattern files
for (const auto &fileEntry : filesystem::directory_iterator( for (const auto &fileEntry : std::filesystem::directory_iterator(
std::filesystem::path(simulationScenarioPath).append("Full"))) { std::filesystem::path(simulationScenarioPath).append("Full"))) {
const auto filepath = fileEntry.path(); const auto filepath = fileEntry.path();
if (filepath.extension() == ".json") { if (filepath.extension() == ".json") {
@ -781,8 +787,8 @@ struct Settings
const double beamWidth = std::sqrt(A); const double beamWidth = std::sqrt(A);
const double beamHeight = beamWidth; const double beamHeight = beamWidth;
CrossSectionType elementDimensions(beamWidth, beamHeight); CrossSectionType elementDimensions(beamWidth, beamHeight);
for (int ei = 0; ei < pTiledReducedPattern_simulationMesh->EN(); ei++) { for (int ei = 0; ei < pReducedPattern_simulationMesh->EN(); ei++) {
Element &e = pTiledReducedPattern_simulationMesh->elements[ei]; Element &e = pReducedPattern_simulationMesh->elements[ei];
e.setDimensions(elementDimensions); e.setDimensions(elementDimensions);
} }
} }
@ -792,8 +798,8 @@ struct Settings
if (optimalXVariables.contains(ymLabel)) { if (optimalXVariables.contains(ymLabel)) {
const double E = optimalXVariables.at(ymLabel); const double E = optimalXVariables.at(ymLabel);
const ElementMaterial elementMaterial(poissonsRatio, E); const ElementMaterial elementMaterial(poissonsRatio, E);
for (int ei = 0; ei < pTiledReducedPattern_simulationMesh->EN(); ei++) { for (int ei = 0; ei < pReducedPattern_simulationMesh->EN(); ei++) {
Element &e = pTiledReducedPattern_simulationMesh->elements[ei]; Element &e = pReducedPattern_simulationMesh->elements[ei];
e.setMaterial(elementMaterial); e.setMaterial(elementMaterial);
} }
} }
@ -801,8 +807,8 @@ struct Settings
const std::string JLabel = "J"; const std::string JLabel = "J";
if (optimalXVariables.contains(JLabel)) { if (optimalXVariables.contains(JLabel)) {
const double J = optimalXVariables.at(JLabel); const double J = optimalXVariables.at(JLabel);
for (int ei = 0; ei < pTiledReducedPattern_simulationMesh->EN(); ei++) { for (int ei = 0; ei < pReducedPattern_simulationMesh->EN(); ei++) {
Element &e = pTiledReducedPattern_simulationMesh->elements[ei]; Element &e = pReducedPattern_simulationMesh->elements[ei];
e.dimensions.inertia.J = J; e.dimensions.inertia.J = J;
} }
} }
@ -810,8 +816,8 @@ struct Settings
const std::string I2Label = "I2"; const std::string I2Label = "I2";
if (optimalXVariables.contains(I2Label)) { if (optimalXVariables.contains(I2Label)) {
const double I2 = optimalXVariables.at(I2Label); const double I2 = optimalXVariables.at(I2Label);
for (int ei = 0; ei < pTiledReducedPattern_simulationMesh->EN(); ei++) { for (int ei = 0; ei < pReducedPattern_simulationMesh->EN(); ei++) {
Element &e = pTiledReducedPattern_simulationMesh->elements[ei]; Element &e = pReducedPattern_simulationMesh->elements[ei];
e.dimensions.inertia.I2 = I2; e.dimensions.inertia.I2 = I2;
} }
} }
@ -819,12 +825,12 @@ struct Settings
const std::string I3Label = "I3"; const std::string I3Label = "I3";
if (optimalXVariables.contains(I3Label)) { if (optimalXVariables.contains(I3Label)) {
const double I3 = optimalXVariables.at(I3Label); const double I3 = optimalXVariables.at(I3Label);
for (int ei = 0; ei < pTiledReducedPattern_simulationMesh->EN(); ei++) { for (int ei = 0; ei < pReducedPattern_simulationMesh->EN(); ei++) {
Element &e = pTiledReducedPattern_simulationMesh->elements[ei]; Element &e = pReducedPattern_simulationMesh->elements[ei];
e.dimensions.inertia.I3 = I3; e.dimensions.inertia.I3 = I3;
} }
} }
pTiledReducedPattern_simulationMesh->reset(); pReducedPattern_simulationMesh->reset();
} }
#if POLYSCOPE_DEFINED #if POLYSCOPE_DEFINED