NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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{
40 // Isotropic
47 // Cubic symmetry
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
77 const ConverterKey & output_parameterization,
78 const ConverterKey & input_parameterization,
79 const DerivativeFlagType & deriv_requested)
80 : _output_parameterization(output_parameterization),
81 _deriv_requested(deriv_requested)
82 {
83 assert_ascending(output_parameterization);
84 assert_ascending(input_parameterization);
85 if (!table.count(input_parameterization))
86 throw NEMLException(
87 "Parameterization not found in the conversion table. This typically means that the "
88 "given combination of Lame parameters is not yet supported.");
89 _converters = table.at(input_parameterization);
90 }
91
93 ResultType convert(const InputType & input) const
94 {
95 ResultType ret{};
96 for (std::size_t i = 0; i < N; ++i)
97 ret[i] = _converters[i](input, _deriv_requested);
98 return ret;
99 }
100
102 ResultType convert(const InputPtrType & input) const
103 {
104 InputType input_values{};
105 for (std::size_t i = 0; i < N; ++i)
106 input_values[i] = *input[i];
107 return convert(input_values);
108 }
109
111 ConversionType convert(const InputType & input, const ElasticConstant p) const
112 {
113 return _converters[find_index(p)](input, _deriv_requested);
114 }
115
118 {
119 InputType input_values{};
120 for (std::size_t i = 0; i < N; ++i)
121 input_values[i] = *input[i];
122 return convert(input_values, p);
123 }
124
125private:
127 void assert_ascending(const ConverterKey & ps) const
128 {
129 for (std::size_t i = 1; i < N; ++i)
130 if (static_cast<std::uint8_t>(ps[i]) <= static_cast<std::uint8_t>(ps[i - 1]))
131 throw NEMLException(
132 "Internal error: ElasticityConverters only accept Lame parameters sorted in the "
133 "following order: LAME_LAMBDA, BULK_MODULUS, SHEAR_MODULUS, YOUNGS_MODULUS, "
134 "POISSONS_RATIO, P_WAVE_MODULUS.");
135 }
136
138 std::size_t find_index(ElasticConstant p) const
139 {
140 auto it = std::find(_output_parameterization.begin(), _output_parameterization.end(), p);
141 if (it == _output_parameterization.end())
142 throw NEMLException(
143 "Internal error: Lame parameter not found in the output parameterization.");
144 return std::distance(_output_parameterization.begin(), it);
145 }
146
148 const ConverterKey _output_parameterization;
149
151 const DerivativeFlagType _deriv_requested = {};
152
154 std::array<ConverterType, N> _converters;
155};
156
157} // namespace neml2
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:102
ConversionType convert(const InputPtrType &input, const ElasticConstant p) const
Convert input to a single elastic constant with derivatives.
Definition ElasticityConverter.h:117
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:111
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:93
std::map< ConverterKey, std::array< ConverterType, N > > ConversionTableType
Definition ElasticityConverter.h:74
std::array< ElasticConstant, N > ConverterKey
Definition ElasticityConverter.h:72
ConversionType(*)(const InputType &, const DerivativeFlagType &) ConverterType
Definition ElasticityConverter.h:73
Definition errors.h:34
Definition DiagnosticsInterface.cxx:30
ElasticConstant
Definition ElasticityConverter.h:38
@ CUBIC_C1
Definition ElasticityConverter.h:48
@ 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
@ CUBIC_C3
Definition ElasticityConverter.h:50
@ INVALID
Definition ElasticityConverter.h:39
@ CUBIC_C2
Definition ElasticityConverter.h:49
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30