def test_A2(self): """A has a two grid cells.""" # ------- Ice Grid gridI = self.gridI # ------- Atmosphere Grid vertices = [ibgrid.Vertex(spec[0], spec[1], spec[2]) for spec in [ (0, -2., -2.), (1, 0., -2.), (2, 2., -2.), (3, 2., 2.), (4, 0., 2.), (5, -2., 2.),]] vx = vertices cells = [ibgrid.Cell(*spec) for spec in [ (0, (vx[0], vx[1], vx[4], vx[5]), 0,0,0,0), (1, (vx[1], vx[2], vx[3], vx[4]), 0,0,0,0)]] gridA = ibgrid.Grid(vertices, cells, projection=projection, type='MESH', coordinates='XY') # ------ Exchange Grid vertices = [ibgrid.Vertex(spec[0], spec[1], spec[2]) for spec in [ (0, -1., -1.), # Original points from gridI (1, 1., -1.), (2, 1., 1.), (3, -1., 1.), (4, 0., 0.), # Middle point (5, 0., -1.), # Split points created by overlap (6, 0., 1.)]] vx = vertices cells = [ibgrid.Cell(*spec) for spec in [ (0, (vx[0], vx[5], vx[4]), 0,0,0,0), (1, (vx[5], vx[1], vx[4]), 1,0,0,0), (2, (vx[1], vx[2], vx[4]), 1,1,0,0), (3, (vx[2], vx[6], vx[4]), 1,2,0,0), (4, (vx[6], vx[3], vx[4]), 0,2,0,0), (5, (vx[3], vx[0], vx[4]), 0,3,0,0)]] gridX = ibgrid.Grid(vx, cells, projection=projection, type='MESH', coordinates='XY', grid1_ncells_full=gridA.cells_num_full, grid2_ncells_full=gridI.cells_num_full, grid1_nvertices_full=gridA.vertices_num_full, grid2_nvertices_full=gridI.vertices_num_full) nA = gridA.cells_num_full AvI,weightsA,weightsI = element_l1.compute_AvI(gridX, nA, gridI) #print(AvI.todense()) #print(weightsA) #print(weightsI) self.assertAlmostEqual(sum(weightsA), sum(weightsI))
def test_A1(self): """A has a single grid cell covering everything.""" # ------- Ice Grid gridI = self.gridI # ------- Atmosphere Grid vertices = [ibgrid.Vertex(spec[0], spec[1], spec[2]) for spec in [ (0, -2., -2.), (1, 2., -2.), (2, 2., 2.), (3, -2., 2.)]] vx = vertices cells = [ibgrid.Cell(0, [vx[0], vx[1], vx[2], vx[3]], 0,0,0,0)] gridA = ibgrid.Grid(vertices, cells, projection=projection, type='MESH', coordinates='XY') # ------ Exchange Grid vertices = dict(gridI.vertices) vx = vertices cells = [ibgrid.Cell(*spec) for spec in [ (0, (vx[0], vx[1], vx[4]), 0,0,0,0), (1, (vx[1], vx[2], vx[4]), 0,1,0,0), (2, (vx[2], vx[3], vx[4]), 0,2,0,0), (3, (vx[3], vx[0], vx[4]), 0,3,0,0)]] gridX = ibgrid.Grid(vx, cells, projection=projection, type='MESH', coordinates='XY', grid1_ncells_full=gridA.cells_num_full, grid2_ncells_full=gridI.cells_num_full, grid1_nvertices_full=gridA.vertices_num_full, grid2_nvertices_full=gridI.vertices_num_full) AvI,weightsA,weightsI = element_l1.compute_AvI(gridX, gridI) print(AvI.todense()) print(weightsA) print(weightsI) self.assertAlmostEqual(2./3., weightsI[0]) self.assertAlmostEqual(2./3., weightsI[1]) self.assertAlmostEqual(2./3., weightsI[2]) self.assertAlmostEqual(2./3., weightsI[3]) self.assertAlmostEqual(1.+1./3., weightsI[4]) M = np.asarray(AvI.todense()) self.assertAlmostEqual(M[0,0], weightsI[0]) self.assertAlmostEqual(M[0,1], weightsI[1]) self.assertAlmostEqual(M[0,2], weightsI[2]) self.assertAlmostEqual(M[0,3], weightsI[3]) self.assertAlmostEqual(M[0,4], weightsI[4]) self.assertAlmostEqual(sum(weightsA), sum(weightsI))
def test_surveyors_formula(self): """Check that our Surveyor's Formula above is correct. Evaluate the area of some simple polygons.""" triangle = [ ibgrid.Vertex(0, 0., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(2, 0., 1.) ] self.assertAlmostEqual(.5, area_of_polygon(triangle)) square = [ ibgrid.Vertex(0, 0., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(2, 1., 1.), ibgrid.Vertex(2, 0., 1.) ] self.assertAlmostEqual(1., area_of_polygon(square))
def setUp(self): # -------------- Ice Grid vertices = [ibgrid.Vertex(spec[0], spec[1], spec[2]) for spec in [ (0, -1., -1.), (1, 1., -1.), (2, 1., 1.), (3, -1., 1.), (4, 0., 0.)]] vx = vertices cells = [ibgrid.Cell(*spec) for spec in [ (0, (vx[0], vx[1], vx[4]), 0,0,0,0), (1, (vx[1], vx[2], vx[4]), 0,0,0,0), (2, (vx[2], vx[3], vx[4]), 0,0,0,0), (3, (vx[3], vx[0], vx[4]), 0,0,0,0)]] self.gridI = ibgrid.Grid(vertices, cells, projection=projection, type='MESH', coordinates='XY')
from icebin import ibgrid, issm from matplotlib import pyplot import giss.basemap # Create our own sample triangular mesh and other grids, and plot them # around Greenland. X0 = -510000. Y0 = -2810000. L = 1.e5 projection = '+proj=stere +lon_0=-39 +lat_0=90 +lat_ts=71.0 +ellps=WGS84' # ---------------------------------- # Atmopshere (GCM) grid vertices = [ ibgrid.Vertex(spec[0], X0 + L * spec[1], Y0 + L * spec[2]) for spec in [(0, -2., -2.), (1, 2., -2.), (2, 2., 2.), (3, -2., 2.)] ] vx = vertices cells = [ibgrid.Cell(0, [vx[0], vx[1], vx[2], vx[3]], 0, 0, 0, 0)] gridA = ibgrid.Grid(vertices, cells, projection=projection, type='MESH', coordinates='XY') # ---------------------------------- # Ice Mesh vertices = [ ibgrid.Vertex(spec[0], X0 + L * spec[1], Y0 + L * spec[2])
def test_cutoff_element(self): element = [ ibgrid.Vertex(0, -1., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(2, 0., 1.) ] # Cut in half poly0 = [ ibgrid.Vertex(0, 0., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(2, 0., 1.) ] i0 = element_l1.integrate_subelement(element, 2, poly0) self.assertAlmostEqual(.5 * (1. / 3.) * area_of_polygon(element), i0) # Cut off just the tip poly1 = [ ibgrid.Vertex(0, -.5, .5), ibgrid.Vertex(1, .5, .5), ibgrid.Vertex(2, 0., 1.) ] i1 = element_l1.integrate_subelement(element, 2, poly1) area_poly1 = area_of_polygon(poly1) vol1_part0 = .5 * ( 1. / 3. ) * area_poly1 # Volume of the tip pyramid, starting at .5 height vol1 = vol1_part0 \ + (area_poly1 * .5) # Volume on the "pedastal" beneath self.assertAlmostEqual(vol1, i1) # Cut off just the base poly2 = [ ibgrid.Vertex(0, -1., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(1, .5, .5), ibgrid.Vertex(0, -.5, .5) ] i2 = element_l1.integrate_subelement(element, 2, poly1) self.assertAlmostEqual(i0 * 2., i1 + i2)
def setUp(self): self.triangle = [ ibgrid.Vertex(0, 0., 0.), ibgrid.Vertex(1, 1., 0.), ibgrid.Vertex(2, 0., 1.) ]