DataMedSci / pymchelper

Python toolkit for SHIELD-HIT12A and FLUKA
http://datamedsci.github.io/pymchelper/
15 stars 7 forks source link

402 update fluka support #656

Closed hendzeld closed 10 months ago

hendzeld commented 1 year ago

Resolves https://github.com/yaptide/yaptide/issues/402

grzanka commented 1 year ago

@hendzeld take a look here: https://github.com/DataMedSci/pymchelper/pull/658 I've also modified couple of tests

feel free to comment on my PR

grzanka commented 1 year ago

I've just merged https://github.com/DataMedSci/pymchelper/pull/658 which introduced a lot of conflicts

reviewpad[bot] commented 10 months ago

AI-Generated Summary: This pull request contains six patches that primarily focus on adding features and updating the test for a fluka runner. A brief summary for each patch is given below:

  1. Adds a draft test for the fluka runner. This includes several import statements and a new pytest fixture for expected fluka results. Another pytest fixture is added for the fluka path. Finally, it brings a new test for the fluka generator which verifies fluka files with expected results.

  2. Updates the runner.py file to handle fluka output. The 'get_data' function is modified to pattern match output files from a fluka simulation.

  3. Adds a function to update the random seed for Fluka input files in runner.py. This includes a private function to update the random number generator settings in the FLUKA input file based on the RNG seed.

  4. Modifies the runner.py file to exclude FLUKA from using RNG seeds, which is previously injected into the settings for each SingleSimulationExecutor call.

  5. Makes several changes to runner.py for running Fluka files, modifying the SingleSimulationExecutor callable class, specifically the call function.

  6. Maps simulator type from a command line interface, making enhancements to both the simulator_type.py and runmc.py, primarily to facilitate the selection of a simulator type through the runmc command-line interface.

grzanka commented 10 months ago

@hendzeld please remove "draft" status if it is in such state that I can take a look

reviewpad[bot] commented 10 months ago

AI-Generated Summary: This pull request introduces several changes to support FLUKA simulator in pyMCHelper. It includes:

  1. A new test for the 'fluka' runner function and modifications to the test_runner.py file.
  2. Updates to the 'runner.py' file to handle FLUKA output.
  3. Refactoring of the update function for the FLUKA input file, with the possibility to update the RNG seed in this input file. Moreover, using seed is now dependent on the simulator type.
  4. A mapping function from the simulator name to the SimulatorType enum in 'simulator_type.py'. This allows the correct identification of the simulator type from the CLI argument.
  5. A series of style fixes and code reformatting for better readability and PEP8 compliance.
  6. In the final patch, it sets the default 'what' value for the RANDOMIZ card in the FLUKA input file.
grzanka commented 10 months ago

This is an original example I was used for tests with Fluka:

*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
TITLE
Charged pion fluence inside and around a proton-irradiated Be target
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
BEAM         50.E+00                                                  PROTON
BEAMPOS          0.0       0.0     -50.0
*
GEOBEGIN                                                              COMBNAME
  0 0                       A simple Be target inside vacuum
RPP body1 -5000000.0 +5000000.0 -5000000.0 +5000000.0 -5000000.0 +5000000.0
RPP body2 -1000000.0 +1000000.0 -1000000.0 +1000000.0    -100.0  +1000000.0
RPP body3     -10.0      +10.0      -10.0      +10.0        0.0       +5.0
* plane to separate the upstream and downstream part of the target
XYP body4       2.5
END
* black hole
regBH1    5     +body1 -body2
* vacuum around
regVA2    5     +body2 -body3
* Be target 1st half
regBE3    5     +body3 +body4
* Be target 2nd half
regBE4    5     +body3 -body4
END
GEOEND
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
MATERIAL         4.0               1.848       5.0                    BERYLLIU
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
*  Be target, 1st and 2nd half
ASSIGNMAT  BERYLLIU   regBE3    regBE4
*  External Black Hole
ASSIGNMAT  BLCKHOLE   regBH1
*  Vacuum
ASSIGNMAT   VACUUM    regVA2
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
* e+e- and gamma production threshold set at 10 MeV
EMFCUT        -0.010     0.010       1.0  BERYLLIU                    PROD-CUT
* score in each region energy deposition and stars produced by primaries
SCORE       ENERGY    BEAMPART
* Boundary crossing fluence in the middle of the target (log intervals, one-way)
USRBDX          99.0   PIONS+-     -47.0   regBE3  regBE4        400. piFluenUD
USRBDX         +50.0               +50.0                 0.0      10.0 &
* Boundary crossing current in the middle of the target (log intervals, one-way)
USRBDX          -1.0   PIONS+-     -47.0   regBE3  regBE4        400. piCurrUD
USRBDX         +50.0               +50.0                 0.0      10.0 &
* Tracklength fluence inside the target, Upstream part and Downstream part
* Logarithmic energy intervals
USRTRACK        -1.0   PIONS+-     -48.0   regBE3     1000.0      20. piFluenU
USRTRACK        50.0     0.001                                           &
USRTRACK        -1.0   PIONS+-     -49.0   regBE4     1000.0      20. piFluenD
USRTRACK        50.0     0.001                                           &
* Cartesian binning of the pion fluence inside and around the target
USRBIN          10.0   PIONS+-     -50.0      50.0      50.0      50. piFluBin
USRBIN         -50.0     -50.0     -10.0     100.0     100.0      60.0   &
* Cartesian binning of the deposited energy inside the target
USRBIN          10.0    ENERGY     -51.0      10.0      10.0       5. Edeposit
USRBIN         -10.0     -10.0       0.0      20.0      20.0       5.0   &
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
RANDOMIZE        1.0
*...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
START       100000.0
STOP

When I ran it with rfluka example.inp on Ares then I get the results as expected.

Then I've tried to run it with runmc from this exact branch, but no output and no error code was produced:

[ares][plgkongruencj@ac0008 pymchelper_test]$ poetry run runmc example.inp -j 2 -k -v
DEBUG:root:Inspecting /net/people/plgrid/plgkongruencj/.cache/pypoetry/virtualenvs/pymchelper-test-RpfZV3ef-py3.10/bin
DEBUG:root:Inspecting /net/software/testing/software/fluka/2021.2.9-gcc-10.3.0
DEBUG:root:Inspecting /net/software/testing/software/fluka/2021.2.9-gcc-10.3.0/flutil
DEBUG:root:Inspecting /net/software/testing/software/binutils/2.36.1-GCCcore-10.3.0/bin
DEBUG:root:Inspecting /net/software/testing/software/GCCcore/10.3.0/bin
DEBUG:root:Inspecting /net/software/testing/software/Python/3.10.8-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/software/testing/software/OpenSSL/1.1/bin
DEBUG:root:Inspecting /net/software/testing/software/XZ/5.2.7-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/software/testing/software/SQLite/3.39.4-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/software/testing/software/Tcl/8.6.12-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/software/testing/software/ncurses/6.3-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/software/testing/software/bzip2/1.0.8-GCCcore-12.2.0/bin
DEBUG:root:Inspecting /net/people/plgrid/plgkongruencj/micromamba/bin
DEBUG:root:Inspecting /net/slurm/releases/production.x86_64/bin
DEBUG:root:Inspecting /net/slurm/releases/production.x86_64/sbin
DEBUG:root:Inspecting /net/people/plgrid/plgkongruencj/micromamba/condabin
DEBUG:root:Inspecting /net/people/plgrid/plgkongruencj/.local/bin
DEBUG:root:Inspecting /net/people/plgrid/plgkongruencj/bin
DEBUG:root:Inspecting /opt/cyfronet/bin
DEBUG:root:Inspecting /usr/local/bin
DEBUG:root:Inspecting /usr/bin
DEBUG:root:Inspecting /usr/local/sbin
DEBUG:root:Inspecting /usr/sbin
DEBUG:root:Inspecting /opt/cyfronet/pudo
INFO:root:Workspace /net/ascratch/people/plgkongruencj/pymchelper_test/run_1
DEBUG:root:Copying input files into /net/ascratch/people/plgkongruencj/pymchelper_test/run_1
INFO:root:Workspace /net/ascratch/people/plgkongruencj/pymchelper_test/run_2
DEBUG:root:Copying input files into /net/ascratch/people/plgkongruencj/pymchelper_test/run_2
DEBUG:root:working directory /net/ascratch/people/plgkongruencj/pymchelper_test/run_2, command /net/software/testing/software/fluka/2021.2.9-gcc-10.3.0/flutil/rfluka /net/ascratch/people/plgkongruencj/pymchelper_test/run_2/example.inp
DEBUG:root:working directory /net/ascratch/people/plgkongruencj/pymchelper_test/run_1, command /net/software/testing/software/fluka/2021.2.9-gcc-10.3.0/flutil/rfluka /net/ascratch/people/plgkongruencj/pymchelper_test/run_1/example.inp
INFO:root:run elapsed time 1.893 seconds
MC simulation took 1.893 seconds
DEBUG:root:Files to merge /net/ascratch/people/plgkongruencj/pymchelper_test/run_*/*_fort.*
INFO:root:Workspace reading 0.001 seconds
Saving output 0.000 seconds
[ares][plgkongruencj@ac0008 pymchelper_test]$ ls run_*/*_fort.*
ls: cannot access 'run_*/*_fort.*': No such file or directory

This is the error log:

[ares][plgkongruencj@ac0008 run_1]$ cat example001.err

 *** Unable to resolve name element REGBE3 in card ***
 ASSIGNMA    BERYLLIU    REGBE3    REGBE4
 *** run stopped ***

I suspect the output Fluka file is ill-formatted now:

[ares][plgkongruencj@ac0008 pymchelper_test]$ diff example.inp run_1/example.inp
1c1
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
---
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
4,5c4,5
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
< BEAM         50.E+00                                                  PROTON
---
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
> BEAM            50.0                                                  PROTON
9,12c9,12
<   0 0                       A simple Be target inside vacuum
< RPP body1 -5000000.0 +5000000.0 -5000000.0 +5000000.0 -5000000.0 +5000000.0
< RPP body2 -1000000.0 +1000000.0 -1000000.0 +1000000.0    -100.0  +1000000.0
< RPP body3     -10.0      +10.0      -10.0      +10.0        0.0       +5.0
---
>     0    0                  A simple Be target inside vacuum
> RPP body1      -5000000.0 5000000.0 -5000000.0 5000000.0 -5000000.0 5000000.0
> RPP body2      -1000000.0 1000000.0 -1000000.0 1000000.0 -100.0 1000000.0
> RPP body3      -10.0 10.0 -10.0 10.0 0.0 5.0
14c14
< XYP body4       2.5
---
> XYP body4      2.5
17c17
< regBH1    5     +body1 -body2
---
> regBH1       5 +body1 -body2
19c19
< regVA2    5     +body2 -body3
---
> regVA2       5 +body2 -body3
21c21
< regBE3    5     +body3 +body4
---
> regBE3       5 +body3 +body4
23c23
< regBE4    5     +body3 -body4
---
> regBE4       5 +body3 -body4
26c26
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
---
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
28c28
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
---
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
30c30
< ASSIGNMAT  BERYLLIU   regBE3    regBE4
---
> ASSIGNMA    BERYLLIU    REGBE3    REGBE4
32c32
< ASSIGNMAT  BLCKHOLE   regBH1
---
> ASSIGNMA    BLCKHOLE    REGBH1
34,35c34,35
< ASSIGNMAT   VACUUM    regVA2
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
---
> ASSIGNMA      VACUUM    REGVA2
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
37c37
< EMFCUT        -0.010     0.010       1.0  BERYLLIU                    PROD-CUT
---
> EMFCUT         -0.01      0.01       1.0  BERYLLIU                    PROD-CUT
39c39
< SCORE       ENERGY    BEAMPART
---
> SCORE         ENERGY  BEAMPART
41,42c41,42
< USRBDX          99.0   PIONS+-     -47.0   regBE3  regBE4        400. piFluenUD
< USRBDX         +50.0               +50.0                 0.0      10.0 &
---
> USRBDX          99.0   PIONS+-     -47.0    REGBE3    REGBE4     400.0piFluenUD
> USRBDX          50.0                50.0                 0.0      10.0 &
44,45c44,45
< USRBDX          -1.0   PIONS+-     -47.0   regBE3  regBE4        400. piCurrUD
< USRBDX         +50.0               +50.0                 0.0      10.0 &
---
> USRBDX          -1.0   PIONS+-     -47.0    REGBE3    REGBE4     400.0piCurrUD
> USRBDX          50.0                50.0                 0.0      10.0 &
48,54c48,54
< USRTRACK        -1.0   PIONS+-     -48.0   regBE3     1000.0      20. piFluenU
< USRTRACK        50.0     0.001                                           &
< USRTRACK        -1.0   PIONS+-     -49.0   regBE4     1000.0      20. piFluenD
< USRTRACK        50.0     0.001                                           &
< * Cartesian binning of the pion fluence inside and around the target
< USRBIN          10.0   PIONS+-     -50.0      50.0      50.0      50. piFluBin
< USRBIN         -50.0     -50.0     -10.0     100.0     100.0      60.0   &
---
> USRTRACK        -1.0   PIONS+-     -48.0    REGBE3    1000.0      20.0piFluenU
> USRTRACK        50.0     0.001                                         &
> USRTRACK        -1.0   PIONS+-     -49.0    REGBE4    1000.0      20.0piFluenD
> USRTRACK        50.0     0.001                                         &
> * Cartesian binning of the pion fluence inside and around the target
> USRBIN          10.0   PIONS+-     -50.0      50.0      50.0      50.0piFluBin
> USRBIN         -50.0     -50.0     -10.0     100.0     100.0      60.0 &
56,60c56,60
< USRBIN          10.0    ENERGY     -51.0      10.0      10.0       5. Edeposit
< USRBIN         -10.0     -10.0       0.0      20.0      20.0       5.0   &
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
< RANDOMIZE        1.0
< *...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
---
> USRBIN          10.0    ENERGY     -51.0      10.0      10.0       5.0Edeposit
> USRBIN         -10.0     -10.0       0.0      20.0      20.0       5.0 &
> * updated random number generator settings
> RANDOMIZ         1.0         1
> * ..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
reviewpad[bot] commented 10 months ago

AI-Generated Summary: This pull request contains a series of changes spanning 12 patches. Notable adjustments are:

  1. It adds a fluka runner test draft, inserting new code, deleting incorrect or obsolete parts, and modifying some exiting elements.
  2. It updates the executor/runner.py file to handle fluka output.
  3. The patch updates the random seed, executor/runner.py file for FLUKA input updates.
  4. It also modifies executor/runner.py in not using seed for fluka.
  5. Changes have been made in how the fluka file runs.
  6. It maps simulator type from runmc CLI.
  7. It updates the fluka mock test.
  8. In an effort to clean up the code, a reformat is performed.
  9. To improve code style, some changes are made.
  10. It sets RANDOMIZ what 1 to the default value.
  11. Updates were made to the logging in executor/runner.py.

The changes mainly bring about enhancements to the runner testing, improved handling of fluka output, the mapping of the simulator type, and test updates for fluka. Overall, the modifications aim to enhance the efficiency, readability, and functionality of the code.

reviewpad[bot] commented 10 months ago

AI-Generated Summary: This pull request introduces additional functionalities to the pymchelper Python package as well as some modifications.

The executor/runner.py file has been edited to improve the handling of different simulation types by expanding it to cater for the FLUKA simulation type. This is alongside the existing SHIELDHIT and TOPAS simulation types. The pull request also adds a method to update the FLUKA input file with a new random number generator (RNG) seed.

Changes are also made to the way the simulation settings are handled, particularly the RNG seeds.

In the file simulator_type.py, functionalities for handling the names of simulators and retrieving a simulator type from its name have been added.

The utils/runmc.py file has been edited to incorporate changes made to the simulator type handling in the SimulationSettings.

In addition, the tests/test_runner.py file has been updated with more detailed tests that also include the new FLUKA simulation type.

Finally, the pull request includes various documentation updates to reflect these changes and improvements.

reviewpad[bot] commented 10 months ago

AI-Generated Summary: This pull request adds support for FLUKA simulator type in the pymchelper executor runner, simulator type and runmc utility. In the runner.py file, logic is included to handle FLUKA specific tasks such as updating the FLUKA input file with a new RNG seed. For the simulator_type, methods are added to get simulator names and get simulator type from its name. For the runmc utility, the simulator options are updated to make use of the new methods added to the simulator_type.

In addition, tests are added and updated to verify these new functionalities, including a test for the new FLUKA simulator type support. The tests also include a mock for the FLUKA simulator. Changes are also made for a few existing tests to ensure compatibility with the new changes.

Furthermore, minor adjustments are made on import statements and comments for a cleaner codebase, and some typos and indentation issues are fixed.