PolyFEM
Loading...
Searching...
No Matches
AMIPSEnergy.hpp
Go to the documentation of this file.
1#pragma once
2
7
8#include <polyfem/Common.hpp>
9
10#include <Eigen/Dense>
11
12#include <functional>
13#include <iostream>
14#include <vector>
15
16namespace polyfem::assembler
17{
18 class AMIPSEnergy : public GenericElastic<AMIPSEnergy>
19 {
20 public:
22
23 // sets material params
24 void add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources) override;
25
26 std::string name() const override { return "AMIPS"; }
27 std::map<std::string, ParamFunc> parameters() const override;
28
29 bool allow_inversion() const override { return false; }
30
31 bool real_def_grad(const RowVectorNd &p, const double t, const int el_id) const override { return use_rest_pose(p, t, el_id); }
32
33 template <typename T>
35 const RowVectorNd &p,
36 const double t,
37 const int el_id,
38 DefGradMatrix<T> &def_grad) const
39 {
40 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> AutoDiffGradMat;
41
42 const double power = 2. / size();
43 const bool rest_pose = use_rest_pose(p, t, el_id);
44
45 AutoDiffGradMat standard;
46
47 if (size() == 2)
48 standard = get_standard<2, T>(size(), rest_pose);
49 else
50 standard = get_standard<3, T>(size(), rest_pose);
51
52 if (!rest_pose)
53 def_grad = def_grad * standard;
54
55 const T det = polyfem::utils::determinant(def_grad);
56 if (det <= 0)
57 {
58 return T(std::nan(""));
59 }
60
61 const T powJ = pow(det, power);
62 const double weight = weight_(p, t, el_id);
63 return T(weight) * (def_grad.transpose() * def_grad).trace() / powJ; //+ barrier<T>::value(det);
64 }
65
66 private:
67 bool use_rest_pose(const RowVectorNd &p, const double t, const int el_id) const;
70
71 template <int dimt, class T>
72 static Eigen::Matrix<T, dimt, dimt> get_standard(const int dim, const bool use_rest_pose)
73 {
74 Eigen::Matrix<double, dimt, dimt> standard(dim, dim);
75 if (use_rest_pose)
76 {
77 standard.setIdentity();
78 }
79 else
80 {
81 if (dim == 2)
82 standard << 1, 0,
83 0.5, std::sqrt(3) / 2;
84 else
85 standard << 1, 0, 0,
86 0.5, std::sqrt(3) / 2., 0,
87 0.5, 0.5 / std::sqrt(3), std::sqrt(2. / 3.);
88 standard = standard.inverse().transpose().eval();
89 }
90
91 Eigen::Matrix<T, dimt, dimt> res(dim, dim);
92 for (int i = 0; i < dim; ++i)
93 {
94 for (int j = 0; j < dim; ++j)
95 {
96 res(i, j) = T(standard(i, j));
97 }
98 }
99
100 return res;
101 }
102
103 public:
104 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> gradient(
105 const RowVectorNd &p,
106 const double t,
107 const int el_id,
108 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const override;
109
110 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> hessian(
111 const RowVectorNd &p,
112 const double t,
113 const int el_id,
114 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const override;
115 };
116
117} // namespace polyfem::assembler
static Eigen::Matrix< T, dimt, dimt > get_standard(const int dim, const bool use_rest_pose)
std::string name() const override
void add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources) override
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > gradient(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F) const override
bool allow_inversion() const override
bool use_rest_pose(const RowVectorNd &p, const double t, const int el_id) const
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9 > hessian(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F) const override
bool real_def_grad(const RowVectorNd &p, const double t, const int el_id) const override
T elastic_energy(const RowVectorNd &p, const double t, const int el_id, DefGradMatrix< T > &def_grad) const
std::map< std::string, ParamFunc > parameters() const override
Read-only interface to a hierarchical collection of simulation resources.
Used for test only.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > DefGradMatrix
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AutoDiffGradMat
T determinant(const Eigen::Matrix< T, rows, cols, option, maxRow, maxCol > &mat)
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13