EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_fano_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ Fano source headers
5 # Copyright (C) 2016 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 # Authors: Ernesto Mainegra-Hing, 2016
25 # Hugo Bouchard, 2016
26 #
27 # Contributors:
28 #
29 ###############################################################################
30 */
31 
32 
38 #ifndef EGS_FANO_SOURCE_
39 #define EGS_FANO_SOURCE_
40 
41 #include "egs_vector.h"
42 #include "egs_base_source.h"
43 #include "egs_rndm.h"
44 #include "egs_shapes.h"
45 #include "egs_base_geometry.h"
46 #include "egs_math.h"
47 
48 
49 #ifdef WIN32
50 
51  #ifdef BUILD_FANO_SOURCE_DLL
52  #define EGS_FANO_SOURCE_EXPORT __declspec(dllexport)
53  #else
54  #define EGS_FANO_SOURCE_EXPORT __declspec(dllimport)
55  #endif
56  #define EGS_FANO_SOURCE_LOCAL
57 
58 #else
59 
60  #ifdef HAVE_VISIBILITY
61  #define EGS_FANO_SOURCE_EXPORT __attribute__ ((visibility ("default")))
62  #define EGS_FANO_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
63  #else
64  #define EGS_FANO_SOURCE_EXPORT
65  #define EGS_FANO_SOURCE_LOCAL
66  #endif
67 
68 #endif
69 
95 class EGS_FANO_SOURCE_EXPORT EGS_FanoSource :
96  public EGS_BaseSimpleSource {
97 
98 public:
99 
106  EGS_BaseGeometry *geometry,
107  const string &Name="", EGS_ObjectFactory *f=0) :
108  EGS_BaseSimpleSource(Q,Spec,Name,f), shape(Shape),
109  min_theta(85.), max_theta(95.), min_phi(0), max_phi(2*M_PI),
110  buf_1(1), buf_2(-1),
111  geom(geometry), regions(0), nrs(0) {
112  setUp();
113  };
114 
120  ~EGS_FanoSource() {
121  egsWarning("destructing Fano source\n");
123  if (geom) {
124  if (!geom->deref()) {
125  delete geom;
126  }
127  }
128  if (nrs > 0 && regions) {
129  delete [] regions;
130  }
131  };
132 
134  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
135  bool ok = true, okfano = false;
136  wt = 1;
137  do {
138  do {
139  x = shape->getRandomPoint(rndm);
140  ok = geom->isInside(x);
141  if (ok) {
142  /*******************************************************************************
143  * Rejection technique generates particles proportional to the region's mass m.
144  * The joint probability of selecting the emission point is the product of the
145  * probability of the point being in volume V times the probability of surviving
146  * the rejection, which is proportional to the density in that volume.
147  *******************************************************************************/
148  if (rndm->getUniform()*max_mass_density > geom->getMediumRho(geom->medium(geom->isWhere(x)))) {
149  okfano = false;
150  }
151  else {
152  okfano = true;
153  }
154 
155  /*********************************************************************
156  * Rather than rejecting, accept always and adjust weight accordingly
157  * A quick check didn't show improved efficiency as one would have expected!
158  * Perhaps the extra calculations? One should have used the values above!
159  *********************************************************************/
160  //wt = wt * geom->getMediumRho(geom->medium(geom->isWhere(x)))/max_mass_density;
161  //real_count += wt;
162  //okfano = true;
163  }
164  }
165  while (!ok);
166  }
167  while (!okfano);
168  u.z = buf_1 - rndm->getUniform()*(buf_1 - buf_2);
169  EGS_Float sinz = 1-u.z*u.z;
170  if (sinz > epsilon) {
171  sinz = sqrt(sinz);
172  EGS_Float cphi, sphi;
173  EGS_Float phi = min_phi + (max_phi - min_phi)*rndm->getUniform();
174  cphi = cos(phi);
175  sphi = sin(phi);
176  u.x = sinz*cphi;
177  u.y = sinz*sphi;
178  }
179  else {
180  u.x = 0;
181  u.y = 0;
182  }
183  };
184 
185  EGS_Float getFluence() const {
186  return count;
187  };
188 
189  /**************************************************
190  * Related to the implementation without rejection
191  * To be further tested !!!
192  **************************************************/
193  //EGS_Float getFluence() const { return Fano_source ? real_count : count; };
194  /*
195  bool storeFluenceState(ostream & data) const {
196  if (Fano_source) data << real_count;
197  return Fano_source ? data.good() : true;
198  };
199 
200  bool setFluenceState(istream & data) {
201  if (Fano_source) data >> real_count;
202  return Fano_source ? data.good() : true;
203  };
204 
205  bool addFluenceData(istream &data) {
206  if (Fano_source){
207  EGS_Float tmp; data >> tmp;
208  if( !data.good() ) return false;
209  real_count += tmp;
210  }
211  return true;
212  };
213 
214  void resetFluenceCounter() {real_count = 0.0; };
215  */
216  /*****************************************************/
217 
218  bool isValid() const {
219  return (s != 0 && shape != 0);
220  };
221 
222 protected:
223 
224  EGS_BaseShape *shape;
225  EGS_BaseGeometry *geom;
226  int *regions;
227 
228  void setUp();
229 
230  EGS_Float min_theta, max_theta;
231  EGS_Float buf_1, buf_2;
232  EGS_Float min_phi, max_phi;
233  //real_count;
234  EGS_Float max_mass_density;
235  int nrs;
236 };
237 
238 
239 #endif
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
EGS_BaseSource class header file.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
EGS_Float y
y-component
Definition: egs_vector.h:61
A class representing 3D vectors.
Definition: egs_vector.h:56
static void deleteObject(EGS_Object *o)
Delete an object.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:112
EGS_I64 count
Number of statistically independent particles delivered so far.
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_Float z
z-component
Definition: egs_vector.h:62
EGS_RandomGenerator class header file.
A Fano source.
EGS_BaseShape and shape classes header file.
An object factory.
Attempts to fix broken math header files.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
EGS_Float min_phi
avoid multi-calculating cos(min_theta) and cos(max_theta)
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
EGS_Float x
x-component
Definition: egs_vector.h:60
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:61
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
Base class for energy spectra. All energy spectra in the EGSnrc C++ class library are derived from th...
EGS_BaseGeometry class header file.
Base class for &#39;simple&#39; particle sources.
virtual bool isValid() const
Is this a valid source?
EGS_FanoSource(int Q, EGS_BaseSpectrum *Spec, EGS_BaseShape *Shape, EGS_BaseGeometry *geometry, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.