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

openmc-dev / openmc / 21991279157

13 Feb 2026 02:53PM UTC coverage: 81.82% (-0.06%) from 81.875%
21991279157

Pull #3805

github

web-flow
Merge 0a7a80411 into bcb939520
Pull Request #3805: Remove xtensor and xtl Dependencies

17242 of 24268 branches covered (71.05%)

Branch coverage included in aggregate %.

977 of 1013 new or added lines in 39 files covered. (96.45%)

404 existing lines in 8 files now uncovered.

57420 of 66983 relevant lines covered (85.72%)

45458907.73 hits per line

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

70.36
/src/mesh.cpp
1
#include "openmc/mesh.h"
2
#include <algorithm> // for copy, equal, min, min_element
3
#include <cassert>
4
#include <cstdint>        // for uint64_t
5
#include <cstring>        // for memcpy
6
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
7
#include <cmath>          // for ceil
8
#include <cstddef>        // for size_t
9
#include <numeric>        // for accumulate
10
#include <string>
11

12
#ifdef _MSC_VER
13
#include <intrin.h> // for _InterlockedCompareExchange
14
#endif
15

16
#ifdef OPENMC_MPI
17
#include "mpi.h"
18
#endif
19

20
#include "openmc/tensor.h"
21
#include <fmt/core.h> // for fmt
22

23
#include "openmc/capi.h"
24
#include "openmc/constants.h"
25
#include "openmc/container_util.h"
26
#include "openmc/error.h"
27
#include "openmc/file_utils.h"
28
#include "openmc/geometry.h"
29
#include "openmc/hdf5_interface.h"
30
#include "openmc/material.h"
31
#include "openmc/memory.h"
32
#include "openmc/message_passing.h"
33
#include "openmc/openmp_interface.h"
34
#include "openmc/output.h"
35
#include "openmc/particle_data.h"
36
#include "openmc/plot.h"
37
#include "openmc/random_dist.h"
38
#include "openmc/search.h"
39
#include "openmc/settings.h"
40
#include "openmc/string_utils.h"
41
#include "openmc/tallies/filter.h"
42
#include "openmc/tallies/tally.h"
43
#include "openmc/timer.h"
44
#include "openmc/volume_calc.h"
45
#include "openmc/xml_interface.h"
46

47
#ifdef OPENMC_LIBMESH_ENABLED
48
#include "libmesh/mesh_modification.h"
49
#include "libmesh/mesh_tools.h"
50
#include "libmesh/numeric_vector.h"
51
#include "libmesh/replicated_mesh.h"
52
#endif
53

54
#ifdef OPENMC_DAGMC_ENABLED
55
#include "moab/FileOptions.hpp"
56
#endif
57

58
namespace openmc {
59

60
//==============================================================================
61
// Global variables
62
//==============================================================================
63

64
#ifdef OPENMC_LIBMESH_ENABLED
65
const bool LIBMESH_ENABLED = true;
66
#else
67
const bool LIBMESH_ENABLED = false;
68
#endif
69

70
// Value used to indicate an empty slot in the hash table. We use -2 because
71
// the value -1 is used to indicate a void material.
72
constexpr int32_t EMPTY = -2;
73

74
namespace model {
75

76
std::unordered_map<int32_t, int32_t> mesh_map;
77
vector<unique_ptr<Mesh>> meshes;
78

79
} // namespace model
80

81
#ifdef OPENMC_LIBMESH_ENABLED
82
namespace settings {
83
unique_ptr<libMesh::LibMeshInit> libmesh_init;
84
const libMesh::Parallel::Communicator* libmesh_comm {nullptr};
85
} // namespace settings
86
#endif
87

88
//==============================================================================
89
// Helper functions
90
//==============================================================================
91

92
//! Update an intersection point if the given candidate is closer.
93
//
94
//! The first 6 arguments are coordinates for the starting point of a particle
95
//! and its intersection with a mesh surface.  If the distance between these
96
//! two points is shorter than the given `min_distance`, then the `r` argument
97
//! will be updated to match the intersection point, and `min_distance` will
98
//! also be updated.
99

100
inline bool check_intersection_point(double x1, double x0, double y1, double y0,
101
  double z1, double z0, Position& r, double& min_distance)
102
{
103
  double dist =
104
    std::pow(x1 - x0, 2) + std::pow(y1 - y0, 2) + std::pow(z1 - z0, 2);
105
  if (dist < min_distance) {
106
    r.x = x1;
107
    r.y = y1;
108
    r.z = z1;
109
    min_distance = dist;
110
    return true;
111
  }
112
  return false;
113
}
114

115
//! Atomic compare-and-swap for signed 32-bit integer
116
//
117
//! \param[in,out] ptr Pointer to value to update
118
//! \param[in,out] expected Value to compare to
119
//! \param[in] desired If comparison is successful, value to update to
120
//! \return True if the comparison was successful and the value was updated
121
inline bool atomic_cas_int32(int32_t* ptr, int32_t& expected, int32_t desired)
1,385✔
122
{
123
#if defined(__GNUC__) || defined(__clang__)
124
  // For gcc/clang, use the __atomic_compare_exchange_n intrinsic
125
  return __atomic_compare_exchange_n(
1,385✔
126
    ptr, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
1,385✔
127

128
#elif defined(_MSC_VER)
129
  // For MSVC, use the _InterlockedCompareExchange intrinsic
130
  int32_t old_val =
131
    _InterlockedCompareExchange(reinterpret_cast<volatile long*>(ptr),
132
      static_cast<long>(desired), static_cast<long>(expected));
133
  return (old_val == expected);
134

135
#else
136
#error "No compare-and-swap implementation available for this compiler."
137
#endif
138
}
139

140
// Helper function equivalent to std::bit_cast in C++20
141
template<typename To, typename From>
142
inline To bit_cast_value(const From& value)
33,859,154✔
143
{
144
  To out;
145
  std::memcpy(&out, &value, sizeof(To));
33,859,154✔
146
  return out;
33,859,154✔
147
}
148

149
inline void atomic_update_double(double* ptr, double value, bool is_min)
33,828,144✔
150
{
151
#if defined(__GNUC__) || defined(__clang__)
152
  using may_alias_uint64_t [[gnu::may_alias]] = uint64_t;
153
  auto* bits_ptr = reinterpret_cast<may_alias_uint64_t*>(ptr);
33,828,144✔
154
  uint64_t current_bits = __atomic_load_n(bits_ptr, __ATOMIC_SEQ_CST);
33,828,144✔
155
  double current = bit_cast_value<double>(current_bits);
33,828,144✔
156
  while (is_min ? (value < current) : (value > current)) {
33,828,192✔
157
    uint64_t desired_bits = bit_cast_value<uint64_t>(value);
30,962✔
158
    uint64_t expected_bits = current_bits;
30,962✔
159
    if (__atomic_compare_exchange_n(bits_ptr, &expected_bits, desired_bits,
30,962✔
160
          false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
161
      return;
30,914✔
162
    }
163
    current_bits = expected_bits;
48✔
164
    current = bit_cast_value<double>(current_bits);
48✔
165
  }
166

167
#elif defined(_MSC_VER)
168
  auto* bits_ptr = reinterpret_cast<volatile long long*>(ptr);
169
  long long current_bits = *bits_ptr;
170
  double current = bit_cast_value<double>(current_bits);
171
  while (is_min ? (value < current) : (value > current)) {
172
    long long desired_bits = bit_cast_value<long long>(value);
173
    long long old_bits =
174
      _InterlockedCompareExchange64(bits_ptr, desired_bits, current_bits);
175
    if (old_bits == current_bits) {
176
      return;
177
    }
178
    current_bits = old_bits;
179
    current = bit_cast_value<double>(current_bits);
180
  }
181

182
#else
183
#error "No compare-and-swap implementation available for this compiler."
184
#endif
185
}
186

187
inline void atomic_max_double(double* ptr, double value)
16,914,072✔
188
{
189
  atomic_update_double(ptr, value, false);
16,914,072✔
190
}
16,914,072✔
191

192
inline void atomic_min_double(double* ptr, double value)
16,914,072✔
193
{
194
  atomic_update_double(ptr, value, true);
16,914,072✔
195
}
16,914,072✔
196

197
namespace detail {
198

199
//==============================================================================
200
// MaterialVolumes implementation
201
//==============================================================================
202

203
void MaterialVolumes::add_volume(
8,050,194✔
204
  int index_elem, int index_material, double volume, const BoundingBox* bbox)
205
{
206
  // This method handles adding elements to the materials hash table,
207
  // implementing open addressing with linear probing. Consistency across
208
  // multiple threads is handled by with an atomic compare-and-swap operation.
209
  // Ideally, we would use #pragma omp atomic compare, but it was introduced in
210
  // OpenMP 5.1 and is not widely supported yet.
211

212
  // Loop for linear probing
213
  for (int attempt = 0; attempt < table_size_; ++attempt) {
8,090,754!
214
    // Determine slot to check, making sure it is positive
215
    int slot = (index_material + attempt) % table_size_;
8,090,754✔
216
    if (slot < 0)
8,090,754✔
217
      slot += table_size_;
5,341,712✔
218
    int32_t* slot_ptr = &this->materials(index_elem, slot);
8,090,754✔
219

220
    // Non-atomic read of current material
221
    int32_t current_val = *slot_ptr;
8,090,754✔
222

223
    // Found the desired material; accumulate volume and bbox
224
    if (current_val == index_material) {
8,090,754✔
225
#pragma omp atomic
4,858,523✔
226
      this->volumes(index_elem, slot) += volume;
8,048,809✔
227
      if (bbox) {
8,048,809✔
228
        atomic_min_double(&this->bboxes(index_elem, slot, 0), bbox->min.x);
5,637,910✔
229
        atomic_min_double(&this->bboxes(index_elem, slot, 1), bbox->min.y);
5,637,910✔
230
        atomic_min_double(&this->bboxes(index_elem, slot, 2), bbox->min.z);
5,637,910✔
231
        atomic_max_double(&this->bboxes(index_elem, slot, 3), bbox->max.x);
5,637,910✔
232
        atomic_max_double(&this->bboxes(index_elem, slot, 4), bbox->max.y);
5,637,910✔
233
        atomic_max_double(&this->bboxes(index_elem, slot, 5), bbox->max.z);
5,637,910✔
234
      }
235
      return;
8,048,809✔
236
    }
237

238
    // Slot appears to be empty; attempt to claim
239
    if (current_val == EMPTY) {
41,945✔
240
      // Attempt compare-and-swap from EMPTY to index_material
241
      int32_t expected_val = EMPTY;
1,385✔
242
      bool claimed_slot =
243
        atomic_cas_int32(slot_ptr, expected_val, index_material);
1,385✔
244

245
      // If we claimed the slot or another thread claimed it but the same
246
      // material was inserted, proceed to accumulate
247
      if (claimed_slot || (expected_val == index_material)) {
1,385!
248
#pragma omp atomic
840✔
249
        this->volumes(index_elem, slot) += volume;
1,385✔
250
        if (bbox) {
1,385✔
251
          atomic_min_double(&this->bboxes(index_elem, slot, 0), bbox->min.x);
114✔
252
          atomic_min_double(&this->bboxes(index_elem, slot, 1), bbox->min.y);
114✔
253
          atomic_min_double(&this->bboxes(index_elem, slot, 2), bbox->min.z);
114✔
254
          atomic_max_double(&this->bboxes(index_elem, slot, 3), bbox->max.x);
114✔
255
          atomic_max_double(&this->bboxes(index_elem, slot, 4), bbox->max.y);
114✔
256
          atomic_max_double(&this->bboxes(index_elem, slot, 5), bbox->max.z);
114✔
257
        }
258
        return;
1,385✔
259
      }
260
    }
261
  }
262

263
  // If table is full, set a flag that can be checked later
264
  table_full_ = true;
×
265
}
266

267
void MaterialVolumes::add_volume_unsafe(
×
268
  int index_elem, int index_material, double volume, const BoundingBox* bbox)
269
{
270
  // Linear probe
271
  for (int attempt = 0; attempt < table_size_; ++attempt) {
×
272
    // Determine slot to check, making sure it is positive
273
    int slot = (index_material + attempt) % table_size_;
×
274
    if (slot < 0)
×
275
      slot += table_size_;
×
276

277
    // Read current material
278
    int32_t current_val = this->materials(index_elem, slot);
×
279

280
    // Found the desired material; accumulate volume and bbox
281
    if (current_val == index_material) {
×
282
      this->volumes(index_elem, slot) += volume;
×
283
      if (bbox) {
×
284
        this->bboxes(index_elem, slot, 0) =
×
285
          std::min(this->bboxes(index_elem, slot, 0), bbox->min.x);
×
286
        this->bboxes(index_elem, slot, 1) =
×
287
          std::min(this->bboxes(index_elem, slot, 1), bbox->min.y);
×
288
        this->bboxes(index_elem, slot, 2) =
×
289
          std::min(this->bboxes(index_elem, slot, 2), bbox->min.z);
×
290
        this->bboxes(index_elem, slot, 3) =
×
291
          std::max(this->bboxes(index_elem, slot, 3), bbox->max.x);
×
292
        this->bboxes(index_elem, slot, 4) =
×
293
          std::max(this->bboxes(index_elem, slot, 4), bbox->max.y);
×
294
        this->bboxes(index_elem, slot, 5) =
×
295
          std::max(this->bboxes(index_elem, slot, 5), bbox->max.z);
×
296
      }
297
      return;
×
298
    }
299

300
    // Claim empty slot
301
    if (current_val == EMPTY) {
×
302
      this->materials(index_elem, slot) = index_material;
×
303
      this->volumes(index_elem, slot) += volume;
×
304
      if (bbox) {
×
305
        this->bboxes(index_elem, slot, 0) =
×
306
          std::min(this->bboxes(index_elem, slot, 0), bbox->min.x);
×
307
        this->bboxes(index_elem, slot, 1) =
×
308
          std::min(this->bboxes(index_elem, slot, 1), bbox->min.y);
×
309
        this->bboxes(index_elem, slot, 2) =
×
310
          std::min(this->bboxes(index_elem, slot, 2), bbox->min.z);
×
311
        this->bboxes(index_elem, slot, 3) =
×
312
          std::max(this->bboxes(index_elem, slot, 3), bbox->max.x);
×
313
        this->bboxes(index_elem, slot, 4) =
×
314
          std::max(this->bboxes(index_elem, slot, 4), bbox->max.y);
×
315
        this->bboxes(index_elem, slot, 5) =
×
316
          std::max(this->bboxes(index_elem, slot, 5), bbox->max.z);
×
317
      }
318
      return;
×
319
    }
320
  }
321

322
  // If table is full, set a flag that can be checked later
323
  table_full_ = true;
×
324
}
325

326
} // namespace detail
327

328
//==============================================================================
329
// Mesh implementation
330
//==============================================================================
331

332
template<typename T>
333
const std::unique_ptr<Mesh>& Mesh::create(
2,717✔
334
  T dataset, const std::string& mesh_type, const std::string& mesh_library)
335
{
336
  // Determine mesh type. Add to model vector and map
337
  if (mesh_type == RegularMesh::mesh_type) {
2,717✔
338
    model::meshes.push_back(make_unique<RegularMesh>(dataset));
1,910✔
339
  } else if (mesh_type == RectilinearMesh::mesh_type) {
807✔
340
    model::meshes.push_back(make_unique<RectilinearMesh>(dataset));
102✔
341
  } else if (mesh_type == CylindricalMesh::mesh_type) {
705✔
342
    model::meshes.push_back(make_unique<CylindricalMesh>(dataset));
354✔
343
  } else if (mesh_type == SphericalMesh::mesh_type) {
351✔
344
    model::meshes.push_back(make_unique<SphericalMesh>(dataset));
304✔
345
#ifdef OPENMC_DAGMC_ENABLED
346
  } else if (mesh_type == UnstructuredMesh::mesh_type &&
48!
347
             mesh_library == MOABMesh::mesh_lib_type) {
24✔
348
    model::meshes.push_back(make_unique<MOABMesh>(dataset));
24✔
349
#endif
350
#ifdef OPENMC_LIBMESH_ENABLED
351
  } else if (mesh_type == UnstructuredMesh::mesh_type &&
46!
352
             mesh_library == LibMesh::mesh_lib_type) {
23✔
353
    model::meshes.push_back(make_unique<LibMesh>(dataset));
23✔
354
#endif
355
  } else if (mesh_type == UnstructuredMesh::mesh_type) {
×
356
    fatal_error("Unstructured mesh support is not enabled or the mesh "
×
357
                "library is invalid.");
358
  } else {
359
    fatal_error(fmt::format("Invalid mesh type: {}", mesh_type));
×
360
  }
361

362
  // Map ID to position in vector
363
  model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
2,717✔
364

365
  return model::meshes.back();
2,717✔
366
}
367

368
Mesh::Mesh(pugi::xml_node node)
2,757✔
369
{
370
  // Read mesh id
371
  id_ = std::stoi(get_node_value(node, "id"));
2,757✔
372
  if (check_for_node(node, "name"))
2,757✔
373
    name_ = get_node_value(node, "name");
14✔
374
}
2,757✔
375

376
Mesh::Mesh(hid_t group)
40✔
377
{
378
  // Read mesh ID
379
  read_attribute(group, "id", id_);
40✔
380

381
  // Read mesh name
382
  if (object_exists(group, "name")) {
40!
383
    read_dataset(group, "name", name_);
×
384
  }
385
}
40✔
386

387
void Mesh::set_id(int32_t id)
21✔
388
{
389
  assert(id >= 0 || id == C_NONE);
17!
390

391
  // Clear entry in mesh map in case one was already assigned
392
  if (id_ != C_NONE) {
21✔
393
    model::mesh_map.erase(id_);
20✔
394
    id_ = C_NONE;
20✔
395
  }
396

397
  // Ensure no other mesh has the same ID
398
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
21!
399
    throw std::runtime_error {
×
400
      fmt::format("Two meshes have the same ID: {}", id)};
×
401
  }
402

403
  // If no ID is specified, auto-assign the next ID in the sequence
404
  if (id == C_NONE) {
21✔
405
    id = 0;
1✔
406
    for (const auto& m : model::meshes) {
3✔
407
      id = std::max(id, m->id_);
2✔
408
    }
409
    ++id;
1✔
410
  }
411

412
  // Update ID and entry in the mesh map
413
  id_ = id;
21✔
414

415
  // find the index of this mesh in the model::meshes vector
416
  // (search in reverse because this mesh was likely just added to the vector)
417
  auto it = std::find_if(model::meshes.rbegin(), model::meshes.rend(),
21✔
418
    [this](const std::unique_ptr<Mesh>& mesh) { return mesh.get() == this; });
31✔
419

420
  model::mesh_map[id] = std::distance(model::meshes.begin(), it.base()) - 1;
21✔
421
}
21✔
422

423
vector<double> Mesh::volumes() const
226✔
424
{
425
  vector<double> volumes(n_bins());
226✔
426
  for (int i = 0; i < n_bins(); i++) {
1,015,252✔
427
    volumes[i] = this->volume(i);
1,015,026✔
428
  }
429
  return volumes;
226✔
430
}
×
431

432
void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
×
433
  int32_t* materials, double* volumes) const
434
{
435
  this->material_volumes(nx, ny, nz, table_size, materials, volumes, nullptr);
×
436
}
×
437

438
void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
170✔
439
  int32_t* materials, double* volumes, double* bboxes) const
440
{
441
  if (mpi::master) {
170!
442
    header("MESH MATERIAL VOLUMES CALCULATION", 7);
170✔
443
  }
444
  write_message(7, "Number of mesh elements = {}", n_bins());
170✔
445
  write_message(7, "Number of rays (x) = {}", nx);
170✔
446
  write_message(7, "Number of rays (y) = {}", ny);
170✔
447
  write_message(7, "Number of rays (z) = {}", nz);
170✔
448
  int64_t n_total = static_cast<int64_t>(nx) * ny +
170✔
449
                    static_cast<int64_t>(ny) * nz +
170✔
450
                    static_cast<int64_t>(nx) * nz;
170✔
451
  write_message(7, "Total number of rays = {}", n_total);
170✔
452
  write_message(7, "Table size per mesh element = {}", table_size);
170✔
453

454
  Timer timer;
170✔
455
  timer.start();
170✔
456

457
  // Create object for keeping track of materials/volumes
458
  detail::MaterialVolumes result(materials, volumes, bboxes, table_size);
170✔
459
  bool compute_bboxes = bboxes != nullptr;
170✔
460

461
  // Determine bounding box
462
  auto bbox = this->bounding_box();
170✔
463

464
  std::array<int, 3> n_rays = {nx, ny, nz};
170✔
465

466
  // Determine effective width of rays
467
  Position width = bbox.max - bbox.min;
170✔
468
  width.x = (nx > 0) ? width.x / nx : 0.0;
170✔
469
  width.y = (ny > 0) ? width.y / ny : 0.0;
170✔
470
  width.z = (nz > 0) ? width.z / nz : 0.0;
170✔
471

472
  // Set flag for mesh being contained within model
473
  bool out_of_model = false;
170✔
474

475
#pragma omp parallel
102✔
476
  {
477
    // Preallocate vector for mesh indices and length fractions and particle
478
    vector<int> bins;
68✔
479
    vector<double> length_fractions;
68✔
480
    Particle p;
68✔
481

482
    SourceSite site;
68✔
483
    site.E = 1.0;
68✔
484
    site.particle = ParticleType::neutron();
68✔
485

486
    for (int axis = 0; axis < 3; ++axis) {
272✔
487
      // Set starting position and direction
488
      site.r = {0.0, 0.0, 0.0};
204✔
489
      site.r[axis] = bbox.min[axis];
204✔
490
      site.u = {0.0, 0.0, 0.0};
204✔
491
      site.u[axis] = 1.0;
204✔
492

493
      // Determine width of rays and number of rays in other directions
494
      int ax1 = (axis + 1) % 3;
204✔
495
      int ax2 = (axis + 2) % 3;
204✔
496
      double min1 = bbox.min[ax1];
204✔
497
      double min2 = bbox.min[ax2];
204✔
498
      double d1 = width[ax1];
204✔
499
      double d2 = width[ax2];
204✔
500
      int n1 = n_rays[ax1];
204✔
501
      int n2 = n_rays[ax2];
204✔
502
      if (n1 == 0 || n2 == 0) {
204✔
503
        continue;
48✔
504
      }
505

506
      // Divide rays in first direction over MPI processes by computing starting
507
      // and ending indices
508
      int min_work = n1 / mpi::n_procs;
156✔
509
      int remainder = n1 % mpi::n_procs;
156✔
510
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
156!
511
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
156✔
512
      int i1_end = i1_start + n1_local;
156✔
513

514
      // Loop over rays on face of bounding box
515
#pragma omp for collapse(2)
516
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
12,640✔
517
        for (int i2 = 0; i2 < n2; ++i2) {
2,382,672✔
518
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
2,370,188✔
519
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
2,370,188✔
520

521
          p.from_source(&site);
2,370,188✔
522

523
          // Determine particle's location
524
          if (!exhaustive_find_cell(p)) {
2,370,188✔
525
            out_of_model = true;
31,944✔
526
            continue;
31,944✔
527
          }
528

529
          // Set birth cell attribute
530
          if (p.cell_born() == C_NONE)
2,338,244!
531
            p.cell_born() = p.lowest_coord().cell();
2,338,244✔
532

533
          // Initialize last cells from current cell
534
          for (int j = 0; j < p.n_coord(); ++j) {
4,676,488✔
535
            p.cell_last(j) = p.coord(j).cell();
2,338,244✔
536
          }
537
          p.n_coord_last() = p.n_coord();
2,338,244✔
538

539
          while (true) {
540
            // Ray trace from r_start to r_end
541
            Position r0 = p.r();
3,027,196✔
542
            double max_distance = bbox.max[axis] - r0[axis];
3,027,196✔
543

544
            // Find the distance to the nearest boundary
545
            BoundaryInfo boundary = distance_to_boundary(p);
3,027,196✔
546

547
            // Advance particle forward
548
            double distance = std::min(boundary.distance(), max_distance);
3,027,196✔
549
            p.move_distance(distance);
3,027,196✔
550

551
            // Determine what mesh elements were crossed by particle
552
            bins.clear();
3,027,196✔
553
            length_fractions.clear();
3,027,196✔
554
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
3,027,196✔
555

556
            // Add volumes to any mesh elements that were crossed
557
            int i_material = p.material();
3,027,196✔
558
            if (i_material != C_NONE) {
3,027,196✔
559
              i_material = model::materials[i_material]->id();
890,932✔
560
            }
561
            double cumulative_frac = 0.0;
3,027,196✔
562
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
6,229,312✔
563
              int mesh_index = bins[i_bin];
3,202,116✔
564
              double length = distance * length_fractions[i_bin];
3,202,116✔
565
              double volume = length * d1 * d2;
3,202,116✔
566

567
              if (compute_bboxes) {
3,202,116✔
568
                double axis_start = r0[axis] + distance * cumulative_frac;
2,237,248✔
569
                double axis_end = axis_start + length;
2,237,248✔
570
                cumulative_frac += length_fractions[i_bin];
2,237,248✔
571

572
                Position contrib_min = site.r;
2,237,248✔
573
                Position contrib_max = site.r;
2,237,248✔
574

575
                contrib_min[ax1] = site.r[ax1] - 0.5 * d1;
2,237,248✔
576
                contrib_max[ax1] = site.r[ax1] + 0.5 * d1;
2,237,248✔
577
                contrib_min[ax2] = site.r[ax2] - 0.5 * d2;
2,237,248✔
578
                contrib_max[ax2] = site.r[ax2] + 0.5 * d2;
2,237,248✔
579
                contrib_min[axis] = std::min(axis_start, axis_end);
2,237,248✔
580
                contrib_max[axis] = std::max(axis_start, axis_end);
2,237,248✔
581

582
                BoundingBox contrib_bbox {contrib_min, contrib_max};
2,237,248✔
583
                contrib_bbox &= bbox;
2,237,248✔
584

585
                result.add_volume(
2,237,248✔
586
                  mesh_index, i_material, volume, &contrib_bbox);
587
              } else {
588
                // Add volume to result
589
                result.add_volume(mesh_index, i_material, volume);
964,868✔
590
              }
591
            }
592

593
            if (distance == max_distance)
3,027,196✔
594
              break;
2,338,244✔
595

596
            // cross next geometric surface
597
            for (int j = 0; j < p.n_coord(); ++j) {
1,377,904✔
598
              p.cell_last(j) = p.coord(j).cell();
688,952✔
599
            }
600
            p.n_coord_last() = p.n_coord();
688,952✔
601

602
            // Set surface that particle is on and adjust coordinate levels
603
            p.surface() = boundary.surface();
688,952✔
604
            p.n_coord() = boundary.coord_level();
688,952✔
605

606
            if (boundary.lattice_translation()[0] != 0 ||
688,952✔
607
                boundary.lattice_translation()[1] != 0 ||
1,377,904!
608
                boundary.lattice_translation()[2] != 0) {
688,952!
609
              // Particle crosses lattice boundary
610
              cross_lattice(p, boundary);
×
611
            } else {
612
              // Particle crosses surface
613
              const auto& surf {model::surfaces[p.surface_index()].get()};
688,952✔
614
              p.cross_surface(*surf);
688,952✔
615
            }
616
          }
688,952✔
617
        }
618
      }
619
    }
620
  }
68✔
621

622
  // Check for errors
623
  if (out_of_model) {
170✔
624
    throw std::runtime_error("Mesh not fully contained in geometry.");
10✔
625
  } else if (result.table_full()) {
160!
626
    throw std::runtime_error("Maximum number of materials for mesh material "
×
627
                             "volume calculation insufficient.");
×
628
  }
629

630
  // Compute time for raytracing
631
  double t_raytrace = timer.elapsed();
160✔
632

633
#ifdef OPENMC_MPI
634
  // Combine results from multiple MPI processes
635
  if (mpi::n_procs > 1) {
64!
636
    int total = this->n_bins() * table_size;
×
637
    int total_bbox = total * 6;
638
    if (mpi::master) {
×
639
      // Allocate temporary buffer for receiving data
640
      vector<int32_t> mats(total);
×
641
      vector<double> vols(total);
×
642
      vector<double> recv_bboxes;
643
      if (compute_bboxes) {
×
644
        recv_bboxes.resize(total_bbox);
×
645
      }
646

647
      for (int i = 1; i < mpi::n_procs; ++i) {
×
648
        // Receive material indices and volumes from process i
649
        MPI_Recv(mats.data(), total, MPI_INT32_T, i, i, mpi::intracomm,
×
650
          MPI_STATUS_IGNORE);
651
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
×
652
          MPI_STATUS_IGNORE);
653
        if (compute_bboxes) {
×
654
          MPI_Recv(recv_bboxes.data(), total_bbox, MPI_DOUBLE, i, i,
×
655
            mpi::intracomm, MPI_STATUS_IGNORE);
656
        }
657

658
        // Combine with existing results; we can call thread unsafe version of
659
        // add_volume because each thread is operating on a different element
660
#pragma omp for
661
        for (int index_elem = 0; index_elem < n_bins(); ++index_elem) {
×
662
          for (int k = 0; k < table_size; ++k) {
×
663
            int index = index_elem * table_size + k;
664
            if (mats[index] != EMPTY) {
×
665
              if (compute_bboxes) {
×
666
                int bbox_index = index * 6;
667
                BoundingBox slot_bbox {
668
                  {recv_bboxes[bbox_index + 0], recv_bboxes[bbox_index + 1],
669
                    recv_bboxes[bbox_index + 2]},
670
                  {recv_bboxes[bbox_index + 3], recv_bboxes[bbox_index + 4],
671
                    recv_bboxes[bbox_index + 5]}};
672
                result.add_volume_unsafe(
×
673
                  index_elem, mats[index], vols[index], &slot_bbox);
674
              } else {
675
                result.add_volume_unsafe(index_elem, mats[index], vols[index]);
×
676
              }
677
            }
678
          }
679
        }
680
      }
681
    } else {
682
      // Send material indices and volumes to process 0
683
      MPI_Send(materials, total, MPI_INT32_T, 0, mpi::rank, mpi::intracomm);
×
684
      MPI_Send(volumes, total, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
×
685
      if (compute_bboxes) {
×
686
        MPI_Send(bboxes, total_bbox, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
×
687
      }
688
    }
689
  }
690

691
  // Report time for MPI communication
692
  double t_mpi = timer.elapsed() - t_raytrace;
64✔
693
#else
694
  double t_mpi = 0.0;
96✔
695
#endif
696

697
  // Normalize based on known volumes of elements
698
  for (int i = 0; i < this->n_bins(); ++i) {
970✔
699
    // Estimated total volume in element i
700
    double volume = 0.0;
810✔
701
    for (int j = 0; j < table_size; ++j) {
7,410✔
702
      volume += result.volumes(i, j);
6,600✔
703
    }
704
    // Renormalize volumes based on known volume of element i
705
    double norm = this->volume(i) / volume;
810✔
706
    for (int j = 0; j < table_size; ++j) {
7,410✔
707
      result.volumes(i, j) *= norm;
6,600✔
708
    }
709
  }
710

711
  // Get total time and normalization time
712
  timer.stop();
160✔
713
  double t_total = timer.elapsed();
160✔
714
  double t_norm = t_total - t_raytrace - t_mpi;
160✔
715

716
  // Show timing statistics
717
  if (settings::verbosity < 7 || !mpi::master)
160!
718
    return;
40✔
719
  header("Timing Statistics", 7);
120✔
720
  fmt::print(" Total time elapsed            = {:.4e} seconds\n", t_total);
120✔
721
  fmt::print("   Ray tracing                 = {:.4e} seconds\n", t_raytrace);
120✔
722
  fmt::print("   MPI communication           = {:.4e} seconds\n", t_mpi);
120✔
723
  fmt::print("   Normalization               = {:.4e} seconds\n", t_norm);
96✔
724
  fmt::print(" Calculation rate              = {:.4e} rays/seconds\n",
96✔
725
    n_total / t_raytrace);
120✔
726
  fmt::print(" Calculation rate (per thread) = {:.4e} rays/seconds\n",
96✔
727
    n_total / (t_raytrace * mpi::n_procs * num_threads()));
120✔
728
  std::fflush(stdout);
120✔
729
}
730

731
void Mesh::to_hdf5(hid_t group) const
2,646✔
732
{
733
  // Create group for mesh
734
  std::string group_name = fmt::format("mesh {}", id_);
4,746✔
735
  hid_t mesh_group = create_group(group, group_name.c_str());
2,646✔
736

737
  // Write mesh type
738
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,646✔
739

740
  // Write mesh ID
741
  write_attribute(mesh_group, "id", id_);
2,646✔
742

743
  // Write mesh name
744
  write_dataset(mesh_group, "name", name_);
2,646✔
745

746
  // Write mesh data
747
  this->to_hdf5_inner(mesh_group);
2,646✔
748

749
  // Close group
750
  close_group(mesh_group);
2,646✔
751
}
2,646✔
752

753
//==============================================================================
754
// Structured Mesh implementation
755
//==============================================================================
756

757
std::string StructuredMesh::bin_label(int bin) const
4,665,610✔
758
{
759
  MeshIndex ijk = get_indices_from_bin(bin);
4,665,610✔
760

761
  if (n_dimension_ > 2) {
4,665,610✔
762
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
9,302,240✔
763
  } else if (n_dimension_ > 1) {
14,490✔
764
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
28,480✔
765
  } else {
766
    return fmt::format("Mesh Index ({})", ijk[0]);
500✔
767
  }
768
}
769

770
tensor::Tensor<int> StructuredMesh::get_shape_tensor() const
2,284✔
771
{
772
  return tensor::Tensor<int>(shape_.data(), static_cast<size_t>(n_dimension_));
2,284✔
773
}
774

775
Position StructuredMesh::sample_element(
1,273,380✔
776
  const MeshIndex& ijk, uint64_t* seed) const
777
{
778
  // lookup the lower/upper bounds for the mesh element
779
  double x_min = negative_grid_boundary(ijk, 0);
1,273,380✔
780
  double x_max = positive_grid_boundary(ijk, 0);
1,273,380✔
781

782
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,273,380!
783
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,273,380!
784

785
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,273,380!
786
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,273,380!
787

788
  return {x_min + (x_max - x_min) * prn(seed),
1,273,380✔
789
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,273,380✔
790
}
791

792
//==============================================================================
793
// Unstructured Mesh implementation
794
//==============================================================================
795

796
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
47✔
797
{
798
  n_dimension_ = 3;
47✔
799

800
  // check the mesh type
801
  if (check_for_node(node, "type")) {
47!
802
    auto temp = get_node_value(node, "type", true, true);
47!
803
    if (temp != mesh_type) {
47!
804
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
805
    }
806
  }
47✔
807

808
  // check if a length unit multiplier was specified
809
  if (check_for_node(node, "length_multiplier")) {
47!
810
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
811
  }
812

813
  // get the filename of the unstructured mesh to load
814
  if (check_for_node(node, "filename")) {
47!
815
    filename_ = get_node_value(node, "filename");
47!
816
    if (!file_exists(filename_)) {
47!
817
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
818
    }
819
  } else {
820
    fatal_error(fmt::format(
×
821
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
822
  }
823

824
  if (check_for_node(node, "options")) {
47!
825
    options_ = get_node_value(node, "options");
16!
826
  }
827

828
  // check if mesh tally data should be written with
829
  // statepoint files
830
  if (check_for_node(node, "output")) {
47!
831
    output_ = get_node_value_bool(node, "output");
×
832
  }
833
}
47✔
834

835
UnstructuredMesh::UnstructuredMesh(hid_t group) : Mesh(group)
×
836
{
837
  n_dimension_ = 3;
×
838

839
  // check the mesh type
840
  if (object_exists(group, "type")) {
×
841
    std::string temp;
×
842
    read_dataset(group, "type", temp);
×
843
    if (temp != mesh_type) {
×
844
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
845
    }
846
  }
×
847

848
  // check if a length unit multiplier was specified
849
  if (object_exists(group, "length_multiplier")) {
×
850
    read_dataset(group, "length_multiplier", length_multiplier_);
×
851
  }
852

853
  // get the filename of the unstructured mesh to load
854
  if (object_exists(group, "filename")) {
×
855
    read_dataset(group, "filename", filename_);
×
856
    if (!file_exists(filename_)) {
×
857
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
858
    }
859
  } else {
860
    fatal_error(fmt::format(
×
861
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
862
  }
863

864
  if (attribute_exists(group, "options")) {
×
865
    read_attribute(group, "options", options_);
×
866
  }
867

868
  // check if mesh tally data should be written with
869
  // statepoint files
870
  if (attribute_exists(group, "output")) {
×
871
    read_attribute(group, "output", output_);
×
872
  }
873
}
×
874

875
void UnstructuredMesh::determine_bounds()
25✔
876
{
877
  double xmin = INFTY;
25✔
878
  double ymin = INFTY;
25✔
879
  double zmin = INFTY;
25✔
880
  double xmax = -INFTY;
25✔
881
  double ymax = -INFTY;
25✔
882
  double zmax = -INFTY;
25✔
883
  int n = this->n_vertices();
25!
884
  for (int i = 0; i < n; ++i) {
55,951✔
885
    auto v = this->vertex(i);
55,926!
886
    xmin = std::min(v.x, xmin);
55,926✔
887
    ymin = std::min(v.y, ymin);
55,926✔
888
    zmin = std::min(v.z, zmin);
55,926✔
889
    xmax = std::max(v.x, xmax);
55,926✔
890
    ymax = std::max(v.y, ymax);
55,926✔
891
    zmax = std::max(v.z, zmax);
55,926✔
892
  }
893
  lower_left_ = {xmin, ymin, zmin};
25!
894
  upper_right_ = {xmax, ymax, zmax};
25!
895
}
25✔
896

897
Position UnstructuredMesh::sample_tet(
601,230✔
898
  std::array<Position, 4> coords, uint64_t* seed) const
899
{
900
  // Uniform distribution
901
  double s = prn(seed);
601,230✔
902
  double t = prn(seed);
601,230✔
903
  double u = prn(seed);
601,230✔
904

905
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
906
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
907
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
908
  if (s + t > 1) {
601,230✔
909
    s = 1.0 - s;
300,785✔
910
    t = 1.0 - t;
300,785✔
911
  }
912
  if (s + t + u > 1) {
601,230✔
913
    if (t + u > 1) {
400,769✔
914
      double old_t = t;
200,577✔
915
      t = 1.0 - u;
200,577✔
916
      u = 1.0 - s - old_t;
200,577✔
917
    } else if (t + u <= 1) {
200,192!
918
      double old_s = s;
200,192✔
919
      s = 1.0 - t - u;
200,192✔
920
      u = old_s + t + u - 1;
200,192✔
921
    }
922
  }
923
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
924
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
925
}
926

927
const std::string UnstructuredMesh::mesh_type = "unstructured";
928

929
std::string UnstructuredMesh::get_mesh_type() const
32✔
930
{
931
  return mesh_type;
32✔
932
}
933

934
void UnstructuredMesh::surface_bins_crossed(
×
935
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
936
{
937
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
938
}
939

940
std::string UnstructuredMesh::bin_label(int bin) const
205,736✔
941
{
942
  return fmt::format("Mesh Index ({})", bin);
205,736!
943
};
944

945
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
32✔
946
{
947
  write_dataset(mesh_group, "filename", filename_);
32!
948
  write_dataset(mesh_group, "library", this->library());
32!
949
  if (!options_.empty()) {
32✔
950
    write_attribute(mesh_group, "options", options_);
8!
951
  }
952

953
  if (length_multiplier_ > 0.0)
32!
954
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
955

956
  // write vertex coordinates
957
  tensor::Tensor<double> vertices(
958
    {static_cast<size_t>(this->n_vertices()), static_cast<size_t>(3)});
32!
959
  for (int i = 0; i < this->n_vertices(); i++) {
70,275!
960
    auto v = this->vertex(i);
70,243!
961
    vertices.slice(i) = {v.x, v.y, v.z};
70,243!
962
  }
963
  write_dataset(mesh_group, "vertices", vertices);
32!
964

965
  int num_elem_skipped = 0;
32✔
966

967
  // write element types and connectivity
968
  vector<double> volumes;
32✔
969
  tensor::Tensor<int> connectivity(
970
    {static_cast<size_t>(this->n_bins()), static_cast<size_t>(8)});
32!
971
  tensor::Tensor<int> elem_types(
972
    {static_cast<size_t>(this->n_bins()), static_cast<size_t>(1)});
32!
973
  for (int i = 0; i < this->n_bins(); i++) {
349,768!
974
    auto conn = this->connectivity(i);
349,736!
975

976
    volumes.emplace_back(this->volume(i));
349,736!
977

978
    // write linear tet element
979
    if (conn.size() == 4) {
349,736✔
980
      elem_types.slice(i) = static_cast<int>(ElementType::LINEAR_TET);
347,736!
981
      connectivity.slice(i) = {
347,736✔
982
        conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1};
695,472!
983
      // write linear hex element
984
    } else if (conn.size() == 8) {
2,000!
985
      elem_types.slice(i) = static_cast<int>(ElementType::LINEAR_HEX);
2,000!
986
      connectivity.slice(i) = {
2,000✔
987
        conn[0], conn[1], conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]};
4,000!
988
    } else {
989
      num_elem_skipped++;
×
NEW
990
      elem_types.slice(i) = static_cast<int>(ElementType::UNSUPPORTED);
×
NEW
991
      connectivity.slice(i) = -1;
×
992
    }
993
  }
349,736✔
994

995
  // warn users that some elements were skipped
996
  if (num_elem_skipped > 0) {
32!
997
    warning(fmt::format("The connectivity of {} elements "
×
998
                        "on mesh {} were not written "
999
                        "because they are not of type linear tet/hex.",
1000
      num_elem_skipped, this->id_));
×
1001
  }
1002

1003
  write_dataset(mesh_group, "volumes", volumes);
32!
1004
  write_dataset(mesh_group, "connectivity", connectivity);
32!
1005
  write_dataset(mesh_group, "element_types", elem_types);
32!
1006
}
32✔
1007

1008
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
1009
{
1010
  length_multiplier_ = length_multiplier;
23✔
1011
}
23✔
1012

1013
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
1014
{
1015
  auto conn = connectivity(bin);
120,000!
1016

1017
  if (conn.size() == 4)
120,000!
1018
    return ElementType::LINEAR_TET;
120,000✔
1019
  else if (conn.size() == 8)
×
1020
    return ElementType::LINEAR_HEX;
×
1021
  else
1022
    return ElementType::UNSUPPORTED;
×
1023
}
120,000✔
1024

1025
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,069,809,737✔
1026
  Position r, bool& in_mesh) const
1027
{
1028
  MeshIndex ijk;
1029
  in_mesh = true;
1,069,809,737✔
1030
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
1031
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
1032

1033
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
1034
      in_mesh = false;
93,489,972✔
1035
  }
1036
  return ijk;
1,069,809,737✔
1037
}
1038

1039
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,577,237,156✔
1040
{
1041
  switch (n_dimension_) {
1,577,237,156!
1042
  case 1:
800,550✔
1043
    return ijk[0] - 1;
800,550✔
1044
  case 2:
123,977,480✔
1045
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
123,977,480✔
1046
  case 3:
1,452,459,126✔
1047
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,452,459,126✔
1048
  default:
×
1049
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
1050
  }
1051
}
1052

1053
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
7,037,026✔
1054
{
1055
  MeshIndex ijk;
1056
  if (n_dimension_ == 1) {
7,037,026✔
1057
    ijk[0] = bin + 1;
250✔
1058
  } else if (n_dimension_ == 2) {
7,036,776✔
1059
    ijk[0] = bin % shape_[0] + 1;
14,240✔
1060
    ijk[1] = bin / shape_[0] + 1;
14,240✔
1061
  } else if (n_dimension_ == 3) {
7,022,536!
1062
    ijk[0] = bin % shape_[0] + 1;
7,022,536✔
1063
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
7,022,536✔
1064
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
7,022,536✔
1065
  }
1066
  return ijk;
7,037,026✔
1067
}
1068

1069
int StructuredMesh::get_bin(Position r) const
224,435,363✔
1070
{
1071
  // Determine indices
1072
  bool in_mesh;
1073
  MeshIndex ijk = get_indices(r, in_mesh);
224,435,363✔
1074
  if (!in_mesh)
224,435,363✔
1075
    return -1;
19,221,962✔
1076

1077
  // Convert indices to bin
1078
  return get_bin_from_indices(ijk);
205,213,401✔
1079
}
1080

1081
int StructuredMesh::n_bins() const
1,028,304✔
1082
{
1083
  return std::accumulate(
1,028,304✔
1084
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,056,608✔
1085
}
1086

1087
int StructuredMesh::n_surface_bins() const
340✔
1088
{
1089
  return 4 * n_dimension_ * n_bins();
340✔
1090
}
1091

NEW
1092
tensor::Tensor<double> StructuredMesh::count_sites(
×
1093
  const SourceSite* bank, int64_t length, bool* outside) const
1094
{
1095
  // Determine shape of array for counts
1096
  std::size_t m = this->n_bins();
×
1097
  vector<std::size_t> shape = {m};
×
1098

1099
  // Create array of zeros
NEW
1100
  tensor::Tensor<double> cnt(shape, 0.0);
×
1101
  bool outside_ = false;
×
1102

1103
  for (int64_t i = 0; i < length; i++) {
×
1104
    const auto& site = bank[i];
×
1105

1106
    // determine scoring bin for entropy mesh
1107
    int mesh_bin = get_bin(site.r);
×
1108

1109
    // if outside mesh, skip particle
1110
    if (mesh_bin < 0) {
×
1111
      outside_ = true;
×
1112
      continue;
×
1113
    }
1114

1115
    // Add to appropriate bin
1116
    cnt(mesh_bin) += site.wgt;
×
1117
  }
1118

1119
  // Create reduced count data
NEW
1120
  tensor::Tensor<double> counts(shape, 0.0);
×
1121
  int total = cnt.size();
×
1122

1123
#ifdef OPENMC_MPI
1124
  // collect values from all processors
1125
  MPI_Reduce(
×
1126
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1127

1128
  // Check if there were sites outside the mesh for any processor
1129
  if (outside) {
×
1130
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
×
1131
  }
1132
#else
1133
  std::copy(cnt.data(), cnt.data() + total, counts.data());
×
1134
  if (outside)
×
1135
    *outside = outside_;
1136
#endif
1137

UNCOV
1138
  return counts;
×
1139
}
×
1140

1141
// raytrace through the mesh. The template class T will do the tallying.
1142
// A modern optimizing compiler can recognize the noop method of T and
1143
// eliminate that call entirely.
1144
template<class T>
1145
void StructuredMesh::raytrace_mesh(
849,842,495✔
1146
  Position r0, Position r1, const Direction& u, T tally) const
1147
{
1148
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
1149
  // enable/disable tally calls for now, T template type needs to provide both
1150
  // surface and track methods, which might be empty. modern optimizing
1151
  // compilers will (hopefully) eliminate the complete code (including
1152
  // calculation of parameters) but for the future: be explicit
1153

1154
  // Compute the length of the entire track.
1155
  double total_distance = (r1 - r0).norm();
849,842,495✔
1156
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
849,842,495✔
1157
    return;
10,989,909✔
1158

1159
  // keep a copy of the original global position to pass to get_indices,
1160
  // which performs its own transformation to local coordinates
1161
  Position global_r = r0;
838,852,586✔
1162
  Position local_r = local_coords(r0);
838,852,586✔
1163

1164
  const int n = n_dimension_;
838,852,586✔
1165

1166
  // Flag if position is inside the mesh
1167
  bool in_mesh;
1168

1169
  // Position is r = r0 + u * traveled_distance, start at r0
1170
  double traveled_distance {0.0};
838,852,586✔
1171

1172
  // Calculate index of current cell. Offset the position a tiny bit in
1173
  // direction of flight
1174
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
838,852,586✔
1175

1176
  // if track is very short, assume that it is completely inside one cell.
1177
  // Only the current cell will score and no surfaces
1178
  if (total_distance < 2 * TINY_BIT) {
838,852,586✔
1179
    if (in_mesh) {
301,404✔
1180
      tally.track(ijk, 1.0);
300,964✔
1181
    }
1182
    return;
301,404✔
1183
  }
1184

1185
  // Calculate initial distances to next surfaces in all three dimensions
1186
  std::array<MeshDistance, 3> distances;
1,677,102,364✔
1187
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
1188
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
1189
  }
1190

1191
  // Loop until r = r1 is eventually reached
1192
  while (true) {
686,066,371✔
1193

1194
    if (in_mesh) {
1,524,617,553✔
1195

1196
      // find surface with minimal distance to current position
1197
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,446,164,041✔
1198
                     distances.begin();
1,446,164,041✔
1199

1200
      // Tally track length delta since last step
1201
      tally.track(ijk,
1,446,164,041✔
1202
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,446,164,041✔
1203
          total_distance);
1204

1205
      // update position and leave, if we have reached end position
1206
      traveled_distance = distances[k].distance;
1,446,164,041✔
1207
      if (traveled_distance >= total_distance)
1,446,164,041✔
1208
        return;
766,619,458✔
1209

1210
      // If we have not reached r1, we have hit a surface. Tally outward
1211
      // current
1212
      tally.surface(ijk, k, distances[k].max_surface, false);
679,544,583✔
1213

1214
      // Update cell and calculate distance to next surface in k-direction.
1215
      // The two other directions are still valid!
1216
      ijk[k] = distances[k].next_index;
679,544,583✔
1217
      distances[k] =
679,544,583✔
1218
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
679,544,583✔
1219

1220
      // Check if we have left the interior of the mesh
1221
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
679,544,583✔
1222

1223
      // If we are still inside the mesh, tally inward current for the next
1224
      // cell
1225
      if (in_mesh)
679,544,583✔
1226
        tally.surface(ijk, k, !distances[k].max_surface, true);
667,402,727✔
1227

1228
    } else { // not inside mesh
1229

1230
      // For all directions outside the mesh, find the distance that we need
1231
      // to travel to reach the next surface. Use the largest distance, as
1232
      // only this will cross all outer surfaces.
1233
      int k_max {-1};
78,453,512✔
1234
      for (int k = 0; k < n; ++k) {
312,500,988✔
1235
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
319,789,266✔
1236
            (distances[k].distance > traveled_distance)) {
85,741,790✔
1237
          traveled_distance = distances[k].distance;
81,205,698✔
1238
          k_max = k;
81,205,698✔
1239
        }
1240
      }
1241
      // Assure some distance is traveled
1242
      if (k_max == -1) {
78,453,512✔
1243
        traveled_distance += TINY_BIT;
100✔
1244
      }
1245

1246
      // If r1 is not inside the mesh, exit here
1247
      if (traveled_distance >= total_distance)
78,453,512✔
1248
        return;
71,931,724✔
1249

1250
      // Calculate the new cell index and update all distances to next
1251
      // surfaces.
1252
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,521,788✔
1253
      for (int k = 0; k < n; ++k) {
25,897,572✔
1254
        distances[k] =
19,375,784✔
1255
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
19,375,784✔
1256
      }
1257

1258
      // If inside the mesh, Tally inward current
1259
      if (in_mesh && k_max >= 0)
6,521,788!
1260
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,133,716✔
1261
    }
1262
  }
1263
}
1264

1265
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
747,908,345✔
1266
  vector<int>& bins, vector<double>& lengths) const
1267
{
1268

1269
  // Helper tally class.
1270
  // stores a pointer to the mesh class and references to bins and lengths
1271
  // parameters. Performs the actual tally through the track method.
1272
  struct TrackAggregator {
1273
    TrackAggregator(
747,908,345✔
1274
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1275
      : mesh(_mesh), bins(_bins), lengths(_lengths)
747,908,345✔
1276
    {}
747,908,345✔
1277
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1,300,209,036✔
1278
    void track(const MeshIndex& ijk, double l) const
1,319,151,765✔
1279
    {
1280
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,319,151,765✔
1281
      lengths.push_back(l);
1,319,151,765✔
1282
    }
1,319,151,765✔
1283

1284
    const StructuredMesh* mesh;
1285
    vector<int>& bins;
1286
    vector<double>& lengths;
1287
  };
1288

1289
  // Perform the mesh raytrace with the helper class.
1290
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
747,908,345✔
1291
}
747,908,345✔
1292

1293
void StructuredMesh::surface_bins_crossed(
101,934,150✔
1294
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1295
{
1296

1297
  // Helper tally class.
1298
  // stores a pointer to the mesh class and a reference to the bins parameter.
1299
  // Performs the actual tally through the surface method.
1300
  struct SurfaceAggregator {
1301
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
101,934,150✔
1302
      : mesh(_mesh), bins(_bins)
101,934,150✔
1303
    {}
101,934,150✔
1304
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
52,871,990✔
1305
    {
1306
      int i_bin =
1307
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
52,871,990✔
1308
      if (max)
52,871,990✔
1309
        i_bin += 2;
26,410,400✔
1310
      if (inward)
52,871,990✔
1311
        i_bin += 1;
25,983,900✔
1312
      bins.push_back(i_bin);
52,871,990✔
1313
    }
52,871,990✔
1314
    void track(const MeshIndex& idx, double l) const {}
127,313,240✔
1315

1316
    const StructuredMesh* mesh;
1317
    vector<int>& bins;
1318
  };
1319

1320
  // Perform the mesh raytrace with the helper class.
1321
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
101,934,150✔
1322
}
101,934,150✔
1323

1324
//==============================================================================
1325
// RegularMesh implementation
1326
//==============================================================================
1327

1328
int RegularMesh::set_grid()
1,930✔
1329
{
1330
  tensor::Tensor<int> shape(shape_.data(), static_cast<size_t>(n_dimension_));
1,930✔
1331

1332
  // Check that dimensions are all greater than zero
1333
  if ((shape <= 0).any()) {
1,930!
1334
    set_errmsg("All entries for a regular mesh dimensions "
×
1335
               "must be positive.");
1336
    return OPENMC_E_INVALID_ARGUMENT;
×
1337
  }
1338

1339
  // Make sure lower_left and dimension match
1340
  if (lower_left_.size() != n_dimension_) {
1,930!
1341
    set_errmsg("Number of entries in lower_left must be the same "
×
1342
               "as the regular mesh dimensions.");
1343
    return OPENMC_E_INVALID_ARGUMENT;
×
1344
  }
1345
  if (width_.size() > 0) {
1,930✔
1346

1347
    // Check to ensure width has same dimensions
1348
    if (width_.size() != n_dimension_) {
43!
1349
      set_errmsg("Number of entries on width must be the same as "
×
1350
                 "the regular mesh dimensions.");
1351
      return OPENMC_E_INVALID_ARGUMENT;
×
1352
    }
1353

1354
    // Check for negative widths
1355
    if ((width_ < 0.0).any()) {
43!
1356
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
1357
      return OPENMC_E_INVALID_ARGUMENT;
×
1358
    }
1359

1360
    // Set width and upper right coordinate
1361
    upper_right_ = lower_left_ + shape * width_;
43✔
1362

1363
  } else if (upper_right_.size() > 0) {
1,887!
1364

1365
    // Check to ensure upper_right_ has same dimensions
1366
    if (upper_right_.size() != n_dimension_) {
1,887!
1367
      set_errmsg("Number of entries on upper_right must be the "
×
1368
                 "same as the regular mesh dimensions.");
1369
      return OPENMC_E_INVALID_ARGUMENT;
×
1370
    }
1371

1372
    // Check that upper-right is above lower-left
1373
    if ((upper_right_ < lower_left_).any()) {
1,887!
1374
      set_errmsg(
×
1375
        "The upper_right coordinates of a regular mesh must be greater than "
1376
        "the lower_left coordinates.");
1377
      return OPENMC_E_INVALID_ARGUMENT;
×
1378
    }
1379

1380
    // Set width
1381
    width_ = (upper_right_ - lower_left_) / shape;
1,887✔
1382
  }
1383

1384
  // Set material volumes
1385
  volume_frac_ = 1.0 / shape.prod();
1,930✔
1386

1387
  element_volume_ = 1.0;
1,930✔
1388
  for (int i = 0; i < n_dimension_; i++) {
7,210✔
1389
    element_volume_ *= width_[i];
5,280✔
1390
  }
1391
  return 0;
1,930✔
1392
}
1,930✔
1393

1394
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
1,920✔
1395
{
1396
  // Determine number of dimensions for mesh
1397
  if (!check_for_node(node, "dimension")) {
1,920!
1398
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1399
  }
1400

1401
  tensor::Tensor<int> shape = get_node_tensor<int>(node, "dimension");
1,920✔
1402
  int n = n_dimension_ = shape.size();
1,920✔
1403
  if (n != 1 && n != 2 && n != 3) {
1,920!
1404
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1405
  }
1406
  std::copy(shape.begin(), shape.end(), shape_.begin());
1,920✔
1407

1408
  // Check for lower-left coordinates
1409
  if (check_for_node(node, "lower_left")) {
1,920!
1410
    // Read mesh lower-left corner location
1411
    lower_left_ = get_node_tensor<double>(node, "lower_left");
1,920✔
1412
  } else {
1413
    fatal_error("Must specify <lower_left> on a mesh.");
×
1414
  }
1415

1416
  if (check_for_node(node, "width")) {
1,920✔
1417
    // Make sure one of upper-right or width were specified
1418
    if (check_for_node(node, "upper_right")) {
43!
1419
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1420
    }
1421

1422
    width_ = get_node_tensor<double>(node, "width");
43✔
1423

1424
  } else if (check_for_node(node, "upper_right")) {
1,877!
1425

1426
    upper_right_ = get_node_tensor<double>(node, "upper_right");
1,877✔
1427

1428
  } else {
1429
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1430
  }
1431

1432
  if (int err = set_grid()) {
1,920!
1433
    fatal_error(openmc_err_msg);
×
1434
  }
1435
}
1,920✔
1436

1437
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
10✔
1438
{
1439
  // Determine number of dimensions for mesh
1440
  if (!object_exists(group, "dimension")) {
10!
1441
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1442
  }
1443

1444
  tensor::Tensor<int> shape;
10✔
1445
  read_dataset(group, "dimension", shape);
10✔
1446
  int n = n_dimension_ = shape.size();
10✔
1447
  if (n != 1 && n != 2 && n != 3) {
10!
1448
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1449
  }
1450
  std::copy(shape.begin(), shape.end(), shape_.begin());
10✔
1451

1452
  // Check for lower-left coordinates
1453
  if (object_exists(group, "lower_left")) {
10!
1454
    // Read mesh lower-left corner location
1455
    read_dataset(group, "lower_left", lower_left_);
10✔
1456
  } else {
1457
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1458
  }
1459

1460
  if (object_exists(group, "upper_right")) {
10!
1461

1462
    read_dataset(group, "upper_right", upper_right_);
10✔
1463

1464
  } else {
1465
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1466
  }
1467

1468
  if (int err = set_grid()) {
10!
1469
    fatal_error(openmc_err_msg);
×
1470
  }
1471
}
10✔
1472

1473
int RegularMesh::get_index_in_direction(double r, int i) const
2,147,483,647✔
1474
{
1475
  return std::ceil((r - lower_left_[i]) / width_[i]);
2,147,483,647✔
1476
}
1477

1478
const std::string RegularMesh::mesh_type = "regular";
1479

1480
std::string RegularMesh::get_mesh_type() const
2,914✔
1481
{
1482
  return mesh_type;
2,914✔
1483
}
1484

1485
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,341,874,506✔
1486
{
1487
  return lower_left_[i] + ijk[i] * width_[i];
1,341,874,506✔
1488
}
1489

1490
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,278,933,207✔
1491
{
1492
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,278,933,207✔
1493
}
1494

1495
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1496
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1497
  double l) const
1498
{
1499
  MeshDistance d;
2,147,483,647✔
1500
  d.next_index = ijk[i];
2,147,483,647✔
1501
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1502
    return d;
12,601,668✔
1503

1504
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1505
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1506
    d.next_index++;
1,338,054,366✔
1507
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,338,054,366✔
1508
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,294,620,929✔
1509
    d.next_index--;
1,275,113,067✔
1510
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,275,113,067✔
1511
  }
1512

1513
  return d;
2,147,483,647✔
1514
}
1515

1516
std::pair<vector<double>, vector<double>> RegularMesh::plot(
20✔
1517
  Position plot_ll, Position plot_ur) const
1518
{
1519
  // Figure out which axes lie in the plane of the plot.
1520
  array<int, 2> axes {-1, -1};
20✔
1521
  if (plot_ur.z == plot_ll.z) {
20!
1522
    axes[0] = 0;
20✔
1523
    if (n_dimension_ > 1)
20!
1524
      axes[1] = 1;
20✔
1525
  } else if (plot_ur.y == plot_ll.y) {
×
1526
    axes[0] = 0;
×
1527
    if (n_dimension_ > 2)
×
1528
      axes[1] = 2;
×
1529
  } else if (plot_ur.x == plot_ll.x) {
×
1530
    if (n_dimension_ > 1)
×
1531
      axes[0] = 1;
×
1532
    if (n_dimension_ > 2)
×
1533
      axes[1] = 2;
×
1534
  } else {
1535
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1536
  }
1537

1538
  // Get the coordinates of the mesh lines along both of the axes.
1539
  array<vector<double>, 2> axis_lines;
20✔
1540
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
60✔
1541
    int axis = axes[i_ax];
40✔
1542
    if (axis == -1)
40!
1543
      continue;
×
1544
    auto& lines {axis_lines[i_ax]};
40✔
1545

1546
    double coord = lower_left_[axis];
40✔
1547
    for (int i = 0; i < shape_[axis] + 1; ++i) {
260✔
1548
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
220!
1549
        lines.push_back(coord);
220✔
1550
      coord += width_[axis];
220✔
1551
    }
1552
  }
1553

1554
  return {axis_lines[0], axis_lines[1]};
40✔
1555
}
20✔
1556

1557
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1,884✔
1558
{
1559
  write_dataset(mesh_group, "dimension", get_shape_tensor());
1,884✔
1560
  write_dataset(mesh_group, "lower_left", lower_left_);
1,884✔
1561
  write_dataset(mesh_group, "upper_right", upper_right_);
1,884✔
1562
  write_dataset(mesh_group, "width", width_);
1,884✔
1563
}
1,884✔
1564

1565
tensor::Tensor<double> RegularMesh::count_sites(
7,116✔
1566
  const SourceSite* bank, int64_t length, bool* outside) const
1567
{
1568
  // Determine shape of array for counts
1569
  std::size_t m = this->n_bins();
7,116✔
1570
  vector<std::size_t> shape = {m};
7,116✔
1571

1572
  // Create array of zeros
1573
  tensor::Tensor<double> cnt(shape, 0.0);
7,116✔
1574
  bool outside_ = false;
7,116✔
1575

1576
  for (int64_t i = 0; i < length; i++) {
6,977,526✔
1577
    const auto& site = bank[i];
6,970,410✔
1578

1579
    // determine scoring bin for entropy mesh
1580
    int mesh_bin = get_bin(site.r);
6,970,410✔
1581

1582
    // if outside mesh, skip particle
1583
    if (mesh_bin < 0) {
6,970,410!
1584
      outside_ = true;
×
1585
      continue;
×
1586
    }
1587

1588
    // Add to appropriate bin
1589
    cnt(mesh_bin) += site.wgt;
6,970,410✔
1590
  }
1591

1592
  // Create reduced count data
1593
  tensor::Tensor<double> counts(shape, 0.0);
7,116✔
1594
  int total = cnt.size();
7,116✔
1595

1596
#ifdef OPENMC_MPI
1597
  // collect values from all processors
1598
  MPI_Reduce(
2,892✔
1599
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
2,892✔
1600

1601
  // Check if there were sites outside the mesh for any processor
1602
  if (outside) {
2,892!
1603
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
2,892✔
1604
  }
1605
#else
1606
  std::copy(cnt.data(), cnt.data() + total, counts.data());
4,224✔
1607
  if (outside)
4,224!
1608
    *outside = outside_;
4,224✔
1609
#endif
1610

1611
  return counts;
14,232✔
1612
}
7,116✔
1613

1614
double RegularMesh::volume(const MeshIndex& ijk) const
1,016,146✔
1615
{
1616
  return element_volume_;
1,016,146✔
1617
}
1618

1619
//==============================================================================
1620
// RectilinearMesh implementation
1621
//==============================================================================
1622

1623
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
112✔
1624
{
1625
  n_dimension_ = 3;
112✔
1626

1627
  grid_[0] = get_node_array<double>(node, "x_grid");
112✔
1628
  grid_[1] = get_node_array<double>(node, "y_grid");
112✔
1629
  grid_[2] = get_node_array<double>(node, "z_grid");
112✔
1630

1631
  if (int err = set_grid()) {
112!
1632
    fatal_error(openmc_err_msg);
×
1633
  }
1634
}
112✔
1635

1636
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
10✔
1637
{
1638
  n_dimension_ = 3;
10✔
1639

1640
  read_dataset(group, "x_grid", grid_[0]);
10✔
1641
  read_dataset(group, "y_grid", grid_[1]);
10✔
1642
  read_dataset(group, "z_grid", grid_[2]);
10✔
1643

1644
  if (int err = set_grid()) {
10!
1645
    fatal_error(openmc_err_msg);
×
1646
  }
1647
}
10✔
1648

1649
const std::string RectilinearMesh::mesh_type = "rectilinear";
1650

1651
std::string RectilinearMesh::get_mesh_type() const
250✔
1652
{
1653
  return mesh_type;
250✔
1654
}
1655

1656
double RectilinearMesh::positive_grid_boundary(
24,096,330✔
1657
  const MeshIndex& ijk, int i) const
1658
{
1659
  return grid_[i][ijk[i]];
24,096,330✔
1660
}
1661

1662
double RectilinearMesh::negative_grid_boundary(
23,399,460✔
1663
  const MeshIndex& ijk, int i) const
1664
{
1665
  return grid_[i][ijk[i] - 1];
23,399,460✔
1666
}
1667

1668
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
48,729,170✔
1669
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1670
  double l) const
1671
{
1672
  MeshDistance d;
48,729,170✔
1673
  d.next_index = ijk[i];
48,729,170✔
1674
  if (std::abs(u[i]) < FP_PRECISION)
48,729,170✔
1675
    return d;
519,840✔
1676

1677
  d.max_surface = (u[i] > 0);
48,209,330✔
1678
  if (d.max_surface && (ijk[i] <= shape_[i])) {
48,209,330✔
1679
    d.next_index++;
24,096,330✔
1680
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
24,096,330✔
1681
  } else if (!d.max_surface && (ijk[i] > 0)) {
24,113,000✔
1682
    d.next_index--;
23,399,460✔
1683
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
23,399,460✔
1684
  }
1685
  return d;
48,209,330✔
1686
}
1687

1688
int RectilinearMesh::set_grid()
162✔
1689
{
1690
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
162✔
1691
    static_cast<int>(grid_[1].size()) - 1,
162✔
1692
    static_cast<int>(grid_[2].size()) - 1};
162✔
1693

1694
  for (const auto& g : grid_) {
648✔
1695
    if (g.size() < 2) {
486!
1696
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1697
                 "must each have at least 2 points");
1698
      return OPENMC_E_INVALID_ARGUMENT;
×
1699
    }
1700
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
486✔
1701
        g.end()) {
972!
1702
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1703
                 "rectilinear meshes must be sorted and unique.");
1704
      return OPENMC_E_INVALID_ARGUMENT;
×
1705
    }
1706
  }
1707

1708
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
162✔
1709
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
162✔
1710

1711
  return 0;
162✔
1712
}
1713

1714
int RectilinearMesh::get_index_in_direction(double r, int i) const
67,371,720✔
1715
{
1716
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
67,371,720✔
1717
}
1718

1719
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
10✔
1720
  Position plot_ll, Position plot_ur) const
1721
{
1722
  // Figure out which axes lie in the plane of the plot.
1723
  array<int, 2> axes {-1, -1};
10✔
1724
  if (plot_ur.z == plot_ll.z) {
10!
1725
    axes = {0, 1};
×
1726
  } else if (plot_ur.y == plot_ll.y) {
10!
1727
    axes = {0, 2};
10✔
1728
  } else if (plot_ur.x == plot_ll.x) {
×
1729
    axes = {1, 2};
×
1730
  } else {
1731
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1732
  }
1733

1734
  // Get the coordinates of the mesh lines along both of the axes.
1735
  array<vector<double>, 2> axis_lines;
10✔
1736
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
30✔
1737
    int axis = axes[i_ax];
20✔
1738
    vector<double>& lines {axis_lines[i_ax]};
20✔
1739

1740
    for (auto coord : grid_[axis]) {
100✔
1741
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
80!
1742
        lines.push_back(coord);
80✔
1743
    }
1744
  }
1745

1746
  return {axis_lines[0], axis_lines[1]};
20✔
1747
}
10✔
1748

1749
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
100✔
1750
{
1751
  write_dataset(mesh_group, "x_grid", grid_[0]);
100✔
1752
  write_dataset(mesh_group, "y_grid", grid_[1]);
100✔
1753
  write_dataset(mesh_group, "z_grid", grid_[2]);
100✔
1754
}
100✔
1755

1756
double RectilinearMesh::volume(const MeshIndex& ijk) const
120✔
1757
{
1758
  double vol {1.0};
120✔
1759

1760
  for (int i = 0; i < n_dimension_; i++) {
480✔
1761
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
360✔
1762
  }
1763
  return vol;
120✔
1764
}
1765

1766
//==============================================================================
1767
// CylindricalMesh implementation
1768
//==============================================================================
1769

1770
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
364✔
1771
  : PeriodicStructuredMesh {node}
364✔
1772
{
1773
  n_dimension_ = 3;
364✔
1774
  grid_[0] = get_node_array<double>(node, "r_grid");
364✔
1775
  grid_[1] = get_node_array<double>(node, "phi_grid");
364✔
1776
  grid_[2] = get_node_array<double>(node, "z_grid");
364✔
1777
  origin_ = get_node_position(node, "origin");
364✔
1778

1779
  if (int err = set_grid()) {
364!
1780
    fatal_error(openmc_err_msg);
×
1781
  }
1782
}
364✔
1783

1784
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
10✔
1785
{
1786
  n_dimension_ = 3;
10✔
1787
  read_dataset(group, "r_grid", grid_[0]);
10✔
1788
  read_dataset(group, "phi_grid", grid_[1]);
10✔
1789
  read_dataset(group, "z_grid", grid_[2]);
10✔
1790
  read_dataset(group, "origin", origin_);
10✔
1791

1792
  if (int err = set_grid()) {
10!
1793
    fatal_error(openmc_err_msg);
×
1794
  }
1795
}
10✔
1796

1797
const std::string CylindricalMesh::mesh_type = "cylindrical";
1798

1799
std::string CylindricalMesh::get_mesh_type() const
440✔
1800
{
1801
  return mesh_type;
440✔
1802
}
1803

1804
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
43,392,810✔
1805
  Position r, bool& in_mesh) const
1806
{
1807
  r = local_coords(r);
43,392,810✔
1808

1809
  Position mapped_r;
43,392,810✔
1810
  mapped_r[0] = std::hypot(r.x, r.y);
43,392,810✔
1811
  mapped_r[2] = r[2];
43,392,810✔
1812

1813
  if (mapped_r[0] < FP_PRECISION) {
43,392,810!
1814
    mapped_r[1] = 0.0;
×
1815
  } else {
1816
    mapped_r[1] = std::atan2(r.y, r.x);
43,392,810✔
1817
    if (mapped_r[1] < 0)
43,392,810✔
1818
      mapped_r[1] += 2 * M_PI;
21,704,420✔
1819
  }
1820

1821
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
43,392,810✔
1822

1823
  idx[1] = sanitize_phi(idx[1]);
43,392,810✔
1824

1825
  return idx;
43,392,810✔
1826
}
1827

1828
Position CylindricalMesh::sample_element(
80,100✔
1829
  const MeshIndex& ijk, uint64_t* seed) const
1830
{
1831
  double r_min = this->r(ijk[0] - 1);
80,100✔
1832
  double r_max = this->r(ijk[0]);
80,100✔
1833

1834
  double phi_min = this->phi(ijk[1] - 1);
80,100✔
1835
  double phi_max = this->phi(ijk[1]);
80,100✔
1836

1837
  double z_min = this->z(ijk[2] - 1);
80,100✔
1838
  double z_max = this->z(ijk[2]);
80,100✔
1839

1840
  double r_min_sq = r_min * r_min;
80,100✔
1841
  double r_max_sq = r_max * r_max;
80,100✔
1842
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
80,100✔
1843
  double phi = uniform_distribution(phi_min, phi_max, seed);
80,100✔
1844
  double z = uniform_distribution(z_min, z_max, seed);
80,100✔
1845

1846
  double x = r * std::cos(phi);
80,100✔
1847
  double y = r * std::sin(phi);
80,100✔
1848

1849
  return origin_ + Position(x, y, z);
80,100✔
1850
}
1851

1852
double CylindricalMesh::find_r_crossing(
129,625,796✔
1853
  const Position& r, const Direction& u, double l, int shell) const
1854
{
1855

1856
  if ((shell < 0) || (shell > shape_[0]))
129,625,796!
1857
    return INFTY;
16,285,380✔
1858

1859
  // solve r.x^2 + r.y^2 == r0^2
1860
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1861
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1862

1863
  const double r0 = grid_[0][shell];
113,340,416✔
1864
  if (r0 == 0.0)
113,340,416✔
1865
    return INFTY;
6,487,340✔
1866

1867
  const double denominator = u.x * u.x + u.y * u.y;
106,853,076✔
1868

1869
  // Direction of flight is in z-direction. Will never intersect r.
1870
  if (std::abs(denominator) < FP_PRECISION)
106,853,076✔
1871
    return INFTY;
53,600✔
1872

1873
  // inverse of dominator to help the compiler to speed things up
1874
  const double inv_denominator = 1.0 / denominator;
106,799,476✔
1875

1876
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
106,799,476✔
1877
  double R = std::sqrt(r.x * r.x + r.y * r.y);
106,799,476✔
1878
  double D = p * p - (R - r0) * (R + r0) * inv_denominator;
106,799,476✔
1879

1880
  if (D < 0.0)
106,799,476✔
1881
    return INFTY;
8,851,020✔
1882

1883
  D = std::sqrt(D);
97,948,456✔
1884

1885
  // Particle is already on the shell surface; avoid spurious crossing
1886
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
97,948,456✔
1887
    return INFTY;
6,030,340✔
1888

1889
  // Check -p - D first because it is always smaller as -p + D
1890
  if (-p - D > l)
91,918,116✔
1891
    return -p - D;
18,370,914✔
1892
  if (-p + D > l)
73,547,202✔
1893
    return -p + D;
45,525,908✔
1894

1895
  return INFTY;
28,021,294✔
1896
}
1897

1898
double CylindricalMesh::find_phi_crossing(
67,687,640✔
1899
  const Position& r, const Direction& u, double l, int shell) const
1900
{
1901
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1902
  if (full_phi_ && (shape_[1] == 1))
67,687,640✔
1903
    return INFTY;
27,714,260✔
1904

1905
  shell = sanitize_phi(shell);
39,973,380✔
1906

1907
  const double p0 = grid_[1][shell];
39,973,380✔
1908

1909
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1910
  // => x(s) * cos(p0) = y(s) * sin(p0)
1911
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1912
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1913

1914
  const double c0 = std::cos(p0);
39,973,380✔
1915
  const double s0 = std::sin(p0);
39,973,380✔
1916

1917
  const double denominator = (u.x * s0 - u.y * c0);
39,973,380✔
1918

1919
  // Check if direction of flight is not parallel to phi surface
1920
  if (std::abs(denominator) > FP_PRECISION) {
39,973,380✔
1921
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,736,340✔
1922
    // Check if solution is in positive direction of flight and crosses the
1923
    // correct phi surface (not -phi)
1924
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,736,340✔
1925
      return s;
18,381,690✔
1926
  }
1927

1928
  return INFTY;
21,591,690✔
1929
}
1930

1931
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
33,359,770✔
1932
  const Position& r, const Direction& u, double l, int shell) const
1933
{
1934
  MeshDistance d;
33,359,770✔
1935
  d.next_index = shell;
33,359,770✔
1936

1937
  // Direction of flight is within xy-plane. Will never intersect z.
1938
  if (std::abs(u.z) < FP_PRECISION)
33,359,770✔
1939
    return d;
1,016,560✔
1940

1941
  d.max_surface = (u.z > 0.0);
32,343,210✔
1942
  if (d.max_surface && (shell <= shape_[2])) {
32,343,210✔
1943
    d.next_index += 1;
15,341,720✔
1944
    d.distance = (grid_[2][shell] - r.z) / u.z;
15,341,720✔
1945
  } else if (!d.max_surface && (shell > 0)) {
17,001,490✔
1946
    d.next_index -= 1;
15,314,750✔
1947
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
15,314,750✔
1948
  }
1949
  return d;
32,343,210✔
1950
}
1951

1952
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
132,016,488✔
1953
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1954
  double l) const
1955
{
1956
  if (i == 0) {
132,016,488✔
1957

1958
    return std::min(
64,812,898✔
1959
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
64,812,898✔
1960
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
129,625,796✔
1961

1962
  } else if (i == 1) {
67,203,590✔
1963

1964
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
33,843,820✔
1965
                      find_phi_crossing(r0, u, l, ijk[i])),
33,843,820✔
1966
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
33,843,820✔
1967
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
67,687,640✔
1968

1969
  } else {
1970
    return find_z_crossing(r0, u, l, ijk[i]);
33,359,770✔
1971
  }
1972
}
1973

1974
int CylindricalMesh::set_grid()
394✔
1975
{
1976
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
394✔
1977
    static_cast<int>(grid_[1].size()) - 1,
394✔
1978
    static_cast<int>(grid_[2].size()) - 1};
394✔
1979

1980
  for (const auto& g : grid_) {
1,576✔
1981
    if (g.size() < 2) {
1,182!
UNCOV
1982
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
1983
                 "must each have at least 2 points");
UNCOV
1984
      return OPENMC_E_INVALID_ARGUMENT;
×
1985
    }
1986
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,182✔
1987
        g.end()) {
2,364!
UNCOV
1988
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
1989
                 "cylindrical meshes must be sorted and unique.");
UNCOV
1990
      return OPENMC_E_INVALID_ARGUMENT;
×
1991
    }
1992
  }
1993
  if (grid_[0].front() < 0.0) {
394!
UNCOV
1994
    set_errmsg("r-grid for "
×
1995
               "cylindrical meshes must start at r >= 0.");
UNCOV
1996
    return OPENMC_E_INVALID_ARGUMENT;
×
1997
  }
1998
  if (grid_[1].front() < 0.0) {
394!
UNCOV
1999
    set_errmsg("phi-grid for "
×
2000
               "cylindrical meshes must start at phi >= 0.");
UNCOV
2001
    return OPENMC_E_INVALID_ARGUMENT;
×
2002
  }
2003
  if (grid_[1].back() > 2.0 * PI) {
394!
UNCOV
2004
    set_errmsg("phi-grids for "
×
2005
               "cylindrical meshes must end with theta <= 2*pi.");
2006

UNCOV
2007
    return OPENMC_E_INVALID_ARGUMENT;
×
2008
  }
2009

2010
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
394!
2011

2012
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
394✔
2013
    origin_[2] + grid_[2].front()};
394✔
2014
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
394✔
2015
    origin_[2] + grid_[2].back()};
394✔
2016

2017
  return 0;
394✔
2018
}
2019

2020
int CylindricalMesh::get_index_in_direction(double r, int i) const
130,178,430✔
2021
{
2022
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
130,178,430✔
2023
}
2024

UNCOV
2025
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
2026
  Position plot_ll, Position plot_ur) const
2027
{
UNCOV
2028
  fatal_error("Plot of cylindrical Mesh not implemented");
×
2029

2030
  // Figure out which axes lie in the plane of the plot.
2031
  array<vector<double>, 2> axis_lines;
2032
  return {axis_lines[0], axis_lines[1]};
2033
}
2034

2035
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
340✔
2036
{
2037
  write_dataset(mesh_group, "r_grid", grid_[0]);
340✔
2038
  write_dataset(mesh_group, "phi_grid", grid_[1]);
340✔
2039
  write_dataset(mesh_group, "z_grid", grid_[2]);
340✔
2040
  write_dataset(mesh_group, "origin", origin_);
340✔
2041
}
340✔
2042

2043
double CylindricalMesh::volume(const MeshIndex& ijk) const
720✔
2044
{
2045
  double r_i = grid_[0][ijk[0] - 1];
720✔
2046
  double r_o = grid_[0][ijk[0]];
720✔
2047

2048
  double phi_i = grid_[1][ijk[1] - 1];
720✔
2049
  double phi_o = grid_[1][ijk[1]];
720✔
2050

2051
  double z_i = grid_[2][ijk[2] - 1];
720✔
2052
  double z_o = grid_[2][ijk[2]];
720✔
2053

2054
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
720✔
2055
}
2056

2057
//==============================================================================
2058
// SphericalMesh implementation
2059
//==============================================================================
2060

2061
SphericalMesh::SphericalMesh(pugi::xml_node node)
314✔
2062
  : PeriodicStructuredMesh {node}
314✔
2063
{
2064
  n_dimension_ = 3;
314✔
2065

2066
  grid_[0] = get_node_array<double>(node, "r_grid");
314✔
2067
  grid_[1] = get_node_array<double>(node, "theta_grid");
314✔
2068
  grid_[2] = get_node_array<double>(node, "phi_grid");
314✔
2069
  origin_ = get_node_position(node, "origin");
314✔
2070

2071
  if (int err = set_grid()) {
314!
UNCOV
2072
    fatal_error(openmc_err_msg);
×
2073
  }
2074
}
314✔
2075

2076
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
10✔
2077
{
2078
  n_dimension_ = 3;
10✔
2079

2080
  read_dataset(group, "r_grid", grid_[0]);
10✔
2081
  read_dataset(group, "theta_grid", grid_[1]);
10✔
2082
  read_dataset(group, "phi_grid", grid_[2]);
10✔
2083
  read_dataset(group, "origin", origin_);
10✔
2084

2085
  if (int err = set_grid()) {
10!
UNCOV
2086
    fatal_error(openmc_err_msg);
×
2087
  }
2088
}
10✔
2089

2090
const std::string SphericalMesh::mesh_type = "spherical";
2091

2092
std::string SphericalMesh::get_mesh_type() const
350✔
2093
{
2094
  return mesh_type;
350✔
2095
}
2096

2097
StructuredMesh::MeshIndex SphericalMesh::get_indices(
62,356,480✔
2098
  Position r, bool& in_mesh) const
2099
{
2100
  r = local_coords(r);
62,356,480✔
2101

2102
  Position mapped_r;
62,356,480✔
2103
  mapped_r[0] = r.norm();
62,356,480✔
2104

2105
  if (mapped_r[0] < FP_PRECISION) {
62,356,480!
2106
    mapped_r[1] = 0.0;
×
UNCOV
2107
    mapped_r[2] = 0.0;
×
2108
  } else {
2109
    mapped_r[1] = std::acos(r.z / mapped_r.x);
62,356,480✔
2110
    mapped_r[2] = std::atan2(r.y, r.x);
62,356,480✔
2111
    if (mapped_r[2] < 0)
62,356,480✔
2112
      mapped_r[2] += 2 * M_PI;
31,153,350✔
2113
  }
2114

2115
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
62,356,480✔
2116

2117
  idx[1] = sanitize_theta(idx[1]);
62,356,480✔
2118
  idx[2] = sanitize_phi(idx[2]);
62,356,480✔
2119

2120
  return idx;
62,356,480✔
2121
}
2122

2123
Position SphericalMesh::sample_element(
100✔
2124
  const MeshIndex& ijk, uint64_t* seed) const
2125
{
2126
  double r_min = this->r(ijk[0] - 1);
100✔
2127
  double r_max = this->r(ijk[0]);
100✔
2128

2129
  double theta_min = this->theta(ijk[1] - 1);
100✔
2130
  double theta_max = this->theta(ijk[1]);
100✔
2131

2132
  double phi_min = this->phi(ijk[2] - 1);
100✔
2133
  double phi_max = this->phi(ijk[2]);
100✔
2134

2135
  double cos_theta =
2136
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
100✔
2137
  double sin_theta = std::sin(std::acos(cos_theta));
100✔
2138
  double phi = uniform_distribution(phi_min, phi_max, seed);
100✔
2139
  double r_min_cub = std::pow(r_min, 3);
100✔
2140
  double r_max_cub = std::pow(r_max, 3);
100✔
2141
  // might be faster to do rejection here?
2142
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
100✔
2143

2144
  double x = r * std::cos(phi) * sin_theta;
100✔
2145
  double y = r * std::sin(phi) * sin_theta;
100✔
2146
  double z = r * cos_theta;
100✔
2147

2148
  return origin_ + Position(x, y, z);
100✔
2149
}
2150

2151
double SphericalMesh::find_r_crossing(
403,664,624✔
2152
  const Position& r, const Direction& u, double l, int shell) const
2153
{
2154
  if ((shell < 0) || (shell > shape_[0]))
403,664,624✔
2155
    return INFTY;
36,019,170✔
2156

2157
  // solve |r+s*u| = r0
2158
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2159
  const double r0 = grid_[0][shell];
367,645,454✔
2160
  if (r0 == 0.0)
367,645,454✔
2161
    return INFTY;
6,980,470✔
2162
  const double p = r.dot(u);
360,664,984✔
2163
  double R = r.norm();
360,664,984✔
2164
  double D = p * p - (R - r0) * (R + r0);
360,664,984✔
2165

2166
  // Particle is already on the shell surface; avoid spurious crossing
2167
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
360,664,984✔
2168
    return INFTY;
9,735,140✔
2169

2170
  if (D >= 0.0) {
350,929,844✔
2171
    D = std::sqrt(D);
325,587,164✔
2172
    // Check -p - D first because it is always smaller as -p + D
2173
    if (-p - D > l)
325,587,164✔
2174
      return -p - D;
58,479,784✔
2175
    if (-p + D > l)
267,107,380✔
2176
      return -p + D;
161,151,572✔
2177
  }
2178

2179
  return INFTY;
131,298,488✔
2180
}
2181

2182
double SphericalMesh::find_theta_crossing(
100,146,680✔
2183
  const Position& r, const Direction& u, double l, int shell) const
2184
{
2185
  // Theta grid is [0, π], thus there is no real surface to cross
2186
  if (full_theta_ && (shape_[1] == 1))
100,146,680✔
2187
    return INFTY;
65,275,280✔
2188

2189
  shell = sanitize_theta(shell);
34,871,400✔
2190

2191
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2192
  // yields
2193
  // a*s^2 + 2*b*s + c == 0 with
2194
  // a = cos(theta)^2 - u.z * u.z
2195
  // b = r*u * cos(theta)^2 - u.z * r.z
2196
  // c = r*r * cos(theta)^2 - r.z^2
2197

2198
  const double cos_t = std::cos(grid_[1][shell]);
34,871,400✔
2199
  const bool sgn = std::signbit(cos_t);
34,871,400✔
2200
  const double cos_t_2 = cos_t * cos_t;
34,871,400✔
2201

2202
  const double a = cos_t_2 - u.z * u.z;
34,871,400✔
2203
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
34,871,400✔
2204
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
34,871,400✔
2205

2206
  // if factor of s^2 is zero, direction of flight is parallel to theta
2207
  // surface
2208
  if (std::abs(a) < FP_PRECISION) {
34,871,400✔
2209
    // if b vanishes, direction of flight is within theta surface and crossing
2210
    // is not possible
2211
    if (std::abs(b) < FP_PRECISION)
438,680!
2212
      return INFTY;
438,680✔
2213

UNCOV
2214
    const double s = -0.5 * c / b;
×
2215
    // Check if solution is in positive direction of flight and has correct
2216
    // sign
UNCOV
2217
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
UNCOV
2218
      return s;
×
2219

2220
    // no crossing is possible
UNCOV
2221
    return INFTY;
×
2222
  }
2223

2224
  const double p = b / a;
34,432,720✔
2225
  double D = p * p - c / a;
34,432,720✔
2226

2227
  if (D < 0.0)
34,432,720✔
2228
    return INFTY;
9,959,080✔
2229

2230
  D = std::sqrt(D);
24,473,640✔
2231

2232
  // the solution -p-D is always smaller as -p+D : Check this one first
2233
  double s = -p - D;
24,473,640✔
2234
  // Check if solution is in positive direction of flight and has correct sign
2235
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
24,473,640✔
2236
    return s;
4,802,370✔
2237

2238
  s = -p + D;
19,671,270✔
2239
  // Check if solution is in positive direction of flight and has correct sign
2240
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
19,671,270✔
2241
    return s;
9,239,360✔
2242

2243
  return INFTY;
10,431,910✔
2244
}
2245

2246
double SphericalMesh::find_phi_crossing(
101,591,660✔
2247
  const Position& r, const Direction& u, double l, int shell) const
2248
{
2249
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2250
  if (full_phi_ && (shape_[2] == 1))
101,591,660✔
2251
    return INFTY;
65,275,280✔
2252

2253
  shell = sanitize_phi(shell);
36,316,380✔
2254

2255
  const double p0 = grid_[2][shell];
36,316,380✔
2256

2257
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2258
  // => x(s) * cos(p0) = y(s) * sin(p0)
2259
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2260
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2261

2262
  const double c0 = std::cos(p0);
36,316,380✔
2263
  const double s0 = std::sin(p0);
36,316,380✔
2264

2265
  const double denominator = (u.x * s0 - u.y * c0);
36,316,380✔
2266

2267
  // Check if direction of flight is not parallel to phi surface
2268
  if (std::abs(denominator) > FP_PRECISION) {
36,316,380✔
2269
    const double s = -(r.x * s0 - r.y * c0) / denominator;
36,103,660✔
2270
    // Check if solution is in positive direction of flight and crosses the
2271
    // correct phi surface (not -phi)
2272
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
36,103,660✔
2273
      return s;
15,981,320✔
2274
  }
2275

2276
  return INFTY;
20,335,060✔
2277
}
2278

2279
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
302,701,482✔
2280
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2281
  double l) const
2282
{
2283

2284
  if (i == 0) {
302,701,482✔
2285
    return std::min(
201,832,312✔
2286
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
201,832,312✔
2287
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
403,664,624✔
2288

2289
  } else if (i == 1) {
100,869,170✔
2290
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
50,073,340✔
2291
                      find_theta_crossing(r0, u, l, ijk[i])),
50,073,340✔
2292
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
50,073,340✔
2293
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
100,146,680✔
2294

2295
  } else {
2296
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
50,795,830✔
2297
                      find_phi_crossing(r0, u, l, ijk[i])),
50,795,830✔
2298
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
50,795,830✔
2299
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
101,591,660✔
2300
  }
2301
}
2302

2303
int SphericalMesh::set_grid()
344✔
2304
{
2305
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
344✔
2306
    static_cast<int>(grid_[1].size()) - 1,
344✔
2307
    static_cast<int>(grid_[2].size()) - 1};
344✔
2308

2309
  for (const auto& g : grid_) {
1,376✔
2310
    if (g.size() < 2) {
1,032!
2311
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2312
                 "must each have at least 2 points");
UNCOV
2313
      return OPENMC_E_INVALID_ARGUMENT;
×
2314
    }
2315
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,032✔
2316
        g.end()) {
2,064!
2317
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2318
                 "spherical meshes must be sorted and unique.");
UNCOV
2319
      return OPENMC_E_INVALID_ARGUMENT;
×
2320
    }
2321
    if (g.front() < 0.0) {
1,032!
2322
      set_errmsg("r-, theta-, and phi- grids for "
×
2323
                 "spherical meshes must start at v >= 0.");
UNCOV
2324
      return OPENMC_E_INVALID_ARGUMENT;
×
2325
    }
2326
  }
2327
  if (grid_[1].back() > PI) {
344!
UNCOV
2328
    set_errmsg("theta-grids for "
×
2329
               "spherical meshes must end with theta <= pi.");
2330

UNCOV
2331
    return OPENMC_E_INVALID_ARGUMENT;
×
2332
  }
2333
  if (grid_[2].back() > 2 * PI) {
344!
2334
    set_errmsg("phi-grids for "
×
2335
               "spherical meshes must end with phi <= 2*pi.");
UNCOV
2336
    return OPENMC_E_INVALID_ARGUMENT;
×
2337
  }
2338

2339
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
344!
2340
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
344✔
2341

2342
  double r = grid_[0].back();
344✔
2343
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
344✔
2344
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
344✔
2345

2346
  return 0;
344✔
2347
}
2348

2349
int SphericalMesh::get_index_in_direction(double r, int i) const
187,069,440✔
2350
{
2351
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
187,069,440✔
2352
}
2353

UNCOV
2354
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2355
  Position plot_ll, Position plot_ur) const
2356
{
UNCOV
2357
  fatal_error("Plot of spherical Mesh not implemented");
×
2358

2359
  // Figure out which axes lie in the plane of the plot.
2360
  array<vector<double>, 2> axis_lines;
2361
  return {axis_lines[0], axis_lines[1]};
2362
}
2363

2364
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
290✔
2365
{
2366
  write_dataset(mesh_group, "r_grid", grid_[0]);
290✔
2367
  write_dataset(mesh_group, "theta_grid", grid_[1]);
290✔
2368
  write_dataset(mesh_group, "phi_grid", grid_[2]);
290✔
2369
  write_dataset(mesh_group, "origin", origin_);
290✔
2370
}
290✔
2371

2372
double SphericalMesh::volume(const MeshIndex& ijk) const
850✔
2373
{
2374
  double r_i = grid_[0][ijk[0] - 1];
850✔
2375
  double r_o = grid_[0][ijk[0]];
850✔
2376

2377
  double theta_i = grid_[1][ijk[1] - 1];
850✔
2378
  double theta_o = grid_[1][ijk[1]];
850✔
2379

2380
  double phi_i = grid_[2][ijk[2] - 1];
850✔
2381
  double phi_o = grid_[2][ijk[2]];
850✔
2382

2383
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
850✔
2384
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
850✔
2385
}
2386

2387
//==============================================================================
2388
// Helper functions for the C API
2389
//==============================================================================
2390

2391
int check_mesh(int32_t index)
5,800✔
2392
{
2393
  if (index < 0 || index >= model::meshes.size()) {
5,800!
UNCOV
2394
    set_errmsg("Index in meshes array is out of bounds.");
×
UNCOV
2395
    return OPENMC_E_OUT_OF_BOUNDS;
×
2396
  }
2397
  return 0;
5,800✔
2398
}
2399

2400
template<class T>
2401
int check_mesh_type(int32_t index)
1,000✔
2402
{
2403
  if (int err = check_mesh(index))
1,000!
UNCOV
2404
    return err;
×
2405

2406
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,000!
2407
  if (!mesh) {
1,000!
UNCOV
2408
    set_errmsg("This function is not valid for input mesh.");
×
UNCOV
2409
    return OPENMC_E_INVALID_TYPE;
×
2410
  }
2411
  return 0;
1,000✔
2412
}
2413

2414
template<class T>
2415
bool is_mesh_type(int32_t index)
2416
{
2417
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2418
  return mesh;
2419
}
2420

2421
//==============================================================================
2422
// C API functions
2423
//==============================================================================
2424

2425
// Return the type of mesh as a C string
2426
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,340✔
2427
{
2428
  if (int err = check_mesh(index))
1,340!
UNCOV
2429
    return err;
×
2430

2431
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
1,340✔
2432

2433
  return 0;
1,340✔
2434
}
2435

2436
//! Extend the meshes array by n elements
2437
extern "C" int openmc_extend_meshes(
230✔
2438
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2439
{
2440
  if (index_start)
230!
2441
    *index_start = model::meshes.size();
230✔
2442
  std::string mesh_type;
230✔
2443

2444
  for (int i = 0; i < n; ++i) {
460✔
2445
    if (RegularMesh::mesh_type == type) {
230✔
2446
      model::meshes.push_back(make_unique<RegularMesh>());
150✔
2447
    } else if (RectilinearMesh::mesh_type == type) {
80✔
2448
      model::meshes.push_back(make_unique<RectilinearMesh>());
40✔
2449
    } else if (CylindricalMesh::mesh_type == type) {
40✔
2450
      model::meshes.push_back(make_unique<CylindricalMesh>());
20✔
2451
    } else if (SphericalMesh::mesh_type == type) {
20!
2452
      model::meshes.push_back(make_unique<SphericalMesh>());
20✔
2453
    } else {
UNCOV
2454
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2455
    }
2456
  }
2457
  if (index_end)
230!
UNCOV
2458
    *index_end = model::meshes.size() - 1;
×
2459

2460
  return 0;
230✔
2461
}
230✔
2462

2463
//! Adds a new unstructured mesh to OpenMC
UNCOV
2464
extern "C" int openmc_add_unstructured_mesh(
×
2465
  const char filename[], const char library[], int* id)
2466
{
2467
  std::string lib_name(library);
×
UNCOV
2468
  std::string mesh_file(filename);
×
UNCOV
2469
  bool valid_lib = false;
×
2470

2471
#ifdef OPENMC_DAGMC_ENABLED
2472
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2473
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2474
    valid_lib = true;
2475
  }
2476
#endif
2477

2478
#ifdef OPENMC_LIBMESH_ENABLED
2479
  if (lib_name == LibMesh::mesh_lib_type) {
×
2480
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2481
    valid_lib = true;
2482
  }
2483
#endif
2484

UNCOV
2485
  if (!valid_lib) {
×
UNCOV
2486
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2487
                           "by this build of OpenMC",
2488
      lib_name));
UNCOV
2489
    return OPENMC_E_INVALID_ARGUMENT;
×
2490
  }
2491

2492
  // auto-assign new ID
UNCOV
2493
  model::meshes.back()->set_id(-1);
×
2494
  *id = model::meshes.back()->id_;
×
2495

UNCOV
2496
  return 0;
×
UNCOV
2497
}
×
2498

2499
//! Return the index in the meshes array of a mesh with a given ID
2500
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
390✔
2501
{
2502
  auto pair = model::mesh_map.find(id);
390✔
2503
  if (pair == model::mesh_map.end()) {
390!
UNCOV
2504
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
UNCOV
2505
    return OPENMC_E_INVALID_ID;
×
2506
  }
2507
  *index = pair->second;
390✔
2508
  return 0;
390✔
2509
}
2510

2511
//! Return the ID of a mesh
2512
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,550✔
2513
{
2514
  if (int err = check_mesh(index))
2,550!
UNCOV
2515
    return err;
×
2516
  *id = model::meshes[index]->id_;
2,550✔
2517
  return 0;
2,550✔
2518
}
2519

2520
//! Set the ID of a mesh
2521
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
230✔
2522
{
2523
  if (int err = check_mesh(index))
230!
UNCOV
2524
    return err;
×
2525
  model::meshes[index]->id_ = id;
230✔
2526
  model::mesh_map[id] = index;
230✔
2527
  return 0;
230✔
2528
}
2529

2530
//! Get the number of elements in a mesh
2531
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
250✔
2532
{
2533
  if (int err = check_mesh(index))
250!
UNCOV
2534
    return err;
×
2535
  *n = model::meshes[index]->n_bins();
250✔
2536
  return 0;
250✔
2537
}
2538

2539
//! Get the volume of each element in the mesh
2540
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
80✔
2541
{
2542
  if (int err = check_mesh(index))
80!
UNCOV
2543
    return err;
×
2544
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
880✔
2545
    volumes[i] = model::meshes[index]->volume(i);
800✔
2546
  }
2547
  return 0;
80✔
2548
}
2549

2550
//! Get the bounding box of a mesh
2551
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
140✔
2552
{
2553
  if (int err = check_mesh(index))
140!
UNCOV
2554
    return err;
×
2555

2556
  BoundingBox bbox = model::meshes[index]->bounding_box();
140✔
2557

2558
  // set lower left corner values
2559
  ll[0] = bbox.min.x;
140✔
2560
  ll[1] = bbox.min.y;
140✔
2561
  ll[2] = bbox.min.z;
140✔
2562

2563
  // set upper right corner values
2564
  ur[0] = bbox.max.x;
140✔
2565
  ur[1] = bbox.max.y;
140✔
2566
  ur[2] = bbox.max.z;
140✔
2567
  return 0;
140✔
2568
}
2569

2570
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
170✔
2571
  int nz, int table_size, int32_t* materials, double* volumes, double* bboxes)
2572
{
2573
  if (int err = check_mesh(index))
170!
UNCOV
2574
    return err;
×
2575

2576
  try {
2577
    model::meshes[index]->material_volumes(
170✔
2578
      nx, ny, nz, table_size, materials, volumes, bboxes);
2579
  } catch (const std::exception& e) {
10!
2580
    set_errmsg(e.what());
10✔
2581
    if (starts_with(e.what(), "Mesh")) {
10!
2582
      return OPENMC_E_GEOMETRY;
10✔
2583
    } else {
UNCOV
2584
      return OPENMC_E_ALLOCATE;
×
2585
    }
2586
  }
10✔
2587

2588
  return 0;
160✔
2589
}
2590

2591
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
40✔
2592
  Position width, int basis, int* pixels, int32_t* data)
2593
{
2594
  if (int err = check_mesh(index))
40!
UNCOV
2595
    return err;
×
2596
  const auto& mesh = model::meshes[index].get();
40✔
2597

2598
  int pixel_width = pixels[0];
40✔
2599
  int pixel_height = pixels[1];
40✔
2600

2601
  // get pixel size
2602
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
40✔
2603
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
40✔
2604

2605
  // setup basis indices and initial position centered on pixel
2606
  int in_i, out_i;
2607
  Position xyz = origin;
40✔
2608
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2609
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
40✔
2610
  switch (basis_enum) {
40!
2611
  case PlotBasis::xy:
40✔
2612
    in_i = 0;
40✔
2613
    out_i = 1;
40✔
2614
    break;
40✔
2615
  case PlotBasis::xz:
×
2616
    in_i = 0;
×
2617
    out_i = 2;
×
2618
    break;
×
2619
  case PlotBasis::yz:
×
2620
    in_i = 1;
×
2621
    out_i = 2;
×
2622
    break;
×
UNCOV
2623
  default:
×
UNCOV
2624
    UNREACHABLE();
×
2625
  }
2626

2627
  // set initial position
2628
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
40✔
2629
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
40✔
2630

2631
#pragma omp parallel
24✔
2632
  {
2633
    Position r = xyz;
16✔
2634

2635
#pragma omp for
2636
    for (int y = 0; y < pixel_height; y++) {
336✔
2637
      r[out_i] = xyz[out_i] - out_pixel * y;
320✔
2638
      for (int x = 0; x < pixel_width; x++) {
6,720✔
2639
        r[in_i] = xyz[in_i] + in_pixel * x;
6,400✔
2640
        data[pixel_width * y + x] = mesh->get_bin(r);
6,400✔
2641
      }
2642
    }
2643
  }
2644

2645
  return 0;
40✔
2646
}
2647

2648
//! Get the dimension of a regular mesh
2649
extern "C" int openmc_regular_mesh_get_dimension(
10✔
2650
  int32_t index, int** dims, int* n)
2651
{
2652
  if (int err = check_mesh_type<RegularMesh>(index))
10!
UNCOV
2653
    return err;
×
2654
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
10!
2655
  *dims = mesh->shape_.data();
10✔
2656
  *n = mesh->n_dimension_;
10✔
2657
  return 0;
10✔
2658
}
2659

2660
//! Set the dimension of a regular mesh
2661
extern "C" int openmc_regular_mesh_set_dimension(
170✔
2662
  int32_t index, int n, const int* dims)
2663
{
2664
  if (int err = check_mesh_type<RegularMesh>(index))
170!
UNCOV
2665
    return err;
×
2666
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
170!
2667

2668
  // Copy dimension
2669
  mesh->n_dimension_ = n;
170✔
2670
  std::copy(dims, dims + n, mesh->shape_.begin());
170✔
2671
  return 0;
170✔
2672
}
2673

2674
//! Get the regular mesh parameters
2675
extern "C" int openmc_regular_mesh_get_params(
190✔
2676
  int32_t index, double** ll, double** ur, double** width, int* n)
2677
{
2678
  if (int err = check_mesh_type<RegularMesh>(index))
190!
UNCOV
2679
    return err;
×
2680
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
190!
2681

2682
  if (m->lower_left_.empty()) {
190!
UNCOV
2683
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2684
    return OPENMC_E_ALLOCATE;
×
2685
  }
2686

2687
  *ll = m->lower_left_.data();
190✔
2688
  *ur = m->upper_right_.data();
190✔
2689
  *width = m->width_.data();
190✔
2690
  *n = m->n_dimension_;
190✔
2691
  return 0;
190✔
2692
}
2693

2694
//! Set the regular mesh parameters
2695
extern "C" int openmc_regular_mesh_set_params(
200✔
2696
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2697
{
2698
  if (int err = check_mesh_type<RegularMesh>(index))
200!
UNCOV
2699
    return err;
×
2700
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
200!
2701

2702
  if (m->n_dimension_ == -1) {
200!
UNCOV
2703
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
UNCOV
2704
    return OPENMC_E_UNASSIGNED;
×
2705
  }
2706

2707
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
200✔
2708
  if (ll && ur) {
200✔
2709
    m->lower_left_ = tensor::Tensor<double>(ll, n);
180✔
2710
    m->upper_right_ = tensor::Tensor<double>(ur, n);
180✔
2711
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_shape_tensor();
180✔
2712
  } else if (ll && width) {
20!
2713
    m->lower_left_ = tensor::Tensor<double>(ll, n);
10✔
2714
    m->width_ = tensor::Tensor<double>(width, n);
10✔
2715
    m->upper_right_ = m->lower_left_ + m->get_shape_tensor() * m->width_;
10✔
2716
  } else if (ur && width) {
10!
2717
    m->upper_right_ = tensor::Tensor<double>(ur, n);
10✔
2718
    m->width_ = tensor::Tensor<double>(width, n);
10✔
2719
    m->lower_left_ = m->upper_right_ - m->get_shape_tensor() * m->width_;
10✔
2720
  } else {
UNCOV
2721
    set_errmsg("At least two parameters must be specified.");
×
UNCOV
2722
    return OPENMC_E_INVALID_ARGUMENT;
×
2723
  }
2724

2725
  // Set material volumes
2726

2727
  // TODO: incorporate this into method in RegularMesh that can be called from
2728
  // here and from constructor
2729
  m->volume_frac_ = 1.0 / m->get_shape_tensor().prod();
200✔
2730
  m->element_volume_ = 1.0;
200✔
2731
  for (int i = 0; i < m->n_dimension_; i++) {
800✔
2732
    m->element_volume_ *= m->width_[i];
600✔
2733
  }
2734

2735
  return 0;
200✔
2736
}
200✔
2737

2738
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2739
template<class C>
2740
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
80✔
2741
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2742
  const int nz)
2743
{
2744
  if (int err = check_mesh_type<C>(index))
80!
UNCOV
2745
    return err;
×
2746

2747
  C* m = dynamic_cast<C*>(model::meshes[index].get());
80!
2748

2749
  m->n_dimension_ = 3;
80✔
2750

2751
  m->grid_[0].reserve(nx);
80✔
2752
  m->grid_[1].reserve(ny);
80✔
2753
  m->grid_[2].reserve(nz);
80✔
2754

2755
  for (int i = 0; i < nx; i++) {
520✔
2756
    m->grid_[0].push_back(grid_x[i]);
440✔
2757
  }
2758
  for (int i = 0; i < ny; i++) {
310✔
2759
    m->grid_[1].push_back(grid_y[i]);
230✔
2760
  }
2761
  for (int i = 0; i < nz; i++) {
290✔
2762
    m->grid_[2].push_back(grid_z[i]);
210✔
2763
  }
2764

2765
  int err = m->set_grid();
80✔
2766
  return err;
80✔
2767
}
2768

2769
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2770
template<class C>
2771
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
350✔
2772
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2773
{
2774
  if (int err = check_mesh_type<C>(index))
350!
UNCOV
2775
    return err;
×
2776
  C* m = dynamic_cast<C*>(model::meshes[index].get());
350!
2777

2778
  if (m->lower_left_.empty()) {
350!
UNCOV
2779
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2780
    return OPENMC_E_ALLOCATE;
×
2781
  }
2782

2783
  *grid_x = m->grid_[0].data();
350✔
2784
  *nx = m->grid_[0].size();
350✔
2785
  *grid_y = m->grid_[1].data();
350✔
2786
  *ny = m->grid_[1].size();
350✔
2787
  *grid_z = m->grid_[2].data();
350✔
2788
  *nz = m->grid_[2].size();
350✔
2789

2790
  return 0;
350✔
2791
}
2792

2793
//! Get the rectilinear mesh grid
2794
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
130✔
2795
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2796
{
2797
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
130✔
2798
    index, grid_x, nx, grid_y, ny, grid_z, nz);
130✔
2799
}
2800

2801
//! Set the rectilienar mesh parameters
2802
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
40✔
2803
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2804
  const double* grid_z, const int nz)
2805
{
2806
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
40✔
2807
    index, grid_x, nx, grid_y, ny, grid_z, nz);
40✔
2808
}
2809

2810
//! Get the cylindrical mesh grid
2811
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
110✔
2812
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2813
{
2814
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
110✔
2815
    index, grid_x, nx, grid_y, ny, grid_z, nz);
110✔
2816
}
2817

2818
//! Set the cylindrical mesh parameters
2819
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
20✔
2820
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2821
  const double* grid_z, const int nz)
2822
{
2823
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
20✔
2824
    index, grid_x, nx, grid_y, ny, grid_z, nz);
20✔
2825
}
2826

2827
//! Get the spherical mesh grid
2828
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
110✔
2829
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2830
{
2831

2832
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
110✔
2833
    index, grid_x, nx, grid_y, ny, grid_z, nz);
110✔
2834
  ;
2835
}
2836

2837
//! Set the spherical mesh parameters
2838
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
20✔
2839
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2840
  const double* grid_z, const int nz)
2841
{
2842
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
20✔
2843
    index, grid_x, nx, grid_y, ny, grid_z, nz);
20✔
2844
}
2845

2846
#ifdef OPENMC_DAGMC_ENABLED
2847

2848
const std::string MOABMesh::mesh_lib_type = "moab";
2849

2850
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
2851
{
2852
  initialize();
24✔
2853
}
24✔
2854

2855
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
2856
{
2857
  initialize();
×
2858
}
2859

2860
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2861
  : UnstructuredMesh()
×
2862
{
2863
  n_dimension_ = 3;
2864
  filename_ = filename;
×
2865
  set_length_multiplier(length_multiplier);
×
2866
  initialize();
×
2867
}
2868

2869
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
2870
{
2871
  mbi_ = external_mbi;
1✔
2872
  filename_ = "unknown (external file)";
1✔
2873
  this->initialize();
1✔
2874
}
1✔
2875

2876
void MOABMesh::initialize()
25✔
2877
{
2878

2879
  // Create the MOAB interface and load data from file
2880
  this->create_interface();
25✔
2881

2882
  // Initialise MOAB error code
2883
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
2884

2885
  // Set the dimension
2886
  n_dimension_ = 3;
25✔
2887

2888
  // set member range of tetrahedral entities
2889
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
2890
  if (rval != moab::MB_SUCCESS) {
25!
2891
    fatal_error("Failed to get all tetrahedral elements");
2892
  }
2893

2894
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
2895
    warning("Non-tetrahedral elements found in unstructured "
×
2896
            "mesh file: " +
2897
            filename_);
2898
  }
2899

2900
  // set member range of vertices
2901
  int vertex_dim = 0;
25✔
2902
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
2903
  if (rval != moab::MB_SUCCESS) {
25!
2904
    fatal_error("Failed to get all vertex handles");
2905
  }
2906

2907
  // make an entity set for all tetrahedra
2908
  // this is used for convenience later in output
2909
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
2910
  if (rval != moab::MB_SUCCESS) {
25!
2911
    fatal_error("Failed to create an entity set for the tetrahedral elements");
2912
  }
2913

2914
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
2915
  if (rval != moab::MB_SUCCESS) {
25!
2916
    fatal_error("Failed to add tetrahedra to an entity set.");
2917
  }
2918

2919
  if (length_multiplier_ > 0.0) {
25!
2920
    // get the connectivity of all tets
2921
    moab::Range adj;
×
2922
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
2923
    if (rval != moab::MB_SUCCESS) {
×
2924
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
2925
    }
2926
    // scale all vertex coords by multiplier (done individually so not all
2927
    // coordinates are in memory twice at once)
2928
    for (auto vert : adj) {
×
2929
      // retrieve coords
2930
      std::array<double, 3> coord;
2931
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
2932
      if (rval != moab::MB_SUCCESS) {
×
2933
        fatal_error("Could not get coordinates of vertex.");
2934
      }
2935
      // scale coords
2936
      for (auto& c : coord) {
×
2937
        c *= length_multiplier_;
2938
      }
2939
      // set new coords
2940
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2941
      if (rval != moab::MB_SUCCESS) {
×
2942
        fatal_error("Failed to set new vertex coordinates");
2943
      }
2944
    }
2945
  }
2946

2947
  // Determine bounds of mesh
2948
  this->determine_bounds();
25✔
2949
}
25✔
2950

2951
void MOABMesh::prepare_for_point_location()
21✔
2952
{
2953
  // if the KDTree has already been constructed, do nothing
2954
  if (kdtree_)
21!
2955
    return;
2956

2957
  // build acceleration data structures
2958
  compute_barycentric_data(ehs_);
21✔
2959
  build_kdtree(ehs_);
21✔
2960
}
2961

2962
void MOABMesh::create_interface()
25✔
2963
{
2964
  // Do not create a MOAB instance if one is already in memory
2965
  if (mbi_)
25✔
2966
    return;
1✔
2967

2968
  // create MOAB instance
2969
  mbi_ = std::make_shared<moab::Core>();
24✔
2970

2971
  // load unstructured mesh file
2972
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
2973
  if (rval != moab::MB_SUCCESS) {
24!
2974
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
2975
  }
2976
}
2977

2978
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
2979
{
2980
  moab::Range all_tris;
21✔
2981
  int adj_dim = 2;
21✔
2982
  write_message("Getting tet adjacencies...", 7);
21✔
2983
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
2984
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2985
  if (rval != moab::MB_SUCCESS) {
21!
2986
    fatal_error("Failed to get adjacent triangles for tets");
2987
  }
2988

2989
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
2990
    warning("Non-triangle elements found in tet adjacencies in "
×
2991
            "unstructured mesh file: " +
2992
            filename_);
×
2993
  }
2994

2995
  // combine into one range
2996
  moab::Range all_tets_and_tris;
21✔
2997
  all_tets_and_tris.merge(all_tets);
21✔
2998
  all_tets_and_tris.merge(all_tris);
21✔
2999

3000
  // create a kd-tree instance
3001
  write_message(
21✔
3002
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
3003
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
3004

3005
  // Determine what options to use
3006
  std::ostringstream options_stream;
21✔
3007
  if (options_.empty()) {
21✔
3008
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
3009
  } else {
3010
    options_stream << options_;
16✔
3011
  }
3012
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
3013

3014
  // Build the k-d tree
3015
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
3016
  if (rval != moab::MB_SUCCESS) {
21!
3017
    fatal_error("Failed to construct KDTree for the "
3018
                "unstructured mesh file: " +
3019
                filename_);
×
3020
  }
3021
}
21✔
3022

3023
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
3024
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
3025
{
3026
  hits.clear();
1,543,584✔
3027

3028
  moab::ErrorCode rval;
3029
  vector<moab::EntityHandle> tris;
1,543,584✔
3030
  // get all intersections with triangles in the tet mesh
3031
  // (distances are relative to the start point, not the previous
3032
  // intersection)
3033
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
3034
    dir.array(), start.array(), tris, hits, 0, track_len);
3035
  if (rval != moab::MB_SUCCESS) {
1,543,584!
3036
    fatal_error(
3037
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
3038
  }
3039

3040
  // remove duplicate intersection distances
3041
  std::unique(hits.begin(), hits.end());
1,543,584✔
3042

3043
  // sorts by first component of std::pair by default
3044
  std::sort(hits.begin(), hits.end());
1,543,584✔
3045
}
1,543,584✔
3046

3047
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
3048
  vector<int>& bins, vector<double>& lengths) const
3049
{
3050
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
3051
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
3052
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
3053
  dir.normalize();
1,543,584✔
3054

3055
  double track_len = (end - start).length();
1,543,584✔
3056
  if (track_len == 0.0)
1,543,584!
3057
    return;
721,692✔
3058

3059
  start -= TINY_BIT * dir;
1,543,584✔
3060
  end += TINY_BIT * dir;
1,543,584✔
3061

3062
  vector<double> hits;
1,543,584✔
3063
  intersect_track(start, dir, track_len, hits);
1,543,584✔
3064

3065
  bins.clear();
1,543,584✔
3066
  lengths.clear();
1,543,584✔
3067

3068
  // if there are no intersections the track may lie entirely
3069
  // within a single tet. If this is the case, apply entire
3070
  // score to that tet and return.
3071
  if (hits.size() == 0) {
1,543,584✔
3072
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
3073
    int bin = this->get_bin(midpoint);
721,692✔
3074
    if (bin != -1) {
721,692✔
3075
      bins.push_back(bin);
242,866✔
3076
      lengths.push_back(1.0);
242,866✔
3077
    }
3078
    return;
721,692✔
3079
  }
3080

3081
  // for each segment in the set of tracks, try to look up a tet
3082
  // at the midpoint of the segment
3083
  Position current = r0;
821,892✔
3084
  double last_dist = 0.0;
821,892✔
3085
  for (const auto& hit : hits) {
5,516,161✔
3086
    // get the segment length
3087
    double segment_length = hit - last_dist;
4,694,269✔
3088
    last_dist = hit;
4,694,269✔
3089
    // find the midpoint of this segment
3090
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
3091
    // try to find a tet for this position
3092
    int bin = this->get_bin(midpoint);
4,694,269✔
3093

3094
    // determine the start point for this segment
3095
    current = r0 + u * hit;
4,694,269✔
3096

3097
    if (bin == -1) {
4,694,269✔
3098
      continue;
20,522✔
3099
    }
3100

3101
    bins.push_back(bin);
4,673,747✔
3102
    lengths.push_back(segment_length / track_len);
4,673,747✔
3103
  }
3104

3105
  // tally remaining portion of track after last hit if
3106
  // the last segment of the track is in the mesh but doesn't
3107
  // reach the other side of the tet
3108
  if (hits.back() < track_len) {
821,892!
3109
    Position segment_start = r0 + u * hits.back();
821,892✔
3110
    double segment_length = track_len - hits.back();
821,892✔
3111
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
3112
    int bin = this->get_bin(midpoint);
821,892✔
3113
    if (bin != -1) {
821,892✔
3114
      bins.push_back(bin);
766,509✔
3115
      lengths.push_back(segment_length / track_len);
766,509✔
3116
    }
3117
  }
3118
};
1,543,584✔
3119

3120
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,172✔
3121
{
3122
  moab::CartVect pos(r.x, r.y, r.z);
7,317,172✔
3123
  // find the leaf of the kd-tree for this position
3124
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,172✔
3125
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,172✔
3126
  if (rval != moab::MB_SUCCESS) {
7,317,172✔
3127
    return 0;
1,011,897✔
3128
  }
3129

3130
  // retrieve the tet elements of this leaf
3131
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,275✔
3132
  moab::Range tets;
6,305,275✔
3133
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,275✔
3134
  if (rval != moab::MB_SUCCESS) {
6,305,275!
3135
    warning("MOAB error finding tets.");
×
3136
  }
3137

3138
  // loop over the tets in this leaf, returning the containing tet if found
3139
  for (const auto& tet : tets) {
260,209,886✔
3140
    if (point_in_tet(pos, tet)) {
260,207,039✔
3141
      return tet;
6,302,428✔
3142
    }
3143
  }
3144

3145
  // if no tet is found, return an invalid handle
3146
  return 0;
2,847✔
3147
}
7,317,172✔
3148

3149
double MOABMesh::volume(int bin) const
167,880✔
3150
{
3151
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3152
}
3153

3154
std::string MOABMesh::library() const
34✔
3155
{
3156
  return mesh_lib_type;
34✔
3157
}
3158

3159
// Sample position within a tet for MOAB type tets
3160
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3161
{
3162

3163
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3164

3165
  // Get vertex coordinates for MOAB tet
3166
  const moab::EntityHandle* conn1;
3167
  int conn1_size;
3168
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3169
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3170
    fatal_error(fmt::format(
×
3171
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3172
      conn1_size));
3173
  }
3174
  moab::CartVect p[4];
1,002,050✔
3175
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3176
  if (rval != moab::MB_SUCCESS) {
200,410!
3177
    fatal_error("Failed to get tet coords");
3178
  }
3179

3180
  std::array<Position, 4> tet_verts;
200,410✔
3181
  for (int i = 0; i < 4; i++) {
1,002,050✔
3182
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3183
  }
3184
  // Samples position within tet using Barycentric stuff
3185
  return this->sample_tet(tet_verts, seed);
400,820✔
3186
}
3187

3188
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3189
{
3190
  vector<moab::EntityHandle> conn;
167,880✔
3191
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3192
  if (rval != moab::MB_SUCCESS) {
167,880!
3193
    fatal_error("Failed to get tet connectivity");
3194
  }
3195

3196
  moab::CartVect p[4];
839,400✔
3197
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3198
  if (rval != moab::MB_SUCCESS) {
167,880!
3199
    fatal_error("Failed to get tet coords");
3200
  }
3201

3202
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
335,760✔
3203
}
167,880✔
3204

3205
int MOABMesh::get_bin(Position r) const
7,317,172✔
3206
{
3207
  moab::EntityHandle tet = get_tet(r);
7,317,172✔
3208
  if (tet == 0) {
7,317,172✔
3209
    return -1;
1,014,744✔
3210
  } else {
3211
    return get_bin_from_ent_handle(tet);
6,302,428✔
3212
  }
3213
}
3214

3215
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3216
{
3217
  moab::ErrorCode rval;
3218

3219
  baryc_data_.clear();
21✔
3220
  baryc_data_.resize(tets.size());
21✔
3221

3222
  // compute the barycentric data for each tet element
3223
  // and store it as a 3x3 matrix
3224
  for (auto& tet : tets) {
239,757✔
3225
    vector<moab::EntityHandle> verts;
239,736✔
3226
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3227
    if (rval != moab::MB_SUCCESS) {
239,736!
3228
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3229
    }
3230

3231
    moab::CartVect p[4];
1,198,680✔
3232
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
239,736✔
3233
    if (rval != moab::MB_SUCCESS) {
239,736!
3234
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
×
3235
    }
3236

3237
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
239,736✔
3238

3239
    // invert now to avoid this cost later
3240
    a = a.transpose().inverse();
239,736✔
3241
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3242
  }
239,736✔
3243
}
21✔
3244

3245
bool MOABMesh::point_in_tet(
260,207,039✔
3246
  const moab::CartVect& r, moab::EntityHandle tet) const
3247
{
3248

3249
  moab::ErrorCode rval;
3250

3251
  // get tet vertices
3252
  vector<moab::EntityHandle> verts;
260,207,039✔
3253
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,207,039✔
3254
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3255
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3256
    return false;
3257
  }
3258

3259
  // first vertex is used as a reference point for the barycentric data -
3260
  // retrieve its coordinates
3261
  moab::CartVect p_zero;
260,207,039✔
3262
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,207,039✔
3263
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3264
    warning("Failed to get coordinates of a vertex in "
×
3265
            "unstructured mesh: " +
3266
            filename_);
×
3267
    return false;
3268
  }
3269

3270
  // look up barycentric data
3271
  int idx = get_bin_from_ent_handle(tet);
260,207,039✔
3272
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,207,039✔
3273

3274
  moab::CartVect bary_coords = a_inv * (r - p_zero);
260,207,039✔
3275

3276
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
421,415,065✔
3277
          bary_coords[2] >= 0.0 &&
443,103,099✔
3278
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
281,895,073✔
3279
}
260,207,039✔
3280

3281
int MOABMesh::get_bin_from_index(int idx) const
3282
{
3283
  if (idx >= n_bins()) {
×
3284
    fatal_error(fmt::format("Invalid bin index: {}", idx));
×
3285
  }
3286
  return ehs_[idx] - ehs_[0];
3287
}
3288

3289
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3290
{
3291
  int bin = get_bin(r);
3292
  *in_mesh = bin != -1;
3293
  return bin;
3294
}
3295

3296
int MOABMesh::get_index_from_bin(int bin) const
3297
{
3298
  return bin;
3299
}
3300

3301
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3302
  Position plot_ll, Position plot_ur) const
3303
{
3304
  // TODO: Implement mesh lines
3305
  return {};
3306
}
3307

3308
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3309
{
3310
  int idx = vert - verts_[0];
815,520✔
3311
  if (idx >= n_vertices()) {
815,520!
3312
    fatal_error(
3313
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3314
  }
3315
  return idx;
815,520✔
3316
}
3317

3318
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,749,203✔
3319
{
3320
  int bin = eh - ehs_[0];
266,749,203✔
3321
  if (bin >= n_bins()) {
266,749,203!
3322
    fatal_error(fmt::format("Invalid bin: {}", bin));
×
3323
  }
3324
  return bin;
266,749,203✔
3325
}
3326

3327
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3328
{
3329
  if (bin >= n_bins()) {
572,170!
3330
    fatal_error(fmt::format("Invalid bin index: ", bin));
×
3331
  }
3332
  return ehs_[0] + bin;
572,170✔
3333
}
3334

3335
int MOABMesh::n_bins() const
267,525,326✔
3336
{
3337
  return ehs_.size();
267,525,326✔
3338
}
3339

3340
int MOABMesh::n_surface_bins() const
3341
{
3342
  // collect all triangles in the set of tets for this mesh
3343
  moab::Range tris;
×
3344
  moab::ErrorCode rval;
3345
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3346
  if (rval != moab::MB_SUCCESS) {
×
3347
    warning("Failed to get all triangles in the mesh instance");
×
3348
    return -1;
3349
  }
3350
  return 2 * tris.size();
×
3351
}
3352

3353
Position MOABMesh::centroid(int bin) const
3354
{
3355
  moab::ErrorCode rval;
3356

3357
  auto tet = this->get_ent_handle_from_bin(bin);
×
3358

3359
  // look up the tet connectivity
3360
  vector<moab::EntityHandle> conn;
3361
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3362
  if (rval != moab::MB_SUCCESS) {
×
3363
    warning("Failed to get connectivity of a mesh element.");
×
3364
    return {};
3365
  }
3366

3367
  // get the coordinates
3368
  vector<moab::CartVect> coords(conn.size());
×
3369
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3370
  if (rval != moab::MB_SUCCESS) {
×
3371
    warning("Failed to get the coordinates of a mesh element.");
×
3372
    return {};
3373
  }
3374

3375
  // compute the centroid of the element vertices
3376
  moab::CartVect centroid(0.0, 0.0, 0.0);
3377
  for (const auto& coord : coords) {
×
3378
    centroid += coord;
3379
  }
3380
  centroid /= double(coords.size());
3381

3382
  return {centroid[0], centroid[1], centroid[2]};
3383
}
3384

3385
int MOABMesh::n_vertices() const
845,874✔
3386
{
3387
  return verts_.size();
845,874✔
3388
}
3389

3390
Position MOABMesh::vertex(int id) const
86,227✔
3391
{
3392

3393
  moab::ErrorCode rval;
3394

3395
  moab::EntityHandle vert = verts_[id];
86,227✔
3396

3397
  moab::CartVect coords;
86,227✔
3398
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3399
  if (rval != moab::MB_SUCCESS) {
86,227!
3400
    fatal_error("Failed to get the coordinates of a vertex.");
3401
  }
3402

3403
  return {coords[0], coords[1], coords[2]};
172,454✔
3404
}
3405

3406
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3407
{
3408
  moab::ErrorCode rval;
3409

3410
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3411

3412
  // look up the tet connectivity
3413
  vector<moab::EntityHandle> conn;
203,880✔
3414
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3415
  if (rval != moab::MB_SUCCESS) {
203,880!
3416
    fatal_error("Failed to get connectivity of a mesh element.");
3417
    return {};
3418
  }
3419

3420
  std::vector<int> verts(4);
203,880✔
3421
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3422
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3423
  }
3424

3425
  return verts;
203,880✔
3426
}
203,880✔
3427

3428
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3429
  std::string score) const
3430
{
3431
  moab::ErrorCode rval;
3432
  // add a tag to the mesh
3433
  // all scores are treated as a single value
3434
  // with an uncertainty
3435
  moab::Tag value_tag;
3436

3437
  // create the value tag if not present and get handle
3438
  double default_val = 0.0;
3439
  auto val_string = score + "_mean";
×
3440
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3441
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3442
  if (rval != moab::MB_SUCCESS) {
×
3443
    auto msg =
3444
      fmt::format("Could not create or retrieve the value tag for the score {}"
3445
                  " on unstructured mesh {}",
3446
        score, id_);
×
3447
    fatal_error(msg);
3448
  }
3449

3450
  // create the std dev tag if not present and get handle
3451
  moab::Tag error_tag;
3452
  std::string err_string = score + "_std_dev";
×
3453
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3454
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3455
  if (rval != moab::MB_SUCCESS) {
×
3456
    auto msg =
3457
      fmt::format("Could not create or retrieve the error tag for the score {}"
3458
                  " on unstructured mesh {}",
3459
        score, id_);
×
3460
    fatal_error(msg);
3461
  }
3462

3463
  // return the populated tag handles
3464
  return {value_tag, error_tag};
3465
}
3466

3467
void MOABMesh::add_score(const std::string& score)
3468
{
3469
  auto score_tags = get_score_tags(score);
×
3470
  tag_names_.push_back(score);
×
3471
}
3472

3473
void MOABMesh::remove_scores()
3474
{
3475
  for (const auto& name : tag_names_) {
×
3476
    auto value_name = name + "_mean";
×
3477
    moab::Tag tag;
3478
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3479
    if (rval != moab::MB_SUCCESS)
×
3480
      return;
3481

3482
    rval = mbi_->tag_delete(tag);
×
3483
    if (rval != moab::MB_SUCCESS) {
×
3484
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3485
                             " on unstructured mesh {}",
3486
        name, id_);
×
3487
      fatal_error(msg);
3488
    }
3489

3490
    auto std_dev_name = name + "_std_dev";
×
3491
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3492
    if (rval != moab::MB_SUCCESS) {
×
3493
      auto msg =
3494
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3495
                    " on unstructured mesh {}",
3496
          name, id_);
×
3497
    }
3498

3499
    rval = mbi_->tag_delete(tag);
×
3500
    if (rval != moab::MB_SUCCESS) {
×
3501
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3502
                             " on unstructured mesh {}",
3503
        name, id_);
×
3504
      fatal_error(msg);
3505
    }
3506
  }
×
3507
  tag_names_.clear();
3508
}
3509

3510
void MOABMesh::set_score_data(const std::string& score,
3511
  const vector<double>& values, const vector<double>& std_dev)
3512
{
3513
  auto score_tags = this->get_score_tags(score);
×
3514

3515
  moab::ErrorCode rval;
3516
  // set the score value
3517
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
×
3518
  if (rval != moab::MB_SUCCESS) {
×
3519
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3520
                           "on unstructured mesh {}",
3521
      score, id_);
×
3522
    warning(msg);
×
3523
  }
3524

3525
  // set the error value
3526
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
×
3527
  if (rval != moab::MB_SUCCESS) {
×
3528
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3529
                           "on unstructured mesh {}",
3530
      score, id_);
×
3531
    warning(msg);
×
3532
  }
3533
}
3534

3535
void MOABMesh::write(const std::string& base_filename) const
3536
{
3537
  // add extension to the base name
3538
  auto filename = base_filename + ".vtk";
×
3539
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3540
  filename = settings::path_output + filename;
×
3541

3542
  // write the tetrahedral elements of the mesh only
3543
  // to avoid clutter from zero-value data on other
3544
  // elements during visualization
3545
  moab::ErrorCode rval;
3546
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3547
  if (rval != moab::MB_SUCCESS) {
×
3548
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3549
    warning(msg);
×
3550
  }
3551
}
3552

3553
#endif
3554

3555
#ifdef OPENMC_LIBMESH_ENABLED
3556

3557
const std::string LibMesh::mesh_lib_type = "libmesh";
3558

3559
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3560
{
3561
  // filename_ and length_multiplier_ will already be set by the
3562
  // UnstructuredMesh constructor
3563
  set_mesh_pointer_from_filename(filename_);
23✔
3564
  set_length_multiplier(length_multiplier_);
23✔
3565
  initialize();
23✔
3566
}
23✔
3567

3568
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3569
{
3570
  // filename_ and length_multiplier_ will already be set by the
3571
  // UnstructuredMesh constructor
3572
  set_mesh_pointer_from_filename(filename_);
×
3573
  set_length_multiplier(length_multiplier_);
×
3574
  initialize();
×
3575
}
3576

3577
// create the mesh from a pointer to a libMesh Mesh
3578
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3579
{
3580
  if (!input_mesh.is_replicated()) {
×
3581
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3582
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3583
  }
3584

3585
  m_ = &input_mesh;
3586
  set_length_multiplier(length_multiplier);
×
3587
  initialize();
×
3588
}
3589

3590
// create the mesh from an input file
3591
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3592
{
3593
  n_dimension_ = 3;
3594
  set_mesh_pointer_from_filename(filename);
×
3595
  set_length_multiplier(length_multiplier);
×
3596
  initialize();
×
3597
}
3598

3599
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3600
{
3601
  filename_ = filename;
23✔
3602
  unique_m_ =
3603
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3604
  m_ = unique_m_.get();
23✔
3605
  m_->read(filename_);
23✔
3606
}
23✔
3607

3608
// build a libMesh equation system for storing values
3609
void LibMesh::build_eqn_sys()
15✔
3610
{
3611
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
30✔
3612
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3613
  libMesh::ExplicitSystem& eq_sys =
3614
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3615
}
15✔
3616

3617
// intialize from mesh file
3618
void LibMesh::initialize()
23✔
3619
{
3620
  if (!settings::libmesh_comm) {
23!
3621
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3622
                "communicator.");
3623
  }
3624

3625
  // assuming that unstructured meshes used in OpenMC are 3D
3626
  n_dimension_ = 3;
23✔
3627

3628
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3629
  // Otherwise assume that it is prepared by its owning application
3630
  if (unique_m_) {
23!
3631
    m_->prepare_for_use();
23✔
3632
  }
3633

3634
  // ensure that the loaded mesh is 3 dimensional
3635
  if (m_->mesh_dimension() != n_dimension_) {
23!
3636
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3637
                            "mesh is not a 3D mesh.",
3638
      filename_));
3639
  }
3640

3641
  for (int i = 0; i < num_threads(); i++) {
69✔
3642
    pl_.emplace_back(m_->sub_point_locator());
46✔
3643
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
3644
    pl_.back()->enable_out_of_mesh_mode();
46✔
3645
  }
3646

3647
  // store first element in the mesh to use as an offset for bin indices
3648
  auto first_elem = *m_->elements_begin();
23✔
3649
  first_element_id_ = first_elem->id();
23✔
3650

3651
  // bounding box for the mesh for quick rejection checks
3652
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23✔
3653
  libMesh::Point ll = bbox_.min();
23✔
3654
  libMesh::Point ur = bbox_.max();
23✔
3655
  if (length_multiplier_ > 0.0) {
23!
3656
    lower_left_ = {length_multiplier_ * ll(0), length_multiplier_ * ll(1),
3657
      length_multiplier_ * ll(2)};
×
3658
    upper_right_ = {length_multiplier_ * ur(0), length_multiplier_ * ur(1),
3659
      length_multiplier_ * ur(2)};
×
3660
  } else {
3661
    lower_left_ = {ll(0), ll(1), ll(2)};
23✔
3662
    upper_right_ = {ur(0), ur(1), ur(2)};
23✔
3663
  }
3664
}
23✔
3665

3666
// Sample position within a tet for LibMesh type tets
3667
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3668
{
3669
  const auto& elem = get_element_from_bin(bin);
400,820✔
3670
  // Get tet vertex coordinates from LibMesh
3671
  std::array<Position, 4> tet_verts;
400,820✔
3672
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3673
    auto node_ref = elem.node_ref(i);
1,603,280✔
3674
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3675
  }
1,603,280✔
3676
  // Samples position within tet using Barycentric coordinates
3677
  Position sampled_position = this->sample_tet(tet_verts, seed);
400,820✔
3678
  if (length_multiplier_ > 0.0) {
400,820!
3679
    return length_multiplier_ * sampled_position;
×
3680
  } else {
3681
    return sampled_position;
400,820✔
3682
  }
3683
}
3684

3685
Position LibMesh::centroid(int bin) const
3686
{
3687
  const auto& elem = this->get_element_from_bin(bin);
×
3688
  auto centroid = elem.vertex_average();
×
3689
  if (length_multiplier_ > 0.0) {
×
3690
    return length_multiplier_ * Position(centroid(0), centroid(1), centroid(2));
×
3691
  } else {
3692
    return {centroid(0), centroid(1), centroid(2)};
3693
  }
3694
}
3695

3696
int LibMesh::n_vertices() const
39,978✔
3697
{
3698
  return m_->n_nodes();
39,978✔
3699
}
3700

3701
Position LibMesh::vertex(int vertex_id) const
39,942✔
3702
{
3703
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
3704
  if (length_multiplier_ > 0.0) {
39,942!
3705
    return length_multiplier_ * Position(node_ref(0), node_ref(1), node_ref(2));
×
3706
  } else {
3707
    return {node_ref(0), node_ref(1), node_ref(2)};
39,942✔
3708
  }
3709
}
39,942✔
3710

3711
std::vector<int> LibMesh::connectivity(int elem_id) const
265,856✔
3712
{
3713
  std::vector<int> conn;
265,856✔
3714
  const auto* elem_ptr = m_->elem_ptr(elem_id);
265,856✔
3715
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,337,280✔
3716
    conn.push_back(elem_ptr->node_id(i));
1,071,424✔
3717
  }
3718
  return conn;
265,856✔
3719
}
3720

3721
std::string LibMesh::library() const
33✔
3722
{
3723
  return mesh_lib_type;
33✔
3724
}
3725

3726
int LibMesh::n_bins() const
1,784,287✔
3727
{
3728
  return m_->n_elem();
1,784,287✔
3729
}
3730

3731
int LibMesh::n_surface_bins() const
3732
{
3733
  int n_bins = 0;
3734
  for (int i = 0; i < this->n_bins(); i++) {
×
3735
    const libMesh::Elem& e = get_element_from_bin(i);
3736
    n_bins += e.n_faces();
3737
    // if this is a boundary element, it will only be visited once,
3738
    // the number of surface bins is incremented to
3739
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
3740
      // null neighbor pointer indicates a boundary face
3741
      if (!neighbor_ptr) {
×
3742
        n_bins++;
3743
      }
3744
    }
3745
  }
3746
  return n_bins;
3747
}
3748

3749
void LibMesh::add_score(const std::string& var_name)
15✔
3750
{
3751
  if (!equation_systems_) {
15!
3752
    build_eqn_sys();
15✔
3753
  }
3754

3755
  // check if this is a new variable
3756
  std::string value_name = var_name + "_mean";
15✔
3757
  if (!variable_map_.count(value_name)) {
15!
3758
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3759
    auto var_num =
3760
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3761
    variable_map_[value_name] = var_num;
15✔
3762
  }
3763

3764
  std::string std_dev_name = var_name + "_std_dev";
15✔
3765
  // check if this is a new variable
3766
  if (!variable_map_.count(std_dev_name)) {
15!
3767
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3768
    auto var_num =
3769
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3770
    variable_map_[std_dev_name] = var_num;
15✔
3771
  }
3772
}
15✔
3773

3774
void LibMesh::remove_scores()
15✔
3775
{
3776
  if (equation_systems_) {
15!
3777
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3778
    eqn_sys.clear();
15✔
3779
    variable_map_.clear();
15✔
3780
  }
3781
}
15✔
3782

3783
void LibMesh::set_score_data(const std::string& var_name,
15✔
3784
  const vector<double>& values, const vector<double>& std_dev)
3785
{
3786
  if (!equation_systems_) {
15!
3787
    build_eqn_sys();
×
3788
  }
3789

3790
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3791

3792
  if (!eqn_sys.is_initialized()) {
15!
3793
    equation_systems_->init();
15✔
3794
  }
3795

3796
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3797

3798
  // look up the value variable
3799
  std::string value_name = var_name + "_mean";
15✔
3800
  unsigned int value_num = variable_map_.at(value_name);
15✔
3801
  // look up the std dev variable
3802
  std::string std_dev_name = var_name + "_std_dev";
15✔
3803
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
3804

3805
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
97,871✔
3806
       it++) {
3807
    if (!(*it)->active()) {
97,856!
3808
      continue;
3809
    }
3810

3811
    auto bin = get_bin_from_element(*it);
97,856✔
3812

3813
    // set value
3814
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
3815
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
3816
    assert(value_dof_indices.size() == 1);
3817
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
3818

3819
    // set std dev
3820
    vector<libMesh::dof_id_type> std_dev_dof_indices;
97,856✔
3821
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
97,856✔
3822
    assert(std_dev_dof_indices.size() == 1);
3823
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
97,856✔
3824
  }
97,871✔
3825
}
15✔
3826

3827
void LibMesh::write(const std::string& filename) const
15✔
3828
{
3829
  write_message(fmt::format(
15✔
3830
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
15✔
3831
  libMesh::ExodusII_IO exo(*m_);
15✔
3832
  std::set<std::string> systems_out = {eq_system_name_};
45✔
3833
  exo.write_discontinuous_exodusII(
15✔
3834
    filename + ".e", *equation_systems_, &systems_out);
30✔
3835
}
15✔
3836

3837
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3838
  vector<int>& bins, vector<double>& lengths) const
3839
{
3840
  // TODO: Implement triangle crossings here
3841
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3842
}
3843

3844
int LibMesh::get_bin(Position r) const
2,340,484✔
3845
{
3846
  // look-up a tet using the point locator
3847
  libMesh::Point p(r.x, r.y, r.z);
2,340,484✔
3848

3849
  if (length_multiplier_ > 0.0) {
2,340,484!
3850
    // Scale the point down
3851
    p /= length_multiplier_;
3852
  }
3853

3854
  // quick rejection check
3855
  if (!bbox_.contains_point(p)) {
2,340,484✔
3856
    return -1;
918,796✔
3857
  }
3858

3859
  const auto& point_locator = pl_.at(thread_num());
1,421,688✔
3860

3861
  const auto elem_ptr = (*point_locator)(p);
1,421,688✔
3862
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,688✔
3863
}
2,340,484✔
3864

3865
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,518,314✔
3866
{
3867
  int bin = elem->id() - first_element_id_;
1,518,314✔
3868
  if (bin >= n_bins() || bin < 0) {
1,518,314!
3869
    fatal_error(fmt::format("Invalid bin: {}", bin));
3870
  }
3871
  return bin;
1,518,314✔
3872
}
3873

3874
std::pair<vector<double>, vector<double>> LibMesh::plot(
3875
  Position plot_ll, Position plot_ur) const
3876
{
3877
  return {};
3878
}
3879

3880
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
3881
{
3882
  return m_->elem_ref(bin);
765,460✔
3883
}
3884

3885
double LibMesh::volume(int bin) const
364,640✔
3886
{
3887
  return this->get_element_from_bin(bin).volume() * length_multiplier_ *
364,640✔
3888
         length_multiplier_ * length_multiplier_;
364,640✔
3889
}
3890

3891
AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh,
3892
  double length_multiplier,
3893
  const std::set<libMesh::subdomain_id_type>& block_ids)
3894
  : LibMesh(input_mesh, length_multiplier), block_ids_(block_ids),
3895
    block_restrict_(!block_ids_.empty()),
3896
    num_active_(
3897
      block_restrict_
×
3898
        ? std::distance(m_->active_subdomain_set_elements_begin(block_ids_),
×
3899
            m_->active_subdomain_set_elements_end(block_ids_))
×
3900
        : m_->n_active_elem())
×
3901
{
3902
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
3903
  // contiguous in ID space, so we need to map from bin indices (defined over
3904
  // active elements) to global dof ids
3905
  bin_to_elem_map_.reserve(num_active_);
×
3906
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
3907
  auto begin = block_restrict_
3908
                 ? m_->active_subdomain_set_elements_begin(block_ids_)
3909
                 : m_->active_elements_begin();
×
3910
  auto end = block_restrict_ ? m_->active_subdomain_set_elements_end(block_ids_)
3911
                             : m_->active_elements_end();
×
3912
  for (const auto& elem : libMesh::as_range(begin, end)) {
×
3913
    bin_to_elem_map_.push_back(elem->id());
×
3914
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
3915
  }
3916
}
3917

3918
int AdaptiveLibMesh::n_bins() const
3919
{
3920
  return num_active_;
3921
}
3922

3923
void AdaptiveLibMesh::add_score(const std::string& var_name)
3924
{
3925
  warning(fmt::format(
×
3926
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3927
    this->id_));
3928
}
3929

3930
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
3931
  const vector<double>& values, const vector<double>& std_dev)
3932
{
3933
  warning(fmt::format(
×
3934
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3935
    this->id_));
3936
}
3937

3938
void AdaptiveLibMesh::write(const std::string& filename) const
3939
{
3940
  warning(fmt::format(
×
3941
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3942
    this->id_));
3943
}
3944

3945
int AdaptiveLibMesh::get_bin(Position r) const
3946
{
3947
  // look-up a tet using the point locator
3948
  libMesh::Point p(r.x, r.y, r.z);
×
3949

3950
  if (length_multiplier_ > 0.0) {
×
3951
    // Scale the point down
3952
    p /= length_multiplier_;
3953
  }
3954

3955
  // quick rejection check
3956
  if (!bbox_.contains_point(p)) {
×
3957
    return -1;
3958
  }
3959

3960
  const auto& point_locator = pl_.at(thread_num());
×
3961

3962
  const auto elem_ptr = (*point_locator)(p, &block_ids_);
×
3963
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
×
3964
}
3965

3966
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3967
{
3968
  int bin = elem_to_bin_map_[elem->id()];
×
3969
  if (bin >= n_bins() || bin < 0) {
×
3970
    fatal_error(fmt::format("Invalid bin: {}", bin));
3971
  }
3972
  return bin;
3973
}
3974

3975
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
3976
{
3977
  return m_->elem_ref(bin_to_elem_map_.at(bin));
3978
}
3979

3980
#endif // OPENMC_LIBMESH_ENABLED
3981

3982
//==============================================================================
3983
// Non-member functions
3984
//==============================================================================
3985

3986
void read_meshes(pugi::xml_node root)
11,401✔
3987
{
3988
  std::unordered_set<int> mesh_ids;
11,401✔
3989

3990
  for (auto node : root.children("mesh")) {
14,118✔
3991
    // Check to make sure multiple meshes in the same file don't share IDs
3992
    int id = std::stoi(get_node_value(node, "id"));
2,717✔
3993
    if (contains(mesh_ids, id)) {
2,717!
UNCOV
3994
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
3995
                              "'{}' in the same input file",
3996
        id));
3997
    }
3998
    mesh_ids.insert(id);
2,717✔
3999

4000
    // If we've already read a mesh with the same ID in a *different* file,
4001
    // assume it is the same here
4002
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
2,717!
UNCOV
4003
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
UNCOV
4004
      continue;
×
4005
    }
4006

4007
    std::string mesh_type;
2,717✔
4008
    if (check_for_node(node, "type")) {
2,717✔
4009
      mesh_type = get_node_value(node, "type", true, true);
869✔
4010
    } else {
4011
      mesh_type = "regular";
1,848✔
4012
    }
4013

4014
    // determine the mesh library to use
4015
    std::string mesh_lib;
2,717✔
4016
    if (check_for_node(node, "library")) {
2,717✔
4017
      mesh_lib = get_node_value(node, "library", true, true);
47!
4018
    }
4019

4020
    Mesh::create(node, mesh_type, mesh_lib);
2,717✔
4021
  }
2,717✔
4022
}
11,401✔
4023

4024
void read_meshes(hid_t group)
20✔
4025
{
4026
  std::unordered_set<int> mesh_ids;
20✔
4027

4028
  std::vector<int> ids;
20✔
4029
  read_attribute(group, "ids", ids);
20✔
4030

4031
  for (auto id : ids) {
50✔
4032

4033
    // Check to make sure multiple meshes in the same file don't share IDs
4034
    if (contains(mesh_ids, id)) {
30!
UNCOV
4035
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4036
                              "'{}' in the same HDF5 input file",
4037
        id));
4038
    }
4039
    mesh_ids.insert(id);
30✔
4040

4041
    // If we've already read a mesh with the same ID in a *different* file,
4042
    // assume it is the same here
4043
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
30!
4044
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
30✔
4045
      continue;
30✔
4046
    }
4047

UNCOV
4048
    std::string name = fmt::format("mesh {}", id);
×
4049
    hid_t mesh_group = open_group(group, name.c_str());
×
4050

4051
    std::string mesh_type;
×
UNCOV
4052
    if (object_exists(mesh_group, "type")) {
×
4053
      read_dataset(mesh_group, "type", mesh_type);
×
4054
    } else {
UNCOV
4055
      mesh_type = "regular";
×
4056
    }
4057

4058
    // determine the mesh library to use
4059
    std::string mesh_lib;
×
UNCOV
4060
    if (object_exists(mesh_group, "library")) {
×
UNCOV
4061
      read_dataset(mesh_group, "library", mesh_lib);
×
4062
    }
4063

UNCOV
4064
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
UNCOV
4065
  }
×
4066
}
20✔
4067

4068
void meshes_to_hdf5(hid_t group)
6,322✔
4069
{
4070
  // Write number of meshes
4071
  hid_t meshes_group = create_group(group, "meshes");
6,322✔
4072
  int32_t n_meshes = model::meshes.size();
6,322✔
4073
  write_attribute(meshes_group, "n_meshes", n_meshes);
6,322✔
4074

4075
  if (n_meshes > 0) {
6,322✔
4076
    // Write IDs of meshes
4077
    vector<int> ids;
1,956✔
4078
    for (const auto& m : model::meshes) {
4,440✔
4079
      m->to_hdf5(meshes_group);
2,484✔
4080
      ids.push_back(m->id_);
2,484✔
4081
    }
4082
    write_attribute(meshes_group, "ids", ids);
1,956✔
4083
  }
1,956✔
4084

4085
  close_group(meshes_group);
6,322✔
4086
}
6,322✔
4087

4088
void free_memory_mesh()
7,507✔
4089
{
4090
  model::meshes.clear();
7,507✔
4091
  model::mesh_map.clear();
7,507✔
4092
}
7,507✔
4093

4094
extern "C" int n_meshes()
280✔
4095
{
4096
  return model::meshes.size();
280✔
4097
}
4098

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