@@ -70,6 +70,14 @@ def fill_BCs(self):
70
70
sys .exit ("invalid BC" )
71
71
72
72
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
+
73
81
class Simulation (object ):
74
82
75
83
def __init__ (self , grid ):
@@ -105,21 +113,22 @@ def timestep(self, C):
105
113
def states (self , dt ):
106
114
""" compute the left and right interface states """
107
115
116
+ g = self .grid
108
117
# compute the piecewise linear slopes -- 2nd order MC limiter
109
118
# we pick a range of cells that includes 1 ghost cell on either
110
119
# side
111
- ib = self . grid .ilo - 1
112
- ie = self . grid .ihi + 1
120
+ ib = g .ilo - 1
121
+ ie = g .ihi + 1
113
122
114
- u = self . grid .u
123
+ u = g .u
115
124
116
125
# this is the MC limiter from van Leer (1977), as given in
117
126
# LeVeque (2002). Note that this is slightly different than
118
127
# the expression from Colella (1990)
119
128
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 ()
123
132
124
133
dc [ib :ie + 1 ] = 0.5 * (u [ib + 1 :ie + 2 ] - u [ib - 1 :ie ])
125
134
dl [ib :ie + 1 ] = u [ib + 1 :ie + 2 ] - u [ib :ie + 1 ]
@@ -185,6 +194,8 @@ def evolve(self, C, tmax):
185
194
186
195
self .t = 0.0
187
196
197
+ g = self .grid
198
+
188
199
# main evolution loop
189
200
while (self .t < tmax ):
190
201
@@ -211,80 +222,80 @@ def evolve(self, C, tmax):
211
222
self .t += dt
212
223
213
224
225
+ if __name__ == "__main__" :
226
+ #-----------------------------------------------------------------------------
227
+ # sine
214
228
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" )
223
234
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
226
237
227
- C = 0.8
238
+ C = 0.8
228
239
229
- plt .clf ()
240
+ plt .clf ()
230
241
231
- s = Simulation (g )
242
+ s = Simulation (g )
232
243
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" )
236
247
237
- uinit = s .grid .u .copy ()
248
+ uinit = s .grid .u .copy ()
238
249
239
- s .evolve (C , tend )
250
+ s .evolve (C , tend )
240
251
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 ))
244
255
245
256
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 )
248
259
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" )
252
263
253
264
254
- #-----------------------------------------------------------------------------
255
- # rarefaction
265
+ #-----------------------------------------------------------------------------
266
+ # rarefaction
256
267
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" )
262
273
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
265
276
266
- C = 0.8
277
+ C = 0.8
267
278
268
- plt .clf ()
279
+ plt .clf ()
269
280
270
- s = Simulation (g )
281
+ s = Simulation (g )
271
282
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
274
285
275
- s .init_cond ("rarefaction" )
286
+ s .init_cond ("rarefaction" )
276
287
277
- uinit = s .grid .u .copy ()
288
+ uinit = s .grid .u .copy ()
278
289
279
- s .evolve (C , tend )
290
+ s .evolve (C , tend )
280
291
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 ))
283
294
284
295
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 )
286
297
287
- plt .xlabel ("$x$" )
288
- plt .ylabel ("$u$" )
298
+ plt .xlabel ("$x$" )
299
+ plt .ylabel ("$u$" )
289
300
290
- plt .savefig ("fv-burger-rarefaction.pdf" )
301
+ plt .savefig ("fv-burger-rarefaction.pdf" )
0 commit comments