Skip to content

Commit d1b8a5c

Browse files
committed
add norm and restrict methods and a convergence plot
1 parent 43da028 commit d1b8a5c

File tree

3 files changed

+111
-35
lines changed

3 files changed

+111
-35
lines changed

multiphysics/burgersvisc.py

+56-34
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def scratch_array(self):
4343
return np.zeros((2*self.ng+self.nx), dtype=np.float64)
4444

4545

46+
def norm(self, e):
47+
return np.sqrt(self.dx*np.sum(e[self.ilo:self.ihi+1]**2))
48+
49+
4650
def fill_BCs(self, var):
4751

4852
if not var in self.data.keys():
@@ -54,6 +58,21 @@ def fill_BCs(self, var):
5458
vp[0:self.ilo+1] = vp[self.ilo]
5559
vp[self.ihi+1:] = vp[self.ihi]
5660

61+
def restrict(self, fac=2):
62+
""" restrict the data q that lives on this grid by a
63+
factor fac and return the new data"""
64+
65+
# we require fac to be a multiple of 2
66+
67+
gnew = Grid1d(self.nx//fac, ng=self.ng,
68+
xmin=self.xmin, xmax=self.xmax, vars=self.data.keys())
69+
70+
for v in self.data:
71+
b = self.data[v][self.ilo:self.ihi+1]
72+
gnew.data[v][gnew.ilo:gnew.ihi+1] = \
73+
np.mean(b.reshape(-1, fac), axis=1)
74+
return gnew
75+
5776

5877
class Simulation(object):
5978

@@ -120,7 +139,7 @@ def diffuse(self, eps, S, dt):
120139

121140
return unew
122141

123-
def advect(self, S, dt):
142+
def advect(self, S, dt, limit=1):
124143
""" compute the advective term that updates u in time. Here, S is
125144
a source term """
126145

@@ -143,8 +162,11 @@ def advect(self, S, dt):
143162
dl[ib:ie+1] = np.fabs(u[ib+1:ie+2] - u[ib :ie+1])
144163
dr[ib:ie+1] = np.fabs(u[ib :ie+1] - u[ib-1:ie ])
145164

146-
minslope = np.minimum(np.fabs(dc), np.minimum(2.0*dl, 2.0*dr))
147-
ldeltau = np.where(test > 0.0, minslope, 0.0)*np.sign(dc)
165+
if limit:
166+
minslope = np.minimum(np.fabs(dc), np.minimum(2.0*dl, 2.0*dr))
167+
ldeltau = np.where(test > 0.0, minslope, 0.0)*np.sign(dc)
168+
else:
169+
ldeltau = dc
148170

149171
# construct the interface states, to second order in space and
150172
# time
@@ -193,7 +215,7 @@ def lap(self):
193215

194216
return lapu
195217

196-
def evolve(self, eps, cfl, tmax, dovis=0):
218+
def evolve(self, eps, cfl, tmax, limit=1, dovis=0):
197219
"""
198220
the main evolution loop. Evolve
199221
@@ -225,7 +247,7 @@ def evolve(self, eps, cfl, tmax, dovis=0):
225247
S = eps*self.lap()
226248

227249
# construct the advective update
228-
A = self.advect(S, dt)
250+
A = self.advect(S, dt, limit=limit)
229251

230252
# diffuse for dt
231253
unew = self.diffuse(eps, A, dt)
@@ -247,43 +269,43 @@ def evolve(self, eps, cfl, tmax, dovis=0):
247269
plt.draw()
248270

249271

250-
nx = 256
251-
cfl = 0.8
252-
tmax = 0.2
253-
254-
g1 = Grid1d(nx, ng=2, vars=["u"])
255-
g2 = Grid1d(nx, ng=2, vars=["u"])
256-
g3 = Grid1d(nx, ng=2, vars=["u"])
257272

258-
eps1 = 0.005
259-
s1 = Simulation(g1)
260-
s1.init_cond()
261-
s1.evolve(eps1, cfl, tmax, dovis=0)
273+
if __name__ == "__main__":
274+
nx = 256
275+
cfl = 0.8
276+
tmax = 0.2
262277

278+
g1 = Grid1d(nx, ng=2, vars=["u"])
279+
g2 = Grid1d(nx, ng=2, vars=["u"])
280+
g3 = Grid1d(nx, ng=2, vars=["u"])
263281

264-
eps2 = 0.0005
265-
s2 = Simulation(g2)
266-
s2.init_cond()
267-
s2.evolve(eps2, cfl, tmax, dovis=0)
282+
eps1 = 0.005
283+
s1 = Simulation(g1)
284+
s1.init_cond()
285+
s1.evolve(eps1, cfl, tmax, dovis=0)
268286

287+
eps2 = 0.0005
288+
s2 = Simulation(g2)
289+
s2.init_cond()
290+
s2.evolve(eps2, cfl, tmax, dovis=0)
269291

270-
eps3 = 0.00005
271-
s3 = Simulation(g3)
272-
s3.init_cond()
273-
s3.evolve(eps3, cfl, tmax, dovis=0)
292+
eps3 = 0.00005
293+
s3 = Simulation(g3)
294+
s3.init_cond()
295+
s3.evolve(eps3, cfl, tmax, dovis=0)
274296

275-
u1 = s1.grid.data["u"]
276-
plt.plot(g1.x, u1, label=r"$\epsilon = %f$" % (eps1))
297+
u1 = s1.grid.data["u"]
298+
plt.plot(g1.x, u1, label=r"$\epsilon = %f$" % (eps1))
277299

278-
u2 = s2.grid.data["u"]
279-
plt.plot(g2.x, u2, label=r"$\epsilon = %f$" % (eps2), ls="--", color="k")
300+
u2 = s2.grid.data["u"]
301+
plt.plot(g2.x, u2, label=r"$\epsilon = %f$" % (eps2), ls="--", color="k")
280302

281-
u3 = s3.grid.data["u"]
282-
plt.plot(g3.x, u3, label=r"$\epsilon = %f$" % (eps3), ls=":", color="k")
303+
u3 = s3.grid.data["u"]
304+
plt.plot(g3.x, u3, label=r"$\epsilon = %f$" % (eps3), ls=":", color="k")
283305

284-
plt.legend(frameon=False)
306+
plt.legend(frameon=False)
285307

286-
plt.xlim(0.0, 1.0)
308+
plt.xlim(0.0, 1.0)
287309

288-
plt.tight_layout()
289-
plt.savefig("burgersvisc.pdf")
310+
plt.tight_layout()
311+
plt.savefig("burgersvisc.pdf")

multiphysics/burgersvisc_converge.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# test convergence of our inviscid Burgers problem by comparing against
2+
# a high-resolution test case
3+
4+
import burgersvisc as bv
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
cfl = 0.8
9+
tmax = 0.2
10+
eps = 0.005
11+
12+
# high-resolution benchmark
13+
nxb = 4096
14+
gb = bv.Grid1d(nxb, ng=2, vars=["u"])
15+
16+
sb = bv.Simulation(gb)
17+
sb.init_cond()
18+
sb.evolve(eps, cfl, tmax, dovis=0)
19+
20+
21+
# comparison simulations
22+
N = [64, 128, 256, 512, 1024]
23+
24+
err = []
25+
for nx in N:
26+
# our comparison -- a coarsen version of the high-resolution
27+
# solution
28+
gc = gb.restrict(fac=nxb//nx)
29+
30+
g = bv.Grid1d(nx, ng=2, vars=["u"])
31+
s = bv.Simulation(g)
32+
s.init_cond()
33+
s.evolve(eps, cfl, tmax, dovis=0)
34+
35+
uc = gc.data["u"]
36+
u = g.data["u"]
37+
38+
e = uc - u
39+
err.append(g.norm(e))
40+
41+
N = np.array(N, dtype=np.float64)
42+
err = np.array(err)
43+
44+
plt.scatter(N, err, color="C1", label="Burgers' solution")
45+
plt.loglog(N, err[len(N)-1]*(N[len(N)-1]/N)**2, color="C0",
46+
label="$\mathcal{O}(\Delta x^2)$")
47+
48+
plt.xlabel(r"$N$", fontsize="large")
49+
plt.ylabel(r"L2 norm of absolute error")
50+
51+
plt.legend(frameon=False)
52+
53+
plt.tight_layout()
54+
plt.savefig("burgersvisc_converge.pdf")

multiphysics/diffusion-reaction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def evolve(nx, kappa, tau, tmax, dovis=0, return_initial=0):
280280

281281
plt.xlabel("$x$")
282282
plt.ylabel("$\phi$")
283-
plt.title(r"Diffusion-Reaction, $N = {}, \, \kappa = {:3.2f}, \, \tau = {:3.2f}$".format(nx, kappa, tau, dt))
283+
#plt.title(r"Diffusion-Reaction, $N = {}, \, \kappa = {:3.2f}, \, \tau = {:3.2f}$".format(nx, kappa, tau, dt))
284284
#, \, \delta t = {:3.2f}$ (between lines)
285285
plt.tight_layout()
286286

0 commit comments

Comments
 (0)