forked from openmc-dev/openmc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflat_source_domain.cpp
1558 lines (1384 loc) · 58.4 KB
/
flat_source_domain.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 "openmc/random_ray/flat_source_domain.h"
#include "openmc/cell.h"
#include "openmc/constants.h"
#include "openmc/eigenvalue.h"
#include "openmc/geometry.h"
#include "openmc/material.h"
#include "openmc/message_passing.h"
#include "openmc/mgxs_interface.h"
#include "openmc/output.h"
#include "openmc/plot.h"
#include "openmc/random_ray/random_ray.h"
#include "openmc/simulation.h"
#include "openmc/tallies/filter.h"
#include "openmc/tallies/tally.h"
#include "openmc/tallies/tally_scoring.h"
#include "openmc/timer.h"
#include "openmc/weight_windows.h"
#include <cstdio>
namespace openmc {
//==============================================================================
// FlatSourceDomain implementation
//==============================================================================
// Static Variable Declarations
RandomRayVolumeEstimator FlatSourceDomain::volume_estimator_ {
RandomRayVolumeEstimator::HYBRID};
bool FlatSourceDomain::volume_normalized_flux_tallies_ {false};
bool FlatSourceDomain::adjoint_ {false};
double FlatSourceDomain::diagonal_stabilization_rho_ {1.0};
std::unordered_map<int, vector<std::pair<Source::DomainType, int>>>
FlatSourceDomain::mesh_domain_map_;
FlatSourceDomain::FlatSourceDomain() : negroups_(data::mg.num_energy_groups_)
{
// Count the number of source regions, compute the cell offset
// indices, and store the material type The reason for the offsets is that
// some cell types may not have material fills, and therefore do not
// produce FSRs. Thus, we cannot index into the global arrays directly
int base_source_regions = 0;
for (const auto& c : model::cells) {
if (c->type_ != Fill::MATERIAL) {
source_region_offsets_.push_back(-1);
} else {
source_region_offsets_.push_back(base_source_regions);
base_source_regions += c->n_instances_;
}
}
// Initialize source regions.
bool is_linear = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
source_regions_ = SourceRegionContainer(negroups_, is_linear);
source_regions_.assign(
base_source_regions, SourceRegion(negroups_, is_linear));
// Initialize materials
int64_t source_region_id = 0;
for (int i = 0; i < model::cells.size(); i++) {
Cell& cell = *model::cells[i];
if (cell.type_ == Fill::MATERIAL) {
for (int j = 0; j < cell.n_instances_; j++) {
source_regions_.material(source_region_id++) = cell.material(j);
}
}
}
// Sanity check
if (source_region_id != base_source_regions) {
fatal_error("Unexpected number of source regions");
}
// Initialize tally volumes
if (volume_normalized_flux_tallies_) {
tally_volumes_.resize(model::tallies.size());
for (int i = 0; i < model::tallies.size(); i++) {
// Get the shape of the 3D result tensor
auto shape = model::tallies[i]->results().shape();
// Create a new 2D tensor with the same size as the first
// two dimensions of the 3D tensor
tally_volumes_[i] =
xt::xtensor<double, 2>::from_shape({shape[0], shape[1]});
}
}
// Compute simulation domain volume based on ray source
auto* is = dynamic_cast<IndependentSource*>(RandomRay::ray_source_.get());
SpatialDistribution* space_dist = is->space();
SpatialBox* sb = dynamic_cast<SpatialBox*>(space_dist);
Position dims = sb->upper_right() - sb->lower_left();
simulation_volume_ = dims.x * dims.y * dims.z;
}
void FlatSourceDomain::batch_reset()
{
// Reset scalar fluxes and iteration volume tallies to zero
#pragma omp parallel for
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
source_regions_.volume(sr) = 0.0;
source_regions_.volume_sq(sr) = 0.0;
}
#pragma omp parallel for
for (int64_t se = 0; se < n_source_elements(); se++) {
source_regions_.scalar_flux_new(se) = 0.0;
}
}
void FlatSourceDomain::accumulate_iteration_flux()
{
#pragma omp parallel for
for (int64_t se = 0; se < n_source_elements(); se++) {
source_regions_.scalar_flux_final(se) +=
source_regions_.scalar_flux_new(se);
}
}
// Compute new estimate of scattering + fission sources in each source region
// based on the flux estimate from the previous iteration.
void FlatSourceDomain::update_neutron_source(double k_eff)
{
simulation::time_update_src.start();
double inverse_k_eff = 1.0 / k_eff;
// Reset all source regions to zero (important for void regions)
#pragma omp parallel for
for (int64_t se = 0; se < n_source_elements(); se++) {
source_regions_.source(se) = 0.0;
}
// Add scattering + fission source
#pragma omp parallel for
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
int material = source_regions_.material(sr);
if (material == MATERIAL_VOID) {
continue;
}
for (int g_out = 0; g_out < negroups_; g_out++) {
double sigma_t = sigma_t_[material * negroups_ + g_out];
double scatter_source = 0.0;
double fission_source = 0.0;
for (int g_in = 0; g_in < negroups_; g_in++) {
double scalar_flux = source_regions_.scalar_flux_old(sr, g_in);
double sigma_s =
sigma_s_[material * negroups_ * negroups_ + g_out * negroups_ + g_in];
double nu_sigma_f = nu_sigma_f_[material * negroups_ + g_in];
double chi = chi_[material * negroups_ + g_out];
scatter_source += sigma_s * scalar_flux;
fission_source += nu_sigma_f * scalar_flux * chi;
}
source_regions_.source(sr, g_out) =
(scatter_source + fission_source * inverse_k_eff) / sigma_t;
}
}
// Add external source if in fixed source mode
if (settings::run_mode == RunMode::FIXED_SOURCE) {
#pragma omp parallel for
for (int64_t se = 0; se < n_source_elements(); se++) {
source_regions_.source(se) += source_regions_.external_source(se);
}
}
simulation::time_update_src.stop();
}
// Normalizes flux and updates simulation-averaged volume estimate
void FlatSourceDomain::normalize_scalar_flux_and_volumes(
double total_active_distance_per_iteration)
{
double normalization_factor = 1.0 / total_active_distance_per_iteration;
double volume_normalization_factor =
1.0 / (total_active_distance_per_iteration * simulation::current_batch);
// Normalize scalar flux to total distance travelled by all rays this
// iteration
#pragma omp parallel for
for (int64_t se = 0; se < n_source_elements(); se++) {
source_regions_.scalar_flux_new(se) *= normalization_factor;
}
// Accumulate cell-wise ray length tallies collected this iteration, then
// update the simulation-averaged cell-wise volume estimates
#pragma omp parallel for
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
source_regions_.volume_t(sr) += source_regions_.volume(sr);
source_regions_.volume_sq_t(sr) += source_regions_.volume_sq(sr);
source_regions_.volume_naive(sr) =
source_regions_.volume(sr) * normalization_factor;
source_regions_.volume_sq(sr) =
source_regions_.volume_sq_t(sr) / source_regions_.volume_t(sr);
source_regions_.volume(sr) =
source_regions_.volume_t(sr) * volume_normalization_factor;
}
}
void FlatSourceDomain::set_flux_to_flux_plus_source(
int64_t sr, double volume, int g)
{
int material = source_regions_.material(sr);
if (material == MATERIAL_VOID) {
source_regions_.scalar_flux_new(sr, g) /= volume;
source_regions_.scalar_flux_new(sr, g) +=
0.5f * source_regions_.external_source(sr, g) *
source_regions_.volume_sq(sr);
} else {
double sigma_t = sigma_t_[source_regions_.material(sr) * negroups_ + g];
source_regions_.scalar_flux_new(sr, g) /= (sigma_t * volume);
source_regions_.scalar_flux_new(sr, g) += source_regions_.source(sr, g);
}
}
void FlatSourceDomain::set_flux_to_old_flux(int64_t sr, int g)
{
source_regions_.scalar_flux_new(sr, g) =
source_regions_.scalar_flux_old(sr, g);
}
void FlatSourceDomain::set_flux_to_source(int64_t sr, int g)
{
source_regions_.scalar_flux_new(sr, g) = source_regions_.source(sr, g);
}
// Combine transport flux contributions and flat source contributions from the
// previous iteration to generate this iteration's estimate of scalar flux.
int64_t FlatSourceDomain::add_source_to_scalar_flux()
{
int64_t n_hits = 0;
double inverse_batch = 1.0 / simulation::current_batch;
#pragma omp parallel for reduction(+ : n_hits)
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
double volume_simulation_avg = source_regions_.volume(sr);
double volume_iteration = source_regions_.volume_naive(sr);
// Increment the number of hits if cell was hit this iteration
if (volume_iteration) {
n_hits++;
}
// Set the SR to small status if its expected number of hits
// per iteration is less than 1.5
if (source_regions_.n_hits(sr) * inverse_batch < MIN_HITS_PER_BATCH) {
source_regions_.is_small(sr) = 1;
} else {
source_regions_.is_small(sr) = 0;
}
// The volume treatment depends on the volume estimator type
// and whether or not an external source is present in the cell.
double volume;
switch (volume_estimator_) {
case RandomRayVolumeEstimator::NAIVE:
volume = volume_iteration;
break;
case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
volume = volume_simulation_avg;
break;
case RandomRayVolumeEstimator::HYBRID:
if (source_regions_.external_source_present(sr) ||
source_regions_.is_small(sr)) {
volume = volume_iteration;
} else {
volume = volume_simulation_avg;
}
break;
default:
fatal_error("Invalid volume estimator type");
}
for (int g = 0; g < negroups_; g++) {
// There are three scenarios we need to consider:
if (volume_iteration > 0.0) {
// 1. If the FSR was hit this iteration, then the new flux is equal to
// the flat source from the previous iteration plus the contributions
// from rays passing through the source region (computed during the
// transport sweep)
set_flux_to_flux_plus_source(sr, volume, g);
} else if (volume_simulation_avg > 0.0) {
// 2. If the FSR was not hit this iteration, but has been hit some
// previous iteration, then we need to make a choice about what
// to do. Naively we will usually want to set the flux to be equal
// to the reduced source. However, in fixed source problems where
// there is a strong external source present in the cell, and where
// the cell has a very low cross section, this approximation will
// cause a huge upward bias in the flux estimate of the cell (in these
// conditions, the flux estimate can be orders of magnitude too large).
// Thus, to avoid this bias, if any external source is present
// in the cell we will use the previous iteration's flux estimate. This
// injects a small degree of correlation into the simulation, but this
// is going to be trivial when the miss rate is a few percent or less.
if (source_regions_.external_source_present(sr) && !adjoint_) {
set_flux_to_old_flux(sr, g);
} else {
set_flux_to_source(sr, g);
}
}
}
}
// Return the number of source regions that were hit this iteration
return n_hits;
}
// Generates new estimate of k_eff based on the differences between this
// iteration's estimate of the scalar flux and the last iteration's estimate.
double FlatSourceDomain::compute_k_eff(double k_eff_old) const
{
double fission_rate_old = 0;
double fission_rate_new = 0;
// Vector for gathering fission source terms for Shannon entropy calculation
vector<float> p(n_source_regions(), 0.0f);
#pragma omp parallel for reduction(+ : fission_rate_old, fission_rate_new)
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
// If simulation averaged volume is zero, don't include this cell
double volume = source_regions_.volume(sr);
if (volume == 0.0) {
continue;
}
int material = source_regions_.material(sr);
if (material == MATERIAL_VOID) {
continue;
}
double sr_fission_source_old = 0;
double sr_fission_source_new = 0;
for (int g = 0; g < negroups_; g++) {
double nu_sigma_f = nu_sigma_f_[material * negroups_ + g];
sr_fission_source_old +=
nu_sigma_f * source_regions_.scalar_flux_old(sr, g);
sr_fission_source_new +=
nu_sigma_f * source_regions_.scalar_flux_new(sr, g);
}
// Compute total fission rates in FSR
sr_fission_source_old *= volume;
sr_fission_source_new *= volume;
// Accumulate totals
fission_rate_old += sr_fission_source_old;
fission_rate_new += sr_fission_source_new;
// Store total fission rate in the FSR for Shannon calculation
p[sr] = sr_fission_source_new;
}
double k_eff_new = k_eff_old * (fission_rate_new / fission_rate_old);
double H = 0.0;
// defining an inverse sum for better performance
double inverse_sum = 1 / fission_rate_new;
#pragma omp parallel for reduction(+ : H)
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
// Only if FSR has non-negative and non-zero fission source
if (p[sr] > 0.0f) {
// Normalize to total weight of bank sites. p_i for better performance
float p_i = p[sr] * inverse_sum;
// Sum values to obtain Shannon entropy.
H -= p_i * std::log2(p_i);
}
}
// Adds entropy value to shared entropy vector in openmc namespace.
simulation::entropy.push_back(H);
return k_eff_new;
}
// This function is responsible for generating a mapping between random
// ray flat source regions (cell instances) and tally bins. The mapping
// takes the form of a "TallyTask" object, which accounts for one single
// score being applied to a single tally. Thus, a single source region
// may have anywhere from zero to many tally tasks associated with it ---
// meaning that the global "tally_task" data structure is in 2D. The outer
// dimension corresponds to the source element (i.e., each entry corresponds
// to a specific energy group within a specific source region), and the
// inner dimension corresponds to the tallying task itself. Mechanically,
// the mapping between FSRs and spatial filters is done by considering
// the location of a single known ray midpoint that passed through the
// FSR. I.e., during transport, the first ray to pass through a given FSR
// will write down its midpoint for use with this function. This is a cheap
// and easy way of mapping FSRs to spatial tally filters, but comes with
// the downside of adding the restriction that spatial tally filters must
// share boundaries with the physical geometry of the simulation (so as
// not to subdivide any FSR). It is acceptable for a spatial tally region
// to contain multiple FSRs, but not the other way around.
// TODO: In future work, it would be preferable to offer a more general
// (but perhaps slightly more expensive) option for handling arbitrary
// spatial tallies that would be allowed to subdivide FSRs.
// Besides generating the mapping structure, this function also keeps track
// of whether or not all flat source regions have been hit yet. This is
// required, as there is no guarantee that all flat source regions will
// be hit every iteration, such that in the first few iterations some FSRs
// may not have a known position within them yet to facilitate mapping to
// spatial tally filters. However, after several iterations, if all FSRs
// have been hit and have had a tally map generated, then this status will
// be passed back to the caller to alert them that this function doesn't
// need to be called for the remainder of the simulation.
void FlatSourceDomain::convert_source_regions_to_tallies()
{
openmc::simulation::time_tallies.start();
// Tracks if we've generated a mapping yet for all source regions.
bool all_source_regions_mapped = true;
// Attempt to generate mapping for all source regions
#pragma omp parallel for
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
// If this source region has not been hit by a ray yet, then
// we aren't going to be able to map it, so skip it.
if (!source_regions_.position_recorded(sr)) {
all_source_regions_mapped = false;
continue;
}
// A particle located at the recorded midpoint of a ray
// crossing through this source region is used to estabilish
// the spatial location of the source region
Particle p;
p.r() = source_regions_.position(sr);
p.r_last() = source_regions_.position(sr);
p.u() = {1.0, 0.0, 0.0};
bool found = exhaustive_find_cell(p);
// Loop over energy groups (so as to support energy filters)
for (int g = 0; g < negroups_; g++) {
// Set particle to the current energy
p.g() = g;
p.g_last() = g;
p.E() = data::mg.energy_bin_avg_[p.g()];
p.E_last() = p.E();
int64_t source_element = sr * negroups_ + g;
// If this task has already been populated, we don't need to do
// it again.
if (source_regions_.tally_task(sr, g).size() > 0) {
continue;
}
// Loop over all active tallies. This logic is essentially identical
// to what happens when scanning for applicable tallies during
// MC transport.
for (auto i_tally : model::active_tallies) {
Tally& tally {*model::tallies[i_tally]};
// Initialize an iterator over valid filter bin combinations.
// If there are no valid combinations, use a continue statement
// to ensure we skip the assume_separate break below.
auto filter_iter = FilterBinIter(tally, p);
auto end = FilterBinIter(tally, true, &p.filter_matches());
if (filter_iter == end)
continue;
// Loop over filter bins.
for (; filter_iter != end; ++filter_iter) {
auto filter_index = filter_iter.index_;
auto filter_weight = filter_iter.weight_;
// Loop over scores
for (int score = 0; score < tally.scores_.size(); score++) {
auto score_bin = tally.scores_[score];
// If a valid tally, filter, and score combination has been found,
// then add it to the list of tally tasks for this source element.
TallyTask task(i_tally, filter_index, score, score_bin);
source_regions_.tally_task(sr, g).push_back(task);
// Also add this task to the list of volume tasks for this source
// region.
source_regions_.volume_task(sr).insert(task);
}
}
}
// Reset all the filter matches for the next tally event.
for (auto& match : p.filter_matches())
match.bins_present_ = false;
}
}
openmc::simulation::time_tallies.stop();
mapped_all_tallies_ = all_source_regions_mapped;
}
// Set the volume accumulators to zero for all tallies
void FlatSourceDomain::reset_tally_volumes()
{
if (volume_normalized_flux_tallies_) {
#pragma omp parallel for
for (int i = 0; i < tally_volumes_.size(); i++) {
auto& tensor = tally_volumes_[i];
tensor.fill(0.0); // Set all elements of the tensor to 0.0
}
}
}
// In fixed source mode, due to the way that volumetric fixed sources are
// converted and applied as volumetric sources in one or more source regions,
// we need to perform an additional normalization step to ensure that the
// reported scalar fluxes are in units per source neutron. This allows for
// direct comparison of reported tallies to Monte Carlo flux results.
// This factor needs to be computed at each iteration, as it is based on the
// volume estimate of each FSR, which improves over the course of the
// simulation
double FlatSourceDomain::compute_fixed_source_normalization_factor() const
{
// If we are not in fixed source mode, then there are no external sources
// so no normalization is needed.
if (settings::run_mode != RunMode::FIXED_SOURCE || adjoint_) {
return 1.0;
}
// Step 1 is to sum over all source regions and energy groups to get the
// total external source strength in the simulation.
double simulation_external_source_strength = 0.0;
#pragma omp parallel for reduction(+ : simulation_external_source_strength)
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
int material = source_regions_.material(sr);
double volume = source_regions_.volume(sr) * simulation_volume_;
for (int g = 0; g < negroups_; g++) {
// For non-void regions, we store the external source pre-divided by
// sigma_t. We need to multiply non-void regions back up by sigma_t
// to get the total source strength in the expected units.
double sigma_t = 1.0;
if (material != MATERIAL_VOID) {
sigma_t = sigma_t_[material * negroups_ + g];
}
simulation_external_source_strength +=
source_regions_.external_source(sr, g) * sigma_t * volume;
}
}
// Step 2 is to determine the total user-specified external source strength
double user_external_source_strength = 0.0;
for (auto& ext_source : model::external_sources) {
user_external_source_strength += ext_source->strength();
}
// The correction factor is the ratio of the user-specified external source
// strength to the simulation external source strength.
double source_normalization_factor =
user_external_source_strength / simulation_external_source_strength;
return source_normalization_factor;
}
// Tallying in random ray is not done directly during transport, rather,
// it is done only once after each power iteration. This is made possible
// by way of a mapping data structure that relates spatial source regions
// (FSRs) to tally/filter/score combinations. The mechanism by which the
// mapping is done (and the limitations incurred) is documented in the
// "convert_source_regions_to_tallies()" function comments above. The present
// tally function simply traverses the mapping data structure and executes
// the scoring operations to OpenMC's native tally result arrays.
void FlatSourceDomain::random_ray_tally()
{
openmc::simulation::time_tallies.start();
// Reset our tally volumes to zero
reset_tally_volumes();
double source_normalization_factor =
compute_fixed_source_normalization_factor();
// We loop over all source regions and energy groups. For each
// element, we check if there are any scores needed and apply
// them.
#pragma omp parallel for
for (int64_t sr = 0; sr < n_source_regions(); sr++) {
// The fsr.volume_ is the unitless fractional simulation averaged volume
// (i.e., it is the FSR's fraction of the overall simulation volume). The
// simulation_volume_ is the total 3D physical volume in cm^3 of the
// entire global simulation domain (as defined by the ray source box).
// Thus, the FSR's true 3D spatial volume in cm^3 is found by multiplying
// its fraction of the total volume by the total volume. Not important in
// eigenvalue solves, but useful in fixed source solves for returning the
// flux shape with a magnitude that makes sense relative to the fixed
// source strength.
double volume = source_regions_.volume(sr) * simulation_volume_;
double material = source_regions_.material(sr);
for (int g = 0; g < negroups_; g++) {
double flux =
source_regions_.scalar_flux_new(sr, g) * source_normalization_factor;
// Determine numerical score value
for (auto& task : source_regions_.tally_task(sr, g)) {
double score = 0.0;
switch (task.score_type) {
case SCORE_FLUX:
score = flux * volume;
break;
case SCORE_TOTAL:
if (material != MATERIAL_VOID) {
score = flux * volume * sigma_t_[material * negroups_ + g];
}
break;
case SCORE_FISSION:
if (material != MATERIAL_VOID) {
score = flux * volume * sigma_f_[material * negroups_ + g];
}
break;
case SCORE_NU_FISSION:
if (material != MATERIAL_VOID) {
score = flux * volume * nu_sigma_f_[material * negroups_ + g];
}
break;
case SCORE_EVENTS:
score = 1.0;
break;
default:
fatal_error("Invalid score specified in tallies.xml. Only flux, "
"total, fission, nu-fission, and events are supported in "
"random ray mode.");
break;
}
// Apply score to the appropriate tally bin
Tally& tally {*model::tallies[task.tally_idx]};
#pragma omp atomic
tally.results_(task.filter_idx, task.score_idx, TallyResult::VALUE) +=
score;
}
}
// For flux tallies, the total volume of the spatial region is needed
// for normalizing the flux. We store this volume in a separate tensor.
// We only contribute to each volume tally bin once per FSR.
if (volume_normalized_flux_tallies_) {
for (const auto& task : source_regions_.volume_task(sr)) {
if (task.score_type == SCORE_FLUX) {
#pragma omp atomic
tally_volumes_[task.tally_idx](task.filter_idx, task.score_idx) +=
volume;
}
}
}
} // end FSR loop
// Normalize any flux scores by the total volume of the FSRs scoring to that
// bin. To do this, we loop over all tallies, and then all filter bins,
// and then scores. For each score, we check the tally data structure to
// see what index that score corresponds to. If that score is a flux score,
// then we divide it by volume.
if (volume_normalized_flux_tallies_) {
for (int i = 0; i < model::tallies.size(); i++) {
Tally& tally {*model::tallies[i]};
#pragma omp parallel for
for (int bin = 0; bin < tally.n_filter_bins(); bin++) {
for (int score_idx = 0; score_idx < tally.n_scores(); score_idx++) {
auto score_type = tally.scores_[score_idx];
if (score_type == SCORE_FLUX) {
double vol = tally_volumes_[i](bin, score_idx);
if (vol > 0.0) {
tally.results_(bin, score_idx, TallyResult::VALUE) /= vol;
}
}
}
}
}
}
openmc::simulation::time_tallies.stop();
}
double FlatSourceDomain::evaluate_flux_at_point(
Position r, int64_t sr, int g) const
{
return source_regions_.scalar_flux_final(sr, g) /
(settings::n_batches - settings::n_inactive);
}
// Outputs all basic material, FSR ID, multigroup flux, and
// fission source data to .vtk file that can be directly
// loaded and displayed by Paraview. Note that .vtk binary
// files require big endian byte ordering, so endianness
// is checked and flipped if necessary.
void FlatSourceDomain::output_to_vtk() const
{
// Rename .h5 plot filename(s) to .vtk filenames
for (int p = 0; p < model::plots.size(); p++) {
PlottableInterface* plot = model::plots[p].get();
plot->path_plot() =
plot->path_plot().substr(0, plot->path_plot().find_last_of('.')) + ".vtk";
}
// Print header information
print_plot();
// Outer loop over plots
for (int p = 0; p < model::plots.size(); p++) {
// Get handle to OpenMC plot object and extract params
Plot* openmc_plot = dynamic_cast<Plot*>(model::plots[p].get());
// Random ray plots only support voxel plots
if (!openmc_plot) {
warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
"is allowed in random ray mode.",
p));
continue;
} else if (openmc_plot->type_ != Plot::PlotType::voxel) {
warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
"is allowed in random ray mode.",
p));
continue;
}
int Nx = openmc_plot->pixels_[0];
int Ny = openmc_plot->pixels_[1];
int Nz = openmc_plot->pixels_[2];
Position origin = openmc_plot->origin_;
Position width = openmc_plot->width_;
Position ll = origin - width / 2.0;
double x_delta = width.x / Nx;
double y_delta = width.y / Ny;
double z_delta = width.z / Nz;
std::string filename = openmc_plot->path_plot();
// Perform sanity checks on file size
uint64_t bytes = Nx * Ny * Nz * (negroups_ + 1 + 1 + 1) * sizeof(float);
write_message(5, "Processing plot {}: {}... (Estimated size is {} MB)",
openmc_plot->id(), filename, bytes / 1.0e6);
if (bytes / 1.0e9 > 1.0) {
warning("Voxel plot specification is very large (>1 GB). Plotting may be "
"slow.");
} else if (bytes / 1.0e9 > 100.0) {
fatal_error("Voxel plot specification is too large (>100 GB). Exiting.");
}
// Relate voxel spatial locations to random ray source regions
vector<int> voxel_indices(Nx * Ny * Nz);
vector<Position> voxel_positions(Nx * Ny * Nz);
vector<double> weight_windows(Nx * Ny * Nz);
float min_weight = 1e20;
#pragma omp parallel for collapse(3) reduction(min : min_weight)
for (int z = 0; z < Nz; z++) {
for (int y = 0; y < Ny; y++) {
for (int x = 0; x < Nx; x++) {
Position sample;
sample.z = ll.z + z_delta / 2.0 + z * z_delta;
sample.y = ll.y + y_delta / 2.0 + y * y_delta;
sample.x = ll.x + x_delta / 2.0 + x * x_delta;
Particle p;
p.r() = sample;
p.r_last() = sample;
p.E() = 1.0;
p.E_last() = 1.0;
p.u() = {1.0, 0.0, 0.0};
bool found = exhaustive_find_cell(p);
if (!found) {
voxel_indices[z * Ny * Nx + y * Nx + x] = -1;
voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
weight_windows[z * Ny * Nx + y * Nx + x] = 0.0;
continue;
}
int i_cell = p.lowest_coord().cell;
int64_t sr = source_region_offsets_[i_cell] + p.cell_instance();
if (RandomRay::mesh_subdivision_enabled_) {
int mesh_idx = base_source_regions_.mesh(sr);
int mesh_bin;
if (mesh_idx == C_NONE) {
mesh_bin = 0;
} else {
mesh_bin = model::meshes[mesh_idx]->get_bin(p.r());
}
SourceRegionKey sr_key {sr, mesh_bin};
auto it = source_region_map_.find(sr_key);
if (it != source_region_map_.end()) {
sr = it->second;
} else {
sr = -1;
}
}
voxel_indices[z * Ny * Nx + y * Nx + x] = sr;
voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
if (variance_reduction::weight_windows.size() == 1) {
WeightWindow ww =
variance_reduction::weight_windows[0]->get_weight_window(p);
float weight = ww.lower_weight;
weight_windows[z * Ny * Nx + y * Nx + x] = weight;
if (weight < min_weight)
min_weight = weight;
}
}
}
}
double source_normalization_factor =
compute_fixed_source_normalization_factor();
// Open file for writing
std::FILE* plot = std::fopen(filename.c_str(), "wb");
// Write vtk metadata
std::fprintf(plot, "# vtk DataFile Version 2.0\n");
std::fprintf(plot, "Dataset File\n");
std::fprintf(plot, "BINARY\n");
std::fprintf(plot, "DATASET STRUCTURED_POINTS\n");
std::fprintf(plot, "DIMENSIONS %d %d %d\n", Nx, Ny, Nz);
std::fprintf(plot, "ORIGIN %lf %lf %lf\n", ll.x, ll.y, ll.z);
std::fprintf(plot, "SPACING %lf %lf %lf\n", x_delta, y_delta, z_delta);
std::fprintf(plot, "POINT_DATA %d\n", Nx * Ny * Nz);
int64_t num_neg = 0;
int64_t num_samples = 0;
float min_flux = 0.0;
float max_flux = -1.0e20;
// Plot multigroup flux data
for (int g = 0; g < negroups_; g++) {
std::fprintf(plot, "SCALARS flux_group_%d float\n", g);
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int i = 0; i < Nx * Ny * Nz; i++) {
int64_t fsr = voxel_indices[i];
int64_t source_element = fsr * negroups_ + g;
float flux = 0;
if (fsr >= 0) {
flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
if (flux < 0.0)
flux = FlatSourceDomain::evaluate_flux_at_point(
voxel_positions[i], fsr, g);
}
if (flux < 0.0) {
num_neg++;
if (flux < min_flux) {
min_flux = flux;
}
}
if (flux > max_flux)
max_flux = flux;
num_samples++;
flux = convert_to_big_endian<float>(flux);
std::fwrite(&flux, sizeof(float), 1, plot);
}
}
// Slightly negative fluxes can be normal when sampling corners of linear
// source regions. However, very common and high magnitude negative fluxes
// may indicate numerical instability.
if (num_neg > 0) {
warning(fmt::format("{} plot samples ({:.4f}%) contained negative fluxes "
"(minumum found = {:.2e} maximum_found = {:.2e})",
num_neg, (100.0 * num_neg) / num_samples, min_flux, max_flux));
}
// Plot FSRs
std::fprintf(plot, "SCALARS FSRs float\n");
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int fsr : voxel_indices) {
float value = future_prn(10, fsr);
value = convert_to_big_endian<float>(value);
std::fwrite(&value, sizeof(float), 1, plot);
}
// Plot Materials
std::fprintf(plot, "SCALARS Materials int\n");
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int fsr : voxel_indices) {
int mat = -1;
if (fsr >= 0)
mat = source_regions_.material(fsr);
mat = convert_to_big_endian<int>(mat);
std::fwrite(&mat, sizeof(int), 1, plot);
}
// Plot fission source
if (settings::run_mode == RunMode::EIGENVALUE) {
std::fprintf(plot, "SCALARS total_fission_source float\n");
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int i = 0; i < Nx * Ny * Nz; i++) {
int64_t fsr = voxel_indices[i];
float total_fission = 0.0;
if (fsr >= 0) {
int mat = source_regions_.material(fsr);
if (mat != MATERIAL_VOID) {
for (int g = 0; g < negroups_; g++) {
int64_t source_element = fsr * negroups_ + g;
float flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
double sigma_f = sigma_f_[mat * negroups_ + g];
total_fission += sigma_f * flux;
}
}
}
total_fission = convert_to_big_endian<float>(total_fission);
std::fwrite(&total_fission, sizeof(float), 1, plot);
}
} else {
std::fprintf(plot, "SCALARS external_source float\n");
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int i = 0; i < Nx * Ny * Nz; i++) {
int64_t fsr = voxel_indices[i];
float total_external = 0.0f;
if (fsr >= 0) {
for (int g = 0; g < negroups_; g++) {
total_external += source_regions_.external_source(fsr, g);
}
}
total_external = convert_to_big_endian<float>(total_external);
std::fwrite(&total_external, sizeof(float), 1, plot);
}
}
// Plot weight window data
if (variance_reduction::weight_windows.size() == 1) {
std::fprintf(plot, "SCALARS weight_window_lower float\n");
std::fprintf(plot, "LOOKUP_TABLE default\n");
for (int i = 0; i < Nx * Ny * Nz; i++) {
float weight = weight_windows[i];
if (weight == 0.0)
weight = min_weight;
weight = convert_to_big_endian<float>(weight);
std::fwrite(&weight, sizeof(float), 1, plot);
}
}
std::fclose(plot);
}
}
void FlatSourceDomain::apply_external_source_to_source_region(
Discrete* discrete, double strength_factor, SourceRegionHandle& srh)
{
srh.external_source_present() = 1;
const auto& discrete_energies = discrete->x();
const auto& discrete_probs = discrete->prob();
for (int i = 0; i < discrete_energies.size(); i++) {
int g = data::mg.get_group_index(discrete_energies[i]);
srh.external_source(g) += discrete_probs[i] * strength_factor;
}
}
void FlatSourceDomain::apply_external_source_to_cell_instances(int32_t i_cell,
Discrete* discrete, double strength_factor, int target_material_id,
const vector<int32_t>& instances)
{
Cell& cell = *model::cells[i_cell];
if (cell.type_ != Fill::MATERIAL)
return;
for (int j : instances) {
int cell_material_idx = cell.material(j);
int cell_material_id;
if (cell_material_idx == MATERIAL_VOID) {
cell_material_id = MATERIAL_VOID;
} else {
cell_material_id = model::materials[cell_material_idx]->id();
}
if (target_material_id == C_NONE ||
cell_material_id == target_material_id) {
int64_t source_region = source_region_offsets_[i_cell] + j;
SourceRegionHandle srh =
source_regions_.get_source_region_handle(source_region);
apply_external_source_to_source_region(discrete, strength_factor, srh);
}
}
}
void FlatSourceDomain::apply_external_source_to_cell_and_children(
int32_t i_cell, Discrete* discrete, double strength_factor,
int32_t target_material_id)
{
Cell& cell = *model::cells[i_cell];
if (cell.type_ == Fill::MATERIAL) {
vector<int> instances(cell.n_instances_);
std::iota(instances.begin(), instances.end(), 0);
apply_external_source_to_cell_instances(
i_cell, discrete, strength_factor, target_material_id, instances);
} else if (target_material_id == C_NONE) {