1515#include " cooling/ResampledCooling.hpp"
1616#include " math/interpolate.hpp"
1717#include " util/BC.hpp"
18+ #include < algorithm>
19+ #include < array>
20+ #include < cmath>
1821#include < format>
1922#include < fstream>
2023#include < iomanip>
24+ #include < limits>
2125#include < sstream>
2226#include < vector>
2327
@@ -97,14 +101,25 @@ template <> struct Physics_Traits<ResampledCoolingTest> {
97101 static constexpr bool is_radiation_enabled = false ;
98102 static constexpr bool is_dust_enabled = false ;
99103 static constexpr int nDustGroups = 1 ; // number of dust groups
100- static constexpr bool is_mhd_enabled = false ;
104+ static constexpr bool is_mhd_enabled = ( AMREX_SPACEDIM == 3 ) ;
101105 static constexpr int nGroups = 1 ; // number of radiation groups
102106 static constexpr UnitSystem unit_system = UnitSystem::CGS ;
103107};
104108
105109// Initial conditions: hot gas that will cool down
106110constexpr double T_initial = 1.0e7 ; // K
107111constexpr double rho_initial = 1.0e-26 ; // g cm^-3 (constant density for isochoric)
112+ constexpr double mu_initial = 0.6 * quokka::EOS_Traits<ResampledCoolingTest>::mean_molecular_weight;
113+ constexpr double pressure_initial = rho_initial * C::k_B * T_initial / mu_initial;
114+ constexpr double Bx_initial = 5.264491941623788e-06 ; // chosen so plasma beta = P_gas / (B^2 / 2) ~= 1
115+ constexpr double magnetic_energy_initial = 0.5 * Bx_initial * Bx_initial;
116+ constexpr double active_magnetic_energy_initial = Physics_Traits<ResampledCoolingTest>::is_mhd_enabled ? magnetic_energy_initial : 0.0 ;
117+
118+ auto computeInitialInternalEnergy () -> double
119+ {
120+ constexpr double gamma = quokka::EOS_Traits<ResampledCoolingTest>::gamma;
121+ return rho_initial * C::k_B * T_initial / ((gamma - 1.0 ) * mu_initial);
122+ }
108123
109124template <> void QuokkaSimulation<ResampledCoolingTest>::setInitialConditionsOnGrid(quokka::grid const &grid_elem)
110125{
@@ -114,25 +129,40 @@ template <> void QuokkaSimulation<ResampledCoolingTest>::setInitialConditionsOnG
114129 // Compute initial internal energy from temperature
115130 const double k_B = C::k_B;
116131 const double gamma = quokka::EOS_Traits<ResampledCoolingTest>::gamma;
117- const double mu = 0.6 * quokka::EOS_Traits<ResampledCoolingTest>::mean_molecular_weight;
118132
119133 // For ideal gas: P = (gamma - 1) * rho * e_int
120134 // and P = rho * k_B * T / (mu * m_u)
121135 // Therefore: e_int = k_B * T / ((gamma - 1) * mu * m_u)
122- const double e_int_initial = k_B * T_initial / ((gamma - 1.0 ) * mu );
136+ const double e_int_initial = k_B * T_initial / ((gamma - 1.0 ) * mu_initial );
123137 const double Eint_initial = rho_initial * e_int_initial;
138+ const double Egas_initial = Eint_initial + active_magnetic_energy_initial;
124139
125140 // loop over the grid and set the initial condition
126141 amrex::ParallelFor (indexRange, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
127142 state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::density_index) = rho_initial;
128143 state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::x1Momentum_index) = 0 .;
129144 state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::x2Momentum_index) = 0 .;
130145 state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::x3Momentum_index) = 0 .;
131- state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::energy_index) = Eint_initial ;
146+ state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::energy_index) = Egas_initial ;
132147 state_cc (i, j, k, HydroSystem<ResampledCoolingTest>::internalEnergy_index) = Eint_initial;
133148 });
134149}
135150
151+ template <> void QuokkaSimulation<ResampledCoolingTest>::setInitialConditionsOnGridFaceVars(quokka::grid const &grid_elem)
152+ {
153+ const amrex::Array4<double > &state_fc = grid_elem.array_ ;
154+ const amrex::Box &indexRange = grid_elem.indexRange_ ;
155+ const quokka::direction dir = grid_elem.dir_ ;
156+
157+ amrex::ParallelFor (indexRange, [=] AMREX_GPU_DEVICE (int i, int j, int k) {
158+ if (dir == quokka::direction::x) {
159+ state_fc (i, j, k, Physics_Indices<ResampledCoolingTest>::mhdFirstIndex) = Bx_initial;
160+ } else {
161+ state_fc (i, j, k, Physics_Indices<ResampledCoolingTest>::mhdFirstIndex) = 0.0 ;
162+ }
163+ });
164+ }
165+
136166template <> void QuokkaSimulation<ResampledCoolingTest>::computeAfterTimestep()
137167{
138168 // Extract solution at center of domain
@@ -143,8 +173,8 @@ template <> void QuokkaSimulation<ResampledCoolingTest>::computeAfterTimestep()
143173
144174 const amrex::Real Etot = values.at (HydroSystem<ResampledCoolingTest>::energy_index)[0 ];
145175 const amrex::Real rho = values.at (HydroSystem<ResampledCoolingTest>::density_index)[0 ];
146- // For isochoric cooling with no kinetic energy, Eint = Etot
147- const amrex::Real Eint = Etot;
176+ // For isochoric MHD cooling with no kinetic energy and a uniform magnetic field, subtract the constant magnetic energy.
177+ const amrex::Real Eint = Etot - active_magnetic_energy_initial ;
148178
149179 // Get temperature from tables
150180 amrex::Real T = NAN ;
0 commit comments