PolyFEM
Loading...
Searching...
No Matches
StateInit.cpp
Go to the documentation of this file.
1#include <polyfem/State.hpp>
2
6
9
14
16
17#include <jse/jse.h>
18
19#include <spdlog/sinks/stdout_color_sinks.h>
20#include <spdlog/sinks/basic_file_sink.h>
21#include <spdlog/sinks/ostream_sink.h>
22
23#include <ipc/utils/logger.hpp>
24#ifdef POLYFEM_WITH_REMESHING
25#include <wmtk/utils/Logger.hpp>
26#endif
27
30
31#include <sstream>
32
33namespace spdlog::level
34{
36 spdlog::level::level_enum,
37 {{spdlog::level::level_enum::trace, "trace"},
38 {spdlog::level::level_enum::debug, "debug"},
39 {spdlog::level::level_enum::info, "info"},
40 {spdlog::level::level_enum::warn, "warning"},
41 {spdlog::level::level_enum::err, "error"},
42 {spdlog::level::level_enum::critical, "critical"},
43 {spdlog::level::level_enum::off, "off"},
44 {spdlog::level::level_enum::trace, 0},
45 {spdlog::level::level_enum::debug, 1},
46 {spdlog::level::level_enum::info, 2},
47 {spdlog::level::level_enum::warn, 3},
48 {spdlog::level::level_enum::err, 3},
49 {spdlog::level::level_enum::critical, 4},
50 {spdlog::level::level_enum::off, 5}})
51}
52
53namespace polyfem
54{
55 using namespace problem;
56 using namespace utils;
57
59 {
60 using namespace polysolve;
61
63
65 }
66
68 const std::string &log_file,
69 const spdlog::level::level_enum log_level,
70 const spdlog::level::level_enum file_log_level,
71 const bool is_quiet)
72 {
73 std::vector<spdlog::sink_ptr> sinks;
74
75 if (!is_quiet)
76 {
77 console_sink_ = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
78 sinks.emplace_back(console_sink_);
79 }
80
81 if (!log_file.empty())
82 {
83 file_sink_ = std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_file, /*truncate=*/true);
84 // Set the file sink separately from the console so it can save all messages
85 file_sink_->set_level(file_log_level);
86 sinks.push_back(file_sink_);
87 }
88
89 init_logger(sinks, log_level);
90 spdlog::flush_every(std::chrono::seconds(3));
91 }
92
93 void State::init_logger(std::ostream &os, const spdlog::level::level_enum log_level)
94 {
95 std::vector<spdlog::sink_ptr> sinks;
96 sinks.emplace_back(std::make_shared<spdlog::sinks::ostream_sink_mt>(os, false));
97 init_logger(sinks, log_level);
98 }
99
101 const std::vector<spdlog::sink_ptr> &sinks,
102 const spdlog::level::level_enum log_level)
103 {
104 set_logger(std::make_shared<spdlog::logger>("polyfem", sinks.begin(), sinks.end()));
106
107 ipc::set_logger(std::make_shared<spdlog::logger>("ipctk", sinks.begin(), sinks.end()));
108
109#ifdef POLYFEM_WITH_REMESHING
110 wmtk::set_logger(std::make_shared<spdlog::logger>("wmtk", sinks.begin(), sinks.end()));
111#endif
112
113 // Set the logger at the lowest level, so all messages are passed to the sinks
114 logger().set_level(spdlog::level::trace);
115 ipc::logger().set_level(spdlog::level::trace);
116#ifdef POLYFEM_WITH_REMESHING
117 wmtk::logger().set_level(spdlog::level::trace);
118#endif
119
120 set_log_level(log_level);
121 }
122
123 void State::set_log_level(const spdlog::level::level_enum log_level)
124 {
125 spdlog::set_level(log_level);
126 if (console_sink_)
127 {
128 // Set only the level of the console
129 console_sink_->set_level(log_level); // Shared by all loggers
130 }
131 else
132 {
133 // Set the level of all sinks
134 logger().set_level(log_level);
135 ipc::logger().set_level(log_level);
136 }
137 }
138
139 void State::init(const json &p_args_in, const bool strict_validation)
140 {
141 json args_in = p_args_in; // mutable copy
142
143 apply_common_params(args_in);
144
145 // CHECK validity json
146 json rules;
147 jse::JSE jse;
148 {
149 jse.strict = strict_validation;
150 const std::string polyfem_input_spec = POLYFEM_INPUT_SPEC;
151 std::ifstream file(polyfem_input_spec);
152
153 if (file.is_open())
154 file >> rules;
155 else
156 {
157 logger().error("unable to open {} rules", polyfem_input_spec);
158 throw std::runtime_error("Invald spec file");
159 }
160
161 jse.include_directories.push_back(POLYFEM_JSON_SPEC_DIR);
162 jse.include_directories.push_back(POLYSOLVE_JSON_SPEC_DIR);
163 rules = jse.inject_include(rules);
164
165 polysolve::linear::Solver::apply_default_solver(rules, "/solver/linear");
166 polysolve::linear::Solver::apply_default_solver(rules, "/solver/adjoint_linear");
167 }
168
169 polysolve::linear::Solver::select_valid_solver(args_in["solver"]["linear"], logger());
170 if (args_in["solver"]["adjoint_linear"].is_null())
171 args_in["solver"]["adjoint_linear"] = args_in["solver"]["linear"];
172 else
173 polysolve::linear::Solver::select_valid_solver(args_in["solver"]["adjoint_linear"], logger());
174
175 // Use the /solver/nonlinear settings as the default for /solver/augmented_lagrangian/nonlinear
176 if (args_in.contains("/solver/nonlinear"_json_pointer))
177 {
178 if (args_in.contains("/solver/augmented_lagrangian/nonlinear"_json_pointer))
179 {
180 assert(args_in["solver"]["augmented_lagrangian"]["nonlinear"].is_object());
181 // Merge the augmented lagrangian settings into the nonlinear settings,
182 // and then replace the augmented lagrangian settings with the merged settings.
183 json nonlinear = args_in["solver"]["nonlinear"]; // copy
184 nonlinear.merge_patch(args_in["solver"]["augmented_lagrangian"]["nonlinear"]);
185 args_in["solver"]["augmented_lagrangian"]["nonlinear"] = nonlinear;
186 }
187 else
188 {
189 // Copy the nonlinear settings to the augmented_lagrangian settings
190 args_in["solver"]["augmented_lagrangian"]["nonlinear"] = args_in["solver"]["nonlinear"];
191 }
192 }
193
194 const bool valid_input = jse.verify_json(args_in, rules);
195
196 if (!valid_input)
197 {
198 logger().error("invalid input json:\n{}", jse.log2str());
199 throw std::runtime_error("Invald input json file");
200 }
201 // end of check
202
203 this->args = jse.inject_defaults(args_in, rules);
204 units.init(this->args["units"]);
205
206 // Save output directory and resolve output paths dynamically
207 const std::string output_dir = resolve_input_path(this->args["output"]["directory"]);
208 if (!output_dir.empty())
209 {
210 std::filesystem::create_directories(output_dir);
211 }
212 this->output_dir = output_dir;
213
214 std::string out_path_log = this->args["output"]["log"]["path"];
215 if (!out_path_log.empty())
216 {
217 out_path_log = resolve_output_path(out_path_log);
218 }
219
221 out_path_log,
222 this->args["output"]["log"]["level"],
223 this->args["output"]["log"]["file_level"],
224 this->args["output"]["log"]["quiet"]);
225
226 logger().info("Saving output to {}", output_dir);
227
228 const unsigned int thread_in = this->args["solver"]["max_threads"];
229 set_max_threads(thread_in);
230
231 has_dhat = args_in["contact"].contains("dhat");
232
233 init_time();
234
235 if (is_contact_enabled())
236 {
237 if (args["solver"]["contact"]["friction_iterations"] == 0)
238 {
239 logger().info("specified friction_iterations is 0; disabling friction");
240 args["contact"]["friction_coefficient"] = 0.0;
241 }
242 else if (args["solver"]["contact"]["friction_iterations"] < 0)
243 {
244 args["solver"]["contact"]["friction_iterations"] = std::numeric_limits<int>::max();
245 }
246 if (args["contact"]["friction_coefficient"] == 0.0)
247 {
248 args["solver"]["contact"]["friction_iterations"] = 0;
249 }
250 }
251 else
252 {
253 args["solver"]["contact"]["friction_iterations"] = 0;
254 args["contact"]["friction_coefficient"] = 0;
255 args["contact"]["periodic"] = false;
256 }
257
258 const std::string formulation = this->formulation();
260 assert(assembler->name() == formulation);
261 mass_matrix_assembler = std::make_shared<assembler::Mass>();
263
264 if (!other_name.empty())
265 {
268 }
269
270 if (!args.contains("preset_problem"))
271 {
272 if (!assembler->is_tensor())
273 problem = std::make_shared<assembler::GenericScalarProblem>("GenericScalar");
274 else
275 problem = std::make_shared<assembler::GenericTensorProblem>("GenericTensor");
276
277 problem->clear();
278 if (!args["time"].is_null())
279 {
280 const auto tmp = R"({"is_time_dependent": true})"_json;
281 problem->set_parameters(tmp);
282 }
283 // important for the BC
284
285 auto bc = args["boundary_conditions"];
286 bc["root_path"] = root_path();
287 problem->set_parameters(bc);
288 problem->set_parameters(args["initial_conditions"]);
289
290 problem->set_parameters(args["output"]);
291 }
292 else
293 {
294 if (args["preset_problem"]["type"] == "Kernel")
295 {
296 problem = std::make_shared<KernelProblem>("Kernel", *assembler);
297 problem->clear();
298 KernelProblem &kprob = *dynamic_cast<KernelProblem *>(problem.get());
299 }
300 else
301 {
302 problem = ProblemFactory::factory().get_problem(args["preset_problem"]["type"]);
303 problem->clear();
304 }
305 // important for the BC
306 problem->set_parameters(args["preset_problem"]);
307 }
308
309 problem->set_units(*assembler, units);
310
312 {
313 if (is_contact_enabled())
314 {
315 if (!args["contact"]["use_convergent_formulation"])
316 {
317 args["contact"]["use_convergent_formulation"] = true;
318 logger().info("Use convergent formulation for differentiable contact...");
319 }
320 if (args["/solver/contact/barrier_stiffness"_json_pointer].is_string())
321 {
322 logger().error("Only constant barrier stiffness is supported in differentiable contact!");
323 }
324 }
325
326 if (args.contains("boundary_conditions") && args["boundary_conditions"].contains("rhs"))
327 {
328 json rhs = args["boundary_conditions"]["rhs"];
329 if ((rhs.is_array() && rhs.size() > 0 && rhs[0].is_string()) || rhs.is_string())
330 logger().error("Only constant rhs over space is supported in differentiable code!");
331 }
332 }
333 }
334
335 void State::set_max_threads(const int max_threads)
336 {
337 NThread::get().set_num_threads(max_threads);
338 }
339
341 {
342 if (!is_param_valid(args, "time"))
343 return;
344
345 const double t0 = Units::convert(args["time"]["t0"], units.time());
346 double tend, dt;
347 int time_steps;
348
349 // from "tend", "dt", "time_steps" only two can be used at a time
350 const int num_valid = is_param_valid(args["time"], "tend")
351 + is_param_valid(args["time"], "dt")
352 + is_param_valid(args["time"], "time_steps");
353 if (num_valid < 2)
354 {
355 log_and_throw_error("Exactly two of (tend, dt, time_steps) must be specified");
356 }
357 else if (num_valid == 2)
358 {
359 if (is_param_valid(args["time"], "tend"))
360 {
361 tend = Units::convert(args["time"]["tend"], units.time());
362 assert(tend > t0);
363 if (is_param_valid(args["time"], "dt"))
364 {
365 dt = Units::convert(args["time"]["dt"], units.time());
366 assert(dt > 0);
367 time_steps = int(ceil((tend - t0) / dt));
368 assert(time_steps > 0);
369 }
370 else if (is_param_valid(args["time"], "time_steps"))
371 {
372 time_steps = args["time"]["time_steps"];
373 assert(time_steps > 0);
374 dt = (tend - t0) / time_steps;
375 assert(dt > 0);
376 }
377 else
378 {
379 throw std::runtime_error("This code should be unreachable!");
380 }
381 }
382 else if (is_param_valid(args["time"], "dt"))
383 {
384 // tend is already confirmed to be invalid, so time_steps must be valid
385 assert(is_param_valid(args["time"], "time_steps"));
386
387 dt = Units::convert(args["time"]["dt"], units.time());
388 assert(dt > 0);
389
390 time_steps = args["time"]["time_steps"];
391 assert(time_steps > 0);
392
393 tend = t0 + time_steps * dt;
394 }
395 else
396 {
397 // tend and dt are already confirmed to be invalid
398 throw std::runtime_error("This code should be unreachable!");
399 }
400 }
401 else if (num_valid == 3)
402 {
403 tend = Units::convert(args["time"]["tend"], units.time());
404 dt = Units::convert(args["time"]["dt"], units.time());
405 time_steps = args["time"]["time_steps"];
406
407 // Check that all parameters agree
408 if (abs(t0 + dt * time_steps - tend) > 1e-12)
409 {
410 log_and_throw_error("Exactly two of (tend, dt, time_steps) must be specified");
411 }
412 }
413
414 // Store these for use later
415 args["time"]["tend"] = tend;
416 args["time"]["dt"] = dt;
417 args["time"]["time_steps"] = time_steps;
418
420
421 logger().info("t0={}, dt={}, tend={}", t0, dt, tend);
422 }
423
424 void State::set_materials(std::vector<std::shared_ptr<assembler::Assembler>> &assemblers) const
425 {
426 const int size = (assembler->is_tensor() || assembler->is_fluid()) ? mesh->dimension() : 1;
427 for (auto &a : assemblers)
428 a->set_size(size);
429
430 if (!utils::is_param_valid(args, "materials"))
431 return;
432
433 if (!args["materials"].is_array() && args["materials"]["type"] == "AMIPS")
434 {
435 json transform_params = {};
436 transform_params["canonical_transformation"] = json::array();
437 if (!mesh->is_volume())
438 {
439 Eigen::MatrixXd regular_tri(3, 3);
440 regular_tri << 0, 0, 1,
441 1, 0, 1,
442 1. / 2., std::sqrt(3) / 2., 1;
443 regular_tri.transposeInPlace();
444 Eigen::MatrixXd regular_tri_inv = regular_tri.inverse();
445
446 const auto &mesh2d = *dynamic_cast<mesh::Mesh2D *>(mesh.get());
447 for (int e = 0; e < mesh->n_elements(); e++)
448 {
449 Eigen::MatrixXd transform;
450 mesh2d.compute_face_jacobian(e, regular_tri_inv, transform);
451 transform_params["canonical_transformation"].push_back(json({
452 {
453 transform(0, 0),
454 transform(0, 1),
455 },
456 {
457 transform(1, 0),
458 transform(1, 1),
459 },
460 }));
461 }
462 }
463 else
464 {
465 Eigen::MatrixXd regular_tet(4, 4);
466 regular_tet << 0, 0, 0, 1,
467 1, 0, 0, 1,
468 1. / 2., std::sqrt(3) / 2., 0, 1,
469 1. / 2., 1. / 2. / std::sqrt(3), std::sqrt(3) / 2., 1;
470 regular_tet.transposeInPlace();
471 Eigen::MatrixXd regular_tet_inv = regular_tet.inverse();
472
473 const auto &mesh3d = *dynamic_cast<mesh::Mesh3D *>(mesh.get());
474 for (int e = 0; e < mesh->n_elements(); e++)
475 {
476 Eigen::MatrixXd transform;
477 mesh3d.compute_cell_jacobian(e, regular_tet_inv, transform);
478 transform_params["canonical_transformation"].push_back(json({
479 {
480 transform(0, 0),
481 transform(0, 1),
482 transform(0, 2),
483 },
484 {
485 transform(1, 0),
486 transform(1, 1),
487 transform(1, 2),
488 },
489 {
490 transform(2, 0),
491 transform(2, 1),
492 transform(2, 2),
493 },
494 }));
495 }
496 }
497 transform_params["solve_displacement"] = true;
498 assembler->set_materials({}, transform_params, units);
499
500 return;
501 }
502
503 std::vector<int> body_ids(mesh->n_elements());
504 for (int i = 0; i < mesh->n_elements(); ++i)
505 body_ids[i] = mesh->get_body_id(i);
506
507 for (auto &a : assemblers)
508 a->set_materials(body_ids, args["materials"], units);
509 }
510
512 {
513 const int size = (this->assembler->is_tensor() || this->assembler->is_fluid()) ? this->mesh->dimension() : 1;
514 assembler.set_size(size);
515
516 if (!utils::is_param_valid(args, "materials"))
517 return;
518
519 std::vector<int> body_ids(mesh->n_elements());
520 for (int i = 0; i < mesh->n_elements(); ++i)
521 body_ids[i] = mesh->get_body_id(i);
522
523 assembler.set_materials(body_ids, args["materials"], units);
524 }
525
526} // namespace polyfem
std::string formulation() const
return the formulation (checks if the problem is scalar or not and deals with multiphysics)
Definition State.cpp:387
void init(const json &args, const bool strict_validation)
initialize the polyfem solver with a json settings
std::string root_path() const
Get the root path for the state (e.g., args["root_path"] or ".")
std::shared_ptr< assembler::Assembler > assembler
assemblers
Definition State.hpp:155
std::string resolve_output_path(const std::string &path) const
Resolve output path relative to output_dir if the path is not absolute.
std::string output_dir
Directory for output files.
Definition State.hpp:593
solver::CacheLevel optimization_enabled
Definition State.hpp:667
void set_materials(std::vector< std::shared_ptr< assembler::Assembler > > &assemblers) const
set the material and the problem dimension
void set_max_threads(const int max_threads=std::numeric_limits< int >::max())
std::shared_ptr< assembler::Assembler > pressure_assembler
Definition State.hpp:160
std::shared_ptr< assembler::Mass > mass_matrix_assembler
Definition State.hpp:157
bool has_dhat
stores if input json contains dhat
Definition State.hpp:586
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
Definition State.hpp:466
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
Definition State.hpp:168
spdlog::sink_ptr file_sink_
Definition State.hpp:143
spdlog::sink_ptr console_sink_
logger sink to stdout
Definition State.hpp:142
State()
Constructor.
Definition StateInit.cpp:58
json args
main input arguments containing all defaults
Definition State.hpp:101
void set_log_level(const spdlog::level::level_enum log_level)
change log level
void init_time()
initialize time settings if args contains "time"
std::string resolve_input_path(const std::string &path, const bool only_if_exists=false) const
Resolve input path relative to root_path() if the path is not absolute.
void init_logger(const std::string &log_file, const spdlog::level::level_enum log_level, const spdlog::level::level_enum file_log_level, const bool is_quiet)
initializing the logger
Definition StateInit.cpp:67
bool is_contact_enabled() const
does the simulation has contact
Definition State.hpp:574
std::shared_ptr< assembler::MixedAssembler > mixed_assembler
Definition State.hpp:159
Eigen::MatrixXd rhs
System right-hand side.
Definition State.hpp:207
void init(const json &json)
Definition Units.cpp:9
double characteristic_length() const
Definition Units.hpp:22
static double convert(const json &val, const std::string &unit_type)
Definition Units.cpp:31
const std::string & time() const
Definition Units.hpp:21
virtual bool is_tensor() const
virtual void set_size(const int size)
Definition Assembler.hpp:64
virtual bool is_fluid() const
static std::shared_ptr< MixedAssembler > make_mixed_assembler(const std::string &formulation)
static std::string other_assembler_name(const std::string &formulation)
static std::shared_ptr< Assembler > make_assembler(const std::string &formulation)
void compute_face_jacobian(const int el_id, const Eigen::MatrixXd &reference_map, Eigen::MatrixXd &jacobian) const
Definition Mesh2D.cpp:101
void compute_cell_jacobian(const int el_id, const Eigen::MatrixXd &reference_map, Eigen::MatrixXd &jacobian) const
Definition Mesh3D.cpp:424
static const ProblemFactory & factory()
std::shared_ptr< assembler::Problem > get_problem(const std::string &problem) const
void set_logger(spdlog::logger &logger)
static GeogramUtils & instance()
static NThread & get()
Definition par_for.hpp:19
void set_num_threads(const int max_threads)
Definition par_for.hpp:27
NLOHMANN_JSON_SERIALIZE_ENUM(CollisionProxyTessellation, {{CollisionProxyTessellation::REGULAR, "regular"}, {CollisionProxyTessellation::IRREGULAR, "irregular"}})
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
void apply_common_params(json &args)
Definition JSONUtils.cpp:14
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:42
nlohmann::json json
Definition Common.hpp:9
void set_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by Polyfem.
Definition Logger.cpp:60
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:71