#ifndef REDUCEDMODELOPTIMIZER_HPP #define REDUCEDMODELOPTIMIZER_HPP #include "csvfile.hpp" #include "drmsimulationmodel.hpp" #include "edgemesh.hpp" #include "linearsimulationmodel.hpp" #ifdef POLYSCOPE_DEFINED #include "matplot/matplot.h" #endif #include "reducedmodeloptimizer_structs.hpp" #include "simulationmesh.hpp" #include #ifdef DLIB_DEFINED #include #include #endif #ifdef POLYSCOPE_DEFINED #include "polyscope/color_management.h" #endif // POLYSCOPE_DEFINED using FullPatternVertexIndex = VertexIndex; using ReducedPatternVertexIndex = VertexIndex; class ReducedModelOptimizer { std::shared_ptr m_pReducedPatternSimulationMesh; std::shared_ptr m_pFullPatternSimulationMesh; std::unordered_map m_fullToReducedInterfaceViMap; std::vector> m_fullPatternOppositeInterfaceViPairs; std::unordered_map nodeToSlot; std::unordered_map> slotToNode; std::string optimizationNotes; std::vector> &, SimulationJob &)>> constructBaseScenarioFunctions; std::vector scenarioIsSymmetrical; int fullPatternNumberOfEdges; constexpr static double youngsModulus{1 * 1e9}; public: struct FunctionEvaluation { FunctionEvaluation() = default; FunctionEvaluation(const std::vector &x, double y) : x(x), y(y) {} std::vector x; double y = std::numeric_limits::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 parameterLabels = {"R", "A", "I2", "I3", "J", "Theta", "R"}; constexpr static std::array simulationScenariosResolution = {11, 11, 20, 20, 20}; constexpr static std::array baseScenarioWeights = {1, 1, 100, 50, 50}; // constexpr static std::array 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}; void optimize( const ReducedPatternOptimization::Settings &xRanges, ReducedPatternOptimization::Results &results, const std::vector &simulationScenarios = std::vector( {ReducedPatternOptimization::Axial, ReducedPatternOptimization::Shear, ReducedPatternOptimization::Bending, ReducedPatternOptimization::Dome, ReducedPatternOptimization::Saddle})); double operator()(const Eigen::VectorXd &x, Eigen::VectorXd &) const; ReducedModelOptimizer(const std::vector &numberOfNodesPerSlot); static void computeReducedModelSimulationJob( const SimulationJob &simulationJobOfFullModel, const std::unordered_map &fullToReducedMap, SimulationJob &simulationJobOfReducedModel); SimulationJob getReducedSimulationJob(const SimulationJob &fullModelSimulationJob); void initializePatterns( PatternGeometry &fullPattern, PatternGeometry &reducedPatterm, const std::vector &optimizationParameters); static void runSimulation(const std::string &filename, std::vector &x); static std::vector> createFullPatternSimulationJobs( const std::shared_ptr &pMesh, const std::unordered_map &fullPatternOppositeInterfaceViMap); static void createSimulationMeshes( PatternGeometry &fullModel, PatternGeometry &reducedModel, std::shared_ptr &pFullPatternSimulationMesh, std::shared_ptr &pReducedPatternSimulationMesh); static void computeMaps(const std::unordered_map> &slotToNode, PatternGeometry &fullPattern, PatternGeometry &reducedPattern, std::unordered_map &reducedToFullInterfaceViMap, std::unordered_map &fullToReducedInterfaceViMap, std::vector> &fullPatternOppositeInterfaceViMap); static void visualizeResults( const std::vector> &fullPatternSimulationJobs, const std::vector> &reducedPatternSimulationJobs, const std::vector &simulationScenarios, const std::unordered_map &reducedToFullInterfaceViMap); static void registerResultsForDrawing( const std::shared_ptr &pFullPatternSimulationJob, const std::shared_ptr &pReducedPatternSimulationJob, const std::unordered_map &reducedToFullInterfaceViMap); static double computeRawTranslationalError( const std::vector &fullPatternDisplacements, const std::vector &reducedPatternDisplacements, const std::unordered_map &reducedToFullInterfaceViMap); static double computeDisplacementError( const std::vector &fullPatternDisplacements, const std::vector &reducedPatternDisplacements, const std::unordered_map &reducedToFullInterfaceViMap, const double &normalizationFactor); static double objective(double E, double A, double innerHexagonSize, double innerHexagonRotationAngle); static double computeRawRotationalError( const std::vector &rotatedQuaternion_fullPattern, const std::vector &rotatedQuaternion_reducedPattern, const std::unordered_map &reducedToFullInterfaceViMap); static double computeRotationalError(const std::vector &rotatedQuaternion_fullPattern, const std::vector &rotatedQuaternion_reducedPattern, const std::unordered_map &reducedToFullInterfaceViMap, const double &normalizationFactor); static double computeError( const SimulationResults &simulationResults_fullPattern, const SimulationResults &simulationResults_reducedPattern, const std::unordered_map &reducedToFullInterfaceViMap, const double &normalizationFactor_translationalDisplacement, const double &normalizationFactor_rotationalDisplacement, const double &scenarioWeight); static void constructAxialSimulationScenario( const double &forceMagnitude, const std::vector> &oppositeInterfaceViPairs, SimulationJob &job); static void constructShearSimulationScenario( const double &forceMagnitude, const std::vector> &oppositeInterfaceViPairs, SimulationJob &job); static void constructBendingSimulationScenario( const double &forceMagnitude, const std::vector> &oppositeInterfaceViPairs, SimulationJob &job); static void constructDomeSimulationScenario( const double &forceMagnitude, const std::vector> &oppositeInterfaceViPairs, SimulationJob &job); static void constructSaddleSimulationScenario( const double &forceMagnitude, const std::vector> &oppositeInterfaceViPairs, SimulationJob &job); static std::function &x, std::shared_ptr &pReducedPatternSimulationMesh)> function_updateReducedPattern; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_E; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_A; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_I; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_I2; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_I3; static std::function &pReducedPatternSimulationMesh)> function_updateReducedPattern_material_J; static double objective(const std::vector &x); static void initializeUpdateReducedPatternFunctions(); static double objective(const double &xValue); private: static void computeDesiredReducedModelDisplacements( const SimulationResults &fullModelResults, const std::unordered_map &displacementsReducedToFullMap, Eigen::MatrixX3d &optimalDisplacementsOfReducedModel); static void runOptimization(const ReducedPatternOptimization::Settings &settings, ReducedPatternOptimization::Results &results); void computeMaps(PatternGeometry &fullModel, PatternGeometry &reducedPattern); void createSimulationMeshes(PatternGeometry &fullModel, PatternGeometry &reducedModel); static void initializeOptimizationParameters( const std::shared_ptr &mesh, const std::vector &optimizationParamters); DRMSimulationModel simulator; void computeObjectiveValueNormalizationFactors(); static void getResults(const FunctionEvaluation &optimalObjective, const ReducedPatternOptimization::Settings &settings, ReducedPatternOptimization::Results &results); double computeFullPatternMaxSimulationForce( const ReducedPatternOptimization::BaseSimulationScenario &scenario) const; #ifdef DLIB_DEFINED static double objective(const dlib::matrix &x); #endif std::array getFullPatternMaxSimulationForces( const std::vector &desiredBaseSimulationScenarioIndices, const std::filesystem::path &intermediateResultsDirectoryPath); void computeScenarioWeights(const std::vector &baseSimulationScenarios, const std::array &maxForceMagnitudePerBaseScenario); std::array computeFullPatternMaxSimulationForces( const std::vector &desiredBaseSimulationScenario) const; std::vector> createFullPatternSimulationJobs( const std::shared_ptr &pMesh, const std::array &baseScenarioMaxForceMagnitudes) const; }; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_E; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_A; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_I; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_I2; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_I3; inline std::function &pReducedPatternSimulationMesh)> ReducedModelOptimizer::function_updateReducedPattern_material_J; inline std::function &x, std::shared_ptr &m)> ReducedModelOptimizer::function_updateReducedPattern; #endif // REDUCEDMODELOPTIMIZER_HPP