PolyFEM
Loading...
Searching...
No Matches
StateDiff.cpp
Go to the documentation of this file.
1#include <polyfem/State.hpp>
3
6
8#include <polysolve/linear/FEMSolver.hpp>
12
15
23
24#include <ipc/ipc.hpp>
25#include <ipc/barrier/barrier.hpp>
26#include <ipc/utils/local_to_global.hpp>
27#include <ipc/potentials/friction_potential.hpp>
28
29#include <Eigen/Dense>
30#include <unsupported/Eigen/SparseExtra>
31#include <deque>
32#include <map>
33#include <algorithm>
34
35#include <fstream>
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 } // namespace
68
69 void State::get_vertices(Eigen::MatrixXd &vertices) const
70 {
71 vertices.setZero(mesh->n_vertices(), mesh->dimension());
72
73 for (int v = 0; v < mesh->n_vertices(); v++)
74 vertices.row(v) = mesh->point(v);
75 }
76
77 void State::get_elements(Eigen::MatrixXi &elements) const
78 {
79 assert(mesh->is_simplicial());
80
81 auto node_to_primitive_map = node_to_primitive();
82
83 const auto &gbases = geom_bases();
84 int dim = mesh->dimension();
85 elements.setZero(gbases.size(), dim + 1);
86 for (int e = 0; e < gbases.size(); e++)
87 {
88 int i = 0;
89 for (const auto &gbs : gbases[e].bases)
90 elements(e, i++) = node_to_primitive_map[gbs.global()[0].index];
91 }
92 }
93
94 void State::set_mesh_vertex(int v_id, const Eigen::VectorXd &vertex)
95 {
96 assert(vertex.size() == mesh->dimension());
97 mesh->set_point(v_id, vertex);
98 }
99
100 void State::cache_transient_adjoint_quantities(const int current_step, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &disp_grad)
101 {
102 StiffnessMatrix gradu_h(sol.size(), sol.size());
103 if (current_step == 0)
104 diff_cached.init(mesh->dimension(), ndof(), problem->is_time_dependent() ? args["time"]["time_steps"].get<int>() : 0);
105
106 ipc::Collisions cur_collision_set;
107 ipc::FrictionCollisions cur_friction_set;
108
110 {
111 if (!problem->is_time_dependent() || current_step > 0)
112 compute_force_jacobian(sol, disp_grad, gradu_h);
113
114 cur_collision_set = solve_data.contact_form ? solve_data.contact_form->collision_set() : ipc::Collisions();
115 cur_friction_set = solve_data.friction_form ? solve_data.friction_form->friction_collision_set() : ipc::FrictionCollisions();
116 }
117 else
118 {
119 cur_collision_set = ipc::Collisions();
120 cur_friction_set = ipc::FrictionCollisions();
121 }
122
123 if (problem->is_time_dependent())
124 {
125 if (args["time"]["quasistatic"].get<bool>())
126 {
127 diff_cached.cache_quantities_quasistatic(current_step, sol, gradu_h, cur_collision_set, disp_grad);
128 }
129 else
130 {
131 Eigen::MatrixXd vel, acc;
132 if (current_step == 0)
133 {
134 if (dynamic_cast<time_integrator::BDF *>(solve_data.time_integrator.get()))
135 {
136 const auto bdf_integrator = dynamic_cast<time_integrator::BDF *>(solve_data.time_integrator.get());
137 vel = bdf_integrator->weighted_sum_v_prevs();
138 }
139 else if (dynamic_cast<time_integrator::ImplicitEuler *>(solve_data.time_integrator.get()))
140 {
141 const auto euler_integrator = dynamic_cast<time_integrator::ImplicitEuler *>(solve_data.time_integrator.get());
142 vel = euler_integrator->v_prev();
143 }
144 else
145 log_and_throw_error("Differentiable code doesn't support this time integrator!");
146
147 acc.setZero(ndof(), 1);
148 }
149 else
150 {
151 vel = solve_data.time_integrator->compute_velocity(sol);
152 acc = solve_data.time_integrator->compute_acceleration(vel);
153 }
154
155 diff_cached.cache_quantities_transient(current_step, solve_data.time_integrator->steps(), sol, vel, acc, gradu_h, cur_collision_set, cur_friction_set);
156 }
157 }
158 else
159 {
160 diff_cached.cache_quantities_static(sol, gradu_h, cur_collision_set, cur_friction_set, disp_grad);
161 }
162 }
163
164 void State::compute_force_jacobian(const Eigen::MatrixXd &sol, const Eigen::MatrixXd &disp_grad, StiffnessMatrix &hessian)
165 {
166 if (problem->is_time_dependent())
167 {
168 if (assembler->is_linear() && !is_contact_enabled())
169 log_and_throw_adjoint_error("Differentiable transient linear solve is not supported!");
170
171 StiffnessMatrix tmp_hess;
172 solve_data.nl_problem->set_project_to_psd(false);
173 solve_data.nl_problem->FullNLProblem::solution_changed(sol);
174 solve_data.nl_problem->FullNLProblem::hessian(sol, tmp_hess);
175 hessian.setZero();
176 replace_rows_by_identity(hessian, tmp_hess, boundary_nodes);
177 }
178 else // static formulation
179 {
180 if (assembler->is_linear() && !is_contact_enabled() && !is_homogenization())
181 {
182 hessian.setZero();
183 StiffnessMatrix stiffness;
184 build_stiffness_mat(stiffness);
185 replace_rows_by_identity(hessian, stiffness, boundary_nodes);
186 }
187 else
188 {
189 solve_data.nl_problem->set_project_to_psd(false);
190 Eigen::VectorXd reduced;
191 if (is_homogenization())
192 {
193 std::shared_ptr<solver::NLHomoProblem> homo_problem = std::dynamic_pointer_cast<solver::NLHomoProblem>(solve_data.nl_problem);
194 reduced = homo_problem->full_to_reduced(sol, disp_grad);
195 }
196 else
197 reduced = solve_data.nl_problem->full_to_reduced(sol);
198
199 solve_data.nl_problem->solution_changed(reduced);
200 solve_data.nl_problem->hessian(reduced, hessian);
201 }
202 }
203 }
204
205 void State::compute_force_jacobian_prev(const int force_step, const int sol_step, StiffnessMatrix &hessian_prev) const
206 {
207 assert(force_step > 0);
208 assert(force_step > sol_step);
209 if (assembler->is_linear() && !is_contact_enabled())
210 {
211 hessian_prev = StiffnessMatrix(ndof(), ndof());
212 }
213 else
214 {
215 const Eigen::MatrixXd u = diff_cached.u(force_step);
216 const Eigen::MatrixXd u_prev = diff_cached.u(sol_step);
217 const double beta = time_integrator::BDF::betas(diff_cached.bdf_order(force_step) - 1);
218 const double dt = solve_data.time_integrator->dt();
219
220 hessian_prev = StiffnessMatrix(u.size(), u.size());
221 if (problem->is_time_dependent())
222 {
224 {
225 if (sol_step == force_step - 1)
226 {
227 Eigen::MatrixXd surface_solution_prev = collision_mesh.vertices(utils::unflatten(u_prev, mesh->dimension()));
228 Eigen::MatrixXd surface_solution = collision_mesh.vertices(utils::unflatten(u, mesh->dimension()));
229
230 // TODO: use the time integration to compute the velocity
231 const Eigen::MatrixXd surface_velocities = (surface_solution - surface_solution_prev) / dt;
232 const double dv_dut = -1 / dt;
233
234 hessian_prev =
235 solve_data.friction_form->friction_potential().force_jacobian(
238 collision_mesh.rest_positions(),
239 /*lagged_displacements=*/surface_solution_prev,
240 surface_velocities,
241 solve_data.contact_form->barrier_potential(),
242 solve_data.contact_form->barrier_stiffness(),
243 ipc::FrictionPotential::DiffWRT::LAGGED_DISPLACEMENTS)
244 + solve_data.friction_form->friction_potential().force_jacobian(
247 collision_mesh.rest_positions(),
248 /*lagged_displacements=*/surface_solution_prev,
249 surface_velocities,
250 solve_data.contact_form->barrier_potential(),
251 solve_data.contact_form->barrier_stiffness(),
252 ipc::FrictionPotential::DiffWRT::VELOCITIES)
253 * dv_dut;
254
255 hessian_prev *= -1;
256
257 // {
258 // Eigen::MatrixXd X = collision_mesh.rest_positions();
259 // Eigen::VectorXd x = utils::flatten(surface_solution_prev);
260 // const double barrier_stiffness = solve_data.contact_form->barrier_stiffness();
261 // const double dhat = solve_data.contact_form->dhat();
262 // const double mu = solve_data.friction_form->mu();
263 // const double epsv = solve_data.friction_form->epsv();
264
265 // Eigen::MatrixXd fgrad;
266 // fd::finite_jacobian(
267 // x, [&](const Eigen::VectorXd &y) -> Eigen::VectorXd
268 // {
269 // Eigen::MatrixXd fd_Ut = utils::unflatten(y, surface_solution_prev.cols());
270
271 // ipc::FrictionCollisions fd_friction_constraints;
272 // ipc::Collisions fd_constraints;
273 // fd_constraints.set_use_convergent_formulation(solve_data.contact_form->use_convergent_formulation());
274 // fd_constraints.set_are_shape_derivatives_enabled(true);
275 // fd_constraints.build(collision_mesh, X + fd_Ut, dhat);
276
277 // fd_friction_constraints.build(
278 // collision_mesh, X + fd_Ut, fd_constraints, dhat, barrier_stiffness,
279 // mu);
280
281 // return fd_friction_constraints.compute_potential_gradient(collision_mesh, (surface_solution - fd_Ut) / dt, epsv);
282
283 // }, fgrad, fd::AccuracyOrder::SECOND, 1e-8);
284
285 // std::cout << "force Ut derivative error " << (fgrad - hessian_prev).norm() << " " << hessian_prev.norm() << "\n";
286 // }
287
288 hessian_prev = collision_mesh.to_full_dof(hessian_prev); // / (beta * dt) / (beta * dt);
289 }
290 else
291 {
292 // const double alpha = time_integrator::BDF::alphas(std::min(diff_cached.bdf_order(force_step), force_step) - 1)[force_step - sol_step - 1];
293 // Eigen::MatrixXd velocity = collision_mesh.map_displacements(utils::unflatten(diff_cached.v(force_step), collision_mesh.dim()));
294 // hessian_prev = diff_cached.friction_collision_set(force_step).compute_potential_hessian( //
295 // collision_mesh, velocity, solve_data.friction_form->epsv(), false) * (-alpha / beta / dt);
296
297 // hessian_prev = collision_mesh.to_full_dof(hessian_prev);
298 }
299 }
300
301 if (damping_assembler->is_valid() && sol_step == force_step - 1) // velocity in damping uses BDF1
302 {
303 utils::SparseMatrixCache mat_cache;
304 StiffnessMatrix damping_hessian_prev(u.size(), u.size());
305 damping_prev_assembler->assemble_hessian(mesh->is_volume(), n_bases, false, bases, geom_bases(), ass_vals_cache, force_step * args["time"]["dt"].get<double>() + args["time"]["t0"].get<double>(), dt, u, u_prev, mat_cache, damping_hessian_prev);
306
307 hessian_prev += damping_hessian_prev;
308 }
309
310 if (sol_step == force_step - 1)
311 {
312 StiffnessMatrix body_force_hessian(u.size(), u.size());
313 solve_data.body_form->hessian_wrt_u_prev(u_prev, force_step * dt, body_force_hessian);
314 hessian_prev += body_force_hessian;
315 }
316 }
317 }
318 }
319
320 void State::solve_adjoint_cached(const Eigen::MatrixXd &rhs)
321 {
323 }
324
325 Eigen::MatrixXd State::solve_adjoint(const Eigen::MatrixXd &rhs) const
326 {
327 if (problem->is_time_dependent())
329 else
331 }
332
333 Eigen::MatrixXd State::solve_static_adjoint(const Eigen::MatrixXd &adjoint_rhs) const
334 {
335 Eigen::MatrixXd b = adjoint_rhs;
336
337 Eigen::MatrixXd adjoint;
339 {
340 b(boundary_nodes, Eigen::all).setZero();
341
343 const int full_size = A.rows();
344 const int problem_dim = problem->is_scalar() ? 1 : mesh->dimension();
345 int precond_num = problem_dim * n_bases;
346
347 b.conservativeResizeLike(Eigen::MatrixXd::Zero(A.rows(), b.cols()));
348
349 std::vector<int> boundary_nodes_tmp;
350 if (has_periodic_bc())
351 {
352 boundary_nodes_tmp = periodic_bc->full_to_periodic(boundary_nodes);
353 precond_num = periodic_bc->full_to_periodic(A);
354 b = periodic_bc->full_to_periodic(b, true);
355 }
356 else
357 boundary_nodes_tmp = boundary_nodes;
358
359 adjoint.setZero(ndof(), adjoint_rhs.cols());
360 for (int i = 0; i < b.cols(); i++)
361 {
362 Eigen::VectorXd x, tmp;
363 tmp = b.col(i);
364 dirichlet_solve_prefactorized(*lin_solver_cached, A, tmp, boundary_nodes_tmp, x);
365
366 if (has_periodic_bc())
367 adjoint.col(i) = periodic_bc->periodic_to_full(full_size, x);
368 else
369 adjoint.col(i) = x;
370 }
371 }
372 else
373 {
374 auto solver = polysolve::linear::Solver::create(args["solver"]["adjoint_linear"], adjoint_logger());
375
376 StiffnessMatrix A = diff_cached.gradu_h(0); // This should be transposed, but A is symmetric in hyper-elastic and diffusion problems
377 solver->analyze_pattern(A, A.rows());
378 solver->factorize(A);
379
380 /*
381 For non-periodic problems, the adjoint solution p's size is the full size in NLProblem
382 For periodic problems, the adjoint solution p's size is the reduced size in NLProblem
383 */
384 if (!is_homogenization())
385 {
386 adjoint.setZero(ndof(), adjoint_rhs.cols());
387 for (int i = 0; i < b.cols(); i++)
388 {
389 Eigen::MatrixXd tmp = b.col(i);
390
391 Eigen::VectorXd x;
392 x.setZero(tmp.size());
393 solver->solve(tmp, x);
394
395 adjoint.col(i) = solve_data.nl_problem->reduced_to_full(x);
396 }
397 // NLProblem sets dirichlet values to forward BC values, but we want zero in adjoint
398 adjoint(boundary_nodes, Eigen::all).setZero();
399 }
400 else
401 {
402 adjoint.setZero(adjoint_rhs.rows(), adjoint_rhs.cols());
403 for (int i = 0; i < b.cols(); i++)
404 {
405 Eigen::MatrixXd tmp = b.col(i);
406
407 Eigen::VectorXd x;
408 x.setZero(tmp.size());
409 solver->solve(tmp, x);
410 x.conservativeResize(adjoint.rows());
411
412 adjoint.col(i) = x;
413 }
414 }
415 }
416
417 return adjoint;
418 }
419
420 Eigen::MatrixXd State::solve_transient_adjoint(const Eigen::MatrixXd &adjoint_rhs) const
421 {
422 const double dt = args["time"]["dt"];
423 const int time_steps = args["time"]["time_steps"];
424
425 int bdf_order = 1;
426 if (args["time"]["integrator"].is_string())
427 bdf_order = 1;
428 else if (args["time"]["integrator"]["type"] == "ImplicitEuler")
429 bdf_order = 1;
430 else if (args["time"]["integrator"]["type"] == "BDF")
431 bdf_order = args["time"]["integrator"]["steps"].get<int>();
432 else
433 log_and_throw_adjoint_error("Integrator type not supported for differentiability.");
434
435 assert(adjoint_rhs.cols() == time_steps + 1);
436
437 const int cols_per_adjoint = time_steps + 1;
438 Eigen::MatrixXd adjoints;
439 adjoints.setZero(ndof(), cols_per_adjoint * 2);
440
441 // set dirichlet rows of mass to identity
442 StiffnessMatrix reduced_mass;
443 replace_rows_by_identity(reduced_mass, mass, boundary_nodes);
444
445 Eigen::MatrixXd sum_alpha_p, sum_alpha_nu;
446 for (int i = time_steps; i >= 0; --i)
447 {
448 {
449 sum_alpha_p.setZero(ndof(), 1);
450 sum_alpha_nu.setZero(ndof(), 1);
451
452 const int num = std::min(bdf_order, time_steps - i);
453
454 Eigen::VectorXd bdf_coeffs = Eigen::VectorXd::Zero(num);
455 for (int j = 0; j < bdf_order && i + j < time_steps; ++j)
456 bdf_coeffs(j) = -time_integrator::BDF::alphas(std::min(bdf_order - 1, i + j))[j];
457
458 sum_alpha_p = adjoints.middleCols(i + 1, num) * bdf_coeffs;
459 sum_alpha_nu = adjoints.middleCols(cols_per_adjoint + i + 1, num) * bdf_coeffs;
460 }
461
462 Eigen::VectorXd rhs_ = -reduced_mass.transpose() * sum_alpha_nu - adjoint_rhs.col(i);
463 for (int j = 1; j <= bdf_order; j++)
464 {
465 if (i + j > time_steps)
466 break;
467
468 StiffnessMatrix gradu_h_prev;
469 compute_force_jacobian_prev(i + j, i, gradu_h_prev);
470 Eigen::VectorXd tmp = adjoints.col(i + j) * (time_integrator::BDF::betas(diff_cached.bdf_order(i + j) - 1) * dt);
471 tmp(boundary_nodes).setZero();
472 rhs_ += -gradu_h_prev.transpose() * tmp;
473 }
474
475 if (i > 0)
476 {
477 double beta_dt = time_integrator::BDF::betas(diff_cached.bdf_order(i) - 1) * dt;
478
479 rhs_ += (1. / beta_dt) * (diff_cached.gradu_h(i) - reduced_mass).transpose() * sum_alpha_p;
480
481 {
482 StiffnessMatrix A = diff_cached.gradu_h(i).transpose();
483 Eigen::VectorXd b_ = rhs_;
484 b_(boundary_nodes).setZero();
485
486 auto solver = polysolve::linear::Solver::create(args["solver"]["adjoint_linear"], adjoint_logger());
487
488 Eigen::VectorXd x;
489 dirichlet_solve(*solver, A, b_, boundary_nodes, x, A.rows(), "", false, false, false);
490 adjoints.col(i + cols_per_adjoint) = x;
491 }
492
493 // TODO: generalize to BDFn
494 Eigen::VectorXd tmp = rhs_(boundary_nodes);
495 if (i + 1 < cols_per_adjoint)
496 tmp += (-2. / beta_dt) * adjoints(boundary_nodes, i + 1);
497 if (i + 2 < cols_per_adjoint)
498 tmp += (1. / beta_dt) * adjoints(boundary_nodes, i + 2);
499
500 tmp -= (diff_cached.gradu_h(i).transpose() * adjoints.col(i + cols_per_adjoint))(boundary_nodes);
501 adjoints(boundary_nodes, i + cols_per_adjoint) = tmp;
502 adjoints.col(i) = beta_dt * adjoints.col(i + cols_per_adjoint) - sum_alpha_p;
503 }
504 else
505 {
506 adjoints.col(i) = -reduced_mass.transpose() * sum_alpha_p;
507 adjoints.col(i + cols_per_adjoint) = rhs_; // adjoint_nu[0] actually stores adjoint_mu[0]
508 }
509 }
510 return adjoints;
511 }
512
513 void State::compute_surface_node_ids(const int surface_selection, std::vector<int> &node_ids) const
514 {
515 node_ids = {};
516
517 const auto &gbases = geom_bases();
518 for (const auto &lb : total_local_boundary)
519 {
520 const int e = lb.element_id();
521 for (int i = 0; i < lb.size(); ++i)
522 {
523 const int primitive_global_id = lb.global_primitive_id(i);
524 const int boundary_id = mesh->get_boundary_id(primitive_global_id);
525 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *mesh);
526
527 if (boundary_id == surface_selection)
528 {
529 for (long n = 0; n < nodes.size(); ++n)
530 {
531 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
532
533 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
534 node_ids.push_back(g_id);
535 }
536 }
537 }
538 }
539 }
540
541 void State::compute_total_surface_node_ids(std::vector<int> &node_ids) const
542 {
543 node_ids = {};
544
545 const auto &gbases = geom_bases();
546 for (const auto &lb : total_local_boundary)
547 {
548 const int e = lb.element_id();
549 for (int i = 0; i < lb.size(); ++i)
550 {
551 const int primitive_global_id = lb.global_primitive_id(i);
552 const auto nodes = gbases[e].local_nodes_for_primitive(primitive_global_id, *mesh);
553
554 for (long n = 0; n < nodes.size(); ++n)
555 {
556 const int g_id = gbases[e].bases[nodes(n)].global()[0].index;
557
558 if (std::count(node_ids.begin(), node_ids.end(), g_id) == 0)
559 node_ids.push_back(g_id);
560 }
561 }
562 }
563 }
564
565 void State::compute_volume_node_ids(const int volume_selection, std::vector<int> &node_ids) const
566 {
567 node_ids = {};
568
569 const auto &gbases = geom_bases();
570 for (int e = 0; e < gbases.size(); e++)
571 {
572 const int body_id = mesh->get_body_id(e);
573 if (body_id == volume_selection)
574 for (const auto &gbs : gbases[e].bases)
575 for (const auto &g : gbs.global())
576 node_ids.push_back(g.index);
577 }
578 }
579
580} // namespace polyfem
int x
void get_vertices(Eigen::MatrixXd &vertices) const
Definition StateDiff.cpp:69
std::shared_ptr< utils::PeriodicBoundary > periodic_bc
periodic BC and periodic mesh utils
Definition State.hpp:389
int n_bases
number of bases
Definition State.hpp:178
void cache_transient_adjoint_quantities(const int current_step, const Eigen::MatrixXd &sol, const Eigen::MatrixXd &disp_grad)
assembler::AssemblyValsCache ass_vals_cache
used to store assembly values for small problems
Definition State.hpp:196
void set_mesh_vertex(int v_id, const Eigen::VectorXd &vertex)
Definition StateDiff.cpp:94
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
Definition State.hpp:223
std::shared_ptr< assembler::ViscousDamping > damping_assembler
Definition State.hpp:164
std::shared_ptr< assembler::Assembler > assembler
assemblers
Definition State.hpp:155
ipc::CollisionMesh collision_mesh
IPC collision mesh.
Definition State.hpp:515
solver::CacheLevel optimization_enabled
Definition State.hpp:647
bool is_homogenization() const
Definition State.hpp:711
Eigen::MatrixXd solve_static_adjoint(const Eigen::MatrixXd &adjoint_rhs) const
void compute_force_jacobian(const Eigen::MatrixXd &sol, const Eigen::MatrixXd &disp_grad, StiffnessMatrix &hessian)
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
Definition State.hpp:466
StiffnessMatrix mass
Mass matrix, it is computed only for time dependent problems.
Definition State.hpp:202
bool has_periodic_bc() const
Definition State.hpp:390
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
Definition State.hpp:168
std::vector< int > node_to_primitive() const
Definition State.cpp:235
json args
main input arguments containing all defaults
Definition State.hpp:101
solver::DiffCache diff_cached
Definition State.hpp:649
std::vector< basis::ElementBases > bases
FE bases, the size is #elements.
Definition State.hpp:171
void solve_adjoint_cached(const Eigen::MatrixXd &rhs)
Eigen::MatrixXd solve_adjoint(const Eigen::MatrixXd &rhs) const
void get_elements(Eigen::MatrixXi &elements) const
Definition StateDiff.cpp:77
void build_stiffness_mat(StiffnessMatrix &stiffness)
utility that builds the stiffness matrix and collects stats, used only for linear problems
void compute_volume_node_ids(const int volume_selection, std::vector< int > &node_ids) const
std::vector< mesh::LocalBoundary > total_local_boundary
mapping from elements to nodes for all mesh
Definition State.hpp:431
int ndof() const
Definition State.hpp:653
std::vector< int > boundary_nodes
list of boundary nodes
Definition State.hpp:427
solver::SolveData solve_data
timedependent stuff cached
Definition State.hpp:324
std::unique_ptr< polysolve::linear::Solver > lin_solver_cached
Definition State.hpp:651
std::shared_ptr< assembler::ViscousDampingPrev > damping_prev_assembler
Definition State.hpp:165
bool is_contact_enabled() const
does the simulation has contact
Definition State.hpp:551
Eigen::MatrixXd solve_transient_adjoint(const Eigen::MatrixXd &adjoint_rhs) const
void compute_force_jacobian_prev(const int force_step, const int sol_step, StiffnessMatrix &hessian_prev) const
void compute_total_surface_node_ids(std::vector< int > &node_ids) const
void compute_surface_node_ids(const int surface_selection, std::vector< int > &node_ids) const
Eigen::MatrixXd rhs
System right-hand side.
Definition State.hpp:207
const StiffnessMatrix & gradu_h(int step) const
int bdf_order(int step) const
void cache_quantities_quasistatic(const int cur_step, const Eigen::MatrixXd &u, const StiffnessMatrix &gradu_h, const ipc::Collisions &contact_set, const Eigen::MatrixXd &disp_grad)
Definition DiffCache.hpp:83
void init(const int dimension, const int ndof, const int n_time_steps=0)
Definition DiffCache.hpp:21
void cache_quantities_static(const Eigen::MatrixXd &u, const StiffnessMatrix &gradu_h, const ipc::Collisions &contact_set, const ipc::FrictionCollisions &friction_constraint_set, const Eigen::MatrixXd &disp_grad)
Definition DiffCache.hpp:40
void cache_adjoints(const Eigen::MatrixXd &adjoint_mat)
Definition DiffCache.hpp:98
void cache_quantities_transient(const int cur_step, const int cur_bdf_order, const Eigen::MatrixXd &u, const Eigen::MatrixXd &v, const Eigen::MatrixXd &acc, const StiffnessMatrix &gradu_h, const ipc::Collisions &collision_set, const ipc::FrictionCollisions &friction_collision_set)
Definition DiffCache.hpp:57
Eigen::VectorXd u(int step) const
const ipc::FrictionCollisions & friction_collision_set(int step) const
std::shared_ptr< solver::FrictionForm > friction_form
std::shared_ptr< solver::BodyForm > body_form
std::shared_ptr< solver::NLProblem > nl_problem
std::shared_ptr< solver::ContactForm > contact_form
std::shared_ptr< time_integrator::ImplicitTimeIntegrator > time_integrator
Backward Differential Formulas.
Definition BDF.hpp:14
static double betas(const int i)
Retrieve the value of beta used for BDF with i steps.
Definition BDF.cpp:35
Eigen::VectorXd weighted_sum_v_prevs() const
Compute the weighted sum of the previous velocities.
Definition BDF.cpp:62
static const std::vector< double > & alphas(const int i)
Retrieve the alphas used for BDF with i steps.
Definition BDF.cpp:21
Implicit Euler time integrator of a second order ODE (equivently a system of coupled first order ODEs...
const Eigen::VectorXd & v_prev() const
Get the most recent previous velocity value.
Eigen::MatrixXd unflatten(const Eigen::VectorXd &x, int dim)
Unflatten rowwises, so every dim elements in x become a row.
spdlog::logger & adjoint_logger()
Retrieves the current logger for adjoint.
Definition Logger.cpp:28
void log_and_throw_adjoint_error(const std::string &msg)
Definition Logger.cpp:77
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:71
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Definition Types.hpp:22