19#include <spdlog/sinks/stdout_color_sinks.h>
20#include <spdlog/sinks/basic_file_sink.h>
21#include <spdlog/sinks/ostream_sink.h>
23#include <ipc/utils/logger.hpp>
24#ifdef POLYFEM_WITH_REMESHING
25#include <wmtk/utils/Logger.hpp>
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}})
55 using namespace problem;
56 using namespace utils;
68 const std::string &log_file,
69 const spdlog::level::level_enum log_level,
70 const spdlog::level::level_enum file_log_level,
73 std::vector<spdlog::sink_ptr> sinks;
77 console_sink_ = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
81 if (!log_file.empty())
83 file_sink_ = std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_file,
true);
90 spdlog::flush_every(std::chrono::seconds(3));
95 std::vector<spdlog::sink_ptr> sinks;
96 sinks.emplace_back(std::make_shared<spdlog::sinks::ostream_sink_mt>(os,
false));
101 const std::vector<spdlog::sink_ptr> &sinks,
102 const spdlog::level::level_enum log_level)
104 set_logger(std::make_shared<spdlog::logger>(
"polyfem", sinks.begin(), sinks.end()));
107 ipc::set_logger(std::make_shared<spdlog::logger>(
"ipctk", sinks.begin(), sinks.end()));
109#ifdef POLYFEM_WITH_REMESHING
110 wmtk::set_logger(std::make_shared<spdlog::logger>(
"wmtk", sinks.begin(), sinks.end()));
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);
125 spdlog::set_level(log_level);
134 logger().set_level(log_level);
135 ipc::logger().set_level(log_level);
141 json args_in = p_args_in;
149 jse.strict = strict_validation;
150 const std::string polyfem_input_spec = POLYFEM_INPUT_SPEC;
151 std::ifstream file(polyfem_input_spec);
157 logger().error(
"unable to open {} rules", polyfem_input_spec);
158 throw std::runtime_error(
"Invald spec file");
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);
165 polysolve::linear::Solver::apply_default_solver(rules,
"/solver/linear");
166 polysolve::linear::Solver::apply_default_solver(rules,
"/solver/adjoint_linear");
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"];
173 polysolve::linear::Solver::select_valid_solver(args_in[
"solver"][
"adjoint_linear"],
logger());
176 if (args_in.contains(
"/solver/nonlinear"_json_pointer))
178 if (args_in.contains(
"/solver/augmented_lagrangian/nonlinear"_json_pointer))
180 assert(args_in[
"solver"][
"augmented_lagrangian"][
"nonlinear"].is_object());
183 json nonlinear = args_in[
"solver"][
"nonlinear"];
184 nonlinear.merge_patch(args_in[
"solver"][
"augmented_lagrangian"][
"nonlinear"]);
185 args_in[
"solver"][
"augmented_lagrangian"][
"nonlinear"] = nonlinear;
190 args_in[
"solver"][
"augmented_lagrangian"][
"nonlinear"] = args_in[
"solver"][
"nonlinear"];
194 const bool valid_input = jse.verify_json(args_in, rules);
198 logger().error(
"invalid input json:\n{}", jse.log2str());
199 throw std::runtime_error(
"Invald input json file");
203 this->
args = jse.inject_defaults(args_in, rules);
208 if (!output_dir.empty())
210 std::filesystem::create_directories(
output_dir);
214 std::string out_path_log = this->
args[
"output"][
"log"][
"path"];
215 if (!out_path_log.empty())
222 this->
args[
"output"][
"log"][
"level"],
223 this->
args[
"output"][
"log"][
"file_level"],
224 this->
args[
"output"][
"log"][
"quiet"]);
226 logger().info(
"Saving output to {}", output_dir);
228 const unsigned int thread_in = this->
args[
"solver"][
"max_threads"];
231 has_dhat = args_in[
"contact"].contains(
"dhat");
237 if (
args[
"solver"][
"contact"][
"friction_iterations"] == 0)
239 logger().info(
"specified friction_iterations is 0; disabling friction");
240 args[
"contact"][
"friction_coefficient"] = 0.0;
242 else if (
args[
"solver"][
"contact"][
"friction_iterations"] < 0)
244 args[
"solver"][
"contact"][
"friction_iterations"] = std::numeric_limits<int>::max();
246 if (
args[
"contact"][
"friction_coefficient"] == 0.0)
248 args[
"solver"][
"contact"][
"friction_iterations"] = 0;
253 args[
"solver"][
"contact"][
"friction_iterations"] = 0;
254 args[
"contact"][
"friction_coefficient"] = 0;
255 args[
"contact"][
"periodic"] =
false;
264 if (!other_name.empty())
270 if (!
args.contains(
"preset_problem"))
273 problem = std::make_shared<assembler::GenericScalarProblem>(
"GenericScalar");
275 problem = std::make_shared<assembler::GenericTensorProblem>(
"GenericTensor");
278 if (!
args[
"time"].is_null())
280 const auto tmp = R
"({"is_time_dependent": true})"_json;
285 auto bc =
args[
"boundary_conditions"];
288 problem->set_parameters(
args[
"initial_conditions"]);
294 if (
args[
"preset_problem"][
"type"] ==
"Kernel")
315 if (!
args[
"contact"][
"use_convergent_formulation"])
317 args[
"contact"][
"use_convergent_formulation"] =
true;
318 logger().info(
"Use convergent formulation for differentiable contact...");
320 if (
args[
"/solver/contact/barrier_stiffness"_json_pointer].is_string())
322 logger().error(
"Only constant barrier stiffness is supported in differentiable contact!");
326 if (
args.contains(
"boundary_conditions") &&
args[
"boundary_conditions"].contains(
"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!");
357 else if (num_valid == 2)
367 time_steps = int(ceil((tend - t0) / dt));
368 assert(time_steps > 0);
372 time_steps =
args[
"time"][
"time_steps"];
373 assert(time_steps > 0);
374 dt = (tend - t0) / time_steps;
379 throw std::runtime_error(
"This code should be unreachable!");
390 time_steps =
args[
"time"][
"time_steps"];
391 assert(time_steps > 0);
393 tend = t0 + time_steps * dt;
398 throw std::runtime_error(
"This code should be unreachable!");
401 else if (num_valid == 3)
405 time_steps =
args[
"time"][
"time_steps"];
408 if (abs(t0 + dt * time_steps - tend) > 1e-12)
415 args[
"time"][
"tend"] = tend;
416 args[
"time"][
"dt"] = dt;
417 args[
"time"][
"time_steps"] = time_steps;
421 logger().info(
"t0={}, dt={}, tend={}", t0, dt, tend);
427 for (
auto &a : assemblers)
433 if (!
args[
"materials"].is_array() &&
args[
"materials"][
"type"] ==
"AMIPS")
435 json transform_params = {};
436 transform_params[
"canonical_transformation"] = json::array();
437 if (!
mesh->is_volume())
439 Eigen::MatrixXd regular_tri(3, 3);
440 regular_tri << 0, 0, 1,
442 1. / 2., std::sqrt(3) / 2., 1;
443 regular_tri.transposeInPlace();
444 Eigen::MatrixXd regular_tri_inv = regular_tri.inverse();
447 for (
int e = 0; e <
mesh->n_elements(); e++)
449 Eigen::MatrixXd transform;
451 transform_params[
"canonical_transformation"].push_back(
json({
465 Eigen::MatrixXd regular_tet(4, 4);
466 regular_tet << 0, 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();
474 for (
int e = 0; e <
mesh->n_elements(); e++)
476 Eigen::MatrixXd transform;
478 transform_params[
"canonical_transformation"].push_back(
json({
497 transform_params[
"solve_displacement"] =
true;
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);
507 for (
auto &a : assemblers)
508 a->set_materials(body_ids,
args[
"materials"],
units);
513 const int size = (this->assembler->
is_tensor() || this->assembler->
is_fluid()) ? this->
mesh->dimension() : 1;
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);
std::string formulation() const
return the formulation (checks if the problem is scalar or not and deals with multiphysics)
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
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.
solver::CacheLevel optimization_enabled
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
std::shared_ptr< assembler::Mass > mass_matrix_assembler
bool has_dhat
stores if input json contains dhat
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
spdlog::sink_ptr file_sink_
spdlog::sink_ptr console_sink_
logger sink to stdout
json args
main input arguments containing all defaults
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
bool is_contact_enabled() const
does the simulation has contact
std::shared_ptr< assembler::MixedAssembler > mixed_assembler
Eigen::MatrixXd rhs
System right-hand side.
void init(const json &json)
double characteristic_length() const
static double convert(const json &val, const std::string &unit_type)
const std::string & time() const
virtual bool is_tensor() const
virtual void set_size(const int size)
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
void compute_cell_jacobian(const int el_id, const Eigen::MatrixXd &reference_map, Eigen::MatrixXd &jacobian) const
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()
void set_num_threads(const int max_threads)
NLOHMANN_JSON_SERIALIZE_ENUM(CollisionProxyTessellation, {{CollisionProxyTessellation::REGULAR, "regular"}, {CollisionProxyTessellation::IRREGULAR, "irregular"}})
bool is_param_valid(const json ¶ms, const std::string &key)
Determine if a key exists and is non-null in a json object.
void apply_common_params(json &args)
spdlog::logger & logger()
Retrieves the current logger.
void set_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by Polyfem.
void log_and_throw_error(const std::string &msg)