diff --git a/pyro/mesh/patch.py b/pyro/mesh/patch.py index d1f8ecab4..5801aadab 100644 --- a/pyro/mesh/patch.py +++ b/pyro/mesh/patch.py @@ -884,5 +884,66 @@ def do_demo(): mydata.pretty_print("a") +# **c checking +class PolarGrid(Grid2d): + """ + the 2-d grid class. The grid object will contain the coordinate + information (at various centerings). + + A basic representation of the layout is:: + + *---* \theta_{i+1/2} + / | + / | + / * \theta_i + / | + / | + *____*____* \theta_{i-1/2} +r_{i-1/2} r_i r_{i+1/2} + + The '*' marks represent the vertices; i index is the data location. + """ + + # pylint: disable=too-many-instance-attributes + + def area_x(self): + """ + Return an array of the face areas. + The shape of the returned array is (ni, nj). + """ + r1, t1 = np.meshgrid(self.xr, self.yr) + r0, t0 = np.meshgrid(self.xl, self.yl) + + # ** this is just 1/2*r*d\theta + + area = 0.5 * r0 * (t1 - t0) + return area + + def area_y(self): + """ + Return an array of the face areas. + The shape of the returned array is (ni, nj). + """ + r1, t1 = np.meshgrid(self.xr, self.yr) + r0, t0 = np.meshgrid(self.xl, self.yl) + + # ** this is just dr + + area = r1 - r0 + return area + + def cell_volumes(self): + """ + Return an array of the cell volume data for the given coordinate box + The shape of the returned array is (ni, nj). + """ + r1, t1 = np.meshgrid(self.xr, self.yr) + r0, t0 = np.meshgrid(self.xl, self.yl) + + # ** this is just the face area + + return 0.5 * (r1 ** 2 - r0 ** 2) * (t1 - t0) + + if __name__ == "__main__": do_demo() diff --git a/pyro/mesh/tests/test_patch.py b/pyro/mesh/tests/test_patch.py index d006f9078..46d07212a 100644 --- a/pyro/mesh/tests/test_patch.py +++ b/pyro/mesh/tests/test_patch.py @@ -244,3 +244,79 @@ def test_bcs(): # top assert_array_equal(d[myg.ilo:myg.ihi+1, myg.jhi-1:myg.jhi+1], -np.fliplr(d[myg.ilo:myg.ihi+1, myg.jhi+1:myg.jhi+3])) + + +# PolarGrid tests +class TestPolarGrid(object): + @classmethod + def setup_class(cls): + """ this is run once for each class before any tests """ + pass + + @classmethod + def teardown_class(cls): + """ this is run once for each class after all tests """ + pass + + def setup_method(self): + """ this is run before each test """ + self.g = patch.PolarGrid(4, 6, ng=2, ymax=1.5) + + def teardown_method(self): + """ this is run after each test """ + self.g = None + + def test_dx_dy(self): + assert self.g.dx == 0.25 + assert self.g.dy == 0.25 + + def test_grid_coords(self): + assert_array_equal(self.g.x[self.g.ilo:self.g.ihi+1], + np.array([0.125, 0.375, 0.625, 0.875])) + assert_array_equal(self.g.y[self.g.jlo:self.g.jhi+1], + np.array([0.125, 0.375, 0.625, 0.875, 1.125, 1.375])) + + def test_grid_2d_coords(self): + assert_array_equal(self.g.x, self.g.x2d[:, self.g.jc]) + assert_array_equal(self.g.y, self.g.y2d[self.g.ic, :]) + + def test_scratch_array(self): + q = self.g.scratch_array() + assert q.shape == (self.g.qx, self.g.qy) + + def test_coarse_like(self): + q = self.g.coarse_like(2) + assert q.qx == 2*self.g.ng + self.g.nx//2 + assert q.qy == 2*self.g.ng + self.g.ny//2 + + def test_fine_like(self): + q = self.g.fine_like(2) + assert q.qx == 2*self.g.ng + 2*self.g.nx + assert q.qy == 2*self.g.ng + 2*self.g.ny + + def test_norm(self): + q = self.g.scratch_array() + # there are 24 elements, the norm L2 norm is + # sqrt(dx*dy*24) + q.v()[:, :] = np.array([[1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1], + [1, 1, 1, 1, 1, 1]]) + + assert q.norm() == np.sqrt(24*self.g.dx*self.g.dy) + + def test_equality(self): + g2 = patch.PolarGrid(2, 5, ng=1) + assert g2 != self.g + + def test_area_x(self): + A = self.g.area_x() + assert A[0,0] == (self.g.yr[0] - self.g.yl[0]) * (self.g.xr[0] - self.g.xl[0]) * 0.5 + + def test_area_y(self): + A = self.g.area_y() + assert A[0] == (self.g.xr - self.g.xl) + + def test_cell_volumes(self): + V = self.g.cell_volumes() + assert V[0,0] == (self.g.yr[0] - self.g.yl[0]) * (self.g.xr[0] ** 2 - self.g.xl[0] ** 2) * 0.5