|
| 1 | +import numpy |
| 2 | +import pylab |
| 3 | + |
| 4 | +from diffusion.problems import * |
| 5 | +import mesh.patch as patch |
| 6 | +import multigrid.multigrid as multigrid |
| 7 | +from util import msg, runparams |
| 8 | + |
| 9 | +class Simulation: |
| 10 | + |
| 11 | + def __init__(self, problem_name, rp): |
| 12 | + |
| 13 | + self.rp = rp |
| 14 | + self.cc_data = None |
| 15 | + |
| 16 | + self.problem_name = problem_name |
| 17 | + |
| 18 | + |
| 19 | + def initialize(self): |
| 20 | + """ |
| 21 | + initialize the grid and variables for diffusion |
| 22 | + """ |
| 23 | + |
| 24 | + # setup the grid |
| 25 | + nx = self.rp.get_param("mesh.nx") |
| 26 | + ny = self.rp.get_param("mesh.ny") |
| 27 | + |
| 28 | + xmin = self.rp.get_param("mesh.xmin") |
| 29 | + xmax = self.rp.get_param("mesh.xmax") |
| 30 | + ymin = self.rp.get_param("mesh.ymin") |
| 31 | + ymax = self.rp.get_param("mesh.ymax") |
| 32 | + |
| 33 | + my_grid = patch.Grid2d(nx, ny, |
| 34 | + xmin=xmin, xmax=xmax, |
| 35 | + ymin=ymin, ymax=ymax, ng=1) |
| 36 | + |
| 37 | + |
| 38 | + # create the variables |
| 39 | + |
| 40 | + # first figure out the boundary conditions -- we allow periodic, |
| 41 | + # Dirichlet, and Neumann. |
| 42 | + |
| 43 | + xlb_type = self.rp.get_param("mesh.xlboundary") |
| 44 | + xrb_type = self.rp.get_param("mesh.xrboundary") |
| 45 | + ylb_type = self.rp.get_param("mesh.ylboundary") |
| 46 | + yrb_type = self.rp.get_param("mesh.yrboundary") |
| 47 | + |
| 48 | + bcparam = [] |
| 49 | + for bc in [xlb_type, xrb_type, ylb_type, yrb_type]: |
| 50 | + if bc == "periodic": bcparam.append("periodic") |
| 51 | + elif bc == "neumann": bcparam.append("neumann") |
| 52 | + elif bc == "dirichlet": bcparam.append("dirichlet") |
| 53 | + else: |
| 54 | + msg.fail("invalid BC") |
| 55 | + |
| 56 | + |
| 57 | + bc = patch.BCObject(xlb=bcparam[0], xrb=bcparam[1], |
| 58 | + ylb=bcparam[2], yrb=bcparam[3]) |
| 59 | + |
| 60 | + |
| 61 | + my_data = patch.CellCenterData2d(my_grid, runtime_parameters=self.rp) |
| 62 | + |
| 63 | + my_data.register_var("phi", bc) |
| 64 | + |
| 65 | + my_data.create() |
| 66 | + |
| 67 | + self.cc_data = my_data |
| 68 | + |
| 69 | + # now set the initial conditions for the problem |
| 70 | + exec self.problem_name + '.initData(self.cc_data)' |
| 71 | + |
| 72 | + |
| 73 | + def timestep(self): |
| 74 | + """ |
| 75 | + The diffusion timestep() function computes the timestep |
| 76 | + using the explicit timestep constraint as the starting point. |
| 77 | + We then multiply by the CFL number to get the timestep. |
| 78 | + Since we are doing an implicit discretization, we do not |
| 79 | + require CFL < 1. |
| 80 | + """ |
| 81 | + |
| 82 | + cfl = self.rp.get_param("driver.cfl") |
| 83 | + k = self.rp.get_param("diffusion.k") |
| 84 | + |
| 85 | + # the timestep is min(dx**2/k, dy**2/k) |
| 86 | + xtmp = self.cc_data.grid.dx**2/k |
| 87 | + ytmp = self.cc_data.grid.dy**2/k |
| 88 | + |
| 89 | + dt = cfl*min(xtmp, ytmp) |
| 90 | + |
| 91 | + return dt |
| 92 | + |
| 93 | + |
| 94 | + def preevolve(myd): |
| 95 | + |
| 96 | + # do nothing |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | + def evolve(self, dt): |
| 101 | + """ |
| 102 | + Diffusion through dt using C-N implicit solve with multigrid |
| 103 | + """ |
| 104 | + |
| 105 | + self.cc_data.fill_BC_all() |
| 106 | + phi = self.cc_data.get_var("phi") |
| 107 | + myg = self.cc_data.grid |
| 108 | + |
| 109 | + # diffusion coefficient |
| 110 | + k = self.rp.get_param("diffusion.k") |
| 111 | + |
| 112 | + |
| 113 | + # setup the MG object -- we want to solve a Helmholtz equation |
| 114 | + # equation of the form: |
| 115 | + # (alpha - beta L) phi = f |
| 116 | + # |
| 117 | + # with alpha = 1 |
| 118 | + # beta = (dt/2) k |
| 119 | + # f = phi + (dt/2) k L phi |
| 120 | + # |
| 121 | + # this is the form that arises with a Crank-Nicolson discretization |
| 122 | + # of the diffusion equation. |
| 123 | + mg = multigrid.CellCenterMG2d(myg.nx, myg.ny, |
| 124 | + xmin=myg.xmin, xmax=myg.xmax, |
| 125 | + ymin=myg.ymin, ymax=myg.ymax, |
| 126 | + xl_BC_type=self.cc_data.BCs['phi'].xlb, |
| 127 | + xr_BC_type=self.cc_data.BCs['phi'].xrb, |
| 128 | + yl_BC_type=self.cc_data.BCs['phi'].ylb, |
| 129 | + yr_BC_type=self.cc_data.BCs['phi'].yrb, |
| 130 | + alpha=1.0, beta=0.5*dt*k, |
| 131 | + verbose=0) |
| 132 | + |
| 133 | + # form the RHS: f = phi + (dt/2) k L phi (where L is the Laplacian) |
| 134 | + f = mg.soln_grid.scratch_array() |
| 135 | + f[mg.ilo:mg.ihi+1,mg.jlo:mg.jhi+1] = \ |
| 136 | + phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1] + 0.5*dt*k * \ |
| 137 | + ((phi[myg.ilo+1:myg.ihi+2,myg.jlo:myg.jhi+1] + |
| 138 | + phi[myg.ilo-1:myg.ihi ,myg.jlo:myg.jhi+1] - |
| 139 | + 2.0*phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dx**2 + |
| 140 | + (phi[myg.ilo:myg.ihi+1,myg.jlo+1:myg.jhi+2] + |
| 141 | + phi[myg.ilo:myg.ihi+1,myg.jlo-1:myg.jhi ] - |
| 142 | + 2.0*phi[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1])/myg.dy**2) |
| 143 | + |
| 144 | + mg.init_RHS(f) |
| 145 | + |
| 146 | + # initial guess is zeros |
| 147 | + mg.init_zeros() |
| 148 | + |
| 149 | + # solve the MG problem for the updated phi |
| 150 | + mg.solve(rtol=1.e-10) |
| 151 | + #mg.smooth(mg.nlevels-1,100) |
| 152 | + |
| 153 | + # update the solution |
| 154 | + phi[:,:] = mg.get_solution() |
| 155 | + |
| 156 | + |
| 157 | + def dovis(self, n): |
| 158 | + |
| 159 | + pylab.clf() |
| 160 | + |
| 161 | + phi = self.cc_data.get_var("phi") |
| 162 | + |
| 163 | + myg = self.cc_data.grid |
| 164 | + |
| 165 | + pylab.imshow(numpy.transpose(phi[myg.ilo:myg.ihi+1, |
| 166 | + myg.jlo:myg.jhi+1]), |
| 167 | + interpolation="nearest", origin="lower", |
| 168 | + extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax]) |
| 169 | + |
| 170 | + pylab.xlabel("x") |
| 171 | + pylab.ylabel("y") |
| 172 | + pylab.title("phi") |
| 173 | + |
| 174 | + pylab.colorbar() |
| 175 | + |
| 176 | + pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t) |
| 177 | + |
| 178 | + pylab.draw() |
| 179 | + |
| 180 | + |
| 181 | + def finalize(self): |
| 182 | + exec self.problem_name + '.finalize()' |
| 183 | + |
| 184 | + |
0 commit comments