NEML2 1.4.0
Loading...
Searching...
No Matches
SR2.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/misc/math.h"
26#include "neml2/tensors/SR2.h"
27#include "neml2/tensors/Scalar.h"
28#include "neml2/tensors/R2.h"
29#include "neml2/tensors/R3.h"
30#include "neml2/tensors/SFR3.h"
31#include "neml2/tensors/SSR4.h"
32#include "neml2/tensors/SFFR4.h"
33#include "neml2/tensors/Rot.h"
34#include "neml2/tensors/SWR4.h"
35#include "neml2/tensors/WR2.h"
36#include "neml2/tensors/R4.h"
37
38namespace neml2
39{
40SR2::SR2(const R2 & T)
41 : SR2(math::full_to_mandel((T + T.transpose()) / 2.0))
42{
43}
44
45SR2
46SR2::fill(const Real & a, const torch::TensorOptions & options)
47{
48 return SR2::fill(Scalar(a, options));
49}
50
51SR2
52SR2::fill(const Scalar & a)
53{
54 auto zero = torch::zeros_like(a);
55 return SR2(torch::stack({a, a, a, zero, zero, zero}, -1), a.batch_dim());
56}
57
58SR2
60 const Real & a22,
61 const Real & a33,
62 const torch::TensorOptions & options)
63{
64 return SR2::fill(Scalar(a11, options), Scalar(a22, options), Scalar(a33, options));
65}
66
67SR2
68SR2::fill(const Scalar & a11, const Scalar & a22, const Scalar & a33)
69{
70 auto zero = torch::zeros_like(a11);
71 return SR2(torch::stack({a11, a22, a33, zero, zero, zero}, -1), a11.batch_dim());
72}
73
74SR2
76 const Real & a22,
77 const Real & a33,
78 const Real & a23,
79 const Real & a13,
80 const Real & a12,
81 const torch::TensorOptions & options)
82{
83 return SR2::fill(Scalar(a11, options),
84 Scalar(a22, options),
85 Scalar(a33, options),
86 Scalar(a23, options),
87 Scalar(a13, options),
88 Scalar(a12, options));
89}
90
91SR2
93 const Scalar & a22,
94 const Scalar & a33,
95 const Scalar & a23,
96 const Scalar & a13,
97 const Scalar & a12)
98{
99 return SR2(torch::stack({a11,
100 a22,
101 a33,
105 -1),
106 a11.batch_dim());
107}
108
109SR2
110SR2::identity(const torch::TensorOptions & options)
111{
112 return SR2(torch::tensor({1, 1, 1, 0, 0, 0}, options), 0);
113}
114
115SSR4
116SR2::identity_map(const torch::TensorOptions & options)
117{
118 return SSR4::identity_sym(options);
119}
120
121SR2
122SR2::rotate(const Rot & r) const
123{
124 return R2(*this).rotate(r);
125}
126
127SR2
128SR2::rotate(const R2 & R) const
129{
130 return R2(*this).rotate(R);
131}
132
133SFR3
134SR2::drotate(const Rot & r) const
135{
136 auto dR = R2(*this).drotate(r);
137 return math::full_to_mandel(dR);
138}
139
140SFFR4
141SR2::drotate(const R2 & R) const
142{
143 auto dR = R2(*this).drotate(R);
144 return math::full_to_mandel(dR);
145}
146
147Scalar
153
154Scalar
155SR2::tr() const
156{
157 return Scalar(torch::sum(base_index({torch::indexing::Slice(0, 3)}), {-1}), batch_dim());
158}
159
160SR2
161SR2::vol() const
162{
163 return SR2::fill(tr()) / 3;
164}
165
166SR2
167SR2::dev() const
168{
169 return *this - vol();
170}
171
172Scalar
173SR2::det() const
174{
175 auto a00 = (*this)(0, 0);
176 auto a11 = (*this)(1, 1);
177 auto a22 = (*this)(2, 2);
178 auto a12 = (*this)(1, 2);
179 auto a02 = (*this)(0, 2);
180 auto a01 = (*this)(0, 1);
181 return a00 * (a11 * a22 - a12 * a12) + a01 * (a12 * a02 - a01 * a22) +
182 a02 * (a01 * a12 - a11 * a02);
183}
184
185Scalar
186SR2::inner(const SR2 & other) const
187{
189 return Scalar(torch::linalg_vecdot(*this, other), broadcast_batch_dim(*this, other));
190}
191
192Scalar
194{
195 return inner(*this);
196}
197
198Scalar
199SR2::norm(Real eps) const
200{
201 return math::sqrt(norm_sq() + eps);
202}
203
204SSR4
205SR2::outer(const SR2 & other) const
206{
208 return SSR4(torch::einsum("...i,...j", {*this, other}), broadcast_batch_dim(*this, other));
209}
210
211SR2
213{
214 return R2(*this).inverse();
215}
216
217SR2
219{
220 return *this;
221}
222
223} // namespace neml2
TorchSize batch_dim() const
Return the number of batch dimensions.
Definition BatchTensorBase.cxx:128
BatchTensor base_index(const TorchSlice &indices) const
Return an index sliced on the base dimensions.
Definition BatchTensorBase.cxx:193
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Derived rotate(const Rot &r) const
Rotate using a Rodrigues vector.
Definition R2Base.cxx:177
R3 drotate(const Rot &r) const
Derivative of the rotated tensor w.r.t. the Rodrigues vector.
Definition R2Base.cxx:191
Derived inverse() const
Inversion.
Definition R2Base.cxx:219
A basic R2.
Definition R2.h:42
Rotation stored as modified Rodrigues parameters.
Definition Rot.h:49
The logical fourth order tensor with minor symmetry in the 1st and 2nd dimensions.
Definition SFFR4.h:37
The logical third order tensor with symmetry in the first two dimensions.
Definition SFR3.h:38
The (logical) symmetric second order tensor.
Definition SR2.h:46
SR2 transpose() const
Transpose, no-op.
Definition SR2.cxx:218
SFR3 drotate(const Rot &r) const
Derivative of the rotated tensor w.r.t. the Rodrigues vector.
Definition SR2.cxx:134
Scalar tr() const
Trace.
Definition SR2.cxx:155
SR2 inverse() const
Inversion.
Definition SR2.cxx:212
static SR2 identity(const torch::TensorOptions &options=default_tensor_options())
Identity.
Definition SR2.cxx:110
SR2(const R2 &T)
Symmetrize an R2 then fill.
Definition SR2.cxx:40
Scalar inner(const SR2 &other) const
Double contraction ij,ij.
Definition SR2.cxx:186
SSR4 outer(const SR2 &other) const
Outer product ij,kl -> ijkl.
Definition SR2.cxx:205
Scalar det() const
Determinant.
Definition SR2.cxx:173
SR2 rotate(const Rot &r) const
Rotate.
Definition SR2.cxx:122
SR2 dev() const
Deviatoric part of the tensor.
Definition SR2.cxx:167
Scalar norm_sq() const
Norm squared.
Definition SR2.cxx:193
Scalar operator()(TorchSize i, TorchSize j) const
Accessor.
Definition SR2.cxx:148
Scalar norm(Real eps=0) const
Norm.
Definition SR2.cxx:199
SR2 vol() const
Volumetric part of the tensor.
Definition SR2.cxx:161
static SSR4 identity_map(const torch::TensorOptions &options=default_tensor_options())
The derivative of a SR2 with respect to itself.
Definition SR2.cxx:116
static SR2 fill(const Real &a, const torch::TensorOptions &options=default_tensor_options())
Fill the diagonals with a11 = a22 = a33 = a.
Definition SR2.cxx:46
The (logical) symmetric fourth order tensor, with symmetry in the first two dimensionss as well as in...
Definition SSR4.h:44
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
constexpr Real mandel_factor(TorchSize i)
Definition math.h:50
constexpr TorchSize mandel_reverse_index[3][3]
Definition math.h:43
BatchTensor full_to_mandel(const BatchTensor &full, TorchSize dim)
Convert a BatchTensor from full notation to Mandel notation.
Definition math.cxx:169
Derived sqrt(const Derived &a)
Definition BatchTensorBase.h:439
Definition CrossRef.cxx:32
int64_t TorchSize
Definition types.h:35
TorchSize broadcast_batch_dim(T &&...)
The batch dimension after broadcasting.
double Real
Definition types.h:33
void neml_assert_broadcastable_dbg(T &&...)
A helper function to assert (in Debug mode) that all tensors are broadcastable.