Line data Source code
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 : #include "neml2/tensors/WR2.h"
26 : #include "neml2/tensors/Scalar.h"
27 : #include "neml2/tensors/Vec.h"
28 : #include "neml2/tensors/R2.h"
29 : #include "neml2/tensors/Rot.h"
30 : #include "neml2/tensors/SR2.h"
31 : #include "neml2/tensors/WSR4.h"
32 : #include "neml2/tensors/R4.h"
33 :
34 : #include "neml2/tensors/mandel_notation.h"
35 : #include "neml2/tensors/functions/tan.h"
36 : #include "neml2/tensors/functions/cos.h"
37 : #include "neml2/tensors/functions/pow.h"
38 :
39 : namespace neml2
40 : {
41 40 : WR2::WR2(const R2 & T)
42 40 : : WR2(full_to_skew((T - T.transpose()) / 2.0))
43 : {
44 40 : }
45 :
46 : Scalar
47 9 : WR2::operator()(Size i, Size j) const
48 : {
49 9 : Size a = skew_reverse_index[i][j];
50 27 : return base_index({a}) * skew_factor[i][j];
51 9 : }
52 :
53 : Rot
54 22 : WR2::exp() const
55 : {
56 : // There are singularities at norm() = 0 and 2*pi
57 : // To the third order near zero this reduces to
58 : // *this * (1/4 + 5 * norm^4 / 96)
59 : // We use this formula for small rotations
60 :
61 : // The other singularity is essentially unavoidable
62 :
63 : // This is what determines which region to sit in
64 22 : auto norm2 = norm_sq();
65 :
66 : // So we want the result to be as accurate as machine precision
67 22 : auto thresh = std::pow(eps, 1.0 / 3.0);
68 :
69 : // Taylor series
70 22 : auto res_taylor = Rot(*this) * (0.25 + 5.0 * norm2 * norm2 / 96.0);
71 :
72 : // Actual definition
73 22 : auto res_actual = Rot(*this) * neml2::tan(norm2 / 2.0) / (2.0 * norm2 * neml2::cos(norm2 / 2));
74 :
75 44 : return Rot(at::where((norm2 > thresh).unsqueeze(-1), res_actual, res_taylor));
76 22 : }
77 :
78 : R2
79 9 : WR2::dexp() const
80 : {
81 : // Same singularities as WR2::exp()
82 9 : auto norm2 = norm_sq();
83 9 : auto thresh = std::pow(eps, 1.0 / 3.0);
84 :
85 18 : auto res_taylor = 5.0 * norm2 / 24.0 * Vec(*this).outer(Vec(*this)) +
86 27 : (0.25 + 5.0 * norm2 * norm2 / 96.0) * R2::identity(options());
87 :
88 9 : auto f1 = neml2::tan(norm2 / 2.0) / (2.0 * norm2 * neml2::cos(norm2 / 2));
89 18 : auto f2 = (norm2 * neml2::pow(1.0 / neml2::cos(norm2 / 2), 3.0) +
90 18 : neml2::tan(norm2 / 2.0) * (norm2 * neml2::tan(norm2 / 2.0) - 2.0) *
91 18 : (1.0 / neml2::cos(norm2 / 2.0))) /
92 27 : (2 * norm2 * norm2);
93 :
94 9 : auto res_actual = f1 * R2::identity(options()) + f2 * Vec(*this).outer(Vec(*this));
95 :
96 18 : return R2(at::where((norm2 > thresh).unsqueeze(-1).unsqueeze(-1), res_actual, res_taylor));
97 9 : }
98 :
99 : } // namespace neml2
|