Python code run with openmc with ubuntu

When I run this code, I encounter such error output.How can solve this problem

import h5py
import matplotlib.pyplot as plt
import numpy as np
import openmc
import os

# Define the number of batches and particles per batch to use for simulation
batches = 10
particles_per_batch = 10000

# Define the time points at which to record the keff and burnup
time_points = np.linspace(0, 10, 101)

# Define the path to the HDF5 output file
output_file = "results.h5"

# Define the materials, geometry, and settings for the OpenMC model
fuel = openmc.Material(name="fuel")
fuel.add_nuclide("U235", 0.03)
fuel.add_nuclide("U238", 0.97)
fuel.set_density("g/cm3", 10.0)

moderator = openmc.Material(name="moderator")
moderator.add_element("H", 2.0)
moderator.add_element("O", 1.0)
moderator.set_density("g/cm3", 1.0)

box = openmc.rectangular_prism(width=1, height=1,boundary_type='reflective')
type(box)
cell = openmc.Cell(fill=fuel, region=box)
universe = openmc.Universe(cells=[cell])
geometry = openmc.Geometry(universe)

settings = openmc.Settings()
settings.batches = batches
settings.particles = particles_per_batch
settings.inactive = 0
settings.source = openmc.Source(space=openmc.stats.Point((0, 0, 0)))

# Define the tallies to compute the keff and burnup
tally_keff = openmc.Tally(name="keff")
tally_keff.scores = ["k-effective"]
tally_keff.estimator = "tracklength"

# Create a fission tally in the fuel
tally_fission = openmc.Tally(name='fission')
tally_fission.scores = ['fission']
tally_fission.filters = [openmc.MaterialFilter(fuel)]
tally_fission.nuclides = ['U235', 'U238']

# Add tallies to a TallyList and export to XML
tallies = openmc.Tallies([tally_fission])
tallies.export_to_xml()


tally_fission = openmc.Tally(name="fission")
tally_fission.scores = ["fission"]
tally_fission.filters = [openmc.MaterialFilter(fuel)]
#tally_fission.filters = [openmc.Filter(type='material', bins=[fuel])]
tally_fission.filters = [openmc.MaterialFilter(fuel),openmc.EnergyFilter([0.0, 20.0e6])]

tallies = openmc.Tallies([tally_keff, tally_fission])

# Run the simulation

cladding = openmc.Material(name="cladding")
cladding.add_element("Zr", 1.0)
cladding.set_density("g/cm3", 6.5)

water = openmc.Material(name="water")
water.add_element("H", 2.0)
water.add_element("O", 1.0)
water.set_density("g/cm3", 1.0)
materials = [fuel, cladding, water]
model = openmc.model.Model(geometry, settings, materials, tallies)
model.run(output_file=output_file)

# Extract the keff and burnup data from the HDF5 output file
with h5py.File(output_file, "r") as f:
    keff = f["keff"][:]
    burnup = f["fission"][:, 0] * 200.0  # Convert fission tally to total energy produced

# Plot the keff and burnup data
fig, ax1 = plt.subplots()

color = "tab:red"
ax1.set_xlabel("Time [days]")
ax1.set_ylabel("keff", color=color)
ax1.plot(time_points, keff, color=color)
ax1.tick_params(axis="y", labelcolor=color)

ax2 = ax1.twinx()

color = "tab:blue"
ax2.set_ylabel("Burnup [MWd/kg]", color=color)
ax2.plot(time_points, burnup, color=color)
ax2.tick_params(axis="y", labelcolor=color)

fig.tight_layout()
plt.show()

emil@DESKTOP-:~/openmc/examples/test$ python3 burn.py
Traceback (most recent call last):
  File "/home/emil/openmc/examples/test/burn.py", line 75, in <module>
    model = openmc.model.Model(geometry, settings, materials, tallies)
  File "/home/emil/.local/lib/python3.10/site-packages/openmc/model/model.py", line 84, in __init__
    self.materials = materials
  File "/home/emil/.local/lib/python3.10/site-packages/openmc/model/model.py", line 171, in materials
    check_type('materials', materials, Iterable, openmc.Material)
  File "/home/emil/.local/lib/python3.10/site-packages/openmc/checkvalue.py", line 42, in check_type
    raise TypeError(msg)
TypeError: Unable to set "materials" to "<openmc.settings.Settings object at 0x7fbf812043a0>" which is not of type "Iterable"

When you create the Model class, the order of the arguments is geometry, materials, settings, tallies:

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

This new error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 13
     11 materials = [fuel, cladding, water]
     12 model = openmc.Model(geometry, materials, settings, tallies)
---> 13 model.run(output_file=output_file)

TypeError: Model.run() got an unexpected keyword argument 'output_file'

The Model.run method does not have an output_file argument, so you need to remove that.

1 Like

I ran it, the result was something like this, are there any other files I need to add?

<bound method Model.run of <openmc.model.model.Model object at 0x7f80375383a0>>

The bound method error sounds like the command was missing the ()

output_file=model.run()

1 Like

Again another error this code

/home/emil/openmc/Cross_Section_Libraries/endfb-viii.0-hdf5/neutron/O16.h5
 Reading O17 from
 /home/emil/openmc/Cross_Section_Libraries/endfb-viii.0-hdf5/neutron/O17.h5
 Reading O18 from
 /home/emil/openmc/Cross_Section_Libraries/endfb-viii.0-hdf5/neutron/O18.h5
 Minimum neutron data temperature: 250 K
 Maximum neutron data temperature: 2500 K
 Reading tallies XML file...
terminate called after throwing an instance of 'std::invalid_argument'
  what():  Invalid tally score "k-effective". See the docs for details: https://docs.openmc.org/en/stable/usersguide/tallies.html#scores
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[7], line 13
     11 materials = [fuel, cladding, water]
     12 model = openmc.Model(geometry, materials, settings, tallies)
---> 13 output_file=model.run()

File ~/.local/lib/python3.10/site-packages/openmc/model/model.py:588, in Model.run(self, particles, threads, geometry_debug, restart_file, tracks, output, cwd, openmc_exec, mpi_args, event_based)
    585 else:
    586     # Then run via the command line
    587     self.export_to_xml()
--> 588     openmc.run(particles, threads, geometry_debug, restart_file,
    589                tracks, output, Path('.'), openmc_exec, mpi_args,
    590                event_based)
    592 # Get output directory and return the last statepoint written
    593 if self.settings.output and 'path' in self.settings.output:

File ~/.local/lib/python3.10/site-packages/openmc/executor.py:280, in run(particles, threads, geometry_debug, restart_file, tracks, output, cwd, openmc_exec, mpi_args, event_based)
    234 """Run an OpenMC simulation.
    235 
    236 Parameters
   (...)
    272 
    273 """
    275 args = _process_CLI_arguments(
    276     volume=False, geometry_debug=geometry_debug, particles=particles,
    277     restart_file=restart_file, threads=threads, tracks=tracks,
    278     event_based=event_based, openmc_exec=openmc_exec, mpi_args=mpi_args)
--> 280 _run(args, output, cwd)

File ~/.local/lib/python3.10/site-packages/openmc/executor.py:118, in _run(args, output, cwd)
    115     error_msg = 'OpenMC aborted unexpectedly.'
    116 error_msg = ' '.join(error_msg.split())
--> 118 raise RuntimeError(error_msg)

RuntimeError: Invalid tally score "k-effective". See the docs for details: https://docs.openmc.org/en/stable/usersguide/tallies.html#scores

I think this one is best explained by the error message printed out.

1 Like

This is the last time i tried
error output from your application

Actually my purpose is to create the graphic in the figure below and I don’t know in which sample file in openmc this will be done
burnup

1 Like