EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_isotropic_source.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ isotropic 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, 2005
25 #
26 # Contributors: Long Zhang
27 # Hubert Ho
28 # Randle Taylor
29 # Marc Chamberland
30 # Ernesto Mainegra-Hing
31 # Reid Townson
32 #
33 ###############################################################################
34 */
35 
36 
42 #include "egs_isotropic_source.h"
43 #include "egs_input.h"
44 #include "egs_math.h"
45 
47  EGS_ObjectFactory *f) : EGS_BaseSimpleSource(input,f), shape(0), geom(0),
48  regions(0), min_theta(0), max_theta(M_PI), min_phi(0), max_phi(2*M_PI),
49  nrs(0), gc(IncludeAll), media(0), nms(0), gcm(IncludeSelected) {
50  vector<EGS_Float> pos;
51  EGS_Input *ishape = input->takeInputItem("shape");
52  if (ishape) {
54  delete ishape;
55  }
56  if (!shape) {
57  string sname;
58  int err = input->getInput("shape name",sname);
59  if (err)
60  egsWarning("EGS_IsotropicSource: missing/wrong inline shape "
61  "definition and missing wrong 'shape name' input\n");
62  else {
64  if (!shape) egsWarning("EGS_IsotropicSource: a shape named %s"
65  " does not exist\n");
66  }
67  }
68  string geom_name;
69  int err = input->getInput("geometry",geom_name);
70  if (!err) {
71  geom = EGS_BaseGeometry::getGeometry(geom_name);
72  if (!geom) egsWarning("EGS_IsotropicSource: no geometry named %s\n",
73  geom_name.c_str());
74  else {
75  // Filter by region
76  vector<string> reg_options;
77  reg_options.push_back("IncludeAll");
78  reg_options.push_back("ExcludeAll");
79  reg_options.push_back("IncludeSelected");
80  reg_options.push_back("ExcludeSelected");
81  gc = (GeometryConfinement) input->getInput("region selection",reg_options,0);
82  if (gc == IncludeSelected || gc == ExcludeSelected) {
83  vector<int> regs;
84  err = input->getInput("selected regions",regs);
85  if (err || regs.size() < 1) {
86  egsWarning("EGS_IsotropicSource: region selection %d used "
87  "but no 'selected regions' input found\n",gc);
88  gc = gc == IncludeSelected ? IncludeAll : ExcludeAll;
89  egsWarning(" using %d\n",gc);
90  }
91  nrs = regs.size();
92  regions = new int [nrs];
93  for (int j=0; j<nrs; j++) {
94  regions[j] = regs[j];
95  }
96  }
97 
98  // Filter by medium
99  gcm = (GeometryConfinement) input->getInput("medium selection",reg_options,0);
100  if (gcm == IncludeSelected || gcm == ExcludeSelected) {
101  vector<string> selectedMedia;
102  err = input->getInput("selected media",selectedMedia);
103  if (err || selectedMedia.size() < 1) {
104  egsWarning("EGS_IsotropicSource: medium selection %d used "
105  "but no 'selected media' input found\n",gcm);
106  }
107  nms = selectedMedia.size();
108  media = new int [nms];
109  for (int j=0; j<nms; j++) {
110  int mediaID = geom->getMediumIndex(selectedMedia[j]);
111  media[j] = mediaID;
112  }
113  }
114  }
115  }
116  EGS_Float tmp_theta;
117  err = input->getInput("min theta", tmp_theta);
118  if (!err) {
119  min_theta = tmp_theta/180.0*M_PI;
120  }
121 
122  err = input->getInput("max theta", tmp_theta);
123  if (!err) {
124  max_theta = tmp_theta/180.0*M_PI;
125  }
126 
127  err = input->getInput("min phi", tmp_theta);
128  if (!err) {
129  min_phi = tmp_theta/180.0*M_PI;
130  }
131 
132  err = input->getInput("max phi", tmp_theta);
133  if (!err) {
134  max_phi = tmp_theta/180.0*M_PI;
135  }
136 
137  buf_1 = cos(min_theta);
138  buf_2 = cos(max_theta);
139 
140  setUp();
141 }
142 
143 void EGS_IsotropicSource::setUp() {
144  otype = "EGS_IsotropicSource";
145  if (!isValid()) {
146  description = "Invalid isotropic source";
147  }
148  else {
149  description = "Isotropic source from a shape of type ";
151  description += " with ";
152  description += s->getType();
153  if (q == -1) {
154  description += ", electrons";
155  }
156  else if (q == 0) {
157  description += ", photons";
158  }
159  else if (q == 1) {
160  description += ", positrons";
161  }
162  else {
163  description += ", unknown particle type";
164  }
165 
166  if (geom) {
167  geom->ref();
168 
169  description += "\n\n";
170  description += " Filtered by regions using option: " + std::to_string(gc);
171 
172  if (nms > 0) {
173  description += "\n\n";
174  description += " Filtered by media using option: " + std::to_string(gcm);
175  description += "\n";
176  description += " Media:";
177  for (auto i=0; i<nms; ++i) {
178  string mediumName = geom->getMediumName(media[i]);
179  description += " " + mediumName + "(" + std::to_string(media[i]) + ")";
180  }
181 
182  description += "\n\n Regions are only included if they match both the region filter and the medium filter.";
183  }
184 
185 
186  }
187  }
188 }
189 
190 extern "C" {
191 
192  EGS_ISOTROPIC_SOURCE_EXPORT EGS_BaseSource *createSource(EGS_Input *input,
193  EGS_ObjectFactory *f) {
194  return
195  createSourceTemplate<EGS_IsotropicSource>(input,f,"isotropic source");
196  }
197 
198 }
int ref()
Increase the reference count to this geometry.
static EGS_BaseGeometry * getGeometry(const string &Name)
Get a pointer to the geometry named Name.
static int getMediumIndex(const string &medname)
Get the index of a medium named medname.
static const char * getMediumName(int ind)
Get the name of medium with index ind.
static EGS_BaseShape * getShape(const string &Name)
Get a pointer to the shape named Name.
Definition: egs_shapes.cpp:64
static EGS_BaseShape * createShape(EGS_Input *inp)
Create a shape from the information pointed to by inp.
Definition: egs_shapes.cpp:51
Base class for 'simple' particle sources.
int q
The charge of this simple source.
EGS_BaseSpectrum * s
The energy spectrum of this source.
Base source class. All particle sources must be derived from this class.
string description
A short source description.
const string & getType() const
Get the spectrum type.
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:226
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:338
EGS_BaseShape * shape
The shape from which particles are emitted.
EGS_IsotropicSource(int Q, EGS_BaseSpectrum *Spec, EGS_BaseShape *Shape, EGS_BaseGeometry *geometry, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
GeometryConfinement
Geometry confinement options.
EGS_Float min_phi
avoid multi-calculating cos(min_theta) and cos(max_theta)
An object factory.
string otype
The object type.
const string & getObjectType() const
Get the object type.
EGS_Input class header file.
An isotropic source.
Attempts to fix broken math header files.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.