NEML2 2.0.0
Loading...
Searching...
No Matches
ElasticityConverter.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/tensors/Scalar.h"
28
29#include <tuple>
30#include <map>
31#include <unordered_map>
32#include <numeric>
33#include <algorithm>
34
35namespace neml2
36{
37enum class ElasticConstant : std::uint8_t
38{
39 INVALID = 0,
40 // Isotropic
41 LAME_LAMBDA = 1,
42 BULK_MODULUS = 2,
43 SHEAR_MODULUS = 3,
47 // Cubic symmetry
48 CUBIC_C1 = 7,
49 CUBIC_C2 = 8,
50 CUBIC_C3 = 9
51};
52
53std::string name(ElasticConstant p);
54
61template <std::size_t N>
63{
64public:
65 using InputType = std::array<Scalar, N>;
66 using InputPtrType = std::array<const Scalar *, N>;
67 using DerivativeFlagType = std::array<bool, N>;
68 using DerivativeType = std::array<Scalar, N>;
69 using ConversionType = std::pair<Scalar, DerivativeType>;
70 using ResultType = std::array<ConversionType, N>;
71
72 using ConverterKey = std::array<ElasticConstant, N>;
74 using ConversionTableType = std::map<ConverterKey, std::array<ConverterType, N>>;
75
80 : _output_parameterization(output_parameterization),
81 _deriv_requested(deriv_requested)
82 {
83 assert_ascending(output_parameterization);
84 assert_ascending(input_parameterization);
86 "Parameterization not found in the conversion table. This typically means that the "
87 "given combination of Lame parameters is not yet supported.");
88 _converters = table.at(input_parameterization);
89 }
90
93 {
94 ResultType ret{};
95 for (std::size_t i = 0; i < N; ++i)
96 ret[i] = _converters[i](input, _deriv_requested);
97 return ret;
98 }
99
102 {
104 for (std::size_t i = 0; i < N; ++i)
105 input_values[i] = *input[i];
106 return convert(input_values);
107 }
108
111 {
112 return _converters[find_index(p)](input, _deriv_requested);
113 }
114
117 {
119 for (std::size_t i = 0; i < N; ++i)
120 input_values[i] = *input[i];
121 return convert(input_values, p);
122 }
123
124private:
126 void assert_ascending(const ConverterKey & ps) const
127 {
128 for (std::size_t i = 1; i < N; ++i)
129 neml_assert(static_cast<std::uint8_t>(ps[i]) > static_cast<std::uint8_t>(ps[i - 1]),
130 "Internal error: ElasticityConverters only accept Lame parameters sorted in the "
131 "following order: LAME_LAMBDA, BULK_MODULUS, SHEAR_MODULUS, YOUNGS_MODULUS, "
132 "POISSONS_RATIO, P_WAVE_MODULUS.");
133 }
134
136 std::size_t find_index(ElasticConstant p) const
137 {
138 auto it = std::find(_output_parameterization.begin(), _output_parameterization.end(), p);
139 neml_assert(it != _output_parameterization.end(),
140 "Internal error: Lame parameter not found in the output parameterization.");
141 return std::distance(_output_parameterization.begin(), it);
142 }
143
145 const ConverterKey _output_parameterization;
146
148 const DerivativeFlagType _deriv_requested = {};
149
151 std::array<ConverterType, N> _converters;
152};
153
154} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:54
Base class for converters responsible for converting between different parameterizations of the linea...
Definition ElasticityConverter.h:63
std::array< Scalar, N > DerivativeType
Definition ElasticityConverter.h:68
ResultType convert(const InputPtrType &input) const
Convert input to independent elastic constants with derivatives.
Definition ElasticityConverter.h:101
ConversionType convert(const InputPtrType &input, const ElasticConstant p) const
Convert input to a single elastic constant with derivatives.
Definition ElasticityConverter.h:116
ElasticityConverter(const ConversionTableType &table, const ConverterKey &output_parameterization, const ConverterKey &input_parameterization, const DerivativeFlagType &deriv_requested)
Definition ElasticityConverter.h:76
std::array< const Scalar *, N > InputPtrType
Definition ElasticityConverter.h:66
std::array< Scalar, N > InputType
Definition ElasticityConverter.h:65
std::array< bool, N > DerivativeFlagType
Definition ElasticityConverter.h:67
ConversionType convert(const InputType &input, const ElasticConstant p) const
Convert input to a single elastic constant with derivatives.
Definition ElasticityConverter.h:110
std::array< ConversionType, N > ResultType
Definition ElasticityConverter.h:70
std::pair< Scalar, DerivativeType > ConversionType
Definition ElasticityConverter.h:69
ResultType convert(const InputType &input) const
Convert input to independent elastic constants with derivatives.
Definition ElasticityConverter.h:92
std::map< ConverterKey, std::array< ConverterType, N > > ConversionTableType
Definition ElasticityConverter.h:74
std::array< ElasticConstant, N > ConverterKey
Definition ElasticityConverter.h:72
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