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

openmc-dev / openmc / 15922743594

27 Jun 2025 09:15AM UTC coverage: 85.262% (+0.01%) from 85.252%
15922743594

Pull #3424

github

web-flow
Merge fe7e4ec10 into 25d64c9b2
Pull Request #3424: Fixed a bug in distribcell offsets logic

35 of 47 new or added lines in 3 files covered. (74.47%)

16 existing lines in 3 files now uncovered.

52627 of 61724 relevant lines covered (85.26%)

37235951.39 hits per line

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

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

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

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

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

26
namespace openmc {
27

28
namespace model {
29
std::unordered_map<int32_t, std::unordered_map<int32_t, int32_t>>
30
  universe_cell_counts;
31
std::unordered_map<int32_t, int32_t> universe_level_counts;
32
} // namespace model
33

34
// adds the cell counts of universe b to universe a
35
void update_universe_cell_count(int32_t a, int32_t b)
887,574✔
36
{
37
  auto& universe_a_counts = model::universe_cell_counts[a];
887,574✔
38
  const auto& universe_b_counts = model::universe_cell_counts[b];
887,574✔
39
  for (const auto& it : universe_b_counts) {
2,495,321✔
40
    universe_a_counts[it.first] += it.second;
1,607,747✔
41
  }
42
}
887,574✔
43

44
void read_geometry_xml()
1,359✔
45
{
46
  // Display output message
47
  write_message("Reading geometry XML file...", 5);
1,359✔
48

49
  // Check if geometry.xml exists
50
  std::string filename = settings::path_input + "geometry.xml";
1,359✔
51
  if (!file_exists(filename)) {
1,359✔
52
    fatal_error("Geometry XML file '" + filename + "' does not exist!");
×
53
  }
54

55
  // Parse settings.xml file
56
  pugi::xml_document doc;
1,359✔
57
  auto result = doc.load_file(filename.c_str());
1,359✔
58
  if (!result) {
1,359✔
59
    fatal_error("Error processing geometry.xml file.");
×
60
  }
61

62
  // Get root element
63
  pugi::xml_node root = doc.document_element();
1,359✔
64

65
  read_geometry_xml(root);
1,359✔
66
}
1,359✔
67

68
void read_geometry_xml(pugi::xml_node root)
6,749✔
69
{
70
  // Read surfaces, cells, lattice
71
  read_surfaces(root);
6,749✔
72
  read_cells(root);
6,749✔
73
  read_lattices(root);
6,747✔
74

75
  // Check to make sure a boundary condition was applied to at least one
76
  // surface
77
  bool boundary_exists = false;
6,747✔
78
  for (const auto& surf : model::surfaces) {
17,056✔
79
    if (surf->bc_) {
17,023✔
80
      boundary_exists = true;
6,714✔
81
      break;
6,714✔
82
    }
83
  }
84

85
  if (settings::run_mode != RunMode::PLOTTING &&
6,747✔
86
      settings::run_mode != RunMode::VOLUME && !boundary_exists) {
6,471✔
87
    fatal_error("No boundary conditions were applied to any surfaces!");
×
88
  }
89

90
  // Allocate universes, universe cell arrays, and assign base universe
91
  model::root_universe = find_root_universe();
6,747✔
92

93
  // if the root universe is DAGMC geometry, make sure the model is well-formed
94
  check_dagmc_root_univ();
6,747✔
95
}
6,747✔
96

97
//==============================================================================
98

99
void adjust_indices()
6,749✔
100
{
101
  // Adjust material/fill idices.
102
  for (auto& c : model::cells) {
38,260✔
103
    if (c->fill_ != C_NONE) {
31,511✔
104
      int32_t id = c->fill_;
6,797✔
105
      auto search_univ = model::universe_map.find(id);
6,797✔
106
      auto search_lat = model::lattice_map.find(id);
6,797✔
107
      if (search_univ != model::universe_map.end()) {
6,797✔
108
        c->type_ = Fill::UNIVERSE;
5,001✔
109
        c->fill_ = search_univ->second;
5,001✔
110
      } else if (search_lat != model::lattice_map.end()) {
1,796✔
111
        c->type_ = Fill::LATTICE;
1,796✔
112
        c->fill_ = search_lat->second;
1,796✔
113
      } else {
114
        fatal_error(fmt::format("Specified fill {} on cell {} is neither a "
×
115
                                "universe nor a lattice.",
116
          id, c->id_));
×
117
      }
118
    } else {
119
      c->type_ = Fill::MATERIAL;
24,714✔
120
      for (auto& mat_id : c->material_) {
51,053✔
121
        if (mat_id != MATERIAL_VOID) {
26,339✔
122
          auto search = model::material_map.find(mat_id);
18,117✔
123
          if (search == model::material_map.end()) {
18,117✔
124
            fatal_error(
×
125
              fmt::format("Could not find material {} specified on cell {}",
×
126
                mat_id, c->id_));
×
127
          }
128
          // Change from ID to index
129
          mat_id = search->second;
18,117✔
130
        }
131
      }
132
    }
133
  }
134

135
  // Change cell.universe values from IDs to indices.
136
  for (auto& c : model::cells) {
38,260✔
137
    auto search = model::universe_map.find(c->universe_);
31,511✔
138
    if (search != model::universe_map.end()) {
31,511✔
139
      c->universe_ = search->second;
31,511✔
140
    } else {
141
      fatal_error(fmt::format("Could not find universe {} specified on cell {}",
×
142
        c->universe_, c->id_));
×
143
    }
144
  }
145

146
  // Change all lattice universe values from IDs to indices.
147
  for (auto& l : model::lattices) {
8,512✔
148
    l->adjust_indices();
1,763✔
149
  }
150
}
6,749✔
151

152
//==============================================================================
153
//! Partition some universes with many z-planes for faster find_cell searches.
154

155
void partition_universes()
6,749✔
156
{
157
  // Iterate over universes with more than 10 cells.  (Fewer than 10 is likely
158
  // not worth partitioning.)
159
  for (const auto& univ : model::universes) {
24,745✔
160
    if (univ->cells_.size() > 10) {
17,996✔
161
      // Collect the set of surfaces in this universe.
162
      std::unordered_set<int32_t> surf_inds;
183✔
163
      for (auto i_cell : univ->cells_) {
3,076✔
164
        for (auto token : model::cells[i_cell]->surfaces()) {
10,964✔
165
          surf_inds.insert(std::abs(token) - 1);
8,071✔
166
        }
2,893✔
167
      }
168

169
      // Partition the universe if there are more than 5 z-planes.  (Fewer than
170
      // 5 is likely not worth it.)
171
      int n_zplanes = 0;
183✔
172
      for (auto i_surf : surf_inds) {
2,351✔
173
        if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
2,264✔
174
          ++n_zplanes;
640✔
175
          if (n_zplanes > 5) {
640✔
176
            univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
96✔
177
            break;
96✔
178
          }
179
        }
180
      }
181
    }
183✔
182
  }
183
}
6,749✔
184

185
//==============================================================================
186

187
void assign_temperatures()
6,749✔
188
{
189
  for (auto& c : model::cells) {
38,260✔
190
    // Ignore non-material cells and cells with defined temperature.
191
    if (c->material_.size() == 0)
31,511✔
192
      continue;
6,797✔
193
    if (c->sqrtkT_.size() > 0)
24,714✔
194
      continue;
437✔
195

196
    c->sqrtkT_.reserve(c->material_.size());
24,277✔
197
    for (auto i_mat : c->material_) {
50,170✔
198
      if (i_mat == MATERIAL_VOID) {
25,893✔
199
        // Set void region to 0K.
200
        c->sqrtkT_.push_back(0);
8,222✔
201
      } else {
202
        const auto& mat {model::materials[i_mat]};
17,671✔
203
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
17,671✔
204
      }
205
    }
206
  }
207
}
6,749✔
208

209
//==============================================================================
210

211
void get_temperatures(
6,473✔
212
  vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
213
{
214
  for (const auto& cell : model::cells) {
37,411✔
215
    // Skip non-material cells.
216
    if (cell->fill_ != C_NONE)
30,938✔
217
      continue;
6,784✔
218

219
    for (int j = 0; j < cell->material_.size(); ++j) {
49,933✔
220
      // Skip void materials
221
      int i_material = cell->material_[j];
25,779✔
222
      if (i_material == MATERIAL_VOID)
25,779✔
223
        continue;
8,106✔
224

225
      // Get temperature(s) of cell (rounding to nearest integer)
226
      vector<double> cell_temps;
17,673✔
227
      if (cell->sqrtkT_.size() == 1) {
17,673✔
228
        double sqrtkT = cell->sqrtkT_[0];
15,825✔
229
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
15,825✔
230
      } else if (cell->sqrtkT_.size() == cell->material_.size()) {
1,848✔
231
        double sqrtkT = cell->sqrtkT_[j];
1,832✔
232
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,832✔
233
      } else {
234
        for (double sqrtkT : cell->sqrtkT_)
80✔
235
          cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
64✔
236
      }
237

238
      const auto& mat {model::materials[i_material]};
17,673✔
239
      for (const auto& i_nuc : mat->nuclide_) {
84,851✔
240
        for (double temperature : cell_temps) {
134,404✔
241
          // Add temperature if it hasn't already been added
242
          if (!contains(nuc_temps[i_nuc], temperature))
67,226✔
243
            nuc_temps[i_nuc].push_back(temperature);
26,995✔
244
        }
245
      }
246

247
      for (const auto& table : mat->thermal_tables_) {
20,799✔
248
        // Get index in data::thermal_scatt array
249
        int i_sab = table.index_table;
3,126✔
250

251
        for (double temperature : cell_temps) {
6,252✔
252
          // Add temperature if it hasn't already been added
253
          if (!contains(thermal_temps[i_sab], temperature))
3,126✔
254
            thermal_temps[i_sab].push_back(temperature);
1,082✔
255
        }
256
      }
257
    }
17,673✔
258
  }
259
}
6,473✔
260

261
//==============================================================================
262

263
void finalize_geometry()
6,749✔
264
{
265
  // Perform some final operations to set up the geometry
266
  adjust_indices();
6,749✔
267
  count_cell_instances(model::root_universe);
6,749✔
268
  partition_universes();
6,749✔
269

270
  // Assign temperatures to cells that don't have temperatures already assigned
271
  assign_temperatures();
6,749✔
272

273
  // Determine number of nested coordinate levels in the geometry
274
  model::n_coord_levels = maximum_levels(model::root_universe);
6,749✔
275
}
6,749✔
276

277
//==============================================================================
278

279
int32_t find_root_universe()
6,749✔
280
{
281
  // Find all the universes listed as a cell fill.
282
  std::unordered_set<int32_t> fill_univ_ids;
6,749✔
283
  for (const auto& c : model::cells) {
38,260✔
284
    fill_univ_ids.insert(c->fill_);
31,511✔
285
  }
286

287
  // Find all the universes contained in a lattice.
288
  for (const auto& lat : model::lattices) {
8,512✔
289
    for (auto it = lat->begin(); it != lat->end(); ++it) {
884,171✔
290
      fill_univ_ids.insert(*it);
882,408✔
291
    }
292
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
1,763✔
293
      fill_univ_ids.insert(lat->outer_);
386✔
294
    }
295
  }
296

297
  // Figure out which universe is not in the set.  This is the root universe.
298
  bool root_found {false};
6,749✔
299
  int32_t root_univ;
300
  for (int32_t i = 0; i < model::universes.size(); i++) {
24,745✔
301
    auto search = fill_univ_ids.find(model::universes[i]->id_);
17,996✔
302
    if (search == fill_univ_ids.end()) {
17,996✔
303
      if (root_found) {
6,749✔
304
        fatal_error("Two or more universes are not used as fill universes, so "
×
305
                    "it is not possible to distinguish which one is the root "
306
                    "universe.");
307
      } else {
308
        root_found = true;
6,749✔
309
        root_univ = i;
6,749✔
310
      }
311
    }
312
  }
313
  if (!root_found)
6,749✔
314
    fatal_error("Could not find a root universe.  Make sure "
×
315
                "there are no circular dependencies in the geometry.");
316

317
  return root_univ;
6,749✔
318
}
6,749✔
319

320
//==============================================================================
321

322
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
6,745✔
323
{
324
  write_message("Preparing distributed cell instances...", 5);
6,745✔
325

326
  std::unordered_set<int32_t> distribcells;
6,745✔
327

328
  // start with any cells manually specified via the C++ API
329
  if (user_distribcells) {
6,745✔
330
    distribcells.insert(user_distribcells->begin(), user_distribcells->end());
16✔
331
  }
332

333
  // Find all cells listed in a DistribcellFilter or CellInstanceFilter
334
  for (auto& filt : model::tally_filters) {
14,487✔
335
    auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
7,742✔
336
    auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
7,742✔
337
    if (distrib_filt) {
7,742✔
338
      distribcells.insert(distrib_filt->cell());
177✔
339
    }
340
    if (cell_inst_filt) {
7,742✔
341
      const auto& filter_cells = cell_inst_filt->cells();
34✔
342
      distribcells.insert(filter_cells.begin(), filter_cells.end());
34✔
343
    }
344
  }
345

346
  // By default, add material cells to the list of distributed cells
347
  if (settings::material_cell_offsets) {
6,745✔
348
    for (int64_t i = 0; i < model::cells.size(); ++i) {
38,308✔
349
      if (model::cells[i]->type_ == Fill::MATERIAL)
31,563✔
350
        distribcells.insert(i);
24,734✔
351
    }
352
  }
353

354
  // Make sure that the number of materials/temperatures matches the number of
355
  // cell instances.
356
  for (int i = 0; i < model::cells.size(); i++) {
38,308✔
357
    Cell& c {*model::cells[i]};
31,563✔
358

359
    if (c.material_.size() > 1) {
31,563✔
360
      if (c.material_.size() != c.n_instances_) {
235✔
361
        fatal_error(fmt::format(
×
362
          "Cell {} was specified with {} materials but has {} distributed "
363
          "instances. The number of materials must equal one or the number "
364
          "of instances.",
365
          c.id_, c.material_.size(), c.n_instances_));
×
366
      }
367
    }
368

369
    if (c.sqrtkT_.size() > 1) {
31,563✔
370
      if (c.sqrtkT_.size() != c.n_instances_) {
248✔
371
        fatal_error(fmt::format(
×
372
          "Cell {} was specified with {} temperatures but has {} distributed "
373
          "instances. The number of temperatures must equal one or the number "
374
          "of instances.",
375
          c.id_, c.sqrtkT_.size(), c.n_instances_));
×
376
      }
377
    }
378
  }
379

380
  // Search through universes for material cells and assign each one a
381
  // distribcell array index according to the containing universe.
382
  vector<int32_t> target_univ_ids;
6,745✔
383
  for (const auto& u : model::universes) {
24,769✔
384
    for (auto idx : u->cells_) {
49,587✔
385
      if (distribcells.find(idx) != distribcells.end()) {
31,563✔
386
        if (std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) ==
24,814✔
387
            target_univ_ids.end()) {
49,628✔
388
          target_univ_ids.push_back(u->id_);
15,461✔
389
        }
390
        model::cells[idx]->distribcell_index_ =
24,814✔
391
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
24,814✔
392
          target_univ_ids.begin();
49,628✔
393
      }
394
    }
395
  }
396

397
  // Allocate the cell and lattice offset tables.
398
  int n_maps = target_univ_ids.size();
6,745✔
399
  for (auto& c : model::cells) {
38,308✔
400
    if (c->type_ != Fill::MATERIAL) {
31,563✔
401
      c->offset_.resize(n_maps, C_NONE);
6,829✔
402
    }
403
  }
404
  for (auto& lat : model::lattices) {
8,524✔
405
    lat->allocate_offset_table(n_maps);
1,779✔
406
  }
407

408
// Fill the cell and lattice offset tables.
409
#pragma omp parallel for
3,766✔
410
  for (int map = 0; map < target_univ_ids.size(); map++) {
7,157✔
411
    auto target_univ_id = target_univ_ids[map];
4,178✔
412
    std::unordered_map<int32_t, int32_t> univ_count_memo;
4,178✔
413
    for (const auto& univ : model::universes) {
21,290✔
414
      int32_t offset = 0;
17,112✔
415
      for (int32_t cell_indx : univ->cells_) {
79,758✔
416
        Cell& c = *model::cells[cell_indx];
62,646✔
417

418
        if (c.type_ == Fill::UNIVERSE) {
62,646✔
419
          c.offset_[map] = offset;
35,866✔
420
          int32_t search_univ = c.fill_;
35,866✔
421
          offset += count_universe_instances(
35,866✔
422
            search_univ, target_univ_id, univ_count_memo);
423

424
        } else if (c.type_ == Fill::LATTICE) {
26,780✔
425
          c.offset_[map] = offset;
2,845✔
426
          Lattice& lat = *model::lattices[c.fill_];
2,845✔
427
          offset += lat.fill_offset_table(target_univ_id, map, univ_count_memo);
2,845✔
428
        }
429
      }
430
    }
431
  }
4,178✔
432

433
  // check distinct distribcell paths from contiguous cell instances
434
  for (const auto& u : model::universes) {
24,769✔
435
    for (auto idx : u->cells_) {
49,587✔
436
      if (distribcells.find(idx) != distribcells.end()) {
31,563✔
437
        int32_t map =
438
          std::find(target_univ_ids.begin(), target_univ_ids.end(), u->id_) -
24,814✔
439
          target_univ_ids.begin();
24,814✔
440
        Cell& c = *model::cells[idx];
24,814✔
441
        std::set<std::string> paths;
24,814✔
442
        for (int32_t i = 0; i < c.n_instances_; i++) {
34,963,562✔
443
          auto path = distribcell_path(idx, map, i);
34,938,748✔
444
          if (paths.find(path) != paths.end()) {
34,938,748✔
NEW
445
            fatal_error(fmt::format(
×
446
              "Two or more cell instances have the same path {}", path));
447
          } else {
448
            paths.insert(path);
34,938,748✔
449
          }
450
        }
34,938,748✔
451
      }
24,814✔
452
    }
453
  }
454
}
6,745✔
455

456
//==============================================================================
457

458
void count_cell_instances(int32_t univ_indx)
894,323✔
459
{
460
  const auto univ_counts = model::universe_cell_counts.find(univ_indx);
894,323✔
461
  if (univ_counts != model::universe_cell_counts.end()) {
894,323✔
462
    for (const auto& it : univ_counts->second) {
2,457,527✔
463
      model::cells[it.first]->n_instances_ += it.second;
1,581,031✔
464
    }
465
  } else {
466
    for (int32_t cell_indx : model::universes[univ_indx]->cells_) {
49,161✔
467
      Cell& c = *model::cells[cell_indx];
31,334✔
468
      ++c.n_instances_;
31,334✔
469
      model::universe_cell_counts[univ_indx][cell_indx] += 1;
31,334✔
470

471
      if (c.type_ == Fill::UNIVERSE) {
31,334✔
472
        // This cell contains another universe.  Recurse into that universe.
473
        count_cell_instances(c.fill_);
5,001✔
474
        update_universe_cell_count(univ_indx, c.fill_);
5,001✔
475
      } else if (c.type_ == Fill::LATTICE) {
26,333✔
476
        // This cell contains a lattice.  Recurse into the lattice universes.
477
        Lattice& lat = *model::lattices[c.fill_];
1,796✔
478
        for (auto it = lat.begin(); it != lat.end(); ++it) {
884,369✔
479
          count_cell_instances(*it);
882,573✔
480
          update_universe_cell_count(univ_indx, *it);
882,573✔
481
        }
482
      }
483
    }
484
  }
485
}
894,323✔
486

487
//==============================================================================
488

489
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
11,767,270✔
490
  std::unordered_map<int32_t, int32_t>& univ_count_memo)
491
{
492
  // If this is the target, it can't contain itself.
493
  if (model::universes[search_univ]->id_ == target_univ_id) {
11,767,270✔
494
    return 1;
1,600,052✔
495
  }
496

497
  // If we have already counted the number of instances, reuse that value.
498
  auto search = univ_count_memo.find(search_univ);
10,167,218✔
499
  if (search != univ_count_memo.end()) {
10,167,218✔
500
    return search->second;
4,148,290✔
501
  }
502

503
  int count {0};
6,018,928✔
504
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
12,123,300✔
505
    Cell& c = *model::cells[cell_indx];
6,104,372✔
506

507
    if (c.type_ == Fill::UNIVERSE) {
6,104,372✔
508
      int32_t next_univ = c.fill_;
77,288✔
509
      count +=
77,288✔
510
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
77,288✔
511

512
    } else if (c.type_ == Fill::LATTICE) {
6,027,084✔
513
      Lattice& lat = *model::lattices[c.fill_];
3,458✔
514
      for (auto it = lat.begin(); it != lat.end(); ++it) {
2,452,834✔
515
        int32_t next_univ = *it;
2,449,376✔
516
        count +=
2,449,376✔
517
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
2,449,376✔
518
      }
519
    }
520
  }
521

522
  // Remember the number of instances in this universe.
523
  univ_count_memo[search_univ] = count;
6,018,928✔
524

525
  return count;
6,018,928✔
526
}
527

528
//==============================================================================
529

530
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
107,175,405✔
531
  int32_t target_offset, const Universe& search_univ, int32_t offset)
532
{
533
  std::stringstream path;
107,175,405✔
534

535
  path << "u" << search_univ.id_ << "->";
107,175,405✔
536

537
  // Check to see if this universe directly contains the target cell.  If so,
538
  // write to the path and return.
539
  for (int32_t cell_indx : search_univ.cells_) {
591,074,859✔
540
    if ((cell_indx == target_cell) && (offset == target_offset)) {
519,765,205✔
541
      Cell& c = *model::cells[cell_indx];
35,865,751✔
542
      path << "c" << c.id_;
35,865,751✔
543
      return path.str();
71,731,502✔
544
    }
545
  }
546

547
  // The target must be further down the geometry tree and contained in a fill
548
  // cell or lattice cell in this universe.  Find which cell contains the
549
  // target.
550
  vector<std::int32_t>::const_reverse_iterator cell_it {
551
    search_univ.cells_.crbegin()};
71,309,654✔
552
  for (; cell_it != search_univ.cells_.crend(); ++cell_it) {
431,240,650✔
553
    Cell& c = *model::cells[*cell_it];
431,240,650✔
554

555
    // Material cells don't contain other cells so ignore them.
556
    if (c.type_ != Fill::MATERIAL) {
431,240,650✔
557
      int32_t temp_offset = offset + c.offset_[map];
88,838,981✔
558
      if (c.type_ == Fill::LATTICE) {
88,838,981✔
559
        Lattice& lat = *model::lattices[c.fill_];
87,852,127✔
560
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
87,852,127✔
561
        temp_offset += lat.offsets_[indx];
87,852,127✔
562
      }
563

564
      // The desired cell is the first cell that gives an offset smaller or
565
      // equal to the target offset.
566
      if (temp_offset <= target_offset)
88,838,981✔
567
        break;
71,309,654✔
568
    }
569
  }
570

571
  // if we get through the loop without finding an appropriate entry, throw
572
  // an error
573
  if (cell_it == search_univ.cells_.crend()) {
71,309,654✔
574
    fatal_error(
×
575
      fmt::format("Failed to generate a text label for distribcell with ID {}."
×
576
                  "The current label is: '{}'",
577
        model::cells[target_cell]->id_, path.str()));
×
578
  }
579

580
  // Add the cell to the path string.
581
  Cell& c = *model::cells[*cell_it];
71,309,654✔
582
  path << "c" << c.id_ << "->";
71,309,654✔
583

584
  if (c.type_ == Fill::UNIVERSE) {
71,309,654✔
585
    // Recurse into the fill cell.
586
    offset += c.offset_[map];
894,310✔
587
    path << distribcell_path_inner(
1,788,620✔
588
      target_cell, map, target_offset, *model::universes[c.fill_], offset);
1,788,620✔
589
    return path.str();
894,310✔
590
  } else {
591
    // Recurse into the lattice cell.
592
    Lattice& lat = *model::lattices[c.fill_];
70,415,344✔
593
    path << "l" << lat.id_;
70,415,344✔
594
    for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) {
2,147,483,647✔
595
      int32_t indx = lat.universes_.size() * map + it.indx_;
2,147,483,647✔
596
      int32_t temp_offset = offset + lat.offsets_[indx] + c.offset_[map];
2,147,483,647✔
597
      if (temp_offset <= target_offset) {
2,147,483,647✔
598
        offset = temp_offset;
70,415,344✔
599
        path << "(" << lat.index_to_string(it.indx_) << ")->";
70,415,344✔
600
        path << distribcell_path_inner(
140,830,688✔
601
          target_cell, map, target_offset, *model::universes[*it], offset);
140,830,688✔
602
        return path.str();
140,830,688✔
603
      }
604
    }
605
    throw std::runtime_error {"Error determining distribcell path."};
×
606
  }
607
}
107,175,405✔
608

609
std::string distribcell_path(
35,865,751✔
610
  int32_t target_cell, int32_t map, int32_t target_offset)
611
{
612
  auto& root_univ = *model::universes[model::root_universe];
35,865,751✔
613
  return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0);
35,865,751✔
614
}
615

616
//==============================================================================
617

618
int maximum_levels(int32_t univ)
894,323✔
619
{
620

621
  const auto level_count = model::universe_level_counts.find(univ);
894,323✔
622
  if (level_count != model::universe_level_counts.end()) {
894,323✔
623
    return level_count->second;
876,496✔
624
  }
625

626
  int levels_below {0};
17,827✔
627

628
  for (int32_t cell_indx : model::universes[univ]->cells_) {
49,161✔
629
    Cell& c = *model::cells[cell_indx];
31,334✔
630
    if (c.type_ == Fill::UNIVERSE) {
31,334✔
631
      int32_t next_univ = c.fill_;
5,001✔
632
      levels_below = std::max(levels_below, maximum_levels(next_univ));
5,001✔
633
    } else if (c.type_ == Fill::LATTICE) {
26,333✔
634
      Lattice& lat = *model::lattices[c.fill_];
1,796✔
635
      for (auto it = lat.begin(); it != lat.end(); ++it) {
884,369✔
636
        int32_t next_univ = *it;
882,573✔
637
        levels_below = std::max(levels_below, maximum_levels(next_univ));
882,573✔
638
      }
639
    }
640
  }
641

642
  ++levels_below;
17,827✔
643
  model::universe_level_counts[univ] = levels_below;
17,827✔
644
  return levels_below;
17,827✔
645
}
646

647
bool is_root_universe(int32_t univ_id)
×
648
{
649
  return model::universe_map[univ_id] == model::root_universe;
×
650
}
651

652
//==============================================================================
653

654
void free_memory_geometry()
6,854✔
655
{
656
  model::cells.clear();
6,854✔
657
  model::cell_map.clear();
6,854✔
658

659
  model::universes.clear();
6,854✔
660
  model::universe_map.clear();
6,854✔
661

662
  model::lattices.clear();
6,854✔
663
  model::lattice_map.clear();
6,854✔
664

665
  model::overlap_check_count.clear();
6,854✔
666
}
6,854✔
667

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