PolyFEM
Loading...
Searching...
No Matches
FullNLProblem.cpp
Go to the documentation of this file.
1#include "FullNLProblem.hpp"
3
4namespace polyfem::solver
5{
6 FullNLProblem::FullNLProblem(const std::vector<std::shared_ptr<Form>> &forms, const bool is_residual)
7 : forms_(forms),
8 is_residual_(is_residual)
9 {
10 }
11
13 {
14 double total_weight = 0;
15 for (const auto &f : forms_)
16 total_weight += f->weight();
17
18 logger().debug("Normalizing forms with scale: {}", total_weight);
19
20 for (auto &f : forms_)
21 f->set_scale(total_weight);
22
23 return total_weight;
24 }
25
26 void FullNLProblem::init(const TVector &x)
27 {
28 for (auto &f : forms_)
29 f->init(x);
30 }
31
32 void FullNLProblem::set_project_to_psd(bool project_to_psd)
33 {
34 for (auto &f : forms_)
35 f->set_project_to_psd(project_to_psd);
36 }
37
38 void FullNLProblem::init_lagging(const TVector &x)
39 {
40 for (auto &f : forms_)
41 f->init_lagging(x);
42 }
43
44 void FullNLProblem::update_lagging(const TVector &x, const int iter_num)
45 {
46 for (auto &f : forms_)
47 f->update_lagging(x, iter_num);
48 }
49
51 {
53 for (auto &f : forms_)
54 max_lagging_iterations = std::max(max_lagging_iterations, f->max_lagging_iterations());
56 }
57
59 {
60 for (auto &f : forms_)
61 if (f->uses_lagging())
62 return true;
63 return false;
64 }
65
66 void FullNLProblem::line_search_begin(const TVector &x0, const TVector &x1)
67 {
68 for (auto &f : forms_)
69 f->line_search_begin(x0, x1);
70 }
71
73 {
74 for (auto &f : forms_)
75 f->line_search_end();
76 }
77
78 double FullNLProblem::max_step_size(const TVector &x0, const TVector &x1)
79 {
80 double step = 1;
81 for (auto &f : forms_)
82 if (f->enabled())
83 step = std::min(step, f->max_step_size(x0, x1));
84 return step;
85 }
86
87 bool FullNLProblem::is_step_valid(const TVector &x0, const TVector &x1)
88 {
89 for (auto &f : forms_)
90 if (f->enabled() && !f->is_step_valid(x0, x1))
91 return false;
92 return true;
93 }
94
95 bool FullNLProblem::is_step_collision_free(const TVector &x0, const TVector &x1)
96 {
97 for (auto &f : forms_)
98 if (f->enabled() && !f->is_step_collision_free(x0, x1))
99 return false;
100 return true;
101 }
102
103 double FullNLProblem::value(const TVector &x)
104 {
105 double val = 0;
106 for (auto &f : forms_)
107 if (f->enabled())
108 val += f->value(x);
109 return val;
110 }
111
112 void FullNLProblem::gradient(const TVector &x, TVector &grad)
113 {
114 grad = TVector::Zero(x.size());
115 for (auto &f : forms_)
116 {
117 if (!f->enabled())
118 continue;
119 TVector tmp;
120 f->first_derivative(x, tmp);
121 grad += tmp;
122 }
123 }
124
125 void FullNLProblem::hessian(const TVector &x, THessian &hessian)
126 {
127 hessian.resize(x.size(), x.size());
128 for (auto &f : forms_)
129 {
130 if (!f->enabled())
131 continue;
132 THessian tmp;
133 f->second_derivative(x, tmp);
134 hessian += tmp;
135 }
136 }
137
139 {
140 for (auto &f : forms_)
141 f->solution_changed(x);
142 }
143
144 void FullNLProblem::post_step(const polysolve::nonlinear::PostStepData &data)
145 {
146 for (auto &f : forms_)
147 f->post_step(data);
148 }
149} // namespace polyfem::solver
double val
Definition Assembler.cpp:89
int x
virtual double max_step_size(const TVector &x0, const TVector &x1) override
virtual void init_lagging(const TVector &x)
virtual void set_project_to_psd(bool val) override
virtual void hessian(const TVector &x, THessian &hessian) override
virtual bool is_step_collision_free(const TVector &x0, const TVector &x1)
std::vector< std::shared_ptr< Form > > forms_
virtual void post_step(const polysolve::nonlinear::PostStepData &data) override
virtual void update_lagging(const TVector &x, const int iter_num)
virtual bool is_step_valid(const TVector &x0, const TVector &x1) override
virtual void line_search_end() override
virtual double value(const TVector &x) override
virtual void init(const TVector &x0) override
virtual void solution_changed(const TVector &new_x) override
virtual void gradient(const TVector &x, TVector &gradv) override
FullNLProblem(const std::vector< std::shared_ptr< Form > > &forms, const bool is_residual=false)
virtual void line_search_begin(const TVector &x0, const TVector &x1) override
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44