EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_source_collection.cpp
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ source collection
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: Marc Chamberland
27 # Blake Walters
28 #
29 ###############################################################################
30 */
31 
32 
38 #include "egs_source_collection.h"
39 #include "egs_input.h"
40 
42  EGS_ObjectFactory *f) : EGS_BaseSource(input,f), nsource(0), count(0) {
43  vector<EGS_BaseSource *> s;
44  egsInformation("EGS_SourceCollection::EGS_BaseSource: input is:\n");
45  input->print(0,cout);
46  EGS_Input *isource;
47  while ((isource = input->takeInputItem("source",false))) {
48  egsInformation("EGS_SourceCollection: got input\n");
49  EGS_BaseSource *this_source = EGS_BaseSource::createSource(isource);
50  if (!this_source) {
51  egsWarning("EGS_SourceCollection: got null source\n");
52  }
53  else {
54  s.push_back(this_source);
55  }
56  delete isource;
57  }
58  vector<string> snames;
59  int err = input->getInput("source names",snames);
60  if (!err) {
61  for (unsigned int j=0; j<snames.size(); j++) {
62  EGS_BaseSource *this_source = EGS_BaseSource::getSource(snames[j]);
63  if (!this_source) {
64  egsWarning("EGS_SourceCollection: got null source\n");
65  }
66  else {
67  s.push_back(this_source);
68  }
69  }
70  }
71  if (s.size() < 1) {
72  egsWarning("EGS_SourceCollection: no sources\n");
73  return;
74  }
75  vector<EGS_Float> prob;
76  err = input->getInput("weights",prob);
77  if (err) {
78  egsWarning("EGS_SourceCollection: missing 'weights' input\n");
79  return;
80  }
81  if (prob.size() != s.size()) {
82  egsWarning("EGS_SourceCollection: the number of sources (%d) is not"
83  " the same as the number of input probabilities (%d)\n",
84  s.size(),prob.size());
85  return;
86  }
87  setUp(s,prob);
88 }
89 
90 void EGS_SourceCollection::setUp(const vector<EGS_BaseSource *> &S,
91  const vector<EGS_Float> &prob) {
92  otype = "EGS_SourceCollection";
93  nsource = S.size();
94  if (prob.size() < nsource) {
95  nsource = prob.size();
96  }
97  description = "Invalid source collection";
98  if (isValid()) {
99  p = new EGS_Float [nsource];
100  last_flu = new EGS_Float [nsource];
101  p_group = new vector<EGS_I64> [nsource];
102  sources = new EGS_BaseSource* [nsource];
103  Emax = 0;
104  for (int j=0; j<nsource; j++) {
105  p[j] = prob[j];
106  sources[j] = S[j];
107  if (p[j] < 0 || !sources[j]) {
108  if (p[j] < 0) egsWarning("EGS_SourceCollection: input "
109  "probability p[%d]=%g is less than zero.\n",j,p[j]);
110  else {
111  egsWarning("EGS_SourceCollection: source %d is null\n",j);
112  }
113  delete [] p;
114  for (int i=0; i<j; j++) {
116  }
117  delete [] sources;
118  nsource = 0;
119  return;
120  }
121  sources[j]->ref();
122  EGS_Float e = sources[j]->getEmax();
123  if (e > Emax) {
124  Emax = e;
125  }
126  }
127  table = new EGS_SimpleAliasTable(nsource,p);
128  description = "Source collection";
129  last_cases = new EGS_I64 [ nsource ];
130  for (int i=0; i<nsource; i++) {
131  last_cases[i] = 0;
132  last_flu[i]=0.;
133  }
134  i_add = false;
135  }
136 }
137 
144  hasdynamic = false;
145  for (int j=0; j<nsource; j++) {
146  bool sourceContainsDynamic = false;
147  sources[j]->containsDynamic(sourceContainsDynamic);
148  if (sourceContainsDynamic) {
149  hasdynamic = true;
150  return;
151  }
152  }
153 }
154 
155 extern "C" {
156 
157  EGS_SOURCE_COLLECTION_EXPORT EGS_BaseSource *createSource(EGS_Input *input,
158  EGS_ObjectFactory *f) {
159  return
160  createSourceTemplate<EGS_SourceCollection>(input,f,"source collection");
161  }
162 
163 }
Base source class. All particle sources must be derived from this class.
static EGS_BaseSource * getSource(const string &Name)
Get a pointer to the source named Name.
virtual EGS_Float getEmax() const =0
Return the maximum energy of this source.
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:226
void print(int nind, ostream &)
Used for debugging purposes.
Definition: egs_input.cpp:1174
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
An object factory.
int ref()
Increase the reference count to this object.
static void deleteObject(EGS_Object *o)
Delete an object.
string otype
The object type.
A class for sampling random bins from a given probability distribution using the alias table techniqu...
EGS_Float * last_flu
Saved value of source_flu.
EGS_SimpleAliasTable * table
Alias table for randomly picking a source.
EGS_SourceCollection(const vector< EGS_BaseSource * > &S, const vector< EGS_Float > &prob, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
bool i_add
Set to true if parallel results have been combined.
EGS_Float Emax
Maximum energy (max of s[j]->getEmax()).
vector< EGS_I64 > * p_group
Vector of sources using the same base source.
EGS_BaseSource ** sources
The sources in the collection.
EGS_Float * p
The probabilities.
void containsDynamic(bool &hasdynamic)
Check if the simulation source contains time indices.
EGS_I64 * last_cases
Last case returned from each source.
EGS_Input class header file.
A source collection.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.