|
| 1 | +import numpy |
| 2 | +import pylab |
| 3 | + |
| 4 | +from advection.problems import * |
| 5 | +from advectiveFluxes import * |
| 6 | +import mesh.patch as patch |
| 7 | + |
| 8 | +class Simulation: |
| 9 | + |
| 10 | + def __init__(self, problem_name, rp): |
| 11 | + |
| 12 | + self.rp = rp |
| 13 | + self.cc_data = None |
| 14 | + |
| 15 | + self.SMALL = 1.e-12 |
| 16 | + |
| 17 | + self.problem_name = problem_name |
| 18 | + |
| 19 | + |
| 20 | + def initialize(self): |
| 21 | + """ |
| 22 | + Initialize the grid and variables for advection and set the initial |
| 23 | + conditions for the chosen problem. |
| 24 | + """ |
| 25 | + |
| 26 | + # setup the grid |
| 27 | + nx = self.rp.get_param("mesh.nx") |
| 28 | + ny = self.rp.get_param("mesh.ny") |
| 29 | + |
| 30 | + xmin = self.rp.get_param("mesh.xmin") |
| 31 | + xmax = self.rp.get_param("mesh.xmax") |
| 32 | + ymin = self.rp.get_param("mesh.ymin") |
| 33 | + ymax = self.rp.get_param("mesh.ymax") |
| 34 | + |
| 35 | + my_grid = patch.Grid2d(nx, ny, |
| 36 | + xmin=xmin, xmax=xmax, |
| 37 | + ymin=ymin, ymax=ymax, ng=4) |
| 38 | + |
| 39 | + |
| 40 | + # create the variables |
| 41 | + |
| 42 | + # first figure out the boundary conditions -- we need to translate |
| 43 | + # between the descriptive type of the boundary specified by the |
| 44 | + # user and the action that will be performed by the fill_BC routine. |
| 45 | + # Usually the actions can vary depending on the variable, but we |
| 46 | + # only have one variable. |
| 47 | + xlb_type = self.rp.get_param("mesh.xlboundary") |
| 48 | + xrb_type = self.rp.get_param("mesh.xrboundary") |
| 49 | + ylb_type = self.rp.get_param("mesh.ylboundary") |
| 50 | + yrb_type = self.rp.get_param("mesh.yrboundary") |
| 51 | + |
| 52 | + bc = patch.BCObject(xlb=xlb_type, xrb=xrb_type, |
| 53 | + ylb=ylb_type, yrb=yrb_type) |
| 54 | + |
| 55 | + my_data = patch.CellCenterData2d(my_grid, runtime_parameters=self.rp) |
| 56 | + |
| 57 | + my_data.register_var("density", bc) |
| 58 | + |
| 59 | + my_data.create() |
| 60 | + |
| 61 | + self.cc_data = my_data |
| 62 | + |
| 63 | + # now set the initial conditions for the problem |
| 64 | + exec self.problem_name + '.initData(self.cc_data)' |
| 65 | + |
| 66 | + |
| 67 | + def timestep(self): |
| 68 | + """ |
| 69 | + Computes the advective timestep (CFL) constraint. We use the |
| 70 | + driver.cfl parameter to control what fraction of the CFL |
| 71 | + step we actually take. |
| 72 | + """ |
| 73 | + |
| 74 | + cfl = self.rp.get_param("driver.cfl") |
| 75 | + |
| 76 | + u = self.rp.get_param("advection.u") |
| 77 | + v = self.rp.get_param("advection.v") |
| 78 | + |
| 79 | + # the timestep is min(dx/|u|, dy|v|) |
| 80 | + xtmp = self.cc_data.grid.dx/max(abs(u),self.SMALL) |
| 81 | + ytmp = self.cc_data.grid.dy/max(abs(v),self.SMALL) |
| 82 | + |
| 83 | + dt = cfl*min(xtmp, ytmp) |
| 84 | + |
| 85 | + return dt |
| 86 | + |
| 87 | + |
| 88 | + def preevolve(self): |
| 89 | + |
| 90 | + # do nothing |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | + def evolve(self, dt): |
| 95 | + """ |
| 96 | + Evolve the linear advection equation through one timestep. We only |
| 97 | + consider the "density" variable in the CellCenterData2d object input |
| 98 | + here. |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + self.cc_data : CellCenterData2d object |
| 103 | + The data object containing the scalar quantity we are advecting |
| 104 | + dt : float |
| 105 | + The timestep to evolve through |
| 106 | + |
| 107 | + """ |
| 108 | + |
| 109 | + dtdx = dt/self.cc_data.grid.dx |
| 110 | + dtdy = dt/self.cc_data.grid.dy |
| 111 | + |
| 112 | + flux_x, flux_y = unsplitFluxes(self.cc_data, dt, "density") |
| 113 | + |
| 114 | + """ |
| 115 | + do the differencing for the fluxes now. Here, we use slices so we |
| 116 | + avoid slow loops in python. This is equivalent to: |
| 117 | +
|
| 118 | + myPatch.data[i,j] = myPatch.data[i,j] + \ |
| 119 | + dtdx*(flux_x[i,j] - flux_x[i+1,j]) + \ |
| 120 | + dtdy*(flux_y[i,j] - flux_y[i,j+1]) |
| 121 | + """ |
| 122 | + |
| 123 | + qx = self.cc_data.grid.qx |
| 124 | + qy = self.cc_data.grid.qy |
| 125 | + |
| 126 | + dens = self.cc_data.get_var("density") |
| 127 | + |
| 128 | + dens[0:qx-1,0:qy-1] = dens[0:qx-1,0:qy-1] + \ |
| 129 | + dtdx*(flux_x[0:qx-1,0:qy-1] - flux_x[1:qx,0:qy-1]) + \ |
| 130 | + dtdy*(flux_y[0:qx-1,0:qy-1] - flux_y[0:qx-1,1:qy]) |
| 131 | + |
| 132 | + |
| 133 | + def dovis(self, n): |
| 134 | + |
| 135 | + pylab.clf() |
| 136 | + |
| 137 | + dens = self.cc_data.get_var("density") |
| 138 | + |
| 139 | + myg = self.cc_data.grid |
| 140 | + |
| 141 | + pylab.imshow(numpy.transpose(dens[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1]), |
| 142 | + interpolation="nearest", origin="lower", |
| 143 | + extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax]) |
| 144 | + |
| 145 | + pylab.xlabel("x") |
| 146 | + pylab.ylabel("y") |
| 147 | + pylab.title("density") |
| 148 | + |
| 149 | + pylab.colorbar() |
| 150 | + |
| 151 | + pylab.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t) |
| 152 | + |
| 153 | + pylab.draw() |
| 154 | + |
| 155 | + |
| 156 | + def finalize(self): |
| 157 | + |
| 158 | + exec self.problem_name + '.finalize()' |
0 commit comments