PolyFEM
Loading...
Searching...
No Matches
StateLoad.cpp
Go to the documentation of this file.
2
5
12
14
16
17#include <igl/Timer.h>
18namespace polyfem::legacy
19{
20 using namespace basis;
21 using namespace mesh;
22 using namespace utils;
23
25 {
26 bases.clear();
27 pressure_bases.clear();
28 geom_bases_.clear();
29 boundary_nodes.clear();
30 local_boundary.clear();
32 polys.clear();
33 poly_edge_to_data.clear();
35
36 mass.resize(0, 0);
37 rhs.resize(0, 0);
38
39 n_bases = 0;
41 }
42
43 void State::load_mesh(GEO::Mesh &meshin, const std::function<int(const size_t, const std::vector<int> &, const RowVectorNd &, bool)> &boundary_marker, bool non_conforming, bool skip_boundary_sideset)
44 {
45 reset_mesh();
46
47 igl::Timer timer;
48 timer.start();
49 logger().info("Loading mesh...");
50 mesh = Mesh::create(mesh::MeshReader::from_geogram(meshin), non_conforming);
51 if (!mesh)
52 {
53 logger().error("Unable to load the mesh");
54 return;
55 }
56
57 RowVectorNd min, max;
58 mesh->bounding_box(min, max);
59
60 logger().info("mesh bb min [{}], max [{}]", min, max);
61
62 if (!skip_boundary_sideset)
63 mesh->compute_boundary_ids(boundary_marker);
64
65 std::vector<std::shared_ptr<assembler::Assembler>> assemblers;
66 assemblers.push_back(assembler);
67 assemblers.push_back(mass_matrix_assembler);
68 if (mixed_assembler != nullptr)
69 // TODO: assemblers.push_back(mixed_assembler);
70 mixed_assembler->set_size(mesh->dimension());
71 if (pressure_assembler != nullptr)
72 assemblers.push_back(pressure_assembler);
73 set_materials(assemblers);
74
75 timer.stop();
76 logger().info(" took {}s", timer.getElapsedTime());
77
78 timer.start();
79 logger().info("Loading obstacles...");
80 const polyfem::io::FileSystemIO resources(args["root_path"].get<std::string>());
81 const mesh::GeometryLoader geometry_loader(units, resources);
82 obstacle = geometry_loader.load_obstacles(
83 args["geometry"],
84 utils::json_as_array(args["boundary_conditions"]["obstacle_displacements"]),
85 utils::json_as_array(args["boundary_conditions"]["dirichlet_boundary"]),
86 mesh->dimension());
87
88 timer.stop();
89 logger().info(" took {}s", timer.getElapsedTime());
90
91 out_geom.init_sampler(*mesh, args["output"]["paraview"]["vismesh_rel_area"]);
92 }
93
94 void State::load_mesh(bool non_conforming,
95 const std::vector<std::string> &names,
96 const std::vector<Eigen::MatrixXi> &cells,
97 const std::vector<Eigen::MatrixXd> &vertices)
98 {
99 assert(names.size() == cells.size());
100 assert(vertices.size() == cells.size());
101 assert(names.empty());
102 assert(cells.empty());
103 assert(vertices.empty());
104
105 reset_mesh();
106
107 igl::Timer timer;
108 timer.start();
109
110 logger().info("Loading mesh ...");
111 const polyfem::io::FileSystemIO resources(args["root_path"].get<std::string>());
112 const mesh::GeometryLoader geometry_loader(units, resources);
113 if (mesh == nullptr)
114 {
115 assert(is_param_valid(args, "geometry"));
116 mesh = geometry_loader.load_fem(args["geometry"], non_conforming);
117 }
118
119 if (mesh == nullptr)
120 {
121 log_and_throw_error("unable to load the mesh!");
122 }
123
124 // if(!flipped_elements.empty())
125 // {
126 // mesh->compute_elements_tag();
127 // for(auto el_id : flipped_elements)
128 // mesh->set_tag(el_id, ElementType::INTERIOR_POLYTOPE);
129 // }
130
131 RowVectorNd min, max;
132 mesh->bounding_box(min, max);
133
134 if (args["solver"]["advanced"]["characteristic_length"] > 0)
135 {
136 characteristic_length = args["solver"]["advanced"]["characteristic_length"];
137 }
138 else
139 {
140 characteristic_length = (max - min).norm();
141 }
142
143 logger().info("mesh bb min [{}], max [{}]", min, max);
144
145 std::vector<std::shared_ptr<assembler::Assembler>> assemblers;
146 assemblers.push_back(assembler);
147 assemblers.push_back(mass_matrix_assembler);
148 if (mixed_assembler != nullptr)
149 // TODO: assemblers.push_back(mixed_assembler);
150 mixed_assembler->set_size(mesh->dimension());
151 if (pressure_assembler != nullptr)
152 assemblers.push_back(pressure_assembler);
153 set_materials(assemblers);
155
156 timer.stop();
157 logger().info(" took {}s", timer.getElapsedTime());
158
159 out_geom.init_sampler(*mesh, args["output"]["paraview"]["vismesh_rel_area"]);
160
161 timer.start();
162 logger().info("Loading obstacles...");
163 obstacle = geometry_loader.load_obstacles(
164 args["geometry"],
165 utils::json_as_array(args["boundary_conditions"]["obstacle_displacements"]),
166 utils::json_as_array(args["boundary_conditions"]["dirichlet_boundary"]),
167 mesh->dimension(), non_conforming);
168 timer.stop();
169 logger().info(" took {}s", timer.getElapsedTime());
170 }
171
172 void State::build_mesh_matrices(Eigen::MatrixXd &V, Eigen::MatrixXi &F)
173 {
174 assert(bases.size() == mesh->n_elements());
175 const size_t n_vertices = n_bases - obstacle.n_vertices();
176 const int dim = mesh->dimension();
177
178 V.resize(n_vertices, dim);
179 F.resize(bases.size(), dim + 1); // TODO: this only works for triangles and tetrahedra
180
181 for (int i = 0; i < bases.size(); i++)
182 {
183 const basis::ElementBases &element = bases[i];
184 for (int j = 0; j < element.bases.size(); j++)
185 {
186 const basis::Basis &basis = element.bases[j];
187 assert(basis.global().size() == 1);
188 V.row(basis.global()[0].index) = basis.global()[0].node;
189 if (j < F.cols()) // Only grab the corners of the triangles/tetrahedra
190 F(i, j) = basis.global()[0].index;
191 }
192 }
193 }
194
195 std::unordered_map<int, std::array<bool, 3>>
196 State::boundary_conditions_ids(const std::string &bc_type) const
197 {
198 assert(args["boundary_conditions"].contains(bc_type));
199 const std::vector<json> json_bcs = json_as_array(args["boundary_conditions"][bc_type]);
200 std::unordered_map<int, std::array<bool, 3>> bcs;
201 for (const json &bc : json_bcs)
202 {
203 assert(bc["dimension"].size() >= mesh->dimension());
204 std::array<bool, 3> dimension{{true, true, true}};
205 for (int d = 0; d < bc["dimension"].size(); ++d)
206 dimension[d] = bc["dimension"][d];
207
208 assert(bc.contains("id") && bc["id"].is_number_integer());
209 bcs[bc["id"].get<int>()] = dimension;
210 }
211
212 return bcs;
213 }
214} // namespace polyfem::legacy
int V
Represents one basis function and its gradient.
Definition Basis.hpp:44
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
std::vector< Basis > bases
one basis function per node in the element
void reset_mesh()
Resets the mesh.
Definition StateLoad.cpp:24
StiffnessMatrix mass
Mass matrix, it is computed only for time dependent problems.
Definition State.hpp:243
std::unordered_map< int, std::array< bool, 3 > > boundary_conditions_ids(const std::string &bc_type) const
Construct a vector of boundary conditions ids with their dimension flags.
std::vector< mesh::LocalBoundary > local_boundary
mapping from elements to nodes for dirichlet boundary conditions
Definition State.hpp:545
std::shared_ptr< assembler::Mass > mass_matrix_assembler
Definition State.hpp:196
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
Definition State.hpp:578
Eigen::MatrixXd rhs
System right-hand side.
Definition State.hpp:252
void build_mesh_matrices(Eigen::MatrixXd &V, Eigen::MatrixXi &F)
Build the mesh matrices (vertices and elements) from the mesh using the bases node ordering.
json args
main input arguments containing all defaults
Definition State.hpp:140
std::vector< basis::ElementBases > geom_bases_
Geometric mapping bases, if the elements are isoparametric, this list is empty.
Definition State.hpp:215
void load_mesh(bool non_conforming=false, const std::vector< std::string > &names=std::vector< std::string >(), const std::vector< Eigen::MatrixXi > &cells=std::vector< Eigen::MatrixXi >(), const std::vector< Eigen::MatrixXd > &vertices=std::vector< Eigen::MatrixXd >())
loads the mesh from the json arguments
Definition StateLoad.cpp:94
std::map< int, Eigen::MatrixXd > polys
polygons, used since poly have no geom mapping
Definition State.hpp:225
int n_pressure_bases
number of pressure bases
Definition State.hpp:220
int n_bases
number of bases
Definition State.hpp:218
std::vector< basis::ElementBases > pressure_bases
FE pressure bases for mixed elements, the size is #elements.
Definition State.hpp:213
mesh::Obstacle obstacle
Obstacles used in collisions.
Definition State.hpp:580
std::shared_ptr< assembler::Assembler > pressure_assembler
Definition State.hpp:200
io::OutGeometryData out_geom
visualization stuff
Definition State.hpp:724
double characteristic_length
Definition State.hpp:248
std::shared_ptr< assembler::Assembler > assembler
assemblers
Definition State.hpp:194
std::map< int, basis::InterfaceData > poly_edge_to_data
nodes on the boundary of polygonal elements, used for harmonic bases
Definition State.hpp:553
void set_materials(std::vector< std::shared_ptr< assembler::Assembler > > &assemblers) const
set the material and the problem dimension
std::vector< basis::ElementBases > bases
FE bases, the size is #elements.
Definition State.hpp:211
std::shared_ptr< assembler::HRZMass > pure_mass_matrix_assembler
Definition State.hpp:197
std::vector< mesh::LocalBoundary > local_neumann_boundary
mapping from elements to nodes for neumann boundary conditions
Definition State.hpp:547
std::vector< int > boundary_nodes
list of boundary nodes
Definition State.hpp:539
std::shared_ptr< assembler::MixedAssembler > mixed_assembler
Definition State.hpp:199
void init_sampler(const polyfem::mesh::Mesh &mesh, const double vismesh_rel_area)
unitalize the ref element sampler
Definition OutData.cpp:2777
Applies geometry JSON semantics while delegating all decoding to MeshLoader.
std::unique_ptr< Mesh > load_fem(const json &geometry, bool non_conforming=false) const
Load and combine the enabled FEM entries in a geometry configuration.
Obstacle load_obstacles(const json &geometry, const std::vector< json > &obstacle_displacements, const std::vector< json > &dirichlet_conditions, int dimension, bool non_conforming=false) const
static std::unique_ptr< Mesh > create(MeshData data, bool non_conforming=false)
Construct a runtime mesh from format-independent decoded data.
Definition Mesh.cpp:80
static MeshData from_geogram(GEO::Mesh &mesh)
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:49
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73