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

openmc-dev / openmc / 27040957505

05 Jun 2026 09:25PM UTC coverage: 81.347% (-0.009%) from 81.356%
27040957505

Pull #3962

github

web-flow
Merge c1aa9b258 into 111eb7706
Pull Request #3962: Random Ray Forward Flux Save in Adjoint Mode

18020 of 26124 branches covered (68.98%)

Branch coverage included in aggregate %.

45 of 48 new or added lines in 7 files covered. (93.75%)

2 existing lines in 2 files now uncovered.

59172 of 68768 relevant lines covered (86.05%)

48501815.73 hits per line

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

71.76
/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
#include <numeric>
22

23
namespace openmc {
24

25
//==============================================================================
26
// FlatSourceDomain implementation
27
//==============================================================================
28

29
// Static Variable Declarations
30
RandomRayVolumeEstimator FlatSourceDomain::volume_estimator_ {
31
  RandomRayVolumeEstimator::HYBRID};
32
bool FlatSourceDomain::volume_normalized_flux_tallies_ {false};
33
bool FlatSourceDomain::adjoint_requested_ {false};
34
RandomRaySolve FlatSourceDomain::solve_ {RandomRaySolve::FORWARD};
35
bool FlatSourceDomain::fw_cadis_local_ {false};
36
double FlatSourceDomain::diagonal_stabilization_rho_ {1.0};
37
std::unordered_map<int, vector<std::pair<Source::DomainType, int>>>
38
  FlatSourceDomain::mesh_domain_map_;
39
std::vector<size_t> FlatSourceDomain::fw_cadis_local_targets_;
40

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

57
  // Initialize source regions.
58
  bool is_linear = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
792✔
59
  source_regions_ = SourceRegionContainer(negroups_, is_linear);
792✔
60

61
  // Initialize tally volumes
62
  if (volume_normalized_flux_tallies_) {
792✔
63
    tally_volumes_.resize(model::tallies.size());
506✔
64
    for (int i = 0; i < model::tallies.size(); i++) {
1,837✔
65
      //  Get the shape of the 3D result tensor
66
      auto shape = model::tallies[i]->results().shape();
1,331✔
67

68
      // Create a new 2D tensor with the same size as the first
69
      // two dimensions of the 3D tensor
70
      tally_volumes_[i] = tensor::Tensor<double>({shape[0], shape[1]});
2,662✔
71
    }
1,331✔
72
  }
73

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

82
void FlatSourceDomain::batch_reset()
15,962✔
83
{
84
// Reset scalar fluxes and iteration volume tallies to zero
85
#pragma omp parallel for
8,712✔
86
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
32,862,035✔
87
    source_regions_.volume(sr) = 0.0;
32,854,785✔
88
    source_regions_.volume_sq(sr) = 0.0;
32,854,785✔
89
  }
90

91
#pragma omp parallel for
8,712✔
92
  for (int64_t se = 0; se < n_source_elements(); se++) {
37,526,975✔
93
    source_regions_.scalar_flux_new(se) = 0.0;
37,519,725✔
94
  }
95
}
15,962✔
96

97
void FlatSourceDomain::accumulate_iteration_flux()
7,486✔
98
{
99
#pragma omp parallel for
4,086✔
100
  for (int64_t se = 0; se < n_source_elements(); se++) {
17,236,600✔
101
    source_regions_.scalar_flux_final(se) +=
17,233,200✔
102
      source_regions_.scalar_flux_new(se);
17,233,200✔
103
  }
104
}
7,486✔
105

106
void FlatSourceDomain::update_single_neutron_source(SourceRegionHandle& srh)
45,065,426✔
107
{
108
  // Reset all source regions to zero (important for void regions)
109
  for (int g = 0; g < negroups_; g++) {
96,355,630✔
110
    srh.source(g) = 0.0;
51,290,204✔
111
  }
112

113
  // Add scattering + fission source
114
  int material = srh.material();
45,065,426✔
115
  int temp = srh.temperature_idx();
45,065,426✔
116
  double density_mult = srh.density_mult();
45,065,426✔
117
  if (material != MATERIAL_VOID) {
45,065,426✔
118
    double inverse_k_eff = 1.0 / k_eff_;
44,623,778✔
119
    const int material_offset = (material * ntemperature_ + temp) * negroups_;
44,623,778✔
120
    const int scatter_offset =
44,623,778✔
121
      (material * ntemperature_ + temp) * negroups_ * negroups_;
122
    for (int g_out = 0; g_out < negroups_; g_out++) {
95,471,566✔
123
      double sigma_t = sigma_t_[material_offset + g_out] * density_mult;
50,847,788✔
124
      double scatter_source = 0.0;
50,847,788✔
125
      double fission_source = 0.0;
50,847,788✔
126

127
      for (int g_in = 0; g_in < negroups_; g_in++) {
145,262,266✔
128
        double scalar_flux = srh.scalar_flux_old(g_in);
94,414,478!
129
        double sigma_s =
94,414,478✔
130
          sigma_s_[scatter_offset + g_out * negroups_ + g_in] * density_mult;
94,414,478!
131
        double nu_sigma_f = nu_sigma_f_[material_offset + g_in] * density_mult;
94,414,478✔
132
        double chi = chi_[material_offset + g_out];
94,414,478✔
133

134
        scatter_source += sigma_s * scalar_flux;
94,414,478✔
135
        if (settings::create_fission_neutrons) {
94,414,478!
136
          fission_source += nu_sigma_f * scalar_flux * chi;
94,414,478✔
137
        }
138
      }
139
      srh.source(g_out) =
50,847,788✔
140
        (scatter_source + fission_source * inverse_k_eff) / sigma_t;
50,847,788✔
141
    }
142
  }
143

144
  // Add external source if in fixed source mode
145
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
45,065,426✔
146
    for (int g = 0; g < negroups_; g++) {
91,772,678✔
147
      srh.source(g) += srh.external_source(g);
47,322,361✔
148
    }
149
  }
150
}
45,065,426✔
151

152
// Compute new estimate of scattering + fission sources in each source region
153
// based on the flux estimate from the previous iteration.
154
void FlatSourceDomain::update_all_neutron_sources()
15,962✔
155
{
156
  simulation::time_update_src.start();
15,962✔
157

158
#pragma omp parallel for
8,712✔
159
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
32,862,035✔
160
    SourceRegionHandle srh = source_regions_.get_source_region_handle(sr);
32,854,785✔
161
    update_single_neutron_source(srh);
32,854,785✔
162
  }
163

164
  simulation::time_update_src.stop();
15,962✔
165
}
15,962✔
166

167
// Normalizes flux and updates simulation-averaged volume estimate
168
void FlatSourceDomain::normalize_scalar_flux_and_volumes(
8,427✔
169
  double total_active_distance_per_iteration)
170
{
171
  double normalization_factor = 1.0 / total_active_distance_per_iteration;
8,427✔
172
  double volume_normalization_factor =
8,427✔
173
    1.0 / (total_active_distance_per_iteration * simulation::current_batch);
8,427✔
174

175
// Normalize scalar flux to total distance travelled by all rays this
176
// iteration
177
#pragma omp parallel for
4,602✔
178
  for (int64_t se = 0; se < n_source_elements(); se++) {
23,285,535✔
179
    source_regions_.scalar_flux_new(se) *= normalization_factor;
23,281,710✔
180
  }
181

182
// Accumulate cell-wise ray length tallies collected this iteration, then
183
// update the simulation-averaged cell-wise volume estimates
184
#pragma omp parallel for
4,602✔
185
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
20,456,595✔
186
    source_regions_.volume_t(sr) += source_regions_.volume(sr);
20,452,770✔
187
    source_regions_.volume_sq_t(sr) += source_regions_.volume_sq(sr);
20,452,770✔
188
    source_regions_.volume_naive(sr) =
20,452,770✔
189
      source_regions_.volume(sr) * normalization_factor;
20,452,770✔
190
    source_regions_.volume_sq(sr) =
20,452,770✔
191
      source_regions_.volume_sq_t(sr) / source_regions_.volume_t(sr);
20,452,770✔
192
    source_regions_.volume(sr) =
20,452,770✔
193
      source_regions_.volume_t(sr) * volume_normalization_factor;
20,452,770✔
194
  }
195
}
8,427✔
196

197
void FlatSourceDomain::set_flux_to_flux_plus_source(
45,626,069✔
198
  int64_t sr, double volume, int g)
199
{
200
  int material = source_regions_.material(sr);
45,626,069✔
201
  int temp = source_regions_.temperature_idx(sr);
45,626,069✔
202
  if (material == MATERIAL_VOID) {
45,626,069✔
203
    source_regions_.scalar_flux_new(sr, g) /= volume;
882,416!
204
    if (settings::run_mode == RunMode::FIXED_SOURCE) {
882,416!
205
      source_regions_.scalar_flux_new(sr, g) +=
882,416✔
206
        0.5f * source_regions_.external_source(sr, g) *
882,416✔
207
        source_regions_.volume_sq(sr);
882,416✔
208
    }
209
  } else {
210
    double sigma_t =
44,743,653✔
211
      sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
44,743,653✔
212
      source_regions_.density_mult(sr);
44,743,653✔
213
    source_regions_.scalar_flux_new(sr, g) /= (sigma_t * volume);
44,743,653✔
214
    source_regions_.scalar_flux_new(sr, g) += source_regions_.source(sr, g);
44,743,653✔
215
  }
216
}
45,626,069✔
217

218
void FlatSourceDomain::set_flux_to_old_flux(int64_t sr, int g)
1,542✔
219
{
220
  source_regions_.scalar_flux_new(sr, g) =
1,542✔
221
    source_regions_.scalar_flux_old(sr, g);
1,542✔
222
}
1,542✔
223

224
void FlatSourceDomain::set_flux_to_source(int64_t sr, int g)
8,927,547✔
225
{
226
  source_regions_.scalar_flux_new(sr, g) = source_regions_.source(sr, g);
8,927,547✔
227
}
8,927,547✔
228

229
// Combine transport flux contributions and flat source contributions from the
230
// previous iteration to generate this iteration's estimate of scalar flux.
231
int64_t FlatSourceDomain::add_source_to_scalar_flux()
15,962✔
232
{
233
  int64_t n_hits = 0;
15,962✔
234
  double inverse_batch = 1.0 / simulation::current_batch;
15,962✔
235

236
#pragma omp parallel for reduction(+ : n_hits)
8,712✔
237
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
33,919,185✔
238

239
    double volume_simulation_avg = source_regions_.volume(sr);
33,911,935✔
240
    double volume_iteration = source_regions_.volume_naive(sr);
33,911,935✔
241

242
    // Increment the number of hits if cell was hit this iteration
243
    if (volume_iteration) {
33,911,935✔
244
      n_hits++;
29,852,780✔
245
    }
246

247
    // Set the SR to small status if its expected number of hits
248
    // per iteration is less than 1.5
249
    if (source_regions_.n_hits(sr) * inverse_batch < MIN_HITS_PER_BATCH) {
33,911,935✔
250
      source_regions_.is_small(sr) = 1;
6,812,705✔
251
    } else {
252
      source_regions_.is_small(sr) = 0;
27,099,230✔
253
    }
254

255
    // The volume treatment depends on the volume estimator type
256
    // and whether or not an external source is present in the cell.
257
    double volume;
33,911,935✔
258
    switch (volume_estimator_) {
33,911,935!
259
    case RandomRayVolumeEstimator::NAIVE:
260
      volume = volume_iteration;
261
      break;
262
    case RandomRayVolumeEstimator::SIMULATION_AVERAGED:
432,000✔
263
      volume = volume_simulation_avg;
432,000✔
264
      break;
432,000✔
265
    case RandomRayVolumeEstimator::HYBRID:
32,290,735✔
266
      if (source_regions_.external_source_present(sr) ||
32,290,735✔
267
          source_regions_.is_small(sr)) {
28,100,835✔
268
        volume = volume_iteration;
269
      } else {
270
        volume = volume_simulation_avg;
271
      }
272
      break;
273
    default:
274
      fatal_error("Invalid volume estimator type");
275
    }
276

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

314
  // Return the number of source regions that were hit this iteration
315
  return n_hits;
15,962✔
316
}
317

318
// Generates new estimate of k_eff based on the differences between this
319
// iteration's estimate of the scalar flux and the last iteration's estimate.
320
void FlatSourceDomain::compute_k_eff()
5,610✔
321
{
322
  double fission_rate_old = 0;
5,610✔
323
  double fission_rate_new = 0;
5,610✔
324

325
  // Vector for gathering fission source terms for Shannon entropy calculation
326
  vector<float> p(n_source_regions(), 0.0f);
5,610✔
327

328
#pragma omp parallel for reduction(+ : fission_rate_old, fission_rate_new)
3,060✔
329
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
388,740✔
330

331
    // If simulation averaged volume is zero, don't include this cell
332
    double volume = source_regions_.volume(sr);
386,190!
333
    if (volume == 0.0) {
386,190!
334
      continue;
335
    }
336

337
    int material = source_regions_.material(sr);
386,190!
338
    int temp = source_regions_.temperature_idx(sr);
386,190!
339
    if (material == MATERIAL_VOID) {
386,190!
340
      continue;
341
    }
342

343
    double sr_fission_source_old = 0;
344
    double sr_fission_source_new = 0;
345

346
    for (int g = 0; g < negroups_; g++) {
2,986,920✔
347
      double nu_sigma_f =
2,600,730✔
348
        nu_sigma_f_[(material * ntemperature_ + temp) * negroups_ + g] *
2,600,730✔
349
        source_regions_.density_mult(sr);
2,600,730✔
350
      sr_fission_source_old +=
2,600,730✔
351
        nu_sigma_f * source_regions_.scalar_flux_old(sr, g);
2,600,730✔
352
      sr_fission_source_new +=
2,600,730✔
353
        nu_sigma_f * source_regions_.scalar_flux_new(sr, g);
2,600,730✔
354
    }
355

356
    // Compute total fission rates in FSR
357
    sr_fission_source_old *= volume;
386,190✔
358
    sr_fission_source_new *= volume;
386,190✔
359

360
    // Accumulate totals
361
    fission_rate_old += sr_fission_source_old;
386,190✔
362
    fission_rate_new += sr_fission_source_new;
386,190✔
363

364
    // Store total fission rate in the FSR for Shannon calculation
365
    p[sr] = sr_fission_source_new;
386,190✔
366
  }
367

368
  double k_eff_new = k_eff_ * (fission_rate_new / fission_rate_old);
5,610✔
369

370
  double H = 0.0;
5,610✔
371
  // defining an inverse sum for better performance
372
  double inverse_sum = 1 / fission_rate_new;
5,610✔
373

374
#pragma omp parallel for reduction(+ : H)
3,060✔
375
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
388,740✔
376
    // Only if FSR has non-negative and non-zero fission source
377
    if (p[sr] > 0.0f) {
386,190✔
378
      // Normalize to total weight of bank sites. p_i for better performance
379
      float p_i = p[sr] * inverse_sum;
164,195✔
380
      // Sum values to obtain Shannon entropy.
381
      H -= p_i * std::log2(p_i);
164,195✔
382
    }
383
  }
384

385
  // Adds entropy value to shared entropy vector in openmc namespace.
386
  simulation::entropy.push_back(H);
5,610✔
387

388
  fission_rate_ = fission_rate_new;
5,610✔
389
  k_eff_ = k_eff_new;
5,610✔
390
}
5,610✔
391

392
// This function is responsible for generating a mapping between random
393
// ray flat source regions (cell instances) and tally bins. The mapping
394
// takes the form of a "TallyTask" object, which accounts for one single
395
// score being applied to a single tally. Thus, a single source region
396
// may have anywhere from zero to many tally tasks associated with it ---
397
// meaning that the global "tally_task" data structure is in 2D. The outer
398
// dimension corresponds to the source element (i.e., each entry corresponds
399
// to a specific energy group within a specific source region), and the
400
// inner dimension corresponds to the tallying task itself. Mechanically,
401
// the mapping between FSRs and spatial filters is done by considering
402
// the location of a single known ray midpoint that passed through the
403
// FSR. I.e., during transport, the first ray to pass through a given FSR
404
// will write down its midpoint for use with this function. This is a cheap
405
// and easy way of mapping FSRs to spatial tally filters, but comes with
406
// the downside of adding the restriction that spatial tally filters must
407
// share boundaries with the physical geometry of the simulation (so as
408
// not to subdivide any FSR). It is acceptable for a spatial tally region
409
// to contain multiple FSRs, but not the other way around.
410

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

415
// Besides generating the mapping structure, this function also keeps track
416
// of whether or not all flat source regions have been hit yet. This is
417
// required, as there is no guarantee that all flat source regions will
418
// be hit every iteration, such that in the first few iterations some FSRs
419
// may not have a known position within them yet to facilitate mapping to
420
// spatial tally filters. However, after several iterations, if all FSRs
421
// have been hit and have had a tally map generated, then this status will
422
// be passed back to the caller to alert them that this function doesn't
423
// need to be called for the remainder of the simulation.
424

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

430
void FlatSourceDomain::convert_source_regions_to_tallies(int64_t start_sr_id)
914✔
431
{
432
  openmc::simulation::time_tallies.start();
914✔
433

434
  // Tracks if we've generated a mapping yet for all source regions.
435
  bool all_source_regions_mapped = true;
914✔
436

437
// Attempt to generate mapping for all source regions
438
#pragma omp parallel for
499✔
439
  for (int64_t sr = start_sr_id; sr < n_source_regions(); sr++) {
1,057,565✔
440

441
    // If this source region has not been hit by a ray yet, then
442
    // we aren't going to be able to map it, so skip it.
443
    if (!source_regions_.position_recorded(sr)) {
1,057,150!
444
      all_source_regions_mapped = false;
445
      continue;
446
    }
447

448
    // A particle located at the recorded midpoint of a ray
449
    // crossing through this source region is used to estabilish
450
    // the spatial location of the source region
451
    Particle p;
1,057,150✔
452
    p.r() = source_regions_.position(sr);
1,057,150✔
453
    p.r_last() = source_regions_.position(sr);
1,057,150✔
454
    p.u() = {1.0, 0.0, 0.0};
1,057,150✔
455
    bool found = exhaustive_find_cell(p);
1,057,150✔
456

457
    // Loop over energy groups (so as to support energy filters)
458
    for (int g = 0; g < negroups_; g++) {
2,273,900✔
459

460
      // Set particle to the current energy
461
      p.g() = g;
1,216,750✔
462
      p.g_last() = g;
1,216,750✔
463
      p.E() = data::mg.energy_bin_avg_[p.g()];
1,216,750!
464
      p.E_last() = p.E();
1,216,750✔
465

466
      int64_t source_element = sr * negroups_ + g;
1,216,750✔
467

468
      // If this task has already been populated, we don't need to do
469
      // it again.
470
      if (source_regions_.tally_task(sr, g).size() > 0) {
1,216,750!
471
        continue;
472
      }
473

474
      // Loop over all active tallies. This logic is essentially identical
475
      // to what happens when scanning for applicable tallies during
476
      // MC transport.
477
      for (int i_tally = 0; i_tally < model::tallies.size(); i_tally++) {
4,654,740✔
478
        Tally& tally {*model::tallies[i_tally]};
3,437,990✔
479

480
        // Initialize an iterator over valid filter bin combinations.
481
        // If there are no valid combinations, use a continue statement
482
        // to ensure we skip the assume_separate break below.
483
        auto filter_iter = FilterBinIter(tally, p);
3,437,990✔
484
        auto end = FilterBinIter(tally, true, &p.filter_matches());
3,437,990✔
485
        if (filter_iter == end)
3,437,990✔
486
          continue;
2,083,840✔
487

488
        // Loop over filter bins.
489
        for (; filter_iter != end; ++filter_iter) {
4,062,450✔
490
          auto filter_index = filter_iter.index_;
491
          auto filter_weight = filter_iter.weight_;
3,065,860✔
492

493
          // Loop over scores
494
          for (int score = 0; score < tally.scores_.size(); score++) {
3,065,860✔
495
            auto score_bin = tally.scores_[score];
1,711,710✔
496
            // If a valid tally, filter, and score combination has been found,
497
            // then add it to the list of tally tasks for this source element.
498
            TallyTask task(i_tally, filter_index, score, score_bin);
1,711,710✔
499
            source_regions_.tally_task(sr, g).push_back(task);
1,711,710✔
500

501
            // Also add this task to the list of volume tasks for this source
502
            // region.
503
            source_regions_.volume_task(sr).insert(task);
1,711,710✔
504
          }
505
        }
506
      }
507
      // Reset all the filter matches for the next tally event.
508
      for (auto& match : p.filter_matches())
5,151,360✔
509
        match.bins_present_ = false;
3,934,610✔
510
    }
511
  }
1,057,150✔
512
  openmc::simulation::time_tallies.stop();
914✔
513

514
  mapped_all_tallies_ = all_source_regions_mapped;
914✔
515
}
914✔
516

517
// Set the volume accumulators to zero for all tallies
518
void FlatSourceDomain::reset_tally_volumes()
7,486✔
519
{
520
  if (volume_normalized_flux_tallies_) {
7,486✔
521
#pragma omp parallel for
3,180✔
522
    for (int i = 0; i < tally_volumes_.size(); i++) {
8,650✔
523
      auto& tensor = tally_volumes_[i];
6,000✔
524
      tensor.fill(0.0); // Set all elements of the tensor to 0.0
12,000✔
525
    }
526
  }
527
}
7,486✔
528

529
// In fixed source mode, due to the way that volumetric fixed sources are
530
// converted and applied as volumetric sources in one or more source regions,
531
// we need to perform an additional normalization step to ensure that the
532
// reported scalar fluxes are in units per source neutron. This allows for
533
// direct comparison of reported tallies to Monte Carlo flux results.
534
// This factor needs to be computed at each iteration, as it is based on the
535
// volume estimate of each FSR, which improves over the course of the
536
// simulation
537
double FlatSourceDomain::compute_fixed_source_normalization_factor() const
8,369✔
538
{
539
  // Eigenvalue mode normalization
540
  if (settings::run_mode == RunMode::EIGENVALUE) {
8,369✔
541
    // Normalize fluxes by total number of fission neutrons produced. This
542
    // ensures consistent scaling of the eigenvector such that its magnitude is
543
    // comparable to the eigenvector produced by the Monte Carlo solver.
544
    // Multiplying by the eigenvalue is unintuitive, but it is necessary.
545
    // If the eigenvalue is 1.2, per starting source neutron, you will
546
    // generate 1.2 neutrons. Thus if we normalize to generating only ONE
547
    // neutron in total for the whole domain, then we don't actually have enough
548
    // flux to generate the required 1.2 neutrons. We only know the flux
549
    // required to generate 1 neutron (which would have required less than one
550
    // starting neutron). Thus, you have to scale the flux up by the eigenvalue
551
    // such that 1.2 neutrons are generated, so as to be consistent with the
552
    // bookkeeping in MC which is all done per starting source neutron (not per
553
    // neutron produced).
554
    return k_eff_ / (fission_rate_ * simulation_volume_);
3,671✔
555
  }
556

557
  // If we are in adjoint mode of a fixed source problem, the external
558
  // source is already normalized, such that all resulting fluxes are
559
  // also normalized.
560
  if (solve_ == RandomRaySolve::ADJOINT) {
4,698✔
561
    return 1.0;
562
  }
563

564
  // Fixed source mode normalization
565

566
  // Step 1 is to sum over all source regions and energy groups to get the
567
  // total external source strength in the simulation.
568
  double simulation_external_source_strength = 0.0;
2,296✔
569
#pragma omp parallel for reduction(+ : simulation_external_source_strength)
2,296✔
570
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
14,761,638✔
571
    int material = source_regions_.material(sr);
14,759,770✔
572
    int temp = source_regions_.temperature_idx(sr);
14,759,770✔
573
    double volume = source_regions_.volume(sr) * simulation_volume_;
14,759,770✔
574
    for (int g = 0; g < negroups_; g++) {
30,062,420✔
575
      // For non-void regions, we store the external source pre-divided by
576
      // sigma_t. We need to multiply non-void regions back up by sigma_t
577
      // to get the total source strength in the expected units.
578
      double sigma_t = 1.0;
15,302,650✔
579
      if (material != MATERIAL_VOID) {
15,302,650✔
580
        sigma_t = sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
15,092,410✔
581
                  source_regions_.density_mult(sr);
15,092,410✔
582
      }
583
      simulation_external_source_strength +=
15,302,650✔
584
        source_regions_.external_source(sr, g) * sigma_t * volume;
15,302,650✔
585
    }
586
  }
587

588
  // Step 2 is to determine the total user-specified external source strength
589
  double user_external_source_strength = 0.0;
4,164✔
590
  for (auto& ext_source : model::external_sources) {
8,328✔
591
    user_external_source_strength += ext_source->strength();
4,164✔
592
  }
593

594
  // The correction factor is the ratio of the user-specified external source
595
  // strength to the simulation external source strength.
596
  double source_normalization_factor =
4,164✔
597
    user_external_source_strength / simulation_external_source_strength;
598

599
  return source_normalization_factor;
4,164✔
600
}
601

602
// Tallying in random ray is not done directly during transport, rather,
603
// it is done only once after each power iteration. This is made possible
604
// by way of a mapping data structure that relates spatial source regions
605
// (FSRs) to tally/filter/score combinations. The mechanism by which the
606
// mapping is done (and the limitations incurred) is documented in the
607
// "convert_source_regions_to_tallies()" function comments above. The present
608
// tally function simply traverses the mapping data structure and executes
609
// the scoring operations to OpenMC's native tally result arrays.
610

611
void FlatSourceDomain::random_ray_tally()
7,486✔
612
{
613
  openmc::simulation::time_tallies.start();
7,486✔
614

615
  // Reset our tally volumes to zero
616
  reset_tally_volumes();
7,486✔
617

618
  double source_normalization_factor =
7,486✔
619
    compute_fixed_source_normalization_factor();
7,486✔
620

621
// We loop over all source regions and energy groups. For each
622
// element, we check if there are any scores needed and apply
623
// them.
624
#pragma omp parallel for
4,086✔
625
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
15,556,000✔
626
    // The fsr.volume_ is the unitless fractional simulation averaged volume
627
    // (i.e., it is the FSR's fraction of the overall simulation volume). The
628
    // simulation_volume_ is the total 3D physical volume in cm^3 of the
629
    // entire global simulation domain (as defined by the ray source box).
630
    // Thus, the FSR's true 3D spatial volume in cm^3 is found by multiplying
631
    // its fraction of the total volume by the total volume. Not important in
632
    // eigenvalue solves, but useful in fixed source solves for returning the
633
    // flux shape with a magnitude that makes sense relative to the fixed
634
    // source strength.
635
    double volume = source_regions_.volume(sr) * simulation_volume_;
15,552,600✔
636

637
    double material = source_regions_.material(sr);
15,552,600✔
638
    int temp = source_regions_.temperature_idx(sr);
15,552,600✔
639
    double density_mult = source_regions_.density_mult(sr);
15,552,600✔
640
    for (int g = 0; g < negroups_; g++) {
32,785,800✔
641
      double flux =
17,233,200✔
642
        source_regions_.scalar_flux_new(sr, g) * source_normalization_factor;
17,233,200✔
643

644
      // Determine numerical score value
645
      for (auto& task : source_regions_.tally_task(sr, g)) {
41,117,800✔
646
        double score = 0.0;
23,884,600✔
647
        switch (task.score_type) {
23,884,600!
648

649
        case SCORE_FLUX:
20,036,800✔
650
          score = flux * volume;
20,036,800✔
651
          break;
20,036,800✔
652

653
        case SCORE_TOTAL:
654
          if (material != MATERIAL_VOID) {
×
655
            score =
656
              flux * volume *
657
              sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
658
              density_mult;
659
          }
660
          break;
661

662
        case SCORE_FISSION:
1,923,600✔
663
          if (material != MATERIAL_VOID) {
1,923,600!
664
            score =
1,923,600✔
665
              flux * volume *
1,923,600✔
666
              sigma_f_[(material * ntemperature_ + temp) * negroups_ + g] *
1,923,600✔
667
              density_mult;
668
          }
669
          break;
670

671
        case SCORE_NU_FISSION:
1,923,600✔
672
          if (material != MATERIAL_VOID) {
1,923,600!
673
            score =
1,923,600✔
674
              flux * volume *
1,923,600✔
675
              nu_sigma_f_[(material * ntemperature_ + temp) * negroups_ + g] *
1,923,600✔
676
              density_mult;
677
          }
678
          break;
679

680
        case SCORE_EVENTS:
681
          score = 1.0;
682
          break;
683

684
        case SCORE_KAPPA_FISSION:
600✔
685
          score =
600✔
686
            flux * volume *
600✔
687
            kappa_fission_[(material * ntemperature_ + temp) * negroups_ + g] *
600✔
688
            density_mult;
689
          break;
600✔
690

691
        default:
692
          fatal_error("Invalid score specified in tallies.xml. Only flux, "
693
                      "total, fission, nu-fission, kappa-fission, and events "
694
                      "are supported in random ray mode.");
695
          break;
23,884,600✔
696
        }
697
        // Apply score to the appropriate tally bin
698
        Tally& tally {*model::tallies[task.tally_idx]};
23,884,600✔
699
#pragma omp atomic
700
        tally.results_(task.filter_idx, task.score_idx, TallyResult::VALUE) +=
23,884,600✔
701
          score;
702
      }
703
    }
704

705
    // For flux tallies, the total volume of the spatial region is needed
706
    // for normalizing the flux. We store this volume in a separate tensor.
707
    // We only contribute to each volume tally bin once per FSR.
708
    if (volume_normalized_flux_tallies_) {
15,552,600✔
709
      for (const auto& task : source_regions_.volume_task(sr)) {
36,769,200✔
710
        if (task.score_type == SCORE_FLUX) {
21,364,800✔
711
#pragma omp atomic
712
          tally_volumes_[task.tally_idx](task.filter_idx, task.score_idx) +=
19,167,600✔
713
            volume;
714
        }
715
      }
716
    }
717
  } // end FSR loop
718

719
  // Normalize any flux scores by the total volume of the FSRs scoring to that
720
  // bin. To do this, we loop over all tallies, and then all filter bins,
721
  // and then scores. For each score, we check the tally data structure to
722
  // see what index that score corresponds to. If that score is a flux score,
723
  // then we divide it by volume.
724
  if (volume_normalized_flux_tallies_) {
7,486✔
725
    for (int i = 0; i < model::tallies.size(); i++) {
19,030✔
726
      Tally& tally {*model::tallies[i]};
13,200✔
727
#pragma omp parallel for
7,200✔
728
      for (int bin = 0; bin < tally.n_filter_bins(); bin++) {
724,775✔
729
        for (int score_idx = 0; score_idx < tally.n_scores(); score_idx++) {
1,460,050✔
730
          auto score_type = tally.scores_[score_idx];
741,275✔
731
          if (score_type == SCORE_FLUX) {
741,275✔
732
            double vol = tally_volumes_[i](bin, score_idx);
718,775!
733
            if (vol > 0.0) {
718,775!
734
              tally.results_(bin, score_idx, TallyResult::VALUE) /= vol;
718,775✔
735
            }
736
          }
737
        }
738
      }
739
    }
740
  }
741

742
  openmc::simulation::time_tallies.stop();
7,486✔
743
}
7,486✔
744

745
double FlatSourceDomain::evaluate_flux_at_point(
×
746
  Position r, int64_t sr, int g) const
747
{
748
  return source_regions_.scalar_flux_final(sr, g) /
×
749
         (settings::n_batches - settings::n_inactive);
×
750
}
751

752
// Outputs all basic material, FSR ID, multigroup flux, and
753
// fission source data to .vtk file that can be directly
754
// loaded and displayed by Paraview. Note that .vtk binary
755
// files require big endian byte ordering, so endianness
756
// is checked and flipped if necessary.
757
void FlatSourceDomain::output_to_vtk() const
×
758
{
759
  // Rename .h5 plot filename(s) to .vtk filenames
760
  for (int p = 0; p < model::plots.size(); p++) {
×
761
    PlottableInterface* plot = model::plots[p].get();
×
762
    plot->path_plot() =
×
763
      plot->path_plot().substr(0, plot->path_plot().find_last_of('.')) + ".vtk";
×
764
  }
765

766
  // Print header information
767
  print_plot();
×
768

769
  // Outer loop over plots
770
  for (int plt = 0; plt < model::plots.size(); plt++) {
×
771

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

775
    // Random ray plots only support voxel plots
776
    if (!openmc_plot) {
×
777
      warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
×
778
                          "is allowed in random ray mode.",
779
        plt));
780
      continue;
×
781
    } else if (openmc_plot->type_ != Plot::PlotType::voxel) {
×
782
      warning(fmt::format("Plot {} is invalid plot type -- only voxel plotting "
×
783
                          "is allowed in random ray mode.",
784
        plt));
785
      continue;
×
786
    }
787

788
    int Nx = openmc_plot->pixels_[0];
×
789
    int Ny = openmc_plot->pixels_[1];
×
790
    int Nz = openmc_plot->pixels_[2];
×
791
    Position origin = openmc_plot->origin_;
×
792
    Position width = openmc_plot->width_;
×
793
    Position ll = origin - width / 2.0;
×
794
    double x_delta = width.x / Nx;
×
795
    double y_delta = width.y / Ny;
×
796
    double z_delta = width.z / Nz;
×
797
    std::string filename = openmc_plot->path_plot();
×
798

799
    // Tag plots written during the forward solve of an adjoint run
NEW
800
    if (solve_ == RandomRaySolve::FORWARD_FOR_ADJOINT) {
×
NEW
801
      auto dot = filename.find_last_of('.');
×
NEW
802
      filename = filename.substr(0, dot) + ".forward" + filename.substr(dot);
×
803
    }
804

805
    // Perform sanity checks on file size
806
    uint64_t bytes = Nx * Ny * Nz * (negroups_ + 1 + 1 + 1) * sizeof(float);
×
807
    write_message(5, "Processing plot {}: {}... (Estimated size is {} MB)",
×
808
      openmc_plot->id(), filename, bytes / 1.0e6);
×
809
    if (bytes / 1.0e9 > 1.0) {
×
810
      warning("Voxel plot specification is very large (>1 GB). Plotting may be "
×
811
              "slow.");
812
    } else if (bytes / 1.0e9 > 100.0) {
×
813
      fatal_error("Voxel plot specification is too large (>100 GB). Exiting.");
×
814
    }
815

816
    // Relate voxel spatial locations to random ray source regions
817
    vector<int> voxel_indices(Nx * Ny * Nz);
×
818
    vector<Position> voxel_positions(Nx * Ny * Nz);
×
819
    vector<double> weight_windows(Nx * Ny * Nz);
×
820
    float min_weight = 1e20;
×
821
#pragma omp parallel for collapse(3) reduction(min : min_weight)
822
    for (int z = 0; z < Nz; z++) {
×
823
      for (int y = 0; y < Ny; y++) {
×
824
        for (int x = 0; x < Nx; x++) {
×
825
          Position sample;
826
          sample.z = ll.z + z_delta / 2.0 + z * z_delta;
827
          sample.y = ll.y + y_delta / 2.0 + y * y_delta;
828
          sample.x = ll.x + x_delta / 2.0 + x * x_delta;
829
          Particle p;
×
830
          p.r() = sample;
×
831
          p.r_last() = sample;
832
          p.E() = 1.0;
833
          p.E_last() = 1.0;
834
          p.u() = {1.0, 0.0, 0.0};
×
835

836
          bool found = exhaustive_find_cell(p);
×
837
          if (!found) {
×
838
            voxel_indices[z * Ny * Nx + y * Nx + x] = -1;
839
            voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
840
            weight_windows[z * Ny * Nx + y * Nx + x] = 0.0;
841
            continue;
842
          }
843

844
          SourceRegionKey sr_key = lookup_source_region_key(p);
×
845
          int64_t sr = -1;
846
          auto it = source_region_map_.find(sr_key);
×
847
          if (it != source_region_map_.end()) {
×
848
            sr = it->second;
849
          }
850

851
          voxel_indices[z * Ny * Nx + y * Nx + x] = sr;
×
852
          voxel_positions[z * Ny * Nx + y * Nx + x] = sample;
853

854
          if (variance_reduction::weight_windows.size() == 1) {
×
855
            auto [ww_found, ww] =
856
              variance_reduction::weight_windows[0]->get_weight_window(p);
×
857
            float weight = ww.lower_weight;
858
            weight_windows[z * Ny * Nx + y * Nx + x] = weight;
×
859
            if (weight < min_weight)
×
860
              min_weight = weight;
861
          }
862
        }
863
      }
864
    }
865

866
    double source_normalization_factor =
×
867
      compute_fixed_source_normalization_factor();
×
868

869
    // Open file for writing
870
    std::FILE* plot = std::fopen(filename.c_str(), "wb");
×
871

872
    // Write vtk metadata
873
    std::fprintf(plot, "# vtk DataFile Version 2.0\n");
×
874
    std::fprintf(plot, "Dataset File\n");
×
875
    std::fprintf(plot, "BINARY\n");
×
876
    std::fprintf(plot, "DATASET STRUCTURED_POINTS\n");
×
877
    std::fprintf(plot, "DIMENSIONS %d %d %d\n", Nx, Ny, Nz);
×
878
    std::fprintf(plot, "ORIGIN %lf %lf %lf\n", ll.x, ll.y, ll.z);
×
879
    std::fprintf(plot, "SPACING %lf %lf %lf\n", x_delta, y_delta, z_delta);
×
880
    std::fprintf(plot, "POINT_DATA %d\n", Nx * Ny * Nz);
×
881

882
    int64_t num_neg = 0;
883
    int64_t num_samples = 0;
884
    float min_flux = 0.0;
885
    float max_flux = -1.0e20;
886
    // Plot multigroup flux data
887
    for (int g = 0; g < negroups_; g++) {
×
888
      std::fprintf(plot, "SCALARS flux_group_%d float\n", g);
×
889
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
890
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
891
        int64_t fsr = voxel_indices[i];
×
892
        int64_t source_element = fsr * negroups_ + g;
×
893
        float flux = 0;
×
894
        if (fsr >= 0) {
×
895
          flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
×
896
          if (flux < 0.0)
×
897
            flux = FlatSourceDomain::evaluate_flux_at_point(
×
898
              voxel_positions[i], fsr, g);
×
899
        }
900
        if (flux < 0.0) {
×
901
          num_neg++;
×
902
          if (flux < min_flux) {
×
903
            min_flux = flux;
×
904
          }
905
        }
906
        if (flux > max_flux)
×
907
          max_flux = flux;
×
908
        num_samples++;
×
909
        flux = convert_to_big_endian<float>(flux);
×
910
        std::fwrite(&flux, sizeof(float), 1, plot);
×
911
      }
912
    }
913

914
    // Slightly negative fluxes can be normal when sampling corners of linear
915
    // source regions. However, very common and high magnitude negative fluxes
916
    // may indicate numerical instability.
917
    if (num_neg > 0) {
×
918
      warning(fmt::format("{} plot samples ({:.4f}%) contained negative fluxes "
×
919
                          "(minumum found = {:.2e} maximum_found = {:.2e})",
920
        num_neg, (100.0 * num_neg) / num_samples, min_flux, max_flux));
×
921
    }
922

923
    // Plot FSRs
924
    std::fprintf(plot, "SCALARS FSRs float\n");
×
925
    std::fprintf(plot, "LOOKUP_TABLE default\n");
×
926
    for (int fsr : voxel_indices) {
×
927
      float value = future_prn(10, fsr);
×
928
      value = convert_to_big_endian<float>(value);
×
929
      std::fwrite(&value, sizeof(float), 1, plot);
×
930
    }
931

932
    // Plot Materials
933
    std::fprintf(plot, "SCALARS Materials int\n");
×
934
    std::fprintf(plot, "LOOKUP_TABLE default\n");
×
935
    for (int fsr : voxel_indices) {
×
936
      int mat = -1;
×
937
      if (fsr >= 0)
×
938
        mat = source_regions_.material(fsr);
×
939
      mat = convert_to_big_endian<int>(mat);
×
940
      std::fwrite(&mat, sizeof(int), 1, plot);
×
941
    }
942

943
    // Plot fission source
944
    if (settings::run_mode == RunMode::EIGENVALUE) {
×
945
      std::fprintf(plot, "SCALARS total_fission_source float\n");
×
946
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
947
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
948
        int64_t fsr = voxel_indices[i];
×
949
        float total_fission = 0.0;
×
950
        if (fsr >= 0) {
×
951
          int mat = source_regions_.material(fsr);
×
952
          int temp = source_regions_.temperature_idx(fsr);
×
953
          if (mat != MATERIAL_VOID) {
×
954
            for (int g = 0; g < negroups_; g++) {
×
955
              int64_t source_element = fsr * negroups_ + g;
×
956
              float flux = evaluate_flux_at_point(voxel_positions[i], fsr, g);
×
957
              double sigma_f =
×
958
                sigma_f_[(mat * ntemperature_ + temp) * negroups_ + g] *
×
959
                source_regions_.density_mult(fsr);
×
960
              total_fission += sigma_f * flux;
×
961
            }
962
          }
963
        }
964
        total_fission = convert_to_big_endian<float>(total_fission);
×
965
        std::fwrite(&total_fission, sizeof(float), 1, plot);
×
966
      }
967
    } else {
968
      std::fprintf(plot, "SCALARS external_source float\n");
×
969
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
970
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
971
        int64_t fsr = voxel_indices[i];
×
972
        int mat = source_regions_.material(fsr);
×
973
        int temp = source_regions_.temperature_idx(fsr);
×
974
        float total_external = 0.0f;
×
975
        if (fsr >= 0) {
×
976
          for (int g = 0; g < negroups_; g++) {
×
977
            // External sources are already divided by sigma_t, so we need to
978
            // multiply it back to get the true external source.
979
            double sigma_t = 1.0;
×
980
            if (mat != MATERIAL_VOID) {
×
981
              sigma_t = sigma_t_[(mat * ntemperature_ + temp) * negroups_ + g] *
×
982
                        source_regions_.density_mult(fsr);
×
983
            }
984
            total_external += source_regions_.external_source(fsr, g) * sigma_t;
×
985
          }
986
        }
987
        total_external = convert_to_big_endian<float>(total_external);
×
988
        std::fwrite(&total_external, sizeof(float), 1, plot);
×
989
      }
990
    }
991

992
    // Plot weight window data
993
    if (variance_reduction::weight_windows.size() == 1) {
×
994
      std::fprintf(plot, "SCALARS weight_window_lower float\n");
×
995
      std::fprintf(plot, "LOOKUP_TABLE default\n");
×
996
      for (int i = 0; i < Nx * Ny * Nz; i++) {
×
997
        float weight = weight_windows[i];
×
998
        if (weight == 0.0)
×
999
          weight = min_weight;
×
1000
        weight = convert_to_big_endian<float>(weight);
×
1001
        std::fwrite(&weight, sizeof(float), 1, plot);
×
1002
      }
1003
    }
1004

1005
    std::fclose(plot);
×
1006
  }
×
1007
}
×
1008

1009
void FlatSourceDomain::apply_external_source_to_source_region(
4,962✔
1010
  int src_idx, SourceRegionHandle& srh)
1011
{
1012
  auto s =
4,962✔
1013
    (solve_ == RandomRaySolve::ADJOINT && !model::adjoint_sources.empty())
176!
1014
      ? model::adjoint_sources[src_idx].get()
5,138✔
1015
      : model::external_sources[src_idx].get();
4,786✔
1016
  auto is = dynamic_cast<IndependentSource*>(s);
4,962!
1017
  auto discrete = dynamic_cast<Discrete*>(is->energy());
4,962!
1018
  double strength_factor = is->strength();
4,962✔
1019
  const auto& discrete_energies = discrete->x();
4,962✔
1020
  const auto& discrete_probs = discrete->prob();
4,962✔
1021

1022
  srh.external_source_present() = 1;
4,962✔
1023

1024
  for (int i = 0; i < discrete_energies.size(); i++) {
10,056✔
1025
    int g = data::mg.get_group_index(discrete_energies[i]);
5,094✔
1026
    srh.external_source(g) += discrete_probs[i] * strength_factor;
5,094✔
1027
  }
1028
}
4,962✔
1029

1030
void FlatSourceDomain::apply_external_source_to_cell_instances(int32_t i_cell,
465✔
1031
  int src_idx, int target_material_id, const vector<int32_t>& instances)
1032
{
1033
  Cell& cell = *model::cells[i_cell];
465!
1034

1035
  if (cell.type_ != Fill::MATERIAL)
465!
1036
    return;
1037

1038
  for (int j : instances) {
29,220✔
1039
    int cell_material_idx = cell.material(j);
28,755!
1040
    int cell_material_id;
28,755✔
1041
    if (cell_material_idx == MATERIAL_VOID) {
28,755✔
1042
      cell_material_id = MATERIAL_VOID;
1043
    } else {
1044
      cell_material_id = model::materials[cell_material_idx]->id();
28,515✔
1045
    }
1046
    if (target_material_id == C_NONE ||
28,755✔
1047
        cell_material_id == target_material_id) {
28,755✔
1048
      int64_t source_region = source_region_offsets_[i_cell] + j;
2,955✔
1049
      external_volumetric_source_map_[source_region].push_back(src_idx);
2,955✔
1050
    }
1051
  }
1052
}
1053

1054
void FlatSourceDomain::apply_external_source_to_cell_and_children(
495✔
1055
  int32_t i_cell, int src_idx, int32_t target_material_id)
1056
{
1057
  Cell& cell = *model::cells[i_cell];
495✔
1058

1059
  if (cell.type_ == Fill::MATERIAL) {
495✔
1060
    vector<int> instances(cell.n_instances());
465✔
1061
    std::iota(instances.begin(), instances.end(), 0);
465✔
1062
    apply_external_source_to_cell_instances(
465✔
1063
      i_cell, src_idx, target_material_id, instances);
1064
  } else if (target_material_id == C_NONE) {
495!
1065
    std::unordered_map<int32_t, vector<int32_t>> cell_instance_list =
×
1066
      cell.get_contained_cells(0, nullptr);
×
1067
    for (const auto& pair : cell_instance_list) {
×
1068
      int32_t i_child_cell = pair.first;
×
1069
      apply_external_source_to_cell_instances(
×
1070
        i_child_cell, src_idx, target_material_id, pair.second);
×
1071
    }
1072
  }
×
1073
}
495✔
1074

1075
void FlatSourceDomain::count_external_source_regions()
1,319✔
1076
{
1077
  n_external_source_regions_ = 0;
1,319✔
1078
#pragma omp parallel for reduction(+ : n_external_source_regions_)
792✔
1079
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
1,228,137✔
1080
    if (source_regions_.external_source_present(sr)) {
1,227,610✔
1081
      n_external_source_regions_++;
157,500✔
1082
    }
1083
  }
1084
}
1,319✔
1085

1086
void FlatSourceDomain::convert_external_sources(bool use_adjoint_sources)
436✔
1087
{
1088
  // Determine whether forward or (local) adjoint sources are desired
1089
  const auto& sources =
436✔
1090
    use_adjoint_sources ? model::adjoint_sources : model::external_sources;
1091

1092
  // Loop over external sources
1093
  for (int es = 0; es < sources.size(); es++) {
872✔
1094

1095
    // Extract source information
1096
    Source* s = sources[es].get();
436!
1097
    IndependentSource* is = dynamic_cast<IndependentSource*>(s);
436!
1098
    Discrete* energy = dynamic_cast<Discrete*>(is->energy());
436✔
1099
    const std::unordered_set<int32_t>& domain_ids = is->domain_ids();
436✔
1100
    double strength_factor = is->strength();
436✔
1101

1102
    // If there is no domain constraint specified, then this must be a point
1103
    // source. In this case, we need to find the source region that contains the
1104
    // point source and apply or relate it to the external source.
1105
    if (is->domain_ids().size() == 0) {
436✔
1106

1107
      // Extract the point source coordinate and find the base source region at
1108
      // that point
1109
      auto sp = dynamic_cast<SpatialPoint*>(is->space());
16!
1110
      GeometryState gs;
16✔
1111
      gs.r() = sp->r();
16✔
1112
      gs.r_last() = sp->r();
16✔
1113
      gs.u() = {1.0, 0.0, 0.0};
16✔
1114
      bool found = exhaustive_find_cell(gs);
16✔
1115
      if (!found) {
16!
1116
        fatal_error(fmt::format("Could not find cell containing external "
×
1117
                                "point source at {}",
1118
          sp->r()));
×
1119
      }
1120
      SourceRegionKey key = lookup_source_region_key(gs);
16✔
1121

1122
      // With the source region and mesh bin known, we can use the
1123
      // accompanying SourceRegionKey as a key into a map that stores the
1124
      // corresponding external source index for the point source. Notably, we
1125
      // do not actually apply the external source to any source regions here,
1126
      // as if mesh subdivision is enabled, they haven't actually been
1127
      // discovered & initilized yet. When discovered, they will read from the
1128
      // external_source_map to determine if there are any external source
1129
      // terms that should be applied.
1130
      external_point_source_map_[key].push_back(es);
16✔
1131

1132
    } else {
16✔
1133
      // If not a point source, then use the volumetric domain constraints to
1134
      // determine which source regions to apply the external source to.
1135
      if (is->domain_type() == Source::DomainType::MATERIAL) {
420✔
1136
        for (int32_t material_id : domain_ids) {
60✔
1137
          for (int i_cell = 0; i_cell < model::cells.size(); i_cell++) {
120✔
1138
            apply_external_source_to_cell_and_children(i_cell, es, material_id);
90✔
1139
          }
1140
        }
1141
      } else if (is->domain_type() == Source::DomainType::CELL) {
390✔
1142
        for (int32_t cell_id : domain_ids) {
75✔
1143
          int32_t i_cell = model::cell_map[cell_id];
45✔
1144
          apply_external_source_to_cell_and_children(i_cell, es, C_NONE);
45✔
1145
        }
1146
      } else if (is->domain_type() == Source::DomainType::UNIVERSE) {
360!
1147
        for (int32_t universe_id : domain_ids) {
720✔
1148
          int32_t i_universe = model::universe_map[universe_id];
360✔
1149
          Universe& universe = *model::universes[i_universe];
360✔
1150
          for (int32_t i_cell : universe.cells_) {
720✔
1151
            apply_external_source_to_cell_and_children(i_cell, es, C_NONE);
360✔
1152
          }
1153
        }
1154
      }
1155
    }
1156
  } // End loop over external sources
1157
}
436✔
1158

1159
void FlatSourceDomain::flux_swap()
15,962✔
1160
{
1161
  source_regions_.flux_swap();
15,962✔
1162
}
15,962✔
1163

1164
void FlatSourceDomain::flatten_xs()
792✔
1165
{
1166
  // Temperature and angle indices, if using multiple temperature
1167
  // data sets and/or anisotropic data sets.
1168
  // TODO: Currently assumes we are only using single angle data.
1169
  const int a = 0;
792✔
1170

1171
  n_materials_ = data::mg.macro_xs_.size();
792✔
1172
  ntemperature_ = 1;
792✔
1173
  for (int i = 0; i < n_materials_; i++) {
2,945✔
1174
    ntemperature_ =
4,306✔
1175
      std::max(ntemperature_, data::mg.macro_xs_[i].n_temperature_points());
2,168✔
1176
  }
1177

1178
  for (int i = 0; i < n_materials_; i++) {
2,945✔
1179
    auto& m = data::mg.macro_xs_[i];
2,153✔
1180
    for (int t = 0; t < ntemperature_; t++) {
4,336✔
1181
      for (int g_out = 0; g_out < negroups_; g_out++) {
11,249✔
1182
        if (m.exists_in_model && t < m.n_temperature_points()) {
9,066✔
1183
          double sigma_t =
8,901✔
1184
            m.get_xs(MgxsType::TOTAL, g_out, NULL, NULL, NULL, t, a);
8,901✔
1185
          sigma_t_.push_back(sigma_t);
8,901✔
1186

1187
          if (sigma_t < MINIMUM_MACRO_XS) {
8,901✔
1188
            Material* mat = model::materials[i].get();
15✔
1189
            warning(fmt::format(
21✔
1190
              "Material \"{}\" (id: {}) has a group {} total cross section "
1191
              "({:.3e}) below the minimum threshold "
1192
              "({:.3e}). Material will be treated as pure void.",
1193
              mat->name(), mat->id(), g_out, sigma_t, MINIMUM_MACRO_XS));
27✔
1194
          }
1195

1196
          double nu_sigma_f =
8,901✔
1197
            m.get_xs(MgxsType::NU_FISSION, g_out, NULL, NULL, NULL, t, a);
8,901✔
1198
          nu_sigma_f_.push_back(nu_sigma_f);
8,901✔
1199

1200
          double sigma_f =
8,901✔
1201
            m.get_xs(MgxsType::FISSION, g_out, NULL, NULL, NULL, t, a);
8,901✔
1202
          sigma_f_.push_back(sigma_f);
8,901✔
1203

1204
          double chi =
8,901✔
1205
            m.get_xs(MgxsType::CHI_PROMPT, g_out, &g_out, NULL, NULL, t, a);
8,901✔
1206
          if (!std::isfinite(chi)) {
8,901✔
1207
            // MGXS interface may return NaN in some cases, such as when
1208
            // material is fissionable but has very small sigma_f.
1209
            chi = 0.0;
660✔
1210
          }
1211
          chi_.push_back(chi);
8,901✔
1212

1213
          double kappa_fission =
8,901✔
1214
            m.get_xs(MgxsType::KAPPA_FISSION, g_out, NULL, NULL, NULL, t, a);
8,901✔
1215
          kappa_fission_.push_back(kappa_fission);
8,901✔
1216

1217
          for (int g_in = 0; g_in < negroups_; g_in++) {
260,198✔
1218
            double sigma_s =
251,297✔
1219
              m.get_xs(MgxsType::NU_SCATTER, g_in, &g_out, NULL, NULL, t, a);
251,297✔
1220
            sigma_s_.push_back(sigma_s);
251,297✔
1221
            // For transport corrected XS data, diagonal elements may be
1222
            // negative. In this case, set a flag to enable transport
1223
            // stabilization for the simulation.
1224
            if (g_out == g_in && sigma_s < 0.0)
251,297✔
1225
              is_transport_stabilization_needed_ = true;
825✔
1226
          }
1227
        } else {
1228
          sigma_t_.push_back(0);
165✔
1229
          nu_sigma_f_.push_back(0);
165✔
1230
          sigma_f_.push_back(0);
165✔
1231
          chi_.push_back(0);
165✔
1232
          kappa_fission_.push_back(0);
165✔
1233
          for (int g_in = 0; g_in < negroups_; g_in++) {
960✔
1234
            sigma_s_.push_back(0);
795✔
1235
          }
1236
        }
1237
      }
1238
    }
1239
  }
1240
}
792✔
1241

1242
void FlatSourceDomain::set_fw_adjoint_sources()
76✔
1243
{
1244
  // Set the adjoint external source to 1/forward_flux. If the forward flux is
1245
  // negative, zero, or extremely close to zero, set the adjoint source to zero,
1246
  // as this is likely a very small source region that we don't need to bother
1247
  // trying to vector particles towards. In the case of flux "being extremely
1248
  // close to zero", we define this as being a fixed fraction of the maximum
1249
  // forward flux, below which we assume the flux would be physically
1250
  // undetectable.
1251

1252
  // First, find the maximum forward flux value
1253
  double max_flux = 0.0;
76✔
1254
#pragma omp parallel for reduction(max : max_flux)
46✔
1255
  for (int64_t se = 0; se < n_source_elements(); se++) {
169,270✔
1256
    double flux = source_regions_.scalar_flux_final(se);
169,240✔
1257
    if (flux > max_flux) {
169,240✔
1258
      max_flux = flux;
30✔
1259
    }
1260
  }
1261

1262
  // Then, compute the adjoint source for each source region
1263
#pragma omp parallel for
46✔
1264
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
169,270✔
1265
    for (int g = 0; g < negroups_; g++) {
338,480✔
1266
      double flux = source_regions_.scalar_flux_final(sr, g);
169,240✔
1267
      if (flux <= ZERO_FLUX_CUTOFF * max_flux) {
169,240✔
1268
        source_regions_.external_source(sr, g) = 0.0;
325✔
1269
      } else {
1270
        source_regions_.external_source(sr, g) = 1.0 / flux;
168,915!
1271
        if (!std::isfinite(source_regions_.external_source(sr, g))) {
168,915!
1272
          // If the flux is NaN or Inf, set the adjoint source to zero
1273
          source_regions_.external_source(sr, g) = 0.0;
1274
        }
1275
      }
1276
      if (flux > 0.0) {
169,240✔
1277
        source_regions_.external_source_present(sr) = 1;
168,915✔
1278
      }
1279
      source_regions_.scalar_flux_final(sr, g) = 0.0;
169,240✔
1280
    }
1281
  }
1282

1283
  // "Small" source regions in OpenMC are defined as those that are hit by
1284
  // MIN_HITS_PER_BATCH rays or fewer each batch. These regions typically have
1285
  // very small volumes combined with a low aspect ratio, and are often
1286
  // generated when applying a source region mesh that clips the edge of a
1287
  // curved surface. As perhaps only a few rays will visit these regions over
1288
  // the entire forward simulation, the forward flux estimates are extremely
1289
  // noisy and unreliable. In some cases, the noise may make the forward fluxes
1290
  // extremely low, leading to unphysically large adjoint source terms,
1291
  // resulting in weight windows that aggressively try to drive particles
1292
  // towards these regions. To fix this, we simply filter out any "small" source
1293
  // regions from consideration. If a source region is "small", we
1294
  // set its adjoint source to zero. This adds negligible bias to the adjoint
1295
  // flux solution, as the true total adjoint source contribution from small
1296
  // regions is likely to be negligible.
1297
#pragma omp parallel for
46✔
1298
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
169,270✔
1299
    if (source_regions_.is_small(sr)) {
169,240!
1300
      for (int g = 0; g < negroups_; g++) {
×
1301
        source_regions_.external_source(sr, g) = 0.0;
1302
      }
1303
      source_regions_.external_source_present(sr) = 0;
1304
    }
1305
  }
1306

1307
  // Divide the fixed source term by sigma t (to save time when applying each
1308
  // iteration)
1309
#pragma omp parallel for
46✔
1310
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
169,270✔
1311
    int material = source_regions_.material(sr);
169,240!
1312
    int temp = source_regions_.temperature_idx(sr);
169,240!
1313
    if (material == MATERIAL_VOID) {
169,240!
1314
      continue;
1315
    }
1316
    for (int g = 0; g < negroups_; g++) {
338,480✔
1317
      double sigma_t =
169,240✔
1318
        sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
169,240!
1319
        source_regions_.density_mult(sr);
169,240!
1320
      source_regions_.external_source(sr, g) /= sigma_t;
169,240!
1321
      if (!std::isfinite(source_regions_.external_source(sr, g))) {
169,240!
1322
        // If the flux is NaN or Inf, set the adjoint source to zero
1323
        source_regions_.external_source(sr, g) = 0.0;
1324
      }
1325
    }
1326
  }
1327

1328
  if (fw_cadis_local_) {
76✔
1329
// Only external sources that have a non-mesh type tally task should remain
1330
// non-zero. Everything else gets zero'd out.
1331
#pragma omp parallel for
9✔
1332
    for (int64_t sr = 0; sr < n_source_regions(); sr++) {
13,726✔
1333

1334
      // If there is already no external source, don't need to do anything
1335
      if (source_regions_.external_source_present(sr) == 0) {
13,720!
1336
        continue;
1337
      }
1338

1339
      // If there is an adjoint source term here, then we need to check it.
1340

1341
      // We will track if ANY group has a valid local FW-CADIS source term
1342
      bool has_any_sources = false;
1343

1344
      // Now, loop over groups
1345
      for (int g = 0; g < negroups_; g++) {
27,440✔
1346

1347
        // If there are no tally tasks associated with this source element
1348
        // then it is not a local FW-CADIS source, so we continue to the next
1349
        // group
1350
        if (source_regions_.tally_task(sr, g).empty()) {
13,720!
1351
          source_regions_.external_source(sr, g) = 0.0;
1352
          continue;
1353
        }
1354

1355
        // If there are tally tasks, we can through them and check if
1356
        // any of them are local FW-CADIS targets.
1357

1358
        // We track if ANY of the tasks are local FW-CADIS target tallies
1359
        bool local_fw_cadis_target_region = false;
27,360✔
1360

1361
        // Now we loop through
1362
        for (const auto& task : source_regions_.tally_task(sr, g)) {
27,360✔
1363
          Tally& tally {*model::tallies[task.tally_idx]};
13,720✔
1364
          const auto t_id = tally.id();
13,720✔
1365

1366
          // Search for target tallies
1367
          if (std::find(fw_cadis_local_targets_.begin(),
13,720✔
1368
                fw_cadis_local_targets_.end(),
1369
                t_id) != fw_cadis_local_targets_.end()) {
13,720✔
1370
            local_fw_cadis_target_region = true;
80✔
1371
            break;
80✔
1372
          }
1373
        }
1374

1375
        // If ANY of the tasks is a local FW-CADIS target,
1376
        // Then we keep the source term and set that this
1377
        // source region has a valid FW-CADIS source term.
1378
        // Otherwise, we zero out the source term.
1379
        if (local_fw_cadis_target_region) {
80✔
1380
          has_any_sources = true;
1381
        } else {
1382
          source_regions_.external_source(sr, g) = 0.0;
13,640✔
1383
        }
1384
      } // End loop over groups
1385

1386
      // If there were any valid FW-CADIS source terms for any
1387
      // of the groups, then the SR as a whole counts as a source
1388
      if (has_any_sources) {
13,720✔
1389
        source_regions_.external_source_present(sr) = 1;
80✔
1390
      } else {
1391
        source_regions_.external_source_present(sr) = 0;
13,640✔
1392
      }
1393
    } // End loop over source regions
1394
  } // End local FW-CADIS logic
1395
}
76✔
1396

1397
void FlatSourceDomain::set_local_adjoint_sources()
15✔
1398
{
1399
  // Set the external source to user-specified adjoint sources.
1400
  convert_external_sources(true);
15✔
1401
}
15✔
1402

1403
void FlatSourceDomain::transpose_scattering_matrix()
106✔
1404
{
1405
  // Transpose the inner two dimensions for each material
1406
#pragma omp parallel for
64✔
1407
  for (int m = 0; m < n_materials_; ++m) {
162✔
1408
    for (int t = 0; t < ntemperature_; t++) {
240✔
1409
      int material_offset = (m * ntemperature_ + t) * negroups_ * negroups_;
120✔
1410
      for (int i = 0; i < negroups_; ++i) {
312✔
1411
        for (int j = i + 1; j < negroups_; ++j) {
444✔
1412
          // Calculate indices of the elements to swap
1413
          int idx1 = material_offset + i * negroups_ + j;
252✔
1414
          int idx2 = material_offset + j * negroups_ + i;
252✔
1415

1416
          // Swap the elements to transpose the matrix
1417
          std::swap(sigma_s_[idx1], sigma_s_[idx2]);
252✔
1418
        }
1419
      }
1420
    }
1421
  }
1422
}
106✔
1423

1424
void FlatSourceDomain::serialize_final_fluxes(vector<double>& flux)
×
1425
{
1426
  // Ensure array is correct size
1427
  flux.resize(n_source_regions() * negroups_);
×
1428
// Serialize the final fluxes for output
1429
#pragma omp parallel for
1430
  for (int64_t se = 0; se < n_source_elements(); se++) {
×
1431
    flux[se] = source_regions_.scalar_flux_final(se);
1432
  }
1433
}
×
1434

1435
void FlatSourceDomain::apply_mesh_to_cell_instances(int32_t i_cell,
1,217✔
1436
  int32_t mesh_idx, int target_material_id, const vector<int32_t>& instances,
1437
  bool is_target_void)
1438
{
1439
  Cell& cell = *model::cells[i_cell];
1,217!
1440
  if (cell.type_ != Fill::MATERIAL)
1,217!
1441
    return;
1442
  for (int32_t j : instances) {
217,054✔
1443
    int cell_material_idx = cell.material(j);
215,837!
1444
    int cell_material_id = (cell_material_idx == C_NONE)
215,837✔
1445
                             ? C_NONE
215,837✔
1446
                             : model::materials[cell_material_idx]->id();
215,836✔
1447

1448
    if ((target_material_id == C_NONE && !is_target_void) ||
215,837✔
1449
        cell_material_id == target_material_id) {
1450
      int64_t sr = source_region_offsets_[i_cell] + j;
185,837!
1451
      // Check if the key is already present in the mesh_map_
1452
      if (mesh_map_.find(sr) != mesh_map_.end()) {
185,837!
1453
        fatal_error(fmt::format("Source region {} already has mesh idx {} "
×
1454
                                "applied, but trying to apply mesh idx {}",
1455
          sr, mesh_map_[sr], mesh_idx));
×
1456
      }
1457
      // If the SR has not already been assigned, then we can write to it
1458
      mesh_map_[sr] = mesh_idx;
185,837✔
1459
    }
1460
  }
1461
}
1462

1463
void FlatSourceDomain::apply_mesh_to_cell_and_children(int32_t i_cell,
1,036✔
1464
  int32_t mesh_idx, int32_t target_material_id, bool is_target_void)
1465
{
1466
  Cell& cell = *model::cells[i_cell];
1,036✔
1467

1468
  if (cell.type_ == Fill::MATERIAL) {
1,036✔
1469
    vector<int> instances(cell.n_instances());
885✔
1470
    std::iota(instances.begin(), instances.end(), 0);
885✔
1471
    apply_mesh_to_cell_instances(
885✔
1472
      i_cell, mesh_idx, target_material_id, instances, is_target_void);
1473
  } else if (target_material_id == C_NONE && !is_target_void) {
1,036✔
1474
    for (int j = 0; j < cell.n_instances(); j++) {
182✔
1475
      std::unordered_map<int32_t, vector<int32_t>> cell_instance_list =
91✔
1476
        cell.get_contained_cells(j, nullptr);
91✔
1477
      for (const auto& pair : cell_instance_list) {
423✔
1478
        int32_t i_child_cell = pair.first;
332✔
1479
        apply_mesh_to_cell_instances(i_child_cell, mesh_idx, target_material_id,
332✔
1480
          pair.second, is_target_void);
332✔
1481
      }
1482
    }
91✔
1483
  }
1484
}
1,036✔
1485

1486
void FlatSourceDomain::apply_meshes()
792✔
1487
{
1488
  // Skip if there are no mappings between mesh IDs and domains
1489
  if (mesh_domain_map_.empty())
792✔
1490
    return;
1491

1492
  // Loop over meshes
1493
  for (int mesh_idx = 0; mesh_idx < model::meshes.size(); mesh_idx++) {
782✔
1494
    Mesh* mesh = model::meshes[mesh_idx].get();
436✔
1495
    int mesh_id = mesh->id();
436✔
1496

1497
    // Skip if mesh id is not present in the map
1498
    if (mesh_domain_map_.find(mesh_id) == mesh_domain_map_.end())
436✔
1499
      continue;
30✔
1500

1501
    // Loop over domains associated with the mesh
1502
    for (auto& domain : mesh_domain_map_[mesh_id]) {
812✔
1503
      Source::DomainType domain_type = domain.first;
406✔
1504
      int domain_id = domain.second;
406✔
1505

1506
      if (domain_type == Source::DomainType::MATERIAL) {
406✔
1507
        for (int i_cell = 0; i_cell < model::cells.size(); i_cell++) {
180✔
1508
          if (domain_id == C_NONE) {
150!
1509
            apply_mesh_to_cell_and_children(i_cell, mesh_idx, domain_id, true);
×
1510
          } else {
1511
            apply_mesh_to_cell_and_children(i_cell, mesh_idx, domain_id, false);
150✔
1512
          }
1513
        }
1514
      } else if (domain_type == Source::DomainType::CELL) {
376✔
1515
        int32_t i_cell = model::cell_map[domain_id];
30✔
1516
        apply_mesh_to_cell_and_children(i_cell, mesh_idx, C_NONE, false);
30✔
1517
      } else if (domain_type == Source::DomainType::UNIVERSE) {
346!
1518
        int32_t i_universe = model::universe_map[domain_id];
346✔
1519
        Universe& universe = *model::universes[i_universe];
346✔
1520
        for (int32_t i_cell : universe.cells_) {
1,202✔
1521
          apply_mesh_to_cell_and_children(i_cell, mesh_idx, C_NONE, false);
856✔
1522
        }
1523
      }
1524
    }
1525
  }
1526
}
1527

1528
SourceRegionHandle FlatSourceDomain::get_subdivided_source_region_handle(
1,495,487,076✔
1529
  SourceRegionKey sr_key, Position r, Direction u)
1530
{
1531
  // Case 1: Check if the source region key is already present in the permanent
1532
  // map. This is the most common condition, as any source region visited in a
1533
  // previous power iteration will already be present in the permanent map. If
1534
  // the source region key is found, we translate the key into a specific 1D
1535
  // source region index and return a handle its position in the
1536
  // source_regions_ vector.
1537
  auto it = source_region_map_.find(sr_key);
1,495,487,076✔
1538
  if (it != source_region_map_.end()) {
1,495,487,076✔
1539
    int64_t sr = it->second;
1,445,661,016✔
1540
    return source_regions_.get_source_region_handle(sr);
1,445,661,016✔
1541
  }
1542

1543
  // Case 2: Check if the source region key is present in the temporary (thread
1544
  // safe) map. This is a common occurrence in the first power iteration when
1545
  // the source region has already been visited already by some other ray. We
1546
  // begin by locking the temporary map before any operations are performed. The
1547
  // lock is not global over the full data structure -- it will be dependent on
1548
  // which key is used.
1549
  discovered_source_regions_.lock(sr_key);
49,826,060✔
1550

1551
  // If the key is found in the temporary map, then we return a handle to the
1552
  // source region that is stored in the temporary map.
1553
  if (discovered_source_regions_.contains(sr_key)) {
49,826,060✔
1554
    SourceRegionHandle handle {discovered_source_regions_[sr_key]};
47,398,680✔
1555
    discovered_source_regions_.unlock(sr_key);
47,398,680✔
1556
    return handle;
47,398,680✔
1557
  }
1558

1559
  // Case 3: The source region key is not present anywhere, but it is only due
1560
  // to floating point artifacts. These artifacts occur when the overlaid mesh
1561
  // overlaps with actual geometry surfaces. In these cases, roundoff error may
1562
  // result in the ray tracer detecting an additional (very short) segment
1563
  // though a mesh bin that is actually past the physical source region
1564
  // boundary. This is a result of the the multi-level ray tracing treatment in
1565
  // OpenMC, which depending on the number of universes in the hierarchy etc can
1566
  // result in the wrong surface being selected as the nearest. This can happen
1567
  // in a lattice when there are two directions that both are very close in
1568
  // distance, within the tolerance of FP_REL_PRECISION, and the are thus
1569
  // treated as being equivalent so alternative logic is used. However, when we
1570
  // go and ray trace on this with the mesh tracer we may go past the surface
1571
  // bounding the current source region.
1572
  //
1573
  // To filter out this case, before we create the new source region, we double
1574
  // check that the actual starting point of this segment (r) is still in the
1575
  // same geometry source region that we started in. If an artifact is detected,
1576
  // we discard the segment (and attenuation through it) as it is not really a
1577
  // valid source region and will have only an infinitessimally small cell
1578
  // combined with the mesh bin. Thankfully, this is a fairly rare condition,
1579
  // and only triggers for very short ray lengths. It can be fixed by decreasing
1580
  // the value of FP_REL_PRECISION in constants.h, but this may have unknown
1581
  // consequences for the general ray tracer, so for now we do the below sanity
1582
  // checks before generating phantom source regions. A significant extra cost
1583
  // is incurred in instantiating the GeometryState object and doing a cell
1584
  // lookup, but again, this is going to be an extremely rare thing to check
1585
  // after the first power iteration has completed.
1586

1587
  // Sanity check on source region id
1588
  GeometryState gs;
2,427,380✔
1589
  gs.r() = r + TINY_BIT * u;
2,427,380✔
1590
  gs.u() = {1.0, 0.0, 0.0};
2,427,380✔
1591
  exhaustive_find_cell(gs);
2,427,380✔
1592
  int64_t sr_found = lookup_base_source_region_idx(gs);
2,427,380✔
1593
  if (sr_found != sr_key.base_source_region_id) {
2,427,380✔
1594
    discovered_source_regions_.unlock(sr_key);
121✔
1595
    SourceRegionHandle handle;
121✔
1596
    handle.is_numerical_fp_artifact_ = true;
121✔
1597
    return handle;
121✔
1598
  }
1599

1600
  // Sanity check on mesh bin
1601
  int mesh_idx = lookup_mesh_idx(sr_key.base_source_region_id);
2,427,259✔
1602
  if (mesh_idx == C_NONE) {
2,427,259✔
1603
    if (sr_key.mesh_bin != 0) {
393,756!
1604
      discovered_source_regions_.unlock(sr_key);
×
1605
      SourceRegionHandle handle;
×
1606
      handle.is_numerical_fp_artifact_ = true;
×
1607
      return handle;
×
1608
    }
1609
  } else {
1610
    Mesh* mesh = model::meshes[mesh_idx].get();
2,033,503✔
1611
    int bin_found = mesh->get_bin(r + TINY_BIT * u);
2,033,503✔
1612
    if (bin_found != sr_key.mesh_bin) {
2,033,503!
1613
      discovered_source_regions_.unlock(sr_key);
×
1614
      SourceRegionHandle handle;
×
1615
      handle.is_numerical_fp_artifact_ = true;
×
1616
      return handle;
×
1617
    }
1618
  }
1619

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

1627
  // Call the basic constructor for the source region and store in the parallel
1628
  // map.
1629
  bool is_linear = RandomRay::source_shape_ != RandomRaySourceShape::FLAT;
2,427,259✔
1630
  SourceRegion* sr_ptr =
2,427,259✔
1631
    discovered_source_regions_.emplace(sr_key, {negroups_, is_linear});
2,427,259✔
1632
  SourceRegionHandle handle {*sr_ptr};
2,427,259✔
1633

1634
  // Determine the material
1635
  int gs_i_cell = gs.lowest_coord().cell();
2,427,259!
1636
  Cell& cell = *model::cells[gs_i_cell];
2,427,259!
1637
  int material = cell.material(gs.cell_instance());
2,427,259!
1638
  int temp = 0;
2,427,259✔
1639

1640
  // If material total XS is extremely low, just set it to void to avoid
1641
  // problems with 1/Sigma_t
1642
  if (material != MATERIAL_VOID) {
2,427,259✔
1643
    temp = data::mg.macro_xs_[material].get_temperature_index(
4,810,390✔
1644
      cell.sqrtkT(gs.cell_instance()));
2,405,195✔
1645
    for (int g = 0; g < negroups_; g++) {
5,161,511✔
1646
      double sigma_t =
2,756,404✔
1647
        sigma_t_[(material * ntemperature_ + temp) * negroups_ + g];
2,756,404✔
1648
      if (sigma_t < MINIMUM_MACRO_XS) {
2,756,404✔
1649
        material = MATERIAL_VOID;
1650
        temp = 0;
1651
        break;
1652
      }
1653
    }
1654
  }
1655

1656
  handle.material() = material;
2,427,259✔
1657
  handle.temperature_idx() = temp;
2,427,259✔
1658

1659
  handle.density_mult() = cell.density_mult(gs.cell_instance());
2,427,259✔
1660

1661
  // Store the mesh index (if any) assigned to this source region
1662
  handle.mesh() = mesh_idx;
2,427,259✔
1663

1664
  if (settings::run_mode == RunMode::FIXED_SOURCE) {
2,427,259✔
1665
    // Determine if there are any volumetric sources, and apply them.
1666
    // Volumetric sources are specifc only to the base SR idx.
1667
    auto it_vol =
2,371,236✔
1668
      external_volumetric_source_map_.find(sr_key.base_source_region_id);
2,371,236✔
1669
    if (it_vol != external_volumetric_source_map_.end()) {
2,371,236✔
1670
      const vector<int>& vol_sources = it_vol->second;
4,950✔
1671
      for (int src_idx : vol_sources) {
9,900✔
1672
        apply_external_source_to_source_region(src_idx, handle);
4,950✔
1673
      }
1674
    }
1675

1676
    // Determine if there are any point sources, and apply them.
1677
    // Point sources are specific to the source region key.
1678
    auto it_point = external_point_source_map_.find(sr_key);
2,371,236✔
1679
    if (it_point != external_point_source_map_.end()) {
2,371,236✔
1680
      const vector<int>& point_sources = it_point->second;
12✔
1681
      for (int src_idx : point_sources) {
24✔
1682
        apply_external_source_to_source_region(src_idx, handle);
12✔
1683
      }
1684
    }
1685

1686
    // Divide external source term by sigma_t
1687
    if (material != C_NONE) {
2,371,236✔
1688
      for (int g = 0; g < negroups_; g++) {
4,744,127✔
1689
        double sigma_t =
2,395,043✔
1690
          sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
2,395,043✔
1691
          handle.density_mult();
2,395,043✔
1692
        handle.external_source(g) /= sigma_t;
2,395,043✔
1693
      }
1694
    }
1695
  }
1696

1697
  // Compute the combined source term
1698
  update_single_neutron_source(handle);
2,427,259✔
1699

1700
  // Unlock the parallel map. Note: we may be tempted to release
1701
  // this lock earlier, and then just use the source region's lock to protect
1702
  // the flux/source initialization stages above. However, the rest of the code
1703
  // only protects updates to the new flux and volume fields, and assumes that
1704
  // the source is constant for the duration of transport. Thus, using just the
1705
  // source region's lock by itself would result in other threads potentially
1706
  // reading from the source before it is computed, as they won't use the lock
1707
  // when only reading from the SR's source. It would be expensive to protect
1708
  // those operations, whereas generating the SR is only done once, so we just
1709
  // hold the map's bucket lock until the source region is fully initialized.
1710
  discovered_source_regions_.unlock(sr_key);
2,427,259✔
1711

1712
  return handle;
2,427,259✔
1713
}
2,427,380✔
1714

1715
void FlatSourceDomain::finalize_discovered_source_regions()
15,962✔
1716
{
1717
  // Extract keys for entries with a valid volume.
1718
  vector<SourceRegionKey> keys;
15,962✔
1719
  for (const auto& pair : discovered_source_regions_) {
4,886,442✔
1720
    if (pair.second.volume_ > 0.0) {
2,427,259✔
1721
      keys.push_back(pair.first);
2,325,817✔
1722
    }
1723
  }
1724

1725
  if (!keys.empty()) {
15,962✔
1726
    // Sort the keys, so as to ensure reproducible ordering given that source
1727
    // regions may have been added to discovered_source_regions_ in an arbitrary
1728
    // order due to shared memory threading.
1729
    std::sort(keys.begin(), keys.end());
914✔
1730

1731
    // Remember the index of the first new source region
1732
    int64_t start_sr_id = source_regions_.n_source_regions();
914✔
1733

1734
    // Append the source regions in the sorted key order.
1735
    for (const auto& key : keys) {
2,326,731✔
1736
      const SourceRegion& sr = discovered_source_regions_[key];
2,325,817✔
1737
      source_region_map_[key] = source_regions_.n_source_regions();
2,325,817✔
1738
      source_regions_.push_back(sr);
2,325,817✔
1739
    }
1740

1741
    // Map all new source regions to tallies
1742
    convert_source_regions_to_tallies(start_sr_id);
914✔
1743
  }
1744

1745
  discovered_source_regions_.clear();
15,962✔
1746
}
15,962✔
1747

1748
// This is the "diagonal stabilization" technique developed by Gunow et al. in:
1749
//
1750
// Geoffrey Gunow, Benoit Forget, Kord Smith, Stabilization of multi-group
1751
// neutron transport with transport-corrected cross-sections, Annals of Nuclear
1752
// Energy, Volume 126, 2019, Pages 211-219, ISSN 0306-4549,
1753
// https://doi.org/10.1016/j.anucene.2018.10.036.
1754
void FlatSourceDomain::apply_transport_stabilization()
15,962✔
1755
{
1756
  // Don't do anything if all in-group scattering
1757
  // cross sections are positive
1758
  if (!is_transport_stabilization_needed_) {
15,962✔
1759
    return;
1760
  }
1761

1762
  // Apply the stabilization factor to all source elements
1763
#pragma omp parallel for
120✔
1764
  for (int64_t sr = 0; sr < n_source_regions(); sr++) {
1,300✔
1765
    int material = source_regions_.material(sr);
1,200!
1766
    int temp = source_regions_.temperature_idx(sr);
1,200!
1767
    double density_mult = source_regions_.density_mult(sr);
1,200!
1768
    if (material == MATERIAL_VOID) {
1,200!
1769
      continue;
1770
    }
1771
    for (int g = 0; g < negroups_; g++) {
85,200✔
1772
      // Only apply stabilization if the diagonal (in-group) scattering XS is
1773
      // negative
1774
      double sigma_s =
84,000✔
1775
        sigma_s_[((material * ntemperature_ + temp) * negroups_ + g) *
84,000✔
1776
                   negroups_ +
84,000✔
1777
                 g] *
84,000✔
1778
        density_mult;
84,000✔
1779
      if (sigma_s < 0.0) {
84,000✔
1780
        double sigma_t =
22,000✔
1781
          sigma_t_[(material * ntemperature_ + temp) * negroups_ + g] *
22,000✔
1782
          density_mult;
22,000✔
1783
        double phi_new = source_regions_.scalar_flux_new(sr, g);
22,000✔
1784
        double phi_old = source_regions_.scalar_flux_old(sr, g);
22,000✔
1785

1786
        // Equation 18 in the above Gunow et al. 2019 paper. For a default
1787
        // rho of 1.0, this ensures there are no negative diagonal elements
1788
        // in the iteration matrix. A lesser rho could be used (or exposed
1789
        // as a user input parameter) to reduce the negative impact on
1790
        // convergence rate though would need to be experimentally tested to see
1791
        // if it doesn't become unstable. rho = 1.0 is good as it gives the
1792
        // highest assurance of stability, and the impacts on convergence rate
1793
        // are pretty mild.
1794
        double D = diagonal_stabilization_rho_ * sigma_s / sigma_t;
22,000✔
1795

1796
        // Equation 16 in the above Gunow et al. 2019 paper
1797
        source_regions_.scalar_flux_new(sr, g) =
22,000✔
1798
          (phi_new - D * phi_old) / (1.0 - D);
22,000✔
1799
      }
1800
    }
1801
  }
1802
}
1803

1804
// Determines the base source region index (i.e., a material filled cell
1805
// instance) that corresponds to a particular location in the geometry. Requires
1806
// that the "gs" object passed in has already been initialized and has called
1807
// find_cell etc.
1808
int64_t FlatSourceDomain::lookup_base_source_region_idx(
1,009,291,276✔
1809
  const GeometryState& gs) const
1810
{
1811
  int i_cell = gs.lowest_coord().cell();
1,009,291,276✔
1812
  int64_t sr = source_region_offsets_[i_cell] + gs.cell_instance();
1,009,291,276✔
1813
  return sr;
1,009,291,276✔
1814
}
1815

1816
// Determines the index of the mesh (if any) that has been applied
1817
// to a particular base source region index.
1818
int FlatSourceDomain::lookup_mesh_idx(int64_t sr) const
1,009,291,155✔
1819
{
1820
  int mesh_idx = C_NONE;
1,009,291,155✔
1821
  auto mesh_it = mesh_map_.find(sr);
1,009,291,155✔
1822
  if (mesh_it != mesh_map_.end()) {
1,009,291,155✔
1823
    mesh_idx = mesh_it->second;
543,833,690✔
1824
  }
1825
  return mesh_idx;
1,009,291,155✔
1826
}
1827

1828
// Determines the source region key that corresponds to a particular location in
1829
// the geometry. This takes into account both the base source region index as
1830
// well as the mesh bin if a mesh is applied to this source region for
1831
// subdivision.
1832
SourceRegionKey FlatSourceDomain::lookup_source_region_key(
2,317,336✔
1833
  const GeometryState& gs) const
1834
{
1835
  int64_t sr = lookup_base_source_region_idx(gs);
2,317,336✔
1836
  int64_t mesh_bin = lookup_mesh_bin(sr, gs.r());
2,317,336✔
1837
  return SourceRegionKey {sr, mesh_bin};
2,317,336✔
1838
}
1839

1840
// Determines the mesh bin that corresponds to a particular base source region
1841
// index and position.
1842
int64_t FlatSourceDomain::lookup_mesh_bin(int64_t sr, Position r) const
2,317,336✔
1843
{
1844
  int mesh_idx = lookup_mesh_idx(sr);
2,317,336✔
1845
  int mesh_bin = 0;
2,317,336✔
1846
  if (mesh_idx != C_NONE) {
2,317,336✔
1847
    mesh_bin = model::meshes[mesh_idx]->get_bin(r);
1,453,836✔
1848
  }
1849
  return mesh_bin;
2,317,336✔
1850
}
1851

1852
} // 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

© 2026 Coveralls, Inc