Loading [MathJax]/extensions/tex2jax.js
PolyFEM
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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)
7 : forms_(forms)
8 {
9 }
10
12 {
13 double total_weight = 0;
14 for (const auto &f : forms_)
15 total_weight += f->weight();
16
17 logger().debug("Normalizing forms with scale: {}", total_weight);
18
19 for (auto &f : forms_)
20 f->set_scale(total_weight);
21
22 return total_weight;
23 }
24
25 void FullNLProblem::init(const TVector &x)
26 {
27 for (auto &f : forms_)
28 f->init(x);
29 }
30
31 void FullNLProblem::set_project_to_psd(bool project_to_psd)
32 {
33 for (auto &f : forms_)
34 f->set_project_to_psd(project_to_psd);
35 }
36
37 void FullNLProblem::init_lagging(const TVector &x)
38 {
39 for (auto &f : forms_)
40 f->init_lagging(x);
41 }
42
43 void FullNLProblem::update_lagging(const TVector &x, const int iter_num)
44 {
45 for (auto &f : forms_)
46 f->update_lagging(x, iter_num);
47 }
48
50 {
52 for (auto &f : forms_)
53 max_lagging_iterations = std::max(max_lagging_iterations, f->max_lagging_iterations());
55 }
56
58 {
59 for (auto &f : forms_)
60 if (f->uses_lagging())
61 return true;
62 return false;
63 }
64
65 void FullNLProblem::line_search_begin(const TVector &x0, const TVector &x1)
66 {
67 for (auto &f : forms_)
68 f->line_search_begin(x0, x1);
69 }
70
72 {
73 for (auto &f : forms_)
74 f->line_search_end();
75 }
76
77 double FullNLProblem::max_step_size(const TVector &x0, const TVector &x1)
78 {
79 double step = 1;
80 for (auto &f : forms_)
81 if (f->enabled())
82 step = std::min(step, f->max_step_size(x0, x1));
83 return step;
84 }
85
86 bool FullNLProblem::is_step_valid(const TVector &x0, const TVector &x1)
87 {
88 for (auto &f : forms_)
89 if (f->enabled() && !f->is_step_valid(x0, x1))
90 return false;
91 return true;
92 }
93
94 bool FullNLProblem::is_step_collision_free(const TVector &x0, const TVector &x1)
95 {
96 for (auto &f : forms_)
97 if (f->enabled() && !f->is_step_collision_free(x0, x1))
98 return false;
99 return true;
100 }
101
102 double FullNLProblem::value(const TVector &x)
103 {
104 double val = 0;
105 for (auto &f : forms_)
106 if (f->enabled())
107 val += f->value(x);
108 return val;
109 }
110
111 void FullNLProblem::gradient(const TVector &x, TVector &grad)
112 {
113 grad = TVector::Zero(x.size());
114 for (auto &f : forms_)
115 {
116 if (!f->enabled())
117 continue;
118 TVector tmp;
119 f->first_derivative(x, tmp);
120 grad += tmp;
121 }
122 }
123
124 void FullNLProblem::hessian(const TVector &x, THessian &hessian)
125 {
126 hessian.resize(x.size(), x.size());
127 for (auto &f : forms_)
128 {
129 if (!f->enabled())
130 continue;
131 THessian tmp;
132 f->second_derivative(x, tmp);
133 hessian += tmp;
134 }
135 }
136
138 {
139 for (auto &f : forms_)
140 f->solution_changed(x);
141 }
142
143 void FullNLProblem::post_step(const polysolve::nonlinear::PostStepData &data)
144 {
145 for (auto &f : forms_)
146 f->post_step(data);
147 }
148} // 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
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44