13 template <
class T,
int p = 3>
16 constexpr static double C = 1e2;
19 static_assert(p % 2 == 1);
20 static T value(T
J,
double JBarrierThreshold)
22 if (
J >= JBarrierThreshold)
24 const T tmp1 =
J / JBarrierThreshold - 1;
25 const T tmp2 = pow(tmp1, p);
26 return C * (1 / (tmp2 + 1) - 1);
29 static T first_derivatives(T
J,
double JBarrierThreshold)
31 if (
J >= JBarrierThreshold)
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);
38 static T second_derivatives(T
J,
double JBarrierThreshold)
40 if (
J >= JBarrierThreshold)
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);
49 void jacobian_cofactor_derivatives(
const Eigen::MatrixXd &def_grad,
const int dim,
50 Eigen::MatrixXd &delJ_delF, Eigen::MatrixXd &del2J_delF2)
52 delJ_delF.setZero(dim, dim);
53 del2J_delF2.setZero(dim * dim, dim * dim);
62 del2J_delF2(0, 3) = 1;
63 del2J_delF2(1, 2) = -1;
64 del2J_delF2(2, 1) = -1;
65 del2J_delF2(3, 0) = 1;
70 delJ_delF.col(0) = v.cross(w);
71 delJ_delF.col(1) = w.cross(u);
72 delJ_delF.col(2) = u.cross(v);
74 auto hat = [](
const Eigen::Vector3d &
x) {
76 prod << 0, -
x(2),
x(1),
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);
93 : JBarrierThreshold_(
"JBarrierThreshold")
104 if (params.count(
"JBarrierThreshold"))
110 json params_with_default = params;
111 params_with_default[
"JBarrierThreshold"] = 0.5;
116 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1>
119 assert(pt.size() ==
size());
120 Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1> res;
130 const auto t = data.
t;
137 output.
energy = mu * barrier<double>::value(
J, threshold);
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;
149 const int dim =
size();
152 Eigen::VectorXd jacs;
156 Eigen::MatrixXd local_disp(n_basis, dim);
157 local_disp.setZero();
158 for (
int i = 0; i < n_basis; ++i)
160 for (
int d = 0; d < dim; ++d)
161 local_disp(i, d) += g.val * data.
x(g.index * dim + d);
164 const int n_pts = data.
da.size();
165 for (
long p = 0; p < n_pts; ++p)
167 Eigen::MatrixXd grad(n_basis, dim);
168 for (
int i = 0; i < n_basis; ++i)
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);
180 energy += mu * barrier<double>::value(
J, JBarrierThreshold) * data.
da(p);
187 const int dim =
size();
190 Eigen::VectorXd jacs;
194 Eigen::MatrixXd local_disp(n_basis, dim);
195 local_disp.setZero();
196 for (
int i = 0; i < n_basis; ++i)
198 for (
int d = 0; d < dim; ++d)
199 local_disp(i, d) += g.val * data.
x(g.index * dim + d);
201 Eigen::MatrixXd G = Eigen::MatrixXd::Zero(n_basis, dim);
203 const int n_pts = data.
da.size();
204 for (
long p = 0; p < n_pts; ++p)
206 Eigen::MatrixXd grad(n_basis, dim);
207 for (
int i = 0; i < n_basis; ++i)
210 const Eigen::MatrixXd jac_it = data.
vals.
jac_it[p];
211 const Eigen::MatrixXd delF_delU = grad * jac_it;
213 const Eigen::MatrixXd def_grad = local_disp.transpose() * delF_delU + Eigen::MatrixXd::Identity(dim, dim);
218 Eigen::MatrixXd delJ_delF, del2J_delF2;
219 jacobian_cofactor_derivatives(def_grad, dim, delJ_delF, del2J_delF2);
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();
227 G.noalias() += gradient * data.
da(p);
230 const Eigen::MatrixXd G_T = G.transpose();
231 return Eigen::Map<const Eigen::VectorXd>(G_T.data(), G_T.size());
237 result.
visit([&](
auto &H) {
239 const int dim =
size();
241 const int N = n_basis * dim;
243 Eigen::VectorXd jacs;
247 Eigen::MatrixXd local_disp(n_basis, dim);
248 local_disp.setZero();
249 for (
int i = 0; i < n_basis; ++i)
251 for (
int d = 0; d < dim; ++d)
252 local_disp(i, d) += g.val * data.
x(g.index * dim + d);
254 const int n_pts = data.
da.size();
255 for (
long p = 0; p < n_pts; ++p)
257 Eigen::MatrixXd grad(n_basis, dim);
258 for (
int i = 0; i < n_basis; ++i)
261 const Eigen::MatrixXd jac_it = data.
vals.
jac_it[p];
262 const Eigen::MatrixXd delF_delU = grad * jac_it;
264 const Eigen::MatrixXd def_grad = local_disp.transpose() * delF_delU + Eigen::MatrixXd::Identity(dim, dim);
269 Eigen::MatrixXd delJ_delF, del2J_delF2;
270 jacobian_cofactor_derivatives(def_grad, dim, delJ_delF, del2J_delF2);
275 const Eigen::Map<const Eigen::VectorXd> g_j(delJ_delF.data(), delJ_delF.size());
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());
280 Eigen::MatrixXd delF_delU_tensor = Eigen::MatrixXd::Zero(dim * dim, N);
281 for (
int i = 0; i < n_basis; ++i)
283 for (
int j = 0; j < dim; ++j)
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());
291 H += (delF_delU_tensor.transpose() * hessian_temp * delF_delU_tensor) * data.
da(p);
298 std::map<std::string, ParamFunc> res;
304 params.lambda_mu(uv, p, t, e, lambda, mu);
308 res[
"JBarrierThreshold"] = [&JBarrierThreshold](
const RowVectorNd &uv,
const RowVectorNd &p,
double t,
int e) {
309 return JBarrierThreshold(p, t, e);
ElementAssemblyValues vals
std::string stress() const
Scratch for one output point. All tensor storage has a fixed 3x3 capacity.
ElasticityTensorType native_type
const MatrixNd & deformation() const
stores per element basis values at given quadrature points and geometric mapping
std::vector< AssemblyValues > basis_values
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
quadrature::Quadrature quadrature
void add_multimaterial(const int index, const json ¶ms, const std::string &unit_type, const io::ResourceIO &resources)
GenericMatParam JBarrierThreshold_
VectorNd compute_rhs(const AutodiffHessianPt &pt) const override
void add_multimaterial(const int index, const json ¶ms, 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 ¶ms, 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)
decltype(auto) visit(F &&f)
const ElementAssemblyValues & vals
const Eigen::MatrixXd & x
const QuadratureVector & da
const Eigen::MatrixXd & local_pts
Read-only interface to a hierarchical collection of simulation resources.
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
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd