12#include <Eigen/Eigenvalues>
29 const MixedNonLinearAssemblerData &data,
31 Eigen::Matrix<T, Eigen::Dynamic, 1> &local_state)
33 const int n_phi_bases = int(data.phi_vals.basis_values.size());
34 const int n_psi_bases = int(data.psi_vals.basis_values.size());
35 const int phi_local_size = n_phi_bases *
dim;
36 const int local_size = phi_local_size + n_psi_bases;
38 Eigen::VectorXd values = Eigen::VectorXd::Zero(local_size);
39 for (
int i = 0; i < n_phi_bases; ++i)
41 const auto &bs = data.phi_vals.basis_values[i];
42 for (
const auto &global : bs.global)
44 for (
int d = 0; d <
dim; ++d)
45 values(i * dim + d) += global.val * data.x_phi(global.index * dim + d);
49 for (
int i = 0; i < n_psi_bases; ++i)
51 const auto &bs = data.psi_vals.basis_values[i];
52 for (
const auto &global : bs.global)
53 values(phi_local_size + i) += global.
val * data.x_psi(global.index);
57 local_state.resize(local_size);
59 const AutoDiffAllocator<T> allocate_auto_diff_scalar;
60 for (
int i = 0; i < local_size; ++i)
61 local_state(i) = allocate_auto_diff_scalar(i, values(i));
65 void displacement_gradient_at_quad(
66 const MixedNonLinearAssemblerData &data,
67 const Eigen::Matrix<T, Eigen::Dynamic, 1> &local_state,
70 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &grad_u)
72 grad_u.resize(dim, dim);
73 for (
int k = 0; k < grad_u.size(); ++k)
76 for (
int i = 0; i < data.phi_vals.basis_values.size(); ++i)
78 const auto &bs = data.phi_vals.basis_values[i];
79 const Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 3, 1>
grad = bs.grad.row(p);
80 assert(
grad.size() == dim);
82 for (
int d = 0; d <
dim; ++d)
84 for (
int c = 0; c <
dim; ++c)
85 grad_u(d, c) +=
grad(c) * local_state(i * dim + d);
89 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> jac_it(dim, dim);
90 for (
int k = 0; k < jac_it.size(); ++k)
91 jac_it(k) =
T(data.phi_vals.jac_it[p](k));
92 grad_u = grad_u * jac_it;
97 const MixedNonLinearAssemblerData &data,
98 const Eigen::Matrix<T, Eigen::Dynamic, 1> &local_state,
102 const int phi_local_size = int(data.phi_vals.basis_values.size()) *
dim;
104 for (
int i = 0; i < data.psi_vals.basis_values.size(); ++i)
105 theta += data.psi_vals.basis_values[i].val(p) * local_state(phi_local_size + i);
109 inline double value_of(
const double x) {
return x; }
110 template <
typename T>
111 double value_of(
const T &
x) {
return x.getValue(); }
121 Eigen::MatrixXd read_vtk_cell_array(
122 std::istream &in,
const std::string &path,
const std::string &field,
const int ncomp_expected)
128 bool in_cell_data =
false;
131 while (std::getline(in, line))
133 std::istringstream ls(line);
137 if (tok ==
"CELL_DATA")
143 if (tok ==
"POINT_DATA")
145 in_cell_data =
false;
151 long ntuples = n_cells;
155 if (tok ==
"VECTORS")
160 else if (tok ==
"FIELD")
164 else if (tok ==
"SCALARS")
168 std::getline(in, line);
174 if (!(ls >> ncomp >> ntuples))
181 long remaining = ntuples * std::max(ncomp, 1);
183 while (remaining > 0 && in >> dummy)
188 if (ncomp != ncomp_expected)
190 "GrowthElasticity: field '{}' in '{}' has {} components, expected {}",
191 field, path, ncomp, ncomp_expected);
193 Eigen::MatrixXd values(ntuples, ncomp);
194 for (
long i = 0; i < ntuples; ++i)
195 for (
int c = 0; c < ncomp; ++c)
196 if (!(in >> values(i, c)))
198 "GrowthElasticity: truncated field '{}' in '{}'", field, path);
203 "GrowthElasticity: cell field '{}' not found in '{}'", field, path);
204 return Eigen::MatrixXd();
227 if (!params.contains(
"elastic_material") || !params[
"elastic_material"].is_object())
228 log_and_throw_error(
"GrowthElasticity requires elastic_material to be an elastic material object.");
230 const json &em = params[
"elastic_material"];
231 if (em.value(
"type",
"") !=
"MaterialSum")
233 "GrowthElasticity requires elastic_material of type 'MaterialSum', got '{}'.",
234 em.value(
"type",
""));
236 for (
const json &child_in : em.at(
"models"))
238 json child = child_in;
240 if (params.contains(
"id"))
241 child[
"id"] = params[
"id"];
245 const std::string type = child.value(
"type",
"");
246 if (type ==
"IsochoricNeoHookean")
248 else if (type ==
"HGODispersion")
250 else if (type ==
"VolumePenalty")
254 "GrowthElasticity elastic_material supports IsochoricNeoHookean, "
255 "HGODispersion, VolumePenalty; got '{}'.",
260 if (!params.contains(
"normal_direction"))
262 const json &nd = params[
"normal_direction"];
266 nd[0].get<double>(), nd[1].get<double>(), nd[2].get<double>())
269 else if (nd.is_object() && nd.value(
"type",
"") ==
"per_element_file")
272 resources, nd.at(
"path").get<std::string>(), nd.value(
"field",
"N0"), 3);
278 "GrowthElasticity normal_direction must be a constant [x,y,z] "
279 "or {type: per_element_file, path, field}.");
284 if (params.contains(
"normal_growth"))
286 const json &ng = params[
"normal_growth"];
289 else if (ng.is_object() && ng.value(
"type",
"") ==
"per_element_file")
291 resources, ng.at(
"path").get<std::string>(), ng.value(
"field",
"VN"), 1);
294 "GrowthElasticity normal_growth must be a number "
295 "or {type: per_element_file, path, field}.");
299 std::map<std::string, Assembler::ParamFunc>
parameters()
const
301 std::map<std::string, Assembler::ParamFunc> res;
308 template <
typename T>
312 log_and_throw_error(
"GrowthElasticity supports 3D only (in-plane growth needs a wall normal).");
317 const int dim =
size_;
320 Eigen::Matrix<T, Eigen::Dynamic, 1> local_state;
321 get_local_state(data, dim, local_state);
323 const Eigen::Vector3d n0v =
n0(el_id);
324 const double vn_e =
vn(el_id);
326 log_and_throw_error(
"GrowthElasticity: normal_growth must be positive, got {} on element {}.", vn_e, el_id);
328 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> grad_u(dim, dim);
329 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3>
F(dim, dim), Fgi(dim, dim), Fe(dim, dim);
332 for (
int p = 0; p < data.
da.size(); ++p)
334 displacement_gradient_at_quad(data, local_state, p, dim, grad_u);
337 for (
int d = 0; d < dim; ++d)
340 const T theta = growth_at_quad(data, local_state, p, dim);
341 if (!(value_of(theta) > 0))
343 "GrowthElasticity: growth theta must stay positive; got {} on element {} "
344 "(the growth-block feasibility filter is responsible for keeping iterates admissible).",
345 value_of(theta), el_id);
352 const T c_ip = T(1) / sqrt(theta) - T(1);
353 const double c_n = 1.0 / vn_e - 1.0;
354 for (
int a = 0; a < dim; ++a)
355 for (
int b = 0; b < dim; ++b)
357 const double id = (a == b) ? 1.0 : 0.0;
358 const double nn = n0v(a) * n0v(b);
359 Fgi(a, b) = T(
id) + c_ip * T(
id - nn) + T(c_n * nn);
363 const T Jg = theta * T(vn_e);
369 energy += (Jg * sum_energy<T>(pos, data.
t, el_id, Fe)
370 - sum_energy<T>(pos, data.
t, el_id,
F))
381 const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es(0.5 * (Ce + Ce.transpose()));
382 if (es.info() != Eigen::Success || !(es.eigenvalues().minCoeff() > 0))
383 log_and_throw_error(
"GrowthElasticity::driving_force: Ce is not positive definite on element {}.", el_id);
386 const Eigen::Matrix3d Fe = es.eigenvectors() * es.eigenvalues().cwiseSqrt().asDiagonal() * es.eigenvectors().transpose();
387 const Eigen::Vector3d n0v =
n0(el_id);
391 const AD theta(0, 1.0);
393 const AD c_ip = AD(1) / sqrt(theta) - AD(1);
394 Eigen::Matrix<AD, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> Fs(3, 3);
395 for (
int a = 0; a < 3; ++a)
396 for (
int b = 0; b < 3; ++b)
400 for (
int k = 0; k < 3; ++k)
402 const double ip = (k == b ? 1.0 : 0.0) - n0v(k) * n0v(b);
403 v += AD(Fe(a, k) * ip) * c_ip;
407 const AD g = theta * sum_energy<AD>(pos, t, el_id, Fs);
408 return -g.getGradient()(0);
414 const int el_id,
const RowVectorNd &pos,
const double t,
415 const Eigen::Matrix3d &
F,
const double theta,
416 Eigen::Matrix3d &cauchy, Eigen::Matrix3d &pk1, Eigen::Matrix3d &pk2,
417 double &energy)
const
422 log_and_throw_error(
"GrowthElasticity::grown_stresses: growth theta must be positive; got {} on element {}.", theta, el_id);
423 const double J =
F.determinant();
429 const Eigen::Vector3d n0v =
n0(el_id);
430 const double vn_e =
vn(el_id);
431 const Eigen::Matrix3d nn = n0v * n0v.transpose();
432 const Eigen::Matrix3d I = Eigen::Matrix3d::Identity();
433 const Eigen::Matrix3d Fgi = I + (1.0 / std::sqrt(theta) - 1.0) * (I - nn) + (1.0 / vn_e - 1.0) * nn;
434 const Eigen::Matrix3d Fe =
F * Fgi;
435 const double Jg = theta * vn_e;
440 Eigen::Matrix<AD, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> Fe_ad(3, 3);
441 for (
int a = 0; a < 3; ++a)
442 for (
int b = 0; b < 3; ++b)
443 Fe_ad(a, b) = AD(a * 3 + b, Fe(a, b));
444 const AD psi = sum_energy<AD>(pos, t, el_id, Fe_ad);
446 for (
int a = 0; a < 3; ++a)
447 for (
int b = 0; b < 3; ++b)
448 Pe(a, b) = psi.getGradient()(a * 3 + b);
450 pk1 = Jg * Pe * Fgi.transpose();
451 cauchy = pk1 *
F.transpose() /
J;
452 pk2 =
F.inverse() * pk1;
453 energy = Jg * psi.getValue();
457 template <
typename Child>
459 std::vector<std::unique_ptr<Child>> &list,
464 list.emplace_back(std::make_unique<Child>());
466 list.front()->set_size(
size_);
470 list.front()->add_multimaterial(index, child, units, resources);
473 template <
typename T>
475 const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3> &
F)
const
478 for (
const auto &m :
iso_)
479 e += m->elastic_energy(p, t, el_id,
F);
480 for (
const auto &m :
fiber_)
481 e += m->elastic_energy(p, t, el_id,
F);
482 for (
const auto &m :
vol_)
483 e += m->elastic_energy(p, t, el_id,
F);
487 Eigen::Vector3d
n0(
const int el_id)
const
491 if (el_id < 0 || el_id >=
normals_.rows())
493 "GrowthElasticity: element {} out of range of normal_direction data ({} rows).",
495 return normals_.row(el_id).transpose();
500 double vn(
const int el_id)
const
504 if (el_id < 0 || el_id >=
vn_.rows())
506 "GrowthElasticity: element {} out of range of normal_growth data ({} rows).",
508 return vn_(el_id, 0);
517 std::vector<std::unique_ptr<IsochoricNeoHookean>>
iso_;
518 std::vector<std::unique_ptr<HGODispersion>>
fiber_;
519 std::vector<std::unique_ptr<VolumePenalty>>
vol_;
529 const io::ResourceIO &resources,
const std::string &path,
const std::string &field,
const int ncomp_expected)
531 auto in = resources.
open(path,
false);
532 return read_vtk_cell_array(*in, resources.
describe(path), field, ncomp_expected);
536 : model_(std::make_unique<detail::GrowthElasticityModel>())
566 return energy.getGradient();
572 hessian.assign(energy.getHessian());
586 const int el_id,
const RowVectorNd &pos,
const double t,
587 const Eigen::Matrix3d &
F,
const double theta,
588 Eigen::Matrix3d &cauchy, Eigen::Matrix3d &pk1, Eigen::Matrix3d &pk2,
589 double &energy)
const
virtual void set_size(const int size)
std::vector< AssemblyValues > basis_values
quadrature::Quadrature quadrature
Eigen::Vector3d wall_normal(const int el_id) const
Reference wall normal n0 of element el_id (unit).
void grown_stresses(const int el_id, const RowVectorNd &pos, const double t, const Eigen::Matrix3d &F, const double theta, Eigen::Matrix3d &cauchy, Eigen::Matrix3d &pk1, Eigen::Matrix3d &pk2, double &energy) const
Stresses and energy density of the grown material, total energy density J^g psi_e(F Fg^{-1}),...
~GrowthElasticity() override
double driving_force(const int el_id, const RowVectorNd &pos, const double t, const Eigen::Matrix3d &Ce) const
Growth driving force f_I = -(1/vn) d(J^g psi_e)/d theta as a function of the elastic right Cauchy-Gre...
double compute_energy(const MixedNonLinearAssemblerData &data) const override
void set_size(const int size) override
void add_multimaterial(const int index, const json ¶ms, const Units &units, const io::ResourceIO &resources) override
std::map< std::string, ParamFunc > parameters() const override
detail::GrowthElasticityModel & model()
Eigen::VectorXd compute_gradient(const MixedNonLinearAssemblerData &data) const override
void compute_hessian(const MixedNonLinearAssemblerData &data, LocalHessian &hessian) const override
std::unique_ptr< detail::GrowthElasticityModel > model_
Reusable element matrix with inline storage for small blocks and a dynamic fallback for high-order el...
const QuadratureVector & da
Contains both the quadrature weight and the change of metric in the integral.
const ElementAssemblyValues & phi_vals
Values for the first block, historically tensor velocity/displacement-like bases.
const ElementAssemblyValues & psi_vals
Values for the second block, historically scalar pressure-like bases.
double driving_force(const int el_id, const RowVectorNd &pos, const double t, const Eigen::Matrix3d &Ce) const
void add_multimaterial(const int index, const json ¶ms, const Units &units, const io::ResourceIO &resources)
std::map< std::string, Assembler::ParamFunc > parameters() const
T compute_energy_aux(const MixedNonLinearAssemblerData &data) const
Eigen::Vector3d wall_normal(const int el_id) const
void set_size(const int size)
void add_child(std::vector< std::unique_ptr< Child > > &list, const int index, const json &child, const Units &units, const io::ResourceIO &resources)
void grown_stresses(const int el_id, const RowVectorNd &pos, const double t, const Eigen::Matrix3d &F, const double theta, Eigen::Matrix3d &cauchy, Eigen::Matrix3d &pk1, Eigen::Matrix3d &pk2, double &energy) const
std::vector< std::unique_ptr< HGODispersion > > fiber_
std::vector< std::unique_ptr< IsochoricNeoHookean > > iso_
double vn(const int el_id) const
Eigen::Vector3d constant_n0_
T sum_energy(const RowVectorNd &p, const double t, const int el_id, const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, 0, 3, 3 > &F) const
Eigen::Vector3d n0(const int el_id) const
std::vector< std::unique_ptr< VolumePenalty > > vol_
Read-only interface to a hierarchical collection of simulation resources.
virtual std::string describe(const std::string &path) const =0
virtual std::unique_ptr< std::istream > open(const std::string &path, bool binary) const =0
Eigen::MatrixXd read_growth_cell_array(const io::ResourceIO &resources, const std::string &path, const std::string &field, const int ncomp_expected)
Mixed (displacement, growth) assembler implementing the correction energy of finite areal growth of a...
constexpr const char * MATERIAL_ELEMENT_INDEX
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)
Automatic differentiation scalar with first-order derivatives.
Automatic differentiation scalar with first- and second-order derivatives.
static void setVariableCount(size_t value)
Set the independent variable count used by the automatic differentiation layer.