/home/runner/work/amr-wind/amr-wind/amr-wind/core/vs/tensorI.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/core/vs/tensorI.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
tensorI.H
Go to the documentation of this file.
1#ifndef VS_TENSORI_H
2#define VS_TENSORI_H
3
4#include <cmath>
7
8namespace amr_wind::vs {
9
10template <typename T>
11AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT<T>::TensorT(
12 const VectorT<T>& x,
13 const VectorT<T>& y,
14 const VectorT<T>& z,
15 const bool transpose)
16{
17 if (transpose) {
18 this->cols(x, y, z);
19 } else {
20 this->rows(x, y, z);
21 }
22}
23
24template <typename T>
25AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void TensorT<T>::rows(
26 const VectorT<T>& x, const VectorT<T>& y, const VectorT<T>& z) noexcept
27{
28 // clang-format off
29 vv[0] = x.x(); vv[1] = x.y(); vv[2] = x.z();
30 vv[3] = y.x(); vv[4] = y.y(); vv[5] = y.z();
31 vv[6] = z.x(); vv[7] = z.y(); vv[8] = z.z();
32 // clang-format on
33}
34
35template <typename T>
36AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void TensorT<T>::cols(
37 const VectorT<T>& x, const VectorT<T>& y, const VectorT<T>& z) noexcept
38{
39 // clang-format off
40 vv[0] = x.x(); vv[1] = y.x(); vv[2] = z.x();
41 vv[3] = x.y(); vv[4] = y.y(); vv[5] = z.y();
42 vv[6] = x.z(); vv[7] = y.z(); vv[8] = z.z();
43 // clang-format on
44}
45
46// clang-format off
47template <typename T>
48AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
49VectorT<T> TensorT<T>::x() const noexcept
50{
51 return VectorT<T>{vv[0], vv[1], vv[2]};
52}
53
54template <typename T>
55AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
56VectorT<T> TensorT<T>::y() const noexcept
57{
58 return VectorT<T>{vv[3], vv[4], vv[5]};
59}
60
61template <typename T>
62AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
63VectorT<T> TensorT<T>::z() const noexcept
64{
65 return VectorT<T>{vv[6], vv[7], vv[8]};
66}
67
68template <typename T>
69AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
70VectorT<T> TensorT<T>::cx() const noexcept
71{
72 return VectorT<T>{vv[0], vv[3], vv[6]};
73}
74
75template <typename T>
76AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
77VectorT<T> TensorT<T>::cy() const noexcept
78{
79 return VectorT<T>{vv[1], vv[4], vv[7]};
80}
81
82template <typename T>
83AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
84VectorT<T> TensorT<T>::cz() const noexcept
85{
86 return VectorT<T>{vv[2], vv[5], vv[8]};
87}
88// clang-format on
89
90template <typename T, typename OStream>
91OStream& operator<<(OStream& out, const TensorT<T>& t)
92{
93 out << "(";
94 for (int i = 0; i < t.ncomp; ++i) {
95 out << " " << t[i];
96 }
97 out << " )";
98 return out;
99}
100
101template <typename T>
102AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT<T>
103operator&(const TensorT<T>& t, const VectorT<T>& v)
104{
105 VectorT<T> out;
106 out.x() = t.xx() * v.x() + t.xy() * v.y() + t.xz() * v.z();
107 out.y() = t.yx() * v.x() + t.yy() * v.y() + t.yz() * v.z();
108 out.z() = t.zx() * v.x() + t.zy() * v.y() + t.zz() * v.z();
109 return out;
110}
111
112template <typename T>
113AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT<T>
114operator&(const VectorT<T>& v, const TensorT<T>& t)
115{
116 VectorT<T> out;
117 out.x() = t.xx() * v.x() + t.yx() * v.y() + t.zx() * v.z();
118 out.y() = t.xy() * v.x() + t.yy() * v.y() + t.zy() * v.z();
119 out.z() = t.xz() * v.x() + t.yz() * v.y() + t.zz() * v.z();
120 return out;
121}
122
123template <typename T>
124AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT<T>
125operator&(const TensorT<T>& t1, const TensorT<T>& t2)
126{
127 TensorT<T> t3;
128
129 t3.xx() = t1.xx() * t2.xx() + t1.xy() * t2.yx() + t1.xz() * t2.zx();
130 t3.xy() = t1.xx() * t2.xy() + t1.xy() * t2.yy() + t1.xz() * t2.zy();
131 t3.xz() = t1.xx() * t2.xz() + t1.xy() * t2.yz() + t1.xz() * t2.zz();
132
133 t3.yx() = t1.yx() * t2.xx() + t1.yy() * t2.yx() + t1.yz() * t2.zx();
134 t3.yy() = t1.yx() * t2.xy() + t1.yy() * t2.yy() + t1.yz() * t2.zy();
135 t3.yz() = t1.yx() * t2.xz() + t1.yy() * t2.yz() + t1.yz() * t2.zz();
136
137 t3.zx() = t1.zx() * t2.xx() + t1.zy() * t2.yx() + t1.zz() * t2.zx();
138 t3.zy() = t1.zx() * t2.xy() + t1.zy() * t2.yy() + t1.zz() * t2.zy();
139 t3.zz() = t1.zx() * t2.xz() + t1.zy() * t2.yz() + t1.zz() * t2.zz();
140
141 return t3;
142}
143
144template <typename T>
145AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT<T>
146operator+(const TensorT<T>& t1, const TensorT<T>& t2)
147{
148 return TensorT<T>{
149 t1.vv[0] + t2.vv[0], t1.vv[1] + t2.vv[1], t1.vv[2] + t2.vv[2],
150 t1.vv[3] + t2.vv[3], t1.vv[4] + t2.vv[4], t1.vv[5] + t2.vv[5],
151 t1.vv[6] + t2.vv[6], t1.vv[7] + t2.vv[7], t1.vv[8] + t2.vv[8]};
152}
153
154template <typename T>
155AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT<T>
156operator-(const TensorT<T>& t1, const TensorT<T>& t2)
157{
158 return TensorT<T>{
159 t1.vv[0] - t2.vv[0], t1.vv[1] - t2.vv[1], t1.vv[2] - t2.vv[2],
160 t1.vv[3] - t2.vv[3], t1.vv[4] - t2.vv[4], t1.vv[5] - t2.vv[5],
161 t1.vv[6] - t2.vv[6], t1.vv[7] - t2.vv[7], t1.vv[8] - t2.vv[8]};
162}
163
164template <typename T>
165AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T
166operator&&(const TensorT<T>& t1, const TensorT<T>& t2)
167{
168 return (
169 t1.vv[0] * t2.vv[0] + t1.vv[1] * t2.vv[1] + t1.vv[2] * t2.vv[2] +
170 t1.vv[3] * t2.vv[3] + t1.vv[4] * t2.vv[4] + t1.vv[5] * t2.vv[5] +
171 t1.vv[6] * t2.vv[6] + t1.vv[7] * t2.vv[7] + t1.vv[8] * t2.vv[8]);
172}
173
174template <typename T>
175AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T mag_sqr(const TensorT<T>& t)
176{
177 // cppcheck-suppress duplicateExpression
178 return (t && t);
179}
180
181template <typename T>
182AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T mag(const TensorT<T>& t)
183{
184 return std::sqrt(mag_sqr(t));
185}
186
187AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor xrot(const amrex::Real angle)
188{
189 const amrex::Real ang = utils::radians(angle);
190 const amrex::Real cval = std::cos(ang);
191 const amrex::Real sval = std::sin(ang);
192
193 return Tensor{1.0, 0.0, 0.0, 0.0, cval, sval, 0.0, -sval, cval};
194}
195
196AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor yrot(const amrex::Real angle)
197{
198 const amrex::Real ang = utils::radians(angle);
199 const amrex::Real cval = std::cos(ang);
200 const amrex::Real sval = std::sin(ang);
201
202 return Tensor{cval, 0.0, -sval, 0.0, 1.0, 0.0, sval, 0.0, cval};
203}
204
205AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor zrot(const amrex::Real angle)
206{
207 const amrex::Real ang = utils::radians(angle);
208 const amrex::Real cval = std::cos(ang);
209 const amrex::Real sval = std::sin(ang);
210
211 return Tensor{cval, sval, 0.0, -sval, cval, 0.0, 0.0, 0.0, 1.0};
212}
213
214AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor
215quaternion(const Vector& axis, const amrex::Real angle)
216{
217 const amrex::Real ang = -1.0 * utils::radians(angle);
218 const amrex::Real cval = std::cos(0.5 * ang);
219 const amrex::Real sval = std::sin(0.5 * ang);
220 const auto vmag = mag(axis);
221 const amrex::Real q0 = cval;
222 const amrex::Real q1 = sval * axis.x() / vmag;
223 const amrex::Real q2 = sval * axis.y() / vmag;
224 const amrex::Real q3 = sval * axis.z() / vmag;
225
226 Tensor t;
227 t.xx() = q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3;
228 t.xy() = 2.0 * (q1 * q2 - q0 * q3);
229 t.xz() = 2.0 * (q0 * q2 + q1 * q3);
230
231 t.yx() = 2.0 * (q1 * q2 + q0 * q3);
232 t.yy() = q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3;
233 t.yz() = 2.0 * (q2 * q3 - q0 * q1);
234
235 t.zx() = 2.0 * (q1 * q3 - q0 * q2);
236 t.zy() = 2.0 * (q0 * q1 + q2 * q3);
237 t.zz() = q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3;
238
239 return t;
240}
241
242} // namespace amr_wind::vs
243
244#endif /* VS_TENSORI_H */
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real radians(const amrex::Real deg_val)
Convert from degrees to radians.
Definition trig_ops.H:35
Definition tensor.H:8
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor quaternion(const Vector &axis, const amrex::Real angle)
Definition tensorI.H:215
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T mag(const TensorT< T > &t)
Definition tensorI.H:182
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > operator&(const TensorT< T > &t, const VectorT< T > &v)
Definition tensorI.H:103
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT< T > operator-(const TensorT< T > &t1, const TensorT< T > &t2)
Definition tensorI.H:156
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T operator&&(const TensorT< T > &t1, const TensorT< T > &t2)
Definition tensorI.H:166
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE TensorT< T > operator+(const TensorT< T > &t1, const TensorT< T > &t2)
Definition tensorI.H:146
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor zrot(const amrex::Real angle)
Definition tensorI.H:205
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor yrot(const amrex::Real angle)
Definition tensorI.H:196
OStream & operator<<(OStream &out, const TensorT< T > &t)
Definition tensorI.H:91
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T mag_sqr(const TensorT< T > &t)
Definition tensorI.H:175
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Tensor xrot(const amrex::Real angle)
Definition tensorI.H:187
TensorT< amrex::Real > Tensor
Definition tensor.H:189
Definition tensor.H:14
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & zy() &noexcept
Definition tensor.H:115
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > y() const noexcept
Definition tensorI.H:56
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & zz() &noexcept
Definition tensor.H:119
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & zx() &noexcept
Definition tensor.H:111
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & yy() &noexcept
Definition tensor.H:102
static constexpr int ncomp
Definition tensor.H:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE constexpr TensorT()=default
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > z() const noexcept
Definition tensorI.H:63
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > cz() const noexcept
Definition tensorI.H:84
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > x() const noexcept
Definition tensorI.H:49
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void cols(const VectorT< T > &x, const VectorT< T > &y, const VectorT< T > &z) noexcept
Definition tensorI.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & xx() &noexcept
Definition tensor.H:85
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > cy() const noexcept
Definition tensorI.H:77
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & yx() &noexcept
Definition tensor.H:98
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void rows(const VectorT< T > &x, const VectorT< T > &y, const VectorT< T > &z) noexcept
Definition tensorI.H:25
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & xz() &noexcept
Definition tensor.H:93
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE VectorT< T > cx() const noexcept
Definition tensorI.H:70
amrex::GpuArray< T, 9 > vv
Definition tensor.H:15
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & xy() &noexcept
Definition tensor.H:89
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & yz() &noexcept
Definition tensor.H:106
Definition vector.H:13
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & z() &noexcept
Definition vector.H:99
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & x() &noexcept
Definition vector.H:97
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T & y() &noexcept
Definition vector.H:98