PolyFEM
Loading...
Searching...
No Matches
Form.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <polysolve/nonlinear/PostStepData.hpp>
5
6#include <filesystem>
7
8namespace polyfem::solver
9{
10 class Form
11 {
12 public:
13 virtual ~Form() {}
14
15 virtual std::string name() const = 0;
16
19 virtual void init(const Eigen::VectorXd &x) {}
20
21 virtual void finish() {}
22
26 inline virtual double value(const Eigen::VectorXd &x) const
27 {
28 return weight() * value_unweighted(x);
29 }
30
34 inline Eigen::VectorXd value_per_element(const Eigen::VectorXd &x) const
35 {
37 }
38
42 inline virtual void first_derivative(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
43 {
45 gradv *= weight();
46 }
47
52 inline void second_derivative(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const
53 {
55 hessian *= weight();
56 }
57
62 virtual bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return true; }
63
68 virtual double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return 1; }
69
73 virtual void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) {}
74
76 virtual void line_search_end() {}
77
82 virtual void post_step(const polysolve::nonlinear::PostStepData &data) {}
83
86 virtual void solution_changed(const Eigen::VectorXd &new_x) {}
87
91 virtual void update_quantities(const double t, const Eigen::VectorXd &x) {}
92
96 virtual void init_lagging(const Eigen::VectorXd &x) {};
97
100 virtual void update_lagging(const Eigen::VectorXd &x, const int iter_num) {};
101
103 virtual int max_lagging_iterations() const { return 1; }
104
107 virtual bool uses_lagging() const { return false; }
108
112
114 bool is_project_to_psd() const { return project_to_psd_; }
115
117 void enable() { enabled_ = true; }
119 void disable() { enabled_ = false; }
121 void set_enabled(const bool enabled) { enabled_ = enabled; }
122
125 bool enabled() const { return enabled_; }
126
128 virtual double weight() const { return weight_; }
129
132 void set_weight(const double weight) { weight_ = weight; }
133
134 // NOTE: The following functions are really specific to the different form and should be implemented in the derived class.
135
138 virtual bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return true; }
139
140 void set_output_dir(const std::string &output_dir) { output_dir_ = output_dir; }
141
142 protected:
143 bool project_to_psd_ = false;
144
145 double weight_ = 1;
146
147 bool enabled_ = true;
148
149 std::string output_dir_;
150
151 std::string resolve_output_path(const std::string &path) const
152 {
153 if (output_dir_.empty() || path.empty() || std::filesystem::path(path).is_absolute())
154 return path;
155 return std::filesystem::weakly_canonical(std::filesystem::path(output_dir_) / path).string();
156 }
157
161 virtual double value_unweighted(const Eigen::VectorXd &x) const = 0;
162
166 virtual Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const
167 {
168 throw std::runtime_error("Not implemented");
169 }
170
174 virtual void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const = 0;
175
179 virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const = 0;
180 };
181} // namespace polyfem::solver
double val
Definition Assembler.cpp:86
int x
virtual void update_lagging(const Eigen::VectorXd &x, const int iter_num)
Update lagged fields.
Definition Form.hpp:100
void enable()
Enable the form.
Definition Form.hpp:117
void set_project_to_psd(bool val)
Set project to psd.
Definition Form.hpp:111
virtual void solution_changed(const Eigen::VectorXd &new_x)
Update cached fields upon a change in the solution.
Definition Form.hpp:86
void disable()
Disable the form.
Definition Form.hpp:119
virtual bool uses_lagging() const
Does this form require lagging?
Definition Form.hpp:107
virtual void update_quantities(const double t, const Eigen::VectorXd &x)
Update time-dependent fields.
Definition Form.hpp:91
virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const =0
Compute the second derivative of the value wrt x.
virtual double value_unweighted(const Eigen::VectorXd &x) const =0
Compute the value of the form.
double weight_
weight of the form (e.g., AL penalty weight or Δt²)
Definition Form.hpp:145
virtual bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const
Checks if the step is collision free.
Definition Form.hpp:138
bool is_project_to_psd() const
Get if the form's second derivative is projected to psd.
Definition Form.hpp:114
virtual void init(const Eigen::VectorXd &x)
Initialize the form.
Definition Form.hpp:19
virtual double value(const Eigen::VectorXd &x) const
Compute the value of the form multiplied with the weigth.
Definition Form.hpp:26
bool enabled() const
Determine if the form is enabled.
Definition Form.hpp:125
void set_weight(const double weight)
Set the form's multiplicative constant weight.
Definition Form.hpp:132
virtual bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const
Determine if a step from solution x0 to solution x1 is allowed.
Definition Form.hpp:62
std::string output_dir_
Definition Form.hpp:149
virtual void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const =0
Compute the first derivative of the value wrt x.
virtual std::string name() const =0
void second_derivative(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const
Compute the second derivative of the value wrt x multiplied with the weigth.
Definition Form.hpp:52
virtual void finish()
Definition Form.hpp:21
void set_output_dir(const std::string &output_dir)
Definition Form.hpp:140
Eigen::VectorXd value_per_element(const Eigen::VectorXd &x) const
Compute the value of the form multiplied with the weigth.
Definition Form.hpp:34
virtual void first_derivative(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
Compute the first derivative of the value wrt x multiplied with the weigth.
Definition Form.hpp:42
virtual void init_lagging(const Eigen::VectorXd &x)
Initialize lagged fields TODO: more than one step.
Definition Form.hpp:96
virtual double weight() const
Get the form's multiplicative constant weight.
Definition Form.hpp:128
virtual void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1)
Initialize variables used during the line search.
Definition Form.hpp:73
virtual void post_step(const polysolve::nonlinear::PostStepData &data)
Update fields after a step in the optimization.
Definition Form.hpp:82
virtual void line_search_end()
Clear variables used during the line search.
Definition Form.hpp:76
virtual double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const
Determine the maximum step size allowable between the current and next solution.
Definition Form.hpp:68
std::string resolve_output_path(const std::string &path) const
Definition Form.hpp:151
bool enabled_
If true, the form is enabled.
Definition Form.hpp:147
virtual Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const
Compute the value of the form multiplied per element.
Definition Form.hpp:166
void set_enabled(const bool enabled)
Set if the form is enabled.
Definition Form.hpp:121
virtual int max_lagging_iterations() const
Get the maximum number of lagging iteration allowable.
Definition Form.hpp:103
virtual ~Form()
Definition Form.hpp:13
bool project_to_psd_
If true, the form's second derivative is projected to be positive semidefinite.
Definition Form.hpp:143
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22