2.5. Read HDF5 file from OpenMC to load multi-group cross sections

Here, we load the cross sections from an OpenMC HDF5 file.

To run the code, simply type: jupyter nbconvert --to python --execute <basename>.ipynb.

To convert it to a python file (named <basename>.py), simply type: jupyter nbconvert --to python <basename>.ipynb

[ ]:
import os
import sys
import numpy as np

sys.path.append("../../..")

from pyopensn.xs import MultiGroupXS
from pyopensn.context import UseColor, Finalize

UseColor(False)

2.5.1. Call OpenMC cross-section reader

[ ]:
# load cross sections
xs_uo2 = MultiGroupXS()
filepath = "../../../test/python/modules/linear_boltzmann_solvers/transport_keigen/uo2.h5"
xs_uo2.LoadFromOpenMC(filepath, "set1", 294.0)

2.5.2. Some cross-section data can be retrieved in Python

[ ]:
# Retrieve properties
ng = xs_uo2.num_groups
sca_order = xs_uo2.scattering_order
print("num_groups       = ", ng)
print("scattering_order = ", sca_order)

# note cross sections are read-only objects of type <memoryview>
# retrieve a numpy array
siga = np.array(xs_uo2.sigma_a)
print("siga = ", siga)
# retrieve as list
sigt = list(xs_uo2.sigma_t)
print("sigt = ", sigt)

2.5.3. Finalize (for Jupyter Notebook only)

In Python script mode, PyOpenSn automatically handles environment termination. However, this automatic finalization does not occur when running in a Jupyter notebook, so explicit finalization of the environment at the end of the notebook is required. Do not call the finalization in Python script mode, or in console mode.

Note that PyOpenSn’s finalization must be called before MPI’s finalization.

[ ]:
from IPython import get_ipython

def finalize_env():
    Finalize()
    MPI.Finalize()

ipython_instance = get_ipython()
if ipython_instance is not None:
    ipython_instance.events.register("post_execute", finalize_env)