Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
mhd_utilities.h
Go to the documentation of this file.
1
8#pragma once
9
10// STL Includes
11#include <vector>
12
13// External Includes
14
15// Local Includes
16#include "../global/global.h"
17#include "../global/global_cuda.h"
18#include "../grid/grid3D.h"
19#include "../utils/basic_structs.h"
20#include "../utils/cuda_utilities.h"
21#include "../utils/gpu.hpp"
22#include "../utils/math_utilities.h"
23
24namespace mhd::utils
25{
32namespace internal
33{
34// =====================================================================
49inline __host__ __device__ Real _magnetosonicSpeed(Real const &density, Real const &gasPressure, Real const &magneticX,
50 Real const &magneticY, Real const &magneticZ, Real const &gamma,
51 Real const &waveChoice)
52{
53 // Compute the sound speed
54 Real bXSquared = magneticX * magneticX;
55 Real bSquared = bXSquared + ((magneticY * magneticY) + (magneticZ * magneticZ));
56
57 Real term1 = gamma * gasPressure + bSquared;
58
59 Real term2 = (term1 * term1) - 4. * gamma * gasPressure * bXSquared;
60 term2 = sqrt(term2);
61
62 return sqrt((term1 + waveChoice * term2) / (2.0 * fmax(density, TINY_NUMBER)));
63}
64// =====================================================================
65} // namespace internal
66
67// =========================================================================
76inline __host__ __device__ Real computeMagneticEnergy(Real const &magneticX, Real const &magneticY,
77 Real const &magneticZ)
78{
79 return 0.5 * math_utils::SquareMagnitude(magneticX, magneticY, magneticZ);
80}
81// =========================================================================
82
83// =========================================================================
98inline __host__ __device__ Real computeThermalEnergy(Real const &energyTot, Real const &density, Real const &momentumX,
99 Real const &momentumY, Real const &momentumZ,
100 Real const &magneticX, Real const &magneticY,
101 Real const &magneticZ, Real const &gamma)
102{
103 return energyTot - 0.5 * math_utils::SquareMagnitude(momentumX, momentumY, momentumZ) / fmax(density, TINY_NUMBER) -
104 computeMagneticEnergy(magneticX, magneticY, magneticZ);
105}
106// =========================================================================
107
108// =========================================================================
119inline __host__ __device__ Real computeTotalPressure(Real const &gasPressure, Real const &magneticX,
120 Real const &magneticY, Real const &magneticZ)
121{
122 Real pTot = gasPressure + computeMagneticEnergy(magneticX, magneticY, magneticZ);
123
124 return fmax(pTot, TINY_NUMBER);
125}
127inline __host__ __device__ Real computeTotalPressure(Real const &gasPressure,
128 hydro_utilities::VectorXYZ<Real> const &magnetic)
129{
130 return computeTotalPressure(gasPressure, magnetic.x(), magnetic.y(), magnetic.z());
131}
132// =========================================================================
133
134// =========================================================================
146inline __host__ __device__ Real fastMagnetosonicSpeed(Real const &density, Real const &pressure, Real const &magneticX,
147 Real const &magneticY, Real const &magneticZ, Real const &gamma)
148{
149 // Compute the sound speed
150 return mhd::utils::internal::_magnetosonicSpeed(density, pressure, magneticX, magneticY, magneticZ, gamma, 1.0);
151}
152// =========================================================================
153
154// =========================================================================
166inline __host__ __device__ Real slowMagnetosonicSpeed(Real const &density, Real const &pressure, Real const &magneticX,
167 Real const &magneticY, Real const &magneticZ, Real const &gamma)
168{
169 // Compute the sound speed
170 return mhd::utils::internal::_magnetosonicSpeed(density, pressure, magneticX, magneticY, magneticZ, gamma, -1.0);
171}
172// =========================================================================
173
174// =========================================================================
183inline __host__ __device__ Real alfvenSpeed(Real const &magneticX, Real const &density)
184{
185 // Compute the Alfven wave speed
186 return fabs(magneticX) / sqrt(fmax(density, TINY_NUMBER));
187}
188// =========================================================================
189
190// =========================================================================
191#ifdef MHD
212inline __host__ __device__ hydro_utilities::VectorXYZ<Real> cellCenteredMagneticFields(
213 Real const *dev_conserved, size_t const &id, size_t const &xid, size_t const &yid, size_t const &zid,
214 size_t const &n_cells, size_t const &nx, size_t const &ny)
215{
216 // Ternary operator to check that no values outside of the magnetic field
217 // arrays are loaded. If the cell is on the edge that doesn't have magnetic
218 // fields on both sides then instead set the centered magnetic field to be
219 // equal to the magnetic field of the closest edge.
220 Real avgBx = (xid > 0) ?
221 /*if true*/ 0.5 * (dev_conserved[(grid_enum::magnetic_x)*n_cells + id] +
222 dev_conserved[(grid_enum::magnetic_x)*n_cells +
223 cuda_utilities::compute1DIndex(xid - 1, yid, zid, nx, ny)])
224 :
225 /*if false*/ dev_conserved[(grid_enum::magnetic_x)*n_cells + id];
226 Real avgBy = (yid > 0) ?
227 /*if true*/ 0.5 * (dev_conserved[(grid_enum::magnetic_y)*n_cells + id] +
228 dev_conserved[(grid_enum::magnetic_y)*n_cells +
229 cuda_utilities::compute1DIndex(xid, yid - 1, zid, nx, ny)])
230 :
231 /*if false*/ dev_conserved[(grid_enum::magnetic_y)*n_cells + id];
232 Real avgBz = (zid > 0) ?
233 /*if true*/ 0.5 * (dev_conserved[(grid_enum::magnetic_z)*n_cells + id] +
234 dev_conserved[(grid_enum::magnetic_z)*n_cells +
235 cuda_utilities::compute1DIndex(xid, yid, zid - 1, nx, ny)])
236 :
237 /*if false*/ dev_conserved[(grid_enum::magnetic_z)*n_cells + id];
238
239 return {avgBx, avgBy, avgBz};
240}
241// =========================================================================
242
243// =========================================================================
251void Init_Magnetic_Field_With_Vector_Potential(Header const &H, Grid3D::Conserved const &C,
252 std::vector<Real> const &vectorPotential);
253// =========================================================================
254#endif // MHD
255} // end namespace mhd::utils
__host__ __device__ Real computeMagneticEnergy(Real const &magneticX, Real const &magneticY, Real const &magneticZ)
Compute the magnetic energy.
Definition mhd_utilities.h:76
__host__ __device__ Real computeThermalEnergy(Real const &energyTot, Real const &density, Real const &momentumX, Real const &momentumY, Real const &momentumZ, Real const &magneticX, Real const &magneticY, Real const &magneticZ, Real const &gamma)
Compute the MHD thermal energy in a cell.
Definition mhd_utilities.h:98
__host__ __device__ Real slowMagnetosonicSpeed(Real const &density, Real const &pressure, Real const &magneticX, Real const &magneticY, Real const &magneticZ, Real const &gamma)
Compute the speed of the slow magnetosonic wave.
Definition mhd_utilities.h:166
__host__ __device__ Real alfvenSpeed(Real const &magneticX, Real const &density)
Compute the speed of the Alfven wave in a cell.
Definition mhd_utilities.h:183
__host__ __device__ Real computeTotalPressure(Real const &gasPressure, Real const &magneticX, Real const &magneticY, Real const &magneticZ)
Compute the total MHD pressure. I.e. magnetic pressure + gas pressure.
Definition mhd_utilities.h:119
__host__ __device__ Real fastMagnetosonicSpeed(Real const &density, Real const &pressure, Real const &magneticX, Real const &magneticY, Real const &magneticZ, Real const &gamma)
Compute the speed of the fast magnetosonic wave.
Definition mhd_utilities.h:146
__host__ __device__ Real _magnetosonicSpeed(Real const &density, Real const &gasPressure, Real const &magneticX, Real const &magneticY, Real const &magneticZ, Real const &gamma, Real const &waveChoice)
Compute the fast or slow magnetosonic wave speeds.
Definition mhd_utilities.h:49
Definition grid3D.h:278
Definition grid3D.h:55
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