“Read” Examples¶
PyMCNP provides endpoints to read MCNP files.
Reading INP¶
Note
This example requires example_00.inp.
Code:
"""
Example reading INP files.
This example reads an INP file using two methods: `__init__` and `from_mcnp`.
First, it reads the input file from a path, and second, it reads an input file
from the source string directly. Finally, it prints both results.
"""
import pathlib
import pymcnp
# Reading INP using `from_file`.
path = pathlib.Path('example_00.inp')
inp = pymcnp.Inp.from_file(path)
print(f'Reading INP from `{path}`:')
print(inp)
# Reading INP using `from_mcnp`.
inp = pymcnp.Inp.from_mcnp(
"""
Create `Inp`
1 0 -12 imp:n=1
2 23 0.5 +12:-13 imp:n=1
3 21 0.5 -12 +13:-14 imp:n=1
4 0 +14 imp:n=1
12 rpp -5 5 -5 5 -5 5
13 rpp -1 1 -1 1 -1 1
14 so 60
m21 007014 -0.797088 008016 -0.199514
m23 082204 -0.014 082206 -0.241 082207 -0.221 082208 -0.524
"""[1:-1]
)
print('Reading INP file from string:')
print(inp)
Output:
Reading INP from `example_00.inp`:
Isotropic neutron source over lunar regolith with two gamma detectors
c
c cell cards
c
100 100 -1.54 -1 imp:P,N 1
200 300 -5.06 -2 imp:P,N 1
201 200 -5.1 -3 imp:P,N 1
777 900 -0.000001 1 2 3 -99 imp:P,N 1
999 0 99 imp:P,N 0
...
Reading INP Cell¶
Note
This example requires example_00.inp.
Code:
"""
Example reading INP cells.
This example reads a cell card from `example_00.inp`. First, it reads the INP
file using `Inp.from_file`, and second it gets the cell using the `cells`
attribute. Then it prints the cell it retrieved.
"""
import pathlib
import pymcnp
# Reading INP.
path = pathlib.Path('example_00.inp')
inp = pymcnp.Inp.from_file(path)
# Reading cell.
cell = inp.cells[3]
print(f'Reading cell from `{path}`:')
print(cell)
Output:
Reading cell from `example_00.inp`:
100 100 -1.54 -1 imp:P,N 1
Reading INP Material¶
Note
This example requires example_00.inp.
Code:
"""
Example reading INP materials.
This example reads a material card from `example_00.inp`. First, it reads the
INP file using `Inp.from_file`, and second it gets the material using the
`data` attribute. Then it prints the material it retrieved.
"""
import pathlib
import pymcnp
# Reading INP.
path = pathlib.Path('example_00.inp')
inp = pymcnp.Inp.from_file(path)
# Reading material.
material = inp.data[4]
print(f'Reading material from `{path}`:')
print(material)
Output:
Reading material from `example_00.inp`:
m100 013027 -0.1701 020040 -0.131742819 020042 -0.000879273 020044 &
-0.002834874 026054 -0.00086506 026056 -0.013579592 026057 -0.000313612 &
008016 -0.45489192 019039 -0.0001865162 019041 -0.0000134604 012024 &
-0.00402849 012025 -0.00051 012026 -0.00056151 011023 -0.0045 014028 &
-0.190532718 014029 -0.00967921 014030 -0.006388072 090232 -0.006 022046 &
-0.000066 022047 -0.00005952 022048 -0.00058976 022049 -0.00004328 022050 &
-0.00004144
Reading INP Surface¶
Note
This example requires example_00.inp.
Code:
"""
Example reading INP surfaces.
This example reads a surface card from `example_00.inp`. First, it reads the
INP file using `Inp.from_file`, and second it gets the surface using the
`surfaces` attribute. Then it prints the surface it retrieved.
"""
import pathlib
import pymcnp
# Reading INP.
path = pathlib.Path('example_00.inp')
inp = pymcnp.Inp.from_file(path)
# Reading surface.
surface = inp.surfaces[4]
print(f'Reading surface from `{path}`:')
print(surface)
Output:
Reading surface from `example_00.inp`:
2 rcc 50 0 0 0 0 7.62 3.81
Reading OUTP¶
Note
This example requires example_00.outp.
Code:
"""
Example reading OUTP files.
This example reads an OUTP file using two methods: `__init__` and `from_mcnp`.
First, it reads the output file from a path, and second, it reads an output
file from the source string directly. Finally, it prints both results.
"""
import pathlib
import pymcnp
# Reading OUTP using `from_file`.
path = pathlib.Path('example_00.outp')
outp = pymcnp.Outp.from_file(path)
print(f'Reading OUTP from `{path}`:')
print(outp)
# Reading OUTP using `from_mcnp`.
outp = pymcnp.Outp.from_mcnp(""" Code Name & Version = MCNP_6.20, 6.2.0
_/ _/ _/_/_/ _/ _/ _/_/_/ _/_/_/
_/_/ _/_/ _/ _/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/_/_/ _/_/_/
_/ _/ _/ _/ _/_/ _/ _/ _/
_/ _/ _/_/_/ _/ _/ _/ _/_/
+-----------------------------------------------------------------------+
| Copyright (2018). Los Alamos National Security, LLC. All rights !
| reserved. !
| This material was produced under U.S. Government contract !
| DE-AC52-06NA25396 for Los Alamos National Laboratory, which is !
| operated by Los Alamos National Security, LLC for the U.S. !
| Department of Energy. The Government is granted for itself and !
| others acting on its behalf a paid-up, nonexclusive, irrevocable !
| worldwide license in this material to reproduce, prepare derivative !
| works, and perform publicly and display publicly. Beginning five (5) !
| years after February 14, 2018, subject to additional five-year !
| worldwide renewals, the Government is granted for itself and others !
| acting on its behalf a paid-up, nonexclusive, irrevocable worldwide !
| license in this material to reproduce, prepare derivative works, !
| distribute copies to the public, perform publicly and display !
| publicly, and to permit others to do so. NEITHER THE UNITED STATES !
| NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR LOS ALAMOS NATIONAL !
| SECURITY, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY, !
| EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY !
| FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, !
| APPARATUS, PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE !
| WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS. !
+-----------------------------------------------------------------------+
1mcnp version 6 ld=02/20/18 06/17/25 16:52:59
************************************************************************* probid = 06/17/25 16:52:59
i=valid_3_3g.i
bad trouble in subroutine oldcd1 of imcn
too many entries on fill card.
""")
print('Reading OUTP file from string:')
print(outp)
Output:
Reading OUTP from `PyMCNP/files/outp/example_00.outp`:
Code Name & Version = MCNP_6.20, 6.2.0
_/ _/ _/_/_/ _/ _/ _/_/_/ _/_/_/
_/_/ _/_/ _/ _/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/ _/ _/_/_/ _/_/_/
_/ _/ _/ _/ _/_/ _/ _/ _/
_/ _/ _/_/_/ _/ _/ _/ _/_/
...
Reading OUTP Tally 1¶
Note
This example requires example_00.outp.
Code:
"""
Example reading OUTP type #1 tallies.
This example reads a tally type #1 tables card from `example_00.inp`. First, it
reads the OUTP file using `Outp.from_file`, and second it gets a dataframe
using `to_dataframe`. Then it prints the resulting dataframe.
"""
import pathlib
import pymcnp
TALLY = '1'
SURFACE = '2.1'
ANGLE_FROM = '180.0'
# Reading OUTP.
path = pathlib.Path('example_00.outp')
outp = pymcnp.Outp.from_file(path)
# Reading tallies.
tallies = outp.to_dataframe()
tally = tallies[TALLY]
tally = tally.loc[tally['surface'] == SURFACE]
tally = tally.loc[tally['angle_from'] == ANGLE_FROM]
print(f'Reading tally #{TALLY} cell #{SURFACE} from `{path}`:')
print(tally)
Output:
Reading tally #1 cell #2.1 from `PyMCNP/files/outp/example_00.outp`:
bins counts ... particles nps
0 0.10000 0.000010 ... photons 1000000
1 0.11934 0.000003 ... photons 1000000
2 0.13867 0.000005 ... photons 1000000
3 0.15801 0.000002 ... photons 1000000
4 0.17734 0.000002 ... photons 1000000
.. ... ... ... ... ...
508 9.92270 0.000000 ... photons 1000000
509 9.94200 0.000000 ... photons 1000000
510 9.96130 0.000000 ... photons 1000000
511 9.98070 0.000000 ... photons 1000000
512 10.00000 0.000000 ... photons 1000000
[513 rows x 10 columns]
Reading OUTP Tally 2¶
Note
This example requires example_03.outp.
Code:
"""
Example reading OUTP type #2 tallies.
This example reads a tally type #2 tables card from `example_03.inp`. First, it
reads the OUTP file using `Outp.from_file`, and second it gets a dataframe
using `to_dataframe`. Then it prints the resulting dataframe.
"""
import pathlib
import pymcnp
TALLY = '2'
SURFACE = '8'
# Reading tallies.
path = pathlib.Path('example_03.outp')
outp = pymcnp.Outp.from_file(path)
tallies = outp.to_dataframe()
tally = tallies[TALLY]
tally = tally.loc[tally['surface'] == SURFACE]
print(f'Reading tally #{TALLY} cell #{SURFACE} from `{path}`:')
print(tally)
Output:
Reading tally #2 cell #8 from `PyMCNP/files/outp/example_03.outp`:
bins counts ... particles nps
0 0.0 0.000000 ... neutrons 100000
1 100.0 0.000078 ... neutrons 100000
2 200.0 0.000085 ... neutrons 100000
3 300.0 0.000080 ... neutrons 100000
4 400.0 0.000085 ... neutrons 100000
... ... ... ... ... ...
4496 449600.0 0.000000 ... neutrons 100000
4497 449700.0 0.000000 ... neutrons 100000
4498 449800.0 0.000000 ... neutrons 100000
4499 449900.0 0.000000 ... neutrons 100000
4500 450000.0 0.000000 ... neutrons 100000
[4501 rows x 8 columns]
Reading OUTP Tally 4¶
Note
This example requires example_02.outp.
Code:
"""
Example reading OUTP type #4 tallies.
This example reads a tally type #4 tables card from `example_02.inp`. First, it
reads the OUTP file using `Outp.from_file`, and second it gets a dataframe
using `to_dataframe`. Then it prints the resulting dataframe.
"""
import pathlib
import pymcnp
TALLY = '14'
CELL = '12'
# Reading tallies.
path = pathlib.Path('example_02.outp')
outp = pymcnp.Outp.from_file(path)
tallies = outp.to_dataframe()
tally = tallies[TALLY]
tally = tally.loc[tally['cell'] == CELL]
print(f'Reading tally #{TALLY} cell #{CELL} from `{path}`:')
print(tally)
Output:
Reading tally #14 cell #12 from `PyMCNP/files/outp/example_02.outp`:
bins counts ... particles nps
0 0.10000 3.622140e-07 ... photons 1000000000
1 0.10989 6.949680e-08 ... photons 1000000000
2 0.11978 6.931230e-08 ... photons 1000000000
3 0.12967 6.818310e-08 ... photons 1000000000
4 0.13956 6.641000e-08 ... photons 1000000000
... ... ... ... ... ...
997 9.96040 2.980820e-10 ... photons 1000000000
998 9.97030 2.593030e-10 ... photons 1000000000
999 9.98020 2.353170e-10 ... photons 1000000000
1000 9.99010 2.747060e-10 ... photons 1000000000
1001 10.00000 2.536390e-10 ... photons 1000000000
[1002 rows x 8 columns]
Reading OUTP Tally 8¶
Note
This example requires example_01.outp.
Code:
"""
Example reading OUTP type #8 tallies.
This example reads a tally type #8 tables card from `example_01.inp`. First, it
reads the OUTP file using `Outp.from_file`, and second it gets a dataframe
using `to_dataframe`. Then it prints the resulting dataframe.
"""
import pathlib
import pymcnp
TALLY = '18'
CELL = '200'
# Reading tallies.
path = pathlib.Path('example_01.outp')
outp = pymcnp.Outp.from_file(path)
tallies = outp.to_dataframe()
tally = tallies[TALLY]
tally = tally.loc[tally['cell'] == CELL]
print(f'Reading tally #{TALLY} cell #{CELL} from `{path}`:')
print(tally)
Output:
Reading tally #18 cell #200 from `PyMCNP/files/outp/example_01.outp`:
bins counts ... particles nps
0 0.10000 0.000109 ... photons 10000000
1 0.11934 0.000008 ... photons 10000000
2 0.13867 0.000008 ... photons 10000000
3 0.15801 0.000007 ... photons 10000000
4 0.17734 0.000006 ... photons 10000000
.. ... ... ... ... ...
508 9.92270 0.000000 ... photons 10000000
509 9.94200 0.000000 ... photons 10000000
510 9.96130 0.000000 ... photons 10000000
511 9.98070 0.000000 ... photons 10000000
512 10.00000 0.000000 ... photons 10000000
[513 rows x 8 columns]
Reading PTRAC¶
Note
This example requires example_02.ptrac.
Code:
"""
Example reading PTRAC files.
This example reads an PTRAC file using two methods: `__init__` and `from_mcnp`.
First, it reads the ptrac file from a path, and second, it reads an ptrac file
from the source string directly. Finally, it prints both results.
"""
import pathlib
import pymcnp
# Reading PTRAC using `from_file`.
path = pathlib.Path('example_02.ptrac')
ptrac = pymcnp.Ptrac.from_file(path)
print(f'Reading PTRAC from `{path}`:')
print(ptrac)
# Reading PTRAC using `from_mcnp`.
ptrac = pymcnp.Ptrac.from_mcnp(""" -1
mcnp 6 05/08/13 07/14/25 11:39:04
Sample Problem Input Deck
1.4000E+01 1.0000E+00 1.0000E+02 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 1.0000E+00 1.0000E+04
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 0.0000E+00
2 5 3 6 3 6 3 6 3 6 3 1 4 0 0 0 0 0 0 0
1 2 7 8 9 17 18 20 21 22 7 8 10 11 17 18 20 21 22 7 8 12 13 17 18 20 21 22 7 8
10 11 17 18 20 21 22 7 8 14 15 17 18 20 21 22
1 1000
3000 1 40 1 1
0.00000E+00 -0.40000E+01 -0.25000E+01
4000 2 7 0 3 3
0.25424E+00 -0.37634E+01 -0.21403E+01
4000 2 6000 51 3 3
0.14362E+01 -0.26632E+01 -0.46819E+00
4000 2 6000 2 3 3
0.13310E+01 -0.10442E+01 0.70227E-02
3000 2 6000 2 3 3
0.34660E+01 -0.27738E+01 0.17002E+00
5000 3 5 55 4 0
0.50000E+01 -0.45169E+01 0.15231E+01
9000 3 1 1 4 0
0.50000E+01 -0.45169E+01 0.15231E+01
9000 1 40 1 1
0.00000E+00 -0.40000E+01 -0.25000E+01
""")
print('Reading PTRAC file from string:')
print(ptrac)
Output:
Reading PTRAC from `PyMCNP/files/ptrac/example_02.ptrac`:
-1
mcnp 6 05/08/13 07/14/25 11:39:04
Detector response single carbon block
1.4000E+01 1.0000E+00 1.0000E+02 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 1.0000E+00 1.0000E+04
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 0.0000E+00
2 6 3 7 3 7 3 7 3 7 3 0 4 0 0 0 0 0 0 0
1 2 7 8 9 16 17 18 20 21 22 7 8 10 11 16 17 18 20 21 22 7 8 12 13 16 17 18 20 21
22 7 8 10 11 16 17 18 20 21 22 7 8 14 15 16 17 18 20 21 22
1 1000
...
9000 1 40 1 77 1
0.00000E+00 0.00000E+00 0.00000E+00
Reading PTRAC file from string:
-1
mcnp 6 05/08/13 07/14/25 11:39:04
Sample Problem Input Deck
1.4000E+01 1.0000E+00 1.0000E+02 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 1.0000E+00 1.0000E+04
0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 0.0000E+00 0.0000E+00
2 5 3 6 3 6 3 6 3 6 3 1 4 0 0 0 0 0 0 0
1 2 7 8 9 17 18 20 21 22 7 8 10 11 17 18 20 21 22 7 8 12 13 17 18 20 21 22 7 8
10 11 17 18 20 21 22 7 8 14 15 17 18 20 21 22
1 1000
3000 1 40 1 1
0.00000E+00 -0.40000E+01 -0.25000E+01
4000 2 7 0 3 3
0.25424E+00 -0.37634E+01 -0.21403E+01
4000 2 6000 51 3 3
0.14362E+01 -0.26632E+01 -0.46819E+00
4000 2 6000 2 3 3
0.13310E+01 -0.10442E+01 0.70227E-02
3000 2 6000 2 3 3
0.34660E+01 -0.27738E+01 0.17002E+00
5000 3 5 55 4 0
0.50000E+01 -0.45169E+01 0.15231E+01
9000 3 1 1 4 0
0.50000E+01 -0.45169E+01 0.15231E+01
9000 1 40 1 1
0.00000E+00 -0.40000E+01 -0.25000E+01