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

openmc-dev / openmc / 27444237628

12 Jun 2026 09:31PM UTC coverage: 81.304% (+0.02%) from 81.281%
27444237628

Pull #3971

github

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

18480 of 26797 branches covered (68.96%)

Branch coverage included in aggregate %.

592 of 644 new or added lines in 20 files covered. (91.93%)

31 existing lines in 3 files now uncovered.

59819 of 69507 relevant lines covered (86.06%)

49514105.74 hits per line

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

81.78
/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
  std::fill(mat_maj.begin(), mat_maj.end(), 0.0);
300✔
220

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

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

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

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

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

249
          // Increment position in thermal_tables_
250
          ++mat_sab_table_idx;
11,887,800✔
251

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

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

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

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

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

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

303
  return max_smooth_tot_xs;
47,351,100✔
304
}
305

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

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

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

325
    int i_energy =
14,400✔
326
      lower_bound_index(&urr.energy_.front(), &urr.energy_.back(), energy);
14,400✔
327

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

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

348
    // Multiply by the smooth cross section (after interpolation) if required.
349
    if (urr.multiply_smooth_) {
14,400!
350
      interp_urr_xs *= smooth_xs;
14,400✔
351
    }
352

353
    max_urr_xs = std::max(max_urr_xs, interp_urr_xs);
28,800!
354
  }
355

356
  return max_urr_xs;
11,887,800✔
357
}
358

359
double NeutronMajorant::calculate_max_sab_tot_xs(
200,100✔
360
  int i_nuclide, int i_sab, double sab_frac, double energy) const
361
{
362
  const auto& nuc = *data::nuclides[i_nuclide];
200,100✔
363
  const auto& thermal = *data::thermal_scatt[i_sab];
200,100✔
364

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

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

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

419
    double thermal_xs = sab_frac * (thermal_elastic + thermal_inelastic);
200,100✔
420
    double sab_corrected_total = tot_xs + thermal_xs - sab_frac * ela_xs;
200,100✔
421
    max_sab_total = std::max(sab_corrected_total, max_sab_total);
200,100!
422
  }
200,100✔
423

424
  return max_sab_total;
200,100✔
425
}
426

427
int NeutronMajorant::get_i_grid(
68,172,006✔
428
  double energy, const Nuclide::EnergyGrid& grid) const
429
{
430
  // Find energy index on energy grid
431
  int i_log_union =
68,172,006✔
432
    std::log(energy / data::energy_min[i_neutron_]) / simulation::log_spacing;
68,172,006✔
433

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

445
    // Perform binary search over reduced range
446
    i_grid = i_low + lower_bound_index(
68,171,706✔
447
                       &grid.energy[i_low], &grid.energy[i_high], energy);
68,171,706✔
448
  }
449

450
  // check for rare case where two energy points are the same
451
  if (grid.energy[i_grid] == grid.energy[i_grid + 1])
68,172,006!
NEW
452
    ++i_grid;
×
453

454
  return i_grid;
68,172,006✔
455
}
456

457
//==============================================================================
458
// PhotonMajorant implementation
459
//==============================================================================
460

461
PhotonMajorant::PhotonMajorant(int i_universe) : Majorant(i_universe) {}
45✔
462

463
void PhotonMajorant::compute_unionized_grid()
45✔
464
{
465
  // This function generates a unionized cross section grid for all elements.
466
  std::set<int> processed_elements;
45✔
467
  for (int i_mat : contained_materials_) {
135✔
468
    const auto& mat = model::materials[i_mat];
90✔
469
    for (int i = 0; i < mat->nuclide_.size(); ++i) {
270✔
470
      // Only unionize elements we haven't checked yet.
471
      if (processed_elements.count(mat->element_[i]) > 0) {
180✔
472
        continue;
45✔
473
      }
474

475
      const auto& element = data::elements[mat->element_[i]];
135✔
476
      grid_.energy.insert(
135✔
477
        grid_.energy.end(), element->energy_.begin(), element->energy_.end());
135✔
478

479
      processed_elements.insert(mat->element_[i]);
135✔
480
    }
481
  }
482

483
  // Post-process the energy grid now that all points from photon interactions
484
  // are included. This sorts the energy points, removes duplicates, and removes
485
  // all energies exceeding photon transport bounds.
486
  post_process_grid(i_photon_, grid_);
45✔
487
}
45✔
488

489
double PhotonMajorant::calculate_photon_xs(double energy) const
19,662,423✔
490
{
491
  double log_energy = std::log(energy);
19,662,423✔
492
  int i_grid = get_i_grid(log_energy, grid_.energy);
19,662,423✔
493

494
  // calculate interpolation factor
495
  double f = (log_energy - grid_.energy[i_grid]) /
19,662,423✔
496
             (grid_.energy[i_grid + 1] - grid_.energy[i_grid]);
19,662,423✔
497

498
  // interpolate the total cross section
499
  return std::exp(xs_[i_grid] + f * (xs_[i_grid + 1] - xs_[i_grid]));
19,662,423✔
500
}
501

502
void PhotonMajorant::fill_material_maj_xs(int i_material,
90✔
503
  double max_density_mult, const std::vector<double>& to_grid,
504
  std::vector<double>& mat_maj) const
505
{
506
  const auto& mat = *model::materials[i_material];
90✔
507

508
  mat_maj.resize(to_grid.size());
90✔
509
  std::fill(mat_maj.begin(), mat_maj.end(), 0.0);
90✔
510

511
  for (int i_energy = 0; i_energy < to_grid.size(); ++i_energy) {
127,620✔
512
    mat_maj[i_energy] = 0.0;
127,530✔
513
    const double union_log_energy = to_grid[i_energy];
127,530✔
514

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

518
      mat_maj[i_energy] += calculate_elem_tot_xs(i_element, union_log_energy) *
255,060✔
519
                           mat.atom_density(i, max_density_mult);
255,060✔
520
    }
521
    mat_maj[i_energy] = std::log(mat_maj[i_energy]);
127,530✔
522
  }
523
}
90✔
524

525
double PhotonMajorant::calculate_elem_tot_xs(
255,060✔
526
  int i_element, double log_energy) const
527
{
528
  const auto& elem = *data::elements[i_element];
255,060✔
529
  int i_grid = get_i_grid(log_energy, elem.energy_);
255,060✔
530

531
  // calculate interpolation factor
532
  double f = (log_energy - elem.energy_(i_grid)) /
255,060✔
533
             (elem.energy_(i_grid + 1) - elem.energy_(i_grid));
255,060✔
534

535
  // Calculate microscopic coherent cross section
536
  double coherent =
255,060✔
537
    std::exp(elem.coherent_(i_grid) +
510,120✔
538
             f * (elem.coherent_(i_grid + 1) - elem.coherent_(i_grid)));
255,060✔
539

540
  // Calculate microscopic incoherent cross section
541
  double incoherent =
255,060✔
542
    std::exp(elem.incoherent_(i_grid) +
255,060✔
543
             f * (elem.incoherent_(i_grid + 1) - elem.incoherent_(i_grid)));
255,060✔
544

545
  // Calculate microscopic photoelectric cross section
546
  double photoelectric = 0.0;
255,060✔
547
  tensor::View<const double> xs_lower = elem.cross_sections_.slice(i_grid);
255,060✔
548
  tensor::View<const double> xs_upper = elem.cross_sections_.slice(i_grid + 1);
255,060✔
549

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

554
  // Calculate microscopic pair production cross section
555
  double pair_production =
255,060✔
556
    std::exp(elem.pair_production_total_(i_grid) +
255,060✔
557
             f * (elem.pair_production_total_(i_grid + 1) -
255,060✔
558
                   elem.pair_production_total_(i_grid)));
255,060✔
559

560
  // Calculate microscopic total cross section
561
  double total = coherent + incoherent + photoelectric + pair_production;
255,060✔
562
  return total;
255,060✔
563
}
510,120✔
564

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

581
  // check for case where two energy points are the same
582
  if (energy_grid[i_grid] == energy_grid[i_grid + 1])
19,662,423!
NEW
583
    ++i_grid;
×
584

585
  return i_grid;
19,662,423✔
586
}
587

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

604
  // check for case where two energy points are the same
605
  if (energy_grid(i_grid) == energy_grid(i_grid + 1))
255,060!
NEW
606
    ++i_grid;
×
607

608
  return i_grid;
255,060✔
609
}
610

611
//! Create a majorant cross section for photons or neutrons.
612
void create_majorants()
150✔
613
{
614
  simulation::time_build_majorant.start();
150✔
615

616
  write_message("Constructing a neutron majorant cross section");
150✔
617
  data::n_majorant = std::make_unique<NeutronMajorant>(model::root_universe);
150✔
618
  data::n_majorant->compute_unionized_grid();
150✔
619
  data::n_majorant->compute_majorant();
150✔
620

621
  if (settings::photon_transport) {
150✔
622
    write_message("Constructing a photon majorant cross section");
45✔
623
    data::p_majorant = std::make_unique<PhotonMajorant>(model::root_universe);
45✔
624
    data::p_majorant->compute_unionized_grid();
45✔
625
    data::p_majorant->compute_majorant();
45✔
626
  }
627

628
  simulation::time_build_majorant.stop();
150✔
629
}
150✔
630

631
//! Reset the photon and neutron majorant cross sections.
632
void reset_majorants()
7,863✔
633
{
634
  openmc::data::n_majorant.reset(nullptr);
7,863✔
635
  openmc::data::p_majorant.reset(nullptr);
7,863✔
636
}
7,863✔
637
} // 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