PolyFEM
Loading...
Searching...
No Matches
StateDiff.cpp
Go to the documentation of this file.
2
3#include <polyfem/State.hpp>
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 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 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 const int full_size = A.rows();
243 const int problem_dim = s.problem->is_scalar() ? 1 : s.mesh->dimension();
244 int precond_num = problem_dim * s.n_bases;
245
246 b.conservativeResizeLike(Eigen::MatrixXd::Zero(A.rows(), b.cols()));
247
248 std::vector<int> boundary_nodes_tmp;
249 if (s.has_periodic_bc())
250 {
251 boundary_nodes_tmp = s.periodic_bc->full_to_periodic(s.boundary_nodes);
252 precond_num = s.periodic_bc->full_to_periodic(A);
253 b = s.periodic_bc->full_to_periodic(b, true);
254 }
255 else
256 boundary_nodes_tmp = s.boundary_nodes;
257
258 adjoint.setZero(s.ndof(), adjoint_rhs.cols());
259 for (int i = 0; i < b.cols(); i++)
260 {
261 Eigen::VectorXd x, tmp;
262 tmp = b.col(i);
263 dirichlet_solve_prefactorized(*s.static_linear_solver_cache, A, tmp, boundary_nodes_tmp, x);
264
265 if (s.has_periodic_bc())
266 adjoint.col(i) = s.periodic_bc->periodic_to_full(full_size, x);
267 else
268 adjoint.col(i) = x;
269 }
270 }
271 else
272 {
273 auto solver = polysolve::linear::Solver::create(s.args["solver"]["adjoint_linear"], adjoint_logger());
274
275 StiffnessMatrix A = diff_cache.gradu_h(0); // This should be transposed, but A is symmetric in hyper-elastic and diffusion problems
276
277 /*
278 For non-periodic problems, the adjoint solution p's size is the full size in NLProblem
279 For periodic problems, the adjoint solution p's size is the reduced size in NLProblem
280 */
281 if (!s.is_homogenization())
282 {
283 adjoint.setZero(s.ndof(), adjoint_rhs.cols());
284 for (int i = 0; i < b.cols(); i++)
285 {
286 Eigen::VectorXd tmp = b.col(i);
287 tmp(s.boundary_nodes).setZero();
288
289 Eigen::VectorXd x;
290 x.setZero(tmp.size());
291 dirichlet_solve(*solver, A, tmp, s.boundary_nodes, x, A.rows(), "", false, false, false);
292
293 adjoint.col(i) = x;
294 adjoint(s.boundary_nodes, i) = -b(s.boundary_nodes, i);
295 }
296 }
297 else
298 {
299 solver->analyze_pattern(A, A.rows());
300 solver->factorize(A);
301
302 adjoint.setZero(adjoint_rhs.rows(), adjoint_rhs.cols());
303 for (int i = 0; i < b.cols(); i++)
304 {
305 Eigen::MatrixXd tmp = b.col(i);
306
307 Eigen::VectorXd x;
308 x.setZero(tmp.size());
309 solver->solve(tmp, x);
310 x.conservativeResize(adjoint.rows());
311
312 adjoint.col(i) = x;
313 }
314 }
315 }
316
317 return adjoint;
318 }
319
320 Eigen::MatrixXd solve_transient_adjoint(const State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &adjoint_rhs)
321 {
322 auto &s = state;
323
324 const double dt = s.args["time"]["dt"];
325 const int time_steps = s.args["time"]["time_steps"];
326
327 int bdf_order = 1;
328 if (s.args["time"]["integrator"].is_string())
329 bdf_order = 1;
330 else if (s.args["time"]["integrator"]["type"] == "ImplicitEuler")
331 bdf_order = 1;
332 else if (s.args["time"]["integrator"]["type"] == "BDF")
333 bdf_order = s.args["time"]["integrator"]["steps"].get<int>();
334 else
335 log_and_throw_adjoint_error("Integrator type not supported for differentiability.");
336
337 assert(adjoint_rhs.cols() == time_steps + 1);
338
339 const int cols_per_adjoint = time_steps + 1;
340 Eigen::MatrixXd adjoints;
341 adjoints.setZero(s.ndof(), cols_per_adjoint * 2);
342
343 // set dirichlet rows of mass to identity
344 StiffnessMatrix reduced_mass;
345 replace_rows_by_identity(reduced_mass, s.mass, s.boundary_nodes);
346
347 Eigen::MatrixXd sum_alpha_p, sum_alpha_nu;
348 for (int i = time_steps; i >= 0; --i)
349 {
350 {
351 sum_alpha_p.setZero(s.ndof(), 1);
352 sum_alpha_nu.setZero(s.ndof(), 1);
353
354 const int num = std::min(bdf_order, time_steps - i);
355
356 Eigen::VectorXd bdf_coeffs = Eigen::VectorXd::Zero(num);
357 for (int j = 0; j < bdf_order && i + j < time_steps; ++j)
358 bdf_coeffs(j) = -time_integrator::BDF::alphas(std::min(bdf_order - 1, i + j))[j];
359
360 sum_alpha_p = adjoints.middleCols(i + 1, num) * bdf_coeffs;
361 sum_alpha_nu = adjoints.middleCols(cols_per_adjoint + i + 1, num) * bdf_coeffs;
362 }
363
364 Eigen::VectorXd rhs_ = -reduced_mass.transpose() * sum_alpha_nu - adjoint_rhs.col(i);
365 for (int j = 1; j <= bdf_order; j++)
366 {
367 if (i + j > time_steps)
368 break;
369
370 StiffnessMatrix gradu_h_prev;
371 compute_force_jacobian_prev(state, diff_cache, i + j, i, gradu_h_prev);
372 Eigen::VectorXd tmp = adjoints.col(i + j) * (time_integrator::BDF::betas(diff_cache.bdf_order(i + j) - 1) * dt);
373 tmp(s.boundary_nodes).setZero();
374 rhs_ += -gradu_h_prev.transpose() * tmp;
375 }
376
377 if (i > 0)
378 {
379 double beta_dt = time_integrator::BDF::betas(diff_cache.bdf_order(i) - 1) * dt;
380
381 rhs_ += (1. / beta_dt) * (diff_cache.gradu_h(i) - reduced_mass).transpose() * sum_alpha_p;
382
383 {
384 StiffnessMatrix A = diff_cache.gradu_h(i).transpose();
385 Eigen::VectorXd b_ = rhs_;
386 b_(s.boundary_nodes).setZero();
387
388 auto solver = polysolve::linear::Solver::create(s.args["solver"]["adjoint_linear"], adjoint_logger());
389
390 Eigen::VectorXd x;
391 dirichlet_solve(*solver, A, b_, s.boundary_nodes, x, A.rows(), "", false, false, false);
392 adjoints.col(i + cols_per_adjoint) = x;
393 }
394
395 // TODO: generalize to BDFn
396 Eigen::VectorXd tmp = rhs_(s.boundary_nodes);
397 if (i + 1 < cols_per_adjoint)
398 tmp += (-2. / beta_dt) * adjoints(s.boundary_nodes, i + 1);
399 if (i + 2 < cols_per_adjoint)
400 tmp += (1. / beta_dt) * adjoints(s.boundary_nodes, i + 2);
401
402 tmp -= (diff_cache.gradu_h(i).transpose() * adjoints.col(i + cols_per_adjoint))(s.boundary_nodes);
403 adjoints(s.boundary_nodes, i + cols_per_adjoint) = tmp;
404 adjoints.col(i) = beta_dt * adjoints.col(i + cols_per_adjoint) - sum_alpha_p;
405 }
406 else
407 {
408 adjoints.col(i) = -reduced_mass.transpose() * sum_alpha_p;
409 adjoints.col(i + cols_per_adjoint) = rhs_; // adjoint_nu[0] actually stores adjoint_mu[0]
410 }
411 }
412 return adjoints;
413 }
414
415 Eigen::MatrixXd solve_adjoint(const State &state, const DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
416 {
417 if (state.problem->is_time_dependent())
418 return solve_transient_adjoint(state, diff_cache, rhs);
419 else
420 return solve_static_adjoint(state, diff_cache, rhs);
421 }
422 } // namespace
423
424 void solve_adjoint_cached(const State &state, DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
425 {
426 diff_cache.cache_adjoints(solve_adjoint(state, diff_cache, rhs));
427 }
428
436 Eigen::MatrixXd get_adjoint_mat(const State &state, const DiffCache &diff_cache, int type)
437 {
438 assert(diff_cache.adjoint_mat().size() > 0);
439
440 auto &s = state;
441
442 if (s.problem->is_time_dependent())
443 {
444 if (type == 0)
445 return diff_cache.adjoint_mat().leftCols(diff_cache.adjoint_mat().cols() / 2);
446 else if (type == 1)
447 return diff_cache.adjoint_mat().middleCols(diff_cache.adjoint_mat().cols() / 2, diff_cache.adjoint_mat().cols() / 2);
448 else
449 log_and_throw_adjoint_error("Invalid adjoint type!");
450 }
451
452 return diff_cache.adjoint_mat();
453 }
454
455 void compute_surface_node_ids(const State &state, const int surface_selection, std::vector<int> &node_ids)
456 {
457 auto &s = state;
458
459 node_ids = {};
460
461 const auto &gbases = s.geom_bases();
462 for (const auto &lb : s.total_local_boundary)
463 {
464 const int e = lb.element_id();
465 for (int i = 0; i < lb.size(); ++i)
466 {
467 const int primitive_global_id = lb.global_primitive_id(i);
468 const int boundary_id = s.mesh->get_boundary_id(primitive_global_id);
469 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *s.mesh);
470
471 if (boundary_id == surface_selection)
472 {
473 for (long n = 0; n < nodes.size(); ++n)
474 {
475 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
476
477 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
478 node_ids.push_back(g_id);
479 }
480 }
481 }
482 }
483 }
484
485 void compute_total_surface_node_ids(const State &state, std::vector<int> &node_ids)
486 {
487 auto &s = state;
488
489 node_ids = {};
490
491 const auto &gbases = s.geom_bases();
492 for (const auto &lb : s.total_local_boundary)
493 {
494 const int e = lb.element_id();
495 for (int i = 0; i < lb.size(); ++i)
496 {
497 const int primitive_global_id = lb.global_primitive_id(i);
498 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *s.mesh);
499
500 for (long n = 0; n < nodes.size(); ++n)
501 {
502 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
503
504 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
505 node_ids.push_back(g_id);
506 }
507 }
508 }
509 }
510
511 void compute_volume_node_ids(const State &state, const int volume_selection, std::vector<int> &node_ids)
512 {
513 auto &s = state;
514
515 node_ids = {};
516
517 const auto &gbases = s.geom_bases();
518 for (int e = 0; e < gbases.size(); e++)
519 {
520 const int body_id = s.mesh->get_body_id(e);
521 if (body_id == volume_selection)
522 for (const auto &gbs : gbases[e].bases)
523 for (const auto &g : gbs.global())
524 node_ids.push_back(g.index);
525 }
526 }
527
528} // 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
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
Definition State.hpp:257
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
Definition State.hpp:202
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:35
static const std::vector< double > & alphas(const int i)
Retrieve the alphas used for BDF with i steps.
Definition BDF.cpp:21
list tmp
Definition p_bases.py:365
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
void compute_volume_node_ids(const State &state, const int volume_selection, std::vector< int > &node_ids)
void compute_surface_node_ids(const State &state, const int surface_selection, std::vector< int > &node_ids)
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:30
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:79
Eigen::MatrixXd get_adjoint_mat(const State &state, const DiffCache &diff_cache, int type)
Get adjoint parameter nu or p.
void compute_total_surface_node_ids(const State &state, std::vector< int > &node_ids)
void solve_adjoint_cached(const State &state, DiffCache &diff_cache, const Eigen::MatrixXd &rhs)
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:24