PolyFEM
Loading...
Searching...
No Matches
NeoHookeanElasticity.cpp
Go to the documentation of this file.
2
5
6#include <type_traits>
7
8namespace polyfem::assembler
9{
10 namespace
11 {
12 void evaluate_lame_parameters(const LameParameters &params, const NonLinearAssemblerData &data,
13 const Eigen::Index p, double &lambda, double &mu)
14 {
15 // Keep these as row views: the MatrixXd overload creates two heap temporaries.
16 const auto uv = data.vals.quadrature.points.row(p);
17 const auto point = data.vals.val.row(p);
18 assert(uv.size() == 2 || uv.size() == 3);
19 assert(uv.size() == point.size());
20 params.lambda_mu(
21 uv(0), uv(1), uv.size() == 3 ? uv(2) : 0.0,
22 point(0), point(1), point.size() == 3 ? point(2) : 0.0,
23 data.t, data.vals.element_id, lambda, mu);
24 }
25
26 bool delta(int i, int j)
27 {
28 return (i == j) ? true : false;
29 }
30 } // namespace
31
35
36 void NeoHookeanElasticity::add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources)
37 {
38 assert(size() == 2 || size() == 3);
39
40 params_.add_multimaterial(index, params, size() == 3, units.stress(), resources);
41 }
42
43 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1>
45 {
46 assert(pt.size() == size());
47 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1> res;
48
49 double lambda, mu;
50 // TODO!
51 params_.lambda_mu(0, 0, 0, pt(0).getValue(), pt(1).getValue(), size() == 2 ? 0. : pt(2).getValue(), 0, 0, lambda, mu);
52
53 if (size() == 2)
54 autogen::neo_hookean_2d_function(pt, lambda, mu, res);
55 else if (size() == 3)
56 autogen::neo_hookean_3d_function(pt, lambda, mu, res);
57 else
58 assert(false);
59
60 return res;
61 }
62
63 Eigen::VectorXd
65 {
66 Eigen::Matrix<double, Eigen::Dynamic, 1> gradient;
67
68 if (size() == 2)
69 {
70 switch (data.vals.basis_values.size())
71 {
72 case 3:
73 {
74 gradient.resize(6);
75 compute_energy_aux_gradient_fast<3, 2>(data, gradient);
76 break;
77 }
78 case 6:
79 {
80 gradient.resize(12);
81 compute_energy_aux_gradient_fast<6, 2>(data, gradient);
82 break;
83 }
84 case 10:
85 {
86 gradient.resize(20);
87 compute_energy_aux_gradient_fast<10, 2>(data, gradient);
88 break;
89 }
90 default:
91 {
92 gradient.resize(data.vals.basis_values.size() * 2);
93 compute_energy_aux_gradient_fast<Eigen::Dynamic, 2>(data, gradient);
94 break;
95 }
96 }
97 }
98 else // if (size() == 3)
99 {
100 assert(size() == 3);
101 switch (data.vals.basis_values.size())
102 {
103 case 4:
104 {
105 gradient.resize(12);
106 compute_energy_aux_gradient_fast<4, 3>(data, gradient);
107 break;
108 }
109 case 10:
110 {
111 gradient.resize(30);
112 compute_energy_aux_gradient_fast<10, 3>(data, gradient);
113 break;
114 }
115 case 20:
116 {
117 gradient.resize(60);
118 compute_energy_aux_gradient_fast<20, 3>(data, gradient);
119 break;
120 }
121 default:
122 {
123 gradient.resize(data.vals.basis_values.size() * 3);
124 compute_energy_aux_gradient_fast<Eigen::Dynamic, 3>(data, gradient);
125 break;
126 }
127 }
128 }
129
130 return gradient;
131 }
132
135 const Eigen::MatrixXd &local_pts,
136 const Eigen::MatrixXd &displacement,
137 Eigen::MatrixXd &tensor) const
138 {
139 tensor.resize(local_pts.rows(), size() * size() * size() * size());
140 assert(displacement.cols() == 1);
141
142 Eigen::MatrixXd displacement_grad(size(), size());
143
144 for (long p = 0; p < local_pts.rows(); ++p)
145 {
146 double lambda, mu;
147 params_.lambda_mu(local_pts.row(p), vals.val.row(p), t, vals.element_id, lambda, mu);
148
149 compute_diplacement_grad(size(), vals, local_pts, p, displacement, displacement_grad);
150 const Eigen::MatrixXd def_grad = Eigen::MatrixXd::Identity(size(), size()) + displacement_grad;
151 const Eigen::MatrixXd FmT = def_grad.inverse().transpose();
152 const Eigen::VectorXd FmT_vec = utils::flatten(FmT);
153 const double J = def_grad.determinant();
154 const double tmp1 = mu - lambda * std::log(J);
155 for (int i = 0, idx = 0; i < size(); i++)
156 for (int j = 0; j < size(); j++)
157 for (int k = 0; k < size(); k++)
158 for (int l = 0; l < size(); l++)
159 {
160 tensor(p, idx) = mu * delta(i, k) * delta(j, l) + tmp1 * FmT(i, l) * FmT(k, j);
161 idx++;
162 }
163
164 tensor.row(p) += lambda * utils::flatten(FmT_vec * FmT_vec.transpose());
165
166 // {
167 // Eigen::MatrixXd hess = utils::unflatten(tensor.row(p), size()*size());
168 // Eigen::MatrixXd fhess;
169 // Eigen::VectorXd x0 = utils::flatten(def_grad);
170 // fd::finite_jacobian(
171 // x0, [this, lambda, mu](const Eigen::VectorXd &x1) -> Eigen::VectorXd
172 // {
173 // Eigen::MatrixXd def_grad = utils::unflatten(x1, this->size());
174 // const Eigen::MatrixXd FmT = def_grad.inverse().transpose();
175 // const double J = def_grad.determinant();
176 // Eigen::MatrixXd stress_tensor = mu * (def_grad - FmT) + lambda * std::log(J) * FmT;
177 // return utils::flatten(stress_tensor);
178 // }, fhess);
179
180 // if (!fd::compare_hessian(hess, fhess))
181 // {
182 // logger().trace("Hessian:{}"", hess);
183 // logger().trace("Finite hessian: {}", fhess);
184 // log_and_throw_error("Hessian in Neohookean mismatch");
185 // }
186 // }
187 }
188 }
189
191 {
192 result.resize(data.vals.basis_values.size() * size(), data.vals.basis_values.size() * size());
193 result.visit([&](auto &hessian) {
194 hessian.setZero();
195 if (size() == 2)
196 {
197 switch (data.vals.basis_values.size())
198 {
199 case 3:
200 {
201 compute_energy_hessian_aux_fast<3, 2>(data, hessian);
202 break;
203 }
204 case 6:
205 {
206 compute_energy_hessian_aux_fast<6, 2>(data, hessian);
207 break;
208 }
209 case 10:
210 {
211 compute_energy_hessian_aux_fast<10, 2>(data, hessian);
212 break;
213 }
214 default:
215 {
216 compute_energy_hessian_aux_fast<Eigen::Dynamic, 2>(data, hessian);
217 break;
218 }
219 }
220 }
221 else // if (size() == 3)
222 {
223 assert(size() == 3);
224 switch (data.vals.basis_values.size())
225 {
226 case 4:
227 {
228 compute_energy_hessian_aux_fast<4, 3>(data, hessian);
229 break;
230 }
231 case 10:
232 {
233 compute_energy_hessian_aux_fast<10, 3>(data, hessian);
234 break;
235 }
236 case 20:
237 {
238 compute_energy_hessian_aux_fast<20, 3>(data, hessian);
239 break;
240 }
241 default:
242 {
243 compute_energy_hessian_aux_fast<Eigen::Dynamic, 3>(data, hessian);
244 break;
245 }
246 }
247 }
248 });
249 }
250
252 const Eigen::Index p, const MatrixNd &displacement_grad, OutputStress &output) const
253 {
254 const auto &local_pts = data.local_pts;
255 const auto t = data.t;
256 const auto &def_grad = output.deformation();
257 double lambda, mu;
258 params_.lambda_mu(local_pts.row(p), vals.val.row(p), t, vals.element_id, lambda, mu);
259 const double J = polyfem::utils::determinant(def_grad);
260 const double log_J = std::log(J);
261 if (output.needs_energy)
262 output.energy = (mu / 2.0) * ((def_grad.transpose() * def_grad).trace() - size() - 2 * log_J)
263 + (lambda / 2.0) * log_J * log_J;
264 if (!output.needs_stress)
265 return true;
266 const MatrixNd b = def_grad * def_grad.transpose();
267 const auto I = MatrixNd::Identity(size(), size());
268 output.native = (lambda * log_J * I + mu * (b - I)) / J;
269 return true;
270 }
271
273 {
274 if (size() == 2)
275 {
276 switch (data.vals.basis_values.size())
277 {
278 case 3:
279 return compute_energy_aux<double, 3, 2>(data);
280 case 6:
281 return compute_energy_aux<double, 6, 2>(data);
282 case 10:
283 return compute_energy_aux<double, 10, 2>(data);
284 default:
285 return compute_energy_aux<double, Eigen::Dynamic, 2>(data);
286 }
287 }
288 else // if (size() == 3)
289 {
290 assert(size() == 3);
291 switch (data.vals.basis_values.size())
292 {
293 case 4:
294 return compute_energy_aux<double, 4, 3>(data);
295 case 10:
296 return compute_energy_aux<double, 10, 3>(data);
297 case 20:
298 return compute_energy_aux<double, 20, 3>(data);
299 default:
300 return compute_energy_aux<double, Eigen::Dynamic, 3>(data);
301 }
302 }
303 }
304
305 // Compute ∫ ½μ (tr(FᵀF) - 3 - 2ln(J)) + ½λ ln²(J) du
306 template <typename T, int n_basis, int dim>
308 {
309 if constexpr (std::is_same_v<T, double>)
310 {
311 Eigen::Matrix<T, n_basis, dim> local_disp(data.vals.basis_values.size(), size());
312 local_disp.setZero();
313 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
314 {
315 const auto &bs = data.vals.basis_values[i];
316 for (size_t ii = 0; ii < bs.global.size(); ++ii)
317 {
318 for (int d = 0; d < dim; ++d)
319 {
320 local_disp(i, d) += bs.global[ii].val * data.x(bs.global[ii].index * size() + d);
321 }
322 }
323 }
324
325 Eigen::VectorXd jacs;
327 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
328
329 Eigen::Matrix<T, dim, dim> def_grad(size(), size());
330
331 T energy = T(0.0);
332
333 const int n_pts = data.da.size();
334 for (long p = 0; p < n_pts; ++p)
335 {
336 Eigen::Matrix<T, n_basis, dim> grad(data.vals.basis_values.size(), size());
337
338 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
339 {
340 grad.row(i) = data.vals.basis_values[i].grad.row(p);
341 }
342
343 const Eigen::Matrix<T, dim, dim> jac_it = data.vals.jac_it[p];
344 // Id + grad d
345 def_grad = (local_disp.transpose() * grad) * jac_it + Eigen::Matrix<T, dim, dim>::Identity(size(), size());
346
347 double lambda, mu;
348 evaluate_lame_parameters(params_, data, p, lambda, mu);
349
350 const T J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
351 const T log_det_j = log(J);
352 const T val = mu / 2 * (def_grad.squaredNorm() - size() - 2 * log_det_j) + lambda / 2 * log_det_j * log_det_j;
353 energy += val * data.da(p);
354 }
355 return energy;
356 }
357 else
358 {
359 typedef Eigen::Matrix<T, Eigen::Dynamic, 1> AutoDiffVect;
360 typedef Eigen::Matrix<T, dim, dim> AutoDiffGradMat;
361
362 AutoDiffVect local_disp;
363 get_local_disp(data, size(), local_disp);
364
365 AutoDiffGradMat def_grad(size(), size());
366
367 T energy = T(0.0);
368
369 const int n_pts = data.da.size();
370 for (long p = 0; p < n_pts; ++p)
371 {
372 compute_disp_grad_at_quad(data, local_disp, p, size(), def_grad);
373
374 // Id + grad d
375 for (int d = 0; d < dim; ++d)
376 def_grad(d, d) += T(1);
377
378 const T val = elastic_energy_density(
379 data.vals.quadrature.points.row(p), data.vals.val.row(p), data.t, data.vals.element_id, def_grad);
380
381 energy += val * data.da(p);
382 }
383 return energy;
384 }
385 }
386
387 template <int dim>
388 Eigen::Matrix<double, dim, dim> hat(const Eigen::Matrix<double, dim, 1> &x)
389 {
390
391 Eigen::Matrix<double, dim, dim> prod;
392 prod.setZero();
393
394 prod(0, 1) = -x(2);
395 prod(0, 2) = x(1);
396 prod(1, 0) = x(2);
397 prod(1, 2) = -x(0);
398 prod(2, 0) = -x(1);
399 prod(2, 1) = x(0);
400
401 return prod;
402 }
403
404 template <int dim>
405 Eigen::Matrix<double, dim, 1> cross(const Eigen::Matrix<double, dim, 1> &x, const Eigen::Matrix<double, dim, 1> &y)
406 {
407
408 Eigen::Matrix<double, dim, 1> z;
409 z.setZero();
410
411 z(0) = x(1) * y(2) - x(2) * y(1);
412 z(1) = x(2) * y(0) - x(0) * y(2);
413 z(2) = x(0) * y(1) - x(1) * y(0);
414
415 return z;
416 }
417
418 template <int n_basis, int dim>
419 void NeoHookeanElasticity::compute_energy_aux_gradient_fast(const NonLinearAssemblerData &data, Eigen::Matrix<double, Eigen::Dynamic, 1> &G_flattened) const
420 {
421 assert(data.x.cols() == 1);
422
423 const int n_pts = data.da.size();
424
425 Eigen::Matrix<double, n_basis, dim> local_disp(data.vals.basis_values.size(), size());
426 local_disp.setZero();
427 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
428 {
429 const auto &bs = data.vals.basis_values[i];
430 for (size_t ii = 0; ii < bs.global.size(); ++ii)
431 {
432 for (int d = 0; d < dim; ++d)
433 {
434 local_disp(i, d) += bs.global[ii].val * data.x(bs.global[ii].index * size() + d);
435 }
436 }
437 }
438
439 Eigen::VectorXd jacs;
441 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
442
443 Eigen::Matrix<double, dim, dim> def_grad(size(), size());
444
445 Eigen::Matrix<double, n_basis, dim> G(data.vals.basis_values.size(), size());
446 G.setZero();
447
448 for (long p = 0; p < n_pts; ++p)
449 {
450 Eigen::Matrix<double, n_basis, dim> grad(data.vals.basis_values.size(), size());
451
452 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
453 {
454 grad.row(i) = data.vals.basis_values[i].grad.row(p);
455 }
456
457 Eigen::Matrix<double, dim, dim> jac_it = data.vals.jac_it[p];
458 Eigen::Matrix<double, n_basis, dim> delF_delU = grad * jac_it;
459
460 // Id + grad d
461 def_grad = local_disp.transpose() * delF_delU + Eigen::Matrix<double, dim, dim>::Identity(size(), size());
462
463 const double J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
464 const double log_det_j = log(J);
465
466 Eigen::Matrix<double, dim, dim> delJ_delF(size(), size());
467 delJ_delF.setZero();
468
469 if (dim == 2)
470 {
471
472 delJ_delF(0, 0) = def_grad(1, 1);
473 delJ_delF(0, 1) = -def_grad(1, 0);
474 delJ_delF(1, 0) = -def_grad(0, 1);
475 delJ_delF(1, 1) = def_grad(0, 0);
476 }
477
478 else if (dim == 3)
479 {
480
481 Eigen::Matrix<double, dim, 1> u(def_grad.rows());
482 Eigen::Matrix<double, dim, 1> v(def_grad.rows());
483 Eigen::Matrix<double, dim, 1> w(def_grad.rows());
484
485 u = def_grad.col(0);
486 v = def_grad.col(1);
487 w = def_grad.col(2);
488
489 delJ_delF.col(0) = cross<dim>(v, w);
490 delJ_delF.col(1) = cross<dim>(w, u);
491 delJ_delF.col(2) = cross<dim>(u, v);
492 }
493
494 double lambda, mu;
495 evaluate_lame_parameters(params_, data, p, lambda, mu);
496
497 Eigen::Matrix<double, dim, dim> gradient_temp = mu * def_grad - mu * (1 / J) * delJ_delF + lambda * log_det_j * (1 / J) * delJ_delF;
498 Eigen::Matrix<double, n_basis, dim> gradient = delF_delU * gradient_temp.transpose();
499
500 double val = mu / 2 * ((def_grad.transpose() * def_grad).trace() - size() - 2 * log_det_j) + lambda / 2 * log_det_j * log_det_j;
501
502 G.noalias() += gradient * data.da(p);
503 }
504
505 Eigen::Matrix<double, dim, n_basis> G_T = G.transpose();
506
507 constexpr int N = (n_basis == Eigen::Dynamic) ? Eigen::Dynamic : n_basis * dim;
508 Eigen::Matrix<double, N, 1> temp(Eigen::Map<Eigen::Matrix<double, N, 1>>(G_T.data(), G_T.size()));
509 G_flattened = temp;
510 }
511
512 template <int n_basis, int dim, typename Matrix>
513 void NeoHookeanElasticity::compute_energy_hessian_aux_fast(const NonLinearAssemblerData &data, Eigen::MatrixBase<Matrix> &H) const
514 {
515 assert(data.x.cols() == 1);
516
517 const int n_pts = data.da.size();
518
519 Eigen::Matrix<double, n_basis, dim> local_disp(data.vals.basis_values.size(), size());
520 local_disp.setZero();
521 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
522 {
523 const auto &bs = data.vals.basis_values[i];
524 for (size_t ii = 0; ii < bs.global.size(); ++ii)
525 {
526 local_disp.row(i) += bs.global[ii].val * data.x.block<dim, 1>(bs.global[ii].index * size(), 0).transpose();
527 }
528 }
529
530 Eigen::VectorXd jacs;
532 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
533
534 Eigen::Matrix<double, dim, dim> def_grad(size(), size());
535
536 for (long p = 0; p < n_pts; ++p)
537 {
538 Eigen::Matrix<double, n_basis, dim> grad(data.vals.basis_values.size(), size());
539
540 for (size_t i = 0; i < data.vals.basis_values.size(); ++i)
541 {
542 grad.row(i) = data.vals.basis_values[i].grad.row(p);
543 }
544
545 Eigen::Matrix<double, dim, dim> jac_it = data.vals.jac_it[p];
546 const Eigen::Matrix<double, n_basis, dim> delF_delU = grad * jac_it;
547
548 // Id + grad d
549 def_grad = local_disp.transpose() * delF_delU + Eigen::Matrix<double, dim, dim>::Identity(size(), size());
550
551 const double J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
552 double log_det_j = log(J);
553
554 Eigen::Matrix<double, dim, dim> delJ_delF(size(), size());
555 delJ_delF.setZero();
556 Eigen::Matrix<double, dim * dim, dim * dim> del2J_delF2(size() * size(), size() * size());
557 del2J_delF2.setZero();
558
559 if (dim == 2)
560 {
561 delJ_delF(0, 0) = def_grad(1, 1);
562 delJ_delF(0, 1) = -def_grad(1, 0);
563 delJ_delF(1, 0) = -def_grad(0, 1);
564 delJ_delF(1, 1) = def_grad(0, 0);
565
566 del2J_delF2(0, 3) = 1;
567 del2J_delF2(1, 2) = -1;
568 del2J_delF2(2, 1) = -1;
569 del2J_delF2(3, 0) = 1;
570 }
571 else if (size() == 3)
572 {
573 Eigen::Matrix<double, dim, 1> u(def_grad.rows());
574 Eigen::Matrix<double, dim, 1> v(def_grad.rows());
575 Eigen::Matrix<double, dim, 1> w(def_grad.rows());
576
577 u = def_grad.col(0);
578 v = def_grad.col(1);
579 w = def_grad.col(2);
580
581 delJ_delF.col(0) = cross<dim>(v, w);
582 delJ_delF.col(1) = cross<dim>(w, u);
583 delJ_delF.col(2) = cross<dim>(u, v);
584
585 del2J_delF2.template block<dim, dim>(0, 6) = hat<dim>(v);
586 del2J_delF2.template block<dim, dim>(6, 0) = -hat<dim>(v);
587 del2J_delF2.template block<dim, dim>(0, 3) = -hat<dim>(w);
588 del2J_delF2.template block<dim, dim>(3, 0) = hat<dim>(w);
589 del2J_delF2.template block<dim, dim>(3, 6) = -hat<dim>(u);
590 del2J_delF2.template block<dim, dim>(6, 3) = hat<dim>(u);
591 }
592
593 double lambda, mu;
594 evaluate_lame_parameters(params_, data, p, lambda, mu);
595
596 Eigen::Matrix<double, dim * dim, dim * dim> id = Eigen::Matrix<double, dim * dim, dim * dim>::Identity(size() * size(), size() * size());
597
598 Eigen::Matrix<double, dim * dim, 1> g_j = Eigen::Map<const Eigen::Matrix<double, dim * dim, 1>>(delJ_delF.data(), delJ_delF.size());
599
600 Eigen::Matrix<double, dim * dim, dim * dim> hessian_temp = (mu * id) + (((mu + lambda * (1 - log_det_j)) / (J * J)) * (g_j * g_j.transpose())) + (((lambda * log_det_j - mu) / (J)) * del2J_delF2);
601
602 for (int a = 0; a < local_disp.rows(); ++a)
603 {
604 // With column-major vec(F), B_a consists of g_a(k) * I blocks.
605 Eigen::Matrix<double, dim, dim * dim> BaA =
606 delF_delU(a, 0) * hessian_temp.template block<dim, dim * dim>(0, 0);
607 for (int k = 1; k < dim; ++k)
608 BaA += delF_delU(a, k) * hessian_temp.template block<dim, dim * dim>(k * dim, 0);
609 BaA *= data.da(p);
610
611 for (int b = 0; b < local_disp.rows(); ++b)
612 {
613 Eigen::Matrix<double, dim, dim> Kab =
614 delF_delU(b, 0) * BaA.template block<dim, dim>(0, 0);
615 for (int k = 1; k < dim; ++k)
616 Kab += delF_delU(b, k) * BaA.template block<dim, dim>(0, k * dim);
617 H.template block<dim, dim>(a * dim, b * dim) += Kab;
618 }
619 }
620 }
621 }
622
624 const OptAssemblerData &data,
625 const Eigen::MatrixXd &mat,
626 Eigen::MatrixXd &stress,
627 Eigen::MatrixXd &result) const
628 {
629 const double t = data.t;
630 const int el_id = data.el_id;
631 const Eigen::MatrixXd &local_pts = data.local_pts;
632 const Eigen::MatrixXd &global_pts = data.global_pts;
633 const Eigen::MatrixXd &grad_u_i = data.grad_u_i;
634
635 double lambda, mu;
636 params_.lambda_mu(local_pts, global_pts, t, el_id, lambda, mu);
637
638 Eigen::MatrixXd def_grad = Eigen::MatrixXd::Identity(grad_u_i.rows(), grad_u_i.cols()) + grad_u_i;
639 Eigen::MatrixXd FmT = def_grad.inverse().transpose();
640
641 stress = mu * (def_grad - FmT) + lambda * std::log(def_grad.determinant()) * FmT;
642 result = mu * mat + FmT * mat.transpose() * FmT * (mu - lambda * std::log(def_grad.determinant())) + lambda * (FmT.array() * mat.array()).sum() * FmT;
643 }
644
646 const OptAssemblerData &data,
647 Eigen::MatrixXd &stress,
648 Eigen::MatrixXd &result) const
649 {
650 const double t = data.t;
651 const int el_id = data.el_id;
652 const Eigen::MatrixXd &local_pts = data.local_pts;
653 const Eigen::MatrixXd &global_pts = data.global_pts;
654 const Eigen::MatrixXd &grad_u_i = data.grad_u_i;
655
656 double lambda, mu;
657 params_.lambda_mu(local_pts, global_pts, t, el_id, lambda, mu);
658
659 Eigen::MatrixXd def_grad = Eigen::MatrixXd::Identity(grad_u_i.rows(), grad_u_i.cols()) + grad_u_i;
660 Eigen::MatrixXd FmT = def_grad.inverse().transpose();
661
662 stress = mu * (def_grad - FmT) + lambda * std::log(def_grad.determinant()) * FmT;
663 result = mu * stress + FmT * stress.transpose() * FmT * (mu - lambda * std::log(def_grad.determinant())) + lambda * (FmT.array() * stress.array()).sum() * FmT;
664 }
665
667 const OptAssemblerData &data,
668 const Eigen::MatrixXd &vect,
669 Eigen::MatrixXd &stress,
670 Eigen::MatrixXd &result) const
671 {
672 const double t = data.t;
673 const int el_id = data.el_id;
674 const Eigen::MatrixXd &local_pts = data.local_pts;
675 const Eigen::MatrixXd &global_pts = data.global_pts;
676 const Eigen::MatrixXd &grad_u_i = data.grad_u_i;
677
678 double lambda, mu;
679 params_.lambda_mu(local_pts, global_pts, t, el_id, lambda, mu);
680
681 Eigen::MatrixXd def_grad = Eigen::MatrixXd::Identity(grad_u_i.rows(), grad_u_i.cols()) + grad_u_i;
682 Eigen::MatrixXd FmT = def_grad.inverse().transpose();
683
684 stress = mu * (def_grad - FmT) + lambda * std::log(def_grad.determinant()) * FmT;
685 result.setZero(size() * size(), size());
686 if (vect.rows() == 1)
687 for (int i = 0; i < size(); ++i)
688 for (int j = 0; j < size(); ++j)
689 for (int l = 0; l < size(); ++l)
690 {
691 result(i * size() + j, l) += mu * vect(i) * ((j == l) ? 1 : 0);
692 // For some reason, the second gives lower error. From the formula, though, it should be the following.
693 // result(i * size() + j, l) += (mu - lambda * std::log(def_grad.determinant())) * FmT(j, l) * (FmT.col(i).array() * vect.transpose().array()).sum();
694 result(i * size() + j, l) += (mu - lambda * std::log(def_grad.determinant())) * FmT(i, l) * (FmT.col(j).array() * vect.transpose().array()).sum();
695 result(i * size() + j, l) += lambda * FmT(i, j) * (FmT.col(l).array() * vect.transpose().array()).sum();
696 }
697 else
698 for (int i = 0; i < size(); ++i)
699 for (int j = 0; j < size(); ++j)
700 for (int k = 0; k < size(); ++k)
701 {
702 result(i * size() + j, k) += mu * vect(j) * ((i == k) ? 1 : 0);
703 // For some reason, the second gives lower error. From the formula, though, it should be the following.
704 // result(i * size() + j, k) += (mu - lambda * std::log(def_grad.determinant())) * FmT(k, j) * (FmT.row(i).array() * vect.transpose().array()).sum();
705 result(i * size() + j, k) += (mu - lambda * std::log(def_grad.determinant())) * FmT(k, i) * (FmT.row(j).array() * vect.transpose().array()).sum();
706 result(i * size() + j, k) += lambda * FmT(i, j) * (FmT.row(k).array() * vect.transpose().array()).sum();
707 }
708 }
709
711 const OptAssemblerData &data,
712 Eigen::MatrixXd &dstress_dmu,
713 Eigen::MatrixXd &dstress_dlambda) const
714 {
715 const double t = data.t;
716 const int el_id = data.el_id;
717 const Eigen::MatrixXd &local_pts = data.local_pts;
718 const Eigen::MatrixXd &global_pts = data.global_pts;
719 const Eigen::MatrixXd &grad_u_i = data.grad_u_i;
720
721 Eigen::MatrixXd def_grad = Eigen::MatrixXd::Identity(grad_u_i.rows(), grad_u_i.cols()) + grad_u_i;
722 Eigen::MatrixXd FmT = def_grad.inverse().transpose();
723 dstress_dmu = def_grad - FmT;
724 dstress_dlambda = std::log(def_grad.determinant()) * FmT;
725 }
726
727 std::map<std::string, Assembler::ParamFunc> NeoHookeanElasticity::parameters() const
728 {
729 std::map<std::string, ParamFunc> res;
730 const auto &params = params_;
731 const int size = this->size();
732
733 res["lambda"] = [&params](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
734 double lambda, mu;
735
736 params.lambda_mu(uv, p, t, e, lambda, mu);
737 return lambda;
738 };
739
740 res["mu"] = [&params](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
741 double lambda, mu;
742
743 params.lambda_mu(uv, p, t, e, lambda, mu);
744 return mu;
745 };
746
747 res["E"] = [&params, size](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
748 double lambda, mu;
749 params.lambda_mu(uv, p, t, e, lambda, mu);
750
751 if (size == 3)
752 return mu * (3.0 * lambda + 2.0 * mu) / (lambda + mu);
753 else
754 return 2 * mu * (2.0 * lambda + 2.0 * mu) / (lambda + 2.0 * mu);
755 };
756
757 res["nu"] = [&params, size](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
758 double lambda, mu;
759
760 params.lambda_mu(uv, p, t, e, lambda, mu);
761
762 if (size == 3)
763 return lambda / (2.0 * (lambda + mu));
764 else
765 return lambda / (lambda + 2.0 * mu);
766 };
767
768 return res;
769 }
770
771} // namespace polyfem::assembler
LocalHessian hessian
double val
ElementAssemblyValues vals
Eigen::RowVectorXd point
double J
int y
int z
int x
std::string stress() const
Definition Units.hpp:27
Scratch for one output point. All tensor storage has a fixed 3x3 capacity.
stores per element basis values at given quadrature points and geometric mapping
std::vector< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > > jac_it
Eigen::VectorXd eval_deformed_jacobian_determinant(const Eigen::VectorXd &disp) const
void lambda_mu(double px, double py, double pz, double x, double y, double z, double t, int el_id, double &lambda, double &mu) const
void add_multimaterial(const int index, const json &params, const bool is_volume, const std::string &stress_unit, const io::ResourceIO &resources)
Reusable element matrix with inline storage for small blocks and a dynamic fallback for high-order el...
void resize(const Eigen::Index rows, const Eigen::Index cols)
T compute_energy_aux(const NonLinearAssemblerData &data) const
double compute_energy(const NonLinearAssemblerData &data) const override
T elastic_energy_density(const RowVectorNd &uv, const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F) const
std::map< std::string, ParamFunc > parameters() const override
void compute_energy_aux_gradient_fast(const NonLinearAssemblerData &data, Eigen::VectorXd &G_flattened) const
void assemble_hessian(const NonLinearAssemblerData &data, LocalHessian &hessian) const override
VectorNd compute_rhs(const AutodiffHessianPt &pt) const override
void compute_stress_grad_multiply_stress(const OptAssemblerData &data, Eigen::MatrixXd &stress, Eigen::MatrixXd &result) const override
bool compute_output_stress(const OutputData &data, const ElementAssemblyValues &vals, Eigen::Index point, const MatrixNd &displacement_gradient, OutputStress &stress) const override
Return false to use a material's legacy assign_stress_tensor implementation.
void compute_energy_hessian_aux_fast(const NonLinearAssemblerData &data, Eigen::MatrixBase< Matrix > &H) const
void compute_dstress_dmu_dlambda(const OptAssemblerData &data, Eigen::MatrixXd &dstress_dmu, Eigen::MatrixXd &dstress_dlambda) const override
void compute_stress_grad_multiply_vect(const OptAssemblerData &data, const Eigen::MatrixXd &vect, Eigen::MatrixXd &stress, Eigen::MatrixXd &result) const override
void add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources) override
void compute_stress_grad_multiply_mat(const OptAssemblerData &data, const Eigen::MatrixXd &mat, Eigen::MatrixXd &stress, Eigen::MatrixXd &result) const override
Eigen::VectorXd assemble_gradient(const NonLinearAssemblerData &data) const override
void compute_stiffness_value(const double t, const assembler::ElementAssemblyValues &vals, const Eigen::MatrixXd &local_pts, const Eigen::MatrixXd &displacement, Eigen::MatrixXd &tensor) const override
const Eigen::MatrixXd & local_pts
Read-only interface to a hierarchical collection of simulation resources.
Used for test only.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AutoDiffGradMat
Eigen::Matrix< double, dim, dim > hat(const Eigen::Matrix< double, dim, 1 > &x)
Eigen::Matrix< double, dim, 1 > cross(const Eigen::Matrix< double, dim, 1 > &x, const Eigen::Matrix< double, dim, 1 > &y)
Eigen::Matrix< T, Eigen::Dynamic, 1 > AutoDiffVect
void neo_hookean_3d_function(const AutodiffHessianPt &pt, const double lambda, const double mu, Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > &res)
void neo_hookean_2d_function(const AutodiffHessianPt &pt, const double lambda, const double mu, Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > &res)
Eigen::VectorXd flatten(const Eigen::MatrixXd &X)
Flatten rowwises.
T determinant(const Eigen::Matrix< T, rows, cols, option, maxRow, maxCol > &mat)
void compute_disp_grad_at_quad(const assembler::NonLinearAssemblerData &data, const AutoDiffVect &local_disp, const int p, const int size, AutoDiffGradMat &def_grad)
Eigen::Matrix< AutodiffScalarHessian, Eigen::Dynamic, 1, 0, 3, 1 > AutodiffHessianPt
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Definition Types.hpp:14
void compute_diplacement_grad(const int size, const ElementAssemblyValues &vals, const Eigen::MatrixXd &local_pts, const int p, const Eigen::MatrixXd &displacement, Eigen::MatrixXd &displacement_grad)
void get_local_disp(const assembler::NonLinearAssemblerData &data, const int size, AutoDiffVect &local_disp)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13