From c9fc6ccd0877f92308795b99e51c8e5dfd1e31eb Mon Sep 17 00:00:00 2001 From: iasonmanolas Date: Thu, 23 Dec 2021 15:51:23 +0200 Subject: [PATCH] Refactoring --- CMakeLists.txt | 48 ++--- drmsimulationmodel.cpp | 5 +- reducedmodeloptimizer_structs.hpp | 263 ++++++++++++------------ simulation_structs.hpp | 319 ++++++++++++++++-------------- simulationmesh.cpp | 10 +- utilities.hpp | 85 +++++++- 6 files changed, 417 insertions(+), 313 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f51fc45..5b7c38d 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,28 +15,19 @@ if(NOT EXISTS ${EXTERNAL_DEPS_DIR}) endif() ##Create directory for the external libraries file(MAKE_DIRECTORY ${EXTERNAL_DEPS_DIR}) -#message(${POLYSCOPE_ALREADY_COMPILED}) if(${USE_POLYSCOPE}) -download_project(PROJ POLYSCOPE - GIT_REPOSITORY https://github.com/nmwsharp/polyscope.git - GIT_TAG master - PREFIX ${EXTERNAL_DEPS_DIR} - ${UPDATE_DISCONNECTED_IF_AVAILABLE} -) -add_subdirectory(${POLYSCOPE_SOURCE_DIR} ${POLYSCOPE_BINARY_DIR}) -add_compile_definitions(POLYSCOPE_DEFINED) + set(POLYSCOPE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/polyscope) + download_project(PROJ POLYSCOPE + GIT_REPOSITORY https://github.com/nmwsharp/polyscope.git + GIT_TAG master + PREFIX ${EXTERNAL_DEPS_DIR} + BINARY_DIR ${POLYSCOPE_BINARY_DIR} + ${UPDATE_DISCONNECTED_IF_AVAILABLE} + ) + add_subdirectory(${POLYSCOPE_SOURCE_DIR} ${POLYSCOPE_BINARY_DIR}) + add_compile_definitions(POLYSCOPE_DEFINED) endif() -#dlib -set(DLIB_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/dlib_bin) -file(MAKE_DIRECTORY ${DLIB_BIN_DIR}) -download_project(PROJ DLIB - GIT_REPOSITORY https://github.com/davisking/dlib.git - GIT_TAG master - BINARY_DIR ${DLIB_BIN_DIR} - PREFIX ${EXTERNAL_DEPS_DIR} - ${UPDATE_DISCONNECTED_IF_AVAILABLE} -) -add_subdirectory(${DLIB_SOURCE_DIR} ${DLIB_BINARY_DIR}) + ##vcglib devel branch download_project(PROJ vcglib_devel GIT_REPOSITORY https://github.com/IasonManolas/vcglib.git @@ -45,27 +36,36 @@ download_project(PROJ vcglib_devel ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) add_subdirectory(${vcglib_devel_SOURCE_DIR} ${vcglib_devel_BINARY_DIR}) + ##matplot++ lib +set(MATPLOTPLUSPLUS_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/matplot) download_project(PROJ MATPLOTPLUSPLUS GIT_REPOSITORY https://github.com/alandefreitas/matplotplusplus GIT_TAG master + BINARY_DIR ${MATPLOTPLUSPLUS_BINARY_DIR} PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) add_subdirectory(${MATPLOTPLUSPLUS_SOURCE_DIR} ${MATPLOTPLUSPLUS_BINARY_DIR}) + ##threed-beam-fea +set(threed-beam-fea_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/threed-beam-fea) download_project(PROJ threed-beam-fea GIT_REPOSITORY https://github.com/IasonManolas/threed-beam-fea.git GIT_TAG master + BINARY_DIR ${threed-beam-fea_BINARY_DIR} PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) add_subdirectory(${threed-beam-fea_SOURCE_DIR} ${threed-beam-fea_BINARY_DIR}) + ##TBB +set(TBB_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/tbb) download_project(PROJ TBB GIT_REPOSITORY https://github.com/wjakob/tbb.git GIT_TAG master PREFIX ${EXTERNAL_DEPS_DIR} + BINARY_DIR ${TBB_BINARY_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) option(TBB_BUILD_TESTS "Build TBB tests and enable testing infrastructure" OFF) @@ -83,13 +83,12 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/boost_graph PUBLIC ${vcglib_devel_SOURCE_DIR} PUBLIC ${threed-beam-fea_SOURCE_DIR} -# PUBLIC ${MySourcesFiles} ) if(${MYSOURCES_STATIC_LINK}) message("Linking statically") - target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen matplot dlib::dlib ThreedBeamFEA ${TBB_BINARY_DIR}/libtbb_static.a pthread gfortran quadmath) + target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen matplot ThreedBeamFEA ${TBB_BINARY_DIR}/libtbb_static.a pthread gfortran quadmath) else() - target_link_libraries(${PROJECT_NAME} Eigen3::Eigen matplot dlib::dlib ThreedBeamFEA tbb pthread) + target_link_libraries(${PROJECT_NAME} Eigen3::Eigen matplot ThreedBeamFEA tbb pthread) if(${USE_POLYSCOPE}) target_link_libraries(${PROJECT_NAME} polyscope) endif() @@ -98,9 +97,11 @@ target_link_directories(MySources PUBLIC ${CMAKE_CURRENT_LIST_DIR}/boost_graph/l if(USE_ENSMALLEN) ##ENSMALLEN + set(ENSMALLEN_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/ensmallen) download_project(PROJ ENSMALLEN GIT_REPOSITORY https://github.com/mlpack/ensmallen.git GIT_TAG master + BINARY_DIR ${ENSMALLEN_BINARY_DIR} PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) @@ -110,7 +111,6 @@ if(USE_ENSMALLEN) if(${MYSOURCES_STATIC_LINK}) target_link_libraries(${PROJECT_NAME} PUBLIC "-static" ensmallen ${EXTERNAL_DEPS_DIR}/armadillo_build/libarmadillo.a) else() - target_link_libraries(${PROJECT_NAME} PUBLIC) target_link_libraries(${PROJECT_NAME} PUBLIC armadillo ensmallen) endif() target_include_directories(${PROJECT_NAME} PUBLIC ${ARMADILLO_SOURCE_DIR}/include) diff --git a/drmsimulationmodel.cpp b/drmsimulationmodel.cpp index 6dde27e..4326d6e 100755 --- a/drmsimulationmodel.cpp +++ b/drmsimulationmodel.cpp @@ -1833,10 +1833,13 @@ SimulationResults DRMSimulationModel::computeResults(const std::shared_ptr parameterRanges; - inline static vector normalizationStrategyStrings{"NonNormalized", "Epsilon"}; - int numberOfFunctionCalls{100000}; - double solverAccuracy{1e-2}; - NormalizationStrategy normalizationStrategy{Epsilon}; - double translationNormalizationParameter{0.003}; - double rotationNormalizationParameter{3}; - bool splitGeometryMaterialOptimization{false}; - struct ObjectiveWeights - { - double translational{1}; - double rotational{1}; - } objectiveWeights; +enum OptimizationParameterIndex { E, A, I2, I3, J, R, Theta, NumberOfOptimizationParameters }; - struct JsonKeys - { - inline static std::string filename{"OptimizationSettings.json"}; - inline static std::string OptimizationVariables{"OptimizationVariables"}; - inline static std::string NumberOfFunctionCalls{"NumberOfFunctionCalls"}; - inline static std::string SolverAccuracy{"SolverAccuracy"}; - inline static std::string TranslationalObjectiveWeight{"TransObjWeight"}; - }; +struct Settings +{ + std::filesystem::path intermediateResultsDirectoryPath; + std::vector> optimizationVariables; + enum NormalizationStrategy { NonNormalized, Epsilon }; + std::vector parameterRanges; + inline static vector normalizationStrategyStrings{"NonNormalized", "Epsilon"}; + int numberOfFunctionCalls{100000}; + double solverAccuracy{1e-2}; + NormalizationStrategy normalizationStrategy{Epsilon}; + double translationNormalizationParameter{0.003}; + double rotationNormalizationParameter{3}; + struct ObjectiveWeights + { + double translational{1}; + double rotational{1}; + } objectiveWeights; - void save(const std::filesystem::path &saveToPath) - { - assert(std::filesystem::is_directory(saveToPath)); + struct JsonKeys + { + inline static std::string filename{"OptimizationSettings.json"}; + inline static std::string OptimizationVariables{"OptimizationVariables"}; + inline static std::string NumberOfFunctionCalls{"NumberOfFunctionCalls"}; + inline static std::string SolverAccuracy{"SolverAccuracy"}; + inline static std::string TranslationalObjectiveWeight{"TransObjWeight"}; + inline static std::string OptimizationParameters{"OptimizationParameters"}; + }; - nlohmann::json json; - //write x ranges - for (size_t xRangeIndex = 0; xRangeIndex < parameterRanges.size(); xRangeIndex++) { - const auto &xRange = parameterRanges[xRangeIndex]; - json[JsonKeys::OptimizationVariables + "_" + std::to_string(xRangeIndex)] - = xRange.toString(); - } + void save(const std::filesystem::path &saveToPath) + { + assert(std::filesystem::is_directory(saveToPath)); - json[JsonKeys::NumberOfFunctionCalls] = numberOfFunctionCalls; - json[JsonKeys::SolverAccuracy] = solverAccuracy; - json[JsonKeys::TranslationalObjectiveWeight] = objectiveWeights.translational; + nlohmann::json json; + //write x ranges + for (size_t xRangeIndex = 0; xRangeIndex < parameterRanges.size(); xRangeIndex++) { + const auto &xRange = parameterRanges[xRangeIndex]; + json[JsonKeys::OptimizationVariables + "_" + std::to_string(xRangeIndex)] + = xRange.toString(); + } - std::filesystem::path jsonFilePath( - std::filesystem::path(saveToPath).append(JsonKeys::filename)); - std::ofstream jsonFile(jsonFilePath.string()); - jsonFile << json; - } + json[JsonKeys::NumberOfFunctionCalls] = numberOfFunctionCalls; + json[JsonKeys::SolverAccuracy] = solverAccuracy; + json[JsonKeys::TranslationalObjectiveWeight] = objectiveWeights.translational; + json[JsonKeys::OptimizationParameters] = optimizationVariables; - bool load(const std::filesystem::path &loadFromPath) - { - assert(std::filesystem::is_directory(loadFromPath)); - //Load optimal X - nlohmann::json json; - std::filesystem::path jsonFilepath( - std::filesystem::path(loadFromPath).append(JsonKeys::filename)); - if (!std::filesystem::exists(jsonFilepath)) { - return false; - } - std::ifstream ifs(jsonFilepath); - ifs >> json; + std::filesystem::path jsonFilePath( + std::filesystem::path(saveToPath).append(JsonKeys::filename)); + std::ofstream jsonFile(jsonFilePath.string()); + jsonFile << json; + } - //read x ranges - size_t xRangeIndex = 0; - while (true) { - const std::string jsonXRangeKey = JsonKeys::OptimizationVariables + "_" - + std::to_string(xRangeIndex); - if (!json.contains(jsonXRangeKey)) { - break; - } - xRange x; - x.fromString(json.at(jsonXRangeKey)); - parameterRanges.push_back(x); - xRangeIndex++; - } + bool load(const std::filesystem::path &loadFromPath) + { + assert(std::filesystem::is_directory(loadFromPath)); + //Load optimal X + nlohmann::json json; + std::filesystem::path jsonFilepath( + std::filesystem::path(loadFromPath).append(JsonKeys::filename)); + if (!std::filesystem::exists(jsonFilepath)) { + return false; + } + std::ifstream ifs(jsonFilepath); + ifs >> json; - numberOfFunctionCalls = json.at(JsonKeys::NumberOfFunctionCalls); - solverAccuracy = json.at(JsonKeys::SolverAccuracy); - objectiveWeights.translational = json.at(JsonKeys::TranslationalObjectiveWeight); - objectiveWeights.rotational = 2 - objectiveWeights.translational; - return true; - } + //read x ranges + size_t xRangeIndex = 0; + while (true) { + const std::string jsonXRangeKey = JsonKeys::OptimizationVariables + "_" + + std::to_string(xRangeIndex); + if (!json.contains(jsonXRangeKey)) { + break; + } + xRange x; + x.fromString(json.at(jsonXRangeKey)); + parameterRanges.push_back(x); + xRangeIndex++; + } - std::string toString() const - { - std::string settingsString; - if (!parameterRanges.empty()) { - std::string xRangesString; - for (const xRange &range : parameterRanges) { - xRangesString += range.toString() + " "; - } - settingsString += xRangesString; - } - settingsString += "FuncCalls=" + std::to_string(numberOfFunctionCalls) - + " Accuracy=" + std::to_string(solverAccuracy) - + " TransWeight=" + std::to_string(objectiveWeights.translational) - + " RotWeight=" + std::to_string(objectiveWeights.rotational); + optimizationVariables + = std::vector>( + (json.at(JsonKeys::OptimizationParameters))); + numberOfFunctionCalls = json.at(JsonKeys::NumberOfFunctionCalls); + solverAccuracy = json.at(JsonKeys::SolverAccuracy); + objectiveWeights.translational = json.at(JsonKeys::TranslationalObjectiveWeight); + objectiveWeights.rotational = 2 - objectiveWeights.translational; + return true; + } - return settingsString; - } + std::string toString() const + { + std::string settingsString; + if (!parameterRanges.empty()) { + std::string xRangesString; + for (const xRange &range : parameterRanges) { + xRangesString += range.toString() + " "; + } + settingsString += xRangesString; + } + settingsString += "FuncCalls=" + std::to_string(numberOfFunctionCalls) + + " Accuracy=" + std::to_string(solverAccuracy) + + " TransWeight=" + std::to_string(objectiveWeights.translational) + + " RotWeight=" + std::to_string(objectiveWeights.rotational); - void writeHeaderTo(csvFile &os) const - { - if (!parameterRanges.empty()) { - for (const xRange &range : parameterRanges) { - os << range.label + " max"; - os << range.label + " min"; - } - } - os << "Function Calls"; - os << "Solution Accuracy"; - os << "Normalization strategy"; - os << "Trans weight"; - os << "Rot weight"; - os << "Splitted geo from mat opt"; - // os << std::endl; - } + return settingsString; + } - void writeSettingsTo(csvFile &os) const - { - if (!parameterRanges.empty()) { - for (const xRange &range : parameterRanges) { - os << range.max; - os << range.min; - } - } - os << numberOfFunctionCalls; - os << solverAccuracy; - os << normalizationStrategyStrings[normalizationStrategy] + "_" - + std::to_string(translationNormalizationParameter); - os << objectiveWeights.translational; - os << objectiveWeights.rotational; - os << (splitGeometryMaterialOptimization == true ? "true" : "false"); - } - }; + void writeHeaderTo(csvFile &os) const + { + if (!parameterRanges.empty()) { + for (const xRange &range : parameterRanges) { + os << range.label + " max"; + os << range.label + " min"; + } + } + os << "Function Calls"; + os << "Solution Accuracy"; + os << "Normalization strategy"; + os << "Trans weight"; + os << "Rot weight"; + os << "Optimization parameters"; + // os << std::endl; + } + + void writeSettingsTo(csvFile &os) const + { + if (!parameterRanges.empty()) { + for (const xRange &range : parameterRanges) { + os << range.max; + os << range.min; + } + } + os << numberOfFunctionCalls; + os << solverAccuracy; + os << normalizationStrategyStrings[normalizationStrategy] + "_" + + std::to_string(translationNormalizationParameter); + os << objectiveWeights.translational; + os << objectiveWeights.rotational; + + //export optimization parameters + std::vector> vv; + for (const std::vector &v : optimizationVariables) { + std::vector vi; + vi.reserve(v.size()); + for (const OptimizationParameterIndex ¶meter : v) { + vi.emplace_back(parameter); + } + vv.push_back(vi); + } + os << Utilities::toString(vv); + } +}; inline bool operator==(const Settings &settings1, const Settings &settings2) noexcept { diff --git a/simulation_structs.hpp b/simulation_structs.hpp index b6e2cbe..fcc2a4e 100755 --- a/simulation_structs.hpp +++ b/simulation_structs.hpp @@ -539,7 +539,7 @@ struct SimulationResults const std::filesystem::path outputFolderPath = outputFolder.empty() ? std::filesystem::current_path() : std::filesystem::path(outputFolder); - std::cout << "Saving results to:" << outputFolderPath << std::endl; + // std::cout << "Saving results to:" << outputFolderPath << std::endl; std::filesystem::path simulationJobOutputFolderPath = std::filesystem::path(outputFolderPath).append("SimulationJob"); std::filesystem::create_directories(simulationJobOutputFolderPath); @@ -569,8 +569,173 @@ struct SimulationResults void load(const std::filesystem::path &loadFromPath, const std::filesystem::path &loadJobFrom) { - //load job pJob->load(std::filesystem::path(loadJobFrom).append("SimulationJob.json").string()); + load(loadFromPath); + } + void load(const std::filesystem::path &loadFromPath, const std::shared_ptr &pJob) + { + this->pJob = pJob; + load(loadFromPath); + } +#ifdef POLYSCOPE_DEFINED + void unregister() const + { + if (!polyscope::hasCurveNetwork(getLabel())) { + std::cerr << "No curve network registered with a name: " << getLabel() << std::endl; + std::cerr << "Nothing to remove." << std::endl; + return; + } + pJob->unregister(getLabel()); + polyscope::removeCurveNetwork(getLabel()); + } + polyscope::CurveNetwork *registerForDrawing( + const std::optional> &desiredColor = std::nullopt, + const bool &shouldEnable = true, + const double &desiredRadius = 0.001, + // const double &desiredRadius = 0.0001, + const bool &shouldShowFrames = false) const + { + PolyscopeInterface::init(); + const std::shared_ptr &mesh = pJob->pMesh; + polyscope::CurveNetwork *polyscopeHandle_deformedEdmeMesh; + if (!polyscope::hasStructure(getLabel())) { + const auto verts = mesh->getEigenVertices(); + const auto edges = mesh->getEigenEdges(); + polyscopeHandle_deformedEdmeMesh = polyscope::registerCurveNetwork(getLabel(), + verts, + edges); + } else { + polyscopeHandle_deformedEdmeMesh = polyscope::getCurveNetwork(getLabel()); + } + polyscopeHandle_deformedEdmeMesh->setEnabled(shouldEnable); + polyscopeHandle_deformedEdmeMesh->setRadius(desiredRadius, true); + if (desiredColor.has_value()) { + const glm::vec3 desiredColor_glm(desiredColor.value()[0], + desiredColor.value()[1], + desiredColor.value()[2]); + polyscopeHandle_deformedEdmeMesh->setColor(desiredColor_glm); + } + Eigen::MatrixX3d nodalDisplacements(mesh->VN(), 3); + Eigen::MatrixX3d framesX(mesh->VN(), 3); + Eigen::MatrixX3d framesY(mesh->VN(), 3); + Eigen::MatrixX3d framesZ(mesh->VN(), 3); + + Eigen::MatrixX3d framesX_initial(mesh->VN(), 3); + Eigen::MatrixX3d framesY_initial(mesh->VN(), 3); + Eigen::MatrixX3d framesZ_initial(mesh->VN(), 3); + + // std::unordered_set interfaceNodes{1, 3, 5, 7, 9, 11}; + // std::unordered_set interfaceNodes{3, 7, 11, 15, 19, 23}; + // std::unordered_set interfaceNodes{}; + for (VertexIndex vi = 0; vi < mesh->VN(); vi++) { + const Vector6d &nodalDisplacement = displacements[vi]; + nodalDisplacements.row(vi) = Eigen::Vector3d(nodalDisplacement[0], + nodalDisplacement[1], + nodalDisplacement[2]); + // Eigen::Quaternion Rx(Eigen::AngleAxis(nodalDisplacement[2],Eigen::Vector3d(1, 0, 0))); + // Eigen::Quaternion Ry(Eigen::AngleAxis(nodalDisplacement[4],Eigen::Vector3d(0, 1, 0))); + // Eigen::Quaternion Rz(Eigen::AngleAxis(nodalDisplacement[5],Eigen::Vector3d(0, 0, 1))); + // Eigen::Quaternion R=Rx*Ry*Rz; + // if (interfaceNodes.contains(vi)) { + auto deformedNormal = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(0, 0, 1); + auto deformedFrameY = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(0, 1, 0); + auto deformedFrameX = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(1, 0, 0); + framesX.row(vi) = Eigen::Vector3d(deformedFrameX[0], + deformedFrameX[1], + deformedFrameX[2]); + framesY.row(vi) = Eigen::Vector3d(deformedFrameY[0], + deformedFrameY[1], + deformedFrameY[2]); + framesZ.row(vi) = Eigen::Vector3d(deformedNormal[0], + deformedNormal[1], + deformedNormal[2]); + framesX_initial.row(vi) = Eigen::Vector3d(1, 0, 0); + framesY_initial.row(vi) = Eigen::Vector3d(0, 1, 0); + framesZ_initial.row(vi) = Eigen::Vector3d(0, 0, 1); + // } else { + // framesX.row(vi) = Eigen::Vector3d(0, 0, 0); + // framesY.row(vi) = Eigen::Vector3d(0, 0, 0); + // framesZ.row(vi) = Eigen::Vector3d(0, 0, 0); + // framesX_initial.row(vi) = Eigen::Vector3d(0, 0, 0); + // framesY_initial.row(vi) = Eigen::Vector3d(0, 0, 0); + // framesZ_initial.row(vi) = Eigen::Vector3d(0, 0, 0); + // } + } + polyscopeHandle_deformedEdmeMesh->updateNodePositions(mesh->getEigenVertices() + + nodalDisplacements); + + const double frameRadius_default = 0.035; + const double frameLength_default = 0.035; + const bool shouldEnable_default = true; + //if(showFramesOn.contains(vi)){ + auto polyscopeHandle_frameX = polyscopeHandle_deformedEdmeMesh + ->addNodeVectorQuantity("FrameX", framesX); + polyscopeHandle_frameX->setVectorLengthScale(frameLength_default); + polyscopeHandle_frameX->setVectorRadius(frameRadius_default); + polyscopeHandle_frameX->setVectorColor( + /*polyscope::getNextUniqueColor()*/ glm::vec3(1, 0, 0)); + auto polyscopeHandle_frameY = polyscopeHandle_deformedEdmeMesh + ->addNodeVectorQuantity("FrameY", framesY); + polyscopeHandle_frameY->setVectorLengthScale(frameLength_default); + polyscopeHandle_frameY->setVectorRadius(frameRadius_default); + polyscopeHandle_frameY->setVectorColor( + /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 1, 0)); + auto polyscopeHandle_frameZ = polyscopeHandle_deformedEdmeMesh + ->addNodeVectorQuantity("FrameZ", framesZ); + polyscopeHandle_frameZ->setVectorLengthScale(frameLength_default); + polyscopeHandle_frameZ->setVectorRadius(frameRadius_default); + polyscopeHandle_frameZ->setVectorColor( + /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 0, 1)); + + auto polyscopeHandle_initialMesh = polyscope::getCurveNetwork(mesh->getLabel()); + if (!polyscopeHandle_initialMesh) { + polyscopeHandle_initialMesh = mesh->registerForDrawing(); + } + + // auto polyscopeHandle_frameX_initial = polyscopeHandle_initialMesh + // ->addNodeVectorQuantity("FrameX", framesX_initial); + // polyscopeHandle_frameX_initial->setVectorLengthScale(frameLength_default); + // polyscopeHandle_frameX_initial->setVectorRadius(frameRadius_default); + // polyscopeHandle_frameX_initial->setVectorColor( + // /*polyscope::getNextUniqueColor()*/ glm::vec3(1, 0, 0)); + // auto polyscopeHandle_frameY_initial = polyscopeHandle_initialMesh + // ->addNodeVectorQuantity("FrameY", framesY_initial); + // polyscopeHandle_frameY_initial->setVectorLengthScale(frameLength_default); + // polyscopeHandle_frameY_initial->setVectorRadius(frameRadius_default); + // polyscopeHandle_frameY_initial->setVectorColor( + // /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 1, 0)); + // auto polyscopeHandle_frameZ_initial = polyscopeHandle_initialMesh + // ->addNodeVectorQuantity("FrameZ", framesZ_initial); + // polyscopeHandle_frameZ_initial->setVectorLengthScale(frameLength_default); + // polyscopeHandle_frameZ_initial->setVectorRadius(frameRadius_default); + // polyscopeHandle_frameZ_initial->setVectorColor( + // /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 0, 1)); + // //} + pJob->registerForDrawing(getLabel()); + static bool wasExecuted = false; + if (!wasExecuted) { + std::function callback = [&]() { + static bool showFrames = shouldShowFrames; + + if (ImGui::Checkbox("Show Frames", &showFrames) && showFrames) { + polyscopeHandle_frameX->setEnabled(showFrames); + polyscopeHandle_frameY->setEnabled(showFrames); + polyscopeHandle_frameZ->setEnabled(showFrames); + } + }; + + PolyscopeInterface::addUserCallback(callback); + wasExecuted = true; + } + return polyscopeHandle_deformedEdmeMesh; + } +#endif +private: + void load(const std::filesystem::path &loadFromPath) + { + converged = true; //assuming it has converged + assert(pJob != nullptr); + //load job //Use the first .eigenBin file for loading the displacements for (auto const &entry : std::filesystem::recursive_directory_iterator(loadFromPath)) { if (filesystem::is_regular_file(entry) && entry.path().extension() == ".eigenBin") { @@ -591,156 +756,6 @@ struct SimulationResults Eigen::Vector3d::UnitZ()); } } - -#ifdef POLYSCOPE_DEFINED - void unregister() const { - if (!polyscope::hasCurveNetwork(getLabel())) { - std::cerr << "No curve network registered with a name: " << getLabel() - << std::endl; - std::cerr << "Nothing to remove." << std::endl; - return; - } - pJob->unregister(getLabel()); - polyscope::removeCurveNetwork(getLabel()); - } - polyscope::CurveNetwork *registerForDrawing( - const std::optional> &desiredColor = std::nullopt, - const bool &shouldEnable = true, - const double &desiredRadius = 0.001, - // const double &desiredRadius = 0.0001, - const bool &shouldShowFrames = false) const - { - PolyscopeInterface::init(); - const std::shared_ptr &mesh = pJob->pMesh; - polyscope::CurveNetwork *polyscopeHandle_deformedEdmeMesh; - if (!polyscope::hasStructure(getLabel())) { - const auto verts = mesh->getEigenVertices(); - const auto edges = mesh->getEigenEdges(); - polyscopeHandle_deformedEdmeMesh = polyscope::registerCurveNetwork(getLabel(), - verts, - edges); - } else { - polyscopeHandle_deformedEdmeMesh = polyscope::getCurveNetwork(getLabel()); - } - polyscopeHandle_deformedEdmeMesh->setEnabled(shouldEnable); - polyscopeHandle_deformedEdmeMesh->setRadius(desiredRadius, true); - if (desiredColor.has_value()) { - const glm::vec3 desiredColor_glm(desiredColor.value()[0], - desiredColor.value()[1], - desiredColor.value()[2]); - polyscopeHandle_deformedEdmeMesh->setColor(desiredColor_glm); - } - Eigen::MatrixX3d nodalDisplacements(mesh->VN(), 3); - Eigen::MatrixX3d framesX(mesh->VN(), 3); - Eigen::MatrixX3d framesY(mesh->VN(), 3); - Eigen::MatrixX3d framesZ(mesh->VN(), 3); - - Eigen::MatrixX3d framesX_initial(mesh->VN(), 3); - Eigen::MatrixX3d framesY_initial(mesh->VN(), 3); - Eigen::MatrixX3d framesZ_initial(mesh->VN(), 3); - - // std::unordered_set interfaceNodes{1, 3, 5, 7, 9, 11}; - // std::unordered_set interfaceNodes{3, 7, 11, 15, 19, 23}; - std::unordered_set interfaceNodes{}; - for (VertexIndex vi = 0; vi < mesh->VN(); vi++) { - const Vector6d &nodalDisplacement = displacements[vi]; - nodalDisplacements.row(vi) = Eigen::Vector3d(nodalDisplacement[0], - nodalDisplacement[1], - nodalDisplacement[2]); - if (interfaceNodes.contains(vi)) { - auto deformedNormal = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(0, 0, 1); - auto deformedFrameY = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(0, 1, 0); - auto deformedFrameX = rotationalDisplacementQuaternion[vi] * Eigen::Vector3d(1, 0, 0); - framesX.row(vi) = Eigen::Vector3d(deformedFrameX[0], - deformedFrameX[1], - deformedFrameX[2]); - framesY.row(vi) = Eigen::Vector3d(deformedFrameY[0], - deformedFrameY[1], - deformedFrameY[2]); - framesZ.row(vi) = Eigen::Vector3d(deformedNormal[0], - deformedNormal[1], - deformedNormal[2]); - framesX_initial.row(vi) = Eigen::Vector3d(1, 0, 0); - framesY_initial.row(vi) = Eigen::Vector3d(0, 1, 0); - framesZ_initial.row(vi) = Eigen::Vector3d(0, 0, 1); - } else { - framesX.row(vi) = Eigen::Vector3d(0, 0, 0); - framesY.row(vi) = Eigen::Vector3d(0, 0, 0); - framesZ.row(vi) = Eigen::Vector3d(0, 0, 0); - framesX_initial.row(vi) = Eigen::Vector3d(0, 0, 0); - framesY_initial.row(vi) = Eigen::Vector3d(0, 0, 0); - framesZ_initial.row(vi) = Eigen::Vector3d(0, 0, 0); - } - } - polyscopeHandle_deformedEdmeMesh->updateNodePositions(mesh->getEigenVertices() - + nodalDisplacements); - - const double frameRadius_default = 0.035; - const double frameLength_default = 0.035; - const bool shouldEnable_default = true; - //if(showFramesOn.contains(vi)){ - auto polyscopeHandle_frameX = polyscopeHandle_deformedEdmeMesh - ->addNodeVectorQuantity("FrameX", framesX); - polyscopeHandle_frameX->setVectorLengthScale(frameLength_default); - polyscopeHandle_frameX->setVectorRadius(frameRadius_default); - polyscopeHandle_frameX->setVectorColor( - /*polyscope::getNextUniqueColor()*/ glm::vec3(1, 0, 0)); - auto polyscopeHandle_frameY = polyscopeHandle_deformedEdmeMesh - ->addNodeVectorQuantity("FrameY", framesY); - polyscopeHandle_frameY->setVectorLengthScale(frameLength_default); - polyscopeHandle_frameY->setVectorRadius(frameRadius_default); - polyscopeHandle_frameY->setVectorColor( - /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 1, 0)); - auto polyscopeHandle_frameZ = polyscopeHandle_deformedEdmeMesh - ->addNodeVectorQuantity("FrameZ", framesZ); - polyscopeHandle_frameZ->setVectorLengthScale(frameLength_default); - polyscopeHandle_frameZ->setVectorRadius(frameRadius_default); - polyscopeHandle_frameZ->setVectorColor( - /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 0, 1)); - - auto polyscopeHandle_initialMesh = polyscope::getCurveNetwork(mesh->getLabel()); - if (!polyscopeHandle_initialMesh) { - polyscopeHandle_initialMesh = mesh->registerForDrawing(); - } - - // auto polyscopeHandle_frameX_initial = polyscopeHandle_initialMesh - // ->addNodeVectorQuantity("FrameX", framesX_initial); - // polyscopeHandle_frameX_initial->setVectorLengthScale(frameLength_default); - // polyscopeHandle_frameX_initial->setVectorRadius(frameRadius_default); - // polyscopeHandle_frameX_initial->setVectorColor( - // /*polyscope::getNextUniqueColor()*/ glm::vec3(1, 0, 0)); - // auto polyscopeHandle_frameY_initial = polyscopeHandle_initialMesh - // ->addNodeVectorQuantity("FrameY", framesY_initial); - // polyscopeHandle_frameY_initial->setVectorLengthScale(frameLength_default); - // polyscopeHandle_frameY_initial->setVectorRadius(frameRadius_default); - // polyscopeHandle_frameY_initial->setVectorColor( - // /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 1, 0)); - // auto polyscopeHandle_frameZ_initial = polyscopeHandle_initialMesh - // ->addNodeVectorQuantity("FrameZ", framesZ_initial); - // polyscopeHandle_frameZ_initial->setVectorLengthScale(frameLength_default); - // polyscopeHandle_frameZ_initial->setVectorRadius(frameRadius_default); - // polyscopeHandle_frameZ_initial->setVectorColor( - // /*polyscope::getNextUniqueColor()*/ glm::vec3(0, 0, 1)); - // //} - pJob->registerForDrawing(getLabel()); - static bool wasExecuted = false; - if (!wasExecuted) { - std::function callback = [&]() { - static bool showFrames = shouldShowFrames; - - if (ImGui::Checkbox("Show Frames", &showFrames) && showFrames) { - polyscopeHandle_frameX->setEnabled(showFrames); - polyscopeHandle_frameY->setEnabled(showFrames); - polyscopeHandle_frameZ->setEnabled(showFrames); - } - }; - - PolyscopeInterface::addUserCallback(callback); - wasExecuted = true; - } - return polyscopeHandle_deformedEdmeMesh; - } -#endif }; #endif // SIMULATIONHISTORY_HPP diff --git a/simulationmesh.cpp b/simulationmesh.cpp index eed8241..446a3eb 100755 --- a/simulationmesh.cpp +++ b/simulationmesh.cpp @@ -499,13 +499,13 @@ double Element::getMass(const double &materialDensity) } void Element::updateRigidity() { - assert(initialLength != 0); + // assert(initialLength != 0); rigidity.axial = material.youngsModulus * dimensions.A / initialLength; - assert(rigidity.axial != 0); + // assert(rigidity.axial != 0); rigidity.torsional = material.G * dimensions.inertia.J / initialLength; - assert(rigidity.torsional != 0); + // assert(rigidity.torsional != 0); rigidity.firstBending = 2 * material.youngsModulus * dimensions.inertia.I2 / initialLength; - assert(rigidity.firstBending != 0); + // assert(rigidity.firstBending != 0); rigidity.secondBending = 2 * material.youngsModulus * dimensions.inertia.I3 / initialLength; - assert(rigidity.secondBending != 0); + // assert(rigidity.secondBending != 0); } diff --git a/utilities.hpp b/utilities.hpp index 1ae3fe2..ad42f77 100755 --- a/utilities.hpp +++ b/utilities.hpp @@ -140,17 +140,85 @@ struct Vector6d : public std::array { }; namespace Utilities { -inline void parseIntegers(const std::string &str, std::vector &result) { - typedef std::regex_iterator re_iterator; - typedef re_iterator::value_type re_iterated; +//inline std::vector split(std::string text, char delim) +//{ +// std::string line; +// std::vector vec; +// std::stringstream ss(text); +// while (std::getline(ss, line, delim)) { +// vec.push_back(line); +// } +// return vec; +//} - std::regex re("(\\d+)"); +inline std::string_view leftTrimSpaces(const std::string_view& str) +{ +std::string_view trimmedString=str; + const auto pos(str.find_first_not_of(" \t\n\r\f\v")); + trimmedString.remove_prefix(std::min(pos, trimmedString.length())); + return trimmedString; +} - re_iterator rit(str.begin(), str.end(), re); - re_iterator rend; +inline std::string_view rightTrimSpaces(const std::string_view& str) +{ +std::string_view trimmedString=str; + const auto pos(trimmedString.find_last_not_of(" \t\n\r\f\v")); + trimmedString.remove_suffix(std::min(trimmedString.length() - pos - 1,trimmedString.length())); + return trimmedString; +} - std::transform(rit, rend, std::back_inserter(result), - [](const re_iterated &it) { return std::stoi(it[1]); }); +inline std::string_view trimLeftAndRightSpaces(std::string_view str) +{ +std::string_view trimmedString=str; + trimmedString = leftTrimSpaces(trimmedString); + trimmedString = rightTrimSpaces(trimmedString); + return trimmedString; +} + +inline std::vector split(const std::string& text, std::string delim) +{ + std::vector vec; + size_t pos = 0, prevPos = 0; + while (1) { + pos = text.find(delim, prevPos); + if (pos == std::string::npos) { + vec.push_back(text.substr(prevPos)); + return vec; + } + + vec.push_back(text.substr(prevPos, pos - prevPos)); + prevPos = pos + delim.length(); + } +} + +inline std::string toString(const std::vector> &vv) +{ + std::string s; + s.append("{"); + for (const std::vector &v : vv) { + s.append("{"); + for (const int &i : v) { + s.append(std::to_string(i) + ","); + } + s.pop_back(); + s.append("}"); + } + s.append("}"); + return s; +} +inline void parseIntegers(const std::string &str, std::vector &result) +{ + typedef std::regex_iterator re_iterator; + typedef re_iterator::value_type re_iterated; + + std::regex re("(\\d+)"); + + re_iterator rit(str.begin(), str.end(), re); + re_iterator rend; + + std::transform(rit, rend, std::back_inserter(result), [](const re_iterated &it) { + return std::stoi(it[1]); + }); } inline Eigen::MatrixXd toEigenMatrix(const std::vector &v) { @@ -179,7 +247,6 @@ inline std::vector fromEigenMatrix(const Eigen::MatrixXd &m) return v; } - // std::string convertToLowercase(const std::string &s) { // std::string lowercase; // std::transform(s.begin(), s.end(), lowercase.begin(),