2.4. Read ASCII file for multi-group cross sections

Here, we load the cross sections from a plain text file (OpenSn format).

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.4.1. Call cross-section reader

[ ]:
# load cross sections
xs_mat = MultiGroupXS()
xs_mat.LoadFromOpenSn("xs_2g.xs")

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

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

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

2.4.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)