PolyFEM
Loading...
Searching...
No Matches
PressureBoundaryVariableToSimulation.cpp
Go to the documentation of this file.
2
3#include <polyfem/Common.hpp>
11
12#include <Eigen/Core>
13
14#include <cassert>
15#include <string>
16#include <utility>
17#include <vector>
18#include <algorithm>
19
20namespace polyfem::solver
21{
22
24 StatePtrs states,
25 DiffCachePtrs diff_caches,
26 CompositeParametrization parametrizations,
27 Eigen::VectorXi active_boundary_ids,
28 Eigen::VectorXi active_time_slices)
29 : is_transient_(states[0]->problem->is_time_dependent()),
30 time_steps_(0),
31 states_(std::move(states)),
32 diff_caches_(std::move(diff_caches)),
33 parametrization_(std::move(parametrizations)),
34 active_boundary_ids_(std::move(active_boundary_ids)),
35 active_time_slices_(std::move(active_time_slices))
36 {
37 assert(!states_.empty());
38 assert(states_.size() == diff_caches_.size());
39
40 for (auto &s : states_)
41 {
42 if (s->problem->is_time_dependent() != is_transient_)
43 {
44 log_and_throw_adjoint_error("Fail to construct pressure boundary variable to simulation. Reason: inconsistent transient/static states.");
45 }
46 }
47
48 // time_step field might not be populated for static problem.
49 if (is_transient_)
50 {
51 time_steps_ = states_[0]->args["time"]["time_steps"].get<int>();
52 }
53
54 // Expand implicit all-active boundary id selection (keep JSON order; no sort/unique pass).
55 if (active_boundary_ids_.size() == 0)
56 {
57 json boundary_json = states_[0]->args["boundary_conditions"]["pressure_boundary"];
58 std::vector<int> tmp;
59 for (const json &bc : utils::json_as_array(boundary_json))
60 {
61 tmp.push_back(bc["id"].get<int>());
62 }
63
64 if (tmp.empty())
65 {
66 log_and_throw_adjoint_error("Fail to construct pressure boundary variable to simulation. Reason: No pressure boundary");
67 }
68
69 active_boundary_ids_ = Eigen::Map<Eigen::VectorXi>(tmp.data(), tmp.size());
70 }
71
72 // Expand implicit all-active time slice selection (transient only).
73 if (is_transient_ && active_time_slices_.size() == 0)
74 {
75 active_time_slices_ = Eigen::VectorXi::LinSpaced(time_steps_, 0, time_steps_ - 1);
76 }
77
78 // Validate expanded selections against every state.
79 std::string reason;
81 {
82 log_and_throw_adjoint_error("Fail to construct pressure boundary variable to simulation. Reason: {}", reason);
83 }
85 {
86 log_and_throw_adjoint_error("Fail to construct pressure boundary variable to simulation. Reason: {}", reason);
87 }
88 }
89
91 {
92 return "pressure";
93 }
94
99
101 {
102 for (auto &s : states_)
103 {
104 if (s.get() == &target)
105 {
106 return true;
107 }
108 }
109 return false;
110 }
111
113 {
114 Eigen::VectorXd y = parametrization_.eval(x);
115 assert(y.size() == para_out_dof());
116
117 for (auto &s : states_)
118 {
119 auto tensor_problem = std::dynamic_pointer_cast<polyfem::assembler::GenericTensorProblem>(s->problem);
120 if (!tensor_problem)
121 {
122 log_and_throw_adjoint_error("Only tensor problems are supported.");
123 }
124
125 if (is_transient_)
126 {
127 for (int ti = 0; ti < active_time_slices_.size(); ++ti)
128 {
129 int time_step = active_time_slices_(ti) + 1;
130 for (int bi = 0; bi < active_boundary_ids_.size(); ++bi)
131 {
132 int boundary_id = active_boundary_ids_(bi);
133 tensor_problem->update_pressure_boundary(boundary_id, time_step, y(ti * active_boundary_ids_.size() + bi));
134 }
135 }
136 }
137 else
138 {
139 for (int bi = 0; bi < active_boundary_ids_.size(); ++bi)
140 {
141 int boundary_id = active_boundary_ids_(bi);
142 tensor_problem->update_pressure_boundary(boundary_id, /*time_step=*/1, y(bi));
143 }
144 }
145 }
146 }
147
148 void PressureBoundaryVariableToSimulation::update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const
149 {
150 assert(state_variables.size() == para_out_dof());
151 state_variables = parametrization_.eval(x);
152 }
153
154 Eigen::VectorXd PressureBoundaryVariableToSimulation::compute_adjoint_term(const Eigen::VectorXd &x) const
155 {
156 Eigen::VectorXd term = Eigen::VectorXd::Zero(para_out_dof());
157 int bnum = active_boundary_ids_.size();
158
159 // AdjointTool helps take std::vector<int> instead of Eigen::Vector.
160 // Create temp to workaround this.
161 std::vector<int> tmp(active_boundary_ids_.data(), active_boundary_ids_.data() + bnum);
162
163 for (int si = 0; si < states_.size(); ++si)
164 {
165 auto &state = states_[si];
166 auto &diff_cache = diff_caches_[si];
167
168 if (is_transient_)
169 {
170 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
171 Eigen::MatrixXd adjoint_nu = get_adjoint_mat(*state, *diff_cache, 1);
172
173 Eigen::VectorXd cur_term;
174 AdjointTools::dJ_pressure_transient_adjoint_term(*state, *diff_cache, tmp, adjoint_nu, adjoint_p, cur_term);
175
176 assert(cur_term.size() == time_steps_ * bnum);
177 for (int ti = 0; ti < active_time_slices_.size(); ++ti)
178 {
179 int slice = active_time_slices_(ti);
180 term.segment(ti * bnum, bnum) += cur_term.segment(slice * bnum, bnum);
181 }
182 }
183 else
184 {
185 Eigen::MatrixXd adjoint_p = get_adjoint_mat(*state, *diff_cache, 0);
186 Eigen::VectorXd cur_term;
187 AdjointTools::dJ_pressure_static_adjoint_term(*state, tmp, diff_cache->u(0), adjoint_p, cur_term);
188 assert(cur_term.size() == bnum);
189 term += cur_term;
190 }
191 }
192
193 assert(term.size() == para_out_dof());
194 return parametrization_.apply_jacobian(term, x);
195 }
196
201
203 {
204 Eigen::VectorXd y = Eigen::VectorXd::Zero(para_out_dof());
205 int bnum = active_boundary_ids_.size();
206
207 json boundary_json = states_[0]->args["boundary_conditions"]["pressure_boundary"];
208 std::vector<json> boundaries = utils::json_as_array(boundary_json);
209
210 for (int bi = 0; bi < bnum; ++bi)
211 {
212 int boundary_id = active_boundary_ids_(bi);
213
214 auto pred = [boundary_id](const json &bc) { return bc["id"].get<int>() == boundary_id; };
215 auto iter = std::find_if(boundaries.begin(), boundaries.end(), pred);
216 if (iter == boundaries.end())
217 {
218 logger().warn("Cannot find pressure boundary id {} in JSON; falling back to zero.", boundary_id);
219 continue;
220 }
221
222 const json &value = (*iter)["value"];
223 if (is_transient_)
224 {
225 Eigen::VectorXd pressures;
226 try
227 {
228 pressures = value;
229 }
230 catch (std::exception &err)
231 {
232 }
233
234 int required = time_steps_ + 1;
235 if (pressures.size() != required)
236 {
237 logger().warn("Unsupported initial value spec for pressure boundary id {}; falling back to zero.", boundary_id);
238 pressures = Eigen::VectorXd::Zero(required);
239 }
240
241 for (int ti = 0; ti < active_time_slices_.size(); ++ti)
242 {
243 int slice = active_time_slices_(ti);
244 y(ti * bnum + bi) = pressures(slice + 1);
245 }
246 }
247 else
248 {
249 if (value.is_number())
250 {
251 y(bi) = value.get<double>();
252 }
253 else
254 {
255 logger().warn("Unsupported initial value spec for pressure boundary id {}; falling back to zero.", boundary_id);
256 }
257 }
258 }
259
261 }
262
263 Eigen::VectorXd PressureBoundaryVariableToSimulation::apply_parametrization_jacobian(const Eigen::VectorXd &, const Eigen::VectorXd &) const
264 {
265 // Not implemented because there's no user
266 log_and_throw_adjoint_error("apply_parametrization_jacobian is not implemented in {} variable to simulation.", name());
267 }
268
273
274} // 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(const Eigen::VectorXd &x) override
Update forward simulation states from optimization variables.
Eigen::VectorXd compute_adjoint_term(const Eigen::VectorXd &x) const override
Compute adjoint contribution of objective gradient.
int inverse_dof() const override
Compute optimization variables dof.
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.
void update_state_variables(const Eigen::VectorXd &x, Eigen::VectorXd &state_variables) const override
Update state variables from optimization variables.
Eigen::VectorXd inverse_eval() const override
Compute optimization variables from forward simulation legacy::State.
bool affect_state(const legacy::State &target) const override
Return true if current var2sim maps to target state.
PressureBoundaryVariableToSimulation(StatePtrs states, DiffCachePtrs diff_caches, CompositeParametrization parametrizations, Eigen::VectorXi active_boundary_ids, Eigen::VectorXi active_time_slices)
Construct ShapeVariableToSimulation.
void dJ_pressure_static_adjoint_term(const legacy::State &state, const std::vector< int > &boundary_ids, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &adjoint, Eigen::VectorXd &one_form)
void dJ_pressure_transient_adjoint_term(const legacy::State &state, const DiffCache &diff_cache, const std::vector< int > &boundary_ids, const Eigen::MatrixXd &adjoint_nu, const Eigen::MatrixXd &adjoint_p, Eigen::VectorXd &one_form)
bool is_active_time_slices_valid(const Eigen::VectorXi &active_time_slices, const std::vector< std::shared_ptr< legacy::State > > &states, std::string &reason)
Validate active time slices selection given states.
bool is_active_pressure_boundary_ids_valid(const Eigen::VectorXi &active_boundary_ids, const std::vector< std::shared_ptr< legacy::State > > &states, std::string &reason)
Validate active pressure boundary ids selection given states.
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
Definition JSONUtils.hpp:38
spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:44
nlohmann::json json
Definition Common.hpp:9
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.