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
24 inline virtual double value(const Eigen::VectorXd &x) const
25 {
26 return weight() * value_unweighted(x);
27 }
28
32 inline Eigen::VectorXd value_per_element(const Eigen::VectorXd &x) const
33 {
35 }
36
40 inline virtual void first_derivative(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const
41 {
43 gradv *= weight();
44 }
45
50 inline void second_derivative(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const
51 {
53 hessian *= weight();
54 }
55
60 virtual bool is_step_valid(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return true; }
61
66 virtual double max_step_size(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return 1; }
67
71 virtual void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) {}
72
74 virtual void line_search_end() {}
75
80 virtual void post_step(const polysolve::nonlinear::PostStepData &data) {}
81
84 virtual void solution_changed(const Eigen::VectorXd &new_x) {}
85
89 virtual void update_quantities(const double t, const Eigen::VectorXd &x) {}
90
94 virtual void init_lagging(const Eigen::VectorXd &x) {};
95
98 virtual void update_lagging(const Eigen::VectorXd &x, const int iter_num) {};
99
101 virtual int max_lagging_iterations() const { return 1; }
102
105 virtual bool uses_lagging() const { return false; }
106
110
112 bool is_project_to_psd() const { return project_to_psd_; }
113
115 void enable() { enabled_ = true; }
117 void disable() { enabled_ = false; }
119 void set_enabled(const bool enabled) { enabled_ = enabled; }
120
123 bool enabled() const { return enabled_; }
124
126 virtual double weight() const { return weight_; }
127
130 void set_weight(const double weight) { weight_ = weight; }
131
132 // NOTE: The following functions are really specific to the different form and should be implemented in the derived class.
133
136 virtual bool is_step_collision_free(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1) const { return true; }
137
138 void set_output_dir(const std::string &output_dir) { output_dir_ = output_dir; }
139
140 protected:
141 bool project_to_psd_ = false;
142
143 double weight_ = 1;
144
145 bool enabled_ = true;
146
147 std::string output_dir_;
148
149 std::string resolve_output_path(const std::string &path) const
150 {
151 if (output_dir_.empty() || path.empty() || std::filesystem::path(path).is_absolute())
152 return path;
153 return std::filesystem::weakly_canonical(std::filesystem::path(output_dir_) / path).string();
154 }
155
159 virtual double value_unweighted(const Eigen::VectorXd &x) const = 0;
160
164 virtual Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const
165 {
166 throw std::runtime_error("Not implemented");
167 }
168
172 virtual void first_derivative_unweighted(const Eigen::VectorXd &x, Eigen::VectorXd &gradv) const = 0;
173
177 virtual void second_derivative_unweighted(const Eigen::VectorXd &x, StiffnessMatrix &hessian) const = 0;
178 };
179} // 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:98
void enable()
Enable the form.
Definition Form.hpp:115
void set_project_to_psd(bool val)
Set project to psd.
Definition Form.hpp:109
virtual void solution_changed(const Eigen::VectorXd &new_x)
Update cached fields upon a change in the solution.
Definition Form.hpp:84
void disable()
Disable the form.
Definition Form.hpp:117
virtual bool uses_lagging() const
Does this form require lagging?
Definition Form.hpp:105
virtual void update_quantities(const double t, const Eigen::VectorXd &x)
Update time-dependent fields.
Definition Form.hpp:89
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:143
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:136
bool is_project_to_psd() const
Get if the form's second derivative is projected to psd.
Definition Form.hpp:112
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:24
bool enabled() const
Determine if the form is enabled.
Definition Form.hpp:123
void set_weight(const double weight)
Set the form's multiplicative constant weight.
Definition Form.hpp:130
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:60
std::string output_dir_
Definition Form.hpp:147
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:50
void set_output_dir(const std::string &output_dir)
Definition Form.hpp:138
Eigen::VectorXd value_per_element(const Eigen::VectorXd &x) const
Compute the value of the form multiplied with the weigth.
Definition Form.hpp:32
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:40
virtual void init_lagging(const Eigen::VectorXd &x)
Initialize lagged fields TODO: more than one step.
Definition Form.hpp:94
virtual double weight() const
Get the form's multiplicative constant weight.
Definition Form.hpp:126
virtual void line_search_begin(const Eigen::VectorXd &x0, const Eigen::VectorXd &x1)
Initialize variables used during the line search.
Definition Form.hpp:71
virtual void post_step(const polysolve::nonlinear::PostStepData &data)
Update fields after a step in the optimization.
Definition Form.hpp:80
virtual void line_search_end()
Clear variables used during the line search.
Definition Form.hpp:74
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:66
std::string resolve_output_path(const std::string &path) const
Definition Form.hpp:149
bool enabled_
If true, the form is enabled.
Definition Form.hpp:145
virtual Eigen::VectorXd value_per_element_unweighted(const Eigen::VectorXd &x) const
Compute the value of the form multiplied per element.
Definition Form.hpp:164
void set_enabled(const bool enabled)
Set if the form is enabled.
Definition Form.hpp:119
virtual int max_lagging_iterations() const
Get the maximum number of lagging iteration allowable.
Definition Form.hpp:101
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:141
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22