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 # Marc Chamberland
33 # Reid Townson
34 #
35 ###############################################################################
36 */
37 
38 
44 #ifndef EGS_ISOTROPIC_SOURCE_
45 #define EGS_ISOTROPIC_SOURCE_
46 
47 #include "egs_vector.h"
48 #include "egs_base_source.h"
49 #include "egs_rndm.h"
50 #include "egs_shapes.h"
51 #include "egs_base_geometry.h"
52 #include "egs_math.h"
53 
54 
55 #ifdef WIN32
56 
57  #ifdef BUILD_ISOTROPIC_SOURCE_DLL
58  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllexport)
59  #else
60  #define EGS_ISOTROPIC_SOURCE_EXPORT __declspec(dllimport)
61  #endif
62  #define EGS_ISOTROPIC_SOURCE_LOCAL
63 
64 #else
65 
66  #ifdef HAVE_VISIBILITY
67  #define EGS_ISOTROPIC_SOURCE_EXPORT __attribute__ ((visibility ("default")))
68  #define EGS_ISOTROPIC_SOURCE_LOCAL __attribute__ ((visibility ("hidden")))
69  #else
70  #define EGS_ISOTROPIC_SOURCE_EXPORT
71  #define EGS_ISOTROPIC_SOURCE_LOCAL
72  #endif
73 
74 #endif
75 
238 class EGS_ISOTROPIC_SOURCE_EXPORT EGS_IsotropicSource :
239  public EGS_BaseSimpleSource {
240 
241 public:
242 
245  IncludeAll = 0,
246  ExcludeAll = 1,
247  IncludeSelected = 2,
248  ExcludeSelected = 3
249  };
250 
257  EGS_BaseGeometry *geometry,
258  const string &Name="", EGS_ObjectFactory *f=0) :
259  EGS_BaseSimpleSource(Q,Spec,Name,f), shape(Shape),
260  geom(geometry), regions(0), min_theta(85.), max_theta(95.),
261  buf_1(1), buf_2(-1), min_phi(0), max_phi(2*M_PI),
262  nrs(0), gc(IncludeAll), media(0), nms(0), gcm(IncludeSelected) {
263  setUp();
264  };
265 
273  if (geom) {
274  if (!geom->deref()) {
275  delete geom;
276  }
277  }
278  if (nrs > 0 && regions) {
279  delete [] regions;
280  }
281  if (nms > 0 && media) {
282  delete [] media;
283  }
284  };
285 
287  EGS_Vector &x, EGS_Vector &u, EGS_Float &wt) {
288  bool ok = true;
289  bool ok2 = true;
290  do {
291  x = shape->getRandomPoint(rndm);
292  if (geom) {
293  // Filter by region
294  if (gc == IncludeAll) {
295  ok = geom->isInside(x);
296  }
297  else if (gc == ExcludeAll) {
298  ok = !geom->isInside(x);
299  }
300  else if (gc == IncludeSelected) {
301  ok = false;
302  int ireg = geom->isWhere(x);
303  for (int j=0; j<nrs; ++j) {
304  if (ireg == regions[j]) {
305  ok = true;
306  break;
307  }
308  }
309  }
310  else if (gc == ExcludeSelected) {
311  ok = true;
312  int ireg = geom->isWhere(x);
313  for (int j=0; j<nrs; ++j) {
314  if (ireg == regions[j]) {
315  ok = false;
316  break;
317  }
318  }
319  }
320 
321  // Filter by medium
322  if (ok && nms > 0) {
323  if (gcm == IncludeSelected) {
324  ok2 = false;
325  int ireg = geom->isWhere(x);
326  int currentMedium = geom->medium(ireg);
327  for (int j=0; j<nms; ++j) {
328  if (currentMedium == media[j]) {
329  ok2 = true;
330  break;
331  }
332  }
333  }
334  else if (gcm == ExcludeSelected) {
335  ok2 = true;
336  int ireg = geom->isWhere(x);
337  for (int j=0; j<nrs; ++j) {
338  if (ireg == media[j]) {
339  ok2 = false;
340  break;
341  }
342  }
343  }
344  }
345 
346  // Combine the filtering by region and medium
347  if (ok2 == false) {
348  ok = false;
349  }
350  }
351  }
352  while (!ok);
353  u.z = buf_1 - rndm->getUniform()*(buf_1 - buf_2);
354  EGS_Float sinz = 1-u.z*u.z;
355  if (sinz > epsilon) {
356  sinz = sqrt(sinz);
357  EGS_Float cphi, sphi;
358  EGS_Float phi = min_phi +(max_phi - min_phi)*rndm->getUniform();
359  cphi = cos(phi);
360  sphi = sin(phi);
361  u.x = sinz*cphi;
362  u.y = sinz*sphi;
363  }
364  else {
365  u.x = 0;
366  u.y = 0;
367  }
368  wt = 1;
369  };
370 
371  EGS_Float getFluence() const {
372  return count;
373  };
374 
375  bool storeFluenceState(ostream &) const {
376  return true;
377  };
378 
379  bool setFluenceState(istream &) {
380  return true;
381  };
382 
383  bool isValid() const {
384  return (s != 0 && shape != 0);
385  };
386 
387 protected:
388 
390  EGS_BaseGeometry *geom;
391  int *regions, *media;
392 
393  void setUp();
394 
395  EGS_Float min_theta, max_theta;
396  EGS_Float buf_1, buf_2;
397  EGS_Float min_phi, max_phi;
398 
399  int nrs, nms;
400  GeometryConfinement gc, gcm;
401 };
402 
403 #endif
Base geometry class. Every geometry class must be derived from EGS_BaseGeometry.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
Definition: egs_shapes.h:114
Base class for 'simple' particle sources.
virtual void getPositionDirection(EGS_RandomGenerator *rndm, EGS_Vector &x, EGS_Vector &u, EGS_Float &wt)=0
Sample a particle position and direction.
virtual bool isValid() const
Is this a valid source?
virtual bool storeFluenceState(ostream &data_out) const
Store the fluence state of this source to the data stream data_out.
virtual bool setFluenceState(istream &data)
Set the data related to the sampling of positions and directions to a state contained in the stream d...
EGS_I64 count
Number of statistically independent particles delivered so far.
virtual EGS_Float getFluence() const =0
Return the fluence this source has emitted so far.
Base class for energy spectra. All energy spectra in the EGSnrc C++ class library are derived from th...
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
An isotropic source.
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.
static void deleteObject(EGS_Object *o)
Delete an object.
Base random number generator class. All random number generators should be derived from this class.
Definition: egs_rndm.h:67
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
A class representing 3D vectors.
Definition: egs_vector.h:57
EGS_Float y
y-component
Definition: egs_vector.h:62
EGS_Float z
z-component
Definition: egs_vector.h:63
EGS_Float x
x-component
Definition: egs_vector.h:61
EGS_BaseGeometry class header file.
EGS_BaseSource class header file.
Attempts to fix broken math header files.
EGS_RandomGenerator class header file.
EGS_BaseShape and shape classes header file.
EGS_Vector methods for the manipulation of 3D vectors in cartesian co-ordinates.
const EGS_Float epsilon
The epsilon constant for floating point comparisons.
Definition: egs_functions.h:62