Calling OpenMC From Python

I want to call the openmc program (command) from Python to make it part of processing sequence but have not figured out how to do it. I am running on Linux and a something that will invoke a regular installed command like:
output = subprocess.check_output([‘ls’])
which works fine fails when I try ‘openmc’.

Setting the working directory explicitly to where my XML files are located:
os.chdir(path)
Does not fix things, and supplying the complete path to the program (e.g. `/home/carey/miniconda3/bin/openmc’)
also does not work.

I am not talking about the Python API, or installing OpenMC, I am talking about simply running openmc from another Python program so that I can get the output of the Monte Carlo simulation.

1 Like

I should have spent a little more time experimenting.

Setting the working directory and supplying the complete path to openmc were necessary, and I was able to run it with subprocess:

output = subprocess.run(openmcPath, capture_output=True)

Hi Carysub;

probably my contribution is not a direct solution for your problem, but I think that could help to share it. I had developped some code with python to call MCNP (not OPENMC yet… but it will be the next step) to execute MCNP via the line command : mcnp5 input output) under both os : windows and linux and i used the “os” library under python:
import os
with the following line script:
os.system(f’mcnp5 in={filename} out={Output}’)

the execution is done where the input file is created (my python scripts generate the whole input file for simple homogeneous cylindrical core)
it works very fine and i don’t have to define the pathdirectory in the execution line.

I hope that could help even you get another alternative for your problem.

regards

@careysub Glad you were able to get something working. For reference, if you are able to use the Python API, you can use openmc.run() in any Python script, which just uses subprocess to call the OpenMC executable under the hood. Note that this function has arguments cwd and openmc_exec that allow you to set the current working directory and/or the executable.

1 Like