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

openmc-dev / openmc / 21321806885

24 Jan 2026 09:21PM UTC coverage: 81.864% (-0.1%) from 81.977%
21321806885

Pull #3749

github

web-flow
Merge 200b0cb49 into 3e2f1f521
Pull Request #3749: C-API bindings for key random ray functions

16678 of 22973 branches covered (72.6%)

Branch coverage included in aggregate %.

31 of 31 new or added lines in 3 files covered. (100.0%)

308 existing lines in 25 files now uncovered.

54820 of 64365 relevant lines covered (85.17%)

43016347.45 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

91
void adjust_indices()
7,132✔
92
{
93
  // Adjust material/fill idices.
94
  for (auto& c : model::cells) {
36,160✔
95
    if (c->fill_ != C_NONE) {
29,028✔
96
      int32_t id = c->fill_;
6,410✔
97
      auto search_univ = model::universe_map.find(id);
6,410✔
98
      auto search_lat = model::lattice_map.find(id);
6,410✔
99
      if (search_univ != model::universe_map.end()) {
6,410✔
100
        c->type_ = Fill::UNIVERSE;
4,698✔
101
        c->fill_ = search_univ->second;
4,698✔
102
      } else if (search_lat != model::lattice_map.end()) {
1,712!
103
        c->type_ = Fill::LATTICE;
1,712✔
104
        c->fill_ = search_lat->second;
1,712✔
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;
22,618✔
112
      for (auto& mat_id : c->material_) {
46,416✔
113
        if (mat_id != MATERIAL_VOID) {
23,798✔
114
          auto search = model::material_map.find(mat_id);
17,405✔
115
          if (search == model::material_map.end()) {
17,405!
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;
17,405✔
122
        }
123
      }
124
    }
125
  }
126

127
  // Change cell.universe values from IDs to indices.
128
  for (auto& c : model::cells) {
36,160✔
129
    auto search = model::universe_map.find(c->universe_);
29,028✔
130
    if (search != model::universe_map.end()) {
29,028!
131
      c->universe_ = search->second;
29,028✔
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) {
8,814✔
140
    l->adjust_indices();
1,682✔
141
  }
142
}
7,132✔
143

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

147
void partition_universes()
7,132✔
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) {
23,312✔
152
    if (univ->cells_.size() > 10) {
16,180✔
153
      // Collect the set of surfaces in this universe.
154
      std::unordered_set<int32_t> surf_inds;
156✔
155
      for (auto i_cell : univ->cells_) {
2,642✔
156
        for (auto token : model::cells[i_cell]->surfaces()) {
9,412✔
157
          surf_inds.insert(std::abs(token) - 1);
6,926✔
158
        }
2,486✔
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;
156✔
164
      for (auto i_surf : surf_inds) {
2,020✔
165
        if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
1,948!
166
          ++n_zplanes;
560✔
167
          if (n_zplanes > 5) {
560✔
168
            univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
84✔
169
            break;
84✔
170
          }
171
        }
172
      }
173
    }
156✔
174
  }
175
}
7,132✔
176

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

179
void assign_temperatures()
7,132✔
180
{
181
  for (auto& c : model::cells) {
36,160✔
182
    // Ignore non-material cells and cells with defined temperature.
183
    if (c->material_.size() == 0)
29,028✔
184
      continue;
6,410✔
185
    if (c->sqrtkT_.size() > 0)
22,618✔
186
      continue;
276✔
187

188
    c->sqrtkT_.reserve(c->material_.size());
22,342✔
189
    for (auto i_mat : c->material_) {
45,864✔
190
      if (i_mat == MATERIAL_VOID) {
23,522✔
191
        // Set void region to 0K.
192
        c->sqrtkT_.push_back(0);
6,393✔
193
      } else {
194
        const auto& mat {model::materials[i_mat]};
17,129✔
195
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
17,129✔
196
      }
197
    }
198
  }
199
}
7,132✔
200

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

203
void finalize_cell_densities()
7,132✔
204
{
205
  for (auto& c : model::cells) {
36,160✔
206
    // Convert to density multipliers.
207
    if (!c->density_mult_.empty()) {
29,028✔
208
      for (int32_t instance = 0; instance < c->density_mult_.size();
1,148✔
209
           ++instance) {
210
        c->density_mult_[instance] /=
1,078✔
211
          model::materials[c->material(instance)]->density_gpcc();
1,078✔
212
      }
213
    } else {
214
      c->density_mult_ = {1.0};
28,958✔
215
    }
216
  }
217
}
7,132✔
218

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

221
void get_temperatures(
7,024✔
222
  vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
223
{
224
  for (const auto& cell : model::cells) {
35,758✔
225
    // Skip non-material cells.
226
    if (cell->fill_ != C_NONE)
28,734✔
227
      continue;
6,400✔
228

229
    for (int j = 0; j < cell->material_.size(); ++j) {
45,848✔
230
      // Skip void materials
231
      int i_material = cell->material_[j];
23,514✔
232
      if (i_material == MATERIAL_VOID)
23,514✔
233
        continue;
6,373✔
234

235
      // Get temperature(s) of cell (rounding to nearest integer)
236
      vector<double> cell_temps;
17,141✔
237
      if (cell->sqrtkT_.size() == 1) {
17,141✔
238
        double sqrtkT = cell->sqrtkT_[0];
15,781✔
239
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
15,781✔
240
      } else if (cell->sqrtkT_.size() == cell->material_.size()) {
1,360✔
241
        double sqrtkT = cell->sqrtkT_[j];
1,346✔
242
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,346✔
243
      } else {
244
        for (double sqrtkT : cell->sqrtkT_)
70✔
245
          cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
56✔
246
      }
247

248
      const auto& mat {model::materials[i_material]};
17,141✔
249
      for (const auto& i_nuc : mat->nuclide_) {
77,458✔
250
        for (double temperature : cell_temps) {
120,676✔
251
          // Add temperature if it hasn't already been added
252
          if (!contains(nuc_temps[i_nuc], temperature))
60,359✔
253
            nuc_temps[i_nuc].push_back(temperature);
26,687✔
254
        }
255
      }
256

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

261
        for (double temperature : cell_temps) {
5,820✔
262
          // Add temperature if it hasn't already been added
263
          if (!contains(thermal_temps[i_sab], temperature))
2,910✔
264
            thermal_temps[i_sab].push_back(temperature);
1,090✔
265
        }
266
      }
267
    }
17,141✔
268
  }
269
}
7,024✔
270

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

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

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

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

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

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

297
  // Find all the universes contained in a lattice.
298
  for (const auto& lat : model::lattices) {
8,814✔
299
    for (auto it = lat->begin(); it != lat->end(); ++it) {
838,682✔
300
      fill_univ_ids.insert(*it);
837,000✔
301
    }
302
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
1,682✔
303
      fill_univ_ids.insert(lat->outer_);
364✔
304
    }
305
  }
306

307
  // Figure out which universe is not in the set.  This is the root universe.
308
  bool root_found {false};
7,132✔
309
  int32_t root_univ;
310
  for (int32_t i = 0; i < model::universes.size(); i++) {
23,312✔
311
    auto search = fill_univ_ids.find(model::universes[i]->id_);
16,180✔
312
    if (search == fill_univ_ids.end()) {
16,180✔
313
      if (root_found) {
7,132!
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,132✔
319
        root_univ = i;
7,132✔
320
      }
321
    }
322
  }
323
  if (!root_found)
7,132!
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,132✔
328
}
7,132✔
329

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

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

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

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

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

356
  // By default, add material cells to the list of distributed cells
357
  if (settings::material_cell_offsets) {
7,130!
358
    for (int64_t i = 0; i < model::cells.size(); ++i) {
36,212✔
359
      if (model::cells[i]->type_ == Fill::MATERIAL)
29,082✔
360
        distribcells.insert(i);
22,644✔
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++) {
36,212✔
367
    Cell& c {*model::cells[i]};
29,082✔
368

369
    if (c.material_.size() > 1) {
29,082✔
370
      if (c.material_.size() != c.n_instances()) {
180!
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.",
375
          c.id_, c.material_.size(), c.n_instances()));
×
376
      }
377
    }
378

379
    if (c.sqrtkT_.size() > 1) {
29,082✔
380
      if (c.sqrtkT_.size() != c.n_instances()) {
194!
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.",
385
          c.id_, c.sqrtkT_.size(), c.n_instances()));
×
386
      }
387
    }
388

389
    if (c.density_mult_.size() > 1) {
29,082✔
390
      if (c.density_mult_.size() != c.n_instances()) {
56!
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.",
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,130✔
404
  for (const auto& u : model::universes) {
23,336✔
405
    for (auto idx : u->cells_) {
45,288✔
406
      if (distribcells.find(idx) != distribcells.end()) {
29,082✔
407
        if (!contains(target_univ_ids, u->id_)) {
22,714✔
408
          target_univ_ids.push_back(u->id_);
13,795✔
409
        }
410
        model::cells[idx]->distribcell_index_ =
22,714✔
411
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
22,714✔
412
          target_univ_ids.begin();
45,428✔
413
      }
414
    }
415
  }
416

417
  // Allocate the cell and lattice offset tables.
418
  int n_maps = target_univ_ids.size();
7,130✔
419
  for (auto& c : model::cells) {
36,212✔
420
    if (c->type_ != Fill::MATERIAL) {
29,082✔
421
      c->offset_.resize(n_maps, C_NONE);
6,438✔
422
    }
423
  }
424
  for (auto& lat : model::lattices) {
8,826✔
425
    lat->allocate_offset_table(n_maps);
1,696✔
426
  }
427

428
// Fill the cell and lattice offset tables.
429
#pragma omp parallel for
3,560✔
430
  for (int map = 0; map < target_univ_ids.size(); map++) {
8,480✔
431
    auto target_univ_id = target_univ_ids[map];
4,910✔
432
    std::unordered_map<int32_t, int32_t> univ_count_memo;
4,910✔
433
    for (const auto& univ : model::universes) {
23,312✔
434
      int32_t offset = 0;
18,402✔
435
      for (int32_t cell_indx : univ->cells_) {
83,112✔
436
        Cell& c = *model::cells[cell_indx];
64,710✔
437

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

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

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

456
void count_universe_instances()
7,132✔
457
{
458
  for (auto& univ : model::universes) {
23,312✔
459
    std::unordered_map<int32_t, int32_t> univ_count_memo;
16,180✔
460
    univ->n_instances_ = count_universe_instances(
16,180✔
461
      model::root_universe, univ->id_, univ_count_memo);
16,180✔
462
  }
16,180✔
463
}
7,132✔
464

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

467
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
17,335,403✔
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) {
17,335,403✔
472
    return 1;
2,365,418✔
473
  }
474

475
  // If we have already counted the number of instances, reuse that value.
476
  auto search = univ_count_memo.find(search_univ);
14,969,985✔
477
  if (search != univ_count_memo.end()) {
14,969,985✔
478
    return search->second;
6,909,706✔
479
  }
480

481
  int count {0};
8,060,279✔
482
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
16,281,072✔
483
    Cell& c = *model::cells[cell_indx];
8,220,793✔
484

485
    if (c.type_ == Fill::UNIVERSE) {
8,220,793✔
486
      int32_t next_univ = c.fill_;
148,060✔
487
      count +=
148,060✔
488
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
148,060✔
489

490
    } else if (c.type_ == Fill::LATTICE) {
8,072,733✔
491
      Lattice& lat = *model::lattices[c.fill_];
17,774✔
492
      for (auto it = lat.begin(); it != lat.end(); ++it) {
10,161,768✔
493
        int32_t next_univ = *it;
10,143,994✔
494
        count +=
10,143,994✔
495
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
10,143,994✔
496
      }
497
    }
498
  }
499

500
  // Remember the number of instances in this universe.
501
  univ_count_memo[search_univ] = count;
8,060,279✔
502

503
  return count;
8,060,279✔
504
}
505

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

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

513
  path << "u" << search_univ.id_ << "->";
2,332,340✔
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_) {
10,820,850✔
518
    if ((cell_indx == target_cell) && (offset == target_offset)) {
9,331,280!
519
      Cell& c = *model::cells[cell_indx];
842,770✔
520
      path << "c" << c.id_;
842,770✔
521
      return path.str();
1,685,540✔
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,489,570✔
530
  for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
8,488,360!
531
    Cell& c = *model::cells[*cell_it];
8,488,360✔
532

533
    // Material cells don't contain other cells so ignore them.
534
    if (c.type_ != Fill::MATERIAL) {
8,488,360✔
535
      int32_t temp_offset = offset + c.offset_[map];
2,125,960✔
536
      if (c.type_ == Fill::LATTICE) {
2,125,960!
537
        Lattice& lat = *model::lattices[c.fill_];
2,125,960✔
538
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
2,125,960✔
539
        temp_offset += lat.offsets_[indx];
2,125,960✔
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,125,960✔
545
        break;
1,489,570✔
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,489,570!
552
    fatal_error(
×
553
      fmt::format("Failed to generate a text label for distribcell with ID {}."
×
554
                  "The current label is: '{}'",
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,489,570✔
560
  path << "c" << c.id_ << "->";
1,489,570✔
561

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

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

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

596
int maximum_levels(int32_t univ)
848,980✔
597
{
598

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

604
  int levels_below {0};
16,038✔
605

606
  for (int32_t cell_indx : model::universes[univ]->cells_) {
44,924✔
607
    Cell& c = *model::cells[cell_indx];
28,886✔
608
    if (c.type_ == Fill::UNIVERSE) {
28,886✔
609
      int32_t next_univ = c.fill_;
4,698✔
610
      levels_below = std::max(levels_below, maximum_levels(next_univ));
4,698✔
611
    } else if (c.type_ == Fill::LATTICE) {
24,188✔
612
      Lattice& lat = *model::lattices[c.fill_];
1,712✔
613
      for (auto it = lat.begin(); it != lat.end(); ++it) {
838,862✔
614
        int32_t next_univ = *it;
837,150✔
615
        levels_below = std::max(levels_below, maximum_levels(next_univ));
837,150✔
616
      }
617
    }
618
  }
619

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

UNCOV
625
bool is_root_universe(int32_t univ_id)
×
626
{
UNCOV
627
  return model::universe_map[univ_id] == model::root_universe;
×
628
}
629

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

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

637
  model::universes.clear();
7,243✔
638
  model::universe_map.clear();
7,243✔
639

640
  model::lattices.clear();
7,243✔
641
  model::lattice_map.clear();
7,243✔
642

643
  model::overlap_check_count.clear();
7,243✔
644
}
7,243✔
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