Repository navigation
Introduce openmc.lib.TemporarySession context manager - #3475
Conversation
|
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 |
pshriwise
left a comment
There was a problem hiding this comment.
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.
|
@pshriwise @eepeterson Thanks for the reviews guys. All your comments have been addressed |
pshriwise
left a comment
There was a problem hiding this comment.
Looks good @paulromano. Thank you!
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_fluxWeightWindowsList.export_to_hdf5Mesh.material_volumesThis PR introduces a new
openmc.lib.TemporarySessioncontext manager class that makes this process a little easier by combining the first few steps together (moving to a temp dir, exporting XML, callingopenmc.lib.init). So the first line of code under thewithstatement can be a call to something fromopenmc.lib. I've updated each of the methods above to use the newTemporarySessionclass.Use for
MicroXSFor some depletion/activation workflows, the user may want to make repeated calls to
MicroXS.from_multigroup_flux. Right now, this will causeopenmc.lib.initto get called (hence loading cross sections) each time, which may result in long execution times. I've added a newsessionargument onfrom_multigroup_fluxthat allows one to use the same temporary session so thatopenmc.libonly needs to be initialized once. So it might look like:Chain caching
In the above example, another issue with repeated calls to
MicroXS.from_multigroup_fluxis that a chain file will get loaded from XML each time. Thanks to PR #3436 by @shimwell, this could be avoided by creating aChainobject and then passing it to thechain_fileargument. In this PR, I've also made things a little bit smoother by cachingChaininstances behind the scenes so that a user can just setopenmc.config['chain_file']instead of having to pass around the Chain object.Checklist