def test_oob(method): ugrid = twenty_one_triangles() face = ugrid.locate_faces((0, 0), method) assert face == -1 face = 0 face = ugrid.locate_faces(np.array(((0, 0), )), method) assert np.array_equal(face, np.array((-1, )))
def test_oob(method): ugrid = twenty_one_triangles() face = ugrid.locate_faces((0, 0), method) assert face == -1 face = 0 face = ugrid.locate_faces(np.array(((0, 0),)), method) assert np.array_equal(face, np.array((-1, )))
def test_locate_exact(): """ The nearest neighbor of the exact node locations had better be the nodes! """ ugrid = twenty_one_triangles() assert np.array_equal(ugrid.locate_nodes(ugrid.nodes), list(range(len(ugrid.nodes))))
def test_build_face_face_connectivity_big(): ugrid = twenty_one_triangles() ugrid.build_face_face_connectivity() face_face = ugrid.face_face_connectivity assert face_face[0].tolist() == [-1, 3, 2] assert face_face[9].tolist() == [8, 10, 7] assert face_face[8].tolist() == [-1, 9, 6] assert face_face[15].tolist() == [14, 16, 13] assert face_face[20].tolist() == [19, -1, -1]
def test_locate_nodes(): """Test finding multiple nodes at once.""" ugrid = twenty_one_triangles() assert np.array_equal(ugrid.locate_nodes(((4.58, 5.08), (4.81, 0.89), (6.43, 12.9), (8.74, 6.86), (5.12, 7.31), )), (6, 0, 17, 10, 8))
def twenty_one_triangles_with_depths(): """Returns a basic triangle grid with 21 triangles, a hole and a tail.""" grid = twenty_one_triangles() depths = UVar('depth', location='node', data=list(range(1, 21))) depths.attributes['units'] = 'unknown' depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid' depths.attributes['positive'] = 'down' grid.add_data(depths) return grid
def test_build_boundaries_big(): ugrid = twenty_one_triangles() ugrid.build_face_face_connectivity() ugrid.build_boundaries() boundaries = sorted(ugrid.boundaries.tolist()) expected_boundaries = [[0, 1], [1, 5], [2, 0], [3, 6], [4, 3], [5, 11], [6, 9], [7, 2], [9, 10], [10, 4], [11, 14], [12, 7], [13, 12], [14, 16], [15, 13], [16, 18], [17, 15], [18, 19], [19, 17]] assert boundaries == expected_boundaries
def test_locate_middle(): """See what happens the point is equidistant to two nodes.""" ugrid = twenty_one_triangles() # (3,5) is equidistant between nodes 2, 6 and 7 # 2 is returned, but might be arbitrary # assert ugrid.locate_nodes( (3, 5) ) == 2 # Perturb the point a bit, and nearest changes to: assert ugrid.locate_nodes((3.0000000001, 5)) == 6 assert ugrid.locate_nodes((3, 5.00000000001)) == 7 assert ugrid.locate_nodes((3, 4.99999999999)) == 2
def test_locate_nodes(): """Test finding multiple nodes at once.""" ugrid = twenty_one_triangles() assert np.array_equal( ugrid.locate_nodes(( (4.58, 5.08), (4.81, 0.89), (6.43, 12.9), (8.74, 6.86), (5.12, 7.31), )), (6, 0, 17, 10, 8))
def test_single(method): ugrid = twenty_one_triangles() face = ugrid.locate_faces((4, 6.5), method) assert face == 6
def test_locate_node(): """Test finding a single node.""" ugrid = twenty_one_triangles() assert ugrid.locate_nodes((4.58, 5.08)) == 6
def test_write_everything(): """An example with all features enabled, and a less trivial grid.""" grid = twenty_one_triangles() grid.build_edges() grid.build_face_face_connectivity() grid.build_edge_coordinates() grid.build_face_coordinates() grid.build_boundary_coordinates() # Depth on the nodes. depths = UVar('depth', location='node', data=np.linspace(1, 10, 20)) depths.attributes['units'] = 'm' depths.attributes['standard_name'] = 'sea_floor_depth_below_geoid' depths.attributes['positive'] = 'down' grid.add_data(depths) # Create a UVar object for u velocity: u_vel = UVar('u', location='face', data=np.sin(np.linspace(3, 12, 21))) u_vel.attributes['units'] = 'm/s' u_vel.attributes['standard_name'] = 'eastward_sea_water_velocity' grid.add_data(u_vel) # Create a UVar object for v velocity: v_vel = UVar('v', location='face', data=np.sin(np.linspace(12, 15, 21))) v_vel.attributes['units'] = 'm/s' v_vel.attributes['standard_name'] = 'northward_sea_water_velocity' grid.add_data(v_vel) # Fluxes on the edges: flux = UVar('flux', location='edge', data=np.linspace(1000, 2000, 41)) flux.attributes['units'] = 'm^3/s' flux.attributes['long_name'] = 'volume flux between cells' flux.attributes['standard_name'] = 'ocean_volume_transport_across_line' grid.add_data(flux) # Boundary conditions: bounds = np.zeros((19,), dtype=np.uint8) bounds[7] = 1 bnds = UVar('bnd_cond', location='boundary', data=bounds) bnds.attributes['long_name'] = 'model boundary conditions' bnds.attributes['flag_values'] = '0 1' bnds.attributes['flag_meanings'] = 'no_flow_boundary open_boundary' grid.add_data(bnds) fname = 'full_example.nc' with chdir(test_files): grid.save_as_netcdf(fname) ds = netCDF4.Dataset(fname) # Now the tests: assert nc_has_variable(ds, 'mesh') assert nc_has_variable(ds, 'depth') assert nc_var_has_attr_vals(ds, 'depth', { 'coordinates': 'mesh_node_lon mesh_node_lat', 'location': 'node'}) assert nc_has_variable(ds, 'u') assert nc_has_variable(ds, 'v') assert nc_var_has_attr_vals(ds, 'u', { 'coordinates': 'mesh_face_lon mesh_face_lat', 'location': 'face', 'mesh': 'mesh'}) assert nc_var_has_attr_vals(ds, 'v', { 'coordinates': 'mesh_face_lon mesh_face_lat', 'location': 'face', 'mesh': 'mesh'}) assert nc_has_variable(ds, 'flux') assert nc_var_has_attr_vals(ds, 'flux', { 'coordinates': 'mesh_edge_lon mesh_edge_lat', 'location': 'edge', 'units': 'm^3/s', 'mesh': 'mesh'}) assert nc_has_variable(ds, 'mesh') assert nc_has_variable(ds, 'bnd_cond') assert nc_var_has_attr_vals(ds, 'mesh', { 'boundary_node_connectivity': 'mesh_boundary_nodes'}) assert nc_var_has_attr_vals(ds, 'bnd_cond', { 'location': 'boundary', 'flag_values': '0 1', 'flag_meanings': 'no_flow_boundary open_boundary', 'mesh': 'mesh'}) ds.close() # And make sure pyugrid can reload it! with chdir(test_files): grid = UGrid.from_ncfile('full_example.nc', load_data=True) # And that some things are the same. # NOTE: more testing might be good here. # maybe some grid comparison functions? assert grid.mesh_name == 'mesh' assert len(grid.nodes) == 20 depth = grid.data['depth'] assert depth.attributes['units'] == 'm' u = grid.data['u'] assert u.attributes['units'] == 'm/s' os.remove(fname)
def test_multi(method): ugrid = twenty_one_triangles() face = ugrid.locate_faces(np.array(((4, 6.5), (7, 2))), method) assert (face == np.array((6, 0))).all()