From 69303bddd24d772e6067bb48ece1962c378d8b5d Mon Sep 17 00:00:00 2001 From: inti Date: Mon, 29 Mar 2021 18:46:32 -0300 Subject: [PATCH 1/5] Add basic hooks for MCNP-PTRAC and TRIPOLI-STORAGE Based on the existent hooks (MCNP SSW, PHITS), new ones for PTRAC files from MCNP(X) and stock files from TRIPOLI-4 (STORAGE) were added. Both formats are text based, and so is the processing made by the added functions. Only the applications for converting to MCPL were included (ptrac2mcpl, stock2mcpl). --- CMakeLists.txt | 58 ++- FILES | 8 + INSTALL | 2 + README | 12 +- examples/example_mcnpptrac.ptrac | 198 ++++++++ examples/example_tripolistock.stock | 721 ++++++++++++++++++++++++++++ src/mcnpptrac/ptrac2mcpl_app.c | 14 + src/mcnpptrac/ptracmcpl.c | 183 +++++++ src/mcnpptrac/ptracmcpl.h | 34 ++ src/mcnpptrac/ptracread.c | 186 +++++++ src/mcnpptrac/ptracread.h | 74 +++ src/tripolistock/stock2mcpl_app.c | 14 + src/tripolistock/stockmcpl.c | 186 +++++++ src/tripolistock/stockmcpl.h | 34 ++ src/tripolistock/stockread.c | 155 ++++++ src/tripolistock/stockread.h | 75 +++ 16 files changed, 1940 insertions(+), 14 deletions(-) create mode 100644 examples/example_mcnpptrac.ptrac create mode 100644 examples/example_tripolistock.stock create mode 100644 src/mcnpptrac/ptrac2mcpl_app.c create mode 100644 src/mcnpptrac/ptracmcpl.c create mode 100644 src/mcnpptrac/ptracmcpl.h create mode 100644 src/mcnpptrac/ptracread.c create mode 100644 src/mcnpptrac/ptracread.h create mode 100644 src/tripolistock/stock2mcpl_app.c create mode 100644 src/tripolistock/stockmcpl.c create mode 100644 src/tripolistock/stockmcpl.h create mode 100644 src/tripolistock/stockread.c create mode 100644 src/tripolistock/stockread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 07883ea..358105e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,4 @@ + ################################################################################## # # # CMake file which can be used to compile and link all code in the MCPL # @@ -59,14 +60,16 @@ if(NOT DEFINED MCPL_NOTOUCH_CMAKE_BUILD_TYPE) if ("${CMAKE_BUILD_TYPE}" STREQUAL "") #This can happen if parent project called the project(..) function before #doing the song and dance we did above. - set(CMAKE_BUILD_TYPE Release) + set(CMAKE_BUILD_TYPE Release) endif() endif() option( BUILD_EXAMPLES "Whether to build examples." OFF ) option( BUILD_WITHZLIB "Whether to link with zlib if available." ON ) option( BUILD_WITHSSW "Whether to build the MCPL-SSW converters (for MCNP)." ON ) +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_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 ) @@ -117,17 +120,17 @@ if ( MODIFY_RPATH ) #via interface targets, so we have to use a variable-based workaround: if ( NOT DEFINED CMAKE_INSTALL_RPATH_USE_LINK_PATH ) #TODO: Figure out if we really need this (perhaps only for geant4 targets?) - set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE ) +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) endif() if( NOT APPLE ) #Relocatable RPATHS: $ORIGIN in RPATH (including the $-char!!) means the #location of the binary requiring the dependency: list( APPEND binaryprops INSTALL_RPATH "$ORIGIN/${MCPL_relpath_BINDIR2LIBDIR}" ) - else() +else () #On OSX, rpaths are absolute paths (todo: revisit if this is still the case) get_filename_component( tmp "${CMAKE_INSTALL_PREFIX}/${MCPL_LIBDIR}" ABSOLUTE) list( APPEND binaryprops INSTALL_RPATH "${tmp}" ) - endif() +endif () #Test if compiler supports -Wl,--disable-new-dtags. If it does, apply it #(otherwise RPATH sections in binaries become RUNPATH instead, which can be @@ -159,7 +162,7 @@ file(WRITE ${TMP_TESTDIR}/test.c "${TMP_TESTLIBMSRC}") try_compile(ALWAYS_HAS_MATH "${TMP_TESTDIR}" "${TMP_TESTDIR}/test.c") if (ALWAYS_HAS_MATH) set( MATH_NEEDS_LIBM OFF ) -else() +else () set( MATH_NEEDS_LIBM ON ) set( TMP_TESTDIR ${PROJECT_BINARY_DIR}/test_libm2 ) file( WRITE ${TMP_TESTDIR}/test.c "${TMP_TESTLIBMSRC}" ) @@ -167,9 +170,9 @@ else() if( NOT tmp ) message(FATAL_ERROR "Could not figure out link flags needed to enable math functions") endif() -endif() +endif () -add_library( mcpl SHARED "${SRC}/mcpl/mcpl.c" ) +add_library(mcpl SHARED "${SRC}/mcpl/mcpl.c") set(MCPL_LIBNAME "${CMAKE_SHARED_LIBRARY_PREFIX}mcpl${CMAKE_SHARED_LIBRARY_SUFFIX}") target_link_libraries( mcpl PRIVATE common ) @@ -222,6 +225,25 @@ if (BUILD_WITHSSW) install(TARGETS mcpl2ssw ssw2mcpl sswmcpl ${INSTDEST}) endif() +if (BUILD_WITHPTRAC) + add_library(ptracmcpl SHARED "${SRC}/mcnpptrac/ptracmcpl.c" "${SRC}/mcnpptrac/ptracread.c") + target_include_directories(ptracmcpl PUBLIC "${SRC}/mcnpptrac") + target_link_libraries(ptracmcpl mcpl common) + if (MATH_NEEDS_LIBM) + target_link_libraries(ptracmcpl m) + endif() + add_executable(ptrac2mcpl "${SRC}/mcnpptrac/ptrac2mcpl_app.c") + target_link_libraries(ptrac2mcpl ptracmcpl) + if (binaryprops) + set_target_properties(ptracmcpl PROPERTIES ${binaryprops}) + set_target_properties(ptrac2mcpl PROPERTIES ${binaryprops}) + endif() + install(TARGETS ptrac2mcpl ptracmcpl ${INSTDEST}) + if (BUILD_EXAMPLES) + install(FILES "${SRCEX}/example_mcnpptrac.ptrac" DESTINATION examples) + endif() +endif() + if (BUILD_WITHPHITS) add_library(phitsmcpl SHARED "${SRC}/phits/phitsmcpl.c" "${SRC}/phits/phitsread.c") target_include_directories(phitsmcpl PUBLIC "${SRC}/phits") @@ -238,6 +260,25 @@ if (BUILD_WITHPHITS) endif() endif() +if (BUILD_WITHT4) + add_library(stockmcpl SHARED "${SRC}/tripolistock/stockmcpl.c" "${SRC}/tripolistock/stockread.c") + target_include_directories(stockmcpl PUBLIC "${SRC}/tripolistock") + target_link_libraries(stockmcpl mcpl common) + if (MATH_NEEDS_LIBM) + target_link_libraries(stockmcpl m) + endif() + add_executable(stock2mcpl "${SRC}/tripolistock/stock2mcpl_app.c") + target_link_libraries(stock2mcpl stockmcpl) + if (binaryprops) + set_target_properties(stockmcpl PROPERTIES ${binaryprops}) + set_target_properties(stock2mcpl PROPERTIES ${binaryprops}) + endif() + install(TARGETS stock2mcpl stockmcpl ${INSTDEST}) + if (BUILD_EXAMPLES) + install(FILES "${SRCEX}/example_tripolistock.stock" DESTINATION examples) + endif() +endif() + if (INSTALL_PY) install(FILES "${SRC}/python/mcpl.py" DESTINATION ${MCPL_PYPATH}) install(PROGRAMS "${SRC}/python/pymcpltool" DESTINATION ${MCPL_BINDIR}) @@ -284,6 +325,7 @@ if (BUILD_FAT) endif() if (BUILD_WITHPHITS) add_executable(phits2mcpl_fat "${SRCFAT}/phits2mcpl_app_fat.c") + target_link_libraries(phits2mcpl_fat m) add_executable(mcpl2phits_fat "${SRCFAT}/mcpl2phits_app_fat.c") if (MATH_NEEDS_LIBM) target_link_libraries( phits2mcpl_fat m common ) @@ -323,7 +365,7 @@ endif() if (BUILD_WITHG4) find_package(Geant4) if(NOT Geant4_FOUND) - message(FATAL_ERROR "BUILD_WITHG4 set to ON but failed to enable Geant4 support.") + message("BUILD_WITHG4 set to ON but failed to enable Geant4 support.") endif() else() set(Geant4_FOUND NO) diff --git a/FILES b/FILES index 40a5f70..10d7017 100644 --- a/FILES +++ b/FILES @@ -26,10 +26,18 @@ src/mcnpssw/ : MCPL hooks for MCNP 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 SSW files used by MCNP. +src/mcnpptrac/ : MCPL hooks for MCNP 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 PTRAC files used + by MCNP. src/phits/ : MCPL hooks for PHITS 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 binary dump files used by PHITS. +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/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 diff --git a/INSTALL b/INSTALL index 6252724..942af00 100644 --- a/INSTALL +++ b/INSTALL @@ -41,7 +41,9 @@ is a quick recipe (using an in-source build for simplicity): * -DBUILD_FAT=ON [whether to build "fat" binaries, default is OFF] * -DBUILD_WITHG4=OFF [whether to build G4 hooks, default is ON] * -DBUILD_WITHSSW=OFF [whether to build SSW hooks, default is ON] + * -DBUILD_WITHPTRAC=OFF [whether to build PTRAC hooks, default is ON] * -DBUILD_WITHPHITS=OFF [whether to build PHITS hooks, default is ON] + * -DBUILD_WITHSTOCK=OFF [whether to build STOCK hooks, default is ON] * -DINSTALL_PY=OFF [whether to install python files, default is ON] * -DMODIFY_RPATH=OFF [do not do any special rpath treatment for binaries] diff --git a/README b/README index 35941b1..89137e8 100644 --- a/README +++ b/README @@ -7,12 +7,12 @@ utilities include both command line tools and programming interfaces for C/C++ and python. MCPL I/O hooks for Geant4 (geant4.cern.ch) and MCNP (SSW files) are included in -this distribution as well. Hooks for McStas (mcstas.org) and McXtrace -(mcxtrace.org) are already included directly upstream in those applications. A -few examples of how to use the Geant4 hooks, or how to interact with MCPL files -from standalone C code, are also provided. Refer to the FILES file for more -information about included files, and refer to the INSTALL file for build -instructions. +this distribution. Basic hooks for TRIPOLI-4 (STORAGE) and MCNP (PTRAC) are +included as well. Hooks for McStas (mcstas.org) and McXtrace (mcxtrace.org) are +already included directly upstream in those applications. Afew examples of how +to use the Geant4 hooks, or how to interact with MCPL filesfrom standalone C +code, are also provided. Refer to the FILES file for moreinformation about +included files, and refer to the INSTALL file for build instructions. MCPL and most code distributed here was written 2015-2019 by Thomas Kittelmann (thomas.kittelmann@ess.eu). The MCNP-SSW converters were written in close diff --git a/examples/example_mcnpptrac.ptrac b/examples/example_mcnpptrac.ptrac new file mode 100644 index 0000000..d384436 --- /dev/null +++ b/examples/example_mcnpptrac.ptrac @@ -0,0 +1,198 @@ + -1 +mcnp 6.mpi 12/11/14 05/12/20 20:06:32 +obtencion_composicion + 1.4000E+01 1.0000E+00 1.0000E+02 0.0000E+00 1.0000E+00 3.0000E+00 1.0000E+00 1.0000E+00 3.0000E+00 2.1691E+04 + 2.1691E+04 1.8000E+01 1.0000E+00 2.1475E+09 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 + 0.0000E+00 1.0000E+00 2.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00 + 2 6 9 7 9 7 9 7 9 7 9 1 4 0 0 0 0 0 0 0 + 1 2 7 8 9 17 18 19 20 21 22 23 24 25 26 27 28 7 8 10 11 17 18 19 20 21 22 23 24 25 + 26 27 28 7 8 12 13 17 18 19 20 21 22 23 24 25 26 27 28 7 8 10 11 17 18 19 20 21 22 23 + 24 25 26 27 28 7 8 14 15 17 18 19 20 21 22 23 24 25 26 27 28 + 1 3000 + 9000 2 21691 170 71270 21 1 + -0.12348E+03 0.90686E+02 -0.14514E+01 -0.94869E+00 0.28302E+00 -0.14099E+00 0.34737E-07 0.51997E+00 0.24240E+06 + 2 3000 + 3000 2 21691 161 71270 21 1 + -0.11754E+03 0.10540E+03 -0.24885E+02 -0.87918E+00 0.35946E+00 -0.31279E+00 0.57708E-07 0.38014E+00 0.37528E+06 + 9000 3 21691 121 71270 21 1 + -0.11885E+03 0.10216E+03 -0.22465E+02 -0.33612E+00 0.57606E+00 -0.74511E+00 0.82093E-07 0.36873E+00 0.42354E+06 + 3 3000 + 9000 2 21691 150 71270 21 1 + -0.11343E+03 0.11556E+03 0.31001E+01 -0.82332E+00 0.28424E+00 0.49127E+00 0.29757E-07 0.25000E+00 0.79573E+06 + 4 3000 + 9000 2 21691 155 71270 21 1 + -0.12044E+03 0.98214E+02 -0.20935E+02 -0.75657E+00 0.56381E+00 -0.33124E+00 0.12381E-06 0.88491E+00 0.35036E+06 + 5 3000 + 9000 2 21691 114 71270 21 1 + -0.11657E+03 0.10781E+03 0.60224E+01 -0.34424E+00 0.26381E+00 0.90106E+00 0.15222E-06 0.25399E+00 0.47661E+05 + 6 3000 + 9000 2 21691 141 71270 21 1 + -0.11735E+03 0.10586E+03 -0.12434E+02 -0.80712E+00 0.80302E-01 -0.58490E+00 0.71215E-07 0.52520E+00 0.24635E+06 + 7 3000 + 9000 2 21691 166 71270 21 1 + -0.12006E+03 0.99148E+02 -0.18061E+02 -0.86210E+00 0.46188E+00 -0.20846E+00 0.22984E-07 0.32946E+00 0.80789E+06 + 8 3000 + 9000 2 21691 164 71270 21 1 + -0.11975E+03 0.99936E+02 -0.13119E+02 -0.97462E+00 0.16506E+00 -0.15123E+00 0.41669E-07 0.38226E+00 0.25482E+06 + 9 3000 + 9000 2 21691 176 71270 21 1 + -0.11937E+03 0.10088E+03 -0.15290E+01 -0.93718E+00 0.34411E+00 0.57311E-01 0.65241E-08 0.93494E+00 0.19140E+06 + 10 3000 + 9000 2 21691 164 71270 21 1 + -0.11577E+03 0.10979E+03 0.20673E+01 -0.98879E+00 0.12803E+00 0.76858E-01 0.15951E-07 0.26455E+00 0.85921E+06 + 11 3000 + 9000 2 21691 115 71270 21 1 + -0.11732E+03 0.10594E+03 0.56059E-01 -0.57754E+00 -0.28348E+00 -0.76557E+00 0.98991E-08 0.29955E+00 0.31389E+06 + 12 3000 + 9000 2 21691 164 71270 21 1 + -0.11388E+03 0.11445E+03 0.75799E+01 -0.89446E+00 0.35661E+00 -0.26977E+00 0.11418E-06 0.53945E+00 0.81803E+06 + 13 3000 + 9000 2 21691 168 71270 21 1 + -0.11214E+03 0.11876E+03 0.12710E+02 -0.90190E+00 0.38046E+00 0.20452E+00 0.32395E-07 0.37904E+00 0.14859E+07 + 14 3000 + 9000 2 21691 142 71270 21 1 + -0.11694E+03 0.10687E+03 0.15809E+02 -0.52885E+00 0.82240E+00 -0.20969E+00 0.15927E-07 0.41768E+00 0.27475E+06 + 15 3000 + 9000 2 21691 177 71270 21 1 + -0.12633E+03 0.83630E+02 -0.44395E+01 -0.93708E+00 0.34789E+00 -0.29301E-01 0.14463E-07 0.29111E+00 0.44436E+06 + 16 3000 + 9000 2 21691 145 71270 21 1 + -0.11993E+03 0.99492E+02 0.11779E+02 -0.67754E+00 0.51607E+00 0.52403E+00 0.61202E-07 0.33558E+00 0.89747E+06 + 17 3000 + 9000 2 21691 169 71270 21 1 + -0.11974E+03 0.99945E+02 -0.19610E+02 -0.86991E+00 0.47435E+00 -0.13513E+00 0.92173E-07 0.66632E+00 0.14158E+06 + 18 3000 + 9000 2 21691 143 71270 21 1 + -0.11231E+03 0.11835E+03 0.20688E+01 -0.70823E+00 0.40256E+00 0.57996E+00 0.14569E-07 0.27091E+00 0.52859E+06 + 19 3000 + 9000 2 21691 152 71270 21 1 + -0.11529E+03 0.11096E+03 0.46545E+01 -0.97544E+00 -0.42995E-01 0.21604E+00 0.43694E-07 0.67728E+00 0.37985E+06 + 20 3000 + 9000 2 21691 157 71270 21 1 + -0.11811E+03 0.10397E+03 -0.20062E+02 -0.77184E+00 0.55038E+00 0.31836E+00 0.34895E-07 0.29713E+00 0.27768E+06 + 21 3000 + 9000 2 21691 142 71270 21 1 + -0.11377E+03 0.11473E+03 0.40484E+01 -0.96129E+00 -0.26617E+00 -0.71236E-01 0.36446E-07 0.40905E+00 0.48406E+06 + 22 3000 + 9000 2 21691 166 71270 21 1 + -0.11252E+03 0.11782E+03 -0.49316E+01 -0.81635E+00 0.57626E+00 -0.38719E-01 0.14502E-06 0.62620E+00 0.52580E+06 + 23 3000 + 9000 2 21691 147 71270 21 1 + -0.11067E+03 0.12240E+03 -0.14105E+02 -0.82283E+00 0.21342E+00 -0.52669E+00 0.17470E-07 0.43749E+00 0.34162E+06 + 24 3000 + 9000 2 21691 172 71270 21 1 + -0.11630E+03 0.10848E+03 -0.19141E+02 -0.87061E+00 0.49193E+00 0.63322E-02 0.53321E-07 0.98502E+00 0.65400E+05 + 25 3000 + 9000 2 21691 169 71270 21 1 + -0.11575E+03 0.10983E+03 -0.22821E+02 -0.88762E+00 0.42540E+00 -0.17652E+00 0.46081E-08 0.48396E+00 0.46871E+06 + 26 3000 + 3000 2 21691 158 71270 21 1 + -0.12637E+03 0.83548E+02 0.89471E+01 -0.95141E+00 0.12480E+00 0.28150E+00 0.14572E-07 0.92826E+00 0.31616E+06 + 9000 3 21691 137 71270 21 1 + -0.11176E+03 0.11970E+03 -0.10415E+02 -0.44690E+00 0.87663E+00 0.17830E+00 0.30608E-07 0.59597E+00 0.49147E+06 + 27 3000 + 9000 2 21691 156 71270 21 1 + -0.11942E+03 0.10073E+03 0.62028E+01 -0.99968E+00 -0.23716E-01 -0.86454E-02 0.72993E-07 0.25718E+00 0.77335E+06 + 28 3000 + 9000 2 21691 170 71270 21 1 + -0.11508E+03 0.11149E+03 -0.21222E+02 -0.85588E+00 0.51676E+00 -0.20714E-01 0.19261E-07 0.46950E+00 0.32294E+06 + 29 3000 + 9000 2 21691 160 71270 21 1 + -0.11721E+03 0.10621E+03 -0.85916E+01 -0.81575E+00 0.49394E+00 -0.30097E+00 0.11470E-06 0.44192E+00 0.30593E+06 + 30 3000 + 9000 2 21691 151 71270 21 1 + -0.12224E+03 0.93757E+02 -0.52175E+01 -0.98769E+00 -0.95939E-01 0.12358E+00 0.13266E-06 0.30597E+00 0.73883E+06 + 31 3000 + 9000 2 21691 127 71270 21 1 + -0.11192E+03 0.11931E+03 -0.14460E+02 -0.28144E+00 0.94674E+00 0.15642E+00 0.13130E-06 0.46811E+00 0.92786E+06 + 32 3000 + 9000 2 21691 171 71270 21 1 + -0.12434E+03 0.88563E+02 0.73791E+01 -0.96260E+00 0.25460E+00 0.92596E-01 0.45344E-05 0.99612E+00 0.54247E+04 + 33 3000 + 9000 2 21691 135 71270 21 1 + -0.12153E+03 0.95530E+02 0.11000E+02 -0.43233E+00 0.84233E+00 -0.32181E+00 0.38766E-07 0.54822E+00 0.27835E+06 + 34 3000 + 9000 2 21691 138 71270 21 1 + -0.11317E+03 0.11621E+03 0.10852E+02 -0.47451E+00 0.81752E+00 0.32635E+00 0.22323E-06 0.58710E+00 0.29377E+06 + 35 3000 + 9000 2 21691 123 71270 21 1 + -0.12117E+03 0.96401E+02 -0.26783E+02 -0.81774E+00 -0.53641E+00 -0.20873E+00 0.71042E-08 0.34633E+00 0.83840E+06 + 36 3000 + 9000 2 21691 152 71270 21 1 + -0.11430E+03 0.11340E+03 -0.21695E+02 -0.86588E+00 0.23154E+00 0.44345E+00 0.41294E-07 0.53730E+00 0.18645E+06 + 37 3000 + 9000 2 21691 162 71270 21 1 + -0.11206E+03 0.11896E+03 0.63219E+01 -0.78403E+00 0.60087E+00 0.15575E+00 0.36756E-07 0.36815E+00 0.15164E+06 + 38 3000 + 9000 2 21691 153 71270 21 1 + -0.11516E+03 0.11128E+03 -0.10381E+02 -0.86863E+00 0.23379E+00 0.43683E+00 0.68389E-07 0.27610E+00 0.58884E+06 + 39 3000 + 9000 2 21691 121 71270 21 1 + -0.12300E+03 0.91884E+02 -0.16709E+02 -0.80010E+00 -0.59958E+00 -0.18378E-01 0.48685E-07 0.43164E+00 0.48134E+06 + 40 3000 + 3000 2 21691 152 71270 21 1 + -0.12516E+03 0.86533E+02 -0.42578E+01 -0.99367E+00 -0.94571E-01 -0.60647E-01 0.60931E-07 0.85161E+00 0.17815E+06 + 9000 3 21691 159 71270 21 1 + -0.11857E+03 0.10285E+03 -0.20297E+02 -0.77331E+00 0.58429E+00 0.24617E+00 0.93870E-08 0.71042E+00 0.28801E+06 + 41 3000 + 9000 2 21691 167 71270 21 1 + -0.11715E+03 0.10635E+03 0.13638E+02 -0.85924E+00 0.48154E+00 0.17269E+00 0.16189E-07 0.43922E+00 0.18955E+06 + 42 3000 + 9000 2 21691 162 71270 21 1 + -0.11363E+03 0.11508E+03 -0.19236E+02 -0.82207E+00 0.50929E+00 -0.25461E+00 0.43508E-07 0.46824E+00 0.12503E+06 + 43 3000 + 9000 2 21691 170 71270 21 1 + -0.12020E+03 0.98808E+02 -0.28687E+02 -0.90286E+00 0.40106E+00 -0.15492E+00 0.10127E-07 0.51855E+00 0.32344E+06 + 44 3000 + 9000 2 21691 145 71270 21 1 + -0.11746E+03 0.10560E+03 0.10557E+02 -0.91279E+00 -0.71406E-01 0.40214E+00 0.43876E-07 0.36942E+00 0.38540E+06 + 45 3000 + 9000 2 21691 173 71270 21 1 + -0.12063E+03 0.97740E+02 -0.19322E+02 -0.95625E+00 0.28635E+00 -0.59847E-01 0.28313E-07 0.97618E+00 0.28564E+06 + 46 3000 + 9000 2 21691 153 71270 21 1 + -0.11679E+03 0.10725E+03 -0.19892E+02 -0.80037E+00 0.40926E+00 -0.43809E+00 0.48783E-07 0.80038E+00 0.99290E+05 + 47 3000 + 9000 2 21691 176 71270 21 1 + -0.10991E+03 0.12427E+03 -0.15530E+02 -0.90257E+00 0.43021E+00 0.17215E-01 0.43411E-07 0.73863E+00 0.74350E+06 + 48 3000 + 9000 2 21691 150 71270 21 1 + -0.11993E+03 0.99485E+02 -0.16906E+01 -0.63205E+00 0.76141E+00 -0.14409E+00 0.94392E-08 0.42236E+00 0.27178E+06 + 49 3000 + 9000 2 21691 144 71270 21 1 + -0.11369E+03 0.11493E+03 0.51734E+01 -0.67662E+00 0.50228E+00 0.53843E+00 0.18903E-07 0.36742E+00 0.71930E+06 + 50 3000 + 9000 2 21691 158 71270 21 1 + -0.11814E+03 0.10390E+03 -0.53807E+01 -0.78659E+00 0.53073E+00 -0.31559E+00 0.15946E-07 0.32213E+00 0.66971E+06 + 51 3000 + 9000 2 21691 156 71270 21 1 + -0.11652E+03 0.10793E+03 -0.23439E+01 -0.69986E+00 0.70730E+00 -0.99587E-01 0.66723E-07 0.25710E+00 0.36270E+06 + 52 3000 + 9000 2 21691 131 71270 21 1 + -0.10999E+03 0.12407E+03 -0.19913E+01 -0.54759E+00 0.40872E+00 0.73014E+00 0.63839E-07 0.70796E+00 0.37944E+06 + 53 3000 + 9000 2 21691 159 71270 21 1 + -0.10917E+03 0.12611E+03 -0.84799E+01 -0.84543E+00 0.40005E+00 0.35386E+00 0.52725E-07 0.84518E+00 0.47617E+06 + 54 3000 + 9000 2 21691 161 71270 21 1 + -0.11197E+03 0.11918E+03 -0.79643E+01 -0.77259E+00 0.62059E+00 -0.13404E+00 0.40021E-07 0.94156E+00 0.14614E+06 + 55 3000 + 3000 2 21691 167 71270 21 1 + -0.12660E+03 0.82974E+02 -0.14265E+02 -0.98536E+00 0.16987E+00 -0.14712E-01 0.29477E-07 0.27304E+00 0.10101E+07 + 9000 3 21691 143 71270 21 1 + -0.12255E+03 0.93001E+02 -0.18238E+02 -0.66312E+00 0.50787E+00 0.54985E+00 0.50488E-07 0.26621E+00 0.10637E+07 + 56 3000 + 9000 2 21691 141 71270 21 1 + -0.12555E+03 0.85561E+02 0.96095E+01 -0.68413E+00 0.39063E+00 0.61594E+00 0.10117E-06 0.36392E+00 0.28315E+06 + 57 3000 + 9000 2 21691 164 71270 21 1 + -0.11752E+03 0.10545E+03 -0.42142E+01 -0.98870E+00 0.13075E+00 -0.73307E-01 0.16034E-06 0.35868E+00 0.30111E+06 + 58 3000 + 9000 2 21691 171 71270 21 1 + -0.11474E+03 0.11232E+03 -0.24022E+01 -0.86294E+00 0.50297E+00 -0.48457E-01 0.51885E-07 0.97663E+00 0.79193E+05 + 59 3000 + 9000 2 21691 177 71270 21 1 + -0.11679E+03 0.10725E+03 0.23716E+01 -0.91534E+00 0.40090E+00 -0.37706E-01 0.35083E-07 0.36307E+00 0.18341E+06 + 60 3000 + 9000 2 21691 161 71270 21 1 + -0.11334E+03 0.11578E+03 -0.13449E+02 -0.76346E+00 0.64516E+00 -0.29916E-01 0.15206E-07 0.58465E+00 0.25266E+06 diff --git a/examples/example_tripolistock.stock b/examples/example_tripolistock.stock new file mode 100644 index 0000000..cc9fe94 --- /dev/null +++ b/examples/example_tripolistock.stock @@ -0,0 +1,721 @@ +Storage of particles crossing surface(s) from volumes : + 84 to volume 80 + 61 to volume 60 +Their energies are between : 20 and 0 MeV. +Following lines are : type, energy in MeV, position = (x,y,z) in cm, direction = (u,v,w), statistical weight +------------------------------------------------------------------------------------------------------------ + + Data on the original simulation which generates this file : +NB_BATCH 100 +SIZE 10000 +PARTICLE NEUTRON +INTENSITY 1 +SIMULATION SHIELDING + +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 1 + + +END_OF_BATCH 1 WEIGHT_OF_BATCH 500 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 2 + +NEUTRON 1.23325e-06 -55.00000 -14.43743 501.67730 -0.64646 0.58176 -0.4936 0.96022 + +END_OF_BATCH 2 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 3 + +NEUTRON 8.4971e-08 -11.45909 105.00000 318.64324 -0.28418 0.84567 -0.45175 0.84907 +NEUTRON 5.5387e-08 -12.11187 105.00000 318.08000 0.045356 0.90551 0.4219 0.84257 +NEUTRON 8.1427e-08 -12.09689 105.00000 317.71910 0.66918 0.47507 -0.5714 0.87868 +NEUTRON 2.37e-08 -11.25141 105.00000 317.31665 0.77925 0.36744 -0.5077 0.8659 + +END_OF_BATCH 3 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 4 + +NEUTRON 0.00025528 55.00000 76.56831 449.56861 0.91648 0.31725 -0.24378 0.99866 +NEUTRON 4.4067 55.00000 16.28612 348.77137 0.88661 0.14629 -0.43877 0.99208 +NEUTRON 1.5286e-07 55.00000 -41.60350 607.64117 0.91922 -0.39326 -0.019364 0.87013 + +END_OF_BATCH 4 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 5 + + +END_OF_BATCH 5 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 6 + +NEUTRON 8.919e-08 -55.00000 67.78594 411.57261 -0.66137 0.60949 0.43715 0.88798 + +END_OF_BATCH 6 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 7 + +NEUTRON 1.5721e-05 13.35642 105.00000 387.78284 0.295 0.95463 0.040749 0.84402 +NEUTRON 5.6094e-08 16.72135 105.00000 388.79918 -0.60479 0.68553 -0.40533 0.88171 +NEUTRON 5.6303e-08 -55.00000 74.66620 307.88289 -0.78927 0.60732 0.090606 0.81207 + +END_OF_BATCH 7 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 8 + + +END_OF_BATCH 8 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 9 + + +END_OF_BATCH 9 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 10 + +NEUTRON 2.4198 55.00000 -18.26516 375.83051 0.36802 0.91555 0.16228 0.99245 +NEUTRON 1.4125 55.00000 -11.36405 369.11657 0.21264 0.60752 0.76531 0.99078 +NEUTRON 0.16439 55.00000 -11.27105 369.38035 0.71382 -0.69485 -0.087427 0.98375 +NEUTRON 0.0040305 55.00000 -8.88876 364.84561 0.97378 -0.15123 -0.16996 0.9686 + +END_OF_BATCH 10 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 11 + +NEUTRON 8.0942e-05 -55.00000 73.33036 940.98080 -0.80436 -0.58071 0.12558 0.92616 + +END_OF_BATCH 11 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 12 + +NEUTRON 4.909e-08 13.29867 105.00000 469.87571 -0.044112 0.75946 0.64906 0.8585 +NEUTRON 1.0799e-07 12.50856 105.00000 471.15376 0.69821 0.63176 -0.33672 0.97961 + +END_OF_BATCH 12 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 13 + + +END_OF_BATCH 13 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 14 + +NEUTRON 2.9989e-08 31.13165 105.00000 296.72201 0.75491 0.57068 0.32317 0.94277 +NEUTRON 7.4035e-08 31.25647 105.00000 295.58456 0.70866 0.70544 0.012457 1 +NEUTRON 2.9907e-08 36.03705 105.00000 290.49144 0.024992 0.82993 0.55731 0.92819 + +END_OF_BATCH 14 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 15 + + +END_OF_BATCH 15 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 16 + + +END_OF_BATCH 16 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 17 + + +END_OF_BATCH 17 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 18 + +NEUTRON 0.068779 55.00000 -24.68792 693.27297 0.59208 -0.49316 -0.63737 0.99993 +NEUTRON 0.023789 55.00000 -66.93682 352.71067 0.50091 -0.11049 0.85842 0.98004 + +END_OF_BATCH 18 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 19 + + +END_OF_BATCH 19 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 20 + + +END_OF_BATCH 20 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 21 + +NEUTRON 0.060871 -55.00000 -42.59892 390.54713 -0.87116 0.31927 0.37301 0.91218 +NEUTRON 0.052523 -55.00000 -43.82774 390.97519 -0.23708 -0.9639 -0.12119 0.91179 + +END_OF_BATCH 21 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 22 + +NEUTRON 0.01002 22.90763 -105.00000 368.14885 -0.085487 -0.41151 0.90739 0.99891 + +END_OF_BATCH 22 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 23 + + +END_OF_BATCH 23 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 24 + +NEUTRON 7.1619e-08 -55.00000 34.87988 493.52486 -0.61341 0.067641 -0.78686 0.98792 + +END_OF_BATCH 24 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 25 + +NEUTRON 4.358 55.00000 -43.94696 345.58890 0.50332 -0.39727 -0.76737 0.97104 + +END_OF_BATCH 25 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 26 + + +END_OF_BATCH 26 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 27 + +NEUTRON 0.044478 4.24637 105.00000 500.18470 0.16134 0.91859 0.36078 0.97519 + +END_OF_BATCH 27 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 28 + +NEUTRON 1.6699 18.51843 -105.00000 423.20166 0.70134 -0.71244 -0.023627 0.99982 + +END_OF_BATCH 28 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 29 + + +END_OF_BATCH 29 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 30 + + +END_OF_BATCH 30 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 31 + + +END_OF_BATCH 31 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 32 + +NEUTRON 5.7044e-08 -26.13322 -105.00000 597.86866 -0.1728 -0.88555 -0.43121 0.90717 +NEUTRON 6.6349e-08 -26.98922 -105.00000 598.53256 0.065977 -0.79229 -0.60656 0.99225 +NEUTRON 2.2272e-08 -55.00000 53.52003 693.04319 -0.81525 -0.49738 -0.29662 0.9411 +NEUTRON 1.6916e-08 -55.00000 54.64606 694.29553 -0.46846 -0.053287 0.88187 0.96823 + +END_OF_BATCH 32 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 33 + + +END_OF_BATCH 33 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 34 + +NEUTRON 1.7182e-07 -55.00000 -39.47886 552.22599 -0.86653 -0.30866 0.39224 0.82962 +NEUTRON 1.9793 43.82209 -105.00000 394.14250 0.54971 -0.75336 -0.36092 0.9977 + +END_OF_BATCH 34 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 35 + +NEUTRON 7.3733e-08 55.00000 -13.40097 310.93981 0.49289 -0.57221 -0.65547 1 +NEUTRON 1.5257e-08 55.00000 -14.57553 311.15317 0.99917 -0.038554 -0.012816 0.80764 +NEUTRON 1.3012 55.00000 -55.63002 473.55162 0.93348 -0.32705 0.14713 0.9878 + +END_OF_BATCH 35 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 36 + + +END_OF_BATCH 36 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 37 + +NEUTRON 0.41459 -55.00000 -49.09851 417.80784 -0.73316 -0.64798 0.20641 0.99999 + +END_OF_BATCH 37 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 38 + +NEUTRON 0.00026111 -14.38866 -105.00000 338.40887 0.12834 -0.82971 -0.54324 0.99965 + +END_OF_BATCH 38 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 39 + + +END_OF_BATCH 39 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 40 + +NEUTRON 7.605e-05 -55.00000 7.97949 714.16496 -0.77271 -0.19942 0.60261 0.94222 +NEUTRON 9.3636e-08 55.00000 70.43350 446.75605 0.39024 -0.76446 -0.51314 0.88327 + +END_OF_BATCH 40 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 41 + +NEUTRON 5.4029e-07 55.00000 -46.61368 479.25479 0.55736 -0.65108 -0.51521 0.86843 +NEUTRON 3.1946e-08 55.00000 -46.77330 480.15310 0.30676 -0.81536 -0.491 0.83176 + +END_OF_BATCH 41 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 42 + + +END_OF_BATCH 42 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 43 + +NEUTRON 0.3852 -2.94360 105.00000 473.08854 -0.5225 0.78045 -0.34334 0.99975 +NEUTRON 0.026776 -3.79169 105.00000 471.02875 -0.085114 0.30496 -0.94855 0.99975 + +END_OF_BATCH 43 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 44 + +NEUTRON 7.8563e-07 22.79006 105.00000 429.28757 0.24113 0.83204 0.49957 1 + +END_OF_BATCH 44 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 45 + +NEUTRON 3.0587e-06 55.00000 -34.78434 254.28499 0.64056 -0.75249 -0.15311 0.86148 +NEUTRON 1.7036e-08 55.00000 -35.08781 253.96611 0.49467 0.56849 0.65735 0.85616 + +END_OF_BATCH 45 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 46 + + +END_OF_BATCH 46 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 47 + + +END_OF_BATCH 47 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 48 + +NEUTRON 4.4475e-08 23.19963 -105.00000 424.15643 0.1498 -0.95984 0.2372 0.94618 +NEUTRON 1.7902e-08 -55.00000 -23.64423 573.39063 -0.89665 0.22542 -0.38106 1 + +END_OF_BATCH 48 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 49 + +NEUTRON 1.3462e-07 -55.00000 -27.42958 404.54623 -0.80212 0.28646 -0.52396 0.86235 + +END_OF_BATCH 49 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 50 + + +END_OF_BATCH 50 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 51 + + +END_OF_BATCH 51 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 52 + +NEUTRON 1.8985 -55.00000 81.85242 532.79067 -0.35205 -0.51984 -0.77835 0.8164 + +END_OF_BATCH 52 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 53 + + +END_OF_BATCH 53 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 54 + +NEUTRON 2.3598 -55.00000 26.35098 359.72478 -0.78337 -0.31316 0.5369 0.9496 + +END_OF_BATCH 54 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 55 + +NEUTRON 0.62082 -33.21515 105.00000 651.64223 0.2032 0.077917 0.97603 0.8099 + +END_OF_BATCH 55 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 56 + + +END_OF_BATCH 56 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 57 + + +END_OF_BATCH 57 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 58 + + +END_OF_BATCH 58 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 59 + + +END_OF_BATCH 59 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 60 + +NEUTRON 1.8184e-07 -55.00000 14.62962 529.69969 -0.70388 -0.4129 -0.57798 0.97203 +NEUTRON 6.2385e-08 -55.00000 12.99471 528.23456 -0.3052 0.22103 0.92628 0.93917 + +END_OF_BATCH 60 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 61 + + +END_OF_BATCH 61 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 62 + +NEUTRON 2.9392e-06 -55.00000 60.91539 284.89969 -0.9015 -0.15146 -0.4054 0.95498 + +END_OF_BATCH 62 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 63 + +NEUTRON 0.00041277 -31.63509 -105.00000 370.28941 0.39578 -0.79757 -0.45523 0.99648 +NEUTRON 0.00019682 -21.57434 -105.00000 956.20832 0.36566 -0.73853 0.56645 0.99977 + +END_OF_BATCH 63 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 64 + +NEUTRON 1.1538e-07 -55.00000 0.88762 478.69952 -0.43908 0.051516 0.89697 0.9082 +NEUTRON 6.558e-08 -55.00000 0.24601 479.10840 -0.37965 -0.30418 0.87369 0.86674 + +END_OF_BATCH 64 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 65 + +NEUTRON 0.00048899 6.09235 105.00000 444.62234 0.0057582 0.57911 0.81523 0.96852 +NEUTRON 8.8429e-07 5.38716 105.00000 444.60003 -0.48141 0.82796 0.28762 0.96058 +NEUTRON 0.007704 -30.59852 -105.00000 970.29489 0.66554 -0.37216 0.64695 0.96096 +NEUTRON 0.00025161 -30.19307 -105.00000 970.02697 0.69094 -0.63775 -0.34041 0.95933 + +END_OF_BATCH 65 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 66 + +NEUTRON 2.7562 -20.66271 105.00000 406.25238 -0.11347 0.70419 -0.70089 0.94484 +NEUTRON 2.3115 -9.65351 -105.00000 365.86325 0.01938 -0.93026 -0.36638 0.9803 +NEUTRON 2.3868e-06 -55.00000 60.16714 357.11999 -0.63153 0.46231 -0.62245 0.92699 + +END_OF_BATCH 66 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 67 + + +END_OF_BATCH 67 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 68 + +NEUTRON 2.4229 9.82015 105.00000 345.95717 0.07014 0.99354 -0.089261 0.94336 +NEUTRON 0.0017721 55.00000 18.86155 427.75198 0.76399 0.55515 -0.32883 0.94797 +NEUTRON 8.1131e-05 55.00000 17.99653 425.08391 0.58186 -0.70957 0.39743 0.99952 +NEUTRON 7.3387e-06 55.00000 17.15526 427.17123 0.84437 -0.32905 0.42281 0.98664 + +END_OF_BATCH 68 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 69 + +NEUTRON 1.2971 55.00000 7.52242 283.57507 0.91389 0.36757 -0.17236 0.80674 +NEUTRON 2.0134 -55.00000 91.99073 406.67067 -0.89096 0.43526 -0.12937 0.98472 + +END_OF_BATCH 69 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 70 + + +END_OF_BATCH 70 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 71 + +NEUTRON 3.139 54.57716 105.00000 392.20906 0.60799 0.75847 -0.23469 0.9595 +NEUTRON 6.9071e-05 55.00000 102.39133 389.12302 0.93815 0.051566 0.34238 0.93618 +NEUTRON 0.012579 55.00000 -43.20274 508.52578 0.76333 -0.62153 -0.17613 0.99995 +NEUTRON 3.1712e-06 36.73755 105.00000 333.58140 0.55821 0.82451 0.092621 0.99609 + +END_OF_BATCH 71 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 72 + +NEUTRON 5.3691e-07 55.00000 70.47284 494.35764 0.96686 -0.25524 -0.0050505 0.98041 + +END_OF_BATCH 72 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 73 + + +END_OF_BATCH 73 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 74 + +NEUTRON 3.919e-08 55.00000 -12.13231 404.01903 0.47204 -0.20389 0.85767 0.85043 + +END_OF_BATCH 74 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 75 + + +END_OF_BATCH 75 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 76 + +NEUTRON 5.8059 -5.70315 -105.00000 437.37691 -0.047225 -0.97183 0.2309 0.84937 + +END_OF_BATCH 76 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 77 + + +END_OF_BATCH 77 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 78 + +NEUTRON 1.4305e-08 55.00000 92.96323 365.40424 0.90664 -0.40198 0.12816 1 +NEUTRON 2.2978e-08 55.00000 5.18870 1863.36566 0.66778 0.063178 -0.74168 0.88885 + +END_OF_BATCH 78 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 79 + + +END_OF_BATCH 79 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 80 + + +END_OF_BATCH 80 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 81 + +NEUTRON 1.1097e-06 -23.86990 -105.00000 427.45460 -0.33377 -0.72964 0.59684 0.94742 + +END_OF_BATCH 81 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 82 + +NEUTRON 2.4859 -55.00000 -2.86799 430.92283 -0.36131 0.70238 0.61328 0.91835 +NEUTRON 1.2075e-05 -55.00000 32.54597 294.76316 -0.90566 -0.10296 -0.41131 0.96992 + +END_OF_BATCH 82 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 83 + + +END_OF_BATCH 83 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 84 + + +END_OF_BATCH 84 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 85 + +NEUTRON 0.00088881 55.00000 -18.12424 300.66390 0.51719 -0.523 -0.67749 0.85328 +NEUTRON 4.7531e-07 55.00000 -16.90128 298.55719 0.87076 0.23314 -0.43293 0.84543 +NEUTRON 2.01e-07 -55.00000 86.96141 585.06725 -0.92847 0.29541 -0.2251 0.95177 + +END_OF_BATCH 85 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 86 + + +END_OF_BATCH 86 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 87 + + +END_OF_BATCH 87 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 88 + +NEUTRON 1.5884e-07 -55.00000 -65.72242 382.68998 -0.99949 -0.021733 -0.023205 0.92834 + +END_OF_BATCH 88 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 89 + + +END_OF_BATCH 89 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 90 + + +END_OF_BATCH 90 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 91 + +NEUTRON 6.0632e-09 55.00000 -30.25243 503.61213 0.44047 0.6613 -0.60718 0.82487 +NEUTRON 1.5083e-08 55.00000 -29.59654 503.72351 0.36916 0.84951 0.3769 1 + +END_OF_BATCH 91 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 92 + +NEUTRON 2.3392 -2.41385 105.00000 443.11163 0.018288 0.99626 0.08444 0.89059 + +END_OF_BATCH 92 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 93 + +NEUTRON 2.9099e-08 17.45005 105.00000 1408.60170 0.34338 0.93284 -0.10913 0.94786 + +END_OF_BATCH 93 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 94 + + +END_OF_BATCH 94 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 95 + +NEUTRON 0.19274 55.00000 26.11035 427.61219 0.98313 0.13959 0.1182 0.99899 +NEUTRON 1.4878e-06 55.00000 29.69936 427.01197 0.68209 0.43637 0.5868 0.98375 + +END_OF_BATCH 95 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 96 + +NEUTRON 0.0032227 -21.04811 -105.00000 383.23478 0.39885 -0.812 -0.42612 0.99117 + +END_OF_BATCH 96 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 97 + +NEUTRON 1.0283e-05 55.00000 -38.15242 529.57107 0.96343 -0.2662 -0.030753 0.99338 +NEUTRON 2.4452 55.00000 -4.82934 443.52130 0.53069 0.44747 0.71982 0.96741 +NEUTRON 0.54956 55.00000 -4.59407 444.90705 0.83343 -0.070063 -0.54816 0.96693 +NEUTRON 0.01867 55.00000 -5.14571 442.84029 0.37636 -0.69535 0.61225 0.9576 + +END_OF_BATCH 97 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 98 + + +END_OF_BATCH 98 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 99 + +NEUTRON 1.5138 -55.00000 -35.49552 356.35884 -0.14157 -0.78877 -0.59816 0.99631 +NEUTRON 0.00023975 -55.00000 16.51489 418.16375 -0.79573 -0.25631 0.54875 0.93797 + +END_OF_BATCH 99 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ +------------------------------------------------------------------------------------------------------------ +BEGIN_OF_BATCH 100 + +NEUTRON 3.2831e-05 -6.07668 -105.00000 289.69255 -0.2356 -0.77851 -0.58174 0.88141 + +END_OF_BATCH 100 WEIGHT_OF_BATCH 10000 +------------------------------------------------------------------------------------------------------------ diff --git a/src/mcnpptrac/ptrac2mcpl_app.c b/src/mcnpptrac/ptrac2mcpl_app.c new file mode 100644 index 0000000..ee3a8f0 --- /dev/null +++ b/src/mcnpptrac/ptrac2mcpl_app.c @@ -0,0 +1,14 @@ +#include "ptracmcpl.h" + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ptrac2mcpl : a simple command line utility for converting PTRAC files from // +// MCNP(X) to MCPL. // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +int main(int argc,char** argv) { + return ptrac2mcpl_app(argc,argv); +} diff --git a/src/mcnpptrac/ptracmcpl.c b/src/mcnpptrac/ptracmcpl.c new file mode 100644 index 0000000..cab4f7d --- /dev/null +++ b/src/mcnpptrac/ptracmcpl.c @@ -0,0 +1,183 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ptracmcpl : Code for converting between MCPL and PTRAC files from MCNP(X). // +// // +// // +// Compilation of ptracmcpl.c can proceed via any compliant C-compiler using // +// -std=c99 later. Furthermore, the following preprocessor flag can be used // +// when compiling ptracmcpl.c to fine tune the build process. // +// // +// SSWMCPL_HDR_INCPATH : Specify alternative value if the ptracmcpl header // +// itself is not to be included as "ptracmcpl.h". // +// SSWREAD_HDR_INCPATH : Specify alternative value if the ptracread header // +// is not to be included as "ptracread.h". // +// MCPL_HEADER_INCPATH : Specify alternative value if the MCPL header is // +// not to be included as "mcpl.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSWMCPL_HDR_INCPATH +# include SSWMCPL_HDR_INCPATH +#else +# include "ptracmcpl.h" +#endif + +#ifdef SSWREAD_HDR_INCPATH +# include SSWREAD_HDR_INCPATH +#else +# include "ptracread.h" +#endif + +#ifdef MCPL_HEADER_INCPATH +# include MCPL_HEADER_INCPATH +#else +# include "mcpl.h" +#endif + +#include +#include +#include +#include +#include + +//fwd declare internal functions from ptracread.c +void ptrac_error(const char * msg); +long ptrac_get_pdgcode(ptrac_file_t * f); + +int ptrac2mcpl(const char * ptracfile, const char * mcplfile) +{ + return ptrac2mcpl2(ptracfile, mcplfile, 0, 1); +} + +int ptrac2mcpl2(const char * ptracfile, const char * mcplfile, + int opt_dp, int opt_gzip) +{ + ptrac_file_t f = ptrac_open_file(ptracfile); + mcpl_outfile_t mcplfh = mcpl_create_outfile(mcplfile); + + mcpl_hdr_set_srcname(mcplfh,"MCNP (PTRAC)"); + + // Copiar info de header de ptrac a archivo mcpl + mcpl_enable_universal_pdgcode(mcplfh, ptrac_get_pdgcode(&f)); + + if (opt_dp) { + mcpl_enable_doubleprec(mcplfh); + } + + mcpl_particle_t mcpl_particle; + memset(&mcpl_particle,0,sizeof(mcpl_particle)); + + const ptrac_particle_t * p; + while ((p=ptrac_load_particle(f))) { + + mcpl_particle.position[0] = p->x;//already in cm + mcpl_particle.position[1] = p->y;//already in cm + mcpl_particle.position[2] = p->z;//already in cm + mcpl_particle.direction[0] = p->dirx; + mcpl_particle.direction[1] = p->diry; + mcpl_particle.direction[2] = p->dirz; + mcpl_particle.time = p->time * 1.0e-5;//"shakes" to milliseconds + mcpl_particle.weight = p->weight; + mcpl_particle.ekin = p->ekin;//already in MeV + + mcpl_add_particle(mcplfh,&mcpl_particle); + + } + + const char * tmp = mcpl_outfile_filename(mcplfh); + size_t laf = strlen(tmp); + char * actual_filename = malloc(laf+1); + actual_filename[0]='\0'; + strcat(actual_filename,tmp); + + int did_gzip = 0; + if (opt_gzip) + did_gzip = mcpl_closeandgzip_outfile(mcplfh); + else + mcpl_close_outfile(mcplfh); + ptrac_close_file(f); + + printf("Created %s%s\n",actual_filename,(did_gzip?".gz":"")); + free(actual_filename); + return 1; +} + +void ptrac2mcpl_parse_args(int argc,char **argv, const char** infile, + const char **outfile, const char **cfgfile, + int* double_prec, int* do_gzip) { + *cfgfile = 0; + *infile = 0; + *outfile = 0; + *double_prec = 0; + *do_gzip = 1; + int i; + for (i=1; i < argc; ++i) { + if (argv[i][0]=='\0') + continue; + if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { + const char * progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + printf("Usage:\n\n"); + printf(" %s [options] input.ptrac [output.mcpl]\n\n",progname); + printf("Converts the Monte Carlo particles in the input.ptrac file (MCNP Particle\n" + "Track format) to MCPL format and stores in the designated output file\n" + "(defaults to \"output.mcpl\").\n" + "\n" + "Options:\n" + "\n" + " -h, --help : Show this usage information.\n" + " -d, --double : Enable double-precision storage of floating point values.\n" + " -n, --nogzip : Do not attempt to gzip output file.\n" + ); + exit(0); + } + + if (strcmp(argv[i],"-d")==0||strcmp(argv[i],"--double")==0) { + *double_prec = 1; + continue; + } + if (strcmp(argv[i],"-n")==0||strcmp(argv[i],"--nogzip")==0) { + *do_gzip = 0; + continue; + } + if (argv[i][0]=='-') { + printf("Error: Unknown argument: %s\n",argv[i]); + exit(1); + } + if (!*infile) { + *infile = argv[i]; + continue; + } + if (!*outfile) { + *outfile = argv[i]; + continue; + } + printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*infile) { + printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*outfile) + *outfile = "output.mcpl"; + if (strcmp(*infile,*outfile)==0) { + //basic test, easy to cheat: + printf("Error: input and output files are identical.\n"); + exit(1); + } +} + +int ptrac2mcpl_app(int argc,char** argv) +{ + const char * infile; + const char * outfile; + const char * cfgfile; + int double_prec, do_gzip; + ptrac2mcpl_parse_args(argc,argv,&infile,&outfile,&cfgfile,&double_prec,&do_gzip); + int ok = ptrac2mcpl2(infile, outfile, double_prec, do_gzip); + return ok ? 0 : 1; +} diff --git a/src/mcnpptrac/ptracmcpl.h b/src/mcnpptrac/ptracmcpl.h new file mode 100644 index 0000000..6b9b8c8 --- /dev/null +++ b/src/mcnpptrac/ptracmcpl.h @@ -0,0 +1,34 @@ +#ifndef ptracmcpl_h +#define ptracmcpl_h + +////////////////////////////////////////////////////////////////////////////////////// +// // +// Functions for converting PTRAC files from MCNP(X) to MCPL files. // +// // +// Written 2021, osiris.abbate@ib.edu.ar, (Instituto Balseiro). // +// // +////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// Create mcplfile based on content in ptracfile. Using this function will not +// enable double-precision or user-flags in the output file. Returns 1 on success, +// 0 on failure: +int ptrac2mcpl(const char * ptracfile, const char * mcplfile); + +////////////////////////////////////////////////////////////////////////////////////// +// Advanced version of the above with more options: +// +// opt_dp : Set to 1 to enable double-precision storage of floating point +// values. Set to 0 for single-precision. +// opt_gzip: Set to 1 to gzip the resulting mcpl file. Set to 0 to leave the +// resulting file uncompressed. +// +int ptrac2mcpl2(const char * ptracfile, const char * mcplfile, + int opt_dp, int opt_gzip); + +////////////////////////////////////////////////////////////////////////////////////// +// For easily creating standard ptrac2mcpl cmdline applications: +int ptrac2mcpl_app(int argc,char** argv); + +#endif diff --git a/src/mcnpptrac/ptracread.c b/src/mcnpptrac/ptracread.c new file mode 100644 index 0000000..b53fa7b --- /dev/null +++ b/src/mcnpptrac/ptracread.c @@ -0,0 +1,186 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ptracread : Code for reading PTRAC files from MCNP(X) // +// // +// // +// Compilation of ptracread.c can proceed via any compliant C-compiler using // +// -std=c99 or later, and the resulting code must always be linked with libm // +// (using -lm). Furthermore, the following preprocessor flags can be used // +// when compiling ptracread.c to fine tune the build process and the // +// capabilities of the resulting binary. // +// // +// SSWREAD_HDR_INCPATH : Specify alternative value if the ptracread header itself // +// is not to be included as "ptracread.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSWREAD_HDR_INCPATH +# include SSWREAD_HDR_INCPATH +#else +# include "ptracread.h" +#endif + +#include +#include +#include +#include +#include +#include + + +//Should be large enough to hold first record in all supported files: +#define PTRACREAD_MAXLINESIZE 1024 + +void ptrac_error(const char * msg) { + printf("ERROR: %s\n",msg); + exit(1); +} + +typedef struct { + FILE * file; + ptrac_particle_t part; + char line[PTRACREAD_MAXLINESIZE];//for holding line from file + + int rawtype;//global raw particle type encoding + long pdgcode;//global rawtype converted to PDG codes. +} ptrac_fileinternal_t; + +long ptrac_get_pdgcode(ptrac_file_t * ff){ + ptrac_fileinternal_t * f = (ptrac_fileinternal_t *)ff->internal; + assert(f); + + return f->pdgcode; +} + +ptrac_file_t ptrac_open_internal( const char * filename ) +{ + ptrac_fileinternal_t * f = (ptrac_fileinternal_t*)calloc(sizeof(ptrac_fileinternal_t),1); + assert(f); + + ptrac_file_t out; + out.internal = f; + + f->file = fopen(filename,"rb"); + if (!f->file) + ptrac_error("Unable to open file!"); + + // Procesar header + if(!fgets(f->line, PTRACREAD_MAXLINESIZE, f->file)) // -1 + ptrac_error("Unexpected format in PTRAC file"); + int minus_one; + sscanf(f->line, "%d", &minus_one); + if(minus_one != -1) + ptrac_error("Unexpected format in PTRAC file"); + if(!fgets(f->line, PTRACREAD_MAXLINESIZE, f->file)) // KOD, VER, LODDAT, IDTM + ptrac_error("Unexpected format in PTRAC file"); + if(!fgets(f->line, PTRACREAD_MAXLINESIZE, f->file)) // AID + ptrac_error("Unexpected format in PTRAC file"); + double m, n, V; // Process input of PTRAC card + int i, j, ret; + ret=fscanf(f->file, "%lf", &m); // Number of keywords + for(i=0; ifile, "%lf", &n); + for(j=0; jfile, "%lf", &V); + if( i == 10 ){ // TYPE keyword + if((int)n!=1 || (int)V < 1) + ptrac_error("TYPE keyword must be set to only one particle"); + f->rawtype = (int)V; + f->pdgcode = conv_mcnp2pdg(f->rawtype); + } + if( i == 12 ) // WRITE keyword + if((int)n!=1 || (int)V != 2) + ptrac_error("WRITE keyword must be ALL"); + } + if(!fgets(f->line, PTRACREAD_MAXLINESIZE, f->file)) // End input line + ptrac_error("Unexpected format in PTRAC file"); + + const char * bn = strrchr(filename, '/'); + bn = bn ? bn + 1 : filename; + printf("ptrac_open_file: Opened file \"%s\":\n",bn); + + return out; +} + +ptrac_file_t ptrac_open_file( const char * filename ) +{ + if (!filename) + ptrac_error("ptrac_open_file called with null string for filename"); + + //Open file and initialize internal: + ptrac_file_t out = ptrac_open_internal( filename ); + ptrac_fileinternal_t * f = (ptrac_fileinternal_t *)out.internal; assert(f); + + //Return handle: + out.internal = f; + return out; +} + +int ptrac_read(const char* line, ptrac_particle_t* part){ + double aux; + ptrac_particle_t buf; + int nreaded = sscanf(line, "%le %le %le %le %le %le %le %le %le %le", + &buf.x, &buf.y, &buf.z, &buf.dirx, &buf.diry, &buf.dirz, &buf.ekin, &buf.weight, &buf.time, &aux); + if (nreaded == 9){ + *part = buf; + return 1; + } + return 0; +} + +const ptrac_particle_t * ptrac_load_particle(ptrac_file_t ff){ + ptrac_fileinternal_t * f = (ptrac_fileinternal_t *)ff.internal; + assert(f); + + double ndir, ndir2; + while(fgets(f->line, PTRACREAD_MAXLINESIZE, f->file)){ + if(ptrac_read(f->line, &f->part)){ + ndir2 = f->part.dirx*f->part.dirx + f->part.diry*f->part.diry + f->part.dirz*f->part.dirz; + if(ndir2 != 1){ // Normalization may be inexact in this format + ndir = sqrt(ndir2); + f->part.dirx /= ndir; + f->part.diry /= ndir; + f->part.dirz /= ndir; + } + return &f->part; + } + } + return 0; +} + +void ptrac_close_file(ptrac_file_t ff){ + ptrac_fileinternal_t * f = (ptrac_fileinternal_t *)ff.internal; + assert(f); + + if (!f) + return; + if (f->file) { + fclose(f->file); + f->file = 0; + } + free(f); + ff.internal = 0; +} + +// Revisar mcnp code + +static int mcnp_codes[] = {0, 2112, 22, 11, 13, -2112, 12, 14, -11, 2212};//... + +int32_t conv_mcnp2pdg(int32_t c) +{ + if( c<0 || c>9 ) + return 0; + return mcnp_codes[c]; +} + +int32_t conv_pdg2mcnp(int32_t c) +{ + int i; + for(i=0; i + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct { + void * internal; + } ptrac_file_t; + + typedef struct { + double x;//cm + double y;//cm + double z;//cm + double dirx; + double diry; + double dirz; + double ekin;//MeV + double weight; + double time;//"shakes" (1e-8seconds) + } ptrac_particle_t; + + //Open file: + ptrac_file_t ptrac_open_file(const char * filename); + + //Query header info: + + //load next particle (null indicates eof): + const ptrac_particle_t * ptrac_load_particle(ptrac_file_t); + + //close file and release resources: + void ptrac_close_file(ptrac_file_t); + + //Advanced info about file layout: + + //////////////////////////////////////////////////////////////////////////// + // // + // Utility functions for converting between particle codes used in ptrac // + // files from TRIPOLI-4 and the codes from the Particle Data Group: // + // // + // http://pdg.lbl.gov/2014/reviews/rpp2014-rev-monte-carlo-numbering.pdf // + // // + // Note that all the functions here return 0 when the code could not be // + // converted. This might not be an error as such, but could indicate an // + // exotic particle which has no code assigned in the target TRIPOLI-4 // + // scheme. // + // // + // TRIPOLI-4 only supports neutrons (1<->2112), gammas (2<->22), // + // electrons (3<->?) and positrons (4<->?). // + // // + //////////////////////////////////////////////////////////////////////////// + + int32_t conv_mcnp2pdg(int32_t); + int32_t conv_pdg2mcnp(int32_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/tripolistock/stock2mcpl_app.c b/src/tripolistock/stock2mcpl_app.c new file mode 100644 index 0000000..e6ec037 --- /dev/null +++ b/src/tripolistock/stock2mcpl_app.c @@ -0,0 +1,14 @@ +#include "stockmcpl.h" + +///////////////////////////////////////////////////////////////////////////////////// +// // +// stock2mcpl : a simple command line utility for converting stock files from // +// TRIPOLI-4 (STORAGE) to MCPL. // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +int main(int argc,char** argv) { + return stock2mcpl_app(argc,argv); +} diff --git a/src/tripolistock/stockmcpl.c b/src/tripolistock/stockmcpl.c new file mode 100644 index 0000000..812656c --- /dev/null +++ b/src/tripolistock/stockmcpl.c @@ -0,0 +1,186 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// stockmcpl : Code for converting between MCPL and stock files from TRIPOLI-4 // +// (STORAGE). // +// // +// // +// Compilation of stockmcpl.c can proceed via any compliant C-compiler using // +// -std=c99 later. Furthermore, the following preprocessor flag can be used // +// when compiling stockmcpl.c to fine tune the build process. // +// // +// SSWMCPL_HDR_INCPATH : Specify alternative value if the stockmcpl header // +// itself is not to be included as "stockmcpl.h". // +// SSWREAD_HDR_INCPATH : Specify alternative value if the stockread header // +// is not to be included as "stockread.h". // +// MCPL_HEADER_INCPATH : Specify alternative value if the MCPL header is // +// not to be included as "mcpl.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSWMCPL_HDR_INCPATH +# include SSWMCPL_HDR_INCPATH +#else +# include "stockmcpl.h" +#endif + +#ifdef SSWREAD_HDR_INCPATH +# include SSWREAD_HDR_INCPATH +#else +# include "stockread.h" +#endif + +#ifdef MCPL_HEADER_INCPATH +# include MCPL_HEADER_INCPATH +#else +# include "mcpl.h" +#endif + +#include +#include +#include +#include +#include + +void stock_error(const char * msg);//fwd declare internal function from stockread.c + +int stock2mcpl(const char * stockfile, const char * mcplfile) +{ + return stock2mcpl2(stockfile, mcplfile, 0, 1); +} + +int stock2mcpl2(const char * stockfile, const char * mcplfile, + int opt_dp, int opt_gzip) +{ + stock_file_t f = stock_open_file(stockfile); + mcpl_outfile_t mcplfh = mcpl_create_outfile(mcplfile); + + mcpl_hdr_set_srcname(mcplfh,"TRIPOLI-4 (STORAGE)"); + + // Aqui se deberia copiar info de header de stock a archivo mcpl + + if (opt_dp) { + mcpl_enable_doubleprec(mcplfh); + } + + mcpl_particle_t mcpl_particle; + memset(&mcpl_particle,0,sizeof(mcpl_particle)); + + const stock_particle_t * p; + while ((p=stock_load_particle(f))) { + mcpl_particle.pdgcode = p->pdgcode; + if (!mcpl_particle.pdgcode) { + printf("Warning: ignored particle with no PDG code set (raw stock type was %i).\n",p->rawtype); + continue; + } + + mcpl_particle.position[0] = p->x;//already in cm + mcpl_particle.position[1] = p->y;//already in cm + mcpl_particle.position[2] = p->z;//already in cm + mcpl_particle.direction[0] = p->dirx; + mcpl_particle.direction[1] = p->diry; + mcpl_particle.direction[2] = p->dirz; + mcpl_particle.time = 0;//particles in stock do not have time + mcpl_particle.weight = p->weight; + mcpl_particle.ekin = p->ekin;//already in MeV + + mcpl_add_particle(mcplfh,&mcpl_particle); + + } + + const char * tmp = mcpl_outfile_filename(mcplfh); + size_t laf = strlen(tmp); + char * actual_filename = malloc(laf+1); + actual_filename[0]='\0'; + strcat(actual_filename,tmp); + + int did_gzip = 0; + if (opt_gzip) + did_gzip = mcpl_closeandgzip_outfile(mcplfh); + else + mcpl_close_outfile(mcplfh); + stock_close_file(f); + + printf("Created %s%s\n",actual_filename,(did_gzip?".gz":"")); + free(actual_filename); + return 1; +} + +void stock2mcpl_parse_args(int argc,char **argv, const char** infile, + const char **outfile, const char **cfgfile, + int* double_prec, int* do_gzip) { + *cfgfile = 0; + *infile = 0; + *outfile = 0; + *double_prec = 0; + *do_gzip = 1; + int i; + for (i=1; i < argc; ++i) { + if (argv[i][0]=='\0') + continue; + if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { + const char * progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + printf("Usage:\n\n"); + printf(" %s [options] input.stock [output.mcpl]\n\n",progname); + printf("Converts the Monte Carlo particles in the input.stock file (MCNP Surface\n" + "Source Write format) to MCPL format and stores in the designated output\n" + "file (defaults to \"output.mcpl\").\n" + "\n" + "Options:\n" + "\n" + " -h, --help : Show this usage information.\n" + " -d, --double : Enable double-precision storage of floating point values.\n" + " -n, --nogzip : Do not attempt to gzip output file.\n" + ); + exit(0); + } + + if (strcmp(argv[i],"-d")==0||strcmp(argv[i],"--double")==0) { + *double_prec = 1; + continue; + } + if (strcmp(argv[i],"-n")==0||strcmp(argv[i],"--nogzip")==0) { + *do_gzip = 0; + continue; + } + if (argv[i][0]=='-') { + printf("Error: Unknown argument: %s\n",argv[i]); + exit(1); + } + if (!*infile) { + *infile = argv[i]; + continue; + } + if (!*outfile) { + *outfile = argv[i]; + continue; + } + printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*infile) { + printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*outfile) + *outfile = "output.mcpl"; + if (strcmp(*infile,*outfile)==0) { + //basic test, easy to cheat: + printf("Error: input and output files are identical.\n"); + exit(1); + } +} + +int stock2mcpl_app(int argc,char** argv) +{ + const char * infile; + const char * outfile; + const char * cfgfile; + int double_prec, do_gzip; + stock2mcpl_parse_args(argc,argv,&infile,&outfile,&cfgfile,&double_prec,&do_gzip); + int ok = stock2mcpl2(infile, outfile, double_prec, do_gzip); + return ok ? 0 : 1; +} diff --git a/src/tripolistock/stockmcpl.h b/src/tripolistock/stockmcpl.h new file mode 100644 index 0000000..ebe3be5 --- /dev/null +++ b/src/tripolistock/stockmcpl.h @@ -0,0 +1,34 @@ +#ifndef stockmcpl_h +#define stockmcpl_h + +////////////////////////////////////////////////////////////////////////////////////// +// // +// Functions for converting stock files from TRIPOLI-4 (STORAGE) to MCPL files. // +// // +// Written 2021, osiris.abbate@ib.edu.ar, (Instituto Balseiro). // +// // +////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// Create mcplfile based on content in stockfile. Using this function will not +// enable double-precision or user-flags in the output file. Returns 1 on success, +// 0 on failure: +int stock2mcpl(const char * stockfile, const char * mcplfile); + +////////////////////////////////////////////////////////////////////////////////////// +// Advanced version of the above with more options: +// +// opt_dp : Set to 1 to enable double-precision storage of floating point +// values. Set to 0 for single-precision. +// opt_gzip: Set to 1 to gzip the resulting mcpl file. Set to 0 to leave the +// resulting file uncompressed. +// +int stock2mcpl2(const char * stockfile, const char * mcplfile, + int opt_dp, int opt_gzip); + +////////////////////////////////////////////////////////////////////////////////////// +// For easily creating standard stock2mcpl cmdline applications: +int stock2mcpl_app(int argc,char** argv); + +#endif diff --git a/src/tripolistock/stockread.c b/src/tripolistock/stockread.c new file mode 100644 index 0000000..2856a0c --- /dev/null +++ b/src/tripolistock/stockread.c @@ -0,0 +1,155 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// stockread : Code for reading stock files from TRIPOLI-4 (STORAGE). // +// // +// // +// Compilation of stockread.c can proceed via any compliant C-compiler using // +// -std=c99 or later, and the resulting code must always be linked with libm // +// (using -lm). Furthermore, the following preprocessor flags can be used // +// when compiling stockread.c to fine tune the build process and the // +// capabilities of the resulting binary. // +// // +// SSWREAD_HDR_INCPATH : Specify alternative value if the stockread header itself // +// is not to be included as "stockread.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSWREAD_HDR_INCPATH +# include SSWREAD_HDR_INCPATH +#else +# include "stockread.h" +#endif + +#include +#include +#include +#include +#include +#include + + +//Should be large enough to hold first record in all supported files: +#define STOCKREAD_MAXLINESIZE 1024 + +void stock_error(const char * msg) { + printf("ERROR: %s\n",msg); + exit(1); +} + +typedef struct { + FILE * file; + stock_particle_t part; + char line[STOCKREAD_MAXLINESIZE];//for holding line from file +} stock_fileinternal_t; + +stock_file_t stock_open_internal( const char * filename ) +{ + stock_fileinternal_t * f = (stock_fileinternal_t*)calloc(sizeof(stock_fileinternal_t),1); + assert(f); + + stock_file_t out; + out.internal = f; + + f->file = fopen(filename,"rb"); + if (!f->file) + stock_error("Unable to open file!"); + + // Aqui deberia procesar header + if(!fgets(f->line, STOCKREAD_MAXLINESIZE, f->file)) + stock_error("Unexpected format in stock file"); + if(strncmp(f->line, "Storage", 7)) + stock_error("Unexpected format in stock file"); + + const char * bn = strrchr(filename, '/'); + bn = bn ? bn + 1 : filename; + printf("stock_open_file: Opened file \"%s\":\n",bn); + + return out; +} + +stock_file_t stock_open_file( const char * filename ) +{ + if (!filename) + stock_error("stock_open_file called with null string for filename"); + + //Open file and initialize internal: + stock_file_t out = stock_open_internal( filename ); + stock_fileinternal_t * f = (stock_fileinternal_t *)out.internal; assert(f); + + //Return handle: + out.internal = f; + return out; +} + +int stock_read(const char* line, stock_particle_t* part){ + if(strncmp(line,"NEUTRON",7)==0){ + part->rawtype = 1; + part->pdgcode = conv_tripoli2pdg(part->rawtype); + sscanf(line,"NEUTRON %lf %lf %lf %lf %lf %lf %lf %lf", + &part->ekin, &part->x, &part->y, &part->z, &part->dirx, &part->diry, &part->dirz, &part->weight); + return 1; + } + if(strncmp(line,"PHOTON",6)==0){ + part->rawtype = 2; + part->pdgcode = conv_tripoli2pdg(part->rawtype); + sscanf(line,"PHOTON %lf %lf %lf %lf %lf %lf %lf %lf", + &part->ekin, &part->x, &part->y, &part->z, &part->dirx, &part->diry, &part->dirz, &part->weight); + return 1; + } + return 0; +} + +const stock_particle_t * stock_load_particle(stock_file_t ff){ + stock_fileinternal_t * f = (stock_fileinternal_t *)ff.internal; + assert(f); + + double ndir, ndir2; + while(fgets(f->line, STOCKREAD_MAXLINESIZE, f->file)){ + if(stock_read(f->line, &f->part)){ + ndir2 = f->part.dirx*f->part.dirx + f->part.diry*f->part.diry + f->part.dirz*f->part.dirz; + if(ndir2 != 1){ // Normalization may be inexact in this format + ndir = sqrt(ndir2); + f->part.dirx /= ndir; + f->part.diry /= ndir; + f->part.dirz /= ndir; + } + return &f->part; + } + } + return 0; +} + +void stock_close_file(stock_file_t ff){ + stock_fileinternal_t * f = (stock_fileinternal_t *)ff.internal; + assert(f); + + if (!f) + return; + if (f->file) { + fclose(f->file); + f->file = 0; + } + free(f); + ff.internal = 0; +} + +static int tripoli_codes[] = {0, 2112, 22, 11, -11}; + +int32_t conv_tripoli2pdg(int32_t c) +{ + if( c<0 || c>4 ) + return 0; + return tripoli_codes[c]; +} + +int32_t conv_pdg2tripoli(int32_t c) +{ + int i; + for(i=0; i + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct { + void * internal; + } stock_file_t; + + typedef struct { + double ekin;//MeV + double x;//cm + double y;//cm + double z;//cm + double dirx; + double diry; + double dirz; + double weight; + int rawtype;//raw particle type encoding (tripoli 'ipt': 1=n, 2=p, 3=e-, 4=e+) + long pdgcode;//rawtype converted to PDG codes. + } stock_particle_t; + + //Open file: + stock_file_t stock_open_file(const char * filename); + + //Query header info: + + //load next particle (null indicates eof): + const stock_particle_t * stock_load_particle(stock_file_t); + + //close file and release resources: + void stock_close_file(stock_file_t); + + //Advanced info about file layout: + + //////////////////////////////////////////////////////////////////////////// + // // + // Utility functions for converting between particle codes used in stock // + // files from TRIPOLI-4 and the codes from the Particle Data Group: // + // // + // http://pdg.lbl.gov/2014/reviews/rpp2014-rev-monte-carlo-numbering.pdf // + // // + // Note that all the functions here return 0 when the code could not be // + // converted. This might not be an error as such, but could indicate an // + // exotic particle which has no code assigned in the target TRIPOLI-4 // + // scheme. // + // // + // TRIPOLI-4 only supports neutrons (1<->2112), gammas (2<->22), // + // electrons (3<->11) and positrons (4<->-11). // + // // + //////////////////////////////////////////////////////////////////////////// + + int32_t conv_tripoli2pdg(int32_t); + int32_t conv_pdg2tripoli(int32_t); + +#ifdef __cplusplus +} +#endif + +#endif From 42beb8d953182b965b1711583e41a583a9967f9c Mon Sep 17 00:00:00 2001 From: inti Date: Wed, 14 Apr 2021 10:43:34 -0300 Subject: [PATCH 2/5] Add hook for converting ASCII SSV files to MCPL 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 #54). --- CMakeLists.txt | 23 +++ FILES | 4 + examples/example_ascii.ssv | 72 +++++++ src/mcnpptrac/ptracread.c | 2 +- src/python/mcpl.py | 58 ++++++ src/ssv/mcpl2ssv_app.c | 14 ++ src/ssv/ssv2mcpl_app.c | 14 ++ src/ssv/ssvmcpl.c | 256 ++++++++++++++++++++++++ src/ssv/ssvmcpl.h | 40 ++++ src/ssv/ssvread.c | 165 ++++++++++++++++ src/ssv/ssvread.h | 57 ++++++ src/tripolistock/stockmcpl.c | 372 +++++++++++++++++------------------ src/tripolistock/stockread.c | 2 +- 13 files changed, 891 insertions(+), 188 deletions(-) create mode 100644 examples/example_ascii.ssv create mode 100644 src/ssv/mcpl2ssv_app.c create mode 100644 src/ssv/ssv2mcpl_app.c create mode 100644 src/ssv/ssvmcpl.c create mode 100644 src/ssv/ssvmcpl.h create mode 100644 src/ssv/ssvread.c create mode 100644 src/ssv/ssvread.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 358105e..7bd4621 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) @@ -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}) diff --git a/FILES b/FILES index 10d7017..4bbfc36 100644 --- a/FILES +++ b/FILES @@ -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 diff --git a/examples/example_ascii.ssv b/examples/example_ascii.ssv new file mode 100644 index 0000000..c7587b8 --- /dev/null +++ b/examples/example_ascii.ssv @@ -0,0 +1,72 @@ +#MCPL-ASCII +#ASCII-FORMAT: v1 +#NPARTICLES: 64 +#NCOMMENTS: 2 +Comment 1 +Comment 2... +#END-HEADER +index pdgcode ekin[MeV] x[cm] y[cm] z[cm] ux uy uz time[ms] weight pol-x pol-y pol-z userflags + 0 2112 3.47369990549850627e-08 -123.480003356933594 90.6859970092773438 -1.4514000415802002 -0.948694166783049964 0.283021241426467896 -0.140990619593062111 2.42400002479553223 0.51996999979019165 0 0 0 0x00000000 + 1 2112 5.77080001562535472e-08 -117.540000915527344 105.400001525878906 -24.8850002288818359 -0.879177118394508961 0.359458833932876587 -0.312788972310636737 3.75279998779296875 0.380140006542205811 0 0 0 0x00000000 + 2 2112 8.20930026179667038e-08 -118.849998474121094 102.160003662109375 -22.4650001525878906 -0.336118191480636597 0.576056897640228271 -0.745106040800168068 4.23540019989013672 0.368730008602142334 0 0 0 0x00000000 + 3 2112 2.97570004192948545e-08 -113.430000305175781 115.55999755859375 3.10010004043579102 -0.823322305114626118 0.284240782260894775 0.491271370629770332 7.95730018615722656 0.25 0 0 0 0x00000000 + 4 2112 1.23809996921409038e-07 -120.44000244140625 98.2139968872070312 -20.9349994659423828 -0.756570069965022074 0.563810050487518311 -0.331240028079916093 3.50359988212585449 0.884909987449645996 0 0 0 0x00000000 + 5 2112 1.52219996607527719e-07 -116.569999694824219 107.80999755859375 6.02239990234375 -0.344238966703414917 0.2638092041015625 0.901057288763741915 0.476610004901885986 0.253989994525909424 0 0 0 0x00000000 + 6 2112 7.12149983428389532e-08 -117.349998474121094 105.860000610351562 -12.4340000152587891 -0.807120352930670859 0.0803020372986793518 -0.5849002638832973 2.46350002288818359 0.525200009346008301 0 0 0 0x00000000 + 7 2112 2.29840004806192155e-08 -120.05999755859375 99.1480026245117188 -18.0610008239746094 -0.862097802653479239 0.461878806352615356 -0.208459461052565465 8.07890033721923828 0.329459995031356812 0 0 0 0x00000000 + 8 2112 4.16690006943554181e-08 -119.75 99.9359970092773438 -13.1190004348754883 -0.974620263372899309 0.165060043334960938 -0.151230037747800611 2.54819989204406738 0.382259994745254517 0 0 0 0x00000000 + 9 2112 6.52409992696334484e-09 -119.370002746582031 100.879997253417969 -1.52900004386901855 -0.937178779853581112 0.344109565019607544 0.0573109226428034427 1.91400003433227539 0.934939980506896973 0 0 0 0x00000000 + 10 2112 1.59510005204310801e-08 -115.769996643066406 109.790000915527344 2.06730008125305176 -0.988787775881025288 0.128029718995094299 0.0768578253809757278 8.59210014343261719 0.264550000429153442 0 0 0 0x00000000 + 11 2112 9.89910020621209696e-09 -117.319999694824219 105.94000244140625 0.0560589991509914398 -0.577536880970001221 -0.283478468656539917 -0.765565874975880689 3.1389000415802002 0.299549996852874756 0 0 0 0x00000000 + 12 2112 1.14179997012797685e-07 -113.879997253417969 114.449996948242188 7.57989978790283203 -0.89445765245867348 0.356609076261520386 -0.269769299005694962 8.18029975891113281 0.539449989795684814 0 0 0 0x00000000 + 13 2112 3.23950004599282693e-08 -112.139999389648438 118.760002136230469 12.7100000381469727 -0.901899171113793918 0.380459636449813843 0.204519803874164963 14.8590002059936523 0.379040002822875977 0 0 0 0x00000000 + 14 2112 1.59270001631739433e-08 -116.94000244140625 106.870002746582031 15.8090000152587891 -0.528851568698883057 0.822402492933156148 -0.209690624258836861 2.74749994277954102 0.41767999529838562 0 0 0 0x00000000 + 15 2112 1.44629996867706723e-08 -126.330001831054688 83.6299972534179688 -4.43949985504150391 -0.937077686949636002 0.347889155149459839 -0.0293009274685129377 4.44360017776489258 0.291110008955001831 0 0 0 0x00000000 + 16 2112 6.1202001688798191e-08 -119.930000305175781 99.4919967651367188 11.7790002822875977 -0.677541277048379742 0.516071021556854248 0.524031028284504985 8.97469997406005859 0.335579991340637207 0 0 0 0x00000000 + 17 2112 9.21730034519896435e-08 -119.739997863769531 99.9449996948242188 -19.6100006103515625 -0.869905015805165882 0.474347293376922607 -0.135129229787643412 1.41579997539520264 0.66632002592086792 0 0 0 0x00000000 + 18 2112 1.45690002284482034e-08 -112.30999755859375 118.349998474121094 2.06879997253417969 -0.708230738599047394 0.402560412883758545 0.579960632183172953 5.28590011596679688 0.270909994840621948 0 0 0 0x00000000 + 19 2112 4.36940013059938792e-08 -115.290000915527344 110.959999084472656 4.65450000762939453 -0.975437539957747735 -0.0429948903620243073 0.216039452517207742 3.79850006103515625 0.677280008792877197 0 0 0 0x00000000 + 20 2112 3.48949988904223574e-08 -118.110000610351562 103.970001220703125 -20.0620002746582031 -0.771836840227308585 0.550377726554870605 0.318358681647314679 2.77679991722106934 0.297129988670349121 0 0 0 0x00000000 + 21 2112 3.64459999957489345e-08 -113.769996643066406 114.730003356933594 4.04839992523193359 -0.961290243332194705 -0.26617005467414856 -0.071236016656538545 4.84060001373291016 0.40904998779296875 0 0 0 0x00000000 + 22 2112 1.45019996011797048e-07 -112.519996643066406 117.819999694824219 -4.93160009384155273 -0.816349174922297194 0.576259374618530273 -0.0387189587662822021 5.25799989700317383 0.626200020313262939 0 0 0 0x00000000 + 23 2112 1.74700005572958617e-08 -110.669998168945312 122.400001525878906 -14.1049995422363281 -0.822830131861657321 0.21342003345489502 -0.526690101882159034 3.41619992256164551 0.437489986419677734 0 0 0 0x00000000 + 24 2112 5.33209991715466458e-08 -116.300003051757812 108.480003356933594 -19.1410007476806641 -0.870611300973659796 0.491930752992630005 0.0063322094924009812 0.653999984264373779 0.985019981861114502 0 0 0 0x00000000 + 25 2112 4.60810012370416189e-09 -115.75 109.830001831054688 -22.8209991455078125 -0.88762278310787468 0.42540132999420166 -0.176520546529278483 4.68709993362426758 0.483960002660751343 0 0 0 0x00000000 + 26 2112 1.45719996069715307e-08 -126.370002746582031 83.5479965209960938 8.94709968566894531 -0.951410820328177365 0.124800108373165131 0.281500237855158542 3.16160011291503906 0.92826002836227417 0 0 0 0x00000000 + 27 2112 3.06079996903463325e-08 -111.760002136230469 119.699996948242188 -10.4149999618530273 -0.446902096271514893 0.876634089705139452 0.178300838794906324 4.91470003128051758 0.595969974994659424 0 0 0 0x00000000 + 28 2112 7.29929965359588095e-08 -119.419998168945312 100.730003356933594 6.20279979705810547 -0.999681352559835812 -0.0237160325050354004 -0.00864541186741608729 7.73350000381469727 0.257180005311965942 0 0 0 0x00000000 + 29 2112 1.92609999061232884e-08 -115.080001831054688 111.489997863769531 -21.2220001220703125 -0.855879760658523558 0.516759872436523438 -0.0207139936877568376 3.2293999195098877 0.46950000524520874 0 0 0 0x00000000 + 30 2112 1.14700000608536357e-07 -117.209999084472656 106.209999084472656 -8.59160041809082031 -0.815746853599094823 0.493938088417053223 -0.300968828376103115 3.05929994583129883 0.441920012235641479 0 0 0 0x00000000 + 31 2112 1.32660005647267099e-07 -122.239997863769531 93.7570037841796875 -5.21750020980834961 -0.987686126538699583 -0.0959386229515075684 0.123579513144972605 7.38829994201660156 0.30597001314163208 0 0 0 0x00000000 + 32 2112 1.31299998429312836e-07 -111.919998168945312 119.30999755859375 -14.4600000381469727 -0.281441092491149902 0.946743632700745352 0.156420604135082103 9.27859973907470703 0.468109995126724243 0 0 0 0x00000000 + 33 2112 4.53439997727400623e-06 -124.339996337890625 88.5630035400390625 7.37909984588623047 -0.962602919437530846 0.254600763320922852 0.0925962785793572468 0.0542469993233680725 0.996119976043701172 0 0 0 0x00000000 + 34 2112 3.87660001877065952e-08 -121.529998779296875 95.529998779296875 11 -0.432332009077072144 0.842333899500592231 -0.321811490906568742 2.78349995613098145 0.548219978809356689 0 0 0 0x00000000 + 35 2112 2.23230003371099883e-07 -113.169998168945312 116.209999084472656 10.8520002365112305 -0.474509298801422119 0.817518758302604787 0.326349513825821336 2.93770003318786621 0.587100028991699219 0 0 0 0x00000000 + 36 2112 7.10419989502497629e-09 -121.169998168945312 96.4010009765625 -26.7830009460449219 -0.817738922428710491 -0.536409318447113037 -0.208729724352414936 8.38399982452392578 0.346329987049102783 0 0 0 0x00000000 + 37 2112 4.12940011074169888e-08 -114.300003051757812 113.400001525878906 -21.6949996948242188 -0.865877025131059574 0.231539204716682434 0.443448502116483179 1.86450004577636719 0.537299990653991699 0 0 0 0x00000000 + 38 2112 3.67559991332200298e-08 -112.05999755859375 118.959999084472656 6.3218998908996582 -0.784027714064757575 0.60086822509765625 0.155749541400233199 1.51639997959136963 0.368149995803833008 0 0 0 0x00000000 + 39 2112 6.83889993524644524e-08 -115.160003662109375 111.279998779296875 -10.3809995651245117 -0.868631611794567271 0.233790427446365356 0.43683081281614955 5.88840007781982422 0.276100009679794312 0 0 0 0x00000000 + 40 2112 4.86849991432336537e-08 -123 91.884002685546875 -16.7089996337890625 -0.800102410332514169 -0.59958183765411377 -0.0183780558116787701 4.8133997917175293 0.431639999151229858 0 0 0 0x00000000 + 41 2112 6.09310006893792888e-08 -125.160003662109375 86.5329971313476562 -4.25780010223388672 -0.993669105092067628 -0.0945709124207496643 -0.0606469464147730702 1.78149998188018799 0.851610004901885986 0 0 0 0x00000000 + 42 2112 9.38699962205191696e-09 -118.569999694824219 102.849998474121094 -20.2970008850097656 -0.773308891415540489 0.584289193153381348 0.246169651301370696 2.88010001182556152 0.710420012474060059 0 0 0 0x00000000 + 43 2112 1.61890003624876044e-08 -117.150001525878906 106.349998474121094 13.6379995346069336 -0.859241717044472941 0.481540977954864502 0.172690353641246103 1.89549994468688965 0.439220011234283447 0 0 0 0x00000000 + 44 2112 4.3507998981340279e-08 -113.629997253417969 115.080001831054688 -19.2360000610351562 -0.822069336612103685 0.509289562702178955 -0.25460979385082394 1.25030004978179932 0.46823999285697937 0 0 0 0x00000000 + 45 2112 1.01269996832797915e-08 -120.199996948242188 98.8079986572265625 -28.6870002746582031 -0.902857518382655377 0.401058882474899292 -0.154919573611271066 3.23440003395080566 0.518549978733062744 0 0 0 0x00000000 + 46 2112 4.38759997223314713e-08 -117.459999084472656 105.599998474121094 10.5570001602172852 -0.912789555618819359 -0.0714059621095657349 0.402139796250514769 3.85400009155273438 0.369419991970062256 0 0 0 0x00000000 + 47 2112 2.83129999445463909e-08 -120.629997253417969 97.7399978637695312 -19.3220005035400391 -0.95625380025360962 0.286351144313812256 -0.0598472359487048494 2.8564000129699707 0.976180016994476318 0 0 0 0x00000000 + 48 2112 4.87830007500633656e-08 -116.790000915527344 107.25 -19.8920001983642578 -0.800366502085877563 0.409258216619491577 -0.438088089849804352 0.99290001392364502 0.80037999153137207 0 0 0 0x00000000 + 49 2112 4.34109992397679889e-08 -109.910003662109375 124.269996643066406 -15.5299997329711914 -0.902565663386487471 0.430207937955856323 0.0172149178179823725 7.43499994277954102 0.738629996776580811 0 0 0 0x00000000 + 50 2112 9.43919964413453272e-09 -119.930000305175781 99.4850006103515625 -1.69060003757476807 -0.632051765918731689 0.761412188024528036 -0.144090406088378115 2.71779990196228027 0.422360002994537354 0 0 0 0x00000000 + 51 2112 1.89030000541379195e-08 -113.69000244140625 114.930000305175781 5.17339992523193359 -0.67661773047210616 0.502278327941894531 0.538428201425845576 7.19299983978271484 0.367419987916946411 0 0 0 0x00000000 + 52 2112 1.59460000759281684e-08 -118.139999389648438 103.900001525878906 -5.38070011138916016 -0.78659189226588544 0.530731260776519775 -0.315590753755776943 6.69710016250610352 0.322129994630813599 0 0 0 0x00000000 + 53 2112 6.67229969053551031e-08 -116.519996643066406 107.930000305175781 -2.34389996528625488 -0.699861764907836914 0.707301837260982724 -0.0995872533375932317 3.62700009346008301 0.25709998607635498 0 0 0 0x00000000 + 54 2112 6.38390034168878628e-08 -109.989997863769531 124.069999694824219 -1.99129998683929443 -0.547586917877197266 0.408717691898345947 0.730135888515994202 3.79439997673034668 0.707960009574890137 0 0 0 0x00000000 + 55 2112 5.27249994775047526e-08 -109.169998168945312 126.110000610351562 -8.47990036010742188 -0.845426278114792873 0.400048255920410156 0.353858447981685087 4.76170015335083008 0.845179975032806396 0 0 0 0x00000000 + 56 2112 4.00209998474565509e-08 -111.970001220703125 119.180000305175781 -7.96430015563964844 -0.772592318760102814 0.620591878890991211 -0.134040400056987002 1.46140003204345703 0.941559970378875732 0 0 0 0x00000000 + 57 2112 2.94770003961275506e-08 -126.599998474121094 82.9739990234375 -14.2650003433227539 -0.985356753959059972 0.16986943781375885 -0.0147119517434693217 10.1009998321533203 0.27303999662399292 0 0 0 0x00000000 + 58 2112 5.0488001335224908e-08 -122.550003051757812 93.0009994506835938 -18.2380008697509766 -0.66312161120084534 0.507871270179748535 0.549851345078289144 10.6370000839233398 0.266209989786148071 0 0 0 0x00000000 + 59 2112 1.01170002153594396e-07 -125.550003051757812 85.5609970092773438 9.60949993133544922 -0.684127339610090202 0.39062848687171936 0.615937633565551201 2.83150005340576172 0.363920003175735474 0 0 0 0x00000000 + 60 2112 1.60339993726665853e-07 -117.519996643066406 105.449996948242188 -4.21420001983642578 -0.98870140047988686 0.130750179290771484 -0.0733071026882216592 3.01110005378723145 0.358680009841918945 0 0 0 0x00000000 + 61 2112 5.18849994080028409e-08 -114.739997863769531 112.319999694824219 -2.40219998359680176 -0.862943289827330151 0.502971947193145752 -0.0484571860379849062 0.791930019855499268 0.976629972457885742 0 0 0 0x00000000 + 62 2112 3.50830013928771223e-08 -116.790000915527344 107.25 2.37159991264343262 -0.915344636292177771 0.400902032852172852 -0.0377061913501757703 1.83410000801086426 0.363070011138916016 0 0 0 0x00000000 + 63 2112 1.52060000147002938e-08 -113.339996337890625 115.779998779296875 -13.449000358581543 -0.763460922568346745 0.645160794258117676 -0.0299160368920024025 2.52659988403320312 0.584649980068206787 0 0 0 0x00000000 diff --git a/src/mcnpptrac/ptracread.c b/src/mcnpptrac/ptracread.c index b53fa7b..91612db 100644 --- a/src/mcnpptrac/ptracread.c +++ b/src/mcnpptrac/ptracread.c @@ -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!"); diff --git a/src/python/mcpl.py b/src/python/mcpl.py index f64e067..b62a0a6 100644 --- a/src/python/mcpl.py +++ b/src/python/mcpl.py @@ -47,6 +47,8 @@ _str('MCPLError'), _str('dump_file'), _str('convert2ascii'), + _str('save2ascii'), + _str('append2ascii'), _str('app_pymcpltool'), _str('collect_stats'), _str('dump_stats'), @@ -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) diff --git a/src/ssv/mcpl2ssv_app.c b/src/ssv/mcpl2ssv_app.c new file mode 100644 index 0000000..60e3885 --- /dev/null +++ b/src/ssv/mcpl2ssv_app.c @@ -0,0 +1,14 @@ +#include "ssvmcpl.h" + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ssv2mcpl : a simple command line utility for converting SSV text files to // +// MCPL. // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +int main(int argc,char** argv) { + return mcpl2ssv_app(argc,argv); +} diff --git a/src/ssv/ssv2mcpl_app.c b/src/ssv/ssv2mcpl_app.c new file mode 100644 index 0000000..fe9e4a7 --- /dev/null +++ b/src/ssv/ssv2mcpl_app.c @@ -0,0 +1,14 @@ +#include "ssvmcpl.h" + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ssv2mcpl : a simple command line utility for converting SSV text files to // +// MCPL. // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +int main(int argc,char** argv) { + return ssv2mcpl_app(argc,argv); +} diff --git a/src/ssv/ssvmcpl.c b/src/ssv/ssvmcpl.c new file mode 100644 index 0000000..677b250 --- /dev/null +++ b/src/ssv/ssvmcpl.c @@ -0,0 +1,256 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ssvmcpl : Code for converting between MCPL and ASCII SSV files (space sep. // +// values). // +// // +// // +// Compilation of ssvmcpl.c can proceed via any compliant C-compiler using // +// -std=c99 later. Furthermore, the following preprocessor flag can be used // +// when compiling ssvmcpl.c to fine tune the build process. // +// // +// SSVMCPL_HDR_INCPATH : Specify alternative value if the ssvmcpl header // +// itself is not to be included as "ssvmcpl.h". // +// SSVREAD_HDR_INCPATH : Specify alternative value if the ssvread header // +// is not to be included as "ssvread.h". // +// MCPL_HEADER_INCPATH : Specify alternative value if the MCPL header is // +// not to be included as "mcpl.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSVMCPL_HDR_INCPATH +# include SSVMCPL_HDR_INCPATH +#else +# include "ssvmcpl.h" +#endif + +#ifdef SSVREAD_HDR_INCPATH +# include SSVREAD_HDR_INCPATH +#else +# include "ssvread.h" +#endif + +#ifdef MCPL_HEADER_INCPATH +# include MCPL_HEADER_INCPATH +#else +# include "mcpl.h" +#endif + +#include +#include +#include +#include +#include + +//fwd declare internal functions from ssvread.c +void ssv_error(const char * msg); +int ssv_get_ncomment(ssv_file_t * ff); +char* ssv_get_comment(ssv_file_t * ff, int i); + +int ssv2mcpl(const char * ssvfile, const char * mcplfile) +{ + return ssv2mcpl2(ssvfile, mcplfile, 0, 1); +} + +int ssv2mcpl2(const char * ssvfile, const char * mcplfile, + int opt_dp, int opt_gzip) +{ + ssv_file_t f = ssv_open_file(ssvfile); + mcpl_outfile_t mcplfh = mcpl_create_outfile(mcplfile); + + mcpl_hdr_set_srcname(mcplfh,"ASCII SSV"); + + int i; + for(i=0; ix;//already in cm + mcpl_particle.position[1] = p->y;//already in cm + mcpl_particle.position[2] = p->z;//already in cm + mcpl_particle.direction[0] = p->dirx; + mcpl_particle.direction[1] = p->diry; + mcpl_particle.direction[2] = p->dirz; + mcpl_particle.time = p->time;//already in milliseconds + mcpl_particle.weight = p->weight; + mcpl_particle.ekin = p->ekin;//already in MeV + mcpl_particle.pdgcode = p->pdgcode; + + mcpl_add_particle(mcplfh,&mcpl_particle); + + } + + const char * tmp = mcpl_outfile_filename(mcplfh); + size_t laf = strlen(tmp); + char * actual_filename = malloc(laf+1); + actual_filename[0]='\0'; + strcat(actual_filename,tmp); + + int did_gzip = 0; + if (opt_gzip) + did_gzip = mcpl_closeandgzip_outfile(mcplfh); + else + mcpl_close_outfile(mcplfh); + ssv_close_file(f); + + printf("Created %s%s\n",actual_filename,(did_gzip?".gz":"")); + free(actual_filename); + return 1; +} + +void ssv2mcpl_parse_args(int argc,char **argv, const char** infile, + const char **outfile, const char **cfgfile, + int* double_prec, int* do_gzip) { + *cfgfile = 0; + *infile = 0; + *outfile = 0; + *double_prec = 0; + *do_gzip = 1; + int i; + for (i=1; i < argc; ++i) { + if (argv[i][0]=='\0') + continue; + if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { + const char * progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + printf("Usage:\n\n"); + printf(" %s [options] input.ssv [output.mcpl]\n\n",progname); + printf("Converts the Monte Carlo particles in the input.ssv file (ASCII SSV\n" + "format) to MCPL format and stores in the designated output file\n" + "(defaults to \"output.mcpl\").\n" + "\n" + "Options:\n" + "\n" + " -h, --help : Show this usage information.\n" + " -d, --double : Enable double-precision storage of floating point values.\n" + " -n, --nogzip : Do not attempt to gzip output file.\n" + ); + exit(0); + } + + if (strcmp(argv[i],"-d")==0||strcmp(argv[i],"--double")==0) { + *double_prec = 1; + continue; + } + if (strcmp(argv[i],"-n")==0||strcmp(argv[i],"--nogzip")==0) { + *do_gzip = 0; + continue; + } + if (argv[i][0]=='-') { + printf("Error: Unknown argument: %s\n",argv[i]); + exit(1); + } + if (!*infile) { + *infile = argv[i]; + continue; + } + if (!*outfile) { + *outfile = argv[i]; + continue; + } + printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*infile) { + printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*outfile) + *outfile = "output.mcpl"; + if (strcmp(*infile,*outfile)==0) { + //basic test, easy to cheat: + printf("Error: input and output files are identical.\n"); + exit(1); + } +} + +int ssv2mcpl_app(int argc,char** argv) +{ + const char * infile; + const char * outfile; + const char * cfgfile; + int double_prec, do_gzip; + ssv2mcpl_parse_args(argc,argv,&infile,&outfile,&cfgfile,&double_prec,&do_gzip); + int ok = ssv2mcpl2(infile, outfile, double_prec, do_gzip); + return ok ? 0 : 1; +} + +int mcpl2ssv(const char * mcplfile, const char * ssvfile){ + char* text_flag = "--text"; + const char* argv[4] = {"", text_flag, mcplfile, ssvfile}; + if( mcpl_tool(4, (char **)argv) ) + return 0; + return 1; +} + +void mcpl2ssv_parse_args(int argc,char **argv, const char** infile, + const char **outfile) { + *infile = 0; + *outfile = 0; + int i; + for (i=1; i < argc; ++i) { + if (argv[i][0]=='\0') + continue; + if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { + const char * progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + printf("Usage:\n\n"); + printf(" %s [options] input.mcpl [output.ssv]\n\n",progname); + printf("Converts the Monte Carlo particles in the input.mcpl file (MCPL\n" + "format) to ASCII SSV format and stores in the designated output file\n" + "(defaults to \"output.ssv\").\n" + "\n" + "Options:\n" + "\n" + " -h, --help : Show this usage information.\n" + ); + exit(0); + } + + if (argv[i][0]=='-') { + printf("Error: Unknown argument: %s\n",argv[i]); + exit(1); + } + if (!*infile) { + *infile = argv[i]; + continue; + } + if (!*outfile) { + *outfile = argv[i]; + continue; + } + printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*infile) { + printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*outfile) + *outfile = "output.ssv"; + if (strcmp(*infile,*outfile)==0) { + //basic test, easy to cheat: + printf("Error: input and output files are identical.\n"); + exit(1); + } +} + +int mcpl2ssv_app(int argc,char** argv) +{ + const char * infile; + const char * outfile; + mcpl2ssv_parse_args(argc,argv,&infile,&outfile); + int ok = mcpl2ssv(infile, outfile); + return ok ? 0 : 1; +} diff --git a/src/ssv/ssvmcpl.h b/src/ssv/ssvmcpl.h new file mode 100644 index 0000000..e0cb1a9 --- /dev/null +++ b/src/ssv/ssvmcpl.h @@ -0,0 +1,40 @@ +#ifndef ssvmcpl_h +#define ssvmcpl_h + +////////////////////////////////////////////////////////////////////////////////////// +// // +// Functions for converting ASCII SSV files (space sep. values) to MCPL files. // +// // +// Written 2021, osiris.abbate@ib.edu.ar, (Instituto Balseiro). // +// // +////////////////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////////////////// +// Create mcplfile based on content in ssvfile. Using this function will not +// enable double-precision or user-flags in the output file. Returns 1 on success, +// 0 on failure: +int ssv2mcpl(const char * ssvfile, const char * mcplfile); + +////////////////////////////////////////////////////////////////////////////////////// +// Advanced version of the above with more options: +// +// opt_dp : Set to 1 to enable double-precision storage of floating point +// values. Set to 0 for single-precision. +// opt_gzip: Set to 1 to gzip the resulting mcpl file. Set to 0 to leave the +// resulting file uncompressed. +// +int ssv2mcpl2(const char * ssvfile, const char * mcplfile, + int opt_dp, int opt_gzip); + +////////////////////////////////////////////////////////////////////////////////////// +// Create ssvfile based on content in mcplfile. This function redirects to mcpl_tool +// function with --text option. Returns 1 on success, 0 on failure. +int mcpl2ssv(const char * mcplfile, const char * ssvfile); + +////////////////////////////////////////////////////////////////////////////////////// +// For easily creating standard ssv2mcpl cmdline applications: +int ssv2mcpl_app(int argc,char** argv); +int mcpl2ssv_app(int argc,char** argv); + +#endif diff --git a/src/ssv/ssvread.c b/src/ssv/ssvread.c new file mode 100644 index 0000000..b275a1c --- /dev/null +++ b/src/ssv/ssvread.c @@ -0,0 +1,165 @@ + +///////////////////////////////////////////////////////////////////////////////////// +// // +// ssvread : Code for reading ASCII SSV files (space sep. values). // +// // +// // +// Compilation of ssvread.c can proceed via any compliant C-compiler using // +// -std=c99 or later, and the resulting code must always be linked with libm // +// (using -lm). Furthermore, the following preprocessor flags can be used // +// when compiling ssvread.c to fine tune the build process and the // +// capabilities of the resulting binary. // +// // +// SSVREAD_HDR_INCPATH : Specify alternative value if the ssvread header itself // +// is not to be included as "ssvread.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSVREAD_HDR_INCPATH +# include SSVREAD_HDR_INCPATH +#else +# include "ssvread.h" +#endif + +#include +#include +#include +#include +#include +#include + + +//Should be large enough to hold first record in all supported files: +#define SSVREAD_MAXLINESIZE 1024 + +void ssv_error(const char * msg) { + printf("ERROR: %s\n",msg); + exit(1); +} + +typedef struct { + FILE * file; + int ncomments; + char** comments; + ssv_particle_t part; + char line[SSVREAD_MAXLINESIZE];//for holding line from file +} ssv_fileinternal_t; + +int ssv_get_ncomment(ssv_file_t * ff){ + ssv_fileinternal_t * f = (ssv_fileinternal_t *)ff->internal; + assert(f); + + return f->ncomments; +} + +char* ssv_get_comment(ssv_file_t * ff, int i){ + ssv_fileinternal_t * f = (ssv_fileinternal_t *)ff->internal; + assert(f); + + if(i < f->ncomments) return f->comments[i]; + return NULL; +} + +ssv_file_t ssv_open_internal( const char * filename ) +{ + ssv_fileinternal_t * f = (ssv_fileinternal_t*)calloc(sizeof(ssv_fileinternal_t),1); + assert(f); + + ssv_file_t out; + out.internal = f; + + f->file = fopen(filename,"r"); + if (!f->file) + ssv_error("Unable to open file!"); + + // Procesar header + f->ncomments = 0; + f->comments = NULL; + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // SSV + if(strcmp(f->line, "#MCPL-ASCII\n") != 0) ssv_error("Unexpected format in SSV file"); + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // ASCII-FORMAT: v1 + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // NPARTICLES + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // END-HEADER / NCOMMENTS + if(strcmp(f->line, "#END-HEADER\n") != 0) + sscanf(f->line, "#NCOMMENTS: %d", &f->ncomments); + if(f->ncomments){ + f->comments = (char**)malloc(f->ncomments*sizeof(char*)); + int i; + for(i=0; incomments; i++){ + f->comments[i] = (char*)malloc(SSVREAD_MAXLINESIZE*sizeof(char*)); + if(!fgets(f->comments[i], SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // COMMENT[i] + f->comments[i][strcspn(f->comments[i], "\n")] = 0; + } + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // END-HEADER + } + if(!fgets(f->line, SSVREAD_MAXLINESIZE, f->file)) ssv_error("Unexpected format in SSV file"); // Variable names + + const char * bn = strrchr(filename, '/'); + bn = bn ? bn + 1 : filename; + printf("ssv_open_file: Opened file \"%s\":\n",bn); + + return out; +} + +ssv_file_t ssv_open_file( const char * filename ) +{ + if (!filename) + ssv_error("ssv_open_file called with null string for filename"); + + //Open file and initialize internal: + ssv_file_t out = ssv_open_internal( filename ); + ssv_fileinternal_t * f = (ssv_fileinternal_t *)out.internal; assert(f); + + //Return handle: + out.internal = f; + return out; +} + +int ssv_read(const char* line, ssv_particle_t* part){ + double aux; + int idx, uf; + ssv_particle_t p; + int nreaded = sscanf(line, "%i %li %g %g %g %g %g %g %g %g %g %g %g %g %x\n", + &idx,&p.pdgcode,&p.ekin,&p.x,&p.y,&p.z,&p.dirx,&p.diry,&p.dirz,&p.time,&p.weight,&p.polx,&p.poly,&p.polz,&p.uf); + if (nreaded == 15){ + *part = p; + return 1; + } + return 0; +} + +const ssv_particle_t * ssv_load_particle(ssv_file_t ff){ + ssv_fileinternal_t * f = (ssv_fileinternal_t *)ff.internal; + assert(f); + + double ndir, ndir2; + while(fgets(f->line, SSVREAD_MAXLINESIZE, f->file)){ + if(ssv_read(f->line, &f->part)){ + ndir2 = f->part.dirx*f->part.dirx + f->part.diry*f->part.diry + f->part.dirz*f->part.dirz; + if(ndir2 != 1){ // Normalization may be inexact in this format + ndir = sqrt(ndir2); + f->part.dirx /= ndir; + f->part.diry /= ndir; + f->part.dirz /= ndir; + } + return &f->part; + } + } + return 0; +} + +void ssv_close_file(ssv_file_t ff){ + ssv_fileinternal_t * f = (ssv_fileinternal_t *)ff.internal; + assert(f); + + if (!f) + return; + if (f->file) { + fclose(f->file); + f->file = 0; + } + free(f); + ff.internal = 0; +} diff --git a/src/ssv/ssvread.h b/src/ssv/ssvread.h new file mode 100644 index 0000000..259c1ef --- /dev/null +++ b/src/ssv/ssvread.h @@ -0,0 +1,57 @@ +#ifndef ssvread_h +#define ssvread_h + +///////////////////////////////////////////////////////////////////////////////////// +// // +// Code for reading ASCII SSV files (space sep. values). // +// // +// The code was written based on other hooks included in mcpl (mcnpssw, phits), // +// but provides a more basic interface. // +// // +// Written 2021, osiris.abbate@ib.edu.ar, (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct { + void * internal; + } ssv_file_t; + + typedef struct { + long pdgcode;//rawtype converted to PDG codes. + float ekin;//MeV + float x;//cm + float y;//cm + float z;//cm + float dirx; + float diry; + float dirz; + float time;//ms + float weight; + float polx; + float poly; + float polz; + int uf; + } ssv_particle_t; + + //Open file: + ssv_file_t ssv_open_file(const char * filename); + + //Query header info: + + //load next particle (null indicates eof): + const ssv_particle_t * ssv_load_particle(ssv_file_t); + + //close file and release resources: + void ssv_close_file(ssv_file_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/tripolistock/stockmcpl.c b/src/tripolistock/stockmcpl.c index 812656c..ca37337 100644 --- a/src/tripolistock/stockmcpl.c +++ b/src/tripolistock/stockmcpl.c @@ -1,186 +1,186 @@ - -///////////////////////////////////////////////////////////////////////////////////// -// // -// stockmcpl : Code for converting between MCPL and stock files from TRIPOLI-4 // -// (STORAGE). // -// // -// // -// Compilation of stockmcpl.c can proceed via any compliant C-compiler using // -// -std=c99 later. Furthermore, the following preprocessor flag can be used // -// when compiling stockmcpl.c to fine tune the build process. // -// // -// SSWMCPL_HDR_INCPATH : Specify alternative value if the stockmcpl header // -// itself is not to be included as "stockmcpl.h". // -// SSWREAD_HDR_INCPATH : Specify alternative value if the stockread header // -// is not to be included as "stockread.h". // -// MCPL_HEADER_INCPATH : Specify alternative value if the MCPL header is // -// not to be included as "mcpl.h". // -// // -// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // -// // -///////////////////////////////////////////////////////////////////////////////////// - -#ifdef SSWMCPL_HDR_INCPATH -# include SSWMCPL_HDR_INCPATH -#else -# include "stockmcpl.h" -#endif - -#ifdef SSWREAD_HDR_INCPATH -# include SSWREAD_HDR_INCPATH -#else -# include "stockread.h" -#endif - -#ifdef MCPL_HEADER_INCPATH -# include MCPL_HEADER_INCPATH -#else -# include "mcpl.h" -#endif - -#include -#include -#include -#include -#include - -void stock_error(const char * msg);//fwd declare internal function from stockread.c - -int stock2mcpl(const char * stockfile, const char * mcplfile) -{ - return stock2mcpl2(stockfile, mcplfile, 0, 1); -} - -int stock2mcpl2(const char * stockfile, const char * mcplfile, - int opt_dp, int opt_gzip) -{ - stock_file_t f = stock_open_file(stockfile); - mcpl_outfile_t mcplfh = mcpl_create_outfile(mcplfile); - - mcpl_hdr_set_srcname(mcplfh,"TRIPOLI-4 (STORAGE)"); - - // Aqui se deberia copiar info de header de stock a archivo mcpl - - if (opt_dp) { - mcpl_enable_doubleprec(mcplfh); - } - - mcpl_particle_t mcpl_particle; - memset(&mcpl_particle,0,sizeof(mcpl_particle)); - - const stock_particle_t * p; - while ((p=stock_load_particle(f))) { - mcpl_particle.pdgcode = p->pdgcode; - if (!mcpl_particle.pdgcode) { - printf("Warning: ignored particle with no PDG code set (raw stock type was %i).\n",p->rawtype); - continue; - } - - mcpl_particle.position[0] = p->x;//already in cm - mcpl_particle.position[1] = p->y;//already in cm - mcpl_particle.position[2] = p->z;//already in cm - mcpl_particle.direction[0] = p->dirx; - mcpl_particle.direction[1] = p->diry; - mcpl_particle.direction[2] = p->dirz; - mcpl_particle.time = 0;//particles in stock do not have time - mcpl_particle.weight = p->weight; - mcpl_particle.ekin = p->ekin;//already in MeV - - mcpl_add_particle(mcplfh,&mcpl_particle); - - } - - const char * tmp = mcpl_outfile_filename(mcplfh); - size_t laf = strlen(tmp); - char * actual_filename = malloc(laf+1); - actual_filename[0]='\0'; - strcat(actual_filename,tmp); - - int did_gzip = 0; - if (opt_gzip) - did_gzip = mcpl_closeandgzip_outfile(mcplfh); - else - mcpl_close_outfile(mcplfh); - stock_close_file(f); - - printf("Created %s%s\n",actual_filename,(did_gzip?".gz":"")); - free(actual_filename); - return 1; -} - -void stock2mcpl_parse_args(int argc,char **argv, const char** infile, - const char **outfile, const char **cfgfile, - int* double_prec, int* do_gzip) { - *cfgfile = 0; - *infile = 0; - *outfile = 0; - *double_prec = 0; - *do_gzip = 1; - int i; - for (i=1; i < argc; ++i) { - if (argv[i][0]=='\0') - continue; - if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { - const char * progname = strrchr(argv[0], '/'); - progname = progname ? progname + 1 : argv[0]; - printf("Usage:\n\n"); - printf(" %s [options] input.stock [output.mcpl]\n\n",progname); - printf("Converts the Monte Carlo particles in the input.stock file (MCNP Surface\n" - "Source Write format) to MCPL format and stores in the designated output\n" - "file (defaults to \"output.mcpl\").\n" - "\n" - "Options:\n" - "\n" - " -h, --help : Show this usage information.\n" - " -d, --double : Enable double-precision storage of floating point values.\n" - " -n, --nogzip : Do not attempt to gzip output file.\n" - ); - exit(0); - } - - if (strcmp(argv[i],"-d")==0||strcmp(argv[i],"--double")==0) { - *double_prec = 1; - continue; - } - if (strcmp(argv[i],"-n")==0||strcmp(argv[i],"--nogzip")==0) { - *do_gzip = 0; - continue; - } - if (argv[i][0]=='-') { - printf("Error: Unknown argument: %s\n",argv[i]); - exit(1); - } - if (!*infile) { - *infile = argv[i]; - continue; - } - if (!*outfile) { - *outfile = argv[i]; - continue; - } - printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); - exit(1); - } - if (!*infile) { - printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); - exit(1); - } - if (!*outfile) - *outfile = "output.mcpl"; - if (strcmp(*infile,*outfile)==0) { - //basic test, easy to cheat: - printf("Error: input and output files are identical.\n"); - exit(1); - } -} - -int stock2mcpl_app(int argc,char** argv) -{ - const char * infile; - const char * outfile; - const char * cfgfile; - int double_prec, do_gzip; - stock2mcpl_parse_args(argc,argv,&infile,&outfile,&cfgfile,&double_prec,&do_gzip); - int ok = stock2mcpl2(infile, outfile, double_prec, do_gzip); - return ok ? 0 : 1; -} + +///////////////////////////////////////////////////////////////////////////////////// +// // +// stockmcpl : Code for converting between MCPL and stock files from TRIPOLI-4 // +// (STORAGE). // +// // +// // +// Compilation of stockmcpl.c can proceed via any compliant C-compiler using // +// -std=c99 later. Furthermore, the following preprocessor flag can be used // +// when compiling stockmcpl.c to fine tune the build process. // +// // +// SSWMCPL_HDR_INCPATH : Specify alternative value if the stockmcpl header // +// itself is not to be included as "stockmcpl.h". // +// SSWREAD_HDR_INCPATH : Specify alternative value if the stockread header // +// is not to be included as "stockread.h". // +// MCPL_HEADER_INCPATH : Specify alternative value if the MCPL header is // +// not to be included as "mcpl.h". // +// // +// Written 2021, osiris.abbate@ib.edu.ar (Instituto Balseiro). // +// // +///////////////////////////////////////////////////////////////////////////////////// + +#ifdef SSWMCPL_HDR_INCPATH +# include SSWMCPL_HDR_INCPATH +#else +# include "stockmcpl.h" +#endif + +#ifdef SSWREAD_HDR_INCPATH +# include SSWREAD_HDR_INCPATH +#else +# include "stockread.h" +#endif + +#ifdef MCPL_HEADER_INCPATH +# include MCPL_HEADER_INCPATH +#else +# include "mcpl.h" +#endif + +#include +#include +#include +#include +#include + +void stock_error(const char * msg);//fwd declare internal function from stockread.c + +int stock2mcpl(const char * stockfile, const char * mcplfile) +{ + return stock2mcpl2(stockfile, mcplfile, 0, 1); +} + +int stock2mcpl2(const char * stockfile, const char * mcplfile, + int opt_dp, int opt_gzip) +{ + stock_file_t f = stock_open_file(stockfile); + mcpl_outfile_t mcplfh = mcpl_create_outfile(mcplfile); + + mcpl_hdr_set_srcname(mcplfh,"TRIPOLI-4 (STORAGE)"); + + // Aqui se deberia copiar info de header de stock a archivo mcpl + + if (opt_dp) { + mcpl_enable_doubleprec(mcplfh); + } + + mcpl_particle_t mcpl_particle; + memset(&mcpl_particle,0,sizeof(mcpl_particle)); + + const stock_particle_t * p; + while ((p=stock_load_particle(f))) { + mcpl_particle.pdgcode = p->pdgcode; + if (!mcpl_particle.pdgcode) { + printf("Warning: ignored particle with no PDG code set (raw stock type was %i).\n",p->rawtype); + continue; + } + + mcpl_particle.position[0] = p->x;//already in cm + mcpl_particle.position[1] = p->y;//already in cm + mcpl_particle.position[2] = p->z;//already in cm + mcpl_particle.direction[0] = p->dirx; + mcpl_particle.direction[1] = p->diry; + mcpl_particle.direction[2] = p->dirz; + mcpl_particle.time = 0;//particles in stock do not have time + mcpl_particle.weight = p->weight; + mcpl_particle.ekin = p->ekin;//already in MeV + + mcpl_add_particle(mcplfh,&mcpl_particle); + + } + + const char * tmp = mcpl_outfile_filename(mcplfh); + size_t laf = strlen(tmp); + char * actual_filename = malloc(laf+1); + actual_filename[0]='\0'; + strcat(actual_filename,tmp); + + int did_gzip = 0; + if (opt_gzip) + did_gzip = mcpl_closeandgzip_outfile(mcplfh); + else + mcpl_close_outfile(mcplfh); + stock_close_file(f); + + printf("Created %s%s\n",actual_filename,(did_gzip?".gz":"")); + free(actual_filename); + return 1; +} + +void stock2mcpl_parse_args(int argc,char **argv, const char** infile, + const char **outfile, const char **cfgfile, + int* double_prec, int* do_gzip) { + *cfgfile = 0; + *infile = 0; + *outfile = 0; + *double_prec = 0; + *do_gzip = 1; + int i; + for (i=1; i < argc; ++i) { + if (argv[i][0]=='\0') + continue; + if (strcmp(argv[i],"-h")==0||strcmp(argv[i],"--help")==0) { + const char * progname = strrchr(argv[0], '/'); + progname = progname ? progname + 1 : argv[0]; + printf("Usage:\n\n"); + printf(" %s [options] input.stock [output.mcpl]\n\n",progname); + printf("Converts the Monte Carlo particles in the input.stock file (TRIPOLI-4\n" + "STORAGE format) to MCPL format and stores in the designated output\n" + "file (defaults to \"output.mcpl\").\n" + "\n" + "Options:\n" + "\n" + " -h, --help : Show this usage information.\n" + " -d, --double : Enable double-precision storage of floating point values.\n" + " -n, --nogzip : Do not attempt to gzip output file.\n" + ); + exit(0); + } + + if (strcmp(argv[i],"-d")==0||strcmp(argv[i],"--double")==0) { + *double_prec = 1; + continue; + } + if (strcmp(argv[i],"-n")==0||strcmp(argv[i],"--nogzip")==0) { + *do_gzip = 0; + continue; + } + if (argv[i][0]=='-') { + printf("Error: Unknown argument: %s\n",argv[i]); + exit(1); + } + if (!*infile) { + *infile = argv[i]; + continue; + } + if (!*outfile) { + *outfile = argv[i]; + continue; + } + printf("Error: Too many arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*infile) { + printf("Error: Too few arguments! (run with -h or --help for usage instructions)\n"); + exit(1); + } + if (!*outfile) + *outfile = "output.mcpl"; + if (strcmp(*infile,*outfile)==0) { + //basic test, easy to cheat: + printf("Error: input and output files are identical.\n"); + exit(1); + } +} + +int stock2mcpl_app(int argc,char** argv) +{ + const char * infile; + const char * outfile; + const char * cfgfile; + int double_prec, do_gzip; + stock2mcpl_parse_args(argc,argv,&infile,&outfile,&cfgfile,&double_prec,&do_gzip); + int ok = stock2mcpl2(infile, outfile, double_prec, do_gzip); + return ok ? 0 : 1; +} diff --git a/src/tripolistock/stockread.c b/src/tripolistock/stockread.c index 2856a0c..4f77586 100644 --- a/src/tripolistock/stockread.c +++ b/src/tripolistock/stockread.c @@ -53,7 +53,7 @@ stock_file_t stock_open_internal( const char * filename ) stock_file_t out; out.internal = f; - f->file = fopen(filename,"rb"); + f->file = fopen(filename,"r"); if (!f->file) stock_error("Unable to open file!"); From e6a0d1b2cf94d407952fcba9a973c44faa173a53 Mon Sep 17 00:00:00 2001 From: inti Date: Sun, 28 Aug 2022 20:06:45 -0300 Subject: [PATCH 3/5] Add new hooks to GitHub workflow --- .github/workflows/cmake.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 24f16da..ad7a9e7 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -73,6 +73,10 @@ jobs: ${{github.workspace}}/install/bin/pymcpltool --help ${{github.workspace}}/install/bin/ssw2mcpl --help ${{github.workspace}}/install/bin/mcpl2ssw --help + ${{github.workspace}}/install/bin/ptrac2mcpl --help ${{github.workspace}}/install/bin/phits2mcpl --help ${{github.workspace}}/install/bin/mcpl2phits --help + ${{github.workspace}}/install/bin/stock2mcpl --help + ${{github.workspace}}/install/bin/ssv2mcpl --help + ${{github.workspace}}/install/bin/mcpl2ssv --help From d47692ed516a7ee55a2ba3b993693449c2153f41 Mon Sep 17 00:00:00 2001 From: inti Date: Sun, 28 Aug 2022 20:10:09 -0300 Subject: [PATCH 4/5] Enable github workflow --- .github/workflows/cmake.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index ad7a9e7..ab898c1 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -2,7 +2,7 @@ name: CMake on: push: - branches: [ develop, master ] + #branches: [ develop, master ] pull_request: branches: [ develop, master ] From 90627c4b62119b43f216ad79ac07d23bb42b6e7b Mon Sep 17 00:00:00 2001 From: inti Date: Mon, 29 Aug 2022 08:18:02 -0300 Subject: [PATCH 5/5] Fix bug in save2ascii and add example --- CMakeLists.txt | 2 +- examples/example_writessv.ipynb | 99 +++++++++++++++++++++++++++++++++ src/python/mcpl.py | 8 +-- 3 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 examples/example_writessv.ipynb diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bd4621..731c98a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -298,7 +298,7 @@ if (BUILD_WITHSSV) endif() install(TARGETS ssv2mcpl mcpl2ssv ssvmcpl ${INSTDEST}) if (BUILD_EXAMPLES) - install(FILES "${SRCEX}/example_ascii.ssv" DESTINATION examples) + install(FILES "${SRCEX}/example_ascii.ssv" "${SRCEX}/example_writessv.ipynb" DESTINATION examples) endif() endif() diff --git a/examples/example_writessv.ipynb b/examples/example_writessv.ipynb new file mode 100644 index 0000000..f4875db --- /dev/null +++ b/examples/example_writessv.ipynb @@ -0,0 +1,99 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "import mcpl" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "nparticles = 3\n", + "particles = np.zeros((nparticles, 13))\n", + "\n", + "# Particle SSV format:\n", + "# pdgcode ekin x y z ux uy uz time weight polx poly polz\n", + "\n", + "particles[0,0] = 2112 # pdgcode\n", + "particles[0,1] = 1e-8 # ekin [MeV]\n", + "particles[0,2] = 1.5 # x [cm]\n", + "particles[0,3] = -1.0 # y [cm]\n", + "particles[0,7] = 1.0 # uz\n", + "particles[0,9] = 1.0 # weight\n", + "\n", + "particles[1,0] = 2112 # pdgcode\n", + "particles[1,1] = 1e-7 # ekin [MeV]\n", + "particles[1,2] = 5.1 # x [cm]\n", + "particles[1,3] = 1.9 # y [cm]\n", + "particles[1,7] = 1.0 # uz\n", + "particles[1,9] = 1.0 # weight\n", + "\n", + "particles[2,0] = 2112 # pdgcode\n", + "particles[2,1] = 1e-6 # ekin [MeV]\n", + "particles[2,2] = -1.3 # x [cm]\n", + "particles[2,3] = -6.0 # y [cm]\n", + "particles[2,6] = 1.0 # uy\n", + "particles[2,9] = 1.0 # weight" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "not all arguments converted during string formatting", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mfilename\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"example_savessv.ssv\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmcpl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msave2ascii\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparticles\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfilename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m~/Universidad/KDSource/MCPLinstall/share/MCPL/python/mcpl.py\u001b[0m in \u001b[0;36msave2ascii\u001b[0;34m(particles, outfile)\u001b[0m\n\u001b[1;32m 1050\u001b[0m \u001b[0mfmtstr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"%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\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1051\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0midx\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mp\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparticles\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1052\u001b[0;31m \u001b[0mfout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfmtstr\u001b[0m\u001b[0;34m%\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0midx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1053\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1054\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: not all arguments converted during string formatting" + ] + } + ], + "source": [ + "filename = \"example_savessv.ssv\"\n", + "mcpl.save2ascii(particles, filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/src/python/mcpl.py b/src/python/mcpl.py index b62a0a6..ee5c994 100644 --- a/src/python/mcpl.py +++ b/src/python/mcpl.py @@ -1047,9 +1047,9 @@ def save2ascii(particles, outfile): +" 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" + 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 0x%08x\n" for idx,p in enumerate(particles): - fout.write(fmtstr%(idx,*p)) + fout.write(fmtstr%(idx, *p, 0)) def append2ascii(particles, outfile): @@ -1072,9 +1072,9 @@ def append2ascii(particles, outfile): 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" + 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 0x%08x\n" for idx,p in enumerate(particles): - fout.write(fmtstr%(idx,*p)) + fout.write(fmtstr%(idx, *p, 0)) def _pymcpltool_usage(progname,errmsg=None): if errmsg: