72 if (state.
args[
"time"][
"integrator"].is_string())
74 if (state.
args[
"time"][
"integrator"][
"type"] ==
"ImplicitEuler")
76 if (state.
args[
"time"][
"integrator"][
"type"] ==
"BDF")
77 return state.
args[
"time"][
"integrator"][
"steps"].get<
int>();
83 double dot(
const Eigen::MatrixXd &A,
const Eigen::MatrixXd &B) {
return (A.array() * B.array()).sum(); }
85 class LocalThreadScalarStorage
92 LocalThreadScalarStorage()
98 class LocalThreadVecStorage
105 LocalThreadVecStorage(
const int size)
114 template <
typename T>
115 T triangle_area(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &
V)
117 Eigen::Matrix<T, Eigen::Dynamic, 1> l1 =
V.row(1) -
V.row(0);
118 Eigen::Matrix<T, Eigen::Dynamic, 1> l2 =
V.row(2) -
V.row(0);
119 T area = 0.5 * sqrt(pow(l1(1) * l2(2) - l1(2) * l2(1), 2) + pow(l1(0) * l2(2) - l1(2) * l2(0), 2) + pow(l1(1) * l2(0) - l1(0) * l2(1), 2));
123 Eigen::MatrixXd triangle_area_grad(
const Eigen::MatrixXd &
F)
126 Eigen::Matrix<Diff, Eigen::Dynamic, Eigen::Dynamic> full_diff(
F.rows(),
F.cols());
127 for (
int i = 0; i <
F.rows(); i++)
128 for (
int j = 0; j <
F.cols(); j++)
129 full_diff(i, j) =
Diff(i + j *
F.rows(),
F(i, j));
132 Eigen::MatrixXd
grad(
F.rows(),
F.cols());
133 for (
int i = 0; i <
F.rows(); ++i)
134 for (
int j = 0; j <
F.cols(); ++j)
135 grad(i, j) = reduced_diff.getGradient()(i + j *
F.rows());
140 template <
typename T>
141 T line_length(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &
V)
143 Eigen::Matrix<T, Eigen::Dynamic, 1> L =
V.row(1) -
V.row(0);
148 Eigen::MatrixXd line_length_grad(
const Eigen::MatrixXd &
F)
151 Eigen::Matrix<Diff, Eigen::Dynamic, Eigen::Dynamic> full_diff(
F.rows(),
F.cols());
152 for (
int i = 0; i <
F.rows(); i++)
153 for (
int j = 0; j <
F.cols(); j++)
154 full_diff(i, j) =
Diff(i + j *
F.rows(),
F(i, j));
155 auto reduced_diff = line_length(full_diff);
157 Eigen::MatrixXd
grad(
F.rows(),
F.cols());
158 for (
int i = 0; i <
F.rows(); ++i)
159 for (
int j = 0; j <
F.cols(); ++j)
160 grad(i, j) = reduced_diff.getGradient()(i + j *
F.rows());
165 template <
typename T>
166 Eigen::Matrix<T, 2, 1> edge_normal(
const Eigen::Matrix<T, 4, 1> &
V)
168 Eigen::Matrix<T, 2, 1> v1 =
V.segment(0, 2);
169 Eigen::Matrix<T, 2, 1> v2 =
V.segment(2, 2);
170 Eigen::Matrix<T, 2, 1> normal = v1 - v2;
172 normal = normal / normal.norm();
176 template <
typename T>
177 Eigen::Matrix<T, 3, 1> face_normal(
const Eigen::Matrix<T, 9, 1> &
V)
179 Eigen::Matrix<T, 3, 1> v1 =
V.segment(0, 3);
180 Eigen::Matrix<T, 3, 1> v2 =
V.segment(3, 3);
181 Eigen::Matrix<T, 3, 1> v3 =
V.segment(6, 3);
182 Eigen::Matrix<T, 3, 1> normal = (v2 - v1).
cross(v3 - v1);
183 normal = normal / normal.norm();
187 Eigen::MatrixXd extract_lame_params(
const std::map<std::string, Assembler::ParamFunc> &lame_params,
const int e,
const int t,
const Eigen::MatrixXd &local_pts,
const Eigen::MatrixXd &pts)
189 Eigen::MatrixXd params = Eigen::MatrixXd::Zero(local_pts.rows(), 2);
191 auto search_lambda = lame_params.find(
"lambda");
192 auto search_mu = lame_params.find(
"mu");
194 if (search_lambda == lame_params.end() || search_mu == lame_params.end())
197 for (
int p = 0; p < local_pts.rows(); p++)
199 params(p, 0) = search_lambda->second(local_pts.row(p), pts.row(p), t, e);
200 params(p, 1) = search_mu->second(local_pts.row(p), pts.row(p), t, e);
210 const Eigen::MatrixXd &solution,
211 const std::set<int> &interested_ids,
215 const auto &bases = state.
bases;
218 const int dim = state.
mesh->dimension();
219 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
220 const int n_elements = int(bases.size());
221 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
222 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
232 params.
t = dt * cur_step + t0;
233 params.
step = cur_step;
235 Eigen::MatrixXd u, grad_u;
236 Eigen::MatrixXd result;
238 for (
int e = start; e < end; ++e)
240 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
256 local_storage.val += dot(result, local_storage.da);
259 for (
const LocalThreadScalarStorage &local_storage : storage)
260 integral += local_storage.val;
266 LocalThreadScalarStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
269 Eigen::MatrixXd points, normal;
270 Eigen::VectorXd weights;
272 Eigen::MatrixXd u, grad_u;
273 Eigen::MatrixXd result;
274 IntegrableFunctional::ParameterType params;
275 params.t = dt * cur_step + t0;
276 params.step = cur_step;
278 for (int lb_id = start; lb_id < end; ++lb_id)
280 const auto &lb = state.total_local_boundary[lb_id];
281 const int e = lb.element_id();
283 for (int i = 0; i < lb.size(); i++)
285 const int global_primitive_id = lb.global_primitive_id(i);
286 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
289 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
291 assembler::ElementAssemblyValues &vals = local_storage.vals;
292 vals.compute(e, state.mesh->is_volume(), points, bases[e], gbases[e]);
293 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim, vals, solution, u, grad_u);
295 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
298 params.body_id = state.mesh->get_body_id(e);
299 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
300 j.evaluate(lame_params, points, vals.val, u, grad_u, normal, vals, params, result);
302 local_storage.val += dot(result, weights);
306 for (
const LocalThreadScalarStorage &local_storage : storage)
307 integral += local_storage.val;
311 std::vector<bool> traversed(state.
n_bases,
false);
313 params.
t = dt * cur_step + t0;
314 params.
step = cur_step;
315 for (
int e = 0; e < bases.size(); e++)
317 const auto &bs = bases[e];
318 for (
int i = 0; i < bs.bases.size(); i++)
320 const auto &b = bs.bases[i];
321 assert(b.global().size() == 1);
322 const auto &g = b.global()[0];
323 if (traversed[g.index])
326 const Eigen::MatrixXd lame_params = extract_lame_params(state.
assembler->parameters(), e, params.
t, Eigen::MatrixXd::Zero(1, dim) , g.node);
328 params.
node = g.index;
332 j.evaluate(lame_params, Eigen::MatrixXd::Zero(1, dim) , g.node, solution.block(g.index * dim, 0, dim, 1).transpose(), Eigen::MatrixXd::Zero(1, dim * actual_dim) , Eigen::MatrixXd::Zero(0, 0) ,
assembler::ElementAssemblyValues(), params,
val);
334 traversed[g.index] =
true;
342 void AdjointTools::compute_shape_derivative_functional_term(
344 const Eigen::MatrixXd &solution,
346 const std::set<int> &interested_ids,
348 Eigen::VectorXd &term,
349 const int cur_time_step)
352 const auto &bases = state.
bases;
353 const int dim = state.
mesh->dimension();
354 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
355 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
356 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
358 const int n_elements = int(bases.size());
361 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
363 if (spatial_integral_type == SpatialIntegralType::Volume)
365 utils::maybe_parallel_for(n_elements, [&](
int start,
int end,
int thread_id) {
366 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
368 Eigen::MatrixXd u, grad_u, j_val, dj_dgradu, dj_dx;
371 params.
t = cur_time_step * dt + t0;
372 params.
step = cur_time_step;
374 for (
int e = start; e < end; ++e)
376 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
381 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim,
vals, solution, u, grad_u);
402 Eigen::MatrixXd tau_q, grad_u_q;
403 for (
auto &v :
gvals.basis_values)
405 for (
int q = 0; q < local_storage.da.size(); ++q)
407 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += (j_val(q) * local_storage.da(q)) * v.grad_t_m.row(q).transpose();
410 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += (v.val(q) * local_storage.da(q)) * dj_dx.row(q).transpose();
414 if (dim == actual_dim)
421 tau_q = dj_dgradu.row(q);
422 grad_u_q = grad_u.row(q);
424 for (
int d = 0; d < dim; d++)
425 local_storage.vec(v.global[0].index * dim + d) += -dot(tau_q, grad_u_q.col(d) * v.grad_t_m.row(q)) * local_storage.da(q);
432 else if (spatial_integral_type == SpatialIntegralType::Surface)
434 utils::maybe_parallel_for(state.
total_local_boundary.size(), [&](
int start,
int end,
int thread_id) {
435 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
437 Eigen::MatrixXd uv, points, normal;
438 Eigen::VectorXd &weights = local_storage.da;
440 Eigen::MatrixXd u, grad_u, x, grad_x, j_val, dj_dgradu, dj_dgradx, dj_dx;
442 IntegrableFunctional::ParameterType params;
443 params.t = cur_time_step * dt + t0;
444 params.step = cur_time_step;
446 for (int lb_id = start; lb_id < end; ++lb_id)
448 const auto &lb = state.total_local_boundary[lb_id];
449 const int e = lb.element_id();
451 for (int i = 0; i < lb.size(); i++)
453 const int global_primitive_id = lb.global_primitive_id(i);
454 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
457 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
459 assembler::ElementAssemblyValues &vals = local_storage.vals;
460 io::Evaluator::interpolate_at_local_vals(*state.mesh, state.problem->is_scalar(), bases, gbases, e, points, solution, u, grad_u);
463 vals.compute(e, state.mesh->is_volume(), points, gbases[e], gbases[e]);
467 const int n_loc_bases_ = int(vals.basis_values.size());
469 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
472 params.body_id = state.mesh->get_body_id(e);
473 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
475 j.evaluate(lame_params, points, vals.val, u, grad_u, normal, vals, params, j_val);
476 j_val = j_val.array().colwise() * weights.array();
478 if (j.depend_on_gradu())
480 j.dj_dgradu(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu);
481 dj_dgradu = dj_dgradu.array().colwise() * weights.array();
484 if (j.depend_on_gradx())
486 j.dj_dgradx(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradx);
487 dj_dgradx = dj_dgradx.array().colwise() * weights.array();
492 j.dj_dx(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dx);
493 dj_dx = dj_dx.array().colwise() * weights.array();
496 const auto nodes = gbases[e].local_nodes_for_primitive(lb.global_primitive_id(i), *state.mesh);
498 if (nodes.size() != dim)
499 log_and_throw_adjoint_error(
"Only linear geometry is supported in differentiable surface integral functional!");
501 Eigen::MatrixXd velocity_div_mat;
502 if (state.mesh->is_volume())
505 for (int d = 0; d < 3; d++)
506 V.row(d) = gbases[e].bases[nodes(d)].global()[0].node;
507 velocity_div_mat = face_velocity_divergence(V);
512 for (int d = 0; d < 2; d++)
513 V.row(d) = gbases[e].bases[nodes(d)].global()[0].node;
514 velocity_div_mat = edge_velocity_divergence(V);
517 Eigen::MatrixXd grad_u_q, tau_q, grad_x_q;
518 for (long n = 0; n < nodes.size(); ++n)
520 const assembler::AssemblyValues &v = vals.basis_values[nodes(n)];
522 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += j_val.sum() * velocity_div_mat.row(n).transpose();
525 for (long n = 0; n < n_loc_bases_; ++n)
527 const assembler::AssemblyValues &v = vals.basis_values[n];
530 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += dj_dx.transpose() * v.val;
533 if (j.depend_on_gradu())
535 for (int q = 0; q < weights.size(); ++q)
537 if (dim == actual_dim)
539 vector2matrix(grad_u.row(q), grad_u_q);
540 vector2matrix(dj_dgradu.row(q), tau_q);
544 grad_u_q = grad_u.row(q);
545 tau_q = dj_dgradu.row(q);
548 for (int d = 0; d < dim; d++)
549 local_storage.vec(v.global[0].index * dim + d) += -dot(tau_q, grad_u_q.col(d) * v.grad_t_m.row(q));
553 if (j.depend_on_gradx())
555 for (int d = 0; d < dim; d++)
557 for (int q = 0; q < weights.size(); ++q)
558 local_storage.vec(v.global[0].index * dim + d) += dot(dj_dgradx.block(q, d * dim, 1, dim), v.grad.row(q));
566 else if (spatial_integral_type == SpatialIntegralType::VertexSum)
570 for (
const LocalThreadVecStorage &local_storage : storage)
571 term += local_storage.
vec;
573 term = utils::flatten(utils::unflatten(term, dim)(state.
primitive_to_node(), Eigen::all));
576 void AdjointTools::dJ_shape_static_adjoint_term(
579 const Eigen::MatrixXd &sol,
580 const Eigen::MatrixXd &adjoint,
581 Eigen::VectorXd &one_form)
583 Eigen::VectorXd elasticity_term, rhs_term, pressure_term, contact_term, adhesion_term;
586 Eigen::MatrixXd adjoint_zeroed = adjoint;
595 rhs_term.setZero(one_form.size());
603 pressure_term.setZero(one_form.size());
609 BarrierContactForceDerivative::force_shape_derivative(*barrier_contact, diff_cache.
collision_set(0), sol, adjoint_zeroed, contact_term);
613 SmoothContactForceDerivative::force_shape_derivative(*smooth_contact, diff_cache.
smooth_collision_set(0), sol, adjoint_zeroed, contact_term);
619 contact_term.setZero(elasticity_term.size());
628 adhesion_term.setZero(elasticity_term.size());
632 one_form -= elasticity_term + rhs_term + pressure_term + contact_term + adhesion_term;
633 one_form = utils::flatten(utils::unflatten(one_form, state.
mesh->dimension())(state.
primitive_to_node(), Eigen::all));
636 void AdjointTools::dJ_shape_homogenization_adjoint_term(
639 const Eigen::MatrixXd &sol,
640 const Eigen::MatrixXd &adjoint,
641 Eigen::VectorXd &one_form)
643 Eigen::VectorXd elasticity_term, contact_term, adhesion_term;
645 std::shared_ptr<NLHomoProblem> homo_problem = std::dynamic_pointer_cast<NLHomoProblem>(state.
solve_data.
nl_problem);
646 assert(homo_problem);
648 const int dim = state.
mesh->dimension();
651 const Eigen::MatrixXd affine_adjoint = homo_problem->reduced_to_disp_grad(adjoint,
true);
652 const Eigen::VectorXd full_adjoint = homo_problem->NLProblem::reduced_to_full(adjoint.topRows(homo_problem->reduced_size())) + io::Evaluator::generate_linear_field(state.
n_bases, state.
mesh_nodes, affine_adjoint);
660 BarrierContactForceDerivative::force_shape_derivative(*barrier_contact, diff_cache.
collision_set(0), sol, full_adjoint, contact_term);
664 SmoothContactForceDerivative::force_shape_derivative(*smooth_contact, diff_cache.
smooth_collision_set(0), sol, full_adjoint, contact_term);
670 contact_term.setZero(elasticity_term.size());
679 adhesion_term.setZero(elasticity_term.size());
682 one_form = -(elasticity_term + contact_term + adhesion_term);
684 Eigen::VectorXd force;
685 homo_problem->FullNLProblem::gradient(sol, force);
688 one_form = utils::flatten(utils::unflatten(one_form, dim)(state.
primitive_to_node(), Eigen::all));
691 void AdjointTools::dJ_periodic_shape_adjoint_term(
695 const Eigen::VectorXd &periodic_mesh_representation,
696 const Eigen::MatrixXd &sol,
697 const Eigen::MatrixXd &adjoint,
698 Eigen::VectorXd &one_form)
700 std::shared_ptr<NLHomoProblem> homo_problem = std::dynamic_pointer_cast<NLHomoProblem>(state.
solve_data.
nl_problem);
701 assert(homo_problem);
703 const Eigen::MatrixXd reduced_sol = homo_problem->full_to_reduced(sol, diff_cache.
disp_grad());
704 const Eigen::VectorXd extended_sol = homo_problem->reduced_to_extended(reduced_sol);
706 const Eigen::VectorXd extended_adjoint = homo_problem->reduced_to_extended(adjoint,
true);
707 const Eigen::MatrixXd affine_adjoint = homo_problem->reduced_to_disp_grad(adjoint,
true);
708 const Eigen::VectorXd full_adjoint = homo_problem->NLProblem::reduced_to_full(adjoint.topRows(homo_problem->reduced_size())) + io::Evaluator::generate_linear_field(state.
n_bases, state.
mesh_nodes, affine_adjoint);
710 const int dim = state.
mesh->dimension();
715 homo_problem->set_project_to_psd(
false);
716 homo_problem->FullNLProblem::hessian(sol, hessian);
717 Eigen::VectorXd partial_term = full_adjoint.transpose() * hessian;
719 one_form -= utils::flatten(utils::unflatten(partial_term, dim)(state.
primitive_to_node(), Eigen::all));
721 one_form = periodic_mesh_map.
apply_jacobian(one_form, periodic_mesh_representation);
725 Eigen::VectorXd contact_term;
728 one_form -= contact_term;
732 void AdjointTools::dJ_shape_transient_adjoint_term(
735 const Eigen::MatrixXd &adjoint_nu,
736 const Eigen::MatrixXd &adjoint_p,
737 Eigen::VectorXd &one_form)
739 const double t0 = state.
args[
"time"][
"t0"];
740 const double dt = state.
args[
"time"][
"dt"];
741 const int time_steps = state.
args[
"time"][
"time_steps"];
742 const int bdf_order = get_bdf_order(state);
744 Eigen::VectorXd elasticity_term, rhs_term, pressure_term, damping_term, mass_term, contact_term, friction_term, adhesion_term, tangential_adhesion_term;
747 Eigen::VectorXd cur_p, cur_nu;
748 for (
int i = time_steps; i > 0; --i)
750 const int real_order = std::min(bdf_order, i);
751 double beta = time_integrator::BDF::betas(real_order - 1);
752 double beta_dt = beta * dt;
753 const double t = i * dt + t0;
755 Eigen::MatrixXd velocity = diff_cache.
v(i);
757 cur_p = adjoint_p.col(i);
758 cur_nu = adjoint_nu.col(i);
763 InertiaForceDerivative::force_shape_derivative(*state.
solve_data.
inertia_form, state.
mesh->is_volume(), state.
n_geom_bases, t, state.
bases, state.
geom_bases(), *(state.
mass_matrix_assembler), state.
mass_ass_vals_cache, velocity, cur_nu, mass_term);
772 damping_term.setZero(mass_term.size());
778 BarrierContactForceDerivative::force_shape_derivative(*barrier_contact, diff_cache.
collision_set(i), diff_cache.
u(i), cur_p, contact_term);
782 SmoothContactForceDerivative::force_shape_derivative(*smooth_contact, diff_cache.
smooth_collision_set(i), diff_cache.
u(i), cur_p, contact_term);
788 contact_term.setZero(mass_term.size());
797 friction_term.setZero(mass_term.size());
806 adhesion_term.setZero(mass_term.size());
816 tangential_adhesion_term.setZero(mass_term.size());
819 one_form += beta_dt * (elasticity_term + rhs_term + pressure_term + damping_term + contact_term + friction_term + mass_term + adhesion_term + tangential_adhesion_term);
823 Eigen::VectorXd sum_alpha_p;
825 sum_alpha_p.setZero(adjoint_p.rows());
826 int num = std::min(bdf_order, time_steps);
827 for (
int j = 0; j < num; ++j)
829 int order = std::min(bdf_order - 1, j);
830 sum_alpha_p -= time_integrator::BDF::alphas(order)[j] * adjoint_p.col(j + 1);
834 InertiaForceDerivative::force_shape_derivative(*state.
solve_data.
inertia_form, state.
mesh->is_volume(), state.
n_geom_bases, t0, state.
bases, state.
geom_bases(), *(state.
mass_matrix_assembler), state.
mass_ass_vals_cache, diff_cache.
v(0), sum_alpha_p, mass_term);
836 one_form += mass_term;
838 one_form = utils::flatten(utils::unflatten(one_form, state.
mesh->dimension())(state.
primitive_to_node(), Eigen::all));
841 void AdjointTools::dJ_material_static_adjoint_term(
843 const Eigen::MatrixXd &sol,
844 const Eigen::MatrixXd &adjoint,
845 Eigen::VectorXd &one_form)
847 Eigen::MatrixXd adjoint_zeroed = adjoint;
849 ElasticForceDerivative::force_material_derivative(*state.
solve_data.
elastic_form, 0, sol, sol, adjoint_zeroed, one_form);
852 void AdjointTools::dJ_material_transient_adjoint_term(
855 const Eigen::MatrixXd &adjoint_nu,
856 const Eigen::MatrixXd &adjoint_p,
857 Eigen::VectorXd &one_form)
859 const double t0 = state.
args[
"time"][
"t0"];
860 const double dt = state.
args[
"time"][
"dt"];
861 const int time_steps = state.
args[
"time"][
"time_steps"];
862 const int bdf_order = get_bdf_order(state);
864 one_form.setZero(state.
bases.size() * 2);
866 auto storage = utils::create_thread_storage(LocalThreadVecStorage(one_form.size()));
868 utils::maybe_parallel_for(time_steps, [&](
int start,
int end,
int thread_id) {
869 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
870 Eigen::VectorXd elasticity_term;
871 for (
int i_aux = start; i_aux < end; ++i_aux)
873 const int i = time_steps - i_aux;
874 const int real_order = std::min(bdf_order, i);
875 double beta_dt = time_integrator::BDF::betas(real_order - 1) * dt;
877 Eigen::VectorXd cur_p = adjoint_p.col(i);
880 ElasticForceDerivative::force_material_derivative(*state.
solve_data.
elastic_form, t0 + dt * i, diff_cache.
u(i), diff_cache.
u(i - 1), -cur_p, elasticity_term);
881 local_storage.vec += beta_dt * elasticity_term;
885 for (
const LocalThreadVecStorage &local_storage : storage)
886 one_form += local_storage.vec;
889 void AdjointTools::dJ_friction_transient_adjoint_term(
892 const Eigen::MatrixXd &adjoint_nu,
893 const Eigen::MatrixXd &adjoint_p,
894 Eigen::VectorXd &one_form)
896 const double dt = state.
args[
"time"][
"dt"];
898 const int time_steps = state.
args[
"time"][
"time_steps"];
899 const int dim = state.
mesh->dimension();
900 const int bdf_order = get_bdf_order(state);
904 std::shared_ptr<time_integrator::ImplicitTimeIntegrator> time_integrator =
905 time_integrator::ImplicitTimeIntegrator::construct_time_integrator(state.
args[
"time"][
"integrator"]);
907 Eigen::MatrixXd solution, velocity, acceleration;
915 solution = diff_cache.
u(0);
918 const double dt = state.
args[
"time"][
"dt"];
919 time_integrator->init(solution, velocity, acceleration, dt);
922 for (
int t = 1; t <= time_steps; ++t)
924 const int real_order = std::min(bdf_order, t);
925 double beta = time_integrator::BDF::betas(real_order - 1);
927 const Eigen::MatrixXd surface_solution_prev = state.
collision_mesh.vertices(utils::unflatten(diff_cache.
u(t - 1), dim));
930 const Eigen::MatrixXd surface_velocities = state.
collision_mesh.map_displacements(utils::unflatten(time_integrator->compute_velocity(diff_cache.
u(t)), state.
collision_mesh.dim()));
931 time_integrator->update_quantities(diff_cache.
u(t));
940 surface_solution_prev,
942 barrier_contact->barrier_potential(),
943 barrier_contact->barrier_stiffness(),
946 Eigen::VectorXd cur_p = adjoint_p.col(t);
949 one_form(0) += dot(cur_p, force) * beta * dt;
954 void AdjointTools::dJ_damping_transient_adjoint_term(
957 const Eigen::MatrixXd &adjoint_nu,
958 const Eigen::MatrixXd &adjoint_p,
959 Eigen::VectorXd &one_form)
961 const double t0 = state.
args[
"time"][
"t0"];
962 const double dt = state.
args[
"time"][
"dt"];
963 const int time_steps = state.
args[
"time"][
"time_steps"];
964 const int bdf_order = get_bdf_order(state);
968 auto storage = utils::create_thread_storage(LocalThreadVecStorage(one_form.size()));
970 utils::maybe_parallel_for(time_steps, [&](
int start,
int end,
int thread_id) {
971 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
972 Eigen::VectorXd damping_term;
973 for (
int t_aux = start; t_aux < end; ++t_aux)
975 const int t = time_steps - t_aux;
976 const int real_order = std::min(bdf_order, t);
977 const double beta = time_integrator::BDF::betas(real_order - 1);
979 Eigen::VectorXd cur_p = adjoint_p.col(t);
982 ElasticForceDerivative::force_material_derivative(*state.
solve_data.
damping_form, t * dt + t0, diff_cache.
u(t), diff_cache.
u(t - 1), -cur_p, damping_term);
983 local_storage.vec += (beta * dt) * damping_term;
987 for (
const LocalThreadVecStorage &local_storage : storage)
988 one_form += local_storage.vec;
991 void AdjointTools::dJ_initial_condition_adjoint_term(
993 const Eigen::MatrixXd &adjoint_nu,
994 const Eigen::MatrixXd &adjoint_p,
995 Eigen::VectorXd &one_form)
997 const int ndof = state.
ndof();
998 one_form.setZero(ndof * 2);
1001 one_form.segment(0, ndof) = -adjoint_nu.col(0);
1002 one_form.segment(ndof, ndof) = -adjoint_p.col(0);
1007 one_form(ndof + b) = 0;
1011 void AdjointTools::dJ_dirichlet_static_adjoint_term(
1014 const Eigen::MatrixXd &adjoint,
1015 Eigen::VectorXd &one_form)
1020 gradd_h.prune([&boundary_nodes_set](
const Eigen::Index &row,
const Eigen::Index &col,
const FullNLProblem::Scalar &value) {
1023 if (boundary_nodes_set.find(row) == boundary_nodes_set.end())
1027 one_form.setZero(state.
ndof());
1030 one_form = utils::flatten(utils::unflatten(one_form, state.
mesh->dimension())(state.
primitive_to_node(), Eigen::all));
1033 void AdjointTools::dJ_dirichlet_transient_adjoint_term(
1035 const Eigen::MatrixXd &adjoint_nu,
1036 const Eigen::MatrixXd &adjoint_p,
1037 Eigen::VectorXd &one_form)
1039 const double dt = state.
args[
"time"][
"dt"];
1040 const int time_steps = state.
args[
"time"][
"time_steps"];
1041 const int bdf_order = get_bdf_order(state);
1046 one_form.setZero(time_steps * n_dirichlet_dof);
1047 for (
int i = 1; i <= time_steps; ++i)
1049 const int real_order = std::min(bdf_order, i);
1050 const double beta_dt = time_integrator::BDF::betas(real_order - 1) * dt;
1052 one_form.segment((i - 1) * n_dirichlet_dof, n_dirichlet_dof) = -(1. / beta_dt) * adjoint_p(state.
boundary_nodes, i);
1056 void AdjointTools::dJ_pressure_static_adjoint_term(
1058 const std::vector<int> &boundary_ids,
1059 const Eigen::MatrixXd &sol,
1060 const Eigen::MatrixXd &adjoint,
1061 Eigen::VectorXd &one_form)
1063 const int n_pressure_dof = boundary_ids.size();
1065 one_form.setZero(n_pressure_dof);
1067 for (
int i = 0; i < boundary_ids.size(); ++i)
1069 double pressure_term = PressureForceDerivative::force_pressure_derivative(
1076 one_form(i) = pressure_term;
1080 void AdjointTools::dJ_pressure_transient_adjoint_term(
1083 const std::vector<int> &boundary_ids,
1084 const Eigen::MatrixXd &adjoint_nu,
1085 const Eigen::MatrixXd &adjoint_p,
1086 Eigen::VectorXd &one_form)
1088 const double t0 = state.
args[
"time"][
"t0"];
1089 const double dt = state.
args[
"time"][
"dt"];
1090 const int time_steps = state.
args[
"time"][
"time_steps"];
1091 const int bdf_order = get_bdf_order(state);
1093 const int n_pressure_dof = boundary_ids.size();
1095 one_form.setZero(time_steps * n_pressure_dof);
1096 Eigen::VectorXd cur_p, cur_nu;
1097 for (
int i = time_steps; i > 0; --i)
1099 const int real_order = std::min(bdf_order, i);
1100 double beta = time_integrator::BDF::betas(real_order - 1);
1101 double beta_dt = beta * dt;
1102 const double t = i * dt + t0;
1104 cur_p = adjoint_p.col(i);
1105 cur_nu = adjoint_nu.col(i);
1109 for (
int b = 0; b < boundary_ids.size(); ++b)
1111 double pressure_term = PressureForceDerivative::force_pressure_derivative(
1118 one_form((i - 1) * n_pressure_dof + b) = -beta_dt * pressure_term;
1123 void AdjointTools::dJ_du_step(
1126 const Eigen::MatrixXd &solution,
1127 const std::set<int> &interested_ids,
1130 Eigen::VectorXd &term)
1132 const auto &bases = state.
bases;
1135 const int dim = state.
mesh->dimension();
1136 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
1137 const int n_elements = int(bases.size());
1138 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
1139 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
1141 term = Eigen::MatrixXd::Zero(state.
n_bases * actual_dim, 1);
1146 if (spatial_integral_type == SpatialIntegralType::Volume)
1148 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
1149 utils::maybe_parallel_for(n_elements, [&](
int start,
int end,
int thread_id) {
1150 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
1152 Eigen::MatrixXd u, grad_u;
1153 Eigen::MatrixXd lambda, mu;
1154 Eigen::MatrixXd dj_du, dj_dgradu, dj_dgradx;
1157 params.
t = dt * cur_step + t0;
1158 params.
step = cur_step;
1160 for (
int e = start; e < end; ++e)
1162 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
1173 const int n_loc_bases_ = int(
vals.basis_values.size());
1175 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim,
vals, solution, u, grad_u);
1180 dj_dgradu.resize(0, 0);
1184 for (
int q = 0; q < dj_dgradu.rows(); q++)
1185 dj_dgradu.row(q) *= local_storage.da(q);
1192 for (
int q = 0; q < dj_du.rows(); q++)
1193 dj_du.row(q) *= local_storage.da(q);
1196 for (
int i = 0; i < n_loc_bases_; ++i)
1199 assert(v.
global.size() == 1);
1200 for (
int d = 0; d < actual_dim; d++)
1207 for (
int q = 0; q < local_storage.da.size(); ++q)
1208 val += dot(dj_dgradu.block(q, d * dim, 1, dim), v.
grad_t_m.row(q));
1214 for (
int q = 0; q < local_storage.da.size(); ++q)
1215 val += dj_du(q, d) * v.
val(q);
1217 local_storage.vec(v.
global[0].index * actual_dim + d) +=
val;
1222 for (
const LocalThreadVecStorage &local_storage : storage)
1223 term += local_storage.vec;
1225 else if (spatial_integral_type == SpatialIntegralType::Surface)
1227 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
1228 utils::maybe_parallel_for(state.
total_local_boundary.size(), [&](
int start,
int end,
int thread_id) {
1229 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
1231 Eigen::MatrixXd uv, samples, gtmp;
1232 Eigen::MatrixXd points, normal;
1233 Eigen::VectorXd weights;
1235 Eigen::MatrixXd u, grad_u;
1236 Eigen::MatrixXd lambda, mu;
1237 Eigen::MatrixXd dj_du, dj_dgradu, dj_dgradu_local;
1239 IntegrableFunctional::ParameterType params;
1240 params.t = dt * cur_step + t0;
1241 params.step = cur_step;
1243 for (int lb_id = start; lb_id < end; ++lb_id)
1245 const auto &lb = state.total_local_boundary[lb_id];
1246 const int e = lb.element_id();
1248 for (int i = 0; i < lb.size(); i++)
1250 const int global_primitive_id = lb.global_primitive_id(i);
1251 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
1254 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
1256 assembler::ElementAssemblyValues &vals = local_storage.vals;
1257 vals.compute(e, state.mesh->is_volume(), points, bases[e], gbases[e]);
1258 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim, vals, solution, u, grad_u);
1260 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
1264 const int n_loc_bases_ = int(vals.basis_values.size());
1267 params.body_id = state.mesh->get_body_id(e);
1268 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
1270 dj_dgradu.resize(0, 0);
1271 if (j.depend_on_gradu())
1273 j.dj_dgradu(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu);
1274 for (int q = 0; q < dj_dgradu.rows(); q++)
1275 dj_dgradu.row(q) *= weights(q);
1278 dj_dgradu_local.resize(0, 0);
1279 if (j.depend_on_gradu_local())
1281 j.dj_dgradu_local(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu_local);
1282 for (int q = 0; q < dj_dgradu_local.rows(); q++)
1283 dj_dgradu_local.row(q) *= weights(q);
1287 if (j.depend_on_u())
1289 j.dj_du(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_du);
1290 for (int q = 0; q < dj_du.rows(); q++)
1291 dj_du.row(q) *= weights(q);
1294 for (int l = 0; l < lb.size(); ++l)
1296 const auto nodes = bases[e].local_nodes_for_primitive(lb.global_primitive_id(l), *state.mesh);
1298 for (long n = 0; n < nodes.size(); ++n)
1300 const assembler::AssemblyValues &v = vals.basis_values[nodes(n)];
1301 assert(v.global.size() == 1);
1302 for (int d = 0; d < actual_dim; d++)
1307 if (j.depend_on_gradu())
1309 for (int q = 0; q < weights.size(); ++q)
1310 val += dot(dj_dgradu.block(q, d * dim, 1, dim), v.grad_t_m.row(q));
1313 if (j.depend_on_gradu_local())
1315 for (int q = 0; q < weights.size(); ++q)
1316 val += dot(dj_dgradu_local.block(q, d * dim, 1, dim), v.grad.row(q));
1319 if (j.depend_on_u())
1321 for (int q = 0; q < weights.size(); ++q)
1322 val += dj_du(q, d) * v.val(q);
1324 local_storage.vec(v.global[0].index * actual_dim + d) += val;
1331 for (
const LocalThreadVecStorage &local_storage : storage)
1332 term += local_storage.vec;
1334 else if (spatial_integral_type == SpatialIntegralType::VertexSum)
1336 std::vector<bool> traversed(state.
n_bases,
false);
1338 params.
t = dt * cur_step + t0;
1339 params.
step = cur_step;
1340 for (
int e = 0; e < bases.size(); e++)
1342 const auto &bs = bases[e];
1343 for (
int i = 0; i < bs.bases.size(); i++)
1345 const auto &b = bs.bases[i];
1346 assert(b.global().size() == 1);
1347 const auto &g = b.global()[0];
1348 if (traversed[g.index])
1351 const Eigen::MatrixXd lame_params = extract_lame_params(state.
assembler->parameters(), e, params.
t, Eigen::MatrixXd::Zero(1, dim) , g.node);
1353 params.
node = g.index;
1356 Eigen::MatrixXd
val;
1357 j.dj_du(lame_params, Eigen::MatrixXd::Zero(1, dim) , g.node, solution.block(g.index * dim, 0, dim, 1).transpose(), Eigen::MatrixXd::Zero(1, dim * actual_dim) , Eigen::MatrixXd::Zero(0, 0) ,
assembler::ElementAssemblyValues(), params,
val);
1358 term.block(g.index * actual_dim, 0, actual_dim, 1) +=
val.transpose();
1359 traversed[g.index] =
true;
1365 Eigen::VectorXd AdjointTools::map_primitive_to_node_order(
const State &state,
const Eigen::VectorXd &primitives)
1367 int dim = state.
mesh->dimension();
1368 assert(primitives.size() == (state.
n_geom_bases * dim));
1369 Eigen::VectorXd nodes(primitives.size());
1372 nodes.segment(map[v] * dim, dim) = primitives.segment(v * dim, dim);
1376 Eigen::VectorXd AdjointTools::map_node_to_primitive_order(
const State &state,
const Eigen::VectorXd &nodes)
1378 int dim = state.
mesh->dimension();
1380 Eigen::VectorXd primitives(nodes.size());
1383 primitives.segment(map[v] * dim, dim) = nodes.segment(v * dim, dim);
1387 Eigen::MatrixXd AdjointTools::edge_normal_gradient(
const Eigen::MatrixXd &
V)
1390 Eigen::Matrix<Diff, 4, 1> full_diff(4, 1);
1391 for (
int i = 0; i < 2; i++)
1392 for (
int j = 0; j < 2; j++)
1393 full_diff(i * 2 + j) =
Diff(i * 2 + j,
V(i, j));
1394 auto reduced_diff = edge_normal(full_diff);
1396 Eigen::MatrixXd grad(2, 4);
1397 for (
int i = 0; i < 2; ++i)
1398 grad.row(i) = reduced_diff[i].getGradient();
1403 Eigen::MatrixXd AdjointTools::face_normal_gradient(
const Eigen::MatrixXd &
V)
1406 Eigen::Matrix<Diff, 9, 1> full_diff(9, 1);
1407 for (
int i = 0; i < 3; i++)
1408 for (
int j = 0; j < 3; j++)
1409 full_diff(i * 3 + j) =
Diff(i * 3 + j,
V(i, j));
1410 auto reduced_diff = face_normal(full_diff);
1412 Eigen::MatrixXd grad(3, 9);
1413 for (
int i = 0; i < 3; ++i)
1414 grad.row(i) = reduced_diff[i].getGradient();
1419 Eigen::MatrixXd AdjointTools::edge_velocity_divergence(
const Eigen::MatrixXd &
V)
1421 return line_length_grad(
V) / line_length<double>(
V);
1424 Eigen::MatrixXd AdjointTools::face_velocity_divergence(
const Eigen::MatrixXd &
V)
1426 return triangle_area_grad(
V) / triangle_area<double>(
V);
1429 void AdjointTools::scaled_jacobian(
const Eigen::MatrixXd &
V,
const Eigen::MatrixXi &F, Eigen::VectorXd &quality)
1431 const int dim = F.cols() - 1;
1433 quality.setZero(F.rows());
1436 for (
int i = 0; i < F.rows(); i++)
1438 Eigen::RowVector3d e0;
1440 e0.head(2) =
V.row(F(i, 2)) -
V.row(F(i, 1));
1441 Eigen::RowVector3d e1;
1443 e1.head(2) =
V.row(F(i, 0)) -
V.row(F(i, 2));
1444 Eigen::RowVector3d e2;
1446 e2.head(2) =
V.row(F(i, 1)) -
V.row(F(i, 0));
1448 double l0 = e0.norm();
1449 double l1 = e1.norm();
1450 double l2 = e2.norm();
1452 double A = 0.5 * (e0.cross(e1)).norm();
1453 double Lmax = std::max(l0 * l1, std::max(l1 * l2, l0 * l2));
1455 quality(i) = 2 * A * (2 / sqrt(3)) / Lmax;
1460 for (
int i = 0; i < F.rows(); i++)
1462 Eigen::RowVector3d e0 =
V.row(F(i, 1)) -
V.row(F(i, 0));
1463 Eigen::RowVector3d e1 =
V.row(F(i, 2)) -
V.row(F(i, 1));
1464 Eigen::RowVector3d e2 =
V.row(F(i, 0)) -
V.row(F(i, 2));
1465 Eigen::RowVector3d e3 =
V.row(F(i, 3)) -
V.row(F(i, 0));
1466 Eigen::RowVector3d e4 =
V.row(F(i, 3)) -
V.row(F(i, 1));
1467 Eigen::RowVector3d e5 =
V.row(F(i, 3)) -
V.row(F(i, 2));
1469 double l0 = e0.norm();
1470 double l1 = e1.norm();
1471 double l2 = e2.norm();
1472 double l3 = e3.norm();
1473 double l4 = e4.norm();
1474 double l5 = e5.norm();
1476 double J = std::abs((e0.cross(e3)).dot(e2));
1478 double a1 = l0 * l2 * l3;
1479 double a2 = l0 * l1 * l4;
1480 double a3 = l1 * l2 * l5;
1481 double a4 = l3 * l4 * l5;
1483 double a = std::max({a1, a2, a3, a4, J});
1484 quality(i) = J * sqrt(2) / a;
ElementAssemblyValues vals
assembler::ElementAssemblyValues gvals
Storage for additional data required by differntial code.
const ipc::NormalCollisions & collision_set(int step) const
Eigen::MatrixXd disp_grad(int step=0) const
Eigen::VectorXd v(int step) const
const ipc::TangentialCollisions & friction_collision_set(int step) const
InitialConditionOverride initial_condition_override
Initial-condition override storage for initial condition optimization.
const StiffnessMatrix & basis_nodes_to_gbasis_nodes() const
const ipc::SmoothCollisions & smooth_collision_set(int step) const
Eigen::VectorXd u(int step) const
const StiffnessMatrix & gradu_h(int step) const
const ipc::NormalCollisions & normal_adhesion_collision_set(int step) const
const ipc::TangentialCollisions & tangential_adhesion_collision_set(int step) const
Runtime override for initial-condition histories.
bool is_empty() const
Returns true when no quantity is overridden.
bool depend_on_gradu_local() const
void dj_du(const Eigen::MatrixXd &elastic_params, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &u, const Eigen::MatrixXd &grad_u, const Eigen::MatrixXd &reference_normals, const assembler::ElementAssemblyValues &vals, ParameterType ¶ms, Eigen::MatrixXd &val) const
bool depend_on_gradu() const
void dj_dgradu(const Eigen::MatrixXd &elastic_params, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &u, const Eigen::MatrixXd &grad_u, const Eigen::MatrixXd &reference_normals, const assembler::ElementAssemblyValues &vals, ParameterType ¶ms, Eigen::MatrixXd &val) const
void evaluate(const Eigen::MatrixXd &elastic_params, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &u, const Eigen::MatrixXd &grad_u, const Eigen::MatrixXd &reference_normals, const assembler::ElementAssemblyValues &vals, ParameterType ¶ms, Eigen::MatrixXd &val) const
void dj_dx(const Eigen::MatrixXd &elastic_params, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &pts, const Eigen::MatrixXd &u, const Eigen::MatrixXd &grad_u, const Eigen::MatrixXd &reference_normals, const assembler::ElementAssemblyValues &vals, ParameterType ¶ms, Eigen::MatrixXd &val) const
main class that contains the polyfem solver and all its state
int n_bases
number of bases
assembler::AssemblyValsCache ass_vals_cache
used to store assembly values for small problems
bool is_adhesion_enabled() const
does the simulation have adhesion
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
std::shared_ptr< assembler::Assembler > assembler
assemblers
void initial_velocity(Eigen::MatrixXd &velocity, const InitialConditionOverride *ic_override=nullptr) const
Load or compute the initial velocity.
ipc::CollisionMesh collision_mesh
IPC collision mesh.
std::shared_ptr< assembler::Mass > mass_matrix_assembler
std::vector< int > primitive_to_node() const
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
std::shared_ptr< polyfem::mesh::MeshNodes > mesh_nodes
Mapping from input nodes to FE nodes.
std::shared_ptr< assembler::Problem > problem
current problem, it contains rhs and bc
std::vector< int > node_to_primitive() const
json args
main input arguments containing all defaults
void initial_acceleration(Eigen::MatrixXd &acceleration, const InitialConditionOverride *ic_override=nullptr) const
Load or compute the initial acceleration.
std::vector< basis::ElementBases > bases
FE bases, the size is #elements.
int n_geom_bases
number of geometric bases
std::vector< mesh::LocalBoundary > total_local_boundary
mapping from elements to nodes for all mesh
assembler::AssemblyValsCache mass_ass_vals_cache
std::vector< int > boundary_nodes
list of boundary nodes
solver::SolveData solve_data
timedependent stuff cached
bool is_contact_enabled() const
does the simulation have contact
void compute(const int el_index, const bool is_volume, const basis::ElementBases &basis, const basis::ElementBases &gbasis, ElementAssemblyValues &vals) const
retrieves cached basis evaluation and geometric for the given element if it doesn't exist,...
stores per local bases evaluations
std::vector< basis::Local2Global > global
stores per element basis values at given quadrature points and geometric mapping
void compute(const int el_index, const bool is_volume, const Eigen::MatrixXd &pts, const basis::ElementBases &basis, const basis::ElementBases &gbasis)
computes the per element values at the local (ref el) points (pts) sets basis_values,...
quadrature::Quadrature quadrature
static void interpolate_at_local_vals(const mesh::Mesh &mesh, const bool is_problem_scalar, const std::vector< basis::ElementBases > &bases, const std::vector< basis::ElementBases > &gbases, const int el_index, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &fun, Eigen::MatrixXd &result, Eigen::MatrixXd &result_grad)
interpolate solution and gradient at element (calls interpolate_at_local_vals with sol)
Eigen::VectorXd apply_jacobian(const Eigen::VectorXd &grad, const Eigen::VectorXd &x) const override
Apply jacobian for chain rule.
std::shared_ptr< solver::FrictionForm > friction_form
std::shared_ptr< solver::InertiaForm > inertia_form
std::shared_ptr< solver::PeriodicContactForm > periodic_contact_form
std::shared_ptr< solver::PressureForm > pressure_form
std::shared_ptr< solver::BodyForm > body_form
std::shared_ptr< solver::NLProblem > nl_problem
std::shared_ptr< solver::NormalAdhesionForm > normal_adhesion_form
std::shared_ptr< solver::ContactForm > contact_form
std::shared_ptr< solver::ElasticForm > damping_form
std::shared_ptr< solver::ElasticForm > elastic_form
std::shared_ptr< solver::TangentialAdhesionForm > tangential_adhesion_form
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
DScalar1< double, Eigen::Matrix< double, Eigen::Dynamic, 1 > > Diff
void vector2matrix(const Eigen::VectorXd &vec, Eigen::MatrixXd &mat)
auto & get_local_thread_storage(Storages &storage, int thread_id)
auto create_thread_storage(const LocalStorage &initial_local_storage)
double triangle_area(const Eigen::MatrixXd V)
Compute the signed area of a triangle defined by three points.
void maybe_parallel_for(int size, const std::function< void(int, int, int)> &partial_for)
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, MAX_QUAD_POINTS, 1 > QuadratureVector
void log_and_throw_adjoint_error(const std::string &msg)
Eigen::SparseMatrix< double, Eigen::ColMajor > StiffnessMatrix
Automatic differentiation scalar with first-order derivatives.
static void setVariableCount(size_t value)
Set the independent variable count used by the automatic differentiation layer.
Parameters for the functional evaluation.