aboutsummaryrefslogtreecommitdiff
path: root/MultiSource/Benchmarks/DOE-ProxyApps-C/SimpleMOC/solver.c
blob: b9fca131a035ebbb9a8d716f26100ad1a69f0f9e (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
#include"SimpleMOC_header.h"

/* Efficient version of attenuate fluxes which determines the change in angular
 * flux along a particular track across a fine axial region and tallies the 
 * contribution to the scalar flux in the fine axial region. This function
 * assumes a quadratic source, which is calculated on the fly using neighboring
 * source values. 
 * 
 * This version decomposes the work into many for loops for efficient SIMD 
 * instructions and to reduce register pressure. For a more descriptive 
 * (but less effiient) version of the code in terms of the underlying physics, 
 * see alt_attenuate_fluxes which solves the problem in a more naive, 
 * straightforward manner. */
void attenuate_fluxes( Track * track, bool forward, Source * QSR, Input * I_in,
		Params * params_in, float ds, float mu, float az_weight, 
		AttenuateVars * A ) 
{
	Input I = *I_in;
	Params params = *params_in;

	// unload attenuate vars
	float * restrict q0 = A->q0;
	float *  restrict q1 = A->q1;
	float *  restrict q2 = A->q2;
	float *  restrict sigT = A->sigT;
	float *  restrict tau = A->tau;
	float *  restrict sigT2 = A->sigT2;
	float *  restrict expVal = A->expVal;
	float *  restrict reuse = A->reuse;
	float *  restrict flux_integral = A->flux_integral;
	float *  restrict tally = A->tally;
	float *  restrict t1 = A->t1;
	float *  restrict t2 = A->t2;
	float *  restrict t3 = A->t3;
	float *  restrict t4 = A->t4;

	// compute fine axial interval spacing
	float dz = I.height / (I.fai * I.decomp_assemblies_ax * I.cai);

	// compute z height in cell
	float zin = track->z_height - dz * 
		( (int)( track->z_height / dz ) + 0.5f );

	// compute fine axial region ID
	int fine_id = (int) ( track->z_height / dz ) % I.fai;

	// compute weight (azimuthal * polar)
	// NOTE: real app would also have volume weight component
	float weight = track->p_weight * az_weight;
	float mu2 = mu * mu;

	// load fine source region flux vector
	float * FSR_flux = QSR -> fine_flux[fine_id];

	if( fine_id == 0 )
	{
		// adjust z height to account for edge
		zin -= dz;

		// cycle over energy groups
		#ifdef INTEL
		#pragma simd
		#elif defined IBM
		#pragma simd_level(10)
		#endif
		for( int g = 0; g < I.n_egroups; g++)
		{
			// load neighboring sources
			float y1 = QSR->fine_source[fine_id][g];
			float y2 = QSR->fine_source[fine_id+1][g];
			float y3 = QSR->fine_source[fine_id+2][g];

			// do quadratic "fitting"
			float c0 = y2;
			float c1 = (y1 - y3) / (2.f*dz);
			float c2 = (y1 - 2.f*y2 + y3) / (2.f*dz*dz);
			
			// calculate q0, q1, q2
			q0[g] = c0 + c1*zin + c2*zin*zin;
			q1[g] = c1 + 2.f*c2*zin;
			q2[g] = c2;
		}
	}
	else if ( fine_id == I.fai - 1 )
	{
		// adjust z height to account for edge
		zin += dz;
		
		// cycle over energy groups
		#ifdef INTEL
		#pragma simd
		#elif defined IBM
		#pragma simd_level(10)
		#endif
		for( int g = 0; g < I.n_egroups; g++)
		{
			// load neighboring sources
			float y1 = QSR->fine_source[fine_id-2][g];
			float y2 = QSR->fine_source[fine_id-1][g];
			float y3 = QSR->fine_source[fine_id][g];

			// do quadratic "fitting"
			float c0 = y2;
			float c1 = (y1 - y3) / (2.f*dz);
			float c2 = (y1 - 2.f*y2 + y3) / (2.f*dz*dz);

			// calculate q0, q1, q2
			q0[g] = c0 + c1*zin + c2*zin*zin;
			q1[g] = c1 + 2.f*c2*zin;
			q2[g] = c2;
		}
	}
	else
	{
		// cycle over energy groups
		#ifdef INTEL
		#pragma simd
		#elif defined IBM
		#pragma simd_level(10)
		#endif
		for( int g = 0; g < I.n_egroups; g++)
		{
			// load neighboring sources
			float y1 = QSR->fine_source[fine_id-1][g];
			float y2 = QSR->fine_source[fine_id][g];
			float y3 = QSR->fine_source[fine_id+1][g];

			// do quadratic "fitting"
			float c0 = y2;
			float c1 = (y1 - y3) / (2.f*dz);
			float c2 = (y1 - 2.f*y2 + y3) / (2.f*dz*dz);

			// calculate q0, q1, q2
			q0[g] = c0 + c1*zin + c2*zin*zin;
			q1[g] = c1 + 2.f*c2*zin;
			q2[g] = c2;
		}
	}

	// cycle over energy groups
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		// load total cross section
		sigT[g] = QSR->sigT[g];

		// calculate common values for efficiency
		tau[g] = sigT[g] * ds;
		sigT2[g] = sigT[g] * sigT[g];
	}

	// cycle over energy groups
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
		expVal[g] = interpolateTable( params.expTable, tau[g] );  

	// Flux Integral

	// Re-used Term
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		reuse[g] = tau[g] * (tau[g] - 2.f) + 2.f * expVal[g] 
			/ (sigT[g] * sigT2[g]); 
	}


	float * psi;
	if(forward)
		psi = track->f_psi;
	else
		psi = track->b_psi;

	//#pragma vector nontemporal
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		// add contribution to new source flux
		flux_integral[g] = (q0[g] * tau[g] + (sigT[g] * psi[g] - q0[g])
			* expVal[g]) / sigT2[g] + q1[g] * mu * reuse[g] + q2[g] * mu2 
			* (tau[g] * (tau[g] * (tau[g] - 3.f) + 6.f) - 6.f * expVal[g]) 
			/ (3.f * sigT2[g] * sigT2[g]);
	}

	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		// Prepare tally
		tally[g] = weight * flux_integral[g];
	}

	#ifdef OPENMP
	omp_set_lock(QSR->locks + fine_id);
	#endif

	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		FSR_flux[g] += tally[g];
	}

	#ifdef OPENMP
	omp_unset_lock(QSR->locks + fine_id);
	#endif

	// Term 1
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		t1[g] = q0[g] * expVal[g] / sigT[g];  
	}
	// Term 2
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		t2[g] = q1[g] * mu * (tau[g] - expVal[g]) / sigT2[g]; 
	}
	// Term 3
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		t3[g] =	q2[g] * mu2 * reuse[g];
	}
	// Term 4
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		t4[g] = psi[g] * (1.f - expVal[g]);
	}
	// Total psi
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I.n_egroups; g++)
	{
		psi[g] = t1[g] + t2[g] + t3[g] + t4[g];
	}
}	

// single direction transport sweep
void transport_sweep( Params * params, Input * I )
{
	if(I->mype==0) printf("Starting transport sweep ...\n");

	// calculate the height of a node's domain and of each FSR
	double node_delta_z = I->height / I->decomp_assemblies_ax;
	double fine_delta_z = node_delta_z / (I->cai * I->fai);

	/* loop over tracks (implicitly azimuthal angles, tracks in azimuthal 
	 * angles, polar angles, and z stacked rays) */

	//print_Input_struct( I );
	long segments_processed = 0;

	#pragma omp parallel default(none) \
	shared( I, params, node_delta_z, fine_delta_z ) \
	reduction(+ : segments_processed )
	{
		#ifdef OPENMP
		int thread = omp_get_thread_num();
		int nthreads = omp_get_num_threads();
		unsigned int seed = time(NULL) * (thread+1);
		#endif
		//print_Input_struct( I );

		#ifdef PAPI
		int eventset = PAPI_NULL;
		int num_papi_events;
		#pragma omp critical
		{
			counter_init(&eventset, &num_papi_events, I);
		}
		#endif

		AttenuateVars A;
		float * ptr = (float * ) malloc( I->n_egroups * 14 * sizeof(float));
		A.q0 = ptr;
		ptr += I->n_egroups;
		A.q1 = ptr;
		ptr += I->n_egroups;
		A.q2 = ptr;
		ptr += I->n_egroups;
		A.sigT = ptr;
		ptr += I->n_egroups;
		A.tau = ptr;
		ptr += I->n_egroups;
		A.sigT2 = ptr;
		ptr += I->n_egroups;
		A.expVal = ptr;
		ptr += I->n_egroups;
		A.reuse = ptr;
		ptr += I->n_egroups;
		A.flux_integral = ptr;
		ptr += I->n_egroups;
		A.tally = ptr;
		ptr += I->n_egroups;
		A.t1 = ptr;
		ptr += I->n_egroups;
		A.t2 = ptr;
		ptr += I->n_egroups;
		A.t3 = ptr;
		ptr += I->n_egroups;
		A.t4 = ptr;

		#pragma omp for schedule( dynamic ) 
		for (long i = 0; i < I->ntracks_2D; i++)
		{
			#if TIMING_INFO | 0
				// print progress
				#ifdef OPENMP
				if(I->mype==0 && thread == 0)
				{
					printf("\rAttenuating Tracks... (%.0lf%% completed)",
							(i / ( (double)I->ntracks_2D / (double) nthreads ))
							/ (double) nthreads * 100.0);
				}
				#else
				if( i % 50 == 0)
					if(I->mype==0)
						printf("%s%ld%s%ld\n","2D Tracks Completed = ", i," / ", 
								I->ntracks_2D );
				#endif
			#endif


			// treat positive-z traveling rays first
			bool pos_z_dir = true;
			for( int j = 0; j < I->n_polar_angles; j++)
			{
				if( j == I->n_polar_angles / 2 )
					pos_z_dir = false;
				float p_angle = params->polar_angles[j];
				float mu = cos(p_angle);

				// start with all z stacked rays
				int begin_stacked = 0;
				int end_stacked = I->z_stacked;

				for( int n = 0; n < params->tracks_2D[i].n_segments; n++)
				{
					// calculate distance traveled in cell if segment completed
					float s_full = params->tracks_2D[i].segments[n].length 
						/ sin(p_angle);

					// allocate varaible for distance traveled in an FSR
					float ds = 0;

					// loop over remaining z-stacked rays
					for( int k = begin_stacked; k < end_stacked; k++)
					{
						// initialize s to full length
						float s = s_full;

						// select current track
						Track * track = &params->tracks[i][j][k];

						// set flag for completeion of segment
						bool seg_complete = false;

						// calculate interval
						int curr_interval;
						if( pos_z_dir)
							curr_interval = get_pos_interval(track->z_height, 
									fine_delta_z);
						else
							curr_interval = get_neg_interval(track->z_height, 
									fine_delta_z);

						while( !seg_complete )
						{
							// flag to reset z position
							bool reset = false;


							/* calculate new height based on s 
							 * (distance traveled in FSR) */
							float z = track->z_height + s * cos(p_angle);

							// check if still in same FSR (fine axial interval)
							int new_interval;
							if( pos_z_dir )
								new_interval = get_pos_interval(z, 
										fine_delta_z);
							else
								new_interval = get_neg_interval(z,
										fine_delta_z);

							if( new_interval == curr_interval )
							{
								seg_complete = true;
								ds = s;
							}

							// otherwise, we need to recalculate distances
							else
							{
								// correct z
								if( pos_z_dir )
								{
									curr_interval++;
									z = fine_delta_z * (float) curr_interval;
								}
								else{
									curr_interval--;
									z = fine_delta_z * (float) curr_interval;
								}

								// calculate distance travelled in FSR (ds)
								ds = (z - track->z_height) / cos(p_angle);

								// update track length remaining
								s -= ds;

								/* check remaining track length to protect
								 * against potential roundoff errors */
								if( s <= 0 )
									seg_complete = true;

								// check if out of bounds or track complete
								if( z <= 0 || z >= node_delta_z )
								{
									// mark segment as completed
									seg_complete = true;

									// remember to no longer treat this track
									if ( pos_z_dir )
										end_stacked--;
									else
										begin_stacked++;

									// reset z height
									reset = true;
								}
							}

							// pick a random FSR (cache miss expected)
							#ifdef OPENMP
							long QSR_id = rand_r(&seed) % 
								I->n_source_regions_per_node;
							#else
							long QSR_id = rand() % 
								I->n_source_regions_per_node;
							#endif

							/* update sources and fluxes from attenuation 
							 * over FSR */
							if( I->axial_exp == 2 )
							{
								attenuate_fluxes( track, true, 
										&params->sources[QSR_id], 
										I, params, ds, mu, 
										params->tracks_2D[i].az_weight, &A );

								segments_processed++;
							}

							else if( I->axial_exp == 0 )
							{
								attenuate_FSR_fluxes( track, true,
										&params->sources[QSR_id],
										I, params, ds, mu,
										params->tracks_2D[i].az_weight, &A );

								segments_processed++;
							}
							else
							{
								printf("Error: invalid axial expansion order");
								printf("\n Please input 0 or 2\n");
								exit(1);
							}

							// update with new z height or reset if finished
							if( n == params->tracks_2D[i].n_segments - 1  
									|| reset)
							{
								if( pos_z_dir)
									track->z_height = I->axial_z_sep * k;
								else
									track->z_height = I->axial_z_sep * (k+1);
							}
							else
								track->z_height = z;

						}
					}
				}
			}
		}
		#ifdef OPENMP
		if(thread == 0 && I->mype==0) printf("\n");
		#endif

		#ifdef PAPI
		if( thread == 0 )
		{
			printf("\n");
			border_print();
			center_print("PAPI COUNTER RESULTS", 79);
			border_print();
			printf("Count          \tSmybol      \tDescription\n");
		}
		{
			#pragma omp barrier
		}
		counter_stop(&eventset, num_papi_events, I);
		#endif
	}
	I->segments_processed = segments_processed;

	return;
}


// run one full transport sweep, return k
void two_way_transport_sweep( Params * params, Input * I )
{
	if(I->mype==0) printf("Starting transport sweep ...\n");

	// calculate the height of a node's domain and of each FSR
	double node_delta_z = I->height / I->decomp_assemblies_ax;
	int num_intervals = (I->cai * I->fai);
	double fine_delta_z = node_delta_z / num_intervals;

	/* loop over tracks (implicitly azimuthal angles, tracks in azimuthal 
	 * angles, polar angles, and z stacked rays) */
		long segments_processed = 0;

	#pragma omp parallel default(none) \
	shared( I, params, node_delta_z, fine_delta_z, num_intervals ) \
	reduction(+ : segments_processed )
	{
		#ifdef OPENMP
		int thread = omp_get_thread_num();
		int nthreads = omp_get_num_threads();
		unsigned int seed = time(NULL) * (thread+1);
		#endif
		//print_Input_struct( I );

		#ifdef PAPI
		int eventset = PAPI_NULL;
		int num_papi_events;
		#pragma omp critical
		{
			counter_init(&eventset, &num_papi_events, I);
		}
		#endif


		AttenuateVars A;
		float * ptr = (float * ) malloc( I->n_egroups * 14 * sizeof(float));
		A.q0 = ptr;
		ptr += I->n_egroups;
		A.q1 = ptr;
		ptr += I->n_egroups;
		A.q2 = ptr;
		ptr += I->n_egroups;
		A.sigT = ptr;
		ptr += I->n_egroups;
		A.tau = ptr;
		ptr += I->n_egroups;
		A.sigT2 = ptr;
		ptr += I->n_egroups;
		A.expVal = ptr;
		ptr += I->n_egroups;
		A.reuse = ptr;
		ptr += I->n_egroups;
		A.flux_integral = ptr;
		ptr += I->n_egroups;
		A.tally = ptr;
		ptr += I->n_egroups;
		A.t1 = ptr;
		ptr += I->n_egroups;
		A.t2 = ptr;
		ptr += I->n_egroups;
		A.t3 = ptr;
		ptr += I->n_egroups;
		A.t4 = ptr;

		#pragma omp for schedule( dynamic ) 
		for (long i = 0; i < I->ntracks_2D; i++)
		{
			// print progress
			#ifdef OPENMP
			if(I->mype==0 && thread == 0)
			{
				printf("\rAttenuating Tracks... (%.0lf%% completed)",
						(i / ( (double)I->ntracks_2D / (double) nthreads ))
						/ (double) nthreads * 100.0);
			}
			#else
			if( i % 50 == 0)
				if(I->mype==0)
					printf("%s%ld%s%ld\n","2D Tracks Completed = ", i," / ", 
							I->ntracks_2D );
			#endif

			// allocate arrays for segment storage FIXME
			double ** seg_dist = malloc( I->z_stacked * sizeof(double *) );
			Source *** seg_src = malloc( I->z_stacked * sizeof(Source**) );
			int * seg_idx = malloc( I->z_stacked * sizeof(int) );
			int * seg_size = malloc( I->z_stacked * sizeof(int) );

			// fill matrix with arrays FIXME
			for( int k = 0; k < I->z_stacked; k++)
			{
				seg_size[k] = 2 * I->segments_per_track;
				seg_dist[k] = malloc( seg_size[k] * sizeof(double) );
				seg_src[k] = malloc( seg_size[k] * sizeof(Source *) );
				seg_idx[k] = 0;
			}

			// treat positive-z traveling rays first
			bool pos_z_dir = true;
			for( int j = 0; j < I->n_polar_angles; j++)
			{
				if( j == I->n_polar_angles / 2 )
					pos_z_dir = false;
				float p_angle = params->polar_angles[j];
				float mu = cos(p_angle);

				// start with all z stacked rays
				int begin_stacked = 0;
				int end_stacked = I->z_stacked;

				// reset semgnet indexes
				for( int k = 0; k < I->z_stacked; k++)
					seg_idx[k] = 0;

				for( int n = 0; n < params->tracks_2D[i].n_segments; n++)
				{
					// calculate distance traveled in cell if segment completed
					float s_full = params->tracks_2D[i].segments[n].length 
						/ sin(p_angle);

					// allocate varaible for distance traveled in an FSR
					float ds = 0;

					// loop over remaining z-stacked rays
					int tracks_completed = 0;
					for( int k = begin_stacked; k < end_stacked; k++)
					{
						// select current track
						Track * track = &params->tracks[i][j][k];

						// determine current axial interval
						int interval = (int) track->z_height / fine_delta_z;

						// calculate distance to domain boundary
						float bound_dist;
						if( pos_z_dir)
							bound_dist = (node_delta_z - track->z_height) / mu;
						else
							bound_dist = -track->z_height / mu;

						// determine track length
						float s;
						if(	s_full < bound_dist )
							s = s_full;
						else
						{
							// note completion of track
							s = bound_dist;
							tracks_completed++;
						}

						// set flag for completeion of segment
						bool seg_complete = false;

						while( !seg_complete )
						{
							// initialize tracking variables
							long QSR_id = interval + num_intervals * n;
							float ds;
							float z;

							// calculate z height of next fine axial interval
							float fai_z_height;
							if( pos_z_dir )
								fai_z_height = (interval + 1) * fine_delta_z ;
							else
								fai_z_height = interval * fine_delta_z;

							// calculate z distance to next fine axial interval
							float z_dist_to_fai = 
								fai_z_height - track->z_height;

							/* calculate total distance (s) to fine axial 
							 * interval */
							float s_dist_to_fai = z_dist_to_fai / mu;

							// determine if a fine axial interval is crossed
							if( s_dist_to_fai < s )
							{
								if( pos_z_dir )
									interval++;
								else
									interval--;
								ds = s_dist_to_fai;
								z = track->z_height + z_dist_to_fai;
							}
							else
							{
								ds = s;
								z = track->z_height + s * mu;
							}	

							/* shorten remaining segment length and check if
							 * completed (accounting for potential roundoff) */
							s -= ds;
							if( s <= 0 || interval < 0 
									|| interval >= num_intervals)
								seg_complete = true;

							// pick a random FSR (cache miss expected)
							#ifdef OPENMP
							QSR_id = rand_r(&seed) % 
								I->n_source_regions_per_node;
							#else
							QSR_id = rand() % I->n_source_regions_per_node;
							#endif

							/* update sources and fluxes from attenuation 
							 * over FSR */
							if( I->axial_exp == 2 )
							{
								attenuate_fluxes( track, true,
										&params->sources[QSR_id], 
										I, params, ds, mu, 
										params->tracks_2D[i].az_weight, &A );
								segments_processed++;
							}

							else if( I->axial_exp == 0 )
								attenuate_FSR_fluxes( track, true,
										&params->sources[QSR_id],
										I, params, ds, mu,
										params->tracks_2D[i].az_weight, &A );
							else
							{
								printf("Error: invalid axial expansion order");
								printf("\n Please input 0 or 2\n");
								exit(1);
							}

							// update track height
							track->z_height = z;
							
							// save segment length and source FIXME
							seg_dist[k][seg_idx[k]] = ds;
							seg_src[k][seg_idx[k]] = &params->sources[QSR_id];
							seg_idx[k]++;

							// check if array needs to grow FIXME
							if( seg_idx[k] >= seg_size[k] )
							{
								seg_size[k] *= 2;
								seg_dist[k] = (double *) realloc( seg_dist[k],
										seg_size[k] * sizeof(double) );
								seg_src[k] = (Source **) realloc( seg_src[k],
										seg_size[k] * sizeof(Source *) );
							}
						}
					}
					if(pos_z_dir)
						end_stacked -= tracks_completed;
					else
						begin_stacked += tracks_completed;
				}

				// loop over all z stacked rays again
				for( int k = 0; k < I->z_stacked; k++ )
				{
					for( int n = seg_idx[k]-1; n >= 0; n--)
					{
						// load distance
						float ds = seg_dist[k][n];

						// select current track
						Track * track = &params->tracks[i][j][k];

						// update sources and fluxes from attenuation over FSR
						if( I->axial_exp == 2 )
						{
							attenuate_fluxes( track, false,
									seg_src[k][n], 
									I, params, ds, -mu, 
									params->tracks_2D[i].az_weight, &A );
								segments_processed++;
						}

						else if( I->axial_exp == 0 )
							attenuate_FSR_fluxes( track, false,
									seg_src[k][n],
									I, params, ds, -mu,
									params->tracks_2D[i].az_weight, &A );

						// update z height
						track->z_height -= ds * mu;
					}
				}


				/* Update all tracks with correct starting z location again
				 * NOTE: this is only here to acocunt for roundoff error */
				for( int k = 0; k < I->z_stacked; k++)
				{
					Track * track = &params->tracks[i][j][k];
					if( pos_z_dir)
						track->z_height = I->axial_z_sep * k;
					else
						track->z_height = I->axial_z_sep * (k+1);
				}
			}

			// free memory
			for( int k = 0; k < I->z_stacked; k++)
			{
				free(seg_dist[k]);
				free(seg_src[k]);
			}
			free(seg_dist);
			free(seg_src);
			free(seg_idx);
			free(seg_size);

		}
		#ifdef OPENMP
		if(thread == 0 && I->mype==0) printf("\n");
		#endif

		#ifdef PAPI
		if( thread == 0 )
		{
			printf("\n");
			border_print();
			center_print("PAPI COUNTER RESULTS", 79);
			border_print();
			printf("Count          \tSmybol      \tDescription\n");
		}
		{
			#pragma omp barrier
		}
		counter_stop(&eventset, num_papi_events, I);
		#endif
	}
	//printf("Number of segments processed: %ld\n", segments_processed);
	I->segments_processed = segments_processed;

	return;
}

/* returns integer number for axial interval for tracks traveling in the
 *  positive direction */
int get_pos_interval( float z, float dz)
{
	int interval = (int) (z/dz);
	return interval;
}

/* returns integer number for axial interval for tracks traveling in the 
 * negative direction */
int get_neg_interval( float z, float dz)
{
	// NOTE: a bit of trickery using floors to obtain ceils 
	int interval = INT_MAX - (int) ( (double) INT_MAX 
			- (double) ( z / dz ) );
	return interval;
}

int calc_next_fai( float z, float dz, bool pos_dir)
{
	int interval = z/dz;
	float lower_z = dz * (float) interval;
	if(pos_dir)
		return interval + 1;
	else
		return interval;
}

/* Determines the change in angular flux along a particular track across a fine
 * axial region and tallies the contribution to the scalar flux in the fine 
 * axial region. This function assumes a quadratic source, which is calculated 
 * on the fly using neighboring source values.
 *
 * This legacy function is unused since it is less efficient than the current 
 * attenuate_fluxes function. However, it provides a more straightforward 
 * description of the underlying physical problem. */
void alt_attenuate_fluxes( Track * track, bool forward, Source * QSR, Input * I,
		Params * params, float ds, float mu, float az_weight ) 
{
	// compute fine axial interval spacing
	float dz = I->height / (I->fai * I->decomp_assemblies_ax * I->cai);

	// compute z height in cell
	float zin = track->z_height - dz * ( (int)( track->z_height / dz ) + 0.5 );

	// compute fine axial region ID
	int fine_id = (int) ( track->z_height / dz ) % I->fai;

	// compute weight (azimuthal * polar)
	// NOTE: real app would also have volume weight component
	float weight = track->p_weight * az_weight;
	float mu2 = mu * mu;

	// load fine source region flux vector
	float * FSR_flux = QSR -> fine_flux[fine_id];

	// cycle over energy groups
	for( int g = 0; g < I->n_egroups; g++)
	{
		// load total cross section
		float sigT = QSR->sigT[g];

		// define source parameters
		float q0, q1, q2;

		// calculate source components
		if( fine_id == 0 )
		{
			// load neighboring sources
			float y2 = QSR->fine_source[fine_id][g];
			float y3 = QSR->fine_source[fine_id+1][g];

			// do linear "fitting"
			float c0 = y2;
			float c1 = (y3 - y2) / dz;

			// calculate q0, q1, q2
			q0 = c0 + c1*zin;
			q1 = c1;
			q2 = 0;
		}
		else if( fine_id == I->fai - 1 )
		{
			// load neighboring sources
			float y1 = QSR->fine_source[fine_id-1][g];
			float y2 = QSR->fine_source[fine_id][g];

			// do linear "fitting"
			float c0 = y2;
			float c1 = (y2 - y1) / dz;

			// calculate q0, q1, q2
			q0 = c0 + c1*zin;
			q1 = c1;
			q2 = 0;
		}		
		else
		{
			// load neighboring sources
			float y1 = QSR->fine_source[fine_id-1][g];
			float y2 = QSR->fine_source[fine_id][g];
			float y3 = QSR->fine_source[fine_id+1][g];

			// do quadratic "fitting"
			float c0 = y2;
			float c1 = (y1 - y3) / (2*dz);
			float c2 = (y1 - 2*y2 + y3) / (2*dz*dz);

			// calculate q0, q1, q2
			q0 = c0 + c1*zin + c2*zin*zin;
			q1 = c1 + 2*c2*zin;
			q2 = c2;
		}

		// calculate common values for efficiency
		float tau = sigT * ds;
		float sigT2 = sigT * sigT;

		// compute exponential ( 1 - exp(-x) ) using table lookup
		float expVal = interpolateTable( params->expTable, tau );  

		// load correct angular flux vector
		float * psi;
		if(forward)
			psi = track->f_psi;
		else
			psi = track->b_psi;

		// add contribution to new source flux
		float flux_integral = (q0 * tau + (sigT * psi[g] - q0) * expVal)
			/ sigT2
			+ q1 * mu * (tau * (tau - 2) + 2 * expVal)
			/ (sigT * sigT2)
			+ q2 * mu2 * (tau * (tau * (tau - 3) + 6) - 6 * expVal)
			/ (3 * sigT2 * sigT2);

		#pragma omp atomic
		FSR_flux[g] += weight * flux_integral;

		// update angular flux
		psi[g] = psi[g] * (1.0 - expVal) + q0 * expVal / sigT
			+ q1 * mu * (tau - expVal) / sigT2 + q2 * mu2 *
			(tau * (tau - 2) + 2 * expVal) / (sigT2 * sigT);
	}
}

/* Determines the change in angular flux along a particular track across a fine
 * axial region and tallies the contribution to the scalar flux in the fine 
 * axial region. This function assumes a constant  source. */ 
void attenuate_FSR_fluxes( Track * track, bool forward, Source * FSR, Input * I,
		Params * params_in, float ds, float mu, float az_weight, 
		AttenuateVars *A)
{
	// upack attenuate vars struct
	float *  restrict tally = A->tally;
	float *  restrict expVal = A->expVal;
	float *  restrict sigT = A->sigT;
	float *  restrict tau = A->tau;

	Params params = * params_in;

	// compute fine axial interval spacing
	float dz = I->height / (I->fai * I->decomp_assemblies_ax * I->cai);

	// compute z height in cell
	float zin = track->z_height - dz * 
		( (int)( track->z_height / dz ) + 0.5f );

	// compute fine axial region ID
	int fine_id = (int) ( track->z_height / dz ) % I->fai;

	// compute weight (azimuthal * polar)
	// NOTE: real app would also have volume weight component
	float weight = track->p_weight * az_weight * mu;

	// load fine source region flux vector
	float * FSR_flux = FSR -> fine_flux[fine_id];

	// cycle over energy groups
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I->n_egroups; g++)
	{
		// load total cross section
		sigT[g] = FSR->sigT[g];
		tau[g] = sigT[g] * ds;
	}

	// compute exponential ( 1 - exp(-x) ) using table lookup
	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for(int g = 0; g < I->n_egroups; g++)
	{
		expVal[g] = interpolateTable( params.expTable, tau[g] );
	}

	float * psi;
	if(forward)
		psi = track->f_psi;
	else
		psi = track->b_psi;

	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I->n_egroups; g++)
	{
		// compute angular flux attenuation
		float q = FSR->fine_source[fine_id][g] / sigT[g];
		float delta_psi = (psi[g] - q) * expVal[g];

		// add contribution to new source flux
		tally[g] = weight * delta_psi;

		// update angular flux
		psi[g] -= delta_psi;
	}



	#ifdef OPENMP
	omp_set_lock(&FSR->locks[fine_id]);
	#endif

	#ifdef INTEL
	#pragma simd
	#elif defined IBM
	#pragma simd_level(10)
	#endif
	for( int g = 0; g < I->n_egroups; g++)
	{
		FSR_flux[g] += tally[g];
	}

	#ifdef OPENMP
	omp_unset_lock(&FSR->locks[fine_id]);
	#endif


}

/* Renormalizes scalar and angular flux for next transport sweep iteration.
 * Calculation requires multiple pair-wise sums and a reduction accross all 
 * nodes. */
void renormalize_flux( Params params, Input I, CommGrid grid )
{
	if( I.mype == 0 ) printf("Renormalizing Flux...\n");
	float node_fission_rate = 0;
	#ifdef OPENMP
	#pragma omp parallel default(none) shared(params, I, grid) \
	reduction(+ : node_fission_rate)
	{
	#endif
		// tally total fission rate (pair-wise sum)
		float * fission_rates = malloc( I.n_source_regions_per_node 
				* sizeof(float) );

		float * fine_fission_rates = malloc( I.fai * sizeof(float) );
		float * g_fission_rates = malloc( I.n_egroups * sizeof(float) );

		// accumulate total fission rate on node domain
		#pragma omp for schedule(dynamic)
		for( int i = 0; i < I.n_source_regions_per_node; i++)
		{
			Source src = params.sources[i];
			for( int j = 0; j < I.fai; j++)
			{
				for( int g = 0; g < I.n_egroups; g++)
					g_fission_rates[g] = src.fine_flux[j][g] * src.vol 
						* src.XS[g][0];
				fine_fission_rates[j] = pairwise_sum( g_fission_rates, 
						I.n_egroups );
			}
			fission_rates[i] = pairwise_sum( fine_fission_rates, I.fai );
		}
		node_fission_rate = pairwise_sum(fission_rates, 
				I.n_source_regions_per_node);
		
		// free allocated memory
		free(fission_rates);
		free(fine_fission_rates);
		free(g_fission_rates);
	
	#ifdef OPENMP
	}
	#endif

	#ifdef MPI	
	// accumulate total fission rate by MPI Allreduce
	float total_fission_rate = 0;
	MPI_Barrier(grid.cart_comm_3d);
	MPI_Allreduce( &node_fission_rate, // Send Buffer
			&total_fission_rate,    // Receive Buffer
			1,                    	// Element Count
			MPI_FLOAT,           	// Element Type
			MPI_SUM,              	// Reduciton Operation Type
			grid.cart_comm_3d );  	// MPI Communicator
	MPI_Barrier(grid.cart_comm_3d);
	#else
	float total_fission_rate = node_fission_rate;
	#endif


	// normalize fluxes by fission reaction rate
	float norm_factor = 1.0 / total_fission_rate;

	#pragma omp parallel for default(none) \
	shared(I, params) private(norm_factor) schedule(dynamic)
	for( int i = 0; i < I.n_source_regions_per_node; i++)
	{
		Source * src = &params.sources[i];
		float adjust = norm_factor * 4 * M_PI * I.fai / src->vol;
		for( int k = 0; k < I.fai; k++)
			for( int g = 0; g < I.n_egroups; g++)
				src->fine_flux[k][g] *= adjust;
	}

	// normalize boundary fluxes by same factor
	#pragma omp parallel for default(none) \
	shared(I, params) private(norm_factor) schedule(dynamic)
	for( long i = 0; i < I.ntracks_2D; i++)
		for( int j = 0; j < I.n_polar_angles; j++)
			for( int k = 0; k < I.z_stacked; k++)
				for( int g = 0; g < I.n_egroups; g++)
				{
					params.tracks[i][j][k].f_psi[g] *= norm_factor;
					params.tracks[i][j][k].b_psi[g] *= norm_factor;
				}

	if( I.mype == 0 ) printf("Renormalizing Flux Complete.\n");
	return;
}

/* Updates sources for next iteration by computing scattering and fission
 * components. Calculation includes multiple pair-wise sums and reductions
 * accross all nodes */
float update_sources( Params params, Input I, float keff )
{
	// source residual
	float residual;

	// calculate inverse multiplication facotr for efficiency
	float inverse_k = 1.0 / keff;

	// allocate residual arrays
	float * group_res = (float *) malloc(I.n_egroups * sizeof(float));
	float * fine_res = (float *) malloc(I.fai * sizeof(float));
	float * residuals = (float *) malloc(I.n_source_regions_per_node 
			* sizeof(float));

	// allocate arrays for summation
	float * fission_rates = malloc(I.n_egroups * sizeof(float));
	float * scatter_rates = malloc(I.n_egroups * sizeof(float));

	// cycle through all coarse axial intervals to update source
	for( long i = 0; i < I.n_source_regions_per_node; i++)
	{
		Source src = params.sources[i];

		// cycle thorugh all fine axial regions to calculate new source
		for( int j = 0; j < I.fai; j++)
		{
			// calculate total fission source and scattering source
			float fission_source;
			float scatter_source;

			// compute total fission source
			for( int g = 0; g < I.n_egroups; g++ )
				fission_rates[g] = src.fine_flux[j][g] * src.XS[g][0];
			fission_source = pairwise_sum( fission_rates, (long) I.n_egroups);

			// normalize fission source by multiplication factor
			fission_source *= inverse_k;

			// compute scattering and new total source for each group
			for( int g = 0; g < I.n_egroups; g++ )
			{
				for( int g2 = 0; g2 < I.n_egroups; g2++ )
				{
					// compute scatter source originating from g2 -> g
					scatter_rates[g2] = src.scattering_matrix[g][g2] * 
						src.fine_flux[j][g2];
				}
				scatter_source = pairwise_sum(scatter_rates, 
						(long) I.n_egroups);

				// compuate new total source
				float chi = src.XS[g][2];

				// calculate new fine source
				float newSrc = (fission_source * chi + scatter_source) 
					/ (4.0 * M_PI);

				// calculate residual
				float oldSrc = src.fine_source[j][g];
				group_res[g] = (newSrc - oldSrc) * (newSrc - oldSrc)
					/ (oldSrc * oldSrc);

				/* calculate new source in fine axial interval assuming 
				 * isotropic source components */
				src.fine_source[j][g] = newSrc;
			}
			fine_res[j] = pairwise_sum(group_res, (long) I.n_egroups);
		}
		residuals[i] = pairwise_sum(fine_res, (long) I.fai);
	}

	// calculate source residual
	residual = pairwise_sum(residuals, I.n_source_regions_per_node);

	// free memory
	free(fission_rates);
	free(scatter_rates);
	free(group_res);
	free(fine_res);
	free(residuals);


	// NOTE: See code around line 600 of CPUSolver.cpp in ClosedMOC/ OpenMOC

	return residual;
}

/* Computes globall k-effective using multiple pair-wise summations and finally
 * a reduction accross all nodes */
float compute_keff(Params params, Input I, CommGrid grid)
{
	// allocate temporary memory
	float * sigma = malloc( I.n_egroups * sizeof(float) );
	float * group_rates = malloc( I.n_egroups * sizeof(float) );
	float * fine_rates = malloc( I.fai * sizeof(float) );
	float * QSR_rates = malloc( I.n_source_regions_per_node * sizeof(float) );

	///////////////////////////////////////////////////////////////////////////

	// compute total absorption rate, looping over source regions
	for( long i = 0; i < I.n_source_regions_per_node; i++)
	{
		// load absorption XS data
		Source src = params.sources[i];
		for( int g = 0; g < I.n_egroups; g++)	
			sigma[g] = src.XS[g][1];

		for( int j = 0; j < I.fai; j++ )
		{
			// calculate absorption rates
			float * fine_flux = src.fine_flux[j];
			for( int g = 0; g < I.n_egroups; g++)
				group_rates[g] = sigma[g] * fine_flux[g];

			// sum absorption over all energy groups
			fine_rates[j] = pairwise_sum( group_rates, (long) I.n_egroups );
		}
		// sum absorption over all fine axial intervals
		QSR_rates[i] = pairwise_sum( fine_rates, (long) I.fai );
	}
	// sum absorption over all source regions in a node
	float node_abs = pairwise_sum( QSR_rates, I.n_source_regions_per_node);

	///////////////////////////////////////////////////////////////////////////

	// compute total absorption rate, looping over source regions
	for( long i = 0; i < I.n_source_regions_per_node; i++)
	{
		// load nuSigmaF XS data
		Source src = params.sources[i];
		for( int g = 0; g < I.n_egroups; g++)	
			sigma[g] = src.XS[g][0];

		for( int j = 0; j < I.fai; j++ )
		{
			// calculate absorption rates
			float * fine_flux = src.fine_flux[j];
			for( int g = 0; g < I.n_egroups; g++)
				group_rates[g] = sigma[g] * fine_flux[g];

			// sum fission over all energy groups
			fine_rates[j] = pairwise_sum( group_rates, (long) I.n_egroups );
		}
		// sum fission over all fine axial intervals
		QSR_rates[i] = pairwise_sum( fine_rates, (long) I.fai );
	}
	// sum fission over all source regions in a node
	float node_fission = pairwise_sum( QSR_rates, I.n_source_regions_per_node);

	///////////////////////////////////////////////////////////////////////////

	// MPi Reduction
	float tot_abs = 0;
	float tot_fission = 0;
	float leakage = 0;

	#ifdef MPI 

	// Total Absorption Reduction
	MPI_Reduce( &node_abs,    		// Send Buffer
			&tot_abs,      			// Receive Buffer
			1,                  	// Element Count
			MPI_FLOAT,          	// Element Type
			MPI_SUM,            	// Reduciton Operation Type
			0,                  	// Master Rank
			grid.cart_comm_3d );	// MPI Communicator

	// Total Fission Reduction
	MPI_Reduce( &node_fission,     	// Send Buffer
			&tot_fission,  			// Receive Buffer
			1,                    	// Element Count
			MPI_FLOAT,           	// Element Type
			MPI_SUM,              	// Reduciton Operation Type
			0,                    	// Master Rank
			grid.cart_comm_3d );  	// MPI Communicator

	// Total Leakage Reduction
	MPI_Reduce( params.leakage,  	// Send Buffer
			&leakage,      			// Receive Buffer
			1,                    	// Element Count
			MPI_FLOAT,           	// Element Type
			MPI_SUM,              	// Reduciton Operation Type
			0,                    	// Master Rank
			grid.cart_comm_3d );  	// MPI Communicator

	MPI_Barrier(grid.cart_comm_3d);

	// calculate keff
	float keff = tot_fission/ (tot_abs + leakage);
	#else
	float keff = node_fission / (node_abs + *params.leakage);
	#endif

	///////////////////////////////////////////////////////////////////////////

	// free memory
	free(sigma);
	free(group_rates);
	free(fine_rates);
	free(QSR_rates);

	return keff;
}

/* Interpolates a formed exponential table to compute ( 1- exp(-x) )
 *  at the desired x value */
float interpolateTable( Table table, float x)
{
	// check to ensure value is in domain
	if( x > table.maxVal )
		return 1.0f;
	else
	{
		int interval = (int) ( x / table.dx + 0.5f * table.dx );
		/*
		   if( interval >= table.N || interval < 0)
		   {
		   printf( "Interval = %d\n", interval);
		   printf( "N = %d\n", table.N);
		   printf( "x = %f\n", x);
		   printf( "dx = %f\n", table.dx);
		   exit(1);
		   }
		   */
		float slope = table.values[ 2 * interval ];
		float intercept = table.values[ 2 * interval + 1 ];
		float val = slope * x + intercept;
		return val;
	}
}