Skip to content

Commit

Permalink
Add hook for converting ASCII SSV files to MCPL
Browse files Browse the repository at this point in the history
Add ASCII-SSV format compatibility. This includes a mcpl2ssv hook, which
redirects to mcpltool --text, and a ssv2mcpl hook, with the inverse
functionality. Also add save2ascii and apend2ascii functions in Python
API, which allow saving a particle list with numpy array format into
an ASCII-SSV file, which then can be converted to MCPL format. This adds
an indirect way of writing MCPL files from Python (github issue mctools#54).
  • Loading branch information
inti-abbate committed Aug 28, 2022
1 parent 69303bd commit 42beb8d
Show file tree
Hide file tree
Showing 13 changed files with 891 additions and 188 deletions.
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ option( BUILD_WITHSSW "Whether to build the MCPL-SSW converters (for MCNP)." O
option( BUILD_WITHPTRAC "Whether to build the MCPL-PTRAC converters (for MCNP)." ON )
option( BUILD_WITHPHITS "Whether to build the MCPL-PHITS converters." ON )
option( BUILD_WITHT4 "Whether to build the TRIPOLI-4 converters." ON )
option( BUILD_WITHSSV "Whether to build the ASCII-SSV converters." ON )
option( BUILD_WITHG4 "Whether to build Geant4 plugins." OFF )
option( BUILD_FAT "Whether to also build the fat binaries." OFF )
option( INSTALL_PY "Whether to also install mcpl python files." ON )
Expand Down Expand Up @@ -279,6 +280,28 @@ if (BUILD_WITHT4)
endif()
endif()

if (BUILD_WITHSSV)
add_library(ssvmcpl SHARED "${SRC}/ssv/ssvmcpl.c" "${SRC}/ssv/ssvread.c")
target_include_directories(ssvmcpl PUBLIC "${SRC}/ssv")
target_link_libraries(ssvmcpl mcpl common)
if (MATH_NEEDS_LIBM)
target_link_libraries(ssvmcpl m)
endif()
add_executable(ssv2mcpl "${SRC}/ssv/ssv2mcpl_app.c")
target_link_libraries(ssv2mcpl ssvmcpl)
add_executable(mcpl2ssv "${SRC}/ssv/mcpl2ssv_app.c")
target_link_libraries(mcpl2ssv ssvmcpl)
if (binaryprops)
set_target_properties(ssvmcpl PROPERTIES ${binaryprops})
set_target_properties(ssv2mcpl PROPERTIES ${binaryprops})
set_target_properties(mcpl2ssv PROPERTIES ${binaryprops})
endif()
install(TARGETS ssv2mcpl mcpl2ssv ssvmcpl ${INSTDEST})
if (BUILD_EXAMPLES)
install(FILES "${SRCEX}/example_ascii.ssv" DESTINATION examples)
endif()
endif()

if (INSTALL_PY)
install(FILES "${SRC}/python/mcpl.py" DESTINATION ${MCPL_PYPATH})
install(PROGRAMS "${SRC}/python/pymcpltool" DESTINATION ${MCPL_BINDIR})
Expand Down
4 changes: 4 additions & 0 deletions FILES
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ src/tripolistock/ : MCPL hooks for TRIPOLI-4 in C, in the form of a few .h/.c
file pairs and two command line applications which can be
used to convert between the MCPL format and the .stock
files used by TRIPOLI-4 (STORAGE).
src/ssv/ : MCPL hooks for ASCII-SSV in C, in the form of a few .h/.c
file pairs and two command line applications which can be
used to convert between the MCPL format and an ASCII
Space Separated Values format.
src/mcstas/ : No actual code is here, just a small reminder of how the
MCPL plugin shipped with McStas can be used.
src/mcxtrace/ : No actual code is here, just a small reminder of how the
Expand Down
72 changes: 72 additions & 0 deletions examples/example_ascii.ssv

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/mcnpptrac/ptracread.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ptrac_file_t ptrac_open_internal( const char * filename )
ptrac_file_t out;
out.internal = f;

f->file = fopen(filename,"rb");
f->file = fopen(filename,"r");
if (!f->file)
ptrac_error("Unable to open file!");

Expand Down
58 changes: 58 additions & 0 deletions src/python/mcpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
_str('MCPLError'),
_str('dump_file'),
_str('convert2ascii'),
_str('save2ascii'),
_str('append2ascii'),
_str('app_pymcpltool'),
_str('collect_stats'),
_str('dump_stats'),
Expand Down Expand Up @@ -1018,6 +1020,62 @@ def convert2ascii(mcplfile,outfile):
for idx,p in enumerate(fin.particles):
fout.write(fmtstr%(idx,p.pdgcode,p.ekin,p.x,p.y,p.z,p.ux,p.uy,p.uz,p.time,p.weight,p.polx,p.poly,p.polz,p.userflags))

def save2ascii(particles, outfile):
"""
Save numpy array particles into outfile using a simple ASCII-based SSV format.
This function is equivalent to convert2ascii from mcpl, and uses the
same format as 'mcpl2ssv' and 'mcpltool --text' commands. Generated
SSV file can be converted to MCPL format with 'ssv2mcpl' command.
Parameters
----------
particles: array-like
Array of particle parameters. Must have shape (nparticles, 13),
with the following column order:
pdgcode ekin[MeV] x[cm] y[cm] z[cm] ux uy uz time[ms] weight pol-x pol-y pol-z
Unused parameters should be filled with 0.
outfile: str
Name of ASCII-SSV file. If it exist, its content will be overwritten.
"""
if(particles.shape[1] != 13):
raise MCPLError('Particle array should have shape (nparticles,13).')
nparticles = len(particles)
with open(outfile, "w") as fout:
fout.write("#MCPL-ASCII\n#ASCII-FORMAT: v1\n#NPARTICLES: %i\n#END-HEADER\n"%nparticles)
fout.write("index pdgcode ekin[MeV] x[cm] "
+" y[cm] z[cm] ux "
+" uy uz time[ms] weight "
+" pol-x pol-y pol-z userflags\n")
fmtstr="%5i %11i %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g\n"
for idx,p in enumerate(particles):
fout.write(fmtstr%(idx,*p))


def append2ascii(particles, outfile):
"""
Append numpy array particles into outfile using a simple ASCII-based SSV format.
This function uses the same format as save2ascii, but appends particles
without writing a header.
Parameters
----------
particles: array-like
Array of particle parameters. Must have shape (nparticles, 13),
with the following column order:
pdgcode ekin[MeV] x[cm] y[cm] z[cm] ux uy uz time[ms] weight pol-x pol-y pol-z
Unused parameters should be filled with 0.
outfile: str
Name of ASCII-SSV file where to append particles.
"""
if(particles.shape[1] != 13):
raise MCPLError('Particle array should have shape (nparticles,13).')
with open(outfile, "a") as fout:
fmtstr="%5i %11i %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g %23.18g\n"
for idx,p in enumerate(particles):
fout.write(fmtstr%(idx,*p))

def _pymcpltool_usage(progname,errmsg=None):
if errmsg:
print("ERROR: %s\n"%errmsg)
Expand Down
14 changes: 14 additions & 0 deletions src/ssv/mcpl2ssv_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "ssvmcpl.h"

/////////////////////////////////////////////////////////////////////////////////////
// //
// ssv2mcpl : a simple command line utility for converting SSV text files to //
// MCPL. //
// //
// Written 2021, [email protected] (Instituto Balseiro). //
// //
/////////////////////////////////////////////////////////////////////////////////////

int main(int argc,char** argv) {
return mcpl2ssv_app(argc,argv);
}
14 changes: 14 additions & 0 deletions src/ssv/ssv2mcpl_app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "ssvmcpl.h"

/////////////////////////////////////////////////////////////////////////////////////
// //
// ssv2mcpl : a simple command line utility for converting SSV text files to //
// MCPL. //
// //
// Written 2021, [email protected] (Instituto Balseiro). //
// //
/////////////////////////////////////////////////////////////////////////////////////

int main(int argc,char** argv) {
return ssv2mcpl_app(argc,argv);
}
Loading

0 comments on commit 42beb8d

Please sign in to comment.