ReducedModelOptimization/src/main.cpp

232 lines
10 KiB
C++
Raw Normal View History

2020-11-23 10:06:45 +01:00
#include "beamformfinder.hpp"
#include "edgemesh.hpp"
#include "flatpattern.hpp"
#include "polyscope/curve_network.h"
#include "polyscope/point_cloud.h"
#include "polyscope/polyscope.h"
#include "reducedmodeloptimizer.hpp"
#include "simulationhistoryplotter.hpp"
2020-12-09 16:58:48 +01:00
#include "trianglepattterntopology.hpp"
2020-11-23 10:06:45 +01:00
#include <chrono>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
2020-12-09 16:58:48 +01:00
#include <vcg/complex/algorithms/update/position.h>
// void scale(const std::vector<size_t>& numberOfNodesPerSlot,
// FlatPattern &pattern) {
// const double desiredSize = 0.0025; // center to boundary
// const size_t interfaceSlotIndex = 4; // bottom edge
// std::unordered_map<size_t, size_t> nodeToSlot;
// std::unordered_map<size_t, std::unordered_set<size_t>> slotToNode;
// FlatPatternTopology::constructNodeToSlotMap(numberOfNodesPerSlot,
// nodeToSlot); FlatPatternTopology::constructSlotToNodeMap(nodeToSlot,
// slotToNode); assert(slotToNode.find(interfaceSlotIndex) != slotToNode.end()
// &&
// slotToNode.find(interfaceSlotIndex)->second.size() == 1);
// // Assuming that in the bottom edge there is only one vertex which is also
// the
// // interface
// const size_t baseTriangleInterfaceVi =
// *(slotToNode.find(interfaceSlotIndex)->second.begin());
// const double currentSize =
// (pattern.vert[baseTriangleInterfaceVi].cP() - pattern.vert[0].cP())
// .Norm();
// const double scaleFactor = desiredSize / currentSize;
// vcg::tri::UpdatePosition<FlatPattern>::Scale(full, scaleFactor);
//}
2020-11-23 10:06:45 +01:00
int main(int argc, char *argv[]) {
// FlatPattern pattern("/home/iason/Models/valid_6777.ply");
2020-11-23 10:06:45 +01:00
// FlatPattern pattern("/home/iason/Models/simple_beam_paper_example.ply");
// pattern.savePly("fannedValid.ply");
// Create reduced models
const std::vector<size_t> numberOfNodesPerSlot{1, 0, 0, 2, 1, 2, 1};
std::vector<vcg::Point2i> singleBarReducedModelEdges{vcg::Point2i(0, 3)};
FlatPattern singleBarReducedModel(numberOfNodesPerSlot,
singleBarReducedModelEdges);
singleBarReducedModel.setLabel("Single bar reduced model");
std::vector<vcg::Point2i> CCWreducedModelEdges{vcg::Point2i(1, 5),
vcg::Point2i(3, 5)};
FlatPattern CWReducedModel(numberOfNodesPerSlot, CCWreducedModelEdges);
CWReducedModel.setLabel("CW reduced model");
std::vector<vcg::Point2i> CWreducedModelEdges{vcg::Point2i(1, 5),
vcg::Point2i(3, 1)};
FlatPattern CCWReducedModel(numberOfNodesPerSlot, CWreducedModelEdges);
CCWReducedModel.setLabel("CCW reduced model");
std::vector<FlatPattern *> reducedModels{&singleBarReducedModel,
&CWReducedModel, &CCWReducedModel};
ReducedModelOptimizer optimizer(numberOfNodesPerSlot);
std::string fullPatternsTestSetDirectory =
"/home/iason/Models/TestSet_validPatterns";
for (const auto &entry :
filesystem::directory_iterator(fullPatternsTestSetDirectory)) {
const auto filepath =
// std::filesystem::path("/home/iason/Models/valid_6777.ply");
// std::filesystem::path(
// "/home/iason/Documents/PhD/Research/Pattern_enumerator/Results/"
// "1v_0v_2e_1e_1c_6fan/3/Valid/222.ply");
// std::filesystem::path(
// "/home/iason/Documents/PhD/Research/Pattern_enumerator/Results/"
// "1v_0v_2e_1e_1c_6fan/2/Valid/33.ply");
std::filesystem::path(
"/home/iason/Documents/PhD/Research/Pattern_enumerator/Results/"
"1v_0v_2e_1e_1c_6fan/3/Valid/431.ply");
// std::filesystem::path(
// "/home/iason/Models/TestSet_validPatterns/865.ply");
entry.path();
const auto filepathString = filepath.string();
// Use only the base triangle version
const std::string tiledSuffix = "_tiled.ply";
if (filepathString.compare(filepathString.size() - tiledSuffix.size(),
tiledSuffix.size(), tiledSuffix) == 0) {
continue;
}
FlatPattern pattern(filepathString);
pattern.setLabel(filepath.stem().string());
std::cout << "Testing Pattern:" << filepathString << std::endl;
for (FlatPattern *pReducedModel : reducedModels) {
// pReducedModel = reducedModels[1];
std::unordered_set<size_t> optimizationExcludedEi;
if (pReducedModel !=
reducedModels[0]) { // assumes that the singleBar reduced model is
// the
// first in the reducedModels vector
optimizationExcludedEi.insert(0);
}
optimizer.initialize(pattern, *pReducedModel, optimizationExcludedEi);
Eigen::VectorXd optimalParameters = optimizer.optimize();
}
}
// // Full model simulation
// std::unordered_map<VertexIndex, std::unordered_set<DoFType>>
// fixedVertices;
// // fixedVertices[0] = std::unordered_set<DoFType>{0, 1, 2, 3, 4, 5};
// fixedVertices[3] = std::unordered_set<DoFType>{0, 1, 2, 3, 4, 5};
// // fixedVertices[7] = std::unordered_set<DoFType>{0, 1, 2};
// std::unordered_map<VertexIndex, Vector6d> nodalForces{
// {15, {0, 0, 2000, 0, 0, 0}}};
// SimulationJob fullModelSimulationJob{
// std::make_shared<ElementalMesh>(pattern), fixedVertices, nodalForces,
// {}};
// Simulator formFinder;
// SimulationResults fullModelResults =
// formFinder.executeSimulation(fullModelSimulationJob);
// fullModelResults.simulationLabel = "Full Model";
// fullModelResults.draw(fullModelSimulationJob);
2020-11-23 10:06:45 +01:00
// fullModelSimulationJob.draw();
// double stiffnessFactor = 1;
// while (true) {
// Reduced model simulation
// SimulationJob reducedModelSimulationJob =
// optimizer.getReducedSimulationJob(fullModelSimulationJob);
// const std::vector<double> stiffnessVector{
// fullModelSimulationJob.mesh->elements[0].properties.A,
// stiffnessFactor *
// fullModelSimulationJob.mesh->elements[0].properties.J, stiffnessFactor
// * fullModelSimulationJob.mesh->elements[0].properties.I2,
// stiffnessFactor *
// fullModelSimulationJob.mesh->elements[0].properties.I3};
// for (EdgeIndex ei = 0; ei < reducedModelSimulationJob.mesh->EN(); ei++) {
// BeamFormFinder::Element &e =
// reducedModelSimulationJob.mesh->elements[ei]; e.properties.A =
// 0.00035185827018667374;
// // stifnessVector[0];
// e.properties.J = // stiffnessVector[1];
// 7.709325104874406e-08;
// e.properties.I2 = -0.0000015661453308127776;
// // stiffnessVector[2];
// e.properties.I3 = 3.7099813776947167e-07; // stiffnessVector[3];
// e.axialConstFactor = e.properties.E * e.properties.A / e.initialLength;
// e.torsionConstFactor = e.properties.G * e.properties.J /
// e.initialLength; e.firstBendingConstFactor =
// 2 * e.properties.E * e.properties.I2 / e.initialLength;
// e.secondBendingConstFactor =
// 2 * e.properties.E * e.properties.I3 / e.initialLength;
// }
// SimulationResults reducedModelResults =
// formFinder.executeSimulation(reducedModelSimulationJob, true);
// reducedModelResults.simulationLabel =
// "Reduced Model_" + std::to_string(stiffnessFactor);
// reducedModelResults.draw(reducedModelSimulationJob);
// SimulationResultsReporter resultsReporter;
// resultsReporter.reportResults(
// {reducedModelResults},
// std::filesystem::current_path().append("Results"),
// "_" + std::to_string(stiffnessFactor));
// if (reducedModelResults.history.numberOfSteps ==
// BeamFormFinder::Simulator::maxDRMIterations) {
// break;
// }
// stiffnessFactor *= 1.5;
// }
2020-12-09 16:58:48 +01:00
// Beam
// registerWorldAxes();
2020-12-09 16:58:48 +01:00
VCGEdgeMesh mesh;
// mesh.loadFromPly("/home/iason/Models/simple_beam_model_10elem_1m.ply");
mesh.loadFromPly("/home/iason/Coding/Projects/Approximating shapes with flat "
"patterns/RodModelOptimizationForPatterns/build/Debug/"
"Fanned_Single bar reduced model.ply");
// vcg::Matrix44d R;
// R.SetRotateDeg(45, {0, 0, 1});
// vcg::tri::UpdatePosition<VCGEdgeMesh>::Matrix(mesh, R);
// mesh.updateEigenEdgeAndVertices();
2020-12-09 16:58:48 +01:00
FormFinder formFinder;
formFinder.runUnitTests();
2020-12-09 16:58:48 +01:00
std::unordered_map<VertexIndex, std::unordered_set<DoFType>> fixedVertices;
fixedVertices[1] = std::unordered_set<DoFType>{2};
fixedVertices[2] = std::unordered_set<DoFType>{2};
fixedVertices[3] = std::unordered_set<DoFType>{1, 2};
fixedVertices[4] = std::unordered_set<DoFType>{2};
fixedVertices[5] = std::unordered_set<DoFType>{2};
fixedVertices[6] = std::unordered_set<DoFType>{0, 1, 2};
2020-12-09 16:58:48 +01:00
// Forced displacements
std::unordered_map<size_t, Eigen::Vector3d> nodalForcedDisplacements;
// nodalForcedDisplacements[10] = Eigen::Vector3d(0, 0.1, 0.1);
2020-12-09 16:58:48 +01:00
std::unordered_map<size_t, VectorType> nodalForcedNormal;
CoordType v14 = (mesh.vert[4].cP() - mesh.vert[0].cP()).Normalize();
CoordType v25 = (mesh.vert[5].cP() - mesh.vert[0].cP()).Normalize();
CoordType v36 = (mesh.vert[6].cP() - mesh.vert[0].cP()).Normalize();
const double forceMagnitude = 0.4;
std::unordered_map<VertexIndex, Vector6d> nodalForces{
// {4, Vector6d({0, 0, 0, v14[0], v14[1], 0}) * forceMagnitude},
// {1, Vector6d({0, 0, 0, -v14[0], -v14[1], 0}) * forceMagnitude},
// {5, Vector6d({0, 0, 0, v25[0], v25[1], 0}) * forceMagnitude},
// {2, Vector6d({0, 0, 0, -v25[0], -v25[1], 0}) * forceMagnitude},
// {6, Vector6d({0, 0, 0, v36[0], v36[1], 0}) * forceMagnitude},
{3, Vector6d({0, 0, 0, -v36[0], -v36[1], 0}) * forceMagnitude}};
2020-12-09 16:58:48 +01:00
// nodalForcedNormal[10] = v;
// nodalForcedNormal[0] = -v;
// fixedVertices[10] = {0, 1};
2020-12-09 16:58:48 +01:00
SimulationJob beamSimulationJob{std::make_shared<SimulationMesh>(mesh),
fixedVertices,
nodalForces,
{},
{}};
2020-12-09 16:58:48 +01:00
beamSimulationJob.mesh->setBeamMaterial(0.3, 1);
beamSimulationJob.mesh->setBeamCrossSection(
RectangularBeamDimensions{0.002, 0.002});
beamSimulationJob.registerForDrawing();
registerWorldAxes();
polyscope::show();
2020-12-09 16:58:48 +01:00
SimulationResults beamSimulationResults =
formFinder.executeSimulation(beamSimulationJob, true, true);
beamSimulationResults.simulationLabel = "Beam";
beamSimulationResults.registerForDrawing(beamSimulationJob);
polyscope::show();
2020-11-23 10:06:45 +01:00
return 0;
}