From 42beb8d953182b965b1711583e41a583a9967f9c Mon Sep 17 00:00:00 2001 From: inti Date: Wed, 14 Apr 2021 10:43:34 -0300 Subject: [PATCH] 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!");