MCNP Input and Output Interfaces – pyne.mcnp

There is a class for a variety of types of files that MCNP produces. The functionality of the module can be obtained by importing as such:

from pyne import mcnp

You may also want to consult the userguide page MCNP.


Module for parsing MCNP output data. MCNP is a general-purpose Monte Carlo N-Particle code developed at Los Alamos National Laboratory that can be used for neutron, photon, electron, or coupled neutron/photon/electron transport. Further information on MCNP can be obtained from

Mctal and Runtpe classes still need work. Also should add Meshtal and Outp classes.

If PyMOAB is not installed, then Wwinp, Meshtal, and Meshtally will not be available to use.

class pyne.mcnp.Meshtal(filename, tags=None, meshes_have_mats=False)[source]

This class stores all the information from an MCNP meshtal file with single or multiple fmesh4 neutron or photon tallies. The “tally” attribute provides key/value access to invidial MeshTally objects.


Path to an MCNP meshtal file


The MCNP verison number


The MCNP verison date


Title card from the MCNP input


Number of histories from the MCNP simulation


A dictionary with MCNP fmesh4 tally numbers (e.g. 4, 14, 24) as keys and MeshTally objects as values.


Maps integer tally numbers to iterables containing four strs, the results tag name, the relative error tag name, the total results tag name, and the total relative error tag name. If tags is None the tags are named ‘x_result’, ‘x_rel_error’, ‘x_result_total’, ‘x_rel_error_total’ where x is n or p for neutrons or photons.


MCNP meshtal file.

tagsdict, optional

Maps integer tally numbers to iterables containing four strs: the results tag name, the relative error tag name, the total results tag name, and the total relative error tag name. If tags is None the tags are named ‘x_result’, ‘x_rel_error’, ‘x_result_total’, ‘x_rel_error_total’ where x is n or p for neutrons or photons.


If false, Meshtally objects will be created without PyNE material material objects.

create_meshtally(self, f, tally_number, tag_names=None, mesh_has_mats=False)[source]

This function creates a Mesh instance from MCNP meshtal file.

read_column_order(self, f)[source]

Create dictionary with table headings as keys and their column location as values. Dictionary is the private attribute _column_idx.

fstr or filestream

Filestream of the meshtal file.

read_meshtally_head(self, f)[source]

Get the particle type and response bool of whether flux-to-dose conversion factors are being used.

fstr or filestream

Filestream of the meshtal file.


The particle type, ‘neutron’ or ‘photon’.


True : if this meshtally is modified by a dose function. False : if this meshtally is not modified by a dose function.

read_tally_results_rel_error(self, f, num_e_groups, num_ves)[source]

Read meshtally results and relative error data.

fstr or filestream

Filestream of the meshtal file.

num_e_groups: int

Number of energy groups.

num_ves: int

Number of volume elements.

resultsnumpy array, shape=(num_ves, num_e_groups)

Tally results data.

rel_errornumpy array, shape=(num_ves, num_e_groups)

Tally relative error data.

res_totnumpy array

Tally total flux result.

rel_err_totnumpy array

Total relative error fo flux.

read_xyze_bounds(self, f)[source]

Read the spatial and energy bounds.

fstr or filestream

Filestream of the meshtal file.

x_boundstuple of float

Mesh boundaries of X dimension.

y_boundstuple of float

Mesh boundaries of Y dimension.

z_boundstuple of float

Mesh boundaries of Z dimension.

e_boundstuple of float

Energy boundaries.

class pyne.mcnp.PtracReader(filename)[source]

Class to read _binary_ PTRAC files generated by MCNP.

Construct a new Ptrac reader for a given filename, determine the number format and read the file’s headers.


Determine the number format (endianness) used in the Ptrac file. For this, the file’s first entry is used. It is always minus one and has a length of 4 bytes.

read_event_line(self, ptrac_event)[source]

Read an event record and save it to a given PtracParticle instance.


Read and save the MCNP version and problem description from the Ptrac file.

read_next(self, format, number=1, auto=False, raw_format=False)[source]

Helper method for reading records from the Ptrac file. All binary records consist of the record content’s length in bytes, the content itself and then the length again. format can be one of the struct module’s format characters (i.e. i for an int, f for a float, s for a string). The length of the record can either be hard-coded by setting the number parameter (e.g. to read 10 floats) or determined automatically by setting auto=True. Setting the parameter raw_format to True means that the format string will not be expanded by number, but will be used directly.


Read an NPS record and save the type of the next event.


Read the list of variable IDs that each record type in the Ptrac file is comprised of. The variables can vary for different problems. Consult the MCNP manual for details.

write_to_hdf5_table(self, hdf5_table, print_progress=0)[source]

Writes the events contained in this Ptrac file to a given HDF5 table. The table must already exist and have rows that match the PtracEvent definition. If desired, the number of processed events can be printed to the console each N events by passing the print_progress=N parameter.

class pyne.mcnp.Runtpe(filename)[source]
class pyne.mcnp.Srctp(filename)[source]

This class stores source site data from a ‘srctp’ file written by MCNP. The source sites are stored in the ‘fso’ array in MCNP.


Path to Srctp file being worked with.

class pyne.mcnp.SurfSrc(filename, mode='rb')[source]

Enables manipulating both the header and tracklists in surface source files.

Example use cases include adding source particles from other codes, and combining multiple files together. Note that typically additional code will be needed to supplement this class in order to modify the header or track information in a way suitable to the use case.


Path to surface source file being read or written.

modestr, optional

String indicating file opening mode to be used (defaults to ‘rb’).


Returns contents of SurfSrc’s header as an informative string.


A line-by-line listing of the contents of the SurfSrc’s header.

print_tracklist(self, max_tracks=None)[source]

Returns tracklists in SurfSrc as a string.

max_tracksint, optional

Maximum number of tracks to print. Defaults to all tracks.


Single string with data for one track on each line.


Write the header part of the header to the surface source file


Write the summary part of the header to the surface source file


Write the record for each surface to the surface source file


Write the table1 part of the header to the surface source file


Write the table2 part of the header to the surface source file


Read in the header block data. This block comprises 4 fortran records which we refer to as: header, table1, table2, summary.


Reads in track records for individual particles.

update_tracklist(self, surf_src)[source]

Update tracklist from another surface source. This updates the surface source in-place.


Write the first part of the MCNP surface source file. The header content comprises five parts shown below.


Write track records for individual particles. Second part of the MCNP surface source file. Tracklist is also known as a ‘phase space’.

class pyne.mcnp.Wwinp[source]

A Wwinp object stores all of the information from a single MCNP WWINP file. Weight window lower bounds are stored on a structured mesh. Only Cartesian mesh WWINP files are supported. Neutron, photon, and simotaneous neutron and photon WWINP files are supported.


Attribute names are identical to names speficied in WWINP file description in the MCNP5 User’s Guide Volume 3 Appendix J.

ninumber of integers on card 2.

ni = 1 for neutron WWINPs, ni = 2 for photon WWINPs or neutron + photon WWINPs.


10 for rectangular, 16 for cylindrical.

nelist of number of energy groups for neutrons and photons.

If ni = 1 the list is only 1 value long, to represent the number of neutron energy groups

nflist of numbers

of fine mesh points in the i, j, k dimensions


total number of fine mesh points

originlist of i, j, k



number of coarse mesh points in the i, j, k dimensions


1 for rectangular, 2 for cylindrical.

cmlist of lists

of coarse mesh points in the i, j, k dimensions. Note the origin is not considered a coarse mesh point (as in MCNP).

fmlist of lists

of number of fine mesh points between the coarses mesh points in the i, j, k dimensions.

elist of lists

of energy upper bounds for neutrons, photons. If ni = 1, the e will look like [[]]. If ni = 2, e will look like [[], []].

boundslist of lists

of spacial bounds in the i, j, k dimensions.

meshMesh object

with a structured mesh containing all the neutron and/or photon weight window lower bounds. These tags have the form “ww_X” where X is n or p The mesh has rootSet tags in the form X_e_upper_bounds.

meshPyMOAB core instance or str, optional

Either a PyMOAB core instance or a file name of a PyMOAB mesh file.

structuredbool, optional

True for structured mesh.

structured_coordslist of lists, optional

A list containing lists of x_points, y_points and z_points that make up a structured mesh.

structured_setPyMOAB entity set handle, optional

A preexisting structured entity set on an PyMOAB core instance with a “BOX_DIMS” tag.

structured_orderingstr, optional

A three character string denoting the iteration order of the mesh (e.g. ‘xyz’, meaning z changest fastest, then y, then x.)

matsMaterialLibrary or dict or Materials or None, optional

This is a mapping of volume element handles to Material objects. If mats is None, then no empty materials are created for the mesh.

Unstructured mesh instantiation:
  • From PyMOAB core instance by specifying: <mesh>

  • From mesh file by specifying: <mesh_file>

Structured mesh instantiation:
  • From PyMOAB core instance with exactly 1 entity set (with BOX_DIMS tag) by specifying <mesh> and structured = True.

  • From mesh file with exactly 1 entity set (with BOX_DIMS tag) by specifying <mesh_file> and structured = True.

  • From a PyMOAB instance with multiple entity sets by specifying <mesh>, <structured_set>, structured=True.

  • From coordinates by specifying <structured_coords>, structured=True, and optional pre-existing PyMOAB core instance <mesh>

The “BOX_DIMS” tag on PyMOAB core instances containing structured mesh is a vector of floats in the following form: [i_min, j_min, k_min, i_max, j_max, k_max] where each value is a volume element index number. Typically volume elements should be indexed from 0. The “BOX_DIMS” information is stored in self.dims.

read_mesh(self, mesh)[source]

This method creates a Wwinp object from a structured mesh object. The mesh must have tags in the form “ww_X” where X is n or p. For every particle there must be a rootSet tag in the form X_e_upper_bounds containing a list of energy upper bounds.

read_wwinp(self, filename)[source]

This method creates a Wwinp object from the WWINP file <filename>.

write_wwinp(self, filename)[source]

This method writes a complete WWINP file to <filename>.

class pyne.mcnp.Xsdir(filename)[source]

This class stores the information contained in a single MCNP xsdir file.


See MCNP5 User’s Guide Volume 3 Appendix K for more information.

ffile handle

The xsdir file.


Path to the xsdir file.


Path to the directory containing the xsdir file.


The data path specified in the first line of the xsdir file, if it exists.


Maps material ids to their atomic weight ratios.


Entries are XsdirTable objects, that appear in the same order as the xsdir table lines.


Path to xsdir file.

find_table(self, name)[source]

Find all tables for a given ZIAD.


The ZIAD name.


All XsdirTable objects for a given ZIAD.


Provides a set of the valid nuclide ids for nuclides contained in the xsdir.


The valid nuclide ids.


Populate the xsdir object by reading the file.

to_xsdata(self, filename)[source]

Writes a Serpent xsdata file for all continuous energy xs tables.


The output filename.

class pyne.mcnp.XsdirTable[source]

Stores all information that describes a xsdir table entry, which appears as a single line in xsdir file. Attribute names are based off of those found in the MCNP5 User’s Guide Volume 3, appendix K.


The ZAID and library identifier, delimited by a ‘.’.


The atomic mass ratio of the nuclide.


The relative path of the file containing the xs table.


Additional string to specify an access route, such as UNIX directory. This entry is typically 0.


Describes whether the file contains formated (1) or unformated (2) file.


If filetype is 1, address is the line number of the xsdir table. If filetype is 2, address is the record number.


Length of the second block of a data table.


Unused for filetype = 1. For filetype = 2, recordlength is the number of entires per record times the size (in bytes) of each entry.


Unused for filetype = 1. For filetype = 2, it is the number of entries per record


Temperature in MeV for neutron data only.


True if xs table describes continuous energy neutron data with unresolved resonance range probability tables.

to_serpent(self, directory='')[source]

Converts table to serpent format.


The directory where Serpent data is to be stored.

property alias

Returns the name of the table entry <ZIAD>.<library id>.

property metastable

Returns 1 is xsdir table nuclide is metastable. Returns zero otherwise.

property serpent_type

Converts cross section table type to Serpent format: :1: continuous energy (c). :2: dosimetry table (y). :3: termal (t).

property zaid

Returns the ZIAD of the nuclide.

pyne.mcnp.mat_from_inp_line(filename, mat_line, densities='None')[source]

This function reads an MCNP material card from a file and returns a Material or Multimaterial object for the material described by the card. This function is used by mats_from_inp().


Name of the MCNP input file


Line number of the material card or interest

densitieslist of floats

The densities associated with the material

finished_matMaterial or MultiMaterial

A Material object is returned if there is 1 density supplied. If multiple densities are supplied a MultiMaterial is returned.


This function reads an MCNP inp file and returns a mapping of material numbers to material objects.


MCNP input file


Keys are MCNP material numbers and values are PyNE material objects (for single density materials) and MultiMaterial objects (for multiple density materials).

pyne.mcnp.mesh_to_geom(mesh, frac_type='mass', title_card='Generated from PyNE Mesh')[source]

This function reads a structured Mesh object and returns the geometry portion of an MCNP input file (cells, surfaces, materials), prepended by a title card. The mesh must be axis aligned. Surfaces and cells are written in xyz iteration order (z changing fastest).

meshPyNE Mesh object

A structured Mesh object with materials and valid densities.

frac_typestr, optional

Either ‘mass’ or ‘atom’. The type of fraction to use for the material definition.

title_cardstr, optional

The MCNP title card to appear at the top of the input file.


The title, cell, surface, and material cards of an MCNP input file in the proper order.