EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_isotropic_source.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ isotropic source headers
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 # Frederic Tessier
28 # Reid Townson
29 # Ernesto Mainegra-Hing
30 # Hugo Bouchard
31 # Hubert Ho
32 #
33 ###############################################################################
34 */
35 
36 
42 #ifndef EGS_ISOTROPIC_SOURCE_
43 #define EGS_ISOTROPIC_SOURCE_
44 
45 #include "egs_vector.h"
46 #include "egs_base_source.h"
47 #include "egs_rndm.h"
48 #include "egs_shapes.h"
49 #include "egs_base_geometry.h"
50 #include "egs_math.h"
51 
52 
53 #ifdef WIN32
54 
55  #ifdef BUILD_ISOTROPIC_SOURCE_DLL
56  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllexport)
57  #else
58  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllimport)
59  #endif
60  #define EGS_ISOTROPIC_SOURCE_LOCAL
61 
62 #else
63 
64  #ifdef HAVE_VISIBILITY
65  #define EGS_ISOTROPIC_SOURCE_EXPORT __attribute__ ((visibility ("default")))
66  #define EGS_ISOTROPIC_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
67  #else
68  #define EGS_ISOTROPIC_SOURCE_EXPORT
69  #define EGS_ISOTROPIC_SOURCE_LOCAL
70  #endif
71 
72 #endif
73 
222 class EGS_ISOTROPIC_SOURCE_EXPORT EGS_IsotropicSource :
223  public EGS_BaseSimpleSource {
224 
225 public:
226 
229  IncludeAll = 0,
230  ExcludeAll = 1,
231  IncludeSelected = 2,
232  ExcludeSelected = 3
233  };
234 
241  EGS_BaseGeometry *geometry,
242  const string &Name="", EGS_ObjectFactory *f=0) :
243  EGS_BaseSimpleSource(Q,Spec,Name,f), shape(Shape),
244  geom(geometry), regions(0), min_theta(85.), max_theta(95.),
245  buf_1(1), buf_2(-1), min_phi(0), max_phi(2*M_PI),
246  nrs(0), gc(IncludeAll) {
247  setUp();
248  };
249 
257  if (geom) {
258  if (!geom->deref()) {
259  delete geom;
260  }
261  }
262  if (nrs > 0 && regions) {
263  delete [] regions;
264  }
265  };
266 
268  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
269  bool ok = true;
270  do {
271  x = shape->getRandomPoint(rndm);
272  if (geom) {
273  if (gc == IncludeAll) {
274  ok = geom->isInside(x);
275  }
276  else if (gc == ExcludeAll) {
277  ok = !geom->isInside(x);
278  }
279  else if (gc == IncludeSelected) {
280  ok = false;
281  int ireg = geom->isWhere(x);
282  for (int j=0; j<nrs; ++j) {
283  if (ireg == regions[j]) {
284  ok = true;
285  break;
286  }
287  }
288  }
289  else {
290  ok = true;
291  int ireg = geom->isWhere(x);
292  for (int j=0; j<nrs; ++j) {
293  if (ireg == regions[j]) {
294  ok = false;
295  break;
296  }
297  }
298  }
299  }
300  }
301  while (!ok);
302  u.z = buf_1 - rndm->getUniform()*(buf_1 - buf_2);
303  EGS_Float sinz = 1-u.z*u.z;
304  if (sinz > epsilon) {
305  sinz = sqrt(sinz);
306  EGS_Float cphi, sphi;
307  EGS_Float phi = min_phi +(max_phi - min_phi)*rndm->getUniform();
308  cphi = cos(phi);
309  sphi = sin(phi);
310  u.x = sinz*cphi;
311  u.y = sinz*sphi;
312  }
313  else {
314  u.x = 0;
315  u.y = 0;
316  }
317  wt = 1;
318  };
319 
320  EGS_Float getFluence() const {
321  return count;
322  };
323 
324  bool storeFluenceState(ostream &) const {
325  return true;
326  };
327 
328  bool setFluenceState(istream &) {
329  return true;
330  };
331 
332  bool isValid() const {
333  return (s != 0 && shape != 0);
334  };
335 
336 protected:
337 
338  EGS_BaseShape *shape;
339  EGS_BaseGeometry *geom;
340  int *regions;
341 
342  void setUp();
343 
344  EGS_Float min_theta, max_theta;
345  EGS_Float buf_1, buf_2;
346  EGS_Float min_phi, max_phi;
347 
348  int nrs;
350 };
351 
352 #endif
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
GeometryConfinement
Geometry confinement options.
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.
EGS_IsotropicSource(int Q, EGS_BaseSpectrum *Spec, EGS_BaseShape *Shape, EGS_BaseGeometry *geometry, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
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.
EGS_BaseShape and shape classes header file.
An isotropic source.
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 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
virtual bool storeFluenceState(ostream &data_out) const
Store the fluence state of this source to the data stream data_out.
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:61
EGS_Float min_phi
avoid multi-calculating cos(min_theta) and cos(max_theta)
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?
virtual bool setFluenceState(istream &data)
Set the data related to the sampling of positions and directions to a state contained in the stream d...