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

openmc-dev / openmc / 22694846817

04 Mar 2026 11:42PM UTC coverage: 81.537% (-0.2%) from 81.69%
22694846817

Pull #3808

github

web-flow
Merge 41317cfa2 into 2bd06660c
Pull Request #3808: Add properties to settings w/ documentation, c++ loading of filename, and python round-trip test

17549 of 25285 branches covered (69.4%)

Branch coverage included in aggregate %.

82 of 92 new or added lines in 8 files covered. (89.13%)

1498 existing lines in 55 files now uncovered.

57990 of 67359 relevant lines covered (86.09%)

44766605.57 hits per line

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

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

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

81
    // When user specifies angles, write them at end of vector
82
    rotation_.push_back(rot[0]);
423✔
83
    rotation_.push_back(rot[1]);
423✔
84
    rotation_.push_back(rot[2]);
423✔
85
  } else {
86
    std::copy(rot.begin(), rot.end(), std::back_inserter(rotation_));
×
87
  }
88
}
423✔
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);
5,034,975✔
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();
264!
121
}
122

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

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

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

192
      // Set density multiplier for the corresponding instance
193
      density_mult_.at(instance) =
188✔
194
        density / model::materials[mat_index]->density_gpcc();
376!
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
154✔
215
{
216
  // Create a group for this cell.
217
  auto cell_group = create_group(group, fmt::format("cell {}", id_));
154✔
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(
176✔
238
  hid_t group, bool read_temperatures, bool read_densities)
239
{
240
  auto cell_group = open_group(group, fmt::format("cell {}", id_));
176✔
241

242
  if (read_temperatures) {
176!
243
    // Read temperatures from file
244
    vector<double> temps;
176✔
245
    read_dataset(cell_group, "temperature", temps);
176✔
246

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

255
    // Modify temperatures for the cell
256
    sqrtkT_.clear();
176✔
257
    sqrtkT_.resize(temps.size());
176✔
258
    for (int64_t i = 0; i < temps.size(); ++i) {
308✔
259
      this->set_temperature(temps[i], i);
132✔
260
    }
261
  }
176✔
262

263
  if (read_densities) {
176!
264
    // Read densities
265
    if (object_exists(cell_group, "density")) {
176✔
266
      vector<double> density;
132✔
267
      read_dataset(cell_group, "density", density);
132✔
268

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

277
      // Set densities.
278
      for (int32_t i = 0; i < n_density; ++i) {
264✔
279
        this->set_density(density[i], i);
132✔
280
      }
281
    }
132✔
282
  }
283

284
  close_group(cell_group);
176✔
285
}
176✔
286

287
void Cell::to_hdf5(hid_t cell_group) const
28,511✔
288
{
289

290
  // Create a group for this cell.
291
  auto group = create_group(cell_group, fmt::format("cell {}", id_));
28,511✔
292

293
  if (!name_.empty()) {
28,511✔
294
    write_string(group, "name", name_, false);
6,122✔
295
  }
296

297
  write_dataset(group, "universe", model::universes[universe_]->id_);
28,511✔
298

299
  to_hdf5_inner(group);
28,511✔
300

301
  // Write fill information.
302
  if (type_ == Fill::MATERIAL) {
28,511✔
303
    write_dataset(group, "fill_type", "material");
23,107✔
304
    std::vector<int32_t> mat_ids;
23,107✔
305
    for (auto i_mat : material_) {
47,507✔
306
      if (i_mat != MATERIAL_VOID) {
24,400✔
307
        mat_ids.push_back(model::materials[i_mat]->id_);
16,008✔
308
      } else {
309
        mat_ids.push_back(MATERIAL_VOID);
8,392✔
310
      }
311
    }
312
    if (mat_ids.size() == 1) {
23,107✔
313
      write_dataset(group, "material", mat_ids[0]);
22,916✔
314
    } else {
315
      write_dataset(group, "material", mat_ids);
191✔
316
    }
317

318
    std::vector<double> temps;
23,107✔
319
    for (auto sqrtkT_val : sqrtkT_)
48,389✔
320
      temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
25,282✔
321
    write_dataset(group, "temperature", temps);
23,107✔
322

323
    write_dataset(group, "density_mult", density_mult_);
23,107✔
324

325
  } else if (type_ == Fill::UNIVERSE) {
28,511✔
326
    write_dataset(group, "fill_type", "universe");
3,939✔
327
    write_dataset(group, "fill", model::universes[fill_]->id_);
3,939✔
328
    if (translation_ != Position(0, 0, 0)) {
3,939✔
329
      write_dataset(group, "translation", translation_);
1,837✔
330
    }
331
    if (!rotation_.empty()) {
3,939✔
332
      if (rotation_.size() == 12) {
264!
333
        std::array<double, 3> rot {rotation_[9], rotation_[10], rotation_[11]};
264✔
334
        write_dataset(group, "rotation", rot);
264✔
335
      } else {
336
        write_dataset(group, "rotation", rotation_);
×
337
      }
338
    }
339

340
  } else if (type_ == Fill::LATTICE) {
1,465!
341
    write_dataset(group, "fill_type", "lattice");
1,465✔
342
    write_dataset(group, "lattice", model::lattices[fill_]->id_);
1,465✔
343
  }
344

345
  close_group(group);
28,511✔
346
}
28,511✔
347

348
//==============================================================================
349
// CSGCell implementation
350
//==============================================================================
351

352
CSGCell::CSGCell(pugi::xml_node cell_node)
34,040✔
353
{
354
  if (check_for_node(cell_node, "id")) {
34,040!
355
    id_ = std::stoi(get_node_value(cell_node, "id"));
68,080✔
356
  } else {
357
    fatal_error("Must specify id of cell in geometry XML file.");
×
358
  }
359

360
  if (check_for_node(cell_node, "name")) {
34,040✔
361
    name_ = get_node_value(cell_node, "name");
8,009✔
362
  }
363

364
  if (check_for_node(cell_node, "universe")) {
34,040✔
365
    universe_ = std::stoi(get_node_value(cell_node, "universe"));
65,810✔
366
  } else {
367
    universe_ = 0;
1,135✔
368
  }
369

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

383
  if (fill_present) {
34,040✔
384
    fill_ = std::stoi(get_node_value(cell_node, "fill"));
14,174✔
385
    if (fill_ == universe_) {
7,087!
386
      fatal_error(fmt::format("Cell {} is filled with the same universe that "
×
387
                              "it is contained in.",
388
        id_));
×
389
    }
390
  } else {
391
    fill_ = C_NONE;
26,953✔
392
  }
393

394
  // Read the material element.  There can be zero materials (filled with a
395
  // universe), more than one material (distribmats), and some materials may
396
  // be "void".
397
  if (material_present) {
34,040✔
398
    vector<std::string> mats {
26,953✔
399
      get_node_array<std::string>(cell_node, "material", true)};
26,953✔
400
    if (mats.size() > 0) {
26,953!
401
      material_.reserve(mats.size());
26,953✔
402
      for (std::string mat : mats) {
55,226✔
403
        if (mat.compare("void") == 0) {
28,273✔
404
          material_.push_back(MATERIAL_VOID);
8,722✔
405
        } else {
406
          material_.push_back(std::stoi(mat));
19,551✔
407
        }
408
      }
28,273✔
409
    } else {
410
      fatal_error(fmt::format(
×
411
        "An empty material element was specified for cell {}", id_));
×
412
    }
413
  }
26,953✔
414

415
  // Read the temperature element which may be distributed like materials.
416
  if (check_for_node(cell_node, "temperature")) {
34,040✔
417
    sqrtkT_ = get_node_array<double>(cell_node, "temperature");
386✔
418
    sqrtkT_.shrink_to_fit();
386✔
419

420
    // Make sure this is a material-filled cell.
421
    if (material_.size() == 0) {
386!
422
      fatal_error(fmt::format(
×
423
        "Cell {} was specified with a temperature but no material. Temperature"
424
        "specification is only valid for cells filled with a material.",
425
        id_));
×
426
    }
427

428
    // Make sure all temperatures are non-negative.
429
    for (auto T : sqrtkT_) {
1,852✔
430
      if (T < 0) {
1,466!
431
        fatal_error(fmt::format(
×
432
          "Cell {} was specified with a negative temperature", id_));
×
433
      }
434
    }
435

436
    // Convert to sqrt(k*T).
437
    for (auto& T : sqrtkT_) {
1,852✔
438
      T = std::sqrt(K_BOLTZMANN * T);
1,466✔
439
    }
440
  }
441

442
  // Read the density element which can be distributed similar to temperature.
443
  // These get assigned to the density multiplier, requiring a division by
444
  // the material density.
445
  // Note: calculating the actual density multiplier is deferred until materials
446
  // are finalized. density_mult_ contains the true density in the meantime.
447
  if (check_for_node(cell_node, "density")) {
34,040✔
448
    density_mult_ = get_node_array<double>(cell_node, "density");
75✔
449
    density_mult_.shrink_to_fit();
75✔
450

451
    // Make sure this is a material-filled cell.
452
    if (material_.size() == 0) {
75!
453
      fatal_error(fmt::format(
×
454
        "Cell {} was specified with a density but no material. Density"
455
        "specification is only valid for cells filled with a material.",
456
        id_));
×
457
    }
458

459
    // Make sure this is a non-void material.
460
    for (auto mat_id : material_) {
150✔
461
      if (mat_id == MATERIAL_VOID) {
75!
462
        fatal_error(fmt::format(
×
463
          "Cell {} was specified with a density, but contains a void "
464
          "material. Density specification is only valid for cells "
465
          "filled with a non-void material.",
466
          id_));
×
467
      }
468
    }
469

470
    // Make sure all densities are non-negative and greater than zero.
471
    for (auto rho : density_mult_) {
1,230✔
472
      if (rho <= 0) {
1,155!
473
        fatal_error(fmt::format(
×
474
          "Cell {} was specified with a density less than or equal to zero",
475
          id_));
×
476
      }
477
    }
478
  }
479

480
  // Read the region specification.
481
  std::string region_spec;
34,040✔
482
  if (check_for_node(cell_node, "region")) {
34,040✔
483
    region_spec = get_node_value(cell_node, "region");
25,061✔
484
  }
485

486
  // Get a tokenized representation of the region specification and apply De
487
  // Morgans law
488
  Region region(region_spec, id_);
34,040✔
489
  region_ = region;
34,040✔
490

491
  // Read the translation vector.
492
  if (check_for_node(cell_node, "translation")) {
34,040✔
493
    if (fill_ == C_NONE) {
2,557!
494
      fatal_error(fmt::format("Cannot apply a translation to cell {}"
×
495
                              " because it is not filled with another universe",
496
        id_));
×
497
    }
498

499
    auto xyz {get_node_array<double>(cell_node, "translation")};
2,557✔
500
    if (xyz.size() != 3) {
2,557!
501
      fatal_error(
×
502
        fmt::format("Non-3D translation vector applied to cell {}", id_));
×
503
    }
504
    translation_ = xyz;
2,557✔
505
  }
2,557✔
506

507
  // Read the rotation transform.
508
  if (check_for_node(cell_node, "rotation")) {
34,040✔
509
    auto rot {get_node_array<double>(cell_node, "rotation")};
390✔
510
    set_rotation(rot);
390✔
511
  }
390✔
512
}
34,040✔
513

514
//==============================================================================
515

516
void CSGCell::to_hdf5_inner(hid_t group_id) const
28,347✔
517
{
518
  write_string(group_id, "geom_type", "csg", false);
28,347✔
519
  write_string(group_id, "region", region_.str(), false);
28,347✔
520
}
28,347✔
521

522
//==============================================================================
523

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

535
    // decrement parenthesis level if there are two adjacent surfaces
536
    if (one < OP_UNION && two < OP_UNION) {
×
537
      parenthesis_level--;
×
538
      // increment if there are two adjacent operators
539
    } else if (one >= OP_UNION && two >= OP_UNION) {
×
540
      parenthesis_level++;
×
541
    }
542

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

551
    // continue loop, one token at a time
552
    it--;
553
  }
554
  return it;
×
555
}
556

557
//==============================================================================
558
// Region implementation
559
//==============================================================================
560

561
Region::Region(std::string region_spec, int32_t cell_id)
34,139✔
562
{
563
  // Check if region_spec is not empty.
564
  if (!region_spec.empty()) {
34,139✔
565
    // Parse all halfspaces and operators except for intersection (whitespace).
566
    for (int i = 0; i < region_spec.size();) {
147,808✔
567
      if (region_spec[i] == '(') {
122,648✔
568
        expression_.push_back(OP_LEFT_PAREN);
1,290✔
569
        i++;
1,290✔
570

571
      } else if (region_spec[i] == ')') {
121,358✔
572
        expression_.push_back(OP_RIGHT_PAREN);
1,290✔
573
        i++;
1,290✔
574

575
      } else if (region_spec[i] == '|') {
120,068✔
576
        expression_.push_back(OP_UNION);
3,823✔
577
        i++;
3,823✔
578

579
      } else if (region_spec[i] == '~') {
116,245✔
580
        expression_.push_back(OP_COMPLEMENT);
30✔
581
        i++;
30✔
582

583
      } else if (region_spec[i] == '-' || region_spec[i] == '+' ||
196,147!
584
                 std::isdigit(region_spec[i])) {
79,932✔
585
        // This is the start of a halfspace specification.  Iterate j until we
586
        // find the end, then push-back everything between i and j.
587
        int j = i + 1;
68,030✔
588
        while (j < region_spec.size() && std::isdigit(region_spec[j])) {
137,540✔
589
          j++;
69,510✔
590
        }
591
        expression_.push_back(std::stoi(region_spec.substr(i, j - i)));
136,060✔
592
        i = j;
68,030✔
593

594
      } else if (std::isspace(region_spec[i])) {
48,185!
595
        i++;
48,185✔
596

597
      } else {
UNCOV
598
        auto err_msg =
×
599
          fmt::format("Region specification contains invalid character, \"{}\"",
600
            region_spec[i]);
×
601
        fatal_error(err_msg);
×
602
      }
×
603
    }
604

605
    // Add in intersection operators where a missing operator is needed.
606
    int i = 0;
607
    while (i < expression_.size() - 1) {
113,510✔
608
      bool left_compat {
88,350✔
609
        (expression_[i] < OP_UNION) || (expression_[i] == OP_RIGHT_PAREN)};
88,350✔
610
      bool right_compat {(expression_[i + 1] < OP_UNION) ||
88,350✔
611
                         (expression_[i + 1] == OP_LEFT_PAREN) ||
88,350✔
612
                         (expression_[i + 1] == OP_COMPLEMENT)};
5,173✔
613
      if (left_compat && right_compat) {
88,350✔
614
        expression_.insert(expression_.begin() + i + 1, OP_INTERSECTION);
39,047✔
615
      }
616
      i++;
617
    }
618

619
    // Remove complement operators using DeMorgan's laws
620
    auto it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
25,160✔
621
    while (it != expression_.end()) {
25,190✔
622
      // Erase complement
623
      expression_.erase(it);
30✔
624

625
      // Define stop given left parenthesis or not
626
      auto stop = it;
30✔
627
      if (*it == OP_LEFT_PAREN) {
30!
628
        int depth = 1;
629
        do {
240✔
630
          stop++;
240✔
631
          if (*stop > OP_COMPLEMENT) {
240✔
632
            if (*stop == OP_RIGHT_PAREN) {
30!
633
              depth--;
30✔
634
            } else {
635
              depth++;
×
636
            }
637
          }
638
        } while (depth > 0);
240✔
639
        it++;
30✔
640
      }
641

642
      // apply DeMorgan's law to any surfaces/operators between these
643
      // positions in the RPN
644
      apply_demorgan(it, stop);
30✔
645
      // update iterator position
646
      it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
30✔
647
    }
648

649
    // Convert user IDs to surface indices.
650
    for (auto& r : expression_) {
138,640✔
651
      if (r < OP_UNION) {
113,480✔
652
        const auto& it {model::surface_map.find(abs(r))};
68,030!
653
        if (it == model::surface_map.end()) {
68,030!
654
          throw std::runtime_error {
×
655
            "Invalid surface ID " + std::to_string(abs(r)) +
×
656
            " specified in region for cell " + std::to_string(cell_id) + "."};
×
657
        }
658
        r = (r > 0) ? it->second + 1 : -(it->second + 1);
68,030✔
659
      }
660
    }
661

662
    // Check if this is a simple cell.
663
    simple_ = true;
25,160✔
664
    for (int32_t token : expression_) {
125,610✔
665
      if (token == OP_UNION) {
101,638✔
666
        simple_ = false;
1,188✔
667
        // Ensure intersections have precedence over unions
668
        enforce_precedence();
1,188✔
669
        break;
670
      }
671
    }
672

673
    // If this cell is simple, remove all the superfluous operator tokens.
674
    if (simple_) {
25,160✔
675
      for (auto it = expression_.begin(); it != expression_.end(); it++) {
118,064✔
676
        if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT) {
94,092!
677
          expression_.erase(it);
35,060✔
678
          it--;
94,092✔
679
        }
680
      }
681
    }
682
    expression_.shrink_to_fit();
25,160✔
683

684
  } else {
685
    simple_ = true;
8,979✔
686
  }
687
}
34,139✔
688

689
//==============================================================================
690

691
void Region::apply_demorgan(
30✔
692
  vector<int32_t>::iterator start, vector<int32_t>::iterator stop)
693
{
694
  do {
210✔
695
    if (*start < OP_UNION) {
210✔
696
      *start *= -1;
120✔
697
    } else if (*start == OP_UNION) {
90!
698
      *start = OP_INTERSECTION;
×
699
    } else if (*start == OP_INTERSECTION) {
90!
700
      *start = OP_UNION;
90✔
701
    }
702
    start++;
210✔
703
  } while (start < stop);
210✔
704
}
30✔
705

706
//==============================================================================
707
//! Add precedence for infix regions so intersections have higher
708
//! precedence than unions using parentheses.
709
//==============================================================================
710

711
void Region::add_parentheses(int64_t start)
96✔
712
{
713
  int32_t start_token = expression_[start];
96!
714
  // Add left parenthesis and set new position to be after parenthesis
715
  if (start_token == OP_UNION) {
96!
716
    start += 2;
×
717
  }
718
  expression_.insert(expression_.begin() + start - 1, OP_LEFT_PAREN);
96✔
719

720
  // Add right parenthesis
721
  // While the start iterator is within the bounds of infix
722
  while (start + 1 < expression_.size()) {
430✔
723
    start++;
408✔
724

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

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

771
void Region::enforce_precedence()
1,188✔
772
{
773
  // Stack tracking the operator type at each depth (0 = no operator seen yet)
774
  vector<int32_t> op_stack = {0};
1,188✔
775

776
  // Stack tracking where the operator sequence started at each depth
777
  vector<std::size_t> pos_stack = {0};
1,188✔
778

779
  for (int64_t i = 0; i < expression_.size(); ++i) {
21,593✔
780
    int32_t token = expression_[i];
20,405✔
781

782
    if (token == OP_LEFT_PAREN) {
20,405✔
783
      // Entering a new parenthesis level - push new tracking state
784
      op_stack.push_back(0);
1,486✔
785
      pos_stack.push_back(0);
1,486✔
786
      continue;
1,486✔
787
    } else if (token == OP_RIGHT_PAREN) {
18,919✔
788
      // Exiting a parenthesis level - pop tracking state (keep at least one)
789
      if (op_stack.size() > 1) {
1,445!
790
        op_stack.pop_back();
1,445✔
791
        pos_stack.pop_back();
1,445✔
792
      }
793
      continue;
1,445✔
794
    }
795

796
    if (token == OP_UNION || token == OP_INTERSECTION) {
17,474✔
797
      if (op_stack.back() == 0) {
8,143✔
798
        // First operator at this depth - record it and its position
799
        op_stack.back() = token;
2,718✔
800
        pos_stack.back() = i;
2,718✔
801
      } else if (token != op_stack.back()) {
5,425✔
802
        // Encountered a different operator at the same depth - need to add
803
        // parentheses to enforce precedence. Intersection has higher
804
        // precedence, so we parenthesize the intersection terms.
805
        if (op_stack.back() == OP_INTERSECTION) {
96✔
806
          add_parentheses(pos_stack.back());
48✔
807
        } else {
808
          add_parentheses(i);
48✔
809
        }
810

811
        // Restart the scan since we modified the expression
812
        i = -1; // Will be incremented to 0 by the for loop
96✔
813
        op_stack = {0};
96✔
814
        pos_stack = {0};
96✔
815
      }
816
    }
817
  }
818
}
1,188✔
819

820
//==============================================================================
821
//! Convert infix region specification to Reverse Polish Notation (RPN)
822
//!
823
//! This function uses the shunting-yard algorithm.
824
//==============================================================================
825

826
vector<int32_t> Region::generate_postfix(int32_t cell_id) const
44✔
827
{
828
  vector<int32_t> rpn;
44✔
829
  vector<int32_t> stack;
44✔
830

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

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

855
      stack.push_back(token);
352✔
856

857
    } else if (token == OP_LEFT_PAREN) {
198✔
858
      // If the token is a left parenthesis, push it onto the stack
859
      stack.push_back(token);
99✔
860

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

876
      // Pop the left parenthesis.
877
      stack.pop_back();
946✔
878
    }
879
  }
880

881
  while (stack.size() > 0) {
44✔
882
    int32_t op = stack.back();
44!
883

884
    // If the operator is a parenthesis it is mismatched.
885
    if (op >= OP_RIGHT_PAREN) {
44!
886
      fatal_error(fmt::format(
×
887
        "Mismatched parentheses in region specification for cell {}", cell_id));
888
    }
889

890
    rpn.push_back(stack.back());
44✔
891
    stack.pop_back();
88✔
892
  }
893

894
  return rpn;
44✔
895
}
44✔
896

897
//==============================================================================
898

899
std::string Region::str() const
28,446✔
900
{
901
  std::stringstream region_spec {};
28,446✔
902
  if (!expression_.empty()) {
28,446✔
903
    for (int32_t token : expression_) {
84,589✔
904
      if (token == OP_LEFT_PAREN) {
64,439✔
905
        region_spec << " (";
1,218✔
906
      } else if (token == OP_RIGHT_PAREN) {
63,221✔
907
        region_spec << " )";
1,218✔
908
      } else if (token == OP_COMPLEMENT) {
62,003!
909
        region_spec << " ~";
×
910
      } else if (token == OP_INTERSECTION) {
62,003✔
911
      } else if (token == OP_UNION) {
58,662✔
912
        region_spec << " |";
3,417✔
913
      } else {
914
        // Note the off-by-one indexing
915
        auto surf_id = model::surfaces[abs(token) - 1]->id_;
55,245✔
916
        region_spec << " " << ((token > 0) ? surf_id : -surf_id);
55,245✔
917
      }
918
    }
919
  }
920
  return region_spec.str();
56,892✔
921
}
28,446✔
922

923
//==============================================================================
924

925
std::pair<double, int32_t> Region::distance(
2,147,483,647✔
926
  Position r, Direction u, int32_t on_surface) const
927
{
928
  double min_dist {INFTY};
2,147,483,647✔
929
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
2,147,483,647✔
930

931
  for (int32_t token : expression_) {
2,147,483,647✔
932
    // Ignore this token if it corresponds to an operator rather than a region.
933
    if (token >= OP_UNION)
2,147,483,647✔
934
      continue;
174,174,736✔
935

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

941
    // Check if this distance is the new minimum.
942
    if (d < min_dist) {
2,147,483,647✔
943
      if (min_dist - d >= FP_PRECISION * min_dist) {
2,147,483,647!
944
        min_dist = d;
2,147,483,647✔
945
        i_surf = -token;
2,147,483,647✔
946
      }
947
    }
948
  }
949

950
  return {min_dist, i_surf};
2,147,483,647✔
951
}
952

953
//==============================================================================
954

955
bool Region::contains(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
956
{
957
  if (simple_) {
2,147,483,647✔
958
    return contains_simple(r, u, on_surface);
2,147,483,647✔
959
  } else {
960
    return contains_complex(r, u, on_surface);
7,413,725✔
961
  }
962
}
963

964
//==============================================================================
965

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

987
//==============================================================================
988

989
bool Region::contains_complex(Position r, Direction u, int32_t on_surface) const
7,413,725✔
990
{
991
  bool in_cell = true;
7,413,725✔
992
  int total_depth = 0;
7,413,725✔
993

994
  // For each token
995
  for (auto it = expression_.begin(); it != expression_.end(); it++) {
116,373,668✔
996
    int32_t token = *it;
110,908,261✔
997

998
    // If the token is a surface evaluate the sense
999
    // If the token is a union or intersection check to
1000
    // short circuit
1001
    if (token < OP_UNION) {
110,908,261✔
1002
      if (token == on_surface) {
48,477,854✔
1003
        in_cell = true;
1004
      } else if (-token == on_surface) {
44,818,014✔
1005
        in_cell = false;
1006
      } else {
1007
        // Note the off-by-one indexing
1008
        bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
44,108,003✔
1009
        in_cell = (sense == (token > 0));
44,108,003✔
1010
      }
1011
    } else if ((token == OP_UNION && in_cell == true) ||
62,430,407✔
1012
               (token == OP_INTERSECTION && in_cell == false)) {
30,059,805✔
1013
      // If the total depth is zero return
1014
      if (total_depth == 0) {
6,678,508✔
1015
        return in_cell;
1,948,318✔
1016
      }
1017

1018
      total_depth--;
4,730,190✔
1019

1020
      // While the iterator is within the bounds of the vector
1021
      int depth = 1;
4,730,190✔
1022
      do {
29,497,892✔
1023
        // Get next token
1024
        it++;
29,497,892✔
1025
        int32_t next_token = *it;
29,497,892✔
1026

1027
        // If the token is an a parenthesis
1028
        if (next_token > OP_COMPLEMENT) {
29,497,892✔
1029
          // Adjust depth accordingly
1030
          if (next_token == OP_RIGHT_PAREN) {
4,921,284✔
1031
            depth--;
4,825,737✔
1032
          } else {
1033
            depth++;
95,547✔
1034
          }
1035
        }
1036
      } while (depth > 0);
29,497,892✔
1037
    } else if (token == OP_LEFT_PAREN) {
55,751,899✔
1038
      total_depth++;
9,708,980✔
1039
    } else if (token == OP_RIGHT_PAREN) {
46,042,919✔
1040
      total_depth--;
4,978,790✔
1041
    }
1042
  }
1043
  return in_cell;
1044
}
1045

1046
//==============================================================================
1047

1048
BoundingBox Region::bounding_box(int32_t cell_id) const
88✔
1049
{
1050
  if (simple_) {
88✔
1051
    return bounding_box_simple();
44✔
1052
  } else {
1053
    auto postfix = generate_postfix(cell_id);
44✔
1054
    return bounding_box_complex(postfix);
88✔
1055
  }
44✔
1056
}
1057

1058
//==============================================================================
1059

1060
BoundingBox Region::bounding_box_simple() const
44✔
1061
{
1062
  BoundingBox bbox;
44✔
1063
  for (int32_t token : expression_) {
176✔
1064
    bbox &= model::surfaces[abs(token) - 1]->bounding_box(token > 0);
132✔
1065
  }
1066
  return bbox;
44✔
1067
}
1068

1069
//==============================================================================
1070

1071
BoundingBox Region::bounding_box_complex(vector<int32_t> postfix) const
44✔
1072
{
1073
  vector<BoundingBox> stack(postfix.size());
44✔
1074
  int i_stack = -1;
44✔
1075

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

1089
  assert(i_stack == 0);
44!
1090
  return stack.front();
44✔
1091
}
44✔
1092

1093
//==============================================================================
1094

1095
vector<int32_t> Region::surfaces() const
5,270✔
1096
{
1097
  if (simple_) {
5,270✔
1098
    return expression_;
5,250✔
1099
  }
1100

1101
  vector<int32_t> surfaces = expression_;
20✔
1102

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

1106
  while (it != surfaces.end()) {
60✔
1107
    surfaces.erase(it);
40✔
1108

1109
    it = std::find_if(surfaces.begin(), surfaces.end(),
40✔
1110
      [&](const auto& value) { return value >= OP_UNION; });
80!
1111
  }
1112

1113
  return surfaces;
20✔
1114
}
5,270✔
1115

1116
//==============================================================================
1117
// Non-method functions
1118
//==============================================================================
1119

1120
void read_cells(pugi::xml_node node)
8,143✔
1121
{
1122
  // Count the number of cells.
1123
  int n_cells = 0;
8,143✔
1124
  for (pugi::xml_node cell_node : node.children("cell")) {
42,183✔
1125
    n_cells++;
34,040✔
1126
  }
1127

1128
  // Loop over XML cell elements and populate the array.
1129
  model::cells.reserve(n_cells);
8,143✔
1130
  for (pugi::xml_node cell_node : node.children("cell")) {
42,183✔
1131
    model::cells.push_back(make_unique<CSGCell>(cell_node));
34,040✔
1132
  }
1133

1134
  // Fill the cell map.
1135
  for (int i = 0; i < model::cells.size(); i++) {
42,183✔
1136
    int32_t id = model::cells[i]->id_;
34,040!
1137
    auto search = model::cell_map.find(id);
34,040!
1138
    if (search == model::cell_map.end()) {
34,040!
1139
      model::cell_map[id] = i;
34,040✔
1140
    } else {
1141
      fatal_error(
×
1142
        fmt::format("Two or more cells use the same unique ID: {}", id));
×
1143
    }
1144
  }
1145

1146
  read_dagmc_universes(node);
8,143✔
1147

1148
  populate_universes();
8,141✔
1149

1150
  // Allocate the cell overlap count if necessary.
1151
  if (settings::check_overlaps) {
8,141✔
1152
    model::overlap_check_count.resize(model::cells.size(), 0);
119✔
1153
  }
1154

1155
  if (model::cells.size() == 0) {
8,141!
1156
    fatal_error("No cells were found in the geometry.xml file");
×
1157
  }
1158
}
8,141✔
1159

1160
void populate_universes()
8,143✔
1161
{
1162
  // Used to map universe index to the index of an implicit complement cell for
1163
  // DAGMC universes
1164
  std::unordered_map<int, int> implicit_comp_cells;
8,143✔
1165

1166
  // Populate the Universe vector and map.
1167
  for (int index_cell = 0; index_cell < model::cells.size(); index_cell++) {
42,399✔
1168
    int32_t uid = model::cells[index_cell]->universe_;
34,256✔
1169
    auto it = model::universe_map.find(uid);
34,256✔
1170
    if (it == model::universe_map.end()) {
34,256✔
1171
      model::universes.push_back(make_unique<Universe>());
39,358✔
1172
      model::universes.back()->id_ = uid;
19,679✔
1173
      model::universes.back()->cells_.push_back(index_cell);
19,679✔
1174
      model::universe_map[uid] = model::universes.size() - 1;
19,679✔
1175
    } else {
1176
#ifdef OPENMC_DAGMC_ENABLED
1177
      // Skip implicit complement cells for now
1178
      Universe* univ = model::universes[it->second].get();
1,974!
1179
      DAGUniverse* dag_univ = dynamic_cast<DAGUniverse*>(univ);
1,974!
1180
      if (dag_univ && (dag_univ->implicit_complement_idx() == index_cell)) {
1,974✔
1181
        implicit_comp_cells[it->second] = index_cell;
46✔
1182
        continue;
46✔
1183
      }
1184
#endif
1185

1186
      model::universes[it->second]->cells_.push_back(index_cell);
14,531✔
1187
    }
1188
  }
1189

1190
  // Add DAGUniverse implicit complement cells last
1191
  for (const auto& it : implicit_comp_cells) {
8,189✔
1192
    int index_univ = it.first;
46✔
1193
    int index_cell = it.second;
46✔
1194
    model::universes[index_univ]->cells_.push_back(index_cell);
46!
1195
  }
1196

1197
  model::universes.shrink_to_fit();
8,143✔
1198
}
8,143✔
1199

1200
//==============================================================================
1201
// C-API functions
1202
//==============================================================================
1203

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

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

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

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

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

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

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

1301
  int32_t instance_index = instance ? *instance : -1;
91✔
1302
  try {
91✔
1303
    *T = model::cells[index]->temperature(instance_index);
91✔
1304
  } catch (const std::exception& e) {
×
1305
    set_errmsg(e.what());
×
1306
    return OPENMC_E_UNASSIGNED;
×
1307
  }
×
1308
  return 0;
91✔
1309
}
1310

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

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

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

1341
//! Get the bounding box of a cell
1342
extern "C" int openmc_cell_bounding_box(
55✔
1343
  const int32_t index, double* llc, double* urc)
1344
{
1345

1346
  BoundingBox bbox;
55✔
1347

1348
  const auto& c = model::cells[index];
55✔
1349
  bbox = c->bounding_box();
55✔
1350

1351
  // set lower left corner values
1352
  llc[0] = bbox.min.x;
55✔
1353
  llc[1] = bbox.min.y;
55✔
1354
  llc[2] = bbox.min.z;
55✔
1355

1356
  // set upper right corner values
1357
  urc[0] = bbox.max.x;
55✔
1358
  urc[1] = bbox.max.y;
55✔
1359
  urc[2] = bbox.max.z;
55✔
1360

1361
  return 0;
55✔
1362
}
1363

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

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

1374
  return 0;
22✔
1375
}
1376

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

1385
  model::cells[index]->set_name(name);
22✔
1386

1387
  return 0;
11✔
1388
}
1389

1390
//==============================================================================
1391
//! Define a containing (parent) cell
1392
//==============================================================================
1393

1394
//! Used to locate a universe fill in the geometry
1395
struct ParentCell {
1396
  bool operator==(const ParentCell& other) const
135✔
1397
  {
1398
    return cell_index == other.cell_index &&
135!
1399
           lattice_index == other.lattice_index;
135!
1400
  }
1401

1402
  bool operator<(const ParentCell& other) const
1403
  {
1404
    return cell_index < other.cell_index ||
1405
           (cell_index == other.cell_index &&
1406
             lattice_index < other.lattice_index);
1407
  }
1408

1409
  int64_t cell_index;
1410
  int64_t lattice_index;
1411
};
1412

1413
//! Structure used to insert ParentCell into hashed STL data structures
1414
struct ParentCellHash {
1415
  std::size_t operator()(const ParentCell& p) const
631✔
1416
  {
1417
    return 4096 * p.cell_index + p.lattice_index;
631!
1418
  }
1419
};
1420

1421
//! Used to manage a traversal stack when locating parent cells of a cell
1422
//! instance in the model
1423
struct ParentCellStack {
106✔
1424

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

1435
  //! removes the last parent_cell and clears the visited cells for the popped
1436
  //! cell's universe
1437
  void pop()
75✔
1438
  {
1439
    visited_cells_[this->current_univ()].clear();
75✔
1440
    parent_cells_.pop_back();
75✔
1441
  }
75✔
1442

1443
  //! checks whether or not the parent cell has been visited already for this
1444
  //! search universe
1445
  bool visited(int32_t search_universe, const ParentCell& parent_cell)
526✔
1446
  {
1447
    return visited_cells_[search_universe].count(parent_cell) != 0;
526✔
1448
  }
1449

1450
  //! return the next universe to search for a parent cell
1451
  int32_t current_univ() const
75✔
1452
  {
1453
    return model::cells[parent_cells_.back().cell_index]->universe_;
75✔
1454
  }
1455

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

1459
  //! compute an instance for the provided distribcell index
1460
  int32_t compute_instance(int32_t distribcell_index) const
181✔
1461
  {
1462
    if (distribcell_index == C_NONE)
181✔
1463
      return 0;
1464

1465
    int32_t instance = 0;
120✔
1466
    for (const auto& parent_cell : this->parent_cells_) {
225✔
1467
      auto& cell = model::cells[parent_cell.cell_index];
105!
1468
      if (cell->type_ == Fill::UNIVERSE) {
105!
1469
        instance += cell->offset_[distribcell_index];
×
1470
      } else if (cell->type_ == Fill::LATTICE) {
105!
1471
        auto& lattice = model::lattices[cell->fill_];
105✔
1472
        instance +=
105✔
1473
          lattice->offset(distribcell_index, parent_cell.lattice_index);
105✔
1474
      }
1475
    }
1476
    return instance;
1477
  }
1478

1479
  // Accessors
1480
  vector<ParentCell>& parent_cells() { return parent_cells_; }
106✔
1481
  const vector<ParentCell>& parent_cells() const { return parent_cells_; }
1482

1483
  // Data Members
1484
  vector<ParentCell> parent_cells_;
1485
  std::unordered_map<int32_t, std::unordered_set<ParentCell, ParentCellHash>>
1486
    visited_cells_;
1487
};
1488

1489
vector<ParentCell> Cell::find_parent_cells(
×
1490
  int32_t instance, const Position& r) const
1491
{
1492

1493
  // create a temporary particle
1494
  GeometryState dummy_particle {};
×
1495
  dummy_particle.r() = r;
×
1496
  dummy_particle.u() = {0., 0., 1.};
×
1497

1498
  return find_parent_cells(instance, dummy_particle);
×
1499
}
×
1500

1501
vector<ParentCell> Cell::find_parent_cells(
×
1502
  int32_t instance, GeometryState& p) const
1503
{
1504
  // look up the particle's location
1505
  exhaustive_find_cell(p);
×
1506
  const auto& coords = p.coord();
×
1507

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

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

1532
  // if this loop finished because the cell was found and
1533
  // the instance matches the one requested in the call
1534
  // we have the correct path and can return the stack
1535
  if (cell_found &&
×
1536
      stack.compute_instance(this->distribcell_index_) == instance) {
×
1537
    return stack.parent_cells();
×
1538
  }
1539

1540
  // fall back on an exhaustive search for the cell's parents
1541
  return exhaustive_find_parent_cells(instance);
×
1542
}
×
1543

1544
vector<ParentCell> Cell::exhaustive_find_parent_cells(int32_t instance) const
106✔
1545
{
1546
  ParentCellStack stack;
106✔
1547
  // start with this cell's universe
1548
  int32_t prev_univ_idx;
106✔
1549
  int32_t univ_idx = this->universe_;
106✔
1550

1551
  while (true) {
181✔
1552
    const auto& univ = model::universes[univ_idx];
181✔
1553
    prev_univ_idx = univ_idx;
181✔
1554

1555
    // search for a cell that is filled w/ this universe
1556
    for (const auto& cell : model::cells) {
1,159✔
1557
      // if this is a material-filled cell, move on
1558
      if (cell->type_ == Fill::MATERIAL)
1,083✔
1559
        continue;
602✔
1560

1561
      if (cell->type_ == Fill::UNIVERSE) {
481✔
1562
        // if this is in the set of cells previously visited for this universe,
1563
        // move on
1564
        if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE}))
286!
1565
          continue;
×
1566

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

1578
        // start search for universe
1579
        auto lat_it = lattice_univs.begin();
195✔
1580
        while (true) {
465✔
1581
          // find the next lattice cell with this universe
1582
          lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
330✔
1583
          if (lat_it == lattice_univs.end())
330✔
1584
            break;
1585

1586
          int lattice_idx = lat_it - lattice_univs.begin();
240✔
1587

1588
          // move iterator forward one to avoid finding the same entry
1589
          lat_it++;
240✔
1590
          if (stack.visited(
480✔
1591
                univ_idx, {model::cell_map[cell->id_], lattice_idx}))
240✔
1592
            continue;
135✔
1593

1594
          // add this cell and lattice index to the stack and exit loop
1595
          stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
105✔
1596
          univ_idx = cell->universe_;
105✔
1597
          break;
105✔
1598
        }
135✔
1599
      }
1600
      // if we've updated the universe, break
1601
      if (prev_univ_idx != univ_idx)
481✔
1602
        break;
1603
    } // end cell loop search for universe
1604

1605
    // if we're at the top of the geometry and the instance matches, we're done
1606
    if (univ_idx == model::root_universe &&
217!
1607
        stack.compute_instance(this->distribcell_index_) == instance)
181✔
1608
      break;
1609

1610
    // if there is no match on the original cell's universe, report an error
1611
    if (univ_idx == this->universe_) {
75!
1612
      fatal_error(
×
1613
        fmt::format("Could not find the parent cells for cell {}, instance {}.",
×
1614
          this->id_, instance));
×
1615
    }
1616

1617
    // if we don't find a suitable update, adjust the stack and continue
1618
    if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
75!
1619
      stack.pop();
75✔
1620
      univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
75!
1621
    }
1622

1623
  } // end while
1624

1625
  // reverse the stack so the highest cell comes first
1626
  std::reverse(stack.parent_cells().begin(), stack.parent_cells().end());
106✔
1627
  return stack.parent_cells();
212✔
1628
}
106✔
1629

1630
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
151✔
1631
  int32_t instance, Position* hint) const
1632
{
1633
  std::unordered_map<int32_t, vector<int32_t>> contained_cells;
151✔
1634

1635
  // if this is a material-filled cell it has no contained cells
1636
  if (this->type_ == Fill::MATERIAL)
151✔
1637
    return contained_cells;
1638

1639
  // find the pathway through the geometry to this cell
1640
  vector<ParentCell> parent_cells;
106!
1641

1642
  // if a positional hint is provided, attempt to do a fast lookup
1643
  // of the parent cells
1644
  parent_cells = hint ? find_parent_cells(instance, *hint)
106!
1645
                      : exhaustive_find_parent_cells(instance);
106✔
1646

1647
  // if this cell is filled w/ a material, it contains no other cells
1648
  if (type_ != Fill::MATERIAL) {
106!
1649
    this->get_contained_cells_inner(contained_cells, parent_cells);
106✔
1650
  }
1651

1652
  return contained_cells;
106✔
1653
}
151✔
1654

1655
//! Get all cells within this cell
1656
void Cell::get_contained_cells_inner(
82,278✔
1657
  std::unordered_map<int32_t, vector<int32_t>>& contained_cells,
1658
  vector<ParentCell>& parent_cells) const
1659
{
1660

1661
  // filled by material, determine instance based on parent cells
1662
  if (type_ == Fill::MATERIAL) {
82,278✔
1663
    int instance = 0;
81,692✔
1664
    if (this->distribcell_index_ >= 0) {
81,692!
1665
      for (auto& parent_cell : parent_cells) {
245,074✔
1666
        auto& cell = model::cells[parent_cell.cell_index];
163,382✔
1667
        if (cell->type_ == Fill::UNIVERSE) {
163,382✔
1668
          instance += cell->offset_[distribcell_index_];
80,192✔
1669
        } else if (cell->type_ == Fill::LATTICE) {
83,190!
1670
          auto& lattice = model::lattices[cell->fill_];
83,190✔
1671
          instance += lattice->offset(
83,190✔
1672
            this->distribcell_index_, parent_cell.lattice_index);
83,190✔
1673
        }
1674
      }
1675
    }
1676
    // add entry to contained cells
1677
    contained_cells[model::cell_map[id_]].push_back(instance);
81,692✔
1678
    // filled with universe, add the containing cell to the parent cells
1679
    // and recurse
1680
  } else if (type_ == Fill::UNIVERSE) {
586✔
1681
    parent_cells.push_back({model::cell_map[id_], -1});
496✔
1682
    auto& univ = model::universes[fill_];
496✔
1683
    for (auto cell_index : univ->cells_) {
2,973✔
1684
      auto& cell = model::cells[cell_index];
2,477✔
1685
      cell->get_contained_cells_inner(contained_cells, parent_cells);
2,477✔
1686
    }
1687
    parent_cells.pop_back();
496✔
1688
    // filled with a lattice, visit each universe in the lattice
1689
    // with a recursive call to collect the cell instances
1690
  } else if (type_ == Fill::LATTICE) {
90!
1691
    auto& lattice = model::lattices[fill_];
90✔
1692
    for (auto i = lattice->begin(); i != lattice->end(); ++i) {
79,470✔
1693
      auto& univ = model::universes[*i];
79,380✔
1694
      parent_cells.push_back({model::cell_map[id_], i.indx_});
79,380✔
1695
      for (auto cell_index : univ->cells_) {
159,075✔
1696
        auto& cell = model::cells[cell_index];
79,695✔
1697
        cell->get_contained_cells_inner(contained_cells, parent_cells);
79,695✔
1698
      }
1699
      parent_cells.pop_back();
79,380✔
1700
    }
1701
  }
1702
}
82,278✔
1703

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

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

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

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

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

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

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

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

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

1835
extern "C" int cells_size()
44✔
1836
{
1837
  return model::cells.size();
44✔
1838
}
1839

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