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

openmc-dev / openmc / 13894606366

17 Mar 2025 08:15AM UTC coverage: 84.924% (-0.1%) from 85.042%
13894606366

Pull #3305

github

web-flow
Merge 042852a3c into 58f2a2177
Pull Request #3305: New Feature Development: The Implementation of chord length sampling (CLS) method for stochastic media( #3286 )

20 of 161 new or added lines in 8 files covered. (12.42%)

1043 existing lines in 45 files now uncovered.

51567 of 60721 relevant lines covered (84.92%)

36823581.23 hits per line

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

89.15
/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/stochastic_media.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)
856,439✔
36
{
37
  auto& universe_a_counts = model::universe_cell_counts[a];
856,439✔
38
  const auto& universe_b_counts = model::universe_cell_counts[b];
856,439✔
39
  for (const auto& it : universe_b_counts) {
2,432,617✔
40
    universe_a_counts[it.first] += it.second;
1,576,178✔
41
  }
42
}
856,439✔
43

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

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

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

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

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

68
void read_geometry_xml(pugi::xml_node root)
6,354✔
69
{
70
  // Read surfaces, cells, lattice, and stochastic media
71
  read_surfaces(root);
6,354✔
72
  read_cells(root);
6,354✔
73
  read_lattices(root);
6,352✔
74
  read_stochastic_media(root);
6,352✔
75

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

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

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

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

98
//==============================================================================
99

100
void adjust_indices()
6,354✔
101
{
102
  // Adjust material/fill idices.
103
  for (auto& c : model::cells) {
36,850✔
104
    if (c->fill_ != C_NONE) {
30,496✔
105
      int32_t id = c->fill_;
6,591✔
106
      auto search_univ = model::universe_map.find(id);
6,591✔
107
      auto search_lat = model::lattice_map.find(id);
6,591✔
108
      auto search_media = model::stochastic_media_map.find(id);
6,591✔
109
      if (search_univ != model::universe_map.end()) {
6,591✔
110
        c->type_ = Fill::UNIVERSE;
4,951✔
111
        c->fill_ = search_univ->second;
4,951✔
112
      } else if (search_lat != model::lattice_map.end()) {
1,640✔
113
        c->type_ = Fill::LATTICE;
1,640✔
114
        c->fill_ = search_lat->second;
1,640✔
NEW
115
      } else if (search_media != model::stochastic_media_map.end()) {
×
NEW
116
        c->type_ = Fill::STOCHASTIC_MEDIA;
×
NEW
117
        c->fill_ = search_media->second;
×
118
      } else {
NEW
119
        fatal_error(fmt::format("Specified fill {} on cell {} must be one of  "
×
120
                                "universe  lattice and stochastic media.",
UNCOV
121
          id, c->id_));
×
122
      }
123
    } else {
124
      c->type_ = Fill::MATERIAL;
23,905✔
125
      for (auto& mat_id : c->material_) {
49,275✔
126
        if (mat_id != MATERIAL_VOID) {
25,370✔
127
          auto search = model::material_map.find(mat_id);
17,182✔
128
          if (search == model::material_map.end()) {
17,182✔
129
            fatal_error(
×
130
              fmt::format("Could not find material {} specified on cell {}",
×
131
                mat_id, c->id_));
×
132
          }
133
          // Change from ID to index
134
          mat_id = search->second;
17,182✔
135
        }
136
      }
137
    }
138
  }
139

140
  // Change cell.universe values from IDs to indices.
141
  for (auto& c : model::cells) {
36,850✔
142
    auto search = model::universe_map.find(c->universe_);
30,496✔
143
    if (search != model::universe_map.end()) {
30,496✔
144
      c->universe_ = search->second;
30,496✔
145
    } else {
146
      fatal_error(fmt::format("Could not find universe {} specified on cell {}",
×
147
        c->universe_, c->id_));
×
148
    }
149
  }
150

151
  // Change all lattice universe values from IDs to indices.
152
  for (auto& l : model::lattices) {
7,961✔
153
    l->adjust_indices();
1,607✔
154
  }
155

156
  for (auto& media : model::stochastic_media) {
6,354✔
NEW
157
    media->adjust_indices();
×
158
  }
159
}
6,354✔
160

161
//==============================================================================
162
//! Partition some universes with many z-planes for faster find_cell searches.
163

164
void partition_universes()
6,354✔
165
{
166
  // Iterate over universes with more than 10 cells.  (Fewer than 10 is likely
167
  // not worth partitioning.)
168
  for (const auto& univ : model::universes) {
23,790✔
169
    if (univ->cells_.size() > 10) {
17,436✔
170
      // Collect the set of surfaces in this universe.
171
      std::unordered_set<int32_t> surf_inds;
182✔
172
      for (auto i_cell : univ->cells_) {
3,064✔
173
        for (auto token : model::cells[i_cell]->surfaces()) {
10,920✔
174
          surf_inds.insert(std::abs(token) - 1);
8,038✔
175
        }
2,882✔
176
      }
177

178
      // Partition the universe if there are more than 5 z-planes.  (Fewer than
179
      // 5 is likely not worth it.)
180
      int n_zplanes = 0;
182✔
181
      for (auto i_surf : surf_inds) {
2,342✔
182
        if (dynamic_cast<const SurfaceZPlane*>(model::surfaces[i_surf].get())) {
2,256✔
183
          ++n_zplanes;
640✔
184
          if (n_zplanes > 5) {
640✔
185
            univ->partitioner_ = make_unique<UniversePartitioner>(*univ);
96✔
186
            break;
96✔
187
          }
188
        }
189
      }
190
    }
182✔
191
  }
192
}
6,354✔
193

194
//==============================================================================
195

196
void assign_temperatures()
6,354✔
197
{
198
  for (auto& c : model::cells) {
36,850✔
199
    // Ignore non-material cells and cells with defined temperature.
200
    if (c->material_.size() == 0) {
30,496✔
201
      if (c->type_ != Fill::STOCHASTIC_MEDIA)
6,591✔
202
        continue;
6,591✔
NEW
203
      c->sqrtkT_.reserve(1);
×
204

NEW
205
      auto fill_idx = model::stochastic_media_map[c->fill_];
×
NEW
206
      auto mat_id {model::stochastic_media[fill_idx]->particle_mat()[0]};
×
NEW
207
      const auto& mat {model::materials[mat_id]};
×
NEW
208
      c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
×
209
    }
210
    if (c->sqrtkT_.size() > 0)
23,905✔
211
      continue;
428✔
212

213
    c->sqrtkT_.reserve(c->material_.size());
23,477✔
214
    for (auto i_mat : c->material_) {
48,410✔
215
      if (i_mat == MATERIAL_VOID) {
24,933✔
216
        // Set void region to 0K.
217
        c->sqrtkT_.push_back(0);
8,188✔
218
      } else {
219
        const auto& mat {model::materials[i_mat]};
16,745✔
220
        c->sqrtkT_.push_back(std::sqrt(K_BOLTZMANN * mat->temperature()));
16,745✔
221
      }
222
    }
223
  }
224
}
6,354✔
225

226
//==============================================================================
227

228
void get_temperatures(
6,081✔
229
  vector<vector<double>>& nuc_temps, vector<vector<double>>& thermal_temps)
230
{
231
  for (const auto& cell : model::cells) {
36,022✔
232
    // Skip non-material cells.
233
    if (cell->fill_ != C_NONE)
29,941✔
234
      continue;
6,580✔
235

236
    for (int j = 0; j < cell->material_.size(); ++j) {
48,187✔
237
      // Skip void materials
238
      int i_material = cell->material_[j];
24,826✔
239
      if (i_material == MATERIAL_VOID)
24,826✔
240
        continue;
8,078✔
241

242
      // Get temperature(s) of cell (rounding to nearest integer)
243
      vector<double> cell_temps;
16,748✔
244
      if (cell->sqrtkT_.size() == 1) {
16,748✔
245
        double sqrtkT = cell->sqrtkT_[0];
15,100✔
246
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
15,100✔
247
      } else if (cell->sqrtkT_.size() == cell->material_.size()) {
1,648✔
248
        double sqrtkT = cell->sqrtkT_[j];
1,632✔
249
        cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
1,632✔
250
      } else {
251
        for (double sqrtkT : cell->sqrtkT_)
80✔
252
          cell_temps.push_back(sqrtkT * sqrtkT / K_BOLTZMANN);
64✔
253
      }
254

255
      const auto& mat {model::materials[i_material]};
16,748✔
256
      for (const auto& i_nuc : mat->nuclide_) {
81,460✔
257
        for (double temperature : cell_temps) {
129,472✔
258
          // Add temperature if it hasn't already been added
259
          if (!contains(nuc_temps[i_nuc], temperature))
64,760✔
260
            nuc_temps[i_nuc].push_back(temperature);
25,558✔
261
        }
262
      }
263

264
      for (const auto& table : mat->thermal_tables_) {
19,748✔
265
        // Get index in data::thermal_scatt array
266
        int i_sab = table.index_table;
3,000✔
267

268
        for (double temperature : cell_temps) {
6,000✔
269
          // Add temperature if it hasn't already been added
270
          if (!contains(thermal_temps[i_sab], temperature))
3,000✔
271
            thermal_temps[i_sab].push_back(temperature);
1,005✔
272
        }
273
      }
274
    }
16,748✔
275
  }
276
}
6,081✔
277

278
//==============================================================================
279

280
void finalize_geometry()
6,354✔
281
{
282
  // Perform some final operations to set up the geometry
283
  adjust_indices();
6,354✔
284
  count_cell_instances(model::root_universe);
6,354✔
285
  partition_universes();
6,354✔
286

287
  // Assign temperatures to cells that don't have temperatures already assigned
288
  assign_temperatures();
6,354✔
289

290
  // Determine number of nested coordinate levels in the geometry
291
  model::n_coord_levels = maximum_levels(model::root_universe);
6,354✔
292
}
6,354✔
293

294
//==============================================================================
295

296
int32_t find_root_universe()
6,354✔
297
{
298
  // Find all the universes listed as a cell fill.
299
  std::unordered_set<int32_t> fill_univ_ids;
6,354✔
300
  for (const auto& c : model::cells) {
36,850✔
301
    fill_univ_ids.insert(c->fill_);
30,496✔
302
  }
303

304
  // Find all the universes contained in a lattice.
305
  for (const auto& lat : model::lattices) {
7,961✔
306
    for (auto it = lat->begin(); it != lat->end(); ++it) {
852,930✔
307
      fill_univ_ids.insert(*it);
851,323✔
308
    }
309
    if (lat->outer_ != NO_OUTER_UNIVERSE) {
1,607✔
310
      fill_univ_ids.insert(lat->outer_);
246✔
311
    }
312
  }
313

314
  // Figure out which universe is not in the set.  This is the root universe.
315
  bool root_found {false};
6,354✔
316
  int32_t root_univ;
317
  for (int32_t i = 0; i < model::universes.size(); i++) {
23,790✔
318
    auto search = fill_univ_ids.find(model::universes[i]->id_);
17,436✔
319
    if (search == fill_univ_ids.end()) {
17,436✔
320
      if (root_found) {
6,354✔
321
        fatal_error("Two or more universes are not used as fill universes, so "
×
322
                    "it is not possible to distinguish which one is the root "
323
                    "universe.");
324
      } else {
325
        root_found = true;
6,354✔
326
        root_univ = i;
6,354✔
327
      }
328
    }
329
  }
330
  if (!root_found)
6,354✔
331
    fatal_error("Could not find a root universe.  Make sure "
×
332
                "there are no circular dependencies in the geometry.");
333

334
  return root_univ;
6,354✔
335
}
6,354✔
336

337
//==============================================================================
338

339
void prepare_distribcell(const std::vector<int32_t>* user_distribcells)
6,368✔
340
{
341
  write_message("Preparing distributed cell instances...", 5);
6,368✔
342

343
  std::unordered_set<int32_t> distribcells;
6,368✔
344

345
  // start with any cells manually specified via the C++ API
346
  if (user_distribcells) {
6,368✔
347
    distribcells.insert(user_distribcells->begin(), user_distribcells->end());
16✔
348
  }
349

350
  // Find all cells listed in a DistribcellFilter or CellInstanceFilter
351
  for (auto& filt : model::tally_filters) {
13,644✔
352
    auto* distrib_filt = dynamic_cast<DistribcellFilter*>(filt.get());
7,276✔
353
    auto* cell_inst_filt = dynamic_cast<CellInstanceFilter*>(filt.get());
7,276✔
354
    if (distrib_filt) {
7,276✔
355
      distribcells.insert(distrib_filt->cell());
177✔
356
    }
357
    if (cell_inst_filt) {
7,276✔
358
      const auto& filter_cells = cell_inst_filt->cells();
34✔
359
      distribcells.insert(filter_cells.begin(), filter_cells.end());
34✔
360
    }
361
  }
362

363
  // By default, add material cells to the list of distributed cells
364
  if (settings::material_cell_offsets) {
6,368✔
365
    for (int64_t i = 0; i < model::cells.size(); ++i) {
36,934✔
366
      if (model::cells[i]->type_ == Fill::MATERIAL)
30,566✔
367
        distribcells.insert(i);
23,943✔
368
    }
369
  }
370

371
  // Make sure that the number of materials/temperatures matches the number of
372
  // cell instances.
373
  for (int i = 0; i < model::cells.size(); i++) {
36,934✔
374
    Cell& c {*model::cells[i]};
30,566✔
375

376
    if (c.material_.size() > 1) {
30,566✔
377
      if (c.material_.size() != c.n_instances_) {
195✔
378
        fatal_error(fmt::format(
×
379
          "Cell {} was specified with {} materials but has {} distributed "
380
          "instances. The number of materials must equal one or the number "
381
          "of instances.",
382
          c.id_, c.material_.size(), c.n_instances_));
×
383
      }
384
    }
385

386
    if (c.sqrtkT_.size() > 1) {
30,566✔
387
      if (c.sqrtkT_.size() != c.n_instances_) {
208✔
388
        fatal_error(fmt::format(
×
389
          "Cell {} was specified with {} temperatures but has {} distributed "
390
          "instances. The number of temperatures must equal one or the number "
391
          "of instances.",
392
          c.id_, c.sqrtkT_.size(), c.n_instances_));
×
393
      }
394
    }
395
  }
396

397
  // Search through universes for material cells and assign each one a
398
  // unique distribcell array index.
399
  int distribcell_index = 0;
6,368✔
400
  vector<int32_t> target_univ_ids;
6,368✔
401
  for (const auto& u : model::universes) {
23,850✔
402
    for (auto idx : u->cells_) {
48,048✔
403
      if (distribcells.find(idx) != distribcells.end()) {
30,566✔
404
        model::cells[idx]->distribcell_index_ = distribcell_index++;
24,023✔
405
        target_univ_ids.push_back(u->id_);
24,023✔
406
      }
407
    }
408
  }
409

410
  // Allocate the cell and lattice offset tables.
411
  int n_maps = target_univ_ids.size();
6,368✔
412
  for (auto& c : model::cells) {
36,934✔
413
    if (c->type_ != Fill::MATERIAL) {
30,566✔
414
      c->offset_.resize(n_maps, C_NONE);
6,623✔
415
    }
416
  }
417
  for (auto& lat : model::lattices) {
7,991✔
418
    lat->allocate_offset_table(n_maps);
1,623✔
419
  }
420

421
// Fill the cell and lattice offset tables.
422
#pragma omp parallel for
3,553✔
423
  for (int map = 0; map < target_univ_ids.size(); map++) {
10,715✔
424
    auto target_univ_id = target_univ_ids[map];
7,900✔
425
    std::unordered_map<int32_t, int32_t> univ_count_memo;
7,900✔
426
    for (const auto& univ : model::universes) {
38,507✔
427
      int32_t offset = 0;
30,607✔
428
      for (int32_t cell_indx : univ->cells_) {
158,927✔
429
        Cell& c = *model::cells[cell_indx];
128,320✔
430

431
        if (c.type_ == Fill::UNIVERSE) {
128,320✔
432
          c.offset_[map] = offset;
43,597✔
433
          int32_t search_univ = c.fill_;
43,597✔
434
          offset += count_universe_instances(
43,597✔
435
            search_univ, target_univ_id, univ_count_memo);
436

437
        } else if (c.type_ == Fill::LATTICE) {
84,723✔
438
          c.offset_[map] = offset;
6,880✔
439
          Lattice& lat = *model::lattices[c.fill_];
6,880✔
440
          offset +=
6,880✔
441
            lat.fill_offset_table(offset, target_univ_id, map, univ_count_memo);
6,880✔
442
        }
443
      }
444
    }
445
  }
7,900✔
446
}
6,368✔
447

448
//==============================================================================
449

450
void count_cell_instances(int32_t univ_indx)
862,793✔
451
{
452
  const auto univ_counts = model::universe_cell_counts.find(univ_indx);
862,793✔
453
  if (univ_counts != model::universe_cell_counts.end()) {
862,793✔
454
    for (const auto& it : univ_counts->second) {
2,395,409✔
455
      model::cells[it.first]->n_instances_ += it.second;
1,549,884✔
456
    }
457
  } else {
458
    for (int32_t cell_indx : model::universes[univ_indx]->cells_) {
47,588✔
459
      Cell& c = *model::cells[cell_indx];
30,320✔
460
      ++c.n_instances_;
30,320✔
461
      model::universe_cell_counts[univ_indx][cell_indx] += 1;
30,320✔
462

463
      if (c.type_ == Fill::UNIVERSE) {
30,320✔
464
        // This cell contains another universe.  Recurse into that universe.
465
        count_cell_instances(c.fill_);
4,951✔
466
        update_universe_cell_count(univ_indx, c.fill_);
4,951✔
467
      } else if (c.type_ == Fill::LATTICE) {
25,369✔
468
        // This cell contains a lattice.  Recurse into the lattice universes.
469
        Lattice& lat = *model::lattices[c.fill_];
1,640✔
470
        for (auto it = lat.begin(); it != lat.end(); ++it) {
853,128✔
471
          count_cell_instances(*it);
851,488✔
472
          update_universe_cell_count(univ_indx, *it);
851,488✔
473
        }
474
      }
475
    }
476
  }
477
}
862,793✔
478

479
//==============================================================================
480

481
int count_universe_instances(int32_t search_univ, int32_t target_univ_id,
15,593,376✔
482
  std::unordered_map<int32_t, int32_t>& univ_count_memo)
483
{
484
  // If this is the target, it can't contain itself.
485
  if (model::universes[search_univ]->id_ == target_univ_id) {
15,593,376✔
486
    return 1;
2,183,660✔
487
  }
488

489
  // If we have already counted the number of instances, reuse that value.
490
  auto search = univ_count_memo.find(search_univ);
13,409,716✔
491
  if (search != univ_count_memo.end()) {
13,409,716✔
492
    return search->second;
7,372,468✔
493
  }
494

495
  int count {0};
6,037,248✔
496
  for (int32_t cell_indx : model::universes[search_univ]->cells_) {
12,194,870✔
497
    Cell& c = *model::cells[cell_indx];
6,157,622✔
498

499
    if (c.type_ == Fill::UNIVERSE) {
6,157,622✔
500
      int32_t next_univ = c.fill_;
94,440✔
501
      count +=
94,440✔
502
        count_universe_instances(next_univ, target_univ_id, univ_count_memo);
94,440✔
503

504
    } else if (c.type_ == Fill::LATTICE) {
6,063,182✔
505
      Lattice& lat = *model::lattices[c.fill_];
7,538✔
506
      for (auto it = lat.begin(); it != lat.end(); ++it) {
3,397,522✔
507
        int32_t next_univ = *it;
3,389,984✔
508
        count +=
3,389,984✔
509
          count_universe_instances(next_univ, target_univ_id, univ_count_memo);
3,389,984✔
510
      }
511
    }
512
  }
513

514
  // Remember the number of instances in this universe.
515
  univ_count_memo[search_univ] = count;
6,037,248✔
516

517
  return count;
6,037,248✔
518
}
519

520
//==============================================================================
521

522
std::string distribcell_path_inner(int32_t target_cell, int32_t map,
2,565,486✔
523
  int32_t target_offset, const Universe& search_univ, int32_t offset)
524
{
525
  std::stringstream path;
2,565,486✔
526

527
  path << "u" << search_univ.id_ << "->";
2,565,486✔
528

529
  // Check to see if this universe directly contains the target cell.  If so,
530
  // write to the path and return.
531
  for (int32_t cell_indx : search_univ.cells_) {
11,902,803✔
532
    if ((cell_indx == target_cell) && (offset == target_offset)) {
10,264,320✔
533
      Cell& c = *model::cells[cell_indx];
927,003✔
534
      path << "c" << c.id_;
927,003✔
535
      return path.str();
1,854,006✔
536
    }
537
  }
538

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

547
    // Material cells don't contain other cells so ignore them.
548
    if (c.type_ != Fill::MATERIAL) {
9,337,152✔
549
      int32_t temp_offset;
550
      if (c.type_ == Fill::UNIVERSE) {
2,338,512✔
551
        temp_offset =
×
552
          offset + c.offset_[map]; // TODO: should also apply to lattice fills?
×
553
      } else {
554
        Lattice& lat = *model::lattices[c.fill_];
2,338,512✔
555
        int32_t indx = lat.universes_.size() * map + lat.begin().indx_;
2,338,512✔
556
        temp_offset = offset + lat.offsets_[indx];
2,338,512✔
557
      }
558

559
      // The desired cell is the first cell that gives an offset smaller or
560
      // equal to the target offset.
561
      if (temp_offset <= target_offset - c.offset_[map])
2,338,512✔
562
        break;
1,638,483✔
563
    }
564
  }
565

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

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

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

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

611
//==============================================================================
612

613
int maximum_levels(int32_t univ)
862,793✔
614
{
615

616
  const auto level_count = model::universe_level_counts.find(univ);
862,793✔
617
  if (level_count != model::universe_level_counts.end()) {
862,793✔
618
    return level_count->second;
845,525✔
619
  }
620

621
  int levels_below {0};
17,268✔
622

623
  for (int32_t cell_indx : model::universes[univ]->cells_) {
47,588✔
624
    Cell& c = *model::cells[cell_indx];
30,320✔
625
    if (c.type_ == Fill::UNIVERSE) {
30,320✔
626
      int32_t next_univ = c.fill_;
4,951✔
627
      levels_below = std::max(levels_below, maximum_levels(next_univ));
4,951✔
628
    } else if (c.type_ == Fill::LATTICE) {
25,369✔
629
      Lattice& lat = *model::lattices[c.fill_];
1,640✔
630
      for (auto it = lat.begin(); it != lat.end(); ++it) {
853,128✔
631
        int32_t next_univ = *it;
851,488✔
632
        levels_below = std::max(levels_below, maximum_levels(next_univ));
851,488✔
633
      }
634
    }
635
  }
636

637
  ++levels_below;
17,268✔
638
  model::universe_level_counts[univ] = levels_below;
17,268✔
639
  return levels_below;
17,268✔
640
}
641

642
bool is_root_universe(int32_t univ_id)
×
643
{
644
  return model::universe_map[univ_id] == model::root_universe;
×
645
}
646

647
//==============================================================================
648

649
void free_memory_geometry()
6,478✔
650
{
651
  model::cells.clear();
6,478✔
652
  model::cell_map.clear();
6,478✔
653

654
  model::universes.clear();
6,478✔
655
  model::universe_map.clear();
6,478✔
656

657
  model::lattices.clear();
6,478✔
658
  model::lattice_map.clear();
6,478✔
659

660
  model::stochastic_media.clear();
6,478✔
661
  model::stochastic_media_map.clear();
6,478✔
662

663
  model::overlap_check_count.clear();
6,478✔
664
}
6,478✔
665

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

© 2025 Coveralls, Inc