New Python API Docs?

Hello All,

Is there documentation on the new Python API? In particular is there now a way to pull out a tally for a whole mesh without having to call get_value() for all x, y, z individually? Preferably as a numpy array…

Be Well

Anthony

Anthony,

To your first question, at the moment there are no docs on the new Python API. This has been a topic of discussion for Paul and I, and we both agree that it would not only be very nice, but really is necessary to make the API truly useful. Of course I can’t make that a research priority, so it is on the back-burner for one of us to do (or for any of our open source collaborators :slight_smile:

As for your second question, it is really quite easy to pull out data from a mesh tally using the “StatePoint.get_tally(…)” and “Tally.get_value(…)” routines. If you know your Tally ID, it is actually quite simple:

from openmc.statepoint import StatePoint

# Initialize handle on the statepoint file
sp = StatePoint(‘statepoint.###.h5’)

# Get a handle on your mesh Tally of interest
tally = sp.tallies[my_tally_id]

# Get a 1D NumPy array of the tally mean data and reshape to 2D
data = tally.mean
data.shape = (mesh_x, mesh_y)

However, for some situations with lots of tallies, or automated Tally creation, you may not know the tally ID. In this case, it is better to get the Tally object using the StatePoint API:

from openmc.statepoint import StatePoint

# Initialize handle on the statepoint file
sp = StatePoint(‘statepoint.###.h5’)

# Create a dummy mesh filter needed to query the StatePoint for your tally

# NOTE: Use the “mesh_id” associated with your mesh of interest
mesh_filter = openmc.Filter(type=‘mesh’, bins=[mesh_id])

# Query the StatePoint for your mesh tally
# NOTE: If you assigned specific Nuclides or a “name” to your tally, specify them here
tally = sp.get_tally(score=‘fission’, filters=[mesh_filter], nuclides=[ ], name=’’)

# Get a 1D NumPy array of the tally mean data and reshape to 2D
data = tally.mean
data.shape = (mesh_x, mesh_y)

I recognize that this second example is pretty cumbersome if you are actually writing Python code to do this by hand. Fortunately, I’m actually working on some code at the moment which will make this type of tally data extraction much easier. It makes the “StatePoint.get_tally(…)” more flexible, and permits the “Tally.get_value(…)” routine to return NumPy arrays of data - depending on the input parameters - which may span across multiple filter and/or score bins. Even better, there is a new “Tally.get_pandas_dataframe()” routine which builds and returns a Pandas dataframe for the tally data, column annotated with filter and score information. If you want early access to these features, see the “pandas” branch on my “wbinventor/openmc” fork on GitHub. In any case, you’ll see a PR on GitHub from me with all of this in the next day or two.

I hope this is helpful,

Will