# Plot tally result when using "DistribcellFilter"

**URL:** <https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247>\
**Category:** User Support\
**Created:** [June 15, 2021, 6:36am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247 "2021-06-15T06:36:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 15, 2021, 6:36am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/1 "2021-06-15T06:36:57Z")

</div>

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](https://drive.google.com/drive/folders/13IIr9eOAWbYLQKazXuDdGKUqRrgvtyFG?usp=sharing)

Thanks

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [June 15, 2021, 9:40am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/2 "2021-06-15T09:40:43Z")

</div>

@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`:

> **[Jupyter Notebook Viewer](https://nbviewer.jupyter.org/gist/paulromano/f2fbf3d4731e324b6f5ab31ef3fcaa26)**
>
> Check out this Jupyter notebook!

Hope this helps!

---

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 16, 2021, 3:33am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/3 "2021-06-16T03:33:59Z")

</div>

Thanks @paulromano

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

```auto
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

```auto
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?

---

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 16, 2021, 5:26am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/4 "2021-06-16T05:26:27Z")

</div>

Code

```auto
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

```auto
 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

```auto
    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

```

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [June 21, 2021, 8:42am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/5 "2021-06-21T08:42:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 21, 2021, 12:45pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/6 "2021-06-21T12:45:28Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [June 21, 2021, 3:48pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/7 "2021-06-21T15:48:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 21, 2021, 4:53pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/8 "2021-06-21T16:53:47Z")

</div>

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

```auto
$ jupyter notebook list
Currently running servers:
$

```

But still having the same error.

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [June 22, 2021, 6:47am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/9 "2021-06-22T06:47:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ayaz](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@ayaz](https://openmc.discourse.group/u/ayaz)\
**Post date:** [June 22, 2021, 1:41pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/10 "2021-06-22T13:41:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mfredd](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mfredd](https://openmc.discourse.group/u/mfredd)\
**Post date:** [June 24, 2021, 8:31pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/11 "2021-06-24T20:31:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [June 28, 2021, 2:31am UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/12 "2021-06-28T02:31:50Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Sivakorn.SSW](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/sivakorn.ssw/32/1424_2.png) [@Sivakorn.SSW](https://openmc.discourse.group/u/Sivakorn.SSW)\
**Post date:** [August 8, 2021, 5:10pm UTC](https://openmc.discourse.group/t/plot-tally-result-when-using-distribcellfilter/1247/14 "2021-08-08T17:10:59Z")

</div>

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.

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

```

Hope this helps,  
Sivakorn Sansawas
