forked from python-hydro/pyro2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
550 lines (408 loc) · 19.4 KB
/
Copy pathsimulation.py
File metadata and controls
550 lines (408 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
from __future__ import print_function
import numpy
import pylab
from incompressible.problems import *
import incompressible.incomp_interface_f as incomp_interface_f
import mesh.reconstruction_f as reconstruction_f
import mesh.patch as patch
import multigrid.MG as MG
from util import profile
class Simulation:
def __init__(self, problem_name, rp, timers=None):
"""
Initialize the Simulation object for incompressible flow.
Parameters
----------
problem_name : str
The name of the problem we wish to run. This should
correspond to one of the modules in incompressible/problems/
rp : RuntimeParameters object
The runtime parameters for the simulation
timers : TimerCollection object, optional
The timers used for profiling this simulation
"""
self.rp = rp
self.cc_data = None
self.problem_name = problem_name
if timers == None:
self.tc = profile.TimerCollection()
else:
self.tc = timers
def initialize(self):
"""
Initialize the grid and variables for incompressible flow and
set the initial conditions for the chosen problem.
"""
# setup the grid
nx = self.rp.get_param("mesh.nx")
ny = self.rp.get_param("mesh.ny")
xmin = self.rp.get_param("mesh.xmin")
xmax = self.rp.get_param("mesh.xmax")
ymin = self.rp.get_param("mesh.ymin")
ymax = self.rp.get_param("mesh.ymax")
my_grid = patch.Grid2d(nx, ny,
xmin=xmin, xmax=xmax,
ymin=ymin, ymax=ymax, ng=4)
# create the variables
# first figure out the BCs
xlb_type = self.rp.get_param("mesh.xlboundary")
xrb_type = self.rp.get_param("mesh.xrboundary")
ylb_type = self.rp.get_param("mesh.ylboundary")
yrb_type = self.rp.get_param("mesh.yrboundary")
bc = patch.BCObject(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type)
# if we are reflecting, we need odd reflection in the normal
# directions for the velocity
bc_xodd = patch.BCObject(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type,
odd_reflect_dir="x")
bc_yodd = patch.BCObject(xlb=xlb_type, xrb=xrb_type,
ylb=ylb_type, yrb=yrb_type,
odd_reflect_dir="y")
my_data = patch.CellCenterData2d(my_grid)
# velocities
my_data.register_var("x-velocity", bc_xodd)
my_data.register_var("y-velocity", bc_yodd)
# phi -- used for the projections
my_data.register_var("phi-MAC", bc)
my_data.register_var("phi", bc)
my_data.register_var("gradp_x", bc)
my_data.register_var("gradp_y", bc)
my_data.create()
self.cc_data = my_data
# now set the initial conditions for the problem
exec(self.problem_name + '.init_data(self.cc_data, self.rp)')
def timestep(self):
"""
The timestep() function computes the advective timestep
(CFL) constraint. The CFL constraint says that information
cannot propagate further than one zone per timestep.
We use the driver.cfl parameter to control what fraction of the CFL
step we actually take.
"""
cfl = self.rp.get_param("driver.cfl")
u = self.cc_data.get_var("x-velocity")
v = self.cc_data.get_var("y-velocity")
# the timestep is min(dx/|u|, dy|v|)
xtmp = self.cc_data.grid.dx/(abs(u))
ytmp = self.cc_data.grid.dy/(abs(v))
dt = cfl*min(xtmp.min(), ytmp.min())
return dt
def preevolve(self):
"""
preevolve is called before we being the timestepping loop. For
the incompressible solver, this does an initial projection on the
velocity field and then goes through the full evolution to get the
value of phi. The fluid state (u, v) is then reset to values
before this evolve.
"""
myg = self.cc_data.grid
u = self.cc_data.get_var("x-velocity")
v = self.cc_data.get_var("y-velocity")
self.cc_data.fill_BC("x-velocity")
self.cc_data.fill_BC("y-velocity")
# 1. do the initial projection. This makes sure that our original
# velocity field satisties div U = 0
# next create the multigrid object. We want Neumann BCs on phi
# at solid walls and periodic on phi for periodic BCs
mg = MG.CellCenterMG2d(myg.nx, myg.ny,
xl_BC_type="periodic",
xr_BC_type="periodic",
yl_BC_type="periodic",
yr_BC_type="periodic",
xmin=myg.xmin, xmax=myg.xmax,
ymin=myg.ymin, ymax=myg.ymax,
verbose=0)
# first compute divU
divU = mg.soln_grid.scratch_array()
divU[mg.ilo:mg.ihi+1,mg.jlo:mg.jhi+1] = \
0.5*(u[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
u[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1])/myg.dx + \
0.5*(v[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
v[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ])/myg.dy
# solve L phi = DU
# initialize our guess to the solution, set the RHS to divU and
# solve
mg.init_zeros()
mg.init_RHS(divU)
mg.solve(rtol=1.e-10)
# store the solution in our self.cc_data object -- include a single
# ghostcell
phi = self.cc_data.get_var("phi")
solution = mg.get_solution()
phi[myg.ilo-1:myg.ihi+2,myg.jlo-1:myg.jhi+2] = \
solution[mg.ilo-1:mg.ihi+2,mg.jlo-1:mg.jhi+2]
# compute the cell-centered gradient of phi and update the
# velocities
gradp_x = myg.scratch_array()
gradp_y = myg.scratch_array()
gradp_x[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(phi[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
phi[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1])/myg.dx
gradp_y[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(phi[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
phi[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ])/myg.dy
u[:,:] -= gradp_x
v[:,:] -= gradp_y
# fill the ghostcells
self.cc_data.fill_BC("x-velocity")
self.cc_data.fill_BC("y-velocity")
# 2. now get an approximation to gradp at n-1/2 by going through the
# evolution.
# store the current solution -- we'll restore it in a bit
orig_data = patch.cell_center_data_clone(self.cc_data)
# get the timestep
dt = self.timestep()
# evolve
self.evolve(dt)
# update gradp_x and gradp_y in our main data object
new_gp_x = self.cc_data.get_var("gradp_x")
new_gp_y = self.cc_data.get_var("gradp_y")
orig_gp_x = orig_data.get_var("gradp_x")
orig_gp_y = orig_data.get_var("gradp_y")
orig_gp_x[:,:] = new_gp_x[:,:]
orig_gp_y[:,:] = new_gp_y[:,:]
self.cc_data = orig_data
print("done with the pre-evolution")
def evolve(self, dt):
"""
Evolve the incompressible equations through one timestep.
"""
u = self.cc_data.get_var("x-velocity")
v = self.cc_data.get_var("y-velocity")
gradp_x = self.cc_data.get_var("gradp_x")
gradp_y = self.cc_data.get_var("gradp_y")
phi = self.cc_data.get_var("phi")
myg = self.cc_data.grid
dtdx = dt/myg.dx
dtdy = dt/myg.dy
#---------------------------------------------------------------------
# create the limited slopes of u and v (in both directions)
#---------------------------------------------------------------------
limiter = self.rp.get_param("incompressible.limiter")
if (limiter == 0): limitFunc = reconstruction_f.nolimit
elif (limiter == 1): limitFunc = reconstruction_f.limit2
else: limitFunc = reconstruction_f.limit4
ldelta_ux = limitFunc(1, u, myg.qx, myg.qy, myg.ng)
ldelta_vx = limitFunc(1, v, myg.qx, myg.qy, myg.ng)
ldelta_uy = limitFunc(2, u, myg.qx, myg.qy, myg.ng)
ldelta_vy = limitFunc(2, v, myg.qx, myg.qy, myg.ng)
#---------------------------------------------------------------------
# get the advective velocities
#---------------------------------------------------------------------
"""
the advective velocities are the normal velocity through each cell
interface, and are defined on the cell edges, in a MAC type
staggered form
n+1/2
v
i,j+1/2
+------+------+
| |
n+1/2 | | n+1/2
u + U + u
i-1/2,j | i,j | i+1/2,j
| |
+------+------+
n+1/2
v
i,j-1/2
"""
# this returns u on x-interfaces and v on y-interfaces. These
# constitute the MAC grid
print(" making MAC velocities")
u_MAC, v_MAC = incomp_interface_f.mac_vels(myg.qx, myg.qy, myg.ng,
myg.dx, myg.dy, dt,
u, v,
ldelta_ux, ldelta_vx,
ldelta_uy, ldelta_vy,
gradp_x, gradp_y)
#---------------------------------------------------------------------
# do a MAC projection ot make the advective velocities divergence
# free
#---------------------------------------------------------------------
# we will solve L phi = D U^MAC, where phi is cell centered, and
# U^MAC is the MAC-type staggered grid of the advective
# velocities.
print(" MAC projection")
# create the multigrid object
mg = MG.CellCenterMG2d(myg.nx, myg.ny,
xl_BC_type="periodic",
xr_BC_type="periodic",
yl_BC_type="periodic",
yr_BC_type="periodic",
xmin=myg.xmin, xmax=myg.xmax,
ymin=myg.ymin, ymax=myg.ymax,
verbose=0)
# first compute divU
divU = mg.soln_grid.scratch_array()
# MAC velocities are edge-centered. divU is cell-centered.
divU[mg.ilo:mg.ihi+1,mg.jlo:mg.jhi+1] = \
(u_MAC[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
u_MAC[myg.ilo :myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx + \
(v_MAC[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
v_MAC[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+1])/myg.dy
# solve the Poisson problem
mg.init_zeros()
mg.init_RHS(divU)
mg.solve(rtol=1.e-12)
# update the normal velocities with the pressure gradient -- these
# constitute our advective velocities
phi_MAC = self.cc_data.get_var("phi-MAC")
solution = mg.get_solution()
phi_MAC[myg.ilo-1:myg.ihi+2,myg.jlo-1:myg.jhi+2] = \
solution[mg.ilo-1:mg.ihi+2,mg.jlo-1:mg.jhi+2]
# we need the MAC velocities on all edges of the computational domain
u_MAC[myg.ilo:myg.ihi+2,myg.jlo:myg.jhi+1] -= \
(phi_MAC[myg.ilo :myg.ihi+2,myg.jlo:myg.jhi+1] -
phi_MAC[myg.ilo-1:myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx
v_MAC[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+2] -= \
(phi_MAC[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+2] -
phi_MAC[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi+1])/myg.dy
#---------------------------------------------------------------------
# recompute the interface states, using the advective velocity
# from above
#---------------------------------------------------------------------
print(" making u, v edge states")
u_xint, v_xint, u_yint, v_yint = \
incomp_interface_f.states(myg.qx, myg.qy, myg.ng,
myg.dx, myg.dy, dt,
u, v,
ldelta_ux, ldelta_vx,
ldelta_uy, ldelta_vy,
gradp_x, gradp_y,
u_MAC, v_MAC)
#---------------------------------------------------------------------
# update U to get the provisional velocity field
#---------------------------------------------------------------------
print(" doing provisional update of u, v")
# compute (U.grad)U
# we want u_MAC U_x + v_MAC U_y
advect_x = myg.scratch_array()
advect_y = myg.scratch_array()
advect_x[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(u_MAC[myg.ilo :myg.ihi+1,myg.jlo:myg.jhi+1] +
u_MAC[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1]) * \
(u_xint[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
u_xint[myg.ilo :myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx + \
0.5*(v_MAC[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+1] +
v_MAC[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2]) * \
(u_yint[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
u_yint[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+1])/myg.dy
advect_y[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(u_MAC[myg.ilo :myg.ihi+1,myg.jlo:myg.jhi+1] +
u_MAC[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1]) * \
(v_xint[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
v_xint[myg.ilo :myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx + \
0.5*(v_MAC[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+1] +
v_MAC[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2]) * \
(v_yint[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
v_yint[myg.ilo:myg.ihi+1,myg.jlo :myg.jhi+1])/myg.dy
proj_type = self.rp.get_param("incompressible.proj_type")
if (proj_type == 1):
u[:,:] -= (dt*advect_x[:,:] + dt*gradp_x[:,:])
v[:,:] -= (dt*advect_y[:,:] + dt*gradp_y[:,:])
elif (proj_type == 2):
u[:,:] -= dt*advect_x[:,:]
v[:,:] -= dt*advect_y[:,:]
self.cc_data.fill_BC("x-velocity")
self.cc_data.fill_BC("y-velocity")
#---------------------------------------------------------------------
# project the final velocity
#---------------------------------------------------------------------
# now we solve L phi = D (U* /dt)
print(" final projection")
# create the multigrid object
mg = MG.CellCenterMG2d(myg.nx, myg.ny,
xl_BC_type="periodic",
xr_BC_type="periodic",
yl_BC_type="periodic",
yr_BC_type="periodic",
xmin=myg.xmin, xmax=myg.xmax,
ymin=myg.ymin, ymax=myg.ymax,
verbose=0)
# first compute divU
# u/v are cell-centered, divU is cell-centered
divU[mg.ilo:mg.ihi+1,mg.jlo:mg.jhi+1] = \
0.5*(u[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
u[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1])/myg.dx + \
0.5*(v[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
v[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ])/myg.dy
mg.init_RHS(divU/dt)
# use the old phi as our initial guess
phiGuess = mg.soln_grid.scratch_array()
phiGuess[mg.ilo-1:mg.ihi+2,mg.jlo-1:mg.jhi+2] = \
phi[myg.ilo-1:myg.ihi+2,myg.jlo-1:myg.jhi+2]
mg.init_solution(phiGuess)
# solve
mg.solve(rtol=1.e-12)
# store the solution
solution = mg.get_solution()
phi[myg.ilo-1:myg.ihi+2,myg.jlo-1:myg.jhi+2] = \
solution[mg.ilo-1:mg.ihi+2,mg.jlo-1:mg.jhi+2]
# compute the cell-centered gradient of p and update the velocities
# this differs depending on what we projected.
gradphi_x = myg.scratch_array()
gradphi_y = myg.scratch_array()
gradphi_x[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(phi[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
phi[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1])/myg.dx
gradphi_y[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(phi[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
phi[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ])/myg.dy
# u = u - grad_x phi dt
u[:,:] -= dt*gradphi_x
v[:,:] -= dt*gradphi_y
# store gradp for the next step
if (proj_type == 1):
gradp_x[:,:] += gradphi_x[:,:]
gradp_y[:,:] += gradphi_y[:,:]
elif (proj_type == 2):
gradp_x[:,:] = gradphi_x[:,:]
gradp_y[:,:] = gradphi_y[:,:]
self.cc_data.fill_BC("x-velocity")
self.cc_data.fill_BC("y-velocity")
def dovis(self):
"""
Do runtime visualization
"""
pylab.clf()
pylab.rc("font", size=10)
u = self.cc_data.get_var("x-velocity")
v = self.cc_data.get_var("y-velocity")
myg = self.cc_data.grid
vort = myg.scratch_array()
divU = myg.scratch_array()
vort[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(v[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
v[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx - \
0.5*(u[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
u[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy
divU[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] = \
0.5*(u[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] -
u[myg.ilo-1:myg.ihi,myg.jlo:myg.jhi+1])/myg.dx + \
0.5*(v[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] -
v[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi])/myg.dy
fig, axes = pylab.subplots(nrows=2, ncols=2, num=1)
pylab.subplots_adjust(hspace=0.25)
fields = [u, v, vort, divU]
field_names = ["u", "v", r"$\nabla \times U$", r"$\nabla \cdot U$"]
for n in range(4):
ax = axes.flat[n]
f = fields[n]
img = ax.imshow(numpy.transpose(f[myg.ilo:myg.ihi+1,
myg.jlo:myg.jhi+1]),
interpolation="nearest", origin="lower",
extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title(field_names[n])
pylab.colorbar(img, ax=ax)
pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t)
pylab.draw()
def finalize(self):
"""
Do any final clean-ups for the simulation and call the problem's
finalize() method.
"""
exec(self.problem_name + '.finalize()')