@@ -70,6 +70,14 @@ def fill_BCs(self):
7070 sys .exit ("invalid BC" )
7171
7272
73+ def norm (self , e ):
74+ """ return the norm of quantity e which lives on the grid """
75+ if len (e ) != 2 * self .ng + self .nx :
76+ return None
77+
78+ return np .sqrt (self .dx * np .sum (e [self .ilo :self .ihi + 1 ]** 2 ))
79+
80+
7381class Simulation (object ):
7482
7583 def __init__ (self , grid ):
@@ -105,21 +113,22 @@ def timestep(self, C):
105113 def states (self , dt ):
106114 """ compute the left and right interface states """
107115
116+ g = self .grid
108117 # compute the piecewise linear slopes -- 2nd order MC limiter
109118 # we pick a range of cells that includes 1 ghost cell on either
110119 # side
111- ib = self . grid .ilo - 1
112- ie = self . grid .ihi + 1
120+ ib = g .ilo - 1
121+ ie = g .ihi + 1
113122
114- u = self . grid .u
123+ u = g .u
115124
116125 # this is the MC limiter from van Leer (1977), as given in
117126 # LeVeque (2002). Note that this is slightly different than
118127 # the expression from Colella (1990)
119128
120- dc = self . grid .scratch_array ()
121- dl = self . grid .scratch_array ()
122- dr = self . grid .scratch_array ()
129+ dc = g .scratch_array ()
130+ dl = g .scratch_array ()
131+ dr = g .scratch_array ()
123132
124133 dc [ib :ie + 1 ] = 0.5 * (u [ib + 1 :ie + 2 ] - u [ib - 1 :ie ])
125134 dl [ib :ie + 1 ] = u [ib + 1 :ie + 2 ] - u [ib :ie + 1 ]
@@ -185,6 +194,8 @@ def evolve(self, C, tmax):
185194
186195 self .t = 0.0
187196
197+ g = self .grid
198+
188199 # main evolution loop
189200 while (self .t < tmax ):
190201
@@ -211,80 +222,80 @@ def evolve(self, C, tmax):
211222 self .t += dt
212223
213224
225+ if __name__ == "__main__" :
226+ #-----------------------------------------------------------------------------
227+ # sine
214228
215- #-----------------------------------------------------------------------------
216- # sine
217-
218- xmin = 0.0
219- xmax = 1.0
220- nx = 256
221- ng = 2
222- g = Grid1d (nx , ng , bc = "periodic" )
229+ xmin = 0.0
230+ xmax = 1.0
231+ nx = 256
232+ ng = 2
233+ g = Grid1d (nx , ng , bc = "periodic" )
223234
224- # maximum evolution time based on period for unit velocity
225- tmax = (xmax - xmin )/ 1.0
235+ # maximum evolution time based on period for unit velocity
236+ tmax = (xmax - xmin )/ 1.0
226237
227- C = 0.8
238+ C = 0.8
228239
229- plt .clf ()
240+ plt .clf ()
230241
231- s = Simulation (g )
242+ s = Simulation (g )
232243
233- for i in range (0 , 10 ):
234- tend = (i + 1 )* 0.02 * tmax
235- s .init_cond ("sine" )
244+ for i in range (0 , 10 ):
245+ tend = (i + 1 )* 0.02 * tmax
246+ s .init_cond ("sine" )
236247
237- uinit = s .grid .u .copy ()
248+ uinit = s .grid .u .copy ()
238249
239- s .evolve (C , tend )
250+ s .evolve (C , tend )
240251
241- c = 1.0 - (0.1 + i * 0.1 )
242- g = s .grid
243- plt .plot (g .x [g .ilo :g .ihi + 1 ], g .u [g .ilo :g .ihi + 1 ], color = str (c ))
252+ c = 1.0 - (0.1 + i * 0.1 )
253+ g = s .grid
254+ plt .plot (g .x [g .ilo :g .ihi + 1 ], g .u [g .ilo :g .ihi + 1 ], color = str (c ))
244255
245256
246- g = s .grid
247- plt .plot (g .x [g .ilo :g .ihi + 1 ], uinit [g .ilo :g .ihi + 1 ], ls = ":" , color = "0.9" , zorder = - 1 )
257+ g = s .grid
258+ plt .plot (g .x [g .ilo :g .ihi + 1 ], uinit [g .ilo :g .ihi + 1 ], ls = ":" , color = "0.9" , zorder = - 1 )
248259
249- plt .xlabel ("$x$" )
250- plt .ylabel ("$u$" )
251- plt .savefig ("fv-burger-sine.pdf" )
260+ plt .xlabel ("$x$" )
261+ plt .ylabel ("$u$" )
262+ plt .savefig ("fv-burger-sine.pdf" )
252263
253264
254- #-----------------------------------------------------------------------------
255- # rarefaction
265+ #-----------------------------------------------------------------------------
266+ # rarefaction
256267
257- xmin = 0.0
258- xmax = 1.0
259- nx = 256
260- ng = 2
261- g = Grid1d (nx , ng , bc = "outflow" )
268+ xmin = 0.0
269+ xmax = 1.0
270+ nx = 256
271+ ng = 2
272+ g = Grid1d (nx , ng , bc = "outflow" )
262273
263- # maximum evolution time based on period for unit velocity
264- tmax = (xmax - xmin )/ 1.0
274+ # maximum evolution time based on period for unit velocity
275+ tmax = (xmax - xmin )/ 1.0
265276
266- C = 0.8
277+ C = 0.8
267278
268- plt .clf ()
279+ plt .clf ()
269280
270- s = Simulation (g )
281+ s = Simulation (g )
271282
272- for i in range (0 , 10 ):
273- tend = (i + 1 )* 0.02 * tmax
283+ for i in range (0 , 10 ):
284+ tend = (i + 1 )* 0.02 * tmax
274285
275- s .init_cond ("rarefaction" )
286+ s .init_cond ("rarefaction" )
276287
277- uinit = s .grid .u .copy ()
288+ uinit = s .grid .u .copy ()
278289
279- s .evolve (C , tend )
290+ s .evolve (C , tend )
280291
281- c = 1.0 - (0.1 + i * 0.1 )
282- plt .plot (g .x [g .ilo :g .ihi + 1 ], g .u [g .ilo :g .ihi + 1 ], color = str (c ))
292+ c = 1.0 - (0.1 + i * 0.1 )
293+ plt .plot (g .x [g .ilo :g .ihi + 1 ], g .u [g .ilo :g .ihi + 1 ], color = str (c ))
283294
284295
285- plt .plot (g .x [g .ilo :g .ihi + 1 ], uinit [g .ilo :g .ihi + 1 ], ls = ":" , color = "0.9" , zorder = - 1 )
296+ plt .plot (g .x [g .ilo :g .ihi + 1 ], uinit [g .ilo :g .ihi + 1 ], ls = ":" , color = "0.9" , zorder = - 1 )
286297
287- plt .xlabel ("$x$" )
288- plt .ylabel ("$u$" )
298+ plt .xlabel ("$x$" )
299+ plt .ylabel ("$u$" )
289300
290- plt .savefig ("fv-burger-rarefaction.pdf" )
301+ plt .savefig ("fv-burger-rarefaction.pdf" )
0 commit comments