10#ifndef PBAT_PHYSICS_STABLENEOHOOKEANENERGY_H
11#define PBAT_PHYSICS_STABLENEOHOOKEANENERGY_H
14#include "pbat/HostDevice.h"
15#include "pbat/math/linalg/mini/Matrix.h"
34 template <
class TScalar,
int M,
int N>
37 template <
class TScalar,
int M>
38 using SVector = pbat::math::linalg::mini::SVector<TScalar, M>;
40 static auto constexpr kDims = 1;
51 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
52 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
53 eval(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
65 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
67 grad(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
79 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
81 hessian(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
97 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
evalWithGrad(
99 typename TMatrix::ScalarType mu,
100 typename TMatrix::ScalarType lambda,
101 TMatrixGF& gF)
const;
120 typename TMatrix::ScalarType mu,
121 typename TMatrix::ScalarType lambda,
123 TMatrixHF& HF)
const;
141 typename TMatrix::ScalarType mu,
142 typename TMatrix::ScalarType lambda,
144 TMatrixHF& HF)
const;
147template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
149 [[maybe_unused]] TMatrix
const& F,
150 [[maybe_unused]]
typename TMatrix::ScalarType mu,
151 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
153 using ScalarType =
typename TMatrix::ScalarType;
155 psi = (1.0 / 2.0) * lambda * ((F[0] - 1 - mu / lambda) * (F[0] - 1 - mu / lambda)) +
156 (1.0 / 2.0) * mu * (((F[0]) * (F[0])) - 1);
169template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
172 [[maybe_unused]] TMatrix
const& F,
173 [[maybe_unused]]
typename TMatrix::ScalarType mu,
174 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
176 using ScalarType =
typename TMatrix::ScalarType;
178 G[0] = (1.0 / 2.0) * lambda * (2 * F[0] - 2 - 2 * mu / lambda) + mu * F[0];
191template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
194 [[maybe_unused]] TMatrix
const& F,
195 [[maybe_unused]]
typename TMatrix::ScalarType mu,
196 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
198 using ScalarType =
typename TMatrix::ScalarType;
208 [[maybe_unused]] TMatrix
const& F,
209 [[maybe_unused]]
typename TMatrix::ScalarType mu,
210 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
214 TMatrixGF::kRows == 1 and TMatrixGF::kCols == 1,
215 "Grad w.r.t. F must have dimensions 1x1");
216 using ScalarType =
typename TMatrix::ScalarType;
218 ScalarType
const a0 = mu / lambda;
219 ScalarType
const a1 = (1.0 / 2.0) * lambda;
220 psi = a1 * ((-a0 + F[0] - 1) * (-a0 + F[0] - 1)) + (1.0 / 2.0) * mu * (((F[0]) * (F[0])) - 1);
221 gF[0] = a1 * (-2 * a0 + 2 * F[0] - 2) + mu * F[0];
230 [[maybe_unused]] TMatrix
const& F,
231 [[maybe_unused]]
typename TMatrix::ScalarType mu,
232 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
237 TMatrixGF::kRows == 1 and TMatrixGF::kCols == 1,
238 "Grad w.r.t. F must have dimensions 1x1");
240 TMatrixHF::kRows == 1 and TMatrixHF::kCols == 1,
241 "Hessian w.r.t. F must have dimensions 1x1");
242 using ScalarType =
typename TMatrix::ScalarType;
244 ScalarType
const a0 = mu / lambda;
245 ScalarType
const a1 = (1.0 / 2.0) * lambda;
246 psi = a1 * ((-a0 + F[0] - 1) * (-a0 + F[0] - 1)) + (1.0 / 2.0) * mu * (((F[0]) * (F[0])) - 1);
247 gF[0] = a1 * (-2 * a0 + 2 * F[0] - 2) + mu * F[0];
257 [[maybe_unused]] TMatrix
const& F,
258 [[maybe_unused]]
typename TMatrix::ScalarType mu,
259 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
264 TMatrixGF::kRows == 1 and TMatrixGF::kCols == 1,
265 "Grad w.r.t. F must have dimensions 1x1");
267 TMatrixHF::kRows == 1 and TMatrixHF::kCols == 1,
268 "Hessian w.r.t. F must have dimensions 1x1");
269 gF[0] = (1.0 / 2.0) * lambda * (2 * F[0] - 2 - 2 * mu / lambda) + mu * F[0];
282 template <
class TScalar,
int M,
int N>
285 template <
class TScalar,
int M>
286 using SVector = pbat::math::linalg::mini::SVector<TScalar, M>;
299 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
300 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
301 eval(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
313 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
315 grad(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
327 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
329 hessian(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
345 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
evalWithGrad(
347 typename TMatrix::ScalarType mu,
348 typename TMatrix::ScalarType lambda,
349 TMatrixGF& gF)
const;
368 typename TMatrix::ScalarType mu,
369 typename TMatrix::ScalarType lambda,
371 TMatrixHF& HF)
const;
389 typename TMatrix::ScalarType mu,
390 typename TMatrix::ScalarType lambda,
392 TMatrixHF& HF)
const;
395template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
397 [[maybe_unused]] TMatrix
const& F,
398 [[maybe_unused]]
typename TMatrix::ScalarType mu,
399 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
401 using ScalarType =
typename TMatrix::ScalarType;
403 psi = (1.0 / 2.0) * lambda *
404 ((F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda) *
405 (F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda)) +
407 (((F[0]) * (F[0])) + ((F[1]) * (F[1])) + ((F[2]) * (F[2])) + ((F[3]) * (F[3])) - 2);
420template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
423 [[maybe_unused]] TMatrix
const& F,
424 [[maybe_unused]]
typename TMatrix::ScalarType mu,
425 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
427 using ScalarType =
typename TMatrix::ScalarType;
429 ScalarType
const a0 = lambda * (F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda);
430 G[0] = a0 * F[3] + mu * F[0];
431 G[1] = -a0 * F[2] + mu * F[1];
432 G[2] = -a0 * F[1] + mu * F[2];
433 G[3] = a0 * F[0] + mu * F[3];
446template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
449 [[maybe_unused]] TMatrix
const& F,
450 [[maybe_unused]]
typename TMatrix::ScalarType mu,
451 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
453 using ScalarType =
typename TMatrix::ScalarType;
455 ScalarType
const a0 = lambda * F[3];
456 ScalarType
const a1 = -a0 * F[2];
457 ScalarType
const a2 = -a0 * F[1];
458 ScalarType
const a3 = lambda * (F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda);
459 ScalarType
const a4 = a3 + lambda * F[0] * F[3];
460 ScalarType
const a5 = -a3 + lambda * F[1] * F[2];
461 ScalarType
const a6 = lambda * F[0];
462 ScalarType
const a7 = -a6 * F[2];
463 ScalarType
const a8 = -a6 * F[1];
464 H[0] = lambda * ((F[3]) * (F[3])) + mu;
469 H[5] = lambda * ((F[2]) * (F[2])) + mu;
474 H[10] = lambda * ((F[1]) * (F[1])) + mu;
479 H[15] = lambda * ((F[0]) * (F[0])) + mu;
487 [[maybe_unused]] TMatrix
const& F,
488 [[maybe_unused]]
typename TMatrix::ScalarType mu,
489 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
493 TMatrixGF::kRows == 4 and TMatrixGF::kCols == 1,
494 "Grad w.r.t. F must have dimensions 4x1");
495 using ScalarType =
typename TMatrix::ScalarType;
497 ScalarType
const a0 = F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda;
498 ScalarType
const a1 = a0 * lambda;
499 psi = (1.0 / 2.0) * ((a0) * (a0)) * lambda +
501 (((F[0]) * (F[0])) + ((F[1]) * (F[1])) + ((F[2]) * (F[2])) + ((F[3]) * (F[3])) - 2);
502 gF[0] = a1 * F[3] + mu * F[0];
503 gF[1] = -a1 * F[2] + mu * F[1];
504 gF[2] = -a1 * F[1] + mu * F[2];
505 gF[3] = a1 * F[0] + mu * F[3];
514 [[maybe_unused]] TMatrix
const& F,
515 [[maybe_unused]]
typename TMatrix::ScalarType mu,
516 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
521 TMatrixGF::kRows == 4 and TMatrixGF::kCols == 1,
522 "Grad w.r.t. F must have dimensions 4x1");
524 TMatrixHF::kRows == 4 and TMatrixHF::kCols == 4,
525 "Hessian w.r.t. F must have dimensions 4x4");
526 using ScalarType =
typename TMatrix::ScalarType;
528 ScalarType
const a0 = ((F[0]) * (F[0]));
529 ScalarType
const a1 = ((F[1]) * (F[1]));
530 ScalarType
const a2 = ((F[2]) * (F[2]));
531 ScalarType
const a3 = ((F[3]) * (F[3]));
532 ScalarType
const a4 = F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda;
533 ScalarType
const a5 = a4 * lambda;
534 ScalarType
const a6 = lambda * F[3];
535 ScalarType
const a7 = -a6 * F[2];
536 ScalarType
const a8 = -a6 * F[1];
537 ScalarType
const a9 = a5 + lambda * F[0] * F[3];
538 ScalarType
const a10 = -a5 + lambda * F[1] * F[2];
539 ScalarType
const a11 = lambda * F[0];
540 ScalarType
const a12 = -a11 * F[2];
541 ScalarType
const a13 = -a11 * F[1];
542 psi = (1.0 / 2.0) * ((a4) * (a4)) * lambda + (1.0 / 2.0) * mu * (a0 + a1 + a2 + a3 - 2);
543 gF[0] = a5 * F[3] + mu * F[0];
544 gF[1] = -a5 * F[2] + mu * F[1];
545 gF[2] = -a5 * F[1] + mu * F[2];
546 gF[3] = a5 * F[0] + mu * F[3];
547 HF[0] = a3 * lambda + mu;
552 HF[5] = a2 * lambda + mu;
557 HF[10] = a1 * lambda + mu;
562 HF[15] = a0 * lambda + mu;
571 [[maybe_unused]] TMatrix
const& F,
572 [[maybe_unused]]
typename TMatrix::ScalarType mu,
573 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
578 TMatrixGF::kRows == 4 and TMatrixGF::kCols == 1,
579 "Grad w.r.t. F must have dimensions 4x1");
581 TMatrixHF::kRows == 4 and TMatrixHF::kCols == 4,
582 "Hessian w.r.t. F must have dimensions 4x4");
583 using ScalarType =
typename TMatrix::ScalarType;
584 ScalarType
const a0 = lambda * (F[0] * F[3] - F[1] * F[2] - 1 - mu / lambda);
585 ScalarType
const a1 = lambda * F[3];
586 ScalarType
const a2 = -a1 * F[2];
587 ScalarType
const a3 = -a1 * F[1];
588 ScalarType
const a4 = a0 + lambda * F[0] * F[3];
589 ScalarType
const a5 = -a0 + lambda * F[1] * F[2];
590 ScalarType
const a6 = lambda * F[0];
591 ScalarType
const a7 = -a6 * F[2];
592 ScalarType
const a8 = -a6 * F[1];
593 gF[0] = a0 * F[3] + mu * F[0];
594 gF[1] = -a0 * F[2] + mu * F[1];
595 gF[2] = -a0 * F[1] + mu * F[2];
596 gF[3] = a0 * F[0] + mu * F[3];
597 HF[0] = lambda * ((F[3]) * (F[3])) + mu;
602 HF[5] = lambda * ((F[2]) * (F[2])) + mu;
607 HF[10] = lambda * ((F[1]) * (F[1])) + mu;
612 HF[15] = lambda * ((F[0]) * (F[0])) + mu;
624 template <
class TScalar,
int M,
int N>
627 template <
class TScalar,
int M>
628 using SVector = pbat::math::linalg::mini::SVector<TScalar, M>;
641 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
642 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
643 eval(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
655 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
657 grad(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
669 template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
671 hessian(TMatrix
const& F,
typename TMatrix::ScalarType mu,
typename TMatrix::ScalarType lambda)
687 PBAT_HOST_DEVICE
typename TMatrix::ScalarType
evalWithGrad(
689 typename TMatrix::ScalarType mu,
690 typename TMatrix::ScalarType lambda,
691 TMatrixGF& gF)
const;
710 typename TMatrix::ScalarType mu,
711 typename TMatrix::ScalarType lambda,
713 TMatrixHF& HF)
const;
731 typename TMatrix::ScalarType mu,
732 typename TMatrix::ScalarType lambda,
734 TMatrixHF& HF)
const;
737template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
739 [[maybe_unused]] TMatrix
const& F,
740 [[maybe_unused]]
typename TMatrix::ScalarType mu,
741 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
743 using ScalarType =
typename TMatrix::ScalarType;
745 psi = (1.0 / 2.0) * lambda *
746 ((F[0] * F[4] * F[8] - F[0] * F[5] * F[7] - F[1] * F[3] * F[8] + F[1] * F[5] * F[6] +
747 F[2] * F[3] * F[7] - F[2] * F[4] * F[6] - 1 - mu / lambda) *
748 (F[0] * F[4] * F[8] - F[0] * F[5] * F[7] - F[1] * F[3] * F[8] + F[1] * F[5] * F[6] +
749 F[2] * F[3] * F[7] - F[2] * F[4] * F[6] - 1 - mu / lambda)) +
751 (((F[0]) * (F[0])) + ((F[1]) * (F[1])) + ((F[2]) * (F[2])) + ((F[3]) * (F[3])) +
752 ((F[4]) * (F[4])) + ((F[5]) * (F[5])) + ((F[6]) * (F[6])) + ((F[7]) * (F[7])) +
753 ((F[8]) * (F[8])) - 3);
766template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
769 [[maybe_unused]] TMatrix
const& F,
770 [[maybe_unused]]
typename TMatrix::ScalarType mu,
771 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
773 using ScalarType =
typename TMatrix::ScalarType;
775 ScalarType
const a0 = F[5] * F[7];
776 ScalarType
const a1 = F[3] * F[8];
777 ScalarType
const a2 = F[4] * F[6];
778 ScalarType
const a3 = (1.0 / 2.0) * lambda *
779 (-a0 * F[0] - a1 * F[1] - a2 * F[2] + F[0] * F[4] * F[8] +
780 F[1] * F[5] * F[6] + F[2] * F[3] * F[7] - 1 - mu / lambda);
781 ScalarType
const a4 = 2 * F[8];
782 ScalarType
const a5 = 2 * F[2];
783 ScalarType
const a6 = 2 * F[0];
784 ScalarType
const a7 = 2 * F[1];
785 G[0] = a3 * (-2 * a0 + 2 * F[4] * F[8]) + mu * F[0];
786 G[1] = a3 * (-2 * a1 + 2 * F[5] * F[6]) + mu * F[1];
787 G[2] = a3 * (-2 * a2 + 2 * F[3] * F[7]) + mu * F[2];
788 G[3] = a3 * (-a4 * F[1] + 2 * F[2] * F[7]) + mu * F[3];
789 G[4] = a3 * (a4 * F[0] - a5 * F[6]) + mu * F[4];
790 G[5] = a3 * (-a6 * F[7] + 2 * F[1] * F[6]) + mu * F[5];
791 G[6] = a3 * (-a5 * F[4] + a7 * F[5]) + mu * F[6];
792 G[7] = a3 * (-a6 * F[5] + 2 * F[2] * F[3]) + mu * F[7];
793 G[8] = a3 * (a6 * F[4] - a7 * F[3]) + mu * F[8];
806template <math::linalg::mini::CReadableVectorizedMatrix TMatrix>
809 [[maybe_unused]] TMatrix
const& F,
810 [[maybe_unused]]
typename TMatrix::ScalarType mu,
811 [[maybe_unused]]
typename TMatrix::ScalarType lambda)
const
813 using ScalarType =
typename TMatrix::ScalarType;
815 ScalarType
const a0 = F[4] * F[8];
816 ScalarType
const a1 = F[5] * F[7];
817 ScalarType
const a2 = a0 - a1;
818 ScalarType
const a3 = (1.0 / 2.0) * lambda;
819 ScalarType
const a4 = a3 * (2 * a0 - 2 * a1);
820 ScalarType
const a5 = F[3] * F[8];
821 ScalarType
const a6 = -a5 + F[5] * F[6];
822 ScalarType
const a7 = F[3] * F[7];
823 ScalarType
const a8 = F[4] * F[6];
824 ScalarType
const a9 = a7 - a8;
825 ScalarType
const a10 = F[1] * F[8];
826 ScalarType
const a11 = -a10 + F[2] * F[7];
827 ScalarType
const a12 = F[0] * F[8];
828 ScalarType
const a13 = F[2] * F[6];
829 ScalarType
const a14 = a12 - a13;
830 ScalarType
const a15 = lambda * (-a1 * F[0] - a5 * F[1] - a8 * F[2] + F[0] * F[4] * F[8] +
831 F[1] * F[5] * F[6] + F[2] * F[3] * F[7] - 1 - mu / lambda);
832 ScalarType
const a16 = a15 * F[8];
833 ScalarType
const a17 = F[0] * F[7];
834 ScalarType
const a18 = -a17 + F[1] * F[6];
835 ScalarType
const a19 = a15 * F[7];
836 ScalarType
const a20 = -a19;
837 ScalarType
const a21 = F[1] * F[5];
838 ScalarType
const a22 = F[2] * F[4];
839 ScalarType
const a23 = a21 - a22;
840 ScalarType
const a24 = F[0] * F[5];
841 ScalarType
const a25 = -a24 + F[2] * F[3];
842 ScalarType
const a26 = a15 * F[5];
843 ScalarType
const a27 = -a26;
844 ScalarType
const a28 = F[0] * F[4];
845 ScalarType
const a29 = F[1] * F[3];
846 ScalarType
const a30 = a28 - a29;
847 ScalarType
const a31 = a15 * F[4];
848 ScalarType
const a32 = a3 * (-2 * a5 + 2 * F[5] * F[6]);
849 ScalarType
const a33 = -a16;
850 ScalarType
const a34 = a15 * F[6];
851 ScalarType
const a35 = a15 * F[3];
852 ScalarType
const a36 = -a35;
853 ScalarType
const a37 = a3 * (2 * a7 - 2 * a8);
854 ScalarType
const a38 = -a34;
855 ScalarType
const a39 = -a31;
856 ScalarType
const a40 = a3 * (-2 * a10 + 2 * F[2] * F[7]);
857 ScalarType
const a41 = a15 * F[2];
858 ScalarType
const a42 = a15 * F[1];
859 ScalarType
const a43 = -a42;
860 ScalarType
const a44 = a3 * (2 * a12 - 2 * a13);
861 ScalarType
const a45 = -a41;
862 ScalarType
const a46 = a15 * F[0];
863 ScalarType
const a47 = a3 * (-2 * a17 + 2 * F[1] * F[6]);
864 ScalarType
const a48 = -a46;
865 ScalarType
const a49 = a3 * (2 * a21 - 2 * a22);
866 ScalarType
const a50 = a3 * (-2 * a24 + 2 * F[2] * F[3]);
867 ScalarType
const a51 = a3 * (2 * a28 - 2 * a29);
872 H[4] = a14 * a4 + a16;
873 H[5] = a18 * a4 + a20;
875 H[7] = a25 * a4 + a27;
876 H[8] = a30 * a4 + a31;
878 H[10] = a32 * a6 + mu;
880 H[12] = a11 * a32 + a33;
882 H[14] = a18 * a32 + a34;
883 H[15] = a23 * a32 + a26;
885 H[17] = a30 * a32 + a36;
888 H[20] = a37 * a9 + mu;
889 H[21] = a11 * a37 + a19;
890 H[22] = a14 * a37 + a38;
892 H[24] = a23 * a37 + a39;
893 H[25] = a25 * a37 + a35;
896 H[28] = a33 + a40 * a6;
897 H[29] = a19 + a40 * a9;
898 H[30] = a11 * a40 + mu;
902 H[34] = a25 * a40 + a41;
903 H[35] = a30 * a40 + a43;
904 H[36] = a16 + a2 * a44;
906 H[38] = a38 + a44 * a9;
908 H[40] = a14 * a44 + mu;
910 H[42] = a23 * a44 + a45;
912 H[44] = a30 * a44 + a46;
913 H[45] = a2 * a47 + a20;
914 H[46] = a34 + a47 * a6;
918 H[50] = a18 * a47 + mu;
919 H[51] = a23 * a47 + a42;
920 H[52] = a25 * a47 + a48;
923 H[55] = a26 + a49 * a6;
924 H[56] = a39 + a49 * a9;
926 H[58] = a14 * a49 + a45;
927 H[59] = a18 * a49 + a42;
928 H[60] = a23 * a49 + mu;
931 H[63] = a2 * a50 + a27;
933 H[65] = a35 + a50 * a9;
934 H[66] = a11 * a50 + a41;
936 H[68] = a18 * a50 + a48;
938 H[70] = a25 * a50 + mu;
940 H[72] = a2 * a51 + a31;
941 H[73] = a36 + a51 * a6;
943 H[75] = a11 * a51 + a43;
944 H[76] = a14 * a51 + a46;
948 H[80] = a30 * a51 + mu;
956 [[maybe_unused]] TMatrix
const& F,
957 [[maybe_unused]]
typename TMatrix::ScalarType mu,
958 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
962 TMatrixGF::kRows == 9 and TMatrixGF::kCols == 1,
963 "Grad w.r.t. F must have dimensions 9x1");
964 using ScalarType =
typename TMatrix::ScalarType;
966 ScalarType
const a0 = F[5] * F[7];
967 ScalarType
const a1 = F[3] * F[8];
968 ScalarType
const a2 = F[4] * F[6];
969 ScalarType
const a3 = -a0 * F[0] - a1 * F[1] - a2 * F[2] + F[0] * F[4] * F[8] +
970 F[1] * F[5] * F[6] + F[2] * F[3] * F[7] - 1 - mu / lambda;
971 ScalarType
const a4 = (1.0 / 2.0) * lambda;
972 ScalarType
const a5 = a3 * a4;
973 ScalarType
const a6 = 2 * F[8];
974 ScalarType
const a7 = 2 * F[2];
975 ScalarType
const a8 = 2 * F[0];
976 ScalarType
const a9 = 2 * F[1];
977 psi = ((a3) * (a3)) * a4 + (1.0 / 2.0) * mu *
978 (((F[0]) * (F[0])) + ((F[1]) * (F[1])) + ((F[2]) * (F[2])) +
979 ((F[3]) * (F[3])) + ((F[4]) * (F[4])) + ((F[5]) * (F[5])) +
980 ((F[6]) * (F[6])) + ((F[7]) * (F[7])) + ((F[8]) * (F[8])) - 3);
981 gF[0] = a5 * (-2 * a0 + 2 * F[4] * F[8]) + mu * F[0];
982 gF[1] = a5 * (-2 * a1 + 2 * F[5] * F[6]) + mu * F[1];
983 gF[2] = a5 * (-2 * a2 + 2 * F[3] * F[7]) + mu * F[2];
984 gF[3] = a5 * (-a6 * F[1] + 2 * F[2] * F[7]) + mu * F[3];
985 gF[4] = a5 * (a6 * F[0] - a7 * F[6]) + mu * F[4];
986 gF[5] = a5 * (-a8 * F[7] + 2 * F[1] * F[6]) + mu * F[5];
987 gF[6] = a5 * (-a7 * F[4] + a9 * F[5]) + mu * F[6];
988 gF[7] = a5 * (-a8 * F[5] + 2 * F[2] * F[3]) + mu * F[7];
989 gF[8] = a5 * (a8 * F[4] - a9 * F[3]) + mu * F[8];
998 [[maybe_unused]] TMatrix
const& F,
999 [[maybe_unused]]
typename TMatrix::ScalarType mu,
1000 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
1002 TMatrixHF& HF)
const
1005 TMatrixGF::kRows == 9 and TMatrixGF::kCols == 1,
1006 "Grad w.r.t. F must have dimensions 9x1");
1008 TMatrixHF::kRows == 9 and TMatrixHF::kCols == 9,
1009 "Hessian w.r.t. F must have dimensions 9x9");
1010 using ScalarType =
typename TMatrix::ScalarType;
1012 ScalarType
const a0 = F[5] * F[7];
1013 ScalarType
const a1 = F[3] * F[8];
1014 ScalarType
const a2 = F[4] * F[6];
1015 ScalarType
const a3 = -a0 * F[0] - a1 * F[1] - a2 * F[2] + F[0] * F[4] * F[8] +
1016 F[1] * F[5] * F[6] + F[2] * F[3] * F[7] - 1 - mu / lambda;
1017 ScalarType
const a4 = (1.0 / 2.0) * lambda;
1018 ScalarType
const a5 = F[4] * F[8];
1019 ScalarType
const a6 = -2 * a0 + 2 * a5;
1020 ScalarType
const a7 = a3 * a4;
1021 ScalarType
const a8 = -2 * a1 + 2 * F[5] * F[6];
1022 ScalarType
const a9 = F[3] * F[7];
1023 ScalarType
const a10 = -2 * a2 + 2 * a9;
1024 ScalarType
const a11 = F[1] * F[8];
1025 ScalarType
const a12 = -2 * a11 + 2 * F[2] * F[7];
1026 ScalarType
const a13 = F[0] * F[8];
1027 ScalarType
const a14 = F[2] * F[6];
1028 ScalarType
const a15 = 2 * a13 - 2 * a14;
1029 ScalarType
const a16 = F[0] * F[7];
1030 ScalarType
const a17 = -2 * a16 + 2 * F[1] * F[6];
1031 ScalarType
const a18 = F[1] * F[5];
1032 ScalarType
const a19 = F[2] * F[4];
1033 ScalarType
const a20 = 2 * a18 - 2 * a19;
1034 ScalarType
const a21 = F[0] * F[5];
1035 ScalarType
const a22 = -2 * a21 + 2 * F[2] * F[3];
1036 ScalarType
const a23 = F[0] * F[4];
1037 ScalarType
const a24 = F[1] * F[3];
1038 ScalarType
const a25 = 2 * a23 - 2 * a24;
1039 ScalarType
const a26 = a4 * (-a0 + a5);
1040 ScalarType
const a27 = a3 * lambda;
1041 ScalarType
const a28 = a27 * F[8];
1042 ScalarType
const a29 = a27 * F[7];
1043 ScalarType
const a30 = -a29;
1044 ScalarType
const a31 = a27 * F[5];
1045 ScalarType
const a32 = -a31;
1046 ScalarType
const a33 = a27 * F[4];
1047 ScalarType
const a34 = a4 * (-a1 + F[5] * F[6]);
1048 ScalarType
const a35 = -a28;
1049 ScalarType
const a36 = a27 * F[6];
1050 ScalarType
const a37 = a27 * F[3];
1051 ScalarType
const a38 = -a37;
1052 ScalarType
const a39 = a4 * (-a2 + a9);
1053 ScalarType
const a40 = -a36;
1054 ScalarType
const a41 = -a33;
1055 ScalarType
const a42 = a4 * (-a11 + F[2] * F[7]);
1056 ScalarType
const a43 = a27 * F[2];
1057 ScalarType
const a44 = a27 * F[1];
1058 ScalarType
const a45 = -a44;
1059 ScalarType
const a46 = a4 * (a13 - a14);
1060 ScalarType
const a47 = -a43;
1061 ScalarType
const a48 = a27 * F[0];
1062 ScalarType
const a49 = a4 * (-a16 + F[1] * F[6]);
1063 ScalarType
const a50 = -a48;
1064 ScalarType
const a51 = a4 * (a18 - a19);
1065 ScalarType
const a52 = a4 * (-a21 + F[2] * F[3]);
1066 ScalarType
const a53 = a4 * (a23 - a24);
1067 psi = ((a3) * (a3)) * a4 + (1.0 / 2.0) * mu *
1068 (((F[0]) * (F[0])) + ((F[1]) * (F[1])) + ((F[2]) * (F[2])) +
1069 ((F[3]) * (F[3])) + ((F[4]) * (F[4])) + ((F[5]) * (F[5])) +
1070 ((F[6]) * (F[6])) + ((F[7]) * (F[7])) + ((F[8]) * (F[8])) - 3);
1071 gF[0] = a6 * a7 + mu * F[0];
1072 gF[1] = a7 * a8 + mu * F[1];
1073 gF[2] = a10 * a7 + mu * F[2];
1074 gF[3] = a12 * a7 + mu * F[3];
1075 gF[4] = a15 * a7 + mu * F[4];
1076 gF[5] = a17 * a7 + mu * F[5];
1077 gF[6] = a20 * a7 + mu * F[6];
1078 gF[7] = a22 * a7 + mu * F[7];
1079 gF[8] = a25 * a7 + mu * F[8];
1080 HF[0] = a26 * a6 + mu;
1084 HF[4] = a15 * a26 + a28;
1085 HF[5] = a17 * a26 + a30;
1087 HF[7] = a22 * a26 + a32;
1088 HF[8] = a25 * a26 + a33;
1090 HF[10] = a34 * a8 + mu;
1092 HF[12] = a12 * a34 + a35;
1094 HF[14] = a17 * a34 + a36;
1095 HF[15] = a20 * a34 + a31;
1097 HF[17] = a25 * a34 + a38;
1100 HF[20] = a10 * a39 + mu;
1101 HF[21] = a12 * a39 + a29;
1102 HF[22] = a15 * a39 + a40;
1104 HF[24] = a20 * a39 + a41;
1105 HF[25] = a22 * a39 + a37;
1108 HF[28] = a35 + a42 * a8;
1109 HF[29] = a10 * a42 + a29;
1110 HF[30] = a12 * a42 + mu;
1114 HF[34] = a22 * a42 + a43;
1115 HF[35] = a25 * a42 + a45;
1116 HF[36] = a28 + a46 * a6;
1118 HF[38] = a10 * a46 + a40;
1120 HF[40] = a15 * a46 + mu;
1122 HF[42] = a20 * a46 + a47;
1124 HF[44] = a25 * a46 + a48;
1125 HF[45] = a30 + a49 * a6;
1126 HF[46] = a36 + a49 * a8;
1130 HF[50] = a17 * a49 + mu;
1131 HF[51] = a20 * a49 + a44;
1132 HF[52] = a22 * a49 + a50;
1135 HF[55] = a31 + a51 * a8;
1136 HF[56] = a10 * a51 + a41;
1138 HF[58] = a15 * a51 + a47;
1139 HF[59] = a17 * a51 + a44;
1140 HF[60] = a20 * a51 + mu;
1143 HF[63] = a32 + a52 * a6;
1145 HF[65] = a10 * a52 + a37;
1146 HF[66] = a12 * a52 + a43;
1148 HF[68] = a17 * a52 + a50;
1150 HF[70] = a22 * a52 + mu;
1152 HF[72] = a33 + a53 * a6;
1153 HF[73] = a38 + a53 * a8;
1155 HF[75] = a12 * a53 + a45;
1156 HF[76] = a15 * a53 + a48;
1160 HF[80] = a25 * a53 + mu;
1169 [[maybe_unused]] TMatrix
const& F,
1170 [[maybe_unused]]
typename TMatrix::ScalarType mu,
1171 [[maybe_unused]]
typename TMatrix::ScalarType lambda,
1173 TMatrixHF& HF)
const
1176 TMatrixGF::kRows == 9 and TMatrixGF::kCols == 1,
1177 "Grad w.r.t. F must have dimensions 9x1");
1179 TMatrixHF::kRows == 9 and TMatrixHF::kCols == 9,
1180 "Hessian w.r.t. F must have dimensions 9x9");
1181 using ScalarType =
typename TMatrix::ScalarType;
1182 ScalarType
const a0 = F[4] * F[8];
1183 ScalarType
const a1 = F[5] * F[7];
1184 ScalarType
const a2 = 2 * a0 - 2 * a1;
1185 ScalarType
const a3 = F[3] * F[8];
1186 ScalarType
const a4 = F[4] * F[6];
1187 ScalarType
const a5 = lambda * (-a1 * F[0] - a3 * F[1] - a4 * F[2] + F[0] * F[4] * F[8] +
1188 F[1] * F[5] * F[6] + F[2] * F[3] * F[7] - 1 - mu / lambda);
1189 ScalarType
const a6 = (1.0 / 2.0) * a5;
1190 ScalarType
const a7 = -2 * a3 + 2 * F[5] * F[6];
1191 ScalarType
const a8 = F[3] * F[7];
1192 ScalarType
const a9 = -2 * a4 + 2 * a8;
1193 ScalarType
const a10 = F[1] * F[8];
1194 ScalarType
const a11 = -2 * a10 + 2 * F[2] * F[7];
1195 ScalarType
const a12 = F[0] * F[8];
1196 ScalarType
const a13 = F[2] * F[6];
1197 ScalarType
const a14 = 2 * a12 - 2 * a13;
1198 ScalarType
const a15 = F[0] * F[7];
1199 ScalarType
const a16 = -2 * a15 + 2 * F[1] * F[6];
1200 ScalarType
const a17 = F[1] * F[5];
1201 ScalarType
const a18 = F[2] * F[4];
1202 ScalarType
const a19 = 2 * a17 - 2 * a18;
1203 ScalarType
const a20 = F[0] * F[5];
1204 ScalarType
const a21 = -2 * a20 + 2 * F[2] * F[3];
1205 ScalarType
const a22 = F[0] * F[4];
1206 ScalarType
const a23 = F[1] * F[3];
1207 ScalarType
const a24 = 2 * a22 - 2 * a23;
1208 ScalarType
const a25 = (1.0 / 2.0) * lambda;
1209 ScalarType
const a26 = a25 * (a0 - a1);
1210 ScalarType
const a27 = a5 * F[8];
1211 ScalarType
const a28 = a5 * F[7];
1212 ScalarType
const a29 = -a28;
1213 ScalarType
const a30 = a5 * F[5];
1214 ScalarType
const a31 = -a30;
1215 ScalarType
const a32 = a5 * F[4];
1216 ScalarType
const a33 = a25 * (-a3 + F[5] * F[6]);
1217 ScalarType
const a34 = -a27;
1218 ScalarType
const a35 = a5 * F[6];
1219 ScalarType
const a36 = a5 * F[3];
1220 ScalarType
const a37 = -a36;
1221 ScalarType
const a38 = a25 * (-a4 + a8);
1222 ScalarType
const a39 = -a35;
1223 ScalarType
const a40 = -a32;
1224 ScalarType
const a41 = a25 * (-a10 + F[2] * F[7]);
1225 ScalarType
const a42 = a5 * F[2];
1226 ScalarType
const a43 = a5 * F[1];
1227 ScalarType
const a44 = -a43;
1228 ScalarType
const a45 = a25 * (a12 - a13);
1229 ScalarType
const a46 = -a42;
1230 ScalarType
const a47 = a5 * F[0];
1231 ScalarType
const a48 = a25 * (-a15 + F[1] * F[6]);
1232 ScalarType
const a49 = -a47;
1233 ScalarType
const a50 = a25 * (a17 - a18);
1234 ScalarType
const a51 = a25 * (-a20 + F[2] * F[3]);
1235 ScalarType
const a52 = a25 * (a22 - a23);
1236 gF[0] = a2 * a6 + mu * F[0];
1237 gF[1] = a6 * a7 + mu * F[1];
1238 gF[2] = a6 * a9 + mu * F[2];
1239 gF[3] = a11 * a6 + mu * F[3];
1240 gF[4] = a14 * a6 + mu * F[4];
1241 gF[5] = a16 * a6 + mu * F[5];
1242 gF[6] = a19 * a6 + mu * F[6];
1243 gF[7] = a21 * a6 + mu * F[7];
1244 gF[8] = a24 * a6 + mu * F[8];
1245 HF[0] = a2 * a26 + mu;
1249 HF[4] = a14 * a26 + a27;
1250 HF[5] = a16 * a26 + a29;
1252 HF[7] = a21 * a26 + a31;
1253 HF[8] = a24 * a26 + a32;
1255 HF[10] = a33 * a7 + mu;
1257 HF[12] = a11 * a33 + a34;
1259 HF[14] = a16 * a33 + a35;
1260 HF[15] = a19 * a33 + a30;
1262 HF[17] = a24 * a33 + a37;
1265 HF[20] = a38 * a9 + mu;
1266 HF[21] = a11 * a38 + a28;
1267 HF[22] = a14 * a38 + a39;
1269 HF[24] = a19 * a38 + a40;
1270 HF[25] = a21 * a38 + a36;
1273 HF[28] = a34 + a41 * a7;
1274 HF[29] = a28 + a41 * a9;
1275 HF[30] = a11 * a41 + mu;
1279 HF[34] = a21 * a41 + a42;
1280 HF[35] = a24 * a41 + a44;
1281 HF[36] = a2 * a45 + a27;
1283 HF[38] = a39 + a45 * a9;
1285 HF[40] = a14 * a45 + mu;
1287 HF[42] = a19 * a45 + a46;
1289 HF[44] = a24 * a45 + a47;
1290 HF[45] = a2 * a48 + a29;
1291 HF[46] = a35 + a48 * a7;
1295 HF[50] = a16 * a48 + mu;
1296 HF[51] = a19 * a48 + a43;
1297 HF[52] = a21 * a48 + a49;
1300 HF[55] = a30 + a50 * a7;
1301 HF[56] = a40 + a50 * a9;
1303 HF[58] = a14 * a50 + a46;
1304 HF[59] = a16 * a50 + a43;
1305 HF[60] = a19 * a50 + mu;
1308 HF[63] = a2 * a51 + a31;
1310 HF[65] = a36 + a51 * a9;
1311 HF[66] = a11 * a51 + a42;
1313 HF[68] = a16 * a51 + a49;
1315 HF[70] = a21 * a51 + mu;
1317 HF[72] = a2 * a52 + a32;
1318 HF[73] = a37 + a52 * a7;
1320 HF[75] = a11 * a52 + a44;
1321 HF[76] = a14 * a52 + a47;
1325 HF[80] = a24 * a52 + mu;
The main namespace of the library.
Definition Aliases.h:15
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy with its gradient and hessian.
Definition StableNeoHookeanEnergy.h:229
PBAT_HOST_DEVICE SMatrix< typename TMatrix::ScalarType, 1, 1 > hessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy hessian.
pbat::math::linalg::mini::SMatrix< TScalar, M, N > SMatrix
Scalar matrix type.
Definition StableNeoHookeanEnergy.h:35
static auto constexpr kDims
Dimension of the space.
Definition StableNeoHookeanEnergy.h:40
pbat::math::linalg::mini::SVector< TScalar, M > SVector
Scalar vector type.
Definition StableNeoHookeanEnergy.h:38
PBAT_HOST_DEVICE SVector< typename TMatrix::ScalarType, 1 > grad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy gradient.
PBAT_HOST_DEVICE void gradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy gradient and hessian.
Definition StableNeoHookeanEnergy.h:256
PBAT_HOST_DEVICE TMatrix::ScalarType eval(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy.
Definition StableNeoHookeanEnergy.h:148
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGrad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF) const
Evaluate the elastic energy and its gradient.
Definition StableNeoHookeanEnergy.h:207
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy with its gradient and hessian.
Definition StableNeoHookeanEnergy.h:513
pbat::math::linalg::mini::SMatrix< TScalar, M, N > SMatrix
Scalar matrix type.
Definition StableNeoHookeanEnergy.h:283
PBAT_HOST_DEVICE void gradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy gradient and hessian.
Definition StableNeoHookeanEnergy.h:570
PBAT_HOST_DEVICE SVector< typename TMatrix::ScalarType, 4 > grad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy gradient.
PBAT_HOST_DEVICE SMatrix< typename TMatrix::ScalarType, 4, 4 > hessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy hessian.
PBAT_HOST_DEVICE TMatrix::ScalarType eval(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy.
Definition StableNeoHookeanEnergy.h:396
pbat::math::linalg::mini::SVector< TScalar, M > SVector
Scalar vector type.
Definition StableNeoHookeanEnergy.h:286
static auto constexpr kDims
Dimension of the space.
Definition StableNeoHookeanEnergy.h:288
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGrad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF) const
Evaluate the elastic energy and its gradient.
Definition StableNeoHookeanEnergy.h:486
static auto constexpr kDims
Dimension of the space.
Definition StableNeoHookeanEnergy.h:630
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGrad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF) const
Evaluate the elastic energy and its gradient.
Definition StableNeoHookeanEnergy.h:955
PBAT_HOST_DEVICE TMatrix::ScalarType evalWithGradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy with its gradient and hessian.
Definition StableNeoHookeanEnergy.h:997
pbat::math::linalg::mini::SMatrix< TScalar, M, N > SMatrix
Scalar matrix type.
Definition StableNeoHookeanEnergy.h:625
PBAT_HOST_DEVICE SMatrix< typename TMatrix::ScalarType, 9, 9 > hessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy hessian.
PBAT_HOST_DEVICE TMatrix::ScalarType eval(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy.
Definition StableNeoHookeanEnergy.h:738
pbat::math::linalg::mini::SVector< TScalar, M > SVector
Scalar vector type.
Definition StableNeoHookeanEnergy.h:628
PBAT_HOST_DEVICE SVector< typename TMatrix::ScalarType, 9 > grad(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda) const
Evaluate the elastic energy gradient.
PBAT_HOST_DEVICE void gradAndHessian(TMatrix const &F, typename TMatrix::ScalarType mu, typename TMatrix::ScalarType lambda, TMatrixGF &gF, TMatrixHF &HF) const
Evaluate the elastic energy gradient and hessian.
Definition StableNeoHookeanEnergy.h:1168
Definition StableNeoHookeanEnergy.h:23