Skip to content

Commit

Permalink
add small e protection
Browse files Browse the repository at this point in the history
  • Loading branch information
zingale committed Jan 25, 2025
1 parent 9167723 commit 0c9b5a5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
22 changes: 20 additions & 2 deletions pyro/compressible/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ def cons_to_prim(U, gamma, ivars, myg):
e_min = e.v().min()
rho_min = q.v(n=ivars.irho).min()

if e_min < 0:
eidx = np.asarray(e < 0).nonzero()
i_idx = eidx[0]
j_jdx = eidx[1]
for i, j in zip(i_idx, j_jdx):
print(f" e < 0: {i}, {j}, {e[i, j]}")

assert e_min > 0.0 and rho_min > 0.0, f"invalid state, min(rho) = {rho_min}, min(e) = {e_min}"

q[:, :, ivars.ip] = eos.pres(gamma, q[:, :, ivars.irho], e)
Expand Down Expand Up @@ -442,8 +449,19 @@ def evolve(self):
def clean_state(self, U):
"""enforce minimum density and eint on the conserved state U"""

U.v(n=self.ivars.idens)[:, :] = np.maximum(U.v(n=self.ivars.idens),
self.rp.get_param("compressible.small_dens"))
U[..., self.ivars.idens] = np.maximum(U[..., self.ivars.idens],
self.rp.get_param("compressible.small_dens"))

if self.small_eint > 0:

ekin = 0.5 * (U[..., self.ivars.ixmom]**2 +
U[..., self.ivars.iymom]**2) / U[..., self.ivars.idens]

rhoe = U[..., self.ivars.iener] - ekin

U[..., self.ivars.iener] = np.where(rhoe < self.small_eint,
U[..., self.ivars.idens] * self.small_eint + ekin,
U[..., self.ivars.iener])

def dovis(self):
"""
Expand Down
1 change: 1 addition & 0 deletions pyro/compressible_fv4/_defaults
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ grav = 0.0 ; gravitational acceleration (in y-direction)
riemann = CGF

small_dens = -1.e200 ; minimum allowed density
small_eint_factor = -1.e200 ; multiplicative factor on the initial minimum eint to use for limiting e

[sponge]
do_sponge = 0 ; do we include a sponge source term
Expand Down
11 changes: 10 additions & 1 deletion pyro/compressible_fv4/simulation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pyro.compressible_fv4.fluxes as flx
from pyro import compressible_rk
from pyro.compressible import get_external_sources, get_sponge_factor
from pyro.compressible import cons_to_prim, get_external_sources, get_sponge_factor
from pyro.mesh import fv


Expand Down Expand Up @@ -68,3 +68,12 @@ def preevolve(self):
# we just initialized cell-centers, but we need to store averages
for var in self.cc_data.names:
self.cc_data.from_centers(var)

efac = self.rp.get_param("compressible.small_eint_factor")

print(type(self.cc_data))

q = cons_to_prim(self.cc_data.data, self.rp.get_param("eos.gamma"), self.ivars, self.cc_data.grid)

self.small_eint = efac * q.v(n=self.ivars.ip).min()
print("small_eint = ", self.small_eint)
2 changes: 2 additions & 0 deletions pyro/compressible_sdc/_defaults
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ riemann = CGF

small_dens = -1.e200 ; minimum allowed density

small_eint_factor = -1.e200 ; multiplicative factor on the initial minimum eint to use for limiting e

[sponge]
do_sponge = 0 ; do we include a sponge source term

Expand Down

0 comments on commit 0c9b5a5

Please sign in to comment.