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

openmc-dev / openmc / 42913773175

10 Nov 2026 10:21PM UTC coverage: 81.618% (-0.4%) from 82.009%
42913773175

Pull #4215

github

web-flow
Merge 5dfbd7384 into 8f79d9d02
Pull Request #4215: XDG Mesh Geometry

19636 of 27762 branches covered (70.73%)

Branch coverage included in aggregate %.

117 of 222 new or added lines in 18 files covered. (52.7%)

712 existing lines in 39 files now uncovered.

62242 of 72557 relevant lines covered (85.78%)

32920641.18 hits per line

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

73.52
/src/material.cpp
1
#include "openmc/material.h"
2

3
#include <algorithm> // for min, max, sort, fill
4
#include <cassert>
5
#include <cmath>
6
#include <iterator>
7
#include <sstream>
8
#include <string>
9
#include <unordered_set>
10

11
#include "openmc/tensor.h"
12

13
#include "openmc/capi.h"
14
#include "openmc/container_util.h"
15
#include "openmc/cross_sections.h"
16
#include "openmc/error.h"
17
#include "openmc/file_utils.h"
18
#include "openmc/hdf5_interface.h"
19
#include "openmc/math_functions.h"
20
#include "openmc/message_passing.h"
21
#include "openmc/mgxs_interface.h"
22
#include "openmc/nuclide.h"
23
#include "openmc/photon.h"
24
#include "openmc/search.h"
25
#include "openmc/settings.h"
26
#include "openmc/simulation.h"
27
#include "openmc/string_utils.h"
28
#include "openmc/thermal.h"
29
#include "openmc/xml_interface.h"
30

31
namespace openmc {
32

33
//==============================================================================
34
// Global variables
35
//==============================================================================
36

37
namespace model {
38

39
std::unordered_map<int32_t, int32_t> material_map;
40
vector<unique_ptr<Material>> materials;
41

42
} // namespace model
43

44
//==============================================================================
45
// Material implementation
46
//==============================================================================
47

48
Material::Material(pugi::xml_node node)
7,714 ✔
49
{
50
  index_ = model::materials.size(); // Avoids warning about narrowing
7,714 ✔
51

52
  if (check_for_node(node, "id")) {
7,714 !
53
    this->set_id(std::stoi(get_node_value(node, "id")));
15,428 ✔
54
  } else {
55
    fatal_error("Must specify id of material in materials XML file.");
×
56
  }
57

58
  if (check_for_node(node, "name")) {
7,714 ✔
59
    name_ = get_node_value(node, "name");
4,008 ✔
60
  }
61

62
  if (check_for_node(node, "cfg")) {
7,714 ✔
63
    auto cfg = get_node_value(node, "cfg");
6 ✔
64
    write_message(
6 ✔
65
      5, "NCrystal config string for material #{}: '{}'", this->id(), cfg);
6 ✔
66
    ncrystal_mat_ = NCrystalMat(cfg);
12 ✔
67
  }
6 ✔
68

69
  if (check_for_node(node, "depletable")) {
7,714 ✔
70
    depletable_ = get_node_value_bool(node, "depletable");
2,630 ✔
71
  }
72

73
  bool sum_density {false};
7,714 ✔
74
  pugi::xml_node density_node = node.child("density");
7,714 ✔
75
  std::string units;
7,714 ✔
76
  if (density_node) {
7,714 !
77
    units = get_node_value(density_node, "units");
7,714 ✔
78
    if (units == "sum") {
7,714 ✔
79
      sum_density = true;
80
    } else if (units == "macro") {
6,552 ✔
81
      if (check_for_node(density_node, "value")) {
1,495 !
82
        density_ = std::stod(get_node_value(density_node, "value"));
2,990 ✔
83
      } else {
84
        density_ = 1.0;
×
85
      }
86
    } else {
87
      double val = std::stod(get_node_value(density_node, "value"));
10,114 ✔
88
      if (val <= 0.0) {
5,057 !
89
        fatal_error("Need to specify a positive density on material " +
×
90
                    std::to_string(id_) + ".");
×
91
      }
92

93
      if (units == "g/cc" || units == "g/cm3") {
8,626 ✔
94
        density_ = -val;
4,927 ✔
95
      } else if (units == "kg/m3") {
130 ✔
96
        density_ = -1.0e-3 * val;
6 ✔
97
      } else if (units == "atom/b-cm") {
124 ✔
98
        density_ = val;
118 ✔
99
      } else if (units == "atom/cc" || units == "atom/cm3") {
12 !
100
        density_ = 1.0e-24 * val;
6 ✔
101
      } else {
102
        fatal_error("Unknown units '" + units + "' specified on material " +
×
103
                    std::to_string(id_) + ".");
×
104
      }
105
    }
106
  } else {
107
    fatal_error("Must specify <density> element in material " +
×
108
                std::to_string(id_) + ".");
×
109
  }
110

111
  if (node.child("element")) {
7,714 !
112
    fatal_error(
×
113
      "Unable to add an element to material " + std::to_string(id_) +
×
114
      " since the element option has been removed from the xml input. "
115
      "Elements can only be added via the Python API, which will expand "
116
      "elements into their natural nuclides.");
117
  }
118

119
  // =======================================================================
120
  // READ AND PARSE <nuclide> TAGS
121

122
  // Check to ensure material has at least one nuclide
123
  if (!check_for_node(node, "nuclide") &&
9,209 !
124
      !check_for_node(node, "macroscopic")) {
1,495 ✔
125
    fatal_error("No macroscopic data or nuclides specified on material " +
×
126
                std::to_string(id_));
×
127
  }
128

129
  // Create list of macroscopic x/s based on those specified, just treat
130
  // them as nuclides. This is all really a facade so the user thinks they
131
  // are entering in macroscopic data but the code treats them the same
132
  // as nuclides internally.
133
  // Get pointer list of XML <macroscopic>
134
  auto node_macros = node.children("macroscopic");
7,714 ✔
135
  int num_macros = std::distance(node_macros.begin(), node_macros.end());
7,714 ✔
136

137
  vector<std::string> names;
7,714 ✔
138
  vector<double> densities;
7,714 ✔
139
  if (settings::run_CE && num_macros > 0) {
7,714 !
140
    fatal_error("Macroscopic can not be used in continuous-energy mode.");
×
141
  } else if (num_macros > 1) {
7,714 !
142
    fatal_error("Only one macroscopic object permitted per material, " +
×
143
                std::to_string(id_));
×
144
  } else if (num_macros == 1) {
7,714 ✔
145
    pugi::xml_node node_nuc = *node_macros.begin();
1,495 ✔
146

147
    // Check for empty name on nuclide
148
    if (!check_for_node(node_nuc, "name")) {
1,495 !
149
      fatal_error("No name specified on macroscopic data in material " +
×
150
                  std::to_string(id_));
×
151
    }
152

153
    // store nuclide name
154
    std::string name = get_node_value(node_nuc, "name", false, true);
1,495 ✔
155
    names.push_back(name);
1,495 ✔
156

157
    // Set density for macroscopic data
158
    if (units == "macro") {
1,495 !
159
      densities.push_back(density_);
1,495 ✔
160
    } else {
161
      fatal_error("Units can only be macro for macroscopic data " + name);
×
162
    }
163
  } else {
1,495 ✔
164
    // Create list of nuclides based on those specified
165
    for (auto node_nuc : node.children("nuclide")) {
30,569 ✔
166
      // Check for empty name on nuclide
167
      if (!check_for_node(node_nuc, "name")) {
24,350 !
168
        fatal_error(
×
169
          "No name specified on nuclide in material " + std::to_string(id_));
×
170
      }
171

172
      // store nuclide name
173
      std::string name = get_node_value(node_nuc, "name", false, true);
24,350 ✔
174
      names.push_back(name);
24,350 ✔
175

176
      // Check if no atom/weight percents were specified or if both atom and
177
      // weight percents were specified
178
      if (units == "macro") {
24,350 !
179
        densities.push_back(density_);
×
180
      } else {
181
        bool has_ao = check_for_node(node_nuc, "ao");
24,350 ✔
182
        bool has_wo = check_for_node(node_nuc, "wo");
24,350 ✔
183

184
        if (!has_ao && !has_wo) {
24,350 !
185
          fatal_error(
×
186
            "No atom or weight percent specified for nuclide: " + name);
×
187
        } else if (has_ao && has_wo) {
24,350 !
188
          fatal_error("Cannot specify both atom and weight percents for a "
×
189
                      "nuclide: " +
×
190
                      name);
191
        }
192

193
        // Copy atom/weight percents
194
        if (has_ao) {
24,350 ✔
195
          densities.push_back(std::stod(get_node_value(node_nuc, "ao")));
42,190 ✔
196
        } else {
197
          densities.push_back(-std::stod(get_node_value(node_nuc, "wo")));
6,510 ✔
198
        }
199
      }
200
    }
24,350 ✔
201
  }
202

203
  // =======================================================================
204
  // READ AND PARSE <isotropic> element
205

206
  vector<std::string> iso_lab;
7,714 ✔
207
  if (check_for_node(node, "isotropic")) {
7,714 ✔
208
    iso_lab = get_node_array<std::string>(node, "isotropic");
144 ✔
209
  }
210

211
  // ========================================================================
212
  // COPY NUCLIDES TO ARRAYS IN MATERIAL
213

214
  // allocate arrays in Material object
215
  auto n = names.size();
7,714 ✔
216
  nuclide_.reserve(n);
7,714 ✔
217
  atom_density_ = tensor::Tensor<double>({n});
7,714 ✔
218
  if (settings::photon_transport)
7,714 ✔
219
    element_.reserve(n);
294 ✔
220

221
  for (int i = 0; i < n; ++i) {
33,559 ✔
222
    const auto& name {names[i]};
25,845 ✔
223

224
    // Check that this nuclide is listed in the nuclear data library
225
    // (cross_sections.xml for CE and the MGXS HDF5 for MG)
226
    if (settings::run_mode != RunMode::PLOTTING) {
25,845 ✔
227
      LibraryKey key {Library::Type::neutron, name};
25,440 ✔
228
      if (data::library_map.find(key) == data::library_map.end()) {
25,440 !
229
        fatal_error("Could not find nuclide " + name +
×
230
                    " in the "
231
                    "nuclear data library.");
232
      }
233
    }
25,440 ✔
234

235
    // If this nuclide hasn't been encountered yet, we need to add its name
236
    // and alias to the nuclide_dict
237
    if (data::nuclide_map.find(name) == data::nuclide_map.end()) {
25,845 ✔
238
      int index = data::nuclide_map.size();
16,205 ✔
239
      data::nuclide_map[name] = index;
16,205 ✔
240
      nuclide_.push_back(index);
16,205 ✔
241
    } else {
242
      nuclide_.push_back(data::nuclide_map[name]);
9,640 ✔
243
    }
244

245
    // If the corresponding element hasn't been encountered yet and photon
246
    // transport will be used, we need to add its symbol to the element_dict
247
    if (settings::photon_transport) {
25,845 ✔
248
      std::string element = to_element(name);
1,359 ✔
249

250
      // Make sure photon cross section data is available
251
      if (settings::run_mode != RunMode::PLOTTING) {
1,359 !
252
        LibraryKey key {Library::Type::photon, element};
1,359 ✔
253
        if (data::library_map.find(key) == data::library_map.end()) {
1,359 !
254
          fatal_error(
×
255
            "Could not find element " + element + " in cross_sections.xml.");
×
256
        }
257
      }
1,359 ✔
258

259
      if (data::element_map.find(element) == data::element_map.end()) {
1,359 ✔
260
        int index = data::element_map.size();
740 ✔
261
        data::element_map[element] = index;
740 ✔
262
        element_.push_back(index);
740 ✔
263
      } else {
264
        element_.push_back(data::element_map[element]);
619 ✔
265
      }
266
    }
1,359 ✔
267

268
    // Copy atom/weight percent
269
    atom_density_(i) = densities[i];
25,845 ✔
270
  }
271

272
  if (settings::run_CE) {
7,714 ✔
273
    // By default, isotropic-in-lab is not used
274
    if (iso_lab.size() > 0) {
6,141 ✔
275
      p0_.resize(n);
72 ✔
276

277
      // Apply isotropic-in-lab treatment to specified nuclides
278
      for (int j = 0; j < n; ++j) {
678 ✔
279
        for (const auto& nuc : iso_lab) {
3,150 !
280
          if (names[j] == nuc) {
3,150 ✔
281
            p0_[j] = true;
606 ✔
282
            break;
606 ✔
283
          }
284
        }
285
      }
286
    }
287
  }
288

289
  // Check to make sure either all atom percents or all weight percents are
290
  // given
291
  if (!((atom_density_ >= 0.0).all() || (atom_density_ <= 0.0).all())) {
23,900 !
292
    fatal_error(
×
293
      "Cannot mix atom and weight percents in material " + std::to_string(id_));
×
294
  }
295

296
  // Determine density if it is a sum value
297
  if (sum_density)
7,714 ✔
298
    density_ = atom_density_.sum();
1,162 ✔
299

300
  if (check_for_node(node, "temperature")) {
7,714 ✔
301
    temperature_ = std::stod(get_node_value(node, "temperature"));
1,792 ✔
302
  }
303

304
  if (check_for_node(node, "volume")) {
7,714 ✔
305
    volume_ = std::stod(get_node_value(node, "volume"));
1,928 ✔
306
  }
307

308
  // =======================================================================
309
  // READ AND PARSE <sab> TAG FOR THERMAL SCATTERING DATA
310
  if (settings::run_CE) {
7,714 ✔
311
    // Loop over <sab> elements
312

313
    vector<std::string> sab_names;
6,141 ✔
314
    for (auto node_sab : node.children("sab")) {
7,072 ✔
315
      // Determine name of thermal scattering table
316
      if (!check_for_node(node_sab, "name")) {
931 !
317
        fatal_error("Need to specify <name> for thermal scattering table.");
×
318
      }
319
      std::string name = get_node_value(node_sab, "name");
931 ✔
320
      sab_names.push_back(name);
931 ✔
321

322
      // Read the fraction of nuclei affected by this thermal scattering table
323
      double fraction = 1.0;
931 ✔
324
      if (check_for_node(node_sab, "fraction")) {
931 ✔
325
        fraction = std::stod(get_node_value(node_sab, "fraction"));
12 ✔
326
      }
327

328
      // Check that the thermal scattering table is listed in the
329
      // cross_sections.xml file
330
      if (settings::run_mode != RunMode::PLOTTING) {
931 ✔
331
        LibraryKey key {Library::Type::thermal, name};
906 ✔
332
        if (data::library_map.find(key) == data::library_map.end()) {
906 !
333
          fatal_error("Could not find thermal scattering data " + name +
×
334
                      " in cross_sections.xml file.");
335
        }
336
      }
906 ✔
337

338
      // Determine index of thermal scattering data in global
339
      // data::thermal_scatt array
340
      int index_table;
931 ✔
341
      if (data::thermal_scatt_map.find(name) == data::thermal_scatt_map.end()) {
931 ✔
342
        index_table = data::thermal_scatt_map.size();
646 ✔
343
        data::thermal_scatt_map[name] = index_table;
646 ✔
344
      } else {
345
        index_table = data::thermal_scatt_map[name];
285 ✔
346
      }
347

348
      // Add entry to thermal tables vector. For now, we put the nuclide index
349
      // as zero since we don't know which nuclides the table is being applied
350
      // to yet (this is assigned in init_thermal)
351
      thermal_tables_.push_back({index_table, 0, fraction});
931 ✔
352
    }
931 ✔
353
  }
6,141 ✔
354
}
7,714 ✔
355

356
Material::~Material()
7,734 ✔
357
{
358
  model::material_map.erase(id_);
7,734 ✔
359
}
23,202 ✔
360

361
Material& Material::clone()
×
362
{
363
  std::unique_ptr<Material> mat = std::make_unique<Material>();
×
364

365
  // set all other parameters to whatever the calling Material has
366
  mat->name_ = name_;
×
367
  mat->nuclide_ = nuclide_;
×
368
  mat->element_ = element_;
×
369
  mat->ncrystal_mat_ = ncrystal_mat_.clone();
×
370
  mat->atom_density_ = atom_density_;
×
371
  mat->density_ = density_;
×
372
  mat->density_gpcc_ = density_gpcc_;
×
373
  mat->volume_ = volume_;
×
374
  mat->fissionable() = fissionable_;
×
375
  mat->depletable() = depletable_;
×
376
  mat->p0_ = p0_;
×
377
  mat->mat_nuclide_index_ = mat_nuclide_index_;
×
378
  mat->thermal_tables_ = thermal_tables_;
×
379
  mat->temperature_ = temperature_;
×
380

381
  if (ttb_)
×
382
    mat->ttb_ = std::make_unique<Bremsstrahlung>(*ttb_);
×
383

384
  mat->index_ = model::materials.size();
×
385
  mat->set_id(C_NONE);
×
386
  model::materials.push_back(std::move(mat));
×
387
  return *model::materials.back();
×
388
}
×
389

390
void Material::finalize()
7,579 ✔
391
{
392
  // Set fissionable if any nuclide is fissionable
393
  if (settings::run_CE) {
7,579 ✔
394
    for (const auto& i_nuc : nuclide_) {
21,572 ✔
395
      if (data::nuclides[i_nuc]->fissionable_) {
18,172 ✔
396
        fissionable_ = true;
2,606 ✔
397
        break;
2,606 ✔
398
      }
399
    }
400

401
    // Generate material bremsstrahlung data for electrons and positrons
402
    if (settings::photon_transport &&
6,006 ✔
403
        settings::electron_treatment == ElectronTreatment::TTB) {
294 ✔
404
      this->init_bremsstrahlung();
212 ✔
405
    }
406

407
    // Assign thermal scattering tables
408
    this->init_thermal();
6,006 ✔
409
  }
410

411
  // Normalize density
412
  this->normalize_density();
7,579 ✔
413
}
7,579 ✔
414

415
void Material::normalize_density()
7,579 ✔
416
{
417
  bool percent_in_atom = (atom_density_(0) >= 0.0);
7,579 ✔
418
  bool density_in_atom = (density_ >= 0.0);
7,579 ✔
419

420
  for (int i = 0; i < nuclide_.size(); ++i) {
33,019 ✔
421
    // determine atomic weight ratio
422
    int i_nuc = nuclide_[i];
25,440 ✔
423
    double awr = settings::run_CE ? data::nuclides[i_nuc]->awr_
25,440 ✔
424
                                  : data::mg.nuclides_[i_nuc].awr;
1,717 ✔
425

426
    // if given weight percent, convert all values so that they are divided
427
    // by awr. thus, when a sum is done over the values, it's actually
428
    // sum(w/awr)
429
    if (!percent_in_atom)
25,440 ✔
430
      atom_density_(i) = -atom_density_(i) / awr;
3,255 ✔
431
  }
432

433
  // determine normalized atom percents. if given atom percents, this is
434
  // straightforward. if given weight percents, the value is w/awr and is
435
  // divided by sum(w/awr)
436
  atom_density_ /= atom_density_.sum();
7,579 ✔
437

438
  // Change density in g/cm^3 to atom/b-cm. Since all values are now in
439
  // atom percent, the sum needs to be re-evaluated as 1/sum(x*awr)
440
  if (!density_in_atom) {
7,579 ✔
441
    double sum_percent = 0.0;
442
    for (int i = 0; i < nuclide_.size(); ++i) {
21,329 ✔
443
      int i_nuc = nuclide_[i];
16,531 ✔
444
      double awr = settings::run_CE ? data::nuclides[i_nuc]->awr_
16,531 ✔
445
                                    : data::mg.nuclides_[i_nuc].awr;
30 ✔
446
      sum_percent += atom_density_(i) * awr;
16,531 ✔
447
    }
448
    sum_percent = 1.0 / sum_percent;
4,798 ✔
449
    density_ = -density_ * N_AVOGADRO / MASS_NEUTRON * sum_percent;
4,798 ✔
450
  }
451

452
  // Calculate nuclide atom densities
453
  atom_density_ *= density_;
7,579 ✔
454

455
  // Calculate density in [g/cm^3] and charge density in [e/b-cm]
456
  density_gpcc_ = 0.0;
7,579 ✔
457
  charge_density_ = 0.0;
7,579 ✔
458
  for (int i = 0; i < nuclide_.size(); ++i) {
33,019 ✔
459
    int i_nuc = nuclide_[i];
25,440 ✔
460
    double awr = settings::run_CE ? data::nuclides[i_nuc]->awr_ : 1.0;
25,440 ✔
461
    int z = settings::run_CE ? data::nuclides[i_nuc]->Z_ : 0.0;
25,440 ✔
462
    density_gpcc_ += atom_density_(i) * awr * MASS_NEUTRON / N_AVOGADRO;
25,440 ✔
463
    charge_density_ += atom_density_(i) * z;
25,440 ✔
464
  }
465
}
7,579 ✔
466

467
void Material::init_thermal()
9,108 ✔
468
{
469
  vector<ThermalTable> tables;
9,108 ✔
470

471
  std::unordered_set<int> already_checked;
9,108 ✔
472
  for (const auto& table : thermal_tables_) {
10,019 ✔
473
    // Make sure each S(a,b) table only gets checked once
474
    if (already_checked.find(table.index_table) != already_checked.end()) {
911 !
475
      continue;
×
476
    }
477
    already_checked.insert(table.index_table);
911 ✔
478

479
    // In order to know which nuclide the S(a,b) table applies to, we need
480
    // to search through the list of nuclides for one which has a matching
481
    // name
482
    bool found = false;
483
    for (int j = 0; j < nuclide_.size(); ++j) {
5,654 ✔
484
      const auto& name {data::nuclides[nuclide_[j]]->name_};
4,743 ✔
485
      if (contains(data::thermal_scatt[table.index_table]->nuclides_, name)) {
4,743 ✔
486
        tables.push_back({table.index_table, j, table.fraction});
935 ✔
487
        found = true;
935 ✔
488
      }
489
    }
490

491
    // Check to make sure thermal scattering table matched a nuclide
492
    if (!found) {
911 !
493
      fatal_error("Thermal scattering table " +
×
494
                  data::thermal_scatt[table.index_table]->name_ +
×
495
                  " did not match any nuclide on material " +
×
496
                  std::to_string(id_));
×
497
    }
498
  }
499

500
  // Make sure each nuclide only appears in one table.
501
  for (int j = 0; j < tables.size(); ++j) {
10,043 ✔
502
    for (int k = j + 1; k < tables.size(); ++k) {
1,031 ✔
503
      if (tables[j].index_nuclide == tables[k].index_nuclide) {
96 !
504
        int index = nuclide_[tables[j].index_nuclide];
×
505
        auto name = data::nuclides[index]->name_;
×
506
        fatal_error(
×
507
          name + " in material " + std::to_string(id_) +
×
508
          " was found "
509
          "in multiple thermal scattering tables. Each nuclide can appear in "
510
          "only one table per material.");
511
      }
×
512
    }
513
  }
514

515
  // If there are multiple S(a,b) tables, we need to make sure that the
516
  // entries in i_sab_nuclides are sorted or else they won't be applied
517
  // correctly in the cross_section module.
518
  std::sort(tables.begin(), tables.end(), [](ThermalTable a, ThermalTable b) {
9,108 ✔
519
    return a.index_nuclide < b.index_nuclide;
66 !
520
  });
521

522
  // Update the list of thermal tables
523
  thermal_tables_ = tables;
9,108 ✔
524
}
9,108 ✔
525

526
void Material::collision_stopping_power(double* s_col, bool positron)
424 ✔
527
{
528
  // Average electron number and average atomic weight
529
  double electron_density = 0.0;
424 ✔
530
  double mass_density = 0.0;
424 ✔
531

532
  // Log of the mean excitation energy of the material
533
  double log_I = 0.0;
424 ✔
534

535
  // Effective number of conduction electrons in the material
536
  double n_conduction = 0.0;
424 ✔
537

538
  // Oscillator strength and square of the binding energy for each oscillator
539
  // in material
540
  vector<double> f;
424 ✔
541
  vector<double> e_b_sq;
424 ✔
542

543
  for (int i = 0; i < element_.size(); ++i) {
2,598 ✔
544
    const auto& elm = *data::elements[element_[i]];
2,174 !
545
    double awr = data::nuclides[nuclide_[i]]->awr_;
2,174 !
546

547
    // Get atomic density of nuclide given atom/weight percent
548
    double atom_density =
2,174 ✔
549
      (atom_density_[0] > 0.0) ? atom_density_[i] : -atom_density_[i] / awr;
2,174 !
550

551
    electron_density += atom_density * elm.Z_;
2,174 ✔
552
    mass_density += atom_density * awr * MASS_NEUTRON;
2,174 ✔
553
    log_I += atom_density * elm.Z_ * std::log(elm.I_);
2,174 ✔
554

555
    for (int j = 0; j < elm.n_electrons_.size(); ++j) {
17,122 ✔
556
      if (elm.n_electrons_[j] < 0) {
14,948 ✔
557
        n_conduction -= elm.n_electrons_[j] * atom_density;
1,566 ✔
558
        continue;
1,566 ✔
559
      }
560
      e_b_sq.push_back(elm.ionization_energy_[j] * elm.ionization_energy_[j]);
13,382 ✔
561
      f.push_back(elm.n_electrons_[j] * atom_density);
13,382 ✔
562
    }
563
  }
564
  log_I /= electron_density;
424 ✔
565
  n_conduction /= electron_density;
424 ✔
566
  for (auto& f_i : f)
13,806 ✔
567
    f_i /= electron_density;
13,382 ✔
568

569
  // Get density in g/cm^3 if it is given in atom/b-cm
570
  double density = (density_ < 0.0) ? -density_ : mass_density / N_AVOGADRO;
424 ✔
571

572
  // Calculate the square of the plasma energy
573
  double e_p_sq =
424 ✔
574
    PLANCK_C * PLANCK_C * PLANCK_C * N_AVOGADRO * electron_density * density /
424 ✔
575
    (2.0 * PI * PI * FINE_STRUCTURE * MASS_ELECTRON_EV * mass_density);
424 ✔
576

577
  // Get the Sternheimer adjustment factor
578
  double rho =
424 ✔
579
    sternheimer_adjustment(f, e_b_sq, e_p_sq, n_conduction, log_I, 1.0e-6, 100);
424 ✔
580

581
  // Classical electron radius in cm
582
  constexpr double CM_PER_ANGSTROM {1.0e-8};
424 ✔
583
  constexpr double r_e =
424 ✔
584
    CM_PER_ANGSTROM * PLANCK_C / (2.0 * PI * FINE_STRUCTURE * MASS_ELECTRON_EV);
585

586
  // Constant in expression for collision stopping power
587
  constexpr double BARN_PER_CM_SQ {1.0e24};
424 ✔
588
  double c =
424 ✔
589
    BARN_PER_CM_SQ * 2.0 * PI * r_e * r_e * MASS_ELECTRON_EV * electron_density;
590

591
  // Loop over incident charged particle energies
592
  for (int i = 0; i < data::ttb_e_grid.size(); ++i) {
85,086 ✔
593
    double E = data::ttb_e_grid(i);
84,662 ✔
594

595
    // Get the density effect correction
596
    double delta =
84,662 ✔
597
      density_effect(f, e_b_sq, e_p_sq, n_conduction, rho, E, 1.0e-6, 100);
84,662 ✔
598

599
    // Square of the ratio of the speed of light to the velocity of the charged
600
    // particle
601
    double beta_sq = E * (E + 2.0 * MASS_ELECTRON_EV) /
84,662 ✔
602
                     ((E + MASS_ELECTRON_EV) * (E + MASS_ELECTRON_EV));
84,662 ✔
603

604
    double tau = E / MASS_ELECTRON_EV;
84,662 ✔
605

606
    double F;
84,662 ✔
607
    if (positron) {
84,662 ✔
608
      double t = tau + 2.0;
42,331 ✔
609
      F = std::log(4.0) - (beta_sq / 12.0) * (23.0 + 14.0 / t + 10.0 / (t * t) +
42,331 ✔
610
                                               4.0 / (t * t * t));
42,331 ✔
611
    } else {
612
      F = (1.0 - beta_sq) *
84,662 ✔
613
          (1.0 + tau * tau / 8.0 - (2.0 * tau + 1.0) * std::log(2.0));
42,331 ✔
614
    }
615

616
    // Calculate the collision stopping power for this energy
617
    s_col[i] =
169,324 ✔
618
      c / beta_sq *
169,324 ✔
619
      (2.0 * (std::log(E) - log_I) + std::log(1.0 + tau / 2.0) + F - delta);
84,662 ✔
620
  }
621
}
424 ✔
622

623
void Material::init_bremsstrahlung()
212 ✔
624
{
625
  // Create new object
626
  ttb_ = make_unique<Bremsstrahlung>();
212 ✔
627

628
  // Get the size of the energy grids
629
  auto n_k = data::ttb_k_grid.size();
212 ✔
630
  auto n_e = data::ttb_e_grid.size();
212 ✔
631

632
  // Determine number of elements
633
  int n = element_.size();
212 ✔
634

635
  for (int particle = 0; particle < 2; ++particle) {
636 ✔
636
    // Loop over logic twice, once for electron, once for positron
637
    BremsstrahlungData* ttb =
424 ✔
638
      (particle == 0) ? &ttb_->electron : &ttb_->positron;
424 ✔
639
    bool positron = (particle == 1);
424 ✔
640

641
    // Allocate arrays for TTB data
642
    ttb->pdf = tensor::zeros<double>({n_e, n_e});
424 ✔
643
    ttb->cdf = tensor::zeros<double>({n_e, n_e});
424 ✔
644
    ttb->yield = tensor::zeros<double>({n_e});
424 ✔
645

646
    // Allocate temporary arrays
647
    auto stopping_power_collision = tensor::zeros<double>({n_e});
424 ✔
648
    auto stopping_power_radiative = tensor::zeros<double>({n_e});
424 ✔
649
    auto dcs = tensor::zeros<double>({n_e, n_k});
424 ✔
650

651
    double Z_eq_sq = 0.0;
424 ✔
652
    double sum_density = 0.0;
424 ✔
653

654
    // Get the collision stopping power of the material
655
    this->collision_stopping_power(stopping_power_collision.data(), positron);
424 ✔
656

657
    // Calculate the molecular DCS and the molecular radiative stopping power
658
    // using Bragg's additivity rule.
659
    for (int i = 0; i < n; ++i) {
2,598 ✔
660
      // Get pointer to current element
661
      const auto& elm = *data::elements[element_[i]];
2,174 !
662
      double awr = data::nuclides[nuclide_[i]]->awr_;
2,174 !
663

664
      // Get atomic density and mass density of nuclide given atom/weight
665
      // percent
666
      double atom_density =
2,174 ✔
667
        (atom_density_[0] > 0.0) ? atom_density_[i] : -atom_density_[i] / awr;
2,174 !
668

669
      // Calculate the "equivalent" atomic number Zeq of the material
670
      Z_eq_sq += atom_density * elm.Z_ * elm.Z_;
2,174 ✔
671
      sum_density += atom_density;
2,174 ✔
672

673
      // Accumulate material DCS
674
      dcs += (atom_density * elm.Z_ * elm.Z_) * elm.dcs_;
2,174 ✔
675

676
      // Accumulate material radiative stopping power
677
      stopping_power_radiative += atom_density * elm.stopping_power_radiative_;
4,348 ✔
678
    }
679
    Z_eq_sq /= sum_density;
424 ✔
680

681
    // Calculate the positron DCS and radiative stopping power. These are
682
    // obtained by multiplying the electron DCS and radiative stopping powers by
683
    // a factor r, which is a numerical approximation of the ratio of the
684
    // radiative stopping powers for positrons and electrons. Source: F. Salvat,
685
    // J. M. Fernández-Varea, and J. Sempau, "PENELOPE-2011: A Code System for
686
    // Monte Carlo Simulation of Electron and Photon Transport," OECD-NEA,
687
    // Issy-les-Moulineaux, France (2011).
688
    if (positron) {
424 ✔
689
      for (int i = 0; i < n_e; ++i) {
42,543 ✔
690
        double t = std::log(
42,331 ✔
691
          1.0 + 1.0e6 * data::ttb_e_grid(i) / (Z_eq_sq * MASS_ELECTRON_EV));
42,331 ✔
692
        double r =
42,331 ✔
693
          1.0 -
694
          std::exp(-1.2359e-1 * t + 6.1274e-2 * std::pow(t, 2) -
42,331 ✔
695
                   3.1516e-2 * std::pow(t, 3) + 7.7446e-3 * std::pow(t, 4) -
42,331 ✔
696
                   1.0595e-3 * std::pow(t, 5) + 7.0568e-5 * std::pow(t, 6) -
42,331 ✔
697
                   1.808e-6 * std::pow(t, 7));
42,331 ✔
698
        stopping_power_radiative(i) *= r;
42,331 ✔
699
        tensor::View<double> dcs_i = dcs.slice(i);
42,331 ✔
700
        dcs_i *= r;
42,331 ✔
701
      }
42,331 ✔
702
    }
703

704
    // Total material stopping power
705
    tensor::Tensor<double> stopping_power =
424 ✔
706
      stopping_power_collision + stopping_power_radiative;
424 ✔
707

708
    // Loop over photon energies
709
    auto f = tensor::zeros<double>({n_e});
424 ✔
710
    auto z = tensor::zeros<double>({n_e});
424 ✔
711
    for (int i = 0; i < n_e - 1; ++i) {
84,662 ✔
712
      double w = data::ttb_e_grid(i);
84,238 ✔
713

714
      // Loop over incident particle energies
715
      for (int j = i; j < n_e; ++j) {
8,579,546 ✔
716
        double e = data::ttb_e_grid(j);
8,495,308 ✔
717

718
        // Reduced photon energy
719
        double k = w / e;
8,495,308 ✔
720

721
        // Find the lower bounding index of the reduced photon energy
722
        int i_k = lower_bound_index(
8,495,308 ✔
723
          data::ttb_k_grid.cbegin(), data::ttb_k_grid.cend(), k);
8,495,308 ✔
724

725
        // Get the interpolation bounds
726
        double k_l = data::ttb_k_grid(i_k);
8,495,308 ✔
727
        double k_r = data::ttb_k_grid(i_k + 1);
8,495,308 ✔
728
        double x_l = dcs(j, i_k);
8,495,308 ✔
729
        double x_r = dcs(j, i_k + 1);
8,495,308 ✔
730

731
        // Find the value of the DCS using linear interpolation in reduced
732
        // photon energy k
733
        double x = x_l + (k - k_l) * (x_r - x_l) / (k_r - k_l);
8,495,308 ✔
734

735
        // Square of the ratio of the speed of light to the velocity of the
736
        // charged particle
737
        double beta_sq = e * (e + 2.0 * MASS_ELECTRON_EV) /
8,495,308 ✔
738
                         ((e + MASS_ELECTRON_EV) * (e + MASS_ELECTRON_EV));
8,495,308 ✔
739

740
        // Compute the integrand of the PDF
741
        f(j) = x / (beta_sq * stopping_power(j) * w);
8,495,308 ✔
742
      }
743

744
      // Number of points to integrate
745
      int n = n_e - i;
84,238 ✔
746

747
      // Integrate the PDF using cubic spline integration over the incident
748
      // particle energy
749
      if (n > 2) {
84,238 ✔
750
        spline(n, &data::ttb_e_grid(i), &f(i), &z(i));
83,814 ✔
751

752
        double c = 0.0;
753
        for (int j = i; j < n_e - 1; ++j) {
8,494,460 ✔
754
          c += spline_integrate(n, &data::ttb_e_grid(i), &f(i), &z(i),
16,821,292 ✔
755
            data::ttb_e_grid(j), data::ttb_e_grid(j + 1));
8,410,646 ✔
756

757
          ttb->pdf(j + 1, i) = c;
8,410,646 ✔
758
        }
759

760
        // Integrate the last two points using trapezoidal rule in log-log space
761
      } else {
762
        double e_l = std::log(data::ttb_e_grid(i));
424 ✔
763
        double e_r = std::log(data::ttb_e_grid(i + 1));
424 ✔
764
        double x_l = std::log(f(i));
424 ✔
765
        double x_r = std::log(f(i + 1));
424 ✔
766

767
        ttb->pdf(i + 1, i) =
848 ✔
768
          0.5 * (e_r - e_l) * (std::exp(e_l + x_l) + std::exp(e_r + x_r));
424 ✔
769
      }
770
    }
771

772
    // Loop over incident particle energies
773
    for (int j = 1; j < n_e; ++j) {
84,662 ✔
774
      // Set last element of PDF to small non-zero value to enable log-log
775
      // interpolation
776
      ttb->pdf(j, j) = std::exp(-500.0);
84,238 ✔
777

778
      // Loop over photon energies
779
      double c = 0.0;
84,238 ✔
780
      for (int i = 0; i < j; ++i) {
8,495,308 ✔
781
        // Integrate the CDF from the PDF using the fact that the PDF is linear
782
        // in log-log space
783
        double w_l = std::log(data::ttb_e_grid(i));
8,411,070 ✔
784
        double w_r = std::log(data::ttb_e_grid(i + 1));
8,411,070 ✔
785
        double x_l = std::log(ttb->pdf(j, i));
8,411,070 ✔
786
        double x_r = std::log(ttb->pdf(j, i + 1));
8,411,070 ✔
787
        double beta = (x_r - x_l) / (w_r - w_l);
8,411,070 ✔
788
        double a = beta + 1.0;
8,411,070 ✔
789
        c += std::exp(w_l + x_l) / a * std::expm1(a * (w_r - w_l));
8,411,070 ✔
790
        ttb->cdf(j, i + 1) = c;
8,411,070 ✔
791
      }
792

793
      // Set photon number yield
794
      ttb->yield(j) = c;
84,238 ✔
795
    }
796

797
    // Use logarithm of number yield since it is log-log interpolated
798
    ttb->yield =
424 ✔
799
      tensor::where(ttb->yield > 0.0, tensor::log(ttb->yield), -500.0);
1,696 ✔
800
  }
2,544 ✔
801
}
212 ✔
802

803
void Material::init_nuclide_index()
9,401 ✔
804
{
805
  int n = settings::run_CE ? data::nuclides.size() : data::mg.nuclides_.size();
9,401 ✔
806
  mat_nuclide_index_.resize(n);
9,401 ✔
807
  std::fill(mat_nuclide_index_.begin(), mat_nuclide_index_.end(), C_NONE);
9,401 ✔
808
  for (int i = 0; i < nuclide_.size(); ++i) {
56,427 ✔
809
    mat_nuclide_index_[nuclide_[i]] = i;
47,026 ✔
810
  }
811
}
9,401 ✔
812

813
void Material::calculate_xs(Particle& p) const
1,361,916,788 ✔
814
{
815
  // Set all material macroscopic cross sections to zero
816
  p.macro_xs().total = 0.0;
1,361,916,788 ✔
817
  p.macro_xs().absorption = 0.0;
1,361,916,788 ✔
818
  p.macro_xs().fission = 0.0;
1,361,916,788 ✔
819
  p.macro_xs().nu_fission = 0.0;
1,361,916,788 ✔
820

821
  if (p.type().is_neutron()) {
1,361,916,788 ✔
822
    this->calculate_neutron_xs(p);
1,344,516,755 ✔
823
  } else if (p.type().is_photon()) {
17,400,033 ✔
824
    this->calculate_photon_xs(p);
17,350,033 ✔
825
  }
826
}
1,361,916,788 ✔
827

828
void Material::calculate_neutron_xs(Particle& p) const
1,344,516,755 ✔
829
{
830
  // Find energy index on energy grid
831
  int neutron = ParticleType::neutron().transport_index();
1,344,516,755 ✔
832
  int i_grid =
1,344,516,755 ✔
833
    std::log(p.E() / data::energy_min[neutron]) / simulation::log_spacing;
1,344,516,755 ✔
834

835
  // Determine if this material has S(a,b) tables
836
  bool check_sab = (thermal_tables_.size() > 0);
1,344,516,755 ✔
837

838
  // Initialize position in i_sab_nuclides
839
  int j = 0;
1,344,516,755 ✔
840

841
  // Calculate NCrystal cross section
842
  double ncrystal_xs = -1.0;
1,344,516,755 ✔
843
  if (ncrystal_mat_ && p.E() < NCRYSTAL_MAX_ENERGY) {
1,344,516,755 !
844
    ncrystal_xs = ncrystal_mat_.xs(p);
5,072,195 ✔
845
  }
846

847
  // Add contribution from each nuclide in material
848
  for (int i = 0; i < nuclide_.size(); ++i) {
2,147,483,647 ✔
849
    // ======================================================================
850
    // CHECK FOR S(A,B) TABLE
851

852
    int i_sab = C_NONE;
2,147,483,647 ✔
853
    double sab_frac = 0.0;
2,147,483,647 ✔
854

855
    // Check if this nuclide matches one of the S(a,b) tables specified.
856
    // This relies on thermal_tables_ being sorted by .index_nuclide
857
    if (check_sab) {
2,147,483,647 ✔
858
      const auto& sab {thermal_tables_[j]};
380,282,540 ✔
859
      if (i == sab.index_nuclide) {
380,282,540 ✔
860
        // Get index in sab_tables
861
        i_sab = sab.index_table;
193,567,740 ✔
862
        sab_frac = sab.fraction;
193,567,740 ✔
863

864
        // If particle energy is greater than the highest energy for the
865
        // S(a,b) table, then don't use the S(a,b) table
866
        if (p.E() > data::thermal_scatt[i_sab]->energy_max_)
193,567,740 ✔
867
          i_sab = C_NONE;
115,697,565 ✔
868

869
        // Increment position in thermal_tables_
870
        ++j;
193,567,740 ✔
871

872
        // Don't check for S(a,b) tables if there are no more left
873
        if (j == thermal_tables_.size())
193,567,740 ✔
874
          check_sab = false;
193,461,585 ✔
875
      }
876
    }
877

878
    // ======================================================================
879
    // CALCULATE MICROSCOPIC CROSS SECTION
880

881
    // Get nuclide index
882
    int i_nuclide = nuclide_[i];
2,147,483,647 ✔
883

884
    // Update microscopic cross section for this nuclide
885
    p.update_neutron_xs(i_nuclide, i_grid, i_sab, sab_frac, ncrystal_xs);
2,147,483,647 ✔
886
    auto& micro = p.neutron_xs(i_nuclide);
2,147,483,647 ✔
887

888
    // ======================================================================
889
    // ADD TO MACROSCOPIC CROSS SECTION
890

891
    // Copy atom density of nuclide in material
892
    double atom_density = this->atom_density(i, p.density_mult());
2,147,483,647 ✔
893

894
    // Add contributions to cross sections
895
    p.macro_xs().total += atom_density * micro.total;
2,147,483,647 ✔
896
    p.macro_xs().absorption += atom_density * micro.absorption;
2,147,483,647 ✔
897
    p.macro_xs().fission += atom_density * micro.fission;
2,147,483,647 ✔
898
    p.macro_xs().nu_fission += atom_density * micro.nu_fission;
2,147,483,647 ✔
899
  }
900
}
1,344,516,755 ✔
901

902
void Material::calculate_photon_xs(Particle& p) const
17,350,033 ✔
903
{
904
  p.macro_xs().coherent = 0.0;
17,350,033 ✔
905
  p.macro_xs().incoherent = 0.0;
17,350,033 ✔
906
  p.macro_xs().photoelectric = 0.0;
17,350,033 ✔
907
  p.macro_xs().pair_production = 0.0;
17,350,033 ✔
908

909
  // Add contribution from each nuclide in material
910
  for (int i = 0; i < nuclide_.size(); ++i) {
239,818,306 ✔
911
    // ========================================================================
912
    // CALCULATE MICROSCOPIC CROSS SECTION
913

914
    // Determine microscopic cross sections for this nuclide
915
    int i_element = element_[i];
222,468,273 ✔
916

917
    // Calculate microscopic cross section for this nuclide
918
    const auto& micro {p.photon_xs(i_element)};
222,468,273 ✔
919
    if (p.E() != micro.last_E) {
222,468,273 ✔
920
      data::elements[i_element]->calculate_xs(p);
87,785,726 ✔
921
    }
922

923
    // ========================================================================
924
    // ADD TO MACROSCOPIC CROSS SECTION
925

926
    // Copy atom density of nuclide in material
927
    double atom_density = this->atom_density(i, p.density_mult());
222,468,273 ✔
928

929
    // Add contributions to material macroscopic cross sections
930
    p.macro_xs().total += atom_density * micro.total;
222,468,273 ✔
931
    p.macro_xs().coherent += atom_density * micro.coherent;
222,468,273 ✔
932
    p.macro_xs().incoherent += atom_density * micro.incoherent;
222,468,273 ✔
933
    p.macro_xs().photoelectric += atom_density * micro.photoelectric;
222,468,273 ✔
934
    p.macro_xs().pair_production += atom_density * micro.pair_production;
222,468,273 ✔
935
  }
936
}
17,350,033 ✔
937

938
void Material::set_id(int32_t id)
7,734 ✔
939
{
940
  assert(id >= 0 || id == C_NONE);
7,734 !
941

942
  // Clear entry in material map if an ID was already assigned before
943
  if (id_ != C_NONE) {
7,734 !
944
    model::material_map.erase(id_);
×
945
    id_ = C_NONE;
×
946
  }
947

948
  // Make sure no other material has same ID
949
  if (model::material_map.find(id) != model::material_map.end()) {
7,734 !
950
    throw std::runtime_error {
×
951
      "Two materials have the same ID: " + std::to_string(id)};
×
952
  }
953

954
  // If no ID specified, auto-assign next ID in sequence
955
  if (id == C_NONE) {
7,734 !
956
    id = 0;
×
957
    for (const auto& m : model::materials) {
×
958
      id = std::max(id, m->id_);
×
959
    }
960
    ++id;
×
961
  }
962

963
  // Update ID and entry in material map
964
  id_ = id;
7,734 ✔
965
  model::material_map[id] = index_;
7,734 ✔
966
}
7,734 ✔
967

968
void Material::set_density(double density, const std::string& units)
3,207 ✔
969
{
970
  assert(density >= 0.0);
3,207 !
971

972
  if (nuclide_.empty()) {
3,207 !
973
    throw std::runtime_error {"No nuclides exist in material yet."};
×
974
  }
975

976
  if (units == "atom/b-cm") {
3,207 ✔
977
    // Set total density based on value provided
978
    density_ = density;
3,187 ✔
979

980
    // Determine normalized atom percents
981
    double sum_percent = atom_density_.sum();
3,187 ✔
982
    atom_density_ /= sum_percent;
36,185 ✔
983

984
    // Recalculate nuclide atom densities based on given density
985
    atom_density_ *= density;
3,187 ✔
986

987
    // Calculate density in g/cm^3 and charge density in [e/b-cm]
988
    density_gpcc_ = 0.0;
3,187 ✔
989
    charge_density_ = 0.0;
3,187 ✔
990
    for (int i = 0; i < nuclide_.size(); ++i) {
32,998 ✔
991
      int i_nuc = nuclide_[i];
29,811 !
992
      double awr = data::nuclides[i_nuc]->awr_;
29,811 !
993
      int z = settings::run_CE ? data::nuclides[i_nuc]->Z_ : 0.0;
29,811 !
994
      density_gpcc_ += atom_density_(i) * awr * MASS_NEUTRON / N_AVOGADRO;
29,811 ✔
995
      charge_density_ += atom_density_(i) * z;
29,811 ✔
996
    }
997
  } else if (units == "g/cm3" || units == "g/cc") {
25 !
998
    // Determine factor by which to change densities
999
    double previous_density_gpcc = density_gpcc_;
15 ✔
1000
    double f = density / previous_density_gpcc;
15 ✔
1001

1002
    // Update densities
1003
    density_gpcc_ = density;
15 ✔
1004
    density_ *= f;
15 ✔
1005
    atom_density_ *= f;
15 ✔
1006
    charge_density_ *= f;
15 ✔
1007
  } else {
1008
    throw std::invalid_argument {
10 ✔
1009
      "Invalid units '" + std::string(units.data()) + "' specified."};
10 ✔
1010
  }
1011
}
3,202 ✔
1012

1013
void Material::set_densities(
3,102 ✔
1014
  const vector<std::string>& name, const vector<double>& density)
1015
{
1016
  auto n = name.size();
3,102 !
1017
  assert(n > 0);
3,102 !
1018
  assert(n == density.size());
3,102 !
1019

1020
  if (n != nuclide_.size()) {
3,102 ✔
1021
    nuclide_.resize(n);
733 ✔
1022
    atom_density_ = tensor::zeros<double>({n});
733 ✔
1023
    if (settings::photon_transport)
733 !
1024
      element_.resize(n);
×
1025
  }
1026

1027
  double sum_density = 0.0;
1028
  for (int64_t i = 0; i < n; ++i) {
32,538 ✔
1029
    const auto& nuc {name[i]};
29,436 ✔
1030
    if (data::nuclide_map.find(nuc) == data::nuclide_map.end()) {
29,436 !
1031
      int err = openmc_load_nuclide(nuc.c_str(), nullptr, 0);
×
1032
      if (err < 0)
×
1033
        throw std::runtime_error {get_errmsg()};
×
1034
    }
1035

1036
    nuclide_[i] = data::nuclide_map.at(nuc);
29,436 !
1037
    assert(density[i] > 0.0);
29,436 !
1038
    atom_density_(i) = density[i];
29,436 !
1039
    sum_density += density[i];
29,436 ✔
1040

1041
    if (settings::photon_transport) {
29,436 !
1042
      auto element_name = to_element(nuc);
×
1043
      element_[i] = data::element_map.at(element_name);
×
1044
    }
×
1045
  }
1046

1047
  // Set total density to the sum of the vector
1048
  this->set_density(sum_density, "atom/b-cm");
3,102 ✔
1049

1050
  // Generate material bremsstrahlung data for electrons and positrons
1051
  if (settings::photon_transport &&
3,102 !
1052
      settings::electron_treatment == ElectronTreatment::TTB) {
×
1053
    this->init_bremsstrahlung();
×
1054
  }
1055

1056
  // Assign S(a,b) tables
1057
  this->init_thermal();
3,102 ✔
1058
}
3,102 ✔
1059

1060
double Material::volume() const
25 ✔
1061
{
1062
  if (volume_ < 0.0) {
25 ✔
1063
    throw std::runtime_error {
5 ✔
1064
      "Volume for material with ID=" + std::to_string(id_) + " not set."};
10 ✔
1065
  }
1066
  return volume_;
20 ✔
1067
}
1068

1069
double Material::temperature() const
9,500 ✔
1070
{
1071
  // If material doesn't have an assigned temperature, use global default
1072
  return temperature_ >= 0 ? temperature_ : settings::temperature_default;
9,500 ✔
1073
}
1074

1075
void Material::to_hdf5(hid_t group) const
6,836 ✔
1076
{
1077
  hid_t material_group = create_group(group, "material " + std::to_string(id_));
6,836 ✔
1078

1079
  write_attribute(material_group, "depletable", static_cast<int>(depletable()));
6,836 ✔
1080
  if (volume_ > 0.0) {
6,836 ✔
1081
    write_attribute(material_group, "volume", volume_);
959 ✔
1082
  }
1083
  if (temperature_ > 0.0) {
6,836 ✔
1084
    write_attribute(material_group, "temperature", temperature_);
884 ✔
1085
  }
1086
  write_dataset(material_group, "name", name_);
6,836 ✔
1087
  write_dataset(material_group, "atom_density", density_);
6,836 ✔
1088

1089
  // Copy nuclide/macro name for each nuclide to vector
1090
  vector<std::string> nuc_names;
6,836 ✔
1091
  vector<std::string> macro_names;
6,836 ✔
1092
  vector<double> nuc_densities;
6,836 ✔
1093
  if (settings::run_CE) {
6,836 ✔
1094
    for (int i = 0; i < nuclide_.size(); ++i) {
28,072 ✔
1095
      int i_nuc = nuclide_[i];
22,431 ✔
1096
      nuc_names.push_back(data::nuclides[i_nuc]->name_);
22,431 ✔
1097
      nuc_densities.push_back(atom_density_(i));
22,431 ✔
1098
    }
1099
  } else {
1100
    for (int i = 0; i < nuclide_.size(); ++i) {
2,405 ✔
1101
      int i_nuc = nuclide_[i];
1,210 ✔
1102
      if (data::mg.nuclides_[i_nuc].awr != MACROSCOPIC_AWR) {
1,210 ✔
1103
        nuc_names.push_back(data::mg.nuclides_[i_nuc].name);
40 ✔
1104
        nuc_densities.push_back(atom_density_(i));
40 ✔
1105
      } else {
1106
        macro_names.push_back(data::mg.nuclides_[i_nuc].name);
1,170 ✔
1107
      }
1108
    }
1109
  }
1110

1111
  // Write vector to 'nuclides'
1112
  if (!nuc_names.empty()) {
6,836 ✔
1113
    write_dataset(material_group, "nuclides", nuc_names);
5,666 ✔
1114
    write_dataset(material_group, "nuclide_densities", nuc_densities);
5,666 ✔
1115
  }
1116

1117
  // Write vector to 'macroscopics'
1118
  if (!macro_names.empty()) {
6,836 ✔
1119
    write_dataset(material_group, "macroscopics", macro_names);
1,170 ✔
1120
  }
1121

1122
  if (!thermal_tables_.empty()) {
6,836 ✔
1123
    vector<std::string> sab_names;
803 ✔
1124
    for (const auto& table : thermal_tables_) {
1,636 ✔
1125
      sab_names.push_back(data::thermal_scatt[table.index_table]->name_);
833 ✔
1126
    }
1127
    write_dataset(material_group, "sab_names", sab_names);
803 ✔
1128
  }
803 ✔
1129

1130
  close_group(material_group);
6,836 ✔
1131
}
6,836 ✔
1132

1133
void Material::export_properties_hdf5(hid_t group) const
75 ✔
1134
{
1135
  hid_t material_group = create_group(group, "material " + std::to_string(id_));
75 ✔
1136
  write_attribute(material_group, "atom_density", density_);
75 ✔
1137
  write_attribute(material_group, "mass_density", density_gpcc_);
75 ✔
1138
  close_group(material_group);
75 ✔
1139
}
75 ✔
1140

1141
void Material::import_properties_hdf5(hid_t group)
60 ✔
1142
{
1143
  hid_t material_group = open_group(group, "material " + std::to_string(id_));
60 ✔
1144
  double density;
60 ✔
1145
  read_attribute(material_group, "atom_density", density);
60 ✔
1146
  this->set_density(density, "atom/b-cm");
60 ✔
1147
  close_group(material_group);
60 ✔
1148
}
60 ✔
1149

1150
void Material::add_nuclide(const std::string& name, double density)
5 ✔
1151
{
1152
  // Check if nuclide is already in material
1153
  for (int i = 0; i < nuclide_.size(); ++i) {
25 ✔
1154
    int i_nuc = nuclide_[i];
20 !
1155
    if (data::nuclides[i_nuc]->name_ == name) {
20 !
1156
      double awr = data::nuclides[i_nuc]->awr_;
×
1157
      density_ += density - atom_density_(i);
×
1158
      density_gpcc_ +=
×
1159
        (density - atom_density_(i)) * awr * MASS_NEUTRON / N_AVOGADRO;
×
1160
      atom_density_(i) = density;
×
1161
      return;
×
1162
    }
1163
  }
1164

1165
  // If nuclide wasn't found, extend nuclide/density arrays
1166
  int err = openmc_load_nuclide(name.c_str(), nullptr, 0);
5 ✔
1167
  if (err < 0)
5 !
1168
    throw std::runtime_error {get_errmsg()};
×
1169

1170
  // Append new nuclide/density
1171
  int i_nuc = data::nuclide_map[name];
5 ✔
1172
  nuclide_.push_back(i_nuc);
5 ✔
1173

1174
  // Append new element if photon transport is on
1175
  if (settings::photon_transport) {
5 !
1176
    int i_elem = data::element_map[to_element(name)];
×
1177
    element_.push_back(i_elem);
×
1178
  }
1179

1180
  auto n = nuclide_.size();
5 ✔
1181

1182
  // Create copy of atom_density_ array with one extra entry
1183
  tensor::Tensor<double> atom_density = tensor::zeros<double>({n});
5 ✔
1184
  atom_density.slice(tensor::range(0, n - 1)) = atom_density_;
5 ✔
1185
  atom_density(n - 1) = density;
5 ✔
1186
  atom_density_ = atom_density;
5 ✔
1187

1188
  density_ += density;
5 ✔
1189
  density_gpcc_ +=
5 ✔
1190
    density * data::nuclides[i_nuc]->awr_ * MASS_NEUTRON / N_AVOGADRO;
5 ✔
1191
}
5 ✔
1192

1193
//==============================================================================
1194
// Non-method functions
1195
//==============================================================================
1196

1197
double sternheimer_adjustment(const vector<double>& f,
424 ✔
1198
  const vector<double>& e_b_sq, double e_p_sq, double n_conduction,
1199
  double log_I, double tol, int max_iter)
1200
{
1201
  // Get the total number of oscillators
1202
  int n = f.size();
424 ✔
1203

1204
  // Calculate the Sternheimer adjustment factor using Newton's method
1205
  double rho = 2.0;
424 ✔
1206
  int iter;
424 ✔
1207
  for (iter = 0; iter < max_iter; ++iter) {
3,626 ✔
1208
    double rho_0 = rho;
1209

1210
    // Function to find the root of and its derivative
1211
    double g = 0.0;
1212
    double gp = 0.0;
1213

1214
    for (int i = 0; i < n; ++i) {
60,176 ✔
1215
      // Square of resonance energy of a bound-shell oscillator
1216
      double e_r_sq = e_b_sq[i] * rho * rho + 2.0 / 3.0 * f[i] * e_p_sq;
56,570 ✔
1217
      g += f[i] * std::log(e_r_sq);
56,570 ✔
1218
      gp += e_b_sq[i] * f[i] * rho / e_r_sq;
56,570 ✔
1219
    }
1220
    // Include conduction electrons
1221
    if (n_conduction > 0.0) {
3,606 ✔
1222
      g += n_conduction * std::log(n_conduction * e_p_sq);
1,412 ✔
1223
    }
1224

1225
    // Set the next guess: rho_n+1 = rho_n - g(rho_n)/g'(rho_n)
1226
    rho -= (g - 2.0 * log_I) / (2.0 * gp);
3,606 ✔
1227

1228
    // If the initial guess is too large, rho can be negative
1229
    if (rho < 0.0)
3,606 ✔
1230
      rho = rho_0 / 2.0;
2,000 ✔
1231

1232
    // Check for convergence
1233
    if (std::abs(rho - rho_0) / rho_0 < tol)
3,606 ✔
1234
      break;
1235
  }
1236
  // Did not converge
1237
  if (iter >= max_iter) {
424 ✔
1238
    warning("Maximum Newton-Raphson iterations exceeded.");
20 ✔
1239
    rho = 1.0e-6;
20 ✔
1240
  }
1241
  return rho;
424 ✔
1242
}
1243

1244
double density_effect(const vector<double>& f, const vector<double>& e_b_sq,
84,662 ✔
1245
  double e_p_sq, double n_conduction, double rho, double E, double tol,
1246
  int max_iter)
1247
{
1248
  // Get the total number of oscillators
1249
  int n = f.size();
84,662 ✔
1250

1251
  // Square of the ratio of the speed of light to the velocity of the charged
1252
  // particle
1253
  double beta_sq = E * (E + 2.0 * MASS_ELECTRON_EV) /
84,662 ✔
1254
                   ((E + MASS_ELECTRON_EV) * (E + MASS_ELECTRON_EV));
84,662 ✔
1255

1256
  // For nonmetals, delta = 0 for beta < beta_0, where beta_0 is obtained by
1257
  // setting the frequency w = 0.
1258
  double beta_0_sq = 0.0;
84,662 ✔
1259
  if (n_conduction == 0.0) {
84,662 ✔
1260
    for (int i = 0; i < n; ++i) {
77,600 ✔
1261
      beta_0_sq += f[i] * e_p_sq / (e_b_sq[i] * rho * rho);
62,800 ✔
1262
    }
1263
    beta_0_sq = 1.0 / (1.0 + beta_0_sq);
14,800 ✔
1264
  }
1265
  double delta = 0.0;
84,662 ✔
1266
  if (beta_sq < beta_0_sq)
84,662 ✔
1267
    return delta;
1268

1269
  // Compute the square of the frequency w^2 using Newton's method, with the
1270
  // initial guess of w^2 equal to beta^2 * gamma^2
1271
  double w_sq = E / MASS_ELECTRON_EV * (E / MASS_ELECTRON_EV + 2);
79,160 ✔
1272
  int iter;
79,160 ✔
1273
  for (iter = 0; iter < max_iter; ++iter) {
550,976 !
1274
    double w_sq_0 = w_sq;
1275

1276
    // Function to find the root of and its derivative
1277
    double g = 0.0;
1278
    double gp = 0.0;
1279

1280
    for (int i = 0; i < n; ++i) {
20,687,854 ✔
1281
      double c = e_b_sq[i] * rho * rho / e_p_sq + w_sq;
20,136,878 ✔
1282
      g += f[i] / c;
20,136,878 ✔
1283
      gp -= f[i] / (c * c);
20,136,878 ✔
1284
    }
1285
    // Include conduction electrons
1286
    g += n_conduction / w_sq;
550,976 ✔
1287
    gp -= n_conduction / (w_sq * w_sq);
550,976 ✔
1288

1289
    // Set the next guess: w_n+1 = w_n - g(w_n)/g'(w_n)
1290
    w_sq -= (g + 1.0 - 1.0 / beta_sq) / gp;
550,976 ✔
1291

1292
    // If the initial guess is too large, w can be negative
1293
    if (w_sq < 0.0)
550,976 ✔
1294
      w_sq = w_sq_0 / 2.0;
119,916 ✔
1295

1296
    // Check for convergence
1297
    if (std::abs(w_sq - w_sq_0) / w_sq_0 < tol)
550,976 ✔
1298
      break;
1299
  }
1300
  // Did not converge
1301
  if (iter >= max_iter) {
79,160 !
1302
    warning("Maximum Newton-Raphson iterations exceeded: setting density "
×
1303
            "effect correction to zero.");
1304
    return delta;
×
1305
  }
1306

1307
  // Solve for the density effect correction
1308
  for (int i = 0; i < n; ++i) {
2,724,452 ✔
1309
    double l_sq = e_b_sq[i] * rho * rho / e_p_sq + 2.0 / 3.0 * f[i];
2,645,292 ✔
1310
    delta += f[i] * std::log((l_sq + w_sq) / l_sq);
2,645,292 ✔
1311
  }
1312
  // Include conduction electrons
1313
  if (n_conduction > 0.0) {
79,160 ✔
1314
    delta += n_conduction * std::log((n_conduction + w_sq) / n_conduction);
69,862 ✔
1315
  }
1316

1317
  return delta - w_sq * (1.0 - beta_sq);
79,160 ✔
1318
}
1319

NEW
1320
int32_t get_material_by_name(const std::string& mat_name)
×
1321
{
NEW
1322
  for (int i = 0; i < model::materials.size(); i++) {
×
NEW
1323
    std::string name = model::materials[i]->name();
×
NEW
1324
    if (to_lower(name) == mat_name) {
×
NEW
1325
      return i;
×
1326
    }
NEW
1327
  }
×
1328
  return MATERIAL_INVALID;
1329
}
1330

1331
void read_materials_xml()
607 ✔
1332
{
1333
  write_message("Reading materials XML file...", 5);
607 ✔
1334

1335
  pugi::xml_document doc;
607 ✔
1336

1337
  // Check if materials.xml exists
1338
  std::string filename = settings::path_input + "materials.xml";
607 ✔
1339
  if (!file_exists(filename)) {
607 !
1340
    fatal_error("Material XML file '" + filename + "' does not exist!");
×
1341
  }
1342

1343
  // Parse materials.xml file and get root element
1344
  doc.load_file(filename.c_str());
607 ✔
1345

1346
  // Loop over XML material elements and populate the array.
1347
  pugi::xml_node root = doc.document_element();
607 ✔
1348

1349
  read_materials_xml(root);
607 ✔
1350
}
607 ✔
1351

1352
void read_materials_xml(pugi::xml_node root)
4,202 ✔
1353
{
1354
  for (pugi::xml_node material_node : root.children("material")) {
11,916 ✔
1355
    model::materials.push_back(make_unique<Material>(material_node));
15,428 ✔
1356
  }
1357
  model::materials.shrink_to_fit();
4,202 ✔
1358
}
4,202 ✔
1359

1360
void free_memory_material()
4,249 ✔
1361
{
1362
  model::materials.clear();
4,249 ✔
1363
  model::material_map.clear();
4,249 ✔
1364
}
4,249 ✔
1365

1366
//==============================================================================
1367
// C API
1368
//==============================================================================
1369

1370
extern "C" int openmc_get_material_index(int32_t id, int32_t* index)
4,310 ✔
1371
{
1372
  auto it = model::material_map.find(id);
4,310 ✔
1373
  if (it == model::material_map.end()) {
4,310 ✔
1374
    set_errmsg("No material exists with ID=" + std::to_string(id) + ".");
10 ✔
1375
    return OPENMC_E_INVALID_ID;
5 ✔
1376
  } else {
1377
    *index = it->second;
4,305 ✔
1378
    return 0;
4,305 ✔
1379
  }
1380
}
1381

1382
extern "C" int openmc_material_add_nuclide(
5 ✔
1383
  int32_t index, const char* name, double density)
1384
{
1385
  int err = 0;
5 ✔
1386
  if (index >= 0 && index < model::materials.size()) {
5 !
1387
    try {
5 ✔
1388
      model::materials[index]->add_nuclide(name, density);
5 ✔
1389
    } catch (const std::runtime_error& e) {
×
1390
      return OPENMC_E_DATA;
×
1391
    }
×
1392
  } else {
1393
    set_errmsg("Index in materials array is out of bounds.");
×
1394
    return OPENMC_E_OUT_OF_BOUNDS;
×
1395
  }
1396
  return err;
5 ✔
1397
}
1398

1399
extern "C" int openmc_material_get_densities(
225 ✔
1400
  int32_t index, const int** nuclides, const double** densities, int* n)
1401
{
1402
  if (index >= 0 && index < model::materials.size()) {
225 !
1403
    auto& mat = model::materials[index];
225 !
1404
    if (!mat->nuclides().empty()) {
225 !
1405
      *nuclides = mat->nuclides().data();
225 ✔
1406
      *densities = mat->densities().data();
225 ✔
1407
      *n = mat->nuclides().size();
225 ✔
1408
      return 0;
225 ✔
1409
    } else {
1410
      set_errmsg("Material atom density array has not been allocated.");
×
1411
      return OPENMC_E_ALLOCATE;
×
1412
    }
1413
  } else {
1414
    set_errmsg("Index in materials array is out of bounds.");
×
1415
    return OPENMC_E_OUT_OF_BOUNDS;
×
1416
  }
1417
}
1418

1419
extern "C" int openmc_material_get_density(int32_t index, double* density)
15 ✔
1420
{
1421
  if (index >= 0 && index < model::materials.size()) {
15 !
1422
    auto& mat = model::materials[index];
15 !
1423
    *density = mat->density_gpcc();
15 !
1424
    return 0;
15 ✔
1425
  } else {
1426
    set_errmsg("Index in materials array is out of bounds.");
×
1427
    return OPENMC_E_OUT_OF_BOUNDS;
×
1428
  }
1429
}
1430

1431
extern "C" int openmc_material_get_fissionable(int32_t index, bool* fissionable)
×
1432
{
1433
  if (index >= 0 && index < model::materials.size()) {
×
1434
    *fissionable = model::materials[index]->fissionable();
×
1435
    return 0;
×
1436
  } else {
1437
    set_errmsg("Index in materials array is out of bounds.");
×
1438
    return OPENMC_E_OUT_OF_BOUNDS;
×
1439
  }
1440
}
1441

1442
extern "C" int openmc_material_get_id(int32_t index, int32_t* id)
5,080 ✔
1443
{
1444
  if (index >= 0 && index < model::materials.size()) {
5,080 !
1445
    *id = model::materials[index]->id();
5,080 ✔
1446
    return 0;
5,080 ✔
1447
  } else {
1448
    set_errmsg("Index in materials array is out of bounds.");
×
1449
    return OPENMC_E_OUT_OF_BOUNDS;
×
1450
  }
1451
}
1452

1453
extern "C" int openmc_material_get_temperature(
30 ✔
1454
  int32_t index, double* temperature)
1455
{
1456
  if (index < 0 || index >= model::materials.size()) {
30 !
1457
    set_errmsg("Index in materials array is out of bounds.");
×
1458
    return OPENMC_E_OUT_OF_BOUNDS;
×
1459
  }
1460
  *temperature = model::materials[index]->temperature();
30 ✔
1461
  return 0;
30 ✔
1462
}
1463

1464
extern "C" int openmc_material_get_volume(int32_t index, double* volume)
25 ✔
1465
{
1466
  if (index >= 0 && index < model::materials.size()) {
25 !
1467
    try {
25 ✔
1468
      *volume = model::materials[index]->volume();
25 ✔
1469
    } catch (const std::exception& e) {
5 !
1470
      set_errmsg(e.what());
5 ✔
1471
      return OPENMC_E_UNASSIGNED;
5 ✔
1472
    }
5 ✔
1473
    return 0;
20 ✔
1474
  } else {
1475
    set_errmsg("Index in materials array is out of bounds.");
×
1476
    return OPENMC_E_OUT_OF_BOUNDS;
×
1477
  }
1478
}
1479

1480
extern "C" int openmc_material_set_density(
45 ✔
1481
  int32_t index, double density, const char* units)
1482
{
1483
  if (index >= 0 && index < model::materials.size()) {
45 !
1484
    try {
45 ✔
1485
      model::materials[index]->set_density(density, units);
50 ✔
1486
    } catch (const std::exception& e) {
5 !
1487
      set_errmsg(e.what());
5 ✔
1488
      return OPENMC_E_UNASSIGNED;
5 ✔
1489
    }
5 ✔
1490
  } else {
1491
    set_errmsg("Index in materials array is out of bounds.");
×
1492
    return OPENMC_E_OUT_OF_BOUNDS;
×
1493
  }
1494
  return 0;
40 ✔
1495
}
1496

1497
extern "C" int openmc_material_set_densities(
3,102 ✔
1498
  int32_t index, int n, const char** name, const double* density)
1499
{
1500
  if (index >= 0 && index < model::materials.size()) {
3,102 !
1501
    try {
3,102 ✔
1502
      model::materials[index]->set_densities(
3,102 ✔
1503
        {name, name + n}, {density, density + n});
3,102 ✔
1504
    } catch (const std::exception& e) {
×
1505
      set_errmsg(e.what());
×
1506
      return OPENMC_E_UNASSIGNED;
×
1507
    }
×
1508
  } else {
1509
    set_errmsg("Index in materials array is out of bounds.");
×
1510
    return OPENMC_E_OUT_OF_BOUNDS;
×
1511
  }
1512
  return 0;
3,102 ✔
1513
}
1514

1515
extern "C" int openmc_material_set_id(int32_t index, int32_t id)
20 ✔
1516
{
1517
  if (index >= 0 && index < model::materials.size()) {
20 !
1518
    try {
20 ✔
1519
      model::materials.at(index)->set_id(id);
20 ✔
1520
    } catch (const std::exception& e) {
×
1521
      set_errmsg(e.what());
×
1522
      return OPENMC_E_UNASSIGNED;
×
1523
    }
×
1524
  } else {
1525
    set_errmsg("Index in materials array is out of bounds.");
×
1526
    return OPENMC_E_OUT_OF_BOUNDS;
×
1527
  }
1528
  return 0;
1529
}
1530

1531
extern "C" int openmc_material_get_name(int32_t index, const char** name)
105 ✔
1532
{
1533
  if (index < 0 || index >= model::materials.size()) {
105 !
1534
    set_errmsg("Index in materials array is out of bounds.");
×
1535
    return OPENMC_E_OUT_OF_BOUNDS;
×
1536
  }
1537

1538
  *name = model::materials[index]->name().data();
105 ✔
1539

1540
  return 0;
105 ✔
1541
}
1542

1543
extern "C" int openmc_material_set_name(int32_t index, const char* name)
5 ✔
1544
{
1545
  if (index < 0 || index >= model::materials.size()) {
5 !
1546
    set_errmsg("Index in materials array is out of bounds.");
×
1547
    return OPENMC_E_OUT_OF_BOUNDS;
×
1548
  }
1549

1550
  model::materials[index]->set_name(name);
10 ✔
1551

1552
  return 0;
5 ✔
1553
}
1554

1555
extern "C" int openmc_material_set_volume(int32_t index, double volume)
15 ✔
1556
{
1557
  if (index >= 0 && index < model::materials.size()) {
15 !
1558
    auto& m {model::materials[index]};
15 !
1559
    if (volume >= 0.0) {
15 !
1560
      m->volume_ = volume;
15 ✔
1561
      return 0;
15 ✔
1562
    } else {
1563
      set_errmsg("Volume must be non-negative");
×
1564
      return OPENMC_E_INVALID_ARGUMENT;
×
1565
    }
1566
  } else {
1567
    set_errmsg("Index in materials array is out of bounds.");
×
1568
    return OPENMC_E_OUT_OF_BOUNDS;
×
1569
  }
1570
}
1571

1572
extern "C" int openmc_material_get_depletable(int32_t index, bool* depletable)
10 ✔
1573
{
1574
  if (index < 0 || index >= model::materials.size()) {
10 !
1575
    set_errmsg("Index in materials array is out of bounds.");
×
1576
    return OPENMC_E_OUT_OF_BOUNDS;
×
1577
  }
1578

1579
  *depletable = model::materials[index]->depletable();
10 ✔
1580

1581
  return 0;
10 ✔
1582
}
1583

1584
extern "C" int openmc_material_set_depletable(int32_t index, bool depletable)
5 ✔
1585
{
1586
  if (index < 0 || index >= model::materials.size()) {
5 !
1587
    set_errmsg("Index in materials array is out of bounds.");
×
1588
    return OPENMC_E_OUT_OF_BOUNDS;
×
1589
  }
1590

1591
  model::materials[index]->depletable() = depletable;
5 ✔
1592

1593
  return 0;
5 ✔
1594
}
1595

1596
extern "C" int openmc_extend_materials(
20 ✔
1597
  int32_t n, int32_t* index_start, int32_t* index_end)
1598
{
1599
  if (index_start)
20 !
1600
    *index_start = model::materials.size();
20 ✔
1601
  if (index_end)
20 !
1602
    *index_end = model::materials.size() + n - 1;
×
1603
  for (int32_t i = 0; i < n; i++) {
40 ✔
1604
    model::materials.push_back(make_unique<Material>());
20 ✔
1605
  }
1606
  return 0;
20 ✔
1607
}
1608

1609
extern "C" size_t n_materials()
65 ✔
1610
{
1611
  return model::materials.size();
65 ✔
1612
}
1613

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