39 #include "iaea_phsp.h"
44 static bool IAEA_PHSP_SOURCE_LOCAL inputSet =
false;
52 void IAEA_PhspSource::init() {
53 otype =
"IAEA_PhspSource";
79 void IAEA_PhspSource::openFile(
const string &phsp_file) {
88 int len = phsp_file.length();
89 char phsp_name_tmp[len+1];
90 phsp_file.copy(phsp_name_tmp,len,0);
92 phsp_name_tmp[len]=
'\0';
97 egsWarning(
"IAEA_PhspSource::openFile: no header file name supplied.\n");
101 egsWarning(
"IAEA_PhspSource::openFile: failed to open header file %s.IAEAheader\n"
102 " for reading\n",phsp_file.c_str());
106 egsWarning(
"IAEA_PhspSource::openFile: failed to open phase space file %s.IAEAphsp\n"
107 " for reading\n",phsp_file.c_str());
111 egsWarning(
"IAEA_PhspSource::openFile: failed to initialize phase space source %s\n",phsp_file.c_str());
115 egsWarning(
"IAEA_PhspSource::openFile: failed to get record contents from header file %s.IAEAheader\n",phsp_file.c_str());
119 egsWarning(
"IAEA_PhspSource::openFile: I/O error ocurred on opening phase space file %s\n",phsp_file.c_str());
128 egsWarning(
"IAEA_PhspSource::openFile: error reading file size/byte order from header %s.IAEAheader\n",phsp_file.c_str());
132 egsWarning(
"IAEA_PhspSource::openFile: byte mismatch between phase space file and machine. Will swap bytes.\n");
136 egsWarning(
"IAEA_PhspSource::openFile: mismatch between file size in header and actual file size of %s\n",phsp_file.c_str());
141 EGS_I64 n, n_photon, pinc;
144 iaea_get_max_particles(&
iaea_fileid,&iaea_type,&n);
146 egsWarning(
"IAEA_PhspSource::openFile: failed to get total no. of particles from %s.IAEAheader\n",phsp_file.c_str());
150 iaea_get_max_particles(&
iaea_fileid,&iaea_type,&n_photon);
152 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of photons from %s.IAEAheader\n",phsp_file.c_str());
158 egsWarning(
"IAEA_PhspSource::openFile: failed to get max. energy from %s.IAEAheader\n",phsp_file.c_str());
162 iaea_get_total_original_particles(&
iaea_fileid,&pinc);
164 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of original particles from %s.IAEAheader\n",phsp_file.c_str());
171 egsWarning(
"IAEA_PhspSource::openFile: failed to get no. of extra floats and longs stored in %s.IAEAphsp\n",phsp_file.c_str());
175 int extrafloat_types[MAXEXTRAS], extralong_types[MAXEXTRAS];
178 egsWarning(
"IAEA_PhspSource::openFile: failed to get Mode of data %s.IAEAheader\n",phsp_file.c_str());
184 if (extrafloat_types[i]==3) {
196 if (extrafloat_types[
i_zlast+1] == 0) {
199 egsInformation(
"IAEA_PhspSource::openFile: Time index included in data in %s.IAEAphsp\n",phsp_file.c_str());
206 if (extralong_types[i]==2) {
213 egsWarning(
"IAEA_PhspSource::openFile: LATCH is not stored in data in %s.IAEAphsp\n",phsp_file.c_str());
232 int err = input->
getInput(
"iaea phase space file",fname);
234 egsWarning(
"IAEA_PhspSource: no 'iaea phase space file' input\n");
239 egsWarning(
"IAEA_PhspSource: errors while opening the phase space file"
240 " %s\n",fname.c_str());
243 vector<EGS_Float> cutout;
244 err = input->
getInput(
"cutout",cutout);
245 if (!err && cutout.size() == 4) {
246 setCutout(cutout[0],cutout[1],cutout[2],cutout[3]);
248 vector<string> ptype;
249 ptype.push_back(
"electrons");
250 ptype.push_back(
"photons");
251 ptype.push_back(
"positrons");
252 ptype.push_back(
"all");
253 ptype.push_back(
"charged");
254 particle_type = input->
getInput(
"particle type",ptype,3)-1;
255 vector<int> the_filter;
256 vector<string> the_latch;
257 err = input->
getInput(
"filter type",the_filter);
258 int err1 = input->
getInput(
"latch setting",the_latch);
260 if (the_filter[0] >= 0 && the_filter[0] <= 3) {
261 int nbit1 = the_latch[0].size();
263 if (!the_filter[0]) {
264 nbit2 = the_latch[1].size();
266 if (nbit1 + nbit2 > 0) {
267 int *the_bits =
new int [nbit1+nbit2];
268 for (
int j=0; j<nbit1+nbit2; j++) {
270 the_bits[j]=(int)(the_latch[0][j] ==
'1');
273 the_bits[j] = (int)(the_latch[1][j-nbit1] ==
'1');
276 setFilter(the_filter[0],nbit1,nbit2,the_bits);
281 vector<EGS_Float> wwindow;
282 err = input->
getInput(
"weight window",wwindow);
283 if (!err && wwindow.size() == 2) {
288 err = input->
getInput(
"reuse photons",ntmp);
289 if (!err && ntmp > 0) {
293 err = input->
getInput(
"recycle photons",ntmp);
294 if (!err && ntmp > 0) {
298 err = input->
getInput(
"reuse electrons",ntmp);
299 if (!err && ntmp > 0) {
303 err = input->
getInput(
"recycle electrons",ntmp);
304 if (!err && ntmp > 0) {
319 int nstat,extrainttemp[MAXEXTRAS];
320 float extrafloattemp[MAXEXTRAS];
323 egsWarning(
"IAEA_PhspSource::getNextParticle(): reached the end of the "
324 "phase space file chunk (%lld)\n will start from the beginning "
325 "of the chunk (%lld) but this "
326 "implies that uncertainty estimates will be inaccurate\n",
330 egsFatal(
"IAEA_PhspSource::getNextParticle(): error rewinding phase space chunk\n");
335 iaea_get_particle(&
iaea_fileid,&nstat,&p.q,&p.E,&p.wt,&p.x,&p.y,&p.z,&p.u,&p.v,&p.w,extrafloattemp,extrainttemp);
344 p.zlast = extrafloattemp[
i_zlast];
347 p.time = extrafloattemp[
i_time];
348 setTimeIndex(p.time);
372 egsFatal(
"IAEA_PhspSource::getNextParticle(): error reading particle number %i\n",
Npos);
385 egsFatal(
"IAEA_PhspSource::getNextParticle: unknown charge on particle\n");
388 if (first || nstat>0) {
409 u.
y = p.v, u.
z = p.w;
411 if (rejectParticle()) {
435 void IAEA_PhspSource::setSimulationChunk(EGS_I64 nstart, EGS_I64 nrun,
int npar,
int nchunk) {
438 EGS_I64 particlesPerChunk =
Nparticle/(npar*nchunk);
439 int ichunk = nstart/nrun;
440 if (ichunk > npar*nchunk-1) {
443 egsInformation(
"IAEA_PhspSource: Remainder of histories will reuse the last chunk of the phase space source.\n");
444 ichunk = npar*nchunk-1;
446 Nfirst = ichunk*particlesPerChunk+1;
447 if (ichunk == npar*nchunk-1) {
457 egsWarning(
"IAEA_PhspSource::setSimulationChunk(): error setting phase space chunk\n");
460 egsInformation(
"IAEA_PhspSource: using phsp portion between %lld and %lld\n",
464 bool IAEA_PhspSource::rejectParticle()
const {
465 if (particle_type < 2 && p.q != particle_type) {
468 if (particle_type == 3 && !p.q) {
471 if (p.x < Xmin || p.x > Xmax || p.y < Ymin || p.y > Ymax) {
474 if (p.wt < wmin || p.wt > wmax) {
477 if (p.latch & 2147483648UL) {
480 if (filter_type < 0) {
483 if (filter_type == 1) {
486 r1 = !(p.latch & filter1);
491 bool r2 = (p.latch & filter2);
494 if (filter_type == 1) {
495 return (p.latch & filter1);
497 int l = p.latch >> 24;
498 bool res = l & filter1;
499 return filter_type == 3 ? res : !res;
502 void IAEA_PhspSource::setFilter(
int type,
int nbit1,
int nbit2,
const int *bits) {
503 if (type < 0 || type > 3) {
504 egsWarning(
"IAEA_PhspSource::setFilter: invalid filter type %d\n",type);
512 egsWarning(
"IAEA_PhspSource::setFilter: maximum number of bits is "
513 "limited to 29, you requested %d\n",ntot);
520 if (filter_type == 0) {
524 for (i=0; i<ntot; i++) {
526 unsigned int aux = 1;
537 else if (filter_type == 1) {
539 else if (filter_type == 2) {
541 else if (filter_type == 3) {
556 static void setInputs() {
559 setBaseSourceInputs(
false,
false);
561 srcBlockInput->getSingleInput(
"library")->setValues({
"iaea_phsp_source"});
564 srcBlockInput->addSingleInput(
"iaea phase space file",
true,
"The path to and name of the phase-space file, no extension. Both the .IAEAphsp and .IAEAheader file must be in the same directory.");
565 srcBlockInput->addSingleInput(
"particle type",
true,
"The type of particle to keep from the phase-space. Other types are discarded.", {
"all",
"charged",
"electrons",
"positrons",
"photons"});
566 srcBlockInput->addSingleInput(
"cutout",
false,
"Discard particles outside of a rectanglular field: 'x1 y1 x2 y2'");
567 srcBlockInput->addSingleInput(
"weight window",
false,
"A weight window, outside of which particles are discarded: 'wtmin wtmax'. This allows you to discard high weight particles.");
568 srcBlockInput->addSingleInput(
"recycle photons",
false,
"The number of time to recycle each photon. E.g. set to 1 to use each photon twice. Set <1 or neglect to disable.");
569 srcBlockInput->addSingleInput(
"recycle electrons",
false,
"The number of time to recycle each electron. E.g. set to 1 to use each electron twice. Set <1 or neglect to disable.");
572 IAEA_PHSP_SOURCE_EXPORT
string getExample() {
576 # Example of iaea_phsp_soure
579 library = iaea_phsp_source
580 iaea phase space file = myPhsp # No extension, both .IAEAphsp and .IAEAheader must be present
584 recycle electrons = 10
590 IAEA_PHSP_SOURCE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
594 return srcBlockInput;
600 createSourceTemplate<IAEA_PhspSource>(input,f,
"iaea phsp source");
Base source class. All particle sources must be derived from this class.
string description
A short source description.
string otype
The object type.
Base random number generator class. All random number generators should be derived from this class.
A class representing 3D vectors.
EGS_I64 Npos
Next record to be read.
int Nrecycle_g
Number of times to recycle a photon.
string the_file_name
The phase-space file name.
EGS_I64 Nlast
Last record this source can use.
EGS_I64 Nphoton
Number of photons in the file.
int i_latch
index of latch in extra_floats array
int Nuse
Number of times current particle was used so far.
EGS_I64 Nread
Number of particles read so far.
EGS_I64 Nfirst
first record this source can use
int Nrecycle
Number of times to recycle current particle.
bool time_stored
true if time index stored
int iaea_fileid
phsp file unit no.
int n_extra_floats
no. of extra floats stored in phsp file
int i_zlast
index of zlast in extra_floats array
int i_time
index of time index in extra_floats array
int Nrewind
Number of times the phsp file was rewinded.
EGS_Float Pinc
Number of incident particles that created the file.
EGS_I64 Nparticle
Number of particles in the file.
bool mode2
true, if a MODE2 file (i.e. storing Zlast)
int n_extra_longs
no. of extra longs stored in phsp file
IAEA_PhspSource(const string &phsp_file, const string &Name="", EGS_ObjectFactory *f=0)
Constructor.
int Nrecycle_e
Number of times to recycle a charged particle.
bool latch_stored
true if LATCH is stored in data
EGS_Float Emax
Maximum k.e. (obtained from the phsp file)
int iaea_iostat
iostat on read/write of iaea phsp file
void containsDynamic(bool &hasdynamic)
Check if the simulation source contains time indices.
Global egspp functions header file.
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 egsFatal
Always use this function for reporting fatal errors.
void egsSwapBytes(int *n)
Swap the bytes of 32 bit integers.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.
const EGS_Float veryFar
A very large float.
An IAEA format phase-space file source.