EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_angular_spread_source.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ angular spread source
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2009
25 #
26 # Contributors: Hannah Gallop
27 #
28 ###############################################################################
29 */
30 
31 
38 #include "egs_input.h"
39 #include "egs_math.h"
40 
41 static bool EGS_ANGULAR_SPREAD_SOURCE_LOCAL inputSet = false;
42 
43 // 1 / (2*sqrt(2*ln(2)))
44 static const EGS_Float FWHM_TO_SIGMA = 0.4246609001440095285;
45 
46 EGS_AngularSpreadSource::EGS_AngularSpreadSource(EGS_Input *input,
47  EGS_ObjectFactory *f) : EGS_BaseSource(input,f), source(0), sigma(0) {
48  EGS_Input *isource = input->takeInputItem("source",false);
49  if (isource) {
50  source = EGS_BaseSource::createSource(isource);
51  delete isource;
52  }
53  if (!source) {
54  string sname;
55  int err = input->getInput("source name",sname);
56  if (err)
57  egsWarning("EGS_AngularSpreadSource: missing/wrong inline source "
58  "definition and missing wrong 'source name' input\n");
59  else {
60  source = EGS_BaseSource::getSource(sname);
61  if (!source) egsWarning("EGS_AngularSpreadSource: a source named %s"
62  " does not exist\n", sname.c_str());
63  }
64  }
65  // Check for the new 'fwhm' input key first
66  EGS_Float fwhm = 0;
67  int err_fwhm = input->getInput("fwhm",fwhm);
68  int err_sigma = input->getInput("sigma",sigma);
69 
70  if (!err_fwhm && !err_sigma) {
71  egsFatal("EGS_AngularSpreadSource: both 'sigma' and 'fwhm' inputs "
72  "provided. Please use only one.\n");
73  }
74  else if (!err_fwhm) {
75  if (fwhm < 0) {
76  egsFatal("EGS_AngularSpreadSource: 'fwhm' must be positive, "
77  "got %g\n", fwhm);
78  }
79  // Convert FWHM (degrees) to sigma (radians), then store as sigma^2 (rad^2)
80  sigma = FWHM_TO_SIGMA * fwhm;
81  sigma *= M_PI/180;
82  sigma *= sigma;
83  }
84  else if (!err_sigma) {
85  if (sigma < 0) {
86  egsFatal("EGS_AngularSpreadSource: negative 'sigma' input is no "
87  "longer supported. To specify the angular spread as FWHM, "
88  "use the 'fwhm' input key instead.\n");
89  }
90  // Convert sigma (degrees) to sigma^2 (rad^2)
91  sigma *= M_PI/180;
92  sigma *= sigma;
93  }
94  setUp();
95 }
96 
97 void EGS_AngularSpreadSource::setUp() {
98  otype = "EGS_AngularSpreadSource";
99  if (!isValid()) {
100  description = "Invalid angular spread source";
101  return;
102  }
103  description = "Angular spread added to a ";
105  source->ref();
106 }
107 
108 extern "C" {
109 
110  static void setInputs() {
111  inputSet = true;
112 
113  setBaseSourceInputs(false, false);
114 
115  srcBlockInput->getSingleInput("library")->setValues({"egs_angular_spread_source"});
116 
117  // Format: name, isRequired, description, vector string of allowed values
118  srcBlockInput->addSingleInput("source name", true, "The name of a previously defined source.");
119  srcBlockInput->addSingleInput("sigma", false, "If positive, it is the sigma of the Gaussian distribution in degrees. If negative, it is the FWHM of the distribution.");
120  }
121 
122  EGS_ANGULAR_SPREAD_SOURCE_EXPORT string getExample() {
123  string example;
124  example = {
125  R"(
126  # Example of egs_angular_spread_source
127  #:start source:
128  library = egs_angular_spread_source
129  name = my_source
130  sigma = 10
131  source name = my_parallel_source
132  #create source called my_parallel_source
133  :stop source:
134 )"};
135  return example;
136  }
137 
138  EGS_ANGULAR_SPREAD_SOURCE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
139  if(!inputSet) {
140  setInputs();
141  }
142  return srcBlockInput;
143  }
144 
145  EGS_ANGULAR_SPREAD_SOURCE_EXPORT EGS_BaseSource *createSource(EGS_Input *input,
146  EGS_ObjectFactory *f) {
147  return
148  createSourceTemplate<EGS_AngularSpreadSource>(input,f,"angular spread source");
149  }
150 
151 }
EGS_BaseSource * source
The source being transformed.
Base source class. All particle sources must be derived from this class.
const char * getSourceDescription() const
Get a short description of this source.
static EGS_BaseSource * getSource(const string &Name)
Get a pointer to the source named Name.
string description
A short source description.
static EGS_BaseSource * createSource(EGS_Input *)
Create sources from the information pointed to by input.
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
EGS_Input * takeInputItem(const string &key, bool self=true)
Get the property named key.
Definition: egs_input.cpp:229
int getInput(const string &key, vector< string > &values) const
Assign values to an array of strings from an input identified by key.
Definition: egs_input.cpp:341
An object factory.
int ref()
Increase the reference count to this object.
string otype
The object type.
An angular spread source: header.
EGS_Input class header file.
Attempts to fix broken math header files.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.