PolyFEM
Loading...
Searching...
No Matches
StateDiff.cpp
Go to the documentation of this file.
2
4
8
11
12#include <polysolve/linear/FEMSolver.hpp>
13
18// Below types in SolverData are forward declared, include them explicitly.
24
26
27#include <ipc/ipc.hpp>
28#include <ipc/potentials/friction_potential.hpp>
29
30#include <Eigen/Dense>
31
32#include <algorithm>
33#include <vector>
34#include <cassert>
35#include <vector>
36
37using namespace polyfem::basis;
38
39namespace polyfem
40{
41 namespace
42 {
43 void replace_rows_by_identity(StiffnessMatrix &reduced_mat, const StiffnessMatrix &mat, const std::vector<int> &rows)
44 {
45 reduced_mat.resize(mat.rows(), mat.cols());
46
47 std::vector<bool> mask(mat.rows(), false);
48 for (int i : rows)
49 mask[i] = true;
50
51 std::vector<Eigen::Triplet<double>> coeffs;
52 for (int k = 0; k < mat.outerSize(); ++k)
53 {
54 for (StiffnessMatrix::InnerIterator it(mat, k); it; ++it)
55 {
56 if (mask[it.row()])
57 {
58 if (it.row() == it.col())
59 coeffs.emplace_back(it.row(), it.col(), 1.0);
60 }
61 else
62 coeffs.emplace_back(it.row(), it.col(), it.value());
63 }
64 }
65 reduced_mat.setFromTriplets(coeffs.begin(), coeffs.end());
66 }
67
68 void compute_force_jacobian_prev(const legacy::State &state, const DiffCache &diff_cache, const int force_step, const int sol_step, StiffnessMatrix &hessian_prev)
69 {
70 assert(force_step > 0);
71 assert(force_step > sol_step);
72
73 auto &s = state;
74
75 if (s.assembler->is_linear() && !s.is_contact_enabled())
76 {
77 hessian_prev = StiffnessMatrix(s.ndof(), s.ndof());
78 }
79 else
80 {
81 const Eigen::MatrixXd u = diff_cache.u(force_step);
82 const Eigen::MatrixXd u_prev = diff_cache.u(sol_step);
83 const double beta = time_integrator::BDF::betas(diff_cache.bdf_order(force_step) - 1);
84 const double dt = s.solve_data.time_integrator->dt();
85
86 hessian_prev = StiffnessMatrix(u.size(), u.size());
87 if (s.problem->is_time_dependent())
88 {
89 if (s.solve_data.friction_form)
90 {
91 if (sol_step == force_step - 1)
92 {
93 Eigen::MatrixXd surface_solution_prev = s.collision_mesh.vertices(utils::unflatten(u_prev, s.mesh->dimension()));
94 Eigen::MatrixXd surface_solution = s.collision_mesh.vertices(utils::unflatten(u, s.mesh->dimension()));
95
96 // TODO: use the time integration to compute the velocity
97 const Eigen::MatrixXd surface_velocities = (surface_solution - surface_solution_prev) / dt;
98 const double dv_dut = -1 / dt;
99
100 if (const auto barrier_contact = dynamic_cast<const solver::BarrierContactForm *>(s.solve_data.contact_form.get()))
101 {
102 ipc::BarrierPotential bp = barrier_contact->barrier_potential();
103 bp.set_stiffness(barrier_contact->barrier_stiffness());
104 hessian_prev =
105 s.solve_data.friction_form->friction_potential().force_jacobian(
106 diff_cache.friction_collision_set(force_step),
107 s.collision_mesh,
108 s.collision_mesh.rest_positions(),
109 /*lagged_displacements=*/surface_solution_prev,
110 surface_velocities,
111 bp,
112 ipc::FrictionPotential::DiffWRT::LAGGED_DISPLACEMENTS)
113 + s.solve_data.friction_form->friction_potential().force_jacobian(
114 diff_cache.friction_collision_set(force_step),
115 s.collision_mesh,
116 s.collision_mesh.rest_positions(),
117 /*lagged_displacements=*/surface_solution_prev,
118 surface_velocities,
119 bp,
120 ipc::FrictionPotential::DiffWRT::VELOCITIES)
121 * dv_dut;
122 }
123
124 hessian_prev *= -1;
125
126 // {
127 // Eigen::MatrixXd X = collision_mesh.rest_positions();
128 // Eigen::VectorXd x = utils::flatten(surface_solution_prev);
129 // const double barrier_stiffness = solve_data.contact_form->barrier_stiffness();
130 // const double dhat = solve_data.contact_form->dhat();
131 // const double mu = solve_data.friction_form->mu();
132 // const double epsv = solve_data.friction_form->epsv();
133
134 // Eigen::MatrixXd fgrad;
135 // fd::finite_jacobian(
136 // x, [&](const Eigen::VectorXd &y) -> Eigen::VectorXd
137 // {
138 // Eigen::MatrixXd fd_Ut = utils::unflatten(y, surface_solution_prev.cols());
139
140 // ipc::TangentialCollisions fd_friction_constraints;
141 // ipc::NormalCollisions fd_constraints;
142 // fd_constraints.set_use_convergent_formulation(solve_data.contact_form->use_convergent_formulation());
143 // fd_constraints.set_enable_shape_derivatives(true);
144 // fd_constraints.build(collision_mesh, X + fd_Ut, dhat);
145
146 // fd_friction_constraints.build(
147 // collision_mesh, X + fd_Ut, fd_constraints, dhat, barrier_stiffness,
148 // mu);
149
150 // return fd_friction_constraints.compute_potential_gradient(collision_mesh, (surface_solution - fd_Ut) / dt, epsv);
151
152 // }, fgrad, fd::AccuracyOrder::SECOND, 1e-8);
153
154 // logger().trace("force Ut derivative error {} {}", (fgrad - hessian_prev).norm(), hessian_prev.norm());
155 // }
156
157 hessian_prev = s.collision_mesh.to_full_dof(hessian_prev); // / (beta * dt) / (beta * dt);
158 }
159 else
160 {
161 // const double alpha = time_integrator::BDF::alphas(std::min(diff_cached.bdf_order(force_step), force_step) - 1)[force_step - sol_step - 1];
162 // Eigen::MatrixXd velocity = collision_mesh.map_displacements(utils::unflatten(diff_cached.v(force_step), collision_mesh.dim()));
163 // hessian_prev = diff_cached.friction_collision_set(force_step).compute_potential_hessian( //
164 // collision_mesh, velocity, solve_data.friction_form->epsv(), false) * (-alpha / beta / dt);
165
166 // hessian_prev = collision_mesh.to_full_dof(hessian_prev);
167 }
168 }
169
170 if (s.solve_data.tangential_adhesion_form)
171 {
172
173 if (sol_step == force_step - 1)
174 {
175 StiffnessMatrix adhesion_hessian_prev(u.size(), u.size());
176
177 Eigen::MatrixXd surface_solution_prev = s.collision_mesh.vertices(utils::unflatten(u_prev, s.mesh->dimension()));
178 Eigen::MatrixXd surface_solution = s.collision_mesh.vertices(utils::unflatten(u, s.mesh->dimension()));
179
180 // TODO: use the time integration to compute the velocity
181 const Eigen::MatrixXd surface_velocities = (surface_solution - surface_solution_prev) / dt;
182 const double dv_dut = -1 / dt;
183
184 adhesion_hessian_prev =
185 s.solve_data.tangential_adhesion_form->tangential_adhesion_potential().force_jacobian(
186 diff_cache.tangential_adhesion_collision_set(force_step),
187 s.collision_mesh,
188 s.collision_mesh.rest_positions(),
189 /*lagged_displacements=*/surface_solution_prev,
190 surface_velocities,
191 s.solve_data.normal_adhesion_form->normal_adhesion_potential(),
192 ipc::TangentialPotential::DiffWRT::LAGGED_DISPLACEMENTS)
193 + s.solve_data.tangential_adhesion_form->tangential_adhesion_potential().force_jacobian(
194 diff_cache.tangential_adhesion_collision_set(force_step),
195 s.collision_mesh,
196 s.collision_mesh.rest_positions(),
197 /*lagged_displacements=*/surface_solution_prev,
198 surface_velocities,
199 s.solve_data.normal_adhesion_form->normal_adhesion_potential(),
200 ipc::TangentialPotential::DiffWRT::VELOCITIES)
201 * dv_dut;
202
203 adhesion_hessian_prev *= -1;
204
205 adhesion_hessian_prev = s.collision_mesh.to_full_dof(adhesion_hessian_prev); // / (beta * dt) / (beta * dt);
206
207 hessian_prev += adhesion_hessian_prev;
208 }
209 }
210
211 if (s.damping_assembler->is_valid() && sol_step == force_step - 1) // velocity in damping uses BDF1
212 {
213 utils::SparseMatrixCache mat_cache;
214 StiffnessMatrix damping_hessian_prev(u.size(), u.size());
215 s.damping_prev_assembler->assemble_hessian(s.mesh->is_volume(), s.n_bases, false, s.bases, s.geom_bases(), s.ass_vals_cache, force_step * s.args["time"]["dt"].get<double>() + s.args["time"]["t0"].get<double>(), dt, u, u_prev, mat_cache, damping_hessian_prev);
216
217 hessian_prev += damping_hessian_prev;
218 }
219
220 if (sol_step == force_step - 1)
221 {
222 StiffnessMatrix body_force_hessian(u.size(), u.size());
223 s.solve_data.body_form->hessian_wrt_u_prev(u_prev, force_step * dt, body_force_hessian);
224 hessian_prev += body_force_hessian;
225 }
226 }
227 }
228 }
229
230 Eigen::MatrixXd solve_static_adjoint(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &adjoint_rhs)
231 {
232 auto &s = state;
233
234 Eigen::MatrixXd b = adjoint_rhs;
235
236 Eigen::MatrixXd adjoint;
237 if (s.static_linear_solver_cache)
238 {
239 b(s.boundary_nodes, Eigen::all).setZero();
240
241 StiffnessMatrix A = diff_cache.gradu_h(0);
242 b.conservativeResizeLike(Eigen::MatrixXd::Zero(A.rows(), b.cols()));
243
244 const std::vector<int> &boundary_nodes_tmp = s.boundary_nodes;
245
246 adjoint.setZero(s.ndof(), adjoint_rhs.cols());
247 for (int i = 0; i < b.cols(); i++)
248 {
249 Eigen::VectorXd x, tmp;
250 tmp = b.col(i);
251 dirichlet_solve_prefactorized(*s.static_linear_solver_cache, A, tmp, boundary_nodes_tmp, x);
252
253 adjoint.col(i) = x;
254 }
255 }
256 else
257 {
258 auto solver = polysolve::linear::Solver::create(s.args["solver"]["adjoint_linear"], adjoint_logger());
259
260 StiffnessMatrix A = diff_cache.gradu_h(0); // This should be transposed, but A is symmetric in hyper-elastic and diffusion problems
261
262 /*
263 For non-periodic problems, the adjoint solution p's size is the full size in NLProblem
264 For periodic problems, the adjoint solution p's size is the reduced size in NLProblem
265 */
266 if (!s.is_homogenization())
267 {
268 adjoint.setZero(s.ndof(), adjoint_rhs.cols());
269 for (int i = 0; i < b.cols(); i++)
270 {
271 Eigen::VectorXd tmp = b.col(i);
272 tmp(s.boundary_nodes).setZero();
273
274 Eigen::VectorXd x;
275 x.setZero(tmp.size());
276 dirichlet_solve(*solver, A, tmp, s.boundary_nodes, x, A.rows(), "", false, false, false);
277
278 adjoint.col(i) = x;
279 adjoint(s.boundary_nodes, i) = -b(s.boundary_nodes, i);
280 }
281 }
282 else
283 {
284 solver->analyze_pattern(A, A.rows());
285 solver->factorize(A);
286
287 adjoint.setZero(adjoint_rhs.rows(), adjoint_rhs.cols());
288 for (int i = 0; i < b.cols(); i++)
289 {
290 Eigen::MatrixXd tmp = b.col(i);
291
292 Eigen::VectorXd x;
293 x.setZero(tmp.size());
294 solver->solve(tmp, x);
295 x.conservativeResize(adjoint.rows());
296
297 adjoint.col(i) = x;
298 }
299 }
300 }
301
302 return adjoint;
303 }
304
305 Eigen::MatrixXd solve_transient_adjoint(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &adjoint_rhs)
306 {
307 auto &s = state;
308
309 const double dt = s.args["time"]["dt"];
310 const int time_steps = s.args["time"]["time_steps"];
311
312 int bdf_order = 1;
313 if (s.args["time"]["integrator"].is_string())
314 bdf_order = 1;
315 else if (s.args["time"]["integrator"]["type"] == "ImplicitEuler")
316 bdf_order = 1;
317 else if (s.args["time"]["integrator"]["type"] == "BDF")
318 bdf_order = s.args["time"]["integrator"]["steps"].get<int>();
319 else
320 log_and_throw_adjoint_error("Integrator type not supported for differentiability.");
321
322 assert(adjoint_rhs.cols() == time_steps + 1);
323
324 const int cols_per_adjoint = time_steps + 1;
325 Eigen::MatrixXd adjoints;
326 adjoints.setZero(s.ndof(), cols_per_adjoint * 2);
327
328 // set dirichlet rows of mass to identity
329 StiffnessMatrix reduced_mass;
330 replace_rows_by_identity(reduced_mass, s.mass, s.boundary_nodes);
331
332 Eigen::MatrixXd sum_alpha_p, sum_alpha_nu;
333 for (int i = time_steps; i >= 0; --i)
334 {
335 {
336 sum_alpha_p.setZero(s.ndof(), 1);
337 sum_alpha_nu.setZero(s.ndof(), 1);
338
339 const int num = std::min(bdf_order, time_steps - i);
340
341 Eigen::VectorXd bdf_coeffs = Eigen::VectorXd::Zero(num);
342 for (int j = 0; j < bdf_order && i + j < time_steps; ++j)
343 bdf_coeffs(j) = -time_integrator::BDF::alphas(std::min(bdf_order - 1, i + j))[j];
344
345 sum_alpha_p = adjoints.middleCols(i + 1, num) * bdf_coeffs;
346 sum_alpha_nu = adjoints.middleCols(cols_per_adjoint + i + 1, num) * bdf_coeffs;
347 }
348
349 Eigen::VectorXd rhs_ = -reduced_mass.transpose() * sum_alpha_nu - adjoint_rhs.col(i);
350 for (int j = 1; j <= bdf_order; j++)
351 {
352 if (i + j > time_steps)
353 break;
354
355 StiffnessMatrix gradu_h_prev;
356 compute_force_jacobian_prev(state, diff_cache, i + j, i, gradu_h_prev);
357 Eigen::VectorXd tmp = adjoints.col(i + j) * (time_integrator::BDF::betas(diff_cache.bdf_order(i + j) - 1) * dt);
358 tmp(s.boundary_nodes).setZero();
359 rhs_ += -gradu_h_prev.transpose() * tmp;
360 }
361
362 if (i > 0)
363 {
364 double beta_dt = time_integrator::BDF::betas(diff_cache.bdf_order(i) - 1) * dt;
365
366 rhs_ += (1. / beta_dt) * (diff_cache.gradu_h(i) - reduced_mass).transpose() * sum_alpha_p;
367
368 {
369 StiffnessMatrix A = diff_cache.gradu_h(i).transpose();
370 Eigen::VectorXd b_ = rhs_;
371 b_(s.boundary_nodes).setZero();
372
373 auto solver = polysolve::linear::Solver::create(s.args["solver"]["adjoint_linear"], adjoint_logger());
374
375 Eigen::VectorXd x;
376 dirichlet_solve(*solver, A, b_, s.boundary_nodes, x, A.rows(), "", false, false, false);
377 adjoints.col(i + cols_per_adjoint) = x;
378 }
379
380 // TODO: generalize to BDFn
381 Eigen::VectorXd tmp = rhs_(s.boundary_nodes);
382 if (i + 1 < cols_per_adjoint)
383 tmp += (-2. / beta_dt) * adjoints(s.boundary_nodes, i + 1);
384 if (i + 2 < cols_per_adjoint)
385 tmp += (1. / beta_dt) * adjoints(s.boundary_nodes, i + 2);
386
387 tmp -= (diff_cache.gradu_h(i).transpose() * adjoints.col(i + cols_per_adjoint))(s.boundary_nodes);
388 adjoints(s.boundary_nodes, i + cols_per_adjoint) = tmp;
389 adjoints.col(i) = beta_dt * adjoints.col(i + cols_per_adjoint) - sum_alpha_p;
390 }
391 else
392 {
393 adjoints.col(i) = -reduced_mass.transpose() * sum_alpha_p;
394 adjoints.col(i + cols_per_adjoint) = rhs_; // adjoint_nu[0] actually stores adjoint_mu[0]
395 }
396 }
397 return adjoints;
398 }
399
400 Eigen::MatrixXd solve_adjoint(const legacy::State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
401 {
402 if (state.problem->is_time_dependent())
403 return solve_transient_adjoint(state, diff_cache, rhs);
404 else
405 return solve_static_adjoint(state, diff_cache, rhs);
406 }
407 } // namespace
408
409 void solve_adjoint_cached(const legacy::State &state, DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
410 {
411 diff_cache.cache_adjoints(solve_adjoint(state, diff_cache, rhs));
412 }
413
421 Eigen::MatrixXd get_adjoint_mat(const legacy::State &state, const DiffCache &diff_cache, int type)
422 {
423 assert(diff_cache.adjoint_mat().size() > 0);
424
425 auto &s = state;
426
427 if (s.problem->is_time_dependent())
428 {
429 if (type == 0)
430 return diff_cache.adjoint_mat().leftCols(diff_cache.adjoint_mat().cols() / 2);
431 else if (type == 1)
432 return diff_cache.adjoint_mat().middleCols(diff_cache.adjoint_mat().cols() / 2, diff_cache.adjoint_mat().cols() / 2);
433 else
434 log_and_throw_adjoint_error("Invalid adjoint type!");
435 }
436
437 return diff_cache.adjoint_mat();
438 }
439
440 void compute_surface_node_ids(const legacy::State &state, const int surface_selection, std::vector<int> &node_ids)
441 {
442 auto &s = state;
443
444 node_ids = {};
445
446 const auto &gbases = s.geom_bases();
447 for (const auto &lb : s.total_local_boundary)
448 {
449 const int e = lb.element_id();
450 for (int i = 0; i < lb.size(); ++i)
451 {
452 const int primitive_global_id = lb.global_primitive_id(i);
453 const int boundary_id = s.mesh->get_boundary_id(primitive_global_id);
454 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *s.mesh);
455
456 if (boundary_id == surface_selection)
457 {
458 for (long n = 0; n < nodes.size(); ++n)
459 {
460 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
461
462 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
463 node_ids.push_back(g_id);
464 }
465 }
466 }
467 }
468 }
469
470 void compute_total_surface_node_ids(const legacy::State &state, std::vector<int> &node_ids)
471 {
472 auto &s = state;
473
474 node_ids = {};
475
476 const auto &gbases = s.geom_bases();
477 for (const auto &lb : s.total_local_boundary)
478 {
479 const int e = lb.element_id();
480 for (int i = 0; i < lb.size(); ++i)
481 {
482 const int primitive_global_id = lb.global_primitive_id(i);
483 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *s.mesh);
484
485 for (long n = 0; n < nodes.size(); ++n)
486 {
487 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
488
489 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
490 node_ids.push_back(g_id);
491 }
492 }
493 }
494 }
495
496 void compute_volume_node_ids(const legacy::State &state, const int volume_selection, std::vector<int> &node_ids)
497 {
498 auto &s = state;
499
500 node_ids = {};
501
502 const auto &gbases = s.geom_bases();
503 for (int e = 0; e < gbases.size(); e++)
504 {
505 const int body_id = s.mesh->get_body_id(e);
506 if (body_id == volume_selection)
507 for (const auto &gbs : gbases[e].bases)
508 for (const auto &g : gbs.global())
509 node_ids.push_back(g.index);
510 }
511 }
512
513} // namespace polyfem
int x
Storage for additional data required by differntial code.
Definition DiffCache.hpp:21
int bdf_order(int step) const
Definition DiffCache.hpp:48
const ipc::TangentialCollisions & friction_collision_set(int step) const
const Eigen::MatrixXd & adjoint_mat() const
Definition DiffCache.hpp:43
Eigen::VectorXd u(int step) const
Definition DiffCache.hpp:64
void cache_adjoints(const Eigen::MatrixXd &adjoint_mat)
const StiffnessMatrix & gradu_h(int step) const
Definition DiffCache.hpp:86
const ipc::TangentialCollisions & tangential_adhesion_collision_set(int step) const
main class that contains the polyfem solver and all its state
Definition State.hpp:113
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
Definition State.hpp:203
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
Definition State.hpp:263
json args
main input arguments containing all defaults
Definition State.hpp:135
static double betas(const int i)
Retrieve the value of beta used for BDF with i steps.
Definition BDF.cpp:36
static const std::vector< double > & alphas(const int i)
Retrieve the alphas used for BDF with i steps.
Definition BDF.cpp:22
list tmp
Definition p_bases.py:366
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
void compute_surface_node_ids(const legacy::State &state, const int surface_selection, std::vector< int > &node_ids)
void solve_adjoint_cached(const legacy::State &state, DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
void compute_volume_node_ids(const legacy::State &state, const int volume_selection, std::vector< int > &node_ids)
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:30
void compute_total_surface_node_ids(const legacy::State &state, std::vector< int > &node_ids)
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.
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:24