-
Notifications
You must be signed in to change notification settings - Fork 5
/
SandiaDecay.cpp
3567 lines (2825 loc) · 122 KB
/
SandiaDecay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SandiaDecay: a library that provides nuclear decay info and calculations.
Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.
For questions contact William Johnson via email at [email protected], or
alternative email of [email protected].
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <map>
#include <cmath>
#include <cerrno>
#include <math.h>
#include <vector>
#include <string>
#include <locale>
#include <limits>
#include <cstring>
#include <sstream>
#include <fstream>
#include <ctype.h>
#include <float.h>
#include <iostream>
#include <assert.h>
#include <stdexcept>
#include <algorithm>
// If you run with CalcFloatType==__float128 on GNU, you'll need this next
// include for `qexp(...)`, and you'll need to link against libquadmath
// #include <quadmath.h>
#include "rapidxml/rapidxml.hpp"
#include "SandiaDecay.h"
/* The sandia.decay.xml file normally has elements like <nuclide/>, <element/>
etc., but if ENABLE_SHORT_NAME is enabled, then either these element names
can be used, or much shorter ones like <n/>, <el/>, etc. Similar story for
attributes.
As of 20180106 there is no notable difference in parsing times of the original
sandia.decay.xml file if this is enabled or disabled, and the results look
identical - but leaving this compile switch in (and associated code) until
next release incase any problems arise it will help deal with.
*/
#define ENABLE_SHORT_NAME 1
/* Previous to 20201118 nuclides with no <transition /> elements in sandia.decay.xml would get left
out of the Bateman coefficients when making a SandiaDecay::NuclideMixture, so those nuclides
activities wouldn't be present in decay results. This wasn't a big deal as you wouldn't observe
those nuclides in data anyway, and only a few (around 18) nuclides has this issue, and they are all
extremely short half-lives that its questionable if they should even be in sandia.decay.xml (they
probably don't have transitions because the nuclides haven't been characterized well), but
none-the-less, its fixed up now.
I tested that all nuclides in DB decayed to 3.5 lives yields the exact same gammas with and without
this fix, and that all activities of nuclides, and their children are the same, except for the ~18
affected nuclides.
However, leaving in this fixed compile-time switch until the next release to make it easier to
debug if any issues popup.
*/
#define DO_EMPTY_TRANSITION_FIX 1
// Ignore this warning. It shouldn't be important, and seems to be Windows-only.
// warning C4244: 'initializing' : conversion from 'std::streamoff' to 'size_t', possible loss of data
#pragma warning(disable:4244)
using namespace std;
// Workaround for Windows portability.
#if ( defined(WIN32) || defined(UNDER_CE) || defined(_WIN32) || defined(WIN64) )
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#define fpu_error(x) (isinf(x) || isnan(x))
#endif
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf c99_snprintf
namespace
{
//from http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
inline int c99_snprintf(char* str, size_t size, const char* format, ...)
{
int count;
va_list ap;
va_start( ap, format );
count = c99_vsnprintf( str, size, format, ap );
va_end( ap );
return count;
}
}//namespace
#endif
#if __cplusplus > 199711L
#define IsInf(x) std::isinf(x)
#define IsNan(x) std::isnan(x)
#else
#define IsInf(x) isinf(x)
#define IsNan(x) isnan(x)
#endif
namespace
{
//Using sscanf costs 75ms over atof for opening sandia.decay.xml on late 2018
// mbp. Using sscanf would be best for error checking (I think strtod
// returns a zero for non-numbers, and does not set errno, except out of
// range, and atof is UB if not a valid float). I guess after checking the
// xml that all values properly parse, we'll go with the fastest one that
// doesnt cause UB. Using stod is about the same speed as atof
double parse_double( const char *str )
{
//return atof( str );
errno = 0;
const double answer = strtod(str,NULL);
if( errno )
throw runtime_error( "Could not convert '" + string(str) + "' to a double" );
return answer;
//double val;
//const int nread = sscanf( str, "%lf", &val );
//if( nread != 1 )
// throw runtime_error( "Could not convert '" + string(str) + "' to a double" );
//return static_cast<float>(val);
}
float parse_float( const char *str )
{
// This function is the primary bottleneck of parsing; to initualize
// SandiaDecayDataBase with sandia.decay.min.xml (i.e., with coincidences, but minimzed tags),
// on an M1 macbook it takes:
// - atof: 80ms
// - strtof: 110ms
// - sscanf: 150ms
//
// Even using atof, this function takes up almost 20% of the CPU time when constructing
// with sandia.decay.min.xml.
//
// Some notes about atof:
// - If input is not a valid float, will return 0.0; doesnt set `errno`.
// - If input is out of the range, its undefined behavior
float answer = (float)atof( str );
if( answer != 0.0 )
return answer;
// If we got zero from atof, it could actually be a zero value, or it could be an error,
// so we'll check this using strtof (we dont expect zero very often at all, and never expect
// an error, so this second check doesnt add any real time).
errno = 0;
answer = strtof(str,NULL);
if( errno )
throw runtime_error( "Could not convert '" + string(str) + "' to a float" );
return answer;
/*
float val;
const int nread = sscanf( str, "%f", &val );
if( nread != 1 )
throw runtime_error( "Could not convert '" + string(str) + "' to a float" );
return val;
*/
}
short int parse_short_int( const char *str )
{
errno = 0;
const long int answer = strtol( str, NULL, 10 );
if( errno )
throw runtime_error( "Could not convert '" + string(str) + "' to a short" );
//ToDo: make sure answer is in short int range.
return static_cast<short int>( answer );
//return (short int)atoi( str );
}
int parse_int( const char *str )
{
errno = 0;
const long int answer = strtol( str, NULL, 10 );
if( errno )
throw runtime_error( "Could not convert '" + string(str) + "' to a int" );
//ToDo: make sure int is in range.
return static_cast<int>( answer );
//return atoi( str );
}
//a struct so that we can sort an array using an extanal array of indexes,
// with out modifying the data array.
template<class T> struct index_compare_assend
{
index_compare_assend(const T arr) : arr(arr) {} //pass the actual values you want sorted into here
bool operator()(const size_t a, const size_t b) const
{
return arr[a] < arr[b];
}
const T arr;
};//struct index_compare
bool convert( const std::string& arg, SandiaDecay::ProductType &s )
{
if( arg == "beta" || arg == "b" ) s = SandiaDecay::BetaParticle;
else if( arg == "gamma" || arg == "g" ) s = SandiaDecay::GammaParticle;
else if( arg == "alpha" || arg == "a" ) s = SandiaDecay::AlphaParticle;
else if( arg == "positron" || arg == "p" ) s = SandiaDecay::PositronParticle;
else if( arg == "electronCapture" || arg == "ec" ) s = SandiaDecay::CaptureElectronParticle;
else if( arg == "xray" || arg == "x" ) s = SandiaDecay::XrayParticle;
else return false;
return true;
}//bool convert( const std::string& arg, ProductType &s )
SandiaDecay::DecayMode decaymode_fromstr( const std::string &decayMode )
{
if( decayMode == "b-" ) return SandiaDecay::BetaDecay;
else if( decayMode == "it" ) return SandiaDecay::IsometricTransitionDecay;
else if( decayMode == "b-n" ) return SandiaDecay::BetaAndNeutronDecay;
else if( decayMode == "b+" ) return SandiaDecay::BetaPlusDecay;
else if( decayMode == "ec" ) return SandiaDecay::ElectronCaptureDecay;
else if( decayMode == "b-a" ) return SandiaDecay::BetaAndAlphaDecay;
else if( decayMode == "b-2n" ) return SandiaDecay::BetaAndTwoNeutronDecay;
else if( decayMode == "ecp" ) return SandiaDecay::ElectronCaptureAndProtonDecay;
else if( decayMode == "eca" ) return SandiaDecay::ElectronCaptureAndAlphaDecay;
else if( decayMode == "b+p" ) return SandiaDecay::BetaPlusAndProtonDecay;
else if( decayMode == "ec2p" ) return SandiaDecay::ElectronCaptureAndTwoProtonDecay;
else if( decayMode == "b+2p" ) return SandiaDecay::BetaPlusAndTwoProtonDecay;
else if( decayMode == "b+3p" ) return SandiaDecay::BetaPlusAndThreeProtonDecay;
else if( decayMode == "b+a" ) return SandiaDecay::BetaPlusAndAlphaDecay;
else if( decayMode == "2b-" ) return SandiaDecay::DoubleBetaDecay;
else if( decayMode == "2ec" ) return SandiaDecay::DoubleElectronCaptureDecay;
else if( decayMode == "a" ) return SandiaDecay::AlphaDecay;
else if( decayMode == "p" ) return SandiaDecay::ProtonDecay;
else if( decayMode == "14c" ) return SandiaDecay::Carbon14Decay;
else if( decayMode == "sf" ) return SandiaDecay::SpontaneousFissionDecay;
else if( decayMode == "2p" ) return SandiaDecay::DoubleProton;
else if( decayMode == "Undefined" ) return SandiaDecay::UndefinedDecay;
return SandiaDecay::UndefinedDecay;
}//DecayMode decaymode_fromstr( const char *str )
SandiaDecay::ForbiddennessType forbiddenness_fromstr( const char *str )
{
const size_t len = strlen(str);
if( len == 1 )
{
if( str[0]=='1' )
return SandiaDecay::FirstForbidden;
if( str[0]=='2' )
return SandiaDecay::SecondForbidden;
if( str[0]=='3' )
return SandiaDecay::ThirdForbidden;
if( str[0]=='4' )
return SandiaDecay::FourthForbidden;
}else if( len==2 && str[1]=='u' )
{
if( str[0]=='1' )
return SandiaDecay::FirstUniqueForbidden;
if( str[0]=='2' )
return SandiaDecay::SecondUniqueForbidden;
if( str[0]=='3' )
return SandiaDecay::ThirdUniqueForbidden;
}
return SandiaDecay::NoForbiddenness;
}//ForbiddennessType forbiddenness_fromstr( const char *str )
/** Revision status in the source XML data for this particle.
*/
enum RevisionType
{
NoRevision,
EditedRevision,
InsertedRevision,
DeletedRevision
};//enum RevisionType
RevisionType revision_fromstr( const char *str )
{
const size_t len = strlen(str);
if( !len )
return NoRevision;
if( !strcmp(str,"edited") )
return EditedRevision;
if( !strcmp(str,"inserted") )
return InsertedRevision;
if( !strcmp(str,"deleted") )
return DeletedRevision;
return NoRevision;
}//RevisionType revision_fromstr( const char *str )
bool lessThanBySymbol( const SandiaDecay::Nuclide *lhs, const std::string &rhs )
{
if( !lhs )
return false;
return (lhs->symbol < rhs);
}//bool lessThanBySymbol( const Nuclide *lhs, const std::string &rhs )
bool pointerLessThanBySymbol( const SandiaDecay::Nuclide *lhs, const SandiaDecay::Nuclide *rhs )
{
if( !lhs )
return false;
if( !rhs )
return true;
return (lhs->symbol < rhs->symbol);
}//bool pointerLessThanBySymbol( const Nuclide *lhs, const Nuclide *lhs )
vector<const SandiaDecay::Nuclide *>::const_iterator find(
const vector<const SandiaDecay::Nuclide *> &data,
const std::string &symbol )
{
vector<const SandiaDecay::Nuclide *>::const_iterator pos
= lower_bound( data.begin(), data.end(), symbol, &lessThanBySymbol );
if( (pos == data.end()) || ((*pos)->symbol != symbol) )
return data.end();
return pos;
}//find(...)
struct BatemanWorkingSpace
{
std::vector< std::vector<const SandiaDecay::Nuclide *> > nuclide_path;
std::vector< std::vector< std::vector<SandiaDecay::CalcFloatType> > > decay_coeffs;
};//struct BatemanWorkingSpace
//The following nuclides are known to give nan or inf coeficients in
// BatemanWorkingSpace::decay_coeffs : Sm136, Pt183, Ir172, Hg179, Eu136
// (this issue was found 20121011, but not investigated further - I think
// its been fixed (20180402), but I didnt check that)
void calc_bateman_coef( std::vector< vector<SandiaDecay::CalcFloatType> > &coefs,
std::vector<const SandiaDecay::Transition *> decay_path,
BatemanWorkingSpace &ws )
{
assert( decay_path.size() );
const SandiaDecay::Transition * const trans = decay_path.back();
assert( trans );
const SandiaDecay::Nuclide * const child = trans->child;
#if( !DO_EMPTY_TRANSITION_FIX )
assert( child );
#endif
const size_t row = coefs.size();
coefs.push_back( vector<SandiaDecay::CalcFloatType>(row+1, 0.0) ); //13.5% of time
for( size_t col = 0; col < row; ++col )
{
#if( !DO_EMPTY_TRANSITION_FIX )
assert( decay_path[row-1]->child );
#endif
assert( decay_path[row-1]->parent );
const SandiaDecay::CalcFloatType lambda_iminus1 = decay_path[row-1]->parent->decayConstant();
#if( DO_EMPTY_TRANSITION_FIX )
const SandiaDecay::CalcFloatType lambda_i = (decay_path[row-1]->child ? decay_path[row-1]->child->decayConstant() : 0.0);
#else
const SandiaDecay::CalcFloatType lambda_i = decay_path[row-1]->child->decayConstant();
#endif
const SandiaDecay::CalcFloatType lambda_j = decay_path[col]->parent->decayConstant();
const SandiaDecay::CalcFloatType br = decay_path[row-1]->branchRatio;
coefs[row][col] = br * (lambda_iminus1/(lambda_i - lambda_j)) * coefs[row-1][col];
coefs[row][row] -= coefs[row][col];
}//for( loop over 'col' of matrix A )
//We have to get rid of any transition without a child for efficiency sake
vector<const SandiaDecay::Transition *> decays;
#if( DO_EMPTY_TRANSITION_FIX )
if( child )
#endif
{
decays.reserve( child->decaysToChildren.size() );
for( size_t t = 0; t < child->decaysToChildren.size(); ++t )
{
#if( !DO_EMPTY_TRANSITION_FIX )
if( child->decaysToChildren[t]->child )
#endif
decays.push_back( child->decaysToChildren[t] );
}//for( loop over children )
}//if( child )
if( decays.empty() )
{
const size_t ndecaypath = decay_path.size();
ws.nuclide_path.push_back( vector<const SandiaDecay::Nuclide *>() );
vector<const SandiaDecay::Nuclide *> &decay_nuclides = ws.nuclide_path.back();
decay_nuclides.resize( ndecaypath + 1 );
for( size_t i = 0; i < ndecaypath; ++i )
decay_nuclides[i] = decay_path[i]->parent;
decay_nuclides[ndecaypath] = child;
ws.decay_coeffs.push_back( coefs ); //17.2% of time
coefs.resize( row );
return;
}//if( decays.empty() )
const size_t ndecays = decays.size();
for( size_t t = 0; t < ndecays; ++t )
{
const size_t ndecaypath = decay_path.size();
vector<const SandiaDecay::Transition *> new_path( ndecaypath + 1 );
for( size_t i = 0; i < ndecaypath; ++i )
new_path[i] = decay_path[i];
new_path[ndecaypath] = decays[t];
calc_bateman_coef( coefs, new_path, ws );
}//for( loop over decaysToChildren )
coefs.resize( row );
}//void calc_bateman_coef(...)
}//namespace
namespace SandiaDecay
{
const char *to_str( SandiaDecay::ProductType s )
{
switch( s )
{
case SandiaDecay::BetaParticle:
return "beta";
case SandiaDecay::GammaParticle:
return "gamma";
case SandiaDecay::AlphaParticle:
return "alpha";
case SandiaDecay::PositronParticle:
return "positron";
case SandiaDecay::CaptureElectronParticle:
return "electronCapture";
case SandiaDecay::XrayParticle:
return "xray";
}//switch( s )
return "InvalidProductType";
}//const char *to_str( ProductType s )
const char *to_str( const DecayMode &s )
{
switch( s )
{
case AlphaDecay:
return "Alpha";
case BetaDecay:
return "Beta";
case BetaPlusDecay:
return "Beta+";
case ProtonDecay:
return "Proton";
case IsometricTransitionDecay:
return "Isometric";
case BetaAndNeutronDecay:
return "Beta and Neutron";
case BetaAndTwoNeutronDecay:
return "Beta and 2 Neutron";
case ElectronCaptureDecay:
return "Electron Capture";
case ElectronCaptureAndProtonDecay:
return "Electron Capture and Proton";
case ElectronCaptureAndAlphaDecay:
return "Electron Capture and Alpha";
case ElectronCaptureAndTwoProtonDecay:
return "Electron Capture and 2 Proton";
case BetaAndAlphaDecay:
return "Beta and Alpha";
case BetaPlusAndProtonDecay:
return "Beta Plus and Proton";
case BetaPlusAndTwoProtonDecay:
return "Beta Plus and 2 Proton";
case BetaPlusAndThreeProtonDecay:
return "Beta Plus and 3 Proton";
case BetaPlusAndAlphaDecay:
return "Beta Plus and Alpha";
case DoubleBetaDecay:
return "2 Beta+";
case DoubleElectronCaptureDecay:
return "2 Electron Capture";
case Carbon14Decay: //Carbon-14 ?
return "Carbon-14";
case SpontaneousFissionDecay: //I think this is spontaneous fission
return "Spontaneous Fission";
case ClusterDecay:
return "Cluster Emission";
case DoubleProton:
return "Double Proton";
case UndefinedDecay:
return "Undefined";
break;
};//
return "";
}//const char *to_str( DecayMode s )
std::istream& operator>>( std::istream& input, ProductType &s )
{
std::string arg;
input >> arg;
if( !convert( arg, s ) )
input.setstate( ios::failbit );
return input;
}//istream & operator>>( istream &, ProductType & )
std::string human_str_summary( const Nuclide &obj )
{
stringstream ostr;
ostr << obj.symbol << " Atomic Number " << obj.atomicNumber <<", Atomic Mass "
<< obj.massNumber << ", Isomer Number " << obj.isomerNumber << " "
<< obj.atomicMass << " AMU, HalfLife=" << obj.halfLife << " seconds";
const size_t nParents = obj.decaysFromParents.size();
if( nParents )
ostr << "\n Parent";
if( nParents > 1 )
ostr << "s";
if( nParents )
ostr << ": ";
for( size_t i = 0; i < nParents; ++i )
{
if( i )
ostr << ", ";
if( obj.decaysFromParents[i] && obj.decaysFromParents[i]->parent )
ostr << obj.decaysFromParents[i]->parent->symbol;
}//for( loop over parents )
const size_t nChilds = obj.decaysToChildren.size();
for( size_t i = 0; i < nChilds; ++i )
ostr << "\n " << human_str_summary(*obj.decaysToChildren[i]);
return ostr.str();
}//std::ostream& operator<<( std::ostream& ostr, const Nuclide &obj )
std::string human_str_summary( const Transition &obj )
{
stringstream ostr;
ostr << "Transition ";
if( obj.parent )
ostr << obj.parent->symbol;
ostr << "→";
if( obj.child )
ostr << obj.child->symbol;
else
ostr << "various";
ostr << ": mode=" << obj.mode << " branchRatio=" << obj.branchRatio;
if( obj.products.size() )
ostr << "; Products:";
for( size_t i = 0; i < obj.products.size(); ++i )
ostr << "\n " << SandiaDecay::human_str_summary( obj.products[i] );
return ostr.str();
}//ostream& operator<<( ostream& ostr, const Transition &obj )
std::string human_str_summary( const RadParticle &obj )
{
stringstream ostr;
switch( obj.type )
{
case BetaParticle: ostr << "beta:"; break;
case GammaParticle: ostr << "gamma:"; break;
case AlphaParticle: ostr << "alpha:"; break;
case PositronParticle: ostr << "positron:"; break;
case CaptureElectronParticle: ostr << "electronCapture:"; break;
case XrayParticle: ostr << "xray:"; break;
};//switch( obj.type )
ostr << " energy=" << obj.energy << "keV intensity=" << obj.intensity;
switch( obj.type )
{
case GammaParticle:
break;
case AlphaParticle:
ostr << " hinderence=" << obj.hindrance;
break;
case BetaParticle: case PositronParticle: case CaptureElectronParticle:
ostr << " forbiddenness=" << obj.forbiddenness << " logFT=" << obj.logFT;
break;
case XrayParticle:
break;
};//switch( obj.type )
return ostr.str();
}//std::string human_str_summary( const RadParticle &obj )
RadParticle::RadParticle( const ::rapidxml::xml_node<char> *node )
{
set( node );
}
RadParticle::RadParticle(ProductType p_type, float p_energy, float p_intensity)
: type( p_type ),
energy( p_energy ),
intensity( p_intensity ),
hindrance( 0.0f ),
logFT( 0.0f ),
forbiddenness( NoForbiddenness )
//, revision( RadParticle::NoRevision )
{
}
void RadParticle::set( const ::rapidxml::xml_node<char> *node )
{
//Note: assumes rapidxml::parse_no_string_terminators has NOT been used
using ::rapidxml::internal::compare;
typedef ::rapidxml::xml_attribute<char> Attribute;
const std::string name( node->name(), node->name()+node->name_size() );
if( !convert( name, type ) )
{
const string msg = "Failed converting '" + name + "' to ProductType";
throw runtime_error( msg );
}//if( !convert( name, type ) )
#if( ENABLE_SHORT_NAME )
const bool shortname = (name.size() <= 2);
const Attribute *energy_att = shortname ? node->first_attribute("e",1) : node->first_attribute( "energy", 6 );
const Attribute *intensity_att = shortname ? node->first_attribute("i",1) : node->first_attribute( "intensity", 9 );
const Attribute *hindrance_att = shortname ? node->first_attribute("h",1) : node->first_attribute( "hindrance", 9 );
const Attribute *forbiddenness_att = shortname ? node->first_attribute("f",1) : node->first_attribute( "forbiddenness", 13 );
const Attribute *logFT_att = shortname ? node->first_attribute( "lft", 3 ) : node->first_attribute( "logFT", 5 );
//const Attribute *revision_att = node->first_attribute( "revision", 8 );
#else
const Attribute *energy_att = node->first_attribute( "energy", 6 );
const Attribute *intensity_att = node->first_attribute( "intensity", 9 );
const Attribute *hindrance_att = node->first_attribute( "hindrance", 9 );
const Attribute *forbiddenness_att = node->first_attribute( "forbiddenness", 13 );
const Attribute *logFT_att = node->first_attribute( "logFT", 5 );
//const Attribute *revision_att = node->first_attribute( "revision", 8 );
#endif
if( energy_att ) energy = parse_float( energy_att->value() );
else energy = 0.0f;
if( intensity_att ) intensity = parse_float( intensity_att->value() );
else intensity = 0.0f;
if( hindrance_att ) hindrance = parse_float( hindrance_att->value() );
else hindrance = 0.0f;
if( forbiddenness_att )
forbiddenness = forbiddenness_fromstr( forbiddenness_att->value() );
else forbiddenness = NoForbiddenness;
if( logFT_att ) logFT = parse_float( logFT_att->value() );
else logFT = 0.0f;
//if( revision_att )
// revision = revision_fromstr( revision_att->value() );
//else revision = RadParticle::NoRevision;
}//void set( ::rapidxml::xml_node<char> *node )
//Transition::Transition()
//{
// parent = child = NULL;
// mode = UndefinedDecay;
// branchRatio = 0.0;
//}//default Transition constructor
Transition::Transition( const ::rapidxml::xml_node<char> *node,
const vector<const Nuclide *> &nuclides )
{
set( node, nuclides );
}
void Transition::set( const ::rapidxml::xml_node<char> *node,
const vector<const Nuclide *> &nuclides )
{
products.clear();
//Note: assumes ::rapidxml::parse_no_string_terminators has NOT been used
using ::rapidxml::internal::compare;
typedef ::rapidxml::xml_attribute<char> Attribute;
assert( compare( node->name(), node->name_size(), "transition", 10, true )
|| compare( node->name(), node->name_size(), "t", 1, true ) );
#if( ENABLE_SHORT_NAME )
const bool shortnames = (node->name_size()==1);
const Attribute *parent_att = shortnames ? node->first_attribute("p",1) : node->first_attribute( "parent", 6 );
const Attribute *child_att = shortnames ? node->first_attribute("c",1) : node->first_attribute( "child", 5 );
const Attribute *mode_att = shortnames ? node->first_attribute("m",1) : node->first_attribute( "mode", 4 );
const Attribute *branchRatio_att = shortnames ? node->first_attribute("br",2) : node->first_attribute( "branchRatio", 11 );
#else
const Attribute *parent_att = node->first_attribute( "parent", 6 );
const Attribute *child_att = node->first_attribute( "child", 5 );
const Attribute *mode_att = node->first_attribute( "mode", 4 );
const Attribute *branchRatio_att = node->first_attribute( "branchRatio", 11 );
#endif
if( parent_att )
{
const std::string parentstr = parent_att->value();
vector<const Nuclide *>::const_iterator iter = find( nuclides, parentstr );
if( (parentstr != "") && (iter != nuclides.end()) )
parent = (*iter);
else parent = NULL;
}else parent = NULL;
if( child_att )
{
const std::string childstr = child_att->value();
vector<const Nuclide *>::const_iterator iter = find( nuclides, childstr );
if( (childstr != "") && (iter != nuclides.end()) )
child = (*iter);
else child = NULL;
}else child = NULL;
if( mode_att )
mode = decaymode_fromstr( mode_att->value() );
else mode = UndefinedDecay;
if( branchRatio_att ) branchRatio = parse_float( branchRatio_att->value() );
else branchRatio = 0.0f;
//In order to link coincidences correctly, we will need to loop over decay
// products twice since we're using a index based mecahnism.
std::vector<RadParticle> new_particles;
std::vector< std::pair<const char *,size_t> > new_particle_ids;
std::vector<const ::rapidxml::xml_node<char> *> new_particle_nodes;
for( const ::rapidxml::xml_node<char> *particle = node->first_node();
particle;
particle = particle->next_sibling() )
{
if( compare( particle->name(), particle->name_size(), "edit", 4, true )
|| compare( particle->name(), particle->name_size(), "insertion", 9, true ) )
continue;
try
{
const Attribute *revision_att = particle->first_attribute( "revision", 8 );
if( revision_att
&& (revision_fromstr(revision_att->value())==DeletedRevision) )
continue;
new_particles.push_back( RadParticle( particle ) );
const rapidxml::xml_attribute<char> *idattrib = particle->first_attribute("id");
if( idattrib && idattrib->value_size() )
new_particle_ids.push_back( std::make_pair( (const char *)idattrib->value(), idattrib->value_size()) );
else
new_particle_ids.push_back( std::make_pair( (const char *)0, size_t(0)) );
new_particle_nodes.push_back( particle );
}catch( std::exception &e )
{
cerr << "Caught: " << e.what()
// << " on transition from "
// << (parent ? parent->symbol : string("null"))
// << " to "
// << (child ? child->symbol : string("null"))
<< endl;
}
}//for( loop over decaysToChildren )
assert( new_particles.size() == new_particle_ids.size() );
assert( new_particles.size() == new_particle_nodes.size() );
for( size_t i = 0; i < new_particle_nodes.size(); ++i )
{
RadParticle &particle = new_particles[i];
const ::rapidxml::xml_node<char> *gamma_node = new_particle_nodes[i];
#if( ENABLE_SHORT_NAME )
const ::rapidxml::xml_node<char> *coinc = shortnames ? gamma_node->first_node( "c", 1 ) : gamma_node->first_node( "coincidentGamma", 15 );
#else
const ::rapidxml::xml_node<char> *coinc = gamma_node->first_node( "coincidentGamma", 15 );
#endif
for( ; coinc; coinc = coinc->next_sibling( coinc->name(), coinc->name_size() ) )
{
const ::rapidxml::xml_attribute<char> *idattr = coinc->first_attribute("id",2);
const char * const id_val = idattr ? idattr->value() : NULL;
const size_t id_len = idattr ? idattr->value_size() : std::size_t(0);
#if( ENABLE_SHORT_NAME )
const ::rapidxml::xml_attribute<char> *intensityattr = shortnames ? coinc->first_attribute("i",1)
: coinc->first_attribute("intensity",9);
#else
const ::rapidxml::xml_attribute<char> *intensityattr = coinc->first_attribute("intensity",9);
#endif
const char * const intensity_val = intensityattr ? intensityattr->value() : NULL;
const size_t intensity_len = intensityattr ? intensityattr->value_size() : std::size_t(0);
if( !id_len || !intensity_len )
continue;
bool found_partner = false;
const float intensity = parse_float( intensity_val );
#if( ENABLE_SHORT_NAME )
// Here we will optimistically assume the "id" value is a string representing an integer
// that gives the index of the matching particle - this assumes the input XML file was
// made for this - which `minimize_xml_file` will do with the '--shrink-coinc' option.
// If this assumption doesnt pan-out, we will instead do the full search, so not that
// much will be lost.
//
// Including the check for `shortnames`, either runtime or compiletime isnt really necassary,
// just currently not
if( shortnames && (id_len <= 3) )
{
unsigned short int trial_index = (id_val[id_len - 1] - '0');
if( id_len >= 2 )
trial_index += 10*(id_val[id_len - 2] - '0');
if( id_len >= 3 )
trial_index += 100*(id_val[id_len - 3] - '0');
if( trial_index < new_particle_ids.size() )
{
const char * const trial_val = new_particle_ids[trial_index].first;
const size_t trial_len = new_particle_ids[trial_index].second;
if( rapidxml::internal::compare( id_val, id_len, trial_val, trial_len, true) )
{
particle.coincidences.push_back( std::make_pair(trial_index,intensity) );
found_partner = true;
}
}//if( trial_index < new_particle_ids.size() )
}//if( shortnames && (id_len <= 3) )
#endif //#if( ENABLE_SHORT_NAME )
if( !found_partner )
{
for( unsigned short int j = 0; j < new_particle_ids.size(); ++j )
{
const char * const trial_val = new_particle_ids[j].first;
const size_t trial_len = new_particle_ids[j].second;
if( rapidxml::internal::compare( id_val, id_len, trial_val, trial_len, true) )
{
particle.coincidences.push_back( std::make_pair(j,intensity) );
found_partner = true;
break;
}
}
}//if( !found_partner )
if( !found_partner )
{
// As of 20221027, there are about 250 missed matches... we'll not print out for the moment.
// const char * const parent = (parent_att ? parent_att->value() : "null");
// const rapidxml::xml_attribute<char> *energy_attrib = gamma_node ? gamma_node->first_attribute("energy") : nullptr;
// const char * const energy = ((energy_attrib && energy_attrib->value()) ? energy_attrib->value() : "null");
// cerr << "SandiaDecay: Failed to find coincident gamma for " << parent
// << " with id=\"" << uuid << "\"" << " for gamma energy " << energy << " keV" << endl;
}//if( !found_partner )
}//for( loop over <coincidentGamma> )
}//for( size_t i = 0; i < new_particle_nodes.size(); ++i )
products.swap( new_particles );
}//void set( ::rapidxml::xml_node<char> *node )
bool Nuclide::lessThanForOrdering( const Nuclide *lhs, const Nuclide *rhs )
{
if( !lhs || !rhs )
return lhs < rhs;
if( (lhs->massNumber == rhs->massNumber)
&& (lhs->atomicNumber == rhs->atomicNumber)
&& (lhs->isomerNumber == rhs->isomerNumber) )
{
return false;
}
if( lhs->massNumber != rhs->massNumber )
return (lhs->massNumber < rhs->massNumber);
if( lhs->atomicNumber == rhs->atomicNumber )
return (lhs->isomerNumber < rhs->isomerNumber);
return lhs->atomicNumber < rhs->atomicNumber;
}
bool Nuclide::greaterThanForOrdering( const Nuclide *lhs, const Nuclide *rhs )
{
return lessThanForOrdering( rhs, lhs );
}
bool Nuclide::lessThanByDecay( const Nuclide *lhs, const Nuclide *rhs )
{
if( !lhs || !rhs )
return lhs < rhs;
if( (lhs->massNumber == rhs->massNumber)
&& (lhs->atomicNumber == rhs->atomicNumber)
&& (lhs->isomerNumber == rhs->isomerNumber) )
{
return false;
}
//is 'this' a child of 'rhs' ? If not, is 'this' lighter than 'rhs
if( lhs->massNumber != rhs->massNumber )
return (lhs->massNumber < rhs->massNumber);
if( lhs->atomicNumber == rhs->atomicNumber )
return (lhs->isomerNumber < rhs->isomerNumber);
// If the atomic numbers differ more than 7, then we know which way the decay goes (I tested
// commenting this particular test out will give identical results, just take more cpu).
const int an_diff = ((lhs->atomicNumber > rhs->atomicNumber)
? (lhs->atomicNumber - rhs->atomicNumber) : (rhs->atomicNumber - lhs->atomicNumber));
if( an_diff > 7 )
return (lhs->atomicNumber < rhs->atomicNumber);
// This next test was verified to give identical results with or without it, but saves CPU with
const float am_diff = fabs(lhs->atomicMass - rhs->atomicMass);
if( (am_diff > 1.0f) && (an_diff > 4) )
return lhs->atomicNumber < rhs->atomicNumber;
// \TODO: We could do a few more tests to avoid calling into Nuclide::branchRatioToDecendant,
// but this is decent for now
assert( lhs->massNumber == rhs->massNumber );
assert( (an_diff > 0) && (an_diff < 8) );
assert( (am_diff <= 1.0f) || (an_diff <= 4) );
const float lhsToRhsBr = lhs->branchRatioToDecendant( rhs );
const float rhsToLhsBr = rhs->branchRatioToDecendant( lhs );
if( lhsToRhsBr == rhsToLhsBr ) //These nuclides dont decay into each other.
return (lhs->atomicNumber < rhs->atomicNumber);
return (lhsToRhsBr < rhsToLhsBr);
}//bool lessThanByDecay( const Nuclide *lhs, const Nuclide *rhs )
bool Nuclide::greaterThanByDecay( const Nuclide *lhs, const Nuclide *rhs )
{
return lessThanByDecay( rhs, lhs );
}
bool Nuclide::operator==( const Nuclide &rhs ) const
{
return ((atomicNumber==rhs.atomicNumber) && (massNumber==rhs.massNumber) && (isomerNumber==rhs.isomerNumber));
}
bool Nuclide::operator!=( const Nuclide &rhs ) const
{