NEML2 1.4.0
Loading...
Searching...
No Matches
math.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/Tensor.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 Size mandel_reverse_index[3][3] = {{0, 5, 4}, {5, 1, 3}, {4, 3, 2}};
44constexpr Size mandel_index[6][2] = {{0, 0}, {1, 1}, {2, 2}, {1, 2}, {0, 2}, {0, 1}};
45
46constexpr Size 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
111Tensor full_to_reduced(const Tensor & full,
112 const torch::Tensor & rmap,
113 const torch::Tensor & rfactors,
114 Size dim = 0);
115
128 const torch::Tensor & rmap,
129 const torch::Tensor & rfactors,
130 Size dim = 0);
131
148Tensor full_to_mandel(const Tensor & full, Size dim = 0);
149
159Tensor mandel_to_full(const Tensor & mandel, Size dim = 0);
160
178Tensor full_to_skew(const Tensor & full, Size dim = 0);
179
189Tensor skew_to_full(const Tensor & skew, Size dim = 0);
190
210Tensor jacrev(const Tensor & y, const Tensor & p);
211
212Tensor base_diag_embed(const Tensor & a, Size offset = 0, Size d1 = -2, Size d2 = -1);
213
215SR2 skew_and_sym_to_sym(const SR2 & e, const WR2 & w);
216
219
222
224WR2 multiply_and_make_skew(const SR2 & a, const SR2 & b);
225
228
231
232template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
233T
234batch_cat(const std::vector<T> & tensors, Size d = 0)
235{
236 neml_assert_dbg(!tensors.empty(), "batch_cat must be given at least one tensor");
237 std::vector<torch::Tensor> torch_tensors(tensors.begin(), tensors.end());
238 auto d2 = d >= 0 ? d : d - tensors.begin()->base_dim();
239 return T(torch::cat(torch_tensors, d2), tensors.begin()->batch_dim());
240}
241
242template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
244base_cat(const std::vector<T> & tensors, Size d = 0)
245{
246 neml_assert_dbg(!tensors.empty(), "base_cat must be given at least one tensor");
247 std::vector<torch::Tensor> torch_tensors(tensors.begin(), tensors.end());
248 auto d2 = d < 0 ? d : d + tensors.begin()->batch_dim();
249 return neml2::Tensor(torch::cat(torch_tensors, d2), tensors.begin()->batch_dim());
250}
251
252template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
253T
254batch_stack(const std::vector<T> & tensors, Size d = 0)
255{
256 neml_assert_dbg(!tensors.empty(), "batch_stack must be given at least one tensor");
257 std::vector<torch::Tensor> torch_tensors(tensors.begin(), tensors.end());
258 auto d2 = d >= 0 ? d : d - tensors.begin()->base_dim();
259 return T(torch::stack(torch_tensors, d2), tensors.begin()->batch_dim() + 1);
260}
261
262template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
264base_stack(const std::vector<T> & tensors, Size d = 0)
265{
266 neml_assert_dbg(!tensors.empty(), "base_stack must be given at least one tensor");
267 std::vector<torch::Tensor> torch_tensors(tensors.begin(), tensors.end());
268 auto d2 = d < 0 ? d : d + tensors.begin()->batch_dim();
269 return neml2::Tensor(torch::stack(torch_tensors, d2), tensors.begin()->batch_dim());
270}
271
272template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
273T
274batch_sum(const T & a, Size d = 0)
275{
276 neml_assert_dbg(a.batch_dim() > 0, "Must have a batch dimension to sum along");
277 auto d2 = d >= 0 ? d : d - a.base_dim();
278 return T(torch::sum(a, d2), a.batch_dim() - 1);
279}
280
281template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
282T
283base_sum(const T & a, Size d = 0)
284{
285 auto d2 = d < 0 ? d : d + a.batch_dim();
286 return T(torch::sum(a, d2), a.batch_dim());
287}
288
289template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
290T
291pow(const T & a, const Real & n)
292{
293 return T(torch::pow(a, n), a.batch_dim());
294}
295
296Tensor pow(const Real & a, const Tensor & n);
297
298Tensor pow(const Tensor & a, const Tensor & n);
299
300template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
301T
302sign(const T & a)
303{
304 return T(torch::sign(a), a.batch_dim());
305}
306
307template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
308T
309cosh(const T & a)
310{
311 return T(torch::cosh(a), a.batch_dim());
312}
313
314template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
315T
316sinh(const T & a)
317{
318 return T(torch::sinh(a), a.batch_dim());
319}
320
321template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
322T
323tanh(const T & a)
324{
325 return T(torch::tanh(a), a.batch_dim());
326}
327
328template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
329T
330where(const torch::Tensor & condition, const T & a, const T & b)
331{
333 return T(torch::where(condition, a, b), broadcast_batch_dim(a, b));
334}
335
342template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
343T
344heaviside(const T & a)
345{
346 return (sign(a) + 1.0) / 2.0;
347}
348
349template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
350T
351macaulay(const T & a)
352{
353 return T(torch::Tensor(a) * torch::Tensor(heaviside(a)), a.batch_dim());
354}
355
356template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
357T
358dmacaulay(const T & a)
359{
360 return heaviside(a);
361}
362
363template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
364T
365sqrt(const T & a)
366{
367 return T(torch::sqrt(a), a.batch_dim());
368}
369
370template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
371T
372exp(const T & a)
373{
374 return T(torch::exp(a), a.batch_dim());
375}
376
377template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
378T
379abs(const T & a)
380{
381 return T(torch::abs(a), a.batch_dim());
382}
383
384template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
385T
386diff(const T & a, Size n = 1, Size dim = -1)
387{
388 return T(torch::diff(a, n, dim), a.batch_dim());
389}
390
391template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
392T
393batch_diag_embed(const T & a, Size offset = 0, Size d1 = -2, Size d2 = -1)
394{
395 return T(torch::diag_embed(
396 a, offset, d1 < 0 ? d1 - a.base_dim() : d1, d2 < 0 ? d2 - a.base_dim() : d2),
397 a.batch_dim() + 1);
398}
399
400template <class T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
401T
402log(const T & a)
403{
404 return T(torch::log(a), a.batch_dim());
405}
406
407namespace linalg
408{
410Tensor vector_norm(const Tensor & v);
411
413Tensor inv(const Tensor & m);
414
416Tensor solve(const Tensor & A, const Tensor & B);
417
418std::tuple<Tensor, Tensor> lu_factor(const Tensor & A, bool pivot = true);
419
420Tensor lu_solve(const Tensor & LU,
421 const Tensor & pivots,
422 const Tensor & B,
423 bool left = true,
424 bool adjoint = false);
425} // namespace linalg
426} // namespace math
427} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
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
Definition Tensor.h:32
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
Tensor solve(const Tensor &A, const Tensor &B)
Solve the linear system A X = B.
Definition math.cxx:347
Tensor vector_norm(const Tensor &v)
Vector norm of a vector. Falls back to math::abs is v is a Scalar.
Definition math.cxx:324
Tensor lu_solve(const Tensor &LU, const Tensor &pivots, const Tensor &B, bool left, bool adjoint)
Definition math.cxx:360
Tensor inv(const Tensor &m)
Inverse of a square matrix.
Definition math.cxx:341
std::tuple< Tensor, Tensor > lu_factor(const Tensor &A, bool pivot)
Definition math.cxx:353
Tensor full_to_reduced(const Tensor &full, const torch::Tensor &rmap, const torch::Tensor &rfactors, Size dim)
Generic function to reduce two axes to one with some map.
Definition math.cxx:113
T dmacaulay(const T &a)
Definition math.h:358
Tensor reduced_to_full(const Tensor &reduced, const torch::Tensor &rmap, const torch::Tensor &rfactors, Size dim)
Convert a Tensor from reduced notation to full notation.
Definition math.cxx:139
constexpr Real skew_factor[3][3]
Definition math.h:47
T batch_sum(const T &a, Size d=0)
Definition math.h:274
constexpr Size mandel_index[6][2]
Definition math.h:44
T cosh(const T &a)
Definition math.h:309
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:273
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:264
constexpr Size skew_reverse_index[3][3]
Definition math.h:46
T heaviside(const T &a)
Definition math.h:344
Tensor full_to_skew(const Tensor &full, Size dim)
Convert a Tensor from full notation to skew vector notation.
Definition math.cxx:182
constexpr Real invsqrt2
Definition math.h:41
Tensor mandel_to_full(const Tensor &mandel, Size dim)
Convert a Tensor from Mandel notation to full notation.
Definition math.cxx:172
Tensor base_diag_embed(const Tensor &a, Size offset, Size d1, Size d2)
Definition math.cxx:245
T exp(const T &a)
Definition math.h:372
neml2::Tensor base_cat(const std::vector< T > &tensors, Size d=0)
Definition math.h:244
T log(const T &a)
Definition math.h:402
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:291
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:254
T sinh(const T &a)
Definition math.h:316
T tanh(const T &a)
Definition math.h:323
constexpr Size mandel_reverse_index[3][3]
Definition math.h:43
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:282
T batch_stack(const std::vector< T > &tensors, Size d=0)
Definition math.h:254
T sqrt(const T &a)
Definition math.h:365
constexpr Real mandel_factor(Size i)
Definition math.h:50
neml2::Tensor base_stack(const std::vector< T > &tensors, Size d=0)
Definition math.h:264
T diff(const T &a, Size n=1, Size dim=-1)
Definition math.h:386
T batch_diag_embed(const T &a, Size offset=0, Size d1=-2, Size d2=-1)
Definition math.h:393
T abs(const T &a)
Definition math.h:379
T batch_cat(const std::vector< T > &tensors, Size d=0)
Definition math.h:234
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:300
T base_sum(const T &a, Size d=0)
Definition math.h:283
Tensor pow(const Real &a, const Tensor &n)
Definition math.cxx:309
Tensor skew_to_full(const Tensor &skew, Size dim)
Convert a Tensor from skew vector notation to full notation.
Definition math.cxx:192
T where(const torch::Tensor &condition, const T &a, const T &b)
Definition math.h:330
Tensor full_to_mandel(const Tensor &full, Size dim)
Convert a Tensor from full notation to Mandel notation.
Definition math.cxx:162
T macaulay(const T &a)
Definition math.h:351
T sign(const T &a)
Definition math.h:302
constexpr Real sqrt2
Definition math.h:40
Tensor jacrev(const Tensor &y, const Tensor &p)
Use automatic differentiation (AD) to calculate the derivatives w.r.t. to the parameter.
Definition math.cxx:202
Definition CrossRef.cxx:30
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:76
Size broadcast_batch_dim(T &&...)
The batch dimension after broadcasting.
double Real
Definition types.h:31
int64_t Size
Definition types.h:33
void neml_assert_broadcastable_dbg(T &&...)
A helper function to assert (in Debug mode) that all tensors are broadcastable.
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:36
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