NEML2 1.4.0
Loading...
Searching...
No Matches
math.h
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#pragma once
26
27#include "neml2/tensors/BatchTensor.h"
28
29namespace neml2
30{
31class SR2;
32class WR2;
33class SWR4;
34class SSR4;
35class WSR4;
36namespace math
37{
38constexpr Real eps = std::numeric_limits<at::scalar_value_type<Real>::type>::epsilon();
39
40constexpr Real sqrt2 = 1.4142135623730951;
41constexpr Real invsqrt2 = 0.7071067811865475;
42
43constexpr TorchSize mandel_reverse_index[3][3] = {{0, 5, 4}, {5, 1, 3}, {4, 3, 2}};
44constexpr TorchSize mandel_index[6][2] = {{0, 0}, {1, 1}, {2, 2}, {1, 2}, {0, 2}, {0, 1}};
45
46constexpr TorchSize skew_reverse_index[3][3] = {{0, 2, 1}, {2, 0, 0}, {1, 0, 0}};
47constexpr Real skew_factor[3][3] = {{0.0, -1.0, 1.0}, {1.0, 0.0, -1.0}, {-1.0, 1.0, 0.0}};
48
49inline constexpr Real
51{
52 return i < 3 ? 1.0 : sqrt2;
53}
54
64{
66
67 // Get the global constants
68 static ConstantTensors & get();
69
70 static const torch::Tensor & full_to_mandel_map();
71 static const torch::Tensor & mandel_to_full_map();
72 static const torch::Tensor & full_to_mandel_factor();
73 static const torch::Tensor & mandel_to_full_factor();
74 static const torch::Tensor & full_to_skew_map();
75 static const torch::Tensor & skew_to_full_map();
76 static const torch::Tensor & full_to_skew_factor();
77 static const torch::Tensor & skew_to_full_factor();
78
79private:
80 torch::Tensor _full_to_mandel_map;
81 torch::Tensor _mandel_to_full_map;
82 torch::Tensor _full_to_mandel_factor;
83 torch::Tensor _mandel_to_full_factor;
84 torch::Tensor _full_to_skew_map;
85 torch::Tensor _skew_to_full_map;
86 torch::Tensor _full_to_skew_factor;
87 torch::Tensor _skew_to_full_factor;
88};
89
112 const torch::Tensor & rmap,
113 const torch::Tensor & rfactors,
114 TorchSize dim = 0);
115
128 const torch::Tensor & rmap,
129 const torch::Tensor & rfactors,
130 TorchSize dim = 0);
131
149
160
179
190
210BatchTensor jacrev(const BatchTensor & y, const BatchTensor & p);
211
214
216SR2 skew_and_sym_to_sym(const SR2 & e, const WR2 & w);
217
220
223
225WR2 multiply_and_make_skew(const SR2 & a, const SR2 & b);
226
229
232
233namespace linalg
234{
237
239BatchTensor solve(const BatchTensor & A, const BatchTensor & B);
240
241std::tuple<BatchTensor, BatchTensor> lu_factor(const BatchTensor & A, bool pivot = true);
242
244 const BatchTensor & pivots,
245 const BatchTensor & B,
246 bool left = true,
247 bool adjoint = false);
248} // namespace linalg
249} // namespace math
250} // namespace neml2
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The (logical) symmetric second order tensor.
Definition SR2.h:46
The (logical) symmetric fourth order tensor, with symmetry in the first two dimensionss as well as in...
Definition SSR4.h:44
The (logical) symmetric fourth order tensor, with symmetry in the first two dimensionss and skew-symm...
Definition SWR4.h:40
A skew rank 2, represented as an axial vector.
Definition WR2.h:43
The (logical) symmetric fourth order tensor, with skew symmetry in the first two dimensionss and symm...
Definition WSR4.h:40
BatchTensor solve(const BatchTensor &A, const BatchTensor &B)
Solve the linear system A X = B.
Definition math.cxx:331
std::tuple< BatchTensor, BatchTensor > lu_factor(const BatchTensor &A, bool pivot)
Definition math.cxx:337
BatchTensor vector_norm(const BatchTensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:314
BatchTensor lu_solve(const BatchTensor &LU, const BatchTensor &pivots, const BatchTensor &B, bool left, bool adjoint)
Definition math.cxx:344
constexpr Real skew_factor[3][3]
Definition math.h:47
constexpr Real eps
Definition math.h:38
SWR4 d_skew_and_sym_to_sym_d_skew(const SR2 &e)
Derivative of w_ik e_kj - e_ik w_kj wrt. w.
Definition math.cxx:276
SSR4 d_skew_and_sym_to_sym_d_sym(const WR2 &w)
Derivative of w_ik e_kj - e_ik w_kj wrt. e.
Definition math.cxx:267
BatchTensor mandel_to_full(const BatchTensor &mandel, TorchSize dim)
Convert a BatchTensor from Mandel notation to full notation.
Definition math.cxx:176
constexpr Real invsqrt2
Definition math.h:41
BatchTensor skew_to_full(const BatchTensor &skew, TorchSize dim)
Convert a BatchTensor from skew vector notation to full notation.
Definition math.cxx:196
BatchTensor base_diag_embed(const BatchTensor &a, TorchSize offset, TorchSize d1, TorchSize d2)
Definition math.cxx:248
BatchTensor reduced_to_full(const BatchTensor &reduced, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim)
Convert a BatchTensor from reduced notation to full notation.
Definition math.cxx:141
constexpr TorchSize mandel_index[6][2]
Definition math.h:44
WSR4 d_multiply_and_make_skew_d_first(const SR2 &b)
Derivative of a_ik b_kj - b_ik a_kj wrt a.
Definition math.cxx:294
SR2 skew_and_sym_to_sym(const SR2 &e, const WR2 &w)
Product w_ik e_kj - e_ik w_kj with e SR2 and w WR2.
Definition math.cxx:257
WR2 multiply_and_make_skew(const SR2 &a, const SR2 &b)
Shortcut product a_ik b_kj - b_ik a_kj with both SR2.
Definition math.cxx:285
BatchTensor full_to_reduced(const BatchTensor &full, const torch::Tensor &rmap, const torch::Tensor &rfactors, TorchSize dim)
Generic function to reduce two axes to one with some map.
Definition math.cxx:113
constexpr Real mandel_factor(TorchSize i)
Definition math.h:50
constexpr TorchSize skew_reverse_index[3][3]
Definition math.h:46
constexpr TorchSize mandel_reverse_index[3][3]
Definition math.h:43
WSR4 d_multiply_and_make_skew_d_second(const SR2 &a)
Derivative of a_ik b_kj - b_ik a_kj wrt b.
Definition math.cxx:303
BatchTensor full_to_mandel(const BatchTensor &full, TorchSize dim)
Convert a BatchTensor from full notation to Mandel notation.
Definition math.cxx:166
BatchTensor jacrev(const BatchTensor &y, const BatchTensor &p)
Use automatic differentiation (AD) to calculate the derivatives w.r.t. to the parameter.
Definition math.cxx:206
constexpr Real sqrt2
Definition math.h:40
BatchTensor full_to_skew(const BatchTensor &full, TorchSize dim)
Convert a BatchTensor from full notation to skew vector notation.
Definition math.cxx:186
Definition CrossRef.cxx:32
int64_t TorchSize
Definition types.h:33
double Real
Definition types.h:31
A helper class to hold static data of type torch::Tensor.
Definition math.h:64
static const torch::Tensor & skew_to_full_map()
Definition math.cxx:95
static const torch::Tensor & full_to_mandel_factor()
Definition math.cxx:77
static const torch::Tensor & full_to_skew_factor()
Definition math.cxx:101
static const torch::Tensor & full_to_skew_map()
Definition math.cxx:89
ConstantTensors()
Definition math.cxx:33
static ConstantTensors & get()
Definition math.cxx:58
static const torch::Tensor & mandel_to_full_factor()
Definition math.cxx:83
static const torch::Tensor & mandel_to_full_map()
Definition math.cxx:71
static const torch::Tensor & full_to_mandel_map()
Definition math.cxx:65
static const torch::Tensor & skew_to_full_factor()
Definition math.cxx:107