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

openmc-dev / openmc / 20368472998

19 Dec 2025 11:20AM UTC coverage: 82.143% (+0.004%) from 82.139%
20368472998

Pull #3566

github

web-flow
Merge a657bcb6c into a230b8612
Pull Request #3566: Resolve merge conflicts for PR #3516 - Implement Virtual Lattice Method for Efficient Simulation of Dispersed Fuels

17200 of 23810 branches covered (72.24%)

Branch coverage included in aggregate %.

266 of 322 new or added lines in 9 files covered. (82.61%)

2 existing lines in 1 file now uncovered.

55371 of 64537 relevant lines covered (85.8%)

43403485.97 hits per line

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

75.91
/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
vector<vector<int32_t>> generate_triso_distribution(vector<int> lattice_shape,
16✔
40
  vector<double> lattice_pitch, vector<double> lattice_lower_left,
41
  vector<std::int32_t> cell_rpn, int id)
42
{
43
  vector<vector<int32_t>> triso_distribution(
44
    lattice_shape[0] * lattice_shape[1] * lattice_shape[2]);
16✔
45
  vector<double> mesh_center(3);
16✔
46
  vector<int> mesh_ind(3);
16✔
47

48
  for (int32_t token : cell_rpn) {
1,616✔
49
    if (token >= OP_UNION)
1,600!
NEW
50
      continue;
×
51
    vector<double> triso_center = model::surfaces[abs(token) - 1]->get_center();
1,600✔
52
    for (int i = 0; i < 3; i++) {
6,400✔
53
      mesh_ind[i] =
4,800✔
54
        floor((triso_center[i] - lattice_lower_left[i]) / lattice_pitch[i]);
4,800✔
55
    }
56
    for (int i = mesh_ind[0] - 1; i <= mesh_ind[0] + 1; i++) {
6,400✔
57
      for (int j = mesh_ind[1] - 1; j <= mesh_ind[1] + 1; j++) {
19,200✔
58
        for (int k = mesh_ind[2] - 1; k <= mesh_ind[2] + 1; k++) {
57,600✔
59
          if (i < 0 || i >= lattice_shape[0] || j < 0 ||
38,160✔
60
              j >= lattice_shape[1] || k < 0 || k >= lattice_shape[2])
81,360✔
61
            continue;
22,784✔
62
          mesh_center[0] = (i + 0.5) * lattice_pitch[0] + lattice_lower_left[0];
20,416✔
63
          mesh_center[1] = (j + 0.5) * lattice_pitch[1] + lattice_lower_left[1];
20,416✔
64
          mesh_center[2] = (k + 0.5) * lattice_pitch[2] + lattice_lower_left[2];
20,416✔
65
          if (model::surfaces[abs(token) - 1]->triso_in_mesh(
20,416✔
66
                mesh_center, lattice_pitch)) {
67
            triso_distribution[i + j * lattice_shape[0] +
2,576✔
68
                               k * lattice_shape[0] * lattice_shape[1]]
2,576✔
69
              .push_back(token);
2,576✔
70
            model::surfaces[abs(token) - 1]->connect_to_triso_base(id, "base");
2,576✔
71
          }
72
        }
73
      }
74
    }
75
  }
1,600✔
76

77
  return triso_distribution;
32✔
78
}
16✔
79

80
//==============================================================================
81
// Cell implementation
82
//==============================================================================
83

84
int32_t Cell::n_instances() const
4,818✔
85
{
86
  return model::universes[universe_]->n_instances_;
4,818✔
87
}
88

89
void Cell::set_rotation(const vector<double>& rot)
449✔
90
{
91
  if (fill_ == C_NONE) {
449!
92
    fatal_error(fmt::format("Cannot apply a rotation to cell {}"
×
93
                            " because it is not filled with another universe",
94
      id_));
×
95
  }
96

97
  if (rot.size() != 3 && rot.size() != 9) {
449!
98
    fatal_error(fmt::format("Non-3D rotation vector applied to cell {}", id_));
×
99
  }
100

101
  // Compute and store the rotation matrix.
102
  rotation_.clear();
449✔
103
  rotation_.reserve(rot.size() == 9 ? 9 : 12);
449!
104
  if (rot.size() == 3) {
449!
105
    double phi = -rot[0] * PI / 180.0;
449✔
106
    double theta = -rot[1] * PI / 180.0;
449✔
107
    double psi = -rot[2] * PI / 180.0;
449✔
108
    rotation_.push_back(std::cos(theta) * std::cos(psi));
449✔
109
    rotation_.push_back(-std::cos(phi) * std::sin(psi) +
×
110
                        std::sin(phi) * std::sin(theta) * std::cos(psi));
449✔
111
    rotation_.push_back(std::sin(phi) * std::sin(psi) +
×
112
                        std::cos(phi) * std::sin(theta) * std::cos(psi));
449✔
113
    rotation_.push_back(std::cos(theta) * std::sin(psi));
449✔
114
    rotation_.push_back(std::cos(phi) * std::cos(psi) +
×
115
                        std::sin(phi) * std::sin(theta) * std::sin(psi));
449✔
116
    rotation_.push_back(-std::sin(phi) * std::cos(psi) +
×
117
                        std::cos(phi) * std::sin(theta) * std::sin(psi));
449✔
118
    rotation_.push_back(-std::sin(theta));
449✔
119
    rotation_.push_back(std::sin(phi) * std::cos(theta));
449✔
120
    rotation_.push_back(std::cos(phi) * std::cos(theta));
449✔
121

122
    // When user specifies angles, write them at end of vector
123
    rotation_.push_back(rot[0]);
449✔
124
    rotation_.push_back(rot[1]);
449✔
125
    rotation_.push_back(rot[2]);
449✔
126
  } else {
127
    std::copy(rot.begin(), rot.end(), std::back_inserter(rotation_));
×
128
  }
129
}
449✔
130

131
double Cell::temperature(int32_t instance) const
97✔
132
{
133
  if (sqrtkT_.size() < 1) {
97!
134
    throw std::runtime_error {"Cell temperature has not yet been set."};
×
135
  }
136

137
  if (instance >= 0) {
97✔
138
    double sqrtkT = sqrtkT_.size() == 1 ? sqrtkT_.at(0) : sqrtkT_.at(instance);
11!
139
    return sqrtkT * sqrtkT / K_BOLTZMANN;
11✔
140
  } else {
141
    return sqrtkT_[0] * sqrtkT_[0] / K_BOLTZMANN;
86✔
142
  }
143
}
144

145
double Cell::density_mult(int32_t instance) const
2,147,483,647✔
146
{
147
  if (instance >= 0) {
2,147,483,647✔
148
    return density_mult_.size() == 1 ? density_mult_.at(0)
2,147,483,647✔
149
                                     : density_mult_.at(instance);
2,147,483,647✔
150
  } else {
151
    return density_mult_[0];
77✔
152
  }
153
}
154

155
double Cell::density(int32_t instance) const
132✔
156
{
157
  const int32_t mat_index = material(instance);
132✔
158
  if (mat_index == MATERIAL_VOID)
132!
159
    return 0.0;
×
160

161
  return density_mult(instance) * model::materials[mat_index]->density_gpcc();
132✔
162
}
163

164
void Cell::set_temperature(double T, int32_t instance, bool set_contained)
492✔
165
{
166
  if (settings::temperature_method == TemperatureMethod::INTERPOLATION) {
492!
167
    if (T < (data::temperature_min - settings::temperature_tolerance)) {
×
168
      throw std::runtime_error {
×
169
        fmt::format("Temperature of {} K is below minimum temperature at "
×
170
                    "which data is available of {} K.",
171
          T, data::temperature_min)};
×
172
    } else if (T > (data::temperature_max + settings::temperature_tolerance)) {
×
173
      throw std::runtime_error {
×
174
        fmt::format("Temperature of {} K is above maximum temperature at "
×
175
                    "which data is available of {} K.",
176
          T, data::temperature_max)};
×
177
    }
178
  }
179

180
  if (type_ == Fill::MATERIAL) {
492✔
181
    if (instance >= 0) {
460✔
182
      // If temperature vector is not big enough, resize it first
183
      if (sqrtkT_.size() != n_instances())
383✔
184
        sqrtkT_.resize(n_instances(), sqrtkT_[0]);
48✔
185

186
      // Set temperature for the corresponding instance
187
      sqrtkT_.at(instance) = std::sqrt(K_BOLTZMANN * T);
383✔
188
    } else {
189
      // Set temperature for all instances
190
      for (auto& T_ : sqrtkT_) {
154✔
191
        T_ = std::sqrt(K_BOLTZMANN * T);
77✔
192
      }
193
    }
194
  } else {
195
    if (!set_contained) {
32!
196
      throw std::runtime_error {
×
197
        fmt::format("Attempted to set the temperature of cell {} "
×
198
                    "which is not filled by a material.",
199
          id_)};
×
200
    }
201

202
    auto contained_cells = this->get_contained_cells(instance);
32✔
203
    for (const auto& entry : contained_cells) {
128✔
204
      auto& cell = model::cells[entry.first];
96✔
205
      assert(cell->type_ == Fill::MATERIAL);
78!
206
      auto& instances = entry.second;
96✔
207
      for (auto instance : instances) {
336✔
208
        cell->set_temperature(T, instance);
240✔
209
      }
210
    }
211
  }
32✔
212
}
492✔
213

214
void Cell::set_density(double density, int32_t instance, bool set_contained)
284✔
215
{
216
  if (type_ != Fill::MATERIAL && !set_contained) {
284!
217
    fatal_error(
×
218
      fmt::format("Attempted to set the density multiplier of cell {} "
×
219
                  "which is not filled by a material.",
220
        id_));
×
221
  }
222

223
  if (type_ == Fill::MATERIAL) {
284✔
224
    const int32_t mat_index = material(instance);
268✔
225
    if (mat_index == MATERIAL_VOID)
268!
226
      return;
×
227

228
    if (instance >= 0) {
268✔
229
      // If density multiplier vector is not big enough, resize it first
230
      if (density_mult_.size() != n_instances())
191✔
231
        density_mult_.resize(n_instances(), density_mult_[0]);
48✔
232

233
      // Set density multiplier for the corresponding instance
234
      density_mult_.at(instance) =
191✔
235
        density / model::materials[mat_index]->density_gpcc();
191✔
236
    } else {
237
      // Set density multiplier for all instances
238
      for (auto& x : density_mult_) {
154✔
239
        x = density / model::materials[mat_index]->density_gpcc();
77✔
240
      }
241
    }
242
  } else {
243
    auto contained_cells = this->get_contained_cells(instance);
16✔
244
    for (const auto& entry : contained_cells) {
64✔
245
      auto& cell = model::cells[entry.first];
48✔
246
      assert(cell->type_ == Fill::MATERIAL);
39!
247
      auto& instances = entry.second;
48✔
248
      for (auto instance : instances) {
96✔
249
        cell->set_density(density, instance);
48✔
250
      }
251
    }
252
  }
16✔
253
}
254

255
void Cell::export_properties_hdf5(hid_t group) const
154✔
256
{
257
  // Create a group for this cell.
258
  auto cell_group = create_group(group, fmt::format("cell {}", id_));
308✔
259

260
  // Write temperature in [K] for one or more cell instances
261
  vector<double> temps;
154✔
262
  for (auto sqrtkT_val : sqrtkT_)
286✔
263
    temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
132✔
264
  write_dataset(cell_group, "temperature", temps);
154✔
265

266
  // Write density for one or more cell instances
267
  if (type_ == Fill::MATERIAL && material_.size() > 0) {
154!
268
    vector<double> density;
132✔
269
    for (int32_t i = 0; i < density_mult_.size(); ++i)
264✔
270
      density.push_back(this->density(i));
132✔
271

272
    write_dataset(cell_group, "density", density);
132✔
273
  }
132✔
274

275
  close_group(cell_group);
154✔
276
}
154✔
277

278
void Cell::import_properties_hdf5(hid_t group)
176✔
279
{
280
  auto cell_group = open_group(group, fmt::format("cell {}", id_));
352✔
281

282
  // Read temperatures from file
283
  vector<double> temps;
176✔
284
  read_dataset(cell_group, "temperature", temps);
176✔
285

286
  // Ensure number of temperatures makes sense
287
  auto n_temps = temps.size();
176✔
288
  if (n_temps > 1 && n_temps != n_instances()) {
176!
289
    fatal_error(fmt::format(
×
290
      "Number of temperatures for cell {} doesn't match number of instances",
291
      id_));
×
292
  }
293

294
  // Modify temperatures for the cell
295
  sqrtkT_.clear();
176✔
296
  sqrtkT_.resize(temps.size());
176✔
297
  for (int64_t i = 0; i < temps.size(); ++i) {
308✔
298
    this->set_temperature(temps[i], i);
132✔
299
  }
300

301
  // Read densities
302
  if (object_exists(cell_group, "density")) {
176✔
303
    vector<double> density;
132✔
304
    read_dataset(cell_group, "density", density);
132✔
305

306
    // Ensure number of densities makes sense
307
    auto n_density = density.size();
132✔
308
    if (n_density > 1 && n_density != n_instances()) {
132!
309
      fatal_error(fmt::format("Number of densities for cell {} "
×
310
                              "doesn't match number of instances",
311
        id_));
×
312
    }
313

314
    // Set densities.
315
    for (int32_t i = 0; i < n_density; ++i) {
264✔
316
      this->set_density(density[i], i);
132✔
317
    }
318
  }
132✔
319

320
  close_group(cell_group);
176✔
321
}
176✔
322

323
void Cell::to_hdf5(hid_t cell_group) const
27,938✔
324
{
325

326
  // Create a group for this cell.
327
  auto group = create_group(cell_group, fmt::format("cell {}", id_));
55,876✔
328

329
  if (!name_.empty()) {
27,938✔
330
    write_string(group, "name", name_, false);
5,153✔
331
  }
332

333
  write_dataset(group, "universe", model::universes[universe_]->id_);
27,938✔
334

335
  to_hdf5_inner(group);
27,938✔
336

337
  // Write fill information.
338
  if (type_ == Fill::MATERIAL) {
27,938✔
339
    write_dataset(group, "fill_type", "material");
21,811✔
340
    std::vector<int32_t> mat_ids;
21,811✔
341
    for (auto i_mat : material_) {
44,851✔
342
      if (i_mat != MATERIAL_VOID) {
23,040✔
343
        mat_ids.push_back(model::materials[i_mat]->id_);
14,927✔
344
      } else {
345
        mat_ids.push_back(MATERIAL_VOID);
8,113✔
346
      }
347
    }
348
    if (mat_ids.size() == 1) {
21,811✔
349
      write_dataset(group, "material", mat_ids[0]);
21,628✔
350
    } else {
351
      write_dataset(group, "material", mat_ids);
183✔
352
    }
353

354
    std::vector<double> temps;
21,811✔
355
    for (auto sqrtkT_val : sqrtkT_)
44,974✔
356
      temps.push_back(sqrtkT_val * sqrtkT_val / K_BOLTZMANN);
23,163✔
357
    write_dataset(group, "temperature", temps);
21,811✔
358

359
    write_dataset(group, "density_mult", density_mult_);
21,811✔
360

361
  } else if (type_ == Fill::UNIVERSE) {
27,938✔
362
    write_dataset(group, "fill_type", "universe");
4,751✔
363
    write_dataset(group, "fill", model::universes[fill_]->id_);
4,751✔
364
    if (translation_ != Position(0, 0, 0)) {
4,751✔
365
      write_dataset(group, "translation", translation_);
2,937✔
366
    }
367
    if (!rotation_.empty()) {
4,751✔
368
      if (rotation_.size() == 12) {
264!
369
        std::array<double, 3> rot {rotation_[9], rotation_[10], rotation_[11]};
264✔
370
        write_dataset(group, "rotation", rot);
264✔
371
      } else {
372
        write_dataset(group, "rotation", rotation_);
×
373
      }
374
    }
375

376
  } else if (type_ == Fill::LATTICE) {
1,376!
377
    write_dataset(group, "fill_type", "lattice");
1,376✔
378
    write_dataset(group, "lattice", model::lattices[fill_]->id_);
1,376✔
379
  }
380

381
  close_group(group);
27,938✔
382
}
27,938✔
383

384
//==============================================================================
385
// CSGCell implementation
386
//==============================================================================
387

388
CSGCell::CSGCell(pugi::xml_node cell_node)
34,851✔
389
{
390
  if (check_for_node(cell_node, "id")) {
34,851!
391
    id_ = std::stoi(get_node_value(cell_node, "id"));
34,851✔
392
  } else {
393
    fatal_error("Must specify id of cell in geometry XML file.");
×
394
  }
395

396
  if (check_for_node(cell_node, "name")) {
34,851✔
397
    name_ = get_node_value(cell_node, "name");
7,153✔
398
  }
399

400
  if (check_for_node(cell_node, "universe")) {
34,851✔
401
    universe_ = std::stoi(get_node_value(cell_node, "universe"));
33,644✔
402
  } else {
403
    universe_ = 0;
1,207✔
404
  }
405

406
  // Check if the cell is the base of a virtual triso lattice
407
  bool virtual_lattice_present = check_for_node(cell_node, "virtual_lattice");
34,851✔
408
  if (virtual_lattice_present) {
34,851✔
409
    virtual_lattice_ = get_node_value_bool(cell_node, "virtual_lattice");
16✔
410
    if (virtual_lattice_) {
16!
411
      if (check_for_node(cell_node, "lower_left") &&
16✔
412
          check_for_node(cell_node, "pitch") &&
32!
413
          check_for_node(cell_node, "shape")) {
16!
414
        vl_lower_left_ = get_node_array<double>(cell_node, "lower_left");
16✔
415
        vl_pitch_ = get_node_array<double>(cell_node, "pitch");
16✔
416
        vl_shape_ = get_node_array<int>(cell_node, "shape");
16✔
417
      } else {
NEW
418
        fatal_error(fmt::format("Lower_left, pitch and shape of the virtual "
×
419
                                "lattice must be specified for cell {}",
NEW
420
          id_));
×
421
      }
422
    }
423
  } else {
424
    virtual_lattice_ = false;
34,835✔
425
  }
426

427
  if (check_for_node(cell_node, "triso_particle")) {
34,851✔
428
    triso_particle_ = get_node_value_bool(cell_node, "triso_particle");
1,600✔
429
  } else {
430
    triso_particle_ = false;
33,251✔
431
  }
432

433
  // Make sure that either material or fill was specified, but not both.
434
  bool fill_present = check_for_node(cell_node, "fill");
34,851✔
435
  bool material_present = check_for_node(cell_node, "material");
34,851✔
436
  if (!(fill_present || material_present)) {
34,851!
437
    fatal_error(
×
438
      fmt::format("Neither material nor fill was specified for cell {}", id_));
×
439
  }
440
  if (fill_present && material_present) {
34,851!
441
    fatal_error(fmt::format("Cell {} has both a material and a fill specified; "
×
442
                            "only one can be specified per cell",
443
      id_));
×
444
  }
445

446
  if (fill_present) {
34,851✔
447
    fill_ = std::stoi(get_node_value(cell_node, "fill"));
8,620✔
448
    if (fill_ == universe_) {
8,620!
449
      fatal_error(fmt::format("Cell {} is filled with the same universe that "
×
450
                              "it is contained in.",
451
        id_));
×
452
    }
453
  } else {
454
    fill_ = C_NONE;
26,231✔
455
  }
456

457
  // Read the material element.  There can be zero materials (filled with a
458
  // universe), more than one material (distribmats), and some materials may
459
  // be "void".
460
  if (material_present) {
34,851✔
461
    vector<std::string> mats {
462
      get_node_array<std::string>(cell_node, "material", true)};
26,231✔
463
    if (mats.size() > 0) {
26,231!
464
      material_.reserve(mats.size());
26,231✔
465
      for (std::string mat : mats) {
53,727✔
466
        if (mat.compare("void") == 0) {
27,496✔
467
          material_.push_back(MATERIAL_VOID);
8,492✔
468
        } else {
469
          material_.push_back(std::stoi(mat));
19,004✔
470
        }
471
      }
27,496✔
472
    } else {
473
      fatal_error(fmt::format(
×
474
        "An empty material element was specified for cell {}", id_));
×
475
    }
476
  }
26,231✔
477

478
  // Read the temperature element which may be distributed like materials.
479
  if (check_for_node(cell_node, "temperature")) {
34,851✔
480
    sqrtkT_ = get_node_array<double>(cell_node, "temperature");
315✔
481
    sqrtkT_.shrink_to_fit();
315✔
482

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

491
    // Make sure all temperatures are non-negative.
492
    for (auto T : sqrtkT_) {
678✔
493
      if (T < 0) {
363!
494
        fatal_error(fmt::format(
×
495
          "Cell {} was specified with a negative temperature", id_));
×
496
      }
497
    }
498

499
    // Convert to sqrt(k*T).
500
    for (auto& T : sqrtkT_) {
678✔
501
      T = std::sqrt(K_BOLTZMANN * T);
363✔
502
    }
503
  }
504

505
  // Read the density element which can be distributed similar to temperature.
506
  // These get assigned to the density multiplier, requiring a division by
507
  // the material density.
508
  // Note: calculating the actual density multiplier is deferred until materials
509
  // are finalized. density_mult_ contains the true density in the meantime.
510
  if (check_for_node(cell_node, "density")) {
34,851✔
511
    density_mult_ = get_node_array<double>(cell_node, "density");
16✔
512
    density_mult_.shrink_to_fit();
16✔
513

514
    // Make sure this is a material-filled cell.
515
    if (material_.size() == 0) {
16!
516
      fatal_error(fmt::format(
×
517
        "Cell {} was specified with a density but no material. Density"
518
        "specification is only valid for cells filled with a material.",
519
        id_));
×
520
    }
521

522
    // Make sure this is a non-void material.
523
    for (auto mat_id : material_) {
32✔
524
      if (mat_id == MATERIAL_VOID) {
16!
525
        fatal_error(fmt::format(
×
526
          "Cell {} was specified with a density, but contains a void "
527
          "material. Density specification is only valid for cells "
528
          "filled with a non-void material.",
529
          id_));
×
530
      }
531
    }
532

533
    // Make sure all densities are non-negative and greater than zero.
534
    for (auto rho : density_mult_) {
80✔
535
      if (rho <= 0) {
64!
536
        fatal_error(fmt::format(
×
537
          "Cell {} was specified with a density less than or equal to zero",
538
          id_));
×
539
      }
540
    }
541
  }
542

543
  // Read the region specification.
544
  std::string region_spec;
34,851✔
545
  if (check_for_node(cell_node, "region")) {
34,851✔
546
    region_spec = get_node_value(cell_node, "region");
25,948✔
547
  }
548

549
  // Get a tokenized representation of the region specification and apply De
550
  // Morgans law
551
  Region region(region_spec, id_);
34,851✔
552
  region_ = region;
34,851✔
553
  vector<int32_t> rpn = region_.generate_postfix(id_);
34,851✔
554

555
  if (virtual_lattice_) {
34,851✔
556
    vl_triso_distribution_ = generate_triso_distribution(
48✔
557
      vl_shape_, vl_pitch_, vl_lower_left_, rpn, id_);
32✔
558
  }
559

560
  if (triso_particle_) {
34,851✔
561
    if (rpn.size() != 1) {
1,600!
NEW
562
      fatal_error(
×
NEW
563
        fmt::format("Wrong surface definition of triso particle cell {}", id_));
×
564
    } else {
565
      model::surfaces[abs(rpn[0]) - 1]->connect_to_triso_base(id_, "particle");
1,600✔
566
    }
567
  }
568

569
  // Read the translation vector.
570
  if (check_for_node(cell_node, "translation")) {
34,851✔
571
    if (fill_ == C_NONE) {
4,326!
572
      fatal_error(fmt::format("Cannot apply a translation to cell {}"
×
573
                              " because it is not filled with another universe",
574
        id_));
×
575
    }
576

577
    auto xyz {get_node_array<double>(cell_node, "translation")};
4,326✔
578
    if (xyz.size() != 3) {
4,326!
579
      fatal_error(
×
580
        fmt::format("Non-3D translation vector applied to cell {}", id_));
×
581
    }
582
    translation_ = xyz;
4,326✔
583
  }
4,326✔
584

585
  // Read the rotation transform.
586
  if (check_for_node(cell_node, "rotation")) {
34,851✔
587
    auto rot {get_node_array<double>(cell_node, "rotation")};
416✔
588
    set_rotation(rot);
416✔
589
  }
416✔
590
}
34,851✔
591

592
//==============================================================================
593

594
void CSGCell::to_hdf5_inner(hid_t group_id) const
27,778✔
595
{
596
  write_string(group_id, "geom_type", "csg", false);
27,778✔
597
  write_string(group_id, "region", region_.str(), false);
27,778✔
598
}
27,778✔
599

600
//==============================================================================
601

602
vector<int32_t>::iterator CSGCell::find_left_parenthesis(
×
603
  vector<int32_t>::iterator start, const vector<int32_t>& infix)
604
{
605
  // start search at zero
606
  int parenthesis_level = 0;
×
607
  auto it = start;
×
608
  while (it != infix.begin()) {
×
609
    // look at two tokens at a time
610
    int32_t one = *it;
×
611
    int32_t two = *(it - 1);
×
612

613
    // decrement parenthesis level if there are two adjacent surfaces
614
    if (one < OP_UNION && two < OP_UNION) {
×
615
      parenthesis_level--;
×
616
      // increment if there are two adjacent operators
617
    } else if (one >= OP_UNION && two >= OP_UNION) {
×
618
      parenthesis_level++;
×
619
    }
620

621
    // if the level gets to zero, return the position
622
    if (parenthesis_level == 0) {
×
623
      // move the iterator back one before leaving the loop
624
      // so that all tokens in the parenthesis block are included
625
      it--;
×
626
      break;
×
627
    }
628

629
    // continue loop, one token at a time
630
    it--;
×
631
  }
632
  return it;
×
633
}
634
std::pair<double, int32_t> CSGCell::distance(
2,147,483,647✔
635
  Position r, Direction u, int32_t on_surface, GeometryState* p) const
636
{
637
  if (virtual_lattice_) {
2,147,483,647✔
638
    return distance_in_virtual_lattice(r, u, on_surface, p);
4,899,400✔
639
  } else {
640
    return region_.distance(r, u, on_surface);
2,147,483,647✔
641
  }
642
}
643

644
std::pair<double, int32_t> CSGCell::distance_in_virtual_lattice(
4,899,400✔
645
  Position r, Direction u, int32_t on_surface, GeometryState* p) const
646
{
647
  double min_dist {INFTY};
4,899,400✔
648
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
4,899,400✔
649
  double min_dis_vl;
650
  int32_t i_surf_vl;
651

652
  double max_dis = p->collision_distance();
4,899,400✔
653
  double tol_dis = 0;
4,899,400✔
654
  vector<double> dis_to_bou(3), dis_to_bou_max(3);
9,798,800✔
655
  double u_value = sqrt(pow(u.x, 2) + pow(u.y, 2) +
4,899,400✔
656
                        pow(u.z, 2)); // don't know if u has been normalized
4,899,400✔
657
  vector<double> norm_u = {u.x / u_value, u.y / u_value, u.z / u_value};
4,899,400✔
658
  vector<int> lat_ind(3);
4,899,400✔
659
  vector<double> temp_pos = {r.x, r.y, r.z};
4,899,400✔
660
  int loop_time;
661
  for (int i = 0; i < 3; i++) {
19,597,600✔
662
    lat_ind[i] = floor((temp_pos[i] - vl_lower_left_[i]) / vl_pitch_[i]);
14,698,200✔
663
    if (lat_ind[i] == vl_shape_[i] && norm_u[i] < 0) {
14,698,200!
664
      lat_ind[i] = vl_shape_[i] - 1;
1,585,540✔
665
    }
666
    if (lat_ind[i] == -1 && norm_u[i] > 0) {
14,698,200!
667
      lat_ind[i] = 0;
10,956✔
668
    }
669
  }
670

671
  dis_to_bou = {INFTY, INFTY, INFTY};
4,899,400✔
672
  for (int i = 0; i < 3; i++) {
19,597,600✔
673
    if (norm_u[i] > 0) {
14,698,200✔
674
      dis_to_bou[i] = std::abs(
7,353,841✔
675
        ((lat_ind[i] + 1) * vl_pitch_[i] + vl_lower_left_[i] - temp_pos[i]) /
7,353,841✔
676
        norm_u[i]);
7,353,841✔
677
      dis_to_bou_max[i] = vl_pitch_[i] / norm_u[i];
7,353,841✔
678
    } else if (norm_u[i] < 0) {
7,344,359!
679
      dis_to_bou[i] =
7,344,359✔
680
        std::abs((lat_ind[i] * vl_pitch_[i] + vl_lower_left_[i] - temp_pos[i]) /
14,688,718✔
681
                 norm_u[i]);
7,344,359✔
682
      dis_to_bou_max[i] = -vl_pitch_[i] / norm_u[i];
7,344,359✔
683
    }
684
  }
685

686
  while (true) {
687
    if (lat_ind[0] < 0 || lat_ind[0] >= vl_shape_[0] || lat_ind[1] < 0 ||
27,865,222✔
688
        lat_ind[1] >= vl_shape_[1] || lat_ind[2] < 0 ||
39,418,335✔
689
        lat_ind[2] >= vl_shape_[2])
11,553,113✔
690
      break;
3,174,798✔
691

692
    for (int token :
11,023,848✔
693
      vl_triso_distribution_[lat_ind[0] + lat_ind[1] * vl_shape_[0] +
11,023,848✔
694
                             lat_ind[2] * vl_shape_[0] * vl_shape_[1]]) {
99,520,553✔
695
      bool coincident {std::abs(token) == std::abs(on_surface)};
66,449,009✔
696
      double d {model::surfaces[abs(token) - 1]->distance(r, u, coincident)};
66,449,009✔
697
      if (d < min_dist) {
66,449,009✔
698
        if (min_dist - d >= FP_PRECISION * min_dist) {
1,262,503!
699
          min_dist = d;
1,262,503✔
700
          i_surf = -token;
1,262,503✔
701
        }
702
      }
703
    }
704

705
    int mes_bou_crossed = 0;
11,023,848✔
706
    if (dis_to_bou[1] < dis_to_bou[0]) {
11,023,848✔
707
      mes_bou_crossed = 1;
5,499,956✔
708
    }
709
    if (dis_to_bou[2] < dis_to_bou[mes_bou_crossed]) {
11,023,848✔
710
      mes_bou_crossed = 2;
3,669,820✔
711
    }
712

713
    tol_dis = dis_to_bou[mes_bou_crossed];
11,023,848✔
714

715
    if (min_dist < tol_dis) {
11,023,848✔
716
      break;
1,223,398✔
717
    }
718

719
    if (norm_u[mes_bou_crossed] > 0) {
9,800,450✔
720
      lat_ind[mes_bou_crossed] += 1;
4,899,433✔
721
    } else {
722
      lat_ind[mes_bou_crossed] += -1;
4,901,017✔
723
    }
724

725
    dis_to_bou[mes_bou_crossed] += dis_to_bou_max[mes_bou_crossed];
9,800,450✔
726

727
    if (tol_dis > max_dis) {
9,800,450✔
728
      break;
501,204✔
729
    }
730
  }
9,299,246✔
731
  return {min_dist, i_surf};
9,798,800✔
732
}
4,899,400✔
733

734
//==============================================================================
735
// Region implementation
736
//==============================================================================
737

738
Region::Region(std::string region_spec, int32_t cell_id)
34,851✔
739
{
740
  // Check if region_spec is not empty.
741
  if (!region_spec.empty()) {
34,851✔
742
    // Parse all halfspaces and operators except for intersection (whitespace).
743
    for (int i = 0; i < region_spec.size();) {
148,240✔
744
      if (region_spec[i] == '(') {
122,292✔
745
        expression_.push_back(OP_LEFT_PAREN);
1,176✔
746
        i++;
1,176✔
747

748
      } else if (region_spec[i] == ')') {
121,116✔
749
        expression_.push_back(OP_RIGHT_PAREN);
1,176✔
750
        i++;
1,176✔
751

752
      } else if (region_spec[i] == '|') {
119,940✔
753
        expression_.push_back(OP_UNION);
3,679✔
754
        i++;
3,679✔
755

756
      } else if (region_spec[i] == '~') {
116,261✔
757
        expression_.push_back(OP_COMPLEMENT);
32✔
758
        i++;
32✔
759

760
      } else if (region_spec[i] == '-' || region_spec[i] == '+' ||
195,951!
761
                 std::isdigit(region_spec[i])) {
79,722✔
762
        // This is the start of a halfspace specification.  Iterate j until we
763
        // find the end, then push-back everything between i and j.
764
        int j = i + 1;
68,454✔
765
        while (j < region_spec.size() && std::isdigit(region_spec[j])) {
144,498✔
766
          j++;
76,044✔
767
        }
768
        expression_.push_back(std::stoi(region_spec.substr(i, j - i)));
68,454✔
769
        i = j;
68,454✔
770

771
      } else if (std::isspace(region_spec[i])) {
47,775!
772
        i++;
47,775✔
773

774
      } else {
775
        auto err_msg =
776
          fmt::format("Region specification contains invalid character, \"{}\"",
777
            region_spec[i]);
×
778
        fatal_error(err_msg);
×
779
      }
×
780
    }
781

782
    // Add in intersection operators where a missing operator is needed.
783
    int i = 0;
25,948✔
784
    while (i < expression_.size() - 1) {
113,344✔
785
      bool left_compat {
786
        (expression_[i] < OP_UNION) || (expression_[i] == OP_RIGHT_PAREN)};
87,396✔
787
      bool right_compat {(expression_[i + 1] < OP_UNION) ||
87,396✔
788
                         (expression_[i + 1] == OP_LEFT_PAREN) ||
92,315✔
789
                         (expression_[i + 1] == OP_COMPLEMENT)};
4,919✔
790
      if (left_compat && right_compat) {
87,396✔
791
        expression_.insert(expression_.begin() + i + 1, OP_INTERSECTION);
38,827✔
792
      }
793
      i++;
87,396✔
794
    }
795

796
    // Remove complement operators using DeMorgan's laws
797
    auto it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
25,948✔
798
    while (it != expression_.end()) {
25,980✔
799
      // Erase complement
800
      expression_.erase(it);
32✔
801

802
      // Define stop given left parenthesis or not
803
      auto stop = it;
32✔
804
      if (*it == OP_LEFT_PAREN) {
32!
805
        int depth = 1;
32✔
806
        do {
807
          stop++;
256✔
808
          if (*stop > OP_COMPLEMENT) {
256✔
809
            if (*stop == OP_RIGHT_PAREN) {
32!
810
              depth--;
32✔
811
            } else {
812
              depth++;
×
813
            }
814
          }
815
        } while (depth > 0);
256✔
816
        it++;
32✔
817
      }
818

819
      // apply DeMorgan's law to any surfaces/operators between these
820
      // positions in the RPN
821
      apply_demorgan(it, stop);
32✔
822
      // update iterator position
823
      it = std::find(expression_.begin(), expression_.end(), OP_COMPLEMENT);
32✔
824
    }
825

826
    // Convert user IDs to surface indices.
827
    for (auto& r : expression_) {
139,260✔
828
      if (r < OP_UNION) {
113,312✔
829
        const auto& it {model::surface_map.find(abs(r))};
68,454✔
830
        if (it == model::surface_map.end()) {
68,454!
831
          throw std::runtime_error {
×
832
            "Invalid surface ID " + std::to_string(abs(r)) +
×
833
            " specified in region for cell " + std::to_string(cell_id) + "."};
×
834
        }
835
        r = (r > 0) ? it->second + 1 : -(it->second + 1);
68,454✔
836
      }
837
    }
838

839
    // Check if this is a simple cell.
840
    simple_ = true;
25,948✔
841
    for (int32_t token : expression_) {
126,936✔
842
      if (token == OP_UNION) {
102,081✔
843
        simple_ = false;
1,093✔
844
        // Ensure intersections have precedence over unions
845
        add_precedence();
1,093✔
846
        break;
1,093✔
847
      }
848
    }
849

850
    // If this cell is simple, remove all the superfluous operator tokens.
851
    if (simple_) {
25,948✔
852
      for (auto it = expression_.begin(); it != expression_.end(); it++) {
119,718✔
853
        if (*it == OP_INTERSECTION || *it > OP_COMPLEMENT) {
94,863!
854
          expression_.erase(it);
35,004✔
855
          it--;
35,004✔
856
        }
857
      }
858
    }
859
    expression_.shrink_to_fit();
25,948✔
860

861
  } else {
862
    simple_ = true;
8,903✔
863
  }
864
}
34,851✔
865

866
//==============================================================================
867

868
void Region::apply_demorgan(
224✔
869
  vector<int32_t>::iterator start, vector<int32_t>::iterator stop)
870
{
871
  do {
872
    if (*start < OP_UNION) {
224✔
873
      *start *= -1;
128✔
874
    } else if (*start == OP_UNION) {
96!
875
      *start = OP_INTERSECTION;
×
876
    } else if (*start == OP_INTERSECTION) {
96!
877
      *start = OP_UNION;
96✔
878
    }
879
    start++;
224✔
880
  } while (start < stop);
224✔
881
}
32✔
882

883
//==============================================================================
884
//! Add precedence for infix regions so intersections have higher
885
//! precedence than unions using parentheses.
886
//==============================================================================
887

888
int64_t Region::add_parentheses(int64_t start)
32✔
889
{
890
  int32_t start_token = expression_[start];
32✔
891
  // Add left parenthesis and set new position to be after parenthesis
892
  if (start_token == OP_UNION) {
32!
893
    start += 2;
×
894
  }
895
  expression_.insert(expression_.begin() + start - 1, OP_LEFT_PAREN);
32✔
896

897
  // Keep track of return iterator distance. If we don't encounter a left
898
  // parenthesis, we return an iterator corresponding to wherever the right
899
  // parenthesis is inserted. If a left parenthesis is encountered, an iterator
900
  // corresponding to the left parenthesis is returned. Also note that we keep
901
  // track of a *distance* instead of an iterator because the underlying memory
902
  // allocation may change.
903
  std::size_t return_it_dist = 0;
32✔
904

905
  // Add right parenthesis
906
  // While the start iterator is within the bounds of infix
907
  while (start + 1 < expression_.size()) {
224!
908
    start++;
224✔
909

910
    // If the current token is an operator and is different than the start token
911
    if (expression_[start] >= OP_UNION && expression_[start] != start_token) {
224✔
912
      // Skip wrapped regions but save iterator position to check precedence and
913
      // add right parenthesis, right parenthesis position depends on the
914
      // operator, when the operator is a union then do not include the operator
915
      // in the region, when the operator is an intersection then include the
916
      // operator and next surface
917
      if (expression_[start] == OP_LEFT_PAREN) {
32!
918
        return_it_dist = start;
×
919
        int depth = 1;
×
920
        do {
921
          start++;
×
922
          if (expression_[start] > OP_COMPLEMENT) {
×
923
            if (expression_[start] == OP_RIGHT_PAREN) {
×
924
              depth--;
×
925
            } else {
926
              depth++;
×
927
            }
928
          }
929
        } while (depth > 0);
×
930
      } else {
931
        if (start_token == OP_UNION) {
32!
932
          --start;
×
933
        }
934
        expression_.insert(expression_.begin() + start, OP_RIGHT_PAREN);
32✔
935
        if (return_it_dist > 0) {
32!
936
          return return_it_dist;
×
937
        } else {
938
          return start - 1;
32✔
939
        }
940
      }
941
    }
942
  }
943
  // If we get here a right parenthesis hasn't been placed,
944
  // return iterator
945
  expression_.push_back(OP_RIGHT_PAREN);
×
946
  if (return_it_dist > 0) {
×
947
    return return_it_dist;
×
948
  } else {
949
    return start - 1;
×
950
  }
951
}
952

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

955
void Region::add_precedence()
1,093✔
956
{
957
  int32_t current_op = 0;
1,093✔
958
  std::size_t current_dist = 0;
1,093✔
959

960
  for (int64_t i = 0; i < expression_.size(); i++) {
19,510✔
961
    int32_t token = expression_[i];
18,417✔
962

963
    if (token == OP_UNION || token == OP_INTERSECTION) {
18,417✔
964
      if (current_op == 0) {
7,486✔
965
        // Set the current operator if is hasn't been set
966
        current_op = token;
2,349✔
967
        current_dist = i;
2,349✔
968
      } else if (token != current_op) {
5,137✔
969
        // If the current operator doesn't match the token, add parenthesis to
970
        // assert precedence
971
        if (current_op == OP_INTERSECTION) {
32✔
972
          i = add_parentheses(current_dist);
16✔
973
        } else {
974
          i = add_parentheses(i);
16✔
975
        }
976
        current_op = 0;
32✔
977
        current_dist = 0;
32✔
978
      }
979
    } else if (token > OP_COMPLEMENT) {
10,931✔
980
      // If the token is a parenthesis reset the current operator
981
      current_op = 0;
2,384✔
982
      current_dist = 0;
2,384✔
983
    }
984
  }
985
}
1,093✔
986

987
//==============================================================================
988
//! Convert infix region specification to Reverse Polish Notation (RPN)
989
//!
990
//! This function uses the shunting-yard algorithm.
991
//==============================================================================
992

993
vector<int32_t> Region::generate_postfix(int32_t cell_id) const
34,895✔
994
{
995
  vector<int32_t> rpn;
34,895✔
996
  vector<int32_t> stack;
34,895✔
997

998
  for (int32_t token : expression_) {
114,213✔
999
    if (token < OP_UNION) {
79,318✔
1000
      // If token is not an operator, add it to output
1001
      rpn.push_back(token);
68,850✔
1002
    } else if (token < OP_RIGHT_PAREN) {
10,468✔
1003
      // Regular operators union, intersection, complement
1004
      while (stack.size() > 0) {
13,264✔
1005
        int32_t op = stack.back();
9,896✔
1006

1007
        if (op < OP_RIGHT_PAREN && ((token == OP_COMPLEMENT && token < op) ||
9,896!
1008
                                     (token != OP_COMPLEMENT && token <= op))) {
5,410!
1009
          // While there is an operator, op, on top of the stack, if the token
1010
          // is left-associative and its precedence is less than or equal to
1011
          // that of op or if the token is right-associative and its precedence
1012
          // is less than that of op, move op to the output queue and push the
1013
          // token on to the stack. Note that only complement is
1014
          // right-associative.
1015
          rpn.push_back(op);
5,410✔
1016
          stack.pop_back();
5,410✔
1017
        } else {
1018
          break;
1019
        }
1020
      }
1021

1022
      stack.push_back(token);
7,854✔
1023

1024
    } else if (token == OP_LEFT_PAREN) {
2,614✔
1025
      // If the token is a left parenthesis, push it onto the stack
1026
      stack.push_back(token);
1,307✔
1027

1028
    } else {
1029
      // If the token is a right parenthesis, move operators from the stack to
1030
      // the output queue until reaching the left parenthesis.
1031
      for (auto it = stack.rbegin(); *it != OP_LEFT_PAREN; it++) {
2,614✔
1032
        // If we run out of operators without finding a left parenthesis, it
1033
        // means there are mismatched parentheses.
1034
        if (it == stack.rend()) {
1,307!
1035
          fatal_error(fmt::format(
×
1036
            "Mismatched parentheses in region specification for cell {}",
1037
            cell_id));
1038
        }
1039
        rpn.push_back(stack.back());
1,307✔
1040
        stack.pop_back();
1,307✔
1041
      }
1042

1043
      // Pop the left parenthesis.
1044
      stack.pop_back();
1,307✔
1045
    }
1046
  }
1047

1048
  while (stack.size() > 0) {
36,032✔
1049
    int32_t op = stack.back();
1,137✔
1050

1051
    // If the operator is a parenthesis it is mismatched.
1052
    if (op >= OP_RIGHT_PAREN) {
1,137!
1053
      fatal_error(fmt::format(
×
1054
        "Mismatched parentheses in region specification for cell {}", cell_id));
1055
    }
1056

1057
    rpn.push_back(stack.back());
1,137✔
1058
    stack.pop_back();
1,137✔
1059
  }
1060

1061
  return rpn;
69,790✔
1062
}
34,895✔
1063

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

1066
std::string Region::str() const
27,778✔
1067
{
1068
  std::stringstream region_spec {};
27,778✔
1069
  if (!expression_.empty()) {
27,778✔
1070
    for (int32_t token : expression_) {
80,956✔
1071
      if (token == OP_LEFT_PAREN) {
61,275✔
1072
        region_spec << " (";
1,027✔
1073
      } else if (token == OP_RIGHT_PAREN) {
60,248✔
1074
        region_spec << " )";
1,027✔
1075
      } else if (token == OP_COMPLEMENT) {
59,221!
1076
        region_spec << " ~";
×
1077
      } else if (token == OP_INTERSECTION) {
59,221✔
1078
      } else if (token == OP_UNION) {
56,112✔
1079
        region_spec << " |";
3,220✔
1080
      } else {
1081
        // Note the off-by-one indexing
1082
        auto surf_id = model::surfaces[abs(token) - 1]->id_;
52,892✔
1083
        region_spec << " " << ((token > 0) ? surf_id : -surf_id);
52,892✔
1084
      }
1085
    }
1086
  }
1087
  return region_spec.str();
55,556✔
1088
}
27,778✔
1089

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

1092
std::pair<double, int32_t> Region::distance(
2,147,483,647✔
1093
  Position r, Direction u, int32_t on_surface) const
1094
{
1095
  double min_dist {INFTY};
2,147,483,647✔
1096
  int32_t i_surf {std::numeric_limits<int32_t>::max()};
2,147,483,647✔
1097

1098
  for (int32_t token : expression_) {
2,147,483,647✔
1099
    // Ignore this token if it corresponds to an operator rather than a region.
1100
    if (token >= OP_UNION)
2,147,483,647✔
1101
      continue;
170,739,011✔
1102

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

1108
    // Check if this distance is the new minimum.
1109
    if (d < min_dist) {
2,147,483,647✔
1110
      if (min_dist - d >= FP_PRECISION * min_dist) {
2,147,483,647!
1111
        min_dist = d;
2,147,483,647✔
1112
        i_surf = -token;
2,147,483,647✔
1113
      }
1114
    }
1115
  }
1116

1117
  return {min_dist, i_surf};
2,147,483,647✔
1118
}
1119

1120
//==============================================================================
1121

1122
bool Region::contains(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
1123
{
1124
  if (simple_) {
2,147,483,647✔
1125
    return contains_simple(r, u, on_surface);
2,147,483,647✔
1126
  } else {
1127
    return contains_complex(r, u, on_surface);
6,808,308✔
1128
  }
1129
}
1130

1131
//==============================================================================
1132

1133
bool Region::contains_simple(Position r, Direction u, int32_t on_surface) const
2,147,483,647✔
1134
{
1135
  for (int32_t token : expression_) {
2,147,483,647✔
1136
    // Assume that no tokens are operators. Evaluate the sense of particle with
1137
    // respect to the surface and see if the token matches the sense. If the
1138
    // particle's surface attribute is set and matches the token, that
1139
    // overrides the determination based on sense().
1140
    if (token == on_surface) {
2,147,483,647✔
1141
    } else if (-token == on_surface) {
2,147,483,647✔
1142
      return false;
987,921,788✔
1143
    } else {
1144
      // Note the off-by-one indexing
1145
      bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
2,147,483,647✔
1146
      if (sense != (token > 0)) {
2,147,483,647✔
1147
        return false;
841,115,851✔
1148
      }
1149
    }
1150
  }
1151
  return true;
2,147,483,647✔
1152
}
1153

1154
//==============================================================================
1155

1156
bool Region::contains_complex(Position r, Direction u, int32_t on_surface) const
6,808,308✔
1157
{
1158
  bool in_cell = true;
6,808,308✔
1159
  int total_depth = 0;
6,808,308✔
1160

1161
  // For each token
1162
  for (auto it = expression_.begin(); it != expression_.end(); it++) {
107,581,988✔
1163
    int32_t token = *it;
102,693,373✔
1164

1165
    // If the token is a surface evaluate the sense
1166
    // If the token is a union or intersection check to
1167
    // short circuit
1168
    if (token < OP_UNION) {
102,693,373✔
1169
      if (token == on_surface) {
44,925,302✔
1170
        in_cell = true;
3,415,192✔
1171
      } else if (-token == on_surface) {
41,510,110✔
1172
        in_cell = false;
669,458✔
1173
      } else {
1174
        // Note the off-by-one indexing
1175
        bool sense = model::surfaces[abs(token) - 1]->sense(r, u);
40,840,652✔
1176
        in_cell = (sense == (token > 0));
40,840,652✔
1177
      }
1178
    } else if ((token == OP_UNION && in_cell == true) ||
57,768,071✔
1179
               (token == OP_INTERSECTION && in_cell == false)) {
28,203,824✔
1180
      // If the total depth is zero return
1181
      if (total_depth == 0) {
6,324,992✔
1182
        return in_cell;
1,919,693✔
1183
      }
1184

1185
      total_depth--;
4,405,299✔
1186

1187
      // While the iterator is within the bounds of the vector
1188
      int depth = 1;
4,405,299✔
1189
      do {
1190
        // Get next token
1191
        it++;
27,853,430✔
1192
        int32_t next_token = *it;
27,853,430✔
1193

1194
        // If the token is an a parenthesis
1195
        if (next_token > OP_COMPLEMENT) {
27,853,430✔
1196
          // Adjust depth accordingly
1197
          if (next_token == OP_RIGHT_PAREN) {
4,599,383✔
1198
            depth--;
4,502,341✔
1199
          } else {
1200
            depth++;
97,042✔
1201
          }
1202
        }
1203
      } while (depth > 0);
27,853,430✔
1204
    } else if (token == OP_LEFT_PAREN) {
55,848,378✔
1205
      total_depth++;
8,865,692✔
1206
    } else if (token == OP_RIGHT_PAREN) {
42,577,387✔
1207
      total_depth--;
4,460,393✔
1208
    }
1209
  }
1210
  return in_cell;
4,888,615✔
1211
}
1212

1213
//==============================================================================
1214

1215
BoundingBox Region::bounding_box(int32_t cell_id) const
88✔
1216
{
1217
  if (simple_) {
88✔
1218
    return bounding_box_simple();
44✔
1219
  } else {
1220
    auto postfix = generate_postfix(cell_id);
44✔
1221
    return bounding_box_complex(postfix);
44✔
1222
  }
44✔
1223
}
1224

1225
//==============================================================================
1226

1227
BoundingBox Region::bounding_box_simple() const
44✔
1228
{
1229
  BoundingBox bbox;
44✔
1230
  for (int32_t token : expression_) {
176✔
1231
    bbox &= model::surfaces[abs(token) - 1]->bounding_box(token > 0);
132✔
1232
  }
1233
  return bbox;
44✔
1234
}
1235

1236
//==============================================================================
1237

1238
BoundingBox Region::bounding_box_complex(vector<int32_t> postfix) const
44✔
1239
{
1240
  vector<BoundingBox> stack(postfix.size());
44✔
1241
  int i_stack = -1;
44✔
1242

1243
  for (auto& token : postfix) {
792✔
1244
    if (token == OP_UNION) {
748✔
1245
      stack[i_stack - 1] = stack[i_stack - 1] | stack[i_stack];
154✔
1246
      i_stack--;
154✔
1247
    } else if (token == OP_INTERSECTION) {
594✔
1248
      stack[i_stack - 1] = stack[i_stack - 1] & stack[i_stack];
198✔
1249
      i_stack--;
198✔
1250
    } else {
1251
      i_stack++;
396✔
1252
      stack[i_stack] = model::surfaces[abs(token) - 1]->bounding_box(token > 0);
396✔
1253
    }
1254
  }
1255

1256
  assert(i_stack == 0);
36!
1257
  return stack.front();
88✔
1258
}
44✔
1259

1260
//==============================================================================
1261

1262
vector<int32_t> Region::surfaces() const
6,939✔
1263
{
1264
  if (simple_) {
6,939!
1265
    return expression_;
6,939✔
1266
  }
1267

1268
  vector<int32_t> surfaces = expression_;
×
1269

1270
  auto it = std::find_if(surfaces.begin(), surfaces.end(),
×
1271
    [&](const auto& value) { return value >= OP_UNION; });
×
1272

1273
  while (it != surfaces.end()) {
×
1274
    surfaces.erase(it);
×
1275

1276
    it = std::find_if(surfaces.begin(), surfaces.end(),
×
1277
      [&](const auto& value) { return value >= OP_UNION; });
×
1278
  }
1279

1280
  return surfaces;
×
1281
}
×
1282

1283
//==============================================================================
1284
// Non-method functions
1285
//==============================================================================
1286

1287
void read_cells(pugi::xml_node node)
7,605✔
1288
{
1289
  // Count the number of cells.
1290
  int n_cells = 0;
7,605✔
1291
  for (pugi::xml_node cell_node : node.children("cell")) {
42,456✔
1292
    n_cells++;
34,851✔
1293
  }
1294

1295
  // Loop over XML cell elements and populate the array.
1296
  model::cells.reserve(n_cells);
7,605✔
1297
  for (pugi::xml_node cell_node : node.children("cell")) {
42,456✔
1298
    model::cells.push_back(make_unique<CSGCell>(cell_node));
34,851✔
1299
  }
1300

1301
  // Fill the cell map.
1302
  for (int i = 0; i < model::cells.size(); i++) {
42,456✔
1303
    int32_t id = model::cells[i]->id_;
34,851✔
1304
    auto search = model::cell_map.find(id);
34,851✔
1305
    if (search == model::cell_map.end()) {
34,851!
1306
      model::cell_map[id] = i;
34,851✔
1307
    } else {
1308
      fatal_error(
×
1309
        fmt::format("Two or more cells use the same unique ID: {}", id));
×
1310
    }
1311
  }
1312

1313
  read_dagmc_universes(node);
7,605✔
1314

1315
  populate_universes();
7,603✔
1316

1317
  // Allocate the cell overlap count if necessary.
1318
  if (settings::check_overlaps) {
7,603✔
1319
    model::overlap_check_count.resize(model::cells.size(), 0);
119✔
1320
  }
1321

1322
  if (model::cells.size() == 0) {
7,603!
1323
    fatal_error("No cells were found in the geometry.xml file");
×
1324
  }
1325
}
7,603✔
1326

1327
void populate_universes()
7,605✔
1328
{
1329
  // Used to map universe index to the index of an implicit complement cell for
1330
  // DAGMC universes
1331
  std::unordered_map<int, int> implicit_comp_cells;
7,605✔
1332

1333
  // Populate the Universe vector and map.
1334
  for (int index_cell = 0; index_cell < model::cells.size(); index_cell++) {
42,668✔
1335
    int32_t uid = model::cells[index_cell]->universe_;
35,063✔
1336
    auto it = model::universe_map.find(uid);
35,063✔
1337
    if (it == model::universe_map.end()) {
35,063✔
1338
      model::universes.push_back(make_unique<Universe>());
19,155✔
1339
      model::universes.back()->id_ = uid;
19,155✔
1340
      model::universes.back()->cells_.push_back(index_cell);
19,155✔
1341
      model::universe_map[uid] = model::universes.size() - 1;
19,155✔
1342
    } else {
1343
#ifdef OPENMC_DAGMC_ENABLED
1344
      // Skip implicit complement cells for now
1345
      Universe* univ = model::universes[it->second].get();
2,066✔
1346
      DAGUniverse* dag_univ = dynamic_cast<DAGUniverse*>(univ);
2,066!
1347
      if (dag_univ && (dag_univ->implicit_complement_idx() == index_cell)) {
2,066✔
1348
        implicit_comp_cells[it->second] = index_cell;
44✔
1349
        continue;
44✔
1350
      }
1351
#endif
1352

1353
      model::universes[it->second]->cells_.push_back(index_cell);
15,864✔
1354
    }
1355
    if (model::cells[index_cell]->virtual_lattice_) {
35,019✔
1356
      model::universes[it->second]->filled_with_triso_base_ =
16✔
1357
        model::cells[index_cell]->id_;
16✔
1358
    }
1359
  }
1360

1361
  // Add DAGUniverse implicit complement cells last
1362
  for (const auto& it : implicit_comp_cells) {
7,649✔
1363
    int index_univ = it.first;
44✔
1364
    int index_cell = it.second;
44✔
1365
    model::universes[index_univ]->cells_.push_back(index_cell);
44!
1366
  }
1367

1368
  model::universes.shrink_to_fit();
7,605✔
1369
}
7,605✔
1370

1371
//==============================================================================
1372
// C-API functions
1373
//==============================================================================
1374

1375
extern "C" int openmc_cell_get_fill(
177✔
1376
  int32_t index, int* type, int32_t** indices, int32_t* n)
1377
{
1378
  if (index >= 0 && index < model::cells.size()) {
177!
1379
    Cell& c {*model::cells[index]};
177✔
1380
    *type = static_cast<int>(c.type_);
177✔
1381
    if (c.type_ == Fill::MATERIAL) {
177!
1382
      *indices = c.material_.data();
177✔
1383
      *n = c.material_.size();
177✔
1384
    } else {
1385
      *indices = &c.fill_;
×
1386
      *n = 1;
×
1387
    }
1388
  } else {
1389
    set_errmsg("Index in cells array is out of bounds.");
×
1390
    return OPENMC_E_OUT_OF_BOUNDS;
×
1391
  }
1392
  return 0;
177✔
1393
}
1394

1395
extern "C" int openmc_cell_set_fill(
11✔
1396
  int32_t index, int type, int32_t n, const int32_t* indices)
1397
{
1398
  Fill filltype = static_cast<Fill>(type);
11✔
1399
  if (index >= 0 && index < model::cells.size()) {
11!
1400
    Cell& c {*model::cells[index]};
11✔
1401
    if (filltype == Fill::MATERIAL) {
11!
1402
      c.type_ = Fill::MATERIAL;
11✔
1403
      c.material_.clear();
11✔
1404
      for (int i = 0; i < n; i++) {
22✔
1405
        int i_mat = indices[i];
11✔
1406
        if (i_mat == MATERIAL_VOID) {
11!
1407
          c.material_.push_back(MATERIAL_VOID);
×
1408
        } else if (i_mat >= 0 && i_mat < model::materials.size()) {
11!
1409
          c.material_.push_back(i_mat);
11✔
1410
        } else {
1411
          set_errmsg("Index in materials array is out of bounds.");
×
1412
          return OPENMC_E_OUT_OF_BOUNDS;
×
1413
        }
1414
      }
1415
      c.material_.shrink_to_fit();
11✔
1416
    } else if (filltype == Fill::UNIVERSE) {
×
1417
      c.type_ = Fill::UNIVERSE;
×
1418
    } else {
1419
      c.type_ = Fill::LATTICE;
×
1420
    }
1421
  } else {
1422
    set_errmsg("Index in cells array is out of bounds.");
×
1423
    return OPENMC_E_OUT_OF_BOUNDS;
×
1424
  }
1425
  return 0;
11✔
1426
}
1427

1428
extern "C" int openmc_cell_set_temperature(
88✔
1429
  int32_t index, double T, const int32_t* instance, bool set_contained)
1430
{
1431
  if (index < 0 || index >= model::cells.size()) {
88!
1432
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1433
    return OPENMC_E_OUT_OF_BOUNDS;
×
1434
  }
1435

1436
  int32_t instance_index = instance ? *instance : -1;
88✔
1437
  try {
1438
    model::cells[index]->set_temperature(T, instance_index, set_contained);
88✔
1439
  } catch (const std::exception& e) {
×
1440
    set_errmsg(e.what());
×
1441
    return OPENMC_E_UNASSIGNED;
×
1442
  }
×
1443
  return 0;
88✔
1444
}
1445

1446
extern "C" int openmc_cell_set_density(
88✔
1447
  int32_t index, double density, const int32_t* instance, bool set_contained)
1448
{
1449
  if (index < 0 || index >= model::cells.size()) {
88!
1450
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1451
    return OPENMC_E_OUT_OF_BOUNDS;
×
1452
  }
1453

1454
  int32_t instance_index = instance ? *instance : -1;
88✔
1455
  try {
1456
    model::cells[index]->set_density(density, instance_index, set_contained);
88✔
1457
  } catch (const std::exception& e) {
×
1458
    set_errmsg(e.what());
×
1459
    return OPENMC_E_UNASSIGNED;
×
1460
  }
×
1461
  return 0;
88✔
1462
}
1463

1464
extern "C" int openmc_cell_get_temperature(
91✔
1465
  int32_t index, const int32_t* instance, double* T)
1466
{
1467
  if (index < 0 || index >= model::cells.size()) {
91!
1468
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1469
    return OPENMC_E_OUT_OF_BOUNDS;
×
1470
  }
1471

1472
  int32_t instance_index = instance ? *instance : -1;
91✔
1473
  try {
1474
    *T = model::cells[index]->temperature(instance_index);
91✔
1475
  } catch (const std::exception& e) {
×
1476
    set_errmsg(e.what());
×
1477
    return OPENMC_E_UNASSIGNED;
×
1478
  }
×
1479
  return 0;
91✔
1480
}
1481

1482
extern "C" int openmc_cell_get_density(
88✔
1483
  int32_t index, const int32_t* instance, double* density)
1484
{
1485
  if (index < 0 || index >= model::cells.size()) {
88!
1486
    strcpy(openmc_err_msg, "Index in cells array is out of bounds.");
×
1487
    return OPENMC_E_OUT_OF_BOUNDS;
×
1488
  }
1489

1490
  int32_t instance_index = instance ? *instance : -1;
88✔
1491
  try {
1492
    if (model::cells[index]->type_ != Fill::MATERIAL) {
88!
1493
      fatal_error(
×
1494
        fmt::format("Cell {}, instance {} is not filled with a material.",
×
1495
          model::cells[index]->id_, instance_index));
×
1496
    }
1497

1498
    int32_t mat_index = model::cells[index]->material(instance_index);
88✔
1499
    if (mat_index == MATERIAL_VOID) {
88!
1500
      *density = 0.0;
×
1501
    } else {
1502
      *density = model::cells[index]->density_mult(instance_index) *
176✔
1503
                 model::materials[mat_index]->density_gpcc();
88✔
1504
    }
1505
  } catch (const std::exception& e) {
×
1506
    set_errmsg(e.what());
×
1507
    return OPENMC_E_UNASSIGNED;
×
1508
  }
×
1509
  return 0;
88✔
1510
}
1511

1512
//! Get the bounding box of a cell
1513
extern "C" int openmc_cell_bounding_box(
55✔
1514
  const int32_t index, double* llc, double* urc)
1515
{
1516

1517
  BoundingBox bbox;
55✔
1518

1519
  const auto& c = model::cells[index];
55✔
1520
  bbox = c->bounding_box();
55✔
1521

1522
  // set lower left corner values
1523
  llc[0] = bbox.xmin;
55✔
1524
  llc[1] = bbox.ymin;
55✔
1525
  llc[2] = bbox.zmin;
55✔
1526

1527
  // set upper right corner values
1528
  urc[0] = bbox.xmax;
55✔
1529
  urc[1] = bbox.ymax;
55✔
1530
  urc[2] = bbox.zmax;
55✔
1531

1532
  return 0;
55✔
1533
}
1534

1535
//! Get the name of a cell
1536
extern "C" int openmc_cell_get_name(int32_t index, const char** name)
22✔
1537
{
1538
  if (index < 0 || index >= model::cells.size()) {
22!
1539
    set_errmsg("Index in cells array is out of bounds.");
×
1540
    return OPENMC_E_OUT_OF_BOUNDS;
×
1541
  }
1542

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

1545
  return 0;
22✔
1546
}
1547

1548
//! Set the name of a cell
1549
extern "C" int openmc_cell_set_name(int32_t index, const char* name)
11✔
1550
{
1551
  if (index < 0 || index >= model::cells.size()) {
11!
1552
    set_errmsg("Index in cells array is out of bounds.");
×
1553
    return OPENMC_E_OUT_OF_BOUNDS;
×
1554
  }
1555

1556
  model::cells[index]->set_name(name);
11✔
1557

1558
  return 0;
11✔
1559
}
1560

1561
//==============================================================================
1562
//! Define a containing (parent) cell
1563
//==============================================================================
1564

1565
//! Used to locate a universe fill in the geometry
1566
struct ParentCell {
1567
  bool operator==(const ParentCell& other) const
144✔
1568
  {
1569
    return cell_index == other.cell_index &&
288!
1570
           lattice_index == other.lattice_index;
288!
1571
  }
1572

1573
  bool operator<(const ParentCell& other) const
1574
  {
1575
    return cell_index < other.cell_index ||
1576
           (cell_index == other.cell_index &&
1577
             lattice_index < other.lattice_index);
1578
  }
1579

1580
  int64_t cell_index;
1581
  int64_t lattice_index;
1582
};
1583

1584
//! Structure used to insert ParentCell into hashed STL data structures
1585
struct ParentCellHash {
1586
  std::size_t operator()(const ParentCell& p) const
673✔
1587
  {
1588
    return 4096 * p.cell_index + p.lattice_index;
673✔
1589
  }
1590
};
1591

1592
//! Used to manage a traversal stack when locating parent cells of a cell
1593
//! instance in the model
1594
struct ParentCellStack {
1595

1596
  //! push method that adds to the parent_cells visited cells for this search
1597
  //! universe
1598
  void push(int32_t search_universe, const ParentCell& pc)
112✔
1599
  {
1600
    parent_cells_.push_back(pc);
112✔
1601
    // add parent cell to the set of cells we've visited for this search
1602
    // universe
1603
    visited_cells_[search_universe].insert(pc);
112✔
1604
  }
112✔
1605

1606
  //! removes the last parent_cell and clears the visited cells for the popped
1607
  //! cell's universe
1608
  void pop()
80✔
1609
  {
1610
    visited_cells_[this->current_univ()].clear();
80✔
1611
    parent_cells_.pop_back();
80✔
1612
  }
80✔
1613

1614
  //! checks whether or not the parent cell has been visited already for this
1615
  //! search universe
1616
  bool visited(int32_t search_universe, const ParentCell& parent_cell)
561✔
1617
  {
1618
    return visited_cells_[search_universe].count(parent_cell) != 0;
561✔
1619
  }
1620

1621
  //! return the next universe to search for a parent cell
1622
  int32_t current_univ() const
80✔
1623
  {
1624
    return model::cells[parent_cells_.back().cell_index]->universe_;
80✔
1625
  }
1626

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

1630
  //! compute an instance for the provided distribcell index
1631
  int32_t compute_instance(int32_t distribcell_index) const
193✔
1632
  {
1633
    if (distribcell_index == C_NONE)
193✔
1634
      return 0;
65✔
1635

1636
    int32_t instance = 0;
128✔
1637
    for (const auto& parent_cell : this->parent_cells_) {
240✔
1638
      auto& cell = model::cells[parent_cell.cell_index];
112✔
1639
      if (cell->type_ == Fill::UNIVERSE) {
112!
1640
        instance += cell->offset_[distribcell_index];
×
1641
      } else if (cell->type_ == Fill::LATTICE) {
112!
1642
        auto& lattice = model::lattices[cell->fill_];
112✔
1643
        instance +=
112✔
1644
          lattice->offset(distribcell_index, parent_cell.lattice_index);
112✔
1645
      }
1646
    }
1647
    return instance;
128✔
1648
  }
1649

1650
  // Accessors
1651
  vector<ParentCell>& parent_cells() { return parent_cells_; }
339✔
1652
  const vector<ParentCell>& parent_cells() const { return parent_cells_; }
1653

1654
  // Data Members
1655
  vector<ParentCell> parent_cells_;
1656
  std::unordered_map<int32_t, std::unordered_set<ParentCell, ParentCellHash>>
1657
    visited_cells_;
1658
};
1659

1660
vector<ParentCell> Cell::find_parent_cells(
×
1661
  int32_t instance, const Position& r) const
1662
{
1663

1664
  // create a temporary particle
1665
  GeometryState dummy_particle {};
×
1666
  dummy_particle.r() = r;
×
1667
  dummy_particle.u() = {0., 0., 1.};
×
1668

1669
  return find_parent_cells(instance, dummy_particle);
×
1670
}
×
1671

1672
vector<ParentCell> Cell::find_parent_cells(
×
1673
  int32_t instance, GeometryState& p) const
1674
{
1675
  // look up the particle's location
1676
  exhaustive_find_cell(p);
×
1677
  const auto& coords = p.coord();
×
1678

1679
  // build a parent cell stack from the particle coordinates
1680
  ParentCellStack stack;
×
1681
  bool cell_found = false;
×
1682
  for (auto it = coords.begin(); it != coords.end(); it++) {
×
1683
    const auto& coord = *it;
×
1684
    const auto& cell = model::cells[coord.cell()];
×
1685
    // if the cell at this level matches the current cell, stop adding to the
1686
    // stack
1687
    if (coord.cell() == model::cell_map[this->id_]) {
×
1688
      cell_found = true;
×
1689
      break;
×
1690
    }
1691

1692
    // if filled with a lattice, get the lattice index from the next
1693
    // level in the coordinates to push to the stack
1694
    int lattice_idx = C_NONE;
×
1695
    if (cell->type_ == Fill::LATTICE) {
×
1696
      const auto& next_coord = *(it + 1);
×
1697
      lattice_idx = model::lattices[next_coord.lattice()]->get_flat_index(
×
1698
        next_coord.lattice_index());
1699
    }
1700
    stack.push(coord.universe(), {coord.cell(), lattice_idx});
×
1701
  }
1702

1703
  // if this loop finished because the cell was found and
1704
  // the instance matches the one requested in the call
1705
  // we have the correct path and can return the stack
1706
  if (cell_found &&
×
1707
      stack.compute_instance(this->distribcell_index_) == instance) {
×
1708
    return stack.parent_cells();
×
1709
  }
1710

1711
  // fall back on an exhaustive search for the cell's parents
1712
  return exhaustive_find_parent_cells(instance);
×
1713
}
×
1714

1715
vector<ParentCell> Cell::exhaustive_find_parent_cells(int32_t instance) const
113✔
1716
{
1717
  ParentCellStack stack;
113✔
1718
  // start with this cell's universe
1719
  int32_t prev_univ_idx;
1720
  int32_t univ_idx = this->universe_;
113✔
1721

1722
  while (true) {
1723
    const auto& univ = model::universes[univ_idx];
193✔
1724
    prev_univ_idx = univ_idx;
193✔
1725

1726
    // search for a cell that is filled w/ this universe
1727
    for (const auto& cell : model::cells) {
1,236✔
1728
      // if this is a material-filled cell, move on
1729
      if (cell->type_ == Fill::MATERIAL)
1,155✔
1730
        continue;
642✔
1731

1732
      if (cell->type_ == Fill::UNIVERSE) {
513✔
1733
        // if this is in the set of cells previously visited for this universe,
1734
        // move on
1735
        if (stack.visited(univ_idx, {model::cell_map[cell->id_], C_NONE}))
305!
1736
          continue;
×
1737

1738
        // if this cell contains the universe we're searching for, add it to the
1739
        // stack
1740
        if (cell->fill_ == univ_idx) {
305!
1741
          stack.push(univ_idx, {model::cell_map[cell->id_], C_NONE});
×
1742
          univ_idx = cell->universe_;
×
1743
        }
1744
      } else if (cell->type_ == Fill::LATTICE) {
208!
1745
        // retrieve the lattice and lattice universes
1746
        const auto& lattice = model::lattices[cell->fill_];
208✔
1747
        const auto& lattice_univs = lattice->universes_;
208✔
1748

1749
        // start search for universe
1750
        auto lat_it = lattice_univs.begin();
208✔
1751
        while (true) {
1752
          // find the next lattice cell with this universe
1753
          lat_it = std::find(lat_it, lattice_univs.end(), univ_idx);
352✔
1754
          if (lat_it == lattice_univs.end())
352✔
1755
            break;
96✔
1756

1757
          int lattice_idx = lat_it - lattice_univs.begin();
256✔
1758

1759
          // move iterator forward one to avoid finding the same entry
1760
          lat_it++;
256✔
1761
          if (stack.visited(
256✔
1762
                univ_idx, {model::cell_map[cell->id_], lattice_idx}))
256✔
1763
            continue;
144✔
1764

1765
          // add this cell and lattice index to the stack and exit loop
1766
          stack.push(univ_idx, {model::cell_map[cell->id_], lattice_idx});
112✔
1767
          univ_idx = cell->universe_;
112✔
1768
          break;
112✔
1769
        }
144✔
1770
      }
1771
      // if we've updated the universe, break
1772
      if (prev_univ_idx != univ_idx)
513✔
1773
        break;
112✔
1774
    } // end cell loop search for universe
1775

1776
    // if we're at the top of the geometry and the instance matches, we're done
1777
    if (univ_idx == model::root_universe &&
386!
1778
        stack.compute_instance(this->distribcell_index_) == instance)
193✔
1779
      break;
113✔
1780

1781
    // if there is no match on the original cell's universe, report an error
1782
    if (univ_idx == this->universe_) {
80!
1783
      fatal_error(
×
1784
        fmt::format("Could not find the parent cells for cell {}, instance {}.",
×
1785
          this->id_, instance));
×
1786
    }
1787

1788
    // if we don't find a suitable update, adjust the stack and continue
1789
    if (univ_idx == model::root_universe || univ_idx == prev_univ_idx) {
80!
1790
      stack.pop();
80✔
1791
      univ_idx = stack.empty() ? this->universe_ : stack.current_univ();
80!
1792
    }
1793

1794
  } // end while
80✔
1795

1796
  // reverse the stack so the highest cell comes first
1797
  std::reverse(stack.parent_cells().begin(), stack.parent_cells().end());
113✔
1798
  return stack.parent_cells();
226✔
1799
}
113✔
1800

1801
std::unordered_map<int32_t, vector<int32_t>> Cell::get_contained_cells(
161✔
1802
  int32_t instance, Position* hint) const
1803
{
1804
  std::unordered_map<int32_t, vector<int32_t>> contained_cells;
161✔
1805

1806
  // if this is a material-filled cell it has no contained cells
1807
  if (this->type_ == Fill::MATERIAL)
161✔
1808
    return contained_cells;
48✔
1809

1810
  // find the pathway through the geometry to this cell
1811
  vector<ParentCell> parent_cells;
113✔
1812

1813
  // if a positional hint is provided, attempt to do a fast lookup
1814
  // of the parent cells
1815
  parent_cells = hint ? find_parent_cells(instance, *hint)
226!
1816
                      : exhaustive_find_parent_cells(instance);
113✔
1817

1818
  // if this cell is filled w/ a material, it contains no other cells
1819
  if (type_ != Fill::MATERIAL) {
113!
1820
    this->get_contained_cells_inner(contained_cells, parent_cells);
113✔
1821
  }
1822

1823
  return contained_cells;
113✔
1824
}
113✔
1825

1826
//! Get all cells within this cell
1827
void Cell::get_contained_cells_inner(
87,763✔
1828
  std::unordered_map<int32_t, vector<int32_t>>& contained_cells,
1829
  vector<ParentCell>& parent_cells) const
1830
{
1831

1832
  // filled by material, determine instance based on parent cells
1833
  if (type_ == Fill::MATERIAL) {
87,763✔
1834
    int instance = 0;
87,138✔
1835
    if (this->distribcell_index_ >= 0) {
87,138!
1836
      for (auto& parent_cell : parent_cells) {
261,412✔
1837
        auto& cell = model::cells[parent_cell.cell_index];
174,274✔
1838
        if (cell->type_ == Fill::UNIVERSE) {
174,274✔
1839
          instance += cell->offset_[distribcell_index_];
85,538✔
1840
        } else if (cell->type_ == Fill::LATTICE) {
88,736!
1841
          auto& lattice = model::lattices[cell->fill_];
88,736✔
1842
          instance += lattice->offset(
177,472✔
1843
            this->distribcell_index_, parent_cell.lattice_index);
88,736✔
1844
        }
1845
      }
1846
    }
1847
    // add entry to contained cells
1848
    contained_cells[model::cell_map[id_]].push_back(instance);
87,138✔
1849
    // filled with universe, add the containing cell to the parent cells
1850
    // and recurse
1851
  } else if (type_ == Fill::UNIVERSE) {
625✔
1852
    parent_cells.push_back({model::cell_map[id_], -1});
529✔
1853
    auto& univ = model::universes[fill_];
529✔
1854
    for (auto cell_index : univ->cells_) {
3,171✔
1855
      auto& cell = model::cells[cell_index];
2,642✔
1856
      cell->get_contained_cells_inner(contained_cells, parent_cells);
2,642✔
1857
    }
1858
    parent_cells.pop_back();
529✔
1859
    // filled with a lattice, visit each universe in the lattice
1860
    // with a recursive call to collect the cell instances
1861
  } else if (type_ == Fill::LATTICE) {
96!
1862
    auto& lattice = model::lattices[fill_];
96✔
1863
    for (auto i = lattice->begin(); i != lattice->end(); ++i) {
84,768✔
1864
      auto& univ = model::universes[*i];
84,672✔
1865
      parent_cells.push_back({model::cell_map[id_], i.indx_});
84,672✔
1866
      for (auto cell_index : univ->cells_) {
169,680✔
1867
        auto& cell = model::cells[cell_index];
85,008✔
1868
        cell->get_contained_cells_inner(contained_cells, parent_cells);
85,008✔
1869
      }
1870
      parent_cells.pop_back();
84,672✔
1871
    }
1872
  }
1873
}
87,763✔
1874

1875
//! Return the index in the cells array of a cell with a given ID
1876
extern "C" int openmc_get_cell_index(int32_t id, int32_t* index)
490✔
1877
{
1878
  auto it = model::cell_map.find(id);
490✔
1879
  if (it != model::cell_map.end()) {
490✔
1880
    *index = it->second;
479✔
1881
    return 0;
479✔
1882
  } else {
1883
    set_errmsg("No cell exists with ID=" + std::to_string(id) + ".");
11✔
1884
    return OPENMC_E_INVALID_ID;
11✔
1885
  }
1886
}
1887

1888
//! Return the ID of a cell
1889
extern "C" int openmc_cell_get_id(int32_t index, int32_t* id)
600,974✔
1890
{
1891
  if (index >= 0 && index < model::cells.size()) {
600,974!
1892
    *id = model::cells[index]->id_;
600,974✔
1893
    return 0;
600,974✔
1894
  } else {
1895
    set_errmsg("Index in cells array is out of bounds.");
×
1896
    return OPENMC_E_OUT_OF_BOUNDS;
×
1897
  }
1898
}
1899

1900
//! Set the ID of a cell
1901
extern "C" int openmc_cell_set_id(int32_t index, int32_t id)
22✔
1902
{
1903
  if (index >= 0 && index < model::cells.size()) {
22!
1904
    model::cells[index]->id_ = id;
22✔
1905
    model::cell_map[id] = index;
22✔
1906
    return 0;
22✔
1907
  } else {
1908
    set_errmsg("Index in cells array is out of bounds.");
×
1909
    return OPENMC_E_OUT_OF_BOUNDS;
×
1910
  }
1911
}
1912

1913
//! Return the translation vector of a cell
1914
extern "C" int openmc_cell_get_translation(int32_t index, double xyz[])
55✔
1915
{
1916
  if (index >= 0 && index < model::cells.size()) {
55!
1917
    auto& cell = model::cells[index];
55✔
1918
    xyz[0] = cell->translation_.x;
55✔
1919
    xyz[1] = cell->translation_.y;
55✔
1920
    xyz[2] = cell->translation_.z;
55✔
1921
    return 0;
55✔
1922
  } else {
1923
    set_errmsg("Index in cells array is out of bounds.");
×
1924
    return OPENMC_E_OUT_OF_BOUNDS;
×
1925
  }
1926
}
1927

1928
//! Set the translation vector of a cell
1929
extern "C" int openmc_cell_set_translation(int32_t index, const double xyz[])
33✔
1930
{
1931
  if (index >= 0 && index < model::cells.size()) {
33!
1932
    if (model::cells[index]->fill_ == C_NONE) {
33✔
1933
      set_errmsg(fmt::format("Cannot apply a translation to cell {}"
11✔
1934
                             " because it is not filled with another universe",
1935
        index));
1936
      return OPENMC_E_GEOMETRY;
11✔
1937
    }
1938
    model::cells[index]->translation_ = Position(xyz);
22✔
1939
    return 0;
22✔
1940
  } else {
1941
    set_errmsg("Index in cells array is out of bounds.");
×
1942
    return OPENMC_E_OUT_OF_BOUNDS;
×
1943
  }
1944
}
1945

1946
//! Return the rotation matrix of a cell
1947
extern "C" int openmc_cell_get_rotation(int32_t index, double rot[], size_t* n)
55✔
1948
{
1949
  if (index >= 0 && index < model::cells.size()) {
55!
1950
    auto& cell = model::cells[index];
55✔
1951
    *n = cell->rotation_.size();
55✔
1952
    std::memcpy(rot, cell->rotation_.data(), *n * sizeof(cell->rotation_[0]));
55✔
1953
    return 0;
55✔
1954
  } else {
1955
    set_errmsg("Index in cells array is out of bounds.");
×
1956
    return OPENMC_E_OUT_OF_BOUNDS;
×
1957
  }
1958
}
1959

1960
//! Set the flattened rotation matrix of a cell
1961
extern "C" int openmc_cell_set_rotation(
44✔
1962
  int32_t index, const double rot[], size_t rot_len)
1963
{
1964
  if (index >= 0 && index < model::cells.size()) {
44!
1965
    if (model::cells[index]->fill_ == C_NONE) {
44✔
1966
      set_errmsg(fmt::format("Cannot apply a rotation to cell {}"
11✔
1967
                             " because it is not filled with another universe",
1968
        index));
1969
      return OPENMC_E_GEOMETRY;
11✔
1970
    }
1971
    std::vector<double> vec_rot(rot, rot + rot_len);
33✔
1972
    model::cells[index]->set_rotation(vec_rot);
33✔
1973
    return 0;
33✔
1974
  } else {
33✔
1975
    set_errmsg("Index in cells array is out of bounds.");
×
1976
    return OPENMC_E_OUT_OF_BOUNDS;
×
1977
  }
1978
}
1979

1980
//! Get the number of instances of the requested cell
1981
extern "C" int openmc_cell_get_num_instances(
11✔
1982
  int32_t index, int32_t* num_instances)
1983
{
1984
  if (index < 0 || index >= model::cells.size()) {
11!
1985
    set_errmsg("Index in cells array is out of bounds.");
×
1986
    return OPENMC_E_OUT_OF_BOUNDS;
×
1987
  }
1988
  *num_instances = model::cells[index]->n_instances();
11✔
1989
  return 0;
11✔
1990
}
1991

1992
//! Extend the cells array by n elements
1993
extern "C" int openmc_extend_cells(
22✔
1994
  int32_t n, int32_t* index_start, int32_t* index_end)
1995
{
1996
  if (index_start)
22!
1997
    *index_start = model::cells.size();
22✔
1998
  if (index_end)
22!
1999
    *index_end = model::cells.size() + n - 1;
×
2000
  for (int32_t i = 0; i < n; i++) {
44✔
2001
    model::cells.push_back(make_unique<CSGCell>());
22✔
2002
  }
2003
  return 0;
22✔
2004
}
2005

2006
extern "C" int cells_size()
44✔
2007
{
2008
  return model::cells.size();
44✔
2009
}
2010

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

© 2025 Coveralls, Inc