Skip to content

Commit 1cdab61

Browse files
authored
reorganize compressible riemann solver interface to prepare for SphericalPolar geometry (#210)
Split up riemann_cgf to 2 parts: getting the conserved states then flux construction added an overall riemann_flux interface to find the flux directly if there is no need to get the conserved states. make consFlux general for both multi-zone U_state or single zone U_state.
1 parent d00a231 commit 1cdab61

3 files changed

Lines changed: 137 additions & 100 deletions

File tree

pyro/compressible/riemann.py

Lines changed: 118 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import numpy as np
22
from numba import njit
33

4+
import pyro.mesh.array_indexer as ai
5+
from pyro.util import msg
6+
47

58
@njit(cache=True)
69
def riemann_cgf(idir, ng,
@@ -56,12 +59,14 @@ def riemann_cgf(idir, ng,
5659
Returns
5760
-------
5861
out : ndarray
59-
Conserved flux
62+
Conserved states.
6063
"""
6164

65+
# pylint: disable=unused-variable
66+
6267
qx, qy, nvar = U_l.shape
6368

64-
F = np.zeros((qx, qy, nvar))
69+
U_out = np.zeros((qx, qy, nvar))
6570

6671
smallc = 1.e-10
6772
smallrho = 1.e-10
@@ -286,24 +291,23 @@ def riemann_cgf(idir, ng,
286291
if j == jhi + 1 and upper_solid == 1:
287292
un_state = 0.0
288293

289-
# compute the fluxes
290-
F[i, j, idens] = rho_state * un_state
294+
# update conserved state
295+
U_out[i, j, idens] = rho_state
291296

292297
if idir == 1:
293-
F[i, j, ixmom] = rho_state * un_state**2 + p_state
294-
F[i, j, iymom] = rho_state * ut_state * un_state
298+
U_out[i, j, ixmom] = rho_state * un_state
299+
U_out[i, j, iymom] = rho_state * ut_state
295300
else:
296-
F[i, j, ixmom] = rho_state * ut_state * un_state
297-
F[i, j, iymom] = rho_state * un_state**2 + p_state
301+
U_out[i, j, ixmom] = rho_state * ut_state
302+
U_out[i, j, iymom] = rho_state * un_state
298303

299-
F[i, j, iener] = rhoe_state * un_state + \
300-
0.5 * rho_state * (un_state**2 + ut_state**2) * un_state + \
301-
p_state * un_state
304+
U_out[i, j, iener] = rhoe_state + \
305+
0.5 * rho_state * (un_state**2 + ut_state**2)
302306

303307
if nspec > 0:
304-
F[i, j, irhoX:irhoX + nspec] = xn * rho_state * un_state
308+
U_out[i, j, irhoX:irhoX + nspec] = xn * rho_state
305309

306-
return F
310+
return U_out
307311

308312

309313
@njit(cache=True)
@@ -623,6 +627,9 @@ def riemann_hllc(idir, ng,
623627
Conserved flux
624628
"""
625629

630+
# Only Cartesian2d is supported in HLLC
631+
coord_type = 0
632+
626633
qx, qy, nvar = U_l.shape
627634

628635
F = np.zeros((qx, qy, nvar))
@@ -781,7 +788,8 @@ def riemann_hllc(idir, ng,
781788
# R region
782789
U_state[:] = U_r[i, j, :]
783790

784-
F[i, j, :] = consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec,
791+
F[i, j, :] = consFlux(idir, coord_type, gamma,
792+
idens, ixmom, iymom, iener, irhoX, nspec,
785793
U_state)
786794

787795
elif S_c <= 0.0 < S_r:
@@ -806,7 +814,8 @@ def riemann_hllc(idir, ng,
806814
U_r[i, j, irhoX:irhoX + nspec] / rho_r
807815

808816
# find the flux on the right interface
809-
F[i, j, :] = consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec,
817+
F[i, j, :] = consFlux(idir, coord_type, gamma,
818+
idens, ixmom, iymom, iener, irhoX, nspec,
810819
U_r[i, j, :])
811820

812821
# correct the flux
@@ -834,7 +843,8 @@ def riemann_hllc(idir, ng,
834843
U_l[i, j, irhoX:irhoX + nspec] / rho_l
835844

836845
# find the flux on the left interface
837-
F[i, j, :] = consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec,
846+
F[i, j, :] = consFlux(idir, coord_type, gamma,
847+
idens, ixmom, iymom, iener, irhoX, nspec,
838848
U_l[i, j, :])
839849

840850
# correct the flux
@@ -844,16 +854,83 @@ def riemann_hllc(idir, ng,
844854
# L region
845855
U_state[:] = U_l[i, j, :]
846856

847-
F[i, j, :] = consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec,
857+
F[i, j, :] = consFlux(idir, coord_type, gamma,
858+
idens, ixmom, iymom, iener, irhoX, nspec,
848859
U_state)
849860

850861
# we should deal with solid boundaries somehow here
851862

852863
return F
853864

854865

866+
def riemann_flux(idir, U_l, U_r, my_data, rp, ivars,
867+
lower_solid, upper_solid, tc):
868+
"""
869+
This is the general interface that constructs the unsplit fluxes through
870+
the x and y interfaces using the left and right conserved states by
871+
using the riemann solver.
872+
873+
Parameters
874+
----------
875+
U_l, U_r: ndarray, ndarray
876+
Conserved states in the left and right interface
877+
my_data : CellCenterData2d object
878+
The data object containing the grid and advective scalar that
879+
we are advecting.
880+
rp : RuntimeParameters object
881+
The runtime parameters for the simulation
882+
ivars : Variables object
883+
The Variables object that tells us which indices refer to which
884+
variables
885+
lower_solid, upper_solid : int
886+
Are we at lower or upper solid boundaries?
887+
tc : TimerCollection object
888+
The timers we are using to profile
889+
890+
Returns
891+
-------
892+
out : ndarray, ndarray
893+
Fluxes in x and y direction
894+
"""
895+
896+
tm_riem = tc.timer("riemann")
897+
tm_riem.begin()
898+
899+
myg = my_data.grid
900+
901+
riemann_method = rp.get_param("compressible.riemann")
902+
gamma = rp.get_param("eos.gamma")
903+
904+
riemann_solvers = {"HLLC": riemann_hllc, "CGF": riemann_cgf}
905+
906+
if riemann_method not in riemann_solvers:
907+
msg.fail("ERROR: Riemann solver undefined")
908+
909+
riemannFunc = riemann_solvers[riemann_method]
910+
911+
_f = riemannFunc(idir, myg.ng,
912+
ivars.idens, ivars.ixmom, ivars.iymom,
913+
ivars.iener, ivars.irhox, ivars.naux,
914+
lower_solid, upper_solid,
915+
gamma, U_l, U_r)
916+
917+
# If riemann_method is not HLLC, then it outputs interface conserved states
918+
if riemann_method != "HLLC":
919+
_f = consFlux(idir, myg.coord_type, gamma,
920+
ivars.idens, ivars.ixmom, ivars.iymom,
921+
ivars.iener, ivars.irhox, ivars.naux,
922+
_f)
923+
924+
F = ai.ArrayIndexer(d=_f, grid=myg)
925+
tm_riem.end()
926+
927+
return F
928+
929+
855930
@njit(cache=True)
856-
def consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec, U_state):
931+
def consFlux(idir, coord_type, gamma,
932+
idens, ixmom, iymom, iener, irhoX, nspec,
933+
U_state):
857934
r"""
858935
Calculate the conservative flux.
859936
@@ -879,27 +956,37 @@ def consFlux(idir, gamma, idens, ixmom, iymom, iener, irhoX, nspec, U_state):
879956

880957
F = np.zeros_like(U_state)
881958

882-
u = U_state[ixmom] / U_state[idens]
883-
v = U_state[iymom] / U_state[idens]
959+
u = U_state[..., ixmom] / U_state[..., idens]
960+
v = U_state[..., iymom] / U_state[..., idens]
884961

885-
p = (U_state[iener] - 0.5 * U_state[idens] * (u * u + v * v)) * (gamma - 1.0)
962+
p = (U_state[..., iener] - 0.5 * U_state[..., idens] * (u * u + v * v)) * (gamma - 1.0)
886963

887964
if idir == 1:
888-
F[idens] = U_state[idens] * u
889-
F[ixmom] = U_state[ixmom] * u + p
890-
F[iymom] = U_state[iymom] * u
891-
F[iener] = (U_state[iener] + p) * u
965+
F[..., idens] = U_state[..., idens] * u
966+
F[..., ixmom] = U_state[..., ixmom] * u
967+
968+
# if Cartesian2d, then add pressure to xmom flux
969+
if coord_type == 0:
970+
F[..., ixmom] += p
971+
972+
F[..., iymom] = U_state[..., iymom] * u
973+
F[..., iener] = (U_state[..., iener] + p) * u
892974

893975
if nspec > 0:
894-
F[irhoX:irhoX + nspec] = U_state[irhoX:irhoX + nspec] * u
976+
F[..., irhoX:irhoX + nspec] = U_state[..., irhoX:irhoX + nspec] * u
895977

896978
else:
897-
F[idens] = U_state[idens] * v
898-
F[ixmom] = U_state[ixmom] * v
899-
F[iymom] = U_state[iymom] * v + p
900-
F[iener] = (U_state[iener] + p) * v
979+
F[..., idens] = U_state[..., idens] * v
980+
F[..., ixmom] = U_state[..., ixmom] * v
981+
F[..., iymom] = U_state[..., iymom] * v
982+
983+
# if Cartesian2d, then add pressure to ymom flux
984+
if coord_type == 0:
985+
F[..., iymom] += p
986+
987+
F[..., iener] = (U_state[..., iener] + p) * v
901988

902989
if nspec > 0:
903-
F[irhoX:irhoX + nspec] = U_state[irhoX:irhoX + nspec] * v
990+
F[..., irhoX:irhoX + nspec] = U_state[..., irhoX:irhoX + nspec] * v
904991

905992
return F

pyro/compressible/unsplit_fluxes.py

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@
127127
import pyro.mesh.array_indexer as ai
128128
from pyro.compressible import riemann
129129
from pyro.mesh import reconstruction
130-
from pyro.util import msg
131130

132131

133132
def unsplit_fluxes(my_data, my_aux, rp, ivars, solid, tc, dt):
@@ -283,33 +282,15 @@ def unsplit_fluxes(my_data, my_aux, rp, ivars, solid, tc, dt):
283282
# =========================================================================
284283
# compute transverse fluxes
285284
# =========================================================================
286-
tm_riem = tc.timer("riemann")
287-
tm_riem.begin()
288285

289-
riemann_method = rp.get_param("compressible.riemann")
286+
# Get the flux through x, y interface via riemann solver
287+
F_x = riemann.riemann_flux(1, U_xl, U_xr,
288+
my_data, rp, ivars,
289+
solid.xl, solid.xr, tc)
290290

291-
riemannFunc = None
292-
if riemann_method == "HLLC":
293-
riemannFunc = riemann.riemann_hllc
294-
elif riemann_method == "CGF":
295-
riemannFunc = riemann.riemann_cgf
296-
else:
297-
msg.fail("ERROR: Riemann solver undefined")
298-
299-
_fx = riemannFunc(1, myg.ng,
300-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
301-
solid.xl, solid.xr,
302-
gamma, U_xl, U_xr)
303-
304-
_fy = riemannFunc(2, myg.ng,
305-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
306-
solid.yl, solid.yr,
307-
gamma, U_yl, U_yr)
308-
309-
F_x = ai.ArrayIndexer(d=_fx, grid=myg)
310-
F_y = ai.ArrayIndexer(d=_fy, grid=myg)
311-
312-
tm_riem.end()
291+
F_y = riemann.riemann_flux(2, U_yl, U_yr,
292+
my_data, rp, ivars,
293+
solid.yl, solid.yr, tc)
313294

314295
# =========================================================================
315296
# construct the interface values of U now
@@ -392,23 +373,13 @@ def unsplit_fluxes(my_data, my_aux, rp, ivars, solid, tc, dt):
392373

393374
# up until now, F_x and F_y stored the transverse fluxes, now we
394375
# overwrite with the fluxes normal to the interfaces
376+
F_x = riemann.riemann_flux(1, U_xl, U_xr,
377+
my_data, rp, ivars,
378+
solid.xl, solid.xr, tc)
395379

396-
tm_riem.begin()
397-
398-
_fx = riemannFunc(1, myg.ng,
399-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
400-
solid.xl, solid.xr,
401-
gamma, U_xl, U_xr)
402-
403-
_fy = riemannFunc(2, myg.ng,
404-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
405-
solid.yl, solid.yr,
406-
gamma, U_yl, U_yr)
407-
408-
F_x = ai.ArrayIndexer(d=_fx, grid=myg)
409-
F_y = ai.ArrayIndexer(d=_fy, grid=myg)
410-
411-
tm_riem.end()
380+
F_y = riemann.riemann_flux(2, U_yl, U_yr,
381+
my_data, rp, ivars,
382+
solid.yl, solid.yr, tc)
412383

413384
# =========================================================================
414385
# apply artificial viscosity

pyro/compressible_rk/fluxes.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import pyro.mesh.array_indexer as ai
2424
from pyro.compressible import interface, riemann
2525
from pyro.mesh import reconstruction
26-
from pyro.util import msg
2726

2827

2928
def fluxes(my_data, rp, ivars, solid, tc):
@@ -161,33 +160,13 @@ def fluxes(my_data, rp, ivars, solid, tc):
161160
# =========================================================================
162161
# construct the fluxes normal to the interfaces
163162
# =========================================================================
164-
tm_riem = tc.timer("Riemann")
165-
tm_riem.begin()
163+
F_x = riemann.riemann_flux(1, U_xl, U_xr,
164+
my_data, rp, ivars,
165+
solid.xl, solid.xr, tc)
166166

167-
riemann_method = rp.get_param("compressible.riemann")
168-
169-
riemannFunc = None
170-
if riemann_method == "HLLC":
171-
riemannFunc = riemann.riemann_hllc
172-
elif riemann_method == "CGF":
173-
riemannFunc = riemann.riemann_cgf
174-
else:
175-
msg.fail("ERROR: Riemann solver undefined")
176-
177-
_fx = riemannFunc(1, myg.ng,
178-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
179-
solid.xl, solid.xr,
180-
gamma, U_xl, U_xr)
181-
182-
_fy = riemannFunc(2, myg.ng,
183-
ivars.idens, ivars.ixmom, ivars.iymom, ivars.iener, ivars.irhox, ivars.naux,
184-
solid.yl, solid.yr,
185-
gamma, U_yl, U_yr)
186-
187-
F_x = ai.ArrayIndexer(d=_fx, grid=myg)
188-
F_y = ai.ArrayIndexer(d=_fy, grid=myg)
189-
190-
tm_riem.end()
167+
F_y = riemann.riemann_flux(2, U_yl, U_yr,
168+
my_data, rp, ivars,
169+
solid.yl, solid.yr, tc)
191170

192171
# =========================================================================
193172
# apply artificial viscosity

0 commit comments

Comments
 (0)