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:
25
26 // sets material params
27 void add_multimaterial(const int index, const json &params, const Units &units, const std::string &root_path) override;
28
29 std::string name() const override { return "AMIPS"; }
30 std::map<std::string, ParamFunc> parameters() const override { return std::map<std::string, ParamFunc>(); }
31
32 bool allow_inversion() const override { return false; }
33
34 bool real_def_grad() const override { return use_rest_pose_; }
35
36 template <typename T>
38 const RowVectorNd &p,
39 const double t,
40 const int el_id,
41 DefGradMatrix<T> &def_grad) const
42 {
43 typedef Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> AutoDiffGradMat;
44
45 double power = -1;
47 power = size() == 2 ? 1. : (2. / 3.);
48 else
49 power = size() == 2 ? 2. : 5. / 3.;
50
51 AutoDiffGradMat standard;
52
53 if (size() == 2)
54 standard = get_standard<2, T>(size(), use_rest_pose_);
55 else
56 standard = get_standard<3, T>(size(), use_rest_pose_);
57
58 if (!use_rest_pose_)
59 def_grad = def_grad * standard;
60
61 const T det = polyfem::utils::determinant(def_grad);
62 if (det <= 0)
63 {
64 return T(std::nan(""));
65 }
66
67 const T powJ = pow(det, power);
68 const double weight = get_energy_weight(el_id);
69 return T(weight) * (def_grad.transpose() * def_grad).trace() / powJ; //+ barrier<T>::value(det);
70 }
71
72 private:
73 double get_energy_weight(const int el_id) const;
74 std::vector<double> energy_weights_;
75 bool use_rest_pose_ = false;
76
77 template <int dimt, class T>
78 static Eigen::Matrix<T, dimt, dimt> get_standard(const int dim, const bool use_rest_pose)
79 {
80 Eigen::Matrix<double, dimt, dimt> standard(dim, dim);
81 if (use_rest_pose)
82 {
83 standard.setIdentity();
84 }
85 else
86 {
87 if (dim == 2)
88 standard << 1, 0,
89 0.5, std::sqrt(3) / 2;
90 else
91 standard << 1, 0, 0,
92 0.5, std::sqrt(3) / 2., 0,
93 0.5, 0.5 / std::sqrt(3), std::sqrt(3) / 2.;
94 standard = standard.inverse().transpose().eval();
95 }
96
97 Eigen::Matrix<T, dimt, dimt> res(dim, dim);
98 for (int i = 0; i < dim; ++i)
99 {
100 for (int j = 0; j < dim; ++j)
101 {
102 res(i, j) = T(standard(i, j));
103 }
104 }
105
106 return res;
107 }
108
109 public:
110 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> gradient(
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 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> hessian(
117 const RowVectorNd &p,
118 const double t,
119 const int el_id,
120 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const override;
121 };
122
123} // namespace polyfem::assembler
static Eigen::Matrix< T, dimt, dimt > get_standard(const int dim, const bool use_rest_pose)
std::string name() const override
double get_energy_weight(const int el_id) const
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
std::vector< double > energy_weights_
bool allow_inversion() const override
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
std::map< std::string, ParamFunc > parameters() const override
T elastic_energy(const RowVectorNd &p, const double t, const int el_id, DefGradMatrix< T > &def_grad) const
bool real_def_grad() const override
void add_multimaterial(const int index, const json &params, const Units &units, const std::string &root_path) override
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