ReducedModelOptimization/src/reducedmodeloptimizer.hpp

292 lines
15 KiB
C++
Raw Normal View History

2020-11-23 10:06:45 +01:00
#ifndef REDUCEDMODELOPTIMIZER_HPP
#define REDUCEDMODELOPTIMIZER_HPP
2021-02-10 12:19:37 +01:00
#include "csvfile.hpp"
#include "drmsimulationmodel.hpp"
2020-11-23 10:06:45 +01:00
#include "edgemesh.hpp"
#include "linearsimulationmodel.hpp"
2021-12-21 20:24:45 +01:00
#ifdef POLYSCOPE_DEFINED
2020-12-09 16:58:48 +01:00
#include "matplot/matplot.h"
2021-12-21 20:24:45 +01:00
#endif
2021-03-23 18:20:46 +01:00
#include "reducedmodeloptimizer_structs.hpp"
#include "simulationmesh.hpp"
2020-11-23 10:06:45 +01:00
#include <Eigen/Dense>
#ifdef DLIB_DEFINED
#include <dlib/global_optimization.h>
#include <dlib/optimization.h>
#endif
2020-11-23 10:06:45 +01:00
#ifdef POLYSCOPE_DEFINED
#include "polyscope/color_management.h"
#endif // POLYSCOPE_DEFINED
using FullPatternVertexIndex = VertexIndex;
using ReducedPatternVertexIndex = VertexIndex;
2020-11-23 10:06:45 +01:00
class ReducedModelOptimizer
{
std::shared_ptr<SimulationMesh> m_pReducedPatternSimulationMesh;
std::shared_ptr<SimulationMesh> m_pFullPatternSimulationMesh;
std::unordered_map<FullPatternVertexIndex, ReducedPatternVertexIndex>
m_fullToReducedInterfaceViMap;
std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
m_fullPatternOppositeInterfaceViPairs;
std::unordered_map<size_t, size_t> nodeToSlot;
std::unordered_map<size_t, std::unordered_set<size_t>> slotToNode;
std::string optimizationNotes;
2021-07-14 16:47:47 +02:00
std::vector<std::function<
void(const double &,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>> &,
SimulationJob &)>>
constructBaseScenarioFunctions;
std::vector<bool> scenarioIsSymmetrical;
int fullPatternNumberOfEdges;
constexpr static double youngsModulus{1 * 1e9};
2020-11-23 10:06:45 +01:00
public:
struct FunctionEvaluation
{
FunctionEvaluation() = default;
FunctionEvaluation(const std::vector<double> &x, double y) : x(x), y(y) {}
std::vector<double> x;
double y = std::numeric_limits<double>::quiet_NaN();
};
// struct ParameterLabels
// {
// inline const static std::string E = {"E"};
// inline const static std::string A = {"A"};
// inline const static std::string I2 ={"I2"};
// inline const static std::string I3 ={"I3"};
// inline const static std::string J = {"J"};
// inline const static std::string th= {"Theta"};
// inline const static std::string R = {"R"};
// };
// inline constexpr static ParameterLabels parameterLabels();
inline static std::array<std::string, ReducedPatternOptimization::NumberOfOptimizationParameters>
parameterLabels = {"R", "A", "I2", "I3", "J", "Theta", "R"};
2021-12-21 20:24:45 +01:00
constexpr static std::array<int, ReducedPatternOptimization::NumberOfBaseSimulationScenarios>
simulationScenariosResolution = {11, 11, 20, 20, 20};
constexpr static std::array<int, ReducedPatternOptimization::NumberOfBaseSimulationScenarios>
baseScenarioWeights = {1, 1, 100, 50, 50};
2021-10-14 13:58:34 +02:00
// constexpr static std::array<int, 5> simulationScenariosResolution = {3, 3, 3, 3, 3};
inline static int totalNumberOfSimulationScenarios
= std::accumulate(simulationScenariosResolution.begin(),
simulationScenariosResolution.end(),
0);
inline static int fanSize{6};
inline static double initialHexagonSize{0.3};
inline static VectorType patternPlaneNormal{0, 0, 1};
2021-04-30 11:47:50 +02:00
void optimize(
const ReducedPatternOptimization::Settings &xRanges,
2021-04-30 11:47:50 +02:00
ReducedPatternOptimization::Results &results,
const std::vector<ReducedPatternOptimization::BaseSimulationScenario> &simulationScenarios
2021-07-14 16:47:47 +02:00
= std::vector<ReducedPatternOptimization::BaseSimulationScenario>(
{ReducedPatternOptimization::Axial,
ReducedPatternOptimization::Shear,
ReducedPatternOptimization::Bending,
ReducedPatternOptimization::Dome,
ReducedPatternOptimization::Saddle}));
double operator()(const Eigen::VectorXd &x, Eigen::VectorXd &) const;
ReducedModelOptimizer(const std::vector<size_t> &numberOfNodesPerSlot);
static void computeReducedModelSimulationJob(
const SimulationJob &simulationJobOfFullModel,
const std::unordered_map<size_t, size_t> &fullToReducedMap,
SimulationJob &simulationJobOfReducedModel);
SimulationJob getReducedSimulationJob(const SimulationJob &fullModelSimulationJob);
2021-11-09 21:13:15 +01:00
void initializePatterns(
PatternGeometry &fullPattern,
PatternGeometry &reducedPatterm,
const std::vector<ReducedPatternOptimization::xRange> &optimizationParameters);
static void runSimulation(const std::string &filename, std::vector<double> &x);
2021-07-14 16:47:47 +02:00
static std::vector<std::shared_ptr<SimulationJob>> createFullPatternSimulationJobs(
const std::shared_ptr<SimulationMesh> &pMesh,
const std::unordered_map<size_t, size_t> &fullPatternOppositeInterfaceViMap);
static void createSimulationMeshes(
PatternGeometry &fullModel,
PatternGeometry &reducedModel,
std::shared_ptr<SimulationMesh> &pFullPatternSimulationMesh,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh);
static void computeMaps(const std::unordered_map<size_t, std::unordered_set<size_t>> &slotToNode,
PatternGeometry &fullPattern,
PatternGeometry &reducedPattern,
std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap,
std::unordered_map<FullPatternVertexIndex, ReducedPatternVertexIndex>
&fullToReducedInterfaceViMap,
std::vector<std::pair<FullPatternVertexIndex, ReducedPatternVertexIndex>>
&fullPatternOppositeInterfaceViMap);
static void visualizeResults(
const std::vector<std::shared_ptr<SimulationJob>> &fullPatternSimulationJobs,
const std::vector<std::shared_ptr<SimulationJob>> &reducedPatternSimulationJobs,
const std::vector<ReducedPatternOptimization::BaseSimulationScenario> &simulationScenarios,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap);
static void registerResultsForDrawing(
const std::shared_ptr<SimulationJob> &pFullPatternSimulationJob,
const std::shared_ptr<SimulationJob> &pReducedPatternSimulationJob,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap);
static double computeRawTranslationalError(
const std::vector<Vector6d> &fullPatternDisplacements,
const std::vector<Vector6d> &reducedPatternDisplacements,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap);
static double computeDisplacementError(
const std::vector<Vector6d> &fullPatternDisplacements,
const std::vector<Vector6d> &reducedPatternDisplacements,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap,
const double &normalizationFactor);
static double objective(double E,
double A,
double innerHexagonSize,
double innerHexagonRotationAngle);
static double computeRawRotationalError(
2021-12-21 20:24:45 +01:00
const std::vector<Eigen::Quaterniond> &rotatedQuaternion_fullPattern,
const std::vector<Eigen::Quaterniond> &rotatedQuaternion_reducedPattern,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap);
2021-12-21 20:24:45 +01:00
static double computeRotationalError(const std::vector<Eigen::Quaterniond> &rotatedQuaternion_fullPattern,
const std::vector<Eigen::Quaterniond> &rotatedQuaternion_reducedPattern,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap,
const double &normalizationFactor);
static double computeError(
const SimulationResults &simulationResults_fullPattern,
const SimulationResults &simulationResults_reducedPattern,
const std::unordered_map<ReducedPatternVertexIndex, FullPatternVertexIndex>
&reducedToFullInterfaceViMap,
const double &normalizationFactor_translationalDisplacement,
2021-12-21 20:24:45 +01:00
const double &normalizationFactor_rotationalDisplacement,
const double &scenarioWeight);
static void constructAxialSimulationScenario(
const double &forceMagnitude,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
&oppositeInterfaceViPairs,
SimulationJob &job);
static void constructShearSimulationScenario(
const double &forceMagnitude,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
&oppositeInterfaceViPairs,
SimulationJob &job);
static void constructBendingSimulationScenario(
const double &forceMagnitude,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
&oppositeInterfaceViPairs,
SimulationJob &job);
static void constructDomeSimulationScenario(
const double &forceMagnitude,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
&oppositeInterfaceViPairs,
SimulationJob &job);
static void constructSaddleSimulationScenario(
const double &forceMagnitude,
const std::vector<std::pair<FullPatternVertexIndex, FullPatternVertexIndex>>
&oppositeInterfaceViPairs,
SimulationJob &job);
static std::function<void(const std::vector<double> &x,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
function_updateReducedPattern;
static std::function<void(const double &newE,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
function_updateReducedPattern_material_E;
static std::function<void(const double &newA,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
function_updateReducedPattern_material_A;
static std::function<void(const double &newI,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
function_updateReducedPattern_material_I;
static std::function<void(const double &newI2,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
function_updateReducedPattern_material_I2;
static std::function<void(const double &newI3,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
function_updateReducedPattern_material_I3;
static std::function<void(const double &newJ,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
function_updateReducedPattern_material_J;
static double objective(const std::vector<double> &x);
static void initializeUpdateReducedPatternFunctions();
static double objective(const double &xValue);
private:
static void computeDesiredReducedModelDisplacements(
const SimulationResults &fullModelResults,
const std::unordered_map<size_t, size_t> &displacementsReducedToFullMap,
Eigen::MatrixX3d &optimalDisplacementsOfReducedModel);
2021-04-30 11:47:50 +02:00
static void runOptimization(const ReducedPatternOptimization::Settings &settings,
ReducedPatternOptimization::Results &results);
void computeMaps(PatternGeometry &fullModel, PatternGeometry &reducedPattern);
void createSimulationMeshes(PatternGeometry &fullModel, PatternGeometry &reducedModel);
2021-11-09 21:13:15 +01:00
static void initializeOptimizationParameters(
const std::shared_ptr<SimulationMesh> &mesh,
const std::vector<ReducedPatternOptimization::xRange> &optimizationParamters);
DRMSimulationModel simulator;
void computeObjectiveValueNormalizationFactors();
static void getResults(const FunctionEvaluation &optimalObjective,
2021-04-30 11:47:50 +02:00
const ReducedPatternOptimization::Settings &settings,
ReducedPatternOptimization::Results &results);
2021-07-14 16:47:47 +02:00
double computeFullPatternMaxSimulationForce(
2021-12-21 20:24:45 +01:00
const ReducedPatternOptimization::BaseSimulationScenario &scenario) const;
#ifdef DLIB_DEFINED
static double objective(const dlib::matrix<double, 0, 1> &x);
#endif
2021-12-21 20:24:45 +01:00
std::array<double, ReducedPatternOptimization::NumberOfBaseSimulationScenarios>
getFullPatternMaxSimulationForces(
const std::vector<ReducedPatternOptimization::BaseSimulationScenario>
&desiredBaseSimulationScenarioIndices,
const std::filesystem::path &intermediateResultsDirectoryPath);
void computeScenarioWeights(const std::vector<ReducedPatternOptimization::BaseSimulationScenario>
&baseSimulationScenarios,
const std::array<double, 5> &maxForceMagnitudePerBaseScenario);
std::array<double, ReducedPatternOptimization::NumberOfBaseSimulationScenarios>
computeFullPatternMaxSimulationForces(
const std::vector<ReducedPatternOptimization::BaseSimulationScenario>
&desiredBaseSimulationScenario) const;
std::vector<std::shared_ptr<SimulationJob>> createFullPatternSimulationJobs(
const std::shared_ptr<SimulationMesh> &pMesh,
const std::array<double, ReducedPatternOptimization::NumberOfBaseSimulationScenarios>
&baseScenarioMaxForceMagnitudes) const;
2020-11-23 10:06:45 +01:00
};
inline std::function<void(const double &newE,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_E;
inline std::function<void(const double &newA,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_A;
inline std::function<void(const double &newI,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_I;
inline std::function<void(const double &newI2,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_I2;
inline std::function<void(const double &newI3,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_I3;
inline std::function<void(const double &newJ,
std::shared_ptr<SimulationMesh> &pReducedPatternSimulationMesh)>
2021-11-09 21:13:15 +01:00
ReducedModelOptimizer::function_updateReducedPattern_material_J;
inline std::function<void(const std::vector<double> &x, std::shared_ptr<SimulationMesh> &m)>
ReducedModelOptimizer::function_updateReducedPattern;
2020-11-23 10:06:45 +01:00
#endif // REDUCEDMODELOPTIMIZER_HPP