def test_shifted(self): geom = gd.UniformGrid( [101, 101], x0=[1, 0], x1=[3, 10], num_ghost=[3, 3], time=1, iteration=1, ) geom2 = gd.UniformGrid( [101, 101], x0=[3, -2], x1=[5, 8], num_ghost=[3, 3], time=1, iteration=1, ) self.assertEqual(geom.shifted([2, -2]), geom2) # Error incompatible dimensions with self.assertRaises(ValueError): geom.shifted(2)
def setUp(self): # Let's test with a TimeSeries, a UniformGridData, and a # HierarchicalGridData x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) self.TS = ts.TimeSeries(x, y) self.grid_2d = gd.UniformGrid([10, 20], x0=[0.5, 1], dx=[1, 1]) self.ugd = gdu.sample_function_from_uniformgrid( lambda x, y: x * (y + 2), self.grid_2d) grid_2d_1 = gd.UniformGrid([10, 20], x0=[0.5, 1], dx=[1, 1], ref_level=0) self.ugd1 = gdu.sample_function_from_uniformgrid( lambda x, y: x * (y + 2), grid_2d_1) grid_2d_2 = gd.UniformGrid([10, 20], x0=[1, 2], dx=[3, 0.4], ref_level=1) self.ugd2 = gdu.sample_function_from_uniformgrid( lambda x, y: x * (y + 2), grid_2d_2) self.hg = gd.HierarchicalGridData([self.ugd1, self.ugd2])
def test_flat_dimensions_remove(self): geom = gd.UniformGrid([101, 1], x0=[0, 0], dx=[0.01, 1]) data = np.array([i * np.linspace(1, 5, 1) for i in range(101)]) ug_data = gd.UniformGridData(geom, data) ug_data.flat_dimensions_remove() flat_geom = gd.UniformGrid([101], x0=[0], x1=[1]) self.assertEqual( ug_data, gd.UniformGridData(flat_geom, np.linspace(0, 100, 101)) ) # Check invalidation of spline self.assertTrue(ug_data.invalid_spline) # Test from 3D to 2D grid_data3d = gdu.sample_function_from_uniformgrid( lambda x, y, z: x * (y + 2) * (z + 5), gd.UniformGrid([10, 20, 1], x0=[0, 1, 0], dx=[1, 1, 1]), ) grid_2d = gd.UniformGrid([10, 20], x0=[0, 1], dx=[1, 1]) expected_data2d = gdu.sample_function_from_uniformgrid( lambda x, y: x * (y + 2) * (0 + 5), grid_2d ) self.assertEqual( grid_data3d.flat_dimensions_removed(), expected_data2d )
def test_ghost_zones_remove(self): geom = gd.UniformGrid( [101, 201], x0=[0, 0], x1=[100, 200], num_ghost=[1, 3] ) data = np.array([i * np.linspace(0, 200, 201) for i in range(101)]) ug_data = gd.UniformGridData(geom, data) ug_data.ghost_zones_remove() expected_data = np.array( [i * np.linspace(3, 197, 195) for i in range(1, 100)] ) expected_grid = gd.UniformGrid( [99, 195], x0=[1, 3], x1=[99, 197], num_ghost=[0, 0] ) expected_ug_data = gd.UniformGridData(expected_grid, expected_data) self.assertEqual(ug_data, expected_ug_data) # Check invalidation of spline self.assertTrue(ug_data.invalid_spline) self.assertCountEqual(ug_data.num_ghost, [0, 0]) # Check with num_ghost = 0 ug_data.ghost_zones_remove() self.assertEqual(ug_data, ug_data.copy())
def test__check_dims(self): # Test multidimensional shape with self.assertRaises(ValueError): gd.UniformGrid(np.array([[1, 2], [3, 4]]), np.array([1, 2])) # Test different len between shape and origin with self.assertRaises(ValueError): gd.UniformGrid(np.array([100, 200]), np.array([1, 2, 3]))
def test_flat_ghost_zones_removed(self): geom = gd.UniformGrid( [101, 101], x0=[1, 1], dx=[1, 0.5], num_ghost=[3, 0] ) geom2 = gd.UniformGrid( [95, 101], x0=[4, 1], dx=[1, 0.5], num_ghost=[0, 0] ) self.assertEqual(geom.ghost_zones_removed(), geom2)
def test_partial_derivated(self): # Here we are also testing _call_component_method geom = gd.UniformGrid( [8001, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=0 ) geom2 = gd.UniformGrid( [10001, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=1 ) sin_wave1 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom ) sin_wave2 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom2 ) original_sin1 = sin_wave1.copy() original_sin2 = sin_wave2.copy() sin_wave = gd.HierarchicalGridData([sin_wave1] + [sin_wave2]) sin_copy = sin_wave.copy() # Second derivative should still be a -sin sin_wave.partial_derive(0, order=2) self.assertTrue( np.allclose(-sin_wave[0][0].data, original_sin1.data, atol=1e-3) ) self.assertTrue( np.allclose(-sin_wave[1][0].data, original_sin2.data, atol=1e-3) ) # Test _call_component_method with non-string name with self.assertRaises(TypeError): sin_wave._call_component_method(sin_wave) # Test _call_component_method with non existing method with self.assertRaises(ValueError): sin_wave._call_component_method("lol") gradient = sin_copy.gradient(order=2) # Along the first direction (it's a HierarchicalGridData) partial_x = gradient[0] self.assertTrue( np.allclose(-partial_x[0][0].data, original_sin1.data, atol=1e-3) ) # First refinement_level self.assertTrue( np.allclose(-partial_x[1][0].data, original_sin2.data, atol=1e-3) )
def test_sample_function(self): # Test not grid as input with self.assertRaises(TypeError): gdu.sample_function_from_uniformgrid(np.sin, 0) # Test 1d geom = gd.UniformGrid(100, x0=0, x1=2 * np.pi) data = np.sin(np.linspace(0, 2 * np.pi, 100)) self.assertEqual( gdu.sample_function(np.sin, 100, 0, 2 * np.pi), gd.UniformGridData(geom, data), ) # Test with additional arguments geom_ref_level = gd.UniformGrid(100, x0=0, x1=2 * np.pi, ref_level=0) self.assertEqual( gdu.sample_function(np.sin, 100, 0, 2 * np.pi, ref_level=0), gd.UniformGridData(geom_ref_level, data), ) # Test 2d geom2d = gd.UniformGrid([100, 200], x0=[0, 1], x1=[1, 2]) def square(x, y): return x * y # Test function takes too few arguments with self.assertRaises(TypeError): gdu.sample_function_from_uniformgrid(lambda x: x, geom2d) # Test function takes too many arguments with self.assertRaises(TypeError): gdu.sample_function_from_uniformgrid(square, geom) # Test other TypeError with self.assertRaises(TypeError): gdu.sample_function_from_uniformgrid(np.sin, geom2d) data2d = np.vectorize(square)(*geom2d.coordinates(as_same_shape=True)) self.assertEqual( gdu.sample_function(square, [100, 200], [0, 1], [1, 2]), gd.UniformGridData(geom2d, data2d), ) self.assertEqual( gdu.sample_function_from_uniformgrid(square, geom2d), gd.UniformGridData(geom2d, data2d), )
def test_merge_refinement_levels(self): # This also tests to_UniformGridData # We redefine this to be ref_level=1 grid1 = gd.UniformGrid([4, 5], x0=[0, 1], x1=[3, 5], ref_level=1) grid2 = gd.UniformGrid([11, 21], x0=[4, 6], x1=[14, 26], ref_level=1) grids = [grid1, grid2] # Here we use the same data with another big refinement level sampled # from the same function big_grid = gd.UniformGrid( [16, 26], x0=[0, 1], x1=[30, 51], ref_level=0 ) # Big grid has resolution 2 dx of grids def product(x, y): return x * (y + 2) grid_data_two_comp = [ gdu.sample_function_from_uniformgrid(product, g) for g in grids ] big_grid_data = gdu.sample_function_from_uniformgrid(product, big_grid) hg = gd.HierarchicalGridData(grid_data_two_comp + [big_grid_data]) # When I merge the data I should just get big_grid at the resolution # of self.grid_data_two_comp expected_grid = gd.UniformGrid( [31, 51], x0=[0, 1], x1=[30, 51], ref_level=-1 ) expected_data = gdu.sample_function_from_uniformgrid( product, expected_grid ) # Test with resample self.assertEqual( hg.merge_refinement_levels(resample=True), expected_data ) # If we don't resample there will be points that are "wrong" because we # compute them with the nearest neighbors of the lowest resolution grid # For example, the point with coordinate (5, 1) falls inside the lowest # resolution grid, so its value will be the value of the closest point # in big_grid (6, 1) -> 18. self.assertEqual(hg.merge_refinement_levels()((5, 1)), 18) self.assertEqual(hg.merge_refinement_levels().grid, expected_grid) # Test a case with only one refinement level, so just returning a copy hg_one = gd.HierarchicalGridData([big_grid_data]) self.assertEqual(hg_one.merge_refinement_levels(), big_grid_data)
def test_hash(self): geom4 = gd.UniformGrid( [101, 101], x0=[1, 1], dx=[1, 0.5], num_ghost=[3, 3], time=1, iteration=1, ) hash_shape = hash(tuple(geom4.shape)) hash_x0 = hash(tuple(geom4.x0)) hash_dx = hash(tuple(geom4.dx)) hash_num_ghost = hash(tuple(geom4.num_ghost)) hash_ref_level = hash(geom4.ref_level) hash_component = hash(geom4.component) hash_time = hash(geom4.time) hash_iteration = hash(geom4.iteration) self.assertEqual( hash(geom4), hash_shape ^ hash_x0 ^ hash_dx ^ hash_num_ghost ^ hash_ref_level ^ hash_component ^ hash_time ^ hash_iteration, )
def test__apply_binary(self): data1 = np.array([i * np.linspace(1, 5, 51) for i in range(101)]) data2 = np.array([i ** 2 * np.linspace(1, 5, 51) for i in range(101)]) ug_data1 = gd.UniformGridData(self.geom, data1) ug_data2 = gd.UniformGridData(self.geom, data2) expected_ug_data = gd.UniformGridData(self.geom, data1 + data2) self.assertEqual(ug_data1 + ug_data2, expected_ug_data) # Test incompatible grids geom = gd.UniformGrid([101, 1], x0=[0, 0], dx=[1, 1]) data3 = np.array([i * np.linspace(1, 5, 1) for i in range(101)]) ug_data3 = gd.UniformGridData(geom, data3) with self.assertRaises(ValueError): ug_data1 + ug_data3 # Add number self.assertEqual( ug_data1 + 1, gd.UniformGridData(self.geom, data1 + 1) ) # Incompatible objects with self.assertRaises(TypeError): ug_data1 + geom
def test_read_hdf5(self): expected_grid = grid_data.UniformGrid( [29, 29], x0=[-14, -14], x1=[14, 14], ref_level=0, num_ghost=[3, 3], time=0, iteration=0, component=0, ) with h5py.File(self.P_file, "r") as fil: # Notice the .T expected_data = fil["ILLINOISGRMHD::P it=0 tl=0 rl=0 c=0"][()].T expected_grid_data = grid_data.UniformGridData( expected_grid, expected_data ) self.assertEqual( self.P._read_component_as_uniform_grid_data(self.P_file, 0, 0, 0), expected_grid_data, )
def test_read_hdf5(self): expected_grid = grid_data.UniformGrid( [29, 29], x0=[-14, -14], x1=[14, 14], ref_level=0, num_ghost=[3, 3], time=0, iteration=0, component=0, ) with h5py.File(self.P_file, "r") as fil: # Notice the .T expected_data = fil["ILLINOISGRMHD::P it=0 tl=0 rl=0 c=0"][()].T expected_grid_data = grid_data.UniformGridData(expected_grid, expected_data) self.assertEqual( self.P._read_component_as_uniform_grid_data(self.P_file, 0, 0, 0), expected_grid_data, ) # Test that we can read a grid array self.assertTrue( isinstance(self.reader["int_em_T_rph"][0], grid_data.HierarchicalGridData))
def test__in__(self): # We test __in__ testing contains, which calls in geom4 = gd.UniformGrid( [101, 101], x0=[1, 1], x1=[101, 51], num_ghost=[3, 3], time=1, iteration=1, ) self.assertTrue(geom4.contains([50, 50])) self.assertTrue(geom4.contains([1, 1])) self.assertFalse(geom4.contains([1, 0])) self.assertFalse(geom4.contains([102, 102])) self.assertFalse(geom4.contains([102, 51])) # Edge cases # The upper edge is not included self.assertFalse(geom4.contains([101.5, 101.25])) self.assertFalse(geom4.contains([101.5, 101])) self.assertFalse(geom4.contains([101, 101.25])) # The lower is self.assertTrue(geom4.contains([0.5, 0.75])) self.assertTrue(geom4.contains([0.5, 1])) self.assertTrue(geom4.contains([1, 0.75]))
def test_call_evalute_with_spline(self): # Teting call is the same as evalute_with_spline hg = gd.HierarchicalGridData(self.grid_data) # Test with multiple components hg3 = gd.HierarchicalGridData(self.grid_data_two_comp) # Scalar input self.assertAlmostEqual(hg((2, 3)), 10) self.assertAlmostEqual(hg3((2, 3)), 10) # Vector input in, vector input out self.assertEqual(hg([(2, 3)]).shape, (1,)) # Scalar input that pretends to be vector self.assertAlmostEqual(hg([(2, 3)]), 10) self.assertAlmostEqual(hg3([(2, 3)]), 10) # Vector input self.assertCountEqual(hg([(2, 3), (3, 2)]), [10, 12]) self.assertCountEqual(hg3([(2, 3), (3, 2)]), [10, 12]) def product(x, y): return x * (y + 2) # Uniform grid as input grid = gd.UniformGrid([3, 5], x0=[0, 1], x1=[2, 5]) grid_data = gdu.sample_function_from_uniformgrid(product, grid) self.assertTrue(np.allclose(hg3(grid), grid_data.data))
def test_init(self): # Test incorrect arguments # Not a list with self.assertRaises(TypeError): gd.HierarchicalGridData(0) # Empty list with self.assertRaises(ValueError): gd.HierarchicalGridData([]) # Not a list of UniformGridData with self.assertRaises(TypeError): gd.HierarchicalGridData([0]) # Inconsistent number of dimensions def product1(x): return x def product2(x, y): return x * y prod_data1 = gdu.sample_function(product1, [101], [0], [3]) prod_data2 = gdu.sample_function(product2, [101, 101], [0, 0], [3, 3]) with self.assertRaises(ValueError): gd.HierarchicalGridData([prod_data1, prod_data2]) # Only one component one = gd.HierarchicalGridData([prod_data1]) # Test content self.assertDictEqual( one.grid_data_dict, {-1: [prod_data1.ghost_zones_removed()]} ) grid = gd.UniformGrid([101], x0=[0], x1=[3], ref_level=2) # Two components at two different levels prod_data1_level2 = gdu.sample_function_from_uniformgrid( product1, grid ) two = gd.HierarchicalGridData([prod_data1, prod_data1_level2]) self.assertDictEqual( two.grid_data_dict, { -1: [prod_data1.ghost_zones_removed()], 2: [prod_data1_level2.ghost_zones_removed()], }, ) # Test a good grid hg_many_components = gd.HierarchicalGridData(self.grid_data) self.assertEqual( hg_many_components.grid_data_dict[0], [self.expected_data] ) # Test a grid with two separate components hg3 = gd.HierarchicalGridData(self.grid_data_two_comp) self.assertEqual(hg3.grid_data_dict[0], self.grid_data_two_comp)
def test_finest_coarsest_level(self): geom = gd.UniformGrid( [81, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=0 ) geom2 = gd.UniformGrid( [11, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=1 ) sin_wave1 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom ) sin_wave2 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom2 ) sin_wave = gd.HierarchicalGridData([sin_wave1] + [sin_wave2]) self.assertEqual(sin_wave.finest_level, sin_wave2) self.assertEqual(sin_wave.coarsest_level, sin_wave1)
def test_iter(self): hg1 = gd.HierarchicalGridData(self.grid_data) for ref_level, comp, data in hg1: self.assertTrue(isinstance(data, gd.UniformGridData)) self.assertEqual(ref_level, 0) self.assertEqual(comp, 0) hg3 = gd.HierarchicalGridData(self.grid_data_two_comp) comp_index = 0 for ref_level, comp, data in hg3: self.assertEqual(ref_level, 0) self.assertEqual(comp, comp_index) self.assertTrue(isinstance(data, gd.UniformGridData)) comp_index += 1 # Test from finest geom = gd.UniformGrid( [81, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=0 ) geom2 = gd.UniformGrid( [11, 3], x0=[0, 0], x1=[2 * np.pi, 1], ref_level=1 ) sin_wave1 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom ) sin_wave2 = gdu.sample_function_from_uniformgrid( lambda x, y: np.sin(x), geom2 ) sin_wave = gd.HierarchicalGridData([sin_wave1] + [sin_wave2]) index = 1 for ref_level, comp, data in sin_wave.iter_from_finest(): self.assertEqual(ref_level, index) self.assertEqual(comp, 0) self.assertTrue(isinstance(data, gd.UniformGridData)) index -= 1
def test__str(self): geom4 = gd.UniformGrid( [101, 101], x0=[1, 1], dx=[1, 0.5], num_ghost=[3, 3], time=1, iteration=1, ) self.assertIn("Num ghost zones = [3 3]", geom4.__str__())
def test_flat_dimensions_removed(self): geom = gd.UniformGrid( [101, 101, 1], x0=[1, 1, 0], dx=[1, 0.5, 0], num_ghost=[3, 3, 3], time=1, iteration=1, ) geom2 = gd.UniformGrid( [101, 101], x0=[1, 1], dx=[1, 0.5], num_ghost=[3, 3], time=1, iteration=1, ) self.assertEqual(geom.flat_dimensions_removed(), geom2)
def test__eq__(self): # The tricky part is the time and iteration geom0 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5]) geom1 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5]) self.assertEqual(geom0, geom1) geom2 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5], time=1) self.assertNotEqual(geom0, geom2) geom3 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5], time=1) self.assertEqual(geom3, geom2) geom4 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5], iteration=1) self.assertNotEqual(geom0, geom4) geom5 = gd.UniformGrid([11, 11], x0=[0, 0], x1=[5, 5], iteration=1) self.assertEqual(geom5, geom4) self.assertNotEqual(geom5, 2)
def test_init(self): self.assertCountEqual(self.rho_star._iterations_to_times, { 0: 0, 1: 0.25, 2: 0.5 }) # TODO: This test doesn't test compressed files nor 3D files, # and it tests only one component/refinement level/iteration # The first component ends at line 880 of rho_star.xy.asc # Let's check this first one # # We have to remove the blank lines from the count expected_data = np.loadtxt(self.rho_star_file, usecols=(12, ), max_rows=875) expected_grid = grid_data.UniformGrid( [29, 29], x0=[-14, -14], x1=[14, 14], ref_level=0, num_ghost=[3, 3], time=0, iteration=0, component=0, ) expected_grid_data = grid_data.UniformGridData( expected_grid, expected_data.reshape((29, 29)).T) self.assertEqual( self.rho_star._read_component_as_uniform_grid_data( self.rho_star_file, 0, 0, 0), expected_grid_data, ) # Test time_at_iteration self.assertEqual(self.rho_star.time_at_iteration(2), 0.5) # Iteration not available with self.assertRaises(ValueError): self.rho_star.time_at_iteration(20) # Test file with wrong name with self.assertRaises(RuntimeError): self.rho_star._parse_file("/tmp/wrongname")
def test_copy(self): geom = gd.UniformGrid( [101, 101, 1], x0=[1, 1, 0], dx=[1, 0.5, 0], num_ghost=[3, 3, 3], time=1, iteration=1, ) geom2 = geom.copy() self.assertEqual(geom, geom2) self.assertIsNot(geom, geom2)
def test_merge_uniform_grids(self): # Test error for not passing a list with self.assertRaises(TypeError): gdu.merge_uniform_grids(1) # Test error for not passing a list of UniformGrid with self.assertRaises(TypeError): gdu.merge_uniform_grids([1, 2]) geom1 = gd.UniformGrid([101, 101], x0=[1, 1], x1=[3, 5], ref_level=1) geom2 = gd.UniformGrid([101, 101], x0=[1, 1], x1=[10, 5], ref_level=2) # Different ref levels with self.assertRaises(ValueError): gdu.merge_uniform_grids([geom1, geom2]) geom3 = gd.UniformGrid([101, 101], x0=[1, 1], x1=[10, 5], ref_level=1) # Different dx with self.assertRaises(ValueError): gdu.merge_uniform_grids([geom1, geom3]) geom4 = gd.UniformGrid([101, 101], x0=[0, -2], dx=geom1.dx, ref_level=1) expected_geom = gd.UniformGrid([151, 176], x0=[0, -2], x1=[3, 5], dx=geom1.dx, ref_level=1) self.assertEqual(gdu.merge_uniform_grids([geom1, geom4]), expected_geom)
def test_slice(self): grid_data = gdu.sample_function_from_uniformgrid( lambda x, y, z: x * (y + 2) * (z + 5), gd.UniformGrid([10, 20, 30], x0=[0, 1, 2], dx=[1, 2, 0.1]), ) grid_data_copied = grid_data.copy() # Test cut is wrong dimension with self.assertRaises(ValueError): grid_data.slice([1, 2]) # Test no cut grid_data.slice([None, None, None]) self.assertEqual(grid_data, grid_data_copied) # Test cut point outside the grid with self.assertRaises(ValueError): grid_data.slice([1, 2, 1000]) # Test resample # Test cut along one dimension grid_data.slice([None, None, 3], resample=True) expected_no_z = gdu.sample_function_from_uniformgrid( lambda x, y: x * (y + 2) * (3 + 5), gd.UniformGrid([10, 20], x0=[0, 1], dx=[1, 2]), ) self.assertEqual(grid_data, expected_no_z) # Test no resample # # Reset grid data grid_data = grid_data_copied.copy() grid_data.slice([None, None, 3], resample=False) self.assertEqual(grid_data, expected_no_z)
def merge_uniform_grids(grids, component=-1): """Return a new grid that covers all the grids in the list. All geometries must belong to the same refinement level and have the same grid spacing. :param geoms: list of grid geometries. :type geoms: list of :py:class:`~.UniformGrid` :returns: Grid geometry covering all input grids. :rtype: :py:class:`~.UniformGrid` """ # Let's check that grids is a list like objects if not hasattr(grids, "__len__"): raise TypeError("merge_uniform_grids takes a list") if not all(isinstance(g, gd.UniformGrid) for g in grids): raise TypeError("merge_uniform_grids works only with UniformGrid") # Check that all the grids have the same refinement levels ref_levels = {g.ref_level for g in grids} if len(ref_levels) != 1: raise ValueError("Can only merge grids on same refinement level.") # Extract the only element from the set (using tuple unpacking) (ref_level, ) = ref_levels dx = [g.dx for g in grids] if not np.allclose(dx, dx[0]): raise ValueError("Can only merge grids with the same spacing.") # Find the bounding box x0, x1 = common_bounding_box(grids) # The additional 1.5 and 0.5 factors are because the points are # cell-centered, so the cells have size # dx here is a list of all the dx, we just want one (they are all the same) shape = ((x1 - x0) / dx[0] + 1.5).astype(np.int64) return gd.UniformGrid(shape, x0=x0, dx=dx[0], ref_level=ref_level, component=component)
def test_coordinate_to_indices(self): geom = gd.UniformGrid([101, 51], x0=[1, 2], dx=[1, 0.5]) # Scalar input self.assertCountEqual(geom.indices_to_coordinates([1, 3]), [2, 3.5]) self.assertCountEqual(geom.coordinates_to_indices([2, 3.5]), [1, 3]) # Vector input self.assertTrue( np.allclose( geom.indices_to_coordinates([[1, 3], [2, 4]]), [[2, 3.5], [3, 4]], ) ) self.assertTrue( np.allclose( geom.coordinates_to_indices([[2, 3.5], [3, 4]]), [[1, 3], [2, 4]], ) )
def test__getitem__(self): geom4 = gd.UniformGrid( [11, 15], x0=[1, 1], dx=[1, 0.5], num_ghost=[3, 3], time=1, iteration=1, ) with self.assertRaises(ValueError): geom4[1] self.assertCountEqual(geom4[1, 3], [2, 2.5]) # Check index outside of the grid with self.assertRaises(ValueError): geom4[[500, 200]]
def test_get(self): # Iteration not present with self.assertRaises(KeyError): self.P[9] # Let's read iteration 0, we have two ref levels and two components data00 = self.P._read_component_as_uniform_grid_data( self.P_file, 0, 0, 0 ) data01 = self.P._read_component_as_uniform_grid_data( self.P_file, 0, 0, 1 ) data10 = self.P._read_component_as_uniform_grid_data( self.P_file, 0, 1, 0 ) data11 = self.P._read_component_as_uniform_grid_data( self.P_file, 0, 1, 1 ) expected_hgd = grid_data.HierarchicalGridData( [data00, data01, data10, data11] ) self.assertEqual(self.P[0], expected_hgd) # default self.assertIs(self.P.get_iteration(3), None) self.assertIs(self.P.get_time(3), None) self.assertEqual(self.P.get_iteration(0), self.P[0]) self.assertEqual(self.P.get_time(0.5), self.P[2]) # Test iteration self.assertEqual(next(iter(self.P)), self.P[0]) new_grid = grid_data.UniformGrid([10, 10], x0=[1, 1], x1=[2, 2]) self.assertEqual( self.P.read_on_grid(0, new_grid), expected_hgd.to_UniformGridData_from_grid(new_grid), )
def test_coordinates(self): geom4 = gd.UniformGrid( [11, 15], x0=[1, 2], dx=[1, 0.5], num_ghost=[3, 3], time=1, iteration=1, ) x = np.linspace(1, 11, 11) y = np.linspace(2, 9, 15) # Test coordinates_1d self.assertTrue(np.allclose(geom4.coordinates_1d[0], x)) self.assertTrue(np.allclose(geom4.coordinates_1d[1], y)) c0 = geom4.coordinates(as_meshgrid=True) X, Y = np.meshgrid(x, y) self.assertTrue(np.allclose(c0[0], X)) self.assertTrue(np.allclose(c0[1], Y)) c1 = geom4.coordinates() self.assertTrue(np.allclose(c1[0], x)) self.assertTrue(np.allclose(c1[1], y)) with self.assertRaises(ValueError): geom4.coordinates(as_meshgrid=True, as_same_shape=True) # Here the output is a list of coordinates shaped like the array itself shaped_array = geom4.coordinates(as_same_shape=True) self.assertCountEqual(shaped_array[0].shape, geom4.shape) # We check that the first column is the same as the coordinates self.assertTrue( np.allclose(shaped_array[0][:, 0], geom4.coordinates()[0]) )