27#include "neml2/models/solid_mechanics/elasticity/ElasticityConverter.h"
28#include "neml2/base/MultiEnumSelection.h"
29#include "neml2/tensors/Scalar.h"
30#include "neml2/base/OptionSet.h"
31#include "neml2/base/TensorName.h"
40template <
class Derived, std::
size_t N>
63 std::vector<TensorName<Scalar>> _coefs;
66 const std::vector<ElasticConstant> _coef_types;
69 std::vector<bool> _coef_as_param;
72 std::size_t _counter = 0;
83template <
class Derived, std::
size_t N>
87 OptionSet options = Derived::expected_options();
106 "Types for each parameter, options are: " +
107 type_selection.
join());
110 "coefficients",
"Coefficients used to define the elasticity tensor");
112 options.
add<std::vector<bool>>(
113 "coefficient_as_parameter",
115 "Whether to treat the coefficients as (trainable) parameters. Default is true. Setting this "
116 "option to false will treat the coefficients as buffers.");
121template <
class Derived, std::
size_t N>
126 _coef_as_param(options.get<std::vector<bool>>(
"coefficient_as_parameter"))
128 if (_coefs.size() != N)
129 throw NEMLException(
"Expected " + std::to_string(N) +
" coefficients, got " +
130 std::to_string(_coefs.size()) +
".");
131 if (_coef_types.size() != N)
132 throw NEMLException(
"Expected " + std::to_string(N) +
" coefficient types, got " +
133 std::to_string(_coef_types.size()) +
".");
134 if (_coef_as_param.size() != 1 && _coef_as_param.size() != N)
136 " entries in coefficient_as_parameter, got " +
137 std::to_string(_coef_as_param.size()) +
".");
139 if (_coef_as_param.size() == 1)
140 _coef_as_param.resize(N, _coef_as_param[0]);
156template <
class Derived, std::
size_t N>
160 for (std::size_t i = 0; i < _coefs.size(); i++)
163 throw NEMLException(
"Invalid coefficient type provided for coefficient " + std::to_string(i) +
166 if (_coef_types[i] != ptype)
171 throw NEMLException(
"Duplicate coefficient type provided for coefficient " +
172 std::to_string(i) +
".");
175 const auto * pval = _coef_as_param[i]
176 ? &Derived::template declare_parameter<Scalar>(
177 pname, _coefs[i],
true)
178 : &Derived::template declare_buffer<Scalar>(pname, _coefs[i]);
std::array< const Scalar *, N > _constants
Input elastic constants (ordered according to ElasticConstant).
Definition ElasticityInterface.h:56
ElasticityInterface(const OptionSet &options)
Definition ElasticityInterface.h:122
std::array< bool, N > _need_derivs
Whether we need to calculate the derivative of the constants.
Definition ElasticityInterface.h:59
static OptionSet expected_options()
Definition ElasticityInterface.h:85
void declare_elastic_constant(ElasticConstant)
Declare elastic constants (by resolving cross-references).
Definition ElasticityInterface.h:158
std::array< ElasticConstant, N > _constant_types
Input elastic constant types (ordered according to ElasticConstant).
Definition ElasticityInterface.h:53
std::string join(const std::string &separator=", ") const
Join the choices into a single string with the given separator.
Selection of multiple enum value from a list of choices.
Definition MultiEnumSelection.h:41
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:54
void add(const std::string &name, const T &default_value, std::string doc)
Create an option with its default value and its docstring.
Definition OptionSet.h:422
Scalar.
Definition Scalar.h:38
Definition DiagnosticsInterface.h:31
ElasticConstant
Definition ElasticityConverter.h:38
@ P_WAVE_MODULUS
Definition ElasticityConverter.h:46
@ SHEAR_MODULUS
Definition ElasticityConverter.h:43
@ POISSONS_RATIO
Definition ElasticityConverter.h:45
@ BULK_MODULUS
Definition ElasticityConverter.h:42
@ YOUNGS_MODULUS
Definition ElasticityConverter.h:44
@ LAME_LAMBDA
Definition ElasticityConverter.h:41
@ INVALID
Definition ElasticityConverter.h:39
std::string name(ElasticConstant p)
@ PARAMETER
Definition types.h:94
The name of a tensor object that can be referenced in the input files.
Definition TensorName.h:47