From 1a27ec16a269e3bad5b87cf7ef3eaa124391c603 Mon Sep 17 00:00:00 2001 From: iasonmanolas Date: Tue, 16 Mar 2021 14:10:08 +0200 Subject: [PATCH 1/3] Refactoring --- src/main.cpp | 4 ++-- src/reducedmodeloptimizer.cpp | 21 ++++++--------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 33606fe..1edaf5b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,13 +31,13 @@ int main(int argc, char *argv[]) { PatternGeometry fullPattern(filepath_fullPattern); fullPattern.setLabel( std::filesystem::path(filepath_fullPattern).stem().string()); - fullPattern.scale(0.03); + fullPattern.scale(0.03,3); ////Reduced pattern const std::string filepath_reducedPattern = argv[2]; PatternGeometry reducedPattern(filepath_reducedPattern); reducedPattern.setLabel( std::filesystem::path(filepath_reducedPattern).stem().string()); - reducedPattern.scale(0.03); + reducedPattern.scale(0.03,3); // Set the optization settings ReducedModelOptimizer::xRange beamWidth{"B", 0.5, 1.5}; diff --git a/src/reducedmodeloptimizer.cpp b/src/reducedmodeloptimizer.cpp index 6cba086..544f5ff 100644 --- a/src/reducedmodeloptimizer.cpp +++ b/src/reducedmodeloptimizer.cpp @@ -174,7 +174,8 @@ double ReducedModelOptimizer::objective(long n, const double *x) { // run simulations double totalError = 0; - LinearSimulationModel simulator; +// LinearSimulationModel simulator; + FormFinder simulator; for (const int simulationScenarioIndex : global.simulationScenarioIndices) { SimulationResults reducedModelResults = simulator.executeSimulation( global.reducedPatternSimulationJobs[simulationScenarioIndex]); @@ -700,20 +701,8 @@ ReducedModelOptimizer::runOptimization(const Settings &settings) { << std::endl; } - // Compute raw objective value - if (global.optimizationSettings.normalizationStrategy != - Settings::NormalizationStrategy::NonNormalized) { - auto temp = global.optimizationSettings.normalizationStrategy; - global.optimizationSettings.normalizationStrategy = - Settings::NormalizationStrategy::NonNormalized; - results.rawObjectiveValue = objective(results.x.size(), results.x.data()); - global.optimizationSettings.normalizationStrategy = temp; - - } else { - results.rawObjectiveValue = results.objectiveValue; - } - // Compute obj value per simulation scenario + results.rawObjectiveValue=0; updateMesh(results.x.size(), results.x.data()); results.objectiveValuePerSimulationScenario.resize( NumberOfSimulationScenarios); @@ -730,7 +719,9 @@ ReducedModelOptimizer::runOptimization(const Settings &settings) { global.fullPatternDisplacements[simulationScenarioIndex], global.reducedToFullInterfaceViMap, global.objectiveNormalizationValues[simulationScenarioIndex]); - + results.rawObjectiveValue+=computeRawError(reducedModelResults.displacements, + global.fullPatternDisplacements[simulationScenarioIndex], + global.reducedToFullInterfaceViMap); results.objectiveValuePerSimulationScenario[simulationScenarioIndex] = error; } From 73dda7e33fd5efd299fab469fdb5ece8b8721828 Mon Sep 17 00:00:00 2001 From: iasonmanolas Date: Wed, 17 Mar 2021 15:43:31 +0200 Subject: [PATCH 2/3] Refactoring. Increased the magnitude of forces in the simulation scenarios since now I am working with more rigid patterns. --- CMakeLists.txt | 30 ++++++++++++++++-------------- src/linearsimulationmodel.cpp | 21 --------------------- src/main.cpp | 2 +- src/reducedmodeloptimizer.cpp | 6 +++--- 4 files changed, 20 insertions(+), 39 deletions(-) delete mode 100644 src/linearsimulationmodel.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 20c5d0d..0cf0cf4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,20 +12,20 @@ else() set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1") endif() +set(EXTERNAL_DEPS_DIR "/home/iason/Coding/build/external dependencies") ##Create directory for the external libraries -file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/external) -#find_package(OpenMP REQUIRED) +file(MAKE_DIRECTORY ${EXTERNAL_DEPS_DIR}) ##Polyscope -set(USE_POLYSCOPE FALSE) +set(USE_POLYSCOPE TRUE) if(${USE_POLYSCOPE}) download_project(PROJ POLYSCOPE GIT_REPOSITORY https://github.com/nmwsharp/polyscope.git GIT_TAG master - PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build/external/ + PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) -add_subdirectory(${POLYSCOPE_SOURCE_DIR}) +add_subdirectory(${POLYSCOPE_SOURCE_DIR} ${POLYSCOPE_BINARY_DIR}) add_compile_definitions(POLYSCOPE_DEFINED) endif() @@ -37,27 +37,29 @@ else() download_project(PROJ MYSOURCES GIT_REPOSITORY https://gitea-s2i2s.isti.cnr.it/manolas/MySources.git GIT_TAG master - PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build/external/ + PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) endif() add_subdirectory(${MYSOURCES_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/MySourcesBinDir) -##dlib +#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 - PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build/external/ + BINARY_DIR ${DLIB_BIN_DIR} + PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) -add_subdirectory(${DLIB_SOURCE_DIR}) - +add_subdirectory(${DLIB_SOURCE_DIR} ${DLIB_BINARY_DIR}) ##vcglib devel branch download_project(PROJ vcglib_devel GIT_REPOSITORY https://github.com/IasonManolas/vcglib.git GIT_TAG devel - PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build/external/ + PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) file(GLOB EXT_SOURCES ${vcglib_devel_SOURCE_DIR}/wrap/ply/plylib.cpp) @@ -66,7 +68,7 @@ file(GLOB EXT_SOURCES ${vcglib_devel_SOURCE_DIR}/wrap/ply/plylib.cpp) download_project(PROJ threed-beam-fea GIT_REPOSITORY https://github.com/IasonManolas/threed-beam-fea.git GIT_TAG master - PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/build/external/ + PREFIX ${EXTERNAL_DEPS_DIR} ${UPDATE_DISCONNECTED_IF_AVAILABLE} ) add_subdirectory(${threed-beam-fea_SOURCE_DIR} ${threed-beam-fea_BINARY_DIR}) @@ -96,7 +98,7 @@ target_include_directories(${PROJECT_NAME} target_link_directories(${PROJECT_NAME} PRIVATE ${MYSOURCES_SOURCE_DIR}/boost_graph/libs/) if(${USE_POLYSCOPE}) - target_link_libraries(${PROJECT_NAME} polyscope Eigen3::Eigen matplot dlib::dlib ThreedBeamFEA MySources) + target_link_libraries(${PROJECT_NAME} polyscope Eigen3::Eigen dlib::dlib ThreedBeamFEA MySources) else() - target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen matplot dlib::dlib ThreedBeamFEA MySources) + target_link_libraries(${PROJECT_NAME} -static Eigen3::Eigen dlib::dlib ThreedBeamFEA MySources) endif() diff --git a/src/linearsimulationmodel.cpp b/src/linearsimulationmodel.cpp deleted file mode 100644 index 6e663c6..0000000 --- a/src/linearsimulationmodel.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "linearsimulationmodel.hpp" -#include -#include -//#include -#include -#include -#include - - - - - - - - - - - - - - diff --git a/src/main.cpp b/src/main.cpp index 1edaf5b..80f3075 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) { // Export results const bool input_resultDirectoryDefined = argc >= 5; std::string optimizationResultsDirectory = - input_resultDirectoryDefined ? argv[4] : "OptimizationResults"; + input_resultDirectoryDefined ? argv[4] : std::filesystem::current_path().append("OptimizationResults"); std::string resultsOutputDir; if (optimizationResults.numberOfSimulationCrashes != 0) { const auto crashedJobsDirPath = diff --git a/src/reducedmodeloptimizer.cpp b/src/reducedmodeloptimizer.cpp index 544f5ff..ea6a408 100644 --- a/src/reducedmodeloptimizer.cpp +++ b/src/reducedmodeloptimizer.cpp @@ -752,7 +752,7 @@ ReducedModelOptimizer::createScenarios( scenarios.resize(SimulationScenario::NumberOfSimulationScenarios); std::unordered_map> fixedVertices; std::unordered_map nodalForces; - const double forceMagnitude = 1; + const double forceMagnitude = 10; //// Axial SimulationScenario scenarioName = SimulationScenario::Axial; @@ -765,7 +765,7 @@ ReducedModelOptimizer::createScenarios( nodalForces[viPair.first] = Vector6d({forceDirection[0], forceDirection[1], forceDirection[2], 0, 0, 0}) * - forceMagnitude * 2; + forceMagnitude * 8; fixedVertices[viPair.second] = std::unordered_set{0, 1, 2, 3, 4, 5}; } @@ -799,7 +799,7 @@ ReducedModelOptimizer::createScenarios( nodalForces[viPair.first] = Vector6d({forceDirection[0], forceDirection[1], forceDirection[2], 0, 0, 0}) * - forceMagnitude * 1; + forceMagnitude * 8; fixedVertices[viPair.second] = std::unordered_set{0, 1, 2, 3, 4, 5}; } From 015dd6ec451adc2a96946af1ef7d5c1ed4a43bf1 Mon Sep 17 00:00:00 2001 From: iasonmanolas Date: Thu, 18 Mar 2021 19:04:22 +0200 Subject: [PATCH 3/3] Moved linear simulation model to MySOurces. Refactored CMakeLists --- CMakeLists.txt | 6 +- src/linearsimulationmodel.hpp | 215 ---------------------------------- src/reducedmodeloptimizer.cpp | 8 +- 3 files changed, 9 insertions(+), 220 deletions(-) delete mode 100644 src/linearsimulationmodel.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cf0cf4..1229e3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,11 @@ set(EXTERNAL_DEPS_DIR "/home/iason/Coding/build/external dependencies") file(MAKE_DIRECTORY ${EXTERNAL_DEPS_DIR}) ##Polyscope -set(USE_POLYSCOPE TRUE) +if(${CMAKE_BUILD_TYPE} STREQUAL "Release") + set(USE_POLYSCOPE FALSE) +else() + set(USE_POLYSCOPE TRUE) +endif() if(${USE_POLYSCOPE}) download_project(PROJ POLYSCOPE GIT_REPOSITORY https://github.com/nmwsharp/polyscope.git diff --git a/src/linearsimulationmodel.hpp b/src/linearsimulationmodel.hpp deleted file mode 100644 index 38bb2d0..0000000 --- a/src/linearsimulationmodel.hpp +++ /dev/null @@ -1,215 +0,0 @@ -#ifndef LINEARSIMULATIONMODEL_HPP -#define LINEARSIMULATIONMODEL_HPP - -//#include "beam.hpp" -#include "simulationresult.hpp" -#include "threed_beam_fea.h" -#include -#include - -// struct BeamSimulationProperties { -// float crossArea; -// float I2; -// float I3; -// float polarInertia; -// float G; -// // Properties used by fea -// float EA; -// float EIz; -// float EIy; -// float GJ; - -// BeamSimulationProperties(const BeamDimensions &dimensions, -// const BeamMaterial &material); -//}; - -// struct NodalForce { -// int index; -// int dof; -// double magnitude; -//}; - -// struct SimulationJob { -// Eigen::MatrixX3d nodes; -// Eigen::MatrixX2i elements; -// Eigen::MatrixX3d elementalNormals; -// Eigen::VectorXi fixedNodes; -// std::vector nodalForces; -// std::vector beamDimensions; -// std::vector beamMaterial; -//}; - -// struct SimulationResults { -// std::vector edgeForces; ///< Force values per force -// component -// ///< #force components x #edges -// Eigen::MatrixXd -// nodalDisplacements; ///< The displacement of each node #nodes x 3 -// SimulationResults(const fea::Summary &feaSummary); -// SimulationResults() {} -//}; - -class LinearSimulationModel { -public: - LinearSimulationModel(){ - - } - static std::vector - getFeaElements(const std::shared_ptr &job) { - const int numberOfEdges = job->pMesh->EN(); - std::vector elements(numberOfEdges); - for (int edgeIndex = 0; edgeIndex < numberOfEdges; edgeIndex++) { - const SimulationMesh::CoordType &evn0 = - job->pMesh->edge[edgeIndex].cV(0)->cN(); - const SimulationMesh::CoordType &evn1 = - job->pMesh->edge[edgeIndex].cV(1)->cN(); - const std::vector nAverageVector{(evn0[0] + evn1[0]) / 2, - (evn0[1] + evn1[1]) / 2, - (evn0[2] + evn1[2]) / 2}; - const Element &element = job->pMesh->elements[edgeIndex]; - const double E = element.material.youngsModulus; - fea::Props feaProperties(E * element.A, E * element.I3, E * element.I2, - element.G * element.J, nAverageVector); - const int vi0 = job->pMesh->getIndex(job->pMesh->edge[edgeIndex].cV(0)); - const int vi1 = job->pMesh->getIndex(job->pMesh->edge[edgeIndex].cV(1)); - elements[edgeIndex] = fea::Elem(vi0, vi1, feaProperties); - } - - return elements; - } - static std::vector - getFeaNodes(const std::shared_ptr &job) { - const int numberOfNodes = job->pMesh->VN(); - std::vector feaNodes(numberOfNodes); - for (int vi = 0; vi < numberOfNodes; vi++) { - const CoordType &p = job->pMesh->vert[vi].cP(); - feaNodes[vi] = fea::Node(p[0], p[1], p[2]); - } - - return feaNodes; - } - static std::vector - getFeaFixedNodes(const std::shared_ptr &job) { - std::vector boundaryConditions; - boundaryConditions.reserve(job->constrainedVertices.size() * 6); - for (auto fixedVertex : job->constrainedVertices) { - const int vertexIndex = fixedVertex.first; - for (int dofIndex : fixedVertex.second) { - boundaryConditions.emplace_back( - fea::BC(vertexIndex, static_cast(dofIndex), 0)); - } - } - - return boundaryConditions; - } - static std::vector - getFeaNodalForces(const std::shared_ptr &job) { - std::vector nodalForces; - nodalForces.reserve(job->nodalExternalForces.size() * 6); - - for (auto nodalForce : job->nodalExternalForces) { - for (int dofIndex = 0; dofIndex < 6; dofIndex++) { - if (nodalForce.second[dofIndex] == 0) { - continue; - } - nodalForces.emplace_back( - fea::Force(nodalForce.first, dofIndex, nodalForce.second[dofIndex])); - } - } - - return nodalForces; - } - static SimulationResults getResults(const fea::Summary &feaSummary) { - SimulationResults results; - - results.executionTime = feaSummary.total_time_in_ms * 1000; - // displacements - results.displacements.resize(feaSummary.num_nodes); - for (int vi = 0; vi < feaSummary.num_nodes; vi++) { - results.displacements[vi] = Vector6d(feaSummary.nodal_displacements[vi]); - } - - // // Convert forces - // // Convert to vector of eigen matrices of the form force component-> per - // // Edge - // const int numDof = 6; - // const size_t numberOfEdges = feaSummary.element_forces.size(); - // edgeForces = - // std::vector(numDof, Eigen::VectorXd(2 * - // numberOfEdges)); - // for (gsl::index edgeIndex = 0; edgeIndex < numberOfEdges; edgeIndex++) { - // for (gsl::index forceComponentIndex = 0; forceComponentIndex < numDof; - // forceComponentIndex++) { - // (edgeForces[forceComponentIndex])(2 * edgeIndex) = - // feaSummary.element_forces[edgeIndex][forceComponentIndex]; - // (edgeForces[forceComponentIndex])(2 * edgeIndex + 1) = - // feaSummary.element_forces[edgeIndex][numDof + - // forceComponentIndex]; - // } - // } - return results; - } - - SimulationResults - executeSimulation(const std::shared_ptr &simulationJob) { - assert(simulationJob->pMesh->VN() != 0); - fea::Job job(getFeaNodes(simulationJob), getFeaElements(simulationJob)); - // printInfo(job); - - // create the default options - fea::Options opts; - opts.save_elemental_forces = false; - opts.save_nodal_displacements = false; - opts.save_nodal_forces = false; - opts.save_report = false; - opts.save_tie_forces = false; - // if (!elementalForcesOutputFilepath.empty()) { - // opts.save_elemental_forces = true; - // opts.elemental_forces_filename = elementalForcesOutputFilepath; - // } - // if (!nodalDisplacementsOutputFilepath.empty()) { - // opts.save_nodal_displacements = true; - // opts.nodal_displacements_filename = nodalDisplacementsOutputFilepath; - // } - - // have the program output status updates - opts.verbose = false; - - // form an empty vector of ties - std::vector ties; - - // also create an empty list of equations - std::vector equations; - auto fixedVertices = getFeaFixedNodes(simulationJob); - auto nodalForces = getFeaNodalForces(simulationJob); - fea::Summary feaResults = - fea::solve(job, fixedVertices, nodalForces, ties, equations, opts); - - SimulationResults results = getResults(feaResults); - results.job = simulationJob; - return results; - } - // SimulationResults getResults() const; - // void setResultsNodalDisplacementCSVFilepath(const std::string - // &outputPath); void setResultsElementalForcesCSVFilepath(const std::string - // &outputPath); - -private: - // std::string nodalDisplacementsOutputFilepath{"nodal_displacement.csv"}; - // std::string elementalForcesOutputFilepath{"elemental_forces.csv"}; - // SimulationResults results; - - static void printInfo(const fea::Job &job) { - std::cout << "Details regarding the fea::Job:" << std::endl; - std::cout << "Nodes:" << std::endl; - for (fea::Node n : job.nodes) { - std::cout << n << std::endl; - } - std::cout << "Elements:" << std::endl; - for (Eigen::Vector2i e : job.elems) { - std::cout << e << std::endl; - } - } -}; - -#endif // LINEARSIMULATIONMODEL_HPP diff --git a/src/reducedmodeloptimizer.cpp b/src/reducedmodeloptimizer.cpp index ea6a408..4ce510c 100644 --- a/src/reducedmodeloptimizer.cpp +++ b/src/reducedmodeloptimizer.cpp @@ -174,8 +174,8 @@ double ReducedModelOptimizer::objective(long n, const double *x) { // run simulations double totalError = 0; -// LinearSimulationModel simulator; - FormFinder simulator; + LinearSimulationModel simulator; +// FormFinder simulator; for (const int simulationScenarioIndex : global.simulationScenarioIndices) { SimulationResults reducedModelResults = simulator.executeSimulation( global.reducedPatternSimulationJobs[simulationScenarioIndex]); @@ -765,7 +765,7 @@ ReducedModelOptimizer::createScenarios( nodalForces[viPair.first] = Vector6d({forceDirection[0], forceDirection[1], forceDirection[2], 0, 0, 0}) * - forceMagnitude * 8; + forceMagnitude * 20; fixedVertices[viPair.second] = std::unordered_set{0, 1, 2, 3, 4, 5}; } @@ -799,7 +799,7 @@ ReducedModelOptimizer::createScenarios( nodalForces[viPair.first] = Vector6d({forceDirection[0], forceDirection[1], forceDirection[2], 0, 0, 0}) * - forceMagnitude * 8; + forceMagnitude * 5; fixedVertices[viPair.second] = std::unordered_set{0, 1, 2, 3, 4, 5}; }