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

openmc-dev / openmc / 27475539848

13 Jun 2026 06:37PM UTC coverage: 81.299% (+0.02%) from 81.281%
27475539848

Pull #3971

github

web-flow
Merge db9c79345 into 02eb999af
Pull Request #3971: Delta tracking

18487 of 26807 branches covered (68.96%)

Branch coverage included in aggregate %.

595 of 648 new or added lines in 20 files covered. (91.82%)

33 existing lines in 3 files now uncovered.

59806 of 69495 relevant lines covered (86.06%)

49460468.05 hits per line

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

81.7
/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/majorant.h"
7
#include "openmc/material.h"
8
#include "openmc/nuclide.h"
9
#include "openmc/photon.h"
10
#include "openmc/search.h"
11
#include "openmc/settings.h"
12
#include "openmc/simulation.h"
13
#include "openmc/thermal.h"
14
#include "openmc/timer.h"
15
#include "openmc/universe.h"
16

17
namespace openmc {
18

19
//==============================================================================
20
// Global variables
21
//==============================================================================
22

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

27
} // namespace data
28

29
//==============================================================================
30
// Majorant implementation
31
//==============================================================================
32

33
Majorant::Majorant(int i_universe) : maj_universe_(i_universe)
195!
34
{
35
  // Find all materials contained in the majorant's universe. This also obtains
36
  // the maximum density multiplier applied to that material.
37
  std::set<int> unique_materials;
195!
38
  if (maj_universe_ == C_NONE || maj_universe_ >= model::universes.size()) {
195!
NEW
39
    fatal_error(fmt::format("Invalid majorant universe: {}", maj_universe_));
×
40
  }
41

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

46
    // If the cell is filled with a material, it won't have any sub-cells.
47
    if (uni_cell->type_ == Fill::MATERIAL) {
195!
48
      // Loop over instances. TODO: confirm if this is unecessary and use 0
49
      // instead?
NEW
50
      for (int instance = 0; instance < uni_cell->n_instances(); ++instance) {
×
NEW
51
        int i_material = uni_cell->material(instance);
×
52

53
        // Check to see if we've found the contained material yet. If not, add
54
        // to the set of materials discovered and add to the map of density
55
        // multipliers.
NEW
56
        if (unique_materials.count(i_material) == 0) {
×
NEW
57
          unique_materials.emplace(i_material);
×
NEW
58
          max_density_mult_[i_material] = uni_cell->density_mult(instance);
×
59
        } else {
60
          // We've found this material already. Need to take the maximum density
61
          // multiplier.
NEW
62
          max_density_mult_.at(i_material) = std::max(
×
NEW
63
            max_density_mult_.at(i_material), uni_cell->density_mult(instance));
×
64
        }
65
      }
66
    } else {
67
      // This cell is filled with a universe or lattice. Need to get the list of
68
      // cells and cell instances.
69
      const auto contained_cells = uni_cell->get_contained_cells();
195✔
70
      for (const auto& [i_con_cell, contained_instances] : contained_cells) {
585✔
71
        const auto& contained_cell = model::cells[i_con_cell];
390✔
72

73
        // Loop over contained cell instances.
74
        for (auto instance : contained_instances) {
1,950✔
75
          // Check to see if we've found the contained material instance yet. If
76
          // not, add to the set of materials discovered and add to the map of
77
          // density multipliers.
78
          int i_material = contained_cell->material(instance);
1,560!
79
          if (unique_materials.count(i_material) == 0) {
1,560✔
80
            unique_materials.emplace(i_material);
390✔
81
            max_density_mult_[i_material] =
390✔
82
              contained_cell->density_mult(instance);
390✔
83
          } else {
84
            // We've found this material already. Need to take the maximum
85
            // density multiplier for the contained instance.
86
            max_density_mult_.at(i_material) =
1,170✔
87
              std::max(max_density_mult_.at(i_material),
1,215✔
88
                contained_cell->density_mult(instance));
2,340✔
89
          }
90
        }
91
      }
92
    }
195✔
93
  }
94

95
  // Clear the contained materials vector and insert the elements from the set.
96
  for (auto i_mat : unique_materials) {
585✔
97
    contained_materials_.push_back(i_mat);
390✔
98
  }
99
}
195✔
100

101
void Majorant::compute_majorant()
195✔
102
{
103
  // Fill with zeros.
104
  xs_.resize(grid_.energy.size(), 0.0);
195✔
105

106
  std::vector<double> material_maj_xs;
195✔
107
  for (int i_material : contained_materials_) {
585✔
108
    // Populate the per-material majorant cross section.
109
    fill_material_maj_xs(i_material, max_density_mult_.at(i_material),
390✔
110
      grid_.energy, material_maj_xs);
390✔
111

112
    // Compute the full majorant by taking the max over each material cross
113
    // section.
114
    for (int i_energy = 0; i_energy < xs_.size(); ++i_energy) {
23,903,520✔
115
      xs_[i_energy] = std::max(xs_[i_energy], material_maj_xs[i_energy]);
45,348,135✔
116
    }
117
  }
118
}
195✔
119

120
void Majorant::post_process_grid(
195✔
121
  int particle_type, Nuclide::EnergyGrid& grid) const
122
{
123
  // Photons use a logarithmic energy grid.
124
  double E_min = data::energy_min[particle_type];
195✔
125
  double E_max = data::energy_max[particle_type];
195✔
126
  if (particle_type == ParticleType::photon().transport_index()) {
195✔
127
    E_min = std::log(E_min);
45✔
128
    E_max = std::log(E_max);
45✔
129
  }
130

131
  std::sort(grid.energy.begin(), grid.energy.end());
195✔
132
  auto unique_end = std::unique(grid.energy.begin(), grid.energy.end());
195✔
133
  grid.energy.resize(std::distance(grid.energy.begin(), unique_end));
195✔
134

135
  // Remove all values below the minimum neutron energy.
136
  auto min_it = grid.energy.begin();
195✔
137
  while (*min_it < E_min) {
195✔
138
    min_it++;
136,320✔
139
  }
140
  grid.energy.erase(grid.energy.begin(), min_it + 1);
195✔
141

142
  // Insert the minimum neutron energy at the beginning.
143
  grid.energy.insert(grid.energy.begin(), E_min);
195✔
144

145
  // Remove all values above the maximum neutron energy.
146
  auto max_it = --grid.energy.end();
195✔
147
  while (*max_it > E_max) {
195✔
148
    max_it--;
34,410✔
149
  }
150
  grid.energy.erase(max_it - 1, grid.energy.end());
195✔
151

152
  // Insert the maximum neutron energy at the end.
153
  grid.energy.insert(grid.energy.end(), E_max);
195✔
154
}
195✔
155

156
//==============================================================================
157
// NeutronMajorant implementation
158
//==============================================================================
159

160
NeutronMajorant::NeutronMajorant(int i_universe) : Majorant(i_universe) {}
150✔
161

162
double NeutronMajorant::calculate_neutron_xs(double energy) const
20,620,806✔
163
{
164
  const int i_grid = get_i_grid(energy, grid_);
20,620,806✔
165
  return interpolate_lin_1D(grid_.energy[i_grid], grid_.energy[i_grid + 1],
20,620,806✔
166
    xs_[i_grid], xs_[i_grid + 1], energy);
20,620,806✔
167
}
168

169
void NeutronMajorant::compute_unionized_grid()
150✔
170
{
171
  // This function generates a unionized cross section grid between smooth cross
172
  // sections and URR probability table grids.
173
  std::set<int> processed_nuclides;
150✔
174
  for (int i_mat : contained_materials_) {
450✔
175
    const auto& mat = model::materials[i_mat];
300✔
176
    for (auto i_nuclide : mat->nuclide_) {
900✔
177
      // Only unionize nuclides we haven't checked yet.
178
      if (processed_nuclides.count(i_nuclide) > 0) {
600✔
179
        continue;
150✔
180
      }
181

182
      const auto& nuclide = data::nuclides[i_nuclide];
450✔
183
      // ======================================================================
184
      // Unionizing the URR energy grids.
185
      if (nuclide->urr_present_ && settings::urr_ptables_on) {
450!
186
        for (const auto& nuc_urr : nuclide->urr_data_) {
300✔
187
          grid_.energy.insert(
150✔
188
            grid_.energy.end(), nuc_urr.energy_.begin(), nuc_urr.energy_.end());
150✔
189
        }
190
      }
191

192
      // ======================================================================
193
      // Unionize the smooth cross section energy grids.
194
      for (const auto& nuc_grid : nuclide->grid_) {
900✔
195
        grid_.energy.insert(
450✔
196
          grid_.energy.end(), nuc_grid.energy.begin(), nuc_grid.energy.end());
450✔
197
      }
198

199
      processed_nuclides.insert(i_nuclide);
450✔
200
    }
201
  }
202

203
  // Post-process the energy grid now that all points from nuclides are
204
  // included. This sorts the energy points, removes duplicates, and removes all
205
  // energies exceeding neutron transport bounds.
206
  post_process_grid(i_neutron_, grid_);
150✔
207

208
  // Initialize the grid for fast lookups. This only applies to neutrons.
209
  grid_.init();
150✔
210
}
150✔
211

212
void NeutronMajorant::fill_material_maj_xs(int i_material,
300✔
213
  double max_density_mult, const std::vector<double>& to_grid,
214
  std::vector<double>& mat_maj) const
215
{
216
  const auto& mat = *model::materials[i_material];
300✔
217

218
  mat_maj.resize(to_grid.size());
300✔
219
  for (int i_energy = 0; i_energy < to_grid.size(); ++i_energy) {
23,775,900✔
220
    mat_maj[i_energy] = 0.0;
23,775,600✔
221
    const double union_energy = to_grid[i_energy];
23,775,600✔
222

223
    int mat_sab_table_idx = 0;
23,775,600✔
224
    bool check_sab = (mat.thermal_tables_.size() > 0);
23,775,600✔
225

226
    for (int i = 0; i < mat.nuclide_.size(); ++i) {
71,326,800✔
227
      // ======================================================================
228
      // CHECK FOR S(A,B) TABLE
229
      int i_sab = C_NONE;
47,551,200✔
230
      double sab_frac = 0.0;
47,551,200✔
231

232
      // Check if this nuclide matches one of the S(a,b) tables specified.
233
      // This relies on thermal_tables_ being sorted by .index_nuclide
234
      if (check_sab) {
47,551,200✔
235
        const auto& sab {mat.thermal_tables_[mat_sab_table_idx]};
11,887,800!
236
        if (i == sab.index_nuclide) {
11,887,800!
237
          // Get index in sab_tables
238
          i_sab = sab.index_table;
11,887,800✔
239
          sab_frac = sab.fraction;
11,887,800✔
240

241
          // If particle energy is greater than the highest energy for the
242
          // S(a,b) table, then don't use the S(a,b) table
243
          if (union_energy > data::thermal_scatt[i_sab]->energy_max_) {
11,887,800✔
244
            i_sab = C_NONE;
11,687,700✔
245
          }
246

247
          // Increment position in thermal_tables_
248
          ++mat_sab_table_idx;
11,887,800✔
249

250
          // Don't check for S(a,b) tables if there are no more left
251
          if (mat_sab_table_idx == mat.thermal_tables_.size()) {
11,887,800!
252
            check_sab = false;
11,887,800✔
253
          }
254
        }
255
      }
256

257
      // ======================================================================
258
      // Compute the maximum smooth total cross section. This is either the
259
      // free gas cross section at energies larger than the Bragg edge, or
260
      // the bound cross section in the thermal scattering region.
261
      double micro_smooth_tot_xs = 0.0;
47,551,200✔
262
      if (i_sab >= 0) {
47,551,200✔
263
        // Thermal scattering cross sections using S(a,b) tables.
264
        micro_smooth_tot_xs = calculate_max_sab_tot_xs(
200,100✔
265
          mat.nuclide_[i], i_sab, sab_frac, union_energy);
200,100✔
266
      } else {
267
        // Free gas smooth cross section
268
        micro_smooth_tot_xs =
47,351,100✔
269
          calculate_max_smooth_xs(mat.nuclide_[i], union_energy);
47,351,100✔
270
      }
271

272
      // ======================================================================
273
      // Compute the URR cross section. This shouldn't intersect with the
274
      // S(a,b) cross section.
275
      double micro_urr_xs = calculate_max_urr_xs(
47,551,200✔
276
        mat.nuclide_[i], union_energy, micro_smooth_tot_xs);
47,551,200✔
277

278
      // ======================================================================
279
      // Accumulate the macroscopic cross section.
280
      mat_maj[i_energy] += std::max(micro_smooth_tot_xs, micro_urr_xs) *
47,565,600✔
281
                           mat.atom_density(i, max_density_mult);
47,551,200✔
282
    }
283
  }
284
}
300✔
285

286
double NeutronMajorant::calculate_max_smooth_xs(
47,351,100✔
287
  int i_nuclide, double energy) const
288
{
289
  const auto& nuc = *data::nuclides[i_nuclide];
47,351,100✔
290

291
  double max_smooth_tot_xs = 0.0;
47,351,100✔
292
  for (int i_temp = 0; i_temp < nuc.kTs_.size(); ++i_temp) {
94,702,200✔
293
    const auto& nuc_grid = nuc.grid_[i_temp];
47,351,100✔
294
    int i_grid = get_i_grid(energy, nuc_grid);
47,351,100✔
295
    auto total = nuc.xs_[i_temp].slice(openmc::tensor::all, 0);
47,351,100✔
296
    double xs = interpolate_lin_1D(nuc_grid.energy[i_grid],
47,351,100✔
297
      nuc_grid.energy[i_grid + 1], total[i_grid], total[i_grid + 1], energy);
47,351,100!
298
    max_smooth_tot_xs = std::max(max_smooth_tot_xs, xs);
94,702,200!
299
  }
47,351,100✔
300

301
  return max_smooth_tot_xs;
47,351,100✔
302
}
303

304
double NeutronMajorant::calculate_max_urr_xs(
47,551,200✔
305
  int i_nuclide, double energy, double smooth_xs) const
306
{
307
  // A tolerance on the URR check to make sure we include the URR energy grid
308
  // bounds.
309
  constexpr double URR_FUZZY_CHECK = 1e-6;
47,551,200✔
310

311
  const auto& nuc = *data::nuclides[i_nuclide];
47,551,200✔
312
  if (!nuc.urr_present_) {
47,551,200✔
313
    return 0.0;
314
  }
315

316
  double max_urr_xs = 0.0;
11,887,800✔
317
  for (const auto& urr : nuc.urr_data_) {
23,775,600✔
318
    if (!(urr.energy_in_bounds(energy - URR_FUZZY_CHECK) ||
11,887,800✔
319
          urr.energy_in_bounds(energy + URR_FUZZY_CHECK))) {
11,873,550✔
320
      continue;
11,873,400✔
321
    }
322

323
    int i_energy;
14,400✔
324
    if (energy <= urr.energy_.front()) {
14,400✔
325
      i_energy = 0;
326
    } else if (energy >= urr.energy_.back()) {
14,250✔
327
      i_energy = urr.energy_.size() - 2;
150✔
328
    } else {
329
      i_energy =
14,100✔
330
        lower_bound_index(&urr.energy_.front(), &urr.energy_.back(), energy);
14,100✔
331
    }
332

333
    // Find the maximum URR cross sections for the two bounding energy points.
334
    double max_urr_xs_E0 = 0.0;
14,400✔
335
    double max_urr_xs_E1 = 0.0;
14,400✔
336
    for (int i_cdf = 0; i_cdf < urr.n_cdf(); ++i_cdf) {
604,800!
337
      max_urr_xs_E0 =
576,000✔
338
        std::max(max_urr_xs_E0, urr.xs_values_(i_energy, i_cdf).total);
288,000!
339
      max_urr_xs_E1 =
576,000✔
340
        std::max(max_urr_xs_E1, urr.xs_values_(i_energy + 1, i_cdf).total);
576,000!
341
    }
342

343
    // Interpolate the bounding energy points.
344
    double interp_urr_xs = 0.0;
14,400✔
345
    if (urr.interp_ == Interpolation::lin_lin) {
14,400!
346
      interp_urr_xs = interpolate_lin_1D(urr.energy_[i_energy],
14,400✔
347
        urr.energy_[i_energy + 1], max_urr_xs_E0, max_urr_xs_E1, energy);
14,400✔
NEW
348
    } else if (urr.interp_ == Interpolation::log_log) {
×
NEW
349
      interp_urr_xs = interpolate_log_1D(urr.energy_[i_energy],
×
NEW
350
        urr.energy_[i_energy + 1], max_urr_xs_E0, max_urr_xs_E1, energy);
×
351
    }
352

353
    // Multiply by the smooth cross section (after interpolation) if required.
354
    if (urr.multiply_smooth_) {
14,400!
355
      interp_urr_xs *= smooth_xs;
14,400✔
356
    }
357

358
    max_urr_xs = std::max(max_urr_xs, interp_urr_xs);
28,800!
359
  }
360

361
  return max_urr_xs;
11,887,800✔
362
}
363

364
double NeutronMajorant::calculate_max_sab_tot_xs(
200,100✔
365
  int i_nuclide, int i_sab, double sab_frac, double energy) const
366
{
367
  const auto& nuc = *data::nuclides[i_nuclide];
200,100✔
368
  const auto& thermal = *data::thermal_scatt[i_sab];
200,100✔
369

370
  // Loop over the nuclide's temperature grid to ensure we're consistent.
371
  double max_sab_total = 0.0;
200,100✔
372
  for (int i_nuc_temp = 0; i_nuc_temp < nuc.kTs_.size(); ++i_nuc_temp) {
400,200✔
373
    double nuc_kT = nuc.kTs_[i_nuc_temp] * nuc.kTs_[i_nuc_temp];
200,100!
374

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

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

424
    double thermal_xs = sab_frac * (thermal_elastic + thermal_inelastic);
200,100✔
425
    double sab_corrected_total = tot_xs + thermal_xs - sab_frac * ela_xs;
200,100✔
426
    max_sab_total = std::max(sab_corrected_total, max_sab_total);
200,100!
427
  }
200,100✔
428

429
  return max_sab_total;
200,100✔
430
}
431

432
int NeutronMajorant::get_i_grid(
68,172,006✔
433
  double energy, const Nuclide::EnergyGrid& grid) const
434
{
435
  // Find energy index on energy grid
436
  int i_log_union =
68,172,006✔
437
    std::log(energy / data::energy_min[i_neutron_]) / simulation::log_spacing;
68,172,006✔
438

439
  int i_grid;
68,172,006✔
440
  if (energy <= grid.energy.front()) {
68,172,006✔
441
    i_grid = 0;
442
  } else if (energy >= grid.energy.back()) {
68,171,406✔
443
    i_grid = grid.energy.size() - 2;
300✔
444
  } else {
445
    // Determine bounding indices based on which equal log-spaced
446
    // interval the energy is in
447
    int i_low = grid.grid_index[i_log_union];
68,171,106!
448
    int i_high = grid.grid_index[i_log_union + 1] + 1;
68,171,106✔
449

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

460
  // check for rare case where two energy points are the same
461
  if (grid.energy[i_grid] == grid.energy[i_grid + 1])
68,172,006!
NEW
462
    ++i_grid;
×
463

464
  return i_grid;
68,172,006✔
465
}
466

467
//==============================================================================
468
// PhotonMajorant implementation
469
//==============================================================================
470

471
PhotonMajorant::PhotonMajorant(int i_universe) : Majorant(i_universe) {}
45✔
472

473
void PhotonMajorant::compute_unionized_grid()
45✔
474
{
475
  // This function generates a unionized cross section grid for all elements.
476
  std::set<int> processed_elements;
45✔
477
  for (int i_mat : contained_materials_) {
135✔
478
    const auto& mat = model::materials[i_mat];
90✔
479
    for (int i = 0; i < mat->nuclide_.size(); ++i) {
270✔
480
      // Only unionize elements we haven't checked yet.
481
      if (processed_elements.count(mat->element_[i]) > 0) {
180✔
482
        continue;
45✔
483
      }
484

485
      const auto& element = data::elements[mat->element_[i]];
135✔
486
      grid_.energy.insert(
135✔
487
        grid_.energy.end(), element->energy_.begin(), element->energy_.end());
135✔
488

489
      processed_elements.insert(mat->element_[i]);
135✔
490
    }
491
  }
492

493
  // Post-process the energy grid now that all points from photon interactions
494
  // are included. This sorts the energy points, removes duplicates, and removes
495
  // all energies exceeding photon transport bounds.
496
  post_process_grid(i_photon_, grid_);
45✔
497
}
45✔
498

499
double PhotonMajorant::calculate_photon_xs(double energy) const
19,662,423✔
500
{
501
  double log_energy = std::log(energy);
19,662,423✔
502
  int i_grid = get_i_grid(log_energy, grid_.energy);
19,662,423✔
503

504
  // calculate interpolation factor
505
  double f = (log_energy - grid_.energy[i_grid]) /
19,662,423✔
506
             (grid_.energy[i_grid + 1] - grid_.energy[i_grid]);
19,662,423✔
507

508
  // interpolate the total cross section
509
  return std::exp(xs_[i_grid] + f * (xs_[i_grid + 1] - xs_[i_grid]));
19,662,423✔
510
}
511

512
void PhotonMajorant::fill_material_maj_xs(int i_material,
90✔
513
  double max_density_mult, const std::vector<double>& to_grid,
514
  std::vector<double>& mat_maj) const
515
{
516
  const auto& mat = *model::materials[i_material];
90✔
517

518
  mat_maj.resize(to_grid.size());
90✔
519
  for (int i_energy = 0; i_energy < to_grid.size(); ++i_energy) {
127,620✔
520
    mat_maj[i_energy] = 0.0;
127,530✔
521
    const double union_log_energy = to_grid[i_energy];
127,530✔
522

523
    for (int i = 0; i < mat.nuclide_.size(); ++i) {
382,590✔
524
      const int i_element = mat.element_[i];
255,060✔
525

526
      mat_maj[i_energy] += calculate_elem_tot_xs(i_element, union_log_energy) *
255,060✔
527
                           mat.atom_density(i, max_density_mult);
255,060✔
528
    }
529
    mat_maj[i_energy] = std::log(mat_maj[i_energy]);
127,530✔
530
  }
531
}
90✔
532

533
double PhotonMajorant::calculate_elem_tot_xs(
255,060✔
534
  int i_element, double log_energy) const
535
{
536
  const auto& elem = *data::elements[i_element];
255,060✔
537
  int i_grid = get_i_grid(log_energy, elem.energy_);
255,060✔
538

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

543
  // Calculate microscopic coherent cross section
544
  double coherent =
255,060✔
545
    std::exp(elem.coherent_(i_grid) +
510,120✔
546
             f * (elem.coherent_(i_grid + 1) - elem.coherent_(i_grid)));
255,060✔
547

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

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

558
  for (int i = 0; i < xs_upper.size(); ++i)
5,356,260✔
559
    if (xs_lower(i) != 0)
2,423,070✔
560
      photoelectric += std::exp(xs_lower(i) + f * (xs_upper(i) - xs_lower(i)));
2,271,060✔
561

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

568
  // Calculate microscopic total cross section
569
  double total = coherent + incoherent + photoelectric + pair_production;
255,060✔
570
  return total;
255,060✔
571
}
510,120✔
572

573
int PhotonMajorant::get_i_grid(
19,662,423✔
574
  double log_energy, const std::vector<double>& energy_grid) const
575
{
576
  int n_grid = energy_grid.size();
19,662,423✔
577
  int i_grid;
19,662,423✔
578
  if (log_energy <= energy_grid[0]) {
19,662,423✔
579
    i_grid = 0;
580
  } else if (log_energy > energy_grid[n_grid - 1]) {
19,606,785!
NEW
581
    i_grid = n_grid - 2;
×
582
  } else {
583
    // We use upper_bound_index here because sometimes photons are created with
584
    // energies that exactly match a grid point
585
    i_grid =
19,606,785✔
586
      upper_bound_index(energy_grid.cbegin(), energy_grid.cend(), log_energy);
19,606,785✔
587
  }
588

589
  // check for case where two energy points are the same
590
  if (energy_grid[i_grid] == energy_grid[i_grid + 1])
19,662,423!
NEW
591
    ++i_grid;
×
592

593
  return i_grid;
19,662,423✔
594
}
595

596
int PhotonMajorant::get_i_grid(
255,060✔
597
  double log_energy, const tensor::Tensor<double>& energy_grid) const
598
{
599
  int n_grid = energy_grid.size();
255,060!
600
  int i_grid;
255,060✔
601
  if (log_energy <= energy_grid[0]) {
255,060!
602
    i_grid = 0;
603
  } else if (log_energy > energy_grid(n_grid - 1)) {
255,060!
NEW
604
    i_grid = n_grid - 2;
×
605
  } else {
606
    // We use upper_bound_index here because sometimes photons are created with
607
    // energies that exactly match a grid point
608
    i_grid =
255,060✔
609
      upper_bound_index(energy_grid.cbegin(), energy_grid.cend(), log_energy);
255,060✔
610
  }
611

612
  // check for case where two energy points are the same
613
  if (energy_grid(i_grid) == energy_grid(i_grid + 1))
255,060!
NEW
614
    ++i_grid;
×
615

616
  return i_grid;
255,060✔
617
}
618

619
//! Create a majorant cross section for photons or neutrons.
620
void create_majorants()
150✔
621
{
622
  simulation::time_build_majorant.start();
150✔
623

624
  write_message("Constructing a neutron majorant cross section");
150✔
625
  data::n_majorant = std::make_unique<NeutronMajorant>(model::root_universe);
150✔
626
  data::n_majorant->compute_unionized_grid();
150✔
627
  data::n_majorant->compute_majorant();
150✔
628

629
  if (settings::photon_transport) {
150✔
630
    write_message("Constructing a photon majorant cross section");
45✔
631
    data::p_majorant = std::make_unique<PhotonMajorant>(model::root_universe);
45✔
632
    data::p_majorant->compute_unionized_grid();
45✔
633
    data::p_majorant->compute_majorant();
45✔
634
  }
635

636
  simulation::time_build_majorant.stop();
150✔
637
}
150✔
638

639
//! Reset the photon and neutron majorant cross sections.
640
void reset_majorants()
7,864✔
641
{
642
  openmc::data::n_majorant.reset(nullptr);
7,864✔
643
  openmc::data::p_majorant.reset(nullptr);
7,864✔
644
}
7,864✔
645
} // 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