nrc-cnrc / EGSnrc

Toolkit for Monte Carlo simulation of ionizing radiation — Trousse d'outils logiciels pour la simulation Monte Carlo du rayonnement ionisant
http://nrc-cnrc.github.io/EGSnrc
GNU Affero General Public License v3.0
242 stars 146 forks source link

Bug in egs_input.cpp that effects egs_glib #1124

Open Apelova opened 6 months ago

Apelova commented 6 months ago

When trying to import a previously defined geometry using egs_glib quite a nerve wracking Bug occurs if there is a space before the line break.

I attached a simple Input-File for the usercode tutor7pp (and a geometry-file) that demonstrates the issue at hand (just comment/uncomment the lines 10 and 11 and make sure that sphere.geom is in the correct directory). It took me quite some time to realize what was happening when this bug first occurred, but after checking the source code, I recognized that the code only clears spaces in front of the file name to be imported.

The Bug can easily be fixed by inserting the line below in line 892 of egs_input.cpp

value = (*(value.end()-1) == ' ') ? string(value.begin(), value.end()-1) : value ;

Tutor7pp Input File

##############################################################
# Tutor7pp Input-File to reproduce Bug in egs_input.cpp
##############################################################

:start geometry definition:
    :start geometry:
        library = egs_glib
        name = imported_sphere 
        #--- to reproduce the bug comment/uncomment the two lines below
        include file = sphere.geom#<--- works
        #include file = sphere.geom #<--- doesn't work 
    :stop geometry:

    simulation geometry = imported_sphere
:stop geometry definition:

###########################################
:start source definition:
    :start source:
        library = egs_parallel_beam
        name = the_source
        :start shape:
            type = point
            position = 0 0 1e-7
        :stop shape:
        direction = 0 0 1
        charge = -1
        :start spectrum:
            type = monoenergetic
            energy = 20.0
        :stop spectrum:
    :stop source:

    simulation source = the_source
:stop source definition:

######################################## Run control
:start run control:
    ncase = 100
:stop run control:

Geometry File

:start geometry definition:
     :start geometry:
        name     = Sphere
        library  = egs_spheres
        midpoint = 0 0 0
        radii    = 2
        :start media input:
            media = H2O521ICRU
            set medium = 0 0
        :stop media input:
    :stop geometry:

    simulation geometry = Sphere
:stop geometry definition:

Operating system

EGSnrc version 2023

rtownson commented 6 months ago

Thanks for reporting this and including a fix!

Note to self: make sure to also account for the case where there are many tabs or spaces.

Apelova commented 6 months ago

P.S. I forgot to mention that I tested this fix against multiple arrangements of whitespace after and before the file name, and it seemed to fix all of them.