• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 18538152141

15 Oct 2025 06:02PM UTC coverage: 81.97% (-3.2%) from 85.194%
18538152141

Pull #3417

github

web-flow
Merge 4604e1321 into e9077b137
Pull Request #3417: Addition of a collision tracking feature

16794 of 23357 branches covered (71.9%)

Branch coverage included in aggregate %.

480 of 522 new or added lines in 13 files covered. (91.95%)

457 existing lines in 53 files now uncovered.

54128 of 63165 relevant lines covered (85.69%)

42776927.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.17
/src/random_ray/flat_source_domain.cpp
1
#include "openmc/random_ray/flat_source_domain.h"
2

3
#include "openmc/cell.h"
4
#include "openmc/constants.h"
5
#include "openmc/eigenvalue.h"
6
#include "openmc/geometry.h"
7
#include "openmc/material.h"
8
#include "openmc/message_passing.h"
9
#include "openmc/mgxs_interface.h"
10
#include "openmc/output.h"
11
#include "openmc/plot.h"
12
#include "openmc/random_ray/random_ray.h"
13
#include "openmc/simulation.h"
14
#include "openmc/tallies/filter.h"
15
#include "openmc/tallies/tally.h"
16
#include "openmc/tallies/tally_scoring.h"
17
#include "openmc/timer.h"
18
#include "openmc/weight_windows.h"
19

20
#include <cstdio>
21

22
namespace openmc {
23

24
//==============================================================================
25
// FlatSourceDomain implementation
26
//==============================================================================
27

28
// Static Variable Declarations
29
RandomRayVolumeEstimator FlatSourceDomain::volume_estimator_ {
30
  RandomRayVolumeEstimator::HYBRID};
31
bool FlatSourceDomain::volume_normalized_flux_tallies_ {false};
32
bool FlatSourceDomain::adjoint_ {false};
33
double FlatSourceDomain::diagonal_stabilization_rho_ {1.0};
34
std::unordered_map<int, vector<std::pair<Source::DomainType, int>>>
35
  FlatSourceDomain::mesh_domain_map_;
36

37
FlatSourceDomain::FlatSourceDomain() : negroups_(data::mg.num_energy_groups_)
577✔
38
{
39
  // Count the number of source regions, compute the cell offset
40
  // indices, and store the material type The reason for the offsets is that
41
  // some cell types may not have material fills, and therefore do not
42
  // produce FSRs. Thus, we cannot index into the global arrays directly
43
  int base_source_regions = 0;
577✔
44
  for (const auto& c : model::cells) {
4,884✔
45
    if (c->type_ != Fill::MATERIAL) {
4,307✔
46
      source_region_offsets_.push_back(-1);
2,065✔
47
    } else {
48
      source_region_offsets_.push_back(base_source_regions);
2,242✔
49
      base_source_regions += c->n_instances();
2,242✔
50
    }
51
  }
52

53
  // Initialize source regions.
54
  bool is_linear = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
577✔
55
  source_regions_ = SourceRegionContainer(negroups_, is_linear);
577✔
56

57
  // Initialize tally volumes
58
  if (volume_normalized_flux_tallies_) {
577✔
59
    tally_volumes_.resize(model::tallies.size());
448✔
60
    for (int i = 0; i < model::tallies.size(); i++) {
1,648✔
61
      //  Get the shape of the 3D result tensor
62
      auto shape = model::tallies[i]->results().shape();
1,200✔
63

64
      // Create a new 2D tensor with the same size as the first
65
      // two dimensions of the 3D tensor
66
      tally_volumes_[i] =
1,200✔
67
        xt::xtensor<double, 2>::from_shape({shape[0], shape[1]});
2,400✔
68
    }
69
  }
70

71
  // Compute simulation domain volume based on ray source
72
  auto* is = dynamic_cast<IndependentSource*>(RandomRay::ray_source_.get());
577!
73
  SpatialDistribution* space_dist = is->space();
577✔
74
  SpatialBox* sb = dynamic_cast<SpatialBox*>(space_dist);
577!
75
  Position dims = sb->upper_right() - sb->lower_left();
577✔
76
  simulation_volume_ = dims.x * dims.y * dims.z;
577✔
77
}
577✔
78

79
void FlatSourceDomain::batch_reset()
11,672✔
80
{
81
// Reset scalar fluxes and iteration volume tallies to zero
82
#pragma omp parallel for
6,372✔
83
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
32,321,595✔
84
    source_regions_.volume(sr) = 0.0;
32,316,295✔
85
    source_regions_.volume_sq(sr) = 0.0;
32,316,295✔
86
  }
87

88
#pragma omp parallel for
6,372✔
89
  for (int64_t se = 0; se < n_source_elements(); se++) {
36,549,855✔
90
    source_regions_.scalar_flux_new(se) = 0.0;
36,544,555✔
91
  }
92
}
11,672✔
93

94
void FlatSourceDomain::accumulate_iteration_flux()
4,736✔
95
{
96
#pragma omp parallel for
2,586✔
97
  for (int64_t se = 0; se < n_source_elements(); se++) {
16,634,750✔
98
    source_regions_.scalar_flux_final(se) +=
16,632,600✔
99
      source_regions_.scalar_flux_new(se);
16,632,600✔
100
  }
101
}
4,736✔
102

103
void FlatSourceDomain::update_single_neutron_source(SourceRegionHandle& srh)
43,807,246✔
104
{
105
  // Reset all source regions to zero (important for void regions)
106
  for (int g = 0; g < negroups_; g++) {
92,883,590✔
107
    srh.source(g) = 0.0;
49,076,344✔
108
  }
109

110
  // Add scattering + fission source
111
  int material = srh.material();
43,807,246✔
112
  if (material != MATERIAL_VOID) {
43,807,246✔
113
    double inverse_k_eff = 1.0 / k_eff_;
43,365,598✔
114
    for (int g_out = 0; g_out < negroups_; g_out++) {
91,999,526✔
115
      double sigma_t = sigma_t_[material * negroups_ + g_out];
48,633,928✔
116
      double scatter_source = 0.0;
48,633,928✔
117
      double fission_source = 0.0;
48,633,928✔
118

119
      for (int g_in = 0; g_in < negroups_; g_in++) {
134,144,786✔
120
        double scalar_flux = srh.scalar_flux_old(g_in);
85,510,858✔
121
        double sigma_s =
122
          sigma_s_[material * negroups_ * negroups_ + g_out * negroups_ + g_in];
85,510,858✔
123
        double nu_sigma_f = nu_sigma_f_[material * negroups_ + g_in];
85,510,858✔
124
        double chi = chi_[material * negroups_ + g_out];
85,510,858✔
125

126
        scatter_source += sigma_s * scalar_flux;
85,510,858✔
127
        fission_source += nu_sigma_f * scalar_flux * chi;
85,510,858✔
128
      }
129
      srh.source(g_out) =
48,633,928✔
130
        (scatter_source + fission_source * inverse_k_eff) / sigma_t;
48,633,928✔
131
    }
132
  }
133

134
  // Add external source if in fixed source mode
135
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
43,807,246✔
136
    for (int g = 0; g < negroups_; g++) {
89,574,878✔
137
      srh.source(g) += srh.external_source(g);
46,223,461✔
138
    }
139
  }
140
}
43,807,246✔
141

142
// Compute new estimate of scattering + fission sources in each source region
143
// based on the flux estimate from the previous iteration.
144
void FlatSourceDomain::update_all_neutron_sources()
11,672✔
145
{
146
  simulation::time_update_src.start();
11,672✔
147

148
#pragma omp parallel for
6,372✔
149
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
32,321,595✔
150
    SourceRegionHandle srh = source_regions_.get_source_region_handle(sr);
32,316,295✔
151
    update_single_neutron_source(srh);
32,316,295✔
152
  }
153

154
  simulation::time_update_src.stop();
11,672✔
155
}
11,672✔
156

157
// Normalizes flux and updates simulation-averaged volume estimate
158
void FlatSourceDomain::normalize_scalar_flux_and_volumes(
5,237✔
159
  double total_active_distance_per_iteration)
160
{
161
  double normalization_factor = 1.0 / total_active_distance_per_iteration;
5,237✔
162
  double volume_normalization_factor =
5,237✔
163
    1.0 / (total_active_distance_per_iteration * simulation::current_batch);
5,237✔
164

165
// Normalize scalar flux to total distance travelled by all rays this
166
// iteration
167
#pragma omp parallel for
2,862✔
168
  for (int64_t se = 0; se < n_source_elements(); se++) {
22,277,785✔
169
    source_regions_.scalar_flux_new(se) *= normalization_factor;
22,275,410✔
170
  }
171

172
// Accumulate cell-wise ray length tallies collected this iteration, then
173
// update the simulation-averaged cell-wise volume estimates
174
#pragma omp parallel for
2,862✔
175
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
19,883,245✔
176
    source_regions_.volume_t(sr) += source_regions_.volume(sr);
19,880,870✔
177
    source_regions_.volume_sq_t(sr) += source_regions_.volume_sq(sr);
19,880,870✔
178
    source_regions_.volume_naive(sr) =
39,761,740✔
179
      source_regions_.volume(sr) * normalization_factor;
19,880,870✔
180
    source_regions_.volume_sq(sr) =
39,761,740✔
181
      source_regions_.volume_sq_t(sr) / source_regions_.volume_t(sr);
19,880,870✔
182
    source_regions_.volume(sr) =
19,880,870✔
183
      source_regions_.volume_t(sr) * volume_normalization_factor;
19,880,870✔
184
  }
185
}
5,237✔
186

187
void FlatSourceDomain::set_flux_to_flux_plus_source(
43,412,209✔
188
  int64_t sr, double volume, int g)
189
{
190
  int material = source_regions_.material(sr);
43,412,209✔
191
  if (material == MATERIAL_VOID) {
43,412,209✔
192
    source_regions_.scalar_flux_new(sr, g) /= volume;
882,416✔
193
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
882,416!
194
      source_regions_.scalar_flux_new(sr, g) +=
882,416✔
195
        0.5f * source_regions_.external_source(sr, g) *
882,416✔
196
        source_regions_.volume_sq(sr);
882,416✔
197
    }
198
  } else {
199
    double sigma_t = sigma_t_[source_regions_.material(sr) * negroups_ + g];
42,529,793✔
200
    source_regions_.scalar_flux_new(sr, g) /= (sigma_t * volume);
42,529,793✔
201
    source_regions_.scalar_flux_new(sr, g) += source_regions_.source(sr, g);
42,529,793✔
202
  }
203
}
43,412,209✔
204

205
void FlatSourceDomain::set_flux_to_old_flux(int64_t sr, int g)
1,542✔
206
{
207
  source_regions_.scalar_flux_new(sr, g) =
3,084✔
208
    source_regions_.scalar_flux_old(sr, g);
1,542✔
209
}
1,542✔
210

211
void FlatSourceDomain::set_flux_to_source(int64_t sr, int g)
8,927,547✔
212
{
213
  source_regions_.scalar_flux_new(sr, g) = source_regions_.source(sr, g);
8,927,547✔
214
}
8,927,547✔
215

216
// Combine transport flux contributions and flat source contributions from the
217
// previous iteration to generate this iteration's estimate of scalar flux.
218
int64_t FlatSourceDomain::add_source_to_scalar_flux()
11,672✔
219
{
220
  int64_t n_hits = 0;
11,672✔
221
  double inverse_batch = 1.0 / simulation::current_batch;
11,672✔
222

223
#pragma omp parallel for reduction(+ : n_hits)
6,372✔
224
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
33,339,335✔
225

226
    double volume_simulation_avg = source_regions_.volume(sr);
33,334,035✔
227
    double volume_iteration = source_regions_.volume_naive(sr);
33,334,035✔
228

229
    // Increment the number of hits if cell was hit this iteration
230
    if (volume_iteration) {
33,334,035✔
231
      n_hits++;
29,274,880✔
232
    }
233

234
    // Set the SR to small status if its expected number of hits
235
    // per iteration is less than 1.5
236
    if (source_regions_.n_hits(sr) * inverse_batch < MIN_HITS_PER_BATCH) {
33,334,035✔
237
      source_regions_.is_small(sr) = 1;
6,812,705✔
238
    } else {
239
      source_regions_.is_small(sr) = 0;
26,521,330✔
240
    }
241

242
    // The volume treatment depends on the volume estimator type
243
    // and whether or not an external source is present in the cell.
244
    double volume;
245
    switch (volume_estimator_) {
33,334,035!
246
    case RandomRayVolumeEstimator::NAIVE:
777,600✔
247
      volume = volume_iteration;
777,600✔
248
      break;
777,600✔
249
    case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
432,000✔
250
      volume = volume_simulation_avg;
432,000✔
251
      break;
432,000✔
252
    case RandomRayVolumeEstimator::HYBRID:
32,124,435✔
253
      if (source_regions_.external_source_present(sr) ||
60,060,870✔
254
          source_regions_.is_small(sr)) {
27,936,435✔
255
        volume = volume_iteration;
11,000,325✔
256
      } else {
257
        volume = volume_simulation_avg;
21,124,110✔
258
      }
259
      break;
32,124,435✔
260
    default:
261
      fatal_error("Invalid volume estimator type");
262
    }
263

264
    for (int g = 0; g < negroups_; g++) {
71,037,810✔
265
      // There are three scenarios we need to consider:
266
      if (volume_iteration > 0.0) {
37,703,775✔
267
        // 1. If the FSR was hit this iteration, then the new flux is equal to
268
        // the flat source from the previous iteration plus the contributions
269
        // from rays passing through the source region (computed during the
270
        // transport sweep)
271
        set_flux_to_flux_plus_source(sr, volume, g);
33,644,470✔
272
      } else if (volume_simulation_avg > 0.0) {
4,059,305✔
273
        // 2. If the FSR was not hit this iteration, but has been hit some
274
        // previous iteration, then we need to make a choice about what
275
        // to do. Naively we will usually want to set the flux to be equal
276
        // to the reduced source. However, in fixed source problems where
277
        // there is a strong external source present in the cell, and where
278
        // the cell has a very low cross section, this approximation will
279
        // cause a huge upward bias in the flux estimate of the cell (in these
280
        // conditions, the flux estimate can be orders of magnitude too large).
281
        // Thus, to avoid this bias, if any external source is present
282
        // in the cell we will use the previous iteration's flux estimate. This
283
        // injects a small degree of correlation into the simulation, but this
284
        // is going to be trivial when the miss rate is a few percent or less.
285
        if (source_regions_.external_source_present(sr)) {
4,059,295✔
286
          set_flux_to_old_flux(sr, g);
1,320✔
287
        } else {
288
          set_flux_to_source(sr, g);
4,057,975✔
289
        }
290
      }
291
      // Halt if NaN implosion is detected
292
      if (!std::isfinite(source_regions_.scalar_flux_new(sr, g))) {
37,703,775!
293
        fatal_error("A source region scalar flux is not finite. "
294
                    "This indicates a numerical instability in the "
295
                    "simulation. Consider increasing ray density or adjusting "
296
                    "the source region mesh.");
297
      }
298
    }
299
  }
300

301
  // Return the number of source regions that were hit this iteration
302
  return n_hits;
11,672✔
303
}
304

305
// Generates new estimate of k_eff based on the differences between this
306
// iteration's estimate of the scalar flux and the last iteration's estimate.
307
void FlatSourceDomain::compute_k_eff()
2,090✔
308
{
309
  double fission_rate_old = 0;
2,090✔
310
  double fission_rate_new = 0;
2,090✔
311

312
  // Vector for gathering fission source terms for Shannon entropy calculation
313
  vector<float> p(n_source_regions(), 0.0f);
2,090✔
314

315
#pragma omp parallel for reduction(+ : fission_rate_old, fission_rate_new)
1,140✔
316
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
308,740✔
317

318
    // If simulation averaged volume is zero, don't include this cell
319
    double volume = source_regions_.volume(sr);
307,790✔
320
    if (volume == 0.0) {
307,790!
321
      continue;
322
    }
323

324
    int material = source_regions_.material(sr);
307,790✔
325
    if (material == MATERIAL_VOID) {
307,790!
326
      continue;
327
    }
328

329
    double sr_fission_source_old = 0;
307,790✔
330
    double sr_fission_source_new = 0;
307,790✔
331

332
    for (int g = 0; g < negroups_; g++) {
2,375,320✔
333
      double nu_sigma_f = nu_sigma_f_[material * negroups_ + g];
2,067,530✔
334
      sr_fission_source_old +=
2,067,530✔
335
        nu_sigma_f * source_regions_.scalar_flux_old(sr, g);
2,067,530✔
336
      sr_fission_source_new +=
2,067,530✔
337
        nu_sigma_f * source_regions_.scalar_flux_new(sr, g);
2,067,530✔
338
    }
339

340
    // Compute total fission rates in FSR
341
    sr_fission_source_old *= volume;
307,790✔
342
    sr_fission_source_new *= volume;
307,790✔
343

344
    // Accumulate totals
345
    fission_rate_old += sr_fission_source_old;
307,790✔
346
    fission_rate_new += sr_fission_source_new;
307,790✔
347

348
    // Store total fission rate in the FSR for Shannon calculation
349
    p[sr] = sr_fission_source_new;
307,790✔
350
  }
351

352
  double k_eff_new = k_eff_ * (fission_rate_new / fission_rate_old);
2,090✔
353

354
  double H = 0.0;
2,090✔
355
  // defining an inverse sum for better performance
356
  double inverse_sum = 1 / fission_rate_new;
2,090✔
357

358
#pragma omp parallel for reduction(+ : H)
1,140✔
359
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
308,740✔
360
    // Only if FSR has non-negative and non-zero fission source
361
    if (p[sr] > 0.0f) {
307,790✔
362
      // Normalize to total weight of bank sites. p_i for better performance
363
      float p_i = p[sr] * inverse_sum;
130,995✔
364
      // Sum values to obtain Shannon entropy.
365
      H -= p_i * std::log2(p_i);
130,995✔
366
    }
367
  }
368

369
  // Adds entropy value to shared entropy vector in openmc namespace.
370
  simulation::entropy.push_back(H);
2,090✔
371

372
  k_eff_ = k_eff_new;
2,090✔
373
}
2,090✔
374

375
// This function is responsible for generating a mapping between random
376
// ray flat source regions (cell instances) and tally bins. The mapping
377
// takes the form of a "TallyTask" object, which accounts for one single
378
// score being applied to a single tally. Thus, a single source region
379
// may have anywhere from zero to many tally tasks associated with it ---
380
// meaning that the global "tally_task" data structure is in 2D. The outer
381
// dimension corresponds to the source element (i.e., each entry corresponds
382
// to a specific energy group within a specific source region), and the
383
// inner dimension corresponds to the tallying task itself. Mechanically,
384
// the mapping between FSRs and spatial filters is done by considering
385
// the location of a single known ray midpoint that passed through the
386
// FSR. I.e., during transport, the first ray to pass through a given FSR
387
// will write down its midpoint for use with this function. This is a cheap
388
// and easy way of mapping FSRs to spatial tally filters, but comes with
389
// the downside of adding the restriction that spatial tally filters must
390
// share boundaries with the physical geometry of the simulation (so as
391
// not to subdivide any FSR). It is acceptable for a spatial tally region
392
// to contain multiple FSRs, but not the other way around.
393

394
// TODO: In future work, it would be preferable to offer a more general
395
// (but perhaps slightly more expensive) option for handling arbitrary
396
// spatial tallies that would be allowed to subdivide FSRs.
397

398
// Besides generating the mapping structure, this function also keeps track
399
// of whether or not all flat source regions have been hit yet. This is
400
// required, as there is no guarantee that all flat source regions will
401
// be hit every iteration, such that in the first few iterations some FSRs
402
// may not have a known position within them yet to facilitate mapping to
403
// spatial tally filters. However, after several iterations, if all FSRs
404
// have been hit and have had a tally map generated, then this status will
405
// be passed back to the caller to alert them that this function doesn't
406
// need to be called for the remainder of the simulation.
407

408
// It takes as an argument the starting index in the source region array,
409
// and it will operate from that index until the end of the array. This
410
// is useful as it can be called for both explicit user source regions or
411
// when a source region mesh is overlaid.
412

413
void FlatSourceDomain::convert_source_regions_to_tallies(int64_t start_sr_id)
727✔
414
{
415
  openmc::simulation::time_tallies.start();
727✔
416

417
  // Tracks if we've generated a mapping yet for all source regions.
418
  bool all_source_regions_mapped = true;
727✔
419

420
// Attempt to generate mapping for all source regions
421
#pragma omp parallel for
397✔
422
  for (int64_t sr = start_sr_id; sr < n_source_regions(); sr++) {
1,018,070✔
423

424
    // If this source region has not been hit by a ray yet, then
425
    // we aren't going to be able to map it, so skip it.
426
    if (!source_regions_.position_recorded(sr)) {
1,017,740!
427
      all_source_regions_mapped = false;
428
      continue;
429
    }
430

431
    // A particle located at the recorded midpoint of a ray
432
    // crossing through this source region is used to estabilish
433
    // the spatial location of the source region
434
    Particle p;
1,017,740✔
435
    p.r() = source_regions_.position(sr);
1,017,740✔
436
    p.r_last() = source_regions_.position(sr);
1,017,740✔
437
    p.u() = {1.0, 0.0, 0.0};
1,017,740✔
438
    bool found = exhaustive_find_cell(p);
1,017,740✔
439

440
    // Loop over energy groups (so as to support energy filters)
441
    for (int g = 0; g < negroups_; g++) {
2,176,960✔
442

443
      // Set particle to the current energy
444
      p.g() = g;
1,159,220✔
445
      p.g_last() = g;
1,159,220✔
446
      p.E() = data::mg.energy_bin_avg_[p.g()];
1,159,220✔
447
      p.E_last() = p.E();
1,159,220✔
448

449
      int64_t source_element = sr * negroups_ + g;
1,159,220✔
450

451
      // If this task has already been populated, we don't need to do
452
      // it again.
453
      if (source_regions_.tally_task(sr, g).size() > 0) {
1,159,220!
454
        continue;
455
      }
456

457
      // Loop over all active tallies. This logic is essentially identical
458
      // to what happens when scanning for applicable tallies during
459
      // MC transport.
460
      for (int i_tally = 0; i_tally < model::tallies.size(); i_tally++) {
4,442,360✔
461
        Tally& tally {*model::tallies[i_tally]};
3,283,140✔
462

463
        // Initialize an iterator over valid filter bin combinations.
464
        // If there are no valid combinations, use a continue statement
465
        // to ensure we skip the assume_separate break below.
466
        auto filter_iter = FilterBinIter(tally, p);
3,283,140✔
467
        auto end = FilterBinIter(tally, true, &p.filter_matches());
3,283,140✔
468
        if (filter_iter == end)
3,283,140✔
469
          continue;
1,984,160✔
470

471
        // Loop over filter bins.
472
        for (; filter_iter != end; ++filter_iter) {
2,597,960✔
473
          auto filter_index = filter_iter.index_;
1,298,980✔
474
          auto filter_weight = filter_iter.weight_;
1,298,980✔
475

476
          // Loop over scores
477
          for (int score = 0; score < tally.scores_.size(); score++) {
2,918,000✔
478
            auto score_bin = tally.scores_[score];
1,619,020✔
479
            // If a valid tally, filter, and score combination has been found,
480
            // then add it to the list of tally tasks for this source element.
481
            TallyTask task(i_tally, filter_index, score, score_bin);
1,619,020✔
482
            source_regions_.tally_task(sr, g).push_back(task);
1,619,020✔
483

484
            // Also add this task to the list of volume tasks for this source
485
            // region.
486
            source_regions_.volume_task(sr).insert(task);
1,619,020✔
487
          }
488
        }
489
      }
490
      // Reset all the filter matches for the next tally event.
491
      for (auto& match : p.filter_matches())
4,896,140✔
492
        match.bins_present_ = false;
3,736,920✔
493
    }
494
  }
1,017,740✔
495
  openmc::simulation::time_tallies.stop();
727✔
496

497
  mapped_all_tallies_ = all_source_regions_mapped;
727✔
498
}
727✔
499

500
// Set the volume accumulators to zero for all tallies
501
void FlatSourceDomain::reset_tally_volumes()
4,736✔
502
{
503
  if (volume_normalized_flux_tallies_) {
4,736✔
504
#pragma omp parallel for
2,100✔
505
    for (int i = 0; i < tally_volumes_.size(); i++) {
6,600✔
506
      auto& tensor = tally_volumes_[i];
4,850✔
507
      tensor.fill(0.0); // Set all elements of the tensor to 0.0
4,850✔
508
    }
509
  }
510
}
4,736✔
511

512
// In fixed source mode, due to the way that volumetric fixed sources are
513
// converted and applied as volumetric sources in one or more source regions,
514
// we need to perform an additional normalization step to ensure that the
515
// reported scalar fluxes are in units per source neutron. This allows for
516
// direct comparison of reported tallies to Monte Carlo flux results.
517
// This factor needs to be computed at each iteration, as it is based on the
518
// volume estimate of each FSR, which improves over the course of the
519
// simulation
520
double FlatSourceDomain::compute_fixed_source_normalization_factor() const
5,313✔
521
{
522
  // If we are not in fixed source mode, then there are no external sources
523
  // so no normalization is needed.
524
  if (settings::run_mode != RunMode::FIXED_SOURCE || adjoint_) {
5,313✔
525
    return 1.0;
1,499✔
526
  }
527

528
  // Step 1 is to sum over all source regions and energy groups to get the
529
  // total external source strength in the simulation.
530
  double simulation_external_source_strength = 0.0;
3,814✔
531
#pragma omp parallel for reduction(+ : simulation_external_source_strength)
2,089✔
532
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
14,626,285✔
533
    int material = source_regions_.material(sr);
14,624,560✔
534
    double volume = source_regions_.volume(sr) * simulation_volume_;
14,624,560✔
535
    for (int g = 0; g < negroups_; g++) {
29,792,000✔
536
      // For non-void regions, we store the external source pre-divided by
537
      // sigma_t. We need to multiply non-void regions back up by sigma_t
538
      // to get the total source strength in the expected units.
539
      double sigma_t = 1.0;
15,167,440✔
540
      if (material != MATERIAL_VOID) {
15,167,440✔
541
        sigma_t = sigma_t_[material * negroups_ + g];
14,957,200✔
542
      }
543
      simulation_external_source_strength +=
15,167,440✔
544
        source_regions_.external_source(sr, g) * sigma_t * volume;
15,167,440✔
545
    }
546
  }
547

548
  // Step 2 is to determine the total user-specified external source strength
549
  double user_external_source_strength = 0.0;
3,814✔
550
  for (auto& ext_source : model::external_sources) {
7,628✔
551
    user_external_source_strength += ext_source->strength();
3,814✔
552
  }
553

554
  // The correction factor is the ratio of the user-specified external source
555
  // strength to the simulation external source strength.
556
  double source_normalization_factor =
3,814✔
557
    user_external_source_strength / simulation_external_source_strength;
558

559
  return source_normalization_factor;
3,814✔
560
}
561

562
// Tallying in random ray is not done directly during transport, rather,
563
// it is done only once after each power iteration. This is made possible
564
// by way of a mapping data structure that relates spatial source regions
565
// (FSRs) to tally/filter/score combinations. The mechanism by which the
566
// mapping is done (and the limitations incurred) is documented in the
567
// "convert_source_regions_to_tallies()" function comments above. The present
568
// tally function simply traverses the mapping data structure and executes
569
// the scoring operations to OpenMC's native tally result arrays.
570

571
void FlatSourceDomain::random_ray_tally()
4,736✔
572
{
573
  openmc::simulation::time_tallies.start();
4,736✔
574

575
  // Reset our tally volumes to zero
576
  reset_tally_volumes();
4,736✔
577

578
  double source_normalization_factor =
579
    compute_fixed_source_normalization_factor();
4,736✔
580

581
// We loop over all source regions and energy groups. For each
582
// element, we check if there are any scores needed and apply
583
// them.
584
#pragma omp parallel for
2,586✔
585
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
15,253,550✔
586
    // The fsr.volume_ is the unitless fractional simulation averaged volume
587
    // (i.e., it is the FSR's fraction of the overall simulation volume). The
588
    // simulation_volume_ is the total 3D physical volume in cm^3 of the
589
    // entire global simulation domain (as defined by the ray source box).
590
    // Thus, the FSR's true 3D spatial volume in cm^3 is found by multiplying
591
    // its fraction of the total volume by the total volume. Not important in
592
    // eigenvalue solves, but useful in fixed source solves for returning the
593
    // flux shape with a magnitude that makes sense relative to the fixed
594
    // source strength.
595
    double volume = source_regions_.volume(sr) * simulation_volume_;
15,251,400✔
596

597
    double material = source_regions_.material(sr);
15,251,400✔
598
    for (int g = 0; g < negroups_; g++) {
31,884,000✔
599
      double flux =
600
        source_regions_.scalar_flux_new(sr, g) * source_normalization_factor;
16,632,600✔
601

602
      // Determine numerical score value
603
      for (auto& task : source_regions_.tally_task(sr, g)) {
39,253,200✔
604
        double score = 0.0;
22,620,600✔
605
        switch (task.score_type) {
22,620,600!
606

607
        case SCORE_FLUX:
19,448,200✔
608
          score = flux * volume;
19,448,200✔
609
          break;
19,448,200✔
610

611
        case SCORE_TOTAL:
612
          if (material != MATERIAL_VOID) {
×
613
            score = flux * volume * sigma_t_[material * negroups_ + g];
614
          }
615
          break;
616

617
        case SCORE_FISSION:
1,586,200✔
618
          if (material != MATERIAL_VOID) {
1,586,200!
619
            score = flux * volume * sigma_f_[material * negroups_ + g];
1,586,200✔
620
          }
621
          break;
1,586,200✔
622

623
        case SCORE_NU_FISSION:
1,586,200✔
624
          if (material != MATERIAL_VOID) {
1,586,200!
625
            score = flux * volume * nu_sigma_f_[material * negroups_ + g];
1,586,200✔
626
          }
627
          break;
1,586,200✔
628

629
        case SCORE_EVENTS:
630
          score = 1.0;
631
          break;
632

633
        default:
634
          fatal_error("Invalid score specified in tallies.xml. Only flux, "
635
                      "total, fission, nu-fission, and events are supported in "
636
                      "random ray mode.");
637
          break;
638
        }
639
        // Apply score to the appropriate tally bin
640
        Tally& tally {*model::tallies[task.tally_idx]};
22,620,600✔
641
#pragma omp atomic
642
        tally.results_(task.filter_idx, task.score_idx, TallyResult::VALUE) +=
22,620,600✔
643
          score;
644
      }
645
    }
646

647
    // For flux tallies, the total volume of the spatial region is needed
648
    // for normalizing the flux. We store this volume in a separate tensor.
649
    // We only contribute to each volume tally bin once per FSR.
650
    if (volume_normalized_flux_tallies_) {
15,251,400✔
651
      for (const auto& task : source_regions_.volume_task(sr)) {
35,857,600✔
652
        if (task.score_type == SCORE_FLUX) {
20,750,400✔
653
#pragma omp atomic
654
          tally_volumes_[task.tally_idx](task.filter_idx, task.score_idx) +=
18,796,000✔
655
            volume;
656
        }
657
      }
658
    }
659
  } // end FSR loop
660

661
  // Normalize any flux scores by the total volume of the FSRs scoring to that
662
  // bin. To do this, we loop over all tallies, and then all filter bins,
663
  // and then scores. For each score, we check the tally data structure to
664
  // see what index that score corresponds to. If that score is a flux score,
665
  // then we divide it by volume.
666
  if (volume_normalized_flux_tallies_) {
4,736✔
667
    for (int i = 0; i < model::tallies.size(); i++) {
14,520✔
668
      Tally& tally {*model::tallies[i]};
10,670✔
669
#pragma omp parallel for
5,820✔
670
      for (int bin = 0; bin < tally.n_filter_bins(); bin++) {
704,025✔
671
        for (int score_idx = 0; score_idx < tally.n_scores(); score_idx++) {
1,416,550✔
672
          auto score_type = tally.scores_[score_idx];
717,375✔
673
          if (score_type == SCORE_FLUX) {
717,375✔
674
            double vol = tally_volumes_[i](bin, score_idx);
699,175✔
675
            if (vol > 0.0) {
699,175!
676
              tally.results_(bin, score_idx, TallyResult::VALUE) /= vol;
699,175✔
677
            }
678
          }
679
        }
680
      }
681
    }
682
  }
683

684
  openmc::simulation::time_tallies.stop();
4,736✔
685
}
4,736✔
686

687
double FlatSourceDomain::evaluate_flux_at_point(
×
688
  Position r, int64_t sr, int g) const
689
{
690
  return source_regions_.scalar_flux_final(sr, g) /
×
691
         (settings::n_batches - settings::n_inactive);
×
692
}
693

694
// Outputs all basic material, FSR ID, multigroup flux, and
695
// fission source data to .vtk file that can be directly
696
// loaded and displayed by Paraview. Note that .vtk binary
697
// files require big endian byte ordering, so endianness
698
// is checked and flipped if necessary.
699
void FlatSourceDomain::output_to_vtk() const
×
700
{
701
  // Rename .h5 plot filename(s) to .vtk filenames
702
  for (int p = 0; p < model::plots.size(); p++) {
×
703
    PlottableInterface* plot = model::plots[p].get();
×
704
    plot->path_plot() =
×
705
      plot->path_plot().substr(0, plot->path_plot().find_last_of('.')) + ".vtk";
×
706
  }
707

708
  // Print header information
709
  print_plot();
×
710

711
  // Outer loop over plots
712
  for (int plt = 0; plt < model::plots.size(); plt++) {
×
713

714
    // Get handle to OpenMC plot object and extract params
715
    Plot* openmc_plot = dynamic_cast<Plot*>(model::plots[plt].get());
×
716

717
    // Random ray plots only support voxel plots
718
    if (!openmc_plot) {
×
719
      warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
×
720
                          "is allowed in random ray mode.",
721
        plt));
722
      continue;
×
723
    } else if (openmc_plot->type_ != Plot::PlotType::voxel) {
×
724
      warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
×
725
                          "is allowed in random ray mode.",
726
        plt));
727
      continue;
×
728
    }
729

730
    int Nx = openmc_plot->pixels_[0];
×
731
    int Ny = openmc_plot->pixels_[1];
×
732
    int Nz = openmc_plot->pixels_[2];
×
733
    Position origin = openmc_plot->origin_;
×
734
    Position width = openmc_plot->width_;
×
735
    Position ll = origin - width / 2.0;
×
736
    double x_delta = width.x / Nx;
×
737
    double y_delta = width.y / Ny;
×
738
    double z_delta = width.z / Nz;
×
739
    std::string filename = openmc_plot->path_plot();
×
740

741
    // Perform sanity checks on file size
742
    uint64_t bytes = Nx * Ny * Nz * (negroups_ + 1 + 1 + 1) * sizeof(float);
×
743
    write_message(5, "Processing plot {}: {}... (Estimated size is {} MB)",
×
744
      openmc_plot->id(), filename, bytes / 1.0e6);
×
745
    if (bytes / 1.0e9 > 1.0) {
×
746
      warning("Voxel plot specification is very large (>1 GB). Plotting may be "
×
747
              "slow.");
748
    } else if (bytes / 1.0e9 > 100.0) {
×
749
      fatal_error("Voxel plot specification is too large (>100 GB). Exiting.");
×
750
    }
751

752
    // Relate voxel spatial locations to random ray source regions
753
    vector<int> voxel_indices(Nx * Ny * Nz);
×
754
    vector<Position> voxel_positions(Nx * Ny * Nz);
×
755
    vector<double> weight_windows(Nx * Ny * Nz);
×
756
    float min_weight = 1e20;
×
757
#pragma omp parallel for collapse(3) reduction(min : min_weight)
758
    for (int z = 0; z < Nz; z++) {
×
759
      for (int y = 0; y < Ny; y++) {
×
760
        for (int x = 0; x < Nx; x++) {
×
761
          Position sample;
762
          sample.z = ll.z + z_delta / 2.0 + z * z_delta;
763
          sample.y = ll.y + y_delta / 2.0 + y * y_delta;
764
          sample.x = ll.x + x_delta / 2.0 + x * x_delta;
765
          Particle p;
×
766
          p.r() = sample;
767
          p.r_last() = sample;
768
          p.E() = 1.0;
769
          p.E_last() = 1.0;
770
          p.u() = {1.0, 0.0, 0.0};
771

772
          bool found = exhaustive_find_cell(p);
×
773
          if (!found) {
×
774
            voxel_indices[z * Ny * Nx + y * Nx + x] = -1;
775
            voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
776
            weight_windows[z * Ny * Nx + y * Nx + x] = 0.0;
777
            continue;
778
          }
779

780
          SourceRegionKey sr_key = lookup_source_region_key(p);
×
781
          int64_t sr = -1;
782
          auto it = source_region_map_.find(sr_key);
×
783
          if (it != source_region_map_.end()) {
×
784
            sr = it->second;
785
          }
786

787
          voxel_indices[z * Ny * Nx + y * Nx + x] = sr;
788
          voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
789

790
          if (variance_reduction::weight_windows.size() == 1) {
×
791
            WeightWindow ww =
792
              variance_reduction::weight_windows[0]->get_weight_window(p);
×
793
            float weight = ww.lower_weight;
794
            weight_windows[z * Ny * Nx + y * Nx + x] = weight;
795
            if (weight < min_weight)
×
796
              min_weight = weight;
797
          }
798
        }
×
799
      }
800
    }
801

802
    double source_normalization_factor =
803
      compute_fixed_source_normalization_factor();
×
804

805
    // Open file for writing
806
    std::FILE* plot = std::fopen(filename.c_str(), "wb");
×
807

808
    // Write vtk metadata
809
    std::fprintf(plot, "# vtk DataFile Version 2.0\n");
×
810
    std::fprintf(plot, "Dataset File\n");
×
811
    std::fprintf(plot, "BINARY\n");
×
812
    std::fprintf(plot, "DATASET STRUCTURED_POINTS\n");
×
813
    std::fprintf(plot, "DIMENSIONS %d %d %d\n", Nx, Ny, Nz);
×
814
    std::fprintf(plot, "ORIGIN %lf %lf %lf\n", ll.x, ll.y, ll.z);
×
815
    std::fprintf(plot, "SPACING %lf %lf %lf\n", x_delta, y_delta, z_delta);
×
816
    std::fprintf(plot, "POINT_DATA %d\n", Nx * Ny * Nz);
×
817

818
    int64_t num_neg = 0;
×
819
    int64_t num_samples = 0;
×
820
    float min_flux = 0.0;
×
821
    float max_flux = -1.0e20;
×
822
    // Plot multigroup flux data
823
    for (int g = 0; g < negroups_; g++) {
×
824
      std::fprintf(plot, "SCALARS flux_group_%d float\n", g);
×
825
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
826
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
827
        int64_t fsr = voxel_indices[i];
×
828
        int64_t source_element = fsr * negroups_ + g;
×
829
        float flux = 0;
×
830
        if (fsr >= 0) {
×
831
          flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
×
832
          if (flux < 0.0)
×
833
            flux = FlatSourceDomain::evaluate_flux_at_point(
×
834
              voxel_positions[i], fsr, g);
×
835
        }
836
        if (flux < 0.0) {
×
837
          num_neg++;
×
838
          if (flux < min_flux) {
×
839
            min_flux = flux;
×
840
          }
841
        }
842
        if (flux > max_flux)
×
843
          max_flux = flux;
×
844
        num_samples++;
×
845
        flux = convert_to_big_endian<float>(flux);
×
846
        std::fwrite(&flux, sizeof(float), 1, plot);
×
847
      }
848
    }
849

850
    // Slightly negative fluxes can be normal when sampling corners of linear
851
    // source regions. However, very common and high magnitude negative fluxes
852
    // may indicate numerical instability.
853
    if (num_neg > 0) {
×
854
      warning(fmt::format("{} plot samples ({:.4f}%) contained negative fluxes "
×
855
                          "(minumum found = {:.2e} maximum_found = {:.2e})",
856
        num_neg, (100.0 * num_neg) / num_samples, min_flux, max_flux));
×
857
    }
858

859
    // Plot FSRs
860
    std::fprintf(plot, "SCALARS FSRs float\n");
×
861
    std::fprintf(plot, "LOOKUP_TABLE default\n");
×
862
    for (int fsr : voxel_indices) {
×
863
      float value = future_prn(10, fsr);
×
864
      value = convert_to_big_endian<float>(value);
×
865
      std::fwrite(&value, sizeof(float), 1, plot);
×
866
    }
867

868
    // Plot Materials
869
    std::fprintf(plot, "SCALARS Materials int\n");
×
870
    std::fprintf(plot, "LOOKUP_TABLE default\n");
×
871
    for (int fsr : voxel_indices) {
×
872
      int mat = -1;
×
873
      if (fsr >= 0)
×
874
        mat = source_regions_.material(fsr);
×
875
      mat = convert_to_big_endian<int>(mat);
×
876
      std::fwrite(&mat, sizeof(int), 1, plot);
×
877
    }
878

879
    // Plot fission source
880
    if (settings::run_mode == RunMode::EIGENVALUE) {
×
881
      std::fprintf(plot, "SCALARS total_fission_source float\n");
×
882
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
883
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
884
        int64_t fsr = voxel_indices[i];
×
885
        float total_fission = 0.0;
×
886
        if (fsr >= 0) {
×
887
          int mat = source_regions_.material(fsr);
×
888
          if (mat != MATERIAL_VOID) {
×
889
            for (int g = 0; g < negroups_; g++) {
×
890
              int64_t source_element = fsr * negroups_ + g;
×
891
              float flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
×
892
              double sigma_f = sigma_f_[mat * negroups_ + g];
×
893
              total_fission += sigma_f * flux;
×
894
            }
895
          }
896
        }
897
        total_fission = convert_to_big_endian<float>(total_fission);
×
898
        std::fwrite(&total_fission, sizeof(float), 1, plot);
×
899
      }
900
    } else {
901
      std::fprintf(plot, "SCALARS external_source float\n");
×
902
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
903
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
904
        int64_t fsr = voxel_indices[i];
×
905
        int mat = source_regions_.material(fsr);
×
906
        float total_external = 0.0f;
×
907
        if (fsr >= 0) {
×
908
          for (int g = 0; g < negroups_; g++) {
×
909
            // External sources are already divided by sigma_t, so we need to
910
            // multiply it back to get the true external source.
911
            double sigma_t = 1.0;
×
912
            if (mat != MATERIAL_VOID) {
×
913
              sigma_t = sigma_t_[mat * negroups_ + g];
×
914
            }
915
            total_external += source_regions_.external_source(fsr, g) * sigma_t;
×
916
          }
917
        }
918
        total_external = convert_to_big_endian<float>(total_external);
×
919
        std::fwrite(&total_external, sizeof(float), 1, plot);
×
920
      }
921
    }
922

923
    // Plot weight window data
924
    if (variance_reduction::weight_windows.size() == 1) {
×
925
      std::fprintf(plot, "SCALARS weight_window_lower float\n");
×
926
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
927
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
928
        float weight = weight_windows[i];
×
929
        if (weight == 0.0)
×
930
          weight = min_weight;
×
931
        weight = convert_to_big_endian<float>(weight);
×
932
        std::fwrite(&weight, sizeof(float), 1, plot);
×
933
      }
934
    }
935

936
    std::fclose(plot);
×
UNCOV
937
  }
×
UNCOV
938
}
×
939

940
void FlatSourceDomain::apply_external_source_to_source_region(
4,500✔
941
  int src_idx, SourceRegionHandle& srh)
942
{
943
  auto s = model::external_sources[src_idx].get();
4,500✔
944
  auto is = dynamic_cast<IndependentSource*>(s);
4,500!
945
  auto discrete = dynamic_cast<Discrete*>(is->energy());
4,500!
946
  double strength_factor = is->strength();
4,500✔
947
  const auto& discrete_energies = discrete->x();
4,500✔
948
  const auto& discrete_probs = discrete->prob();
4,500✔
949

950
  srh.external_source_present() = 1;
4,500✔
951

952
  for (int i = 0; i < discrete_energies.size(); i++) {
9,132✔
953
    int g = data::mg.get_group_index(discrete_energies[i]);
4,632✔
954
    srh.external_source(g) += discrete_probs[i] * strength_factor;
4,632✔
955
  }
956
}
4,500✔
957

958
void FlatSourceDomain::apply_external_source_to_cell_instances(int32_t i_cell,
416✔
959
  int src_idx, int target_material_id, const vector<int32_t>& instances)
960
{
961
  Cell& cell = *model::cells[i_cell];
416✔
962

963
  if (cell.type_ != Fill::MATERIAL)
416!
964
    return;
×
965

966
  for (int j : instances) {
30,784✔
967
    int cell_material_idx = cell.material(j);
30,368✔
968
    int cell_material_id;
969
    if (cell_material_idx == MATERIAL_VOID) {
30,368✔
970
      cell_material_id = MATERIAL_VOID;
256✔
971
    } else {
972
      cell_material_id = model::materials[cell_material_idx]->id();
30,112✔
973
    }
974
    if (target_material_id == C_NONE ||
30,368✔
975
        cell_material_id == target_material_id) {
976
      int64_t source_region = source_region_offsets_[i_cell] + j;
2,848✔
977
      external_volumetric_source_map_[source_region].push_back(src_idx);
2,848✔
978
    }
979
  }
980
}
981

982
void FlatSourceDomain::apply_external_source_to_cell_and_children(
448✔
983
  int32_t i_cell, int src_idx, int32_t target_material_id)
984
{
985
  Cell& cell = *model::cells[i_cell];
448✔
986

987
  if (cell.type_ == Fill::MATERIAL) {
448✔
988
    vector<int> instances(cell.n_instances());
416✔
989
    std::iota(instances.begin(), instances.end(), 0);
416✔
990
    apply_external_source_to_cell_instances(
416✔
991
      i_cell, src_idx, target_material_id, instances);
992
  } else if (target_material_id == C_NONE) {
448!
993
    std::unordered_map<int32_t, vector<int32_t>> cell_instance_list =
994
      cell.get_contained_cells(0, nullptr);
×
995
    for (const auto& pair : cell_instance_list) {
×
996
      int32_t i_child_cell = pair.first;
×
997
      apply_external_source_to_cell_instances(
×
998
        i_child_cell, src_idx, target_material_id, pair.second);
×
999
    }
UNCOV
1000
  }
×
1001
}
448✔
1002

1003
void FlatSourceDomain::count_external_source_regions()
1,059✔
1004
{
1005
  n_external_source_regions_ = 0;
1,059✔
1006
#pragma omp parallel for reduction(+ : n_external_source_regions_)
597✔
1007
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
1,174,942✔
1008
    if (source_regions_.external_source_present(sr)) {
1,174,480✔
1009
      n_external_source_regions_++;
157,210✔
1010
    }
1011
  }
1012
}
1,059✔
1013

1014
void FlatSourceDomain::convert_external_sources()
401✔
1015
{
1016
  // Loop over external sources
1017
  for (int es = 0; es < model::external_sources.size(); es++) {
802✔
1018

1019
    // Extract source information
1020
    Source* s = model::external_sources[es].get();
401✔
1021
    IndependentSource* is = dynamic_cast<IndependentSource*>(s);
401!
1022
    Discrete* energy = dynamic_cast<Discrete*>(is->energy());
401!
1023
    const std::unordered_set<int32_t>& domain_ids = is->domain_ids();
401✔
1024
    double strength_factor = is->strength();
401✔
1025

1026
    // If there is no domain constraint specified, then this must be a point
1027
    // source. In this case, we need to find the source region that contains the
1028
    // point source and apply or relate it to the external source.
1029
    if (is->domain_ids().size() == 0) {
401✔
1030

1031
      // Extract the point source coordinate and find the base source region at
1032
      // that point
1033
      auto sp = dynamic_cast<SpatialPoint*>(is->space());
17!
1034
      GeometryState gs;
17✔
1035
      gs.r() = sp->r();
17✔
1036
      gs.r_last() = sp->r();
17✔
1037
      gs.u() = {1.0, 0.0, 0.0};
17✔
1038
      bool found = exhaustive_find_cell(gs);
17✔
1039
      if (!found) {
17!
1040
        fatal_error(fmt::format("Could not find cell containing external "
×
1041
                                "point source at {}",
1042
          sp->r()));
×
1043
      }
1044
      SourceRegionKey key = lookup_source_region_key(gs);
17✔
1045

1046
      // With the source region and mesh bin known, we can use the
1047
      // accompanying SourceRegionKey as a key into a map that stores the
1048
      // corresponding external source index for the point source. Notably, we
1049
      // do not actually apply the external source to any source regions here,
1050
      // as if mesh subdivision is enabled, they haven't actually been
1051
      // discovered & initilized yet. When discovered, they will read from the
1052
      // external_source_map to determine if there are any external source
1053
      // terms that should be applied.
1054
      external_point_source_map_[key].push_back(es);
17✔
1055

1056
    } else {
17✔
1057
      // If not a point source, then use the volumetric domain constraints to
1058
      // determine which source regions to apply the external source to.
1059
      if (is->domain_type() == Source::DomainType::MATERIAL) {
384✔
1060
        for (int32_t material_id : domain_ids) {
32✔
1061
          for (int i_cell = 0; i_cell < model::cells.size(); i_cell++) {
96✔
1062
            apply_external_source_to_cell_and_children(i_cell, es, material_id);
80✔
1063
          }
1064
        }
1065
      } else if (is->domain_type() == Source::DomainType::CELL) {
368✔
1066
        for (int32_t cell_id : domain_ids) {
32✔
1067
          int32_t i_cell = model::cell_map[cell_id];
16✔
1068
          apply_external_source_to_cell_and_children(i_cell, es, C_NONE);
16✔
1069
        }
1070
      } else if (is->domain_type() == Source::DomainType::UNIVERSE) {
352!
1071
        for (int32_t universe_id : domain_ids) {
704✔
1072
          int32_t i_universe = model::universe_map[universe_id];
352✔
1073
          Universe& universe = *model::universes[i_universe];
352✔
1074
          for (int32_t i_cell : universe.cells_) {
704✔
1075
            apply_external_source_to_cell_and_children(i_cell, es, C_NONE);
352✔
1076
          }
1077
        }
1078
      }
1079
    }
1080
  } // End loop over external sources
1081
}
401✔
1082

1083
void FlatSourceDomain::flux_swap()
11,672✔
1084
{
1085
  source_regions_.flux_swap();
11,672✔
1086
}
11,672✔
1087

1088
void FlatSourceDomain::flatten_xs()
577✔
1089
{
1090
  // Temperature and angle indices, if using multiple temperature
1091
  // data sets and/or anisotropic data sets.
1092
  // TODO: Currently assumes we are only using single temp/single angle data.
1093
  const int t = 0;
577✔
1094
  const int a = 0;
577✔
1095

1096
  n_materials_ = data::mg.macro_xs_.size();
577✔
1097
  for (int i = 0; i < n_materials_; i++) {
2,146✔
1098
    auto& m = data::mg.macro_xs_[i];
1,569✔
1099
    for (int g_out = 0; g_out < negroups_; g_out++) {
8,131✔
1100
      if (m.exists_in_model) {
6,562✔
1101
        double sigma_t =
1102
          m.get_xs(MgxsType::TOTAL, g_out, NULL, NULL, NULL, t, a);
6,498✔
1103
        sigma_t_.push_back(sigma_t);
6,498✔
1104

1105
        if (sigma_t < MINIMUM_MACRO_XS) {
6,498✔
1106
          Material* mat = model::materials[i].get();
16✔
1107
          warning(fmt::format(
16✔
1108
            "Material \"{}\" (id: {}) has a group {} total cross section "
1109
            "({:.3e}) below the minimum threshold "
1110
            "({:.3e}). Material will be treated as pure void.",
1111
            mat->name(), mat->id(), g_out, sigma_t, MINIMUM_MACRO_XS));
32✔
1112
        }
1113

1114
        double nu_sigma_f =
1115
          m.get_xs(MgxsType::NU_FISSION, g_out, NULL, NULL, NULL, t, a);
6,498✔
1116
        nu_sigma_f_.push_back(nu_sigma_f);
6,498✔
1117

1118
        double sigma_f =
1119
          m.get_xs(MgxsType::FISSION, g_out, NULL, NULL, NULL, t, a);
6,498✔
1120
        sigma_f_.push_back(sigma_f);
6,498✔
1121

1122
        double chi =
1123
          m.get_xs(MgxsType::CHI_PROMPT, g_out, &g_out, NULL, NULL, t, a);
6,498✔
1124
        if (!std::isfinite(chi)) {
6,498!
1125
          // MGXS interface may return NaN in some cases, such as when material
1126
          // is fissionable but has very small sigma_f.
1127
          chi = 0.0;
×
1128
        }
1129
        chi_.push_back(chi);
6,498✔
1130

1131
        for (int g_in = 0; g_in < negroups_; g_in++) {
255,878✔
1132
          double sigma_s =
1133
            m.get_xs(MgxsType::NU_SCATTER, g_in, &g_out, NULL, NULL, t, a);
249,380✔
1134
          sigma_s_.push_back(sigma_s);
249,380✔
1135
          // For transport corrected XS data, diagonal elements may be negative.
1136
          // In this case, set a flag to enable transport stabilization for the
1137
          // simulation.
1138
          if (g_out == g_in && sigma_s < 0.0)
249,380✔
1139
            is_transport_stabilization_needed_ = true;
848✔
1140
        }
1141
      } else {
1142
        sigma_t_.push_back(0);
64✔
1143
        nu_sigma_f_.push_back(0);
64✔
1144
        sigma_f_.push_back(0);
64✔
1145
        chi_.push_back(0);
64✔
1146
        for (int g_in = 0; g_in < negroups_; g_in++) {
128✔
1147
          sigma_s_.push_back(0);
64✔
1148
        }
1149
      }
1150
    }
1151
  }
1152
}
577✔
1153

1154
void FlatSourceDomain::set_adjoint_sources()
65✔
1155
{
1156
  // Set the adjoint external source to 1/forward_flux. If the forward flux is
1157
  // negative, zero, or extremely close to zero, set the adjoint source to zero,
1158
  // as this is likely a very small source region that we don't need to bother
1159
  // trying to vector particles towards. In the case of flux "being extremely
1160
  // close to zero", we define this as being a fixed fraction of the maximum
1161
  // forward flux, below which we assume the flux would be physically
1162
  // undetectable.
1163

1164
  // First, find the maximum forward flux value
1165
  double max_flux = 0.0;
65✔
1166
#pragma omp parallel for reduction(max : max_flux)
37✔
1167
  for (int64_t se = 0; se < n_source_elements(); se++) {
155,548✔
1168
    double flux = source_regions_.scalar_flux_final(se);
155,520✔
1169
    if (flux > max_flux) {
155,520✔
1170
      max_flux = flux;
25✔
1171
    }
1172
  }
1173

1174
  // Then, compute the adjoint source for each source region
1175
#pragma omp parallel for
37✔
1176
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
155,548✔
1177
    for (int g = 0; g < negroups_; g++) {
311,040✔
1178
      double flux = source_regions_.scalar_flux_final(sr, g);
155,520✔
1179
      if (flux <= ZERO_FLUX_CUTOFF * max_flux) {
155,520✔
1180
        source_regions_.external_source(sr, g) = 0.0;
325✔
1181
      } else {
1182
        source_regions_.external_source(sr, g) = 1.0 / flux;
155,195✔
1183
      }
1184
      if (flux > 0.0) {
155,520✔
1185
        source_regions_.external_source_present(sr) = 1;
155,195✔
1186
      }
1187
      source_regions_.scalar_flux_final(sr, g) = 0.0;
155,520✔
1188
    }
1189
  }
1190

1191
  // "Small" source regions in OpenMC are defined as those that are hit by
1192
  // MIN_HITS_PER_BATCH rays or fewer each batch. These regions typically have
1193
  // very small volumes combined with a low aspect ratio, and are often
1194
  // generated when applying a source region mesh that clips the edge of a
1195
  // curved surface. As perhaps only a few rays will visit these regions over
1196
  // the entire forward simulation, the forward flux estimates are extremely
1197
  // noisy and unreliable. In some cases, the noise may make the forward fluxes
1198
  // extremely low, leading to unphysically large adjoint source terms,
1199
  // resulting in weight windows that aggressively try to drive particles
1200
  // towards these regions. To fix this, we simply filter out any "small" source
1201
  // regions from consideration. If a source region is "small", we
1202
  // set its adjoint source to zero. This adds negligible bias to the adjoint
1203
  // flux solution, as the true total adjoint source contribution from small
1204
  // regions is likely to be negligible.
1205
#pragma omp parallel for
37✔
1206
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
155,548✔
1207
    if (source_regions_.is_small(sr)) {
155,520!
1208
      for (int g = 0; g < negroups_; g++) {
×
1209
        source_regions_.external_source(sr, g) = 0.0;
1210
      }
1211
      source_regions_.external_source_present(sr) = 0;
1212
    }
1213
  }
1214
  // Divide the fixed source term by sigma t (to save time when applying each
1215
  // iteration)
1216
#pragma omp parallel for
37✔
1217
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
155,548✔
1218
    int material = source_regions_.material(sr);
155,520✔
1219
    if (material == MATERIAL_VOID) {
155,520!
1220
      continue;
1221
    }
1222
    for (int g = 0; g < negroups_; g++) {
311,040✔
1223
      double sigma_t = sigma_t_[material * negroups_ + g];
155,520✔
1224
      source_regions_.external_source(sr, g) /= sigma_t;
155,520✔
1225
    }
1226
  }
1227
}
65✔
1228

1229
void FlatSourceDomain::transpose_scattering_matrix()
81✔
1230
{
1231
  // Transpose the inner two dimensions for each material
1232
  for (int m = 0; m < n_materials_; ++m) {
306✔
1233
    int material_offset = m * negroups_ * negroups_;
225✔
1234
    for (int i = 0; i < negroups_; ++i) {
643✔
1235
      for (int j = i + 1; j < negroups_; ++j) {
1,091✔
1236
        // Calculate indices of the elements to swap
1237
        int idx1 = material_offset + i * negroups_ + j;
673✔
1238
        int idx2 = material_offset + j * negroups_ + i;
673✔
1239

1240
        // Swap the elements to transpose the matrix
1241
        std::swap(sigma_s_[idx1], sigma_s_[idx2]);
673✔
1242
      }
1243
    }
1244
  }
1245
}
81✔
1246

1247
void FlatSourceDomain::serialize_final_fluxes(vector<double>& flux)
×
1248
{
1249
  // Ensure array is correct size
1250
  flux.resize(n_source_regions() * negroups_);
×
1251
// Serialize the final fluxes for output
1252
#pragma omp parallel for
1253
  for (int64_t se = 0; se < n_source_elements(); se++) {
×
1254
    flux[se] = source_regions_.scalar_flux_final(se);
1255
  }
UNCOV
1256
}
×
1257

1258
void FlatSourceDomain::apply_mesh_to_cell_instances(int32_t i_cell,
610✔
1259
  int32_t mesh_idx, int target_material_id, const vector<int32_t>& instances,
1260
  bool is_target_void)
1261
{
1262
  Cell& cell = *model::cells[i_cell];
610✔
1263
  if (cell.type_ != Fill::MATERIAL)
610!
1264
    return;
×
1265
  for (int32_t j : instances) {
174,948✔
1266
    int cell_material_idx = cell.material(j);
174,338✔
1267
    int cell_material_id = (cell_material_idx == C_NONE)
1268
                             ? C_NONE
174,338✔
1269
                             : model::materials[cell_material_idx]->id();
174,337✔
1270

1271
    if ((target_material_id == C_NONE && !is_target_void) ||
174,338!
1272
        cell_material_id == target_material_id) {
1273
      int64_t sr = source_region_offsets_[i_cell] + j;
142,338✔
1274
      // Check if the key is already present in the mesh_map_
1275
      if (mesh_map_.find(sr) != mesh_map_.end()) {
142,338!
1276
        fatal_error(fmt::format("Source region {} already has mesh idx {} "
×
1277
                                "applied, but trying to apply mesh idx {}",
1278
          sr, mesh_map_[sr], mesh_idx));
×
1279
      }
1280
      // If the SR has not already been assigned, then we can write to it
1281
      mesh_map_[sr] = mesh_idx;
142,338✔
1282
    }
1283
  }
1284
}
1285

1286
void FlatSourceDomain::apply_mesh_to_cell_and_children(int32_t i_cell,
481✔
1287
  int32_t mesh_idx, int32_t target_material_id, bool is_target_void)
1288
{
1289
  Cell& cell = *model::cells[i_cell];
481✔
1290

1291
  if (cell.type_ == Fill::MATERIAL) {
481✔
1292
    vector<int> instances(cell.n_instances());
352✔
1293
    std::iota(instances.begin(), instances.end(), 0);
352✔
1294
    apply_mesh_to_cell_instances(
352✔
1295
      i_cell, mesh_idx, target_material_id, instances, is_target_void);
1296
  } else if (target_material_id == C_NONE && !is_target_void) {
481!
1297
    for (int j = 0; j < cell.n_instances(); j++) {
130✔
1298
      std::unordered_map<int32_t, vector<int32_t>> cell_instance_list =
1299
        cell.get_contained_cells(j, nullptr);
65✔
1300
      for (const auto& pair : cell_instance_list) {
323✔
1301
        int32_t i_child_cell = pair.first;
258✔
1302
        apply_mesh_to_cell_instances(i_child_cell, mesh_idx, target_material_id,
258✔
1303
          pair.second, is_target_void);
258✔
1304
      }
1305
    }
65✔
1306
  }
1307
}
481✔
1308

1309
void FlatSourceDomain::apply_meshes()
577✔
1310
{
1311
  // Skip if there are no mappings between mesh IDs and domains
1312
  if (mesh_domain_map_.empty())
577✔
1313
    return;
416✔
1314

1315
  // Loop over meshes
1316
  for (int mesh_idx = 0; mesh_idx < model::meshes.size(); mesh_idx++) {
402✔
1317
    Mesh* mesh = model::meshes[mesh_idx].get();
241✔
1318
    int mesh_id = mesh->id();
241✔
1319

1320
    // Skip if mesh id is not present in the map
1321
    if (mesh_domain_map_.find(mesh_id) == mesh_domain_map_.end())
241✔
1322
      continue;
16✔
1323

1324
    // Loop over domains associated with the mesh
1325
    for (auto& domain : mesh_domain_map_[mesh_id]) {
450✔
1326
      Source::DomainType domain_type = domain.first;
225✔
1327
      int domain_id = domain.second;
225✔
1328

1329
      if (domain_type == Source::DomainType::MATERIAL) {
225✔
1330
        for (int i_cell = 0; i_cell < model::cells.size(); i_cell++) {
192✔
1331
          if (domain_id == C_NONE) {
160!
1332
            apply_mesh_to_cell_and_children(i_cell, mesh_idx, domain_id, true);
×
1333
          } else {
1334
            apply_mesh_to_cell_and_children(i_cell, mesh_idx, domain_id, false);
160✔
1335
          }
1336
        }
1337
      } else if (domain_type == Source::DomainType::CELL) {
193✔
1338
        int32_t i_cell = model::cell_map[domain_id];
32✔
1339
        apply_mesh_to_cell_and_children(i_cell, mesh_idx, C_NONE, false);
32✔
1340
      } else if (domain_type == Source::DomainType::UNIVERSE) {
161!
1341
        int32_t i_universe = model::universe_map[domain_id];
161✔
1342
        Universe& universe = *model::universes[i_universe];
161✔
1343
        for (int32_t i_cell : universe.cells_) {
450✔
1344
          apply_mesh_to_cell_and_children(i_cell, mesh_idx, C_NONE, false);
289✔
1345
        }
1346
      }
1347
    }
1348
  }
1349
}
1350

1351
SourceRegionHandle FlatSourceDomain::get_subdivided_source_region_handle(
1,219,405,633✔
1352
  SourceRegionKey sr_key, Position r, Direction u)
1353
{
1354
  // Case 1: Check if the source region key is already present in the permanent
1355
  // map. This is the most common condition, as any source region visited in a
1356
  // previous power iteration will already be present in the permanent map. If
1357
  // the source region key is found, we translate the key into a specific 1D
1358
  // source region index and return a handle its position in the
1359
  // source_regions_ vector.
1360
  auto it = source_region_map_.find(sr_key);
1,219,405,633✔
1361
  if (it != source_region_map_.end()) {
1,219,405,633✔
1362
    int64_t sr = it->second;
1,184,926,003✔
1363
    return source_regions_.get_source_region_handle(sr);
1,184,926,003✔
1364
  }
1365

1366
  // Case 2: Check if the source region key is present in the temporary (thread
1367
  // safe) map. This is a common occurrence in the first power iteration when
1368
  // the source region has already been visited already by some other ray. We
1369
  // begin by locking the temporary map before any operations are performed. The
1370
  // lock is not global over the full data structure -- it will be dependent on
1371
  // which key is used.
1372
  discovered_source_regions_.lock(sr_key);
34,479,630✔
1373

1374
  // If the key is found in the temporary map, then we return a handle to the
1375
  // source region that is stored in the temporary map.
1376
  if (discovered_source_regions_.contains(sr_key)) {
34,479,630✔
1377
    SourceRegionHandle handle {discovered_source_regions_[sr_key]};
32,138,985✔
1378
    discovered_source_regions_.unlock(sr_key);
32,138,985✔
1379
    return handle;
32,138,985✔
1380
  }
1381

1382
  // Case 3: The source region key is not present anywhere, but it is only due
1383
  // to floating point artifacts. These artifacts occur when the overlaid mesh
1384
  // overlaps with actual geometry surfaces. In these cases, roundoff error may
1385
  // result in the ray tracer detecting an additional (very short) segment
1386
  // though a mesh bin that is actually past the physical source region
1387
  // boundary. This is a result of the the multi-level ray tracing treatment in
1388
  // OpenMC, which depending on the number of universes in the hierarchy etc can
1389
  // result in the wrong surface being selected as the nearest. This can happen
1390
  // in a lattice when there are two directions that both are very close in
1391
  // distance, within the tolerance of FP_REL_PRECISION, and the are thus
1392
  // treated as being equivalent so alternative logic is used. However, when we
1393
  // go and ray trace on this with the mesh tracer we may go past the surface
1394
  // bounding the current source region.
1395
  //
1396
  // To filter out this case, before we create the new source region, we double
1397
  // check that the actual starting point of this segment (r) is still in the
1398
  // same geometry source region that we started in. If an artifact is detected,
1399
  // we discard the segment (and attenuation through it) as it is not really a
1400
  // valid source region and will have only an infinitessimally small cell
1401
  // combined with the mesh bin. Thankfully, this is a fairly rare condition,
1402
  // and only triggers for very short ray lengths. It can be fixed by decreasing
1403
  // the value of FP_REL_PRECISION in constants.h, but this may have unknown
1404
  // consequences for the general ray tracer, so for now we do the below sanity
1405
  // checks before generating phantom source regions. A significant extra cost
1406
  // is incurred in instantiating the GeometryState object and doing a cell
1407
  // lookup, but again, this is going to be an extremely rare thing to check
1408
  // after the first power iteration has completed.
1409

1410
  // Sanity check on source region id
1411
  GeometryState gs;
2,340,645✔
1412
  gs.r() = r + TINY_BIT * u;
2,340,645✔
1413
  gs.u() = {1.0, 0.0, 0.0};
2,340,645✔
1414
  exhaustive_find_cell(gs);
2,340,645✔
1415
  int64_t sr_found = lookup_base_source_region_idx(gs);
2,340,645✔
1416
  if (sr_found != sr_key.base_source_region_id) {
2,340,645✔
1417
    discovered_source_regions_.unlock(sr_key);
88✔
1418
    SourceRegionHandle handle;
88✔
1419
    handle.is_numerical_fp_artifact_ = true;
88✔
1420
    return handle;
88✔
1421
  }
1422

1423
  // Sanity check on mesh bin
1424
  int mesh_idx = lookup_mesh_idx(sr_key.base_source_region_id);
2,340,557✔
1425
  if (mesh_idx == C_NONE) {
2,340,557✔
1426
    if (sr_key.mesh_bin != 0) {
368,852!
1427
      discovered_source_regions_.unlock(sr_key);
×
1428
      SourceRegionHandle handle;
×
1429
      handle.is_numerical_fp_artifact_ = true;
×
1430
      return handle;
×
1431
    }
1432
  } else {
1433
    Mesh* mesh = model::meshes[mesh_idx].get();
1,971,705✔
1434
    int bin_found = mesh->get_bin(r + TINY_BIT * u);
1,971,705✔
1435
    if (bin_found != sr_key.mesh_bin) {
1,971,705!
1436
      discovered_source_regions_.unlock(sr_key);
×
1437
      SourceRegionHandle handle;
×
1438
      handle.is_numerical_fp_artifact_ = true;
×
1439
      return handle;
×
1440
    }
1441
  }
1442

1443
  // Case 4: The source region key is valid, but is not present anywhere. This
1444
  // condition only occurs the first time the source region is discovered
1445
  // (typically in the first power iteration). In this case, we need to handle
1446
  // creation of the new source region and its storage into the parallel map.
1447
  // Additionally, we need to determine the source region's material, initialize
1448
  // the starting scalar flux guess, and apply any known external sources.
1449

1450
  // Call the basic constructor for the source region and store in the parallel
1451
  // map.
1452
  bool is_linear = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
2,340,557✔
1453
  SourceRegion* sr_ptr =
1454
    discovered_source_regions_.emplace(sr_key, {negroups_, is_linear});
2,340,557✔
1455
  SourceRegionHandle handle {*sr_ptr};
2,340,557✔
1456

1457
  // Determine the material
1458
  int gs_i_cell = gs.lowest_coord().cell();
2,340,557✔
1459
  Cell& cell = *model::cells[gs_i_cell];
2,340,557✔
1460
  int material = cell.material(gs.cell_instance());
2,340,557✔
1461

1462
  // If material total XS is extremely low, just set it to void to avoid
1463
  // problems with 1/Sigma_t
1464
  for (int g = 0; g < negroups_; g++) {
4,970,307✔
1465
    double sigma_t = sigma_t_[material * negroups_ + g];
2,651,902✔
1466
    if (sigma_t < MINIMUM_MACRO_XS) {
2,651,902✔
1467
      material = MATERIAL_VOID;
22,152✔
1468
      break;
22,152✔
1469
    }
1470
  }
1471

1472
  handle.material() = material;
2,340,557✔
1473

1474
  // Store the mesh index (if any) assigned to this source region
1475
  handle.mesh() = mesh_idx;
2,340,557✔
1476

1477
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
2,340,557✔
1478
    // Determine if there are any volumetric sources, and apply them.
1479
    // Volumetric sources are specifc only to the base SR idx.
1480
    auto it_vol =
1481
      external_volumetric_source_map_.find(sr_key.base_source_region_id);
2,291,750✔
1482
    if (it_vol != external_volumetric_source_map_.end()) {
2,291,750✔
1483
      const vector<int>& vol_sources = it_vol->second;
4,488✔
1484
      for (int src_idx : vol_sources) {
8,976✔
1485
        apply_external_source_to_source_region(src_idx, handle);
4,488✔
1486
      }
1487
    }
1488

1489
    // Determine if there are any point sources, and apply them.
1490
    // Point sources are specific to the source region key.
1491
    auto it_point = external_point_source_map_.find(sr_key);
2,291,750✔
1492
    if (it_point != external_point_source_map_.end()) {
2,291,750✔
1493
      const vector<int>& point_sources = it_point->second;
12✔
1494
      for (int src_idx : point_sources) {
24✔
1495
        apply_external_source_to_source_region(src_idx, handle);
12✔
1496
      }
1497
    }
1498

1499
    // Divide external source term by sigma_t
1500
    if (material != C_NONE) {
2,291,750✔
1501
      for (int g = 0; g < negroups_; g++) {
4,585,155✔
1502
        double sigma_t = sigma_t_[material * negroups_ + g];
2,315,557✔
1503
        handle.external_source(g) /= sigma_t;
2,315,557✔
1504
      }
1505
    }
1506
  }
1507

1508
  // Compute the combined source term
1509
  update_single_neutron_source(handle);
2,340,557✔
1510

1511
  // Unlock the parallel map. Note: we may be tempted to release
1512
  // this lock earlier, and then just use the source region's lock to protect
1513
  // the flux/source initialization stages above. However, the rest of the code
1514
  // only protects updates to the new flux and volume fields, and assumes that
1515
  // the source is constant for the duration of transport. Thus, using just the
1516
  // source region's lock by itself would result in other threads potentially
1517
  // reading from the source before it is computed, as they won't use the lock
1518
  // when only reading from the SR's source. It would be expensive to protect
1519
  // those operations, whereas generating the SR is only done once, so we just
1520
  // hold the map's bucket lock until the source region is fully initialized.
1521
  discovered_source_regions_.unlock(sr_key);
2,340,557✔
1522

1523
  return handle;
2,340,557✔
1524
}
2,340,645✔
1525

1526
void FlatSourceDomain::finalize_discovered_source_regions()
11,672✔
1527
{
1528
  // Extract keys for entries with a valid volume.
1529
  vector<SourceRegionKey> keys;
11,672✔
1530
  for (const auto& pair : discovered_source_regions_) {
2,352,229✔
1531
    if (pair.second.volume_ > 0.0) {
2,340,557✔
1532
      keys.push_back(pair.first);
2,239,115✔
1533
    }
1534
  }
1535

1536
  if (!keys.empty()) {
11,672✔
1537
    // Sort the keys, so as to ensure reproducible ordering given that source
1538
    // regions may have been added to discovered_source_regions_ in an arbitrary
1539
    // order due to shared memory threading.
1540
    std::sort(keys.begin(), keys.end());
727✔
1541

1542
    // Remember the index of the first new source region
1543
    int64_t start_sr_id = source_regions_.n_source_regions();
727✔
1544

1545
    // Append the source regions in the sorted key order.
1546
    for (const auto& key : keys) {
2,239,842✔
1547
      const SourceRegion& sr = discovered_source_regions_[key];
2,239,115✔
1548
      source_region_map_[key] = source_regions_.n_source_regions();
2,239,115✔
1549
      source_regions_.push_back(sr);
2,239,115✔
1550
    }
1551

1552
    // Map all new source regions to tallies
1553
    convert_source_regions_to_tallies(start_sr_id);
727✔
1554
  }
1555

1556
  discovered_source_regions_.clear();
11,672✔
1557
}
11,672✔
1558

1559
// This is the "diagonal stabilization" technique developed by Gunow et al. in:
1560
//
1561
// Geoffrey Gunow, Benoit Forget, Kord Smith, Stabilization of multi-group
1562
// neutron transport with transport-corrected cross-sections, Annals of Nuclear
1563
// Energy, Volume 126, 2019, Pages 211-219, ISSN 0306-4549,
1564
// https://doi.org/10.1016/j.anucene.2018.10.036.
1565
void FlatSourceDomain::apply_transport_stabilization()
11,672✔
1566
{
1567
  // Don't do anything if all in-group scattering
1568
  // cross sections are positive
1569
  if (!is_transport_stabilization_needed_) {
11,672✔
1570
    return;
11,452✔
1571
  }
1572

1573
  // Apply the stabilization factor to all source elements
1574
#pragma omp parallel for
120✔
1575
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
1,300✔
1576
    int material = source_regions_.material(sr);
1,200✔
1577
    if (material == MATERIAL_VOID) {
1,200!
1578
      continue;
1579
    }
1580
    for (int g = 0; g < negroups_; g++) {
85,200✔
1581
      // Only apply stabilization if the diagonal (in-group) scattering XS is
1582
      // negative
1583
      double sigma_s =
1584
        sigma_s_[material * negroups_ * negroups_ + g * negroups_ + g];
84,000✔
1585
      if (sigma_s < 0.0) {
84,000✔
1586
        double sigma_t = sigma_t_[material * negroups_ + g];
21,200✔
1587
        double phi_new = source_regions_.scalar_flux_new(sr, g);
21,200✔
1588
        double phi_old = source_regions_.scalar_flux_old(sr, g);
21,200✔
1589

1590
        // Equation 18 in the above Gunow et al. 2019 paper. For a default
1591
        // rho of 1.0, this ensures there are no negative diagonal elements
1592
        // in the iteration matrix. A lesser rho could be used (or exposed
1593
        // as a user input parameter) to reduce the negative impact on
1594
        // convergence rate though would need to be experimentally tested to see
1595
        // if it doesn't become unstable. rho = 1.0 is good as it gives the
1596
        // highest assurance of stability, and the impacts on convergence rate
1597
        // are pretty mild.
1598
        double D = diagonal_stabilization_rho_ * sigma_s / sigma_t;
21,200✔
1599

1600
        // Equation 16 in the above Gunow et al. 2019 paper
1601
        source_regions_.scalar_flux_new(sr, g) =
21,200✔
1602
          (phi_new - D * phi_old) / (1.0 - D);
21,200✔
1603
      }
1604
    }
1605
  }
1606
}
1607

1608
// Determines the base source region index (i.e., a material filled cell
1609
// instance) that corresponds to a particular location in the geometry. Requires
1610
// that the "gs" object passed in has already been initialized and has called
1611
// find_cell etc.
1612
int64_t FlatSourceDomain::lookup_base_source_region_idx(
776,449,063✔
1613
  const GeometryState& gs) const
1614
{
1615
  int i_cell = gs.lowest_coord().cell();
776,449,063✔
1616
  int64_t sr = source_region_offsets_[i_cell] + gs.cell_instance();
776,449,063✔
1617
  return sr;
776,449,063✔
1618
}
1619

1620
// Determines the index of the mesh (if any) that has been applied
1621
// to a particular base source region index.
1622
int FlatSourceDomain::lookup_mesh_idx(int64_t sr) const
776,448,975✔
1623
{
1624
  int mesh_idx = C_NONE;
776,448,975✔
1625
  auto mesh_it = mesh_map_.find(sr);
776,448,975✔
1626
  if (mesh_it != mesh_map_.end()) {
776,448,975✔
1627
    mesh_idx = mesh_it->second;
428,265,414✔
1628
  }
1629
  return mesh_idx;
776,448,975✔
1630
}
1631

1632
// Determines the source region key that corresponds to a particular location in
1633
// the geometry. This takes into account both the base source region index as
1634
// well as the mesh bin if a mesh is applied to this source region for
1635
// subdivision.
1636
SourceRegionKey FlatSourceDomain::lookup_source_region_key(
1,867,437✔
1637
  const GeometryState& gs) const
1638
{
1639
  int64_t sr = lookup_base_source_region_idx(gs);
1,867,437✔
1640
  int64_t mesh_bin = lookup_mesh_bin(sr, gs.r());
1,867,437✔
1641
  return SourceRegionKey {sr, mesh_bin};
1,867,437✔
1642
}
1643

1644
// Determines the mesh bin that corresponds to a particular base source region
1645
// index and position.
1646
int64_t FlatSourceDomain::lookup_mesh_bin(int64_t sr, Position r) const
1,867,437✔
1647
{
1648
  int mesh_idx = lookup_mesh_idx(sr);
1,867,437✔
1649
  int mesh_bin = 0;
1,867,437✔
1650
  if (mesh_idx != C_NONE) {
1,867,437✔
1651
    mesh_bin = model::meshes[mesh_idx]->get_bin(r);
1,145,837✔
1652
  }
1653
  return mesh_bin;
1,867,437✔
1654
}
1655

1656
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc