diff --git a/openmc/mesh.py b/openmc/mesh.py index af830158c..8ac4b1594 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -216,8 +216,8 @@ class StructuredMesh(MeshBase): Raises ------ - RuntimeError - When the size of a dataset doesn't match the number of cells + ValueError + When the size of a dataset doesn't match the number of mesh cells Returns ------- @@ -229,14 +229,17 @@ class StructuredMesh(MeshBase): from vtk.util import numpy_support as nps # check that the data sets are appropriately sized - errmsg = "The size of the dataset {} should be equal to the number of cells" for label, dataset in datasets.items(): + errmsg = ( + f"The size of the dataset '{label}' ({dataset.size}) should be" + f" equal to the number of mesh cells ({self.num_mesh_cells})" + ) if isinstance(dataset, np.ndarray): - if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: - raise RuntimeError(errmsg.format(label)) + if not dataset.size == self.num_mesh_cells: + raise ValueError(errmsg) else: - if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: - raise RuntimeError(errmsg.format(label)) + if len(dataset) == self.num_mesh_cells: + raise ValueError(errmsg) cv.check_type('label', label, str) vtk_grid = vtk.vtkStructuredGrid() diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index 26a393b41..d7d5907a2 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -28,6 +28,7 @@ spherical_mesh.r_grid = np.linspace(1, 2, num=30) spherical_mesh.phi_grid = np.linspace(0, np.pi, num=50) spherical_mesh.theta_grid = np.linspace(0, np.pi / 2, num=30) + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk(mesh, tmpdir): # BUILD @@ -43,7 +44,7 @@ def test_write_data_to_vtk(mesh, tmpdir): # read file reader = vtk.vtkStructuredGridReader() - reader.SetFileName(filename) + reader.SetFileName(str(filename)) reader.Update() # check name of datasets @@ -58,6 +59,7 @@ def test_write_data_to_vtk(mesh, tmpdir): assert nps.vtk_to_numpy(array1).size == data.size assert nps.vtk_to_numpy(array2).size == data.size + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk_size_mismatch(mesh): """Checks that an error is raised when the size of the dataset @@ -71,6 +73,12 @@ def test_write_data_to_vtk_size_mismatch(mesh): right_size = mesh.num_mesh_cells data = np.random.random(right_size + 1) - expected_error_msg = "The size of the dataset label should be equal to the number of cells" - with pytest.raises(RuntimeError, match=expected_error_msg): - mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data}) \ No newline at end of file + # Error message has \ in to escape characters that are otherwise recognized + # by regex. These are needed to make the test string match the error message + # string when using the match argument as that uses regular expression + expected_error_msg = ( + f"The size of the dataset 'label' \({len(data)}\) should be equal to " + f"the number of mesh cells \({mesh.num_mesh_cells}\)" + ) + with pytest.raises(ValueError, match=expected_error_msg): + mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})