Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adding another test that uses VTK to gather the data directly.
Co-authored-by: Jonathan Shimwell <jonathan.shimwell@firstlightfusion.com>
  • Loading branch information
pshriwise and shimwell committed Mar 1, 2023
commit 728f48c2aef77fadcabef52fdaae33de0dd9323a
58 changes: 58 additions & 0 deletions tests/unit_tests/test_mesh_to_vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,61 @@ def test_write_data_to_vtk_size_mismatch(mesh):
)
with pytest.raises(ValueError, match=expected_error_msg):
mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})

def test_write_data_to_vtk_round_trip(run_in_tmpdir):
cmesh = openmc.CylindricalMesh()
cmesh.r_grid = (0.0, 1.0, 2.0)
cmesh.z_grid = (0.0, 2.0, 4.0, 5.0)
cmesh.phi_grid = (0.0, 3.0, 6.0)

smesh = openmc.SphericalMesh()
smesh.r_grid = (0.0, 1.0, 2.0)
smesh.theta_grid = (0.0, 2.0, 4.0, 5.0)
smesh.phi_grid = (0.0, 3.0, 6.0)

rmesh = openmc.RegularMesh()
rmesh.lower_left = (0.0, 0.0, 0.0)
rmesh.upper_right = (1.0, 3.0, 5.0)
rmesh.dimension = (2, 1, 6)

for mesh in [smesh, cmesh, rmesh]:

filename = "mesh.vtk"
data = np.array([1.0] * 12) # there are 12 voxels in each mesh
mesh.write_data_to_vtk(
filename=filename,
datasets={"normalized": data},
volume_normalization=True
)

reader = vtk.vtkStructuredGridReader()
reader.SetFileName(filename)
reader.ReadAllFieldsOn()
reader.Update()

cell_data = reader.GetOutput().GetCellData()
uniform_array = cell_data.GetArray("normalized")
num_tuples = uniform_array.GetNumberOfTuples()
vtk_values = [uniform_array.GetValue(i) for i in range(num_tuples)]

# checks that the vtk cell values are equal to the data / mesh volumes
assert np.allclose(vtk_values, data / mesh.volumes.T.flatten())

mesh.write_data_to_vtk(
filename=filename,
datasets={"not_normalized": data},
volume_normalization=False,
)

reader = vtk.vtkStructuredGridReader()
reader.SetFileName(filename)
reader.ReadAllFieldsOn()
reader.Update()

cell_data = reader.GetOutput().GetCellData()
uniform_array = cell_data.GetArray("not_normalized")
num_tuples = uniform_array.GetNumberOfTuples()
vtk_values = [uniform_array.GetValue(i) for i in range(num_tuples)]

# checks that the vtk cell values are equal to the data
assert np.array_equal(vtk_values, data)