EGSnrc C++ class library
Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
|
The tutor2pp application calculates the deposited, transmitted and reflected energy fractions for any source of electrons or photons that can be defined with the egspp source package, incident on any geometry that can be constructed using the egspp geometry package. The corresponding original tutorial mortran user code tutor2
, which calculates the deposited, transmitted and reflected energy loss fractions for 20 MeV electrons perpendicularly incident on a semi-infinite 1 mm plate of Tantalum, only provides a very small fraction of the functionality offered by tutor2pp.
The complete source code of the application is found at the end of this documentation page.
To obtain the required functionality we implement a Tutor2_Application class derived from EGS_SimpleApplication
edep
and nreg
will become clear in the following. The constructor of the Tutor2_Application class takes the command line arguments given to the application as arguments and passes them to the EGS_SimpleApplication constructor, edep
) and the energy transmitted (defined as the energy of particles exiting the geometry and moving forward and scored in region nreg+1
of edep
) in addition to the energy deposited in the nreg
regions of the geometry (scored in regions 1 ... nreg
of edep
). This is all that is needed in the Tutor2_Application constructor.
To demonstrate good coding habits we provide a destructor for the Tutor2_Application that deallocates the memory needed by the scoring array
The next step is the implementation of the actual scoring which is accomplished by re-implementing the ausgab
function to collect energy deposition. Energy deposition is indicated by values of its argument less or equal to 4
the_stack
, which is a pointer to the Fortran common block STACK
Note that the mortran back-end uses Fortran style indexing and therefore we have to use the_stack->np-1
for the particle index. The convention for region numbers used by the geometry package is that the outside region has index -1 and inside regions are between 0 and nreg-1
. However, this is translated to the convention typically adopted by EGSnrc mortran user codes that the outside region is region 1 and inside regions are 2 to nreg+1
by EGS_SimpleApplication. We have to therefore subtract 1 from the region index and change it to nreg+1
if it is 0 (particle is outside) and the particle is movinge forward to obtain the edep
region for collecting the energy: We then simply use the score() function of the scoring array object to score the energy This is all about ausgab
. We automatically obtain history-by-history statistical analysis, provided we inform our scoring object each time the simulation of a new history begins. This is accomplished by reimplementing the startHistory
function, which is called from within the shower loop before transporting a new particle from the source:
The final step in the implementation of the Tutor2_Application class is to report the simulation results. For this purpose we define a separate function reportResults
, which will be called from our main program. In the implementation of this function we simply use the reportResults() method of the scoring array. This method divides the accumulated results by the number of statistically independent events. Hence, in order to get fractions of the energy imparted into the phantom, we have to multiply by this number (available in the last_case data member) and divide by the total energy that entered the phantom (available in the Etot data member). Our reportResults
tfunction herefore looks like this
The main program of the tutor2pp application is very simple, if fact we use the defined macros APP_MAIN or APP_LIB, from EGS_SimpleApplication. APP_MAIN creates the application object, runs a simulation, outputs results and finishes. APP_LIB just creates and returns the application object.
That's all. With 35 lines of code (excluding comments and blank lines) we have implemented a complete EGSnrc C++ application, which provides much more functionality than the original tutor2
mortran application (97 lines of code excluding comments and blank lines).
tutor2pp
application. We include the general EGSnrc config file, the general egspp config file and the config file describing our C++ compiler: EGSPP_USER_MACROS
make variable. In our case we don't use this capability and therefore leave EGSPP_USER_MACROS
empty: ABS_EGSPP
is defined in egspp1.spec
to be the absolute path to the egspp sources ($HEN_HOUSE/egs++/
) Finally we simply include the generic Makefile for EGSnrc C++ applications, which provides rules for compiling the various sources and building the application: We now can build our application by simply going to the tutor2pp
directory and typing make
. The resulting executable will be called tutor2pp
(tutor2pp.exe
on Windows) and will be put into the bin/$my_machine
subdirectory of $EGS_HOME
.
To run the application, we simply type
tutor2pp -i test1 -p tutor_data
with test1
being the name of the input file without extension (test1.egsinp
is an example input file provided with the distribution) and tutor_data
the name of the PEGS data file for the media of the geometry defined in test1
. Alternatively, we can use the egs_gui
graphical user interface. We can also set the number of histories to be run on the command line with the -n
option:
tutor2pp -i test1 -p tutor_data -n 1000000
(all applications derived from EGS_SimpleApplication automatically understand the -n
option).
Here is the complete source code of the tutor2pp application: