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

openmc-dev / openmc / 17598143272

09 Sep 2025 11:15PM UTC coverage: 85.18% (-0.03%) from 85.209%
17598143272

Pull #3546

github

web-flow
Merge f81d18091 into 366509051
Pull Request #3546: Add distributed cell density multipliers

148 of 192 new or added lines in 12 files covered. (77.08%)

57 existing lines in 1 file now uncovered.

53067 of 62300 relevant lines covered (85.18%)

38200638.89 hits per line

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

77.69
/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
772,862✔
44
{
45
  return model::universes[universe_]->n_instances_;
772,862✔
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 rotation matrix.
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
112✔
91
{
92
  if (sqrtkT_.size() < 1) {
112✔
93
    throw std::runtime_error {"Cell temperature has not yet been set."};
×
94
  }
95

96
  if (instance >= 0) {
112✔
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;
101✔
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
    double rho =
108
      rho_mult_.size() == 1 ? rho_mult_.at(0) : rho_mult_.at(instance);
2,147,483,647✔
109
    return rho;
2,147,483,647✔
110
  } else {
111
    return rho_mult_[0];
55✔
112
  }
113
}
114

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

NEW
121
  if (instance >= 0) {
×
122
    double rho =
NEW
123
      rho_mult_.size() == 1 ? rho_mult_.at(0) : rho_mult_.at(instance);
×
NEW
124
    return rho * model::materials[mat_index]->density_gpcc();
×
125
  } else {
NEW
126
    return rho_mult_[0];
×
127
  }
128
}
129

130
void Cell::set_temperature(double T, int32_t instance, bool set_contained)
492✔
131
{
132
  if (settings::temperature_method == TemperatureMethod::INTERPOLATION) {
492✔
133
    if (T < (data::temperature_min - settings::temperature_tolerance)) {
×
134
      throw std::runtime_error {
×
135
        fmt::format("Temperature of {} K is below minimum temperature at "
×
136
                    "which data is available of {} K.",
137
          T, data::temperature_min)};
×
138
    } else if (T > (data::temperature_max + settings::temperature_tolerance)) {
×
139
      throw std::runtime_error {
×
140
        fmt::format("Temperature of {} K is above maximum temperature at "
×
141
                    "which data is available of {} K.",
142
          T, data::temperature_max)};
×
143
    }
144
  }
145

146
  if (type_ == Fill::MATERIAL) {
492✔
147
    if (instance >= 0) {
460✔
148
      // If temperature vector is not big enough, resize it first
149
      if (sqrtkT_.size() != n_instances())
383✔
150
        sqrtkT_.resize(n_instances(), sqrtkT_[0]);
48✔
151

152
      // Set temperature for the corresponding instance
153
      sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T);
383✔
154
    } else {
155
      // Set temperature for all instances
156
      for (auto& T_ : sqrtkT_) {
154✔
157
        T_ = std::sqrt(K_BOLTZMANN * T);
77✔
158
      }
159
    }
160
  } else {
161
    if (!set_contained) {
32✔
162
      throw std::runtime_error {
×
163
        fmt::format("Attempted to set the temperature of cell {} "
×
164
                    "which is not filled by a material.",
165
          id_)};
×
166
    }
167

168
    auto contained_cells = this->get_contained_cells(instance);
32✔
169
    for (const auto& entry : contained_cells) {
128✔
170
      auto& cell = model::cells[entry.first];
96✔
171
      assert(cell->type_ == Fill::MATERIAL);
78✔
172
      auto& instances = entry.second;
96✔
173
      for (auto instance : instances) {
336✔
174
        cell->set_temperature(T, instance);
240✔
175
      }
176
    }
177
  }
32✔
178
}
492✔
179

180
void Cell::set_density(double rho, int32_t instance, bool set_contained)
141✔
181
{
182
  if (type_ != Fill::MATERIAL && !set_contained) {
141✔
NEW
183
    fatal_error(
×
NEW
184
      fmt::format("Attempted to set the density multiplier of cell {} "
×
185
                  "which is not filled by a material.",
NEW
186
        id_));
×
187
  }
188

189
  if (type_ == Fill::MATERIAL) {
141✔
190
    const int32_t mat_index = material(instance);
125✔
191
    if (mat_index == MATERIAL_VOID)
125✔
NEW
192
      return;
×
193

194
    if (instance >= 0) {
125✔
195
      // If density multiplier vector is not big enough, resize it first
196
      if (rho_mult_.size() != n_instances())
59✔
197
        rho_mult_.resize(n_instances(), rho_mult_[0]);
48✔
198

199
      // Set density multiplier for the corresponding instance
200
      rho_mult_.at(instance) = rho / model::materials[mat_index]->density_gpcc();
59✔
201
    } else {
202
      // Set density multiplier for all instances
203
      for (auto& Rho_ : rho_mult_) {
132✔
204
        Rho_ = rho / model::materials[mat_index]->density_gpcc();
66✔
205
      }
206
    }
207
  } else {
208
    auto contained_cells = this->get_contained_cells(instance);
16✔
209
    for (const auto& entry : contained_cells) {
64✔
210
      auto& cell = model::cells[entry.first];
48✔
211
      assert(cell->type_ == Fill::MATERIAL);
39✔
212
      auto& instances = entry.second;
48✔
213
      for (auto instance : instances) {
96✔
214
        cell->set_density(rho, instance);
48✔
215
      }
216
    }
217
  }
16✔
218
}
219

220
void Cell::export_properties_hdf5(hid_t group) const
154✔
221
{
222
  // Create a group for this cell.
223
  auto cell_group = create_group(group, fmt::format("cell {}", id_));
308✔
224

225
  // Write temperature in [K] for one or more cell instances
226
  vector<double> temps;
154✔
227
  for (auto sqrtkT_val : sqrtkT_)
286✔
228
    temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
132✔
229
  write_dataset(cell_group, "temperature", temps);
154✔
230

231
  // Write density multipliers for one or more cell instances
232
  write_dataset(cell_group, "density_mult", rho_mult_);
154✔
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✔
UNCOV
248
    fatal_error(fmt::format(
×
249
      "Number of temperatures for cell {} doesn't match number of instances",
UNCOV
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 density multipliers
261
  if (object_exists(cell_group, "density_mult")) {
176✔
262
    read_dataset(cell_group, "density_mult", rho_mult_);
176✔
263

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

273
  close_group(cell_group);
176✔
274
}
176✔
275

276
void Cell::to_hdf5(hid_t cell_group) const
26,439✔
277
{
278

279
  // Create a group for this cell.
280
  auto group = create_group(cell_group, fmt::format("cell {}", id_));
52,878✔
281

282
  if (!name_.empty()) {
26,439✔
283
    write_string(group, "name", name_, false);
4,872✔
284
  }
285

286
  write_dataset(group, "universe", model::universes[universe_]->id_);
26,439✔
287

288
  to_hdf5_inner(group);
26,439✔
289

290
  // Write fill information.
291
  if (type_ == Fill::MATERIAL) {
26,439✔
292
    write_dataset(group, "fill_type", "material");
21,434✔
293
    std::vector<int32_t> mat_ids;
21,434✔
294
    for (auto i_mat : material_) {
44,462✔
295
      if (i_mat != MATERIAL_VOID) {
23,028✔
296
        mat_ids.push_back(model::materials[i_mat]->id_);
14,957✔
297
      } else {
298
        mat_ids.push_back(MATERIAL_VOID);
8,071✔
299
      }
300
    }
301
    if (mat_ids.size() == 1) {
21,434✔
302
      write_dataset(group, "material", mat_ids[0]);
21,196✔
303
    } else {
304
      write_dataset(group, "material", mat_ids);
238✔
305
    }
306

307
    std::vector<double> temps;
21,434✔
308
    for (auto sqrtkT_val : sqrtkT_)
44,540✔
309
      temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
23,106✔
310
    write_dataset(group, "temperature", temps);
21,434✔
311

312
    write_dataset(group, "density_mult", rho_mult_);
21,434✔
313

314
  } else if (type_ == Fill::UNIVERSE) {
26,439✔
315
    write_dataset(group, "fill_type", "universe");
3,561✔
316
    write_dataset(group, "fill", model::universes[fill_]->id_);
3,561✔
317
    if (translation_ != Position(0, 0, 0)) {
3,561✔
318
      write_dataset(group, "translation", translation_);
1,837✔
319
    }
320
    if (!rotation_.empty()) {
3,561✔
321
      if (rotation_.size() == 12) {
264✔
322
        std::array<double, 3> rot {rotation_[9], rotation_[10], rotation_[11]};
264✔
323
        write_dataset(group, "rotation", rot);
264✔
324
      } else {
UNCOV
325
        write_dataset(group, "rotation", rotation_);
×
326
      }
327
    }
328

329
  } else if (type_ == Fill::LATTICE) {
1,444✔
330
    write_dataset(group, "fill_type", "lattice");
1,444✔
331
    write_dataset(group, "lattice", model::lattices[fill_]->id_);
1,444✔
332
  }
333

334
  close_group(group);
26,439✔
335
}
26,439✔
336

337
//==============================================================================
338
// CSGCell implementation
339
//==============================================================================
340

341
CSGCell::CSGCell(pugi::xml_node cell_node)
32,500✔
342
{
343
  if (check_for_node(cell_node, "id")) {
32,500✔
344
    id_ = std::stoi(get_node_value(cell_node, "id"));
32,500✔
345
  } else {
UNCOV
346
    fatal_error("Must specify id of cell in geometry XML file.");
×
347
  }
348

349
  if (check_for_node(cell_node, "name")) {
32,500✔
350
    name_ = get_node_value(cell_node, "name");
6,891✔
351
  }
352

353
  if (check_for_node(cell_node, "universe")) {
32,500✔
354
    universe_ = std::stoi(get_node_value(cell_node, "universe"));
31,277✔
355
  } else {
356
    universe_ = 0;
1,223✔
357
  }
358

359
  // Make sure that either material or fill was specified, but not both.
360
  bool fill_present = check_for_node(cell_node, "fill");
32,500✔
361
  bool material_present = check_for_node(cell_node, "material");
32,500✔
362
  if (!(fill_present || material_present)) {
32,500✔
UNCOV
363
    fatal_error(
×
364
      fmt::format("Neither material nor fill was specified for cell {}", id_));
×
365
  }
366
  if (fill_present && material_present) {
32,500✔
UNCOV
367
    fatal_error(fmt::format("Cell {} has both a material and a fill specified; "
×
368
                            "only one can be specified per cell",
UNCOV
369
      id_));
×
370
  }
371

372
  if (fill_present) {
32,500✔
373
    fill_ = std::stoi(get_node_value(cell_node, "fill"));
6,950✔
374
    if (fill_ == universe_) {
6,950✔
UNCOV
375
      fatal_error(fmt::format("Cell {} is filled with the same universe that "
×
376
                              "it is contained in.",
UNCOV
377
        id_));
×
378
    }
379
  } else {
380
    fill_ = C_NONE;
25,550✔
381
  }
382

383
  // Read the material element.  There can be zero materials (filled with a
384
  // universe), more than one material (distribmats), and some materials may
385
  // be "void".
386
  if (material_present) {
32,500✔
387
    vector<std::string> mats {
388
      get_node_array<std::string>(cell_node, "material", true)};
25,550✔
389
    if (mats.size() > 0) {
25,550✔
390
      material_.reserve(mats.size());
25,550✔
391
      for (std::string mat : mats) {
52,685✔
392
        if (mat.compare("void") == 0) {
27,135✔
393
          material_.push_back(MATERIAL_VOID);
8,418✔
394
        } else {
395
          material_.push_back(std::stoi(mat));
18,717✔
396
        }
397
      }
27,135✔
398
    } else {
UNCOV
399
      fatal_error(fmt::format(
×
400
        "An empty material element was specified for cell {}", id_));
×
401
    }
402
  }
25,550✔
403

404
  // Read the temperature element which may be distributed like materials.
405
  if (check_for_node(cell_node, "temperature")) {
32,500✔
406
    sqrtkT_ = get_node_array<double>(cell_node, "temperature");
315✔
407
    sqrtkT_.shrink_to_fit();
315✔
408

409
    // Make sure this is a material-filled cell.
410
    if (material_.size() == 0) {
315✔
UNCOV
411
      fatal_error(fmt::format(
×
412
        "Cell {} was specified with a temperature but no material. Temperature"
413
        "specification is only valid for cells filled with a material.",
UNCOV
414
        id_));
×
415
    }
416

417
    // Make sure all temperatures are non-negative.
418
    for (auto T : sqrtkT_) {
678✔
419
      if (T < 0) {
363✔
UNCOV
420
        fatal_error(fmt::format(
×
421
          "Cell {} was specified with a negative temperature", id_));
×
422
      }
423
    }
424

425
    // Convert to sqrt(k*T).
426
    for (auto& T : sqrtkT_) {
678✔
427
      T = std::sqrt(K_BOLTZMANN * T);
363✔
428
    }
429
  }
430

431
  // Read the density element which can be distributed similar to temperature.
432
  // These get assigned to the density multiplier, requiring a division by
433
  // the material density.
434
  // Note: calculating the actual density multiplier is deferred until materials
435
  // are finalized. rho_mult_ contains the true density in the meantime.
436
  if (check_for_node(cell_node, "density")) {
32,500✔
437
    rho_mult_ = get_node_array<double>(cell_node, "density");
16✔
438
    rho_mult_.shrink_to_fit();
16✔
439
    xml_set_density_ = true;
16✔
440

441
    // Make sure this is a material-filled cell.
442
    if (material_.size() == 0) {
16✔
NEW
443
      fatal_error(fmt::format(
×
444
        "Cell {} was specified with a density but no material. Density"
445
        "specification is only valid for cells filled with a material.",
NEW
446
        id_));
×
447
    }
448

449
    // Make sure this is a non-void material.
450
    for (auto mat_id : material_) {
32✔
451
      if (mat_id == MATERIAL_VOID) {
16✔
NEW
452
        fatal_error(fmt::format(
×
453
          "Cell {} was specified with a density, but contains a void "
454
          "material. Density specification is only valid for cells "
455
          "filled with a non-void material.",
NEW
456
          id_));
×
457
      }
458
    }
459

460
    // Make sure all densities are non-negative and greater than zero.
461
    for (auto rho : rho_mult_) {
80✔
462
      if (rho <= 0) {
64✔
NEW
463
        fatal_error(fmt::format(
×
464
          "Cell {} was specified with a density less than or equal to zero",
NEW
465
          id_));
×
466
      }
467
    }
468
  }
469

470
  // Read the region specification.
471
  std::string region_spec;
32,500✔
472
  if (check_for_node(cell_node, "region")) {
32,500✔
473
    region_spec = get_node_value(cell_node, "region");
23,721✔
474
  }
475

476
  // Get a tokenized representation of the region specification and apply De
477
  // Morgans law
478
  Region region(region_spec, id_);
32,500✔
479
  region_ = region;
32,500✔
480

481
  // Read the translation vector.
482
  if (check_for_node(cell_node, "translation")) {
32,500✔
483
    if (fill_ == C_NONE) {
2,743✔
UNCOV
484
      fatal_error(fmt::format("Cannot apply a translation to cell {}"
×
485
                              " because it is not filled with another universe",
UNCOV
486
        id_));
×
487
    }
488

489
    auto xyz {get_node_array<double>(cell_node, "translation")};
2,743✔
490
    if (xyz.size() != 3) {
2,743✔
UNCOV
491
      fatal_error(
×
492
        fmt::format("Non-3D translation vector applied to cell {}", id_));
×
493
    }
494
    translation_ = xyz;
2,743✔
495
  }
2,743✔
496

497
  // Read the rotation transform.
498
  if (check_for_node(cell_node, "rotation")) {
32,500✔
499
    auto rot {get_node_array<double>(cell_node, "rotation")};
416✔
500
    set_rotation(rot);
416✔
501
  }
416✔
502
}
32,500✔
503

504
//==============================================================================
505

506
void CSGCell::to_hdf5_inner(hid_t group_id) const
25,969✔
507
{
508
  write_string(group_id, "geom_type", "csg", false);
25,969✔
509
  write_string(group_id, "region", region_.str(), false);
25,969✔
510
}
25,969✔
511

512
//==============================================================================
513

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

525
    // decrement parenthesis level if there are two adjacent surfaces
UNCOV
526
    if (one < OP_UNION && two < OP_UNION) {
×
527
      parenthesis_level--;
×
528
      // increment if there are two adjacent operators
UNCOV
529
    } else if (one >= OP_UNION && two >= OP_UNION) {
×
530
      parenthesis_level++;
×
531
    }
532

533
    // if the level gets to zero, return the position
UNCOV
534
    if (parenthesis_level == 0) {
×
535
      // move the iterator back one before leaving the loop
536
      // so that all tokens in the parenthesis block are included
UNCOV
537
      it--;
×
538
      break;
×
539
    }
540

541
    // continue loop, one token at a time
UNCOV
542
    it--;
×
543
  }
UNCOV
544
  return it;
×
545
}
546

547
//==============================================================================
548
// Region implementation
549
//==============================================================================
550

551
Region::Region(std::string region_spec, int32_t cell_id)
32,500✔
552
{
553
  // Check if region_spec is not empty.
554
  if (!region_spec.empty()) {
32,500✔
555
    // Parse all halfspaces and operators except for intersection (whitespace).
556
    for (int i = 0; i < region_spec.size();) {
142,002✔
557
      if (region_spec[i] == '(') {
118,281✔
558
        expression_.push_back(OP_LEFT_PAREN);
1,819✔
559
        i++;
1,819✔
560

561
      } else if (region_spec[i] == ')') {
116,462✔
562
        expression_.push_back(OP_RIGHT_PAREN);
1,819✔
563
        i++;
1,819✔
564

565
      } else if (region_spec[i] == '|') {
114,643✔
566
        expression_.push_back(OP_UNION);
4,015✔
567
        i++;
4,015✔
568

569
      } else if (region_spec[i] == '~') {
110,628✔
570
        expression_.push_back(OP_COMPLEMENT);
32✔
571
        i++;
32✔
572

573
      } else if (region_spec[i] == '-' || region_spec[i] == '+' ||
186,805✔
574
                 std::isdigit(region_spec[i])) {
76,209✔
575
        // This is the start of a halfspace specification.  Iterate j until we
576
        // find the end, then push-back everything between i and j.
577
        int j = i + 1;
64,356✔
578
        while (j < region_spec.size() && std::isdigit(region_spec[j])) {
130,866✔
579
          j++;
66,510✔
580
        }
581
        expression_.push_back(std::stoi(region_spec.substr(i, j - i)));
64,356✔
582
        i = j;
64,356✔
583

584
      } else if (std::isspace(region_spec[i])) {
46,240✔
585
        i++;
46,240✔
586

587
      } else {
588
        auto err_msg =
589
          fmt::format("Region specification contains invalid character, \"{}\"",
UNCOV
590
            region_spec[i]);
×
591
        fatal_error(err_msg);
×
592
      }
×
593
    }
594

595
    // Add in intersection operators where a missing operator is needed.
596
    int i = 0;
23,721✔
597
    while (i < expression_.size() - 1) {
108,661✔
598
      bool left_compat {
599
        (expression_[i] < OP_UNION) || (expression_[i] == OP_RIGHT_PAREN)};
84,940✔
600
      bool right_compat {(expression_[i + 1] < OP_UNION) ||
84,940✔
601
                         (expression_[i + 1] == OP_LEFT_PAREN) ||
90,838✔
602
                         (expression_[i + 1] == OP_COMPLEMENT)};
5,898✔
603
      if (left_compat && right_compat) {
84,940✔
604
        expression_.insert(expression_.begin() + i + 1, OP_INTERSECTION);
36,620✔
605
      }
606
      i++;
84,940✔
607
    }
608

609
    // Remove complement operators using DeMorgan's laws
610
    auto it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
23,721✔
611
    while (it != expression_.end()) {
23,753✔
612
      // Erase complement
613
      expression_.erase(it);
32✔
614

615
      // Define stop given left parenthesis or not
616
      auto stop = it;
32✔
617
      if (*it == OP_LEFT_PAREN) {
32✔
618
        int depth = 1;
32✔
619
        do {
620
          stop++;
256✔
621
          if (*stop > OP_COMPLEMENT) {
256✔
622
            if (*stop == OP_RIGHT_PAREN) {
32✔
623
              depth--;
32✔
624
            } else {
UNCOV
625
              depth++;
×
626
            }
627
          }
628
        } while (depth > 0);
256✔
629
        it++;
32✔
630
      }
631

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

639
    // Convert user IDs to surface indices.
640
    for (auto& r : expression_) {
132,350✔
641
      if (r < OP_UNION) {
108,629✔
642
        const auto& it {model::surface_map.find(abs(r))};
64,356✔
643
        if (it == model::surface_map.end()) {
64,356✔
UNCOV
644
          throw std::runtime_error {
×
645
            "Invalid surface ID " + std::to_string(abs(r)) +
×
646
            " specified in region for cell " + std::to_string(cell_id) + "."};
×
647
        }
648
        r = (r > 0) ? it->second + 1 : -(it->second + 1);
64,356✔
649
      }
650
    }
651

652
    // Check if this is a simple cell.
653
    simple_ = true;
23,721✔
654
    for (int32_t token : expression_) {
116,501✔
655
      if (token == OP_UNION) {
93,969✔
656
        simple_ = false;
1,189✔
657
        // Ensure intersections have precedence over unions
658
        add_precedence();
1,189✔
659
        break;
1,189✔
660
      }
661
    }
662

663
    // If this cell is simple, remove all the superfluous operator tokens.
664
    if (simple_) {
23,721✔
665
      for (auto it = expression_.begin(); it != expression_.end(); it++) {
109,062✔
666
        if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT) {
86,530✔
667
          expression_.erase(it);
31,999✔
668
          it--;
31,999✔
669
        }
670
      }
671
    }
672
    expression_.shrink_to_fit();
23,721✔
673

674
  } else {
675
    simple_ = true;
8,779✔
676
  }
677
}
32,500✔
678

679
//==============================================================================
680

681
void Region::apply_demorgan(
224✔
682
  vector<int32_t>::iterator start, vector<int32_t>::iterator stop)
683
{
684
  do {
685
    if (*start < OP_UNION) {
224✔
686
      *start *= -1;
128✔
687
    } else if (*start == OP_UNION) {
96✔
UNCOV
688
      *start = OP_INTERSECTION;
×
689
    } else if (*start == OP_INTERSECTION) {
96✔
690
      *start = OP_UNION;
96✔
691
    }
692
    start++;
224✔
693
  } while (start < stop);
224✔
694
}
32✔
695

696
//==============================================================================
697
//! Add precedence for infix regions so intersections have higher
698
//! precedence than unions using parentheses.
699
//==============================================================================
700

701
int64_t Region::add_parentheses(int64_t start)
32✔
702
{
703
  int32_t start_token = expression_[start];
32✔
704
  // Add left parenthesis and set new position to be after parenthesis
705
  if (start_token == OP_UNION) {
32✔
UNCOV
706
    start += 2;
×
707
  }
708
  expression_.insert(expression_.begin() + start - 1, OP_LEFT_PAREN);
32✔
709

710
  // Keep track of return iterator distance. If we don't encounter a left
711
  // parenthesis, we return an iterator corresponding to wherever the right
712
  // parenthesis is inserted. If a left parenthesis is encountered, an iterator
713
  // corresponding to the left parenthesis is returned. Also note that we keep
714
  // track of a *distance* instead of an iterator because the underlying memory
715
  // allocation may change.
716
  std::size_t return_it_dist = 0;
32✔
717

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

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

766
//==============================================================================
767

768
void Region::add_precedence()
1,189✔
769
{
770
  int32_t current_op = 0;
1,189✔
771
  std::size_t current_dist = 0;
1,189✔
772

773
  for (int64_t i = 0; i < expression_.size(); i++) {
23,256✔
774
    int32_t token = expression_[i];
22,067✔
775

776
    if (token == OP_UNION || token == OP_INTERSECTION) {
22,067✔
777
      if (current_op == 0) {
8,620✔
778
        // Set the current operator if is hasn't been set
779
        current_op = token;
3,088✔
780
        current_dist = i;
3,088✔
781
      } else if (token != current_op) {
5,532✔
782
        // If the current operator doesn't match the token, add parenthesis to
783
        // assert precedence
784
        if (current_op == OP_INTERSECTION) {
32✔
785
          i = add_parentheses(current_dist);
16✔
786
        } else {
787
          i = add_parentheses(i);
16✔
788
        }
789
        current_op = 0;
32✔
790
        current_dist = 0;
32✔
791
      }
792
    } else if (token > OP_COMPLEMENT) {
13,447✔
793
      // If the token is a parenthesis reset the current operator
794
      current_op = 0;
3,670✔
795
      current_dist = 0;
3,670✔
796
    }
797
  }
798
}
1,189✔
799

800
//==============================================================================
801
//! Convert infix region specification to Reverse Polish Notation (RPN)
802
//!
803
//! This function uses the shunting-yard algorithm.
804
//==============================================================================
805

806
vector<int32_t> Region::generate_postfix(int32_t cell_id) const
112✔
807
{
808
  vector<int32_t> rpn;
112✔
809
  vector<int32_t> stack;
112✔
810

811
  for (int32_t token : expression_) {
2,520✔
812
    if (token < OP_UNION) {
2,408✔
813
      // If token is not an operator, add it to output
814
      rpn.push_back(token);
1,008✔
815
    } else if (token < OP_RIGHT_PAREN) {
1,400✔
816
      // Regular operators union, intersection, complement
817
      while (stack.size() > 0) {
1,428✔
818
        int32_t op = stack.back();
1,176✔
819

820
        if (op < OP_RIGHT_PAREN && ((token == OP_COMPLEMENT && token < op) ||
1,176✔
821
                                     (token != OP_COMPLEMENT && token <= op))) {
532✔
822
          // While there is an operator, op, on top of the stack, if the token
823
          // is left-associative and its precedence is less than or equal to
824
          // that of op or if the token is right-associative and its precedence
825
          // is less than that of op, move op to the output queue and push the
826
          // token on to the stack. Note that only complement is
827
          // right-associative.
828
          rpn.push_back(op);
532✔
829
          stack.pop_back();
532✔
830
        } else {
831
          break;
832
        }
833
      }
834

835
      stack.push_back(token);
896✔
836

837
    } else if (token == OP_LEFT_PAREN) {
504✔
838
      // If the token is a left parenthesis, push it onto the stack
839
      stack.push_back(token);
252✔
840

841
    } else {
842
      // If the token is a right parenthesis, move operators from the stack to
843
      // the output queue until reaching the left parenthesis.
844
      for (auto it = stack.rbegin(); *it != OP_LEFT_PAREN; it++) {
504✔
845
        // If we run out of operators without finding a left parenthesis, it
846
        // means there are mismatched parentheses.
847
        if (it == stack.rend()) {
252✔
UNCOV
848
          fatal_error(fmt::format(
×
849
            "Mismatched parentheses in region specification for cell {}",
850
            cell_id));
851
        }
852
        rpn.push_back(stack.back());
252✔
853
        stack.pop_back();
252✔
854
      }
855

856
      // Pop the left parenthesis.
857
      stack.pop_back();
252✔
858
    }
859
  }
860

861
  while (stack.size() > 0) {
224✔
862
    int32_t op = stack.back();
112✔
863

864
    // If the operator is a parenthesis it is mismatched.
865
    if (op >= OP_RIGHT_PAREN) {
112✔
UNCOV
866
      fatal_error(fmt::format(
×
867
        "Mismatched parentheses in region specification for cell {}", cell_id));
868
    }
869

870
    rpn.push_back(stack.back());
112✔
871
    stack.pop_back();
112✔
872
  }
873

874
  return rpn;
224✔
875
}
112✔
876

877
//==============================================================================
878

879
std::string Region::str() const
25,969✔
880
{
881
  std::stringstream region_spec {};
25,969✔
882
  if (!expression_.empty()) {
25,969✔
883
    for (int32_t token : expression_) {
79,828✔
884
      if (token == OP_LEFT_PAREN) {
61,857✔
885
        region_spec << " (";
1,705✔
886
      } else if (token == OP_RIGHT_PAREN) {
60,152✔
887
        region_spec << " )";
1,705✔
888
      } else if (token == OP_COMPLEMENT) {
58,447✔
UNCOV
889
        region_spec << " ~";
×
890
      } else if (token == OP_INTERSECTION) {
58,447✔
891
      } else if (token == OP_UNION) {
54,330✔
892
        region_spec << " |";
3,801✔
893
      } else {
894
        // Note the off-by-one indexing
895
        auto surf_id = model::surfaces[abs(token) - 1]->id_;
50,529✔
896
        region_spec << " " << ((token > 0) ? surf_id : -surf_id);
50,529✔
897
      }
898
    }
899
  }
900
  return region_spec.str();
51,938✔
901
}
25,969✔
902

903
//==============================================================================
904

905
std::pair<double, int32_t> Region::distance(
2,147,483,647✔
906
  Position r, Direction u, int32_t on_surface) const
907
{
908
  double min_dist {INFTY};
2,147,483,647✔
909
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
2,147,483,647✔
910

911
  for (int32_t token : expression_) {
2,147,483,647✔
912
    // Ignore this token if it corresponds to an operator rather than a region.
913
    if (token >= OP_UNION)
2,147,483,647✔
914
      continue;
169,611,236✔
915

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

921
    // Check if this distance is the new minimum.
922
    if (d < min_dist) {
2,147,483,647✔
923
      if (min_dist - d >= FP_PRECISION * min_dist) {
2,147,483,647✔
924
        min_dist = d;
2,147,483,647✔
925
        i_surf = -token;
2,147,483,647✔
926
      }
927
    }
928
  }
929

930
  return {min_dist, i_surf};
2,147,483,647✔
931
}
932

933
//==============================================================================
934

935
bool Region::contains(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
936
{
937
  if (simple_) {
2,147,483,647✔
938
    return contains_simple(r, u, on_surface);
2,147,483,647✔
939
  } else {
940
    return contains_complex(r, u, on_surface);
6,688,579✔
941
  }
942
}
943

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

946
bool Region::contains_simple(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
947
{
948
  for (int32_t token : expression_) {
2,147,483,647✔
949
    // Assume that no tokens are operators. Evaluate the sense of particle with
950
    // respect to the surface and see if the token matches the sense. If the
951
    // particle's surface attribute is set and matches the token, that
952
    // overrides the determination based on sense().
953
    if (token == on_surface) {
2,147,483,647✔
954
    } else if (-token == on_surface) {
2,147,483,647✔
955
      return false;
956,402,875✔
956
    } else {
957
      // Note the off-by-one indexing
958
      bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
2,147,483,647✔
959
      if (sense != (token > 0)) {
2,147,483,647✔
960
        return false;
816,859,440✔
961
      }
962
    }
963
  }
964
  return true;
2,147,483,647✔
965
}
966

967
//==============================================================================
968

969
bool Region::contains_complex(Position r, Direction u, int32_t on_surface) const
6,688,579✔
970
{
971
  bool in_cell = true;
6,688,579✔
972
  int total_depth = 0;
6,688,579✔
973

974
  // For each token
975
  for (auto it = expression_.begin(); it != expression_.end(); it++) {
106,237,786✔
976
    int32_t token = *it;
101,409,338✔
977

978
    // If the token is a surface evaluate the sense
979
    // If the token is a union or intersection check to
980
    // short circuit
981
    if (token < OP_UNION) {
101,409,338✔
982
      if (token == on_surface) {
44,318,609✔
983
        in_cell = true;
3,307,259✔
984
      } else if (-token == on_surface) {
41,011,350✔
985
        in_cell = false;
670,929✔
986
      } else {
987
        // Note the off-by-one indexing
988
        bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
40,340,421✔
989
        in_cell = (sense == (token > 0));
40,340,421✔
990
      }
991
    } else if ((token == OP_UNION && in_cell == true) ||
57,090,729✔
992
               (token == OP_INTERSECTION && in_cell == false)) {
27,898,552✔
993
      // If the total depth is zero return
994
      if (total_depth == 0) {
6,228,196✔
995
        return in_cell;
1,860,131✔
996
      }
997

998
      total_depth--;
4,368,065✔
999

1000
      // While the iterator is within the bounds of the vector
1001
      int depth = 1;
4,368,065✔
1002
      do {
1003
        // Get next token
1004
        it++;
27,571,078✔
1005
        int32_t next_token = *it;
27,571,078✔
1006

1007
        // If the token is an a parenthesis
1008
        if (next_token > OP_COMPLEMENT) {
27,571,078✔
1009
          // Adjust depth accordingly
1010
          if (next_token == OP_RIGHT_PAREN) {
4,554,181✔
1011
            depth--;
4,461,123✔
1012
          } else {
1013
            depth++;
93,058✔
1014
          }
1015
        }
1016
      } while (depth > 0);
27,571,078✔
1017
    } else if (token == OP_LEFT_PAREN) {
55,230,598✔
1018
      total_depth++;
8,800,284✔
1019
    } else if (token == OP_RIGHT_PAREN) {
42,062,249✔
1020
      total_depth--;
4,432,219✔
1021
    }
1022
  }
1023
  return in_cell;
4,828,448✔
1024
}
1025

1026
//==============================================================================
1027

1028
BoundingBox Region::bounding_box(int32_t cell_id) const
173✔
1029
{
1030
  if (simple_) {
173✔
1031
    return bounding_box_simple();
61✔
1032
  } else {
1033
    auto postfix = generate_postfix(cell_id);
112✔
1034
    return bounding_box_complex(postfix);
112✔
1035
  }
112✔
1036
}
1037

1038
//==============================================================================
1039

1040
BoundingBox Region::bounding_box_simple() const
61✔
1041
{
1042
  BoundingBox bbox;
61✔
1043
  for (int32_t token : expression_) {
261✔
1044
    bbox &= model::surfaces[abs(token) - 1]->bounding_box(token > 0);
200✔
1045
  }
1046
  return bbox;
61✔
1047
}
1048

1049
//==============================================================================
1050

1051
BoundingBox Region::bounding_box_complex(vector<int32_t> postfix) const
112✔
1052
{
1053
  vector<BoundingBox> stack(postfix.size());
112✔
1054
  int i_stack = -1;
112✔
1055

1056
  for (auto& token : postfix) {
2,016✔
1057
    if (token == OP_UNION) {
1,904✔
1058
      stack[i_stack - 1] = stack[i_stack - 1] | stack[i_stack];
392✔
1059
      i_stack--;
392✔
1060
    } else if (token == OP_INTERSECTION) {
1,512✔
1061
      stack[i_stack - 1] = stack[i_stack - 1] & stack[i_stack];
504✔
1062
      i_stack--;
504✔
1063
    } else {
1064
      i_stack++;
1,008✔
1065
      stack[i_stack] = model::surfaces[abs(token) - 1]->bounding_box(token > 0);
1,008✔
1066
    }
1067
  }
1068

1069
  assert(i_stack == 0);
92✔
1070
  return stack.front();
224✔
1071
}
112✔
1072

1073
//==============================================================================
1074

1075
vector<int32_t> Region::surfaces() const
5,378✔
1076
{
1077
  if (simple_) {
5,378✔
1078
    return expression_;
5,378✔
1079
  }
1080

UNCOV
1081
  vector<int32_t> surfaces = expression_;
×
1082

UNCOV
1083
  auto it = std::find_if(surfaces.begin(), surfaces.end(),
×
1084
    [&](const auto& value) { return value >= OP_UNION; });
×
1085

UNCOV
1086
  while (it != surfaces.end()) {
×
1087
    surfaces.erase(it);
×
1088

UNCOV
1089
    it = std::find_if(surfaces.begin(), surfaces.end(),
×
1090
      [&](const auto& value) { return value >= OP_UNION; });
×
1091
  }
1092

UNCOV
1093
  return surfaces;
×
1094
}
1095

1096
//==============================================================================
1097
// Non-method functions
1098
//==============================================================================
1099

1100
void read_cells(pugi::xml_node node)
7,244✔
1101
{
1102
  // Count the number of cells.
1103
  int n_cells = 0;
7,244✔
1104
  for (pugi::xml_node cell_node : node.children("cell")) {
39,744✔
1105
    n_cells++;
32,500✔
1106
  }
1107

1108
  // Loop over XML cell elements and populate the array.
1109
  model::cells.reserve(n_cells);
7,244✔
1110
  for (pugi::xml_node cell_node : node.children("cell")) {
39,744✔
1111
    model::cells.push_back(make_unique<CSGCell>(cell_node));
32,500✔
1112
  }
1113

1114
  // Fill the cell map.
1115
  for (int i = 0; i < model::cells.size(); i++) {
39,744✔
1116
    int32_t id = model::cells[i]->id_;
32,500✔
1117
    auto search = model::cell_map.find(id);
32,500✔
1118
    if (search == model::cell_map.end()) {
32,500✔
1119
      model::cell_map[id] = i;
32,500✔
1120
    } else {
UNCOV
1121
      fatal_error(
×
1122
        fmt::format("Two or more cells use the same unique ID: {}", id));
×
1123
    }
1124
  }
1125

1126
  read_dagmc_universes(node);
7,244✔
1127

1128
  populate_universes();
7,242✔
1129

1130
  // Allocate the cell overlap count if necessary.
1131
  if (settings::check_overlaps) {
7,242✔
1132
    model::overlap_check_count.resize(model::cells.size(), 0);
276✔
1133
  }
1134

1135
  if (model::cells.size() == 0) {
7,242✔
UNCOV
1136
    fatal_error("No cells were found in the geometry.xml file");
×
1137
  }
1138
}
7,242✔
1139

1140
void populate_universes()
7,244✔
1141
{
1142
  // Used to map universe index to the index of an implicit complement cell for
1143
  // DAGMC universes
1144
  std::unordered_map<int, int> implicit_comp_cells;
7,244✔
1145

1146
  // Populate the Universe vector and map.
1147
  for (int index_cell = 0; index_cell < model::cells.size(); index_cell++) {
40,277✔
1148
    int32_t uid = model::cells[index_cell]->universe_;
33,033✔
1149
    auto it = model::universe_map.find(uid);
33,033✔
1150
    if (it == model::universe_map.end()) {
33,033✔
1151
      model::universes.push_back(make_unique<Universe>());
18,523✔
1152
      model::universes.back()->id_ = uid;
18,523✔
1153
      model::universes.back()->cells_.push_back(index_cell);
18,523✔
1154
      model::universe_map[uid] = model::universes.size() - 1;
18,523✔
1155
    } else {
1156
#ifdef OPENMC_DAGMC_ENABLED
1157
      // Skip implicit complement cells for now
1158
      Universe* univ = model::universes[it->second].get();
2,254✔
1159
      DAGUniverse* dag_univ = dynamic_cast<DAGUniverse*>(univ);
2,254✔
1160
      if (dag_univ && (dag_univ->implicit_complement_idx() == index_cell)) {
2,254✔
1161
        implicit_comp_cells[it->second] = index_cell;
108✔
1162
        continue;
108✔
1163
      }
1164
#endif
1165

1166
      model::universes[it->second]->cells_.push_back(index_cell);
14,402✔
1167
    }
1168
  }
1169

1170
  // Add DAGUniverse implicit complement cells last
1171
  for (const auto& it : implicit_comp_cells) {
7,352✔
1172
    int index_univ = it.first;
108✔
1173
    int index_cell = it.second;
108✔
1174
    model::universes[index_univ]->cells_.push_back(index_cell);
108✔
1175
  }
1176

1177
  model::universes.shrink_to_fit();
7,244✔
1178
}
7,244✔
1179

1180
//==============================================================================
1181
// C-API functions
1182
//==============================================================================
1183

1184
extern "C" int openmc_cell_get_fill(
762✔
1185
  int32_t index, int* type, int32_t** indices, int32_t* n)
1186
{
1187
  if (index >= 0 && index < model::cells.size()) {
762✔
1188
    Cell& c {*model::cells[index]};
762✔
1189
    *type = static_cast<int>(c.type_);
762✔
1190
    if (c.type_ == Fill::MATERIAL) {
762✔
1191
      *indices = c.material_.data();
762✔
1192
      *n = c.material_.size();
762✔
1193
    } else {
UNCOV
1194
      *indices = &c.fill_;
×
1195
      *n = 1;
×
1196
    }
1197
  } else {
UNCOV
1198
    set_errmsg("Index in cells array is out of bounds.");
×
1199
    return OPENMC_E_OUT_OF_BOUNDS;
×
1200
  }
1201
  return 0;
762✔
1202
}
1203

1204
extern "C" int openmc_cell_set_fill(
11✔
1205
  int32_t index, int type, int32_t n, const int32_t* indices)
1206
{
1207
  Fill filltype = static_cast<Fill>(type);
11✔
1208
  if (index >= 0 && index < model::cells.size()) {
11✔
1209
    Cell& c {*model::cells[index]};
11✔
1210
    if (filltype == Fill::MATERIAL) {
11✔
1211
      c.type_ = Fill::MATERIAL;
11✔
1212
      c.material_.clear();
11✔
1213
      for (int i = 0; i < n; i++) {
22✔
1214
        int i_mat = indices[i];
11✔
1215
        if (i_mat == MATERIAL_VOID) {
11✔
UNCOV
1216
          c.material_.push_back(MATERIAL_VOID);
×
1217
        } else if (i_mat >= 0 && i_mat < model::materials.size()) {
11✔
1218
          c.material_.push_back(i_mat);
11✔
1219
        } else {
UNCOV
1220
          set_errmsg("Index in materials array is out of bounds.");
×
1221
          return OPENMC_E_OUT_OF_BOUNDS;
×
1222
        }
1223
      }
1224
      c.material_.shrink_to_fit();
11✔
UNCOV
1225
    } else if (filltype == Fill::UNIVERSE) {
×
1226
      c.type_ = Fill::UNIVERSE;
×
1227
    } else {
UNCOV
1228
      c.type_ = Fill::LATTICE;
×
1229
    }
1230
  } else {
UNCOV
1231
    set_errmsg("Index in cells array is out of bounds.");
×
1232
    return OPENMC_E_OUT_OF_BOUNDS;
×
1233
  }
1234
  return 0;
11✔
1235
}
1236

1237
extern "C" int openmc_cell_set_temperature(
88✔
1238
  int32_t index, double T, const int32_t* instance, bool set_contained)
1239
{
1240
  if (index < 0 || index >= model::cells.size()) {
88✔
UNCOV
1241
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1242
    return OPENMC_E_OUT_OF_BOUNDS;
×
1243
  }
1244

1245
  int32_t instance_index = instance ? *instance : -1;
88✔
1246
  try {
1247
    model::cells[index]->set_temperature(T, instance_index, set_contained);
88✔
UNCOV
1248
  } catch (const std::exception& e) {
×
1249
    set_errmsg(e.what());
×
1250
    return OPENMC_E_UNASSIGNED;
×
1251
  }
×
1252
  return 0;
88✔
1253
}
1254

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

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

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

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

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

1300
  int32_t instance_index = instance ? *instance : -1;
66✔
1301
  try {
1302
    if (model::cells[index]->type_ != Fill::MATERIAL) {
66✔
NEW
1303
      fatal_error(
×
NEW
1304
        fmt::format("Cell {}, instance {} is not filled with a material.",
×
NEW
1305
          model::cells[index]->id_, instance_index));
×
1306
    }
1307

1308
    int32_t mat_index = model::cells[index]->material(instance_index);
66✔
1309
    if (mat_index == MATERIAL_VOID) {
66✔
NEW
1310
      *rho = 0.0;
×
1311
    } else {
1312
      *rho = model::cells[index]->density_mult(instance_index) *
132✔
1313
             model::materials[mat_index]->density_gpcc();
66✔
1314
    }
NEW
1315
  } catch (const std::exception& e) {
×
NEW
1316
    set_errmsg(e.what());
×
NEW
1317
    return OPENMC_E_UNASSIGNED;
×
NEW
1318
  }
×
1319
  return 0;
66✔
1320
}
1321

1322
//! Get the bounding box of a cell
1323
extern "C" int openmc_cell_bounding_box(
140✔
1324
  const int32_t index, double* llc, double* urc)
1325
{
1326

1327
  BoundingBox bbox;
140✔
1328

1329
  const auto& c = model::cells[index];
140✔
1330
  bbox = c->bounding_box();
140✔
1331

1332
  // set lower left corner values
1333
  llc[0] = bbox.xmin;
140✔
1334
  llc[1] = bbox.ymin;
140✔
1335
  llc[2] = bbox.zmin;
140✔
1336

1337
  // set upper right corner values
1338
  urc[0] = bbox.xmax;
140✔
1339
  urc[1] = bbox.ymax;
140✔
1340
  urc[2] = bbox.zmax;
140✔
1341

1342
  return 0;
140✔
1343
}
1344

1345
//! Get the name of a cell
1346
extern "C" int openmc_cell_get_name(int32_t index, const char** name)
22✔
1347
{
1348
  if (index < 0 || index >= model::cells.size()) {
22✔
1349
    set_errmsg("Index in cells array is out of bounds.");
×
1350
    return OPENMC_E_OUT_OF_BOUNDS;
×
1351
  }
1352

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

1355
  return 0;
22✔
1356
}
1357

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

1366
  model::cells[index]->set_name(name);
11✔
1367

1368
  return 0;
11✔
1369
}
1370

1371
//==============================================================================
1372
//! Define a containing (parent) cell
1373
//==============================================================================
1374

1375
//! Used to locate a universe fill in the geometry
1376
struct ParentCell {
1377
  bool operator==(const ParentCell& other) const
144✔
1378
  {
1379
    return cell_index == other.cell_index &&
288✔
1380
           lattice_index == other.lattice_index;
288✔
1381
  }
1382

1383
  bool operator<(const ParentCell& other) const
1384
  {
1385
    return cell_index < other.cell_index ||
1386
           (cell_index == other.cell_index &&
1387
             lattice_index < other.lattice_index);
1388
  }
1389

1390
  int64_t cell_index;
1391
  int64_t lattice_index;
1392
};
1393

1394
//! Structure used to insert ParentCell into hashed STL data structures
1395
struct ParentCellHash {
1396
  std::size_t operator()(const ParentCell& p) const
673✔
1397
  {
1398
    return 4096 * p.cell_index + p.lattice_index;
673✔
1399
  }
1400
};
1401

1402
//! Used to manage a traversal stack when locating parent cells of a cell
1403
//! instance in the model
1404
struct ParentCellStack {
1405

1406
  //! push method that adds to the parent_cells visited cells for this search
1407
  //! universe
1408
  void push(int32_t search_universe, const ParentCell& pc)
112✔
1409
  {
1410
    parent_cells_.push_back(pc);
112✔
1411
    // add parent cell to the set of cells we've visited for this search
1412
    // universe
1413
    visited_cells_[search_universe].insert(pc);
112✔
1414
  }
112✔
1415

1416
  //! removes the last parent_cell and clears the visited cells for the popped
1417
  //! cell's universe
1418
  void pop()
80✔
1419
  {
1420
    visited_cells_[this->current_univ()].clear();
80✔
1421
    parent_cells_.pop_back();
80✔
1422
  }
80✔
1423

1424
  //! checks whether or not the parent cell has been visited already for this
1425
  //! search universe
1426
  bool visited(int32_t search_universe, const ParentCell& parent_cell)
561✔
1427
  {
1428
    return visited_cells_[search_universe].count(parent_cell) != 0;
561✔
1429
  }
1430

1431
  //! return the next universe to search for a parent cell
1432
  int32_t current_univ() const
80✔
1433
  {
1434
    return model::cells[parent_cells_.back().cell_index]->universe_;
80✔
1435
  }
1436

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

1440
  //! compute an instance for the provided distribcell index
1441
  int32_t compute_instance(int32_t distribcell_index) const
193✔
1442
  {
1443
    if (distribcell_index == C_NONE)
193✔
1444
      return 0;
65✔
1445

1446
    int32_t instance = 0;
128✔
1447
    for (const auto& parent_cell : this->parent_cells_) {
240✔
1448
      auto& cell = model::cells[parent_cell.cell_index];
112✔
1449
      if (cell->type_ == Fill::UNIVERSE) {
112✔
1450
        instance += cell->offset_[distribcell_index];
×
1451
      } else if (cell->type_ == Fill::LATTICE) {
112✔
1452
        auto& lattice = model::lattices[cell->fill_];
112✔
1453
        instance +=
112✔
1454
          lattice->offset(distribcell_index, parent_cell.lattice_index);
112✔
1455
      }
1456
    }
1457
    return instance;
128✔
1458
  }
1459

1460
  // Accessors
1461
  vector<ParentCell>& parent_cells() { return parent_cells_; }
339✔
1462
  const vector<ParentCell>& parent_cells() const { return parent_cells_; }
1463

1464
  // Data Members
1465
  vector<ParentCell> parent_cells_;
1466
  std::unordered_map<int32_t, std::unordered_set<ParentCell, ParentCellHash>>
1467
    visited_cells_;
1468
};
1469

1470
vector<ParentCell> Cell::find_parent_cells(
×
1471
  int32_t instance, const Position& r) const
1472
{
1473

1474
  // create a temporary particle
1475
  GeometryState dummy_particle {};
×
1476
  dummy_particle.r() = r;
×
1477
  dummy_particle.u() = {0., 0., 1.};
×
1478

1479
  return find_parent_cells(instance, dummy_particle);
×
1480
}
1481

1482
vector<ParentCell> Cell::find_parent_cells(
×
1483
  int32_t instance, GeometryState& p) const
1484
{
1485
  // look up the particle's location
1486
  exhaustive_find_cell(p);
×
1487
  const auto& coords = p.coord();
×
1488

1489
  // build a parent cell stack from the particle coordinates
1490
  ParentCellStack stack;
×
1491
  bool cell_found = false;
×
1492
  for (auto it = coords.begin(); it != coords.end(); it++) {
×
1493
    const auto& coord = *it;
×
1494
    const auto& cell = model::cells[coord.cell()];
×
1495
    // if the cell at this level matches the current cell, stop adding to the
1496
    // stack
1497
    if (coord.cell() == model::cell_map[this->id_]) {
×
1498
      cell_found = true;
×
1499
      break;
×
1500
    }
1501

1502
    // if filled with a lattice, get the lattice index from the next
1503
    // level in the coordinates to push to the stack
1504
    int lattice_idx = C_NONE;
×
1505
    if (cell->type_ == Fill::LATTICE) {
×
1506
      const auto& next_coord = *(it + 1);
×
1507
      lattice_idx = model::lattices[next_coord.lattice()]->get_flat_index(
×
1508
        next_coord.lattice_index());
1509
    }
1510
    stack.push(coord.universe(), {coord.cell(), lattice_idx});
×
1511
  }
1512

1513
  // if this loop finished because the cell was found and
1514
  // the instance matches the one requested in the call
1515
  // we have the correct path and can return the stack
1516
  if (cell_found &&
×
1517
      stack.compute_instance(this->distribcell_index_) == instance) {
×
1518
    return stack.parent_cells();
×
1519
  }
1520

1521
  // fall back on an exhaustive search for the cell's parents
1522
  return exhaustive_find_parent_cells(instance);
×
1523
}
1524

1525
vector<ParentCell> Cell::exhaustive_find_parent_cells(int32_t instance) const
113✔
1526
{
1527
  ParentCellStack stack;
113✔
1528
  // start with this cell's universe
1529
  int32_t prev_univ_idx;
1530
  int32_t univ_idx = this->universe_;
113✔
1531

1532
  while (true) {
1533
    const auto& univ = model::universes[univ_idx];
193✔
1534
    prev_univ_idx = univ_idx;
193✔
1535

1536
    // search for a cell that is filled w/ this universe
1537
    for (const auto& cell : model::cells) {
1,236✔
1538
      // if this is a material-filled cell, move on
1539
      if (cell->type_ == Fill::MATERIAL)
1,155✔
1540
        continue;
642✔
1541

1542
      if (cell->type_ == Fill::UNIVERSE) {
513✔
1543
        // if this is in the set of cells previously visited for this universe,
1544
        // move on
1545
        if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE}))
305✔
1546
          continue;
×
1547

1548
        // if this cell contains the universe we're searching for, add it to the
1549
        // stack
1550
        if (cell->fill_ == univ_idx) {
305✔
1551
          stack.push(univ_idx, {model::cell_map[cell->id_], C_NONE});
×
1552
          univ_idx = cell->universe_;
×
1553
        }
1554
      } else if (cell->type_ == Fill::LATTICE) {
208✔
1555
        // retrieve the lattice and lattice universes
1556
        const auto& lattice = model::lattices[cell->fill_];
208✔
1557
        const auto& lattice_univs = lattice->universes_;
208✔
1558

1559
        // start search for universe
1560
        auto lat_it = lattice_univs.begin();
208✔
1561
        while (true) {
1562
          // find the next lattice cell with this universe
1563
          lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
352✔
1564
          if (lat_it == lattice_univs.end())
352✔
1565
            break;
96✔
1566

1567
          int lattice_idx = lat_it - lattice_univs.begin();
256✔
1568

1569
          // move iterator forward one to avoid finding the same entry
1570
          lat_it++;
256✔
1571
          if (stack.visited(
256✔
1572
                univ_idx, {model::cell_map[cell->id_], lattice_idx}))
256✔
1573
            continue;
144✔
1574

1575
          // add this cell and lattice index to the stack and exit loop
1576
          stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
112✔
1577
          univ_idx = cell->universe_;
112✔
1578
          break;
112✔
1579
        }
144✔
1580
      }
1581
      // if we've updated the universe, break
1582
      if (prev_univ_idx != univ_idx)
513✔
1583
        break;
112✔
1584
    } // end cell loop search for universe
1585

1586
    // if we're at the top of the geometry and the instance matches, we're done
1587
    if (univ_idx == model::root_universe &&
386✔
1588
        stack.compute_instance(this->distribcell_index_) == instance)
193✔
1589
      break;
113✔
1590

1591
    // if there is no match on the original cell's universe, report an error
1592
    if (univ_idx == this->universe_) {
80✔
1593
      fatal_error(
×
1594
        fmt::format("Could not find the parent cells for cell {}, instance {}.",
×
1595
          this->id_, instance));
×
1596
    }
1597

1598
    // if we don't find a suitable update, adjust the stack and continue
1599
    if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
80✔
1600
      stack.pop();
80✔
1601
      univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
80✔
1602
    }
1603

1604
  } // end while
80✔
1605

1606
  // reverse the stack so the highest cell comes first
1607
  std::reverse(stack.parent_cells().begin(), stack.parent_cells().end());
113✔
1608
  return stack.parent_cells();
226✔
1609
}
113✔
1610

1611
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
161✔
1612
  int32_t instance, Position* hint) const
1613
{
1614
  std::unordered_map<int32_t, vector<int32_t>> contained_cells;
161✔
1615

1616
  // if this is a material-filled cell it has no contained cells
1617
  if (this->type_ == Fill::MATERIAL)
161✔
1618
    return contained_cells;
48✔
1619

1620
  // find the pathway through the geometry to this cell
1621
  vector<ParentCell> parent_cells;
113✔
1622

1623
  // if a positional hint is provided, attempt to do a fast lookup
1624
  // of the parent cells
1625
  parent_cells = hint ? find_parent_cells(instance, *hint)
226✔
1626
                      : exhaustive_find_parent_cells(instance);
113✔
1627

1628
  // if this cell is filled w/ a material, it contains no other cells
1629
  if (type_ != Fill::MATERIAL) {
113✔
1630
    this->get_contained_cells_inner(contained_cells, parent_cells);
113✔
1631
  }
1632

1633
  return contained_cells;
113✔
1634
}
113✔
1635

1636
//! Get all cells within this cell
1637
void Cell::get_contained_cells_inner(
87,763✔
1638
  std::unordered_map<int32_t, vector<int32_t>>& contained_cells,
1639
  vector<ParentCell>& parent_cells) const
1640
{
1641

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

1685
//! Return the index in the cells array of a cell with a given ID
1686
extern "C" int openmc_get_cell_index(int32_t id, int32_t* index)
815✔
1687
{
1688
  auto it = model::cell_map.find(id);
815✔
1689
  if (it != model::cell_map.end()) {
815✔
1690
    *index = it->second;
804✔
1691
    return 0;
804✔
1692
  } else {
1693
    set_errmsg("No cell exists with ID=" + std::to_string(id) + ".");
11✔
1694
    return OPENMC_E_INVALID_ID;
11✔
1695
  }
1696
}
1697

1698
//! Return the ID of a cell
1699
extern "C" int openmc_cell_get_id(int32_t index, int32_t* id)
601,673✔
1700
{
1701
  if (index >= 0 && index < model::cells.size()) {
601,673✔
1702
    *id = model::cells[index]->id_;
601,673✔
1703
    return 0;
601,673✔
1704
  } else {
1705
    set_errmsg("Index in cells array is out of bounds.");
×
1706
    return OPENMC_E_OUT_OF_BOUNDS;
×
1707
  }
1708
}
1709

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

1723
//! Return the translation vector of a cell
1724
extern "C" int openmc_cell_get_translation(int32_t index, double xyz[])
55✔
1725
{
1726
  if (index >= 0 && index < model::cells.size()) {
55✔
1727
    auto& cell = model::cells[index];
55✔
1728
    xyz[0] = cell->translation_.x;
55✔
1729
    xyz[1] = cell->translation_.y;
55✔
1730
    xyz[2] = cell->translation_.z;
55✔
1731
    return 0;
55✔
1732
  } else {
1733
    set_errmsg("Index in cells array is out of bounds.");
×
1734
    return OPENMC_E_OUT_OF_BOUNDS;
×
1735
  }
1736
}
1737

1738
//! Set the translation vector of a cell
1739
extern "C" int openmc_cell_set_translation(int32_t index, const double xyz[])
33✔
1740
{
1741
  if (index >= 0 && index < model::cells.size()) {
33✔
1742
    if (model::cells[index]->fill_ == C_NONE) {
33✔
1743
      set_errmsg(fmt::format("Cannot apply a translation to cell {}"
11✔
1744
                             " because it is not filled with another universe",
1745
        index));
1746
      return OPENMC_E_GEOMETRY;
11✔
1747
    }
1748
    model::cells[index]->translation_ = Position(xyz);
22✔
1749
    return 0;
22✔
1750
  } else {
1751
    set_errmsg("Index in cells array is out of bounds.");
×
1752
    return OPENMC_E_OUT_OF_BOUNDS;
×
1753
  }
1754
}
1755

1756
//! Return the rotation matrix of a cell
1757
extern "C" int openmc_cell_get_rotation(int32_t index, double rot[], size_t* n)
55✔
1758
{
1759
  if (index >= 0 && index < model::cells.size()) {
55✔
1760
    auto& cell = model::cells[index];
55✔
1761
    *n = cell->rotation_.size();
55✔
1762
    std::memcpy(rot, cell->rotation_.data(), *n * sizeof(cell->rotation_[0]));
55✔
1763
    return 0;
55✔
1764
  } else {
1765
    set_errmsg("Index in cells array is out of bounds.");
×
1766
    return OPENMC_E_OUT_OF_BOUNDS;
×
1767
  }
1768
}
1769

1770
//! Set the flattened rotation matrix of a cell
1771
extern "C" int openmc_cell_set_rotation(
44✔
1772
  int32_t index, const double rot[], size_t rot_len)
1773
{
1774
  if (index >= 0 && index < model::cells.size()) {
44✔
1775
    if (model::cells[index]->fill_ == C_NONE) {
44✔
1776
      set_errmsg(fmt::format("Cannot apply a rotation to cell {}"
11✔
1777
                             " because it is not filled with another universe",
1778
        index));
1779
      return OPENMC_E_GEOMETRY;
11✔
1780
    }
1781
    std::vector<double> vec_rot(rot, rot + rot_len);
33✔
1782
    model::cells[index]->set_rotation(vec_rot);
33✔
1783
    return 0;
33✔
1784
  } else {
33✔
1785
    set_errmsg("Index in cells array is out of bounds.");
×
1786
    return OPENMC_E_OUT_OF_BOUNDS;
×
1787
  }
1788
}
1789

1790
//! Get the number of instances of the requested cell
1791
extern "C" int openmc_cell_get_num_instances(
11✔
1792
  int32_t index, int32_t* num_instances)
1793
{
1794
  if (index < 0 || index >= model::cells.size()) {
11✔
1795
    set_errmsg("Index in cells array is out of bounds.");
×
1796
    return OPENMC_E_OUT_OF_BOUNDS;
×
1797
  }
1798
  *num_instances = model::cells[index]->n_instances();
11✔
1799
  return 0;
11✔
1800
}
1801

1802
//! Extend the cells array by n elements
1803
extern "C" int openmc_extend_cells(
22✔
1804
  int32_t n, int32_t* index_start, int32_t* index_end)
1805
{
1806
  if (index_start)
22✔
1807
    *index_start = model::cells.size();
22✔
1808
  if (index_end)
22✔
1809
    *index_end = model::cells.size() + n - 1;
×
1810
  for (int32_t i = 0; i < n; i++) {
44✔
1811
    model::cells.push_back(make_unique<CSGCell>());
22✔
1812
  }
1813
  return 0;
22✔
1814
}
1815

1816
extern "C" int cells_size()
44✔
1817
{
1818
  return model::cells.size();
44✔
1819
}
1820

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