1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341 | // *****************************************************************************
/*!
\file src/Base/Vector.hpp
\copyright 2012-2015 J. Bakosi,
2016-2018 Los Alamos National Security, LLC.,
2019-2021 Triad National Security, LLC.
All rights reserved. See the LICENSE file for details.
\brief Vector algebra
\details Vector algebra.
*/
// *****************************************************************************
#ifndef Vector_h
#define Vector_h
#include <array>
#include <cmath>
#include <vector>
#include "Types.hpp"
#include "Exception.hpp"
namespace tk {
//! Flip sign of vector components
//! \param[in] v Vector whose components to multiply by -1.0
inline void
flip( std::array< real, 3 >& v )
{
v[0] = -v[0];
v[1] = -v[1];
v[2] = -v[2];
}
//! Compute the cross-product of two vectors
//! \param[in] v1x x coordinate of the 1st vector
//! \param[in] v1y y coordinate of the 1st vector
//! \param[in] v1z z coordinate of the 1st vector
//! \param[in] v2x x coordinate of the 2nd vector
//! \param[in] v2y y coordinate of the 2nd vector
//! \param[in] v2z z coordinate of the 2nd vector
//! \param[out] rx x coordinate of the product vector
//! \param[out] ry y coordinate of the product vector
//! \param[out] rz z coordinate of the product vector
#pragma omp declare simd
inline void
cross( real v1x, real v1y, real v1z,
real v2x, real v2y, real v2z,
real& rx, real& ry, real& rz )
{
rx = v1y*v2z - v2y*v1z;
ry = v1z*v2x - v2z*v1x;
rz = v1x*v2y - v2x*v1y;
}
//! Compute the cross-product of two vectors
//! \param[in] v1 1st vector
//! \param[in] v2 2nd vector
//! \return Cross-product
inline std::array< real, 3 >
cross( const std::array< real, 3 >& v1, const std::array< real, 3 >& v2 )
{
real rx, ry, rz;
cross( v1[0], v1[1], v1[2], v2[0], v2[1], v2[2], rx, ry, rz );
return { std::move(rx), std::move(ry), std::move(rz) };
}
//! Compute the cross-product of two vectors divided by a scalar
//! \param[in] v1x x coordinate of the 1st vector
//! \param[in] v1y y coordinate of the 1st vector
//! \param[in] v1z z coordinate of the 1st vector
//! \param[in] v2x x coordinate of the 2nd vector
//! \param[in] v2y y coordinate of the 2nd vector
//! \param[in] v2z z coordinate of the 2nd vector
//! \param[in] j The scalar to divide the product with
//! \param[out] rx x coordinate of the product vector
//! \param[out] ry y coordinate of the product vector
//! \param[out] rz z coordinate of the product vector
#pragma omp declare simd uniform(j)
inline void
crossdiv( real v1x, real v1y, real v1z,
real v2x, real v2y, real v2z,
real j,
real& rx, real& ry, real& rz )
{
cross( v1x, v1y, v1z, v2x, v2y, v2z, rx, ry, rz );
rx /= j;
ry /= j;
rz /= j;
}
//! Compute the cross-product of two vectors divided by a scalar
//! \param[in] v1 1st vector
//! \param[in] v2 2nd vector
//! \param[in] j Scalar to divide each component by
//! \return Cross-product divided by scalar
inline std::array< real, 3 >
crossdiv( const std::array< real, 3 >& v1,
const std::array< real, 3 >& v2,
real j )
{
return {{ (v1[1]*v2[2] - v2[1]*v1[2]) / j,
(v1[2]*v2[0] - v2[2]*v1[0]) / j,
(v1[0]*v2[1] - v2[0]*v1[1]) / j }};
}
//! Compute the dot-product of two vectors
//! \param[in] v1 1st vector
//! \param[in] v2 2nd vector
//! \return Dot-product
inline real
dot( const std::array< real, 3 >& v1, const std::array< real, 3 >& v2 )
{
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
//! Compute length of a vector
//! \param[in] x X coordinate of vector
//! \param[in] y Y coordinate of vector
//! \param[in] z Z coordinate of vector
//! \return length
#pragma omp declare simd
inline real
length( real x, real y, real z )
{
return std::sqrt( x*x + y*y + z*z );
}
//! Compute length of a vector
//! \param[in] v vector
//! \return length
inline real
length( const std::array< real, 3 >& v )
{
return std::sqrt( dot(v,v) );
}
//! Scale vector to unit length
//! \param[in,out] v Vector to normalize
inline void
unit( std::array< real, 3 >& v ) noexcept(ndebug)
{
auto len = length( v );
Assert( len > std::numeric_limits< tk::real >::epsilon(), "div by zero" );<--- Exception thrown in function declared not to throw exceptions.
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
//! Compute the triple-product of three vectors
//! \param[in] v1x x coordinate of the 1st vector
//! \param[in] v1y y coordinate of the 1st vector
//! \param[in] v1z z coordinate of the 1st vector
//! \param[in] v2x x coordinate of the 2nd vector
//! \param[in] v2y y coordinate of the 2nd vector
//! \param[in] v2z z coordinate of the 2nd vector
//! \param[in] v3x x coordinate of the 3rd vector
//! \param[in] v3y y coordinate of the 3rd vector
//! \param[in] v3z z coordinate of the 3rd vector
//! \return Scalar value of the triple product
#pragma omp declare simd
inline tk::real
triple( real v1x, real v1y, real v1z,
real v2x, real v2y, real v2z,
real v3x, real v3y, real v3z )
{
real cx, cy, cz;
cross( v2x, v2y, v2z, v3x, v3y, v3z, cx, cy, cz );
return v1x*cx + v1y*cy + v1z*cz;
}
//! Compute the triple-product of three vectors
//! \param[in] v1 1st vector
//! \param[in] v2 2nd vector
//! \param[in] v3 3rd vector
//! \return Triple-product
inline real
triple( const std::array< real, 3 >& v1,
const std::array< real, 3 >& v2,
const std::array< real, 3 >& v3 )
{
return dot( v1, cross(v2,v3) );
}
//! Rotate vector about X axis
//! \param[in] v Vector to rotate
//! \param[in] angle Angle to use to rotate with
//! \return Rotated vector
inline std::array< real, 3 >
rotateX( const std::array< real, 3 >& v, real angle )
{
using std::cos; using std::sin;
std::array< std::array< real, 3 >, 3 >
R{{ {{ 1.0, 0.0, 0.0 }},
{{ 0.0, cos(angle), -sin(angle) }},
{{ 0.0, sin(angle), cos(angle) }} }};
return {{ dot(R[0],v), dot(R[1],v), dot(R[2],v) }};
}
//! Rotate vector about Y axis
//! \param[in] v Vector to rotate
//! \param[in] angle Angle to use to rotate with
//! \return Rotated vector
inline std::array< real, 3 >
rotateY( const std::array< real, 3 >& v, real angle )
{
using std::cos; using std::sin;
std::array< std::array< real, 3 >, 3 >
R{{ {{ cos(angle), 0.0, sin(angle) }},
{{ 0.0, 1.0, 0.0 }},
{{ -sin(angle), 0.0, cos(angle) }} }};
return {{ dot(R[0],v), dot(R[1],v), dot(R[2],v) }};
}
//! Rotate vector about Z axis
//! \param[in] v Vector to rotate
//! \param[in] angle Angle to use to rotate with
//! \return Rotated vector
inline std::array< real, 3 >
rotateZ( const std::array< real, 3 >& v, real angle )
{
using std::cos; using std::sin;
std::array< std::array< real, 3 >, 3 >
R{{ {{ cos(angle), -sin(angle), 0.0 }},
{{ sin(angle), cos(angle), 0.0 }},
{{ 0.0, 0.0, 1.0 }} }};
return {{ dot(R[0],v), dot(R[1],v), dot(R[2],v) }};
}
//! \brief Compute the determinant of the Jacobian of a coordinate
//! transformation over a tetrahedron
//! \param[in] v1 (x,y,z) coordinates of 1st vertex of the tetrahedron
//! \param[in] v2 (x,y,z) coordinates of 2nd vertex of the tetrahedron
//! \param[in] v3 (x,y,z) coordinates of 3rd vertex of the tetrahedron
//! \param[in] v4 (x,y,z) coordinates of 4th vertex of the tetrahedron
//! \return Determinant of the Jacobian of transformation of physical
//! tetrahedron to reference (xi, eta, zeta) space
inline real
Jacobian( const std::array< real, 3 >& v1,
const std::array< real, 3 >& v2,
const std::array< real, 3 >& v3,
const std::array< real, 3 >& v4 )
{
std::array< real, 3 > ba{{ v2[0]-v1[0], v2[1]-v1[1], v2[2]-v1[2] }},
ca{{ v3[0]-v1[0], v3[1]-v1[1], v3[2]-v1[2] }},
da{{ v4[0]-v1[0], v4[1]-v1[1], v4[2]-v1[2] }};
return triple( ba, ca, da );
}
//! \brief Compute the inverse of the Jacobian of a coordinate transformation
//! over a tetrahedron
//! \param[in] v1 (x,y,z) coordinates of 1st vertex of the tetrahedron
//! \param[in] v2 (x,y,z) coordinates of 2nd vertex of the tetrahedron
//! \param[in] v3 (x,y,z) coordinates of 3rd vertex of the tetrahedron
//! \param[in] v4 (x,y,z) coordinates of 4th vertex of the tetrahedron
//! \return Inverse of the Jacobian of transformation of physical
//! tetrahedron to reference (xi, eta, zeta) space
inline std::array< std::array< real, 3 >, 3 >
inverseJacobian( const std::array< real, 3 >& v1,
const std::array< real, 3 >& v2,
const std::array< real, 3 >& v3,
const std::array< real, 3 >& v4 )
{
std::array< std::array< real, 3 >, 3 > jacInv;
auto detJ = Jacobian( v1, v2, v3, v4 );
jacInv[0][0] = ( (v3[1]-v1[1])*(v4[2]-v1[2])
- (v4[1]-v1[1])*(v3[2]-v1[2])) / detJ;
jacInv[1][0] = -( (v2[1]-v1[1])*(v4[2]-v1[2])
- (v4[1]-v1[1])*(v2[2]-v1[2])) / detJ;
jacInv[2][0] = ( (v2[1]-v1[1])*(v3[2]-v1[2])
- (v3[1]-v1[1])*(v2[2]-v1[2])) / detJ;
jacInv[0][1] = -( (v3[0]-v1[0])*(v4[2]-v1[2])
- (v4[0]-v1[0])*(v3[2]-v1[2])) / detJ;
jacInv[1][1] = ( (v2[0]-v1[0])*(v4[2]-v1[2])
- (v4[0]-v1[0])*(v2[2]-v1[2])) / detJ;
jacInv[2][1] = -( (v2[0]-v1[0])*(v3[2]-v1[2])
- (v3[0]-v1[0])*(v2[2]-v1[2])) / detJ;
jacInv[0][2] = ( (v3[0]-v1[0])*(v4[1]-v1[1])
- (v4[0]-v1[0])*(v3[1]-v1[1])) / detJ;
jacInv[1][2] = -( (v2[0]-v1[0])*(v4[1]-v1[1])
- (v4[0]-v1[0])*(v2[1]-v1[1])) / detJ;
jacInv[2][2] = ( (v2[0]-v1[0])*(v3[1]-v1[1])
- (v3[0]-v1[0])*(v2[1]-v1[1])) / detJ;
return jacInv;
}
//! Compute the determinant of 3x3 matrix
//! \param[in] a 3x3 matrix
//! \return Determinant of the 3x3 matrix
inline tk::real
determinant( const std::array< std::array< tk::real, 3 >, 3 >& a )
{
return ( a[0][0] * (a[1][1]*a[2][2]-a[1][2]*a[2][1])
- a[0][1] * (a[1][0]*a[2][2]-a[1][2]*a[2][0])
+ a[0][2] * (a[1][0]*a[2][1]-a[1][1]*a[2][0]) );
}
//! Solve a 3x3 system of equations using Cramer's rule
//! \param[in] a 3x3 lhs matrix
//! \param[in] b 3x1 rhs matrix
//! \return Array of solutions of the 3x3 system
inline std::array < tk::real, 3 >
cramer( const std::array< std::array< tk::real, 3 >, 3>& a,
const std::array< tk::real, 3 >& b )
{
auto de = determinant( a );
auto nu(0.0);
std::array < real, 3 > x;
nu = determinant( {{{{b[0], a[0][1], a[0][2]}},
{{b[1], a[1][1], a[1][2]}},
{{b[2], a[2][1], a[2][2]}}}} );
x[0] = nu/de;
nu = determinant( {{{{a[0][0], b[0], a[0][2]}},
{{a[1][0], b[1], a[1][2]}},
{{a[2][0], b[2], a[2][2]}}}} );
x[1] = nu/de;
nu = determinant( {{{{a[0][0], a[0][1], b[0]}},
{{a[1][0], a[1][1], b[1]}},
{{a[2][0], a[2][1], b[2]}}}} );
x[2] = nu/de;
return x;
}
} // tk::
#endif // Vector_h
|