NEML2 2.0.0
Loading...
Searching...
No Matches
ElasticityInterface.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/models/solid_mechanics/elasticity/ElasticityConverter.h"
28#include "neml2/base/MultiEnumSelection.h"
29
30namespace neml2
31{
37template <class Derived, std::size_t N>
38class ElasticityInterface : public Derived
39{
40public:
42
44
45protected:
48
50 std::array<ElasticConstant, N> _constant_types;
51
53 std::array<const Scalar *, N> _constants;
54
56 std::array<bool, N> _need_derivs;
57
58private:
60 const std::vector<CrossRef<Scalar>> _coefs;
61
63 const std::vector<ElasticConstant> _coef_types;
64
66 std::vector<bool> _coef_as_param;
67
69 std::size_t _counter = 0;
70};
71
72} // namespace neml2
73
75// Implementation
77
78namespace neml2
79{
80template <class Derived, std::size_t N>
81OptionSet
83{
84 OptionSet options = Derived::expected_options();
85
87 "BULK_MODULUS",
88 "SHEAR_MODULUS",
89 "YOUNGS_MODULUS",
90 "POISSONS_RATIO",
91 "P_WAVE_MODULUS",
92 "INVALID"},
93 {static_cast<int>(ElasticConstant::LAME_LAMBDA),
94 static_cast<int>(ElasticConstant::BULK_MODULUS),
95 static_cast<int>(ElasticConstant::SHEAR_MODULUS),
96 static_cast<int>(ElasticConstant::YOUNGS_MODULUS),
97 static_cast<int>(ElasticConstant::POISSONS_RATIO),
98 static_cast<int>(ElasticConstant::P_WAVE_MODULUS),
99 static_cast<int>(ElasticConstant::INVALID)},
100 {"INVALID"});
101 options.set<MultiEnumSelection>("coefficient_types") = type_selection;
102 options.set("coefficient_types").doc() =
103 "Types for each parameter, options are: " + type_selection.candidates_str();
104
105 options.set_parameter<std::vector<CrossRef<Scalar>>>("coefficients");
106 options.set("coefficients").doc() = "Coefficients used to define the elasticity tensor";
107
108 options.set<std::vector<bool>>("coefficient_as_parameter") = {true};
109 options.set("coefficient_as_parameter").doc() =
110 "Whether to treat the coefficients as (trainable) parameters. Default is true. Setting this "
111 "option to false will treat the coefficients as buffers.";
112
113 return options;
114}
115
116template <class Derived, std::size_t N>
118 : Derived(options),
119 _coefs(options.get<std::vector<CrossRef<Scalar>>>("coefficients")),
120 _coef_types(options.get<MultiEnumSelection>("coefficient_types").as<ElasticConstant>()),
121 _coef_as_param(options.get<std::vector<bool>>("coefficient_as_parameter"))
122{
123 neml_assert(_coefs.size() == N, "Expected ", N, " coefficients, got ", _coefs.size(), ".");
124 neml_assert(_coef_types.size() == N,
125 "Expected ",
126 N,
127 " entries in coefficient_types, got ",
128 _coef_types.size(),
129 ".");
130 neml_assert(_coef_as_param.size() == 1 || _coef_as_param.size() == N,
131 "Expected 1 or ",
132 N,
133 " entrie(s) in coefficient_as_parameter, got ",
134 _coef_as_param.size(),
135 ".");
136
137 if (_coef_as_param.size() == 1)
138 _coef_as_param.resize(N, _coef_as_param[0]);
139
140 // Fill out _constants, _constant_types, and _constant_names by sorting the coefficients according
141 // to the order defined by ElasticConstant
148
149 // Figure out which coefficients need derivatives
150 for (std::size_t i = 0; i < _constant_types.size(); i++)
151 _need_derivs[i] = (Derived::nl_param(neml2::name(_constant_types[i])) != nullptr);
152}
153
154template <class Derived, std::size_t N>
155void
157{
158 for (std::size_t i = 0; i < _coefs.size(); i++)
159 {
160 neml_assert(_coef_types[i] != ElasticConstant::INVALID,
161 "Invalid coefficient type provided for coefficient ",
162 i,
163 ".");
164
165 if (_coef_types[i] != ptype)
166 continue;
167
168 neml_assert(std::find(_constant_types.begin(),
169 std::next(_constant_types.begin(), _counter),
170 ptype) == std::next(_constant_types.begin(), _counter),
171 "Duplicate coefficient type provided for coefficient ",
172 i,
173 ".");
174
175 const auto pname = neml2::name(ptype);
176 const auto * pval =
177 _coef_as_param[i] ? &Derived::declare_parameter(pname, _coefs[i], /*allow_nonlinear*/ true)
178 : &Derived::declare_buffer(pname, _coefs[i]);
179
180 neml_assert(_counter < N, "Too many coefficients provided.");
181 _constant_types[_counter] = ptype;
182 _constants[_counter] = pval;
183 _counter++;
184
185 return;
186 }
187}
188} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
CrossRef()=default
Interface for objects defining elasticity tensors in terms of other parameters.
Definition ElasticityInterface.h:39
std::array< const Scalar *, N > _constants
Input elastic constants (ordered according to ElasticConstant)
Definition ElasticityInterface.h:53
ElasticityInterface(const OptionSet &options)
Definition ElasticityInterface.h:117
std::array< bool, N > _need_derivs
Whether we need to calculate the derivative of the constants.
Definition ElasticityInterface.h:56
static OptionSet expected_options()
Definition ElasticityInterface.h:82
void declare_elastic_constant(ElasticConstant)
Declare elastic constants (by resolving cross-references)
Definition ElasticityInterface.h:156
std::array< ElasticConstant, N > _constant_types
Input elastic constant types (ordered according to ElasticConstant)
Definition ElasticityInterface.h:50
Selection of multiple enum value from a list of candidates.
Definition MultiEnumSelection.h:41
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:85
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:534
T & set(const std::string &)
Definition OptionSet.h:523
Scalar.
Definition Scalar.h:38
Definition CrossRef.cxx:31
ElasticConstant
Definition ElasticityConverter.h:38
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64