Skip to content

Commit c407ace

Browse files
committed
Made it a little more pythonic
1 parent a215408 commit c407ace

4 files changed

Lines changed: 134 additions & 149 deletions

File tree

compressible/interface.py

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ def states(idir, qx, qy, ng, dx, dt,
108108
dtdx = dt / dx
109109
dtdx4 = 0.25 * dtdx
110110

111-
dq = np.zeros(nvar)
112-
q = np.zeros(nvar)
113111
lvec = np.zeros((nvar, nvar))
114112
rvec = np.zeros((nvar, nvar))
115113
e_val = np.zeros(nvar)
@@ -120,8 +118,8 @@ def states(idir, qx, qy, ng, dx, dt,
120118
for j in range(jlo - 2, jhi + 2):
121119
for i in range(ilo - 2, ihi + 2):
122120

123-
dq[:] = dqv[i, j, :]
124-
q[:] = qv[i, j, :]
121+
dq = dqv[i, j, :]
122+
q = qv[i, j, :]
125123

126124
cs = np.sqrt(gamma * q[ip] / q[irho])
127125

@@ -133,18 +131,18 @@ def states(idir, qx, qy, ng, dx, dt,
133131
if (idir == 1):
134132
e_val[:] = np.array([q[iu] - cs, q[iu], q[iu], q[iu] + cs])
135133

136-
lvec[0, 0:ns] = [0.0, -0.5 *
134+
lvec[0, :ns] = [0.0, -0.5 *
137135
q[irho] / cs, 0.0, 0.5 / (cs * cs)]
138-
lvec[1, 0:ns] = [1.0, 0.0,
136+
lvec[1, :ns] = [1.0, 0.0,
139137
0.0, -1.0 / (cs * cs)]
140-
lvec[2, 0:ns] = [0.0, 0.0, 1.0, 0.0]
141-
lvec[3, 0:ns] = [0.0, 0.5 *
138+
lvec[2, :ns] = [0.0, 0.0, 1.0, 0.0]
139+
lvec[3, :ns] = [0.0, 0.5 *
142140
q[irho] / cs, 0.0, 0.5 / (cs * cs)]
143141

144-
rvec[0, 0:ns] = [1.0, -cs / q[irho], 0.0, cs * cs]
145-
rvec[1, 0:ns] = [1.0, 0.0, 0.0, 0.0]
146-
rvec[2, 0:ns] = [0.0, 0.0, 1.0, 0.0]
147-
rvec[3, 0:ns] = [1.0, cs / q[irho], 0.0, cs * cs]
142+
rvec[0, :ns] = [1.0, -cs / q[irho], 0.0, cs * cs]
143+
rvec[1, :ns] = [1.0, 0.0, 0.0, 0.0]
144+
rvec[2, :ns] = [0.0, 0.0, 1.0, 0.0]
145+
rvec[3, :ns] = [1.0, cs / q[irho], 0.0, cs * cs]
148146

149147
# now the species -- they only have a 1 in their corresponding slot
150148
e_val[ns:] = q[iu]
@@ -153,20 +151,20 @@ def states(idir, qx, qy, ng, dx, dt,
153151
rvec[n, n] = 1.0
154152

155153
else:
156-
e_val = np.array([q[iv] - cs, q[iv], q[iv], q[iv] + cs])
154+
e_val[:] = np.array([q[iv] - cs, q[iv], q[iv], q[iv] + cs])
157155

158-
lvec[0, 0:ns] = [0.0, 0.0, -0.5 *
156+
lvec[0, :ns] = [0.0, 0.0, -0.5 *
159157
q[irho] / cs, 0.5 / (cs * cs)]
160-
lvec[1, 0:ns] = [1.0, 0.0,
158+
lvec[1, :ns] = [1.0, 0.0,
161159
0.0, -1.0 / (cs * cs)]
162-
lvec[2, 0:ns] = [0.0, 1.0, 0.0, 0.0]
163-
lvec[3, 0:ns] = [0.0, 0.0, 0.5 *
160+
lvec[2, :ns] = [0.0, 1.0, 0.0, 0.0]
161+
lvec[3, :ns] = [0.0, 0.0, 0.5 *
164162
q[irho] / cs, 0.5 / (cs * cs)]
165163

166-
rvec[0, 0:ns] = [1.0, 0.0, -cs / q[irho], cs * cs]
167-
rvec[1, 0:ns] = [1.0, 0.0, 0.0, 0.0]
168-
rvec[2, 0:ns] = [0.0, 1.0, 0.0, 0.0]
169-
rvec[3, 0:ns] = [1.0, 0.0, cs / q[irho], cs * cs]
164+
rvec[0, :ns] = [1.0, 0.0, -cs / q[irho], cs * cs]
165+
rvec[1, :ns] = [1.0, 0.0, 0.0, 0.0]
166+
rvec[2, :ns] = [0.0, 1.0, 0.0, 0.0]
167+
rvec[3, :ns] = [1.0, 0.0, cs / q[irho], cs * cs]
170168

171169
# now the species -- they only have a 1 in their corresponding slot
172170
e_val[ns:] = q[iv]
@@ -179,20 +177,20 @@ def states(idir, qx, qy, ng, dx, dt,
179177
# this is one the right face of the current zone,
180178
# so the fastest moving eigenvalue is e_val[3] = u + c
181179
factor = 0.5 * (1.0 - dtdx * max(e_val[3], 0.0))
182-
q_l[i + 1, j, :] = q[:] + factor * dq[:]
180+
q_l[i + 1, j, :] = q + factor * dq
183181

184182
# left face of the current zone, so the fastest moving
185183
# eigenvalue is e_val[3] = u - c
186184
factor = 0.5 * (1.0 + dtdx * min(e_val[0], 0.0))
187-
q_r[i, j, :] = q[:] - factor * dq[:]
185+
q_r[i, j, :] = q - factor * dq
188186

189187
else:
190188

191189
factor = 0.5 * (1.0 - dtdx * max(e_val[3], 0.0))
192-
q_l[i, j + 1, :] = q[:] + factor * dq[:]
190+
q_l[i, j + 1, :] = q + factor * dq
193191

194192
factor = 0.5 * (1.0 + dtdx * min(e_val[0], 0.0))
195-
q_r[i, j, :] = q[:] - factor * dq[:]
193+
q_r[i, j, :] = q - factor * dq
196194

197195
# compute the Vhat functions
198196
for m in range(nvar):
@@ -285,8 +283,6 @@ def riemann_cgf(idir, qx, qy, ng,
285283
smallrho = 1.e-10
286284
smallp = 1.e-10
287285

288-
xn = np.zeros(nspec)
289-
290286
nx = qx - 2 * ng
291287
ny = qy - 2 * ng
292288
ilo = ng
@@ -483,12 +479,12 @@ def riemann_cgf(idir, qx, qy, ng,
483479
# species now
484480
if (nspec > 0):
485481
if (ustar > 0.0):
486-
xn[:] = U_l[i, j, irhoX:irhoX + nspec] / U_l[i, j, idens]
482+
xn = U_l[i, j, irhoX:irhoX + nspec] / U_l[i, j, idens]
487483

488484
elif (ustar < 0.0):
489-
xn[:] = U_r[i, j, irhoX:irhoX + nspec] / U_r[i, j, idens]
485+
xn = U_r[i, j, irhoX:irhoX + nspec] / U_r[i, j, idens]
490486
else:
491-
xn[:] = 0.5 * (U_l[i, j, irhoX:irhoX + nspec] / U_l[i, j, idens] +
487+
xn = 0.5 * (U_l[i, j, irhoX:irhoX + nspec] / U_l[i, j, idens] +
492488
U_r[i, j, irhoX:irhoX + nspec] / U_r[i, j, idens])
493489

494490
# are we on a solid boundary?
@@ -521,7 +517,7 @@ def riemann_cgf(idir, qx, qy, ng,
521517
p_state * un_state
522518

523519
if (nspec > 0):
524-
F[i, j, irhoX:irhoX + nspec] = xn[:] * rho_state * un_state
520+
F[i, j, irhoX:irhoX + nspec] = xn * rho_state * un_state
525521

526522
return F
527523

@@ -596,8 +592,6 @@ def riemann_prim(idir, qx, qy, ng,
596592
smallrho = 1.e-10
597593
smallp = 1.e-10
598594

599-
xn = np.zeros(nspec)
600-
601595
nx = qx - 2 * ng
602596
ny = qy - 2 * ng
603597
ilo = ng
@@ -774,12 +768,12 @@ def riemann_prim(idir, qx, qy, ng,
774768
# species now
775769
if (nspec > 0):
776770
if (ustar > 0.0):
777-
xn[:] = q_l[i, j, iX:iX + nspec]
771+
xn = q_l[i, j, iX:iX + nspec]
778772

779773
elif (ustar < 0.0):
780-
xn[:] = q_r[i, j, iX:iX + nspec]
774+
xn = q_r[i, j, iX:iX + nspec]
781775
else:
782-
xn[:] = 0.5 * (q_l[i, j, iX:iX + nspec] +
776+
xn = 0.5 * (q_l[i, j, iX:iX + nspec] +
783777
q_r[i, j, iX:iX + nspec])
784778

785779
# are we on a solid boundary?
@@ -808,7 +802,7 @@ def riemann_prim(idir, qx, qy, ng,
808802
q_int[i, j, ip] = p_state
809803

810804
if (nspec > 0):
811-
q_int[i, j, iX:iX + nspec] = xn[:]
805+
q_int[i, j, iX:iX + nspec] = xn
812806

813807
return q_int
814808

incompressible/incomp_interface.py

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def mac_vels(qx, qy, ng, dx, dy, dt,
3636
MAC velocities in the x and y directions
3737
"""
3838

39-
u_MAC = np.zeros((qx, qy))
40-
v_MAC = np.zeros((qx, qy))
41-
4239
# get the full u and v left and right states (including transverse
4340
# terms) on both the x- and y-interfaces
4441
u_xl, u_xr, u_yl, u_yr, v_xl, v_xr, v_yl, v_yr = get_interface_states(qx, qy, ng, dx, dy, dt,
@@ -50,8 +47,8 @@ def mac_vels(qx, qy, ng, dx, dy, dt,
5047
# Riemann problem -- this follows Burger's equation. We don't use
5148
# any input velocity for the upwinding. Also, we only care about
5249
# the normal states here (u on x and v on y)
53-
riemann_and_upwind(qx, qy, ng, u_xl, u_xr, u_MAC)
54-
riemann_and_upwind(qx, qy, ng, v_yl, v_yr, v_MAC)
50+
u_MAC = riemann_and_upwind(qx, qy, ng, u_xl, u_xr)
51+
v_MAC = riemann_and_upwind(qx, qy, ng, v_yl, v_yr)
5552

5653
return u_MAC, v_MAC
5754

@@ -96,11 +93,6 @@ def states(qx, qy, ng, dx, dy, dt,
9693
x and y velocities predicted to the interfaces
9794
"""
9895

99-
u_xint = np.zeros((qx, qy))
100-
u_yint = np.zeros((qx, qy))
101-
v_xint = np.zeros((qx, qy))
102-
v_yint = np.zeros((qx, qy))
103-
10496
# get the full u and v left and right states (including transverse
10597
# terms) on both the x- and y-interfaces
10698
u_xl, u_xr, u_yl, u_yr, v_xl, v_xr, v_yl, v_yr = get_interface_states(qx, qy, ng, dx, dy, dt,
@@ -111,10 +103,10 @@ def states(qx, qy, ng, dx, dy, dt,
111103

112104
# upwind using the MAC velocity to determine which state exists on
113105
# the interface
114-
upwind(qx, qy, ng, u_xl, u_xr, u_MAC, u_xint)
115-
upwind(qx, qy, ng, v_xl, v_xr, u_MAC, v_xint)
116-
upwind(qx, qy, ng, u_yl, u_yr, v_MAC, u_yint)
117-
upwind(qx, qy, ng, v_yl, v_yr, v_MAC, v_yint)
106+
u_xint = upwind(qx, qy, ng, u_xl, u_xr, u_MAC)
107+
v_xint = upwind(qx, qy, ng, v_xl, v_xr, u_MAC)
108+
u_yint = upwind(qx, qy, ng, u_yl, u_yr, v_MAC)
109+
v_yint = upwind(qx, qy, ng, v_yl, v_yr, v_MAC)
118110

119111
return u_xint, u_yint, v_xint, v_yint
120112

@@ -166,14 +158,6 @@ def get_interface_states(qx, qy, ng, dx, dy, dt,
166158
v_yl = np.zeros((qx, qy))
167159
v_yr = np.zeros((qx, qy))
168160

169-
uhat_adv = np.zeros((qx, qy))
170-
vhat_adv = np.zeros((qx, qy))
171-
172-
u_xint = np.zeros((qx, qy))
173-
u_yint = np.zeros((qx, qy))
174-
v_xint = np.zeros((qx, qy))
175-
v_yint = np.zeros((qx, qy))
176-
177161
nx = qx - 2 * ng
178162
ny = qy - 2 * ng
179163
ilo = ng
@@ -216,19 +200,19 @@ def get_interface_states(qx, qy, ng, dx, dy, dt,
216200

217201
# now get the normal advective velocities on the interfaces by solving
218202
# the Riemann problem.
219-
riemann(qx, qy, ng, u_xl, u_xr, uhat_adv)
220-
riemann(qx, qy, ng, v_yl, v_yr, vhat_adv)
203+
uhat_adv = riemann(qx, qy, ng, u_xl, u_xr)
204+
vhat_adv = riemann(qx, qy, ng, v_yl, v_yr)
221205

222206
# now that we have the advective velocities, upwind the left and right
223207
# states using the appropriate advective velocity.
224208

225209
# on the x-interfaces, we upwind based on uhat_adv
226-
upwind(qx, qy, ng, u_xl, u_xr, uhat_adv, u_xint)
227-
upwind(qx, qy, ng, v_xl, v_xr, uhat_adv, v_xint)
210+
u_xint = upwind(qx, qy, ng, u_xl, u_xr, uhat_adv)
211+
v_xint = upwind(qx, qy, ng, v_xl, v_xr, uhat_adv)
228212

229213
# on the y-interfaces, we upwind based on vhat_adv
230-
upwind(qx, qy, ng, u_yl, u_yr, vhat_adv, u_yint)
231-
upwind(qx, qy, ng, v_yl, v_yr, vhat_adv, v_yint)
214+
u_yint = upwind(qx, qy, ng, u_yl, u_yr, vhat_adv)
215+
v_yint = upwind(qx, qy, ng, v_yl, v_yr, vhat_adv)
232216

233217
# at this point, these states are the `hat' states -- they only
234218
# considered the normal to the interface portion of the predictor.
@@ -277,7 +261,7 @@ def get_interface_states(qx, qy, ng, dx, dy, dt,
277261

278262
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
279263
@njit(cache=True)
280-
def upwind(qx, qy, ng, q_l, q_r, s, q_int):
264+
def upwind(qx, qy, ng, q_l, q_r, s):
281265
r"""
282266
upwind the left and right states based on the specified input
283267
velocity, s. The resulting interface state is q_int
@@ -292,7 +276,10 @@ def upwind(qx, qy, ng, q_l, q_r, s, q_int):
292276
left and right states
293277
s : ndarray
294278
velocity
295-
q_int : ndarray
279+
280+
Returns
281+
-------
282+
out : ndarray
296283
Upwinded state
297284
"""
298285

@@ -303,6 +290,8 @@ def upwind(qx, qy, ng, q_l, q_r, s, q_int):
303290
jlo = ng
304291
jhi = ng + ny
305292

293+
q_int = np.zeros((qx, qy))
294+
306295
for j in range(jlo - 1, jhi + 1):
307296
for i in range(ilo - 1, ihi + 1):
308297

@@ -313,10 +302,12 @@ def upwind(qx, qy, ng, q_l, q_r, s, q_int):
313302
else:
314303
q_int[i, j] = q_r[i, j]
315304

305+
return q_int
306+
316307

317308
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
318309
@njit(cache=True)
319-
def riemann(qx, qy, ng, q_l, q_r, s):
310+
def riemann(qx, qy, ng, q_l, q_r):
320311
"""
321312
Solve the Burger's Riemann problem given the input left and right
322313
states and return the state on the interface.
@@ -331,7 +322,10 @@ def riemann(qx, qy, ng, q_l, q_r, s):
331322
The number of ghost cells
332323
q_l, q_r : ndarray
333324
left and right states
334-
s : ndarray
325+
326+
Returns
327+
-------
328+
out : ndarray
335329
Interface state
336330
"""
337331

@@ -342,6 +336,8 @@ def riemann(qx, qy, ng, q_l, q_r, s):
342336
jlo = ng
343337
jhi = ng + ny
344338

339+
s = np.zeros((qx, qy))
340+
345341
for j in range(jlo - 1, jhi + 1):
346342
for i in range(ilo - 1, ihi + 1):
347343

@@ -352,10 +348,12 @@ def riemann(qx, qy, ng, q_l, q_r, s):
352348
else:
353349
s[i, j] = q_r[i, j]
354350

351+
return s
352+
355353

356354
# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
357355
@njit(cache=True)
358-
def riemann_and_upwind(qx, qy, ng, q_l, q_r, q_int):
356+
def riemann_and_upwind(qx, qy, ng, q_l, q_r):
359357
r"""
360358
First solve the Riemann problem given q_l and q_r to give the
361359
velocity on the interface and: use this velocity to upwind to
@@ -372,11 +370,12 @@ def riemann_and_upwind(qx, qy, ng, q_l, q_r, q_int):
372370
The number of ghost cells
373371
q_l, q_r : ndarray
374372
left and right states
375-
q_int : ndarray
373+
374+
Returns
375+
-------
376+
out : ndarray
376377
Upwinded state
377378
"""
378379

379-
s = np.zeros((qx, qy))
380-
381-
riemann(qx, qy, ng, q_l, q_r, s)
382-
upwind(qx, qy, ng, q_l, q_r, s, q_int)
380+
s = riemann(qx, qy, ng, q_l, q_r)
381+
return upwind(qx, qy, ng, q_l, q_r, s)

0 commit comments

Comments
 (0)