Refactoring

This commit is contained in:
Iason 2020-12-17 23:14:59 +02:00
parent 0876d4bd23
commit 9fe6b83ee8
1 changed files with 251 additions and 0 deletions

251
src/main.cpp.autosave Normal file
View File

@ -0,0 +1,251 @@
#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"
#include "trianglepattterntopology.hpp"
#include <chrono>
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
#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);
//}
int main(int argc, char *argv[]) {
// FlatPattern pattern("/home/iason/Models/valid_6777.ply");
// FlatPattern pattern("/home/iason/Models/simple_beam_paper_example.ply");
// pattern.savePly("fannedValid.ply");
// Create reduced models
// FormFinder::runUnitTests();
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);
// FlatPattern pattern("/home/iason/Models/TestSet_validPatterns/59.ply");
// optimizer.initialize(pattern, *reducedModels[1], {});
// optimizer.setInitialGuess
// ({0.050000000000000003, 4.0001308096448325, 4.2377893536538567,
// 5.6122373437520334});
// Eigen::VectorXd optimalParameters = optimizer.optimize(0);
std::string fullPatternsTestSetDirectory =
// "/home/iason/Models/TestSet_validPatterns";
"/home/iason/Documents/PhD/Research/Pattern_enumerator/Results/"
"1v_0v_2e_1e_1c_6fan/3/Valid";
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/59.ply");
entry.path();
const auto filepathString = filepath.string();
std::cout << "Full pattern:" << filepathString << std::endl;
// 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 (int reducedPatternIndex = 0;
// reducedPatternIndex < reducedModels.size();
// reducedPatternIndex++) {
// FlatPattern *pReducedModel = reducedModels[reducedPatternIndex];
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);
// }
std::cout << "Full pattern:" << filepathString << std::endl;
std::cout << "Reduced pattern:" << 1 << std::endl;
optimizer.initialize(pattern, *reducedModels[1], 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);
// 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;
// }
// // Beam
// // registerWorldAxes();
// 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();
// FormFinder formFinder;
// formFinder.runUnitTests();
// 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};
// // Forced displacements
// std::unordered_map<size_t, Eigen::Vector3d> nodalForcedDisplacements;
// // nodalForcedDisplacements[10] = Eigen::Vector3d(0, 0.1, 0.1);
// 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}};
// // nodalForcedNormal[10] = v;
// // nodalForcedNormal[0] = -v;
// // fixedVertices[10] = {0, 1};
// SimulationJob beamSimulationJob{std::make_shared<SimulationMesh>(mesh),
// fixedVertices,
// nodalForces,
// {},
// {}};
// beamSimulationJob.mesh->setBeamMaterial(0.3, 1);
// beamSimulationJob.mesh->setBeamCrossSection(
// RectangularBeamDimensions{0.002, 0.002});
// beamSimulationJob.registerForDrawing();
// registerWorldAxes();
// polyscope::show();
// SimulationResults beamSimulationResults =
// formFinder.executeSimulation(beamSimulationJob, true, true);
// beamSimulationResults.label = "Beam";
// beamSimulationResults.registerForDrawing(beamSimulationJob);
// polyscope::show();
return 0;
}