NEML2 1.4.0
Loading...
Searching...
No Matches
WR2.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/tensors/WR2.h"
26#include "neml2/tensors/Scalar.h"
27#include "neml2/tensors/R2.h"
28#include "neml2/tensors/Rot.h"
29#include "neml2/tensors/SR2.h"
30#include "neml2/tensors/WSR4.h"
31#include "neml2/tensors/R4.h"
32
33#include "neml2/misc/math.h"
34
35namespace neml2
36{
37WR2::WR2(const R2 & T)
38 : WR2(math::full_to_skew((T - T.transpose()) / 2.0))
39{
40}
41
48
49Rot
50WR2::exp() const
51{
52 // There are singularities at norm() = 0 and 2*pi
53 // To the third order near zero this reduces to
54 // *this * (1/4 + 5 * norm^4 / 96)
55 // We use this formula for small rotations
56
57 // The other singularity is essentially unavoidable
58
59 // This is what determines which region to sit in
60 auto norm2 = this->norm_sq();
61
62 // So we want the result to be as accurate as machine precision
63 auto thresh = std::pow(math::eps, 1.0 / 3.0);
64
65 // Taylor series
66 Rot res_taylor = (*this) * (0.25 + 5.0 * norm2 * norm2 / 96.0);
67
68 // Actual definition
69 Rot res_actual = (*this) * Scalar(torch::tan(norm2 / 2.0) /
70 (2.0 * torch::Tensor(norm2) * torch::cos(norm2 / 2)));
71
72 return torch::where((norm2 > thresh).unsqueeze(-1), res_actual, res_taylor);
73}
74
75R2
76WR2::dexp() const
77{
78 // Same singularities as WR2::exp()
79 auto norm2 = this->norm_sq();
80 auto thresh = std::pow(math::eps, 1.0 / 3.0);
81
82 R2 res_taylor = 5.0 * norm2 / 24.0 * this->outer(*this) +
83 (0.25 + 5.0 * norm2 * norm2 / 96.0) * R2::identity(options());
84
85 auto f1 = Scalar(torch::tan(norm2 / 2.0) / (2.0 * torch::Tensor(norm2) * torch::cos(norm2 / 2)));
86 auto f2 =
87 Scalar(torch::Tensor(norm2) * torch::pow(1.0 / torch::cos(norm2 / 2), 3.0) +
88 torch::tan(norm2 / 2.0) * (torch::Tensor(norm2) * torch::tan(norm2 / 2.0) - 2.0) *
89 (1.0 / torch::cos(norm2 / 2.0))) /
90 Scalar(2 * torch::pow(norm2, 2.0));
91
92 R2 res_actual = f1 * R2::identity(options()) + f2 * this->outer(*this);
93
94 return torch::where((norm2 > thresh).unsqueeze(-1).unsqueeze(-1), res_actual, res_taylor);
95}
96
97} // namespace neml2
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
static R2 identity(const torch::TensorOptions &options=default_tensor_options())
Identity.
Definition R2Base.cxx:170
A basic R2.
Definition R2.h:42
Rotation stored as modified Rodrigues parameters.
Definition Rot.h:49
The (logical) scalar.
Definition Scalar.h:38
R2 outer(const VecBase< Derived2 > &v) const
outer product
Definition VecBase.h:119
Scalar norm_sq() const
Norm squared.
Definition VecBase.cxx:64
A skew rank 2, represented as an axial vector.
Definition WR2.h:43
R2 dexp() const
Derivative of the exponential map.
Definition WR2.cxx:76
WR2(const R2 &T)
Skew-symmetrize a R2 then fill.
Definition WR2.cxx:37
Scalar operator()(TorchSize i, TorchSize j) const
Accessor.
Definition WR2.cxx:43
Rot exp() const
Exponential map to make this into a rotation (Rot)
Definition WR2.cxx:50
constexpr Real skew_factor[3][3]
Definition math.h:47
constexpr Real eps
Definition math.h:38
constexpr TorchSize skew_reverse_index[3][3]
Definition math.h:46
Definition CrossRef.cxx:32
int64_t TorchSize
Definition types.h:35