diff --git a/openmc/mesh.py b/openmc/mesh.py index b883a4a096..ada69887ad 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -635,16 +635,22 @@ class RegularMesh(StructuredMesh): Returns: vtk.vtkStructuredGrid: the VTK object + + Raises: + RuntimeError: when the size of a dataset doesn't match the number of cells """ import vtk 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(): if isinstance(dataset, np.ndarray): - assert dataset.size == self.dimension[0] * self.dimension[1] * self.dimension[2] + if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) else: - assert len(dataset) == self.dimension[0] * self.dimension[1] * self.dimension[2] + if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) cv.check_type('label', label, str) x_vals = np.linspace( @@ -907,16 +913,22 @@ class RectilinearMesh(StructuredMesh): Returns: vtk.vtkStructuredGrid: the VTK object + + Raises: + RuntimeError: when the size of a dataset doesn't match the number of cells """ import vtk 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(): if isinstance(dataset, np.ndarray): - assert dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2] + if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) else: - assert len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2] + if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) cv.check_type('label', label, str) x_vals = self.x_grid @@ -1160,16 +1172,22 @@ class CylindricalMesh(StructuredMesh): Returns: vtk.vtkStructuredGrid: the VTK object + + Raises: + RuntimeError: when the size of a dataset doesn't match the number of cells """ import vtk 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(): if isinstance(dataset, np.ndarray): - assert dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2] + if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) else: - assert len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2] + if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) cv.check_type('label', label, str) vtk_grid = vtk.vtkStructuredGrid() @@ -1414,16 +1432,23 @@ class SphericalMesh(StructuredMesh): Returns: vtk.vtkStructuredGrid: the VTK object + + Raises: + RuntimeError: when the size of a dataset doesn't match the number of cells """ import vtk 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(): if isinstance(dataset, np.ndarray): - assert dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2] + if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) else: - assert len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2] + if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) + cv.check_type('label', label, str) vtk_grid = vtk.vtkStructuredGrid() @@ -1642,6 +1667,11 @@ class UnstructuredMesh(MeshBase): volume_normalization : bool Whether or not to normalize the data by the volume of the mesh elements + + Raises + ------ + RuntimeError + when the size of a dataset doesn't match the number of cells """ import vtk @@ -1658,11 +1688,14 @@ class UnstructuredMesh(MeshBase): " mesh information from a statepoint file.") # 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(): if isinstance(dataset, np.ndarray): - assert dataset.size == self.n_elements + if not dataset.size == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) else: - assert len(dataset) == self.n_elements + if len(dataset) == self.dimension[0] * self.dimension[1]* self.dimension[2]: + raise RuntimeError(errmsg.format(label)) cv.check_type('label', label, str) # create data arrays for the cells/points diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index f651f5acb5..9022feecd0 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -56,4 +56,19 @@ def test_write_data_to_vtk(mesh, tmpdir): # check size of datasets assert nps.vtk_to_numpy(array1).size == data.size - assert nps.vtk_to_numpy(array2).size == data.size \ No newline at end of file + 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 + doesn't match the mesh number of cells + + Args: + mesh (openmc.StructuredMesh): the mesh to test + """ + right_size = mesh.dimension[0]*mesh.dimension[1]*mesh.dimension[2] + 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