PolyFEM
Loading...
Searching...
No Matches
AMIPSEnergy.cpp
Go to the documentation of this file.
1#include "AMIPSEnergy.hpp"
2
4
9
10namespace polyfem::assembler
11{
13 : use_rest_pose_("use_rest_pose"), weight_("weight")
14 {
16 }
17
18 void AMIPSEnergy::add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources)
19 {
20 assert(size() == 2 || size() == 3);
21
22 use_rest_pose_.add_multimaterial(index, params, "", resources);
23
24 weight_.add_multimaterial(index, params, "", resources);
25
26 power_.add_multimaterial(index, params, "", resources);
27
28 normalized_.add_multimaterial(index, params, "", resources);
29 }
30
31 std::map<std::string, Assembler::ParamFunc> AMIPSEnergy::parameters() const
32 {
33 std::map<std::string, ParamFunc> res;
34
35 res["use_rest_pose"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
36 return use_rest_pose_(p, t, e);
37 };
38 res["weight"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
39 return weight_(p, t, e);
40 };
41
42 res["power"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
43 return power_(p, t, e);
44 };
45
46 res["normalized"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
47 return normalized_(p, t, e);
48 };
49
50 return res;
51 }
52
53 bool AMIPSEnergy::use_rest_pose(const RowVectorNd &p, const double t, const int el_id) const
54 {
55 return use_rest_pose_(p, t, el_id) != 0;
56 }
57
58 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> AMIPSEnergy::gradient(
59 const RowVectorNd &p,
60 const double t,
61 const int el_id,
62 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const
63 {
64 const double det = polyfem::utils::determinant(F);
65 if (det <= 0)
66 {
67 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> grad(size(), size());
68 grad.setConstant(std::nan(""));
69 return grad;
70 }
71
72 const double weight = weight_(p, t, el_id);
73 const double power = power_(p, t, el_id);
74 const double normalization = normalized_(p, t, el_id) != 0 ? size() : 1;
75 if (use_rest_pose(p, t, el_id))
76 {
77 if (size() == 2)
78 return weight * autogen::AMIPS2drest_gradient(p, t, el_id, F, power, normalization);
79 else
80 return weight * autogen::AMIPS3drest_gradient(p, t, el_id, F, power, normalization);
81 }
82 else
83 {
84 if (size() == 2)
85 return weight * autogen::AMIPS2d_gradient(p, t, el_id, F, power, normalization);
86 else
87 return weight * autogen::AMIPS3d_gradient(p, t, el_id, F, power, normalization);
88 }
89 }
90
91 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> AMIPSEnergy::hessian(
92 const RowVectorNd &p,
93 const double t,
94 const int el_id,
95 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const
96 {
97 const double det = polyfem::utils::determinant(F);
98 if (det <= 0)
99 {
100 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> hessian(size() * size(), size() * size());
101 hessian.setConstant(std::nan(""));
102 return hessian;
103 }
104
105 const double weight = weight_(p, t, el_id);
106 const double power = power_(p, t, el_id);
107 const double normalization = normalized_(p, t, el_id) != 0 ? size() : 1;
108 if (use_rest_pose(p, t, el_id))
109 {
110 if (size() == 2)
111 return weight * autogen::AMIPS2drest_hessian(p, t, el_id, F, power, normalization);
112 else
113 return weight * autogen::AMIPS3drest_hessian(p, t, el_id, F, power, normalization);
114 }
115 else
116 {
117 if (size() == 2)
118 return weight * autogen::AMIPS2d_hessian(p, t, el_id, F, power, normalization);
119 else
120 return weight * autogen::AMIPS3d_hessian(p, t, el_id, F, power, normalization);
121 }
122 }
123
124} // namespace polyfem::assembler
LocalHessian hessian
const double weight_
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 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
std::map< std::string, ParamFunc > parameters() const override
void add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
Definition MatParams.cpp:89
Read-only interface to a hierarchical collection of simulation resources.
Used for test only.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9 > AMIPS3drest_hessian(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9 > AMIPS2drest_hessian(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AMIPS3d_gradient(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Definition AMIPS3d.hpp:7
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9 > AMIPS2d_hessian(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Definition AMIPS2d.hpp:28
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9 > AMIPS3d_hessian(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Definition AMIPS3d.hpp:60
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AMIPS2d_gradient(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Definition AMIPS2d.hpp:7
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AMIPS3drest_gradient(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > AMIPS2drest_gradient(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F, const double power, const double normalization)
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