def test_element_volume_data(self): # Set up Extents and Tensor Volume data tensor_component_1 = TensorComponent("tensor component one", DataVector([1.5, 1.1])) tensor_component_2 = TensorComponent("tensor component two", DataVector([7.1, 5])) basis = Basis.Legendre quad = Quadrature.Gauss element_data = ElementVolumeData( [1, 2, 3, 4], [tensor_component_1, tensor_component_2], [basis, basis], [quad, quad]) # Test extents self.assertEqual(element_data.extents, [1, 2, 3, 4]) element_data.extents = [5, 6, 7, 8] self.assertEqual(element_data.extents, [5, 6, 7, 8]) # Test tensor components self.assertEqual(element_data.tensor_components, [tensor_component_1, tensor_component_2]) element_data.tensor_components = [ tensor_component_1, tensor_component_1 ] self.assertEqual(element_data.tensor_components, [tensor_component_1, tensor_component_1]) # Test str, repr self.assertEqual( str(element_data), "((5,6,7,8),((tensor component one, (1.5,1.1)" "),(tensor component one, (1.5,1.1))))") self.assertEqual( repr(element_data), "((5,6,7,8),((tensor component one, (1.5,1.1)" "),(tensor component one, (1.5,1.1))))") # Test basis and quadrature self.assertEqual(element_data.basis, [basis, basis]) self.assertEqual(element_data.quadrature, [quad, quad])
def test_numpy_compatibility(self): b = np.array([1.0, 2.0, 3.0]) c = DataVector([1.0, 2.0, 3.0]) self.assertTrue(((b + c) == np.array([2.0, 4.0, 6.0])).all()) x = np.linspace(0, 2 * np.pi, 10) self.assertTrue((DataVector(list(x)).sin() == np.sin(x)).all()) # Convert a DataVector to a Numpy array c_array_copy = np.array(c) npt.assert_equal(c_array_copy, b) # Changing the copy shouldn't change the DataVector c_array_copy[2] = 4.0 npt.assert_equal(c, b) c_array_reference = np.array(c, copy=False) npt.assert_equal(c_array_reference, b) # Changing the reference should change the DataVector as well c_array_reference[2] = 4.0 self.assertEqual(c[2], 4.0) # Convert a Numpy array to a DataVector b_dv_copy = DataVector(b) self.assertEqual(b_dv_copy, DataVector([1.0, 2.0, 3.0])) b_dv_copy[2] = 4.0 self.assertEqual(b[2], 3.0) b_dv_reference = DataVector(b, copy=False) b_dv_reference[2] = 4.0 self.assertEqual(b[2], 4.0)
def test_extents_and_tensor_volume_data(self): # Set up Extents and Tensor Volume data tensor_component_1 = TensorComponent("tensor component one", DataVector([1.5, 1.1])) tensor_component_2 = TensorComponent("tensor component two", DataVector([7.1, 5])) extents_and_data = ExtentsAndTensorVolumeData( [1, 2, 3, 4], [tensor_component_1, tensor_component_2]) # Test extents self.assertEqual(extents_and_data.extents, [1, 2, 3, 4]) extents_and_data.extents = [5, 6, 7, 8] self.assertEqual(extents_and_data.extents, [5, 6, 7, 8]) # Test tensor components self.assertEqual(extents_and_data.tensor_components, [tensor_component_1, tensor_component_2]) extents_and_data.tensor_components = [ tensor_component_1, tensor_component_1 ] self.assertEqual(extents_and_data.tensor_components, [tensor_component_1, tensor_component_1]) # Test str, repr self.assertEqual( str(extents_and_data), "((5,6,7,8),((tensor component one, (1.5,1.1)" "),(tensor component one, (1.5,1.1))))") self.assertEqual( repr(extents_and_data), "((5,6,7,8),((tensor component one, (1.5,1.1)" "),(tensor component one, (1.5,1.1))))")
def test_atan2(self): a = DataVector(5, 0.67) b = DataVector(5, -4.0) self.assertEqual(a.atan2(b), DataVector(5, math.atan2(0.67, -4.0))) x = DataVector([3, 100]) y = DataVector([1, -3]) z = DataVector([math.atan2(3, 1), math.atan2(100, -3)]) self.assertEqual(x.atan2(y), z)
def test_hypot(self): a = DataVector(5, 3.0) b = DataVector(5, 4.0) self.assertEqual(a.hypot(b)[0], 5.0) x = DataVector([0.0, 3.0]) y = DataVector([5.0, 4.0]) self.assertEqual(x.hypot(y), DataVector(2, 5.0))
def test_bounds_check(self): a = DataVector(5, 6.7) self.assertRaises(RuntimeError, lambda: a[5]) def assignment_test(): a[5] = 8 self.assertRaises(RuntimeError, assignment_test)
def test_tensor_component(self): # Set up Tensor Component tensor_component = TensorComponent("tensor component", DataVector([1.5, 1.1])) # Test name self.assertEqual(tensor_component.name, "tensor component") tensor_component.name = "new tensor component" self.assertEqual(tensor_component.name, "new tensor component") # Test data npt.assert_array_almost_equal(np.array(tensor_component.data), np.array([1.5, 1.1])) tensor_component.data = DataVector([6.7, 3.2]) npt.assert_array_almost_equal(np.array(tensor_component.data), np.array([6.7, 3.2])) # Test str, repr self.assertEqual(str(tensor_component), "(new tensor component, (6.7,3.2))") self.assertEqual(repr(tensor_component), "(new tensor component, (6.7,3.2))")
def test_math_datavector(self): a = DataVector(5, 6.7) b = DataVector(5, 8.7) self.assertEqual(a + b, DataVector(5, 6.7 + 8.7)) self.assertEqual(a - b, DataVector(5, 6.7 - 8.7)) self.assertEqual(a * b, DataVector(5, 6.7 * 8.7)) self.assertEqual(a / b, DataVector(5, 6.7 / 8.7)) c = DataVector([1.5, 2.5]) d = DataVector([3.0, 7.5]) self.assertEqual((c + d)[0], 4.5) self.assertEqual((c + d)[1], 10.0) self.assertEqual((c - d)[0], -1.5) self.assertEqual((c - d)[1], -5.0) self.assertEqual((c * d)[0], 4.5) self.assertEqual((c * d)[1], 18.75) self.assertEqual((c / d)[0], 0.5) self.assertEqual((c / d)[1], 1.0 / 3.0)
def test_list_constructor(self): a = DataVector([1.0, 2.0, 3.0, 4.0, 5.0]) self.assertEqual(len(a), 5) self.assertEqual(a[3], 4.0)
def test_size_constructor(self): a = DataVector(3) self.assertEqual(len(a), 3)
def test_iterator(self): a = DataVector([1.0, 2.0, 3.0, 4.0, 5.0]) for i, val in enumerate(a): self.assertEqual(val, a[i])
def test_log2(self): a = DataVector(5, 4.0) self.assertEqual(a.log2(), DataVector(5, 2.0)) b = DataVector([1.0, 0.5]) self.assertEqual(b.log2()[0], 0.0) self.assertEqual(b.log2()[1], -1.0)
def interpolate_h5_file(source_file_path, target_mesh, target_file_path, source_volume_data, target_volume_data, components_to_interpolate=None, obs_start=-np.inf, obs_end=np.inf, obs_stride=1): """Interpolates an h5 file to a desired grid The function reads data from `source_volume_data` inside `source_file_path`, interpolates all components specified by `components_to_interpolate` to the grid specified by `target_mesh` and writes the results into `target_volume_data` inside `target_file_path`. The `target_file_path` can be the same as the `source_file_path` if the volume subfile paths are different. Parameters ---------- source_file_path: str the path to the source file where the `source_volume_data` is target_mesh: spectre.Spectral.Mesh the mesh to which the data is interpolated components_to_interpolate: list of str, optional a list of all components that are to be interpolated. accepts regular expressions. By default ALL tensor components are interpolated. target_file_path: str, optional the path to where the interpolated data is written. By default this is set to `source_file_path` so the interpolated data is written to the same file, but in a different subfile specified by `target_volume_data`. source_volume_data: str, optional the name of the .vol file inside the source file where the source data can be found. Requires leading `/` but no `.vol` file extension. target_volume_data: str, optional the name of the .vol file inside the target file where the target data is written. Requires leading `/` but no `.vol` file extension. obs_start: float, optional disregards all observations with observation value strictly before `obs_start` obs_end: float, optional disregards all observations with observation value strictly after `obs_end` obs_stride: float, optional will only take every `obs_stride` observation """ if target_file_path == source_file_path: if source_volume_data == target_volume_data: raise NameError( "If the source and target files are the same, " "the source and target volume_data need to be different.") source_file = spectre_h5.H5File(source_file_path, "r+") target_file = source_file else: source_file = spectre_h5.H5File(source_file_path, "r") target_file = spectre_h5.H5File(target_file_path, "a") source_vol = source_file.get_vol(source_volume_data) dim = source_vol.get_dimension() # apply observation filter observations = [ obs for obs in source_vol.list_observation_ids() if obs_start <= source_vol.get_observation_value(obs) <= obs_end ][::obs_stride] target_file.insert_vol(target_volume_data, source_vol.get_version()) for obs in observations: # the vols memory address may shift as we write to file, # so we need to get them every iteration source_vol = source_file.get_vol(source_volume_data) extents = source_vol.get_extents(obs) bases = source_vol.get_bases(obs) quadratures = source_vol.get_quadratures(obs) tensor_names = source_vol.list_tensor_components(obs) grid_names = source_vol.get_grid_names(obs) obs_value = source_vol.get_observation_value(obs) if components_to_interpolate is not None: tensor_names = list( set(tensor_name for pattern in components_to_interpolate for tensor_name in tensor_names if re.match(pattern, tensor_name))) # pre-load all tensors to avoid loading the full tensor for each element tensors = [ np.array(source_vol.get_tensor_component(obs, name), copy=False) for name in tensor_names ] volume_data = [] # iterate over elements for grid_name, extent, basis, quadrature in zip( grid_names, extents, bases, quadratures): source_mesh = Mesh[dim](extent, basis_from_string(basis), quadrature_from_string(quadrature)) interpolant = RegularGrid[dim](source_mesh, target_mesh) tensor_comps = [] offset, length = spectre_h5.offset_and_length_for_grid( grid_name, grid_names, extents) # iterate over tensors for j, tensor in enumerate(tensors): component_data = DataVector(tensor[offset:offset + length], copy=False) interpolated_tensor = interpolant.interpolate(component_data) tensor_path = "{}/{}".format(grid_name, tensor_names[j]) tensor_comps.append( TensorComponent( tensor_path, DataVector(interpolated_tensor, copy=False))) volume_data.append( ElementVolumeData(target_mesh.extents(), tensor_comps, target_mesh.basis(), target_mesh.quadrature())) target_vol = target_file.get_vol(target_volume_data) target_vol.write_volume_data(obs, obs_value, volume_data) source_file.close() if not target_file is source_file: target_file.close()
def test_abs(self): a = DataVector(5, -6.0) self.assertEqual(a.abs(), DataVector(5, 6.0)) b = DataVector([1.0, -2.0]) self.assertEqual(b.abs()[0], 1.0) self.assertEqual(b.abs()[1], 2.0)
def test_sinh(self): a = DataVector(5, 3.0) self.assertEqual(a.sinh(), DataVector(5, math.sinh(3.0))) b = DataVector([0.0, -0.5]) self.assertEqual(b.sinh()[0], 0.0) self.assertEqual(b.sinh()[1], math.sinh(-0.5))
def test_sin(self): a = DataVector(5, 6.7) self.assertEqual(a.sin(), DataVector(5, math.sin(6.7))) b = DataVector([0.0, -0.5]) self.assertEqual(b.sin()[0], 0.0) self.assertEqual(b.sin()[1], math.sin(-0.5))
def test_pow(self): a = DataVector(5, 4.0) self.assertEqual(a.pow(2), DataVector(5, 16.0)) b = DataVector([1.0, -0.5]) self.assertEqual(b.pow(2)[0], 1.0) self.assertEqual(b.pow(2)[1], 0.25)
def test_min(self): a = DataVector([4.0, 2.0, 4.0, 4.0, 4.0]) self.assertEqual(a.min(), 2.0) b = DataVector([1.0, -0.5]) self.assertEqual(b.min(), -0.5)
def test_max(self): a = DataVector([4.0, 5.0, 4.0, 4.0, 4.0]) self.assertEqual(a.max(), 5.0) b = DataVector([1.0, 0.5]) self.assertEqual(b.max(), 1.0)
def test_log10(self): a = DataVector(5, 100.0) self.assertEqual(a.log10(), DataVector(5, 2.0)) b = DataVector([1.0, 0.1]) self.assertEqual(b.log10()[0], 0.0) self.assertEqual(b.log10()[1], -1.0)
def test_negate(self): a = DataVector(5, 6.7) self.assertEqual(-a, DataVector(5, -6.7)) b = DataVector([1.5, 3.7]) self.assertEqual((-b)[0], -1.5) self.assertEqual((-b)[1], -3.7)
def test_sqrt(self): a = DataVector(5, 4.0) self.assertEqual(a.sqrt(), DataVector(5, 2.0)) b = DataVector([1.0, 0.25]) self.assertEqual(b.sqrt()[0], 1.0) self.assertEqual(b.sqrt()[1], 0.5)
def test_output(self): a = DataVector(5, 6.7) self.assertEqual(str(a), "(6.7,6.7,6.7,6.7,6.7)") b = DataVector([3.8, 7.2]) self.assertEqual(str(b), "(3.8,7.2)")
def test_step_function(self): a = DataVector(5, 4.0) self.assertEqual(a.step_function(), DataVector(5, 1.0)) b = DataVector([1.0, -0.5]) self.assertEqual(b.step_function()[0], 1.0) self.assertEqual(b.step_function()[1], 0.0)
def test_acos(self): a = DataVector(5, 0.67) self.assertEqual(a.acos()[0], math.acos(0.67)) b = DataVector([1.0, -0.5]) self.assertEqual(b.acos()[0], 0) self.assertEqual(b.acos()[1], math.acos(-.5))
def test_invsqrt(self): a = DataVector(5, 4.0) self.assertEqual(a.invsqrt(), DataVector(5, 0.5)) b = DataVector([1.0, 16.0]) self.assertEqual(b.invsqrt()[0], 1.0) self.assertEqual(b.invsqrt()[1], 0.25)
def test_invcbrt(self): a = DataVector(5, 8.0) self.assertEqual(a.invcbrt(), DataVector(5, 0.5)) b = DataVector([1.0, -64.0]) self.assertEqual(b.invcbrt()[0], 1.0) self.assertEqual(b.invcbrt()[1], -0.25)
def test_log(self): a = DataVector(5, 4.0) self.assertEqual(a.log(), DataVector(5, math.log(4.0))) b = DataVector([1.0, 0.5]) self.assertEqual(b.log()[0], 0.0) self.assertEqual(b.log()[1], math.log(0.5))
def test_tanh(self): a = DataVector(5, 4.0) self.assertEqual(a.tanh(), DataVector(5, math.tanh(4.0))) b = DataVector([0.0, -0.5]) self.assertEqual(b.tanh()[0], 0.0) self.assertEqual(b.tanh()[1], math.tanh(-0.5))
def test_numpy_compatibility(self): b = np.array([1.0, 2.0, 3.0]) c = DataVector([1.0, 2.0, 3.0]) self.assertTrue(((b + c) == np.array([2.0, 4.0, 6.0])).all()) x = np.linspace(0, 2 * np.pi, 10) self.assertTrue((DataVector(list(x)).sin() == np.sin(x)).all())