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

openmc-dev / openmc / 30830496237

03 Aug 2026 04:04PM UTC coverage: 81.469% (+0.04%) from 81.429%
30830496237

Pull #3971

github

web-flow
Merge 1d36d1147 into 5982acdf8
Pull Request #3971: Delta tracking

18832 of 27249 branches covered (69.11%)

Branch coverage included in aggregate %.

597 of 645 new or added lines in 20 files covered. (92.56%)

60805 of 70502 relevant lines covered (86.25%)

50421428.58 hits per line

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

85.61
/src/geometry_aux.cpp
1
#include "openmc/geometry_aux.h"
2

3
#include <algorithm> // for std::max
4
#include <sstream>
5
#include <unordered_set>
6

7
#include <fmt/core.h>
8
#include <pugixml.hpp>
9

10
#include "openmc/cell.h"
11
#include "openmc/constants.h"
12
#include "openmc/container_util.h"
13
#include "openmc/dagmc.h"
14
#include "openmc/error.h"
15
#include "openmc/file_utils.h"
16
#include "openmc/geometry.h"
17
#include "openmc/lattice.h"
18
#include "openmc/material.h"
19
#include "openmc/settings.h"
20
#include "openmc/surface.h"
21
#include "openmc/tallies/filter.h"
22
#include "openmc/tallies/filter_cell_instance.h"
23
#include "openmc/tallies/filter_distribcell.h"
24

25
namespace openmc {
26

27
namespace model {
28
std::unordered_map<int32_t, int32_t> universe_level_counts;
29
} // namespace model
30

31
void read_geometry_xml()
1,413✔
32
{
33
  // Display output message
34
  write_message("Reading geometry XML file...", 5);
1,413✔
35

36
  // Check if geometry.xml exists
37
  std::string filename = settings::path_input + "geometry.xml";
1,413✔
38
  if (!file_exists(filename)) {
1,413!
39
    fatal_error("Geometry XML file '" + filename + "' does not exist!");
×
40
  }
41

42
  // Parse settings.xml file
43
  pugi::xml_document doc;
1,413✔
44
  auto result = doc.load_file(filename.c_str());
1,413✔
45
  if (!result) {
1,413!
46
    fatal_error("Error processing geometry.xml file.");
×
47
  }
48

49
  // Get root element
50
  pugi::xml_node root = doc.document_element();
1,413✔
51

52
  read_geometry_xml(root);
1,413✔
53
}
1,413✔
54

55
void read_geometry_xml(pugi::xml_node root)
9,267✔
56
{
57
  // Read surfaces, cells, lattice
58
  std::set<std::pair<int, int>> periodic_pairs;
9,267✔
59
  std::unordered_map<int, double> albedo_map;
9,267✔
60
  std::unordered_map<int, int> periodic_sense_map;
9,267✔
61

62
  read_surfaces(root, periodic_pairs, albedo_map, periodic_sense_map);
9,267✔
63
  read_cells(root);
9,267✔
64
  prepare_boundary_conditions(periodic_pairs, albedo_map, periodic_sense_map);
9,265✔
65
  read_lattices(root);
9,265✔
66

67
  // Check to make sure a boundary condition was applied to at least one
68
  // surface
69
  bool boundary_exists = false;
9,265✔
70
  for (const auto& surf : model::surfaces) {
22,397✔
71
    if (surf->bc_) {
22,364✔
72
      boundary_exists = true;
73
      break;
74
    }
75
  }
76

77
  if (settings::run_mode != RunMode::PLOTTING &&
9,265✔
78
      settings::run_mode != RunMode::VOLUME && !boundary_exists) {
8,226!
79
    fatal_error("No boundary conditions were applied to any surfaces!");
×
80
  }
81

82
  // Allocate universes, universe cell arrays, and assign base universe
83
  model::root_universe = find_root_universe();
9,265✔
84

85
  // if the root universe is DAGMC geometry, make sure the model is well-formed
86
  check_dagmc_root_univ();
9,265✔
87
}
18,530✔
88

89
//==============================================================================
90

91
void adjust_indices()
9,267✔
92
{
93
  // Adjust material/fill idices.
94
  for (auto& c : model::cells) {
46,366✔
95
    if (c->fill_ != C_NONE) {
37,099✔
96
      int32_t id = c->fill_;
7,423✔
97
      auto search_univ = model::universe_map.find(id);
7,423✔
98
      auto search_lat = model::lattice_map.find(id);
7,423✔
99
      if (search_univ != model::universe_map.end()) {
7,423✔
100
        c->type_ = Fill::UNIVERSE;
5,280✔
101
        c->fill_ = search_univ->second;
5,280✔
102
      } else if (search_lat != model::lattice_map.end()) {
2,143!
103
        c->type_ = Fill::LATTICE;
2,143✔
104
        c->fill_ = search_lat->second;
2,143✔
105
      } else {
106
        fatal_error(fmt::format("Specified fill {} on cell {} is neither a "
×
107
                                "universe nor a lattice.",
108
          id, c->id_));
×
109
      }
110
    } else {
111
      c->type_ = Fill::MATERIAL;
29,676✔
112
      for (auto& mat_id : c->material_) {
60,681✔
113
        if (mat_id != MATERIAL_VOID) {
31,005✔
114
          auto search = model::material_map.find(mat_id);
21,740!
115
          if (search == model::material_map.end()) {
21,740!
116
            fatal_error(
×
117
              fmt::format("Could not find material {} specified on cell {}",
×
118
                mat_id, c->id_));
×
119
          }
120
          // Change from ID to index
121
          mat_id = search->second;
21,740✔
122
        }
123
      }
124
    }
125
  }
126

127
  // Change cell.universe values from IDs to indices.
128
  for (auto& c : model::cells) {
46,366✔
129
    auto search = model::universe_map.find(c->universe_);
37,099!
130
    if (search != model::universe_map.end()) {
37,099!
131
      c->universe_ = search->second;
37,099✔
132
    } else {
133
      fatal_error(fmt::format("Could not find universe {} specified on cell {}",
×
134
        c->universe_, c->id_));
×
135
    }
136
  }
137

138
  // Change all lattice universe values from IDs to indices.
139
  for (auto& l : model::lattices) {
11,377✔
140
    l->adjust_indices();
2,110✔
141
  }
142
}
9,267✔
143

144
//==============================================================================
145
//! Partition some universes with many z-planes for faster find_cell searches.
146

147
void partition_universes()
9,267✔
148
{
149
  // Iterate over universes with more than 10 cells.  (Fewer than 10 is likely
150
  // not worth partitioning.)
151
  for (const auto& univ : model::universes) {
30,571✔
152
    if (univ->cells_.size() > 10) {
21,304✔
153
      // Collect the set of surfaces in this universe.
154
      std::unordered_set<int32_t> surf_inds;
168✔
155
      for (auto i_cell : univ->cells_) {
2,841✔
156
        for (auto token : model::cells[i_cell]->surfaces()) {
10,122✔
157
          surf_inds.insert(std::abs(token) - 1);
7,449✔
158
        }
2,673✔
159
      }
160

161
      // Partition the universe if there are more than 5 z-planes.  (Fewer than
162
      // 5 is likely not worth it.)
163
      int n_zplanes = 0;
168✔
164
      for (auto i_surf : surf_inds) {
2,172✔
165
        if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
2,094!
166
          ++n_zplanes;
600✔
167
          if (n_zplanes > 5) {
600✔
168
            univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
90✔
169
            break;
90✔
170
          }
171
        }
172
      }
173
    }
168✔
174
  }
175
}
9,267✔
176

177
//==============================================================================
178

179
void assign_temperatures()
9,267✔
180
{
181
  for (auto& c : model::cells) {
46,366✔
182
    // Ignore non-material cells and cells with defined temperature.
183
    if (c->material_.size() == 0)
37,099✔
184
      continue;
7,423✔
185
    if (c->sqrtkT_.size() > 0)
29,676✔
186
      continue;
554✔
187

188
    c->sqrtkT_.reserve(c->material_.size());
29,122✔
189
    for (auto i_mat : c->material_) {
59,564✔
190
      if (i_mat == MATERIAL_VOID) {
30,442✔
191
        // Set void region to 0K.
192
        c->sqrtkT_.push_back(0);
9,265✔
193
      } else {
194
        const auto& mat {model::materials[i_mat]};
21,177✔
195
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
21,177✔
196
      }
197
    }
198
  }
199
}
9,267✔
200

201
//==============================================================================
202

203
void finalize_cell_densities()
9,267✔
204
{
205
  for (auto& c : model::cells) {
46,366✔
206
    // Convert to density multipliers.
207
    if (!c->density_mult_.empty()) {
37,099✔
208
      for (int32_t instance = 0; instance < c->density_mult_.size();
1,380✔
209
           ++instance) {
210
        c->density_mult_[instance] /=
2,550!
211
          model::materials[c->material(instance)]->density_gpcc();
3,825!
212
      }
213
    } else {
214
      c->density_mult_ = {1.0};
36,994✔
215
    }
216
  }
217
}
9,267✔
218

219
//==============================================================================
220

221
void get_temperatures(
9,126✔
222
  vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
223
{
224
  for (const auto& cell : model::cells) {
45,857✔
225
    // Skip non-material cells.
226
    if (cell->fill_ != C_NONE)
36,731✔
227
      continue;
7,412✔
228

229
    for (int j = 0; j < cell->material_.size(); ++j) {
59,967✔
230
      // Skip void materials
231
      int i_material = cell->material_[j];
30,648✔
232
      if (i_material == MATERIAL_VOID)
30,648✔
233
        continue;
9,199✔
234

235
      // Get temperature(s) of cell (rounding to nearest integer)
236
      vector<double> cell_temps;
21,449✔
237
      if (cell->sqrtkT_.size() == 1) {
21,449✔
238
        double sqrtkT = cell->sqrtkT_[0];
19,884✔
239
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
19,884✔
240
      } else if (cell->sqrtkT_.size() == cell->material_.size()) {
1,565✔
241
        double sqrtkT = cell->sqrtkT_[j];
1,505✔
242
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,505✔
243
      } else {
244
        for (double sqrtkT : cell->sqrtkT_)
1,200✔
245
          cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,140✔
246
      }
247

248
      const auto& mat {model::materials[i_material]};
21,449✔
249
      for (const auto& i_nuc : mat->nuclide_) {
95,281✔
250
        for (double temperature : cell_temps) {
148,744✔
251
          // Add temperature if it hasn't already been added
252
          if (!contains(nuc_temps[i_nuc], temperature))
74,912✔
253
            nuc_temps[i_nuc].push_back(temperature);
35,829✔
254
        }
255
      }
256

257
      for (const auto& table : mat->thermal_tables_) {
25,039✔
258
        // Get index in data::thermal_scatt array
259
        int i_sab = table.index_table;
3,590✔
260

261
        for (double temperature : cell_temps) {
7,180✔
262
          // Add temperature if it hasn't already been added
263
          if (!contains(thermal_temps[i_sab], temperature))
3,590✔
264
            thermal_temps[i_sab].push_back(temperature);
1,589✔
265
        }
266
      }
267
    }
21,449✔
268
  }
269
}
9,126✔
270

271
//==============================================================================
272

273
void detect_boundary_surfaces()
9,267✔
274
{
275
  for (int i = 0; i < model::surfaces.size(); i++) {
58,181✔
276
    // if the surface has a non-transmission boundary condition,
277
    // add it to the list of surfaces to track during delta tracking
278
    const auto& s = model::surfaces[i];
48,914✔
279
    if (s->bc_) {
48,914✔
280
      // A bug in MOAB causes ray_fire to fail on surface primatives in
281
      // DAGSurface. This prevents us from tracking the distance to the closest
282
      // boundary for DAGMC geometry, and so we need to avoid applying BCs with
283
      // DAGMC. This doesn't happen in surface tracking as ray_fire is called on
284
      // a volume primative through DAGCell.
285
      if (settings::delta_tracking && s->geom_type() == GeometryType::DAG) {
29,503!
NEW
286
        fatal_error(
×
287
          "At present, the application of boundary conditions to "
288
          "DAGMC surfaces is not supported when running with delta "
289
          "tracking. If you wish to use DAGMC models with delta "
290
          "tracking, please remove all boundary conditions from the "
291
          "DAGMC universe (including the graveyard) and instead apply "
292
          "them with CSG cells filled with the DAGMC universe.");
293
      }
294
      model::boundary_surfaces.push_back(i);
29,503✔
295
    }
296
  }
297
}
9,267✔
298

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

301
void finalize_geometry()
9,267✔
302
{
303
  // Perform some final operations to set up the geometry
304
  adjust_indices();
9,267✔
305
  count_universe_instances();
9,267✔
306
  partition_universes();
9,267✔
307

308
  // Assign temperatures to cells that don't have temperatures already assigned
309
  assign_temperatures();
9,267✔
310

311
  // Find all boundary surfaces. Used in delta tracking to trace through the
312
  // geometry.
313
  detect_boundary_surfaces();
9,267✔
314

315
  // Determine number of nested coordinate levels in the geometry
316
  model::n_coord_levels = maximum_levels(model::root_universe);
9,267✔
317
}
9,267✔
318

319
//==============================================================================
320

321
int32_t find_root_universe()
9,267✔
322
{
323
  // Find all the universes listed as a cell fill.
324
  std::unordered_set<int32_t> fill_univ_ids;
9,267✔
325
  for (const auto& c : model::cells) {
46,366✔
326
    fill_univ_ids.insert(c->fill_);
37,099✔
327
  }
328

329
  // Find all the universes contained in a lattice.
330
  for (const auto& lat : model::lattices) {
11,377✔
331
    for (auto it = lat->begin(); it != lat->end(); ++it) {
972,673✔
332
      fill_univ_ids.insert(*it);
970,563✔
333
    }
334
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
2,110✔
335
      fill_univ_ids.insert(lat->outer_);
441✔
336
    }
337
  }
338

339
  // Figure out which universe is not in the set.  This is the root universe.
340
  bool root_found {false};
341
  int32_t root_univ;
342
  for (int32_t i = 0; i < model::universes.size(); i++) {
30,571✔
343
    auto search = fill_univ_ids.find(model::universes[i]->id_);
21,304✔
344
    if (search == fill_univ_ids.end()) {
21,304✔
345
      if (root_found) {
9,267!
346
        fatal_error("Two or more universes are not used as fill universes, so "
×
347
                    "it is not possible to distinguish which one is the root "
348
                    "universe.");
349
      } else {
350
        root_found = true;
351
        root_univ = i;
352
      }
353
    }
354
  }
355
  if (!root_found)
9,267!
356
    fatal_error("Could not find a root universe.  Make sure "
×
357
                "there are no circular dependencies in the geometry.");
358

359
  return root_univ;
9,267✔
360
}
9,267✔
361

362
//==============================================================================
363

364
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
9,240✔
365
{
366
  write_message("Preparing distributed cell instances...", 5);
9,240✔
367

368
  std::unordered_set<int32_t> distribcells;
9,240✔
369

370
  // start with any cells manually specified via the C++ API
371
  if (user_distribcells) {
9,240✔
372
    distribcells.insert(user_distribcells->begin(), user_distribcells->end());
15✔
373
  }
374

375
  // Find all cells listed in a DistribcellFilter or CellInstanceFilter
376
  for (auto& filt : model::tally_filters) {
20,305✔
377
    auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
11,065!
378
    auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
11,065!
379
    if (distrib_filt) {
11,065✔
380
      distribcells.insert(distrib_filt->cell());
179✔
381
    }
382
    if (cell_inst_filt) {
11,065✔
383
      const auto& filter_cells = cell_inst_filt->cells();
32✔
384
      distribcells.insert(filter_cells.begin(), filter_cells.end());
32✔
385
    }
386
  }
387

388
  // By default, add material cells to the list of distributed cells
389
  if (settings::material_cell_offsets) {
9,240!
390
    for (int64_t i = 0; i < model::cells.size(); ++i) {
46,342✔
391
      if (model::cells[i]->type_ == Fill::MATERIAL)
37,102✔
392
        distribcells.insert(i);
29,649✔
393
    }
394
  }
395

396
  // Make sure that the number of materials/temperatures matches the number of
397
  // cell instances.
398
  for (int i = 0; i < model::cells.size(); i++) {
46,342✔
399
    Cell& c {*model::cells[i]};
37,102✔
400

401
    if (c.material_.size() > 1) {
37,102✔
402
      if (c.material_.size() != c.n_instances()) {
203!
403
        fatal_error(fmt::format(
×
404
          "Cell {} was specified with {} materials but has {} distributed "
405
          "instances. The number of materials must equal one or the number "
406
          "of instances.",
407
          c.id_, c.material_.size(), c.n_instances()));
×
408
      }
409
    }
410

411
    if (c.sqrtkT_.size() > 1) {
37,102✔
412
      if (c.sqrtkT_.size() != c.n_instances()) {
260!
413
        fatal_error(fmt::format(
×
414
          "Cell {} was specified with {} temperatures but has {} distributed "
415
          "instances. The number of temperatures must equal one or the number "
416
          "of instances.",
417
          c.id_, c.sqrtkT_.size(), c.n_instances()));
×
418
      }
419
    }
420

421
    if (c.density_mult_.size() > 1) {
37,102✔
422
      if (c.density_mult_.size() != c.n_instances()) {
90!
423
        fatal_error(fmt::format("Cell {} was specified with {} density "
×
424
                                "multipliers but has {} distributed "
425
                                "instances. The number of density multipliers "
426
                                "must equal one or the number "
427
                                "of instances.",
428
          c.id_, c.density_mult_.size(), c.n_instances()));
×
429
      }
430
    }
431
  }
432

433
  // Search through universes for material cells and assign each one a
434
  // distribcell array index according to the containing universe.
435
  vector<int32_t> target_univ_ids;
9,240✔
436
  for (const auto& u : model::universes) {
30,547✔
437
    for (auto idx : u->cells_) {
58,409✔
438
      if (distribcells.find(idx) != distribcells.end()) {
37,102✔
439
        if (!contains(target_univ_ids, u->id_)) {
29,724✔
440
          target_univ_ids.push_back(u->id_);
18,374✔
441
        }
442
        model::cells[idx]->distribcell_index_ =
29,724✔
443
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
29,724✔
444
          target_univ_ids.begin();
29,724✔
445
      }
446
    }
447
  }
448

449
  // Allocate the cell and lattice offset tables.
450
  int n_maps = target_univ_ids.size();
9,240✔
451
  for (auto& c : model::cells) {
46,342✔
452
    if (c->type_ != Fill::MATERIAL) {
37,102✔
453
      c->offset_.resize(n_maps, C_NONE);
7,453✔
454
    }
455
  }
456
  for (auto& lat : model::lattices) {
11,365✔
457
    lat->allocate_offset_table(n_maps);
2,125✔
458
  }
459

460
// Fill the cell and lattice offset tables.
461
#pragma omp parallel for
5,278✔
462
  for (int map = 0; map < target_univ_ids.size(); map++) {
9,217✔
463
    auto target_univ_id = target_univ_ids[map];
5,255✔
464
    std::unordered_map<int32_t, int32_t> univ_count_memo;
5,255✔
465
    for (const auto& univ : model::universes) {
22,775✔
466
      int32_t offset = 0;
17,520✔
467
      for (int32_t cell_indx : univ->cells_) {
76,171✔
468
        Cell& c = *model::cells[cell_indx];
58,651✔
469

470
        if (c.type_ == Fill::UNIVERSE) {
58,651✔
471
          c.offset_[map] = offset;
31,283✔
472
          int32_t search_univ = c.fill_;
31,283✔
473
          offset += count_universe_instances(
31,283✔
474
            search_univ, target_univ_id, univ_count_memo);
475

476
        } else if (c.type_ == Fill::LATTICE) {
27,368✔
477
          c.offset_[map] = offset;
2,826✔
478
          Lattice& lat = *model::lattices[c.fill_];
2,826✔
479
          offset += lat.fill_offset_table(target_univ_id, map, univ_count_memo);
2,826✔
480
        }
481
      }
482
    }
483
  }
5,255✔
484
}
18,480✔
485

486
//==============================================================================
487

488
void count_universe_instances()
9,267✔
489
{
490
  for (auto& univ : model::universes) {
30,571✔
491
    std::unordered_map<int32_t, int32_t> univ_count_memo;
21,304✔
492
    univ->n_instances_ = count_universe_instances(
21,304✔
493
      model::root_universe, univ->id_, univ_count_memo);
21,304✔
494
  }
21,304✔
495
}
9,267✔
496

497
//==============================================================================
498

499
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
22,687,348✔
500
  std::unordered_map<int32_t, int32_t>& univ_count_memo)
501
{
502
  // If this is the target, it can't contain itself.
503
  if (model::universes[search_univ]->id_ == target_univ_id) {
22,687,348✔
504
    return 1;
505
  }
506

507
  // If we have already counted the number of instances, reuse that value.
508
  auto search = univ_count_memo.find(search_univ);
19,948,945✔
509
  if (search != univ_count_memo.end()) {
19,948,945✔
510
    return search->second;
7,883,751✔
511
  }
512

513
  int count {0};
12,065,194✔
514
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
24,304,089✔
515
    Cell& c = *model::cells[cell_indx];
12,238,895✔
516

517
    if (c.type_ == Fill::UNIVERSE) {
12,238,895✔
518
      int32_t next_univ = c.fill_;
159,534✔
519
      count +=
159,534✔
520
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
159,534✔
521

522
    } else if (c.type_ == Fill::LATTICE) {
12,079,361✔
523
      Lattice& lat = *model::lattices[c.fill_];
21,471✔
524
      for (auto it = lat.begin(); it != lat.end(); ++it) {
13,025,722✔
525
        int32_t next_univ = *it;
13,004,251✔
526
        count +=
13,004,251✔
527
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
13,004,251✔
528
      }
529
    }
530
  }
531

532
  // Remember the number of instances in this universe.
533
  univ_count_memo[search_univ] = count;
12,065,194✔
534

535
  return count;
12,065,194✔
536
}
537

538
//==============================================================================
539

540
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
2,565,574✔
541
  int32_t target_offset, const Universe& search_univ, int32_t offset)
542
{
543
  std::stringstream path;
2,565,574✔
544

545
  path << "u" << search_univ.id_ << "->";
2,565,574✔
546

547
  // Check to see if this universe directly contains the target cell.  If so,
548
  // write to the path and return.
549
  for (int32_t cell_indx : search_univ.cells_) {
11,902,935✔
550
    if ((cell_indx == target_cell) && (offset == target_offset)) {
10,264,408✔
551
      Cell& c = *model::cells[cell_indx];
927,047✔
552
      path << "c" << c.id_;
927,047✔
553
      return path.str();
927,047✔
554
    }
555
  }
556

557
  // The target must be further down the geometry tree and contained in a fill
558
  // cell or lattice cell in this universe.  Find which cell contains the
559
  // target.
560
  vector<std::int32_t>::const_reverse_iterator cell_it {
561
    search_univ.cells_.crbegin()};
562
  for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
9,337,196!
563
    Cell& c = *model::cells[*cell_it];
9,337,196✔
564

565
    // Material cells don't contain other cells so ignore them.
566
    if (c.type_ != Fill::MATERIAL) {
9,337,196✔
567
      int32_t temp_offset = offset + c.offset_[map];
2,338,556!
568
      if (c.type_ == Fill::LATTICE) {
2,338,556!
569
        Lattice& lat = *model::lattices[c.fill_];
2,338,556✔
570
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
2,338,556✔
571
        temp_offset += lat.offsets_[indx];
2,338,556✔
572
      }
573

574
      // The desired cell is the first cell that gives an offset smaller or
575
      // equal to the target offset.
576
      if (temp_offset <= target_offset)
2,338,556✔
577
        break;
578
    }
579
  }
580

581
  // if we get through the loop without finding an appropriate entry, throw
582
  // an error
583
  if (cell_it == search_univ.cells_.crend()) {
1,638,527!
584
    fatal_error(
×
585
      fmt::format("Failed to generate a text label for distribcell with ID {}."
×
586
                  "The current label is: '{}'",
587
        model::cells[target_cell]->id_, path.str()));
×
588
  }
589

590
  // Add the cell to the path string.
591
  Cell& c = *model::cells[*cell_it];
1,638,527✔
592
  path << "c" << c.id_ << "->";
1,638,527✔
593

594
  if (c.type_ == Fill::UNIVERSE) {
1,638,527!
595
    // Recurse into the fill cell.
596
    offset += c.offset_[map];
×
597
    path << distribcell_path_inner(
×
598
      target_cell, map, target_offset, *model::universes[c.fill_], offset);
×
599
    return path.str();
×
600
  } else {
601
    // Recurse into the lattice cell.
602
    Lattice& lat = *model::lattices[c.fill_];
1,638,527✔
603
    path << "l" << lat.id_;
1,638,527✔
604
    for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) {
289,048,760!
605
      int32_t indx = lat.universes_.size() * map + it.indx_;
289,048,760✔
606
      int32_t temp_offset = offset + lat.offsets_[indx] + c.offset_[map];
289,048,760✔
607
      if (temp_offset <= target_offset) {
289,048,760✔
608
        offset = temp_offset;
1,638,527✔
609
        path << "(" << lat.index_to_string(it.indx_) << ")->";
3,277,054✔
610
        path << distribcell_path_inner(
1,638,527✔
611
          target_cell, map, target_offset, *model::universes[*it], offset);
3,277,054✔
612
        return path.str();
1,638,527✔
613
      }
614
    }
615
    throw std::runtime_error {"Error determining distribcell path."};
×
616
  }
617
}
2,565,574✔
618

619
std::string distribcell_path(
927,047✔
620
  int32_t target_cell, int32_t map, int32_t target_offset)
621
{
622
  auto& root_univ = *model::universes[model::root_universe];
927,047✔
623
  return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0);
927,047✔
624
}
625

626
//==============================================================================
627

628
int maximum_levels(int32_t univ)
985,275✔
629
{
630

631
  const auto level_count = model::universe_level_counts.find(univ);
985,275✔
632
  if (level_count != model::universe_level_counts.end()) {
985,275✔
633
    return level_count->second;
964,126✔
634
  }
635

636
  int levels_below {0};
21,149✔
637

638
  for (int32_t cell_indx : model::universes[univ]->cells_) {
58,085✔
639
    Cell& c = *model::cells[cell_indx];
36,936✔
640
    if (c.type_ == Fill::UNIVERSE) {
36,936✔
641
      int32_t next_univ = c.fill_;
5,280✔
642
      levels_below = std::max(levels_below, maximum_levels(next_univ));
7,300✔
643
    } else if (c.type_ == Fill::LATTICE) {
31,656✔
644
      Lattice& lat = *model::lattices[c.fill_];
2,143✔
645
      for (auto it = lat.begin(); it != lat.end(); ++it) {
972,871✔
646
        int32_t next_univ = *it;
970,728✔
647
        levels_below = std::max(levels_below, maximum_levels(next_univ));
972,778✔
648
      }
649
    }
650
  }
651

652
  ++levels_below;
21,149✔
653
  model::universe_level_counts[univ] = levels_below;
21,149✔
654
  return levels_below;
21,149✔
655
}
656

657
bool is_root_universe(int32_t univ_id)
18,923✔
658
{
659
  return model::universe_map[univ_id] == model::root_universe;
18,923✔
660
}
661

662
//==============================================================================
663

664
void free_memory_geometry()
9,396✔
665
{
666
  model::cells.clear();
9,396✔
667
  model::cell_map.clear();
9,396✔
668

669
  model::universes.clear();
9,396✔
670
  model::universe_map.clear();
9,396✔
671

672
  model::lattices.clear();
9,396✔
673
  model::lattice_map.clear();
9,396✔
674

675
  model::overlap_check_count.clear();
9,396✔
676
}
9,396✔
677

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