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

openmc-dev / openmc / 30101107213

24 Jul 2026 02:27PM UTC coverage: 81.325% (+0.02%) from 81.301%
30101107213

Pull #4012

github

web-flow
Merge b648c618d into 01790598d
Pull Request #4012: Geometry debug function

18382 of 26610 branches covered (69.08%)

Branch coverage included in aggregate %.

20 of 104 new or added lines in 1 file covered. (19.23%)

979 existing lines in 27 files now uncovered.

59984 of 69752 relevant lines covered (86.0%)

49062666.97 hits per line

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

74.79
/src/cell.cpp
1

2
#include "openmc/cell.h"
3

4
#include <algorithm>
5
#include <cassert>
6
#include <cctype>
7
#include <cmath>
8
#include <iterator>
9
#include <set>
10
#include <sstream>
11
#include <string>
12

13
#include <fmt/core.h>
14

15
#include "openmc/capi.h"
16
#include "openmc/constants.h"
17
#include "openmc/dagmc.h"
18
#include "openmc/error.h"
19
#include "openmc/geometry.h"
20
#include "openmc/hdf5_interface.h"
21
#include "openmc/lattice.h"
22
#include "openmc/material.h"
23
#include "openmc/nuclide.h"
24
#include "openmc/settings.h"
25
#include "openmc/xml_interface.h"
26

27
namespace openmc {
28

29
//==============================================================================
30
// Global variables
31
//==============================================================================
32

33
namespace model {
34
std::unordered_map<int32_t, int32_t> cell_map;
35
vector<unique_ptr<Cell>> cells;
36

37
} // namespace model
38

39
//==============================================================================
40
// Cell implementation
41
//==============================================================================
42

43
int32_t Cell::n_instances() const
15,958 ✔
44
{
45
  return model::universes[universe_]->n_instances_;
15,958 ✔
46
}
47

48
void Cell::set_rotation(const vector<double>& rot)
445 ✔
49
{
50
  if (fill_ == C_NONE) {
445 !
51
    fatal_error(fmt::format("Cannot apply a rotation to cell {}"
×
52
                            " because it is not filled with another universe",
53
      id_));
×
54
  }
55

56
  if (rot.size() != 3 && rot.size() != 9) {
445 !
57
    fatal_error(fmt::format("Non-3D rotation vector applied to cell {}", id_));
×
58
  }
59

60
  // Compute and store the inverse rotation matrix for the angles given.
61
  rotation_.clear();
445 ✔
62
  rotation_.reserve(rot.size() == 9 ? 9 : 12);
890 !
63
  if (rot.size() == 3) {
445 !
64
    double phi = -rot[0] * PI / 180.0;
445 ✔
65
    double theta = -rot[1] * PI / 180.0;
445 ✔
66
    double psi = -rot[2] * PI / 180.0;
445 ✔
67
    rotation_.push_back(std::cos(theta) * std::cos(psi));
445 ✔
68
    rotation_.push_back(-std::cos(phi) * std::sin(psi) +
445 ✔
69
                        std::sin(phi) * std::sin(theta) * std::cos(psi));
445 ✔
70
    rotation_.push_back(std::sin(phi) * std::sin(psi) +
445 ✔
71
                        std::cos(phi) * std::sin(theta) * std::cos(psi));
445 ✔
72
    rotation_.push_back(std::cos(theta) * std::sin(psi));
445 ✔
73
    rotation_.push_back(std::cos(phi) * std::cos(psi) +
445 ✔
74
                        std::sin(phi) * std::sin(theta) * std::sin(psi));
445 ✔
75
    rotation_.push_back(-std::sin(phi) * std::cos(psi) +
445 ✔
76
                        std::cos(phi) * std::sin(theta) * std::sin(psi));
445 ✔
77
    rotation_.push_back(-std::sin(theta));
445 ✔
78
    rotation_.push_back(std::sin(phi) * std::cos(theta));
445 ✔
79
    rotation_.push_back(std::cos(phi) * std::cos(theta));
445 ✔
80

81
    // When user specifies angles, write them at end of vector
82
    rotation_.push_back(rot[0]);
445 ✔
83
    rotation_.push_back(rot[1]);
445 ✔
84
    rotation_.push_back(rot[2]);
445 ✔
85
  } else {
86
    std::copy(rot.begin(), rot.end(), std::back_inserter(rotation_));
×
87
  }
88
}
445 ✔
89

90
double Cell::temperature(int32_t instance) const
9,634 ✔
91
{
92
  if (sqrtkT_.size() < 1) {
9,634 !
93
    throw std::runtime_error {"Cell temperature has not yet been set."};
×
94
  }
95

96
  if (instance >= 0) {
9,634 ✔
97
    double sqrtkT = sqrtkT_.size() == 1 ? sqrtkT_.at(0) : sqrtkT_.at(instance);
9,548 ✔
98
    return sqrtkT * sqrtkT / K_BOLTZMANN;
9,548 ✔
99
  } else {
100
    return sqrtkT_[0] * sqrtkT_[0] / K_BOLTZMANN;
86 ✔
101
  }
102
}
103

104
double Cell::density_mult(int32_t instance) const
2,147,483,647 ✔
105
{
106
  if (instance >= 0) {
2,147,483,647 ✔
107
    return density_mult_.size() == 1 ? density_mult_.at(0)
2,147,483,647 ✔
108
                                     : density_mult_.at(instance);
5,034,975 ✔
109
  } else {
110
    return density_mult_[0];
77 ✔
111
  }
112
}
113

114
double Cell::density(int32_t instance) const
1,199,827 ✔
115
{
116
  const int32_t mat_index = material(instance);
1,199,827 ✔
117
  if (mat_index == MATERIAL_VOID)
1,199,827 !
118
    return 0.0;
119

120
  return density_mult(instance) * model::materials[mat_index]->density_gpcc();
2,399,654 ✔
121
}
122

123
void Cell::set_temperature(double T, int32_t instance, bool set_contained)
10,012 ✔
124
{
125
  if (settings::temperature_method == TemperatureMethod::INTERPOLATION) {
10,012 !
126
    if (T < (data::temperature_min - settings::temperature_tolerance)) {
×
127
      throw std::runtime_error {
×
128
        fmt::format("Temperature of {} K is below minimum temperature at "
×
129
                    "which data is available of {} K.",
130
          T, data::temperature_min)};
×
131
    } else if (T > (data::temperature_max + settings::temperature_tolerance)) {
×
132
      throw std::runtime_error {
×
133
        fmt::format("Temperature of {} K is above maximum temperature at "
×
134
                    "which data is available of {} K.",
135
          T, data::temperature_max)};
×
136
    }
137
  }
138

139
  if (type_ == Fill::MATERIAL) {
10,012 ✔
140
    if (instance >= 0) {
9,982 ✔
141
      // If temperature vector is not big enough, resize it first
142
      if (sqrtkT_.size() != n_instances())
9,905 ✔
143
        sqrtkT_.resize(n_instances(), sqrtkT_[0]);
45 ✔
144

145
      // Set temperature for the corresponding instance
146
      sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T);
9,905 ✔
147
    } else {
148
      // Set temperature for all instances
149
      for (auto& T_ : sqrtkT_) {
154 ✔
150
        T_ = std::sqrt(K_BOLTZMANN * T);
77 ✔
151
      }
152
    }
153
  } else {
154
    if (!set_contained) {
30 !
155
      throw std::runtime_error {
×
156
        fmt::format("Attempted to set the temperature of cell {} "
×
157
                    "which is not filled by a material.",
158
          id_)};
×
159
    }
160

161
    auto contained_cells = this->get_contained_cells(instance);
30 ✔
162
    for (const auto& entry : contained_cells) {
120 ✔
163
      auto& cell = model::cells[entry.first];
90 !
164
      assert(cell->type_ == Fill::MATERIAL);
90 !
165
      auto& instances = entry.second;
90 ✔
166
      for (auto instance : instances) {
315 ✔
167
        cell->set_temperature(T, instance);
225 ✔
168
      }
169
    }
170
  }
30 ✔
171
}
10,012 ✔
172

173
void Cell::set_density(double density, int32_t instance, bool set_contained)
346 ✔
174
{
175
  if (type_ != Fill::MATERIAL && !set_contained) {
346 !
176
    fatal_error(
×
177
      fmt::format("Attempted to set the density multiplier of cell {} "
×
178
                  "which is not filled by a material.",
179
        id_));
×
180
  }
181

182
  if (type_ == Fill::MATERIAL) {
346 ✔
183
    const int32_t mat_index = material(instance);
331 !
184
    if (mat_index == MATERIAL_VOID)
331 !
185
      return;
186

187
    if (instance >= 0) {
331 ✔
188
      // If density multiplier vector is not big enough, resize it first
189
      if (density_mult_.size() != n_instances())
254 ✔
190
        density_mult_.resize(n_instances(), density_mult_[0]);
111 ✔
191

192
      // Set density multiplier for the corresponding instance
193
      density_mult_.at(instance) =
254 ✔
194
        density / model::materials[mat_index]->density_gpcc();
508 !
195
    } else {
196
      // Set density multiplier for all instances
197
      for (auto& x : density_mult_) {
154 ✔
198
        x = density / model::materials[mat_index]->density_gpcc();
154 !
199
      }
200
    }
201
  } else {
202
    auto contained_cells = this->get_contained_cells(instance);
15 ✔
203
    for (const auto& entry : contained_cells) {
60 ✔
204
      auto& cell = model::cells[entry.first];
45 !
205
      assert(cell->type_ == Fill::MATERIAL);
45 !
206
      auto& instances = entry.second;
45 ✔
207
      for (auto instance : instances) {
90 ✔
208
        cell->set_density(density, instance);
45 ✔
209
      }
210
    }
211
  }
15 ✔
212
}
213

214
void Cell::export_properties_hdf5(hid_t group) const
231 ✔
215
{
216
  // Create a group for this cell.
217
  auto cell_group = create_group(group, fmt::format("cell {}", id_));
231 ✔
218

219
  // Write temperature in [K] for one or more cell instances
220
  vector<double> temps;
231 ✔
221
  for (auto sqrtkT_val : sqrtkT_)
429 ✔
222
    temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
198 ✔
223
  write_dataset(cell_group, "temperature", temps);
231 ✔
224

225
  // Write density for one or more cell instances
226
  if (type_ == Fill::MATERIAL && material_.size() > 0) {
231 ✔
227
    vector<double> density;
198 ✔
228
    for (int32_t i = 0; i < density_mult_.size(); ++i)
396 ✔
229
      density.push_back(this->density(i));
198 ✔
230

231
    write_dataset(cell_group, "density", density);
198 ✔
232
  }
198 ✔
233

234
  close_group(cell_group);
231 ✔
235
}
231 ✔
236

237
void Cell::import_properties_hdf5(hid_t group)
253 ✔
238
{
239
  auto cell_group = open_group(group, fmt::format("cell {}", id_));
253 ✔
240

241
  // Read temperatures from file
242
  vector<double> temps;
253 ✔
243
  read_dataset(cell_group, "temperature", temps);
253 ✔
244

245
  // Ensure number of temperatures makes sense
246
  auto n_temps = temps.size();
253 ✔
247
  if (n_temps > 1 && n_temps != n_instances()) {
253 !
248
    fatal_error(fmt::format(
×
249
      "Number of temperatures for cell {} doesn't match number of instances",
250
      id_));
×
251
  }
252

253
  // Modify temperatures for the cell
254
  sqrtkT_.clear();
253 ✔
255
  sqrtkT_.resize(temps.size());
253 ✔
256
  for (int64_t i = 0; i < temps.size(); ++i) {
9,922 ✔
257
    this->set_temperature(temps[i], i);
9,669 ✔
258
  }
259

260
  // Read densities
261
  if (object_exists(cell_group, "density")) {
253 ✔
262
    vector<double> density;
198 ✔
263
    read_dataset(cell_group, "density", density);
198 ✔
264

265
    // Ensure number of densities makes sense
266
    auto n_density = density.size();
198 !
267
    if (n_density > 1 && n_density != n_instances()) {
198 !
268
      fatal_error(fmt::format("Number of densities for cell {} "
×
269
                              "doesn't match number of instances",
270
        id_));
×
271
    }
272

273
    // Set densities.
274
    for (int32_t i = 0; i < n_density; ++i) {
396 ✔
275
      this->set_density(density[i], i);
198 ✔
276
    }
277
  }
198 ✔
278

279
  close_group(cell_group);
253 ✔
280
}
253 ✔
281

282
void Cell::to_hdf5(hid_t cell_group) const
30,490 ✔
283
{
284

285
  // Create a group for this cell.
286
  auto group = create_group(cell_group, fmt::format("cell {}", id_));
30,490 ✔
287

288
  if (!name_.empty()) {
30,490 ✔
289
    write_string(group, "name", name_, false);
7,298 ✔
290
  }
291

292
  write_dataset(group, "universe", model::universes[universe_]->id_);
30,490 ✔
293

294
  to_hdf5_inner(group);
30,490 ✔
295

296
  // Write fill information.
297
  if (type_ == Fill::MATERIAL) {
30,490 ✔
298
    write_dataset(group, "fill_type", "material");
24,923 ✔
299
    std::vector<int32_t> mat_ids;
24,923 ✔
300
    for (auto i_mat : material_) {
51,139 ✔
301
      if (i_mat != MATERIAL_VOID) {
26,216 ✔
302
        mat_ids.push_back(model::materials[i_mat]->id_);
17,531 ✔
303
      } else {
304
        mat_ids.push_back(MATERIAL_VOID);
8,685 ✔
305
      }
306
    }
307
    if (mat_ids.size() == 1) {
24,923 ✔
308
      write_dataset(group, "material", mat_ids[0]);
24,732 ✔
309
    } else {
310
      write_dataset(group, "material", mat_ids);
191 ✔
311
    }
312

313
    std::vector<double> temps;
24,923 ✔
314
    for (auto sqrtkT_val : sqrtkT_)
61,492 ✔
315
      temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
36,569 ✔
316
    write_dataset(group, "temperature", temps);
24,923 ✔
317

318
    write_dataset(group, "density_mult", density_mult_);
24,923 ✔
319

320
  } else if (type_ == Fill::UNIVERSE) {
30,490 ✔
321
    write_dataset(group, "fill_type", "universe");
4,028 ✔
322
    write_dataset(group, "fill", model::universes[fill_]->id_);
4,028 ✔
323
    if (translation_ != Position(0, 0, 0)) {
4,028 ✔
324
      write_dataset(group, "translation", translation_);
1,837 ✔
325
    }
326
    if (!rotation_.empty()) {
4,028 ✔
327
      if (rotation_.size() == 12) {
264 !
328
        std::array<double, 3> rot {rotation_[9], rotation_[10], rotation_[11]};
264 ✔
329
        write_dataset(group, "rotation", rot);
264 ✔
330
      } else {
331
        write_dataset(group, "rotation", rotation_);
×
332
      }
333
    }
334

335
  } else if (type_ == Fill::LATTICE) {
1,539 !
336
    write_dataset(group, "fill_type", "lattice");
1,539 ✔
337
    write_dataset(group, "lattice", model::lattices[fill_]->id_);
1,539 ✔
338
  }
339

340
  close_group(group);
30,490 ✔
341
}
30,490 ✔
342

343
//==============================================================================
344
// XML parsing helpers for <cell> nodes
345
//==============================================================================
346

347
vector<int32_t> parse_cell_material_xml(pugi::xml_node node, int32_t cell_id)
29,064 ✔
348
{
349
  vector<std::string> mats {
29,064 ✔
350
    get_node_array<std::string>(node, "material", true)};
29,064 ✔
351
  if (mats.empty()) {
29,064 !
352
    fatal_error(fmt::format(
×
353
      "An empty material element was specified for cell {}", cell_id));
354
  }
355
  vector<int32_t> material;
29,064 ✔
356
  material.reserve(mats.size());
29,064 ✔
357
  for (const auto& mat : mats) {
59,457 ✔
358
    if (mat == "void") {
30,393 ✔
359
      material.push_back(MATERIAL_VOID);
9,141 ✔
360
    } else {
361
      material.push_back(std::stoi(mat));
21,252 ✔
362
    }
363
  }
364
  return material;
29,064 ✔
365
}
29,064 ✔
366

367
vector<double> parse_cell_temperature_xml(pugi::xml_node node, int32_t cell_id)
431 ✔
368
{
369
  auto temperatures = get_node_array<double>(node, "temperature");
431 ✔
370
  if (temperatures.empty()) {
431 !
371
    fatal_error(fmt::format(
×
372
      "An empty temperature element was specified for cell {}", cell_id));
373
  }
374
  for (auto T : temperatures) {
1,942 ✔
375
    if (T < 0) {
1,511 !
376
      fatal_error(fmt::format(
×
377
        "Cell {} was specified with a negative temperature", cell_id));
378
    }
379
  }
380
  return temperatures;
431 ✔
381
}
×
382

383
vector<double> parse_cell_density_xml(pugi::xml_node node, int32_t cell_id)
75 ✔
384
{
385
  auto densities = get_node_array<double>(node, "density");
75 ✔
386
  if (densities.empty()) {
75 !
387
    fatal_error(fmt::format(
×
388
      "An empty density element was specified for cell {}", cell_id));
389
  }
390
  for (auto rho : densities) {
1,230 ✔
391
    if (rho <= 0) {
1,155 !
392
      fatal_error(fmt::format(
×
393
        "Cell {} was specified with a density less than or equal to zero",
394
        cell_id));
395
    }
396
  }
397
  return densities;
75 ✔
398
}
×
399

400
//==============================================================================
401
// CSGCell implementation
402
//==============================================================================
403

404
CSGCell::CSGCell(pugi::xml_node cell_node)
36,325 ✔
405
{
406
  if (check_for_node(cell_node, "id")) {
36,325 !
407
    id_ = std::stoi(get_node_value(cell_node, "id"));
72,650 ✔
408
  } else {
409
    fatal_error("Must specify id of cell in geometry XML file.");
×
410
  }
411

412
  if (check_for_node(cell_node, "name")) {
36,325 ✔
413
    name_ = get_node_value(cell_node, "name");
9,289 ✔
414
  }
415

416
  if (check_for_node(cell_node, "universe")) {
36,325 ✔
417
    universe_ = std::stoi(get_node_value(cell_node, "universe"));
70,232 ✔
418
  } else {
419
    universe_ = 0;
1,209 ✔
420
  }
421

422
  // Make sure that either material or fill was specified, but not both.
423
  bool fill_present = check_for_node(cell_node, "fill");
36,325 ✔
424
  bool material_present = check_for_node(cell_node, "material");
36,325 ✔
425
  if (!(fill_present || material_present)) {
36,325 !
426
    fatal_error(
×
427
      fmt::format("Neither material nor fill was specified for cell {}", id_));
×
428
  }
429
  if (fill_present && material_present) {
36,325 !
430
    fatal_error(fmt::format("Cell {} has both a material and a fill specified; "
×
431
                            "only one can be specified per cell",
432
      id_));
×
433
  }
434

435
  if (fill_present) {
36,325 ✔
436
    fill_ = std::stoi(get_node_value(cell_node, "fill"));
14,540 ✔
437
    if (fill_ == universe_) {
7,270 !
438
      fatal_error(fmt::format("Cell {} is filled with the same universe that "
×
439
                              "it is contained in.",
440
        id_));
×
441
    }
442
  } else {
443
    fill_ = C_NONE;
29,055 ✔
444
  }
445

446
  // Read the material element.  There can be zero materials (filled with a
447
  // universe), more than one material (distribmats), and some materials may
448
  // be "void".
449
  if (material_present) {
36,325 ✔
450
    material_ = parse_cell_material_xml(cell_node, id_);
29,055 ✔
451
  }
452

453
  // Read the temperature element which may be distributed like materials.
454
  if (check_for_node(cell_node, "temperature")) {
36,325 ✔
455
    sqrtkT_ = parse_cell_temperature_xml(cell_node, id_);
431 ✔
456
    sqrtkT_.shrink_to_fit();
431 ✔
457

458
    // Make sure this is a material-filled cell.
459
    if (material_.size() == 0) {
431 !
460
      fatal_error(fmt::format(
×
461
        "Cell {} was specified with a temperature but no material. Temperature"
462
        "specification is only valid for cells filled with a material.",
463
        id_));
×
464
    }
465

466
    // Convert to sqrt(k*T).
467
    for (auto& T : sqrtkT_) {
1,942 ✔
468
      T = std::sqrt(K_BOLTZMANN * T);
1,511 ✔
469
    }
470
  }
471

472
  // Read the density element which can be distributed similar to temperature.
473
  // These get assigned to the density multiplier, requiring a division by
474
  // the material density.
475
  // Note: calculating the actual density multiplier is deferred until materials
476
  // are finalized. density_mult_ contains the true density in the meantime.
477
  if (check_for_node(cell_node, "density")) {
36,325 ✔
478
    density_mult_ = parse_cell_density_xml(cell_node, id_);
75 ✔
479
    density_mult_.shrink_to_fit();
75 ✔
480

481
    // Make sure this is a material-filled cell.
482
    if (material_.size() == 0) {
75 !
483
      fatal_error(fmt::format(
×
484
        "Cell {} was specified with a density but no material. Density"
485
        "specification is only valid for cells filled with a material.",
486
        id_));
×
487
    }
488

489
    // Make sure this is a non-void material.
490
    for (auto mat_id : material_) {
150 ✔
491
      if (mat_id == MATERIAL_VOID) {
75 !
492
        fatal_error(fmt::format(
×
493
          "Cell {} was specified with a density, but contains a void "
494
          "material. Density specification is only valid for cells "
495
          "filled with a non-void material.",
496
          id_));
×
497
      }
498
    }
499
  }
500

501
  // Read the region specification.
502
  std::string region_spec;
36,325 ✔
503
  if (check_for_node(cell_node, "region")) {
36,325 ✔
504
    region_spec = get_node_value(cell_node, "region");
27,141 ✔
505
  }
506

507
  // Get a tokenized representation of the region specification and apply De
508
  // Morgans law
509
  Region region(region_spec, id_);
36,325 ✔
510
  region_ = region;
36,325 ✔
511

512
  // Read the translation vector.
513
  if (check_for_node(cell_node, "translation")) {
36,325 ✔
514
    if (fill_ == C_NONE) {
2,557 !
515
      fatal_error(fmt::format("Cannot apply a translation to cell {}"
×
516
                              " because it is not filled with another universe",
517
        id_));
×
518
    }
519

520
    auto xyz {get_node_array<double>(cell_node, "translation")};
2,557 ✔
521
    if (xyz.size() != 3) {
2,557 !
522
      fatal_error(
×
523
        fmt::format("Non-3D translation vector applied to cell {}", id_));
×
524
    }
525
    translation_ = xyz;
2,557 ✔
526
  }
2,557 ✔
527

528
  // Read the rotation transform.
529
  if (check_for_node(cell_node, "rotation")) {
36,325 ✔
530
    auto rot {get_node_array<double>(cell_node, "rotation")};
390 ✔
531
    set_rotation(rot);
390 ✔
532
  }
390 ✔
533
}
36,325 ✔
534

535
//==============================================================================
536

537
void CSGCell::to_hdf5_inner(hid_t group_id) const
30,341 ✔
538
{
539
  write_string(group_id, "geom_type", "csg", false);
30,341 ✔
540
  write_string(group_id, "region", region_.str(), false);
30,341 ✔
541
}
30,341 ✔
542

543
//==============================================================================
544

545
vector<int32_t>::iterator CSGCell::find_left_parenthesis(
×
546
  vector<int32_t>::iterator start, const vector<int32_t>& infix)
547
{
548
  // start search at zero
549
  int parenthesis_level = 0;
×
550
  auto it = start;
×
551
  while (it != infix.begin()) {
×
552
    // look at two tokens at a time
553
    int32_t one = *it;
×
554
    int32_t two = *(it - 1);
×
555

556
    // decrement parenthesis level if there are two adjacent surfaces
557
    if (one < OP_UNION && two < OP_UNION) {
×
558
      parenthesis_level--;
×
559
      // increment if there are two adjacent operators
560
    } else if (one >= OP_UNION && two >= OP_UNION) {
×
561
      parenthesis_level++;
×
562
    }
563

564
    // if the level gets to zero, return the position
565
    if (parenthesis_level == 0) {
×
566
      // move the iterator back one before leaving the loop
567
      // so that all tokens in the parenthesis block are included
568
      it--;
×
569
      break;
570
    }
571

572
    // continue loop, one token at a time
573
    it--;
574
  }
575
  return it;
×
576
}
577

578
//==============================================================================
579
// Region implementation
580
//==============================================================================
581

582
Region::Region(std::string region_spec, int32_t cell_id)
36,424 ✔
583
{
584
  // Check if region_spec is not empty.
585
  if (!region_spec.empty()) {
36,424 ✔
586
    // Parse all halfspaces and operators except for intersection (whitespace).
587
    for (int i = 0; i < region_spec.size();) {
164,402 ✔
588
      if (region_spec[i] == '(') {
137,162 ✔
589
        expression_.push_back(OP_LEFT_PAREN);
1,766 ✔
590
        i++;
1,766 ✔
591

592
      } else if (region_spec[i] == ')') {
135,396 ✔
593
        expression_.push_back(OP_RIGHT_PAREN);
1,766 ✔
594
        i++;
1,766 ✔
595

596
      } else if (region_spec[i] == '|') {
133,630 ✔
597
        expression_.push_back(OP_UNION);
4,461 ✔
598
        i++;
4,461 ✔
599

600
      } else if (region_spec[i] == '~') {
129,169 ✔
601
        expression_.push_back(OP_COMPLEMENT);
30 ✔
602
        i++;
30 ✔
603

604
      } else if (region_spec[i] == '-' || region_spec[i] == '+' ||
218,126 !
605
                 std::isdigit(region_spec[i])) {
88,987 ✔
606
        // This is the start of a halfspace specification.  Iterate j until we
607
        // find the end, then push-back everything between i and j.
608
        int j = i + 1;
75,213 ✔
609
        while (j < region_spec.size() && std::isdigit(region_spec[j])) {
151,191 ✔
610
          j++;
75,978 ✔
611
        }
612
        expression_.push_back(std::stoi(region_spec.substr(i, j - i)));
150,426 ✔
613
        i = j;
75,213 ✔
614

615
      } else if (std::isspace(region_spec[i])) {
53,926 !
616
        i++;
53,926 ✔
617

618
      } else {
619
        auto err_msg =
×
620
          fmt::format("Region specification contains invalid character, \"{}\"",
621
            region_spec[i]);
×
622
        fatal_error(err_msg);
×
623
      }
×
624
    }
625

626
    // Add in intersection operators where a missing operator is needed.
627
    int i = 0;
628
    while (i < expression_.size() - 1) {
126,748 ✔
629
      bool left_compat {
99,508 ✔
630
        (expression_[i] < OP_UNION) || (expression_[i] == OP_RIGHT_PAREN)};
99,508 ✔
631
      bool right_compat {(expression_[i + 1] < OP_UNION) ||
99,508 ✔
632
                         (expression_[i + 1] == OP_LEFT_PAREN) ||
99,508 ✔
633
                         (expression_[i + 1] == OP_COMPLEMENT)};
6,287 ✔
634
      if (left_compat && right_compat) {
99,508 ✔
635
        expression_.insert(expression_.begin() + i + 1, OP_INTERSECTION);
43,512 ✔
636
      }
637
      i++;
638
    }
639

640
    // Remove complement operators using DeMorgan's laws
641
    auto it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
27,240 ✔
642
    while (it != expression_.end()) {
27,270 ✔
643
      // Erase complement
644
      expression_.erase(it);
30 ✔
645

646
      // Define stop given left parenthesis or not
647
      auto stop = it;
30 ✔
648
      if (*it == OP_LEFT_PAREN) {
30 !
649
        int depth = 1;
650
        do {
240 ✔
651
          stop++;
240 ✔
652
          if (*stop > OP_COMPLEMENT) {
240 ✔
653
            if (*stop == OP_RIGHT_PAREN) {
30 !
654
              depth--;
30 ✔
655
            } else {
656
              depth++;
×
657
            }
658
          }
659
        } while (depth > 0);
240 ✔
660
        it++;
30 ✔
661
      }
662

663
      // apply DeMorgan's law to any surfaces/operators between these
664
      // positions in the RPN
665
      apply_demorgan(it, stop);
30 ✔
666
      // update iterator position
667
      it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
30 ✔
668
    }
669

670
    // Convert user IDs to surface indices.
671
    for (auto& r : expression_) {
153,958 ✔
672
      if (r < OP_UNION) {
126,718 ✔
673
        const auto& it {model::surface_map.find(abs(r))};
75,213 !
674
        if (it == model::surface_map.end()) {
75,213 !
675
          throw std::runtime_error {
×
676
            "Invalid surface ID " + std::to_string(abs(r)) +
×
677
            " specified in region for cell " + std::to_string(cell_id) + "."};
×
678
        }
679
        r = (r > 0) ? it->second + 1 : -(it->second + 1);
75,213 ✔
680
      }
681
    }
682

683
    // Check if this is a simple cell.
684
    simple_ = true;
27,240 ✔
685
    for (int32_t token : expression_) {
137,562 ✔
686
      if (token == OP_UNION) {
111,568 ✔
687
        simple_ = false;
1,246 ✔
688
        // Ensure intersections have precedence over unions
689
        enforce_precedence();
1,246 ✔
690
        break;
691
      }
692
    }
693

694
    // If this cell is simple, remove all the superfluous operator tokens.
695
    if (simple_) {
27,240 ✔
696
      for (auto it = expression_.begin(); it != expression_.end(); it++) {
129,466 ✔
697
        if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT) {
103,472 !
698
          expression_.erase(it);
38,739 ✔
699
          it--;
103,472 ✔
700
        }
701
      }
702
    }
703
    expression_.shrink_to_fit();
27,240 ✔
704

705
  } else {
706
    simple_ = true;
9,184 ✔
707
  }
708
}
36,424 ✔
709

710
//==============================================================================
711

712
void Region::apply_demorgan(
30 ✔
713
  vector<int32_t>::iterator start, vector<int32_t>::iterator stop)
714
{
715
  do {
210 ✔
716
    if (*start < OP_UNION) {
210 ✔
717
      *start *= -1;
120 ✔
718
    } else if (*start == OP_UNION) {
90 !
719
      *start = OP_INTERSECTION;
×
720
    } else if (*start == OP_INTERSECTION) {
90 !
721
      *start = OP_UNION;
90 ✔
722
    }
723
    start++;
210 ✔
724
  } while (start < stop);
210 ✔
725
}
30 ✔
726

727
//==============================================================================
728
//! Add precedence for infix regions so intersections have higher
729
//! precedence than unions using parentheses.
730
//==============================================================================
731

732
void Region::add_parentheses(int64_t start)
96 ✔
733
{
734
  int32_t start_token = expression_[start];
96 !
735
  // Add left parenthesis and set new position to be after parenthesis
736
  if (start_token == OP_UNION) {
96 !
737
    start += 2;
×
738
  }
739
  expression_.insert(expression_.begin() + start - 1, OP_LEFT_PAREN);
96 ✔
740

741
  // Add right parenthesis
742
  // While the start iterator is within the bounds of infix
743
  while (start + 1 < expression_.size()) {
430 ✔
744
    start++;
408 ✔
745

746
    // If the current token is an operator and is different than the start token
747
    if (expression_[start] >= OP_UNION && expression_[start] != start_token) {
408 ✔
748
      // Skip wrapped regions but save iterator position to check precedence and
749
      // add right parenthesis, right parenthesis position depends on the
750
      // operator, when the operator is a union then do not include the operator
751
      // in the region, when the operator is an intersection then include the
752
      // operator and next surface
753
      if (expression_[start] == OP_LEFT_PAREN) {
85 ✔
754
        int depth = 1;
755
        do {
44 ✔
756
          start++;
44 ✔
757
          if (expression_[start] > OP_COMPLEMENT) {
44 ✔
758
            if (expression_[start] == OP_RIGHT_PAREN) {
11 !
759
              depth--;
11 ✔
760
            } else {
761
              depth++;
×
762
            }
763
          }
764
        } while (depth > 0);
44 ✔
765
      } else {
766
        if (start_token == OP_UNION) {
74 !
767
          --start;
×
768
        }
769
        expression_.insert(expression_.begin() + start, OP_RIGHT_PAREN);
74 ✔
770
        return;
74 ✔
771
      }
772
    }
773
  }
774
  // If we get here a right parenthesis hasn't been placed
775
  expression_.push_back(OP_RIGHT_PAREN);
22 ✔
776
}
777

778
//==============================================================================
779
//! Add parentheses to enforce operator precedence in region expressions
780
//!
781
//! This function ensures that intersection operators have higher precedence
782
//! than union operators by adding parentheses where needed. For example:
783
//!   "1 2 | 3" becomes "(1 2) | 3"
784
//!   "1 | 2 3" becomes "1 | (2 3)"
785
//!
786
//! The algorithm uses stacks to track the current operator type and its
787
//! position at each parenthesis depth level. When it encounters a different
788
//! operator at the same depth, it adds parentheses to group the
789
//! higher-precedence operations.
790
//==============================================================================
791

792
void Region::enforce_precedence()
1,246 ✔
793
{
794
  // Stack tracking the operator type at each depth (0 = no operator seen yet)
795
  vector<int32_t> op_stack = {0};
1,246 ✔
796

797
  // Stack tracking where the operator sequence started at each depth
798
  vector<std::size_t> pos_stack = {0};
1,246 ✔
799

800
  for (int64_t i = 0; i < expression_.size(); ++i) {
25,509 ✔
801
    int32_t token = expression_[i];
24,263 ✔
802

803
    if (token == OP_LEFT_PAREN) {
24,263 ✔
804
      // Entering a new parenthesis level - push new tracking state
805
      op_stack.push_back(0);
1,962 ✔
806
      pos_stack.push_back(0);
1,962 ✔
807
      continue;
1,962 ✔
808
    } else if (token == OP_RIGHT_PAREN) {
22,301 ✔
809
      // Exiting a parenthesis level - pop tracking state (keep at least one)
810
      if (op_stack.size() > 1) {
1,921 !
811
        op_stack.pop_back();
1,921 ✔
812
        pos_stack.pop_back();
1,921 ✔
813
      }
814
      continue;
1,921 ✔
815
    }
816

817
    if (token == OP_UNION || token == OP_INTERSECTION) {
20,380 ✔
818
      if (op_stack.back() == 0) {
9,567 ✔
819
        // First operator at this depth - record it and its position
820
        op_stack.back() = token;
3,252 ✔
821
        pos_stack.back() = i;
3,252 ✔
822
      } else if (token != op_stack.back()) {
6,315 ✔
823
        // Encountered a different operator at the same depth - need to add
824
        // parentheses to enforce precedence. Intersection has higher
825
        // precedence, so we parenthesize the intersection terms.
826
        if (op_stack.back() == OP_INTERSECTION) {
96 ✔
827
          add_parentheses(pos_stack.back());
48 ✔
828
        } else {
829
          add_parentheses(i);
48 ✔
830
        }
831

832
        // Restart the scan since we modified the expression
833
        i = -1; // Will be incremented to 0 by the for loop
96 ✔
834
        op_stack = {0};
96 ✔
835
        pos_stack = {0};
96 ✔
836
      }
837
    }
838
  }
839
}
1,246 ✔
840

841
//==============================================================================
842
//! Convert infix region specification to Reverse Polish Notation (RPN)
843
//!
844
//! This function uses the shunting-yard algorithm.
845
//==============================================================================
846

847
vector<int32_t> Region::generate_postfix(int32_t cell_id) const
44 ✔
848
{
849
  vector<int32_t> rpn;
44 ✔
850
  vector<int32_t> stack;
44 ✔
851

852
  for (int32_t token : expression_) {
990 ✔
853
    if (token < OP_UNION) {
946 ✔
854
      // If token is not an operator, add it to output
855
      rpn.push_back(token);
396 ✔
856
    } else if (token < OP_RIGHT_PAREN) {
550 ✔
857
      // Regular operators union, intersection, complement
858
      while (stack.size() > 0) {
561 ✔
859
        int32_t op = stack.back();
462 ✔
860

861
        if (op < OP_RIGHT_PAREN && ((token == OP_COMPLEMENT && token < op) ||
462 !
862
                                     (token != OP_COMPLEMENT && token <= op))) {
209 !
863
          // While there is an operator, op, on top of the stack, if the token
864
          // is left-associative and its precedence is less than or equal to
865
          // that of op or if the token is right-associative and its precedence
866
          // is less than that of op, move op to the output queue and push the
867
          // token on to the stack. Note that only complement is
868
          // right-associative.
869
          rpn.push_back(op);
209 ✔
870
          stack.pop_back();
209 ✔
871
        } else {
872
          break;
873
        }
874
      }
875

876
      stack.push_back(token);
352 ✔
877

878
    } else if (token == OP_LEFT_PAREN) {
198 ✔
879
      // If the token is a left parenthesis, push it onto the stack
880
      stack.push_back(token);
99 ✔
881

882
    } else {
883
      // If the token is a right parenthesis, move operators from the stack to
884
      // the output queue until reaching the left parenthesis.
885
      for (auto it = stack.rbegin(); *it != OP_LEFT_PAREN; it++) {
198 ✔
886
        // If we run out of operators without finding a left parenthesis, it
887
        // means there are mismatched parentheses.
888
        if (it == stack.rend()) {
99 !
889
          fatal_error(fmt::format(
×
890
            "Mismatched parentheses in region specification for cell {}",
891
            cell_id));
892
        }
893
        rpn.push_back(stack.back());
99 ✔
894
        stack.pop_back();
99 ✔
895
      }
896

897
      // Pop the left parenthesis.
898
      stack.pop_back();
946 ✔
899
    }
900
  }
901

902
  while (stack.size() > 0) {
44 ✔
903
    int32_t op = stack.back();
44 !
904

905
    // If the operator is a parenthesis it is mismatched.
906
    if (op >= OP_RIGHT_PAREN) {
44 !
907
      fatal_error(fmt::format(
×
908
        "Mismatched parentheses in region specification for cell {}", cell_id));
909
    }
910

911
    rpn.push_back(stack.back());
44 ✔
912
    stack.pop_back();
88 ✔
913
  }
914

915
  return rpn;
44 ✔
916
}
44 ✔
917

918
//==============================================================================
919

920
std::string Region::str() const
30,440 ✔
921
{
922
  std::stringstream region_spec {};
30,440 ✔
923
  if (!expression_.empty()) {
30,440 ✔
924
    for (int32_t token : expression_) {
94,581 ✔
925
      if (token == OP_LEFT_PAREN) {
72,602 ✔
926
        region_spec << " (";
1,582 ✔
927
      } else if (token == OP_RIGHT_PAREN) {
71,020 ✔
928
        region_spec << " )";
1,582 ✔
929
      } else if (token == OP_COMPLEMENT) {
69,438 !
930
        region_spec << " ~";
×
931
      } else if (token == OP_INTERSECTION) {
69,438 ✔
932
      } else if (token == OP_UNION) {
65,519 ✔
933
        region_spec << " |";
3,963 ✔
934
      } else {
935
        // Note the off-by-one indexing
936
        auto surf_id = model::surfaces[abs(token) - 1]->id_;
61,556 ✔
937
        region_spec << " " << ((token > 0) ? surf_id : -surf_id);
61,556 ✔
938
      }
939
    }
940
  }
941
  return region_spec.str();
60,880 ✔
942
}
30,440 ✔
943

944
//==============================================================================
945

946
std::pair<double, int32_t> Region::distance(
2,147,483,647 ✔
947
  Position r, Direction u, int32_t on_surface) const
948
{
949
  double min_dist {INFTY};
2,147,483,647 ✔
950
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
2,147,483,647 ✔
951

UNCOV
952
  for (int32_t token : expression_) {
✔
953
    // Ignore this token if it corresponds to an operator rather than a region.
954
    if (token >= OP_UNION)
2,147,483,647 ✔
955
      continue;
2,147,483,647 ✔
956

957
    // Calculate the distance to this surface.
958
    // Note the off-by-one indexing
959
    bool coincident {std::abs(token) == std::abs(on_surface)};
2,147,483,647 ✔
960
    double d {model::surfaces[abs(token) - 1]->distance(r, u, coincident)};
2,147,483,647 ✔
961

962
    // Check if this distance is the new minimum.
963
    if (d < min_dist) {
2,147,483,647 ✔
964
      if (min_dist - d >= FP_PRECISION * min_dist) {
2,147,483,647 !
965
        min_dist = d;
2,147,483,647 ✔
966
        i_surf = -token;
2,147,483,647 ✔
967
      }
968
    }
969
  }
970

971
  return {min_dist, i_surf};
2,147,483,647 ✔
972
}
973

974
//==============================================================================
975

976
bool Region::contains(Position r, Direction u, int32_t on_surface) const
2,147,483,647 ✔
977
{
978
  if (simple_) {
2,147,483,647 ✔
979
    return contains_simple(r, u, on_surface);
2,147,483,647 ✔
980
  } else {
981
    return contains_complex(r, u, on_surface);
53,116,639 ✔
982
  }
983
}
984

985
//==============================================================================
986

987
bool Region::contains_simple(Position r, Direction u, int32_t on_surface) const
2,147,483,647 ✔
988
{
989
  for (int32_t token : expression_) {
2,147,483,647 ✔
990
    // Assume that no tokens are operators. Evaluate the sense of particle with
991
    // respect to the surface and see if the token matches the sense. If the
992
    // particle's surface attribute is set and matches the token, that
993
    // overrides the determination based on sense().
994
    if (token == on_surface) {
2,147,483,647 ✔
995
    } else if (-token == on_surface) {
2,147,483,647 ✔
996
      return false;
997
    } else {
998
      // Note the off-by-one indexing
999
      bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
2,147,483,647 ✔
1000
      if (sense != (token > 0)) {
2,147,483,647 ✔
1001
        return false;
1002
      }
1003
    }
1004
  }
1005
  return true;
1006
}
1007

1008
//==============================================================================
1009

1010
bool Region::contains_complex(Position r, Direction u, int32_t on_surface) const
53,116,639 ✔
1011
{
1012
  bool in_cell = true;
53,116,639 ✔
1013
  int total_depth = 0;
53,116,639 ✔
1014

1015
  // For each token
1016
  for (auto it = expression_.begin(); it != expression_.end(); it++) {
760,319,268 ✔
1017
    int32_t token = *it;
720,264,514 ✔
1018

1019
    // If the token is a surface evaluate the sense
1020
    // If the token is a union or intersection check to
1021
    // short circuit
1022
    if (token < OP_UNION) {
720,264,514 ✔
1023
      if (token == on_surface) {
265,096,980 ✔
1024
        in_cell = true;
1025
      } else if (-token == on_surface) {
249,399,267 ✔
1026
        in_cell = false;
1027
      } else {
1028
        // Note the off-by-one indexing
1029
        bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
239,510,466 ✔
1030
        in_cell = (sense == (token > 0));
239,510,466 ✔
1031
      }
1032
    } else if ((token == OP_UNION && in_cell == true) ||
455,167,534 ✔
1033
               (token == OP_INTERSECTION && in_cell == false)) {
165,067,244 ✔
1034
      // If the total depth is zero return
1035
      if (total_depth == 0) {
102,207,309 ✔
1036
        return in_cell;
13,061,885 ✔
1037
      }
1038

1039
      total_depth--;
89,145,424 ✔
1040

1041
      // While the iterator is within the bounds of the vector
1042
      int depth = 1;
89,145,424 ✔
1043
      do {
1,099,707,144 ✔
1044
        // Get next token
1045
        it++;
1,099,707,144 ✔
1046
        int32_t next_token = *it;
1,099,707,144 ✔
1047

1048
        // If the token is an a parenthesis
1049
        if (next_token > OP_COMPLEMENT) {
1,099,707,144 ✔
1050
          // Adjust depth accordingly
1051
          if (next_token == OP_RIGHT_PAREN) {
217,558,172 ✔
1052
            depth--;
153,351,798 ✔
1053
          } else {
1054
            depth++;
64,206,374 ✔
1055
          }
1056
        }
1057
      } while (depth > 0);
1,099,707,144 ✔
1058
    } else if (token == OP_LEFT_PAREN) {
352,960,225 ✔
1059
      total_depth++;
115,062,654 ✔
1060
    } else if (token == OP_RIGHT_PAREN) {
237,897,571 ✔
1061
      total_depth--;
25,917,230 ✔
1062
    }
1063
  }
1064
  return in_cell;
1065
}
1066

1067
//==============================================================================
1068

1069
BoundingBox Region::bounding_box(int32_t cell_id) const
88 ✔
1070
{
1071
  if (simple_) {
88 ✔
1072
    return bounding_box_simple();
44 ✔
1073
  } else {
1074
    auto postfix = generate_postfix(cell_id);
44 ✔
1075
    return bounding_box_complex(postfix);
88 ✔
1076
  }
44 ✔
1077
}
1078

1079
//==============================================================================
1080

1081
BoundingBox Region::bounding_box_simple() const
44 ✔
1082
{
1083
  BoundingBox bbox;
44 ✔
1084
  for (int32_t token : expression_) {
176 ✔
1085
    bbox &= model::surfaces[abs(token) - 1]->bounding_box(token > 0);
132 ✔
1086
  }
1087
  return bbox;
44 ✔
1088
}
1089

1090
//==============================================================================
1091

1092
BoundingBox Region::bounding_box_complex(vector<int32_t> postfix) const
44 ✔
1093
{
1094
  vector<BoundingBox> stack(postfix.size());
44 ✔
1095
  int i_stack = -1;
44 ✔
1096

1097
  for (auto& token : postfix) {
792 ✔
1098
    if (token == OP_UNION) {
748 ✔
1099
      stack[i_stack - 1] = stack[i_stack - 1] | stack[i_stack];
154 ✔
1100
      i_stack--;
154 ✔
1101
    } else if (token == OP_INTERSECTION) {
594 ✔
1102
      stack[i_stack - 1] = stack[i_stack - 1] & stack[i_stack];
198 ✔
1103
      i_stack--;
198 ✔
1104
    } else {
1105
      i_stack++;
396 ✔
1106
      stack[i_stack] = model::surfaces[abs(token) - 1]->bounding_box(token > 0);
396 ✔
1107
    }
1108
  }
1109

1110
  assert(i_stack == 0);
44 !
1111
  return stack.front();
44 ✔
1112
}
44 ✔
1113

1114
//==============================================================================
1115

1116
vector<int32_t> Region::surfaces() const
5,270 ✔
1117
{
1118
  if (simple_) {
5,270 ✔
1119
    return expression_;
5,250 ✔
1120
  }
1121

1122
  vector<int32_t> surfaces = expression_;
20 ✔
1123

1124
  auto it = std::find_if(surfaces.begin(), surfaces.end(),
20 ✔
1125
    [&](const auto& value) { return value >= OP_UNION; });
20 !
1126

1127
  while (it != surfaces.end()) {
60 ✔
1128
    surfaces.erase(it);
40 ✔
1129

1130
    it = std::find_if(surfaces.begin(), surfaces.end(),
40 ✔
1131
      [&](const auto& value) { return value >= OP_UNION; });
80 !
1132
  }
1133

1134
  return surfaces;
20 ✔
1135
}
5,270 ✔
1136

1137
//==============================================================================
1138
// Non-method functions
1139
//==============================================================================
1140

1141
void read_cells(pugi::xml_node node)
9,044 ✔
1142
{
1143
  // Count the number of cells.
1144
  int n_cells = 0;
9,044 ✔
1145
  for (pugi::xml_node cell_node : node.children("cell")) {
45,369 ✔
1146
    n_cells++;
36,325 ✔
1147
  }
1148

1149
  // Loop over XML cell elements and populate the array.
1150
  model::cells.reserve(n_cells);
9,044 ✔
1151
  for (pugi::xml_node cell_node : node.children("cell")) {
45,369 ✔
1152
    model::cells.push_back(make_unique<CSGCell>(cell_node));
36,325 ✔
1153
  }
1154

1155
  // Fill the cell map.
1156
  for (int i = 0; i < model::cells.size(); i++) {
45,369 ✔
1157
    int32_t id = model::cells[i]->id_;
36,325 !
1158
    auto search = model::cell_map.find(id);
36,325 !
1159
    if (search == model::cell_map.end()) {
36,325 !
1160
      model::cell_map[id] = i;
36,325 ✔
1161
    } else {
1162
      fatal_error(
×
1163
        fmt::format("Two or more cells use the same unique ID: {}", id));
×
1164
    }
1165
  }
1166

1167
  read_dagmc_universes(node);
9,044 ✔
1168

1169
  populate_universes();
9,042 ✔
1170

1171
  // Allocate the cell overlap count if necessary.
1172
  if (settings::check_overlaps) {
9,042 ✔
1173
    model::overlap_check_count.resize(model::cells.size(), 0);
119 ✔
1174
  }
1175

1176
  if (model::cells.size() == 0) {
9,042 !
1177
    fatal_error("No cells were found in the geometry.xml file");
×
1178
  }
1179
}
9,042 ✔
1180

1181
void populate_universes()
9,044 ✔
1182
{
1183
  // Used to map universe index to the index of an implicit complement cell for
1184
  // DAGMC universes
1185
  std::unordered_map<int, int> implicit_comp_cells;
9,044 ✔
1186

1187
  // Populate the Universe vector and map.
1188
  for (int index_cell = 0; index_cell < model::cells.size(); index_cell++) {
45,570 ✔
1189
    int32_t uid = model::cells[index_cell]->universe_;
36,526 ✔
1190
    auto it = model::universe_map.find(uid);
36,526 ✔
1191
    if (it == model::universe_map.end()) {
36,526 ✔
1192
      model::universes.push_back(make_unique<Universe>());
41,770 ✔
1193
      model::universes.back()->id_ = uid;
20,885 ✔
1194
      model::universes.back()->cells_.push_back(index_cell);
20,885 ✔
1195
      model::universe_map[uid] = model::universes.size() - 1;
20,885 ✔
1196
    } else {
1197
#ifdef OPENMC_DAGMC_ENABLED
1198
      // Skip implicit complement cells for now
1199
      Universe* univ = model::universes[it->second].get();
2,069 !
1200
      DAGUniverse* dag_univ = dynamic_cast<DAGUniverse*>(univ);
2,069 !
1201
      if (dag_univ && (dag_univ->implicit_complement_idx() == index_cell)) {
2,069 ✔
1202
        implicit_comp_cells[it->second] = index_cell;
43 ✔
1203
        continue;
43 ✔
1204
      }
1205
#endif
1206

1207
      model::universes[it->second]->cells_.push_back(index_cell);
15,598 ✔
1208
    }
1209
  }
1210

1211
  // Add DAGUniverse implicit complement cells last
1212
  for (const auto& it : implicit_comp_cells) {
9,087 ✔
1213
    int index_univ = it.first;
43 ✔
1214
    int index_cell = it.second;
43 ✔
1215
    model::universes[index_univ]->cells_.push_back(index_cell);
43 !
1216
  }
1217

1218
  model::universes.shrink_to_fit();
9,044 ✔
1219
}
9,044 ✔
1220

1221
//==============================================================================
1222
// C-API functions
1223
//==============================================================================
1224

1225
extern "C" int openmc_cell_get_fill(
220 ✔
1226
  int32_t index, int* type, int32_t** indices, int32_t* n)
1227
{
1228
  if (index >= 0 && index < model::cells.size()) {
220 !
1229
    Cell& c {*model::cells[index]};
220 ✔
1230
    *type = static_cast<int>(c.type_);
220 ✔
1231
    if (c.type_ == Fill::MATERIAL) {
220 ✔
1232
      *indices = c.material_.data();
209 ✔
1233
      *n = c.material_.size();
209 ✔
1234
    } else {
1235
      *indices = &c.fill_;
11 ✔
1236
      *n = 1;
11 ✔
1237
    }
1238
  } else {
1239
    set_errmsg("Index in cells array is out of bounds.");
×
1240
    return OPENMC_E_OUT_OF_BOUNDS;
×
1241
  }
1242
  return 0;
1243
}
1244

1245
extern "C" int openmc_cell_set_fill(
11 ✔
1246
  int32_t index, int type, int32_t n, const int32_t* indices)
1247
{
1248
  Fill filltype = static_cast<Fill>(type);
11 ✔
1249
  if (index >= 0 && index < model::cells.size()) {
11 !
1250
    Cell& c {*model::cells[index]};
11 !
1251
    if (filltype == Fill::MATERIAL) {
11 !
1252
      c.type_ = Fill::MATERIAL;
11 ✔
1253
      c.material_.clear();
11 !
1254
      for (int i = 0; i < n; i++) {
22 ✔
1255
        int i_mat = indices[i];
11 ✔
1256
        if (i_mat == MATERIAL_VOID) {
11 !
1257
          c.material_.push_back(MATERIAL_VOID);
×
1258
        } else if (i_mat >= 0 && i_mat < model::materials.size()) {
11 !
1259
          c.material_.push_back(i_mat);
11 ✔
1260
        } else {
1261
          set_errmsg("Index in materials array is out of bounds.");
×
1262
          return OPENMC_E_OUT_OF_BOUNDS;
×
1263
        }
1264
      }
1265
      c.material_.shrink_to_fit();
11 ✔
1266
    } else if (filltype == Fill::UNIVERSE) {
×
1267
      c.type_ = Fill::UNIVERSE;
×
1268
    } else {
1269
      c.type_ = Fill::LATTICE;
×
1270
    }
1271
  } else {
1272
    set_errmsg("Index in cells array is out of bounds.");
×
1273
    return OPENMC_E_OUT_OF_BOUNDS;
×
1274
  }
1275
  return 0;
1276
}
1277

1278
extern "C" int openmc_cell_set_temperature(
88 ✔
1279
  int32_t index, double T, const int32_t* instance, bool set_contained)
1280
{
1281
  if (index < 0 || index >= model::cells.size()) {
88 !
1282
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1283
    return OPENMC_E_OUT_OF_BOUNDS;
×
1284
  }
1285

1286
  int32_t instance_index = instance ? *instance : -1;
88 ✔
1287
  try {
88 ✔
1288
    model::cells[index]->set_temperature(T, instance_index, set_contained);
88 ✔
1289
  } catch (const std::exception& e) {
×
1290
    set_errmsg(e.what());
×
1291
    return OPENMC_E_UNASSIGNED;
×
1292
  }
×
1293
  return 0;
1294
}
1295

1296
extern "C" int openmc_cell_set_density(
88 ✔
1297
  int32_t index, double density, const int32_t* instance, bool set_contained)
1298
{
1299
  if (index < 0 || index >= model::cells.size()) {
88 !
1300
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1301
    return OPENMC_E_OUT_OF_BOUNDS;
×
1302
  }
1303

1304
  int32_t instance_index = instance ? *instance : -1;
88 ✔
1305
  try {
88 ✔
1306
    model::cells[index]->set_density(density, instance_index, set_contained);
88 ✔
1307
  } catch (const std::exception& e) {
×
1308
    set_errmsg(e.what());
×
1309
    return OPENMC_E_UNASSIGNED;
×
1310
  }
×
1311
  return 0;
1312
}
1313

1314
extern "C" int openmc_cell_get_temperature(
9,628 ✔
1315
  int32_t index, const int32_t* instance, double* T)
1316
{
1317
  if (index < 0 || index >= model::cells.size()) {
9,628 !
1318
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1319
    return OPENMC_E_OUT_OF_BOUNDS;
×
1320
  }
1321

1322
  int32_t instance_index = instance ? *instance : -1;
9,628 ✔
1323
  try {
9,628 ✔
1324
    *T = model::cells[index]->temperature(instance_index);
9,628 ✔
1325
  } catch (const std::exception& e) {
×
1326
    set_errmsg(e.what());
×
1327
    return OPENMC_E_UNASSIGNED;
×
1328
  }
×
1329
  return 0;
9,628 ✔
1330
}
1331

1332
extern "C" int openmc_cell_get_density(
88 ✔
1333
  int32_t index, const int32_t* instance, double* density)
1334
{
1335
  if (index < 0 || index >= model::cells.size()) {
88 !
1336
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1337
    return OPENMC_E_OUT_OF_BOUNDS;
×
1338
  }
1339

1340
  int32_t instance_index = instance ? *instance : -1;
88 ✔
1341
  try {
88 ✔
1342
    if (model::cells[index]->type_ != Fill::MATERIAL) {
88 !
1343
      fatal_error(
×
1344
        fmt::format("Cell {}, instance {} is not filled with a material.",
×
1345
          model::cells[index]->id_, instance_index));
×
1346
    }
1347

1348
    int32_t mat_index = model::cells[index]->material(instance_index);
88 !
1349
    if (mat_index == MATERIAL_VOID) {
88 !
1350
      *density = 0.0;
×
1351
    } else {
1352
      *density = model::cells[index]->density_mult(instance_index) *
88 ✔
1353
                 model::materials[mat_index]->density_gpcc();
176 !
1354
    }
1355
  } catch (const std::exception& e) {
×
1356
    set_errmsg(e.what());
×
1357
    return OPENMC_E_UNASSIGNED;
×
1358
  }
×
1359
  return 0;
1360
}
1361

1362
//! Get the bounding box of a cell
1363
extern "C" int openmc_cell_bounding_box(
55 ✔
1364
  const int32_t index, double* llc, double* urc)
1365
{
1366

1367
  BoundingBox bbox;
55 ✔
1368

1369
  const auto& c = model::cells[index];
55 ✔
1370
  bbox = c->bounding_box();
55 ✔
1371

1372
  // set lower left corner values
1373
  llc[0] = bbox.min.x;
55 ✔
1374
  llc[1] = bbox.min.y;
55 ✔
1375
  llc[2] = bbox.min.z;
55 ✔
1376

1377
  // set upper right corner values
1378
  urc[0] = bbox.max.x;
55 ✔
1379
  urc[1] = bbox.max.y;
55 ✔
1380
  urc[2] = bbox.max.z;
55 ✔
1381

1382
  return 0;
55 ✔
1383
}
1384

1385
//! Get the name of a cell
1386
extern "C" int openmc_cell_get_name(int32_t index, const char** name)
413 ✔
1387
{
1388
  if (index < 0 || index >= model::cells.size()) {
413 !
1389
    set_errmsg("Index in cells array is out of bounds.");
×
1390
    return OPENMC_E_OUT_OF_BOUNDS;
×
1391
  }
1392

1393
  *name = model::cells[index]->name().data();
413 ✔
1394

1395
  return 0;
413 ✔
1396
}
1397

1398
//! Set the name of a cell
1399
extern "C" int openmc_cell_set_name(int32_t index, const char* name)
11 ✔
1400
{
1401
  if (index < 0 || index >= model::cells.size()) {
11 !
1402
    set_errmsg("Index in cells array is out of bounds.");
×
1403
    return OPENMC_E_OUT_OF_BOUNDS;
×
1404
  }
1405

1406
  model::cells[index]->set_name(name);
22 ✔
1407

1408
  return 0;
11 ✔
1409
}
1410

1411
//==============================================================================
1412
//! Define a containing (parent) cell
1413
//==============================================================================
1414

1415
//! Used to locate a universe fill in the geometry
1416
struct ParentCell {
1417
  bool operator==(const ParentCell& other) const
135 ✔
1418
  {
1419
    return cell_index == other.cell_index &&
135 !
1420
           lattice_index == other.lattice_index;
135 !
1421
  }
1422

1423
  bool operator<(const ParentCell& other) const
1424
  {
1425
    return cell_index < other.cell_index ||
1426
           (cell_index == other.cell_index &&
1427
             lattice_index < other.lattice_index);
1428
  }
1429

1430
  int64_t cell_index;
1431
  int64_t lattice_index;
1432
};
1433

1434
//! Structure used to insert ParentCell into hashed STL data structures
1435
struct ParentCellHash {
1436
  std::size_t operator()(const ParentCell& p) const
661 ✔
1437
  {
1438
    return 4096 * p.cell_index + p.lattice_index;
661 !
1439
  }
1440
};
1441

1442
//! Used to manage a traversal stack when locating parent cells of a cell
1443
//! instance in the model
1444
struct ParentCellStack {
136 ✔
1445

1446
  //! push method that adds to the parent_cells visited cells for this search
1447
  //! universe
1448
  void push(int32_t search_universe, const ParentCell& pc)
105 ✔
1449
  {
1450
    parent_cells_.push_back(pc);
105 ✔
1451
    // add parent cell to the set of cells we've visited for this search
1452
    // universe
1453
    visited_cells_[search_universe].insert(pc);
105 ✔
1454
  }
105 ✔
1455

1456
  //! removes the last parent_cell and clears the visited cells for the popped
1457
  //! cell's universe
1458
  void pop()
75 ✔
1459
  {
1460
    visited_cells_[this->current_univ()].clear();
75 ✔
1461
    parent_cells_.pop_back();
75 ✔
1462
  }
75 ✔
1463

1464
  //! checks whether or not the parent cell has been visited already for this
1465
  //! search universe
1466
  bool visited(int32_t search_universe, const ParentCell& parent_cell)
556 ✔
1467
  {
1468
    return visited_cells_[search_universe].count(parent_cell) != 0;
556 ✔
1469
  }
1470

1471
  //! return the next universe to search for a parent cell
1472
  int32_t current_univ() const
75 ✔
1473
  {
1474
    return model::cells[parent_cells_.back().cell_index]->universe_;
75 ✔
1475
  }
1476

1477
  //! indicates whether nor not parent cells are present on the stack
1478
  bool empty() const { return parent_cells_.empty(); }
75 ✔
1479

1480
  //! compute an instance for the provided distribcell index
1481
  int32_t compute_instance(int32_t distribcell_index) const
211 ✔
1482
  {
1483
    if (distribcell_index == C_NONE)
211 ✔
1484
      return 0;
1485

1486
    int32_t instance = 0;
120 ✔
1487
    for (const auto& parent_cell : this->parent_cells_) {
225 ✔
1488
      auto& cell = model::cells[parent_cell.cell_index];
105 !
1489
      if (cell->type_ == Fill::UNIVERSE) {
105 !
1490
        instance += cell->offset_[distribcell_index];
×
1491
      } else if (cell->type_ == Fill::LATTICE) {
105 !
1492
        auto& lattice = model::lattices[cell->fill_];
105 ✔
1493
        instance +=
105 ✔
1494
          lattice->offset(distribcell_index, parent_cell.lattice_index);
105 ✔
1495
      }
1496
    }
1497
    return instance;
1498
  }
1499

1500
  // Accessors
1501
  vector<ParentCell>& parent_cells() { return parent_cells_; }
136 ✔
1502
  const vector<ParentCell>& parent_cells() const { return parent_cells_; }
1503

1504
  // Data Members
1505
  vector<ParentCell> parent_cells_;
1506
  std::unordered_map<int32_t, std::unordered_set<ParentCell, ParentCellHash>>
1507
    visited_cells_;
1508
};
1509

1510
vector<ParentCell> Cell::find_parent_cells(
×
1511
  int32_t instance, const Position& r) const
1512
{
1513

1514
  // create a temporary particle
1515
  GeometryState dummy_particle {};
×
1516
  dummy_particle.r() = r;
×
1517
  dummy_particle.u() = {0., 0., 1.};
×
1518

1519
  return find_parent_cells(instance, dummy_particle);
×
1520
}
×
1521

1522
vector<ParentCell> Cell::find_parent_cells(
×
1523
  int32_t instance, GeometryState& p) const
1524
{
1525
  // look up the particle's location
1526
  exhaustive_find_cell(p);
×
1527
  const auto& coords = p.coord();
×
1528

1529
  // build a parent cell stack from the particle coordinates
1530
  ParentCellStack stack;
×
1531
  bool cell_found = false;
×
1532
  for (auto it = coords.begin(); it != coords.end(); it++) {
×
1533
    const auto& coord = *it;
×
1534
    const auto& cell = model::cells[coord.cell()];
×
1535
    // if the cell at this level matches the current cell, stop adding to the
1536
    // stack
1537
    if (coord.cell() == model::cell_map[this->id_]) {
×
1538
      cell_found = true;
1539
      break;
1540
    }
1541

1542
    // if filled with a lattice, get the lattice index from the next
1543
    // level in the coordinates to push to the stack
1544
    int lattice_idx = C_NONE;
×
1545
    if (cell->type_ == Fill::LATTICE) {
×
1546
      const auto& next_coord = *(it + 1);
×
1547
      lattice_idx = model::lattices[next_coord.lattice()]->get_flat_index(
×
1548
        next_coord.lattice_index());
1549
    }
1550
    stack.push(coord.universe(), {coord.cell(), lattice_idx});
×
1551
  }
1552

1553
  // if this loop finished because the cell was found and
1554
  // the instance matches the one requested in the call
1555
  // we have the correct path and can return the stack
1556
  if (cell_found &&
×
1557
      stack.compute_instance(this->distribcell_index_) == instance) {
×
1558
    return stack.parent_cells();
×
1559
  }
1560

1561
  // fall back on an exhaustive search for the cell's parents
1562
  return exhaustive_find_parent_cells(instance);
×
1563
}
×
1564

1565
vector<ParentCell> Cell::exhaustive_find_parent_cells(int32_t instance) const
136 ✔
1566
{
1567
  ParentCellStack stack;
136 ✔
1568
  // start with this cell's universe
1569
  int32_t prev_univ_idx;
136 ✔
1570
  int32_t univ_idx = this->universe_;
136 ✔
1571

1572
  while (true) {
211 ✔
1573
    const auto& univ = model::universes[univ_idx];
211 ✔
1574
    prev_univ_idx = univ_idx;
211 ✔
1575

1576
    // search for a cell that is filled w/ this universe
1577
    for (const auto& cell : model::cells) {
1,429 ✔
1578
      // if this is a material-filled cell, move on
1579
      if (cell->type_ == Fill::MATERIAL)
1,323 ✔
1580
        continue;
782 ✔
1581

1582
      if (cell->type_ == Fill::UNIVERSE) {
541 ✔
1583
        // if this is in the set of cells previously visited for this universe,
1584
        // move on
1585
        if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE}))
316 !
1586
          continue;
×
1587

1588
        // if this cell contains the universe we're searching for, add it to the
1589
        // stack
1590
        if (cell->fill_ == univ_idx) {
316 !
1591
          stack.push(univ_idx, {model::cell_map[cell->id_], C_NONE});
×
1592
          univ_idx = cell->universe_;
×
1593
        }
1594
      } else if (cell->type_ == Fill::LATTICE) {
225 !
1595
        // retrieve the lattice and lattice universes
1596
        const auto& lattice = model::lattices[cell->fill_];
225 ✔
1597
        const auto& lattice_univs = lattice->universes_;
225 ✔
1598

1599
        // start search for universe
1600
        auto lat_it = lattice_univs.begin();
225 ✔
1601
        while (true) {
495 ✔
1602
          // find the next lattice cell with this universe
1603
          lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
360 ✔
1604
          if (lat_it == lattice_univs.end())
360 ✔
1605
            break;
1606

1607
          int lattice_idx = lat_it - lattice_univs.begin();
240 ✔
1608

1609
          // move iterator forward one to avoid finding the same entry
1610
          lat_it++;
240 ✔
1611
          if (stack.visited(
480 ✔
1612
                univ_idx, {model::cell_map[cell->id_], lattice_idx}))
240 ✔
1613
            continue;
135 ✔
1614

1615
          // add this cell and lattice index to the stack and exit loop
1616
          stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
105 ✔
1617
          univ_idx = cell->universe_;
105 ✔
1618
          break;
105 ✔
1619
        }
135 ✔
1620
      }
1621
      // if we've updated the universe, break
1622
      if (prev_univ_idx != univ_idx)
541 ✔
1623
        break;
1624
    } // end cell loop search for universe
1625

1626
    // if we're at the top of the geometry and the instance matches, we're done
1627
    if (univ_idx == model::root_universe &&
253 !
1628
        stack.compute_instance(this->distribcell_index_) == instance)
211 ✔
1629
      break;
1630

1631
    // if there is no match on the original cell's universe, report an error
1632
    if (univ_idx == this->universe_) {
75 !
1633
      fatal_error(
×
1634
        fmt::format("Could not find the parent cells for cell {}, instance {}.",
×
1635
          this->id_, instance));
×
1636
    }
1637

1638
    // if we don't find a suitable update, adjust the stack and continue
1639
    if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
75 !
1640
      stack.pop();
75 ✔
1641
      univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
75 !
1642
    }
1643

1644
  } // end while
1645

1646
  // reverse the stack so the highest cell comes first
1647
  std::reverse(stack.parent_cells().begin(), stack.parent_cells().end());
136 ✔
1648
  return stack.parent_cells();
272 ✔
1649
}
136 ✔
1650

1651
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
181 ✔
1652
  int32_t instance, Position* hint) const
1653
{
1654
  std::unordered_map<int32_t, vector<int32_t>> contained_cells;
181 ✔
1655

1656
  // if this is a material-filled cell it has no contained cells
1657
  if (this->type_ == Fill::MATERIAL)
181 ✔
1658
    return contained_cells;
1659

1660
  // find the pathway through the geometry to this cell
1661
  vector<ParentCell> parent_cells;
136 !
1662

1663
  // if a positional hint is provided, attempt to do a fast lookup
1664
  // of the parent cells
1665
  parent_cells = hint ? find_parent_cells(instance, *hint)
136 !
1666
                      : exhaustive_find_parent_cells(instance);
136 ✔
1667

1668
  // if this cell is filled w/ a material, it contains no other cells
1669
  if (type_ != Fill::MATERIAL) {
136 !
1670
    this->get_contained_cells_inner(contained_cells, parent_cells);
136 ✔
1671
  }
1672

1673
  return contained_cells;
136 ✔
1674
}
181 ✔
1675

1676
//! Get all cells within this cell
1677
void Cell::get_contained_cells_inner(
134,178 ✔
1678
  std::unordered_map<int32_t, vector<int32_t>>& contained_cells,
1679
  vector<ParentCell>& parent_cells) const
1680
{
1681

1682
  // filled by material, determine instance based on parent cells
1683
  if (type_ == Fill::MATERIAL) {
134,178 ✔
1684
    int instance = 0;
133,532 ✔
1685
    if (this->distribcell_index_ >= 0) {
133,532 !
1686
      for (auto& parent_cell : parent_cells) {
400,594 ✔
1687
        auto& cell = model::cells[parent_cell.cell_index];
267,062 ✔
1688
        if (cell->type_ == Fill::UNIVERSE) {
267,062 ✔
1689
          instance += cell->offset_[distribcell_index_];
132,032 ✔
1690
        } else if (cell->type_ == Fill::LATTICE) {
135,030 !
1691
          auto& lattice = model::lattices[cell->fill_];
135,030 ✔
1692
          instance += lattice->offset(
135,030 ✔
1693
            this->distribcell_index_, parent_cell.lattice_index);
135,030 ✔
1694
        }
1695
      }
1696
    }
1697
    // add entry to contained cells
1698
    contained_cells[model::cell_map[id_]].push_back(instance);
133,532 ✔
1699
    // filled with universe, add the containing cell to the parent cells
1700
    // and recurse
1701
  } else if (type_ == Fill::UNIVERSE) {
646 ✔
1702
    parent_cells.push_back({model::cell_map[id_], -1});
526 ✔
1703
    auto& univ = model::universes[fill_];
526 ✔
1704
    for (auto cell_index : univ->cells_) {
3,033 ✔
1705
      auto& cell = model::cells[cell_index];
2,507 ✔
1706
      cell->get_contained_cells_inner(contained_cells, parent_cells);
2,507 ✔
1707
    }
1708
    parent_cells.pop_back();
526 ✔
1709
    // filled with a lattice, visit each universe in the lattice
1710
    // with a recursive call to collect the cell instances
1711
  } else if (type_ == Fill::LATTICE) {
120 !
1712
    auto& lattice = model::lattices[fill_];
120 ✔
1713
    for (auto i = lattice->begin(); i != lattice->end(); ++i) {
131,340 ✔
1714
      auto& univ = model::universes[*i];
131,220 ✔
1715
      parent_cells.push_back({model::cell_map[id_], i.indx_});
131,220 ✔
1716
      for (auto cell_index : univ->cells_) {
262,755 ✔
1717
        auto& cell = model::cells[cell_index];
131,535 ✔
1718
        cell->get_contained_cells_inner(contained_cells, parent_cells);
131,535 ✔
1719
      }
1720
      parent_cells.pop_back();
131,220 ✔
1721
    }
1722
  }
1723
}
134,178 ✔
1724

1725
//! Return the index in the cells array of a cell with a given ID
1726
extern "C" int openmc_get_cell_index(int32_t id, int32_t* index)
1,021 ✔
1727
{
1728
  auto it = model::cell_map.find(id);
1,021 ✔
1729
  if (it != model::cell_map.end()) {
1,021 ✔
1730
    *index = it->second;
1,010 ✔
1731
    return 0;
1,010 ✔
1732
  } else {
1733
    set_errmsg("No cell exists with ID=" + std::to_string(id) + ".");
11 ✔
1734
    return OPENMC_E_INVALID_ID;
11 ✔
1735
  }
1736
}
1737

1738
//! Return the ID of a cell
1739
extern "C" int openmc_cell_get_id(int32_t index, int32_t* id)
602,407 ✔
1740
{
1741
  if (index >= 0 && index < model::cells.size()) {
602,407 !
1742
    *id = model::cells[index]->id_;
602,407 ✔
1743
    return 0;
602,407 ✔
1744
  } else {
1745
    set_errmsg("Index in cells array is out of bounds.");
×
1746
    return OPENMC_E_OUT_OF_BOUNDS;
×
1747
  }
1748
}
1749

1750
//! Set the ID of a cell
1751
extern "C" int openmc_cell_set_id(int32_t index, int32_t id)
22 ✔
1752
{
1753
  if (index >= 0 && index < model::cells.size()) {
22 !
1754
    model::cells[index]->id_ = id;
22 ✔
1755
    model::cell_map[id] = index;
22 ✔
1756
    return 0;
22 ✔
1757
  } else {
1758
    set_errmsg("Index in cells array is out of bounds.");
×
1759
    return OPENMC_E_OUT_OF_BOUNDS;
×
1760
  }
1761
}
1762

1763
//! Return the translation vector of a cell
1764
extern "C" int openmc_cell_get_translation(int32_t index, double xyz[])
55 ✔
1765
{
1766
  if (index >= 0 && index < model::cells.size()) {
55 !
1767
    auto& cell = model::cells[index];
55 ✔
1768
    xyz[0] = cell->translation_.x;
55 ✔
1769
    xyz[1] = cell->translation_.y;
55 ✔
1770
    xyz[2] = cell->translation_.z;
55 ✔
1771
    return 0;
55 ✔
1772
  } else {
1773
    set_errmsg("Index in cells array is out of bounds.");
×
1774
    return OPENMC_E_OUT_OF_BOUNDS;
×
1775
  }
1776
}
1777

1778
//! Set the translation vector of a cell
1779
extern "C" int openmc_cell_set_translation(int32_t index, const double xyz[])
55 ✔
1780
{
1781
  if (index >= 0 && index < model::cells.size()) {
55 !
1782
    if (model::cells[index]->fill_ == C_NONE) {
55 ✔
1783
      set_errmsg(fmt::format("Cannot apply a translation to cell {}"
11 ✔
1784
                             " because it is not filled with another universe",
1785
        index));
1786
      return OPENMC_E_GEOMETRY;
11 ✔
1787
    }
1788
    model::cells[index]->translation_ = Position(xyz);
44 ✔
1789
    return 0;
44 ✔
1790
  } else {
1791
    set_errmsg("Index in cells array is out of bounds.");
×
1792
    return OPENMC_E_OUT_OF_BOUNDS;
×
1793
  }
1794
}
1795

1796
//! Return the rotation matrix of a cell
1797
extern "C" int openmc_cell_get_rotation(int32_t index, double rot[], size_t* n)
55 ✔
1798
{
1799
  if (index >= 0 && index < model::cells.size()) {
55 !
1800
    auto& cell = model::cells[index];
55 ✔
1801
    *n = cell->rotation_.size();
55 ✔
1802
    std::memcpy(rot, cell->rotation_.data(), *n * sizeof(cell->rotation_[0]));
55 ✔
1803
    return 0;
55 ✔
1804
  } else {
1805
    set_errmsg("Index in cells array is out of bounds.");
×
1806
    return OPENMC_E_OUT_OF_BOUNDS;
×
1807
  }
1808
}
1809

1810
//! Set the flattened rotation matrix of a cell
1811
extern "C" int openmc_cell_set_rotation(
66 ✔
1812
  int32_t index, const double rot[], size_t rot_len)
1813
{
1814
  if (index >= 0 && index < model::cells.size()) {
66 !
1815
    if (model::cells[index]->fill_ == C_NONE) {
66 ✔
1816
      set_errmsg(fmt::format("Cannot apply a rotation to cell {}"
11 ✔
1817
                             " because it is not filled with another universe",
1818
        index));
1819
      return OPENMC_E_GEOMETRY;
11 ✔
1820
    }
1821
    std::vector<double> vec_rot(rot, rot + rot_len);
55 ✔
1822
    model::cells[index]->set_rotation(vec_rot);
55 ✔
1823
    return 0;
55 ✔
1824
  } else {
66 ✔
1825
    set_errmsg("Index in cells array is out of bounds.");
×
1826
    return OPENMC_E_OUT_OF_BOUNDS;
×
1827
  }
1828
}
1829

1830
//! Get the number of instances of the requested cell
1831
extern "C" int openmc_cell_get_num_instances(
77 ✔
1832
  int32_t index, int32_t* num_instances)
1833
{
1834
  if (index < 0 || index >= model::cells.size()) {
77 !
1835
    set_errmsg("Index in cells array is out of bounds.");
×
1836
    return OPENMC_E_OUT_OF_BOUNDS;
×
1837
  }
1838
  *num_instances = model::cells[index]->n_instances();
77 ✔
1839
  return 0;
77 ✔
1840
}
1841

1842
//! Extend the cells array by n elements
1843
extern "C" int openmc_extend_cells(
22 ✔
1844
  int32_t n, int32_t* index_start, int32_t* index_end)
1845
{
1846
  if (index_start)
22 !
1847
    *index_start = model::cells.size();
22 ✔
1848
  if (index_end)
22 !
1849
    *index_end = model::cells.size() + n - 1;
×
1850
  for (int32_t i = 0; i < n; i++) {
44 ✔
1851
    model::cells.push_back(make_unique<CSGCell>());
22 ✔
1852
  }
1853
  return 0;
22 ✔
1854
}
1855

1856
extern "C" int cells_size()
99 ✔
1857
{
1858
  return model::cells.size();
99 ✔
1859
}
1860

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