PolyFEM
Loading...
Searching...
No Matches
OptState.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <polyfem/Common.hpp>
4
7
11
12#include <Eigen/Core>
13
14#include <polysolve/nonlinear/Criteria.hpp>
15
16#include <string>
17#include <vector>
18#include <memory>
19
20namespace polyfem
21{
22 namespace solver
23 {
24 class AdjointNLProblem;
25 } // namespace solver
26
29 {
30 public:
31 //---------------------------------------------------
32 //-----------------initialization--------------------
33 //---------------------------------------------------
34
35 ~OptState();
37 OptState();
38
42 void init(const json &args, const bool strict_validation);
43
45 int run(json args, const bool strict_validation);
46
49
55 void init_logger(
56 const std::string &log_file,
57 const spdlog::level::level_enum log_level,
58 const spdlog::level::level_enum file_log_level,
59 const bool is_quiet);
60
64 void init_logger(std::ostream &os, const spdlog::level::level_enum log_level);
65
68 void set_log_level(const spdlog::level::level_enum log_level);
69
71 void create_varforms(const int max_threads = -1);
72
74 void init_variables();
75
76 void create_problem();
77
78 void initial_guess(Eigen::VectorXd &x); // shoud be const
79
80 double eval(Eigen::VectorXd &x) const;
81
82 polysolve::nonlinear::Status solve(Eigen::VectorXd &x);
83
84 //---------------------------------------------------
85 //-----------------varforms--------------------
86 //---------------------------------------------------
87
89 std::vector<std::shared_ptr<varform::DifferentiableVarForm>> varforms;
90 std::vector<json> state_args;
91 std::vector<std::shared_ptr<DiffCache>> diff_caches;
92
94 std::vector<int> variable_sizes;
95 int ndof;
96
98
99 std::unique_ptr<solver::AdjointNLProblem> nl_problem;
100
101 private:
102 inline std::string root_path() const
103 {
104 if (utils::is_param_valid(args, "root_path"))
105 return args["root_path"].get<std::string>();
106 return "";
107 }
108
110 void check_unsupported() const;
111
113 void init_logger(const std::vector<spdlog::sink_ptr> &sinks, const spdlog::level::level_enum log_level);
114
116 spdlog::sink_ptr console_sink_ = nullptr;
117 spdlog::sink_ptr file_sink_ = nullptr;
118
121
122 //---------------------------------------------------
123 //-----------------output--------------------
124 //---------------------------------------------------
125
126 public:
128 std::string output_dir;
129 };
130} // namespace polyfem
int x
Main class containing the PolyFEM adjoint solver and its optimization data.
Definition OptState.hpp:29
void initial_guess(Eigen::VectorXd &x)
Definition OptState.cpp:650
void set_log_level(const spdlog::level::level_enum log_level)
change log level
Definition OptState.cpp:412
json args
main input arguments containing all defaults
Definition OptState.hpp:48
std::vector< std::shared_ptr< varform::DifferentiableVarForm > > varforms
Variational formulations used by the optimization.
Definition OptState.hpp:89
void init(const json &args, const bool strict_validation)
initialize the polyfem solver with a json settings
Definition OptState.cpp:419
int run(json args, const bool strict_validation)
Run optimization, including remeshing restarts when the selected trigger requests them.
Definition OptState.cpp:278
std::vector< std::shared_ptr< DiffCache > > diff_caches
Definition OptState.hpp:91
spdlog::sink_ptr file_sink_
Definition OptState.hpp:117
std::string root_path() const
Definition OptState.hpp:102
void check_unsupported() const
Check and throw if any forward simulation varform::DifferentiableVarForm is not supported.
Definition OptState.cpp:479
double eval(Eigen::VectorXd &x) const
Definition OptState.cpp:657
std::vector< int > variable_sizes
variables
Definition OptState.hpp:94
std::unique_ptr< solver::AdjointNLProblem > nl_problem
Definition OptState.hpp:99
std::string output_dir
Directory for output files.
Definition OptState.hpp:128
std::vector< json > state_args
Definition OptState.hpp:90
spdlog::sink_ptr console_sink_
logger sink to stdout
Definition OptState.hpp:116
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 OptState.cpp:368
solver::VariableToSimulationGroup variable_to_simulations
Definition OptState.hpp:97
void create_varforms(const int max_threads=-1)
Create the optimization variational formulations.
Definition OptState.cpp:451
polysolve::nonlinear::Status solve(Eigen::VectorXd &x)
Definition OptState.cpp:663
void init_variables()
init variables
Definition OptState.cpp:542
OptState()
Constructor.
Definition OptState.cpp:273
bool is_param_valid(const json &params, const std::string &key)
Determine if a key exists and is non-null in a json object.
nlohmann::json json
Definition Common.hpp:9