Plot tally result when using "DistribcellFilter"

Below is the python code I’m using to get the fission rate value for each fuel pin:

tally_fn = openmc.Tally(name=‘fuel normalized power’)
tally_fn.filters = [openmc.DistribcellFilter([fuel_cell])]
tally_fn.scores = [‘fission’]
tally_fn.estimators=[‘collision’]

How to create a color 2D plot for “hexagonal assembly” which will show the fission rate over each fuel pin?

StatePoint File

Thanks

@ayaz There is no “easy” way to visualize tallies with distributed cell filters. However, with a bit of Python scripting and use of our openmc.lib module (Python bindings to the C/C++ API), it can be done. Since this seems to be a common question, I’ve put an example together showing how a plot of the flux for pins in a 2D hexagonal lattice could be done using DistribcellFilter as well as the newer CellInstanceFilter:

Hope this helps!

Thanks @paulromano

With my statepoint.400.h5 file, when I try to run

with openmc.lib.run_in_memory():
    for row, y in enumerate(np.linspace(ymin, ymax, resolution[0])):
        for col, x in enumerate(np.linspace(xmin, xmax, resolution[1])):
            try:
                # For each (x, y, z) point, determine the cell and distribcell index
                cell, distribcell_index = openmc.lib.find_cell((x, y, 0.))
            except openmc.exceptions.GeometryError:
                # If a point appears outside the geometry, you'll get a GeometryError exception.
                # These lines catch the exception and continue on
                continue

            if cell.id == fuel_cell_f.id:
                # When the cell ID matches, we set the corresponding pixel in the image using the
                # distribcell index. Note that we're taking advantage of the fact that the i-th element
                # in the flux array corresponds to the i-th distribcell instance.
                img[row, col] = flux[distribcell_index]

code, I got
The kernel appears to have died. It will restart automatically.

Now start from the beginning and import statepoint file gives

sp = openmc.StatePoint('statepoint.400.h5')

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-2-820df5b6e8f5> in <module>
----> 1 sp = openmc.StatePoint('statepoint.400.h5')

~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/openmc/statepoint.py in __init__(self, filepath, autolink)
    141             path_summary = os.path.join(os.path.dirname(filename), 'summary.h5')
    142             if os.path.exists(path_summary):
--> 143                 su = openmc.Summary(path_summary)
    144                 self.link_with_summary(su)
    145 

~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/openmc/summary.py in __init__(self, filename)
     38             raise ValueError(msg)
     39 
---> 40         self._f = h5py.File(filename, 'r')
     41         cv.check_filetype_version(self._f, 'summary', _VERSION_SUMMARY)
     42 

~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/h5py/_hl/files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, rdcc_nslots, rdcc_nbytes, rdcc_w0, track_order, fs_strategy, fs_persist, fs_threshold, **kwds)
    422             with phil:
    423                 fapl = make_fapl(driver, libver, rdcc_nslots, rdcc_nbytes, rdcc_w0, **kwds)
--> 424                 fid = make_fid(name, mode, userblock_size,
    425                                fapl, fcpl=make_fcpl(track_order=track_order, fs_strategy=fs_strategy,
    426                                fs_persist=fs_persist, fs_threshold=fs_threshold),

~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
    188         if swmr and swmr_support:
    189             flags |= h5f.ACC_SWMR_READ
--> 190         fid = h5f.open(name, flags, fapl=fapl)
    191     elif mode == 'r+':
    192         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (file signature not found)

I have already tried a number of times but the same error comes again and again. How do I solve this problem?

Code

sp = openmc.StatePoint('statepoint.100.h5')

fp = sp.get_tally(name='fuel normalized power')
df = fp.get_pandas_dataframe()
fission = df[df['score'] == 'fission']

m = (fission[['mean']] / 0.0043)
df2 = fission.assign(Normalized=m)
fis_value = df2.Normalized.ravel()

resolution = (600, 600)
img = np.full(resolution, np.nan)
xmin, xmax = -27., 27.
ymin, ymax = -27., 27.

with openmc.lib.run_in_memory():
    for row, y in enumerate(np.linspace(ymin, ymax, resolution[0])):
        for col, x in enumerate(np.linspace(xmin, xmax, resolution[1])):
            try:
                cell, distribcell_index = openmc.lib.find_cell((x, y, -20.))
            except openmc.exceptions.GeometryError:
                continue
            if cell.id == uzrh_cell_f.id:
                img[row, col] = fis_value[distribcell_index]


options = {
    'origin': 'lower',
    'extent': (xmin, xmax, ymin, ymax),
    'vmin': 0.03,
    'vmax': 0.06,
    'cmap': 'RdYlBu_r',
}

plt.imshow(img, **options)
plt.xlabel('x [cm]')
plt.ylabel('y [cm]')
plt.colorbar()
plt.show()

Now If I run this, I am getting the following error message on the terminal

 Minimum neutron data temperature: 250.0 K
 Maximum neutron data temperature: 2500.0 K
 Reading tallies XML file...
 Preparing distributed cell instances...
 Writing summary.h5 file...
 ERROR: Failed to open HDF5 file with mode 'w': summary.h5

If I remove summary.h5 file, I get

    df = fp.get_pandas_dataframe()
  File "~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/openmc/tallies.py", line 1288, in get_pandas_dataframe
    filter_df = f.get_pandas_dataframe(
  File "~/anaconda3/envs/openmc-dev/lib/python3.9/site-packages/openmc/filter.py", line 1500, in get_pandas_dataframe
    raise ValueError(msg)
ValueError: Unable to construct distribcell paths since the Summary is not linked to the StatePoint

I would try restarting your Jupyter kernel and rerunning. Things can get a bit wacky if an HDF5 file is opened from Python, and then you start OpenMC which attempts to open the same file. Note that when you call StatePoint(...) it will automatically try to load geometry information from summary.h5. Then, running OpenMC (via openmc.lib.find_cell) will try to write summary.h5 while it is already open.

An alternative here that might be safer is to write two scripts, one that runs OpenMC and one that does the visualization.

@paulromano I did and created a separate python script which I run it from terminal instead of jupyter notebook, But still Same error.

Writing summary.h5 file…
ERROR: Failed to open HDF5 file with mode ‘w’: summary.h5

This means that the summary.h5 file is still open on some process on your computer. You might want to shut down all the Jupyter notebook servers before trying to run your scripts.

@paulromano one server was running on port 8888, then I clean jupyter run time path folder rm -rf .local/share/jupyter/runtime/*and type

$ jupyter notebook list
Currently running servers:
$

But still having the same error.

Maybe you just have a hung process somewhere that is holding that file open? Restarting your computer should take care of any dead processes.

@paulromano I did, also clean all the jupyter runtime path folder and reinstall Miniconda but still the same error message.

Hi @paulromano. I wanted to ask a follow-up question to this thread. I tried using your example from jupyter notebook and edited in my own fuel cell name, but the kernel itself dies when it tries to run the code? Did you experience anything similar to this? I can provide screenshots if necessary.

@mfredd If the kernel dies, you should look at the terminal output from OpenMC to see what caused it to abort. If you start jupyter notebook from a terminal, that is where you will see the output from OpenMC.

Hi @ayaz ,
I had faced this problem before , Not sure whether my solution is relating to yours or not. I solved
ERROR: Failed to open HDF5 file with mode ‘w’: summary.h5

by set the value of the environment variable HDF5_USE_FILE_LOCKING to FALSE via terminal.

/bin/nano ~/.bashrc
export HDF5_USE_FILE_LOCKING=FALSE

Hope this helps,
Sivakorn Sansawas

1 Like