Skip to content

Introduce openmc.lib.TemporarySession context manager - #3475

Merged
paulromano merged 6 commits into
openmc-dev:developfrom
paulromano:temp-session
Jul 1, 2025
Merged

paulromano merged 6 commits into
openmc-dev:developfrom
paulromano:temp-session

Conversation

@paulromano

Copy link
Copy Markdown
Contributor

Description

We have an increasing number of use cases where want to use functionality from the OpenMC shared library without actually running OpenMC itself. This usually involves creating a temporary directory, exporting a Model to XML, initializing the OpenMC shared library via openmc.lib.init, and then calling the function(s) of interest from openmc.lib. Examples of this include:

  • MicroXS.from_multigroup_flux
  • WeightWindowsList.export_to_hdf5
  • Mesh.material_volumes

This PR introduces a new openmc.lib.TemporarySession context manager class that makes this process a little easier by combining the first few steps together (moving to a temp dir, exporting XML, calling openmc.lib.init). So the first line of code under the with statement can be a call to something from openmc.lib. I've updated each of the methods above to use the new TemporarySession class.

Use for MicroXS

For some depletion/activation workflows, the user may want to make repeated calls to MicroXS.from_multigroup_flux. Right now, this will cause openmc.lib.init to get called (hence loading cross sections) each time, which may result in long execution times. I've added a new session argument on from_multigroup_flux that allows one to use the same temporary session so that openmc.lib only needs to be initialized once. So it might look like:

with openmc.lib.TemporarySession() as session:
    for mat in long_list_of_materials:
        micros = MicroXS.from_multigroup_flux(energy, flux, nuclides=mat.get_nuclides(), session=session)

Chain caching

In the above example, another issue with repeated calls to MicroXS.from_multigroup_flux is that a chain file will get loaded from XML each time. Thanks to PR #3436 by @shimwell, this could be avoided by creating a Chain object and then passing it to the chain_file argument. In this PR, I've also made things a little bit smoother by caching Chain instances behind the scenes so that a user can just set openmc.config['chain_file'] instead of having to pass around the Chain object.

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)

@paulromano
paulromano requested a review from shimwell June 27, 2025 16:21
@paulromano
paulromano requested a review from pshriwise as a code owner June 27, 2025 16:21
@MicahGale

Copy link
Copy Markdown
Contributor

I love this feature. I'm trying to think of any end user use cases, and am blanking. Long term I think if users are using openmc.lib this should be the way they do so, and the documentation should point towards that.

@pshriwise pshriwise left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Echoing @MicahGale, love this. Thanks @paulromano! The code diff makes it evident that you've hit a sweet spot of functionality for this context manager. Thanks in particular for the addition of the minimal model in the absence of a model object.

I've just got a couple of questions on the semantics of passing a context manager instance as an argument and the existence of multiple TemporarySession objects at oncce.

Comment thread openmc/lib/core.py
Comment thread openmc/deplete/microxs.py Outdated
Comment thread openmc/deplete/microxs.py
Comment thread openmc/lib/core.py Outdated
@paulromano

Copy link
Copy Markdown
Contributor Author

@pshriwise @eepeterson Thanks for the reviews guys. All your comments have been addressed

@pshriwise pshriwise left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good @paulromano. Thank you!

@paulromano
paulromano merged commit eb74d49 into openmc-dev:develop Jul 1, 2025
@paulromano
paulromano deleted the temp-session branch July 1, 2025 17:44
ahnaf-tahmid-chowdhury pushed a commit to ahnaf-tahmid-chowdhury/OpenMC that referenced this pull request Jul 7, 2025
@shimwell shimwell mentioned this pull request Aug 13, 2025
2 tasks done
apingegno pushed a commit to apingegno/openmc that referenced this pull request May 7, 2026
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.

4 participants