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
27 std::map<std::string, Assembler::ParamFunc> AMIPSEnergy::parameters() const
28 {
29 std::map<std::string, ParamFunc> res;
30
31 res["use_rest_pose"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
32 return use_rest_pose_(p, t, e);
33 };
34 res["weight"] = [this](const RowVectorNd &, const RowVectorNd &p, double t, int e) {
35 return weight_(p, t, e);
36 };
37
38 return res;
39 }
40
41 bool AMIPSEnergy::use_rest_pose(const RowVectorNd &p, const double t, const int el_id) const
42 {
43 return use_rest_pose_(p, t, el_id) != 0;
44 }
45
46 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> AMIPSEnergy::gradient(
47 const RowVectorNd &p,
48 const double t,
49 const int el_id,
50 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const
51 {
52 const double det = polyfem::utils::determinant(F);
53 if (det <= 0)
54 {
55 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> grad(size(), size());
56 grad.setConstant(std::nan(""));
57 return grad;
58 }
59
60 const double weight = weight_(p, t, el_id);
61
62 if (use_rest_pose(p, t, el_id))
63 {
64 if (size() == 2)
65 return weight * autogen::AMIPS2drest_gradient(p, t, el_id, F);
66 else
67 return weight * autogen::AMIPS3drest_gradient(p, t, el_id, F);
68 }
69 else
70 {
71 if (size() == 2)
72 return weight * autogen::AMIPS2d_gradient(p, t, el_id, F);
73 else
74 return weight * autogen::AMIPS3d_gradient(p, t, el_id, F);
75 }
76 }
77
78 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> AMIPSEnergy::hessian(
79 const RowVectorNd &p,
80 const double t,
81 const int el_id,
82 const Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &F) const
83 {
84 const double det = polyfem::utils::determinant(F);
85 if (det <= 0)
86 {
87 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 0, 9, 9> hessian(size() * size(), size() * size());
88 hessian.setConstant(std::nan(""));
89 return hessian;
90 }
91
92 const double weight = weight_(p, t, el_id);
93
94 if (use_rest_pose(p, t, el_id))
95 {
96 if (size() == 2)
97 return weight * autogen::AMIPS2drest_hessian(p, t, el_id, F);
98 else
99 return weight * autogen::AMIPS3drest_hessian(p, t, el_id, F);
100 }
101 else
102 {
103 if (size() == 2)
104 return weight * autogen::AMIPS2d_hessian(p, t, el_id, F);
105 else
106 return weight * autogen::AMIPS3d_hessian(p, t, el_id, F);
107 }
108 }
109
110} // namespace polyfem::assembler
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:78
Read-only interface to a hierarchical collection of simulation resources.
Used for test only.
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)
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)
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)
Definition AMIPS2d.hpp:7
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)
Definition AMIPS3d.hpp:60
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)
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)
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)
Definition AMIPS2d.hpp:28
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)
Definition AMIPS3d.hpp:7
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