Add close method to openmc.StatePoint
and update docs/examples for best practices
#1854
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Movitation
A recurring issue in the forum is users running into the error
RuntimeError: Failed to open HDF5 file with mode 'w': summary.h5
when working with OpenMC's Python API. This is usually related to an open StatePoint file when OpenMC is being executed. The open HDF5 file handle prevents an overwrite of the
summary.h5
file (auto-linked to theStatePoint
object by default) and causes OpenMC to crash. This can also happen if OpenMC tries to overwrite a statepoint file with an open handle, so we can see this even if writing the summary file is disabled.A couple of forum discussions related to this issue:
Changes
This PR adds a
openmc.StatePoint.close
method that closes the HDF5 files related to the statepoint object.StatePoint
file handles are most easily managed using context managers when extracting data, but there are times when these regions may become unreasonably large. TheStatePoint.close
method is meant to handle these cases.To increase user awareness, I've also updated the examples to use context managers when possible and always close the
StatePoint
objects as a matter of best practice if needed.