PolyFEM
Loading...
Searching...
No Matches
ShapeVariableToSimulation.cpp
Go to the documentation of this file.
2
8
9#include <Eigen/Core>
10
11#include <string>
12#include <cassert>
13
14namespace polyfem::solver
15{
16
18 DiffCachePtrs diff_caches,
19 CompositeParametrization parametrizations,
20 Eigen::VectorXi active_dimensions,
21 Eigen::VectorXi active_geom_nodes)
22 : dim_(states[0]->mesh->dimension()),
23 vertex_num_(states[0]->mesh->n_vertices()),
24 states_(std::move(states)),
25 diff_caches_(std::move(diff_caches)),
26 parametrization_(std::move(parametrizations)),
27 active_dimensions_(std::move(active_dimensions)),
28 active_geom_nodes_(std::move(active_geom_nodes))
29 {
30 assert(!states_.empty());
31 assert(states_.size() == diff_caches_.size());
32
33 // Validates active selections.
34 std::string reason;
36 {
37 log_and_throw_adjoint_error("Fail to construct shape variable to simulation. Reason: {}", reason);
38 }
40 {
41 log_and_throw_adjoint_error("Fail to construct shape variable to simulation. Reason: {}", reason);
42 }
43
44 // Expand implicit all active selection.
45 if (active_dimensions_.size() == 0)
46 {
47 active_dimensions_ = Eigen::VectorXi::LinSpaced(dim_, 0, dim_ - 1);
48 }
49 if (active_geom_nodes_.size() == 0)
50 {
51 active_geom_nodes_ = Eigen::VectorXi::LinSpaced(vertex_num_, 0, vertex_num_ - 1);
52 }
53 }
54
56 {
57 return "shape";
58 }
59
64
66 {
67 for (auto &s : states_)
68 {
69 if (s.get() == &target)
70 {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 void ShapeVariableToSimulation::update(const Eigen::VectorXd &x)
78 {
79 Eigen::VectorXd y = parametrization_.eval(x);
80 assert(y.size() == para_out_dof());
81
82 int active_dim_num = active_dimensions_.size();
83 for (auto &s : states_)
84 {
85 for (int ni = 0; ni < active_geom_nodes_.size(); ++ni)
86 {
87 int node_id = active_geom_nodes_(ni);
88 Eigen::VectorXd p = s->mesh->point(node_id);
89 for (int di = 0; di < active_dimensions_.size(); ++di)
90 {
91 int d = active_dimensions_(di);
92 p(d) = y(ni * active_dim_num + di);
93 }
94 s->mesh->set_point(node_id, p);
95 }
96 }
97 }
98
99 void ShapeVariableToSimulation::update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const
100 {
101 assert(state_variables.size() == dim_ * vertex_num_);
102
103 Eigen::VectorXd y = parametrization_.eval(x);
104 assert(y.size() == para_out_dof());
105
106 int active_dim_num = active_dimensions_.size();
107 for (int ni = 0; ni < active_geom_nodes_.size(); ++ni)
108 {
109 int vertex_id = active_geom_nodes_(ni);
110 for (int di = 0; di < active_dimensions_.size(); ++di)
111 {
112 int d = active_dimensions_(di);
113 state_variables(vertex_id * dim_ + d) = y(ni * active_dim_num + di);
114 }
115 }
116 }
117
118 Eigen::VectorXd ShapeVariableToSimulation::compute_adjoint_term(const Eigen::VectorXd &x) const
119 {
120 Eigen::VectorXd term, cur_term;
121 for (int i = 0; i < states_.size(); ++i)
122 {
123 auto &state = states_[i];
124 auto &diff_cache = diff_caches_[i];
125
126 if (state->problem->is_time_dependent())
127 {
128 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
129 Eigen::MatrixXd adjoint_nu = get_adjoint_mat(*state, *diff_cache, 1);
130 AdjointTools::dJ_shape_transient_adjoint_term(*state, *diff_cache, adjoint_nu, adjoint_p, cur_term);
131 }
132 else if (state->is_homogenization())
133 {
134 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
136 *diff_cache,
137 diff_cache->u(0),
138 adjoint_p,
139 cur_term);
140 }
141 else
142 {
143 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
145 *diff_cache,
146 diff_cache->u(0),
147 adjoint_p,
148 cur_term);
149 }
150
151 if (term.size() != cur_term.size())
152 {
153 term = cur_term;
154 }
155 else
156 {
157 term += cur_term;
158 }
159 }
160
161 assert(term.size() == vertex_num_ * dim_);
162
163 Eigen::VectorXd active_term(para_out_dof());
164 int active_dim_num = active_dimensions_.size();
165 for (int i = 0; i < active_geom_nodes_.size(); ++i)
166 {
167 int vertex_id = active_geom_nodes_(i);
168 for (int di = 0; di < active_dimensions_.size(); ++di)
169 {
170 int d = active_dimensions_(di);
171 active_term(i * active_dim_num + di) = term(vertex_id * dim_ + d);
172 }
173 }
174
175 assert(active_term.size() == para_out_dof());
176 return parametrization_.apply_jacobian(active_term, x);
177 }
178
183
185 {
186 Eigen::VectorXd x = Eigen::VectorXd::Zero(para_out_dof());
187 int active_dim_num = active_dimensions_.size();
188 for (int i = 0; i < active_geom_nodes_.size(); ++i)
189 {
190 Eigen::VectorXd p = states_[0]->mesh->point(active_geom_nodes_(i));
191 for (int di = 0; di < active_dimensions_.size(); ++di)
192 {
193 int d = active_dimensions_(di);
194 x(i * active_dim_num + di) = p(d);
195 }
196 }
197
199 }
200
201 Eigen::VectorXd ShapeVariableToSimulation::apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const
202 {
203 // Forms expect term to be full dof before any selection.
204 assert(term.size() == vertex_num_ * dim_);
205
206 Eigen::VectorXd active_term(para_out_dof());
207 int active_dim_num = active_dimensions_.size();
208 for (int i = 0; i < active_geom_nodes_.size(); ++i)
209 {
210 int vertex_id = active_geom_nodes_(i);
211 for (int di = 0; di < active_dimensions_.size(); ++di)
212 {
213 int d = active_dimensions_(di);
214 active_term(i * active_dim_num + di) = term(vertex_id * dim_ + d);
215 }
216 }
217 return parametrization_.apply_jacobian(active_term, x);
218 }
219
221 {
222 return active_dimensions_.size() * active_geom_nodes_.size();
223 }
224
225} // namespace polyfem::solver
int y
int x
main class that contains the polyfem solver and all its state
Definition State.hpp:114
Eigen::VectorXd apply_jacobian(const Eigen::VectorXd &grad_full, const Eigen::VectorXd &x) const override
Apply jacobian for chain rule.
Eigen::VectorXd inverse_eval(const Eigen::VectorXd &y) const override
Eval x = f^-1 (y).
int inverse_size(int y_size) const override
Compute DOF of x given DOF of y.
Eigen::VectorXd eval(const Eigen::VectorXd &x) const override
Eval y = f(x).
void update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const override
Update state variables from optimization variables.
int inverse_dof() const override
Compute optimization variables dof.
std::vector< std::shared_ptr< DiffCache > > DiffCachePtrs
std::vector< std::shared_ptr< legacy::State > > StatePtrs
bool affect_state(const legacy::State &target) const override
Return true if current var2sim maps to target state.
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Compute adjoint contribution of objective gradient.
Eigen::VectorXd inverse_eval() const override
Compute optimization variables from forward simulation legacy::State.
int para_out_dof() const
Return variable dof after parametrization mapping.
Eigen::VectorXd apply_parametrization_jacobian(const Eigen::VectorXd &term, const Eigen::VectorXd &x) const override
Apply parametrization jacobian to compute the gradient w.r.t.
ShapeVariableToSimulation(StatePtrs states, DiffCachePtrs diff_caches, CompositeParametrization parametrizations, Eigen::VectorXi active_dimensions, Eigen::VectorXi active_geom_nodes)
Construct ShapeVariableToSimulation.
void update(const Eigen::VectorXd &x) override
Update forward simulation states from optimization variables.
void dJ_shape_homogenization_adjoint_term(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &adjoint, Eigen::VectorXd &one_form)
void dJ_shape_static_adjoint_term(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &adjoint, Eigen::VectorXd &one_form)
void dJ_shape_transient_adjoint_term(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &adjoint_nu, const Eigen::MatrixXd &adjoint_p, Eigen::VectorXd &one_form)
bool is_active_geom_nodes_valid(const Eigen::VectorXi &active_geom_nodes, const std::vector< std::shared_ptr< legacy::State > > &states, std::string &reason)
Validate active geometry nodes selection given states.
bool is_active_dims_valid(const Eigen::VectorXi &active_dimensions, const std::vector< std::shared_ptr< legacy::State > > &states, std::string &reason)
Validate active dimensions selection given states.
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:79
Eigen::MatrixXd get_adjoint_mat(const legacy::State &state, const DiffCache &diff_cache, int type)
Get adjoint parameter nu or p.