Calculating the flux distribution for a CAD geometry generated from Cubit using DAGMC plugin

I was trying to calculate the flux distribution of a CAD geometry. But is shows KeyError. My code is given below:

import openmc

from IPython.display import Image
from matplotlib import pyplot as plt

materials

u235 = openmc.Material(name=“fuel”)
u235.add_nuclide(‘U235’, 1.0, ‘ao’)
u235.set_density(‘g/cc’, 11)

water = openmc.Material(name=“water”)
water.add_nuclide(‘H1’, 2.0, ‘ao’)
water.add_nuclide(‘O16’, 1.0, ‘ao’)
water.set_density(‘g/cc’, 1.0)
water.add_s_alpha_beta(‘c_H_in_H2O’)
materials = openmc.Materials([u235,water])
#Importing_CAD_GEOMETRY
dagmc_univ = openmc.DAGMCUniverse(filename=‘bal4.h5m’, auto_geom_ids=True, auto_mat_ids=True)
bounding_box = openmc.model.RectangularParallelepiped(-20,20,-20,20,-20,20,boundary_type=‘vacuum’)
#Geometry
cell = openmc.Cell(region=-bounding_box, fill=dagmc_univ)
geometry = openmc.Geometry([cell])
#Settings
settings = openmc.Settings()
settings.batches = 120
settings.inactive = 2
settings.particles = 1000

model = openmc.Model(geometry=geometry, materials=materials, settings=settings)
model.export_to_model_xml()
#ploting
p = openmc.Plot()
p.width = (25.0, 25.0)
p.pixels = (400, 400)
p.color_by = ‘material’
p.colors = {u235: ‘yellow’, water:‘red’}
openmc.plot_inline(p)
#Tally
itally = openmc.Tally()
tally.filters = [openmc.DistribcellFilter(cell)]
tally.scores = [‘flux’]

tallies = openmc.Tallies([tally])
tallies.export_to_xml()

openmc.run()

with openmc.StatePoint(‘statepoint.120.h5’) as sp:
t = sp.tallies[1]
flux = t.mean.ravel()

df = t.get_pandas_dataframe()    
print(df)

If someone can correct my code that how can I calculate the flux distribution or power distribution for a CAD geometry than, it would be great for me.
Thanks in advance

Hi Sabyasachi

Are you able to also post the KeyError message

Thanks

1 Like

It shows:

KeyError Traceback (most recent call last)
Cell In[5], line 2
1 with openmc.StatePoint(‘statepoint.120.h5’) as sp:
----> 2 t = sp.tallies[1]
3 # Get the mean value of the flux for each instance of the fuel cell as a flattened (1D) numpy array
4 flux = t.mean.ravel()

KeyError: 1

Ah right, this means that tally number 1 does not exist in the statepoint file.

Try changing this like

sp.tallies[1]

To a tally number that is present in the statepoint file instead of 1

The statepoint.120.h5 file contains the keys:

All keys: <KeysViewHDF5 [‘current_batch’, ‘energy_mode’, ‘generations_per_batch’, ‘global_tallies’, ‘k_abs_tra’, ‘k_col_abs’, ‘k_col_tra’, ‘k_combined’, ‘k_generation’, ‘n_batches’, ‘n_inactive’, ‘n_particles’, ‘n_realizations’, ‘run_mode’, ‘runtime’, ‘seed’, ‘source_bank’, ‘tallies’]>
Object:
Object keys for tallies: <KeysViewHDF5 [‘filters’, ‘meshes’]>

But I am not being able to find out the appropriate number for tally. How can I find it? will you please help me?

The tallies attribute on the StatePoint class is a dictionary, so if you want to know the keys, you can run:

with openmc.StatePoint('statepoint.120.h5') as sp:
    print(sp.tallies.keys())

Is there any problem in the code I have given here? Actually I want to calculate the flux distribution of a CAD geometry where a spherical shaped fuel is covered by a water domain. I am not getting the tallies output here. The above code gives result:

k-effective (Collision) = 0.33074 +/- 0.00192
k-effective (Track-length) = 0.33073 +/- 0.00174
k-effective (Absorption) = 0.33217 +/- 0.00265
Combined k-effective = 0.33100 +/- 0.00167
Leakage Fraction = 0.79762 +/- 0.00119

Dear Romano and Shimwell ,

As I have previously mentioned I was trying to calculate the flux of a CAD geometry. And I have followed the way in the which is mentioned in the documentation. The code is given below:
Untitled.ipynb (17.1 KB)
statepoint.10.h5 (12.2 KB)
model.xml (1.4 KB)
tallies.xml (337 Bytes)
summary.h5 (51.6 KB)
plots.xml (287 Bytes)

Please suggest me some solution

Looks like you are making a model from geometry, materials and settings.

then you are running the model

then you are adding tallies

these tallies should be included in the model so that they are included in the run

try moving tallies up in the code and add them to the openmc.Model

model = openmc.Model(geometry=geometry, materials=materials, settings=settings)

should become

model = openmc.Model(geometry=geometry, materials=materials, settings=settings, tallies=tallies)

Thank you it has solved my problem. The key error is basically generated because of the id where tally calculation is being generated. In my case if i put

sp.tallies[2] instead of sp.tallies[1] then it works