17 return (E * nu) / ((1.0 + nu) * (1.0 - 2.0 * nu));
19 return (nu * E) / (1.0 - nu * nu);
24 std::vector<Eigen::Vector3d> read_cell_vectors_legacy_vtk(
26 const std::string &description,
27 const std::string &field_name)
29 std::vector<Eigen::Vector3d> out;
34 while (std::getline(in, line))
36 std::istringstream ss(line);
39 if (tok ==
"CELL_DATA")
43 else if (tok ==
"VECTORS")
47 if (name != field_name)
51 out.resize(n_cell_data);
52 for (
int i = 0; i < n_cell_data; ++i)
54 if (!(in >> out[i].
x() >> out[i].
y() >> out[i].
z()))
56 "Fiber VTK '{}' ended early: expected {} vectors", description, n_cell_data));
64 "VECTORS '{}' not found under CELL_DATA in {}", field_name, description));
70 return E / (2.0 * (1.0 + nu));
75 : param_name_(param_name)
91 for (
int i =
param_.size(); i <= index; ++i)
94 param_.back().set_unit_type(unit_type);
99 const bool was_constant =
param_[index].is_constant();
100 const bool was_time_dependent =
param_[index].is_time_dependent();
113 const double x = p(0);
114 const double y = p(1);
115 const double z = p.size() == 3 ? p(2) : 0;
117 return (*
this)(
x,
y,
z, t, index);
126 return tmp_param(
x,
y,
z, t, index);
130 : param_name_(param_name)
143 for (
int i = 0; i < params_array.size(); ++i)
146 for (
int i = 0; i <
params_.size(); ++i)
148 for (
int j =
params_.at(i).param_.size(); j <= index; ++j)
150 params_.at(i).param_.emplace_back();
151 params_.at(i).param_.back().set_unit_type(unit_type);
154 const bool was_constant =
params_.at(i).param_[index].is_constant();
155 const bool was_time_dependent =
params_.at(i).param_[index].is_time_dependent();
156 params_.at(i).param_[index].init(params_array[i], resources);
157 params_.at(i).nonconstant_count_ -= !was_constant;
158 params_.at(i).nonconstant_count_ += !
params_.at(i).param_[index].is_constant();
159 params_.at(i).time_dependent_count_ -= was_time_dependent;
160 params_.at(i).time_dependent_count_ +=
params_.at(i).param_[index].is_time_dependent();
210 entries[3], stress_units, resources);
236 stress_units, resources);
251 entries[8], stress_units, resources);
290 (*this)(0, 0) = 2 * mu + lambda;
291 (*this)(0, 1) = lambda;
294 (*this)(1, 1) = 2 * mu + lambda;
301 (*this)(0, 0) = 2 * mu + lambda;
302 (*this)(0, 1) = lambda;
303 (*this)(0, 2) = lambda;
308 (*this)(1, 1) = 2 * mu + lambda;
309 (*this)(1, 2) = lambda;
314 (*this)(2, 2) = 2 * mu + lambda;
336 0.0, 0.0, (1.0 - nu) / 2.0;
344 v, 1. - v, v, 0, 0, 0,
345 v, v, 1. - v, 0, 0, 0,
346 0, 0, 0, (1. - 2. * v) / 2., 0, 0,
347 0, 0, 0, 0, (1. - 2. * v) / 2., 0,
348 0, 0, 0, 0, 0, (1. - 2. * v) / 2.;
354 double Ex,
double Ey,
double Ez,
355 double nuXY,
double nuXZ,
double nuYZ,
356 double muYZ,
double muZX,
double muXY,
const std::string &stress_units,
const io::ResourceIO &resources)
361 double nuYX = nuXY * Ey / Ex;
362 double nuZX = nuXZ * Ez / Ex;
363 double nuZY = nuYZ * Ez / Ey;
365 Eigen::MatrixXd compliance;
366 compliance.setZero(6, 6);
367 compliance << 1 / Ex, -nuYX / Ey, -nuZX / Ez, 0, 0, 0,
368 -nuXY / Ex, 1 / Ey, -nuZY / Ez, 0, 0, 0,
369 -nuXZ / Ex, -nuYZ / Ey, 1 / Ez, 0, 0, 0,
370 0, 0, 0, 1 / (2 * muYZ), 0, 0,
371 0, 0, 0, 0, 1 / (2 * muZX), 0,
372 0, 0, 0, 0, 0, 1 / (2 * muXY);
381 double nuYX = nuXY * Ey / Ex;
383 Eigen::MatrixXd compliance;
384 compliance.setZero(3, 3);
385 compliance << 1.0 / Ex, -nuYX / Ey, 0.0,
386 -nuXY / Ex, 1.0 / Ey, 0.0,
387 0.0, 0.0, 1.0 / (2 * muXY);
392 double Et,
double Ea,
393 double nu_t,
double nu_a,
394 double Ga,
const std::string &stress_units,
const io::ResourceIO &resources)
399 Eigen::MatrixXd compliance;
400 compliance.setZero(6, 6);
401 compliance << 1 / Et, -nu_t / Et, -nu_a / Ea, 0, 0, 0,
402 -nu_t / Et, 1 / Et, -nu_a / Ea, 0, 0, 0,
403 -nu_a / Ea, -nu_a / Ea, 1 / Ea, 0, 0, 0,
404 0, 0, 0, 1 / Ga, 0, 0,
405 0, 0, 0, 0, 1 / Ga, 0,
406 0, 0, 0, 0, 0, (2 * (1 + nu_t)) / Et;
415 for (
int k = 0; k < DIM; ++k)
416 res += (*
this)(j, k) * strain[k];
452 double llambda = tmp1(
x,
y,
z, t, el_id);
453 double mmu = tmp2(
x,
y,
z, t, el_id);
472 assert(!std::isnan(lambda));
473 assert(!std::isnan(mu));
474 assert(!std::isinf(lambda));
475 assert(!std::isinf(mu));
480 const int size = is_volume ? 3 : 2;
492 if (params.count(
"young"))
494 set_e_nu(index, params[
"young"], params[
"nu"], stress_unit, resources);
496 else if (params.count(
"E"))
498 set_e_nu(index, params[
"E"], params[
"nu"], stress_unit, resources);
500 else if (params.count(
"lambda"))
503 mu_or_nu_[index].init(params[
"mu"], resources);
506 mu_or_nu_[index].set_unit_type(stress_unit);
532 rho_.back().init(1.0);
547 assert(
rho_.size() == 1 || el_id <
rho_.size());
549 const auto &tmp =
rho_.size() == 1 ?
rho_[0] :
rho_[el_id];
550 const double res = tmp(
x,
y,
z, t, el_id);
551 assert(!std::isnan(res));
552 assert(!std::isinf(res));
558 for (
int i =
rho_.size(); i <= index; ++i)
563 const bool was_constant =
rho_[index].is_constant();
564 const bool was_time_dependent =
rho_[index].is_time_dependent();
565 if (params.count(
"rho"))
567 rho_[index].init(params[
"rho"], resources);
569 else if (params.count(
"density"))
571 rho_[index].init(params[
"density"], resources);
576 rho_[index].set_unit_type(density_unit);
584 : rho_(
"rho"), heat_capacity_(
"heat_capacity")
614 assert(!std::isnan(res));
615 assert(!std::isinf(res));
621 return rho_(p, t, el_id);
635 assert(size == 2 || size == 3);
639 for (
const auto &m :
dir_)
641 assert((m.rows() == size && m.cols() == size) || (m.rows() == size && m.cols() == 1));
646 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3>
FiberDirection::operator()(
double px,
double py,
double pz,
double x,
double y,
double z,
double t,
int el_id)
const
652 if (el_id < 0 || el_id >=
static_cast<int>(
per_el_fibers_.size()))
654 "Fiber el_id {} out of range [0,{})", el_id,
per_el_fibers_.size()));
655 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> res;
656 res.resize(
size_, 1);
657 for (
int i = 0; i <
size_; ++i)
662 assert(
dir_.size() == 1 || el_id <
dir_.size());
664 const auto &tmp =
dir_.size() == 1 ?
dir_[0] :
dir_[el_id];
665 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> res;
666 res.resize(tmp.rows(), tmp.cols());
667 for (
int i = 0; i < tmp.rows(); ++i)
669 for (
int j = 0; j < tmp.cols(); ++j)
671 res(i, j) = tmp(i, j)(
x,
y,
z, t, el_id);
673 assert(!std::isnan(res(i, j)));
674 assert(!std::isinf(res(i, j)));
680 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6>
FiberDirection::stiffness_rotation_voigt(
double px,
double py,
double pz,
double x,
double y,
double z,
double t,
int el_id)
const
685 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3> rot = (*this)(px, py, pz,
x,
y,
z, t, el_id);
686 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6> res;
688 int dim = rot.rows();
690 static const double sqrt2 = std::sqrt(2.0);
705 res << rot(0, 0) * rot(0, 0), rot(0, 1) * rot(0, 1), rot(0, 2) * rot(0, 2), sqrt2 * rot(0, 1) * rot(0, 2), sqrt2 * rot(0, 0) * rot(0, 2), sqrt2 * rot(0, 0) * rot(0, 1),
706 rot(1, 0) * rot(1, 0), rot(1, 1) * rot(1, 1), rot(1, 2) * rot(1, 2), sqrt2 * rot(1, 1) * rot(1, 2), sqrt2 * rot(1, 0) * rot(1, 2), sqrt2 * rot(1, 0) * rot(1, 1),
707 rot(2, 0) * rot(2, 0), rot(2, 1) * rot(2, 1), rot(2, 2) * rot(2, 2), sqrt2 * rot(2, 1) * rot(2, 2), sqrt2 * rot(2, 0) * rot(2, 2), sqrt2 * rot(2, 0) * rot(2, 1),
708 sqrt2 * rot(1, 0) * rot(2, 0), sqrt2 * rot(1, 1) * rot(2, 1), sqrt2 * rot(1, 2) * rot(2, 2), rot(1, 1) * rot(2, 2) + rot(1, 2) * rot(2, 1), rot(1, 0) * rot(2, 2) + rot(1, 2) * rot(2, 0), rot(1, 0) * rot(2, 1) + rot(1, 1) * rot(2, 0),
709 sqrt2 * rot(0, 0) * rot(2, 0), sqrt2 * rot(0, 1) * rot(2, 1), sqrt2 * rot(0, 2) * rot(2, 2), rot(0, 1) * rot(2, 2) + rot(0, 2) * rot(2, 1), rot(0, 0) * rot(2, 2) + rot(0, 2) * rot(2, 0), rot(0, 0) * rot(2, 1) + rot(0, 1) * rot(2, 0),
710 sqrt2 * rot(0, 0) * rot(1, 0), sqrt2 * rot(0, 1) * rot(1, 1), sqrt2 * rot(0, 2) * rot(1, 2), rot(0, 1) * rot(1, 2) + rot(0, 2) * rot(1, 1), rot(0, 0) * rot(1, 2) + rot(0, 2) * rot(1, 0), rot(0, 0) * rot(1, 1) + rot(0, 1) * rot(1, 0);
724 if (dir.is_object() && dir.value(
"type", std::string()) ==
"per_element_file")
726 const std::string field = dir.value(
"field", std::string(
"FIB_DIR1"));
727 const std::string path = dir.at(
"path").get<std::string>();
728 const std::string description = resources.
describe(path);
729 auto input = resources.
open(path,
false);
731 per_el_fibers_ = read_cell_vectors_legacy_vtk(*input, description, field);
734 const double n = v.norm();
741 logger().info(
"FiberDirection: loaded {} per-element fibers ('{}') from {}",
746 for (
int i =
dir_.size(); i <= index; ++i)
751 if (dir.size() == 3 || dir.size() == 2)
753 const int size = dir.size();
754 const int other_size = dir[0].is_array() ? size : 1;
756 assert(size ==
size_);
757 dir_[index].resize(size, other_size);
758 for (
int i = 0; i < size; ++i)
762 if (dir[i].is_array())
764 log_and_throw_error(fmt::format(
"Fiber must be a {} vector, row {} is {}", size, i, dir[i].dump()));
766 dir_[index](i, 0).init(dir[i], resources);
767 dir_[index](i, 0).set_unit_type(unit);
770 if (dir[i].size() != size)
772 log_and_throw_error(fmt::format(
"Fiber must be {}x{}, row {} is {}", size, other_size, i, dir[i].dump()));
774 for (
int j = 0; j < size; ++j)
776 dir_[index](i, j).init(dir[i][j], resources);
777 dir_[index](i, j).set_unit_type(unit);
782 else if (dir.size() == 9 || dir.size() == 4)
784 const int size = dir.size() == 9 ? 3 : 2;
785 assert(size ==
size_);
786 dir_[index].resize(size, size);
787 for (
int i = 0; i < size; ++i)
789 for (
int j = 0; j < size; ++j)
791 dir_[index](i, j).init(dir[i * size + j], resources);
792 dir_[index](i, j).set_unit_type(unit);
797 else if (dir.empty())
800 for (
int i = 0; i <
size_; ++i)
802 for (
int j = 0; j <
size_; ++j)
804 dir_[index](i, j).init(i == j ? 1.0 : 0.0);
805 dir_[index](i, j).set_unit_type(unit);
817 template double ElasticityTensor::compute_stress<3>(
const std::array<double, 3> &strain,
const int j)
const;
818 template double ElasticityTensor::compute_stress<6>(
const std::array<double, 6> &strain,
const int j)
const;
std::vector< Eigen::Triplet< double > > entries
std::vector< utils::ExpressionValue > rho_
std::size_t time_dependent_count_
virtual bool is_time_dependent() const
std::size_t nonconstant_count_
virtual void add_multimaterial(const int index, const json ¶ms, const std::string &density_unit, const io::ResourceIO &resources)
virtual double operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
virtual bool is_constant() const
Each material entry is independent of coordinates, time and index.
double operator()(int i, int j) const
void set_from_young_poisson(const double young, const double poisson, const std::string &stress_unit, const io::ResourceIO &resources)
void set_from_entries(const std::vector< double > &entries, const std::string &stress_unit, const io::ResourceIO &resources)
void rotate_stiffness(const Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > &rotation_mtx_voigt)
void resize(const int size)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > stiffness_tensor_
void set_transversely_isotropic(double Et, double Ea, double nu_t, double nu_a, double Ga, const std::string &stress_units, const io::ResourceIO &resources)
void set_orthotropic(double Ex, double Ey, double Ez, double nuXY, double nuXZ, double nuYZ, double muYZ, double muZX, double muXY, const std::string &stress_unit, const io::ResourceIO &resources)
void set_from_lambda_mu(const double lambda, const double mu, const std::string &stress_unit, const io::ResourceIO &resources)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 0, 6, 6 > reference_stiffness_tensor_
double compute_stress(const std::array< double, DIM > &strain, const int j) const
void unrotate_stiffness()
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 1, 6, 6 > stiffness_rotation_voigt(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
std::vector< Eigen::Matrix< utils::ExpressionValue, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3 > > dir_
bool use_per_element_file_
void resize(const int size)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, 1, 3, 3 > operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const
std::vector< Eigen::Vector3d > per_el_fibers_
void add_multimaterial(const int index, const json ¶ms, const std::string &unit, const io::ResourceIO &resources)
std::size_t time_dependent_count_
double operator()(const RowVectorNd &p, double t, int index) const
bool is_time_dependent() const
GenericMatParam(const std::string ¶m_name)
void add_multimaterial(const int index, const json ¶ms, const std::string &unit_type, const io::ResourceIO &resources)
std::vector< utils::ExpressionValue > param_
const std::string param_name_
std::size_t nonconstant_count_
std::vector< GenericMatParam > params_
void add_multimaterial(const int index, const json ¶ms, const std::string &unit_type, const io::ResourceIO &resources)
GenericMatParams(const std::string ¶m_name)
const std::string param_name_
std::vector< utils::ExpressionValue > mu_or_nu_
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)
std::vector< utils::ExpressionValue > lambda_or_E_
void set_e_nu(const int index, const json &E, const json &nu, const std::string &stress_unit, const io::ResourceIO &resources)
Eigen::MatrixXd lambda_mat_
bool is_constant() const override
Each material entry is independent of coordinates, time and index.
bool is_time_dependent() const override
double rho(const RowVectorNd &p, double t, int el_id) const
double heat_capacity(const RowVectorNd &p, double t, int el_id) const
GenericMatParam heat_capacity_
double operator()(double px, double py, double pz, double x, double y, double z, double t, int el_id) const override
void add_multimaterial(const int index, const json ¶ms, const std::string &density_unit, const io::ResourceIO &resources) override
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
constexpr const char * MATERIAL_ELEMENT_INDEX
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
spdlog::logger & logger()
Retrieves the current logger.
double convert_to_mu(const double E, const double nu)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
double convert_to_lambda(const bool is_volume, const double E, const double nu)
void log_and_throw_error(const std::string &msg)