PolyFEM
Loading...
Searching...
No Matches
InversionBarrier.cpp
Go to the documentation of this file.
2
4
5#include <cmath>
6
7namespace polyfem::assembler
8{
9 namespace
10 {
11 // Zero for J >= JBarrierThreshold, smoothly diverges to +inf as J -> 0.
12 // Scaled by mu at the call sites.
13 template <class T, int p = 3>
14 class barrier
15 {
16 constexpr static double C = 1e2;
17
18 public:
19 static_assert(p % 2 == 1);
20 static T value(T J, double JBarrierThreshold)
21 {
22 if (J >= JBarrierThreshold)
23 return T(0.);
24 const T tmp1 = J / JBarrierThreshold - 1;
25 const T tmp2 = pow(tmp1, p);
26 return C * (1 / (tmp2 + 1) - 1);
27 }
28
29 static T first_derivatives(T J, double JBarrierThreshold)
30 {
31 if (J >= JBarrierThreshold)
32 return T(0);
33 const T tmp1 = J / JBarrierThreshold - 1;
34 const T tmp2 = pow(tmp1, p);
35 return C * (-1 / JBarrierThreshold) * p * tmp2 / tmp1 / pow(1 + tmp2, 2);
36 }
37
38 static T second_derivatives(T J, double JBarrierThreshold)
39 {
40 if (J >= JBarrierThreshold)
41 return T(0);
42 const T tmp1 = J / JBarrierThreshold - 1;
43 const T tmp2 = pow(tmp1, p);
44 return C * (1 / (JBarrierThreshold * JBarrierThreshold)) * p * tmp2 / pow(tmp1, 2) * ((1 - p) + (1 + p) * tmp2) / pow(1 + tmp2, 3);
45 }
46 };
47
48 // Cofactor matrix dJ/dF, and its derivative d2J/dF2 (flattened column-major).
49 void jacobian_cofactor_derivatives(const Eigen::MatrixXd &def_grad, const int dim,
50 Eigen::MatrixXd &delJ_delF, Eigen::MatrixXd &del2J_delF2)
51 {
52 delJ_delF.setZero(dim, dim);
53 del2J_delF2.setZero(dim * dim, dim * dim);
54
55 if (dim == 2)
56 {
57 delJ_delF(0, 0) = def_grad(1, 1);
58 delJ_delF(0, 1) = -def_grad(1, 0);
59 delJ_delF(1, 0) = -def_grad(0, 1);
60 delJ_delF(1, 1) = def_grad(0, 0);
61
62 del2J_delF2(0, 3) = 1;
63 del2J_delF2(1, 2) = -1;
64 del2J_delF2(2, 1) = -1;
65 del2J_delF2(3, 0) = 1;
66 }
67 else // dim == 3
68 {
69 const Eigen::Vector3d u = def_grad.col(0), v = def_grad.col(1), w = def_grad.col(2);
70 delJ_delF.col(0) = v.cross(w);
71 delJ_delF.col(1) = w.cross(u);
72 delJ_delF.col(2) = u.cross(v);
73
74 auto hat = [](const Eigen::Vector3d &x) {
75 Eigen::Matrix3d prod;
76 prod << 0, -x(2), x(1),
77 x(2), 0, -x(0),
78 -x(1), x(0), 0;
79 return prod;
80 };
81
82 del2J_delF2.block<3, 3>(0, 6) = hat(v);
83 del2J_delF2.block<3, 3>(6, 0) = -hat(v);
84 del2J_delF2.block<3, 3>(0, 3) = -hat(w);
85 del2J_delF2.block<3, 3>(3, 0) = hat(w);
86 del2J_delF2.block<3, 3>(3, 6) = -hat(u);
87 del2J_delF2.block<3, 3>(6, 3) = hat(u);
88 }
89 }
90 } // namespace
91
93 : JBarrierThreshold_("JBarrierThreshold")
94 {
95 }
96
97 void InversionBarrier::add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources)
98 {
99 assert(size() == 2 || size() == 3);
100
101 params_.add_multimaterial(index, params, size() == 3, units.stress(), resources);
102
103 // Default activation threshold matches the original hardcoded barrier (J >= 0.5 is inactive).
104 if (params.count("JBarrierThreshold"))
105 {
106 JBarrierThreshold_.add_multimaterial(index, params, "", resources);
107 }
108 else
109 {
110 json params_with_default = params;
111 params_with_default["JBarrierThreshold"] = 0.5;
112 JBarrierThreshold_.add_multimaterial(index, params_with_default, "", resources);
113 }
114 }
115
116 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1>
118 {
119 assert(pt.size() == size());
120 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1> res;
121 assert(false);
122
123 return res;
124 }
125
127 const Eigen::Index p, const MatrixNd &displacement_grad, OutputStress &output) const
128 {
129 const auto &local_pts = data.local_pts;
130 const auto t = data.t;
131 const auto &def_grad = output.deformation();
132 const double J = polyfem::utils::determinant(def_grad);
133 const double threshold = JBarrierThreshold_(local_pts.row(p), t, vals.element_id);
134 double lambda, mu;
135 params_.lambda_mu(local_pts.row(p), vals.val.row(p), t, vals.element_id, lambda, mu);
136 if (output.needs_energy)
137 output.energy = mu * barrier<double>::value(J, threshold);
138 if (!output.needs_stress)
139 return true;
140 Eigen::MatrixXd delJ_delF, del2J_delF2;
141 jacobian_cofactor_derivatives(def_grad, size(), delJ_delF, del2J_delF2);
142 output.native = mu * barrier<double>::first_derivatives(J, threshold) * delJ_delF;
144 return true;
145 }
146
148 {
149 const int dim = size();
150 const int n_basis = data.vals.basis_values.size();
151
152 Eigen::VectorXd jacs;
154 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
155
156 Eigen::MatrixXd local_disp(n_basis, dim);
157 local_disp.setZero();
158 for (int i = 0; i < n_basis; ++i)
159 for (const auto &g : data.vals.basis_values[i].global)
160 for (int d = 0; d < dim; ++d)
161 local_disp(i, d) += g.val * data.x(g.index * dim + d);
162
163 double energy = 0.;
164 const int n_pts = data.da.size();
165 for (long p = 0; p < n_pts; ++p)
166 {
167 Eigen::MatrixXd grad(n_basis, dim);
168 for (int i = 0; i < n_basis; ++i)
169 grad.row(i) = data.vals.basis_values[i].grad.row(p);
170
171 const Eigen::MatrixXd jac_it = data.vals.jac_it[p];
172 const Eigen::MatrixXd def_grad = (local_disp.transpose() * grad) * jac_it + Eigen::MatrixXd::Identity(dim, dim);
173
174 const double J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
175 const double JBarrierThreshold = JBarrierThreshold_(data.vals.quadrature.points.row(p), data.t, data.vals.element_id);
176
177 double lambda, mu;
178 params_.lambda_mu(data.vals.quadrature.points.row(p), data.vals.val.row(p), data.t, data.vals.element_id, lambda, mu);
179
180 energy += mu * barrier<double>::value(J, JBarrierThreshold) * data.da(p);
181 }
182 return energy;
183 }
184
186 {
187 const int dim = size();
188 const int n_basis = data.vals.basis_values.size();
189
190 Eigen::VectorXd jacs;
192 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
193
194 Eigen::MatrixXd local_disp(n_basis, dim);
195 local_disp.setZero();
196 for (int i = 0; i < n_basis; ++i)
197 for (const auto &g : data.vals.basis_values[i].global)
198 for (int d = 0; d < dim; ++d)
199 local_disp(i, d) += g.val * data.x(g.index * dim + d);
200
201 Eigen::MatrixXd G = Eigen::MatrixXd::Zero(n_basis, dim);
202
203 const int n_pts = data.da.size();
204 for (long p = 0; p < n_pts; ++p)
205 {
206 Eigen::MatrixXd grad(n_basis, dim);
207 for (int i = 0; i < n_basis; ++i)
208 grad.row(i) = data.vals.basis_values[i].grad.row(p);
209
210 const Eigen::MatrixXd jac_it = data.vals.jac_it[p];
211 const Eigen::MatrixXd delF_delU = grad * jac_it;
212
213 const Eigen::MatrixXd def_grad = local_disp.transpose() * delF_delU + Eigen::MatrixXd::Identity(dim, dim);
214
215 const double J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
216 const double JBarrierThreshold = JBarrierThreshold_(data.vals.quadrature.points.row(p), data.t, data.vals.element_id);
217
218 Eigen::MatrixXd delJ_delF, del2J_delF2;
219 jacobian_cofactor_derivatives(def_grad, dim, delJ_delF, del2J_delF2);
220
221 double lambda, mu;
222 params_.lambda_mu(data.vals.quadrature.points.row(p), data.vals.val.row(p), data.t, data.vals.element_id, lambda, mu);
223
224 const Eigen::MatrixXd gradient_temp = mu * barrier<double>::first_derivatives(J, JBarrierThreshold) * delJ_delF;
225 const Eigen::MatrixXd gradient = delF_delU * gradient_temp.transpose();
226
227 G.noalias() += gradient * data.da(p);
228 }
229
230 const Eigen::MatrixXd G_T = G.transpose();
231 return Eigen::Map<const Eigen::VectorXd>(G_T.data(), G_T.size());
232 }
233
235 {
236 result.resize(data.vals.basis_values.size() * size(), data.vals.basis_values.size() * size());
237 result.visit([&](auto &H) {
238 H.setZero();
239 const int dim = size();
240 const int n_basis = data.vals.basis_values.size();
241 const int N = n_basis * dim;
242
243 Eigen::VectorXd jacs;
245 jacs = data.vals.eval_deformed_jacobian_determinant(data.x);
246
247 Eigen::MatrixXd local_disp(n_basis, dim);
248 local_disp.setZero();
249 for (int i = 0; i < n_basis; ++i)
250 for (const auto &g : data.vals.basis_values[i].global)
251 for (int d = 0; d < dim; ++d)
252 local_disp(i, d) += g.val * data.x(g.index * dim + d);
253
254 const int n_pts = data.da.size();
255 for (long p = 0; p < n_pts; ++p)
256 {
257 Eigen::MatrixXd grad(n_basis, dim);
258 for (int i = 0; i < n_basis; ++i)
259 grad.row(i) = data.vals.basis_values[i].grad.row(p);
260
261 const Eigen::MatrixXd jac_it = data.vals.jac_it[p];
262 const Eigen::MatrixXd delF_delU = grad * jac_it;
263
264 const Eigen::MatrixXd def_grad = local_disp.transpose() * delF_delU + Eigen::MatrixXd::Identity(dim, dim);
265
266 const double J = use_robust_jacobian ? jacs(p) * jac_it.determinant() : def_grad.determinant();
267 const double JBarrierThreshold = JBarrierThreshold_(data.vals.quadrature.points.row(p), data.t, data.vals.element_id);
268
269 Eigen::MatrixXd delJ_delF, del2J_delF2;
270 jacobian_cofactor_derivatives(def_grad, dim, delJ_delF, del2J_delF2);
271
272 double lambda, mu;
273 params_.lambda_mu(data.vals.quadrature.points.row(p), data.vals.val.row(p), data.t, data.vals.element_id, lambda, mu);
274
275 const Eigen::Map<const Eigen::VectorXd> g_j(delJ_delF.data(), delJ_delF.size());
276
277 const Eigen::MatrixXd hessian_temp = mu * barrier<double>::first_derivatives(J, JBarrierThreshold) * del2J_delF2
278 + mu * barrier<double>::second_derivatives(J, JBarrierThreshold) * (g_j * g_j.transpose());
279
280 Eigen::MatrixXd delF_delU_tensor = Eigen::MatrixXd::Zero(dim * dim, N);
281 for (int i = 0; i < n_basis; ++i)
282 {
283 for (int j = 0; j < dim; ++j)
284 {
285 Eigen::MatrixXd temp = Eigen::MatrixXd::Zero(dim, dim);
286 temp.row(j) = delF_delU.row(i);
287 delF_delU_tensor.col(i * dim + j) = Eigen::Map<const Eigen::VectorXd>(temp.data(), temp.size());
288 }
289 }
290
291 H += (delF_delU_tensor.transpose() * hessian_temp * delF_delU_tensor) * data.da(p);
292 }
293 });
294 }
295
296 std::map<std::string, Assembler::ParamFunc> InversionBarrier::parameters() const
297 {
298 std::map<std::string, ParamFunc> res;
299 const auto &params = params_;
300 const auto &JBarrierThreshold = JBarrierThreshold_;
301
302 res["mu"] = [&params](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
303 double lambda, mu;
304 params.lambda_mu(uv, p, t, e, lambda, mu);
305 return mu;
306 };
307
308 res["JBarrierThreshold"] = [&JBarrierThreshold](const RowVectorNd &uv, const RowVectorNd &p, double t, int e) {
309 return JBarrierThreshold(p, t, e);
310 };
311
312 return res;
313 }
314} // namespace polyfem::assembler
ElementAssemblyValues vals
double J
int x
std::string stress() const
Definition Units.hpp:27
Scratch for one output point. All tensor storage has a fixed 3x3 capacity.
stores per element basis values at given quadrature points and geometric mapping
std::vector< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > > jac_it
Eigen::VectorXd eval_deformed_jacobian_determinant(const Eigen::VectorXd &disp) const
void add_multimaterial(const int index, const json &params, const std::string &unit_type, const io::ResourceIO &resources)
Definition MatParams.cpp:89
VectorNd compute_rhs(const AutodiffHessianPt &pt) const override
void add_multimaterial(const int index, const json &params, const Units &units, const io::ResourceIO &resources) override
bool compute_output_stress(const OutputData &data, const ElementAssemblyValues &vals, Eigen::Index point, const MatrixNd &displacement_gradient, OutputStress &stress) const override
Return false to use a material's legacy assign_stress_tensor implementation.
std::map< std::string, ParamFunc > parameters() const override
double compute_energy(const NonLinearAssemblerData &data) const override
Eigen::VectorXd assemble_gradient(const NonLinearAssemblerData &data) const override
void assemble_hessian(const NonLinearAssemblerData &data, LocalHessian &hessian) const override
void lambda_mu(double px, double py, double pz, double x, double y, double z, double t, int el_id, double &lambda, double &mu) const
void add_multimaterial(const int index, const json &params, const bool is_volume, const std::string &stress_unit, const io::ResourceIO &resources)
Reusable element matrix with inline storage for small blocks and a dynamic fallback for high-order el...
void resize(const Eigen::Index rows, const Eigen::Index cols)
const Eigen::MatrixXd & local_pts
Read-only interface to a hierarchical collection of simulation resources.
Used for test only.
Eigen::Matrix< double, dim, dim > hat(const Eigen::Matrix< double, dim, 1 > &x)
T determinant(const Eigen::Matrix< T, rows, cols, option, maxRow, maxCol > &mat)
Eigen::Matrix< AutodiffScalarHessian, Eigen::Dynamic, 1, 0, 3, 1 > AutodiffHessianPt
nlohmann::json json
Definition Common.hpp:9
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Definition Types.hpp:14
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13