Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
math_utilities.h
Go to the documentation of this file.
1
8#pragma once
9
10// STL Includes
11#include <cmath>
12#include <tuple>
13
14// External Includes
15
16// Local Includes
17#include "../global/global.h"
18#include "../global/global_cuda.h"
19#include "../utils/basic_structs.h"
20#include "../utils/gpu.hpp"
21
22namespace math_utils
23{
24// =========================================================================
42template <typename T>
43inline std::tuple<T, T, T> rotateCoords(Real const &x_1, Real const &x_2, Real const &x_3, Real const &pitch,
44 Real const &yaw)
45{
46 // Compute the sines and cosines. Correct for floating point errors if
47 // the angle is 0.5*M_PI
48 Real const sin_yaw = std::sin(yaw);
49 Real const cos_yaw = (yaw == 0.5 * M_PI) ? 0 : std::cos(yaw);
50 Real const sin_pitch = std::sin(pitch);
51 Real const cos_pitch = (pitch == 0.5 * M_PI) ? 0 : std::cos(pitch);
52
53 // Perform the rotation
54 Real const x_1_rot = (x_1 * cos_pitch * cos_yaw) + (x_2 * sin_yaw) + (x_3 * sin_pitch * cos_yaw);
55 Real const x_2_rot = (x_1 * cos_pitch * sin_yaw) + (x_2 * cos_yaw) + (x_3 * sin_pitch * sin_yaw);
56 Real const x_3_rot = (x_1 * sin_pitch) + (x_3 * cos_pitch);
57
58 if (std::is_same<T, int>::value) {
59 return {round(x_1_rot), round(x_2_rot), round(x_3_rot)};
60 } else if (std::is_same<T, Real>::value) {
61 return {x_1_rot, x_2_rot, x_3_rot};
62 }
63}
64// =========================================================================
65
66// =========================================================================
79inline __device__ __host__ Real dotProduct(Real const &a1, Real const &a2, Real const &a3, Real const &b1,
80 Real const &b2, Real const &b3)
81{
82 return a1 * b1 + ((a2 * b2) + (a3 * b3));
83};
84// =========================================================================
85
86// =========================================================================
96inline __device__ __host__ Real SquareMagnitude(Real const &v1, Real const &v2, Real const &v3)
97{
98 return dotProduct(v1, v2, v3, v1, v2, v3);
99};
100// =========================================================================
101
102// =====================================================================================================================
108inline __device__ __host__ void Cyclic_Permute_Once(hydro_utilities::VectorXYZ<Real> &vec)
109{
110 Real temp = vec.x();
111 vec.x() = vec.y();
112 vec.y() = vec.z();
113 vec.z() = temp;
114}
115// =====================================================================================================================
116
117// =====================================================================================================================
123inline __device__ __host__ void Cyclic_Permute_Twice(hydro_utilities::VectorXYZ<Real> &vec)
124{
125 Real temp = vec.y();
126 vec.y() = vec.x();
127 vec.x() = vec.z();
128 vec.z() = temp;
129}
130// =====================================================================================================================
131
143template <typename T>
144__device__ __host__ T clamp(T val, T lo, T hi)
145{
146 const T tmp = val < lo ? lo : val;
147 return tmp > hi ? hi : tmp;
148}
149
150} // namespace math_utils
std::tuple< T, T, T > rotateCoords(Real const &x_1, Real const &x_2, Real const &x_3, Real const &pitch, Real const &yaw)
Rotate cartesian coordinates. All arguments are cast to double then rotated. If the type is 'int' the...
Definition math_utilities.h:43
__device__ __host__ T clamp(T val, T lo, T hi)
When val lies within the inclusive range [lo, hi], returns val. Otherwise, return the closest value i...
Definition math_utilities.h:144
__device__ __host__ Real SquareMagnitude(Real const &v1, Real const &v2, Real const &v3)
Compute the magnitude of a vector.
Definition math_utilities.h:96
__device__ __host__ void Cyclic_Permute_Twice(hydro_utilities::VectorXYZ< Real > &vec)
Cyclically permute a Vector twice. i.e. (x,y,z) becomes (z,x,y)
Definition math_utilities.h:123
__device__ __host__ Real dotProduct(Real const &a1, Real const &a2, Real const &a3, Real const &b1, Real const &b2, Real const &b3)
Compute the dot product of a and b.
Definition math_utilities.h:79
__device__ __host__ void Cyclic_Permute_Once(hydro_utilities::VectorXYZ< Real > &vec)
Cyclically permute a Vector once. i.e. (x,y,z) becomes (y,z,x)
Definition math_utilities.h:108
A data only struct that acts as a simple 3 element vector.
Definition basic_structs.h:32
__device__ __host__ T & x() noexcept
Directly access the x, y, and z elements. Const version is needed if the object instance is declared ...
Definition basic_structs.h:68