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
215 lines (156 loc) · 6.33 KB
/
Copy pathsimulation.py
File metadata and controls
215 lines (156 loc) · 6.33 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
import numpy
import pylab
from diffusion.problems import *
import mesh.patch as patch
import multigrid.MG as MG
from util import msg, profile, runparams
class Simulation:
def __init__(self, problem_name, rp, timers=None):
"""
Initialize the Simulation object for diffusion:
a = k a
t xx
Parameters
----------
problem_name : str
The name of the problem we wish to run. This should
correspond to one of the modules in diffusion/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 diffusion 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=1)
# create the variables
# first figure out the boundary conditions -- we allow periodic,
# Dirichlet, and Neumann.
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")
bcparam = []
for bc in [xlb_type, xrb_type, ylb_type, yrb_type]:
if bc == "periodic": bcparam.append("periodic")
elif bc == "neumann": bcparam.append("neumann")
elif bc == "dirichlet": bcparam.append("dirichlet")
else:
msg.fail("invalid BC")
bc = patch.BCObject(xlb=bcparam[0], xrb=bcparam[1],
ylb=bcparam[2], yrb=bcparam[3])
my_data = patch.CellCenterData2d(my_grid)
my_data.register_var("phi", 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 diffusion timestep() function computes the timestep
using the explicit timestep constraint as the starting point.
We then multiply by the CFL number to get the timestep.
Since we are doing an implicit discretization, we do not
require CFL < 1.
"""
cfl = self.rp.get_param("driver.cfl")
k = self.rp.get_param("diffusion.k")
# the timestep is min(dx**2/k, dy**2/k)
xtmp = self.cc_data.grid.dx**2/k
ytmp = self.cc_data.grid.dy**2/k
dt = cfl*min(xtmp, ytmp)
return dt
def preevolve(myd):
"""
Do any necessary evolution before the main evolve loop. This
is not needed for diffusion.
"""
pass
def evolve(self, dt):
"""
Diffusion through dt using C-N implicit solve with multigrid
"""
self.cc_data.fill_BC_all()
phi = self.cc_data.get_var("phi")
myg = self.cc_data.grid
# diffusion coefficient
k = self.rp.get_param("diffusion.k")
# setup the MG object -- we want to solve a Helmholtz equation
# equation of the form:
# (alpha - beta L) phi = f
#
# with alpha = 1
# beta = (dt/2) k
# f = phi + (dt/2) k L phi
#
# this is the form that arises with a Crank-Nicolson discretization
# of the diffusion equation.
mg = MG.CellCenterMG2d(myg.nx, myg.ny,
xmin=myg.xmin, xmax=myg.xmax,
ymin=myg.ymin, ymax=myg.ymax,
xl_BC_type=self.cc_data.BCs['phi'].xlb,
xr_BC_type=self.cc_data.BCs['phi'].xrb,
yl_BC_type=self.cc_data.BCs['phi'].ylb,
yr_BC_type=self.cc_data.BCs['phi'].yrb,
alpha=1.0, beta=0.5*dt*k,
verbose=0)
# form the RHS: f = phi + (dt/2) k L phi (where L is the Laplacian)
f = mg.soln_grid.scratch_array()
f[mg.ilo:mg.ihi+1,mg.jlo:mg.jhi+1] = \
phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] + 0.5*dt*k * \
((phi[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] +
phi[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1] -
2.0*phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx**2 +
(phi[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] +
phi[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ] -
2.0*phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dy**2)
mg.init_RHS(f)
# initial guess is zeros
mg.init_zeros()
# solve the MG problem for the updated phi
mg.solve(rtol=1.e-10)
#mg.smooth(mg.nlevels-1,100)
# update the solution
phi[:,:] = mg.get_solution()
def dovis(self):
"""
Do runtime visualization.
"""
pylab.clf()
phi = self.cc_data.get_var("phi")
myg = self.cc_data.grid
pylab.imshow(numpy.transpose(phi[myg.ilo:myg.ihi+1,
myg.jlo:myg.jhi+1]),
interpolation="nearest", origin="lower",
extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])
pylab.xlabel("x")
pylab.ylabel("y")
pylab.title("phi")
pylab.colorbar()
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()')