/home/runner/work/amr-wind/amr-wind/amr-wind/utilities/tensor_ops.H Source File

AMR-Wind API: /home/runner/work/amr-wind/amr-wind/amr-wind/utilities/tensor_ops.H Source File
AMR-Wind API v0.1.0
CFD solver for wind plant simulations
Loading...
Searching...
No Matches
tensor_ops.H
Go to the documentation of this file.
1#ifndef TENSOR_OPS_H
2#define TENSOR_OPS_H
3
4#include "AMReX_Gpu.H"
5#include "AMReX_REAL.H"
6
7using namespace amrex::literals;
8
9namespace amr_wind::utils {
10
13template <typename T>
14AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T dot_prod(const T* x, const T* y)
15{
16 static_assert(
17 AMREX_SPACEDIM == 3, "Vector ops only defined for AMREX_SPACEDIM == 3");
18 return (x[0] * y[0] + x[1] * y[1] + x[2] * y[2]);
19}
20
23template <typename T>
24AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T vec_mag(const T* x)
25{
26 return std::sqrt(dot_prod(x, x));
27}
28
31template <typename T>
32AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void vec_normalize(T* x)
33{
34 static_assert(
35 AMREX_SPACEDIM == 3, "Vector ops only defined for AMREX_SPACEDIM == 3");
36 const T inv_mag = 1.0_rt / vec_mag(x);
37 x[0] *= inv_mag;
38 x[1] *= inv_mag;
39 x[2] *= inv_mag;
40}
41
42template <typename T>
43AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T
44dot_prod(const T* x, const T* y, const int ndim)
45{
46 T sum = 0.0_rt;
47 for (int i = 0; i < ndim; ++i) {
48 sum += x[i] * y[i];
49 }
50 return sum;
51}
52
55template <typename T>
56AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void
57cross_prod(const T* a, const T* b, T* c)
58{
59 static_assert(
60 AMREX_SPACEDIM == 3, "Vector ops only defined for AMREX_SPACEDIM == 3");
61 c[0] = a[1] * b[2] - a[2] * b[1];
62 c[1] = a[2] * b[0] - a[0] * b[2];
63 c[2] = a[0] * b[1] - a[1] * b[0];
64}
65
68template <typename T>
69AMREX_GPU_DEVICE AMREX_FORCE_INLINE void transform_vec(
70 const amrex::Array2D<T, 0, AMREX_SPACEDIM, 0, AMREX_SPACEDIM>& tmat,
71 const T* x,
72 T* y)
73{
74 static_assert(
75 AMREX_SPACEDIM == 3, "Vector ops only defined for AMREX_SPACEDIM == 3");
76 y[0] = tmat(0, 0) * x[0] + tmat(0, 1) * x[1] + tmat(0, 2) * x[2];
77 y[1] = tmat(1, 0) * x[0] + tmat(1, 1) * x[1] + tmat(1, 2) * x[2];
78 y[2] = tmat(2, 0) * x[0] + tmat(2, 1) * x[1] + tmat(2, 2) * x[2];
79}
80
83template <typename T>
84AMREX_GPU_DEVICE AMREX_FORCE_INLINE void inv_transform_vec(
85 const amrex::Array2D<T, 0, AMREX_SPACEDIM, 0, AMREX_SPACEDIM>& tmat,
86 const T* x,
87 T* y)
88{
89 static_assert(
90 AMREX_SPACEDIM == 3, "Vector ops only defined for AMREX_SPACEDIM == 3");
91 y[0] = tmat(0, 0) * x[0] + tmat(1, 0) * x[1] + tmat(2, 0) * x[2];
92 y[1] = tmat(0, 1) * x[0] + tmat(1, 1) * x[1] + tmat(2, 1) * x[2];
93 y[2] = tmat(0, 2) * x[0] + tmat(1, 2) * x[1] + tmat(2, 2) * x[2];
94}
95
96} // namespace amr_wind::utils
97
98#endif /* TENSOR_OPS_H */
Definition MultiParser.H:7
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void vec_normalize(T *x)
Definition tensor_ops.H:32
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T dot_prod(const T *x, const T *y)
Definition tensor_ops.H:14
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void cross_prod(const T *a, const T *b, T *c)
Definition tensor_ops.H:57
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE T vec_mag(const T *x)
Definition tensor_ops.H:24
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void transform_vec(const amrex::Array2D< T, 0, AMREX_SPACEDIM, 0, AMREX_SPACEDIM > &tmat, const T *x, T *y)
Definition tensor_ops.H:69
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void inv_transform_vec(const amrex::Array2D< T, 0, AMREX_SPACEDIM, 0, AMREX_SPACEDIM > &tmat, const T *x, T *y)
Definition tensor_ops.H:84