Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VTU export for Unstructured meshes #3290

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

rherrero-pf
Copy link

@rherrero-pf rherrero-pf commented Feb 4, 2025

Description

Added method to export an unstructured mesh in .vtu format, and removed the dependency on the VTK module, which clashes with CadQuery.

Fixes # (issue)
Fixes issue #3289

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

@rherrero-pf rherrero-pf changed the title Rherrero add vtu export umesh Add VTU export for Unstructured meshes, and remove dependency on VTK Feb 4, 2025
@rherrero-pf rherrero-pf changed the title Add VTU export for Unstructured meshes, and remove dependency on VTK Add VTU export for Unstructured meshes Feb 4, 2025
@rherrero-pf
Copy link
Author

@shimwell With this PR one should be able to mesh CadQuery shapes, and export legacy VTK, or VTU, without running into module conflicts

openmc/mesh.py Outdated Show resolved Hide resolved
Copy link
Contributor

@pshriwise pshriwise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rherrero-pf for the contribution! The additional format support will be nice to have. And thank you for updating the VTK imports.

Mostly comments on the new test here.

Comment on lines +470 to +480
if export_type == ".vtk":
reader = vtkIOLegacy.vtkGenericDataObjectReader()
reader.SetFileName(str(filename))
reader.Update()
assert reader.GetOutput().GetCellData().GetArray("mean")

elif export_type == ".vtk":
reader = vtkIOXML.vtkGenericDataObjectReader()
reader.SetFileName(str(filename))
reader.Update()
assert reader.GetOutput().GetCellData().GetArray("mean")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should one of these conditions check export_type against ".vtu" instead of ".vtk"?

Since the tests are currently passing, and if the vtkIOXML.vtkGenericDataObjectReader works for either format , maybe we can unify these blocks?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also verify that the data retrieved matches what was written here.

materials=materials, geometry=geometry, settings=settings, tallies=tallies
)

with tempfile.TemporaryDirectory() as tmpdir:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the run_in_tmpdir fixture to this test should do the same thing as this context manager.

def test_umesh(run_in_tmpdir, export_type):
    ...

Comment on lines +454 to +458
statepoint_file = model.run(cwd=tmpdir)

statepoint = openmc.StatePoint(statepoint_file)
tally: openmc.Tally = statepoint.get_tally(name="test_tally")
umesh_from_sp: openmc.UnstructuredMesh = statepoint.meshes[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically best to open the statepoint files in a context manager to avoid an occupied HDF5 file handle:

Suggested change
statepoint_file = model.run(cwd=tmpdir)
statepoint = openmc.StatePoint(statepoint_file)
tally: openmc.Tally = statepoint.get_tally(name="test_tally")
umesh_from_sp: openmc.UnstructuredMesh = statepoint.meshes[1]
with openmc.StatePoint(statepoint_file) as statepoint:
tally: openmc.Tally = statepoint.get_tally(name="test_tally")
umesh_from_sp: openmc.UnstructuredMesh = statepoint.meshes[1]

Parameters
----------
filename : str or pathlib.Path
Name of the VTK file to write
Name of the VTK file to write. If the filename ends in '.vtu' then a VTU
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Name of the VTK file to write. If the filename ends in '.vtu' then a VTU
Name of the VTK file to write. If the filename ends in '.vtu' then a binary VTU

@pshriwise pshriwise added this to the v0.15.1 milestone Feb 6, 2025
@pshriwise pshriwise removed the v0.15.1 label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants