# Python code run with openmc with ubuntu

**URL:** https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759
**Category:** User Support
**Created:** [February 23, 2023, 6:03am UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759 "2023-02-23T06:03:08Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 6:03am UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/1 "2023-02-23T06:03:08Z")

</div>

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

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

```

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

```

---

<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: [February 23, 2023, 12:42pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/2 "2023-02-23T12:42:21Z")

</div>

When you create the `Model` class, the [order of the arguments](https://docs.openmc.org/en/stable/pythonapi/generated/openmc.model.Model.html#openmc.model.Model) is geometry, materials, settings, tallies:

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

---

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 5:33pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/3 "2023-02-23T17:33:56Z")

</div>

This new error

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

```

---

<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: [February 23, 2023, 5:51pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/4 "2023-02-23T17:51:09Z")

</div>

The [`Model.run`](https://docs.openmc.org/en/stable/pythonapi/generated/openmc.model.Model.html#openmc.model.Model.run) method does not have an `output_file` argument, so you need to remove that.

---

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 6:18pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/5 "2023-02-23T18:18:44Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)
#### Post date: [February 23, 2023, 8:50pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/6 "2023-02-23T20:50:52Z")

</div>

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

output\_file=model.run()

---

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 8:59pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/7 "2023-02-23T20:59:26Z")

</div>

Again another error this code

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

```

---

<div class="post-metadata">

### Author: ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)
#### Post date: [February 23, 2023, 9:01pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/8 "2023-02-23T21:01:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 9:15pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/9 "2023-02-23T21:15:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![emilmammadzada](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@emilmammadzada](https://openmc.discourse.group/u/emilmammadzada)
#### Post date: [February 23, 2023, 9:17pm UTC](https://openmc.discourse.group/t/python-code-run-with-openmc-with-ubuntu/2759/10 "2023-02-23T21:17:47Z")

</div>

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](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/6/6edc9deb47060e02d410d74bbe8dd4fc9b95b1a6.jpeg)
