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

openmc-dev / openmc / 29109839871

10 Jul 2026 05:07PM UTC coverage: 81.355% (+0.06%) from 81.295%
29109839871

Pull #3971

github

web-flow
Merge b03e8c77b into 7256d5046
Pull Request #3971: Delta tracking

18567 of 26880 branches covered (69.07%)

Branch coverage included in aggregate %.

604 of 650 new or added lines in 20 files covered. (92.92%)

144 existing lines in 3 files now uncovered.

59962 of 69646 relevant lines covered (86.1%)

49564145.57 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

91
void adjust_indices()
9,036✔
92
{
93
  // Adjust material/fill idices.
94
  for (auto& c : model::cells) {
45,633✔
95
    if (c->fill_ != C_NONE) {
36,597✔
96
      int32_t id = c->fill_;
7,409✔
97
      auto search_univ = model::universe_map.find(id);
7,409✔
98
      auto search_lat = model::lattice_map.find(id);
7,409✔
99
      if (search_univ != model::universe_map.end()) {
7,409✔
100
        c->type_ = Fill::UNIVERSE;
5,277✔
101
        c->fill_ = search_univ->second;
5,277✔
102
      } else if (search_lat != model::lattice_map.end()) {
2,132!
103
        c->type_ = Fill::LATTICE;
2,132✔
104
        c->fill_ = search_lat->second;
2,132✔
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,188✔
112
      for (auto& mat_id : c->material_) {
59,705✔
113
        if (mat_id != MATERIAL_VOID) {
30,517✔
114
          auto search = model::material_map.find(mat_id);
21,468!
115
          if (search == model::material_map.end()) {
21,468!
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,468✔
122
        }
123
      }
124
    }
125
  }
126

127
  // Change cell.universe values from IDs to indices.
128
  for (auto& c : model::cells) {
45,633✔
129
    auto search = model::universe_map.find(c->universe_);
36,597!
130
    if (search != model::universe_map.end()) {
36,597!
131
      c->universe_ = search->second;
36,597✔
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,135✔
140
    l->adjust_indices();
2,099✔
141
  }
142
}
9,036✔
143

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

147
void partition_universes()
9,036✔
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,073✔
152
    if (univ->cells_.size() > 10) {
21,037✔
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,036✔
176

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

179
void assign_temperatures()
9,036✔
180
{
181
  for (auto& c : model::cells) {
45,633✔
182
    // Ignore non-material cells and cells with defined temperature.
183
    if (c->material_.size() == 0)
36,597✔
184
      continue;
7,409✔
185
    if (c->sqrtkT_.size() > 0)
29,188✔
186
      continue;
551✔
187

188
    c->sqrtkT_.reserve(c->material_.size());
28,637✔
189
    for (auto i_mat : c->material_) {
58,594✔
190
      if (i_mat == MATERIAL_VOID) {
29,957✔
191
        // Set void region to 0K.
192
        c->sqrtkT_.push_back(0);
9,049✔
193
      } else {
194
        const auto& mat {model::materials[i_mat]};
20,908✔
195
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
20,908✔
196
      }
197
    }
198
  }
199
}
9,036✔
200

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

203
void finalize_cell_densities()
9,036✔
204
{
205
  for (auto& c : model::cells) {
45,633✔
206
    // Convert to density multipliers.
207
    if (!c->density_mult_.empty()) {
36,597✔
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,492✔
215
    }
216
  }
217
}
9,036✔
218

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

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

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

235
      // Get temperature(s) of cell (rounding to nearest integer)
236
      vector<double> cell_temps;
21,177✔
237
      if (cell->sqrtkT_.size() == 1) {
21,177✔
238
        double sqrtkT = cell->sqrtkT_[0];
19,612✔
239
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
19,612✔
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,177✔
249
      for (const auto& i_nuc : mat->nuclide_) {
94,321✔
250
        for (double temperature : cell_temps) {
147,368✔
251
          // Add temperature if it hasn't already been added
252
          if (!contains(nuc_temps[i_nuc], temperature))
74,224✔
253
            nuc_temps[i_nuc].push_back(temperature);
35,222✔
254
        }
255
      }
256

257
      for (const auto& table : mat->thermal_tables_) {
24,767✔
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,177✔
268
  }
269
}
8,917✔
270

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

273
void detect_boundary_surfaces()
9,036✔
274
{
275
  for (int i = 0; i < model::surfaces.size(); i++) {
56,606✔
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];
47,570✔
279
    if (s->bc_) {
47,570✔
280
      model::boundary_surfaces.push_back(i);
28,929✔
281
    }
282
  }
283
}
9,036✔
284

285
//==============================================================================
286

287
void finalize_geometry()
9,036✔
288
{
289
  // Perform some final operations to set up the geometry
290
  adjust_indices();
9,036✔
291
  count_universe_instances();
9,036✔
292
  partition_universes();
9,036✔
293

294
  // Assign temperatures to cells that don't have temperatures already assigned
295
  assign_temperatures();
9,036✔
296

297
  // Find all boundary surfaces. Used in delta tracking to trace through the
298
  // geometry.
299
  detect_boundary_surfaces();
9,036✔
300

301
  // Determine number of nested coordinate levels in the geometry
302
  model::n_coord_levels = maximum_levels(model::root_universe);
9,036✔
303
}
9,036✔
304

305
//==============================================================================
306

307
int32_t find_root_universe()
9,036✔
308
{
309
  // Find all the universes listed as a cell fill.
310
  std::unordered_set<int32_t> fill_univ_ids;
9,036✔
311
  for (const auto& c : model::cells) {
45,633✔
312
    fill_univ_ids.insert(c->fill_);
36,597✔
313
  }
314

315
  // Find all the universes contained in a lattice.
316
  for (const auto& lat : model::lattices) {
11,135✔
317
    for (auto it = lat->begin(); it != lat->end(); ++it) {
969,362✔
318
      fill_univ_ids.insert(*it);
967,263✔
319
    }
320
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
2,099✔
321
      fill_univ_ids.insert(lat->outer_);
430✔
322
    }
323
  }
324

325
  // Figure out which universe is not in the set.  This is the root universe.
326
  bool root_found {false};
327
  int32_t root_univ;
328
  for (int32_t i = 0; i < model::universes.size(); i++) {
30,073✔
329
    auto search = fill_univ_ids.find(model::universes[i]->id_);
21,037✔
330
    if (search == fill_univ_ids.end()) {
21,037✔
331
      if (root_found) {
9,036!
UNCOV
332
        fatal_error("Two or more universes are not used as fill universes, so "
×
333
                    "it is not possible to distinguish which one is the root "
334
                    "universe.");
335
      } else {
336
        root_found = true;
337
        root_univ = i;
338
      }
339
    }
340
  }
341
  if (!root_found)
9,036!
UNCOV
342
    fatal_error("Could not find a root universe.  Make sure "
×
343
                "there are no circular dependencies in the geometry.");
344

345
  return root_univ;
9,036✔
346
}
9,036✔
347

348
//==============================================================================
349

350
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
9,031✔
351
{
352
  write_message("Preparing distributed cell instances...", 5);
9,031✔
353

354
  std::unordered_set<int32_t> distribcells;
9,031✔
355

356
  // start with any cells manually specified via the C++ API
357
  if (user_distribcells) {
9,031✔
358
    distribcells.insert(user_distribcells->begin(), user_distribcells->end());
15✔
359
  }
360

361
  // Find all cells listed in a DistribcellFilter or CellInstanceFilter
362
  for (auto& filt : model::tally_filters) {
19,743✔
363
    auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
10,712!
364
    auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
10,712!
365
    if (distrib_filt) {
10,712✔
366
      distribcells.insert(distrib_filt->cell());
179✔
367
    }
368
    if (cell_inst_filt) {
10,712✔
369
      const auto& filter_cells = cell_inst_filt->cells();
32✔
370
      distribcells.insert(filter_cells.begin(), filter_cells.end());
32✔
371
    }
372
  }
373

374
  // By default, add material cells to the list of distributed cells
375
  if (settings::material_cell_offsets) {
9,031!
376
    for (int64_t i = 0; i < model::cells.size(); ++i) {
45,675✔
377
      if (model::cells[i]->type_ == Fill::MATERIAL)
36,644✔
378
        distribcells.insert(i);
29,205✔
379
    }
380
  }
381

382
  // Make sure that the number of materials/temperatures matches the number of
383
  // cell instances.
384
  for (int i = 0; i < model::cells.size(); i++) {
45,675✔
385
    Cell& c {*model::cells[i]};
36,644✔
386

387
    if (c.material_.size() > 1) {
36,644✔
388
      if (c.material_.size() != c.n_instances()) {
203!
UNCOV
389
        fatal_error(fmt::format(
×
390
          "Cell {} was specified with {} materials but has {} distributed "
391
          "instances. The number of materials must equal one or the number "
392
          "of instances.",
UNCOV
393
          c.id_, c.material_.size(), c.n_instances()));
×
394
      }
395
    }
396

397
    if (c.sqrtkT_.size() > 1) {
36,644✔
398
      if (c.sqrtkT_.size() != c.n_instances()) {
260!
UNCOV
399
        fatal_error(fmt::format(
×
400
          "Cell {} was specified with {} temperatures but has {} distributed "
401
          "instances. The number of temperatures must equal one or the number "
402
          "of instances.",
403
          c.id_, c.sqrtkT_.size(), c.n_instances()));
×
404
      }
405
    }
406

407
    if (c.density_mult_.size() > 1) {
36,644✔
408
      if (c.density_mult_.size() != c.n_instances()) {
90!
UNCOV
409
        fatal_error(fmt::format("Cell {} was specified with {} density "
×
410
                                "multipliers but has {} distributed "
411
                                "instances. The number of density multipliers "
412
                                "must equal one or the number "
413
                                "of instances.",
UNCOV
414
          c.id_, c.density_mult_.size(), c.n_instances()));
×
415
      }
416
    }
417
  }
418

419
  // Search through universes for material cells and assign each one a
420
  // distribcell array index according to the containing universe.
421
  vector<int32_t> target_univ_ids;
9,031✔
422
  for (const auto& u : model::universes) {
30,093✔
423
    for (auto idx : u->cells_) {
57,706✔
424
      if (distribcells.find(idx) != distribcells.end()) {
36,644✔
425
        if (!contains(target_univ_ids, u->id_)) {
29,280✔
426
          target_univ_ids.push_back(u->id_);
18,143✔
427
        }
428
        model::cells[idx]->distribcell_index_ =
29,280✔
429
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
29,280✔
430
          target_univ_ids.begin();
29,280✔
431
      }
432
    }
433
  }
434

435
  // Allocate the cell and lattice offset tables.
436
  int n_maps = target_univ_ids.size();
9,031✔
437
  for (auto& c : model::cells) {
45,675✔
438
    if (c->type_ != Fill::MATERIAL) {
36,644✔
439
      c->offset_.resize(n_maps, C_NONE);
7,439✔
440
    }
441
  }
442
  for (auto& lat : model::lattices) {
11,145✔
443
    lat->allocate_offset_table(n_maps);
2,114✔
444
  }
445

446
// Fill the cell and lattice offset tables.
447
#pragma omp parallel for
5,161✔
448
  for (int map = 0; map < target_univ_ids.size(); map++) {
9,023✔
449
    auto target_univ_id = target_univ_ids[map];
5,153✔
450
    std::unordered_map<int32_t, int32_t> univ_count_memo;
5,153✔
451
    for (const auto& univ : model::universes) {
22,526✔
452
      int32_t offset = 0;
17,373✔
453
      for (int32_t cell_indx : univ->cells_) {
75,784✔
454
        Cell& c = *model::cells[cell_indx];
58,411✔
455

456
        if (c.type_ == Fill::UNIVERSE) {
58,411✔
457
          c.offset_[map] = offset;
31,283✔
458
          int32_t search_univ = c.fill_;
31,283✔
459
          offset += count_universe_instances(
31,283✔
460
            search_univ, target_univ_id, univ_count_memo);
461

462
        } else if (c.type_ == Fill::LATTICE) {
27,128✔
463
          c.offset_[map] = offset;
2,811✔
464
          Lattice& lat = *model::lattices[c.fill_];
2,811✔
465
          offset += lat.fill_offset_table(target_univ_id, map, univ_count_memo);
2,811✔
466
        }
467
      }
468
    }
469
  }
5,153✔
470
}
18,062✔
471

472
//==============================================================================
473

474
void count_universe_instances()
9,036✔
475
{
476
  for (auto& univ : model::universes) {
30,073✔
477
    std::unordered_map<int32_t, int32_t> univ_count_memo;
21,037✔
478
    univ->n_instances_ = count_universe_instances(
21,037✔
479
      model::root_universe, univ->id_, univ_count_memo);
21,037✔
480
  }
21,037✔
481
}
9,036✔
482

483
//==============================================================================
484

485
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
22,667,275✔
486
  std::unordered_map<int32_t, int32_t>& univ_count_memo)
487
{
488
  // If this is the target, it can't contain itself.
489
  if (model::universes[search_univ]->id_ == target_univ_id) {
22,667,275✔
490
    return 1;
491
  }
492

493
  // If we have already counted the number of instances, reuse that value.
494
  auto search = univ_count_memo.find(search_univ);
19,935,709✔
495
  if (search != univ_count_memo.end()) {
19,935,709✔
496
    return search->second;
7,870,683✔
497
  }
498

499
  int count {0};
12,065,026✔
500
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
24,303,753✔
501
    Cell& c = *model::cells[cell_indx];
12,238,727✔
502

503
    if (c.type_ == Fill::UNIVERSE) {
12,238,727✔
504
      int32_t next_univ = c.fill_;
159,531✔
505
      count +=
159,531✔
506
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
159,531✔
507

508
    } else if (c.type_ == Fill::LATTICE) {
12,079,196✔
509
      Lattice& lat = *model::lattices[c.fill_];
21,438✔
510
      for (auto it = lat.begin(); it != lat.end(); ++it) {
13,015,789✔
511
        int32_t next_univ = *it;
12,994,351✔
512
        count +=
12,994,351✔
513
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
12,994,351✔
514
      }
515
    }
516
  }
517

518
  // Remember the number of instances in this universe.
519
  univ_count_memo[search_univ] = count;
12,065,026✔
520

521
  return count;
12,065,026✔
522
}
523

524
//==============================================================================
525

526
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
2,565,574✔
527
  int32_t target_offset, const Universe& search_univ, int32_t offset)
528
{
529
  std::stringstream path;
2,565,574✔
530

531
  path << "u" << search_univ.id_ << "->";
2,565,574✔
532

533
  // Check to see if this universe directly contains the target cell.  If so,
534
  // write to the path and return.
535
  for (int32_t cell_indx : search_univ.cells_) {
11,902,935✔
536
    if ((cell_indx == target_cell) && (offset == target_offset)) {
10,264,408✔
537
      Cell& c = *model::cells[cell_indx];
927,047✔
538
      path << "c" << c.id_;
927,047✔
539
      return path.str();
927,047✔
540
    }
541
  }
542

543
  // The target must be further down the geometry tree and contained in a fill
544
  // cell or lattice cell in this universe.  Find which cell contains the
545
  // target.
546
  vector<std::int32_t>::const_reverse_iterator cell_it {
547
    search_univ.cells_.crbegin()};
548
  for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
9,337,196!
549
    Cell& c = *model::cells[*cell_it];
9,337,196✔
550

551
    // Material cells don't contain other cells so ignore them.
552
    if (c.type_ != Fill::MATERIAL) {
9,337,196✔
553
      int32_t temp_offset = offset + c.offset_[map];
2,338,556!
554
      if (c.type_ == Fill::LATTICE) {
2,338,556!
555
        Lattice& lat = *model::lattices[c.fill_];
2,338,556✔
556
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
2,338,556✔
557
        temp_offset += lat.offsets_[indx];
2,338,556✔
558
      }
559

560
      // The desired cell is the first cell that gives an offset smaller or
561
      // equal to the target offset.
562
      if (temp_offset <= target_offset)
2,338,556✔
563
        break;
564
    }
565
  }
566

567
  // if we get through the loop without finding an appropriate entry, throw
568
  // an error
569
  if (cell_it == search_univ.cells_.crend()) {
1,638,527!
UNCOV
570
    fatal_error(
×
UNCOV
571
      fmt::format("Failed to generate a text label for distribcell with ID {}."
×
572
                  "The current label is: '{}'",
UNCOV
573
        model::cells[target_cell]->id_, path.str()));
×
574
  }
575

576
  // Add the cell to the path string.
577
  Cell& c = *model::cells[*cell_it];
1,638,527✔
578
  path << "c" << c.id_ << "->";
1,638,527✔
579

580
  if (c.type_ == Fill::UNIVERSE) {
1,638,527!
581
    // Recurse into the fill cell.
UNCOV
582
    offset += c.offset_[map];
×
UNCOV
583
    path << distribcell_path_inner(
×
584
      target_cell, map, target_offset, *model::universes[c.fill_], offset);
×
585
    return path.str();
×
586
  } else {
587
    // Recurse into the lattice cell.
588
    Lattice& lat = *model::lattices[c.fill_];
1,638,527✔
589
    path << "l" << lat.id_;
1,638,527✔
590
    for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) {
289,048,760!
591
      int32_t indx = lat.universes_.size() * map + it.indx_;
289,048,760✔
592
      int32_t temp_offset = offset + lat.offsets_[indx] + c.offset_[map];
289,048,760✔
593
      if (temp_offset <= target_offset) {
289,048,760✔
594
        offset = temp_offset;
1,638,527✔
595
        path << "(" << lat.index_to_string(it.indx_) << ")->";
3,277,054✔
596
        path << distribcell_path_inner(
1,638,527✔
597
          target_cell, map, target_offset, *model::universes[*it], offset);
3,277,054✔
598
        return path.str();
1,638,527✔
599
      }
600
    }
UNCOV
601
    throw std::runtime_error {"Error determining distribcell path."};
×
602
  }
603
}
2,565,574✔
604

605
std::string distribcell_path(
927,047✔
606
  int32_t target_cell, int32_t map, int32_t target_offset)
607
{
608
  auto& root_univ = *model::universes[model::root_universe];
927,047✔
609
  return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0);
927,047✔
610
}
611

612
//==============================================================================
613

614
int maximum_levels(int32_t univ)
981,741✔
615
{
616

617
  const auto level_count = model::universe_level_counts.find(univ);
981,741✔
618
  if (level_count != model::universe_level_counts.end()) {
981,741✔
619
    return level_count->second;
960,859✔
620
  }
621

622
  int levels_below {0};
20,882✔
623

624
  for (int32_t cell_indx : model::universes[univ]->cells_) {
57,316✔
625
    Cell& c = *model::cells[cell_indx];
36,434✔
626
    if (c.type_ == Fill::UNIVERSE) {
36,434✔
627
      int32_t next_univ = c.fill_;
5,277✔
628
      levels_below = std::max(levels_below, maximum_levels(next_univ));
7,294✔
629
    } else if (c.type_ == Fill::LATTICE) {
31,157✔
630
      Lattice& lat = *model::lattices[c.fill_];
2,132✔
631
      for (auto it = lat.begin(); it != lat.end(); ++it) {
969,560✔
632
        int32_t next_univ = *it;
967,428✔
633
        levels_below = std::max(levels_below, maximum_levels(next_univ));
969,467✔
634
      }
635
    }
636
  }
637

638
  ++levels_below;
20,882✔
639
  model::universe_level_counts[univ] = levels_below;
20,882✔
640
  return levels_below;
20,882✔
641
}
642

643
bool is_root_universe(int32_t univ_id)
18,923✔
644
{
645
  return model::universe_map[univ_id] == model::root_universe;
18,923✔
646
}
647

648
//==============================================================================
649

650
void free_memory_geometry()
9,162✔
651
{
652
  model::cells.clear();
9,162✔
653
  model::cell_map.clear();
9,162✔
654

655
  model::universes.clear();
9,162✔
656
  model::universe_map.clear();
9,162✔
657

658
  model::lattices.clear();
9,162✔
659
  model::lattice_map.clear();
9,162✔
660

661
  model::overlap_check_count.clear();
9,162✔
662
}
9,162✔
663

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