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

openmc-dev / openmc / 21116084326

18 Jan 2026 05:53PM UTC coverage: 82.04% (-0.004%) from 82.044%
21116084326

Pull #3732

github

web-flow
Merge 8a0f48648 into 5847b0de2
Pull Request #3732: Volume Calculation enhancement including refactoring and real-valued scoring implementation

17295 of 24049 branches covered (71.92%)

Branch coverage included in aggregate %.

302 of 306 new or added lines in 5 files covered. (98.69%)

96 existing lines in 3 files now uncovered.

55771 of 65012 relevant lines covered (85.79%)

43326460.86 hits per line

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

75.73
/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
5,042✔
44
{
45
  return model::universes[universe_]->n_instances_;
5,042✔
46
}
47

48
void Cell::set_rotation(const vector<double>& rot)
449✔
49
{
50
  if (fill_ == C_NONE) {
449!
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) {
449!
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();
449✔
62
  rotation_.reserve(rot.size() == 9 ? 9 : 12);
449!
63
  if (rot.size() == 3) {
449!
64
    double phi = -rot[0] * PI / 180.0;
449✔
65
    double theta = -rot[1] * PI / 180.0;
449✔
66
    double psi = -rot[2] * PI / 180.0;
449✔
67
    rotation_.push_back(std::cos(theta) * std::cos(psi));
449✔
68
    rotation_.push_back(-std::cos(phi) * std::sin(psi) +
×
69
                        std::sin(phi) * std::sin(theta) * std::cos(psi));
449✔
70
    rotation_.push_back(std::sin(phi) * std::sin(psi) +
×
71
                        std::cos(phi) * std::sin(theta) * std::cos(psi));
449✔
72
    rotation_.push_back(std::cos(theta) * std::sin(psi));
449✔
73
    rotation_.push_back(std::cos(phi) * std::cos(psi) +
×
74
                        std::sin(phi) * std::sin(theta) * std::sin(psi));
449✔
75
    rotation_.push_back(-std::sin(phi) * std::cos(psi) +
×
76
                        std::cos(phi) * std::sin(theta) * std::sin(psi));
449✔
77
    rotation_.push_back(-std::sin(theta));
449✔
78
    rotation_.push_back(std::sin(phi) * std::cos(theta));
449✔
79
    rotation_.push_back(std::cos(phi) * std::cos(theta));
449✔
80

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

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

96
  if (instance >= 0) {
97✔
97
    double sqrtkT = sqrtkT_.size() == 1 ? sqrtkT_.at(0) : sqrtkT_.at(instance);
11!
98
    return sqrtkT * sqrtkT / K_BOLTZMANN;
11✔
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);
2,147,483,647✔
109
  } else {
110
    return density_mult_[0];
77✔
111
  }
112
}
113

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

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

123
void Cell::set_temperature(double T, int32_t instance, bool set_contained)
492✔
124
{
125
  if (settings::temperature_method == TemperatureMethod::INTERPOLATION) {
492!
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) {
492✔
140
    if (instance >= 0) {
460✔
141
      // If temperature vector is not big enough, resize it first
142
      if (sqrtkT_.size() != n_instances())
383✔
143
        sqrtkT_.resize(n_instances(), sqrtkT_[0]);
48✔
144

145
      // Set temperature for the corresponding instance
146
      sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T);
383✔
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) {
32!
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);
32✔
162
    for (const auto& entry : contained_cells) {
128✔
163
      auto& cell = model::cells[entry.first];
96✔
164
      assert(cell->type_ == Fill::MATERIAL);
78!
165
      auto& instances = entry.second;
96✔
166
      for (auto instance : instances) {
336✔
167
        cell->set_temperature(T, instance);
240✔
168
      }
169
    }
170
  }
32✔
171
}
492✔
172

173
void Cell::set_density(double density, int32_t instance, bool set_contained)
284✔
174
{
175
  if (type_ != Fill::MATERIAL && !set_contained) {
284!
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) {
284✔
183
    const int32_t mat_index = material(instance);
268✔
184
    if (mat_index == MATERIAL_VOID)
268!
185
      return;
×
186

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

192
      // Set density multiplier for the corresponding instance
193
      density_mult_.at(instance) =
191✔
194
        density / model::materials[mat_index]->density_gpcc();
191✔
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();
77✔
199
      }
200
    }
201
  } else {
202
    auto contained_cells = this->get_contained_cells(instance);
16✔
203
    for (const auto& entry : contained_cells) {
64✔
204
      auto& cell = model::cells[entry.first];
48✔
205
      assert(cell->type_ == Fill::MATERIAL);
39!
206
      auto& instances = entry.second;
48✔
207
      for (auto instance : instances) {
96✔
208
        cell->set_density(density, instance);
48✔
209
      }
210
    }
211
  }
16✔
212
}
213

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

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

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

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

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

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

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

245
  // Ensure number of temperatures makes sense
246
  auto n_temps = temps.size();
176✔
247
  if (n_temps > 1 && n_temps != n_instances()) {
176!
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();
176✔
255
  sqrtkT_.resize(temps.size());
176✔
256
  for (int64_t i = 0; i < temps.size(); ++i) {
308✔
257
    this->set_temperature(temps[i], i);
132✔
258
  }
259

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

265
    // Ensure number of densities makes sense
266
    auto n_density = density.size();
132✔
267
    if (n_density > 1 && n_density != n_instances()) {
132!
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) {
264✔
275
      this->set_density(density[i], i);
132✔
276
    }
277
  }
132✔
278

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

282
void Cell::to_hdf5(hid_t cell_group) const
27,355✔
283
{
284

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

288
  if (!name_.empty()) {
27,355✔
289
    write_string(group, "name", name_, false);
5,373✔
290
  }
291

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

294
  to_hdf5_inner(group);
27,355✔
295

296
  // Write fill information.
297
  if (type_ == Fill::MATERIAL) {
27,355✔
298
    write_dataset(group, "fill_type", "material");
22,196✔
299
    std::vector<int32_t> mat_ids;
22,196✔
300
    for (auto i_mat : material_) {
45,621✔
301
      if (i_mat != MATERIAL_VOID) {
23,425✔
302
        mat_ids.push_back(model::materials[i_mat]->id_);
15,136✔
303
      } else {
304
        mat_ids.push_back(MATERIAL_VOID);
8,289✔
305
      }
306
    }
307
    if (mat_ids.size() == 1) {
22,196✔
308
      write_dataset(group, "material", mat_ids[0]);
22,013✔
309
    } else {
310
      write_dataset(group, "material", mat_ids);
183✔
311
    }
312

313
    std::vector<double> temps;
22,196✔
314
    for (auto sqrtkT_val : sqrtkT_)
45,744✔
315
      temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
23,548✔
316
    write_dataset(group, "temperature", temps);
22,196✔
317

318
    write_dataset(group, "density_mult", density_mult_);
22,196✔
319

320
  } else if (type_ == Fill::UNIVERSE) {
27,355✔
321
    write_dataset(group, "fill_type", "universe");
3,750✔
322
    write_dataset(group, "fill", model::universes[fill_]->id_);
3,750✔
323
    if (translation_ != Position(0, 0, 0)) {
3,750✔
324
      write_dataset(group, "translation", translation_);
1,837✔
325
    }
326
    if (!rotation_.empty()) {
3,750✔
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,409!
336
    write_dataset(group, "fill_type", "lattice");
1,409✔
337
    write_dataset(group, "lattice", model::lattices[fill_]->id_);
1,409✔
338
  }
339

340
  close_group(group);
27,355✔
341
}
27,355✔
342

343
//==============================================================================
344
// CSGCell implementation
345
//==============================================================================
346

347
CSGCell::CSGCell(pugi::xml_node cell_node)
33,898✔
348
{
349
  if (check_for_node(cell_node, "id")) {
33,898!
350
    id_ = std::stoi(get_node_value(cell_node, "id"));
33,898✔
351
  } else {
352
    fatal_error("Must specify id of cell in geometry XML file.");
×
353
  }
354

355
  if (check_for_node(cell_node, "name")) {
33,898✔
356
    name_ = get_node_value(cell_node, "name");
7,473✔
357
  }
358

359
  if (check_for_node(cell_node, "universe")) {
33,898✔
360
    universe_ = std::stoi(get_node_value(cell_node, "universe"));
32,691✔
361
  } else {
362
    universe_ = 0;
1,207✔
363
  }
364

365
  // Make sure that either material or fill was specified, but not both.
366
  bool fill_present = check_for_node(cell_node, "fill");
33,898✔
367
  bool material_present = check_for_node(cell_node, "material");
33,898✔
368
  if (!(fill_present || material_present)) {
33,898!
369
    fatal_error(
×
370
      fmt::format("Neither material nor fill was specified for cell {}", id_));
×
371
  }
372
  if (fill_present && material_present) {
33,898!
373
    fatal_error(fmt::format("Cell {} has both a material and a fill specified; "
×
374
                            "only one can be specified per cell",
375
      id_));
×
376
  }
377

378
  if (fill_present) {
33,898✔
379
    fill_ = std::stoi(get_node_value(cell_node, "fill"));
7,212✔
380
    if (fill_ == universe_) {
7,212!
381
      fatal_error(fmt::format("Cell {} is filled with the same universe that "
×
382
                              "it is contained in.",
383
        id_));
×
384
    }
385
  } else {
386
    fill_ = C_NONE;
26,686✔
387
  }
388

389
  // Read the material element.  There can be zero materials (filled with a
390
  // universe), more than one material (distribmats), and some materials may
391
  // be "void".
392
  if (material_present) {
33,898✔
393
    vector<std::string> mats {
394
      get_node_array<std::string>(cell_node, "material", true)};
26,686✔
395
    if (mats.size() > 0) {
26,686!
396
      material_.reserve(mats.size());
26,686✔
397
      for (std::string mat : mats) {
54,637✔
398
        if (mat.compare("void") == 0) {
27,951✔
399
          material_.push_back(MATERIAL_VOID);
8,668✔
400
        } else {
401
          material_.push_back(std::stoi(mat));
19,283✔
402
        }
403
      }
27,951✔
404
    } else {
405
      fatal_error(fmt::format(
×
406
        "An empty material element was specified for cell {}", id_));
×
407
    }
408
  }
26,686✔
409

410
  // Read the temperature element which may be distributed like materials.
411
  if (check_for_node(cell_node, "temperature")) {
33,898✔
412
    sqrtkT_ = get_node_array<double>(cell_node, "temperature");
315✔
413
    sqrtkT_.shrink_to_fit();
315✔
414

415
    // Make sure this is a material-filled cell.
416
    if (material_.size() == 0) {
315!
417
      fatal_error(fmt::format(
×
418
        "Cell {} was specified with a temperature but no material. Temperature"
419
        "specification is only valid for cells filled with a material.",
420
        id_));
×
421
    }
422

423
    // Make sure all temperatures are non-negative.
424
    for (auto T : sqrtkT_) {
678✔
425
      if (T < 0) {
363!
426
        fatal_error(fmt::format(
×
427
          "Cell {} was specified with a negative temperature", id_));
×
428
      }
429
    }
430

431
    // Convert to sqrt(k*T).
432
    for (auto& T : sqrtkT_) {
678✔
433
      T = std::sqrt(K_BOLTZMANN * T);
363✔
434
    }
435
  }
436

437
  // Read the density element which can be distributed similar to temperature.
438
  // These get assigned to the density multiplier, requiring a division by
439
  // the material density.
440
  // Note: calculating the actual density multiplier is deferred until materials
441
  // are finalized. density_mult_ contains the true density in the meantime.
442
  if (check_for_node(cell_node, "density")) {
33,898✔
443
    density_mult_ = get_node_array<double>(cell_node, "density");
80✔
444
    density_mult_.shrink_to_fit();
80✔
445

446
    // Make sure this is a material-filled cell.
447
    if (material_.size() == 0) {
80!
448
      fatal_error(fmt::format(
×
449
        "Cell {} was specified with a density but no material. Density"
450
        "specification is only valid for cells filled with a material.",
451
        id_));
×
452
    }
453

454
    // Make sure this is a non-void material.
455
    for (auto mat_id : material_) {
160✔
456
      if (mat_id == MATERIAL_VOID) {
80!
457
        fatal_error(fmt::format(
×
458
          "Cell {} was specified with a density, but contains a void "
459
          "material. Density specification is only valid for cells "
460
          "filled with a non-void material.",
461
          id_));
×
462
      }
463
    }
464

465
    // Make sure all densities are non-negative and greater than zero.
466
    for (auto rho : density_mult_) {
1,312✔
467
      if (rho <= 0) {
1,232!
468
        fatal_error(fmt::format(
×
469
          "Cell {} was specified with a density less than or equal to zero",
470
          id_));
×
471
      }
472
    }
473
  }
474

475
  // Read the region specification.
476
  std::string region_spec;
33,898✔
477
  if (check_for_node(cell_node, "region")) {
33,898✔
478
    region_spec = get_node_value(cell_node, "region");
24,883✔
479
  }
480

481
  // Get a tokenized representation of the region specification and apply De
482
  // Morgans law
483
  Region region(region_spec, id_);
33,898✔
484
  region_ = region;
33,898✔
485

486
  // Read the translation vector.
487
  if (check_for_node(cell_node, "translation")) {
33,898✔
488
    if (fill_ == C_NONE) {
2,726!
489
      fatal_error(fmt::format("Cannot apply a translation to cell {}"
×
490
                              " because it is not filled with another universe",
491
        id_));
×
492
    }
493

494
    auto xyz {get_node_array<double>(cell_node, "translation")};
2,726✔
495
    if (xyz.size() != 3) {
2,726!
496
      fatal_error(
×
497
        fmt::format("Non-3D translation vector applied to cell {}", id_));
×
498
    }
499
    translation_ = xyz;
2,726✔
500
  }
2,726✔
501

502
  // Read the rotation transform.
503
  if (check_for_node(cell_node, "rotation")) {
33,898✔
504
    auto rot {get_node_array<double>(cell_node, "rotation")};
416✔
505
    set_rotation(rot);
416✔
506
  }
416✔
507
}
33,898✔
508

509
//==============================================================================
510

511
void CSGCell::to_hdf5_inner(hid_t group_id) const
27,195✔
512
{
513
  write_string(group_id, "geom_type", "csg", false);
27,195✔
514
  write_string(group_id, "region", region_.str(), false);
27,195✔
515
}
27,195✔
516

517
//==============================================================================
518

519
vector<int32_t>::iterator CSGCell::find_left_parenthesis(
×
520
  vector<int32_t>::iterator start, const vector<int32_t>& infix)
521
{
522
  // start search at zero
523
  int parenthesis_level = 0;
×
524
  auto it = start;
×
525
  while (it != infix.begin()) {
×
526
    // look at two tokens at a time
527
    int32_t one = *it;
×
528
    int32_t two = *(it - 1);
×
529

530
    // decrement parenthesis level if there are two adjacent surfaces
531
    if (one < OP_UNION && two < OP_UNION) {
×
532
      parenthesis_level--;
×
533
      // increment if there are two adjacent operators
534
    } else if (one >= OP_UNION && two >= OP_UNION) {
×
535
      parenthesis_level++;
×
536
    }
537

538
    // if the level gets to zero, return the position
539
    if (parenthesis_level == 0) {
×
540
      // move the iterator back one before leaving the loop
541
      // so that all tokens in the parenthesis block are included
542
      it--;
×
543
      break;
×
544
    }
545

546
    // continue loop, one token at a time
547
    it--;
×
548
  }
549
  return it;
×
550
}
551

552
//==============================================================================
553
// Region implementation
554
//==============================================================================
555

556
Region::Region(std::string region_spec, int32_t cell_id)
33,997✔
557
{
558
  // Check if region_spec is not empty.
559
  if (!region_spec.empty()) {
33,997✔
560
    // Parse all halfspaces and operators except for intersection (whitespace).
561
    for (int i = 0; i < region_spec.size();) {
146,174✔
562
      if (region_spec[i] == '(') {
121,192✔
563
        expression_.push_back(OP_LEFT_PAREN);
1,312✔
564
        i++;
1,312✔
565

566
      } else if (region_spec[i] == ')') {
119,880✔
567
        expression_.push_back(OP_RIGHT_PAREN);
1,312✔
568
        i++;
1,312✔
569

570
      } else if (region_spec[i] == '|') {
118,568✔
571
        expression_.push_back(OP_UNION);
3,896✔
572
        i++;
3,896✔
573

574
      } else if (region_spec[i] == '~') {
114,672✔
575
        expression_.push_back(OP_COMPLEMENT);
32✔
576
        i++;
32✔
577

578
      } else if (region_spec[i] == '-' || region_spec[i] == '+' ||
193,445!
579
                 std::isdigit(region_spec[i])) {
78,805✔
580
        // This is the start of a halfspace specification.  Iterate j until we
581
        // find the end, then push-back everything between i and j.
582
        int j = i + 1;
67,068✔
583
        while (j < region_spec.size() && std::isdigit(region_spec[j])) {
136,946✔
584
          j++;
69,878✔
585
        }
586
        expression_.push_back(std::stoi(region_spec.substr(i, j - i)));
67,068✔
587
        i = j;
67,068✔
588

589
      } else if (std::isspace(region_spec[i])) {
47,572!
590
        i++;
47,572✔
591

592
      } else {
593
        auto err_msg =
594
          fmt::format("Region specification contains invalid character, \"{}\"",
595
            region_spec[i]);
×
596
        fatal_error(err_msg);
×
597
      }
×
598
    }
599

600
    // Add in intersection operators where a missing operator is needed.
601
    int i = 0;
24,982✔
602
    while (i < expression_.size() - 1) {
111,810✔
603
      bool left_compat {
604
        (expression_[i] < OP_UNION) || (expression_[i] == OP_RIGHT_PAREN)};
86,828✔
605
      bool right_compat {(expression_[i + 1] < OP_UNION) ||
86,828✔
606
                         (expression_[i + 1] == OP_LEFT_PAREN) ||
92,100✔
607
                         (expression_[i + 1] == OP_COMPLEMENT)};
5,272✔
608
      if (left_compat && right_compat) {
86,828✔
609
        expression_.insert(expression_.begin() + i + 1, OP_INTERSECTION);
38,190✔
610
      }
611
      i++;
86,828✔
612
    }
613

614
    // Remove complement operators using DeMorgan's laws
615
    auto it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
24,982✔
616
    while (it != expression_.end()) {
25,014✔
617
      // Erase complement
618
      expression_.erase(it);
32✔
619

620
      // Define stop given left parenthesis or not
621
      auto stop = it;
32✔
622
      if (*it == OP_LEFT_PAREN) {
32!
623
        int depth = 1;
32✔
624
        do {
625
          stop++;
256✔
626
          if (*stop > OP_COMPLEMENT) {
256✔
627
            if (*stop == OP_RIGHT_PAREN) {
32!
628
              depth--;
32✔
629
            } else {
630
              depth++;
×
631
            }
632
          }
633
        } while (depth > 0);
256✔
634
        it++;
32✔
635
      }
636

637
      // apply DeMorgan's law to any surfaces/operators between these
638
      // positions in the RPN
639
      apply_demorgan(it, stop);
32✔
640
      // update iterator position
641
      it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
32✔
642
    }
643

644
    // Convert user IDs to surface indices.
645
    for (auto& r : expression_) {
136,760✔
646
      if (r < OP_UNION) {
111,778✔
647
        const auto& it {model::surface_map.find(abs(r))};
67,068✔
648
        if (it == model::surface_map.end()) {
67,068!
649
          throw std::runtime_error {
×
650
            "Invalid surface ID " + std::to_string(abs(r)) +
×
651
            " specified in region for cell " + std::to_string(cell_id) + "."};
×
652
        }
653
        r = (r > 0) ? it->second + 1 : -(it->second + 1);
67,068✔
654
      }
655
    }
656

657
    // Check if this is a simple cell.
658
    simple_ = true;
24,982✔
659
    for (int32_t token : expression_) {
123,493✔
660
      if (token == OP_UNION) {
99,724✔
661
        simple_ = false;
1,213✔
662
        // Ensure intersections have precedence over unions
663
        enforce_precedence();
1,213✔
664
        break;
1,213✔
665
      }
666
    }
667

668
    // If this cell is simple, remove all the superfluous operator tokens.
669
    if (simple_) {
24,982✔
670
      for (auto it = expression_.begin(); it != expression_.end(); it++) {
115,762✔
671
        if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT) {
91,993!
672
          expression_.erase(it);
34,112✔
673
          it--;
34,112✔
674
        }
675
      }
676
    }
677
    expression_.shrink_to_fit();
24,982✔
678

679
  } else {
680
    simple_ = true;
9,015✔
681
  }
682
}
33,997✔
683

684
//==============================================================================
685

686
void Region::apply_demorgan(
32✔
687
  vector<int32_t>::iterator start, vector<int32_t>::iterator stop)
688
{
689
  do {
690
    if (*start < OP_UNION) {
224✔
691
      *start *= -1;
128✔
692
    } else if (*start == OP_UNION) {
96!
693
      *start = OP_INTERSECTION;
×
694
    } else if (*start == OP_INTERSECTION) {
96!
695
      *start = OP_UNION;
96✔
696
    }
697
    start++;
224✔
698
  } while (start < stop);
224✔
699
}
32✔
700

701
//==============================================================================
702
//! Add precedence for infix regions so intersections have higher
703
//! precedence than unions using parentheses.
704
//==============================================================================
705

706
void Region::add_parentheses(int64_t start)
98✔
707
{
708
  int32_t start_token = expression_[start];
98✔
709
  // Add left parenthesis and set new position to be after parenthesis
710
  if (start_token == OP_UNION) {
98!
711
    start += 2;
×
712
  }
713
  expression_.insert(expression_.begin() + start - 1, OP_LEFT_PAREN);
98✔
714

715
  // Add right parenthesis
716
  // While the start iterator is within the bounds of infix
717
  while (start + 1 < expression_.size()) {
444✔
718
    start++;
422✔
719

720
    // If the current token is an operator and is different than the start token
721
    if (expression_[start] >= OP_UNION && expression_[start] != start_token) {
422✔
722
      // Skip wrapped regions but save iterator position to check precedence and
723
      // add right parenthesis, right parenthesis position depends on the
724
      // operator, when the operator is a union then do not include the operator
725
      // in the region, when the operator is an intersection then include the
726
      // operator and next surface
727
      if (expression_[start] == OP_LEFT_PAREN) {
87✔
728
        int depth = 1;
11✔
729
        do {
730
          start++;
44✔
731
          if (expression_[start] > OP_COMPLEMENT) {
44✔
732
            if (expression_[start] == OP_RIGHT_PAREN) {
11!
733
              depth--;
11✔
734
            } else {
UNCOV
735
              depth++;
×
736
            }
737
          }
738
        } while (depth > 0);
44✔
739
      } else {
740
        if (start_token == OP_UNION) {
76!
741
          --start;
×
742
        }
743
        expression_.insert(expression_.begin() + start, OP_RIGHT_PAREN);
76✔
744
        return;
76✔
745
      }
746
    }
747
  }
748
  // If we get here a right parenthesis hasn't been placed
749
  expression_.push_back(OP_RIGHT_PAREN);
22✔
750
}
751

752
//==============================================================================
753
//! Add parentheses to enforce operator precedence in region expressions
754
//!
755
//! This function ensures that intersection operators have higher precedence
756
//! than union operators by adding parentheses where needed. For example:
757
//!   "1 2 | 3" becomes "(1 2) | 3"
758
//!   "1 | 2 3" becomes "1 | (2 3)"
759
//!
760
//! The algorithm uses stacks to track the current operator type and its
761
//! position at each parenthesis depth level. When it encounters a different
762
//! operator at the same depth, it adds parentheses to group the
763
//! higher-precedence operations.
764
//==============================================================================
765

766
void Region::enforce_precedence()
1,213✔
767
{
768
  // Stack tracking the operator type at each depth (0 = no operator seen yet)
769
  vector<int32_t> op_stack = {0};
1,213✔
770

771
  // Stack tracking where the operator sequence started at each depth
772
  vector<std::size_t> pos_stack = {0};
1,213✔
773

774
  for (int64_t i = 0; i < expression_.size(); ++i) {
22,041✔
775
    int32_t token = expression_[i];
20,828✔
776

777
    if (token == OP_LEFT_PAREN) {
20,828✔
778
      // Entering a new parenthesis level - push new tracking state
779
      op_stack.push_back(0);
1,513✔
780
      pos_stack.push_back(0);
1,513✔
781
      continue;
1,513✔
782
    } else if (token == OP_RIGHT_PAREN) {
19,315✔
783
      // Exiting a parenthesis level - pop tracking state (keep at least one)
784
      if (op_stack.size() > 1) {
1,470!
785
        op_stack.pop_back();
1,470✔
786
        pos_stack.pop_back();
1,470✔
787
      }
788
      continue;
1,470✔
789
    }
790

791
    if (token == OP_UNION || token == OP_INTERSECTION) {
17,845✔
792
      if (op_stack.back() == 0) {
8,316✔
793
        // First operator at this depth - record it and its position
794
        op_stack.back() = token;
2,770✔
795
        pos_stack.back() = i;
2,770✔
796
      } else if (token != op_stack.back()) {
5,546✔
797
        // Encountered a different operator at the same depth - need to add
798
        // parentheses to enforce precedence. Intersection has higher
799
        // precedence, so we parenthesize the intersection terms.
800
        if (op_stack.back() == OP_INTERSECTION) {
98✔
801
          add_parentheses(pos_stack.back());
49✔
802
        } else {
803
          add_parentheses(i);
49✔
804
        }
805

806
        // Restart the scan since we modified the expression
807
        i = -1; // Will be incremented to 0 by the for loop
98✔
808
        op_stack = {0};
98✔
809
        pos_stack = {0};
98✔
810
      }
811
    }
812
  }
813
}
1,213✔
814

815
//==============================================================================
816
//! Convert infix region specification to Reverse Polish Notation (RPN)
817
//!
818
//! This function uses the shunting-yard algorithm.
819
//==============================================================================
820

821
vector<int32_t> Region::generate_postfix(int32_t cell_id) const
44✔
822
{
823
  vector<int32_t> rpn;
44✔
824
  vector<int32_t> stack;
44✔
825

826
  for (int32_t token : expression_) {
990✔
827
    if (token < OP_UNION) {
946✔
828
      // If token is not an operator, add it to output
829
      rpn.push_back(token);
396✔
830
    } else if (token < OP_RIGHT_PAREN) {
550✔
831
      // Regular operators union, intersection, complement
832
      while (stack.size() > 0) {
561✔
833
        int32_t op = stack.back();
462✔
834

835
        if (op < OP_RIGHT_PAREN && ((token == OP_COMPLEMENT && token < op) ||
462!
836
                                     (token != OP_COMPLEMENT && token <= op))) {
209!
837
          // While there is an operator, op, on top of the stack, if the token
838
          // is left-associative and its precedence is less than or equal to
839
          // that of op or if the token is right-associative and its precedence
840
          // is less than that of op, move op to the output queue and push the
841
          // token on to the stack. Note that only complement is
842
          // right-associative.
843
          rpn.push_back(op);
209✔
844
          stack.pop_back();
209✔
845
        } else {
846
          break;
847
        }
848
      }
849

850
      stack.push_back(token);
352✔
851

852
    } else if (token == OP_LEFT_PAREN) {
198✔
853
      // If the token is a left parenthesis, push it onto the stack
854
      stack.push_back(token);
99✔
855

856
    } else {
857
      // If the token is a right parenthesis, move operators from the stack to
858
      // the output queue until reaching the left parenthesis.
859
      for (auto it = stack.rbegin(); *it != OP_LEFT_PAREN; it++) {
198✔
860
        // If we run out of operators without finding a left parenthesis, it
861
        // means there are mismatched parentheses.
862
        if (it == stack.rend()) {
99!
UNCOV
863
          fatal_error(fmt::format(
×
864
            "Mismatched parentheses in region specification for cell {}",
865
            cell_id));
866
        }
867
        rpn.push_back(stack.back());
99✔
868
        stack.pop_back();
99✔
869
      }
870

871
      // Pop the left parenthesis.
872
      stack.pop_back();
99✔
873
    }
874
  }
875

876
  while (stack.size() > 0) {
88✔
877
    int32_t op = stack.back();
44✔
878

879
    // If the operator is a parenthesis it is mismatched.
880
    if (op >= OP_RIGHT_PAREN) {
44!
UNCOV
881
      fatal_error(fmt::format(
×
882
        "Mismatched parentheses in region specification for cell {}", cell_id));
883
    }
884

885
    rpn.push_back(stack.back());
44✔
886
    stack.pop_back();
44✔
887
  }
888

889
  return rpn;
88✔
890
}
44✔
891

892
//==============================================================================
893

894
std::string Region::str() const
27,294✔
895
{
896
  std::stringstream region_spec {};
27,294✔
897
  if (!expression_.empty()) {
27,294✔
898
    for (int32_t token : expression_) {
80,450✔
899
      if (token == OP_LEFT_PAREN) {
61,330✔
900
        region_spec << " (";
1,214✔
901
      } else if (token == OP_RIGHT_PAREN) {
60,116✔
902
        region_spec << " )";
1,214✔
903
      } else if (token == OP_COMPLEMENT) {
58,902!
UNCOV
904
        region_spec << " ~";
×
905
      } else if (token == OP_INTERSECTION) {
58,902✔
906
      } else if (token == OP_UNION) {
55,573✔
907
        region_spec << " |";
3,407✔
908
      } else {
909
        // Note the off-by-one indexing
910
        auto surf_id = model::surfaces[abs(token) - 1]->id_;
52,166✔
911
        region_spec << " " << ((token > 0) ? surf_id : -surf_id);
52,166✔
912
      }
913
    }
914
  }
915
  return region_spec.str();
54,588✔
916
}
27,294✔
917

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

920
std::pair<double, int32_t> Region::distance(
2,147,483,647✔
921
  Position r, Direction u, int32_t on_surface) const
922
{
923
  double min_dist {INFTY};
2,147,483,647✔
924
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
2,147,483,647✔
925

926
  for (int32_t token : expression_) {
2,147,483,647✔
927
    // Ignore this token if it corresponds to an operator rather than a region.
928
    if (token >= OP_UNION)
2,147,483,647✔
929
      continue;
174,135,151✔
930

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

936
    // Check if this distance is the new minimum.
937
    if (d < min_dist) {
2,147,483,647✔
938
      if (min_dist - d >= FP_PRECISION * min_dist) {
2,147,483,647!
939
        min_dist = d;
2,147,483,647✔
940
        i_surf = -token;
2,147,483,647✔
941
      }
942
    }
943
  }
944

945
  return {min_dist, i_surf};
2,147,483,647✔
946
}
947

948
//==============================================================================
949

950
bool Region::contains(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
951
{
952
  if (simple_) {
2,147,483,647✔
953
    return contains_simple(r, u, on_surface);
2,147,483,647✔
954
  } else {
955
    return contains_complex(r, u, on_surface);
7,338,890✔
956
  }
957
}
958

959
//==============================================================================
960

961
bool Region::contains_simple(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
962
{
963
  for (int32_t token : expression_) {
2,147,483,647✔
964
    // Assume that no tokens are operators. Evaluate the sense of particle with
965
    // respect to the surface and see if the token matches the sense. If the
966
    // particle's surface attribute is set and matches the token, that
967
    // overrides the determination based on sense().
968
    if (token == on_surface) {
2,147,483,647✔
969
    } else if (-token == on_surface) {
2,147,483,647✔
970
      return false;
994,455,989✔
971
    } else {
972
      // Note the off-by-one indexing
973
      bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
2,147,483,647✔
974
      if (sense != (token > 0)) {
2,147,483,647✔
975
        return false;
847,621,103✔
976
      }
977
    }
978
  }
979
  return true;
2,147,483,647✔
980
}
981

982
//==============================================================================
983

984
bool Region::contains_complex(Position r, Direction u, int32_t on_surface) const
7,338,890✔
985
{
986
  bool in_cell = true;
7,338,890✔
987
  int total_depth = 0;
7,338,890✔
988

989
  // For each token
990
  for (auto it = expression_.begin(); it != expression_.end(); it++) {
114,977,498✔
991
    int32_t token = *it;
109,582,022✔
992

993
    // If the token is a surface evaluate the sense
994
    // If the token is a union or intersection check to
995
    // short circuit
996
    if (token < OP_UNION) {
109,582,022✔
997
      if (token == on_surface) {
47,902,801✔
998
        in_cell = true;
3,658,424✔
999
      } else if (-token == on_surface) {
44,244,377✔
1000
        in_cell = false;
703,425✔
1001
      } else {
1002
        // Note the off-by-one indexing
1003
        bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
43,540,952✔
1004
        in_cell = (sense == (token > 0));
43,540,952✔
1005
      }
1006
    } else if ((token == OP_UNION && in_cell == true) ||
61,679,221✔
1007
               (token == OP_INTERSECTION && in_cell == false)) {
29,620,860✔
1008
      // If the total depth is zero return
1009
      if (total_depth == 0) {
6,610,311✔
1010
        return in_cell;
1,943,414✔
1011
      }
1012

1013
      total_depth--;
4,666,897✔
1014

1015
      // While the iterator is within the bounds of the vector
1016
      int depth = 1;
4,666,897✔
1017
      do {
1018
        // Get next token
1019
        it++;
28,963,390✔
1020
        int32_t next_token = *it;
28,963,390✔
1021

1022
        // If the token is an a parenthesis
1023
        if (next_token > OP_COMPLEMENT) {
28,963,390✔
1024
          // Adjust depth accordingly
1025
          if (next_token == OP_RIGHT_PAREN) {
4,863,289✔
1026
            depth--;
4,765,093✔
1027
          } else {
1028
            depth++;
98,196✔
1029
          }
1030
        }
1031
      } while (depth > 0);
28,963,390✔
1032
    } else if (token == OP_LEFT_PAREN) {
59,735,807✔
1033
      total_depth++;
9,585,948✔
1034
    } else if (token == OP_RIGHT_PAREN) {
45,482,962✔
1035
      total_depth--;
4,919,051✔
1036
    }
1037
  }
1038
  return in_cell;
5,395,476✔
1039
}
1040

1041
//==============================================================================
1042

1043
BoundingBox Region::bounding_box(int32_t cell_id) const
88✔
1044
{
1045
  if (simple_) {
88✔
1046
    return bounding_box_simple();
44✔
1047
  } else {
1048
    auto postfix = generate_postfix(cell_id);
44✔
1049
    return bounding_box_complex(postfix);
44✔
1050
  }
44✔
1051
}
1052

1053
//==============================================================================
1054

1055
BoundingBox Region::bounding_box_simple() const
44✔
1056
{
1057
  BoundingBox bbox;
44✔
1058
  for (int32_t token : expression_) {
176✔
1059
    bbox &= model::surfaces[abs(token) - 1]->bounding_box(token > 0);
132✔
1060
  }
1061
  return bbox;
44✔
1062
}
1063

1064
//==============================================================================
1065

1066
BoundingBox Region::bounding_box_complex(vector<int32_t> postfix) const
44✔
1067
{
1068
  vector<BoundingBox> stack(postfix.size());
44✔
1069
  int i_stack = -1;
44✔
1070

1071
  for (auto& token : postfix) {
792✔
1072
    if (token == OP_UNION) {
748✔
1073
      stack[i_stack - 1] = stack[i_stack - 1] | stack[i_stack];
154✔
1074
      i_stack--;
154✔
1075
    } else if (token == OP_INTERSECTION) {
594✔
1076
      stack[i_stack - 1] = stack[i_stack - 1] & stack[i_stack];
198✔
1077
      i_stack--;
198✔
1078
    } else {
1079
      i_stack++;
396✔
1080
      stack[i_stack] = model::surfaces[abs(token) - 1]->bounding_box(token > 0);
396✔
1081
    }
1082
  }
1083

1084
  assert(i_stack == 0);
36!
1085
  return stack.front();
88✔
1086
}
44✔
1087

1088
//==============================================================================
1089

1090
vector<int32_t> Region::surfaces() const
5,588✔
1091
{
1092
  if (simple_) {
5,588✔
1093
    return expression_;
5,568✔
1094
  }
1095

1096
  vector<int32_t> surfaces = expression_;
20!
1097

1098
  auto it = std::find_if(surfaces.begin(), surfaces.end(),
20!
1099
    [&](const auto& value) { return value >= OP_UNION; });
40✔
1100

1101
  while (it != surfaces.end()) {
60✔
1102
    surfaces.erase(it);
40!
1103

1104
    it = std::find_if(surfaces.begin(), surfaces.end(),
40!
1105
      [&](const auto& value) { return value >= OP_UNION; });
120✔
1106
  }
1107

1108
  return surfaces;
20✔
1109
}
20✔
1110

1111
//==============================================================================
1112
// Non-method functions
1113
//==============================================================================
1114

1115
void read_cells(pugi::xml_node node)
7,932✔
1116
{
1117
  // Count the number of cells.
1118
  int n_cells = 0;
7,932✔
1119
  for (pugi::xml_node cell_node : node.children("cell")) {
41,830✔
1120
    n_cells++;
33,898✔
1121
  }
1122

1123
  // Loop over XML cell elements and populate the array.
1124
  model::cells.reserve(n_cells);
7,932✔
1125
  for (pugi::xml_node cell_node : node.children("cell")) {
41,830✔
1126
    model::cells.push_back(make_unique<CSGCell>(cell_node));
33,898✔
1127
  }
1128

1129
  // Fill the cell map.
1130
  for (int i = 0; i < model::cells.size(); i++) {
41,830✔
1131
    int32_t id = model::cells[i]->id_;
33,898✔
1132
    auto search = model::cell_map.find(id);
33,898✔
1133
    if (search == model::cell_map.end()) {
33,898!
1134
      model::cell_map[id] = i;
33,898✔
1135
    } else {
UNCOV
1136
      fatal_error(
×
UNCOV
1137
        fmt::format("Two or more cells use the same unique ID: {}", id));
×
1138
    }
1139
  }
1140

1141
  read_dagmc_universes(node);
7,932✔
1142

1143
  populate_universes();
7,930✔
1144

1145
  // Allocate the cell overlap count if necessary.
1146
  if (settings::check_overlaps) {
7,930✔
1147
    model::overlap_check_count.resize(model::cells.size(), 0);
119✔
1148
  }
1149

1150
  if (model::cells.size() == 0) {
7,930!
UNCOV
1151
    fatal_error("No cells were found in the geometry.xml file");
×
1152
  }
1153
}
7,930✔
1154

1155
void populate_universes()
7,932✔
1156
{
1157
  // Used to map universe index to the index of an implicit complement cell for
1158
  // DAGMC universes
1159
  std::unordered_map<int, int> implicit_comp_cells;
7,932✔
1160

1161
  // Populate the Universe vector and map.
1162
  for (int index_cell = 0; index_cell < model::cells.size(); index_cell++) {
42,042✔
1163
    int32_t uid = model::cells[index_cell]->universe_;
34,110✔
1164
    auto it = model::universe_map.find(uid);
34,110✔
1165
    if (it == model::universe_map.end()) {
34,110✔
1166
      model::universes.push_back(make_unique<Universe>());
19,594✔
1167
      model::universes.back()->id_ = uid;
19,594✔
1168
      model::universes.back()->cells_.push_back(index_cell);
19,594✔
1169
      model::universe_map[uid] = model::universes.size() - 1;
19,594✔
1170
    } else {
1171
#ifdef OPENMC_DAGMC_ENABLED
1172
      // Skip implicit complement cells for now
1173
      Universe* univ = model::universes[it->second].get();
1,892✔
1174
      DAGUniverse* dag_univ = dynamic_cast<DAGUniverse*>(univ);
1,892!
1175
      if (dag_univ && (dag_univ->implicit_complement_idx() == index_cell)) {
1,892✔
1176
        implicit_comp_cells[it->second] = index_cell;
44✔
1177
        continue;
44✔
1178
      }
1179
#endif
1180

1181
      model::universes[it->second]->cells_.push_back(index_cell);
14,472✔
1182
    }
1183
  }
1184

1185
  // Add DAGUniverse implicit complement cells last
1186
  for (const auto& it : implicit_comp_cells) {
7,976✔
1187
    int index_univ = it.first;
44✔
1188
    int index_cell = it.second;
44✔
1189
    model::universes[index_univ]->cells_.push_back(index_cell);
44!
1190
  }
1191

1192
  model::universes.shrink_to_fit();
7,932✔
1193
}
7,932✔
1194

1195
//==============================================================================
1196
// C-API functions
1197
//==============================================================================
1198

1199
extern "C" int openmc_cell_get_fill(
177✔
1200
  int32_t index, int* type, int32_t** indices, int32_t* n)
1201
{
1202
  if (index >= 0 && index < model::cells.size()) {
177!
1203
    Cell& c {*model::cells[index]};
177✔
1204
    *type = static_cast<int>(c.type_);
177✔
1205
    if (c.type_ == Fill::MATERIAL) {
177!
1206
      *indices = c.material_.data();
177✔
1207
      *n = c.material_.size();
177✔
1208
    } else {
UNCOV
1209
      *indices = &c.fill_;
×
UNCOV
1210
      *n = 1;
×
1211
    }
1212
  } else {
UNCOV
1213
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1214
    return OPENMC_E_OUT_OF_BOUNDS;
×
1215
  }
1216
  return 0;
177✔
1217
}
1218

1219
extern "C" int openmc_cell_set_fill(
11✔
1220
  int32_t index, int type, int32_t n, const int32_t* indices)
1221
{
1222
  Fill filltype = static_cast<Fill>(type);
11✔
1223
  if (index >= 0 && index < model::cells.size()) {
11!
1224
    Cell& c {*model::cells[index]};
11✔
1225
    if (filltype == Fill::MATERIAL) {
11!
1226
      c.type_ = Fill::MATERIAL;
11✔
1227
      c.material_.clear();
11✔
1228
      for (int i = 0; i < n; i++) {
22✔
1229
        int i_mat = indices[i];
11✔
1230
        if (i_mat == MATERIAL_VOID) {
11!
1231
          c.material_.push_back(MATERIAL_VOID);
×
1232
        } else if (i_mat >= 0 && i_mat < model::materials.size()) {
11!
1233
          c.material_.push_back(i_mat);
11✔
1234
        } else {
UNCOV
1235
          set_errmsg("Index in materials array is out of bounds.");
×
1236
          return OPENMC_E_OUT_OF_BOUNDS;
×
1237
        }
1238
      }
1239
      c.material_.shrink_to_fit();
11✔
UNCOV
1240
    } else if (filltype == Fill::UNIVERSE) {
×
UNCOV
1241
      c.type_ = Fill::UNIVERSE;
×
1242
    } else {
UNCOV
1243
      c.type_ = Fill::LATTICE;
×
1244
    }
1245
  } else {
1246
    set_errmsg("Index in cells array is out of bounds.");
×
1247
    return OPENMC_E_OUT_OF_BOUNDS;
×
1248
  }
1249
  return 0;
11✔
1250
}
1251

1252
extern "C" int openmc_cell_set_temperature(
88✔
1253
  int32_t index, double T, const int32_t* instance, bool set_contained)
1254
{
1255
  if (index < 0 || index >= model::cells.size()) {
88!
1256
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
UNCOV
1257
    return OPENMC_E_OUT_OF_BOUNDS;
×
1258
  }
1259

1260
  int32_t instance_index = instance ? *instance : -1;
88✔
1261
  try {
1262
    model::cells[index]->set_temperature(T, instance_index, set_contained);
88✔
UNCOV
1263
  } catch (const std::exception& e) {
×
1264
    set_errmsg(e.what());
×
1265
    return OPENMC_E_UNASSIGNED;
×
UNCOV
1266
  }
×
1267
  return 0;
88✔
1268
}
1269

1270
extern "C" int openmc_cell_set_density(
88✔
1271
  int32_t index, double density, const int32_t* instance, bool set_contained)
1272
{
1273
  if (index < 0 || index >= model::cells.size()) {
88!
1274
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
UNCOV
1275
    return OPENMC_E_OUT_OF_BOUNDS;
×
1276
  }
1277

1278
  int32_t instance_index = instance ? *instance : -1;
88✔
1279
  try {
1280
    model::cells[index]->set_density(density, instance_index, set_contained);
88✔
UNCOV
1281
  } catch (const std::exception& e) {
×
1282
    set_errmsg(e.what());
×
1283
    return OPENMC_E_UNASSIGNED;
×
UNCOV
1284
  }
×
1285
  return 0;
88✔
1286
}
1287

1288
extern "C" int openmc_cell_get_temperature(
91✔
1289
  int32_t index, const int32_t* instance, double* T)
1290
{
1291
  if (index < 0 || index >= model::cells.size()) {
91!
1292
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
UNCOV
1293
    return OPENMC_E_OUT_OF_BOUNDS;
×
1294
  }
1295

1296
  int32_t instance_index = instance ? *instance : -1;
91✔
1297
  try {
1298
    *T = model::cells[index]->temperature(instance_index);
91✔
UNCOV
1299
  } catch (const std::exception& e) {
×
1300
    set_errmsg(e.what());
×
1301
    return OPENMC_E_UNASSIGNED;
×
UNCOV
1302
  }
×
1303
  return 0;
91✔
1304
}
1305

1306
extern "C" int openmc_cell_get_density(
88✔
1307
  int32_t index, const int32_t* instance, double* density)
1308
{
1309
  if (index < 0 || index >= model::cells.size()) {
88!
UNCOV
1310
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
UNCOV
1311
    return OPENMC_E_OUT_OF_BOUNDS;
×
1312
  }
1313

1314
  int32_t instance_index = instance ? *instance : -1;
88✔
1315
  try {
1316
    if (model::cells[index]->type_ != Fill::MATERIAL) {
88!
UNCOV
1317
      fatal_error(
×
UNCOV
1318
        fmt::format("Cell {}, instance {} is not filled with a material.",
×
1319
          model::cells[index]->id_, instance_index));
×
1320
    }
1321

1322
    int32_t mat_index = model::cells[index]->material(instance_index);
88✔
1323
    if (mat_index == MATERIAL_VOID) {
88!
UNCOV
1324
      *density = 0.0;
×
1325
    } else {
1326
      *density = model::cells[index]->density_mult(instance_index) *
176✔
1327
                 model::materials[mat_index]->density_gpcc();
88✔
1328
    }
UNCOV
1329
  } catch (const std::exception& e) {
×
UNCOV
1330
    set_errmsg(e.what());
×
UNCOV
1331
    return OPENMC_E_UNASSIGNED;
×
UNCOV
1332
  }
×
1333
  return 0;
88✔
1334
}
1335

1336
//! Get the bounding box of a cell
1337
extern "C" int openmc_cell_bounding_box(
55✔
1338
  const int32_t index, double* llc, double* urc)
1339
{
1340

1341
  BoundingBox bbox;
55✔
1342

1343
  const auto& c = model::cells[index];
55✔
1344
  bbox = c->bounding_box();
55✔
1345

1346
  // set lower left corner values
1347
  llc[0] = bbox.min.x;
55✔
1348
  llc[1] = bbox.min.y;
55✔
1349
  llc[2] = bbox.min.z;
55✔
1350

1351
  // set upper right corner values
1352
  urc[0] = bbox.max.x;
55✔
1353
  urc[1] = bbox.max.y;
55✔
1354
  urc[2] = bbox.max.z;
55✔
1355

1356
  return 0;
55✔
1357
}
1358

1359
//! Get the name of a cell
1360
extern "C" int openmc_cell_get_name(int32_t index, const char** name)
22✔
1361
{
1362
  if (index < 0 || index >= model::cells.size()) {
22!
UNCOV
1363
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1364
    return OPENMC_E_OUT_OF_BOUNDS;
×
1365
  }
1366

1367
  *name = model::cells[index]->name().data();
22✔
1368

1369
  return 0;
22✔
1370
}
1371

1372
//! Set the name of a cell
1373
extern "C" int openmc_cell_set_name(int32_t index, const char* name)
11✔
1374
{
1375
  if (index < 0 || index >= model::cells.size()) {
11!
UNCOV
1376
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1377
    return OPENMC_E_OUT_OF_BOUNDS;
×
1378
  }
1379

1380
  model::cells[index]->set_name(name);
11✔
1381

1382
  return 0;
11✔
1383
}
1384

1385
//==============================================================================
1386
//! Define a containing (parent) cell
1387
//==============================================================================
1388

1389
//! Used to locate a universe fill in the geometry
1390
struct ParentCell {
1391
  bool operator==(const ParentCell& other) const
144✔
1392
  {
1393
    return cell_index == other.cell_index &&
288!
1394
           lattice_index == other.lattice_index;
288!
1395
  }
1396

1397
  bool operator<(const ParentCell& other) const
1398
  {
1399
    return cell_index < other.cell_index ||
1400
           (cell_index == other.cell_index &&
1401
             lattice_index < other.lattice_index);
1402
  }
1403

1404
  int64_t cell_index;
1405
  int64_t lattice_index;
1406
};
1407

1408
//! Structure used to insert ParentCell into hashed STL data structures
1409
struct ParentCellHash {
1410
  std::size_t operator()(const ParentCell& p) const
673✔
1411
  {
1412
    return 4096 * p.cell_index + p.lattice_index;
673✔
1413
  }
1414
};
1415

1416
//! Used to manage a traversal stack when locating parent cells of a cell
1417
//! instance in the model
1418
struct ParentCellStack {
1419

1420
  //! push method that adds to the parent_cells visited cells for this search
1421
  //! universe
1422
  void push(int32_t search_universe, const ParentCell& pc)
112✔
1423
  {
1424
    parent_cells_.push_back(pc);
112✔
1425
    // add parent cell to the set of cells we've visited for this search
1426
    // universe
1427
    visited_cells_[search_universe].insert(pc);
112✔
1428
  }
112✔
1429

1430
  //! removes the last parent_cell and clears the visited cells for the popped
1431
  //! cell's universe
1432
  void pop()
80✔
1433
  {
1434
    visited_cells_[this->current_univ()].clear();
80✔
1435
    parent_cells_.pop_back();
80✔
1436
  }
80✔
1437

1438
  //! checks whether or not the parent cell has been visited already for this
1439
  //! search universe
1440
  bool visited(int32_t search_universe, const ParentCell& parent_cell)
561✔
1441
  {
1442
    return visited_cells_[search_universe].count(parent_cell) != 0;
561✔
1443
  }
1444

1445
  //! return the next universe to search for a parent cell
1446
  int32_t current_univ() const
80✔
1447
  {
1448
    return model::cells[parent_cells_.back().cell_index]->universe_;
80✔
1449
  }
1450

1451
  //! indicates whether nor not parent cells are present on the stack
1452
  bool empty() const { return parent_cells_.empty(); }
80✔
1453

1454
  //! compute an instance for the provided distribcell index
1455
  int32_t compute_instance(int32_t distribcell_index) const
193✔
1456
  {
1457
    if (distribcell_index == C_NONE)
193✔
1458
      return 0;
65✔
1459

1460
    int32_t instance = 0;
128✔
1461
    for (const auto& parent_cell : this->parent_cells_) {
240✔
1462
      auto& cell = model::cells[parent_cell.cell_index];
112✔
1463
      if (cell->type_ == Fill::UNIVERSE) {
112!
UNCOV
1464
        instance += cell->offset_[distribcell_index];
×
1465
      } else if (cell->type_ == Fill::LATTICE) {
112!
1466
        auto& lattice = model::lattices[cell->fill_];
112✔
1467
        instance +=
112✔
1468
          lattice->offset(distribcell_index, parent_cell.lattice_index);
112✔
1469
      }
1470
    }
1471
    return instance;
128✔
1472
  }
1473

1474
  // Accessors
1475
  vector<ParentCell>& parent_cells() { return parent_cells_; }
339✔
1476
  const vector<ParentCell>& parent_cells() const { return parent_cells_; }
1477

1478
  // Data Members
1479
  vector<ParentCell> parent_cells_;
1480
  std::unordered_map<int32_t, std::unordered_set<ParentCell, ParentCellHash>>
1481
    visited_cells_;
1482
};
1483

1484
vector<ParentCell> Cell::find_parent_cells(
×
1485
  int32_t instance, const Position& r) const
1486
{
1487

1488
  // create a temporary particle
UNCOV
1489
  GeometryState dummy_particle {};
×
1490
  dummy_particle.r() = r;
×
1491
  dummy_particle.u() = {0., 0., 1.};
×
1492

UNCOV
1493
  return find_parent_cells(instance, dummy_particle);
×
1494
}
×
1495

1496
vector<ParentCell> Cell::find_parent_cells(
×
1497
  int32_t instance, GeometryState& p) const
1498
{
1499
  // look up the particle's location
UNCOV
1500
  exhaustive_find_cell(p);
×
1501
  const auto& coords = p.coord();
×
1502

1503
  // build a parent cell stack from the particle coordinates
UNCOV
1504
  ParentCellStack stack;
×
UNCOV
1505
  bool cell_found = false;
×
UNCOV
1506
  for (auto it = coords.begin(); it != coords.end(); it++) {
×
UNCOV
1507
    const auto& coord = *it;
×
1508
    const auto& cell = model::cells[coord.cell()];
×
1509
    // if the cell at this level matches the current cell, stop adding to the
1510
    // stack
1511
    if (coord.cell() == model::cell_map[this->id_]) {
×
UNCOV
1512
      cell_found = true;
×
UNCOV
1513
      break;
×
1514
    }
1515

1516
    // if filled with a lattice, get the lattice index from the next
1517
    // level in the coordinates to push to the stack
UNCOV
1518
    int lattice_idx = C_NONE;
×
UNCOV
1519
    if (cell->type_ == Fill::LATTICE) {
×
1520
      const auto& next_coord = *(it + 1);
×
1521
      lattice_idx = model::lattices[next_coord.lattice()]->get_flat_index(
×
1522
        next_coord.lattice_index());
1523
    }
UNCOV
1524
    stack.push(coord.universe(), {coord.cell(), lattice_idx});
×
1525
  }
1526

1527
  // if this loop finished because the cell was found and
1528
  // the instance matches the one requested in the call
1529
  // we have the correct path and can return the stack
UNCOV
1530
  if (cell_found &&
×
UNCOV
1531
      stack.compute_instance(this->distribcell_index_) == instance) {
×
UNCOV
1532
    return stack.parent_cells();
×
1533
  }
1534

1535
  // fall back on an exhaustive search for the cell's parents
UNCOV
1536
  return exhaustive_find_parent_cells(instance);
×
UNCOV
1537
}
×
1538

1539
vector<ParentCell> Cell::exhaustive_find_parent_cells(int32_t instance) const
113✔
1540
{
1541
  ParentCellStack stack;
113✔
1542
  // start with this cell's universe
1543
  int32_t prev_univ_idx;
1544
  int32_t univ_idx = this->universe_;
113✔
1545

1546
  while (true) {
1547
    const auto& univ = model::universes[univ_idx];
193✔
1548
    prev_univ_idx = univ_idx;
193✔
1549

1550
    // search for a cell that is filled w/ this universe
1551
    for (const auto& cell : model::cells) {
1,236✔
1552
      // if this is a material-filled cell, move on
1553
      if (cell->type_ == Fill::MATERIAL)
1,155✔
1554
        continue;
642✔
1555

1556
      if (cell->type_ == Fill::UNIVERSE) {
513✔
1557
        // if this is in the set of cells previously visited for this universe,
1558
        // move on
1559
        if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE}))
305!
UNCOV
1560
          continue;
×
1561

1562
        // if this cell contains the universe we're searching for, add it to the
1563
        // stack
1564
        if (cell->fill_ == univ_idx) {
305!
UNCOV
1565
          stack.push(univ_idx, {model::cell_map[cell->id_], C_NONE});
×
UNCOV
1566
          univ_idx = cell->universe_;
×
1567
        }
1568
      } else if (cell->type_ == Fill::LATTICE) {
208!
1569
        // retrieve the lattice and lattice universes
1570
        const auto& lattice = model::lattices[cell->fill_];
208✔
1571
        const auto& lattice_univs = lattice->universes_;
208✔
1572

1573
        // start search for universe
1574
        auto lat_it = lattice_univs.begin();
208✔
1575
        while (true) {
1576
          // find the next lattice cell with this universe
1577
          lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
352✔
1578
          if (lat_it == lattice_univs.end())
352✔
1579
            break;
96✔
1580

1581
          int lattice_idx = lat_it - lattice_univs.begin();
256✔
1582

1583
          // move iterator forward one to avoid finding the same entry
1584
          lat_it++;
256✔
1585
          if (stack.visited(
256✔
1586
                univ_idx, {model::cell_map[cell->id_], lattice_idx}))
256✔
1587
            continue;
144✔
1588

1589
          // add this cell and lattice index to the stack and exit loop
1590
          stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
112✔
1591
          univ_idx = cell->universe_;
112✔
1592
          break;
112✔
1593
        }
144✔
1594
      }
1595
      // if we've updated the universe, break
1596
      if (prev_univ_idx != univ_idx)
513✔
1597
        break;
112✔
1598
    } // end cell loop search for universe
1599

1600
    // if we're at the top of the geometry and the instance matches, we're done
1601
    if (univ_idx == model::root_universe &&
386!
1602
        stack.compute_instance(this->distribcell_index_) == instance)
193✔
1603
      break;
113✔
1604

1605
    // if there is no match on the original cell's universe, report an error
1606
    if (univ_idx == this->universe_) {
80!
UNCOV
1607
      fatal_error(
×
UNCOV
1608
        fmt::format("Could not find the parent cells for cell {}, instance {}.",
×
UNCOV
1609
          this->id_, instance));
×
1610
    }
1611

1612
    // if we don't find a suitable update, adjust the stack and continue
1613
    if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
80!
1614
      stack.pop();
80✔
1615
      univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
80!
1616
    }
1617

1618
  } // end while
80✔
1619

1620
  // reverse the stack so the highest cell comes first
1621
  std::reverse(stack.parent_cells().begin(), stack.parent_cells().end());
113✔
1622
  return stack.parent_cells();
226✔
1623
}
113✔
1624

1625
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
161✔
1626
  int32_t instance, Position* hint) const
1627
{
1628
  std::unordered_map<int32_t, vector<int32_t>> contained_cells;
161✔
1629

1630
  // if this is a material-filled cell it has no contained cells
1631
  if (this->type_ == Fill::MATERIAL)
161✔
1632
    return contained_cells;
48✔
1633

1634
  // find the pathway through the geometry to this cell
1635
  vector<ParentCell> parent_cells;
113✔
1636

1637
  // if a positional hint is provided, attempt to do a fast lookup
1638
  // of the parent cells
1639
  parent_cells = hint ? find_parent_cells(instance, *hint)
226!
1640
                      : exhaustive_find_parent_cells(instance);
113✔
1641

1642
  // if this cell is filled w/ a material, it contains no other cells
1643
  if (type_ != Fill::MATERIAL) {
113!
1644
    this->get_contained_cells_inner(contained_cells, parent_cells);
113✔
1645
  }
1646

1647
  return contained_cells;
113✔
1648
}
113✔
1649

1650
//! Get all cells within this cell
1651
void Cell::get_contained_cells_inner(
87,763✔
1652
  std::unordered_map<int32_t, vector<int32_t>>& contained_cells,
1653
  vector<ParentCell>& parent_cells) const
1654
{
1655

1656
  // filled by material, determine instance based on parent cells
1657
  if (type_ == Fill::MATERIAL) {
87,763✔
1658
    int instance = 0;
87,138✔
1659
    if (this->distribcell_index_ >= 0) {
87,138!
1660
      for (auto& parent_cell : parent_cells) {
261,412✔
1661
        auto& cell = model::cells[parent_cell.cell_index];
174,274✔
1662
        if (cell->type_ == Fill::UNIVERSE) {
174,274✔
1663
          instance += cell->offset_[distribcell_index_];
85,538✔
1664
        } else if (cell->type_ == Fill::LATTICE) {
88,736!
1665
          auto& lattice = model::lattices[cell->fill_];
88,736✔
1666
          instance += lattice->offset(
177,472✔
1667
            this->distribcell_index_, parent_cell.lattice_index);
88,736✔
1668
        }
1669
      }
1670
    }
1671
    // add entry to contained cells
1672
    contained_cells[model::cell_map[id_]].push_back(instance);
87,138✔
1673
    // filled with universe, add the containing cell to the parent cells
1674
    // and recurse
1675
  } else if (type_ == Fill::UNIVERSE) {
625✔
1676
    parent_cells.push_back({model::cell_map[id_], -1});
529✔
1677
    auto& univ = model::universes[fill_];
529✔
1678
    for (auto cell_index : univ->cells_) {
3,171✔
1679
      auto& cell = model::cells[cell_index];
2,642✔
1680
      cell->get_contained_cells_inner(contained_cells, parent_cells);
2,642✔
1681
    }
1682
    parent_cells.pop_back();
529✔
1683
    // filled with a lattice, visit each universe in the lattice
1684
    // with a recursive call to collect the cell instances
1685
  } else if (type_ == Fill::LATTICE) {
96!
1686
    auto& lattice = model::lattices[fill_];
96✔
1687
    for (auto i = lattice->begin(); i != lattice->end(); ++i) {
84,768✔
1688
      auto& univ = model::universes[*i];
84,672✔
1689
      parent_cells.push_back({model::cell_map[id_], i.indx_});
84,672✔
1690
      for (auto cell_index : univ->cells_) {
169,680✔
1691
        auto& cell = model::cells[cell_index];
85,008✔
1692
        cell->get_contained_cells_inner(contained_cells, parent_cells);
85,008✔
1693
      }
1694
      parent_cells.pop_back();
84,672✔
1695
    }
1696
  }
1697
}
87,763✔
1698

1699
//! Return the index in the cells array of a cell with a given ID
1700
extern "C" int openmc_get_cell_index(int32_t id, int32_t* index)
490✔
1701
{
1702
  auto it = model::cell_map.find(id);
490✔
1703
  if (it != model::cell_map.end()) {
490✔
1704
    *index = it->second;
479✔
1705
    return 0;
479✔
1706
  } else {
1707
    set_errmsg("No cell exists with ID=" + std::to_string(id) + ".");
11✔
1708
    return OPENMC_E_INVALID_ID;
11✔
1709
  }
1710
}
1711

1712
//! Return the ID of a cell
1713
extern "C" int openmc_cell_get_id(int32_t index, int32_t* id)
600,974✔
1714
{
1715
  if (index >= 0 && index < model::cells.size()) {
600,974!
1716
    *id = model::cells[index]->id_;
600,974✔
1717
    return 0;
600,974✔
1718
  } else {
UNCOV
1719
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1720
    return OPENMC_E_OUT_OF_BOUNDS;
×
1721
  }
1722
}
1723

1724
//! Set the ID of a cell
1725
extern "C" int openmc_cell_set_id(int32_t index, int32_t id)
22✔
1726
{
1727
  if (index >= 0 && index < model::cells.size()) {
22!
1728
    model::cells[index]->id_ = id;
22✔
1729
    model::cell_map[id] = index;
22✔
1730
    return 0;
22✔
1731
  } else {
UNCOV
1732
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1733
    return OPENMC_E_OUT_OF_BOUNDS;
×
1734
  }
1735
}
1736

1737
//! Return the translation vector of a cell
1738
extern "C" int openmc_cell_get_translation(int32_t index, double xyz[])
55✔
1739
{
1740
  if (index >= 0 && index < model::cells.size()) {
55!
1741
    auto& cell = model::cells[index];
55✔
1742
    xyz[0] = cell->translation_.x;
55✔
1743
    xyz[1] = cell->translation_.y;
55✔
1744
    xyz[2] = cell->translation_.z;
55✔
1745
    return 0;
55✔
1746
  } else {
UNCOV
1747
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1748
    return OPENMC_E_OUT_OF_BOUNDS;
×
1749
  }
1750
}
1751

1752
//! Set the translation vector of a cell
1753
extern "C" int openmc_cell_set_translation(int32_t index, const double xyz[])
33✔
1754
{
1755
  if (index >= 0 && index < model::cells.size()) {
33!
1756
    if (model::cells[index]->fill_ == C_NONE) {
33✔
1757
      set_errmsg(fmt::format("Cannot apply a translation to cell {}"
11✔
1758
                             " because it is not filled with another universe",
1759
        index));
1760
      return OPENMC_E_GEOMETRY;
11✔
1761
    }
1762
    model::cells[index]->translation_ = Position(xyz);
22✔
1763
    return 0;
22✔
1764
  } else {
UNCOV
1765
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1766
    return OPENMC_E_OUT_OF_BOUNDS;
×
1767
  }
1768
}
1769

1770
//! Return the rotation matrix of a cell
1771
extern "C" int openmc_cell_get_rotation(int32_t index, double rot[], size_t* n)
55✔
1772
{
1773
  if (index >= 0 && index < model::cells.size()) {
55!
1774
    auto& cell = model::cells[index];
55✔
1775
    *n = cell->rotation_.size();
55✔
1776
    std::memcpy(rot, cell->rotation_.data(), *n * sizeof(cell->rotation_[0]));
55✔
1777
    return 0;
55✔
1778
  } else {
UNCOV
1779
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1780
    return OPENMC_E_OUT_OF_BOUNDS;
×
1781
  }
1782
}
1783

1784
//! Set the flattened rotation matrix of a cell
1785
extern "C" int openmc_cell_set_rotation(
44✔
1786
  int32_t index, const double rot[], size_t rot_len)
1787
{
1788
  if (index >= 0 && index < model::cells.size()) {
44!
1789
    if (model::cells[index]->fill_ == C_NONE) {
44✔
1790
      set_errmsg(fmt::format("Cannot apply a rotation to cell {}"
11✔
1791
                             " because it is not filled with another universe",
1792
        index));
1793
      return OPENMC_E_GEOMETRY;
11✔
1794
    }
1795
    std::vector<double> vec_rot(rot, rot + rot_len);
33✔
1796
    model::cells[index]->set_rotation(vec_rot);
33✔
1797
    return 0;
33✔
1798
  } else {
33✔
1799
    set_errmsg("Index in cells array is out of bounds.");
×
1800
    return OPENMC_E_OUT_OF_BOUNDS;
×
1801
  }
1802
}
1803

1804
//! Get the number of instances of the requested cell
1805
extern "C" int openmc_cell_get_num_instances(
11✔
1806
  int32_t index, int32_t* num_instances)
1807
{
1808
  if (index < 0 || index >= model::cells.size()) {
11!
UNCOV
1809
    set_errmsg("Index in cells array is out of bounds.");
×
UNCOV
1810
    return OPENMC_E_OUT_OF_BOUNDS;
×
1811
  }
1812
  *num_instances = model::cells[index]->n_instances();
11✔
1813
  return 0;
11✔
1814
}
1815

1816
//! Extend the cells array by n elements
1817
extern "C" int openmc_extend_cells(
22✔
1818
  int32_t n, int32_t* index_start, int32_t* index_end)
1819
{
1820
  if (index_start)
22!
1821
    *index_start = model::cells.size();
22✔
1822
  if (index_end)
22!
UNCOV
1823
    *index_end = model::cells.size() + n - 1;
×
1824
  for (int32_t i = 0; i < n; i++) {
44✔
1825
    model::cells.push_back(make_unique<CSGCell>());
22✔
1826
  }
1827
  return 0;
22✔
1828
}
1829

1830
extern "C" int cells_size()
44✔
1831
{
1832
  return model::cells.size();
44✔
1833
}
1834

1835
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc