11#include <polysolve/nonlinear/Solver.hpp>
13#include <spdlog/sinks/stdout_color_sinks.h>
14#include <spdlog/sinks/basic_file_sink.h>
15#include <spdlog/sinks/ostream_sink.h>
19 NLOHMANN_JSON_SERIALIZE_ENUM(
20 spdlog::level::level_enum,
21 {{spdlog::level::level_enum::trace,
"trace"},
22 {spdlog::level::level_enum::debug,
"debug"},
23 {spdlog::level::level_enum::info,
"info"},
24 {spdlog::level::level_enum::warn,
"warning"},
25 {spdlog::level::level_enum::err,
"error"},
26 {spdlog::level::level_enum::critical,
"critical"},
27 {spdlog::level::level_enum::off,
"off"},
28 {spdlog::level::level_enum::trace, 0},
29 {spdlog::level::level_enum::debug, 1},
30 {spdlog::level::level_enum::info, 2},
31 {spdlog::level::level_enum::warn, 3},
32 {spdlog::level::level_enum::err, 3},
33 {spdlog::level::level_enum::critical, 4},
34 {spdlog::level::level_enum::off, 5}})
50 const std::string &log_file,
51 const spdlog::level::level_enum log_level,
52 const spdlog::level::level_enum file_log_level,
55 std::vector<spdlog::sink_ptr> sinks;
59 console_sink_ = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
63 if (!log_file.empty())
65 file_sink_ = std::make_shared<spdlog::sinks::basic_file_sink_mt>(log_file,
true);
72 spdlog::flush_every(std::chrono::seconds(3));
77 std::vector<spdlog::sink_ptr> sinks;
78 sinks.emplace_back(std::make_shared<spdlog::sinks::ostream_sink_mt>(os,
false));
83 const std::vector<spdlog::sink_ptr> &sinks,
84 const spdlog::level::level_enum log_level)
86 set_adjoint_logger(std::make_shared<spdlog::logger>(
"adjoint-polyfem", sinks.begin(), sinks.end()));
102 json args_in = p_args_in;
107 if (!output_dir.empty())
109 std::filesystem::create_directories(
output_dir);
113 std::string out_path_log = this->
args[
"output"][
"log"][
"path"];
114 if (!out_path_log.empty())
121 this->
args[
"output"][
"log"][
"level"],
122 this->
args[
"output"][
"log"][
"file_level"],
123 this->
args[
"output"][
"log"][
"quiet"]);
127 const int thread_in = this->
args[
"solver"][
"max_threads"];
136 max_threads <= 0 ? std::numeric_limits<unsigned int>::max() : max_threads);
145 for (
const auto &arg :
args[
"parameters"])
163 std::vector<std::shared_ptr<solver::AdjointForm>> stopping_conditions;
164 for (
const auto &arg :
args[
"stopping_conditions"])
165 stopping_conditions.push_back(
168 nl_problem = std::make_unique<solver::AdjointNLProblem>(
188 args[
"solver"][
"nonlinear"],
189 args[
"solver"][
"linear"],
190 args[
"solver"][
"advanced"][
"characteristic_length"]);
std::vector< std::shared_ptr< State > > states
State used in the opt.
void solve(Eigen::VectorXd &x)
void initial_guess(Eigen::VectorXd &x)
void set_log_level(const spdlog::level::level_enum log_level)
change log level
json args
main input arguments containing all defaults
void init(const json &args, const bool strict_validation)
initialize the polyfem solver with a json settings
spdlog::sink_ptr file_sink_
std::string root_path() const
double eval(Eigen::VectorXd &x) const
std::vector< int > variable_sizes
variables
std::unique_ptr< solver::AdjointNLProblem > nl_problem
std::string output_dir
Directory for output files.
spdlog::sink_ptr console_sink_
logger sink to stdout
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
void create_states(const polyfem::solver::CacheLevel level, const int max_threads=-1)
create the opt states
solver::VariableToSimulationGroup variable_to_simulations
void init_variables()
init variables
void init(const json &args, const std::vector< std::shared_ptr< State > > &states, const std::vector< int > &variable_sizes)
void update(const Eigen::VectorXd &x)
Update parameters in simulators.
void set_logger(spdlog::logger &logger)
static GeogramUtils & instance()
void set_num_threads(const int max_threads)
std::string resolve_path(const std::string &path, const std::string &input_file_path, const bool only_if_exists=false)
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
void set_adjoint_logger(std::shared_ptr< spdlog::logger > p_logger)
Setup a logger object to be used by adjoint Polyfem.
static std::shared_ptr< AdjointForm > create_form(const json &args, const VariableToSimulationGroup &var2sim, const std::vector< std::shared_ptr< State > > &states)
static std::shared_ptr< polysolve::nonlinear::Solver > make_nl_solver(const json &solver_params, const json &linear_solver_params, const double characteristic_length)
static json apply_opt_json_spec(const json &input_args, bool strict_validation)
static Eigen::VectorXd inverse_evaluation(const json &args, const int ndof, const std::vector< int > &variable_sizes, VariableToSimulationGroup &var2sim)
static int compute_variable_size(const json &args, const std::vector< std::shared_ptr< State > > &states)
static std::vector< std::shared_ptr< State > > create_states(const json &state_args, const CacheLevel &level, const size_t max_threads)