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