37 if (state.
args[
"time"][
"integrator"].is_string())
39 if (state.
args[
"time"][
"integrator"][
"type"] ==
"ImplicitEuler")
41 if (state.
args[
"time"][
"integrator"][
"type"] ==
"BDF")
42 return state.
args[
"time"][
"integrator"][
"steps"].get<
int>();
53 double dot(
const Eigen::MatrixXd &A,
const Eigen::MatrixXd &B) {
return (A.array() * B.array()).sum(); }
55 class LocalThreadScalarStorage
62 LocalThreadScalarStorage()
68 class LocalThreadVecStorage
75 LocalThreadVecStorage(
const int size)
85 T
triangle_area(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &
V)
87 Eigen::Matrix<T, Eigen::Dynamic, 1> l1 =
V.row(1) -
V.row(0);
88 Eigen::Matrix<T, Eigen::Dynamic, 1> l2 =
V.row(2) -
V.row(0);
89 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));
93 Eigen::MatrixXd triangle_area_grad(
const Eigen::MatrixXd &
F)
96 Eigen::Matrix<Diff, Eigen::Dynamic, Eigen::Dynamic> full_diff(
F.rows(),
F.cols());
97 for (
int i = 0; i <
F.rows(); i++)
98 for (
int j = 0; j <
F.cols(); j++)
99 full_diff(i, j) = Diff(i + j *
F.rows(),
F(i, j));
102 Eigen::MatrixXd
grad(
F.rows(),
F.cols());
103 for (
int i = 0; i <
F.rows(); ++i)
104 for (
int j = 0; j <
F.cols(); ++j)
105 grad(i, j) = reduced_diff.getGradient()(i + j *
F.rows());
110 template <
typename T>
111 T line_length(
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &
V)
113 Eigen::Matrix<T, Eigen::Dynamic, 1> L =
V.row(1) -
V.row(0);
118 Eigen::MatrixXd line_length_grad(
const Eigen::MatrixXd &
F)
121 Eigen::Matrix<Diff, Eigen::Dynamic, Eigen::Dynamic> full_diff(
F.rows(),
F.cols());
122 for (
int i = 0; i <
F.rows(); i++)
123 for (
int j = 0; j <
F.cols(); j++)
124 full_diff(i, j) = Diff(i + j *
F.rows(),
F(i, j));
125 auto reduced_diff = line_length(full_diff);
127 Eigen::MatrixXd
grad(
F.rows(),
F.cols());
128 for (
int i = 0; i <
F.rows(); ++i)
129 for (
int j = 0; j <
F.cols(); ++j)
130 grad(i, j) = reduced_diff.getGradient()(i + j *
F.rows());
135 template <
typename T>
136 Eigen::Matrix<T, 2, 1> edge_normal(
const Eigen::Matrix<T, 4, 1> &
V)
138 Eigen::Matrix<T, 2, 1> v1 =
V.segment(0, 2);
139 Eigen::Matrix<T, 2, 1> v2 =
V.segment(2, 2);
140 Eigen::Matrix<T, 2, 1> normal = v1 - v2;
142 normal = normal / normal.norm();
146 template <
typename T>
147 Eigen::Matrix<T, 3, 1> face_normal(
const Eigen::Matrix<T, 9, 1> &
V)
149 Eigen::Matrix<T, 3, 1> v1 =
V.segment(0, 3);
150 Eigen::Matrix<T, 3, 1> v2 =
V.segment(3, 3);
151 Eigen::Matrix<T, 3, 1> v3 =
V.segment(6, 3);
152 Eigen::Matrix<T, 3, 1> normal = (v2 - v1).
cross(v3 - v1);
153 normal = normal / normal.norm();
157 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)
159 Eigen::MatrixXd params = Eigen::MatrixXd::Zero(local_pts.rows(), 2);
161 auto search_lambda = lame_params.find(
"lambda");
162 auto search_mu = lame_params.find(
"mu");
164 if (search_lambda == lame_params.end() || search_mu == lame_params.end())
167 for (
int p = 0; p < local_pts.rows(); p++)
169 params(p, 0) = search_lambda->second(local_pts.row(p), pts.row(p), t, e);
170 params(p, 1) = search_mu->second(local_pts.row(p), pts.row(p), t, e);
180 const Eigen::MatrixXd &solution,
181 const std::set<int> &interested_ids,
185 const auto &bases = state.
bases;
188 const int dim = state.
mesh->dimension();
189 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
190 const int n_elements = int(bases.size());
191 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
192 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
202 params.
t = dt * cur_step + t0;
203 params.
step = cur_step;
205 Eigen::MatrixXd u, grad_u;
206 Eigen::MatrixXd result;
208 for (
int e = start; e < end; ++e)
210 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
226 local_storage.val += dot(result, local_storage.da);
229 for (
const LocalThreadScalarStorage &local_storage : storage)
230 integral += local_storage.val;
236 LocalThreadScalarStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
239 Eigen::MatrixXd points, normal;
240 Eigen::VectorXd weights;
242 Eigen::MatrixXd u, grad_u;
243 Eigen::MatrixXd result;
244 IntegrableFunctional::ParameterType params;
245 params.t = dt * cur_step + t0;
246 params.step = cur_step;
248 for (int lb_id = start; lb_id < end; ++lb_id)
250 const auto &lb = state.total_local_boundary[lb_id];
251 const int e = lb.element_id();
253 for (int i = 0; i < lb.size(); i++)
255 const int global_primitive_id = lb.global_primitive_id(i);
256 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
259 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
261 assembler::ElementAssemblyValues &vals = local_storage.vals;
262 vals.compute(e, state.mesh->is_volume(), points, bases[e], gbases[e]);
263 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim, vals, solution, u, grad_u);
265 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
268 params.body_id = state.mesh->get_body_id(e);
269 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
270 j.evaluate(lame_params, points, vals.val, u, grad_u, normal, vals, params, result);
272 local_storage.val += dot(result, weights);
276 for (
const LocalThreadScalarStorage &local_storage : storage)
277 integral += local_storage.val;
281 std::vector<bool> traversed(state.
n_bases,
false);
283 params.
t = dt * cur_step + t0;
284 params.
step = cur_step;
285 for (
int e = 0; e < bases.size(); e++)
287 const auto &bs = bases[e];
288 for (
int i = 0; i < bs.bases.size(); i++)
290 const auto &b = bs.bases[i];
291 assert(b.global().size() == 1);
292 const auto &g = b.global()[0];
293 if (traversed[g.index])
296 const Eigen::MatrixXd lame_params = extract_lame_params(state.
assembler->parameters(), e, params.
t, Eigen::MatrixXd::Zero(1, dim) , g.node);
298 params.
node = g.index;
302 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);
304 traversed[g.index] =
true;
312 void AdjointTools::compute_shape_derivative_functional_term(
314 const Eigen::MatrixXd &solution,
316 const std::set<int> &interested_ids,
318 Eigen::VectorXd &term,
319 const int cur_time_step)
322 const auto &bases = state.
bases;
323 const int dim = state.
mesh->dimension();
324 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
325 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
326 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
328 const int n_elements = int(bases.size());
331 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
333 if (spatial_integral_type == SpatialIntegralType::Volume)
335 utils::maybe_parallel_for(n_elements, [&](
int start,
int end,
int thread_id) {
336 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
338 Eigen::MatrixXd u, grad_u, j_val, dj_dgradu, dj_dx;
341 params.
t = cur_time_step * dt + t0;
342 params.
step = cur_time_step;
344 for (
int e = start; e < end; ++e)
346 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
351 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim,
vals, solution, u, grad_u);
372 Eigen::MatrixXd tau_q, grad_u_q;
373 for (
auto &v :
gvals.basis_values)
375 for (
int q = 0; q < local_storage.da.size(); ++q)
377 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();
380 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += (v.val(q) * local_storage.da(q)) * dj_dx.row(q).transpose();
384 if (dim == actual_dim)
391 tau_q = dj_dgradu.row(q);
392 grad_u_q = grad_u.row(q);
394 for (
int d = 0; d < dim; d++)
395 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);
402 else if (spatial_integral_type == SpatialIntegralType::Surface)
404 utils::maybe_parallel_for(state.
total_local_boundary.size(), [&](
int start,
int end,
int thread_id) {
405 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
407 Eigen::MatrixXd uv, points, normal;
408 Eigen::VectorXd &weights = local_storage.da;
410 Eigen::MatrixXd u, grad_u, x, grad_x, j_val, dj_dgradu, dj_dgradx, dj_dx;
412 IntegrableFunctional::ParameterType params;
413 params.t = cur_time_step * dt + t0;
414 params.step = cur_time_step;
416 for (int lb_id = start; lb_id < end; ++lb_id)
418 const auto &lb = state.total_local_boundary[lb_id];
419 const int e = lb.element_id();
421 for (int i = 0; i < lb.size(); i++)
423 const int global_primitive_id = lb.global_primitive_id(i);
424 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
427 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
429 assembler::ElementAssemblyValues &vals = local_storage.vals;
430 io::Evaluator::interpolate_at_local_vals(*state.mesh, state.problem->is_scalar(), bases, gbases, e, points, solution, u, grad_u);
433 vals.compute(e, state.mesh->is_volume(), points, gbases[e], gbases[e]);
437 const int n_loc_bases_ = int(vals.basis_values.size());
439 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
442 params.body_id = state.mesh->get_body_id(e);
443 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
445 j.evaluate(lame_params, points, vals.val, u, grad_u, normal, vals, params, j_val);
446 j_val = j_val.array().colwise() * weights.array();
448 if (j.depend_on_gradu())
450 j.dj_dgradu(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu);
451 dj_dgradu = dj_dgradu.array().colwise() * weights.array();
454 if (j.depend_on_gradx())
456 j.dj_dgradx(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradx);
457 dj_dgradx = dj_dgradx.array().colwise() * weights.array();
462 j.dj_dx(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dx);
463 dj_dx = dj_dx.array().colwise() * weights.array();
466 const auto nodes = gbases[e].local_nodes_for_primitive(lb.global_primitive_id(i), *state.mesh);
468 if (nodes.size() != dim)
469 log_and_throw_adjoint_error(
"Only linear geometry is supported in differentiable surface integral functional!");
471 Eigen::MatrixXd velocity_div_mat;
472 if (state.mesh->is_volume())
475 for (int d = 0; d < 3; d++)
476 V.row(d) = gbases[e].bases[nodes(d)].global()[0].node;
477 velocity_div_mat = face_velocity_divergence(V);
482 for (int d = 0; d < 2; d++)
483 V.row(d) = gbases[e].bases[nodes(d)].global()[0].node;
484 velocity_div_mat = edge_velocity_divergence(V);
487 Eigen::MatrixXd grad_u_q, tau_q, grad_x_q;
488 for (long n = 0; n < nodes.size(); ++n)
490 const assembler::AssemblyValues &v = vals.basis_values[nodes(n)];
492 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += j_val.sum() * velocity_div_mat.row(n).transpose();
495 for (long n = 0; n < n_loc_bases_; ++n)
497 const assembler::AssemblyValues &v = vals.basis_values[n];
500 local_storage.vec.block(v.global[0].index * dim, 0, dim, 1) += dj_dx.transpose() * v.val;
503 if (j.depend_on_gradu())
505 for (int q = 0; q < weights.size(); ++q)
507 if (dim == actual_dim)
509 vector2matrix(grad_u.row(q), grad_u_q);
510 vector2matrix(dj_dgradu.row(q), tau_q);
514 grad_u_q = grad_u.row(q);
515 tau_q = dj_dgradu.row(q);
518 for (int d = 0; d < dim; d++)
519 local_storage.vec(v.global[0].index * dim + d) += -dot(tau_q, grad_u_q.col(d) * v.grad_t_m.row(q));
523 if (j.depend_on_gradx())
525 for (int d = 0; d < dim; d++)
527 for (int q = 0; q < weights.size(); ++q)
528 local_storage.vec(v.global[0].index * dim + d) += dot(dj_dgradx.block(q, d * dim, 1, dim), v.grad.row(q));
536 else if (spatial_integral_type == SpatialIntegralType::VertexSum)
540 for (
const LocalThreadVecStorage &local_storage : storage)
541 term += local_storage.
vec;
543 term = utils::flatten(utils::unflatten(term, dim)(state.
primitive_to_node(), Eigen::all));
546 void AdjointTools::dJ_shape_static_adjoint_term(
548 const Eigen::MatrixXd &sol,
549 const Eigen::MatrixXd &adjoint,
550 Eigen::VectorXd &one_form)
552 Eigen::VectorXd elasticity_term, rhs_term, pressure_term, contact_term;
562 rhs_term.setZero(one_form.size());
570 pressure_term.setZero(one_form.size());
578 contact_term.setZero(elasticity_term.size());
579 one_form -= elasticity_term + rhs_term + pressure_term + contact_term;
582 one_form = utils::flatten(utils::unflatten(one_form, state.
mesh->dimension())(state.
primitive_to_node(), Eigen::all));
585 void AdjointTools::dJ_shape_transient_adjoint_term(
587 const Eigen::MatrixXd &adjoint_nu,
588 const Eigen::MatrixXd &adjoint_p,
589 Eigen::VectorXd &one_form)
591 const double t0 = state.
args[
"time"][
"t0"];
592 const double dt = state.
args[
"time"][
"dt"];
593 const int time_steps = state.
args[
"time"][
"time_steps"];
594 const int bdf_order = get_bdf_order(state);
596 Eigen::VectorXd elasticity_term, rhs_term, pressure_term, damping_term, mass_term, contact_term, friction_term;
599 Eigen::VectorXd cur_p, cur_nu;
600 for (
int i = time_steps; i > 0; --i)
602 const int real_order = std::min(bdf_order, i);
603 double beta = time_integrator::BDF::betas(real_order - 1);
604 double beta_dt = beta * dt;
605 const double t = i * dt + t0;
609 cur_p = adjoint_p.col(i);
610 cur_nu = adjoint_nu.col(i);
624 damping_term.setZero(mass_term.size());
633 contact_term.setZero(mass_term.size());
642 friction_term.setZero(mass_term.size());
645 one_form += beta_dt * (elasticity_term + rhs_term + pressure_term + damping_term + contact_term + friction_term + mass_term);
649 Eigen::VectorXd sum_alpha_p;
651 sum_alpha_p.setZero(adjoint_p.rows());
652 int num = std::min(bdf_order, time_steps);
653 for (
int j = 0; j < num; ++j)
655 int order = std::min(bdf_order - 1, j);
656 sum_alpha_p -= time_integrator::BDF::alphas(order)[j] * adjoint_p.col(j + 1);
662 one_form += mass_term;
664 one_form = utils::flatten(utils::unflatten(one_form, state.
mesh->dimension())(state.
primitive_to_node(), Eigen::all));
667 void AdjointTools::dJ_material_static_adjoint_term(
669 const Eigen::MatrixXd &sol,
670 const Eigen::MatrixXd &adjoint,
671 Eigen::VectorXd &one_form)
676 void AdjointTools::dJ_material_transient_adjoint_term(
678 const Eigen::MatrixXd &adjoint_nu,
679 const Eigen::MatrixXd &adjoint_p,
680 Eigen::VectorXd &one_form)
682 const double t0 = state.
args[
"time"][
"t0"];
683 const double dt = state.
args[
"time"][
"dt"];
684 const int time_steps = state.
args[
"time"][
"time_steps"];
685 const int bdf_order = get_bdf_order(state);
687 one_form.setZero(state.
bases.size() * 2);
689 auto storage = utils::create_thread_storage(LocalThreadVecStorage(one_form.size()));
691 utils::maybe_parallel_for(time_steps, [&](
int start,
int end,
int thread_id) {
692 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
693 Eigen::VectorXd elasticity_term;
694 for (
int i_aux = start; i_aux < end; ++i_aux)
696 const int i = time_steps - i_aux;
697 const int real_order = std::min(bdf_order, i);
698 double beta_dt = time_integrator::BDF::betas(real_order - 1) * dt;
700 Eigen::VectorXd cur_p = adjoint_p.col(i);
704 local_storage.vec += beta_dt * elasticity_term;
708 for (
const LocalThreadVecStorage &local_storage : storage)
709 one_form += local_storage.vec;
712 void AdjointTools::dJ_friction_transient_adjoint_term(
714 const Eigen::MatrixXd &adjoint_nu,
715 const Eigen::MatrixXd &adjoint_p,
716 Eigen::VectorXd &one_form)
718 const double dt = state.
args[
"time"][
"dt"];
720 const int time_steps = state.
args[
"time"][
"time_steps"];
721 const int dim = state.
mesh->dimension();
722 const int bdf_order = get_bdf_order(state);
726 auto storage = utils::create_thread_storage(LocalThreadScalarStorage());
728 utils::maybe_parallel_for(time_steps, [&](
int start,
int end,
int thread_id) {
729 LocalThreadScalarStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
730 for (
int t_aux = start; t_aux < end; ++t_aux)
732 const int t = time_steps - t_aux;
733 const int real_order = std::min(bdf_order, t);
734 double beta = time_integrator::BDF::betas(real_order - 1);
736 const Eigen::MatrixXd surface_solution_prev = state.
collision_mesh.vertices(utils::unflatten(state.
diff_cached.
u(t - 1), dim));
740 const Eigen::MatrixXd surface_velocities = (surface_solution - surface_solution_prev) / dt;
747 surface_solution_prev,
753 Eigen::VectorXd cur_p = adjoint_p.col(t);
756 local_storage.val += dot(cur_p, force) / (beta * mu * dt);
760 for (
const LocalThreadScalarStorage &local_storage : storage)
761 one_form(0) += local_storage.val;
764 void AdjointTools::dJ_damping_transient_adjoint_term(
766 const Eigen::MatrixXd &adjoint_nu,
767 const Eigen::MatrixXd &adjoint_p,
768 Eigen::VectorXd &one_form)
770 const double t0 = state.
args[
"time"][
"t0"];
771 const double dt = state.
args[
"time"][
"dt"];
772 const int time_steps = state.
args[
"time"][
"time_steps"];
773 const int bdf_order = get_bdf_order(state);
777 auto storage = utils::create_thread_storage(LocalThreadVecStorage(one_form.size()));
779 utils::maybe_parallel_for(time_steps, [&](
int start,
int end,
int thread_id) {
780 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
781 Eigen::VectorXd damping_term;
782 for (
int t_aux = start; t_aux < end; ++t_aux)
784 const int t = time_steps - t_aux;
785 const int real_order = std::min(bdf_order, t);
786 const double beta = time_integrator::BDF::betas(real_order - 1);
788 Eigen::VectorXd cur_p = adjoint_p.col(t);
792 local_storage.vec += (beta * dt) * damping_term;
796 for (
const LocalThreadVecStorage &local_storage : storage)
797 one_form += local_storage.vec;
800 void AdjointTools::dJ_initial_condition_adjoint_term(
802 const Eigen::MatrixXd &adjoint_nu,
803 const Eigen::MatrixXd &adjoint_p,
804 Eigen::VectorXd &one_form)
806 const int ndof = state.
ndof();
807 one_form.setZero(ndof * 2);
810 one_form.segment(0, ndof) = -adjoint_nu.col(0);
811 one_form.segment(ndof, ndof) = -adjoint_p.col(0);
816 one_form(ndof + b) = 0;
820 void AdjointTools::dJ_dirichlet_transient_adjoint_term(
822 const Eigen::MatrixXd &adjoint_nu,
823 const Eigen::MatrixXd &adjoint_p,
824 Eigen::VectorXd &one_form)
826 const double dt = state.
args[
"time"][
"dt"];
827 const int time_steps = state.
args[
"time"][
"time_steps"];
828 const int bdf_order = get_bdf_order(state);
831 one_form.setZero(time_steps * n_dirichlet_dof);
832 for (
int i = 1; i <= time_steps; ++i)
834 const int real_order = std::min(bdf_order, i);
835 const double beta_dt = time_integrator::BDF::betas(real_order - 1) * dt;
837 one_form.segment((i - 1) * n_dirichlet_dof, n_dirichlet_dof) = -(1. / beta_dt) * adjoint_p(state.
boundary_nodes, i);
841 void AdjointTools::dJ_du_step(
844 const Eigen::MatrixXd &solution,
845 const std::set<int> &interested_ids,
848 Eigen::VectorXd &term)
850 const auto &bases = state.
bases;
853 const int dim = state.
mesh->dimension();
854 const int actual_dim = state.
problem->is_scalar() ? 1 : dim;
855 const int n_elements = int(bases.size());
856 const double t0 = state.
problem->is_time_dependent() ? state.
args[
"time"][
"t0"].get<
double>() : 0.0;
857 const double dt = state.
problem->is_time_dependent() ? state.
args[
"time"][
"dt"].get<
double>() : 0.0;
859 term = Eigen::MatrixXd::Zero(state.
n_bases * actual_dim, 1);
864 if (spatial_integral_type == SpatialIntegralType::Volume)
866 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
867 utils::maybe_parallel_for(n_elements, [&](
int start,
int end,
int thread_id) {
868 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
870 Eigen::MatrixXd u, grad_u;
871 Eigen::MatrixXd lambda, mu;
872 Eigen::MatrixXd dj_du, dj_dgradu, dj_dgradx;
875 params.
t = dt * cur_step + t0;
876 params.
step = cur_step;
878 for (
int e = start; e < end; ++e)
880 if (interested_ids.size() != 0 && interested_ids.find(state.
mesh->get_body_id(e)) == interested_ids.end())
891 const int n_loc_bases_ = int(
vals.basis_values.size());
893 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim,
vals, solution, u, grad_u);
898 dj_dgradu.resize(0, 0);
902 for (
int q = 0; q < dj_dgradu.rows(); q++)
903 dj_dgradu.row(q) *= local_storage.da(q);
910 for (
int q = 0; q < dj_du.rows(); q++)
911 dj_du.row(q) *= local_storage.da(q);
914 for (
int i = 0; i < n_loc_bases_; ++i)
917 assert(v.
global.size() == 1);
918 for (
int d = 0; d < actual_dim; d++)
925 for (
int q = 0; q < local_storage.da.size(); ++q)
926 val += dot(dj_dgradu.block(q, d * dim, 1, dim), v.
grad_t_m.row(q));
932 for (
int q = 0; q < local_storage.da.size(); ++q)
933 val += dj_du(q, d) * v.
val(q);
935 local_storage.vec(v.
global[0].index * actual_dim + d) +=
val;
940 for (
const LocalThreadVecStorage &local_storage : storage)
941 term += local_storage.vec;
943 else if (spatial_integral_type == SpatialIntegralType::Surface)
945 auto storage = utils::create_thread_storage(LocalThreadVecStorage(term.size()));
946 utils::maybe_parallel_for(state.
total_local_boundary.size(), [&](
int start,
int end,
int thread_id) {
947 LocalThreadVecStorage &local_storage = utils::get_local_thread_storage(storage, thread_id);
949 Eigen::MatrixXd uv, samples, gtmp;
950 Eigen::MatrixXd points, normal;
951 Eigen::VectorXd weights;
953 Eigen::MatrixXd u, grad_u;
954 Eigen::MatrixXd lambda, mu;
955 Eigen::MatrixXd dj_du, dj_dgradu, dj_dgradu_local;
957 IntegrableFunctional::ParameterType params;
958 params.t = dt * cur_step + t0;
959 params.step = cur_step;
961 for (int lb_id = start; lb_id < end; ++lb_id)
963 const auto &lb = state.total_local_boundary[lb_id];
964 const int e = lb.element_id();
966 for (int i = 0; i < lb.size(); i++)
968 const int global_primitive_id = lb.global_primitive_id(i);
969 if (interested_ids.size() != 0 && interested_ids.find(state.mesh->get_boundary_id(global_primitive_id)) == interested_ids.end())
972 utils::BoundarySampler::boundary_quadrature(lb, state.n_boundary_samples(), *state.mesh, i, false, uv, points, normal, weights);
974 assembler::ElementAssemblyValues &vals = local_storage.vals;
975 vals.compute(e, state.mesh->is_volume(), points, bases[e], gbases[e]);
976 io::Evaluator::interpolate_at_local_vals(e, dim, actual_dim, vals, solution, u, grad_u);
978 const Eigen::MatrixXd lame_params = extract_lame_params(state.assembler->parameters(), e, params.t, points, vals.val);
982 const int n_loc_bases_ = int(vals.basis_values.size());
985 params.body_id = state.mesh->get_body_id(e);
986 params.boundary_id = state.mesh->get_boundary_id(global_primitive_id);
988 dj_dgradu.resize(0, 0);
989 if (j.depend_on_gradu())
991 j.dj_dgradu(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu);
992 for (int q = 0; q < dj_dgradu.rows(); q++)
993 dj_dgradu.row(q) *= weights(q);
996 dj_dgradu_local.resize(0, 0);
997 if (j.depend_on_gradu_local())
999 j.dj_dgradu_local(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_dgradu_local);
1000 for (int q = 0; q < dj_dgradu_local.rows(); q++)
1001 dj_dgradu_local.row(q) *= weights(q);
1005 if (j.depend_on_u())
1007 j.dj_du(lame_params, points, vals.val, u, grad_u, normal, vals, params, dj_du);
1008 for (int q = 0; q < dj_du.rows(); q++)
1009 dj_du.row(q) *= weights(q);
1012 for (int l = 0; l < lb.size(); ++l)
1014 const auto nodes = bases[e].local_nodes_for_primitive(lb.global_primitive_id(l), *state.mesh);
1016 for (long n = 0; n < nodes.size(); ++n)
1018 const assembler::AssemblyValues &v = vals.basis_values[nodes(n)];
1019 assert(v.global.size() == 1);
1020 for (int d = 0; d < actual_dim; d++)
1025 if (j.depend_on_gradu())
1027 for (int q = 0; q < weights.size(); ++q)
1028 val += dot(dj_dgradu.block(q, d * dim, 1, dim), v.grad_t_m.row(q));
1031 if (j.depend_on_gradu_local())
1033 for (int q = 0; q < weights.size(); ++q)
1034 val += dot(dj_dgradu_local.block(q, d * dim, 1, dim), v.grad.row(q));
1037 if (j.depend_on_u())
1039 for (int q = 0; q < weights.size(); ++q)
1040 val += dj_du(q, d) * v.val(q);
1042 local_storage.vec(v.global[0].index * actual_dim + d) += val;
1049 for (
const LocalThreadVecStorage &local_storage : storage)
1050 term += local_storage.vec;
1052 else if (spatial_integral_type == SpatialIntegralType::VertexSum)
1054 std::vector<bool> traversed(state.
n_bases,
false);
1056 params.
t = dt * cur_step + t0;
1057 params.
step = cur_step;
1058 for (
int e = 0; e < bases.size(); e++)
1060 const auto &bs = bases[e];
1061 for (
int i = 0; i < bs.bases.size(); i++)
1063 const auto &b = bs.bases[i];
1064 assert(b.global().size() == 1);
1065 const auto &g = b.global()[0];
1066 if (traversed[g.index])
1069 const Eigen::MatrixXd lame_params = extract_lame_params(state.
assembler->parameters(), e, params.
t, Eigen::MatrixXd::Zero(1, dim) , g.node);
1071 params.
node = g.index;
1074 Eigen::MatrixXd
val;
1075 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);
1076 term.block(g.index * actual_dim, 0, actual_dim, 1) +=
val.transpose();
1077 traversed[g.index] =
true;
1083 Eigen::VectorXd AdjointTools::map_primitive_to_node_order(
const State &state,
const Eigen::VectorXd &primitives)
1085 int dim = state.
mesh->dimension();
1086 assert(primitives.size() == (state.
n_geom_bases * dim));
1087 Eigen::VectorXd nodes(primitives.size());
1090 nodes.segment(map[v] * dim, dim) = primitives.segment(v * dim, dim);
1094 Eigen::VectorXd AdjointTools::map_node_to_primitive_order(
const State &state,
const Eigen::VectorXd &nodes)
1096 int dim = state.
mesh->dimension();
1098 Eigen::VectorXd primitives(nodes.size());
1101 primitives.segment(map[v] * dim, dim) = nodes.segment(v * dim, dim);
1105 Eigen::MatrixXd AdjointTools::edge_normal_gradient(
const Eigen::MatrixXd &
V)
1108 Eigen::Matrix<Diff, 4, 1> full_diff(4, 1);
1109 for (
int i = 0; i < 2; i++)
1110 for (
int j = 0; j < 2; j++)
1111 full_diff(i * 2 + j) = Diff(i * 2 + j,
V(i, j));
1112 auto reduced_diff = edge_normal(full_diff);
1114 Eigen::MatrixXd grad(2, 4);
1115 for (
int i = 0; i < 2; ++i)
1116 grad.row(i) = reduced_diff[i].getGradient();
1121 Eigen::MatrixXd AdjointTools::face_normal_gradient(
const Eigen::MatrixXd &
V)
1124 Eigen::Matrix<Diff, 9, 1> full_diff(9, 1);
1125 for (
int i = 0; i < 3; i++)
1126 for (
int j = 0; j < 3; j++)
1127 full_diff(i * 3 + j) = Diff(i * 3 + j,
V(i, j));
1128 auto reduced_diff = face_normal(full_diff);
1130 Eigen::MatrixXd grad(3, 9);
1131 for (
int i = 0; i < 3; ++i)
1132 grad.row(i) = reduced_diff[i].getGradient();
1137 Eigen::MatrixXd AdjointTools::edge_velocity_divergence(
const Eigen::MatrixXd &
V)
1139 return line_length_grad(
V) / line_length<double>(
V);
1142 Eigen::MatrixXd AdjointTools::face_velocity_divergence(
const Eigen::MatrixXd &
V)
1144 return triangle_area_grad(
V) / triangle_area<double>(
V);
ElementAssemblyValues vals
assembler::ElementAssemblyValues gvals
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
const std::vector< basis::ElementBases > & geom_bases() const
Get a constant reference to the geometry mapping bases.
std::shared_ptr< assembler::Assembler > assembler
assemblers
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
StiffnessMatrix gbasis_nodes_to_basis_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
solver::DiffCache diff_cached
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 has 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)
const ipc::Collisions & collision_set(int step) const
Eigen::VectorXd v(int step) const
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::InertiaForm > inertia_form
std::shared_ptr< solver::PressureForm > pressure_form
std::shared_ptr< solver::BodyForm > body_form
std::shared_ptr< solver::ContactForm > contact_form
std::shared_ptr< solver::ElasticForm > damping_form
std::shared_ptr< solver::ElasticForm > elastic_form
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
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)
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.