@@ -43,6 +43,10 @@ def scratch_array(self):
43
43
return np .zeros ((2 * self .ng + self .nx ), dtype = np .float64 )
44
44
45
45
46
+ def norm (self , e ):
47
+ return np .sqrt (self .dx * np .sum (e [self .ilo :self .ihi + 1 ]** 2 ))
48
+
49
+
46
50
def fill_BCs (self , var ):
47
51
48
52
if not var in self .data .keys ():
@@ -54,6 +58,21 @@ def fill_BCs(self, var):
54
58
vp [0 :self .ilo + 1 ] = vp [self .ilo ]
55
59
vp [self .ihi + 1 :] = vp [self .ihi ]
56
60
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
+
57
76
58
77
class Simulation (object ):
59
78
@@ -120,7 +139,7 @@ def diffuse(self, eps, S, dt):
120
139
121
140
return unew
122
141
123
- def advect (self , S , dt ):
142
+ def advect (self , S , dt , limit = 1 ):
124
143
""" compute the advective term that updates u in time. Here, S is
125
144
a source term """
126
145
@@ -143,8 +162,11 @@ def advect(self, S, dt):
143
162
dl [ib :ie + 1 ] = np .fabs (u [ib + 1 :ie + 2 ] - u [ib :ie + 1 ])
144
163
dr [ib :ie + 1 ] = np .fabs (u [ib :ie + 1 ] - u [ib - 1 :ie ])
145
164
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
148
170
149
171
# construct the interface states, to second order in space and
150
172
# time
@@ -193,7 +215,7 @@ def lap(self):
193
215
194
216
return lapu
195
217
196
- def evolve (self , eps , cfl , tmax , dovis = 0 ):
218
+ def evolve (self , eps , cfl , tmax , limit = 1 , dovis = 0 ):
197
219
"""
198
220
the main evolution loop. Evolve
199
221
@@ -225,7 +247,7 @@ def evolve(self, eps, cfl, tmax, dovis=0):
225
247
S = eps * self .lap ()
226
248
227
249
# construct the advective update
228
- A = self .advect (S , dt )
250
+ A = self .advect (S , dt , limit = limit )
229
251
230
252
# diffuse for dt
231
253
unew = self .diffuse (eps , A , dt )
@@ -247,43 +269,43 @@ def evolve(self, eps, cfl, tmax, dovis=0):
247
269
plt .draw ()
248
270
249
271
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" ])
257
272
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
262
277
278
+ g1 = Grid1d (nx , ng = 2 , vars = ["u" ])
279
+ g2 = Grid1d (nx , ng = 2 , vars = ["u" ])
280
+ g3 = Grid1d (nx , ng = 2 , vars = ["u" ])
263
281
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 )
268
286
287
+ eps2 = 0.0005
288
+ s2 = Simulation (g2 )
289
+ s2 .init_cond ()
290
+ s2 .evolve (eps2 , cfl , tmax , dovis = 0 )
269
291
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 )
274
296
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 ))
277
299
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" )
280
302
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" )
283
305
284
- plt .legend (frameon = False )
306
+ plt .legend (frameon = False )
285
307
286
- plt .xlim (0.0 , 1.0 )
308
+ plt .xlim (0.0 , 1.0 )
287
309
288
- plt .tight_layout ()
289
- plt .savefig ("burgersvisc.pdf" )
310
+ plt .tight_layout ()
311
+ plt .savefig ("burgersvisc.pdf" )
0 commit comments