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

openmc-dev / openmc / 30942440215

04 Aug 2026 07:15PM UTC coverage: 81.481% (+0.06%) from 81.425%
30942440215

Pull #4044

github

web-flow
Merge 5f06a886a into 8202ef6fb
Pull Request #4044: Hybrid delta tracking

18963 of 27411 branches covered (69.18%)

Branch coverage included in aggregate %.

721 of 782 new or added lines in 20 files covered. (92.2%)

3 existing lines in 1 file now uncovered.

60943 of 70656 relevant lines covered (86.25%)

51279318.47 hits per line

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

85.86
/src/majorant.cpp
1
#include <fmt/core.h>
2

3
#include "openmc/capi.h"
4
#include "openmc/constants.h"
5
#include "openmc/geometry.h"
6
#include "openmc/interpolate.h"
7
#include "openmc/majorant.h"
8
#include "openmc/material.h"
9
#include "openmc/nuclide.h"
10
#include "openmc/photon.h"
11
#include "openmc/search.h"
12
#include "openmc/settings.h"
13
#include "openmc/simulation.h"
14
#include "openmc/thermal.h"
15
#include "openmc/timer.h"
16
#include "openmc/universe.h"
17

18
namespace openmc {
19

20
//==============================================================================
21
// Global variables
22
//==============================================================================
23

24
namespace data {
25
std::unique_ptr<NeutronMajorant> n_majorant;
26
std::unique_ptr<PhotonMajorant> p_majorant;
27

28
} // namespace data
29

30
//==============================================================================
31
// Majorant implementation
32
//==============================================================================
33

34
Majorant::Majorant(int i_universe) : maj_universe_(i_universe)
375!
35
{
36
  if (maj_universe_ == C_NONE || maj_universe_ >= model::universes.size()) {
375!
NEW
37
    fatal_error(fmt::format("Invalid majorant universe: {}", maj_universe_));
×
38
  }
39

40
  // First, find unique cells contained in this universe.
41
  std::unordered_set<int> unique_mat_cells;
375✔
42

43
  const auto& maj_uni = model::universes[maj_universe_];
375✔
44
  for (int i_cell : maj_uni->cells_) {
750✔
45
    const auto& uni_cell = model::cells[i_cell];
375!
46

47
    // If the cell is filled with a material, it won't have any sub-cells.
48
    if (uni_cell->type_ == Fill::MATERIAL) {
375!
NEW
49
      if (unique_mat_cells.count(i_cell) == 0) {
×
50
        unique_mat_cells.emplace(i_cell);
375!
51
      }
52
    } else {
53
      // This cell is filled with a universe or lattice. Need to get the list of
54
      // cells and cell instances.
55
      const auto contained_cells = uni_cell->get_contained_cells();
375✔
56
      for (const auto& [i_con_cell, contained_instances] : contained_cells) {
1,125!
57
        if (unique_mat_cells.count(i_con_cell) == 0) {
750!
58
          unique_mat_cells.emplace(i_con_cell);
750✔
59
        }
60
      }
61
    }
375✔
62
  }
63

64
  // Next, find all materials contained in the majorant's universe. This also
65
  // obtains the maximum density multiplier applied to that material.
66
  std::unordered_set<int> unique_materials;
375✔
67
  for (int i_cell : unique_mat_cells) {
1,125✔
68
    auto& cell = model::cells[i_cell];
750✔
69

70
    for (int instance = 0; instance < cell->n_instances(); ++instance) {
3,750✔
71
      int i_material = cell->material(instance);
3,000!
72
      // Skip over void materials.
73
      if (i_material == MATERIAL_VOID) {
3,000!
NEW
74
        continue;
×
75
      }
76

77
      // Check to see if we've found the contained material yet. If not, add
78
      // to the set of materials discovered and add to the map of density
79
      // multipliers.
80
      if (unique_materials.count(i_material) == 0) {
3,000✔
81
        unique_materials.emplace(i_material);
750✔
82
        max_density_mult_[i_material] = cell->density_mult(instance);
750✔
83
      } else {
84
        // We've found this material already. Need to take the maximum density
85
        // multiplier.
86
        max_density_mult_.at(i_material) = std::max(
4,500✔
87
          max_density_mult_.at(i_material), cell->density_mult(instance));
4,500✔
88
      }
89
    }
90
  }
91

92
  // Insert the elements from the set.
93
  contained_materials_.assign(unique_materials.begin(), unique_materials.end());
375✔
94
}
750✔
95

96
void Majorant::compute_majorant()
375✔
97
{
98
  // Fill with zeros.
99
  xs_.resize(grid_.energy.size(), 0.0);
375✔
100

101
  vector<double> material_maj_xs;
375✔
102
  for (int i_material : contained_materials_) {
1,125✔
103
    // Populate the per-material majorant cross section. We pass in
104
    // 'material_maj_xs' instead of returning a vector with the per-material
105
    // majorant every time to avoid costly reallocations and copy operations,
106
    // which have a fairly large impact on the time it takes to build the
107
    // majorant. The function 'fill_material_maj_xs(...)' is responsible for
108
    // resizing 'material_maj_xs' and populating each value at a given energy
109
    // grid point.
110
    fill_material_maj_xs(i_material, max_density_mult_.at(i_material),
750✔
111
      grid_.energy, material_maj_xs);
750✔
112

113
    // Compute the full majorant by taking the max over each material cross
114
    // section.
115
    for (int i_energy = 0; i_energy < xs_.size(); ++i_energy) {
43,094,400✔
116
      xs_[i_energy] = std::max(xs_[i_energy], material_maj_xs[i_energy]);
68,284,770✔
117
    }
118
  }
119
}
375✔
120

121
void Majorant::post_process_grid()
375✔
122
{
123
  // Fetch the minimum and maximum transport energies from the superclass.
124
  const double E_min = min_transport_energy();
375✔
125
  const double E_max = max_transport_energy();
375✔
126

127
  std::sort(grid_.energy.begin(), grid_.energy.end());
375✔
128
  auto unique_end = std::unique(grid_.energy.begin(), grid_.energy.end());
375✔
129
  grid_.energy.resize(std::distance(grid_.energy.begin(), unique_end));
375✔
130

131
  // Remove all values below the minimum neutron energy.
132
  auto min_it = grid_.energy.begin();
375✔
133
  while (*min_it < E_min) {
375✔
134
    min_it++;
318,000✔
135
  }
136
  grid_.energy.erase(grid_.energy.begin(), min_it + 1);
375✔
137

138
  // Insert the minimum neutron energy at the beginning.
139
  grid_.energy.insert(grid_.energy.begin(), E_min);
375✔
140

141
  // Remove all values above the maximum neutron energy.
142
  auto max_it = --grid_.energy.end();
375✔
143
  while (*max_it > E_max) {
375✔
144
    max_it--;
63,330✔
145
  }
146
  grid_.energy.erase(max_it - 1, grid_.energy.end());
375✔
147

148
  // Insert the maximum neutron energy at the end.
149
  grid_.energy.insert(grid_.energy.end(), E_max);
375✔
150
}
375✔
151

152
//==============================================================================
153
// NeutronMajorant implementation
154
//==============================================================================
155

156
NeutronMajorant::NeutronMajorant(int i_universe) : Majorant(i_universe) {}
270✔
157

158
double NeutronMajorant::calculate_neutron_xs(double energy) const
39,635,068✔
159
{
160
  const int i_grid = get_i_grid(energy, grid_);
39,635,068✔
161
  return interpolate_lin_lin(grid_.energy[i_grid], grid_.energy[i_grid + 1],
39,635,068✔
162
    xs_[i_grid], xs_[i_grid + 1], energy);
39,635,068✔
163
}
164

165
void NeutronMajorant::compute_unionized_grid()
270✔
166
{
167
  // This function generates a unionized cross section grid between smooth cross
168
  // sections and URR probability table grids.
169
  std::unordered_set<int> processed_nuclides;
270✔
170
  for (int i_mat : contained_materials_) {
810✔
171
    const auto& mat = model::materials[i_mat];
540✔
172
    for (auto i_nuclide : mat->nuclide_) {
1,620✔
173
      // Only unionize nuclides we haven't checked yet.
174
      if (processed_nuclides.count(i_nuclide) > 0) {
1,080✔
175
        continue;
270✔
176
      }
177

178
      const auto& nuclide = data::nuclides[i_nuclide];
810✔
179
      // ======================================================================
180
      // Unionizing the URR energy grids.
181
      if (nuclide->urr_present_ && settings::urr_ptables_on) {
810!
182
        for (const auto& nuc_urr : nuclide->urr_data_) {
540✔
183
          grid_.energy.insert(
270✔
184
            grid_.energy.end(), nuc_urr.energy_.begin(), nuc_urr.energy_.end());
270✔
185
        }
186
      }
187

188
      // ======================================================================
189
      // Unionize the smooth cross section energy grids.
190
      for (const auto& nuc_grid : nuclide->grid_) {
1,620✔
191
        grid_.energy.insert(
810✔
192
          grid_.energy.end(), nuc_grid.energy.begin(), nuc_grid.energy.end());
810✔
193
      }
194

195
      processed_nuclides.insert(i_nuclide);
810✔
196
    }
197
  }
198

199
  // Post-process the energy grid now that all points from nuclides are
200
  // included. This sorts the energy points, removes duplicates, and removes all
201
  // energies exceeding neutron transport bounds.
202
  post_process_grid();
270✔
203

204
  // Initialize the grid for fast lookups. This only applies to neutrons.
205
  grid_.init();
270✔
206
}
270✔
207

208
void NeutronMajorant::fill_material_maj_xs(int i_material,
540✔
209
  double max_density_mult, const vector<double>& to_grid,
210
  vector<double>& mat_maj) const
211
{
212
  const auto& mat = *model::materials[i_material];
540✔
213

214
  mat_maj.resize(to_grid.size());
540✔
215

216
#pragma omp parallel for
324✔
217
  for (int i_energy = 0; i_energy < to_grid.size(); ++i_energy) {
17,118,648✔
218
    mat_maj[i_energy] = 0.0;
17,118,432✔
219
    const double union_energy = to_grid[i_energy];
17,118,432✔
220

221
    int mat_sab_table_idx = 0;
17,118,432✔
222
    bool check_sab = (mat.thermal_tables_.size() > 0);
17,118,432✔
223

224
    for (int i = 0; i < mat.nuclide_.size(); ++i) {
51,355,296✔
225
      // ======================================================================
226
      // CHECK FOR S(A,B) TABLE
227
      int i_sab = C_NONE;
34,236,864✔
228
      double sab_frac = 0.0;
34,236,864✔
229

230
      // Check if this nuclide matches one of the S(a,b) tables specified.
231
      // This relies on thermal_tables_ being sorted by .index_nuclide
232
      if (check_sab) {
34,236,864✔
233
        const auto& sab {mat.thermal_tables_[mat_sab_table_idx]};
8,559,216!
234
        if (i == sab.index_nuclide) {
8,559,216!
235
          // Get index in sab_tables
236
          i_sab = sab.index_table;
8,559,216✔
237
          sab_frac = sab.fraction;
8,559,216✔
238

239
          // If particle energy is greater than the highest energy for the
240
          // S(a,b) table, then don't use the S(a,b) table
241
          if (union_energy > data::thermal_scatt[i_sab]->energy_max_) {
8,559,216✔
242
            i_sab = C_NONE;
8,415,144✔
243
          }
244

245
          // Increment position in thermal_tables_
246
          ++mat_sab_table_idx;
8,559,216✔
247

248
          // Don't check for S(a,b) tables if there are no more left
249
          if (mat_sab_table_idx == mat.thermal_tables_.size()) {
8,559,216!
250
            check_sab = false;
8,559,216✔
251
          }
252
        }
253
      }
254

255
      // ======================================================================
256
      // Compute the maximum smooth total cross section. This is either the
257
      // free gas cross section at energies larger than the Bragg edge, or
258
      // the bound cross section in the thermal scattering region.
259
      double micro_smooth_tot_xs = 0.0;
34,236,864✔
260
      if (i_sab >= 0) {
34,236,864✔
261
        // Thermal scattering cross sections using S(a,b) tables.
262
        micro_smooth_tot_xs = calculate_max_sab_micro_tot_xs(
144,072✔
263
          mat.nuclide_[i], i_sab, sab_frac, union_energy);
144,072✔
264
      } else {
265
        // Free gas smooth cross section
266
        micro_smooth_tot_xs =
34,092,792✔
267
          calculate_max_smooth_micro_xs(mat.nuclide_[i], union_energy);
34,092,792✔
268
      }
269

270
      // ======================================================================
271
      // Compute the URR cross section. This shouldn't intersect with the
272
      // S(a,b) cross section.
273
      double micro_urr_xs = calculate_max_urr_micro_xs(
34,236,864✔
274
        mat.nuclide_[i], union_energy, micro_smooth_tot_xs);
34,236,864✔
275

276
      // ======================================================================
277
      // Accumulate the macroscopic cross section.
278
      mat_maj[i_energy] += std::max(micro_smooth_tot_xs, micro_urr_xs) *
34,247,232✔
279
                           mat.atom_density(i, max_density_mult);
34,236,864✔
280
    }
281
  }
282
}
540✔
283

284
double NeutronMajorant::calculate_max_smooth_micro_xs(
85,231,980✔
285
  int i_nuclide, double energy) const
286
{
287
  const auto& nuc = *data::nuclides[i_nuclide];
85,231,980✔
288

289
  double max_smooth_tot_xs = 0.0;
85,231,980✔
290
  for (int i_temp = 0; i_temp < nuc.kTs_.size(); ++i_temp) {
170,463,960✔
291
    const auto& nuc_grid = nuc.grid_[i_temp];
85,231,980✔
292
    int i_grid = get_i_grid(energy, nuc_grid);
85,231,980✔
293
    auto total = nuc.xs_[i_temp].slice(openmc::tensor::all, 0);
85,231,980✔
294
    double xs = interpolate_lin_lin(nuc_grid.energy[i_grid],
85,231,980✔
295
      nuc_grid.energy[i_grid + 1], total[i_grid], total[i_grid + 1], energy);
85,231,980!
296
    max_smooth_tot_xs = std::max(max_smooth_tot_xs, xs);
170,463,960!
297
  }
85,231,980✔
298

299
  return max_smooth_tot_xs;
85,231,980✔
300
}
301

302
double NeutronMajorant::calculate_max_urr_micro_xs(
85,592,160✔
303
  int i_nuclide, double energy, double smooth_xs) const
304
{
305
  const auto& nuc = *data::nuclides[i_nuclide];
85,592,160✔
306
  if (!nuc.urr_present_) {
85,592,160✔
307
    return 0.0;
308
  }
309

310
  double max_urr_xs = 0.0;
21,398,040✔
311
  for (const auto& urr : nuc.urr_data_) {
42,796,080✔
312
    if (!(urr.energy_in_bounds(energy - MAJORANT_URR_TOL) ||
21,398,040✔
313
          urr.energy_in_bounds(energy + MAJORANT_URR_TOL))) {
21,372,390✔
314
      continue;
21,372,120✔
315
    }
316

317
    int i_energy;
25,920✔
318
    if (energy <= urr.energy_.front()) {
25,920✔
319
      i_energy = 0;
320
    } else if (energy >= urr.energy_.back()) {
25,650✔
321
      i_energy = urr.energy_.size() - 2;
270✔
322
    } else {
323
      i_energy =
25,380✔
324
        lower_bound_index(&urr.energy_.front(), &urr.energy_.back(), energy);
25,380✔
325
    }
326

327
    // Find the maximum URR cross sections for the two bounding energy points.
328
    double max_urr_xs_E0 = 0.0;
25,920✔
329
    double max_urr_xs_E1 = 0.0;
25,920✔
330
    for (int i_cdf = 0; i_cdf < urr.n_cdf(); ++i_cdf) {
1,088,640!
331
      max_urr_xs_E0 =
1,036,800✔
332
        std::max(max_urr_xs_E0, urr.xs_values_(i_energy, i_cdf).total);
518,400!
333
      max_urr_xs_E1 =
1,036,800✔
334
        std::max(max_urr_xs_E1, urr.xs_values_(i_energy + 1, i_cdf).total);
1,036,800!
335
    }
336
    // Handle the rare case where the points could be negative.
337
    max_urr_xs_E0 = std::max(max_urr_xs_E0, 0.0);
25,920!
338
    max_urr_xs_E1 = std::max(max_urr_xs_E1, 0.0);
25,920!
339

340
    // Interpolate the bounding energy points.
341
    double interp_urr_xs = 0.0;
25,920✔
342
    if (urr.interp_ == Interpolation::lin_lin) {
25,920!
343
      interp_urr_xs = interpolate_lin_lin(urr.energy_[i_energy],
25,920✔
344
        urr.energy_[i_energy + 1], max_urr_xs_E0, max_urr_xs_E1, energy);
25,920✔
NEW
345
    } else if (urr.interp_ == Interpolation::log_log) {
×
NEW
346
      interp_urr_xs = interpolate_log_log(urr.energy_[i_energy],
×
NEW
347
        urr.energy_[i_energy + 1], max_urr_xs_E0, max_urr_xs_E1, energy);
×
348
    }
349

350
    // Multiply by the smooth cross section (after interpolation) if required.
351
    if (urr.multiply_smooth_) {
25,920!
352
      interp_urr_xs *= smooth_xs;
25,920✔
353
    }
354

355
    max_urr_xs = std::max(max_urr_xs, interp_urr_xs);
51,840!
356
  }
357

358
  return max_urr_xs;
21,398,040✔
359
}
360

361
double NeutronMajorant::calculate_max_sab_micro_tot_xs(
360,180✔
362
  int i_nuclide, int i_sab, double sab_frac, double energy) const
363
{
364
  const auto& nuc = *data::nuclides[i_nuclide];
360,180✔
365
  const auto& thermal = *data::thermal_scatt[i_sab];
360,180✔
366

367
  // Loop over the nuclide's temperature grid to ensure we're consistent.
368
  double max_sab_total = 0.0;
360,180✔
369
  for (int i_nuc_temp = 0; i_nuc_temp < nuc.kTs_.size(); ++i_nuc_temp) {
720,360✔
370
    double nuc_kT = nuc.kTs_[i_nuc_temp] * nuc.kTs_[i_nuc_temp];
360,180!
371

372
    // Compute the elastic and inelastic scattering cross sections. The S(a,b)
373
    // cross sections are interpolated to match the nuclide temperature point.
374
    double thermal_elastic;
360,180✔
375
    double thermal_inelastic;
360,180✔
376
    const auto& tkTs = thermal.kTs_;
360,180✔
377
    if (tkTs.size() > 1) {
360,180!
NEW
378
      if (nuc_kT < tkTs.front()) {
×
NEW
379
        thermal.data_.front().calculate_xs(
×
380
          energy, &thermal_elastic, &thermal_inelastic);
NEW
381
      } else if (nuc_kT > tkTs.back()) {
×
NEW
382
        thermal.data_.back().calculate_xs(
×
383
          energy, &thermal_elastic, &thermal_inelastic);
384
      } else {
385
        // Find temperatures that bound the actual temperature
386
        int i_sab_temp = 0;
387
        while (
NEW
388
          tkTs[i_sab_temp + 1] < nuc_kT && i_sab_temp + 1 < tkTs.size() - 1) {
×
389
          ++i_sab_temp;
390
        }
391
        // Interpolate the scattering cross sections to the nuclide temperature
392
        // grid point.
NEW
393
        double T0_elastic, T1_elastic, T0_inelastic, T1_inelastic;
×
NEW
394
        thermal.data_[i_sab_temp].calculate_xs(
×
395
          energy, &T0_elastic, &T0_inelastic);
NEW
396
        thermal.data_[i_sab_temp + 1].calculate_xs(
×
397
          energy, &T1_elastic, &T1_inelastic);
NEW
398
        thermal_elastic = interpolate_lin_lin(tkTs[i_sab_temp],
×
NEW
399
          tkTs[i_sab_temp + 1], T0_elastic, T1_elastic, nuc_kT);
×
NEW
400
        thermal_inelastic = interpolate_lin_lin(tkTs[i_sab_temp],
×
NEW
401
          tkTs[i_sab_temp + 1], T0_inelastic, T1_inelastic, nuc_kT);
×
402
      }
403
    } else {
404
      thermal.data_[0].calculate_xs(
360,180✔
405
        energy, &thermal_elastic, &thermal_inelastic);
406
    }
407

408
    // Compute the free gas total and elastic cross sections interpolated on the
409
    // majorant grid.
410
    const auto& nuc_grid = nuc.grid_[i_nuc_temp];
360,180✔
411
    int i_grid = get_i_grid(energy, nuc_grid);
360,180✔
412
    const auto& free_tot = nuc.xs_[i_nuc_temp].slice(openmc::tensor::all, 0);
360,180✔
413
    const auto& free_ela = nuc.reactions_[0]->xs_[i_nuc_temp].value;
360,180!
414
    double tot_xs =
360,180✔
415
      interpolate_lin_lin(nuc_grid.energy[i_grid], nuc_grid.energy[i_grid + 1],
360,180!
416
        free_tot[i_grid], free_tot[i_grid + 1], energy);
360,180!
417
    double ela_xs =
360,180✔
418
      interpolate_lin_lin(nuc_grid.energy[i_grid], nuc_grid.energy[i_grid + 1],
360,180✔
419
        free_ela[i_grid], free_ela[i_grid + 1], energy);
360,180✔
420

421
    double thermal_xs = sab_frac * (thermal_elastic + thermal_inelastic);
360,180✔
422
    double sab_corrected_total = tot_xs + thermal_xs - sab_frac * ela_xs;
360,180✔
423
    max_sab_total = std::max(sab_corrected_total, max_sab_total);
360,180!
424
  }
360,180✔
425

426
  return max_sab_total;
360,180✔
427
}
428

429
int NeutronMajorant::get_i_grid(
125,227,228✔
430
  double energy, const Nuclide::EnergyGrid& grid) const
431
{
432
  // Find energy index on energy grid
433
  int i_log_union =
125,227,228✔
434
    std::log(energy / data::energy_min[I_NEUTRON]) / simulation::log_spacing;
125,227,228✔
435

436
  int i_grid;
125,227,228✔
437
  if (energy <= grid.energy.front()) {
125,227,228✔
438
    i_grid = 0;
439
  } else if (energy >= grid.energy.back()) {
125,226,148✔
440
    i_grid = grid.energy.size() - 2;
540✔
441
  } else {
442
    // Determine bounding indices based on which equal log-spaced
443
    // interval the energy is in
444
    int i_low = grid.grid_index[i_log_union];
125,225,608!
445
    int i_high = grid.grid_index[i_log_union + 1] + 1;
125,225,608✔
446

447
    // This catches the very rare case where floating point comparisons fail.
448
    if (i_low >= grid.energy.size() || i_high >= grid.energy.size()) {
125,225,608!
NEW
449
      i_grid = grid.energy.size() - 2;
×
450
    } else {
451
      // Perform binary search over reduced range
452
      i_grid = i_low + lower_bound_index(
125,225,608✔
453
                         &grid.energy[i_low], &grid.energy[i_high], energy);
125,225,608✔
454
    }
455
  }
456

457
  // check for rare case where two energy points are the same
458
  if (grid.energy[i_grid] == grid.energy[i_grid + 1])
125,227,228!
NEW
459
    ++i_grid;
×
460

461
  return i_grid;
125,227,228✔
462
}
463

464
//==============================================================================
465
// PhotonMajorant implementation
466
//==============================================================================
467

468
PhotonMajorant::PhotonMajorant(int i_universe) : Majorant(i_universe) {}
105✔
469

470
void PhotonMajorant::compute_unionized_grid()
105✔
471
{
472
  // This function generates a unionized cross section grid for all elements.
473
  std::unordered_set<int> processed_elements;
105✔
474
  for (int i_mat : contained_materials_) {
315✔
475
    const auto& mat = model::materials[i_mat];
210✔
476
    for (int i = 0; i < mat->nuclide_.size(); ++i) {
630✔
477
      // Only unionize elements we haven't checked yet.
478
      if (processed_elements.count(mat->element_[i]) > 0) {
420✔
479
        continue;
105✔
480
      }
481

482
      const auto& element = data::elements[mat->element_[i]];
315✔
483
      grid_.energy.insert(
315✔
484
        grid_.energy.end(), element->energy_.begin(), element->energy_.end());
315✔
485

486
      processed_elements.insert(mat->element_[i]);
315✔
487
    }
488
  }
489

490
  // Post-process the energy grid now that all points from photon interactions
491
  // are included. This sorts the energy points, removes duplicates, and removes
492
  // all energies exceeding photon transport bounds.
493
  post_process_grid();
105✔
494
}
105✔
495

496
double PhotonMajorant::calculate_photon_xs(double energy) const
45,616,065✔
497
{
498
  double log_energy = std::log(energy);
45,616,065✔
499
  int i_grid = get_i_grid<vector<double>>(log_energy, grid_.energy);
45,616,065✔
500

501
  // calculate interpolation factor
502
  double f = (log_energy - grid_.energy[i_grid]) /
45,616,065✔
503
             (grid_.energy[i_grid + 1] - grid_.energy[i_grid]);
45,616,065✔
504

505
  // interpolate the total cross section
506
  return std::exp(xs_[i_grid] + f * (xs_[i_grid + 1] - xs_[i_grid]));
45,616,065✔
507
}
508

509
void PhotonMajorant::fill_material_maj_xs(int i_material,
210✔
510
  double max_density_mult, const vector<double>& to_grid,
511
  vector<double>& mat_maj) const
512
{
513
  const auto& mat = *model::materials[i_material];
210✔
514

515
  mat_maj.resize(to_grid.size());
210✔
516

517
#pragma omp parallel for
126✔
518
  for (int i_energy = 0; i_energy < to_grid.size(); ++i_energy) {
119,112✔
519
    mat_maj[i_energy] = 0.0;
119,028✔
520
    const double union_log_energy = to_grid[i_energy];
119,028✔
521

522
    for (int i = 0; i < mat.nuclide_.size(); ++i) {
357,084✔
523
      const int i_element = mat.element_[i];
238,056✔
524

525
      mat_maj[i_energy] +=
476,112✔
526
        calculate_elem_micro_tot_xs(i_element, union_log_energy) *
238,056✔
527
        mat.atom_density(i, max_density_mult);
238,056✔
528
    }
529
    mat_maj[i_energy] = std::log(mat_maj[i_energy]);
119,028✔
530
  }
531
}
210✔
532

533
double PhotonMajorant::calculate_elem_micro_tot_xs(
595,140✔
534
  int i_element, double log_energy) const
535
{
536
  const auto& elem = *data::elements[i_element];
595,140✔
537
  int i_grid = get_i_grid<tensor::Tensor<double>>(log_energy, elem.energy_);
595,140✔
538

539
  // calculate interpolation factor
540
  double f = (log_energy - elem.energy_(i_grid)) /
595,140✔
541
             (elem.energy_(i_grid + 1) - elem.energy_(i_grid));
595,140✔
542

543
  // Calculate microscopic coherent cross section
544
  double coherent =
595,140✔
545
    std::exp(elem.coherent_(i_grid) +
1,190,280✔
546
             f * (elem.coherent_(i_grid + 1) - elem.coherent_(i_grid)));
595,140✔
547

548
  // Calculate microscopic incoherent cross section
549
  double incoherent =
595,140✔
550
    std::exp(elem.incoherent_(i_grid) +
595,140✔
551
             f * (elem.incoherent_(i_grid + 1) - elem.incoherent_(i_grid)));
595,140✔
552

553
  // Calculate microscopic photoelectric cross section
554
  double photoelectric = 0.0;
595,140✔
555
  tensor::View<const double> xs_lower = elem.cross_sections_.slice(i_grid);
595,140✔
556
  tensor::View<const double> xs_upper = elem.cross_sections_.slice(i_grid + 1);
595,140✔
557

558
  for (int i = 0; i < xs_upper.size(); ++i)
12,497,940✔
559
    if (xs_lower(i) != 0)
5,653,830✔
560
      photoelectric += std::exp(xs_lower(i) + f * (xs_upper(i) - xs_lower(i)));
5,299,140✔
561

562
  // Calculate microscopic pair production cross section
563
  double pair_production =
595,140✔
564
    std::exp(elem.pair_production_total_(i_grid) +
595,140✔
565
             f * (elem.pair_production_total_(i_grid + 1) -
595,140✔
566
                   elem.pair_production_total_(i_grid)));
595,140✔
567

568
  // Calculate microscopic total cross section
569
  return coherent + incoherent + photoelectric + pair_production;
595,140✔
570
}
1,190,280✔
571

572
//! Create a majorant cross section for photons or neutrons.
573
void create_majorants()
270✔
574
{
575
  simulation::time_build_majorant.start();
270✔
576

577
  write_message("Constructing a neutron majorant cross section");
270✔
578
  data::n_majorant = std::make_unique<NeutronMajorant>(model::root_universe);
270✔
579
  data::n_majorant->compute_unionized_grid();
270✔
580
  data::n_majorant->compute_majorant();
270✔
581

582
  if (settings::photon_transport) {
270✔
583
    write_message("Constructing a photon majorant cross section");
105✔
584
    data::p_majorant = std::make_unique<PhotonMajorant>(model::root_universe);
105✔
585
    data::p_majorant->compute_unionized_grid();
105✔
586
    data::p_majorant->compute_majorant();
105✔
587
  }
588

589
  simulation::time_build_majorant.stop();
270✔
590
}
270✔
591

592
//! Reset the photon and neutron majorant cross sections.
593
void reset_majorants()
8,292✔
594
{
595
  openmc::data::n_majorant.reset(nullptr);
8,292✔
596
  openmc::data::p_majorant.reset(nullptr);
8,292✔
597
}
8,292✔
598
} // 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