NEML2 1.4.0
Loading...
Searching...
No Matches
SR2Invariant.cxx
1// Copyright 2023, 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#include "neml2/models/SR2Invariant.h"
26#include "neml2/tensors/SSR4.h"
27
28namespace neml2
29{
30register_NEML2_object(SR2Invariant);
31
34{
36 options.doc() = "Calculate the invariant of a symmetric second order tensor (of type SR2).";
37
38 options.set<VariableName>("tensor");
39 options.set("tensor").doc() = "SR2 which is used to calculate the invariant of";
40
41 options.set<VariableName>("invariant");
42 options.set("invariant").doc() = "Invariant";
43
44 options.set<std::string>("invariant_type");
45 options.set("invariant_type").doc() = "Type of invariant. Options are I1, I2, and VONMISES.";
46
47 return options;
48}
49
51 : Model(options),
52 _type(options.get<std::string>("invariant_type")),
53 _A(declare_input_variable<SR2>("tensor")),
54 _invariant(declare_output_variable<Scalar>("invariant"))
55{
56}
57
58void
60{
61 auto A = SR2(_A);
62
63 if (_type == "I1")
64 {
65 if (out)
66 _invariant = A.tr();
67 if (dout_din)
69 if (d2out_din2)
70 {
71 // zero
72 }
73 }
74 else if (_type == "I2")
75 {
76 if (out)
77 _invariant = (A.tr() * A.tr() - A.inner(A)) / 2.0;
78 if (dout_din || d2out_din2)
79 {
80 auto I2 = SR2::identity(options());
81 if (dout_din)
82 _invariant.d(_A) = A.tr() * I2 - A;
83 if (d2out_din2)
84 {
88 }
89 }
90 }
91 else if (_type == "VONMISES")
92 {
93 auto S = A.dev();
94 Scalar vm = std::sqrt(3.0 / 2.0) * S.norm(NEML2_EPS);
95
96 if (out)
97 _invariant = vm;
98 if (dout_din || d2out_din2)
99 {
100 auto dvm_dA = 3.0 / 2.0 * S / vm;
101 if (dout_din)
103 if (d2out_din2)
104 {
105 auto I = SSR4::identity_sym(options());
106 auto J = SSR4::identity_dev(options());
107 _invariant.d(_A, _A) = 3.0 / 2.0 * (I - 2.0 / 3.0 * dvm_dA.outer(dvm_dA)) * J / vm;
108 }
109 }
110 }
111 else if (_type == "EFFECTIVE_STRAIN")
112 {
113 Scalar r = std::sqrt(2.0 / 3.0) * A.norm(NEML2_EPS);
114
115 if (out)
116 _invariant = r;
117
118 if (dout_din || d2out_din2)
119 {
120 auto d = 2.0 / 3.0 * A / r;
121
122 if (dout_din)
123 _invariant.d(_A) = 2.0 / 3.0 * A / r;
124
125 if (d2out_din2)
126 _invariant.d(_A, _A) =
127 2.0 / 3.0 * (SSR4::identity_sym(options()) - 3.0 / 2.0 * d.outer(d)) / r;
128 }
129 }
130 else
131 throw NEMLException("Unsupported invariant type: " + _type);
132}
133} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
The base class for all constitutive models.
Definition Model.h:53
const torch::TensorOptions & options() const
This model's tensor options.
Definition Model.h:116
static OptionSet expected_options()
Definition Model.cxx:32
Definition error.h:33
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
Definition SR2Invariant.h:32
SR2Invariant(const OptionSet &options)
Definition SR2Invariant.cxx:50
const Variable< SR2 > & _A
Input second order tensor.
Definition SR2Invariant.h:44
const std::string _type
Definition SR2Invariant.h:41
Variable< Scalar > & _invariant
Invariant of the input tensor.
Definition SR2Invariant.h:47
static OptionSet expected_options()
Definition SR2Invariant.cxx:33
void set_value(bool out, bool dout_din, bool d2out_din2) override
The map between input -> output, and optionally its derivatives.
Definition SR2Invariant.cxx:59
The (logical) symmetric second order tensor.
Definition SR2.h:46
static SR2 identity(const torch::TensorOptions &options=default_tensor_options())
Identity.
Definition SR2.cxx:110
static SSR4 identity_dev(const torch::TensorOptions &options=default_tensor_options())
Create the deviatoric identity tensor .
Definition SSR4.cxx:70
static SSR4 identity(const torch::TensorOptions &options=default_tensor_options())
Create the identity tensor .
Definition SSR4.cxx:45
static SSR4 identity_sym(const torch::TensorOptions &options=default_tensor_options())
Create the symmetric identity tensor .
Definition SSR4.cxx:58
The (logical) scalar.
Definition Scalar.h:38
Derivative d(const VariableBase &x)
Create a wrapper representing the derivative dy/dx.
Definition Variable.cxx:102
Definition CrossRef.cxx:32