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

openmc-dev / openmc / 20752759853

06 Jan 2026 03:18PM UTC coverage: 81.984% (-0.2%) from 82.154%
20752759853

Pull #3702

github

web-flow
Merge 93792a96d into c7d7fa461
Pull Request #3702: Random Ray Kinetic Simulation Mode

17538 of 24346 branches covered (72.04%)

Branch coverage included in aggregate %.

903 of 1115 new or added lines in 20 files covered. (80.99%)

224 existing lines in 14 files now uncovered.

55923 of 65258 relevant lines covered (85.7%)

50584462.14 hits per line

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

87.87
/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,371✔
32
{
33
  // Display output message
34
  write_message("Reading geometry XML file...", 5);
1,371✔
35

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

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

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

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

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

62
  read_surfaces(root, periodic_pairs, albedo_map, periodic_sense_map);
7,902✔
63
  read_cells(root);
7,902✔
64
  prepare_boundary_conditions(periodic_pairs, albedo_map, periodic_sense_map);
7,900✔
65
  read_lattices(root);
7,900✔
66

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

77
  if (settings::run_mode != RunMode::PLOTTING &&
7,900✔
78
      settings::run_mode != RunMode::VOLUME && !boundary_exists) {
7,781!
UNCOV
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();
7,900✔
84

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

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

91
void adjust_indices()
7,902✔
92
{
93
  // Adjust material/fill idices.
94
  for (auto& c : model::cells) {
42,571✔
95
    if (c->fill_ != C_NONE) {
34,669✔
96
      int32_t id = c->fill_;
7,479✔
97
      auto search_univ = model::universe_map.find(id);
7,479✔
98
      auto search_lat = model::lattice_map.find(id);
7,479✔
99
      if (search_univ != model::universe_map.end()) {
7,479✔
100
        c->type_ = Fill::UNIVERSE;
5,533✔
101
        c->fill_ = search_univ->second;
5,533✔
102
      } else if (search_lat != model::lattice_map.end()) {
1,946!
103
        c->type_ = Fill::LATTICE;
1,946✔
104
        c->fill_ = search_lat->second;
1,946✔
105
      } else {
UNCOV
106
        fatal_error(fmt::format("Specified fill {} on cell {} is neither a "
×
107
                                "universe nor a lattice.",
UNCOV
108
          id, c->id_));
×
109
      }
110
    } else {
111
      c->type_ = Fill::MATERIAL;
27,190✔
112
      for (auto& mat_id : c->material_) {
55,654✔
113
        if (mat_id != MATERIAL_VOID) {
28,464✔
114
          auto search = model::material_map.find(mat_id);
19,887✔
115
          if (search == model::material_map.end()) {
19,887!
UNCOV
116
            fatal_error(
×
UNCOV
117
              fmt::format("Could not find material {} specified on cell {}",
×
UNCOV
118
                mat_id, c->id_));
×
119
          }
120
          // Change from ID to index
121
          mat_id = search->second;
19,887✔
122
        }
123
      }
124
    }
125
  }
126

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

138
  // Change all lattice universe values from IDs to indices.
139
  for (auto& l : model::lattices) {
9,815✔
140
    l->adjust_indices();
1,913✔
141
  }
142
}
7,902✔
143

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

147
void partition_universes()
7,902✔
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) {
27,543✔
152
    if (univ->cells_.size() > 10) {
19,641✔
153
      // Collect the set of surfaces in this universe.
154
      std::unordered_set<int32_t> surf_inds;
177✔
155
      for (auto i_cell : univ->cells_) {
3,004✔
156
        for (auto token : model::cells[i_cell]->surfaces()) {
10,700✔
157
          surf_inds.insert(std::abs(token) - 1);
7,873✔
158
        }
2,827✔
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;
177✔
164
      for (auto i_surf : surf_inds) {
2,297✔
165
        if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
2,216!
166
          ++n_zplanes;
640✔
167
          if (n_zplanes > 5) {
640✔
168
            univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
96✔
169
            break;
96✔
170
          }
171
        }
172
      }
173
    }
177✔
174
  }
175
}
7,902✔
176

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

179
void assign_temperatures()
7,902✔
180
{
181
  for (auto& c : model::cells) {
42,571✔
182
    // Ignore non-material cells and cells with defined temperature.
183
    if (c->material_.size() == 0)
34,669✔
184
      continue;
7,479✔
185
    if (c->sqrtkT_.size() > 0)
27,190✔
186
      continue;
442✔
187

188
    c->sqrtkT_.reserve(c->material_.size());
26,748✔
189
    for (auto i_mat : c->material_) {
54,761✔
190
      if (i_mat == MATERIAL_VOID) {
28,013✔
191
        // Set void region to 0K.
192
        c->sqrtkT_.push_back(0);
8,577✔
193
      } else {
194
        const auto& mat {model::materials[i_mat]};
19,436✔
195
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
19,436✔
196
      }
197
    }
198
  }
199
}
7,902✔
200

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

203
void finalize_cell_densities()
7,902✔
204
{
205
  for (auto& c : model::cells) {
42,571✔
206
    // Convert to density multipliers.
207
    if (!c->density_mult_.empty()) {
34,669✔
208
      for (int32_t instance = 0; instance < c->density_mult_.size();
80✔
209
           ++instance) {
210
        c->density_mult_[instance] /=
64✔
211
          model::materials[c->material(instance)]->density_gpcc();
64✔
212
      }
213
    } else {
214
      c->density_mult_ = {1.0};
34,653✔
215
    }
216
  }
217
}
7,902✔
218

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

221
void get_temperatures(
7,783✔
222
  vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
223
{
224
  for (const auto& cell : model::cells) {
42,128✔
225
    // Skip non-material cells.
226
    if (cell->fill_ != C_NONE)
34,345✔
227
      continue;
7,468✔
228

229
    for (int j = 0; j < cell->material_.size(); ++j) {
55,028✔
230
      // Skip void materials
231
      int i_material = cell->material_[j];
28,151✔
232
      if (i_material == MATERIAL_VOID)
28,151✔
233
        continue;
8,555✔
234

235
      // Get temperature(s) of cell (rounding to nearest integer)
236
      vector<double> cell_temps;
19,596✔
237
      if (cell->sqrtkT_.size() == 1) {
19,596✔
238
        double sqrtkT = cell->sqrtkT_[0];
18,136✔
239
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
18,136✔
240
      } else if (cell->sqrtkT_.size() == cell->material_.size()) {
1,460✔
241
        double sqrtkT = cell->sqrtkT_[j];
1,444✔
242
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,444✔
243
      } else {
244
        for (double sqrtkT : cell->sqrtkT_)
80✔
245
          cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
64✔
246
      }
247

248
      const auto& mat {model::materials[i_material]};
19,596✔
249
      for (const auto& i_nuc : mat->nuclide_) {
87,662✔
250
        for (double temperature : cell_temps) {
136,180✔
251
          // Add temperature if it hasn't already been added
252
          if (!contains(nuc_temps[i_nuc], temperature))
68,114✔
253
            nuc_temps[i_nuc].push_back(temperature);
30,374✔
254
        }
255
      }
256

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

261
        for (double temperature : cell_temps) {
6,702✔
262
          // Add temperature if it hasn't already been added
263
          if (!contains(thermal_temps[i_sab], temperature))
3,351✔
264
            thermal_temps[i_sab].push_back(temperature);
1,269✔
265
        }
266
      }
267
    }
19,596✔
268
  }
269
}
7,783✔
270

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

273
void finalize_geometry()
7,902✔
274
{
275
  // Perform some final operations to set up the geometry
276
  adjust_indices();
7,902✔
277
  count_universe_instances();
7,902✔
278
  partition_universes();
7,902✔
279

280
  // Assign temperatures to cells that don't have temperatures already assigned
281
  assign_temperatures();
7,902✔
282

283
  // Determine number of nested coordinate levels in the geometry
284
  model::n_coord_levels = maximum_levels(model::root_universe);
7,902✔
285
}
7,902✔
286

287
//==============================================================================
288

289
int32_t find_root_universe()
7,902✔
290
{
291
  // Find all the universes listed as a cell fill.
292
  std::unordered_set<int32_t> fill_univ_ids;
7,902✔
293
  for (const auto& c : model::cells) {
42,571✔
294
    fill_univ_ids.insert(c->fill_);
34,669✔
295
  }
296

297
  // Find all the universes contained in a lattice.
298
  for (const auto& lat : model::lattices) {
9,815✔
299
    for (auto it = lat->begin(); it != lat->end(); ++it) {
930,145✔
300
      fill_univ_ids.insert(*it);
928,232✔
301
    }
302
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
1,913✔
303
      fill_univ_ids.insert(lat->outer_);
414✔
304
    }
305
  }
306

307
  // Figure out which universe is not in the set.  This is the root universe.
308
  bool root_found {false};
7,902✔
309
  int32_t root_univ;
310
  for (int32_t i = 0; i < model::universes.size(); i++) {
27,543✔
311
    auto search = fill_univ_ids.find(model::universes[i]->id_);
19,641✔
312
    if (search == fill_univ_ids.end()) {
19,641✔
313
      if (root_found) {
7,902!
UNCOV
314
        fatal_error("Two or more universes are not used as fill universes, so "
×
315
                    "it is not possible to distinguish which one is the root "
316
                    "universe.");
317
      } else {
318
        root_found = true;
7,902✔
319
        root_univ = i;
7,902✔
320
      }
321
    }
322
  }
323
  if (!root_found)
7,902!
UNCOV
324
    fatal_error("Could not find a root universe.  Make sure "
×
325
                "there are no circular dependencies in the geometry.");
326

327
  return root_univ;
7,902✔
328
}
7,902✔
329

330
//==============================================================================
331

332
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
7,898✔
333
{
334
  write_message("Preparing distributed cell instances...", 5);
7,898✔
335

336
  std::unordered_set<int32_t> distribcells;
7,898✔
337

338
  // start with any cells manually specified via the C++ API
339
  if (user_distribcells) {
7,898✔
340
    distribcells.insert(user_distribcells->begin(), user_distribcells->end());
16✔
341
  }
342

343
  // Find all cells listed in a DistribcellFilter or CellInstanceFilter
344
  for (auto& filt : model::tally_filters) {
16,938✔
345
    auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
9,040!
346
    auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
9,040!
347
    if (distrib_filt) {
9,040✔
348
      distribcells.insert(distrib_filt->cell());
188✔
349
    }
350
    if (cell_inst_filt) {
9,040✔
351
      const auto& filter_cells = cell_inst_filt->cells();
34✔
352
      distribcells.insert(filter_cells.begin(), filter_cells.end());
34✔
353
    }
354
  }
355

356
  // By default, add material cells to the list of distributed cells
357
  if (settings::material_cell_offsets) {
7,898!
358
    for (int64_t i = 0; i < model::cells.size(); ++i) {
42,619✔
359
      if (model::cells[i]->type_ == Fill::MATERIAL)
34,721✔
360
        distribcells.insert(i);
27,210✔
361
    }
362
  }
363

364
  // Make sure that the number of materials/temperatures matches the number of
365
  // cell instances.
366
  for (int i = 0; i < model::cells.size(); i++) {
42,619✔
367
    Cell& c {*model::cells[i]};
34,721✔
368

369
    if (c.material_.size() > 1) {
34,721✔
370
      if (c.material_.size() != c.n_instances()) {
198!
UNCOV
371
        fatal_error(fmt::format(
×
372
          "Cell {} was specified with {} materials but has {} distributed "
373
          "instances. The number of materials must equal one or the number "
374
          "of instances.",
UNCOV
375
          c.id_, c.material_.size(), c.n_instances()));
×
376
      }
377
    }
378

379
    if (c.sqrtkT_.size() > 1) {
34,721✔
380
      if (c.sqrtkT_.size() != c.n_instances()) {
211!
UNCOV
381
        fatal_error(fmt::format(
×
382
          "Cell {} was specified with {} temperatures but has {} distributed "
383
          "instances. The number of temperatures must equal one or the number "
384
          "of instances.",
UNCOV
385
          c.id_, c.sqrtkT_.size(), c.n_instances()));
×
386
      }
387
    }
388

389
    if (c.density_mult_.size() > 1) {
34,721✔
390
      if (c.density_mult_.size() != c.n_instances()) {
16!
391
        fatal_error(fmt::format("Cell {} was specified with {} density "
×
392
                                "multipliers but has {} distributed "
393
                                "instances. The number of density multipliers "
394
                                "must equal one or the number "
395
                                "of instances.",
UNCOV
396
          c.id_, c.density_mult_.size(), c.n_instances()));
×
397
      }
398
    }
399
  }
400

401
  // Search through universes for material cells and assign each one a
402
  // distribcell array index according to the containing universe.
403
  vector<int32_t> target_univ_ids;
7,898✔
404
  for (const auto& u : model::universes) {
27,567✔
405
    for (auto idx : u->cells_) {
54,390✔
406
      if (distribcells.find(idx) != distribcells.end()) {
34,721✔
407
        if (!contains(target_univ_ids, u->id_)) {
27,290✔
408
          target_univ_ids.push_back(u->id_);
16,896✔
409
        }
410
        model::cells[idx]->distribcell_index_ =
27,290✔
411
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
27,290✔
412
          target_univ_ids.begin();
54,580✔
413
      }
414
    }
415
  }
416

417
  // Allocate the cell and lattice offset tables.
418
  int n_maps = target_univ_ids.size();
7,898✔
419
  for (auto& c : model::cells) {
42,619✔
420
    if (c->type_ != Fill::MATERIAL) {
34,721✔
421
      c->offset_.resize(n_maps, C_NONE);
7,511✔
422
    }
423
  }
424
  for (auto& lat : model::lattices) {
9,827✔
425
    lat->allocate_offset_table(n_maps);
1,929✔
426
  }
427

428
// Fill the cell and lattice offset tables.
429
#pragma omp parallel for
4,394✔
430
  for (int map = 0; map < target_univ_ids.size(); map++) {
8,334✔
431
    auto target_univ_id = target_univ_ids[map];
4,830✔
432
    std::unordered_map<int32_t, int32_t> univ_count_memo;
4,830✔
433
    for (const auto& univ : model::universes) {
23,086✔
434
      int32_t offset = 0;
18,256✔
435
      for (int32_t cell_indx : univ->cells_) {
82,952✔
436
        Cell& c = *model::cells[cell_indx];
64,696✔
437

438
        if (c.type_ == Fill::UNIVERSE) {
64,696✔
439
          c.offset_[map] = offset;
36,223✔
440
          int32_t search_univ = c.fill_;
36,223✔
441
          offset += count_universe_instances(
36,223✔
442
            search_univ, target_univ_id, univ_count_memo);
443

444
        } else if (c.type_ == Fill::LATTICE) {
28,473✔
445
          c.offset_[map] = offset;
2,993✔
446
          Lattice& lat = *model::lattices[c.fill_];
2,993✔
447
          offset += lat.fill_offset_table(target_univ_id, map, univ_count_memo);
2,993✔
448
        }
449
      }
450
    }
451
  }
4,830✔
452
}
7,898✔
453

454
//==============================================================================
455

456
void count_universe_instances()
7,902✔
457
{
458
  for (auto& univ : model::universes) {
27,543✔
459
    std::unordered_map<int32_t, int32_t> univ_count_memo;
19,641✔
460
    univ->n_instances_ = count_universe_instances(
19,641✔
461
      model::root_universe, univ->id_, univ_count_memo);
19,641✔
462
  }
19,641✔
463
}
7,902✔
464

465
//==============================================================================
466

467
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
22,404,572✔
468
  std::unordered_map<int32_t, int32_t>& univ_count_memo)
469
{
470
  // If this is the target, it can't contain itself.
471
  if (model::universes[search_univ]->id_ == target_univ_id) {
22,404,572✔
472
    return 1;
2,617,758✔
473
  }
474

475
  // If we have already counted the number of instances, reuse that value.
476
  auto search = univ_count_memo.find(search_univ);
19,786,814✔
477
  if (search != univ_count_memo.end()) {
19,786,814✔
478
    return search->second;
7,719,680✔
479
  }
480

481
  int count {0};
12,067,134✔
482
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
24,317,960✔
483
    Cell& c = *model::cells[cell_indx];
12,250,826✔
484

485
    if (c.type_ == Fill::UNIVERSE) {
12,250,826✔
486
      int32_t next_univ = c.fill_;
169,509✔
487
      count +=
169,509✔
488
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
169,509✔
489

490
    } else if (c.type_ == Fill::LATTICE) {
12,081,317✔
491
      Lattice& lat = *model::lattices[c.fill_];
21,639✔
492
      for (auto it = lat.begin(); it != lat.end(); ++it) {
12,866,635✔
493
        int32_t next_univ = *it;
12,844,996✔
494
        count +=
12,844,996✔
495
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
12,844,996✔
496
      }
497
    }
498
  }
499

500
  // Remember the number of instances in this universe.
501
  univ_count_memo[search_univ] = count;
12,067,134✔
502

503
  return count;
12,067,134✔
504
}
505

506
//==============================================================================
507

508
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
2,565,574✔
509
  int32_t target_offset, const Universe& search_univ, int32_t offset)
510
{
511
  std::stringstream path;
2,565,574✔
512

513
  path << "u" << search_univ.id_ << "->";
2,565,574✔
514

515
  // Check to see if this universe directly contains the target cell.  If so,
516
  // write to the path and return.
517
  for (int32_t cell_indx : search_univ.cells_) {
11,902,935✔
518
    if ((cell_indx == target_cell) && (offset == target_offset)) {
10,264,408!
519
      Cell& c = *model::cells[cell_indx];
927,047✔
520
      path << "c" << c.id_;
927,047✔
521
      return path.str();
1,854,094✔
522
    }
523
  }
524

525
  // The target must be further down the geometry tree and contained in a fill
526
  // cell or lattice cell in this universe.  Find which cell contains the
527
  // target.
528
  vector<std::int32_t>::const_reverse_iterator cell_it {
529
    search_univ.cells_.crbegin()};
1,638,527✔
530
  for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
9,337,196!
531
    Cell& c = *model::cells[*cell_it];
9,337,196✔
532

533
    // Material cells don't contain other cells so ignore them.
534
    if (c.type_ != Fill::MATERIAL) {
9,337,196✔
535
      int32_t temp_offset = offset + c.offset_[map];
2,338,556✔
536
      if (c.type_ == Fill::LATTICE) {
2,338,556!
537
        Lattice& lat = *model::lattices[c.fill_];
2,338,556✔
538
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
2,338,556✔
539
        temp_offset += lat.offsets_[indx];
2,338,556✔
540
      }
541

542
      // The desired cell is the first cell that gives an offset smaller or
543
      // equal to the target offset.
544
      if (temp_offset <= target_offset)
2,338,556✔
545
        break;
1,638,527✔
546
    }
547
  }
548

549
  // if we get through the loop without finding an appropriate entry, throw
550
  // an error
551
  if (cell_it == search_univ.cells_.crend()) {
1,638,527!
UNCOV
552
    fatal_error(
×
UNCOV
553
      fmt::format("Failed to generate a text label for distribcell with ID {}."
×
554
                  "The current label is: '{}'",
UNCOV
555
        model::cells[target_cell]->id_, path.str()));
×
556
  }
557

558
  // Add the cell to the path string.
559
  Cell& c = *model::cells[*cell_it];
1,638,527✔
560
  path << "c" << c.id_ << "->";
1,638,527✔
561

562
  if (c.type_ == Fill::UNIVERSE) {
1,638,527!
563
    // Recurse into the fill cell.
UNCOV
564
    offset += c.offset_[map];
×
UNCOV
565
    path << distribcell_path_inner(
×
UNCOV
566
      target_cell, map, target_offset, *model::universes[c.fill_], offset);
×
UNCOV
567
    return path.str();
×
568
  } else {
569
    // Recurse into the lattice cell.
570
    Lattice& lat = *model::lattices[c.fill_];
1,638,527✔
571
    path << "l" << lat.id_;
1,638,527✔
572
    for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) {
289,048,760!
573
      int32_t indx = lat.universes_.size() * map + it.indx_;
289,048,760✔
574
      int32_t temp_offset = offset + lat.offsets_[indx] + c.offset_[map];
289,048,760✔
575
      if (temp_offset <= target_offset) {
289,048,760✔
576
        offset = temp_offset;
1,638,527✔
577
        path << "(" << lat.index_to_string(it.indx_) << ")->";
1,638,527✔
578
        path << distribcell_path_inner(
3,277,054✔
579
          target_cell, map, target_offset, *model::universes[*it], offset);
3,277,054✔
580
        return path.str();
3,277,054✔
581
      }
582
    }
UNCOV
583
    throw std::runtime_error {"Error determining distribcell path."};
×
584
  }
585
}
2,565,574✔
586

587
std::string distribcell_path(
927,047✔
588
  int32_t target_cell, int32_t map, int32_t target_offset)
589
{
590
  auto& root_univ = *model::universes[model::root_universe];
927,047✔
591
  return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0);
927,047✔
592
}
593

594
//==============================================================================
595

596
int maximum_levels(int32_t univ)
941,832✔
597
{
598

599
  const auto level_count = model::universe_level_counts.find(univ);
941,832✔
600
  if (level_count != model::universe_level_counts.end()) {
941,832✔
601
    return level_count->second;
922,354✔
602
  }
603

604
  int levels_below {0};
19,478✔
605

606
  for (int32_t cell_indx : model::universes[univ]->cells_) {
53,976✔
607
    Cell& c = *model::cells[cell_indx];
34,498✔
608
    if (c.type_ == Fill::UNIVERSE) {
34,498✔
609
      int32_t next_univ = c.fill_;
5,533✔
610
      levels_below = std::max(levels_below, maximum_levels(next_univ));
5,533✔
611
    } else if (c.type_ == Fill::LATTICE) {
28,965✔
612
      Lattice& lat = *model::lattices[c.fill_];
1,946✔
613
      for (auto it = lat.begin(); it != lat.end(); ++it) {
930,343✔
614
        int32_t next_univ = *it;
928,397✔
615
        levels_below = std::max(levels_below, maximum_levels(next_univ));
928,397✔
616
      }
617
    }
618
  }
619

620
  ++levels_below;
19,478✔
621
  model::universe_level_counts[univ] = levels_below;
19,478✔
622
  return levels_below;
19,478✔
623
}
624

625
bool is_root_universe(int32_t univ_id)
16,882✔
626
{
627
  return model::universe_map[univ_id] == model::root_universe;
16,882✔
628
}
629

630
//==============================================================================
631

632
void free_memory_geometry()
8,020✔
633
{
634
  model::cells.clear();
8,020✔
635
  model::cell_map.clear();
8,020✔
636

637
  model::universes.clear();
8,020✔
638
  model::universe_map.clear();
8,020✔
639

640
  model::lattices.clear();
8,020✔
641
  model::lattice_map.clear();
8,020✔
642

643
  model::overlap_check_count.clear();
8,020✔
644
}
8,020✔
645

646
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc