Wildmeshing Toolkit
periodic_optimization.cpp
Go to the documentation of this file.
2 
3 #include <wmtk/EdgeMesh.hpp>
4 #include <wmtk/TetMesh.hpp>
5 #include <wmtk/TriMesh.hpp>
6 
7 #include <wmtk/utils/Logger.hpp>
8 
11 
14 
15 
16 namespace wmtk::components {
17 
18 void periodic_optimization(const utils::Paths& paths, const nlohmann::json& j, io::Cache& cache)
19 {
20  using namespace internal;
21 
23 
24  std::shared_ptr<Mesh> periodic_mesh = cache.read_mesh(options.periodic_mesh);
25  assert(periodic_mesh->top_simplex_type() == PrimitiveType::Tetrahedron);
26 
27  std::shared_ptr<Mesh> position_mesh = cache.read_mesh(options.position_mesh);
28  assert(position_mesh->top_simplex_type() == PrimitiveType::Tetrahedron);
29 
30  assert(position_mesh->is_from_same_multi_mesh_structure(*periodic_mesh));
31 
32  // register surface child mesh to position mesh
33  auto surface_handle =
34  position_mesh->register_attribute<int64_t>("surface", PrimitiveType::Triangle, 1);
35  auto surface_accessor = position_mesh->create_accessor<int64_t>(surface_handle);
36 
37  for (const auto& f : position_mesh->get_all(PrimitiveType::Triangle)) {
38  surface_accessor.scalar_attribute(f) =
39  position_mesh->is_boundary(PrimitiveType::Triangle, f) ? 1 : 0;
40  }
41 
42  internal::MultiMeshFromTag getchild(*position_mesh, surface_handle, 1);
43  getchild.compute_substructure_mesh();
44 
45  std::shared_ptr<Mesh> surface_mesh = position_mesh->get_child_meshes().back();
46 
47  getchild.remove_soup();
48 
49  assert(position_mesh->get_child_meshes().size() == 1);
50  assert(periodic_mesh->get_child_meshes().size() == 1);
51 
52  // propagate position
53  auto position_handle =
54  position_mesh->get_attribute_handle<double>("vertices", PrimitiveType::Vertex);
55 
56  auto surface_position_handle =
57  surface_mesh->register_attribute<double>("vertices", PrimitiveType::Vertex, 3);
58 
59  auto propagate_to_child_position =
60  [](const Eigen::MatrixX<double>& P) -> Eigen::VectorX<double> { return P; };
61 
62  auto update_child_positon =
63  std::make_shared<wmtk::operations::SingleAttributeTransferStrategy<double, double>>(
64  surface_position_handle,
65  position_handle,
66  propagate_to_child_position);
67 
68  update_child_positon->run_on_all();
69 
70  wmtk::logger().info("registered surface mesh to position mesh");
71 
72  std::vector<attribute::MeshAttributeHandle> pass_through_attributes;
73  pass_through_attributes.push_back(surface_handle);
74 
75  wmtk::logger().info("start optimization");
76 
78  *periodic_mesh,
79  *position_mesh,
80  *surface_mesh,
81  options.target_edge_length,
82  options.target_max_amips,
83  options.passes,
84  options.envelope_size,
85  options.intermediate_output,
86  pass_through_attributes,
87  paths.output_dir,
88  options.output);
89 
90  wmtk::logger().info("finished optimization");
91 
92  cache.write_mesh(*periodic_mesh, options.output);
93 }
94 
95 } // namespace wmtk::components
This class generates a multi-mesh from a mesh where the substructure is represented by a tag.
void remove_soup()
Remove the substructure soup from the multimesh.
void compute_substructure_mesh()
Create a manifold mesh from the substructure.
void write_mesh(const Mesh &m, const std::string &name, const std::map< std::string, std::vector< int64_t >> &multimesh_names={})
Write a mesh to cache.
Definition: Cache.cpp:194
std::shared_ptr< Mesh > read_mesh(const std::string &name) const
Load a mesh from cache.
Definition: Cache.cpp:171
void periodic_optimization(Mesh &periodic_mesh, Mesh &position_mesh, Mesh &surface_mesh, const double target_edge_length, const double max_amips_energy, const int64_t passes, const double envelope_size, const bool intermediate_output, std::vector< attribute::MeshAttributeHandle > &pass_through_attributes, std::string output_dir, std::string output)
void periodic_optimization(const utils::Paths &paths, const nlohmann::json &j, io::Cache &cache)
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58
nlohmann::json json
Definition: input.cpp:9