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

openmc-dev / openmc / 44413430952

18 Nov 2026 10:37PM UTC coverage: 82.378% (+0.02%) from 82.354%
44413430952

Pull #4141

github

web-flow
Merge 85b2906c8 into b3443897d
Pull Request #4141: Adding multigroup photon transport capability in MC mode

21006 of 29768 branches covered (70.57%)

Branch coverage included in aggregate %.

43 of 44 new or added lines in 6 files covered. (97.73%)

88 existing lines in 3 files now uncovered.

64485 of 74011 relevant lines covered (87.13%)

42516521.04 hits per line

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

92.37
/src/scattdata.cpp
1
#include "openmc/scattdata.h"
2

3
#include <algorithm>
4
#include <cmath>
5
#include <numeric>
6

7
#include "openmc/tensor.h"
8

9
#include "openmc/constants.h"
10
#include "openmc/error.h"
11
#include "openmc/math_functions.h"
12
#include "openmc/random_lcg.h"
13
#include "openmc/settings.h"
14

15
namespace openmc {
16

17
//==============================================================================
18
// ScattData base-class methods
19
//==============================================================================
20

21
void ScattData::base_init(int order, const tensor::Tensor<int>& in_gmin,
8,900 ✔
22
  const tensor::Tensor<int>& in_gmax, const double_2dvec& in_energy,
23
  const double_2dvec& in_mult)
24
{
25
  size_t groups = in_energy.size();
8,900 ✔
26

27
  gmin = in_gmin;
8,900 ✔
28
  gmax = in_gmax;
8,900 ✔
29
  energy.resize(groups);
8,900 ✔
30
  mult.resize(groups);
8,900 ✔
31
  dist.resize(groups);
8,900 ✔
32
  weight_mult_.assign(groups, 1.);
8,900 ✔
33

34
  for (int gin = 0; gin < groups; gin++) {
42,437 ✔
35
    // Store the inputted data
36
    energy[gin] = in_energy[gin];
33,537 ✔
37
    mult[gin] = in_mult[gin];
33,537 ✔
38

39
    // Make sure the multiplicity does not have 0s
40
    for (int go = 0; go < mult[gin].size(); go++) {
201,691 ✔
41
      if (mult[gin][go] == 0.) {
168,154 ✔
42
        mult[gin][go] = 1.;
16,810 ✔
43
      }
44
    }
45

46
    // Make sure the energy is normalized
47
    double norm = std::accumulate(energy[gin].begin(), energy[gin].end(), 0.);
33,537 ✔
48

49
    if (norm != 0.) {
33,537 ✔
50
      // The outgoing group is sampled from neutron production, B_gh / B_g.
51
      // Use the row multiplier B_g / C_g, not the sampled channel multiplicity,
52
      // so the expected production remains C_g * (B_gh / B_g) * (B_g / C_g).
53
      double scatter_norm = 0.;
54
      for (int go = 0; go < energy[gin].size(); go++) {
200,779 ✔
55
        if (energy[gin][go] != 0.) {
167,686 ✔
56
          scatter_norm += energy[gin][go] / mult[gin][go];
143,116 ✔
57
        }
58
      }
59
      weight_mult_[gin] = norm / scatter_norm;
33,093 ✔
60
      for (auto& n : energy[gin])
200,779 ✔
61
        n /= norm;
167,686 ✔
62
    }
63

64
    // Initialize the distribution data
65
    dist[gin].resize(in_gmax[gin] - in_gmin[gin] + 1);
33,537 ✔
66
    for (auto& v : dist[gin]) {
201,691 ✔
67
      v.resize(order);
168,154 ✔
68
    }
69
  }
70
}
8,900 ✔
71

72
//==============================================================================
73

74
void ScattData::base_combine(size_t max_order, size_t order_dim,
2,780 ✔
75
  const vector<ScattData*>& those_scatts, const vector<double>& scalars,
76
  tensor::Tensor<int>& in_gmin, tensor::Tensor<int>& in_gmax,
77
  double_2dvec& sparse_mult, double_3dvec& sparse_scatter)
78
{
79
  size_t groups = those_scatts[0]->energy.size();
2,780 ✔
80

81
  // Now allocate and zero our storage spaces
82
  tensor::Tensor<double> this_nuscatt_matrix =
2,780 ✔
83
    tensor::zeros<double>({groups, groups, order_dim});
2,780 ✔
84
  tensor::Tensor<double> this_nuscatt_P0 =
2,780 ✔
85
    tensor::zeros<double>({groups, groups});
2,780 ✔
86
  tensor::Tensor<double> this_scatt_P0 =
2,780 ✔
87
    tensor::zeros<double>({groups, groups});
2,780 ✔
88
  tensor::Tensor<double> this_mult = tensor::ones<double>({groups, groups});
2,780 ✔
89

90
  // Build the dense scattering and multiplicity matrices
91
  for (int i = 0; i < those_scatts.size(); i++) {
5,944 ✔
92
    ScattData* that = those_scatts[i];
3,164 ✔
93

94
    // Build the dense matrix for that object
95
    tensor::Tensor<double> that_matrix = that->get_matrix(max_order);
3,164 ✔
96

97
    // Now add that to this for the nu-scatter matrix
98
    this_nuscatt_matrix += scalars[i] * that_matrix;
3,164 ✔
99

100
    // Do the same with the P0 matrices
101
    for (int gin = 0; gin < groups; gin++) {
14,723 ✔
102
      for (int go = 0; go < groups; go++) {
336,362 ✔
103
        this_nuscatt_P0(gin, go) +=
649,606 ✔
104
          scalars[i] * that->get_xs(MgxsType::NU_SCATTER, gin, &go, nullptr);
324,803 ✔
105
        this_scatt_P0(gin, go) +=
649,606 ✔
106
          scalars[i] * that->get_xs(MgxsType::SCATTER, gin, &go, nullptr);
324,803 ✔
107
      }
108
    }
109
  }
3,164 ✔
110

111
  // Now we have the dense nuscatt and scatt, we can easily compute the
112
  // multiplicity matrix by dividing the two and fixing any nans
113
  this_mult = tensor::nan_to_num(this_nuscatt_P0 / this_scatt_P0);
5,560 ✔
114

115
  // We have the data, now we need to convert to a jagged array and then use
116
  // the initialize function to store it on the object.
117
  for (int gin = 0; gin < groups; gin++) {
13,601 ✔
118
    // Find the minimum and maximum group boundaries
119
    int gmin_;
120
    for (gmin_ = 0; gmin_ < groups; gmin_++) {
163,180 ✔
121
      bool non_zero = false;
479,141 ✔
122
      for (int l = 0; l < this_nuscatt_matrix.shape(2); l++) {
958,282 !
123
        if (this_nuscatt_matrix(gin, gmin_, l) != 0.) {
326,782 ✔
124
          non_zero = true;
125
          break;
126
        }
127
      }
128
      if (non_zero)
163,040 ✔
129
        break;
130
    }
131
    int gmax_;
10,821 ✔
132
    for (gmax_ = groups - 1; gmax_ >= 0; gmax_--) {
132,221 ✔
133
      bool non_zero = false;
377,081 ✔
134
      for (int l = 0; l < this_nuscatt_matrix.shape(2); l++) {
754,162 !
135
        if (this_nuscatt_matrix(gin, gmax_, l) != 0.) {
255,681 ✔
136
          non_zero = true;
137
          break;
138
        }
139
      }
140
      if (non_zero)
132,081 ✔
141
        break;
142
    }
143

144
    // treat the case of all values being 0
145
    if (gmin_ > gmax_) {
10,821 ✔
146
      gmin_ = gin;
140 ✔
147
      gmax_ = gin;
140 ✔
148
    }
149

150
    // Store the group bounds
151
    in_gmin[gin] = gmin_;
10,821 ✔
152
    in_gmax[gin] = gmax_;
10,821 ✔
153

154
    // Store the data in the compressed format
155
    sparse_scatter[gin].resize(gmax_ - gmin_ + 1);
10,821 ✔
156
    sparse_mult[gin].resize(gmax_ - gmin_ + 1);
10,821 ✔
157
    int i_gout = 0;
158
    for (int gout = gmin_; gout <= gmax_; gout++) {
66,325 ✔
159
      sparse_scatter[gin][i_gout].resize(this_nuscatt_matrix.shape(2));
111,008 !
160
      for (int l = 0; l < this_nuscatt_matrix.shape(2); l++) {
251,584 !
161
        sparse_scatter[gin][i_gout][l] = this_nuscatt_matrix(gin, gout, l);
140,576 ✔
162
      }
163
      sparse_mult[gin][i_gout] = this_mult(gin, gout);
55,504 ✔
164
      i_gout++;
55,504 ✔
165
    }
166
  }
167
}
11,120 ✔
168

169
//==============================================================================
170

171
void ScattData::sample_energy(int gin, int& gout, int& i_gout, uint64_t* seed)
1,229,047,360 ✔
172
{
173
  // Sample the outgoing group
174
  double xi = prn(seed);
1,229,047,360 ✔
175
  double prob = 0.;
1,229,047,360 ✔
176
  i_gout = 0;
1,229,047,360 ✔
177
  for (gout = gmin[gin]; gout < gmax[gin]; ++gout) {
1,496,917,256 ✔
178
    prob += energy[gin][i_gout];
1,182,490,984 ✔
179
    if (xi < prob)
1,182,490,984 ✔
180
      break;
181
    ++i_gout;
267,869,896 ✔
182
  }
183
}
1,229,047,360 ✔
184

185
//==============================================================================
186

187
double ScattData::get_xs(
38,873,770 ✔
188
  MgxsType xstype, int gin, const int* gout, const double* mu)
189
{
190
  // Set the outgoing group offset index as needed
191
  int i_gout = 0;
38,873,770 ✔
192
  if (gout != nullptr) {
38,873,770 ✔
193
    // short circuit the function if gout is from a zero portion of the
194
    // scattering matrix
195
    if ((*gout < gmin[gin]) || (*gout > gmax[gin])) { // > gmax?
34,718,834 ✔
196
      return 0.;
197
    }
198
    i_gout = *gout - gmin[gin];
33,916,180 ✔
199
  }
200

201
  double val = scattxs[gin];
38,071,116 !
202
  switch (xstype) {
38,071,116 !
203
  case MgxsType::NU_SCATTER:
12,351,492 ✔
204
    if (gout != nullptr)
12,351,492 ✔
205
      val *= energy[gin][i_gout];
10,274,036 ✔
206
    break;
207
  case MgxsType::SCATTER:
12,298,952 ✔
208
    if (gout != nullptr) {
12,298,952 ✔
209
      val *= energy[gin][i_gout] / mult[gin][i_gout];
10,221,472 ✔
210
    } else {
211
      val /= weight_mult_[gin];
2,077,480 ✔
212
    }
213
    break;
214
  case MgxsType::NU_SCATTER_FMU:
6,710,336 ✔
215
    if ((gout != nullptr) && (mu != nullptr)) {
6,710,336 !
216
      val *= energy[gin][i_gout] * calc_f(gin, *gout, *mu);
6,710,336 ✔
217
    } else {
218
      // This is not an expected path (asking for f_mu without asking for a
219
      // group or mu is not useful
UNCOV
220
      fatal_error("Invalid call to get_xs");
×
221
    }
222
    break;
6,710,336 ✔
223
  case MgxsType::SCATTER_FMU:
6,710,336 ✔
224
    if ((gout != nullptr) && (mu != nullptr)) {
6,710,336 !
225
      val *= energy[gin][i_gout] * calc_f(gin, *gout, *mu) / mult[gin][i_gout];
6,710,336 ✔
226
    } else {
227
      // This is not an expected path (asking for f_mu without asking for a
228
      // group or mu is not useful
UNCOV
229
      fatal_error("Invalid call to get_xs");
×
230
    }
231
    break;
6,710,336 ✔
232
  default:
233
    break;
234
  }
235
  return val;
236
}
237

238
//==============================================================================
239
// ScattDataLegendre methods
240
//==============================================================================
241

242
void ScattDataLegendre::init(const tensor::Tensor<int>& in_gmin,
3,275 ✔
243
  const tensor::Tensor<int>& in_gmax, const double_2dvec& in_mult,
244
  const double_3dvec& coeffs)
245
{
246
  size_t groups = coeffs.size();
3,275 ✔
247
  size_t order = coeffs[0][0].size();
3,275 ✔
248

249
  // make a copy of coeffs that we can use to both extract data and normalize
250
  double_3dvec matrix = coeffs;
3,275 ✔
251

252
  // Get the scattering cross section value by summing the un-normalized P0
253
  // coefficient in the variable matrix over all outgoing groups.
254
  scattxs = tensor::zeros<double>({groups});
3,275 ✔
255
  for (int gin = 0; gin < groups; gin++) {
15,063 ✔
256
    int num_groups = in_gmax[gin] - in_gmin[gin] + 1;
11,788 ✔
257
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
68,758 ✔
258
      scattxs[gin] += matrix[gin][i_gout][0];
56,970 ✔
259
    }
260
  }
261

262
  // Build the energy transfer matrix from data in the variable matrix while
263
  // also normalizing the variable matrix itself
264
  // (forcing the CDF of f(mu=1) == 1)
265
  double_2dvec in_energy;
3,275 ✔
266
  in_energy.resize(groups);
3,275 ✔
267
  for (int gin = 0; gin < groups; gin++) {
15,063 ✔
268
    int num_groups = in_gmax[gin] - in_gmin[gin] + 1;
11,788 ✔
269
    in_energy[gin].resize(num_groups);
11,788 ✔
270
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
68,758 ✔
271
      double norm = matrix[gin][i_gout][0];
56,970 ✔
272
      in_energy[gin][i_gout] = norm;
56,970 ✔
273
      if (norm != 0.) {
56,970 ✔
274
        for (auto& n : matrix[gin][i_gout])
99,786 ✔
275
          n /= norm;
51,162 ✔
276
      }
277
    }
278
  }
279

280
  // Initialize the base class attributes
281
  ScattData::base_init(order, in_gmin, in_gmax, in_energy, in_mult);
3,275 ✔
282

283
  // Set the distribution (sdata.dist) values and initialize max_val
284
  max_val.resize(groups);
3,275 ✔
285
  for (int gin = 0; gin < groups; gin++) {
15,063 ✔
286
    int num_groups = gmax[gin] - gmin[gin] + 1;
11,788 ✔
287
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
68,758 ✔
288
      dist[gin][i_gout] = matrix[gin][i_gout];
56,970 ✔
289
    }
290
    max_val[gin].resize(num_groups);
11,788 ✔
291
    for (auto& n : max_val[gin])
68,758 ✔
292
      n = 0.;
56,970 ✔
293
  }
294

295
  // Now update the maximum value
296
  update_max_val();
3,275 ✔
297
}
3,275 ✔
298

299
//==============================================================================
300

301
void ScattDataLegendre::update_max_val()
3,275 ✔
302
{
303
  size_t groups = max_val.size();
3,275 ✔
304
  // Step through the polynomial with fixed number of points to identify the
305
  // maximal value
306
  int Nmu = 1001;
3,275 ✔
307
  double dmu = 2. / (Nmu - 1);
3,275 ✔
308
  for (int gin = 0; gin < groups; gin++) {
15,063 ✔
309
    int num_groups = gmax[gin] - gmin[gin] + 1;
11,788 ✔
310
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
68,758 ✔
311
      for (int imu = 0; imu < Nmu; imu++) {
57,083,940 ✔
312
        double mu;
57,026,970 ✔
313
        if (imu == 0) {
57,026,970 ✔
314
          mu = -1.;
315
        } else if (imu == (Nmu - 1)) {
56,970,000 ✔
316
          mu = 1.;
317
        } else {
318
          mu = -1. + (imu - 1) * dmu;
56,913,030 ✔
319
        }
320

321
        // Calculate probability
322
        double f = evaluate_legendre(
114,053,940 ✔
323
          dist[gin][i_gout].size() - 1, dist[gin][i_gout].data(), mu);
57,026,970 ✔
324

325
        // if this is a new maximum, store it
326
        if (f > max_val[gin][i_gout])
57,026,970 ✔
327
          max_val[gin][i_gout] = f;
1,318,776 ✔
328
      } // end imu loop
329

330
      // Since we may not have caught the true max, add 10% margin
331
      max_val[gin][i_gout] *= 1.1;
56,970 ✔
332
    }
333
  }
334
}
3,275 ✔
335

336
//==============================================================================
337

338
double ScattDataLegendre::calc_f(int gin, int gout, double mu)
55,490,640 ✔
339
{
340
  double f;
55,490,640 ✔
341
  if ((gout < gmin[gin]) || (gout > gmax[gin])) {
55,490,640 !
342
    f = 0.;
343
  } else {
344
    int i_gout = gout - gmin[gin];
55,490,640 ✔
345
    f = evaluate_legendre(
55,490,640 ✔
346
      dist[gin][i_gout].size() - 1, dist[gin][i_gout].data(), mu);
55,490,640 ✔
347
  }
348
  return f;
55,490,640 ✔
349
}
350

351
//==============================================================================
352

353
void ScattDataLegendre::sample(
23,748,400 ✔
354
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed)
355
{
356
  // Sample the outgoing energy using the base-class method
357
  int i_gout;
23,748,400 ✔
358
  sample_energy(gin, gout, i_gout, seed);
23,748,400 ✔
359

360
  // Now we can sample mu using the scattering kernel using rejection
361
  // sampling from a rectangular bounding box
362
  double M = max_val[gin][i_gout];
23,748,400 ✔
363
  int samples;
23,748,400 ✔
364
  for (samples = 0; samples < MAX_SAMPLE; ++samples) {
42,069,888 !
365
    mu = 2. * prn(seed) - 1.;
42,069,888 ✔
366
    double f = calc_f(gin, gout, mu);
42,069,888 ✔
367
    if (f > 0.) {
42,069,888 !
368
      double u = prn(seed) * M;
42,069,888 ✔
369
      if (u <= f)
42,069,888 ✔
370
        break;
371
    }
372
  }
373
  if (samples == MAX_SAMPLE) {
23,748,400 !
UNCOV
374
    fatal_error("Maximum number of Legendre expansion samples reached!");
×
375
  }
376

377
  // Update the weight to reflect neutron multiplicity
378
  wgt *= weight_mult_[gin];
23,748,400 ✔
379
}
23,748,400 ✔
380

381
//==============================================================================
382

383
void ScattDataLegendre::combine(
198 ✔
384
  const vector<ScattData*>& those_scatts, const vector<double>& scalars)
385
{
386
  // Find the max order in the data set and make sure we can combine the sets
387
  size_t max_order = 0;
198 ✔
388
  for (int i = 0; i < those_scatts.size(); i++) {
414 ✔
389
    // Lets also make sure these items are combineable
390
    ScattDataLegendre* that = dynamic_cast<ScattDataLegendre*>(those_scatts[i]);
216 !
391
    if (!that) {
216 !
UNCOV
392
      fatal_error("Cannot combine the ScattData objects!");
×
393
    }
394
    size_t that_order = that->get_order();
216 ✔
395
    if (that_order > max_order)
216 ✔
396
      max_order = that_order;
397
  }
398

399
  size_t groups = those_scatts[0]->energy.size();
198 ✔
400

401
  tensor::Tensor<int> in_gmin({groups}, 0);
198 ✔
402
  tensor::Tensor<int> in_gmax({groups}, 0);
198 ✔
403
  double_3dvec sparse_scatter(groups);
198 ✔
404
  double_2dvec sparse_mult(groups);
198 ✔
405

406
  // The rest of the steps do not depend on the type of angular representation
407
  // so we use a base class method to sum up xs and create new energy and mult
408
  // matrices
409
  size_t order_dim = max_order + 1;
198 ✔
410
  ScattData::base_combine(max_order, order_dim, those_scatts, scalars, in_gmin,
198 ✔
411
    in_gmax, sparse_mult, sparse_scatter);
412

413
  // Got everything we need, store it.
414
  init(in_gmin, in_gmax, sparse_mult, sparse_scatter);
198 ✔
415
}
594 ✔
416

417
//==============================================================================
418

419
tensor::Tensor<double> ScattDataLegendre::get_matrix(size_t max_order)
216 ✔
420
{
421
  // Get the sizes and initialize the data to 0
422
  size_t groups = energy.size();
216 ✔
423
  size_t order_dim = max_order + 1;
216 ✔
424
  tensor::Tensor<double> matrix =
216 ✔
425
    tensor::zeros<double>({groups, groups, order_dim});
216 ✔
426

427
  for (int gin = 0; gin < groups; gin++) {
648 ✔
428
    for (int i_gout = 0; i_gout < energy[gin].size(); i_gout++) {
1,080 ✔
429
      int gout = i_gout + gmin[gin];
648 ✔
430
      for (int l = 0; l < order_dim; l++) {
1,866 ✔
431
        matrix(gin, gout, l) =
1,218 ✔
432
          scattxs[gin] * energy[gin][i_gout] * dist[gin][i_gout][l];
1,218 ✔
433
      }
434
    }
435
  }
436
  return matrix;
216 ✔
437
}
438

439
//==============================================================================
440
// ScattDataHistogram methods
441
//==============================================================================
442

443
void ScattDataHistogram::init(const tensor::Tensor<int>& in_gmin,
164 ✔
444
  const tensor::Tensor<int>& in_gmax, const double_2dvec& in_mult,
445
  const double_3dvec& coeffs)
446
{
447
  size_t groups = coeffs.size();
164 ✔
448
  size_t order = coeffs[0][0].size();
164 ✔
449

450
  // make a copy of coeffs that we can use to both extract data and normalize
451
  double_3dvec matrix = coeffs;
164 ✔
452

453
  // Get the scattering cross section value by summing the distribution
454
  // over all the histogram bins in angle and outgoing energy groups
455
  scattxs = tensor::zeros<double>({groups});
164 ✔
456
  for (int gin = 0; gin < groups; gin++) {
492 ✔
457
    for (int i_gout = 0; i_gout < matrix[gin].size(); i_gout++) {
820 ✔
458
      scattxs[gin] += std::accumulate(
492 ✔
459
        matrix[gin][i_gout].begin(), matrix[gin][i_gout].end(), 0.);
492 ✔
460
    }
461
  }
462

463
  // Build the energy transfer matrix from data in the variable matrix
464
  double_2dvec in_energy;
164 ✔
465
  in_energy.resize(groups);
164 ✔
466
  for (int gin = 0; gin < groups; gin++) {
492 ✔
467
    int num_groups = in_gmax[gin] - in_gmin[gin] + 1;
328 ✔
468
    in_energy[gin].resize(num_groups);
328 ✔
469
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
820 ✔
470
      double norm = std::accumulate(
984 ✔
471
        matrix[gin][i_gout].begin(), matrix[gin][i_gout].end(), 0.);
492 ✔
472
      in_energy[gin][i_gout] = norm;
492 ✔
473
      if (norm != 0.) {
492 ✔
474
        for (auto& n : matrix[gin][i_gout])
7,728 ✔
475
          n /= norm;
7,252 ✔
476
      }
477
    }
478
  }
479

480
  // Initialize the base class attributes
481
  ScattData::base_init(order, in_gmin, in_gmax, in_energy, in_mult);
164 ✔
482

483
  // Build the angular distribution mu values
484
  mu = tensor::linspace(-1., 1., order + 1);
164 ✔
485
  dmu = 2. / order;
164 ✔
486

487
  // Calculate f(mu) and integrate it so we can avoid rejection sampling
488
  fmu.resize(groups);
164 ✔
489
  for (int gin = 0; gin < groups; gin++) {
492 ✔
490
    int num_groups = gmax[gin] - gmin[gin] + 1;
328 ✔
491
    fmu[gin].resize(num_groups);
328 ✔
492
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
820 ✔
493
      fmu[gin][i_gout].resize(order);
492 ✔
494
      // The variable matrix contains f(mu); so directly assign it
495
      fmu[gin][i_gout] = matrix[gin][i_gout];
492 ✔
496

497
      // Integrate the histogram
498
      dist[gin][i_gout][0] = dmu * matrix[gin][i_gout][0];
492 ✔
499
      for (int imu = 1; imu < order; imu++) {
7,284 ✔
500
        dist[gin][i_gout][imu] =
6,792 ✔
501
          dmu * matrix[gin][i_gout][imu] + dist[gin][i_gout][imu - 1];
6,792 ✔
502
      }
503

504
      // Now re-normalize for integral to unity
505
      double norm = dist[gin][i_gout][order - 1];
492 ✔
506
      if (norm > 0.) {
492 ✔
507
        for (int imu = 0; imu < order; imu++) {
7,728 ✔
508
          fmu[gin][i_gout][imu] /= norm;
7,252 ✔
509
          dist[gin][i_gout][imu] /= norm;
7,252 ✔
510
        }
511
      }
512
    }
513
  }
514
}
164 ✔
515

516
//==============================================================================
517

UNCOV
518
double ScattDataHistogram::calc_f(int gin, int gout, double mu)
×
519
{
UNCOV
520
  double f;
×
UNCOV
521
  if ((gout < gmin[gin]) || (gout > gmax[gin])) {
×
522
    f = 0.;
523
  } else {
524
    // Find mu bin
525
    int i_gout = gout - gmin[gin];
×
526
    int imu;
×
UNCOV
527
    if (mu == 1.) {
×
528
      // use size -2 to have the index one before the end
UNCOV
529
      imu = this->mu.shape(0) - 2;
×
530
    } else {
531
      imu = std::floor((mu + 1.) / dmu + 1.) - 1;
×
532
    }
533

534
    f = fmu[gin][i_gout][imu];
×
535
  }
536
  return f;
×
537
}
538

539
//==============================================================================
540

541
void ScattDataHistogram::sample(
26,728 ✔
542
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed)
543
{
544
  // Sample the outgoing energy using the base-class method
545
  int i_gout;
26,728 ✔
546
  sample_energy(gin, gout, i_gout, seed);
26,728 ✔
547

548
  // Determine the outgoing cosine bin
549
  double xi = prn(seed);
26,728 ✔
550

551
  int imu;
26,728 ✔
552
  if (xi < dist[gin][i_gout][0]) {
26,728 ✔
553
    imu = 0;
554
  } else {
555
    imu =
24,336 ✔
556
      std::upper_bound(dist[gin][i_gout].begin(), dist[gin][i_gout].end(), xi) -
24,336 ✔
557
      dist[gin][i_gout].begin();
24,336 ✔
558
  }
559

560
  // Randomly select mu within the imu bin
561
  mu = prn(seed) * dmu + this->mu[imu];
26,728 !
562

563
  mu = std::clamp(mu, -1., 1.);
26,728 !
564

565
  // Update the weight to reflect neutron multiplicity
566
  wgt *= weight_mult_[gin];
26,728 ✔
567
}
26,728 ✔
568

569
//==============================================================================
570

571
tensor::Tensor<double> ScattDataHistogram::get_matrix(size_t max_order)
66 ✔
572
{
573
  // Get the sizes and initialize the data to 0
574
  size_t groups = energy.size();
66 ✔
575
  // We ignore the requested order for Histogram and Tabular representations
576
  size_t order_dim = get_order();
66 ✔
577
  tensor::Tensor<double> matrix({groups, groups, order_dim}, 0);
66 ✔
578

579
  for (int gin = 0; gin < groups; gin++) {
198 ✔
580
    for (int i_gout = 0; i_gout < energy[gin].size(); i_gout++) {
330 ✔
581
      int gout = i_gout + gmin[gin];
198 ✔
582
      for (int l = 0; l < order_dim; l++) {
3,744 ✔
583
        matrix(gin, gout, l) =
3,546 ✔
584
          scattxs[gin] * energy[gin][i_gout] * fmu[gin][i_gout][l];
3,546 ✔
585
      }
586
    }
587
  }
588
  return matrix;
66 ✔
589
}
590

591
//==============================================================================
592

593
void ScattDataHistogram::combine(
58 ✔
594
  const vector<ScattData*>& those_scatts, const vector<double>& scalars)
595
{
596
  // Find the max order in the data set and make sure we can combine the sets
597
  size_t max_order = those_scatts[0]->get_order();
58 ✔
598
  for (int i = 0; i < those_scatts.size(); i++) {
124 ✔
599
    // Lets also make sure these items are combineable
600
    ScattDataHistogram* that =
66 ✔
601
      dynamic_cast<ScattDataHistogram*>(those_scatts[i]);
66 !
602
    if (!that) {
66 !
UNCOV
603
      fatal_error("Cannot combine the ScattData objects!");
×
604
    }
605
    if (max_order != that->get_order()) {
66 !
UNCOV
606
      fatal_error("Cannot combine the ScattData objects!");
×
607
    }
608
  }
609

610
  size_t groups = those_scatts[0]->energy.size();
58 ✔
611

612
  tensor::Tensor<int> in_gmin({groups}, 0);
58 ✔
613
  tensor::Tensor<int> in_gmax({groups}, 0);
58 ✔
614
  double_3dvec sparse_scatter(groups);
58 ✔
615
  double_2dvec sparse_mult(groups);
58 ✔
616

617
  // The rest of the steps do not depend on the type of angular representation
618
  // so we use a base class method to sum up xs and create new energy and mult
619
  // matrices
620
  size_t order_dim = max_order;
58 ✔
621
  ScattData::base_combine(max_order, order_dim, those_scatts, scalars, in_gmin,
58 ✔
622
    in_gmax, sparse_mult, sparse_scatter);
623

624
  // Got everything we need, store it.
625
  init(in_gmin, in_gmax, sparse_mult, sparse_scatter);
58 ✔
626
}
174 ✔
627

628
//==============================================================================
629
// ScattDataTabular methods
630
//==============================================================================
631

632
void ScattDataTabular::init(const tensor::Tensor<int>& in_gmin,
2,630 ✔
633
  const tensor::Tensor<int>& in_gmax, const double_2dvec& in_mult,
634
  const double_3dvec& coeffs)
635
{
636
  size_t groups = coeffs.size();
2,630 ✔
637
  size_t order = coeffs[0][0].size();
2,630 ✔
638

639
  // make a copy of coeffs that we can use to both extract data and normalize
640
  double_3dvec matrix = coeffs;
2,630 ✔
641

642
  // Build the angular distribution mu values
643
  mu = tensor::linspace(-1., 1., order);
2,630 ✔
644
  dmu = 2. / (order - 1);
2,630 ✔
645

646
  // Get the scattering cross section value by integrating the distribution
647
  // over all mu points and then combining over all outgoing groups
648
  scattxs = tensor::zeros<double>({groups});
2,630 ✔
649
  for (int gin = 0; gin < groups; gin++) {
13,151 ✔
650
    for (int i_gout = 0; i_gout < matrix[gin].size(); i_gout++) {
65,575 ✔
651
      for (int imu = 1; imu < order; imu++) {
139,430 ✔
652
        scattxs[gin] +=
84,376 ✔
653
          0.5 * dmu * (matrix[gin][i_gout][imu - 1] + matrix[gin][i_gout][imu]);
84,376 ✔
654
      }
655
    }
656
  }
657

658
  // Build the energy transfer matrix from data in the variable matrix
659
  double_2dvec in_energy(groups);
2,630 ✔
660
  for (int gin = 0; gin < groups; gin++) {
13,151 ✔
661
    int num_groups = in_gmax[gin] - in_gmin[gin] + 1;
10,521 ✔
662
    in_energy[gin].resize(num_groups);
10,521 ✔
663
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
65,575 ✔
664
      double norm = 0.;
665
      for (int imu = 1; imu < order; imu++) {
139,430 ✔
666
        norm +=
84,376 ✔
667
          0.5 * dmu * (matrix[gin][i_gout][imu - 1] + matrix[gin][i_gout][imu]);
84,376 ✔
668
      }
669
      in_energy[gin][i_gout] = norm;
55,054 ✔
670
    }
671
  }
672

673
  // Initialize the base class attributes
674
  ScattData::base_init(order, in_gmin, in_gmax, in_energy, in_mult);
2,630 ✔
675

676
  // Calculate f(mu) and integrate it so we can avoid rejection sampling
677
  fmu.resize(groups);
2,630 ✔
678
  for (int gin = 0; gin < groups; gin++) {
13,151 ✔
679
    int num_groups = gmax[gin] - gmin[gin] + 1;
10,521 ✔
680
    fmu[gin].resize(num_groups);
10,521 ✔
681
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
65,575 ✔
682
      fmu[gin][i_gout].resize(order);
55,054 ✔
683
      // The variable matrix contains f(mu); so directly assign it
684
      fmu[gin][i_gout] = matrix[gin][i_gout];
55,054 ✔
685

686
      // Ensure positivity
687
      for (auto& val : fmu[gin][i_gout]) {
194,484 ✔
688
        if (val < 0.)
139,430 ✔
689
          val = 0.;
2,260 ✔
690
      }
691

692
      // Now re-normalize for numerical integration issues and to take care of
693
      // the above negative fix-up.  Also accrue the CDF
694
      double norm = 0.;
695
      for (int imu = 1; imu < order; imu++) {
139,430 ✔
696
        norm += 0.5 * dmu * (fmu[gin][i_gout][imu - 1] + fmu[gin][i_gout][imu]);
84,376 ✔
697
        // incorporate to the CDF
698
        dist[gin][i_gout][imu] = norm;
84,376 ✔
699
      }
700

701
      // now do the normalization
702
      if (norm > 0.) {
55,054 ✔
703
        for (int imu = 0; imu < order; imu++) {
165,746 ✔
704
          fmu[gin][i_gout][imu] /= norm;
120,168 ✔
705
          dist[gin][i_gout][imu] /= norm;
120,168 ✔
706
        }
707
      }
708
    }
709
  }
710
}
2,630 ✔
711

712
//==============================================================================
713

714
double ScattDataTabular::calc_f(int gin, int gout, double mu)
80 ✔
715
{
716
  double f;
80 ✔
717
  if ((gout < gmin[gin]) || (gout > gmax[gin])) {
80 !
718
    f = 0.;
719
  } else {
720
    // Find mu bin
721
    int i_gout = gout - gmin[gin];
80 ✔
722
    int imu;
80 ✔
723
    if (mu == 1.) {
80 ✔
724
      // use size -2 to have the index one before the end
725
      imu = this->mu.shape(0) - 2;
32 !
726
    } else {
727
      imu = std::floor((mu + 1.) / dmu + 1.) - 1;
64 ✔
728
    }
729

730
    double r = (mu - this->mu[imu]) / (this->mu[imu + 1] - this->mu[imu]);
80 ✔
731
    f = (1. - r) * fmu[gin][i_gout][imu] + r * fmu[gin][i_gout][imu + 1];
80 ✔
732
  }
733
  return f;
80 ✔
734
}
735

736
//==============================================================================
737

738
void ScattDataTabular::sample(
1,205,272,232 ✔
739
  int gin, int& gout, double& mu, double& wgt, uint64_t* seed)
740
{
741
  // Sample the outgoing energy using the base-class method
742
  int i_gout;
1,205,272,232 ✔
743
  sample_energy(gin, gout, i_gout, seed);
1,205,272,232 ✔
744

745
  // Determine the outgoing cosine bin
746
  int NP = this->mu.shape(0);
1,205,272,232 !
747
  double xi = prn(seed);
1,205,272,232 ✔
748

749
  double c_k = dist[gin][i_gout][0];
1,205,272,232 ✔
750
  int k;
1,205,272,232 ✔
751
  for (k = 0; k < NP - 1; k++) {
1,241,301,784 !
752
    double c_k1 = dist[gin][i_gout][k + 1];
1,241,301,784 ✔
753
    if (xi < c_k1)
1,241,301,784 ✔
754
      break;
755
    c_k = c_k1;
36,029,552 ✔
756
  }
757

758
  // Check to make sure k is <= NP - 1
759
  k = std::min(k, NP - 2);
1,205,272,232 !
760

761
  // Find the pdf values we want
762
  double p0 = fmu[gin][i_gout][k];
1,205,272,232 ✔
763
  double mu0 = this->mu[k];
1,205,272,232 ✔
764
  double p1 = fmu[gin][i_gout][k + 1];
1,205,272,232 ✔
765
  double mu1 = this->mu[k + 1];
1,205,272,232 ✔
766

767
  if (p0 == p1) {
1,205,272,232 ✔
768
    mu = mu0 + (xi - c_k) / p0;
1,203,400,888 ✔
769
  } else {
770
    double frac = (p1 - p0) / (mu1 - mu0);
1,871,344 ✔
771
    mu =
3,742,688 ✔
772
      mu0 +
1,871,344 ✔
773
      (std::sqrt(std::max(0., p0 * p0 + 2. * frac * (xi - c_k))) - p0) / frac;
3,742,688 !
774
  }
775

776
  mu = std::clamp(mu, -1., 1.);
1,205,272,232 !
777

778
  // Update the weight to reflect neutron multiplicity
779
  wgt *= weight_mult_[gin];
1,205,272,232 ✔
780
}
1,205,272,232 ✔
781

782
//==============================================================================
783

784
tensor::Tensor<double> ScattDataTabular::get_matrix(size_t max_order)
2,882 ✔
785
{
786
  // Get the sizes and initialize the data to 0
787
  size_t groups = energy.size();
2,882 ✔
788
  // We ignore the requested order for Histogram and Tabular representations
789
  size_t order_dim = get_order();
2,882 ✔
790
  tensor::Tensor<double> matrix =
2,882 ✔
791
    tensor::zeros<double>({groups, groups, order_dim});
2,882 ✔
792

793
  for (int gin = 0; gin < groups; gin++) {
13,877 ✔
794
    for (int i_gout = 0; i_gout < energy[gin].size(); i_gout++) {
66,765 ✔
795
      int gout = i_gout + gmin[gin];
55,770 ✔
796
      for (int l = 0; l < order_dim; l++) {
193,782 ✔
797
        matrix(gin, gout, l) =
138,012 ✔
798
          scattxs[gin] * energy[gin][i_gout] * fmu[gin][i_gout][l];
138,012 ✔
799
      }
800
    }
801
  }
802
  return matrix;
2,882 ✔
803
}
804

805
//==============================================================================
806

807
void ScattDataTabular::combine(
2,524 ✔
808
  const vector<ScattData*>& those_scatts, const vector<double>& scalars)
809
{
810
  // Find the max order in the data set and make sure we can combine the sets
811
  size_t max_order = those_scatts[0]->get_order();
2,524 ✔
812
  for (int i = 0; i < those_scatts.size(); i++) {
5,406 ✔
813
    // Lets also make sure these items are combineable
814
    ScattDataTabular* that = dynamic_cast<ScattDataTabular*>(those_scatts[i]);
2,882 !
815
    if (!that) {
2,882 !
UNCOV
816
      fatal_error("Cannot combine the ScattData objects!");
×
817
    }
818
    if (max_order != that->get_order()) {
2,882 !
UNCOV
819
      fatal_error("Cannot combine the ScattData objects!");
×
820
    }
821
  }
822

823
  size_t groups = those_scatts[0]->energy.size();
2,524 ✔
824

825
  tensor::Tensor<int> in_gmin({groups}, 0);
2,524 ✔
826
  tensor::Tensor<int> in_gmax({groups}, 0);
2,524 ✔
827
  double_3dvec sparse_scatter(groups);
2,524 ✔
828
  double_2dvec sparse_mult(groups);
2,524 ✔
829

830
  // The rest of the steps do not depend on the type of angular representation
831
  // so we use a base class method to sum up xs and create new energy and mult
832
  // matrices
833
  size_t order_dim = max_order;
2,524 ✔
834
  ScattData::base_combine(max_order, order_dim, those_scatts, scalars, in_gmin,
2,524 ✔
835
    in_gmax, sparse_mult, sparse_scatter);
836

837
  // Got everything we need, store it.
838
  init(in_gmin, in_gmax, sparse_mult, sparse_scatter);
2,524 ✔
839
}
7,572 ✔
840

841
//==============================================================================
842
// module-level methods
843
//==============================================================================
844

845
void convert_legendre_to_tabular(ScattDataLegendre& leg, ScattDataTabular& tab)
2,831 ✔
846
{
847
  // See if the user wants us to figure out how many points to use
848
  int n_mu = settings::legendre_to_tabular_points;
2,831 ✔
849
  if (n_mu == C_NONE) {
2,831 !
850
    // then we will use 2 pts if its P0, or the default if a higher order
851
    // TODO use an error minimization algorithm that also picks n_mu
852
    if (leg.get_order() == 0) {
2,831 ✔
853
      n_mu = 2;
854
    } else {
855
      n_mu = DEFAULT_NMU;
232 ✔
856
    }
857
  }
858

859
  tab.base_init(n_mu, leg.gmin, leg.gmax, leg.energy, leg.mult);
2,831 ✔
860
  tab.scattxs = leg.scattxs;
2,831 ✔
861

862
  // Build mu and dmu
863
  tab.mu = tensor::linspace(-1., 1., n_mu);
2,831 ✔
864
  tab.dmu = 2. / (n_mu - 1);
2,831 ✔
865

866
  // Calculate f(mu) and integrate it so we can avoid rejection sampling
867
  size_t groups = tab.energy.size();
2,831 ✔
868
  tab.fmu.resize(groups);
2,831 ✔
869
  for (int gin = 0; gin < groups; gin++) {
13,731 ✔
870
    int num_groups = tab.gmax[gin] - tab.gmin[gin] + 1;
10,900 ✔
871
    tab.fmu[gin].resize(num_groups);
10,900 ✔
872
    for (int i_gout = 0; i_gout < num_groups; i_gout++) {
66,538 ✔
873
      tab.fmu[gin][i_gout].resize(n_mu);
55,638 ✔
874
      for (int imu = 0; imu < n_mu; imu++) {
192,582 ✔
875
        tab.fmu[gin][i_gout][imu] =
136,944 ✔
876
          evaluate_legendre(leg.dist[gin][i_gout].size() - 1,
136,944 ✔
877
            leg.dist[gin][i_gout].data(), tab.mu[imu]);
136,944 ✔
878
      }
879

880
      // Ensure positivity
881
      for (auto& val : tab.fmu[gin][i_gout]) {
192,582 ✔
882
        if (val < 0.)
136,944 ✔
883
          val = 0.;
580 ✔
884
      }
885

886
      // Now re-normalize for numerical integration issues and to take care of
887
      // the above negative fix-up.  Also accrue the CDF
888
      double norm = 0.;
55,638 ✔
889
      tab.dist[gin][i_gout][0] = 0.;
55,638 ✔
890
      for (int imu = 1; imu < n_mu; imu++) {
136,944 ✔
891
        norm += 0.5 * tab.dmu *
81,306 ✔
892
                (tab.fmu[gin][i_gout][imu - 1] + tab.fmu[gin][i_gout][imu]);
81,306 ✔
893
        // incorporate to the CDF
894
        tab.dist[gin][i_gout][imu] = norm;
81,306 ✔
895
      }
896

897
      // now do the normalization
898
      if (norm > 0.) {
55,638 ✔
899
        for (int imu = 0; imu < n_mu; imu++) {
167,282 ✔
900
          tab.fmu[gin][i_gout][imu] /= norm;
119,974 ✔
901
          tab.dist[gin][i_gout][imu] /= norm;
119,974 ✔
902
        }
903
      }
904
    }
905
  }
906
}
2,831 ✔
907

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

© 2026 Coveralls, Inc