NEML2 1.4.0
Loading...
Searching...
No Matches
Rot.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/Rot.h"
26#include "neml2/tensors/Scalar.h"
27#include "neml2/tensors/Vec.h"
28#include "neml2/tensors/R2.h"
29#include "neml2/tensors/SR2.h"
30#include "neml2/tensors/R3.h"
31#include "neml2/tensors/R4.h"
32#include "neml2/tensors/SSR4.h"
33#include "neml2/tensors/WR2.h"
34
35namespace neml2
36{
37Rot::Rot(const Vec & v)
38 : Rot(BatchTensor(v))
39{
40}
41
42Rot
43Rot::identity(const torch::TensorOptions & options)
44{
45 return Rot::zeros(options);
46}
47
48Rot
50{
51 return -(*this);
52}
53
54R2
56{
57 auto rr = norm_sq();
58 auto E = R3::levi_civita(options());
59 auto W = R2::skew(*this);
60
61 return 1.0 / math::pow(1 + rr, 2.0) *
62 (math::pow(1 + rr, 2.0) * R2::identity(options()) + 4 * (1.0 - rr) * W + 8.0 * W * W);
63}
64
65R3
67{
68 auto rr = norm_sq();
69 auto I = R2::identity(options());
70 auto E = R3::levi_civita(options());
71 auto W = R2::skew(*this);
72
73 return 8.0 * (rr - 3.0) / math::pow(1.0 + rr, 3.0) * R3(torch::einsum("...ij,...k", {W, *this})) -
74 32.0 / math::pow(1 + rr, 3.0) * R3(torch::einsum("...ij,...k", {(W * W), *this})) -
75 4.0 * (1 - rr) / math::pow(1.0 + rr, 2.0) * R3(torch::einsum("...kij->...ijk", {E})) -
76 8.0 / math::pow(1.0 + rr, 2.0) *
77 R3(torch::einsum("...kim,...mj", {E, W}) + torch::einsum("...im,...kmj", {W, E}));
78}
79
80Rot
81Rot::rotate(const Rot & r) const
82{
83 return r * (*this);
84}
85
86R2
87Rot::drotate(const Rot & r) const
88{
89 auto r1 = *this;
90 auto r2 = r;
91
92 auto rr1 = r1.norm_sq();
93 auto rr2 = r2.norm_sq();
94 auto d = 1.0 + rr1 * rr2 - 2 * r1.dot(r2);
95 auto r3 = this->rotate(r);
96 auto I = R2::identity(options());
97
98 return 1.0 / d *
99 (-r3.outer(2 * rr1 * r2 - 2.0 * r1) - 2 * r1.outer(r2) + (1 - rr1) * I - 2 * R2::skew(r1));
100}
101
102R2
103Rot::drotate_self(const Rot & r) const
104{
105 auto r1 = r;
106 auto r2 = *this;
107
108 auto rr1 = r1.norm_sq();
109 auto rr2 = r2.norm_sq();
110 auto d = 1.0 + rr1 * rr2 - 2 * r1.dot(r2);
111 auto r3 = this->rotate(r);
112 auto I = R2::identity(options());
113
114 return 1.0 / d *
115 (-r3.outer(2 * rr1 * r2 - 2.0 * r1) - 2 * r1.outer(r2) + (1 - rr1) * I + 2 * R2::skew(r1));
116}
117
118Rot
120{
121 return -*this / this->norm_sq();
122}
123
124R2
126{
127 auto ns = this->norm_sq();
128
129 return (2.0 / ns * this->outer(*this) - R2::identity(options())) / ns;
130}
131
132Rot
133operator*(const Rot & r1, const Rot & r2)
134{
135 auto rr1 = r1.norm_sq();
136 auto rr2 = r2.norm_sq();
137
138 return ((1 - rr2) * r1 + (1.0 - rr1) * r2 - 2.0 * r2.cross(r1)) /
139 (1.0 + rr1 * rr2 - 2 * r1.dot(r2));
140}
141
142} // namemspace neml2
Definition BatchTensor.h:32
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
static Derived zeros(const torch::TensorOptions &options=default_tensor_options())
Unbatched zero tensor.
Definition FixedDimTensor.h:145
static R2 identity(const torch::TensorOptions &options=default_tensor_options())
Identity.
Definition R2Base.cxx:170
static R2 skew(const Vec &v)
Skew matrix from Vec.
Definition R2Base.cxx:158
A basic R2.
Definition R2.h:42
The (logical) full third order tensor.
Definition R3.h:41
static R3 levi_civita(const torch::TensorOptions &options=default_tensor_options())
Alternating symbol.
Definition R3.cxx:33
Rotation stored as modified Rodrigues parameters.
Definition Rot.h:49
Rot rotate(const Rot &r) const
Rotate.
Definition Rot.cxx:81
R3 deuler_rodrigues() const
d(R2)/d(r) – useful in constructing other derivatives
Definition Rot.cxx:66
Rot shadow() const
Return the shadow parameter set (a set of MRPs that define the same orientation)
Definition Rot.cxx:119
R2 dshadow() const
Return the derivative of the shadow map.
Definition Rot.cxx:125
static Rot identity(const torch::TensorOptions &options=default_tensor_options())
The identity rotation, helpfully the zero vector.
Definition Rot.cxx:43
R2 euler_rodrigues() const
Generate a rotation matrix using the Euler-Rodrigues formula.
Definition Rot.cxx:55
R2 drotate_self(const Rot &r) const
Derivative of the rotated Rodrigues vector w.r.t. this vector.
Definition Rot.cxx:103
Rot inverse() const
Inversion.
Definition Rot.cxx:49
Rot(const Vec &v)
Definition Rot.cxx:37
R2 drotate(const Rot &r) const
Derivative of the rotated Rodrigues vector w.r.t. the other Rodrigues vector.
Definition Rot.cxx:87
R2 outer(const VecBase< Derived2 > &v) const
outer product
Definition VecBase.h:119
Scalar norm_sq() const
Norm squared.
Definition VecBase.cxx:64
The (logical) vector.
Definition Vec.h:42
Derived pow(const Derived &a, const Real &n)
Definition BatchTensorBase.h:332
Definition CrossRef.cxx:32
BatchTensor operator*(const BatchTensor &a, const BatchTensor &b)
Definition BatchTensor.cxx:153