Skip to content

Commit

Permalink
Fix #1065: source collection fluence bug
Browse files Browse the repository at this point in the history
Fix a bug where the source collection fluence was not calculated
properly when multiple transformations were performed on a single base
source.
  • Loading branch information
Blake Walters authored and ftessier committed Sep 4, 2024
1 parent 1260b99 commit 6c738bf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# Author: Iwan Kawrakow, 2005
#
# Contributors: Marc Chamberland
# Blake Walters
#
###############################################################################
*/
Expand Down Expand Up @@ -96,6 +97,8 @@ void EGS_SourceCollection::setUp(const vector<EGS_BaseSource *> &S,
description = "Invalid source collection";
if (isValid()) {
p = new EGS_Float [nsource];
last_flu = new EGS_Float [nsource];
p_group = new vector<EGS_I64> [nsource];
sources = new EGS_BaseSource* [nsource];
Emax = 0;
for (int j=0; j<nsource; j++) {
Expand Down Expand Up @@ -126,7 +129,9 @@ void EGS_SourceCollection::setUp(const vector<EGS_BaseSource *> &S,
last_cases = new EGS_I64 [ nsource ];
for (int i=0; i<nsource; i++) {
last_cases[i] = 0;
last_flu[i]=0.;
}
i_add = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#
# Contributors: Reid Townson
# Marc Chamberland
# Blake Walters
#
###############################################################################
*/
Expand All @@ -43,6 +44,8 @@
#include "egs_rndm.h"
#include "egs_alias_table.h"

#include <algorithm>

#ifdef WIN32

#ifdef BUILD_SOURCE_COLLECTION_DLL
Expand Down Expand Up @@ -148,6 +151,8 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
delete table;
delete [] p;
delete [] last_cases;
delete [] last_flu;
delete [] p_group;
}
};

Expand All @@ -158,6 +163,21 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
EGS_I64 this_case = sources[j]->getNextParticle(rndm,q,latch,E,wt,x,u);
count += this_case - last_cases[j];
last_cases[j] = this_case;
for (int i=0; i<nsource; i++) {
//prevent "false" fluence counts in case of a collection consisting of multiple
//transformations of a common base source using a vector that, for each source,
//stores all other sources sharing the same base source
//use fluence increments in the unselected sources to detect this
if (i != j && sources[i]->getFluence() > last_flu[i]) {
if (std::find(p_group[i].begin(), p_group[i].end(), j) == p_group[i].end()) {
p_group[i].push_back(j);
p_group[j].push_back(i);
}
//set last_case for this source equal to that for source j
last_cases[i]=last_cases[j];
}
last_flu[i] = sources[i]->getFluence();
}
return count;
};
EGS_Float getEmax() const {
Expand All @@ -166,7 +186,18 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
EGS_Float getFluence() const {
EGS_Float flu = 0;
for (int j=0; j<nsource; j++) {
flu += sources[j]->getFluence();
EGS_Float p_tot=p[j];
for (int i=0; i<p_group[j].size(); i++) {
p_tot += p[p_group[j][i]];
}
int norm = 1;
//if we've combined results where a group of sources using a single base source
//is used, then at this point the summed fluence for EACH source is actually multiplied
//by the number of sources in the group
if (i_add) {
norm = p_group[j].size()+1;
}
flu += p[j]/p_tot*sources[j]->getFluence()/norm;
}
return flu;
};
Expand All @@ -186,6 +217,11 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
return res;
}
data << " ";
for (int i=0; i<p_group[j].size(); i++) {
data << p_group[j][i] << " ";
}
// use -1 to denote end of group sharing a common base source
data << -1 << " ";
if (!sources[j]->storeState(data)) {
return false;
}
Expand All @@ -206,9 +242,18 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
if (!res) {
return res;
}
EGS_I64 tmp_int;
data >> tmp_int;
while (tmp_int != -1) {
if (std::find(p_group[j].begin(), p_group[j].end(), tmp_int) == p_group[j].end()) {
p_group[j].push_back(tmp_int);
}
data >> tmp_int;
}
if (!sources[j]->setState(data)) {
return false;
}
last_flu[j]=sources[j]->getFluence();
}
return true;
}
Expand All @@ -218,6 +263,7 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
count = 0;
for (int j=0; j<nsource; ++j) {
last_cases[j] = 0;
last_flu[j] = 0;
sources[j]->resetCounter();
}
};
Expand All @@ -239,10 +285,20 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
return res;
}
last_cases[j] += tmp;
EGS_I64 tmp_int;
data_in >> tmp_int;
while (tmp_int != -1) {
if (std::find(p_group[j].begin(), p_group[j].end(), tmp_int) == p_group[j].end()) {
p_group[j].push_back(tmp_int);
}
data_in >> tmp_int;
}
if (!sources[j]->addState(data_in)) {
return false;
}
last_flu[j]=sources[j]->getFluence();
}
i_add = true;
return true;
};

Expand All @@ -263,8 +319,11 @@ class EGS_SOURCE_COLLECTION_EXPORT EGS_SourceCollection :
EGS_SimpleAliasTable *table; //!< Alias table for randomly picking a source
EGS_I64 *last_cases;//!< Last case returned from each source
EGS_Float *p; //!< The probabilities
EGS_Float *last_flu; //!< Saved value of source_flu
EGS_Float Emax; //!< Maximum energy (max of s[j]->getEmax()).
EGS_I64 count; //!< Independent particles delivered
vector<EGS_I64> *p_group; //!< Vector of sources using the same base source
bool i_add; //!< Set to true if parallel results have been combined

void setUp(const vector<EGS_BaseSource *> &S, const vector<EGS_Float> &);

Expand Down

0 comments on commit 6c738bf

Please sign in to comment.