1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#!/usr/bin/env python3
###################################################################################
# #
# Monte Carlo Particle Lists : MCPL #
# #
# Simple example with comments, showing how one can use the mcpl.py module to #
# access MCPL files. #
# #
# Find more information and updates at https://mctools.github.io/mcpl/ #
# #
# This file can be freely used as per the terms in the LICENSE file. #
# #
# Written by Thomas Kittelmann, 2017. #
# #
###################################################################################
#Make this example work with both python2 and python3:
from __future__ import print_function
import sys, os
#Import mcpl module (with a fall-back sys.path edit, so the example can be run
#from an MCPL installation even though the user did not set PYTHONPATH correctly):
try:
import mcpl
except ImportError:
try:
sys.path.insert(0,os.path.join(os.path.dirname(__file__),'..','python'))
import mcpl
except ImportError:
sys.path.insert(0,os.path.join(os.path.dirname(__file__),'..','src','python'))
import mcpl
#Get name of input file to open from command line:
inputfile = sys.argv[1]
#Uncomment next line for module documentation:
#help(mcpl)
#Open the file:
f = mcpl.MCPLFile(inputfile)
#Dump entire header to stdout:
f.dump_hdr()
#Or access relevant parts:
print( 'Number of particles in file: %i' % f.nparticles )
print( 'Numbers are in single-precision: %s' % f.opt_singleprec )
for c in f.comments:
print( "Some comment: %s" % c )
#Loop over all particles and print their positions and energies:
for p in f.particles:
print( p.x, p.y, p.z, p.ekin )
#help(p) #<-- uncomment to see all field names
#Same, but each iteration will be over a big "block" of 10000 particles, so all
#fields are now numpy arrays of length 10000:
for p in f.particle_blocks:
print( p.x, p.y, p.z, p.ekin )
#NB: change blocklength to, say, 1000, by opening file with:
# mcpl.MCPLFile('example.mcpl',blocklength=1000)
|