-
Notifications
You must be signed in to change notification settings - Fork 31
/
MCNPInput.cpp
1536 lines (1225 loc) · 46.7 KB
/
MCNPInput.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
#include "MCNPInput.hpp"
#include "geometry.hpp"
#include "options.hpp"
#include "mcnp2cad.hpp"
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
/******************
* IMPLEMENTATIONS OF DataRef
******************/
template <class T>
class CardRef : public DataRef<T>{
protected:
InputDeck& deck;
DataCard::id_t key;
public:
CardRef( InputDeck& deck_p, DataCard::kind kind, int ident ) :
DataRef<T>(), deck(deck_p), key( std::make_pair( kind, ident ) )
{}
virtual const T& getData() const {
DataCard* c = deck.lookup_data_card( key );
const T& ref = dynamic_cast< DataRef<T>* >(c)->getData();
return ref;
}
virtual CardRef<T> * clone(){
return new CardRef<T>( *this );
}
const DataCard::id_t& getKey() const { return key; }
};
/******************
* HELPER FUNCTIONS
******************/
static void strlower( std::string& str ){
// convert to lowercase
for(size_t i = 0; i < str.length(); ++i){
str[i] = tolower( str.at(i) );
}
}
static int makeint( const std::string& token ){
const char* str = token.c_str();
char* end;
int ret = strtol(str, &end, 10);
if( end != str+token.length() ){
record << "Warning: string [" << token << "] did not convert to int as expected." << std::endl;
std::cerr << "Warning: string [" << token << "] did not convert to int as expected." << std::endl;
}
return ret;
}
static std::vector<std::string> parseID( const token_list_t tokens ){
//This function returns the first two or three arguments in token_list, which
//name and define the type of surface.
std::vector<std::string> identifier (3);
identifier[0] = tokens.at(0);
if(identifier[0].find_first_of("*+") != identifier[0].npos){
std::cerr << "Warning: no special handling for reflecting or white-boundary surfaces" << std::endl;
record << "Warning: no special handling for reflecting or white-boundary surfaces" << std::endl;
identifier[0][0] = ' ';
}
if(tokens.at(1).find_first_of("1234567890-") != 0){
identifier[1] = '0';
identifier[2] = tokens.at(1);
}
else{
identifier[1] = tokens.at(1);
identifier[2] = tokens.at(2);
if( makeint( identifier.at(1) ) == 0 ){
record << "I don't think 0 is a valid surface transformation ID, so I'm ignoring it." << std::endl;
std::cerr << "I don't think 0 is a valid surface transformation ID, so I'm ignoring it." << std::endl;
}
}
return identifier;
}
static double makedouble( const std::string& token ){
std::string tmp = token;
// MCNP allows FORTRAN-style floating point values where the 'e' in the exponent is missing,
// e.g. 1.23-45 means 1.23e-45. The following check inserts such a missing 'e' to avoid
// confusing strtod().
size_t s_idx = tmp.find_last_of("+-");
if( s_idx != tmp.npos && s_idx > tmp.find_first_of("1234567890") && tmp.at(s_idx-1) != 'e' ){
tmp.insert( tmp.find_last_of("+-"), "e" );
if( OPT_DEBUG ) record << "Formatting FORTRAN value: converted " << token << " to " << tmp << std::endl;
}
const char* str = tmp.c_str();
char* end;
double ret = strtod(str, &end);
if( end != str+tmp.length() ){
record << "Warning: string [" << tmp << "] did not convert to double as expected." << std::endl;
std::cerr << "Warning: string [" << tmp << "] did not convert to double as expected." << std::endl;
}
return ret;
}
/** parse the args of an MCNP geometry transform */
static std::vector<double> makeTransformArgs( const token_list_t tokens ){
std::vector<double> args;
for( token_list_t::const_iterator i = tokens.begin(); i!=tokens.end(); ++i){
std::string token = *i;
size_t idx;
while( (idx = token.find_first_of("()")) != token.npos){
token.replace( idx, 1, "" ); // remove parentheses
}
if( token.find_first_of( "1234567890" ) != token.npos){
args.push_back( makedouble( token ) );
}
else if( token.length() > 0) {
record << "Warning: makeTransformArgs ignoring unrecognized input token [" << token << "]" << std::endl;
std::cerr << "Warning: makeTransformArgs ignoring unrecognized input token [" << token << "]" << std::endl;
}
}
return args;
}
/**
* Attempt to create a Transform object using the given numbers. Bounding parentheses are allowed
* and will be removed.
*
* The returned object is allocated with new and becomes the property of the caller.
*/
static DataRef<Transform>* parseTransform( InputDeck& deck, const token_list_t tokens, bool degree_format = false ){
std::vector<double> args = makeTransformArgs( tokens );
if( args.size() == 1 ){
return new CardRef<Transform>( deck, DataCard::TR, static_cast<int>(args[0]) );
}
else{
return new ImmediateRef<Transform>( Transform( args, degree_format ) );
}
}
static DataRef<Transform>* parseTransform( InputDeck& deck, token_list_t::iterator& i, bool degree_format = false ){
token_list_t args;
std::string next_token = *i;
if( next_token.find("(") != next_token.npos ){
do{
args.push_back( next_token );
next_token = *(++i);
}
while( next_token.find(")") == next_token.npos );
}
args.push_back( next_token );
return parseTransform( deck, args, degree_format );
}
static FillNode parseFillNode( InputDeck& deck, token_list_t::iterator& i, const token_list_t::iterator& end, bool degree_format = false ){
// simple fill. Format is n or n (transform). Transform may be either a TR card number
// or an immediate transform
int n; // the filling universe
DataRef<Transform>* t;
bool has_transform = false;
std::string first_token = *i;
size_t paren_idx = first_token.find("(");
std::string second_token;
if( paren_idx != first_token.npos ){
// first_token has an open paren
std::string n_str(first_token, 0, paren_idx);
n = makeint(n_str);
second_token = first_token.substr(paren_idx,first_token.npos);
has_transform = true;
}
else{
n = makeint(first_token);
if( ++i != end ){
second_token = *i;
if( second_token[0] == '(' ){
has_transform = true;
}
else{
// the next token didn't belong to this fill
i--;
}
}
else{ i--; }
}
if( has_transform ){
token_list_t transform_tokens;
std::string next_token = second_token;
while( next_token.find(")") == next_token.npos ){
transform_tokens.push_back(next_token);
next_token = *(++i);
}
transform_tokens.push_back( next_token );
t = parseTransform( deck, transform_tokens, degree_format );
}
else{
t = new NullRef<Transform>();
}
if( n < 0 ){
n = -n; // TODO: handle negative universe numbers specially
}
return FillNode (n, t );
}
static bool isblank( const std::string& line ){
return (line=="" || line.find_first_not_of(" ") == line.npos );
}
template < class T >
std::ostream& operator<<( std::ostream& out, const std::vector<T>& list ){
out << "[";
for(typename std::vector<T>::const_iterator i = list.begin(); i!=list.end(); ++i){
out << *i << "|";
}
if(list.size() > 0)
out << "\b"; // unless list was empty, backspace the last | character
out << "]";
return out;
}
/******************
* CELL CARDS
******************/
class CellCardImpl : public CellCard {
protected:
static geom_list_entry_t make_geom_entry(geom_token_t t, int param = 0){
return std::make_pair(t, param);
}
static bool is_num_token( geom_list_entry_t t ){
return t.first == CELLNUM || t.first == SURFNUM || t.first == MBODYFACET;
}
static bool is_op_token( geom_list_entry_t t ){
return t.first == COMPLEMENT || t.first == UNION || t.first == INTERSECT;
}
static int operator_priority( geom_list_entry_t t ){
switch(t.first){
case COMPLEMENT: return 3;
case INTERSECT: return 2;
case UNION: return 1;
default:
throw std::runtime_error("queried operator priority for a non-operator token");
}
}
/**
* Build the geom list as part of cell construction.
* Each item in the list will be a string; either " ", ":", or "#", indicating
* operators, or parentheses, or numbers indicating surface or cell identities.
*
* @param The list of geometry tokens in the input file, as a list of strings that were
* separated by white space in the original file.
*/
void retokenize_geometry( const token_list_t& tokens ){
for(token_list_t::const_iterator i = tokens.begin(); i!=tokens.end(); ++i){
const std::string& token = *i;
size_t j = 0;
while( j < token.length() ){
char cj = token.at(j);
switch(cj){
// the following macro pushes an intersect token onto the geom list
// if the end of that list indicates that one is needed
#define IMPLICIT_INTERSECT() do{ \
if(geom.size()){ \
geom_list_entry_t &t = geom.at(geom.size()-1); \
if( is_num_token(t) || t.first == RPAREN ){ \
geom.push_back( make_geom_entry( INTERSECT ) ); \
}}} while(0)
case '(':
IMPLICIT_INTERSECT();
geom.push_back(make_geom_entry(LPAREN)); j++;
break;
case ')':
geom.push_back(make_geom_entry(RPAREN)); j++;
break;
case '#':
IMPLICIT_INTERSECT();
geom.push_back(make_geom_entry(COMPLEMENT)); j++;
break;
case ':':
geom.push_back(make_geom_entry(UNION)); j++;
break;
default: // a number
// the number refers to a cell if the previous token is a complement
bool is_cell = geom.size() && ((geom.at(geom.size()-1)).first == COMPLEMENT);
IMPLICIT_INTERSECT();
if( !(isdigit(cj) || cj == '+' || cj == '-' ) ){
if( OPT_DEBUG ){
record << "Error in CellCard::retokenize_geometry( const token_list_t& tokens ) in MCNPInput.cpp" << std::endl;
record << "isdigit(cj) || cj == '+' || cj == '-'" << std::endl;
}
throw std::runtime_error("Illegal character found while evaluating cell geometry.");
}
size_t end = token.find_first_not_of("1234567890-+.",j);
if( j == end ){
if( OPT_DEBUG ){
record << "Error in CellCard::retokenize_geometry( const token_list_t& tokens ) in MCNPInput.cpp" << std::endl;
record << "j == end" << std::endl;
}
throw std::runtime_error("Error tokenizing geometry.");
}
std::string numstr( token, j, end-j );
const char* numstr_c = numstr.c_str();
char* p;
int num = strtol( numstr_c, &p, 10 );
if( *p == '.' ){
// This is a macrobody facet
if( is_cell ){
if( OPT_DEBUG ){
record << "Error in CellCard::retokenize_geometry( const token_list_t& tokens ) in MCNPInput.cpp" << std::endl;
record << "is_cell" << std::endl;
}
throw std::runtime_error("Macrobody facet used where cell needed.");
}
int facet = strtol( p+1, NULL, 10 );
if( facet < 1 || facet > 8 ){
if( OPT_DEBUG ){
record << "Error in CellCard::retokenize_geometry( const token_list_t& tokens ) in MCNPInput.cpp" << std::endl;
record << "facet = 0 || facet > 8" << std::endl;
}
throw std::runtime_error("facet number " + std::to_string(facet) + " is not viable.");
}
// storage of macrobody facets: multiply cell number by ten, add facet number
num *= 10;
// don't add a positive facet number to a negative cell numer
num += (num > 0) ? facet : -facet;
geom.push_back( make_geom_entry( MBODYFACET, num ) );
}
else{
geom.push_back( make_geom_entry( is_cell ? CELLNUM : SURFNUM, num ));
}
j += (end-j);
break;
#undef IMPLICIT_INTERSECT
}
}
}
if( OPT_DEBUG ) record << tokens << " -> " << geom << std::endl;
}
/**
* The final step of geometry parsing: convert the geometry list to RPN, which
* greatly simplifies the process of evaluating the geometry later. This function
* uses the shunting yard algorithm. For more info consult
* http://en.wikipedia.org/wiki/Shunting_yard_algorithm
*/
void shunt_geometry( ){
geom_list_t geom_copy( geom );
geom.clear();
geom_list_t stack;
for(geom_list_t::iterator i = geom_copy.begin(); i!=geom_copy.end(); ++i){
geom_list_entry_t token = *i;
if( is_num_token(token) ){
geom.push_back(token);
}
else if( is_op_token(token) ){
while(stack.size()){
geom_list_entry_t& stack_top = stack.back();
if( is_op_token(stack_top) && operator_priority(stack_top) > operator_priority(token) ){
geom.push_back(stack_top);
stack.pop_back();
}
else{
break;
}
}
stack.push_back(token);
}
else if( token.first == LPAREN ){
stack.push_back(token);
}
else if( token.first == RPAREN ){
while( stack.back().first != LPAREN ){
geom.push_back( stack.back() );
stack.pop_back();
}
stack.pop_back(); // remove the LPAREN
}
}
while( stack.size() ){
geom.push_back( stack.back() );
stack.pop_back();
}
}
Vector3d latticeVectorHelper( Vector3d difference_along_normal, Vector3d v_dir ){
double length = difference_along_normal.length() / (difference_along_normal.normalize().dot(v_dir));
return v_dir * length;
}
void setupLattice(){
if( OPT_DEBUG ) record << "Setting up lattice for cell " << ident << std::endl;
std::vector< std::pair<SurfaceCard*,bool> > surfaceCards;
for( geom_list_t::iterator i = geom.begin(); i!=geom.end(); ++i){
geom_list_entry_t entry = *i;
if( entry.first == SURFNUM ){
SurfaceCard* surf = parent_deck.lookup_surface_card( std::abs(entry.second) );
if( !surf ){
if( OPT_DEBUG ){
record << "Error in CellCard::setupLattice() in MCNPInput.cpp" << std::endl;
record << "!surf" << std::endl;
}
throw std::runtime_error("Problem looking up a surface for a lattice");
}
surfaceCards.push_back( std::make_pair(surf, (entry.second>0) ) );
}
}
int num_finite_dims = 0;
Vector3d v1, v2, v3;
std::vector< std::pair<Vector3d, double> > planes;
if( surfaceCards.size() == 1 ){
planes = surfaceCards.at(0).first->getMacrobodyPlaneParams();
if( surfaceCards.at(0).second != false ){
record << "Warning: macrobody lattice with positive sense, will proceed as if it was negative.";
std::cerr << "Warning: macrobody lattice with positive sense, will proceed as if it was negative.";
}
}
else{
for( unsigned int i = 0; i < surfaceCards.size(); ++i){
planes.push_back( surfaceCards.at(i).first->getPlaneParams() );
if( surfaceCards.at(i).second == true ){ planes[i].first = -planes[i].first; }
}
}
if( OPT_DEBUG ){
for( unsigned int i = 0; i < planes.size(); ++i){
record << " plane " << i << " normal = " << planes[i].first << " d = " << planes[i].second << std::endl;
}
}
if( lat_type == HEXAHEDRAL ){
if( planes.size() != 2 && planes.size() != 4 && planes.size() != 6 ){
if( OPT_DEBUG ){
record << "Error in CellCard::setupLattice() in MCNPInput.cpp" << std::endl;
record << "planes.size() != 2 && planes.size() != 4 && planes.size() != 6" << std::endl;
}
throw std::runtime_error("Error while setting up hexahedral lattice.");
}
if( planes.size() == 2 ){
num_finite_dims = 1;
v1 = planes[0].first.normalize() * std::fabs( planes[0].second - planes[1].second );
}
else if( planes.size() == 4 ){
num_finite_dims = 2;
Vector3d v3 = planes[0].first.cross( planes[2].first ).normalize(); // infer a third (infinite) direction
// vector from planes[1] to planes[0]
Vector3d xv = planes[0].first.normalize() * std::fabs( planes[0].second - planes[1].second );
// direction of l.v1: cross product of normals planes[2] and v3
Vector3d xv2 = planes[2].first.normalize().cross( v3 ).normalize();
v1 = latticeVectorHelper( xv, xv2 );
Vector3d yv = planes[2].first.normalize() * std::fabs( planes[2].second - planes[3].second );
Vector3d yv2 = planes[0].first.normalize().cross( v3 ).normalize();
v2 = latticeVectorHelper( yv, yv2 );
}
else{ // planes.size() == 6
num_finite_dims = 3;
// vector from planes[1] to planes[0]
Vector3d xv = planes[0].first.normalize() * std::fabs( planes[0].second - planes[1].second );
// direction of v1: cross product of normals planes[2] and planes[4]
Vector3d xv2 = planes[2].first.normalize().cross( planes[4].first.normalize() ).normalize();
v1 = latticeVectorHelper( xv, xv2 );
Vector3d yv = planes[2].first.normalize() * std::fabs( planes[2].second - planes[3].second );
Vector3d yv2 = planes[0].first.normalize().cross( planes[4].first.normalize() ).normalize();
v2 = latticeVectorHelper( yv, yv2 );
Vector3d zv = planes[4].first.normalize() * std::fabs( planes[4].second - planes[5].second );
Vector3d zv2 = planes[0].first.normalize().cross( planes[2].first.normalize() ).normalize();
v3 = latticeVectorHelper( zv, zv2 );
}
}
else if( lat_type == HEXAGONAL ){
if( planes.size() != 6 && planes.size() != 8 ){
if( OPT_DEBUG ){
record << "Error in CellCard::setupLattice() in MCNPInput.cpp" << std::endl;
record << "planes.size() != 6 && planes.size() != 8" << std::endl;
}
throw std::runtime_error("Error while setting up hexagonal lattice.");
}
v3 = planes[0].first.cross( planes[2].first ).normalize(); // prism's primary axis
// vector from planes[1] to planes[0]
Vector3d xv = planes[0].first.normalize() * std::fabs( planes[0].second - planes[1].second );
// direction of l.v1: cross product of normals average(planes[2]+planes[4]) and v3
// TODO: this averaging trick only works with regular hexagons...
Vector3d xv2 = (planes[2].first.normalize()+planes[4].first.normalize()).normalize().cross( v3 ).normalize();
v1 = latticeVectorHelper( xv, xv2 );
Vector3d yv = planes[2].first.normalize() * std::fabs( planes[2].second - planes[3].second );
Vector3d yv2 = (planes[1].first.normalize()+planes[4].first.normalize()).normalize().cross( v3 ).normalize();
v2 = latticeVectorHelper( yv, yv2 );
if( planes.size() == 6 ){
num_finite_dims = 2;
}
else{ // planes.size() == 8
num_finite_dims = 3;
Vector3d zv = planes[6].first.normalize() * std::fabs( planes[6].second - planes[7].second );
Vector3d zv2 = v3;
v3 = latticeVectorHelper( zv, zv2 );
}
}
if( OPT_DEBUG )record << " dims " << num_finite_dims << " vectors " << v1 << v2 << v3 << std::endl;
Lattice l;
if( fill->hasData() ){
l = Lattice( num_finite_dims, v1, v2, v3, fill->getData() );
}
else{
FillNode n( this->universe );
l = Lattice( num_finite_dims, v1, v2, v3, n );
}
lattice = new ImmediateRef<Lattice>( l );
}
void makeData(){
for( token_list_t::iterator i = data.begin(); i!=data.end(); ++i ){
std::string token = *i;
if( token == "trcl" || token == "*trcl" ){
bool degree_format = (token[0] == '*');
i++;
trcl = parseTransform( parent_deck, i, degree_format );
} // token == {*}trcl
else if( token == "u" ){
universe = makeint(*(++i));
} // token == "u"
else if ( token == "lat" ){
int lat_designator = makeint(*(++i));
if( lat_designator < 0 || lat_designator > 2 ){
if( OPT_DEBUG ){
record << "Error in CellCard::makeData() in MCNPInput.cpp" << std::endl;
record << "lat_designator < 0 || lat_designator > 2" << std::endl;
}
throw std::runtime_error("Error while setting up data for a lattice.");
}
lat_type = static_cast<lattice_type_t>(lat_designator);
if( OPT_DEBUG ) record << "cell " << ident << " is lattice type " << lat_type << std::endl;
}
else if ( token == "mat" ){
material = makeint(*(++i));
}
else if ( token == "rho" ){
rho = makedouble(*(++i));
}
else if ( token.length() == 5 && token.substr(0,4) == "imp:" ){
double imp = makedouble(*(++i));
importances[token[4]] = imp;
}
else if( token == "fill" || token == "*fill" ){
bool degree_format = (token[0] == '*');
std::string next_token = *(++i);
// an explicit lattice grid exists if
// * the next token contains a colon, or
// * the token after it exists and starts with a colon
bool explicit_grid = next_token.find(":") != next_token.npos;
explicit_grid = explicit_grid || (i+1 != data.end() && (*(i+1)).at(0) == ':' );
if( explicit_grid ){
// convert the grid specifiers (x1:x2, y1:y2, z1:z2) into three spaceless strings for easier parsing
std::string gridspec[3];
for( int dim = 0; dim < 3; ++dim ){
std::string spec;
// add tokens to the spec string until it contains a colon but does not end with one
do{
spec += *i;
i++;
}
while( spec.find(":") == spec.npos || spec.at(spec.length()-1) == ':' );
if(OPT_DEBUG) record << "gridspec[" << dim << "]: " << spec << std::endl;
gridspec[dim] = spec;
}
irange ranges[3];
const char* range_str;
char *p;
int num_elements = 1;
for( int dim = 0; dim < 3; ++dim ){
range_str = gridspec[dim].c_str();
ranges[dim].first = strtol(range_str, &p, 10);
ranges[dim].second = strtol(p+1, NULL, 10);
if( ranges[dim].second != ranges[dim].first ){
num_elements *= ( ranges[dim].second - ranges[dim].first )+1;
}
}
std::vector<FillNode> elements;
for( int j = 0; j < num_elements; ++j ){
elements.push_back( parseFillNode( parent_deck, i, data.end(), degree_format ) );
i++;
}
i--;
fill = new ImmediateRef< Fill >( Fill( ranges[0], ranges[1], ranges[2], elements) );
}
else{ // no explicit grid; fill card is a single fill node
FillNode filler = parseFillNode( parent_deck, i, data.end(), degree_format );
fill = new ImmediateRef< Fill >( Fill(filler) );
}
} // token == {*}fill
}
// ensure data pointers are valid
if( !trcl ) {
trcl = new NullRef< Transform >();
}
if( !fill ){
fill = new NullRef< Fill > ();
}
}
int ident;
int material;
double rho; // material density
std::map<char, double> importances;
geom_list_t geom;
token_list_t data;
DataRef<Transform>* trcl;
DataRef<Fill>* fill;
int universe;
bool likenbut;
int likeness_cell_n;
lattice_type_t lat_type;
DataRef<Lattice> *lattice;
public:
CellCardImpl( InputDeck& deck, const token_list_t& tokens ) :
CellCard( deck ), trcl(NULL), fill(NULL), universe(0), likenbut(false), likeness_cell_n(0),
lat_type(NONE), lattice(NULL)
{
unsigned int idx = 0;
ident = makeint(tokens.at(idx++));
if(tokens.at(idx) == "like"){
idx++;
likenbut = true;
likeness_cell_n = makeint(tokens.at(idx++));
idx++; // skip the "but" token
while(idx < tokens.size()){
data.push_back(tokens[idx++]);
}
return;
}
material = makeint(tokens.at(idx++));
rho = 0.0;
if(material != 0){
rho = makedouble(tokens.at(idx++)); // material density
}
token_list_t temp_geom;
// while the tokens appear in geometry-specification syntax, store them into temporary list
while(idx < tokens.size() && tokens.at(idx).find_first_of("1234567890:#-+()") == 0){
temp_geom.push_back(tokens[idx++]);
}
// retokenize the geometry list, which follows a specialized syntax.
retokenize_geometry( temp_geom );
shunt_geometry();
// store the rest of the tokens into the data list
while(idx < tokens.size()){
data.push_back(tokens[idx++]);
}
makeData();
}
~CellCardImpl(){
if(trcl)
delete trcl;
if(fill)
delete fill;
if(lattice)
delete lattice;
}
virtual int getIdent() const{ return ident; }
virtual const geom_list_t getGeom() const { return geom; }
virtual const DataRef<Transform>& getTrcl() const { return *trcl; }
virtual int getUniverse() const { return universe; }
virtual bool hasFill() const { return fill && fill->hasData(); }
virtual const Fill& getFill() const {
if( fill && fill->hasData() ){
return fill->getData();
}
throw std::runtime_error( "Called getFill() on an unfilled cell" );
}
virtual bool isLattice() const {
return lat_type != NONE;
}
virtual lattice_type_t getLatticeType() const {
return lat_type;
}
virtual const Lattice& getLattice() const {
if( lattice && lattice->hasData() ){
return lattice->getData();
}
throw std::runtime_error( "Called getLattice() on a cell that hasn't got one" );
}
virtual int getMat() const { return material; }
virtual double getRho() const { return rho; }
virtual const std::map<char,double>& getImportances() const { return importances; }
virtual void print( std::ostream& s ) const{
s << "Cell " << ident << " geom " << geom << std::endl;
}
protected:
void finish(){
if( likenbut ){
CellCardImpl* host = dynamic_cast<CellCardImpl*>(parent_deck.lookup_cell_card( likeness_cell_n ));
if(host->likenbut){
host->finish(); // infinite recursion if cells are circularly defined... but our users wouldn't do that, right?
}
geom = host->geom;
universe = host->universe;
lat_type = host->lat_type;
material = host->material;
rho = host->rho;
importances = host->importances;
if( host->trcl->hasData()){
trcl = host->trcl->clone();
}
if( host->hasFill()){
fill = host->fill->clone();
}
if( host->isLattice()){
lattice = host->lattice->clone();
}
makeData();
likenbut = false;
}
if( lat_type != NONE && lattice == NULL ){
setupLattice();
}
}
friend class InputDeck;
};
CellCard::CellCard( InputDeck& deck ) :
Card(deck)
{}
std::ostream& operator<<(std::ostream& str, const CellCard::geom_list_entry_t& t ){
switch(t.first){
case CellCard::LPAREN: str << "("; break;
case CellCard::RPAREN: str << ")"; break;
case CellCard::COMPLEMENT: str << "#"; break;
case CellCard::UNION: str << ":"; break;
case CellCard::INTERSECT: str << "*"; break;
case CellCard::SURFNUM: str << t.second; break;
case CellCard::CELLNUM: str << "c" << t.second; break;
case CellCard::MBODYFACET:
int cell = t.second / 10;
int facet = abs(t.second) - (abs(cell)*10);
str << cell << "." << facet;
break;
}
return str;
}
/******************
* SURFACE CARDS
******************/
SurfaceCard::SurfaceCard( InputDeck& deck, const token_list_t tokens ):
Card(deck)
{
std::vector<std::string> identifier = parseID( tokens );
ident = makeint(identifier.at(0));
tx_id = makeint(identifier.at(1));
mnemonic = identifier.at(2);
size_t idx = 2;
if( tx_id == 0 ){
coord_xform = new NullRef<Transform>();
}
else if ( tx_id < 0 ){
// abs(tx_id) is the ID of surface with respect to which this surface is periodic.
record << "Warning: surface " << ident << " periodic, but this program has no special handling for periodic surfaces";
std::cerr << "Warning: surface " << ident << " periodic, but this program has no special handling for periodic surfaces";
}
else{ // tx_id is positive and nonzero
coord_xform = new CardRef<Transform>( deck, DataCard::TR, tx_id );
}
if( tokens.at(2) == mnemonic ){
idx++;
}
while( idx < tokens.size() ){
args.push_back( makedouble(tokens[idx++]) );
}
}
SurfaceCard::SurfaceCard( InputDeck& deck, const SurfaceCard s, int facetNum ):
Card(deck)
{
//This form of SurfaceCard makes a copy of a macrobody for use with one of its facets.
ident = facetNum;
mnemonic = s.getMnemonic();
args = s.getArgs();
tx_id = s.getTxid();
if( tx_id == 0 ){
coord_xform = new NullRef<Transform>();
}
else if ( tx_id < 0 ){
// abs(tx_id) is the ID of surface with respect to which this surface is periodic.
std::cerr << "Warning: surface " << ident << " periodic, but this program has no special handling for periodic surfaces";
record << "Warning: surface " << ident << " periodic, but this program has no special handling for periodic surfaces";
}
else{ // tx_id is positive and nonzero
coord_xform = new CardRef<Transform>( deck, DataCard::TR, tx_id );
}
}
const DataRef<Transform>& SurfaceCard::getTransform() const {
return *coord_xform;
}
void SurfaceCard::print( std::ostream& s ) const {
s << "Surface " << ident << " " << mnemonic << args;
if( coord_xform->hasData() ){
// this ugly lookup returns the integer ID of the TR card
s << " TR" << dynamic_cast<CardRef<Transform>*>(coord_xform)->getKey().second;
}
s << std::endl;
}
std::pair<Vector3d,double> SurfaceCard::getPlaneParams() const {
if(mnemonic == "px"){
return std::make_pair(Vector3d(1,0,0), args[0]);
}
else if(mnemonic == "py"){
return std::make_pair(Vector3d(0,1,0), args[0]);
}
else if(mnemonic == "pz"){
return std::make_pair(Vector3d(0,0,1), args[0]);
}
else if(mnemonic == "p"){
return std::make_pair(Vector3d( args ), args[3]/Vector3d(args).length());
}
else{
throw std::runtime_error("Tried to get plane normal of non-plane surface!");
}
}
std::vector< std::pair<Vector3d, double> > SurfaceCard::getMacrobodyPlaneParams() const {
std::vector< std::pair< Vector3d, double> > ret;
if( mnemonic == "box" ){
Vector3d corner( args );
const Vector3d v[3] = {Vector3d(args,3), Vector3d(args,6), Vector3d(args,9)};
// face order: v1 -v1 v2 -v2 v3 -v3
for( int i = 0; i < 3; ++i ){
Vector3d p = corner + v[i];
ret.push_back( std::make_pair( v[i].normalize(), v[i].projection( p ).length() ) );
ret.push_back( std::make_pair( -v[i].normalize(), v[i].projection( p ).length()-v[i].length() ) );
}
}
else if( mnemonic == "rpp" ){
Vector3d min( args.at(0), args.at(2), args.at(4) );
Vector3d max( args.at(1), args.at(3), args.at(5) );
for( int i = 0; i < 3; ++i ){
Vector3d v; v.v[i] = 1;
ret.push_back( std::make_pair( v.normalize(), max.v[i] ));
ret.push_back( std::make_pair(-v.normalize(), min.v[i] ));
}
}
else if( mnemonic == "hex" || mnemonic == "rhp" ){
Vector3d vertex( args ), height( args,3 ), RV( args, 6 ), SV, TV;
if( args.size() == 9 ){
SV = RV.rotate_about(height, 60); TV = RV.rotate_about(height, 120);