Skip to content

Commit

Permalink
Enforce checks and limits on mass scalars (#378)
Browse files Browse the repository at this point in the history
* return invalid state if mass scalars have negative partial densities

* enforce limits on mass scalars

* use if constexpr

---------

Co-authored-by: Piyush Sharda <[email protected]>
  • Loading branch information
psharda and Piyush Sharda authored Sep 14, 2023
1 parent 16d5737 commit a3fc85c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/hydro_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "radiation_system.hpp"
#include "valarray.hpp"

// Microphysics headers
#include "extern_parameters.H"

// this struct is specialized by the user application code
//
template <typename problem_t> struct HydroSystem_Traits {
Expand Down Expand Up @@ -427,6 +430,18 @@ AMREX_GPU_DEVICE AMREX_FORCE_INLINE auto HydroSystem<problem_t>::isStateValid(am
const amrex::Real rho = cons(i, j, k, density_index);
bool isDensityPositive = (rho > 0.);

bool isMassScalarPositive = true;
if constexpr (nmscalars_ > 0) {
amrex::GpuArray<Real, nmscalars_> massScalars_ = RadSystem<problem_t>::ComputeMassScalars(cons, i, j, k);

for (int idx = 0; idx < nmscalars_; ++idx) {
if (massScalars_[idx] < 0.0) {
isMassScalarPositive = false;
break; // Exit the loop early if any element is not positive
}
}

}
// when the dual energy method is used, we *cannot* reset on pressure
// failures. on the other hand, we don't need to -- the auxiliary internal
// energy is used instead!
Expand All @@ -441,7 +456,7 @@ AMREX_GPU_DEVICE AMREX_FORCE_INLINE auto HydroSystem<problem_t>::isStateValid(am
#endif
// return (isDensityPositive && isPressurePositive);

return isDensityPositive;
return isDensityPositive && isMassScalarPositive;
}

template <typename problem_t>
Expand Down Expand Up @@ -726,6 +741,17 @@ void HydroSystem<problem_t>::EnforceLimits(amrex::Real const densityFloor, amrex
state[bx](i, j, k, x3Momentum_index) *= rescale_factor;
}

// Enforcing Limits on mass scalars
if (nmscalars_ > 0) {

for (int idx = 0; idx < nmscalars_; ++idx) {
if (state[bx](i, j, k, scalar0_index + idx) < 0.0) {
state[bx](i, j, k, scalar0_index + idx) = small_x * rho;
}
}

}

// Enforcing Limits on temperature estimated from Etot and Ekin
if (!HydroSystem<problem_t>::is_eos_isothermal()) {
// recompute gas energy (to prevent P < 0)
Expand Down

0 comments on commit a3fc85c

Please sign in to comment.