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

openmc-dev / openmc / 28612311732

02 Jul 2026 06:21PM UTC coverage: 81.29% (+0.009%) from 81.281%
28612311732

Pull #3734

github

web-flow
Merge b05aeac7a into df6f94300
Pull Request #3734: Specify temperature from a field (structured mesh only)

18335 of 26578 branches covered (68.99%)

Branch coverage included in aggregate %.

292 of 323 new or added lines in 18 files covered. (90.4%)

318 existing lines in 12 files now uncovered.

59539 of 69220 relevant lines covered (86.01%)

49339464.46 hits per line

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

71.03
/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 <limits>
10
#include <numeric> // for accumulate
11
#include <string>
12

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

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

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

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

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

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

59
namespace openmc {
60

61
//==============================================================================
62
// Global variables
63
//==============================================================================
64

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

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

75
namespace model {
76

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

80
} // namespace model
81

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

89
//==============================================================================
90
// Helper functions
91
//==============================================================================
92

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

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

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

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

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

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

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

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

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

188
inline void atomic_max_double(double* ptr, double value)
19,074,960✔
189
{
190
  atomic_update_double(ptr, value, false);
6,358,320✔
191
}
6,358,320✔
192

193
inline void atomic_min_double(double* ptr, double value)
19,074,960✔
194
{
195
  atomic_update_double(ptr, value, true);
6,358,320✔
196
}
197

198
namespace detail {
199

200
//==============================================================================
201
// MaterialVolumes implementation
202
//==============================================================================
203

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

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

221
    // Non-atomic read of current material
222
    int32_t current_val = *slot_ptr;
9,011,707✔
223

224
    // Found the desired material; accumulate volume and bbox
225
    if (current_val == index_material) {
9,011,707✔
226
#pragma omp atomic
5,090,600✔
227
      this->volumes(index_elem, slot) += volume;
9,010,142✔
228
      if (bbox) {
9,010,142✔
229
        atomic_min_double(&this->bboxes(index_elem, slot, 0), bbox->min.x);
6,358,153✔
230
        atomic_min_double(&this->bboxes(index_elem, slot, 1), bbox->min.y);
6,358,153✔
231
        atomic_min_double(&this->bboxes(index_elem, slot, 2), bbox->min.z);
6,358,153✔
232
        atomic_max_double(&this->bboxes(index_elem, slot, 3), bbox->max.x);
6,358,153✔
233
        atomic_max_double(&this->bboxes(index_elem, slot, 4), bbox->max.y);
6,358,153✔
234
        atomic_max_double(&this->bboxes(index_elem, slot, 5), bbox->max.z);
6,358,153✔
235
      }
236
      return;
9,010,142✔
237
    }
238

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

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

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

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

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

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

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

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

327
} // namespace detail
328

329
//==============================================================================
330
// Mesh implementation
331
//==============================================================================
332

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

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

366
  return model::meshes.back();
3,545✔
367
}
368

369
Mesh::Mesh(pugi::xml_node node)
3,622✔
370
{
371
  // Read mesh id
372
  id_ = std::stoi(get_node_value(node, "id"));
7,244✔
373
  if (check_for_node(node, "name"))
3,622✔
374
    name_ = get_node_value(node, "name");
15✔
375
}
3,622✔
376

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

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

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

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

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

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

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

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

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

424
vector<double> Mesh::volumes() const
287✔
425
{
426
  vector<double> volumes(n_bins());
287✔
427
  for (int i = 0; i < n_bins(); i++) {
1,122,895✔
428
    volumes[i] = this->volume(i);
1,122,608✔
429
  }
430
  return volumes;
287✔
UNCOV
431
}
×
432

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

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

455
  Timer timer;
209✔
456
  timer.start();
209✔
457

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

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

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

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

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

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

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

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

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

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

515
      // Loop over rays on face of bounding box
516
#pragma omp for collapse(2)
517
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
17,600✔
518
        for (int i2 = 0; i2 < n2; ++i2) {
3,080,220✔
519
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
3,062,845✔
520
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
3,062,845✔
521

522
          p.from_source(&site);
3,062,845✔
523

524
          // Determine particle's location
525
          if (!exhaustive_find_cell(p)) {
3,062,845✔
526
            out_of_model = true;
39,930✔
527
            continue;
39,930✔
528
          }
529

530
          // Set birth cell attribute
531
          if (p.cell_born() == C_NONE)
3,022,915!
532
            p.cell_born() = p.lowest_coord().cell();
3,022,915✔
533

534
          // Initialize last cells from current cell
535
          for (int j = 0; j < p.n_coord(); ++j) {
6,045,830✔
536
            p.cell_last(j) = p.coord(j).cell();
3,022,915✔
537
          }
538
          p.n_coord_last() = p.n_coord();
3,022,915✔
539

540
          while (true) {
4,688,483✔
541
            // Ray trace from r_start to r_end
542
            Position r0 = p.r();
3,855,699✔
543
            double max_distance = bbox.max[axis] - r0[axis];
3,855,699✔
544

545
            // Find the distance to the nearest boundary
546
            BoundaryInfo boundary = distance_to_boundary(p);
3,855,699✔
547

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

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

557
            // Add volumes to any mesh elements that were crossed
558
            int i_material = p.material();
3,855,699✔
559
            if (i_material != C_NONE) {
3,855,699✔
560
              i_material = model::materials[i_material]->id();
1,211,997✔
561
            }
562
            double cumulative_frac = 0.0;
3,855,699✔
563
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
7,930,048✔
564
              int mesh_index = bins[i_bin];
4,074,349✔
565
              double length = distance * length_fractions[i_bin];
4,074,349✔
566
              double volume = length * d1 * d2;
4,074,349✔
567

568
              if (compute_bboxes) {
4,074,349✔
569
                double axis_start = r0[axis] + distance * cumulative_frac;
2,868,264✔
570
                double axis_end = axis_start + length;
2,868,264✔
571
                cumulative_frac += length_fractions[i_bin];
2,868,264✔
572

573
                Position contrib_min = site.r;
2,868,264✔
574
                Position contrib_max = site.r;
2,868,264✔
575

576
                contrib_min[ax1] = site.r[ax1] - 0.5 * d1;
2,868,264✔
577
                contrib_max[ax1] = site.r[ax1] + 0.5 * d1;
2,868,264✔
578
                contrib_min[ax2] = site.r[ax2] - 0.5 * d2;
2,868,264✔
579
                contrib_max[ax2] = site.r[ax2] + 0.5 * d2;
2,868,264✔
580
                contrib_min[axis] = std::min(axis_start, axis_end);
2,868,264!
581
                contrib_max[axis] = std::max(axis_start, axis_end);
5,736,528!
582

583
                BoundingBox contrib_bbox {contrib_min, contrib_max};
2,868,264✔
584
                contrib_bbox &= bbox;
2,868,264✔
585

586
                result.add_volume(
2,868,264✔
587
                  mesh_index, i_material, volume, &contrib_bbox);
588
              } else {
589
                // Add volume to result
590
                result.add_volume(mesh_index, i_material, volume);
1,206,085✔
591
              }
592
            }
593

594
            if (distance == max_distance)
3,855,699✔
595
              break;
596

597
            // cross next geometric surface
598
            for (int j = 0; j < p.n_coord(); ++j) {
1,665,568✔
599
              p.cell_last(j) = p.coord(j).cell();
832,784✔
600
            }
601
            p.n_coord_last() = p.n_coord();
832,784✔
602

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

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

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

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

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

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

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

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

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

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

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

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

738
  // Write mesh type
739
  write_dataset(mesh_group, "type", this->get_mesh_type());
3,339✔
740

741
  // Write mesh ID
742
  write_attribute(mesh_group, "id", id_);
3,339✔
743

744
  // Write mesh name
745
  write_dataset(mesh_group, "name", name_);
3,339✔
746

747
  // Write mesh data
748
  this->to_hdf5_inner(mesh_group);
3,339✔
749

750
  // Close group
751
  close_group(mesh_group);
3,339✔
752
}
3,339✔
753

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

758
std::string StructuredMesh::bin_label(int bin) const
5,194,368✔
759
{
760
  MeshIndex ijk = get_indices_from_bin(bin);
5,194,368✔
761

762
  if (n_dimension_ > 2) {
5,194,368✔
763
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
5,178,429✔
764
  } else if (n_dimension_ > 1) {
15,939✔
765
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
15,664✔
766
  } else {
767
    return fmt::format("Mesh Index ({})", ijk[0]);
275✔
768
  }
769
}
770

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

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

783
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,421,416!
784
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,421,416!
785

786
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,421,416!
787
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,421,416!
788

789
  return {x_min + (x_max - x_min) * prn(seed),
1,421,416✔
790
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,421,416✔
791
}
792

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

797
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
49!
798
{
799
  n_dimension_ = 3;
49✔
800

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

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

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

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

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

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

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

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

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

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

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

NEW
876
double UnstructuredMesh::distance_to_next_boundary(
×
877
  int current_bin, Position r, Direction u, int& bin_next) const
878
{
NEW
879
  fatal_error("Not implemented");
×
880
  return -1.0;
881
}
882

883
void UnstructuredMesh::determine_bounds()
25✔
884
{
885
  double xmin = INFTY;
25✔
886
  double ymin = INFTY;
25✔
887
  double zmin = INFTY;
25✔
888
  double xmax = -INFTY;
25✔
889
  double ymax = -INFTY;
25✔
890
  double zmax = -INFTY;
25✔
891
  int n = this->n_vertices();
25✔
892
  for (int i = 0; i < n; ++i) {
55,951✔
893
    auto v = this->vertex(i);
55,926✔
894
    xmin = std::min(v.x, xmin);
55,926✔
895
    ymin = std::min(v.y, ymin);
55,926✔
896
    zmin = std::min(v.z, zmin);
55,926✔
897
    xmax = std::max(v.x, xmax);
55,926✔
898
    ymax = std::max(v.y, ymax);
55,926✔
899
    zmax = std::max(v.z, zmax);
79,911✔
900
  }
901
  lower_left_ = {xmin, ymin, zmin};
25✔
902
  upper_right_ = {xmax, ymax, zmax};
25✔
903
}
25✔
904

905
Position UnstructuredMesh::sample_tet(
601,230✔
906
  std::array<Position, 4> coords, uint64_t* seed) const
907
{
908
  // Uniform distribution
909
  double s = prn(seed);
601,230✔
910
  double t = prn(seed);
601,230✔
911
  double u = prn(seed);
601,230✔
912

913
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
914
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
915
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
916
  if (s + t > 1) {
601,230✔
917
    s = 1.0 - s;
300,234✔
918
    t = 1.0 - t;
300,234✔
919
  }
920
  if (s + t + u > 1) {
601,230✔
921
    if (t + u > 1) {
400,898✔
922
      double old_t = t;
200,450✔
923
      t = 1.0 - u;
200,450✔
924
      u = 1.0 - s - old_t;
200,450✔
925
    } else if (t + u <= 1) {
200,448!
926
      double old_s = s;
200,448✔
927
      s = 1.0 - t - u;
200,448✔
928
      u = old_s + t + u - 1;
200,448✔
929
    }
930
  }
931
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,803,690✔
932
         u * (coords[3] - coords[0]) + coords[0];
601,230✔
933
}
934

935
const std::string UnstructuredMesh::mesh_type = "unstructured";
936

937
std::string UnstructuredMesh::get_mesh_type() const
34✔
938
{
939
  return mesh_type;
34✔
940
}
941

UNCOV
942
void UnstructuredMesh::surface_bins_crossed(
×
943
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
944
{
UNCOV
945
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
946
}
947

948
std::string UnstructuredMesh::bin_label(int bin) const
207,736✔
949
{
950
  return fmt::format("Mesh Index ({})", bin);
207,736✔
951
};
952

953
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
34✔
954
{
955
  write_dataset(mesh_group, "filename", filename_);
34!
956
  write_dataset(mesh_group, "library", this->library());
34!
957
  if (!options_.empty()) {
34✔
958
    write_attribute(mesh_group, "options", options_);
8✔
959
  }
960

961
  if (length_multiplier_ > 0.0)
34!
UNCOV
962
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
963

964
  // write vertex coordinates
965
  tensor::Tensor<double> vertices(
34✔
966
    {static_cast<size_t>(this->n_vertices()), static_cast<size_t>(3)});
34✔
967
  for (int i = 0; i < this->n_vertices(); i++) {
72,939!
968
    auto v = this->vertex(i);
72,905!
969
    vertices.slice(i) = {v.x, v.y, v.z};
145,810!
970
  }
971
  write_dataset(mesh_group, "vertices", vertices);
34!
972

973
  int num_elem_skipped = 0;
34✔
974

975
  // write element types and connectivity
976
  vector<double> volumes;
34!
977
  tensor::Tensor<int> connectivity(
34✔
978
    {static_cast<size_t>(this->n_bins()), static_cast<size_t>(8)});
34!
979
  tensor::Tensor<int> elem_types(
34✔
980
    {static_cast<size_t>(this->n_bins()), static_cast<size_t>(1)});
34!
981
  for (int i = 0; i < this->n_bins(); i++) {
351,770!
982
    auto conn = this->connectivity(i);
351,736!
983

984
    volumes.emplace_back(this->volume(i));
351,736!
985

986
    // write linear tet element
987
    if (conn.size() == 4) {
351,736✔
988
      elem_types.slice(i) = static_cast<int>(ElementType::LINEAR_TET);
347,736!
989
      connectivity.slice(i) = {
347,736!
990
        conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1};
695,472!
991
      // write linear hex element
992
    } else if (conn.size() == 8) {
4,000!
993
      elem_types.slice(i) = static_cast<int>(ElementType::LINEAR_HEX);
4,000!
994
      connectivity.slice(i) = {
4,000!
995
        conn[0], conn[1], conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]};
8,000!
996
    } else {
997
      num_elem_skipped++;
×
998
      elem_types.slice(i) = static_cast<int>(ElementType::UNSUPPORTED);
×
UNCOV
999
      connectivity.slice(i) = -1;
×
1000
    }
1001
  }
351,736✔
1002

1003
  // warn users that some elements were skipped
1004
  if (num_elem_skipped > 0) {
34!
UNCOV
1005
    warning(fmt::format("The connectivity of {} elements "
×
1006
                        "on mesh {} were not written "
1007
                        "because they are not of type linear tet/hex.",
UNCOV
1008
      num_elem_skipped, this->id_));
×
1009
  }
1010

1011
  write_dataset(mesh_group, "volumes", volumes);
34!
1012
  write_dataset(mesh_group, "connectivity", connectivity);
34!
1013
  write_dataset(mesh_group, "element_types", elem_types);
34!
1014
}
102✔
1015

1016
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
25✔
1017
{
1018
  length_multiplier_ = length_multiplier;
25✔
1019
}
25✔
1020

1021
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
1022
{
1023
  auto conn = connectivity(bin);
120,000✔
1024

1025
  if (conn.size() == 4)
120,000!
1026
    return ElementType::LINEAR_TET;
UNCOV
1027
  else if (conn.size() == 8)
×
1028
    return ElementType::LINEAR_HEX;
1029
  else
UNCOV
1030
    return ElementType::UNSUPPORTED;
×
1031
}
120,000✔
1032

1033
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,634,370,214✔
1034
  Position r, bool& in_mesh) const
1035
{
1036
  MeshIndex ijk;
1,634,370,214✔
1037
  in_mesh = true;
1,634,370,214✔
1038
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
1039
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
1040

1041
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
1042
      in_mesh = false;
117,544,342✔
1043
  }
1044
  return ijk;
1,634,370,214✔
1045
}
1046

1047
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
2,147,483,647✔
1048
{
1049
  switch (n_dimension_) {
2,147,483,647!
1050
  case 1:
880,605✔
1051
    return ijk[0] - 1;
880,605✔
1052
  case 2:
136,375,228✔
1053
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
136,375,228✔
1054
  case 3:
2,094,922,509✔
1055
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
2,094,922,509✔
1056
  default:
×
UNCOV
1057
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
1058
  }
1059
}
1060

1061
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
13,952,340✔
1062
{
1063
  MeshIndex ijk;
13,952,340✔
1064
  if (n_dimension_ == 1) {
13,952,340✔
1065
    ijk[0] = bin + 1;
275✔
1066
  } else if (n_dimension_ == 2) {
13,952,065✔
1067
    ijk[0] = bin % shape_[0] + 1;
15,664✔
1068
    ijk[1] = bin / shape_[0] + 1;
15,664✔
1069
  } else if (n_dimension_ == 3) {
13,936,401!
1070
    ijk[0] = bin % shape_[0] + 1;
13,936,401✔
1071
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
13,936,401✔
1072
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
13,936,401✔
1073
  }
1074
  return ijk;
13,952,340✔
1075
}
1076

1077
int StructuredMesh::get_bin(Position r) const
427,946,200✔
1078
{
1079
  // Determine indices
1080
  bool in_mesh;
427,946,200✔
1081
  MeshIndex ijk = get_indices(r, in_mesh);
427,946,200✔
1082
  if (!in_mesh)
427,946,200✔
1083
    return -1;
1084

1085
  // Convert indices to bin
1086
  return get_bin_from_indices(ijk);
406,457,808✔
1087
}
1088

1089
int StructuredMesh::n_bins() const
1,138,807✔
1090
{
1091
  // Bin indices are stored as 32-bit ints in the tally system.
1092
  int64_t n = 1;
1,138,807✔
1093
  for (int i = 0; i < n_dimension_; ++i)
4,554,816✔
1094
    n *= shape_[i];
3,416,009✔
1095
  if (n > std::numeric_limits<int>::max()) {
1,138,807!
UNCOV
1096
    fatal_error(fmt::format(
×
UNCOV
1097
      "Mesh {} has too many bins ({}) for 32-bit tally indexing", id_, n));
×
1098
  }
1099
  return static_cast<int>(n);
1,138,807✔
1100
}
1101

1102
int StructuredMesh::n_surface_bins() const
370✔
1103
{
1104
  // Surface bin indices are stored as 32-bit ints in the tally system.
1105
  int64_t n = static_cast<int64_t>(n_bins()) * 4 * n_dimension_;
370✔
1106
  if (n > std::numeric_limits<int>::max()) {
370!
1107
    fatal_error(fmt::format(
×
UNCOV
1108
      "Mesh {} has too many surface bins ({}) for tally indexing", id_, n));
×
1109
  }
1110
  return static_cast<int>(n);
370✔
1111
}
1112

UNCOV
1113
tensor::Tensor<double> StructuredMesh::count_sites(
×
1114
  const SourceSite* bank, int64_t length, bool* outside) const
1115
{
1116
  // Determine shape of array for counts
1117
  std::size_t m = this->n_bins();
×
1118
  vector<std::size_t> shape = {m};
×
1119

1120
  // Create array of zeros
UNCOV
1121
  auto cnt = tensor::zeros<double>(shape);
×
1122
  bool outside_ = false;
1123

UNCOV
1124
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
1125
    const auto& site = bank[i];
×
1126

1127
    // determine scoring bin for entropy mesh
1128
    int mesh_bin = get_bin(site.r);
×
1129

1130
    // if outside mesh, skip particle
UNCOV
1131
    if (mesh_bin < 0) {
×
UNCOV
1132
      outside_ = true;
×
UNCOV
1133
      continue;
×
1134
    }
1135

1136
    // Add to appropriate bin
UNCOV
1137
    cnt(mesh_bin) += site.wgt;
×
1138
  }
1139

1140
  // Create reduced count data
UNCOV
1141
  auto counts = tensor::zeros<double>(shape);
×
UNCOV
1142
  int total = cnt.size();
×
1143

1144
#ifdef OPENMC_MPI
1145
  // collect values from all processors
1146
  MPI_Reduce(
×
1147
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
×
1148

1149
  // Check if there were sites outside the mesh for any processor
1150
  if (outside) {
×
1151
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
×
1152
  }
1153
#else
1154
  std::copy(cnt.data(), cnt.data() + total, counts.data());
1155
  if (outside)
×
1156
    *outside = outside_;
1157
#endif
1158

UNCOV
1159
  return counts;
×
UNCOV
1160
}
×
1161

1162
// raytrace through the mesh. The template class T will do the tallying.
1163
// A modern optimizing compiler can recognize the noop method of T and
1164
// eliminate that call entirely.
1165
template<class T>
1166
void StructuredMesh::raytrace_mesh(
1,250,291,862✔
1167
  Position r0, Position r1, const Direction& u, T tally) const
1168
{
1169
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
1170
  // enable/disable tally calls for now, T template type needs to provide both
1171
  // surface and track methods, which might be empty. modern optimizing
1172
  // compilers will (hopefully) eliminate the complete code (including
1173
  // calculation of parameters) but for the future: be explicit
1174

1175
  // Compute the length of the entire track.
1176
  double total_distance = (r1 - r0).norm();
1,250,291,862✔
1177
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
1,250,291,862✔
1178
    return;
1179

1180
  // keep a copy of the original global position to pass to get_indices,
1181
  // which performs its own transformation to local coordinates
1182
  Position global_r = r0;
1,198,637,177✔
1183
  Position local_r = local_coords(r0);
1,198,637,177✔
1184

1185
  const int n = n_dimension_;
1,198,637,177✔
1186

1187
  // Flag if position is inside the mesh
1188
  bool in_mesh;
1189

1190
  // Position is r = r0 + u * traveled_distance, start at r0
1191
  double traveled_distance {0.0};
1,198,637,177✔
1192

1193
  // Calculate index of current cell. Offset the position a tiny bit in
1194
  // direction of flight
1195
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
1,198,637,177✔
1196

1197
  // if track is very short, assume that it is completely inside one cell.
1198
  // Only the current cell will score and no surfaces
1199
  if (total_distance < 2 * TINY_BIT) {
1,198,637,177✔
1200
    if (in_mesh) {
361,887✔
1201
      tally.track(ijk, 1.0);
361,403✔
1202
    }
1203
    return;
361,887✔
1204
  }
1205

1206
  // Calculate initial distances to next surfaces in all three dimensions
1207
  std::array<MeshDistance, 3> distances;
2,147,483,647✔
1208
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
1209
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
1210
  }
1211

1212
  // Loop until r = r1 is eventually reached
1213
  while (true) {
1214

1215
    if (in_mesh) {
1,998,733,585✔
1216

1217
      // find surface with minimal distance to current position
1218
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,903,744,586✔
1219
                     distances.begin();
1,903,744,586✔
1220

1221
      // Tally track length delta since last step
1222
      tally.track(ijk,
1,903,744,586✔
1223
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
2,147,483,647✔
1224
          total_distance);
1225

1226
      // update position and leave, if we have reached end position
1227
      traveled_distance = distances[k].distance;
1,903,744,586✔
1228
      if (traveled_distance >= total_distance)
1,903,744,586✔
1229
        return;
1230

1231
      // If we have not reached r1, we have hit a surface. Tally outward
1232
      // current
1233
      tally.surface(ijk, k, distances[k].max_surface, false);
793,126,801✔
1234

1235
      // Update cell and calculate distance to next surface in k-direction.
1236
      // The two other directions are still valid!
1237
      ijk[k] = distances[k].next_index;
793,126,801✔
1238
      distances[k] =
793,126,801✔
1239
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
793,126,801✔
1240

1241
      // Check if we have left the interior of the mesh
1242
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
800,270,197✔
1243

1244
      // If we are still inside the mesh, tally inward current for the next
1245
      // cell
1246
      if (in_mesh)
29,576,899✔
1247
        tally.surface(ijk, k, !distances[k].max_surface, true);
799,042,078✔
1248

1249
    } else { // not inside mesh
1250

1251
      // For all directions outside the mesh, find the distance that we need
1252
      // to travel to reach the next surface. Use the largest distance, as
1253
      // only this will cross all outer surfaces.
1254
      int k_max {-1};
1255
      for (int k = 0; k < n; ++k) {
378,511,630✔
1256
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
283,522,631✔
1257
            (distances[k].distance > traveled_distance)) {
108,823,055✔
1258
          traveled_distance = distances[k].distance;
1259
          k_max = k;
1260
        }
1261
      }
1262
      // Assure some distance is traveled
1263
      if (k_max == -1) {
94,988,999✔
1264
        traveled_distance += TINY_BIT;
110✔
1265
      }
1266

1267
      // If r1 is not inside the mesh, exit here
1268
      if (traveled_distance >= total_distance)
94,988,999✔
1269
        return;
1270

1271
      // Calculate the new cell index and update all distances to next
1272
      // surfaces.
1273
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
7,331,494✔
1274
      for (int k = 0; k < n; ++k) {
29,117,438✔
1275
        distances[k] =
21,785,944✔
1276
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
21,785,944✔
1277
      }
1278

1279
      // If inside the mesh, Tally inward current
1280
      if (in_mesh && k_max >= 0)
7,331,494!
1281
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
770,858,428✔
1282
    }
1283
  }
1284
}
1285

1286
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,138,164,297✔
1287
  vector<int>& bins, vector<double>& lengths) const
1288
{
1289

1290
  // Helper tally class.
1291
  // stores a pointer to the mesh class and references to bins and lengths
1292
  // parameters. Performs the actual tally through the track method.
1293
  struct TrackAggregator {
1,138,164,297✔
1294
    TrackAggregator(
1,138,164,297✔
1295
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1296
      : mesh(_mesh), bins(_bins), lengths(_lengths)
1,138,164,297✔
1297
    {}
1298
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1299
    void track(const MeshIndex& ijk, double l) const
1,764,061,425✔
1300
    {
1301
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,764,061,425✔
1302
      lengths.push_back(l);
1,764,061,425✔
1303
    }
1,764,061,425✔
1304

1305
    const StructuredMesh* mesh;
1306
    vector<int>& bins;
1307
    vector<double>& lengths;
1308
  };
1309

1310
  // Perform the mesh raytrace with the helper class.
1311
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1,138,164,297✔
1312
}
1,138,164,297✔
1313

1314
void StructuredMesh::surface_bins_crossed(
112,127,565✔
1315
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1316
{
1317

1318
  // Helper tally class.
1319
  // stores a pointer to the mesh class and a reference to the bins parameter.
1320
  // Performs the actual tally through the surface method.
1321
  struct SurfaceAggregator {
112,127,565✔
1322
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
112,127,565✔
1323
      : mesh(_mesh), bins(_bins)
112,127,565✔
1324
    {}
1325
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
58,159,189✔
1326
    {
1327
      int i_bin =
58,159,189✔
1328
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
58,159,189✔
1329
      if (max)
58,159,189✔
1330
        i_bin += 2;
29,051,440✔
1331
      if (inward)
58,159,189✔
1332
        i_bin += 1;
28,582,290✔
1333
      bins.push_back(i_bin);
58,159,189✔
1334
    }
58,159,189✔
1335
    void track(const MeshIndex& idx, double l) const {}
1336

1337
    const StructuredMesh* mesh;
1338
    vector<int>& bins;
1339
  };
1340

1341
  // Perform the mesh raytrace with the helper class.
1342
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
112,127,565✔
1343
}
112,127,565✔
1344

1345
double StructuredMesh::distance_to_next_boundary(
11,669,942✔
1346
  int current_bin, Position r, Direction u, int& bin_next) const
1347
{
1348
  Position global_r = r;
11,669,942✔
1349
  Position local_r = local_coords(r);
11,669,942✔
1350

1351
  double distance;
11,669,942✔
1352
  bool in_mesh;
11,669,942✔
1353

1354
  // Find the cell indices
1355
  StructuredMesh::MeshIndex ijk;
11,669,942✔
1356
  if (current_bin >= 0) {
11,669,942✔
1357

1358
    ijk = get_indices_from_bin(current_bin);
6,122,615✔
1359

1360
    // Calculate initial distances to next surfaces in all three dimensions
1361
    std::array<StructuredMesh::MeshDistance, 3> distances;
12,245,230✔
1362
    for (int k = 0; k < n_dimension_; ++k) {
24,490,460✔
1363
      distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
18,367,845✔
1364
    }
1365

1366
    // Find next ijk
1367
    auto k_min =
1368
      std::min_element(distances.begin(), distances.end()) - distances.begin();
6,122,615✔
1369
    distance = distances[k_min].distance;
6,122,615✔
1370
    ijk[k_min] = distances[k_min].next_index;
6,122,615✔
1371

1372
    // If the particle is on a surface, test using the next index
1373
    if (distance <= FP_COINCIDENT) {
6,122,615✔
1374

1375
      // Update distances
1376
      for (int k = 0; k < n_dimension_; ++k) {
176✔
1377
        distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
132✔
1378
      }
1379

1380
      k_min = std::min_element(distances.begin(), distances.end()) -
44✔
1381
              distances.begin();
44✔
1382
      distance = distances[k_min].distance;
44✔
1383
      ijk[k_min] = distances[k_min].next_index;
44✔
1384
    }
1385

1386
    // Determine next bin
1387
    in_mesh = true;
6,122,615✔
1388
    for (int k = 0; k < n_dimension_; ++k) {
24,490,460✔
1389
      if ((ijk[k] < 1) || (ijk[k] > shape_[k])) {
18,367,845✔
1390
        in_mesh = false;
3,078,038✔
1391
      }
1392
    }
1393
    if (in_mesh) {
6,122,615✔
1394
      bin_next = get_bin_from_indices(ijk);
3,044,577✔
1395
    } else {
1396
      bin_next = C_NONE;
3,078,038✔
1397
    }
1398

1399
  } else { // Outside mesh
1400

1401
    // Calculate distance to mesh from outside
1402
    distance = INFTY;
1403
    for (int k = 0; k < n_dimension_; k++) {
22,189,308✔
1404
      double d = distance_to_mesh_boundary_from_outside(k, r, u);
16,641,981✔
1405
      if (d < INFTY) {
16,641,981✔
1406
        distance = d;
455,343✔
1407
      }
1408
    }
1409

1410
    // Determine next bin
1411
    if (distance < INFTY) {
5,547,327✔
1412
      ijk = get_indices(global_r + (distance + TINY_BIT) * u, in_mesh);
455,343✔
1413
      bin_next = get_bin_from_indices(ijk);
455,343✔
1414
    } else {
1415
      bin_next = C_NONE;
5,091,984✔
1416
    }
1417
  }
1418

1419
  return distance;
11,669,942✔
1420
}
1421

1422
//==============================================================================
1423
// RegularMesh implementation
1424
//==============================================================================
1425

1426
int RegularMesh::set_grid()
2,706✔
1427
{
1428
  tensor::Tensor<int> shape(shape_.data(), static_cast<size_t>(n_dimension_));
2,706✔
1429

1430
  // Check that dimensions are all greater than zero
1431
  if ((shape <= 0).any()) {
8,118!
UNCOV
1432
    set_errmsg("All entries for a regular mesh dimensions "
×
1433
               "must be positive.");
UNCOV
1434
    return OPENMC_E_INVALID_ARGUMENT;
×
1435
  }
1436

1437
  // Make sure lower_left and dimension match
1438
  if (lower_left_.size() != n_dimension_) {
2,706!
UNCOV
1439
    set_errmsg("Number of entries in lower_left must be the same "
×
1440
               "as the regular mesh dimensions.");
1441
    return OPENMC_E_INVALID_ARGUMENT;
×
1442
  }
1443
  if (width_.size() > 0) {
2,706✔
1444

1445
    // Check to ensure width has same dimensions
1446
    if (width_.size() != n_dimension_) {
46!
UNCOV
1447
      set_errmsg("Number of entries on width must be the same as "
×
1448
                 "the regular mesh dimensions.");
UNCOV
1449
      return OPENMC_E_INVALID_ARGUMENT;
×
1450
    }
1451

1452
    // Check for negative widths
1453
    if ((width_ < 0.0).any()) {
138!
UNCOV
1454
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
UNCOV
1455
      return OPENMC_E_INVALID_ARGUMENT;
×
1456
    }
1457

1458
    // Set width and upper right coordinate
1459
    upper_right_ = lower_left_ + shape * width_;
138✔
1460

1461
  } else if (upper_right_.size() > 0) {
2,660!
1462

1463
    // Check to ensure upper_right_ has same dimensions
1464
    if (upper_right_.size() != n_dimension_) {
2,660!
UNCOV
1465
      set_errmsg("Number of entries on upper_right must be the "
×
1466
                 "same as the regular mesh dimensions.");
UNCOV
1467
      return OPENMC_E_INVALID_ARGUMENT;
×
1468
    }
1469

1470
    // Check that upper-right is above lower-left
1471
    if ((upper_right_ < lower_left_).any()) {
7,980!
UNCOV
1472
      set_errmsg(
×
1473
        "The upper_right coordinates of a regular mesh must be greater than "
1474
        "the lower_left coordinates.");
UNCOV
1475
      return OPENMC_E_INVALID_ARGUMENT;
×
1476
    }
1477

1478
    // Set width
1479
    width_ = (upper_right_ - lower_left_) / shape;
7,980✔
1480
  }
1481

1482
  // Set material volumes
1483
  volume_frac_ = 1.0 / shape.prod();
2,706✔
1484

1485
  element_volume_ = 1.0;
2,706✔
1486
  for (int i = 0; i < n_dimension_; i++) {
10,277✔
1487
    element_volume_ *= width_[i];
7,571✔
1488
  }
1489
  return 0;
1490
}
2,706✔
1491

1492
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
2,695✔
1493
{
1494
  // Determine number of dimensions for mesh
1495
  if (!check_for_node(node, "dimension")) {
2,695!
UNCOV
1496
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1497
  }
1498

1499
  tensor::Tensor<int> shape = get_node_tensor<int>(node, "dimension");
2,695✔
1500
  int n = n_dimension_ = shape.size();
2,695!
1501
  if (n != 1 && n != 2 && n != 3) {
2,695!
UNCOV
1502
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1503
  }
1504
  std::copy(shape.begin(), shape.end(), shape_.begin());
2,695✔
1505

1506
  // Check for lower-left coordinates
1507
  if (check_for_node(node, "lower_left")) {
2,695!
1508
    // Read mesh lower-left corner location
1509
    lower_left_ = get_node_tensor<double>(node, "lower_left");
2,695✔
1510
  } else {
UNCOV
1511
    fatal_error("Must specify <lower_left> on a mesh.");
×
1512
  }
1513

1514
  if (check_for_node(node, "width")) {
2,695✔
1515
    // Make sure one of upper-right or width were specified
1516
    if (check_for_node(node, "upper_right")) {
46!
1517
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1518
    }
1519

1520
    width_ = get_node_tensor<double>(node, "width");
92✔
1521

1522
  } else if (check_for_node(node, "upper_right")) {
2,649!
1523

1524
    upper_right_ = get_node_tensor<double>(node, "upper_right");
5,298✔
1525

1526
  } else {
UNCOV
1527
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1528
  }
1529

1530
  if (int err = set_grid()) {
2,695!
UNCOV
1531
    fatal_error(openmc_err_msg);
×
1532
  }
1533
}
2,695✔
1534

1535
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
11✔
1536
{
1537
  // Determine number of dimensions for mesh
1538
  if (!object_exists(group, "dimension")) {
11!
UNCOV
1539
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1540
  }
1541

1542
  tensor::Tensor<int> shape;
11✔
1543
  read_dataset(group, "dimension", shape);
11✔
1544
  int n = n_dimension_ = shape.size();
11!
1545
  if (n != 1 && n != 2 && n != 3) {
11!
UNCOV
1546
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1547
  }
1548
  std::copy(shape.begin(), shape.end(), shape_.begin());
11✔
1549

1550
  // Check for lower-left coordinates
1551
  if (object_exists(group, "lower_left")) {
11!
1552
    // Read mesh lower-left corner location
1553
    read_dataset(group, "lower_left", lower_left_);
11✔
1554
  } else {
UNCOV
1555
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1556
  }
1557

1558
  if (object_exists(group, "upper_right")) {
11!
1559

1560
    read_dataset(group, "upper_right", upper_right_);
11✔
1561

1562
  } else {
NEW
1563
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1564
  }
1565

1566
  if (int err = set_grid()) {
11!
UNCOV
1567
    fatal_error(openmc_err_msg);
×
1568
  }
1569
}
11✔
1570

1571
int RegularMesh::get_index_in_direction(double r, int i) const
2,147,483,647✔
1572
{
1573
  if (r == lower_left_[i]) {
2,147,483,647✔
1574
    return 1;
1575
  } else {
1576
    return std::ceil((r - lower_left_[i]) / width_[i]);
2,147,483,647✔
1577
  }
1578
}
1579

1580
const std::string RegularMesh::mesh_type = "regular";
1581

1582
std::string RegularMesh::get_mesh_type() const
3,657✔
1583
{
1584
  return mesh_type;
3,657✔
1585
}
1586

1587
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,917,501,900✔
1588
{
1589
  return lower_left_[i] + ijk[i] * width_[i];
1,917,501,900✔
1590
}
1591

1592
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,848,497,912✔
1593
{
1594
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,848,497,912✔
1595
}
1596

1597
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1598
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1599
  double l) const
1600
{
1601
  MeshDistance d;
2,147,483,647✔
1602
  d.next_index = ijk[i];
2,147,483,647✔
1603
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1604
    return d;
15,143,174✔
1605

1606
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1607
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1608
    d.next_index++;
1,913,237,652✔
1609
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,913,237,652✔
1610
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,873,532,154✔
1611
    d.next_index--;
1,844,233,664✔
1612
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,844,233,664✔
1613
  }
1614

1615
  return d;
2,147,483,647✔
1616
}
1617

1618
double RegularMesh::distance_to_mesh_boundary_from_outside(
16,641,915✔
1619
  int k, const Position& r, const Direction& u) const
1620
{
1621
  double t;
16,641,915✔
1622
  double distance = INFTY;
16,641,915✔
1623

1624
  if (u[k] > 0.0) {
16,641,915✔
1625
    t = (lower_left_[k] - r[k]) / u[k];
8,311,960✔
1626
  } else {
1627
    t = (upper_right_[k] - r[k]) / u[k];
8,329,955✔
1628
  }
1629

1630
  if (t > FP_COINCIDENT) {
16,641,915✔
1631
    bool reenter = true;
1632
    for (int i = 0; i < n_dimension_; i++) {
22,142,112✔
1633
      if (i != k) {
16,606,584✔
1634
        double a = r[i] + u[i] * t;
11,071,056✔
1635
        reenter &= (a >= lower_left_[i]);
11,071,056✔
1636
        reenter &= (a <= upper_right_[i]);
11,071,056✔
1637
      }
1638
    }
1639
    if (reenter) {
5,535,528✔
1640
      distance = t;
455,332✔
1641
    }
1642
  }
1643
  return distance;
16,641,915✔
1644
}
1645

1646
std::pair<vector<double>, vector<double>> RegularMesh::plot(
22✔
1647
  Position plot_ll, Position plot_ur) const
1648
{
1649
  // Figure out which axes lie in the plane of the plot.
1650
  array<int, 2> axes {-1, -1};
22✔
1651
  if (plot_ur.z == plot_ll.z) {
22!
1652
    axes[0] = 0;
22!
1653
    if (n_dimension_ > 1)
22!
1654
      axes[1] = 1;
22✔
UNCOV
1655
  } else if (plot_ur.y == plot_ll.y) {
×
UNCOV
1656
    axes[0] = 0;
×
UNCOV
1657
    if (n_dimension_ > 2)
×
UNCOV
1658
      axes[1] = 2;
×
1659
  } else if (plot_ur.x == plot_ll.x) {
×
UNCOV
1660
    if (n_dimension_ > 1)
×
UNCOV
1661
      axes[0] = 1;
×
UNCOV
1662
    if (n_dimension_ > 2)
×
UNCOV
1663
      axes[1] = 2;
×
1664
  } else {
UNCOV
1665
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1666
  }
1667

1668
  // Get the coordinates of the mesh lines along both of the axes.
1669
  array<vector<double>, 2> axis_lines;
1670
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
66✔
1671
    int axis = axes[i_ax];
44!
1672
    if (axis == -1)
44!
UNCOV
1673
      continue;
×
1674
    auto& lines {axis_lines[i_ax]};
44✔
1675

1676
    double coord = lower_left_[axis];
44✔
1677
    for (int i = 0; i < shape_[axis] + 1; ++i) {
286✔
1678
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
242!
1679
        lines.push_back(coord);
242✔
1680
      coord += width_[axis];
242✔
1681
    }
1682
  }
1683

1684
  return {axis_lines[0], axis_lines[1]};
44✔
1685
}
1686

1687
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
2,502✔
1688
{
1689
  write_dataset(mesh_group, "dimension", get_shape_tensor());
2,502✔
1690
  write_dataset(mesh_group, "lower_left", lower_left_);
2,502✔
1691
  write_dataset(mesh_group, "upper_right", upper_right_);
2,502✔
1692
  write_dataset(mesh_group, "width", width_);
2,502✔
1693
}
2,502✔
1694

1695
tensor::Tensor<double> RegularMesh::count_sites(
7,820✔
1696
  const SourceSite* bank, int64_t length, bool* outside) const
1697
{
1698
  // Determine shape of array for counts
1699
  std::size_t m = this->n_bins();
7,820✔
1700
  vector<std::size_t> shape = {m};
7,820✔
1701

1702
  // Create array of zeros
1703
  auto cnt = tensor::zeros<double>(shape);
7,820✔
1704
  bool outside_ = false;
2,892✔
1705

1706
  for (int64_t i = 0; i < length; i++) {
7,675,271✔
1707
    const auto& site = bank[i];
7,667,451✔
1708

1709
    // determine scoring bin for entropy mesh
1710
    int mesh_bin = get_bin(site.r);
7,667,451✔
1711

1712
    // if outside mesh, skip particle
1713
    if (mesh_bin < 0) {
7,667,451!
UNCOV
1714
      outside_ = true;
×
UNCOV
1715
      continue;
×
1716
    }
1717

1718
    // Add to appropriate bin
1719
    cnt(mesh_bin) += site.wgt;
7,667,451✔
1720
  }
1721

1722
  // Create reduced count data
1723
  auto counts = tensor::zeros<double>(shape);
7,820✔
1724
  int total = cnt.size();
7,820✔
1725

1726
#ifdef OPENMC_MPI
1727
  // collect values from all processors
1728
  MPI_Reduce(
2,892✔
1729
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
2,892✔
1730

1731
  // Check if there were sites outside the mesh for any processor
1732
  if (outside) {
2,892!
1733
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
2,892✔
1734
  }
1735
#else
1736
  std::copy(cnt.data(), cnt.data() + total, counts.data());
4,928✔
1737
  if (outside)
4,928!
1738
    *outside = outside_;
4,928✔
1739
#endif
1740

1741
  return counts;
7,820✔
1742
}
7,820✔
1743

1744
double RegularMesh::volume(const MeshIndex& ijk) const
1,123,862✔
1745
{
1746
  return element_volume_;
1,123,862✔
1747
}
1748

1749
//==============================================================================
1750
// RectilinearMesh implementation
1751
//==============================================================================
1752

1753
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
133✔
1754
{
1755
  n_dimension_ = 3;
133✔
1756

1757
  grid_[0] = get_node_array<double>(node, "x_grid");
133✔
1758
  grid_[1] = get_node_array<double>(node, "y_grid");
133✔
1759
  grid_[2] = get_node_array<double>(node, "z_grid");
133✔
1760

1761
  if (int err = set_grid()) {
133!
UNCOV
1762
    fatal_error(openmc_err_msg);
×
1763
  }
1764
}
133✔
1765

1766
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
11✔
1767
{
1768
  n_dimension_ = 3;
11✔
1769

1770
  read_dataset(group, "x_grid", grid_[0]);
11✔
1771
  read_dataset(group, "y_grid", grid_[1]);
11✔
1772
  read_dataset(group, "z_grid", grid_[2]);
11✔
1773

1774
  if (int err = set_grid()) {
11!
UNCOV
1775
    fatal_error(openmc_err_msg);
×
1776
  }
1777
}
11✔
1778

1779
const std::string RectilinearMesh::mesh_type = "rectilinear";
1780

1781
std::string RectilinearMesh::get_mesh_type() const
275✔
1782
{
1783
  return mesh_type;
275✔
1784
}
1785

1786
double RectilinearMesh::positive_grid_boundary(
26,506,007✔
1787
  const MeshIndex& ijk, int i) const
1788
{
1789
  return grid_[i][ijk[i]];
26,506,007✔
1790
}
1791

1792
double RectilinearMesh::negative_grid_boundary(
25,739,406✔
1793
  const MeshIndex& ijk, int i) const
1794
{
1795
  return grid_[i][ijk[i] - 1];
25,739,406✔
1796
}
1797

1798
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
53,602,252✔
1799
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1800
  double l) const
1801
{
1802
  MeshDistance d;
53,602,252✔
1803
  d.next_index = ijk[i];
53,602,252✔
1804
  if (std::abs(u[i]) < FP_PRECISION)
53,602,252✔
1805
    return d;
571,934✔
1806

1807
  d.max_surface = (u[i] > 0);
53,030,318✔
1808
  if (d.max_surface && (ijk[i] <= shape_[i])) {
53,030,318✔
1809
    d.next_index++;
26,506,007✔
1810
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
26,506,007✔
1811
  } else if (!d.max_surface && (ijk[i] > 0)) {
26,524,311✔
1812
    d.next_index--;
25,739,406✔
1813
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
25,739,406✔
1814
  }
1815
  return d;
53,030,318✔
1816
}
1817

1818
double RectilinearMesh::distance_to_mesh_boundary_from_outside(
66✔
1819
  int k, const Position& r, const Direction& u) const
1820
{
1821
  double t;
66✔
1822
  double distance = INFTY;
66✔
1823

1824
  if (u[k] > 0.0) {
66✔
1825
    t = (lower_left_[k] - r[k]) / u[k];
11✔
1826
  } else {
1827
    t = (upper_right_[k] - r[k]) / u[k];
55✔
1828
  }
1829

1830
  if (t > FP_COINCIDENT) {
66✔
1831
    bool reenter = true;
1832
    for (int i = 0; i < n_dimension_; i++) {
220✔
1833
      if (i != k) {
165✔
1834
        double a = r[i] + u[i] * t;
110✔
1835
        reenter &= (a >= lower_left_[i]);
110✔
1836
        reenter &= (a <= upper_right_[i]);
110✔
1837
      }
1838
    }
1839
    if (reenter) {
55✔
1840
      distance = t;
11✔
1841
    }
1842
  }
1843
  return distance;
66✔
1844
}
1845

1846
int RectilinearMesh::set_grid()
188✔
1847
{
1848
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
188✔
1849
    static_cast<int>(grid_[1].size()) - 1,
188✔
1850
    static_cast<int>(grid_[2].size()) - 1};
188✔
1851

1852
  for (const auto& g : grid_) {
752✔
1853
    if (g.size() < 2) {
564!
UNCOV
1854
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1855
                 "must each have at least 2 points");
UNCOV
1856
      return OPENMC_E_INVALID_ARGUMENT;
×
1857
    }
1858
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
564!
1859
        g.end()) {
564!
UNCOV
1860
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1861
                 "rectilinear meshes must be sorted and unique.");
UNCOV
1862
      return OPENMC_E_INVALID_ARGUMENT;
×
1863
    }
1864
  }
1865

1866
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
188✔
1867
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
188✔
1868

1869
  return 0;
188✔
1870
}
1871

1872
int RectilinearMesh::get_index_in_direction(double r, int i) const
74,108,925✔
1873
{
1874
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,108,925✔
1875
}
1876

1877
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
11✔
1878
  Position plot_ll, Position plot_ur) const
1879
{
1880
  // Figure out which axes lie in the plane of the plot.
1881
  array<int, 2> axes {-1, -1};
11✔
1882
  if (plot_ur.z == plot_ll.z) {
11!
UNCOV
1883
    axes = {0, 1};
×
1884
  } else if (plot_ur.y == plot_ll.y) {
11!
1885
    axes = {0, 2};
11✔
UNCOV
1886
  } else if (plot_ur.x == plot_ll.x) {
×
UNCOV
1887
    axes = {1, 2};
×
1888
  } else {
UNCOV
1889
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1890
  }
1891

1892
  // Get the coordinates of the mesh lines along both of the axes.
1893
  array<vector<double>, 2> axis_lines;
1894
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
33✔
1895
    int axis = axes[i_ax];
22✔
1896
    vector<double>& lines {axis_lines[i_ax]};
22✔
1897

1898
    for (auto coord : grid_[axis]) {
110✔
1899
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
88!
1900
        lines.push_back(coord);
88✔
1901
    }
1902
  }
1903

1904
  return {axis_lines[0], axis_lines[1]};
22✔
1905
}
1906

1907
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
110✔
1908
{
1909
  write_dataset(mesh_group, "x_grid", grid_[0]);
110✔
1910
  write_dataset(mesh_group, "y_grid", grid_[1]);
110✔
1911
  write_dataset(mesh_group, "z_grid", grid_[2]);
110✔
1912
}
110✔
1913

1914
double RectilinearMesh::volume(const MeshIndex& ijk) const
132✔
1915
{
1916
  double vol {1.0};
132✔
1917

1918
  for (int i = 0; i < n_dimension_; i++) {
528✔
1919
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
396✔
1920
  }
1921
  return vol;
132✔
1922
}
1923

1924
//==============================================================================
1925
// CylindricalMesh implementation
1926
//==============================================================================
1927

1928
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
400✔
1929
  : PeriodicStructuredMesh {node}
400✔
1930
{
1931
  n_dimension_ = 3;
400✔
1932
  grid_[0] = get_node_array<double>(node, "r_grid");
400✔
1933
  grid_[1] = get_node_array<double>(node, "phi_grid");
400✔
1934
  grid_[2] = get_node_array<double>(node, "z_grid");
400✔
1935
  origin_ = get_node_position(node, "origin");
400✔
1936

1937
  if (int err = set_grid()) {
400!
UNCOV
1938
    fatal_error(openmc_err_msg);
×
1939
  }
1940
}
400✔
1941

1942
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1943
{
1944
  n_dimension_ = 3;
11✔
1945
  read_dataset(group, "r_grid", grid_[0]);
11✔
1946
  read_dataset(group, "phi_grid", grid_[1]);
11✔
1947
  read_dataset(group, "z_grid", grid_[2]);
11✔
1948
  read_dataset(group, "origin", origin_);
11✔
1949

1950
  if (int err = set_grid()) {
11!
UNCOV
1951
    fatal_error(openmc_err_msg);
×
1952
  }
1953
}
11✔
1954

1955
const std::string CylindricalMesh::mesh_type = "cylindrical";
1956

1957
std::string CylindricalMesh::get_mesh_type() const
484✔
1958
{
1959
  return mesh_type;
484✔
1960
}
1961

1962
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
47,732,091✔
1963
  Position r, bool& in_mesh) const
1964
{
1965
  r = local_coords(r);
47,732,091✔
1966

1967
  Position mapped_r;
47,732,091✔
1968
  mapped_r[0] = std::hypot(r.x, r.y);
47,732,091✔
1969
  mapped_r[2] = r[2];
47,732,091✔
1970

1971
  if (mapped_r[0] < FP_PRECISION) {
47,732,091!
1972
    mapped_r[1] = 0.0;
1973
  } else {
1974
    mapped_r[1] = std::atan2(r.y, r.x);
47,732,091✔
1975
    if (mapped_r[1] < 0)
47,732,091✔
1976
      mapped_r[1] += 2 * M_PI;
23,874,862✔
1977
  }
1978

1979
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
47,732,091✔
1980

1981
  idx[1] = sanitize_phi(idx[1]);
47,732,091✔
1982

1983
  return idx;
47,732,091✔
1984
}
1985

1986
Position CylindricalMesh::sample_element(
88,110✔
1987
  const MeshIndex& ijk, uint64_t* seed) const
1988
{
1989
  double r_min = this->r(ijk[0] - 1);
88,110✔
1990
  double r_max = this->r(ijk[0]);
88,110✔
1991

1992
  double phi_min = this->phi(ijk[1] - 1);
88,110✔
1993
  double phi_max = this->phi(ijk[1]);
88,110✔
1994

1995
  double z_min = this->z(ijk[2] - 1);
88,110✔
1996
  double z_max = this->z(ijk[2]);
88,110✔
1997

1998
  double r_min_sq = r_min * r_min;
88,110✔
1999
  double r_max_sq = r_max * r_max;
88,110✔
2000
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
88,110✔
2001
  double phi = uniform_distribution(phi_min, phi_max, seed);
88,110✔
2002
  double z = uniform_distribution(z_min, z_max, seed);
88,110✔
2003

2004
  double x = r * std::cos(phi);
88,110✔
2005
  double y = r * std::sin(phi);
88,110✔
2006

2007
  return origin_ + Position(x, y, z);
88,110✔
2008
}
2009

2010
double CylindricalMesh::find_r_crossing(
142,585,674✔
2011
  const Position& r, const Direction& u, double l, int shell) const
2012
{
2013

2014
  if ((shell < 0) || (shell > shape_[0]))
142,585,674!
2015
    return INFTY;
2016

2017
  // solve r.x^2 + r.y^2 == r0^2
2018
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
2019
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
2020

2021
  const double r0 = grid_[0][shell];
124,671,855✔
2022
  if (r0 == 0.0)
124,671,855✔
2023
    return INFTY;
2024

2025
  const double denominator = u.x * u.x + u.y * u.y;
117,535,781✔
2026

2027
  // Direction of flight is in z-direction. Will never intersect r.
2028
  if (std::abs(denominator) < FP_PRECISION)
117,535,781✔
2029
    return INFTY;
2030

2031
  // inverse of dominator to help the compiler to speed things up
2032
  const double inv_denominator = 1.0 / denominator;
117,476,821✔
2033

2034
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,476,821✔
2035
  double R = std::sqrt(r.x * r.x + r.y * r.y);
117,476,821✔
2036
  double D = p * p - (R - r0) * (R + r0) * inv_denominator;
117,476,821✔
2037

2038
  if (D < 0.0)
117,476,821✔
2039
    return INFTY;
2040

2041
  D = std::sqrt(D);
107,740,699✔
2042

2043
  // Particle is already on the shell surface; avoid spurious crossing
2044
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
107,740,699✔
2045
    return INFTY;
2046

2047
  // Check -p - D first because it is always smaller as -p + D
2048
  if (-p - D > l)
101,107,325✔
2049
    return -p - D;
2050
  if (-p + D > l)
80,899,742✔
2051
    return -p + D;
50,077,247✔
2052

2053
  return INFTY;
2054
}
2055

2056
double CylindricalMesh::find_phi_crossing(
74,456,404✔
2057
  const Position& r, const Direction& u, double l, int shell) const
2058
{
2059
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2060
  if (full_phi_ && (shape_[1] == 1))
74,456,404✔
2061
    return INFTY;
2062

2063
  shell = sanitize_phi(shell);
43,970,718✔
2064

2065
  const double p0 = grid_[1][shell];
43,970,718✔
2066

2067
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2068
  // => x(s) * cos(p0) = y(s) * sin(p0)
2069
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2070
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2071

2072
  const double c0 = std::cos(p0);
43,970,718✔
2073
  const double s0 = std::sin(p0);
43,970,718✔
2074

2075
  const double denominator = (u.x * s0 - u.y * c0);
43,970,718✔
2076

2077
  // Check if direction of flight is not parallel to phi surface
2078
  if (std::abs(denominator) > FP_PRECISION) {
43,970,718✔
2079
    const double s = -(r.x * s0 - r.y * c0) / denominator;
43,709,974✔
2080
    // Check if solution is in positive direction of flight and crosses the
2081
    // correct phi surface (not -phi)
2082
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
43,709,974✔
2083
      return s;
20,219,859✔
2084
  }
2085

2086
  return INFTY;
2087
}
2088

2089
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
36,695,747✔
2090
  const Position& r, const Direction& u, double l, int shell) const
2091
{
2092
  MeshDistance d;
36,695,747✔
2093
  d.next_index = shell;
36,695,747✔
2094

2095
  // Direction of flight is within xy-plane. Will never intersect z.
2096
  if (std::abs(u.z) < FP_PRECISION)
36,695,747✔
2097
    return d;
1,118,216✔
2098

2099
  d.max_surface = (u.z > 0.0);
35,577,531✔
2100
  if (d.max_surface && (shell <= shape_[2])) {
35,577,531✔
2101
    d.next_index += 1;
16,875,892✔
2102
    d.distance = (grid_[2][shell] - r.z) / u.z;
16,875,892✔
2103
  } else if (!d.max_surface && (shell > 0)) {
18,701,639✔
2104
    d.next_index -= 1;
16,846,225✔
2105
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
16,846,225✔
2106
  }
2107
  return d;
35,577,531✔
2108
}
2109

2110
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,216,786✔
2111
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2112
  double l) const
2113
{
2114
  if (i == 0) {
145,216,786✔
2115

2116
    return std::min(
142,585,674✔
2117
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
71,292,837✔
2118
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,585,674✔
2119

2120
  } else if (i == 1) {
73,923,949✔
2121

2122
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
37,228,202✔
2123
                      find_phi_crossing(r0, u, l, ijk[i])),
37,228,202✔
2124
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
37,228,202✔
2125
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
74,456,404✔
2126

2127
  } else {
2128
    return find_z_crossing(r0, u, l, ijk[i]);
36,695,747✔
2129
  }
2130
}
2131

2132
int CylindricalMesh::set_grid()
433✔
2133
{
2134
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
433✔
2135
    static_cast<int>(grid_[1].size()) - 1,
433✔
2136
    static_cast<int>(grid_[2].size()) - 1};
433✔
2137

2138
  for (const auto& g : grid_) {
1,732✔
2139
    if (g.size() < 2) {
1,299!
2140
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
2141
                 "must each have at least 2 points");
UNCOV
2142
      return OPENMC_E_INVALID_ARGUMENT;
×
2143
    }
2144
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,299!
2145
        g.end()) {
1,299!
UNCOV
2146
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
2147
                 "cylindrical meshes must be sorted and unique.");
2148
      return OPENMC_E_INVALID_ARGUMENT;
×
2149
    }
2150
  }
2151
  if (grid_[0].front() < 0.0) {
433!
UNCOV
2152
    set_errmsg("r-grid for "
×
2153
               "cylindrical meshes must start at r >= 0.");
UNCOV
2154
    return OPENMC_E_INVALID_ARGUMENT;
×
2155
  }
2156
  if (grid_[1].front() < 0.0) {
433!
UNCOV
2157
    set_errmsg("phi-grid for "
×
2158
               "cylindrical meshes must start at phi >= 0.");
UNCOV
2159
    return OPENMC_E_INVALID_ARGUMENT;
×
2160
  }
2161
  if (grid_[1].back() > 2.0 * PI) {
433!
UNCOV
2162
    set_errmsg("phi-grids for "
×
2163
               "cylindrical meshes must end with theta <= 2*pi.");
2164

UNCOV
2165
    return OPENMC_E_INVALID_ARGUMENT;
×
2166
  }
2167

2168
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
433!
2169

2170
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
433✔
2171
    origin_[2] + grid_[2].front()};
433✔
2172
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
433✔
2173
    origin_[2] + grid_[2].back()};
433✔
2174

2175
  return 0;
433✔
2176
}
2177

2178
int CylindricalMesh::get_index_in_direction(double r, int i) const
143,196,273✔
2179
{
2180
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
143,196,273✔
2181
}
2182

UNCOV
2183
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
2184
  Position plot_ll, Position plot_ur) const
2185
{
UNCOV
2186
  fatal_error("Plot of cylindrical Mesh not implemented");
×
2187

2188
  // Figure out which axes lie in the plane of the plot.
2189
  array<vector<double>, 2> axis_lines;
2190
  return {axis_lines[0], axis_lines[1]};
2191
}
2192

2193
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
374✔
2194
{
2195
  write_dataset(mesh_group, "r_grid", grid_[0]);
374✔
2196
  write_dataset(mesh_group, "phi_grid", grid_[1]);
374✔
2197
  write_dataset(mesh_group, "z_grid", grid_[2]);
374✔
2198
  write_dataset(mesh_group, "origin", origin_);
374✔
2199
}
374✔
2200

2201
double CylindricalMesh::volume(const MeshIndex& ijk) const
792✔
2202
{
2203
  double r_i = grid_[0][ijk[0] - 1];
792✔
2204
  double r_o = grid_[0][ijk[0]];
792✔
2205

2206
  double phi_i = grid_[1][ijk[1] - 1];
792✔
2207
  double phi_o = grid_[1][ijk[1]];
792✔
2208

2209
  double z_i = grid_[2][ijk[2] - 1];
792✔
2210
  double z_o = grid_[2][ijk[2]];
792✔
2211

2212
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
792✔
2213
}
2214

2215
//==============================================================================
2216
// SphericalMesh implementation
2217
//==============================================================================
2218

2219
SphericalMesh::SphericalMesh(pugi::xml_node node)
345✔
2220
  : PeriodicStructuredMesh {node}
345✔
2221
{
2222
  n_dimension_ = 3;
345✔
2223

2224
  grid_[0] = get_node_array<double>(node, "r_grid");
345✔
2225
  grid_[1] = get_node_array<double>(node, "theta_grid");
345✔
2226
  grid_[2] = get_node_array<double>(node, "phi_grid");
345✔
2227
  origin_ = get_node_position(node, "origin");
345✔
2228

2229
  if (int err = set_grid()) {
345!
2230
    fatal_error(openmc_err_msg);
×
2231
  }
2232
}
345✔
2233

2234
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
2235
{
2236
  n_dimension_ = 3;
11✔
2237

2238
  read_dataset(group, "r_grid", grid_[0]);
11✔
2239
  read_dataset(group, "theta_grid", grid_[1]);
11✔
2240
  read_dataset(group, "phi_grid", grid_[2]);
11✔
2241
  read_dataset(group, "origin", origin_);
11✔
2242

2243
  if (int err = set_grid()) {
11!
UNCOV
2244
    fatal_error(openmc_err_msg);
×
2245
  }
2246
}
11✔
2247

2248
const std::string SphericalMesh::mesh_type = "spherical";
2249

2250
std::string SphericalMesh::get_mesh_type() const
385✔
2251
{
2252
  return mesh_type;
385✔
2253
}
2254

2255
StructuredMesh::MeshIndex SphericalMesh::get_indices(
68,592,128✔
2256
  Position r, bool& in_mesh) const
2257
{
2258
  r = local_coords(r);
68,592,128✔
2259

2260
  Position mapped_r;
68,592,128✔
2261
  mapped_r[0] = r.norm();
68,592,128✔
2262

2263
  if (mapped_r[0] < FP_PRECISION) {
68,592,128!
2264
    mapped_r[1] = 0.0;
2265
    mapped_r[2] = 0.0;
2266
  } else {
2267
    mapped_r[1] = std::acos(r.z / mapped_r.x);
68,592,128✔
2268
    mapped_r[2] = std::atan2(r.y, r.x);
68,592,128✔
2269
    if (mapped_r[2] < 0)
68,592,128✔
2270
      mapped_r[2] += 2 * M_PI;
34,268,685✔
2271
  }
2272

2273
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
68,592,128✔
2274

2275
  idx[1] = sanitize_theta(idx[1]);
68,592,128✔
2276
  idx[2] = sanitize_phi(idx[2]);
68,592,128✔
2277

2278
  return idx;
68,592,128✔
2279
}
2280

2281
Position SphericalMesh::sample_element(
110✔
2282
  const MeshIndex& ijk, uint64_t* seed) const
2283
{
2284
  double r_min = this->r(ijk[0] - 1);
110✔
2285
  double r_max = this->r(ijk[0]);
110✔
2286

2287
  double theta_min = this->theta(ijk[1] - 1);
110✔
2288
  double theta_max = this->theta(ijk[1]);
110✔
2289

2290
  double phi_min = this->phi(ijk[2] - 1);
110✔
2291
  double phi_max = this->phi(ijk[2]);
110✔
2292

2293
  double cos_theta =
110✔
2294
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
110✔
2295
  double sin_theta = std::sin(std::acos(cos_theta));
110✔
2296
  double phi = uniform_distribution(phi_min, phi_max, seed);
110✔
2297
  double r_min_cub = std::pow(r_min, 3);
110✔
2298
  double r_max_cub = std::pow(r_max, 3);
110✔
2299
  // might be faster to do rejection here?
2300
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110✔
2301

2302
  double x = r * std::cos(phi) * sin_theta;
110✔
2303
  double y = r * std::sin(phi) * sin_theta;
110✔
2304
  double z = r * cos_theta;
110✔
2305

2306
  return origin_ + Position(x, y, z);
110✔
2307
}
2308

2309
double SphericalMesh::find_r_crossing(
443,967,392✔
2310
  const Position& r, const Direction& u, double l, int shell) const
2311
{
2312
  if ((shell < 0) || (shell > shape_[0]))
443,967,392✔
2313
    return INFTY;
2314

2315
  // solve |r+s*u| = r0
2316
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2317
  const double r0 = grid_[0][shell];
404,346,305✔
2318
  if (r0 == 0.0)
404,346,305✔
2319
    return INFTY;
2320
  const double p = r.dot(u);
396,667,788✔
2321
  double R = r.norm();
396,667,788✔
2322
  double D = p * p - (R - r0) * (R + r0);
396,667,788✔
2323

2324
  // Particle is already on the shell surface; avoid spurious crossing
2325
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
396,667,788✔
2326
    return INFTY;
2327

2328
  if (D >= 0.0) {
385,959,134✔
2329
    D = std::sqrt(D);
358,082,186✔
2330
    // Check -p - D first because it is always smaller as -p + D
2331
    if (-p - D > l)
358,082,186✔
2332
      return -p - D;
2333
    if (-p + D > l)
293,772,622✔
2334
      return -p + D;
177,234,882✔
2335
  }
2336

2337
  return INFTY;
2338
}
2339

2340
double SphericalMesh::find_theta_crossing(
110,161,348✔
2341
  const Position& r, const Direction& u, double l, int shell) const
2342
{
2343
  // Theta grid is [0, π], thus there is no real surface to cross
2344
  if (full_theta_ && (shape_[1] == 1))
110,161,348✔
2345
    return INFTY;
2346

2347
  shell = sanitize_theta(shell);
38,358,540✔
2348

2349
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2350
  // yields
2351
  // a*s^2 + 2*b*s + c == 0 with
2352
  // a = cos(theta)^2 - u.z * u.z
2353
  // b = r*u * cos(theta)^2 - u.z * r.z
2354
  // c = r*r * cos(theta)^2 - r.z^2
2355

2356
  const double cos_t = std::cos(grid_[1][shell]);
38,358,540✔
2357
  const bool sgn = std::signbit(cos_t);
38,358,540✔
2358
  const double cos_t_2 = cos_t * cos_t;
38,358,540✔
2359

2360
  const double a = cos_t_2 - u.z * u.z;
38,358,540✔
2361
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
38,358,540✔
2362
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
38,358,540✔
2363

2364
  // if factor of s^2 is zero, direction of flight is parallel to theta
2365
  // surface
2366
  if (std::abs(a) < FP_PRECISION) {
38,358,540✔
2367
    // if b vanishes, direction of flight is within theta surface and crossing
2368
    // is not possible
2369
    if (std::abs(b) < FP_PRECISION)
482,548!
2370
      return INFTY;
2371

UNCOV
2372
    const double s = -0.5 * c / b;
×
2373
    // Check if solution is in positive direction of flight and has correct
2374
    // sign
UNCOV
2375
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
UNCOV
2376
      return s;
×
2377

2378
    // no crossing is possible
2379
    return INFTY;
2380
  }
2381

2382
  const double p = b / a;
37,875,992✔
2383
  double D = p * p - c / a;
37,875,992✔
2384

2385
  if (D < 0.0)
37,875,992✔
2386
    return INFTY;
2387

2388
  D = std::sqrt(D);
26,921,004✔
2389

2390
  // the solution -p-D is always smaller as -p+D : Check this one first
2391
  double s = -p - D;
26,921,004✔
2392
  // Check if solution is in positive direction of flight and has correct sign
2393
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
26,921,004✔
2394
    return s;
2395

2396
  s = -p + D;
21,638,397✔
2397
  // Check if solution is in positive direction of flight and has correct sign
2398
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
21,638,397✔
2399
    return s;
10,163,296✔
2400

2401
  return INFTY;
2402
}
2403

2404
double SphericalMesh::find_phi_crossing(
111,750,826✔
2405
  const Position& r, const Direction& u, double l, int shell) const
2406
{
2407
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2408
  if (full_phi_ && (shape_[2] == 1))
111,750,826✔
2409
    return INFTY;
2410

2411
  shell = sanitize_phi(shell);
39,948,018✔
2412

2413
  const double p0 = grid_[2][shell];
39,948,018✔
2414

2415
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2416
  // => x(s) * cos(p0) = y(s) * sin(p0)
2417
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2418
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2419

2420
  const double c0 = std::cos(p0);
39,948,018✔
2421
  const double s0 = std::sin(p0);
39,948,018✔
2422

2423
  const double denominator = (u.x * s0 - u.y * c0);
39,948,018✔
2424

2425
  // Check if direction of flight is not parallel to phi surface
2426
  if (std::abs(denominator) > FP_PRECISION) {
39,948,018✔
2427
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,714,026✔
2428
    // Check if solution is in positive direction of flight and crosses the
2429
    // correct phi surface (not -phi)
2430
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,714,026✔
2431
      return s;
17,579,452✔
2432
  }
2433

2434
  return INFTY;
2435
}
2436

2437
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
332,939,783✔
2438
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2439
  double l) const
2440
{
2441

2442
  if (i == 0) {
332,939,783✔
2443
    return std::min(
443,967,392✔
2444
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
221,983,696✔
2445
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
443,967,392✔
2446

2447
  } else if (i == 1) {
110,956,087✔
2448
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
55,080,674✔
2449
                      find_theta_crossing(r0, u, l, ijk[i])),
55,080,674✔
2450
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
55,080,674✔
2451
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
110,161,348✔
2452

2453
  } else {
2454
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
55,875,413✔
2455
                      find_phi_crossing(r0, u, l, ijk[i])),
55,875,413✔
2456
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
55,875,413✔
2457
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
111,750,826✔
2458
  }
2459
}
2460

2461
int SphericalMesh::set_grid()
378✔
2462
{
2463
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
378✔
2464
    static_cast<int>(grid_[1].size()) - 1,
378✔
2465
    static_cast<int>(grid_[2].size()) - 1};
378✔
2466

2467
  for (const auto& g : grid_) {
1,512✔
2468
    if (g.size() < 2) {
1,134!
UNCOV
2469
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2470
                 "must each have at least 2 points");
UNCOV
2471
      return OPENMC_E_INVALID_ARGUMENT;
×
2472
    }
2473
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,134!
2474
        g.end()) {
1,134!
2475
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2476
                 "spherical meshes must be sorted and unique.");
UNCOV
2477
      return OPENMC_E_INVALID_ARGUMENT;
×
2478
    }
2479
    if (g.front() < 0.0) {
1,134!
2480
      set_errmsg("r-, theta-, and phi- grids for "
×
2481
                 "spherical meshes must start at v >= 0.");
UNCOV
2482
      return OPENMC_E_INVALID_ARGUMENT;
×
2483
    }
2484
  }
2485
  if (grid_[1].back() > PI) {
378!
UNCOV
2486
    set_errmsg("theta-grids for "
×
2487
               "spherical meshes must end with theta <= pi.");
2488

UNCOV
2489
    return OPENMC_E_INVALID_ARGUMENT;
×
2490
  }
2491
  if (grid_[2].back() > 2 * PI) {
378!
UNCOV
2492
    set_errmsg("phi-grids for "
×
2493
               "spherical meshes must end with phi <= 2*pi.");
UNCOV
2494
    return OPENMC_E_INVALID_ARGUMENT;
×
2495
  }
2496

2497
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
378!
2498
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
378✔
2499

2500
  double r = grid_[0].back();
378✔
2501
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
378✔
2502
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
378✔
2503

2504
  return 0;
378✔
2505
}
2506

2507
int SphericalMesh::get_index_in_direction(double r, int i) const
205,776,384✔
2508
{
2509
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
205,776,384✔
2510
}
2511

UNCOV
2512
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2513
  Position plot_ll, Position plot_ur) const
2514
{
UNCOV
2515
  fatal_error("Plot of spherical Mesh not implemented");
×
2516

2517
  // Figure out which axes lie in the plane of the plot.
2518
  array<vector<double>, 2> axis_lines;
2519
  return {axis_lines[0], axis_lines[1]};
2520
}
2521

2522
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
319✔
2523
{
2524
  write_dataset(mesh_group, "r_grid", grid_[0]);
319✔
2525
  write_dataset(mesh_group, "theta_grid", grid_[1]);
319✔
2526
  write_dataset(mesh_group, "phi_grid", grid_[2]);
319✔
2527
  write_dataset(mesh_group, "origin", origin_);
319✔
2528
}
319✔
2529

2530
double SphericalMesh::volume(const MeshIndex& ijk) const
935✔
2531
{
2532
  double r_i = grid_[0][ijk[0] - 1];
935✔
2533
  double r_o = grid_[0][ijk[0]];
935✔
2534

2535
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2536
  double theta_o = grid_[1][ijk[1]];
935✔
2537

2538
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2539
  double phi_o = grid_[2][ijk[2]];
935✔
2540

2541
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
1,870✔
2542
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
935✔
2543
}
2544

2545
//==============================================================================
2546
// Helper functions for the C API
2547
//==============================================================================
2548

2549
int check_mesh(int32_t index)
6,490✔
2550
{
2551
  if (index < 0 || index >= model::meshes.size()) {
6,490!
2552
    set_errmsg("Index in meshes array is out of bounds.");
×
2553
    return OPENMC_E_OUT_OF_BOUNDS;
×
2554
  }
2555
  return 0;
2556
}
2557

2558
template<class T>
2559
int check_mesh_type(int32_t index)
1,100✔
2560
{
2561
  if (int err = check_mesh(index))
1,100!
2562
    return err;
2563

2564
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,100!
2565
  if (!mesh) {
1,100!
UNCOV
2566
    set_errmsg("This function is not valid for input mesh.");
×
UNCOV
2567
    return OPENMC_E_INVALID_TYPE;
×
2568
  }
2569
  return 0;
2570
}
2571

2572
template<class T>
2573
bool is_mesh_type(int32_t index)
2574
{
2575
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2576
  return mesh;
2577
}
2578

2579
//==============================================================================
2580
// C API functions
2581
//==============================================================================
2582

2583
// Return the type of mesh as a C string
2584
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,496✔
2585
{
2586
  if (int err = check_mesh(index))
1,496!
2587
    return err;
2588

2589
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
1,496✔
2590

2591
  return 0;
1,496✔
2592
}
2593

2594
//! Extend the meshes array by n elements
2595
extern "C" int openmc_extend_meshes(
253✔
2596
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2597
{
2598
  if (index_start)
253!
2599
    *index_start = model::meshes.size();
253✔
2600
  std::string mesh_type;
253✔
2601

2602
  for (int i = 0; i < n; ++i) {
506✔
2603
    if (RegularMesh::mesh_type == type) {
253✔
2604
      model::meshes.push_back(make_unique<RegularMesh>());
165✔
2605
    } else if (RectilinearMesh::mesh_type == type) {
88✔
2606
      model::meshes.push_back(make_unique<RectilinearMesh>());
44✔
2607
    } else if (CylindricalMesh::mesh_type == type) {
44✔
2608
      model::meshes.push_back(make_unique<CylindricalMesh>());
22✔
2609
    } else if (SphericalMesh::mesh_type == type) {
22!
2610
      model::meshes.push_back(make_unique<SphericalMesh>());
22✔
2611
    } else {
2612
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2613
    }
2614
  }
2615
  if (index_end)
253!
UNCOV
2616
    *index_end = model::meshes.size() - 1;
×
2617

2618
  return 0;
253✔
2619
}
253✔
2620

2621
//! Adds a new unstructured mesh to OpenMC
UNCOV
2622
extern "C" int openmc_add_unstructured_mesh(
×
2623
  const char filename[], const char library[], int* id)
2624
{
UNCOV
2625
  std::string lib_name(library);
×
UNCOV
2626
  std::string mesh_file(filename);
×
UNCOV
2627
  bool valid_lib = false;
×
2628

2629
#ifdef OPENMC_DAGMC_ENABLED
2630
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2631
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2632
    valid_lib = true;
2633
  }
2634
#endif
2635

2636
#ifdef OPENMC_LIBMESH_ENABLED
2637
  if (lib_name == LibMesh::mesh_lib_type) {
×
2638
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2639
    valid_lib = true;
2640
  }
2641
#endif
2642

UNCOV
2643
  if (!valid_lib) {
×
UNCOV
2644
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2645
                           "by this build of OpenMC",
2646
      lib_name));
UNCOV
2647
    return OPENMC_E_INVALID_ARGUMENT;
×
2648
  }
2649

2650
  // auto-assign new ID
2651
  model::meshes.back()->set_id(-1);
×
2652
  *id = model::meshes.back()->id_;
2653

2654
  return 0;
UNCOV
2655
}
×
2656

2657
//! Return the index in the meshes array of a mesh with a given ID
2658
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
429✔
2659
{
2660
  auto pair = model::mesh_map.find(id);
429!
2661
  if (pair == model::mesh_map.end()) {
429!
UNCOV
2662
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
UNCOV
2663
    return OPENMC_E_INVALID_ID;
×
2664
  }
2665
  *index = pair->second;
429✔
2666
  return 0;
429✔
2667
}
2668

2669
//! Return the ID of a mesh
2670
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,827✔
2671
{
2672
  if (int err = check_mesh(index))
2,827!
2673
    return err;
2674
  *id = model::meshes[index]->id_;
2,827✔
2675
  return 0;
2,827✔
2676
}
2677

2678
//! Set the ID of a mesh
2679
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
253✔
2680
{
2681
  if (int err = check_mesh(index))
253!
2682
    return err;
2683
  model::meshes[index]->id_ = id;
253✔
2684
  model::mesh_map[id] = index;
253✔
2685
  return 0;
253✔
2686
}
2687

2688
//! Get the number of elements in a mesh
2689
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
297✔
2690
{
2691
  if (int err = check_mesh(index))
297!
2692
    return err;
2693
  *n = model::meshes[index]->n_bins();
297✔
2694
  return 0;
297✔
2695
}
2696

2697
//! Get the volume of each element in the mesh
2698
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
88✔
2699
{
2700
  if (int err = check_mesh(index))
88!
2701
    return err;
2702
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
968✔
2703
    volumes[i] = model::meshes[index]->volume(i);
880✔
2704
  }
2705
  return 0;
2706
}
2707

2708
//! Get the bounding box of a mesh
2709
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
176✔
2710
{
2711
  if (int err = check_mesh(index))
176!
2712
    return err;
2713

2714
  BoundingBox bbox = model::meshes[index]->bounding_box();
176✔
2715

2716
  // set lower left corner values
2717
  ll[0] = bbox.min.x;
176✔
2718
  ll[1] = bbox.min.y;
176✔
2719
  ll[2] = bbox.min.z;
176✔
2720

2721
  // set upper right corner values
2722
  ur[0] = bbox.max.x;
176✔
2723
  ur[1] = bbox.max.y;
176✔
2724
  ur[2] = bbox.max.z;
176✔
2725
  return 0;
176✔
2726
}
2727

2728
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
209✔
2729
  int nz, int table_size, int32_t* materials, double* volumes, double* bboxes)
2730
{
2731
  if (int err = check_mesh(index))
209!
2732
    return err;
2733

2734
  try {
209✔
2735
    model::meshes[index]->material_volumes(
209✔
2736
      nx, ny, nz, table_size, materials, volumes, bboxes);
2737
  } catch (const std::exception& e) {
11!
2738
    set_errmsg(e.what());
11✔
2739
    if (starts_with(e.what(), "Mesh")) {
11!
2740
      return OPENMC_E_GEOMETRY;
11✔
2741
    } else {
UNCOV
2742
      return OPENMC_E_ALLOCATE;
×
2743
    }
2744
  }
11✔
2745

2746
  return 0;
2747
}
2748

2749
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
44✔
2750
  Position width, int basis, int* pixels, int32_t* data)
2751
{
2752
  if (int err = check_mesh(index))
44!
2753
    return err;
2754
  const auto& mesh = model::meshes[index].get();
44!
2755

2756
  int pixel_width = pixels[0];
44✔
2757
  int pixel_height = pixels[1];
44✔
2758

2759
  // get pixel size
2760
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
44✔
2761
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
44✔
2762

2763
  // setup basis indices and initial position centered on pixel
2764
  int in_i, out_i;
44✔
2765
  Position xyz = origin;
44✔
2766
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
44✔
2767
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
44✔
2768
  switch (basis_enum) {
44!
2769
  case PlotBasis::xy:
2770
    in_i = 0;
2771
    out_i = 1;
2772
    break;
2773
  case PlotBasis::xz:
2774
    in_i = 0;
2775
    out_i = 2;
2776
    break;
2777
  case PlotBasis::yz:
2778
    in_i = 1;
2779
    out_i = 2;
2780
    break;
UNCOV
2781
  default:
×
UNCOV
2782
    UNREACHABLE();
×
2783
  }
2784

2785
  // set initial position
2786
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
44✔
2787
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
44✔
2788

2789
#pragma omp parallel
24✔
2790
  {
20✔
2791
    Position r = xyz;
20✔
2792

2793
#pragma omp for
2794
    for (int y = 0; y < pixel_height; y++) {
420✔
2795
      r[out_i] = xyz[out_i] - out_pixel * y;
400✔
2796
      for (int x = 0; x < pixel_width; x++) {
8,400✔
2797
        r[in_i] = xyz[in_i] + in_pixel * x;
8,000✔
2798
        data[pixel_width * y + x] = mesh->get_bin(r);
8,000✔
2799
      }
2800
    }
2801
  }
2802

2803
  return 0;
44✔
2804
}
2805

2806
//! Get the dimension of a regular mesh
2807
extern "C" int openmc_regular_mesh_get_dimension(
11✔
2808
  int32_t index, int** dims, int* n)
2809
{
2810
  if (int err = check_mesh_type<RegularMesh>(index))
11!
2811
    return err;
2812
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
11!
2813
  *dims = mesh->shape_.data();
11✔
2814
  *n = mesh->n_dimension_;
11✔
2815
  return 0;
11✔
2816
}
2817

2818
//! Set the dimension of a regular mesh
2819
extern "C" int openmc_regular_mesh_set_dimension(
187✔
2820
  int32_t index, int n, const int* dims)
2821
{
2822
  if (int err = check_mesh_type<RegularMesh>(index))
187!
2823
    return err;
2824
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
187!
2825

2826
  // Copy dimension
2827
  mesh->n_dimension_ = n;
187✔
2828
  std::copy(dims, dims + n, mesh->shape_.begin());
187✔
2829
  return 0;
187✔
2830
}
2831

2832
//! Get the regular mesh parameters
2833
extern "C" int openmc_regular_mesh_get_params(
209✔
2834
  int32_t index, double** ll, double** ur, double** width, int* n)
2835
{
2836
  if (int err = check_mesh_type<RegularMesh>(index))
209!
2837
    return err;
2838
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
209!
2839

2840
  if (m->lower_left_.empty()) {
209!
UNCOV
2841
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2842
    return OPENMC_E_ALLOCATE;
×
2843
  }
2844

2845
  *ll = m->lower_left_.data();
209✔
2846
  *ur = m->upper_right_.data();
209✔
2847
  *width = m->width_.data();
209✔
2848
  *n = m->n_dimension_;
209✔
2849
  return 0;
209✔
2850
}
2851

2852
//! Set the regular mesh parameters
2853
extern "C" int openmc_regular_mesh_set_params(
220✔
2854
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2855
{
2856
  if (int err = check_mesh_type<RegularMesh>(index))
220!
2857
    return err;
2858
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
220!
2859

2860
  if (m->n_dimension_ == -1) {
220!
UNCOV
2861
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
UNCOV
2862
    return OPENMC_E_UNASSIGNED;
×
2863
  }
2864

2865
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
220✔
2866
  if (ll && ur) {
220✔
2867
    m->lower_left_ = tensor::Tensor<double>(ll, n);
198✔
2868
    m->upper_right_ = tensor::Tensor<double>(ur, n);
198✔
2869
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_shape_tensor();
792✔
2870
  } else if (ll && width) {
22✔
2871
    m->lower_left_ = tensor::Tensor<double>(ll, n);
11✔
2872
    m->width_ = tensor::Tensor<double>(width, n);
11✔
2873
    m->upper_right_ = m->lower_left_ + m->get_shape_tensor() * m->width_;
44✔
2874
  } else if (ur && width) {
11!
2875
    m->upper_right_ = tensor::Tensor<double>(ur, n);
11✔
2876
    m->width_ = tensor::Tensor<double>(width, n);
11✔
2877
    m->lower_left_ = m->upper_right_ - m->get_shape_tensor() * m->width_;
44✔
2878
  } else {
UNCOV
2879
    set_errmsg("At least two parameters must be specified.");
×
UNCOV
2880
    return OPENMC_E_INVALID_ARGUMENT;
×
2881
  }
2882

2883
  // Set material volumes
2884

2885
  // TODO: incorporate this into method in RegularMesh that can be called from
2886
  // here and from constructor
2887
  m->volume_frac_ = 1.0 / m->get_shape_tensor().prod();
220✔
2888
  m->element_volume_ = 1.0;
220✔
2889
  for (int i = 0; i < m->n_dimension_; i++) {
880✔
2890
    m->element_volume_ *= m->width_[i];
660✔
2891
  }
2892

2893
  return 0;
2894
}
220✔
2895

2896
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2897
template<class C>
2898
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
88✔
2899
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2900
  const int nz)
2901
{
2902
  if (int err = check_mesh_type<C>(index))
88!
2903
    return err;
2904

2905
  C* m = dynamic_cast<C*>(model::meshes[index].get());
88!
2906

2907
  m->n_dimension_ = 3;
88✔
2908

2909
  m->grid_[0].reserve(nx);
88✔
2910
  m->grid_[1].reserve(ny);
88✔
2911
  m->grid_[2].reserve(nz);
88✔
2912

2913
  for (int i = 0; i < nx; i++) {
572✔
2914
    m->grid_[0].push_back(grid_x[i]);
484✔
2915
  }
2916
  for (int i = 0; i < ny; i++) {
341✔
2917
    m->grid_[1].push_back(grid_y[i]);
253✔
2918
  }
2919
  for (int i = 0; i < nz; i++) {
319✔
2920
    m->grid_[2].push_back(grid_z[i]);
231✔
2921
  }
2922

2923
  int err = m->set_grid();
88✔
2924
  return err;
88✔
2925
}
2926

2927
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2928
template<class C>
2929
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
385✔
2930
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2931
{
2932
  if (int err = check_mesh_type<C>(index))
385!
2933
    return err;
2934
  C* m = dynamic_cast<C*>(model::meshes[index].get());
385!
2935

2936
  if (m->lower_left_.empty()) {
385!
UNCOV
2937
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2938
    return OPENMC_E_ALLOCATE;
×
2939
  }
2940

2941
  *grid_x = m->grid_[0].data();
385✔
2942
  *nx = m->grid_[0].size();
385✔
2943
  *grid_y = m->grid_[1].data();
385✔
2944
  *ny = m->grid_[1].size();
385✔
2945
  *grid_z = m->grid_[2].data();
385✔
2946
  *nz = m->grid_[2].size();
385✔
2947

2948
  return 0;
385✔
2949
}
2950

2951
//! Get the rectilinear mesh grid
2952
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
143✔
2953
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2954
{
2955
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
143✔
2956
    index, grid_x, nx, grid_y, ny, grid_z, nz);
143✔
2957
}
2958

2959
//! Set the rectilienar mesh parameters
2960
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
44✔
2961
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2962
  const double* grid_z, const int nz)
2963
{
2964
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
44✔
2965
    index, grid_x, nx, grid_y, ny, grid_z, nz);
44✔
2966
}
2967

2968
//! Get the cylindrical mesh grid
2969
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2970
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2971
{
2972
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
121✔
2973
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2974
}
2975

2976
//! Set the cylindrical mesh parameters
2977
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
22✔
2978
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2979
  const double* grid_z, const int nz)
2980
{
2981
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
22✔
2982
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2983
}
2984

2985
//! Get the spherical mesh grid
2986
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2987
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2988
{
2989

2990
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
121✔
2991
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2992
  ;
121✔
2993
}
2994

2995
//! Set the spherical mesh parameters
2996
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
22✔
2997
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2998
  const double* grid_z, const int nz)
2999
{
3000
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
22✔
3001
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
3002
}
3003

3004
#ifdef OPENMC_DAGMC_ENABLED
3005

3006
const std::string MOABMesh::mesh_lib_type = "moab";
3007

3008
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
3009
{
3010
  initialize();
24✔
3011
}
24!
3012

3013
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
3014
{
3015
  initialize();
×
3016
}
×
3017

3018
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
3019
  : UnstructuredMesh()
×
3020
{
3021
  n_dimension_ = 3;
3022
  filename_ = filename;
×
3023
  set_length_multiplier(length_multiplier);
×
3024
  initialize();
×
3025
}
×
3026

3027
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
3028
{
3029
  mbi_ = external_mbi;
1✔
3030
  filename_ = "unknown (external file)";
1✔
3031
  this->initialize();
1✔
3032
}
1!
3033

3034
void MOABMesh::initialize()
25✔
3035
{
3036

3037
  // Create the MOAB interface and load data from file
3038
  this->create_interface();
25✔
3039

3040
  // Initialise MOAB error code
3041
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
3042

3043
  // Set the dimension
3044
  n_dimension_ = 3;
25✔
3045

3046
  // set member range of tetrahedral entities
3047
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
3048
  if (rval != moab::MB_SUCCESS) {
25!
3049
    fatal_error("Failed to get all tetrahedral elements");
3050
  }
3051

3052
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
3053
    warning("Non-tetrahedral elements found in unstructured "
×
3054
            "mesh file: " +
3055
            filename_);
3056
  }
3057

3058
  // set member range of vertices
3059
  int vertex_dim = 0;
25✔
3060
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
3061
  if (rval != moab::MB_SUCCESS) {
25!
3062
    fatal_error("Failed to get all vertex handles");
3063
  }
3064

3065
  // make an entity set for all tetrahedra
3066
  // this is used for convenience later in output
3067
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
3068
  if (rval != moab::MB_SUCCESS) {
25!
3069
    fatal_error("Failed to create an entity set for the tetrahedral elements");
3070
  }
3071

3072
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
3073
  if (rval != moab::MB_SUCCESS) {
25!
3074
    fatal_error("Failed to add tetrahedra to an entity set.");
3075
  }
3076

3077
  if (length_multiplier_ > 0.0) {
25!
3078
    // get the connectivity of all tets
3079
    moab::Range adj;
×
3080
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
3081
    if (rval != moab::MB_SUCCESS) {
×
3082
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
3083
    }
3084
    // scale all vertex coords by multiplier (done individually so not all
3085
    // coordinates are in memory twice at once)
3086
    for (auto vert : adj) {
×
3087
      // retrieve coords
3088
      std::array<double, 3> coord;
3089
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
3090
      if (rval != moab::MB_SUCCESS) {
×
3091
        fatal_error("Could not get coordinates of vertex.");
3092
      }
3093
      // scale coords
3094
      for (auto& c : coord) {
×
3095
        c *= length_multiplier_;
3096
      }
3097
      // set new coords
3098
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
3099
      if (rval != moab::MB_SUCCESS) {
×
3100
        fatal_error("Failed to set new vertex coordinates");
3101
      }
3102
    }
3103
  }
3104

3105
  // Determine bounds of mesh
3106
  this->determine_bounds();
25✔
3107
}
25✔
3108

3109
void MOABMesh::prepare_for_point_location()
21✔
3110
{
3111
  // if the KDTree has already been constructed, do nothing
3112
  if (kdtree_)
21!
3113
    return;
3114

3115
  // build acceleration data structures
3116
  compute_barycentric_data(ehs_);
21✔
3117
  build_kdtree(ehs_);
21✔
3118
}
3119

3120
void MOABMesh::create_interface()
25✔
3121
{
3122
  // Do not create a MOAB instance if one is already in memory
3123
  if (mbi_)
25✔
3124
    return;
3125

3126
  // create MOAB instance
3127
  mbi_ = std::make_shared<moab::Core>();
24!
3128

3129
  // load unstructured mesh file
3130
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
3131
  if (rval != moab::MB_SUCCESS) {
24!
3132
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
3133
  }
3134
}
3135

3136
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
3137
{
3138
  moab::Range all_tris;
21✔
3139
  int adj_dim = 2;
21✔
3140
  write_message("Getting tet adjacencies...", 7);
21✔
3141
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
3142
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
3143
  if (rval != moab::MB_SUCCESS) {
21!
3144
    fatal_error("Failed to get adjacent triangles for tets");
3145
  }
3146

3147
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
3148
    warning("Non-triangle elements found in tet adjacencies in "
×
3149
            "unstructured mesh file: " +
3150
            filename_);
×
3151
  }
3152

3153
  // combine into one range
3154
  moab::Range all_tets_and_tris;
21✔
3155
  all_tets_and_tris.merge(all_tets);
21✔
3156
  all_tets_and_tris.merge(all_tris);
21✔
3157

3158
  // create a kd-tree instance
3159
  write_message(
21✔
3160
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
3161
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
3162

3163
  // Determine what options to use
3164
  std::ostringstream options_stream;
21✔
3165
  if (options_.empty()) {
21✔
3166
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
3167
  } else {
3168
    options_stream << options_;
16✔
3169
  }
3170
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
3171

3172
  // Build the k-d tree
3173
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
3174
  if (rval != moab::MB_SUCCESS) {
21!
3175
    fatal_error("Failed to construct KDTree for the "
3176
                "unstructured mesh file: " +
3177
                filename_);
×
3178
  }
3179
}
21✔
3180

3181
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
3182
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
3183
{
3184
  hits.clear();
1,543,584!
3185

3186
  moab::ErrorCode rval;
1,543,584✔
3187
  vector<moab::EntityHandle> tris;
1,543,584✔
3188
  // get all intersections with triangles in the tet mesh
3189
  // (distances are relative to the start point, not the previous
3190
  // intersection)
3191
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
3192
    dir.array(), start.array(), tris, hits, 0, track_len);
3193
  if (rval != moab::MB_SUCCESS) {
1,543,584!
3194
    fatal_error(
3195
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
3196
  }
3197

3198
  // remove duplicate intersection distances
3199
  std::unique(hits.begin(), hits.end());
1,543,584✔
3200

3201
  // sorts by first component of std::pair by default
3202
  std::sort(hits.begin(), hits.end());
1,543,584✔
3203
}
1,543,584✔
3204

3205
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
3206
  vector<int>& bins, vector<double>& lengths) const
3207
{
3208
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
3209
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
3210
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
3211
  dir.normalize();
1,543,584✔
3212

3213
  double track_len = (end - start).length();
1,543,584✔
3214
  if (track_len == 0.0)
1,543,584!
3215
    return;
721,692✔
3216

3217
  start -= TINY_BIT * dir;
1,543,584✔
3218
  end += TINY_BIT * dir;
1,543,584✔
3219

3220
  vector<double> hits;
1,543,584✔
3221
  intersect_track(start, dir, track_len, hits);
1,543,584✔
3222

3223
  bins.clear();
1,543,584!
3224
  lengths.clear();
1,543,584!
3225

3226
  // if there are no intersections the track may lie entirely
3227
  // within a single tet. If this is the case, apply entire
3228
  // score to that tet and return.
3229
  if (hits.size() == 0) {
1,543,584✔
3230
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
3231
    int bin = this->get_bin(midpoint);
721,692✔
3232
    if (bin != -1) {
721,692✔
3233
      bins.push_back(bin);
242,866✔
3234
      lengths.push_back(1.0);
242,866✔
3235
    }
3236
    return;
721,692✔
3237
  }
3238

3239
  // for each segment in the set of tracks, try to look up a tet
3240
  // at the midpoint of the segment
3241
  Position current = r0;
3242
  double last_dist = 0.0;
3243
  for (const auto& hit : hits) {
5,516,161✔
3244
    // get the segment length
3245
    double segment_length = hit - last_dist;
4,694,269✔
3246
    last_dist = hit;
4,694,269✔
3247
    // find the midpoint of this segment
3248
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
3249
    // try to find a tet for this position
3250
    int bin = this->get_bin(midpoint);
4,694,269✔
3251

3252
    // determine the start point for this segment
3253
    current = r0 + u * hit;
4,694,269✔
3254

3255
    if (bin == -1) {
4,694,269✔
3256
      continue;
20,522✔
3257
    }
3258

3259
    bins.push_back(bin);
4,673,747✔
3260
    lengths.push_back(segment_length / track_len);
4,673,747✔
3261
  }
3262

3263
  // tally remaining portion of track after last hit if
3264
  // the last segment of the track is in the mesh but doesn't
3265
  // reach the other side of the tet
3266
  if (hits.back() < track_len) {
821,892!
3267
    Position segment_start = r0 + u * hits.back();
821,892✔
3268
    double segment_length = track_len - hits.back();
821,892✔
3269
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
3270
    int bin = this->get_bin(midpoint);
821,892✔
3271
    if (bin != -1) {
821,892✔
3272
      bins.push_back(bin);
766,509✔
3273
      lengths.push_back(segment_length / track_len);
766,509✔
3274
    }
3275
  }
3276
};
1,543,584✔
3277

3278
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,232✔
3279
{
3280
  moab::CartVect pos(r.x, r.y, r.z);
7,317,232✔
3281
  // find the leaf of the kd-tree for this position
3282
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,232✔
3283
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,232✔
3284
  if (rval != moab::MB_SUCCESS) {
7,317,232✔
3285
    return 0;
3286
  }
3287

3288
  // retrieve the tet elements of this leaf
3289
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,335✔
3290
  moab::Range tets;
6,305,335✔
3291
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,335✔
3292
  if (rval != moab::MB_SUCCESS) {
6,305,335!
3293
    warning("MOAB error finding tets.");
×
3294
  }
3295

3296
  // loop over the tets in this leaf, returning the containing tet if found
3297
  for (const auto& tet : tets) {
260,211,273✔
3298
    if (point_in_tet(pos, tet)) {
260,208,426✔
3299
      return tet;
6,302,488✔
3300
    }
3301
  }
3302

3303
  // if no tet is found, return an invalid handle
3304
  return 0;
2,847✔
3305
}
14,634,464✔
3306

3307
double MOABMesh::volume(int bin) const
167,880✔
3308
{
3309
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3310
}
3311

3312
std::string MOABMesh::library() const
34✔
3313
{
3314
  return mesh_lib_type;
34✔
3315
}
3316

3317
// Sample position within a tet for MOAB type tets
3318
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3319
{
3320

3321
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3322

3323
  // Get vertex coordinates for MOAB tet
3324
  const moab::EntityHandle* conn1;
200,410✔
3325
  int conn1_size;
200,410✔
3326
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3327
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3328
    fatal_error(fmt::format(
3329
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3330
      conn1_size));
3331
  }
3332
  moab::CartVect p[4];
200,410✔
3333
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3334
  if (rval != moab::MB_SUCCESS) {
200,410!
3335
    fatal_error("Failed to get tet coords");
3336
  }
3337

3338
  std::array<Position, 4> tet_verts;
200,410✔
3339
  for (int i = 0; i < 4; i++) {
1,002,050✔
3340
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3341
  }
3342
  // Samples position within tet using Barycentric stuff
3343
  return this->sample_tet(tet_verts, seed);
200,410✔
3344
}
3345

3346
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3347
{
3348
  vector<moab::EntityHandle> conn;
167,880✔
3349
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3350
  if (rval != moab::MB_SUCCESS) {
167,880!
3351
    fatal_error("Failed to get tet connectivity");
3352
  }
3353

3354
  moab::CartVect p[4];
167,880✔
3355
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3356
  if (rval != moab::MB_SUCCESS) {
167,880!
3357
    fatal_error("Failed to get tet coords");
3358
  }
3359

3360
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
167,880✔
3361
}
167,880✔
3362

3363
int MOABMesh::get_bin(Position r) const
7,317,232✔
3364
{
3365
  moab::EntityHandle tet = get_tet(r);
7,317,232✔
3366
  if (tet == 0) {
7,317,232✔
3367
    return -1;
3368
  } else {
3369
    return get_bin_from_ent_handle(tet);
6,302,488✔
3370
  }
3371
}
3372

3373
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3374
{
3375
  moab::ErrorCode rval;
21✔
3376

3377
  baryc_data_.clear();
21!
3378
  baryc_data_.resize(tets.size());
21✔
3379

3380
  // compute the barycentric data for each tet element
3381
  // and store it as a 3x3 matrix
3382
  for (auto& tet : tets) {
239,757✔
3383
    vector<moab::EntityHandle> verts;
239,736✔
3384
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3385
    if (rval != moab::MB_SUCCESS) {
239,736!
3386
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3387
    }
3388

3389
    moab::CartVect p[4];
239,736✔
3390
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
239,736✔
3391
    if (rval != moab::MB_SUCCESS) {
239,736!
3392
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
×
3393
    }
3394

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

3397
    // invert now to avoid this cost later
3398
    a = a.transpose().inverse();
239,736✔
3399
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3400
  }
239,736✔
3401
}
21✔
3402

3403
bool MOABMesh::point_in_tet(
260,208,426✔
3404
  const moab::CartVect& r, moab::EntityHandle tet) const
3405
{
3406

3407
  moab::ErrorCode rval;
260,208,426✔
3408

3409
  // get tet vertices
3410
  vector<moab::EntityHandle> verts;
260,208,426✔
3411
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,208,426✔
3412
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3413
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3414
    return false;
3415
  }
3416

3417
  // first vertex is used as a reference point for the barycentric data -
3418
  // retrieve its coordinates
3419
  moab::CartVect p_zero;
260,208,426✔
3420
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,208,426✔
3421
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3422
    warning("Failed to get coordinates of a vertex in "
×
3423
            "unstructured mesh: " +
3424
            filename_);
×
3425
    return false;
3426
  }
3427

3428
  // look up barycentric data
3429
  int idx = get_bin_from_ent_handle(tet);
260,208,426✔
3430
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,208,426✔
3431

3432
  moab::CartVect bary_coords = a_inv * (r - p_zero);
260,208,426✔
3433

3434
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
161,208,987✔
3435
          bary_coords[2] >= 0.0 &&
318,957,185✔
3436
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
21,688,225✔
3437
}
260,208,426✔
3438

3439
int MOABMesh::get_bin_from_index(int idx) const
3440
{
3441
  if (idx >= n_bins()) {
×
3442
    fatal_error(fmt::format("Invalid bin index: {}", idx));
3443
  }
3444
  return ehs_[idx] - ehs_[0];
3445
}
3446

3447
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3448
{
3449
  int bin = get_bin(r);
3450
  *in_mesh = bin != -1;
3451
  return bin;
3452
}
3453

3454
int MOABMesh::get_index_from_bin(int bin) const
3455
{
3456
  return bin;
3457
}
3458

3459
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3460
  Position plot_ll, Position plot_ur) const
3461
{
3462
  // TODO: Implement mesh lines
3463
  return {};
3464
}
3465

3466
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3467
{
3468
  int idx = vert - verts_[0];
815,520✔
3469
  if (idx >= n_vertices()) {
815,520!
3470
    fatal_error(
3471
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3472
  }
3473
  return idx;
815,520✔
3474
}
3475

3476
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,750,650✔
3477
{
3478
  int bin = eh - ehs_[0];
266,750,650✔
3479
  if (bin >= n_bins()) {
266,750,650!
3480
    fatal_error(fmt::format("Invalid bin: {}", bin));
3481
  }
3482
  return bin;
266,750,650✔
3483
}
3484

3485
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3486
{
3487
  if (bin >= n_bins()) {
572,170!
3488
    fatal_error(fmt::format("Invalid bin index: ", bin));
3489
  }
3490
  return ehs_[0] + bin;
572,170✔
3491
}
3492

3493
int MOABMesh::n_bins() const
267,526,773✔
3494
{
3495
  return ehs_.size();
267,526,773✔
3496
}
3497

3498
int MOABMesh::n_surface_bins() const
3499
{
3500
  // collect all triangles in the set of tets for this mesh
3501
  moab::Range tris;
×
3502
  moab::ErrorCode rval;
3503
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3504
  if (rval != moab::MB_SUCCESS) {
×
3505
    warning("Failed to get all triangles in the mesh instance");
×
3506
    return -1;
3507
  }
3508
  return 2 * tris.size();
×
3509
}
3510

3511
Position MOABMesh::centroid(int bin) const
3512
{
3513
  moab::ErrorCode rval;
3514

3515
  auto tet = this->get_ent_handle_from_bin(bin);
3516

3517
  // look up the tet connectivity
3518
  vector<moab::EntityHandle> conn;
×
3519
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3520
  if (rval != moab::MB_SUCCESS) {
×
3521
    warning("Failed to get connectivity of a mesh element.");
×
3522
    return {};
3523
  }
3524

3525
  // get the coordinates
3526
  vector<moab::CartVect> coords(conn.size());
×
3527
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3528
  if (rval != moab::MB_SUCCESS) {
×
3529
    warning("Failed to get the coordinates of a mesh element.");
×
3530
    return {};
3531
  }
3532

3533
  // compute the centroid of the element vertices
3534
  moab::CartVect centroid(0.0, 0.0, 0.0);
3535
  for (const auto& coord : coords) {
×
3536
    centroid += coord;
3537
  }
3538
  centroid /= double(coords.size());
3539

3540
  return {centroid[0], centroid[1], centroid[2]};
3541
}
3542

3543
int MOABMesh::n_vertices() const
845,874✔
3544
{
3545
  return verts_.size();
845,874✔
3546
}
3547

3548
Position MOABMesh::vertex(int id) const
86,227✔
3549
{
3550

3551
  moab::ErrorCode rval;
86,227✔
3552

3553
  moab::EntityHandle vert = verts_[id];
86,227✔
3554

3555
  moab::CartVect coords;
86,227✔
3556
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3557
  if (rval != moab::MB_SUCCESS) {
86,227!
3558
    fatal_error("Failed to get the coordinates of a vertex.");
3559
  }
3560

3561
  return {coords[0], coords[1], coords[2]};
86,227✔
3562
}
3563

3564
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3565
{
3566
  moab::ErrorCode rval;
203,880✔
3567

3568
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3569

3570
  // look up the tet connectivity
3571
  vector<moab::EntityHandle> conn;
203,880✔
3572
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3573
  if (rval != moab::MB_SUCCESS) {
203,880!
3574
    fatal_error("Failed to get connectivity of a mesh element.");
3575
    return {};
3576
  }
3577

3578
  std::vector<int> verts(4);
203,880✔
3579
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3580
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3581
  }
3582

3583
  return verts;
203,880✔
3584
}
203,880✔
3585

3586
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3587
  std::string score) const
3588
{
3589
  moab::ErrorCode rval;
3590
  // add a tag to the mesh
3591
  // all scores are treated as a single value
3592
  // with an uncertainty
3593
  moab::Tag value_tag;
3594

3595
  // create the value tag if not present and get handle
3596
  double default_val = 0.0;
3597
  auto val_string = score + "_mean";
3598
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3599
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3600
  if (rval != moab::MB_SUCCESS) {
×
3601
    auto msg =
3602
      fmt::format("Could not create or retrieve the value tag for the score {}"
3603
                  " on unstructured mesh {}",
3604
        score, id_);
×
3605
    fatal_error(msg);
3606
  }
3607

3608
  // create the std dev tag if not present and get handle
3609
  moab::Tag error_tag;
3610
  std::string err_string = score + "_std_dev";
×
3611
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3612
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3613
  if (rval != moab::MB_SUCCESS) {
×
3614
    auto msg =
3615
      fmt::format("Could not create or retrieve the error tag for the score {}"
3616
                  " on unstructured mesh {}",
3617
        score, id_);
×
3618
    fatal_error(msg);
3619
  }
3620

3621
  // return the populated tag handles
3622
  return {value_tag, error_tag};
3623
}
3624

3625
void MOABMesh::add_score(const std::string& score)
3626
{
3627
  auto score_tags = get_score_tags(score);
×
3628
  tag_names_.push_back(score);
3629
}
3630

3631
void MOABMesh::remove_scores()
3632
{
3633
  for (const auto& name : tag_names_) {
×
3634
    auto value_name = name + "_mean";
3635
    moab::Tag tag;
3636
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3637
    if (rval != moab::MB_SUCCESS)
×
3638
      return;
3639

3640
    rval = mbi_->tag_delete(tag);
×
3641
    if (rval != moab::MB_SUCCESS) {
×
3642
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3643
                             " on unstructured mesh {}",
3644
        name, id_);
×
3645
      fatal_error(msg);
3646
    }
3647

3648
    auto std_dev_name = name + "_std_dev";
×
3649
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3650
    if (rval != moab::MB_SUCCESS) {
×
3651
      auto msg =
3652
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3653
                    " on unstructured mesh {}",
3654
          name, id_);
×
3655
    }
3656

3657
    rval = mbi_->tag_delete(tag);
×
3658
    if (rval != moab::MB_SUCCESS) {
×
3659
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3660
                             " on unstructured mesh {}",
3661
        name, id_);
×
3662
      fatal_error(msg);
3663
    }
3664
  }
3665
  tag_names_.clear();
3666
}
3667

3668
void MOABMesh::set_score_data(const std::string& score,
3669
  const vector<double>& values, const vector<double>& std_dev)
3670
{
3671
  auto score_tags = this->get_score_tags(score);
×
3672

3673
  moab::ErrorCode rval;
3674
  // set the score value
3675
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
3676
  if (rval != moab::MB_SUCCESS) {
×
3677
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3678
                           "on unstructured mesh {}",
3679
      score, id_);
3680
    warning(msg);
×
3681
  }
3682

3683
  // set the error value
3684
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3685
  if (rval != moab::MB_SUCCESS) {
×
3686
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3687
                           "on unstructured mesh {}",
3688
      score, id_);
3689
    warning(msg);
×
3690
  }
3691
}
3692

3693
void MOABMesh::write(const std::string& base_filename) const
3694
{
3695
  // add extension to the base name
3696
  auto filename = base_filename + ".vtk";
3697
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3698
  filename = settings::path_output + filename;
×
3699

3700
  // write the tetrahedral elements of the mesh only
3701
  // to avoid clutter from zero-value data on other
3702
  // elements during visualization
3703
  moab::ErrorCode rval;
3704
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3705
  if (rval != moab::MB_SUCCESS) {
×
3706
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3707
    warning(msg);
×
3708
  }
3709
}
3710

3711
#endif
3712

3713
#ifdef OPENMC_LIBMESH_ENABLED
3714

3715
const std::string LibMesh::mesh_lib_type = "libmesh";
3716

3717
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
25✔
3718
{
3719
  // filename_ and length_multiplier_ will already be set by the
3720
  // UnstructuredMesh constructor
3721
  set_mesh_pointer_from_filename(filename_);
25✔
3722
  set_length_multiplier(length_multiplier_);
25✔
3723
  initialize();
25✔
3724
}
25✔
3725

3726
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3727
{
3728
  // filename_ and length_multiplier_ will already be set by the
3729
  // UnstructuredMesh constructor
3730
  set_mesh_pointer_from_filename(filename_);
×
3731
  set_length_multiplier(length_multiplier_);
×
3732
  initialize();
×
3733
}
3734

3735
// create the mesh from a pointer to a libMesh Mesh
3736
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3737
{
3738
  if (!input_mesh.is_replicated()) {
×
3739
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3740
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3741
  }
3742

3743
  m_ = &input_mesh;
3744
  set_length_multiplier(length_multiplier);
×
3745
  initialize();
×
3746
}
3747

3748
// create the mesh from an input file
3749
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3750
{
3751
  n_dimension_ = 3;
3752
  set_mesh_pointer_from_filename(filename);
×
3753
  set_length_multiplier(length_multiplier);
×
3754
  initialize();
×
3755
}
3756

3757
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
25✔
3758
{
3759
  filename_ = filename;
25✔
3760
  unique_m_ =
25✔
3761
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
25✔
3762
  m_ = unique_m_.get();
25✔
3763
  m_->read(filename_);
25✔
3764
}
25✔
3765

3766
// build a libMesh equation system for storing values
3767
void LibMesh::build_eqn_sys()
17✔
3768
{
3769
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
17✔
3770
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
17✔
3771
  libMesh::ExplicitSystem& eq_sys =
17✔
3772
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
17✔
3773
}
17✔
3774

3775
// intialize from mesh file
3776
void LibMesh::initialize()
25✔
3777
{
3778
  if (!settings::libmesh_comm) {
25!
3779
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3780
                "communicator.");
3781
  }
3782

3783
  // assuming that unstructured meshes used in OpenMC are 3D
3784
  n_dimension_ = 3;
25✔
3785

3786
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3787
  // Otherwise assume that it is prepared by its owning application
3788
  if (unique_m_) {
25!
3789
    m_->prepare_for_use();
25✔
3790
  }
3791

3792
  // ensure that the loaded mesh is 3 dimensional
3793
  if (m_->mesh_dimension() != n_dimension_) {
25!
3794
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3795
                            "mesh is not a 3D mesh.",
3796
      filename_));
3797
  }
3798

3799
  for (int i = 0; i < num_threads(); i++) {
75✔
3800
    pl_.emplace_back(m_->sub_point_locator());
50✔
3801
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
50✔
3802
    pl_.back()->enable_out_of_mesh_mode();
50✔
3803
  }
3804

3805
  // store first element in the mesh to use as an offset for bin indices
3806
  auto first_elem = *m_->elements_begin();
50✔
3807
  first_element_id_ = first_elem->id();
25✔
3808

3809
  // bounding box for the mesh for quick rejection checks
3810
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
25!
3811
  libMesh::Point ll = bbox_.min();
25!
3812
  libMesh::Point ur = bbox_.max();
25!
3813
  if (length_multiplier_ > 0.0) {
25!
3814
    lower_left_ = {length_multiplier_ * ll(0), length_multiplier_ * ll(1),
3815
      length_multiplier_ * ll(2)};
3816
    upper_right_ = {length_multiplier_ * ur(0), length_multiplier_ * ur(1),
3817
      length_multiplier_ * ur(2)};
3818
  } else {
3819
    lower_left_ = {ll(0), ll(1), ll(2)};
25✔
3820
    upper_right_ = {ur(0), ur(1), ur(2)};
25✔
3821
  }
3822
}
25✔
3823

3824
// Sample position within a tet for LibMesh type tets
3825
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3826
{
3827
  const auto& elem = get_element_from_bin(bin);
400,820✔
3828
  // Get tet vertex coordinates from LibMesh
3829
  std::array<Position, 4> tet_verts;
400,820✔
3830
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3831
    const auto& node_ref = elem.node_ref(i);
1,603,280✔
3832
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3833
  }
3834
  // Samples position within tet using Barycentric coordinates
3835
  Position sampled_position = this->sample_tet(tet_verts, seed);
400,820✔
3836
  if (length_multiplier_ > 0.0) {
400,820!
3837
    return length_multiplier_ * sampled_position;
3838
  } else {
3839
    return sampled_position;
400,820✔
3840
  }
3841
}
3842

3843
Position LibMesh::centroid(int bin) const
3844
{
3845
  const auto& elem = this->get_element_from_bin(bin);
3846
  auto centroid = elem.vertex_average();
3847
  if (length_multiplier_ > 0.0) {
×
3848
    return length_multiplier_ * Position(centroid(0), centroid(1), centroid(2));
3849
  } else {
3850
    return {centroid(0), centroid(1), centroid(2)};
3851
  }
3852
}
3853

3854
int LibMesh::n_vertices() const
42,644✔
3855
{
3856
  return m_->n_nodes();
42,644✔
3857
}
3858

3859
Position LibMesh::vertex(int vertex_id) const
42,604✔
3860
{
3861
  const auto& node_ref = m_->node_ref(vertex_id);
42,604✔
3862
  if (length_multiplier_ > 0.0) {
42,604!
3863
    return length_multiplier_ * Position(node_ref(0), node_ref(1), node_ref(2));
3864
  } else {
3865
    return {node_ref(0), node_ref(1), node_ref(2)};
42,604✔
3866
  }
3867
}
3868

3869
std::vector<int> LibMesh::connectivity(int elem_id) const
267,856✔
3870
{
3871
  std::vector<int> conn;
267,856✔
3872
  const auto* elem_ptr = m_->elem_ptr(elem_id);
267,856✔
3873
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,355,280✔
3874
    conn.push_back(elem_ptr->node_id(i));
1,087,424✔
3875
  }
3876
  return conn;
267,856✔
3877
}
3878

3879
std::string LibMesh::library() const
37✔
3880
{
3881
  return mesh_lib_type;
37✔
3882
}
3883

3884
int LibMesh::n_bins() const
1,788,419✔
3885
{
3886
  return m_->n_elem();
1,788,419✔
3887
}
3888

3889
int LibMesh::n_surface_bins() const
3890
{
3891
  int n_bins = 0;
3892
  for (int i = 0; i < this->n_bins(); i++) {
×
3893
    const libMesh::Elem& e = get_element_from_bin(i);
3894
    n_bins += e.n_faces();
3895
    // if this is a boundary element, it will only be visited once,
3896
    // the number of surface bins is incremented to
3897
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
3898
      // null neighbor pointer indicates a boundary face
3899
      if (!neighbor_ptr) {
×
3900
        n_bins++;
3901
      }
3902
    }
3903
  }
3904
  return n_bins;
3905
}
3906

3907
void LibMesh::add_score(const std::string& var_name)
17✔
3908
{
3909
  if (!equation_systems_) {
17!
3910
    build_eqn_sys();
17✔
3911
  }
3912

3913
  // check if this is a new variable
3914
  std::string value_name = var_name + "_mean";
17✔
3915
  if (!variable_map_.count(value_name)) {
17✔
3916
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
17✔
3917
    auto var_num =
17✔
3918
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
17✔
3919
    variable_map_[value_name] = var_num;
17✔
3920
  }
3921

3922
  std::string std_dev_name = var_name + "_std_dev";
17✔
3923
  // check if this is a new variable
3924
  if (!variable_map_.count(std_dev_name)) {
17✔
3925
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
17✔
3926
    auto var_num =
17✔
3927
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
17✔
3928
    variable_map_[std_dev_name] = var_num;
17✔
3929
  }
3930
}
17✔
3931

3932
void LibMesh::remove_scores()
17✔
3933
{
3934
  if (equation_systems_) {
17!
3935
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
17✔
3936
    eqn_sys.clear();
17✔
3937
    variable_map_.clear();
17✔
3938
  }
3939
}
17✔
3940

3941
void LibMesh::set_score_data(const std::string& var_name,
17✔
3942
  const vector<double>& values, const vector<double>& std_dev)
3943
{
3944
  if (!equation_systems_) {
17!
3945
    build_eqn_sys();
3946
  }
3947

3948
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
17✔
3949

3950
  if (!eqn_sys.is_initialized()) {
17!
3951
    equation_systems_->init();
17✔
3952
  }
3953

3954
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
17✔
3955

3956
  // look up the value variable
3957
  std::string value_name = var_name + "_mean";
17✔
3958
  unsigned int value_num = variable_map_.at(value_name);
17✔
3959
  // look up the std dev variable
3960
  std::string std_dev_name = var_name + "_std_dev";
17✔
3961
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
17✔
3962

3963
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
199,763✔
3964
       it++) {
3965
    if (!(*it)->active()) {
99,856!
3966
      continue;
3967
    }
3968

3969
    auto bin = get_bin_from_element(*it);
99,856✔
3970

3971
    // set value
3972
    vector<libMesh::dof_id_type> value_dof_indices;
99,856✔
3973
    dof_map.dof_indices(*it, value_dof_indices, value_num);
99,856✔
3974
    assert(value_dof_indices.size() == 1);
99,856✔
3975
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
99,856✔
3976

3977
    // set std dev
3978
    vector<libMesh::dof_id_type> std_dev_dof_indices;
99,856✔
3979
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
99,856✔
3980
    assert(std_dev_dof_indices.size() == 1);
99,856✔
3981
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
99,856✔
3982
  }
99,873✔
3983
}
17✔
3984

3985
void LibMesh::write(const std::string& filename) const
17✔
3986
{
3987
  write_message(fmt::format(
17✔
3988
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
17✔
3989
  libMesh::ExodusII_IO exo(*m_);
17✔
3990
  std::set<std::string> systems_out = {eq_system_name_};
34!
3991
  exo.write_discontinuous_exodusII(
17✔
3992
    filename + ".e", *equation_systems_, &systems_out);
34✔
3993
}
17✔
3994

3995
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3996
  vector<int>& bins, vector<double>& lengths) const
3997
{
3998
  // TODO: Implement triangle crossings here
3999
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
4000
}
4001

4002
int LibMesh::get_bin(Position r) const
2,340,604✔
4003
{
4004
  // look-up a tet using the point locator
4005
  libMesh::Point p(r.x, r.y, r.z);
2,340,604!
4006

4007
  if (length_multiplier_ > 0.0) {
2,340,604!
4008
    // Scale the point down
4009
    p /= length_multiplier_;
2,340,604✔
4010
  }
4011

4012
  // quick rejection check
4013
  if (!bbox_.contains_point(p)) {
2,340,604✔
4014
    return -1;
4015
  }
4016

4017
  const auto& point_locator = pl_.at(thread_num());
1,421,808✔
4018

4019
  const auto elem_ptr = (*point_locator)(p);
1,421,808✔
4020
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,808✔
4021
}
2,340,604✔
4022

4023
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,520,434✔
4024
{
4025
  int bin = elem->id() - first_element_id_;
1,520,434✔
4026
  if (bin >= n_bins() || bin < 0) {
1,520,434!
4027
    fatal_error(fmt::format("Invalid bin: {}", bin));
4028
  }
4029
  return bin;
1,520,434✔
4030
}
4031

4032
std::pair<vector<double>, vector<double>> LibMesh::plot(
4033
  Position plot_ll, Position plot_ur) const
4034
{
4035
  return {};
4036
}
4037

4038
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
769,460✔
4039
{
4040
  return m_->elem_ref(bin);
769,460✔
4041
}
4042

4043
double LibMesh::volume(int bin) const
368,640✔
4044
{
4045
  return this->get_element_from_bin(bin).volume() * length_multiplier_ *
368,640✔
4046
         length_multiplier_ * length_multiplier_;
368,640✔
4047
}
4048

4049
AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh,
4050
  double length_multiplier,
4051
  const std::set<libMesh::subdomain_id_type>& block_ids)
4052
  : LibMesh(input_mesh, length_multiplier), block_ids_(block_ids),
4053
    block_restrict_(!block_ids_.empty()),
×
4054
    num_active_(
×
4055
      block_restrict_
4056
        ? std::distance(m_->active_subdomain_set_elements_begin(block_ids_),
×
4057
            m_->active_subdomain_set_elements_end(block_ids_))
×
4058
        : m_->n_active_elem())
×
4059
{
4060
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
4061
  // contiguous in ID space, so we need to map from bin indices (defined over
4062
  // active elements) to global dof ids
4063
  bin_to_elem_map_.reserve(num_active_);
×
4064
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
4065
  auto begin = block_restrict_
4066
                 ? m_->active_subdomain_set_elements_begin(block_ids_)
×
4067
                 : m_->active_elements_begin();
×
4068
  auto end = block_restrict_ ? m_->active_subdomain_set_elements_end(block_ids_)
×
4069
                             : m_->active_elements_end();
×
4070
  for (const auto& elem : libMesh::as_range(begin, end)) {
×
4071
    bin_to_elem_map_.push_back(elem->id());
×
4072
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
4073
  }
4074
}
4075

4076
int AdaptiveLibMesh::n_bins() const
4077
{
4078
  return num_active_;
4079
}
4080

4081
void AdaptiveLibMesh::add_score(const std::string& var_name)
4082
{
4083
  warning(fmt::format(
×
4084
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4085
    this->id_));
4086
}
4087

4088
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
4089
  const vector<double>& values, const vector<double>& std_dev)
4090
{
4091
  warning(fmt::format(
×
4092
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4093
    this->id_));
4094
}
4095

4096
void AdaptiveLibMesh::write(const std::string& filename) const
4097
{
4098
  warning(fmt::format(
×
4099
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4100
    this->id_));
4101
}
4102

4103
int AdaptiveLibMesh::get_bin(Position r) const
4104
{
4105
  // look-up a tet using the point locator
4106
  libMesh::Point p(r.x, r.y, r.z);
×
4107

4108
  if (length_multiplier_ > 0.0) {
×
4109
    // Scale the point down
4110
    p /= length_multiplier_;
4111
  }
4112

4113
  // quick rejection check
4114
  if (!bbox_.contains_point(p)) {
×
4115
    return -1;
4116
  }
4117

4118
  const auto& point_locator = pl_.at(thread_num());
×
4119

4120
  const auto elem_ptr = (*point_locator)(p, &block_ids_);
×
4121
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
×
4122
}
4123

4124
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
4125
{
4126
  int bin = elem_to_bin_map_[elem->id()];
4127
  if (bin >= n_bins() || bin < 0) {
×
4128
    fatal_error(fmt::format("Invalid bin: {}", bin));
4129
  }
4130
  return bin;
4131
}
4132

4133
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
4134
{
4135
  return m_->elem_ref(bin_to_elem_map_.at(bin));
4136
}
4137

4138
#endif // OPENMC_LIBMESH_ENABLED
4139

4140
//==============================================================================
4141
// Non-member functions
4142
//==============================================================================
4143

4144
void read_meshes(pugi::xml_node root)
14,167✔
4145
{
4146
  std::unordered_set<int> mesh_ids;
14,167✔
4147

4148
  for (auto node : root.children("mesh")) {
17,712✔
4149
    // Check to make sure multiple meshes in the same file don't share IDs
4150
    int id = std::stoi(get_node_value(node, "id"));
7,090✔
4151
    if (contains(mesh_ids, id)) {
7,090!
UNCOV
4152
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4153
                              "'{}' in the same input file",
4154
        id));
4155
    }
4156
    mesh_ids.insert(id);
3,545✔
4157

4158
    // If we've already read a mesh with the same ID in a *different* file,
4159
    // assume it is the same here
4160
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3,545!
UNCOV
4161
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
UNCOV
4162
      continue;
×
4163
    }
4164

4165
    std::string mesh_type;
3,545✔
4166
    if (check_for_node(node, "type")) {
3,545✔
4167
      mesh_type = get_node_value(node, "type", true, true);
950✔
4168
    } else {
4169
      mesh_type = "regular";
2,595✔
4170
    }
4171

4172
    // determine the mesh library to use
4173
    std::string mesh_lib;
3,545✔
4174
    if (check_for_node(node, "library")) {
3,545✔
4175
      mesh_lib = get_node_value(node, "library", true, true);
49!
4176
    }
4177

4178
    Mesh::create(node, mesh_type, mesh_lib);
3,545✔
4179
  }
3,545✔
4180
}
14,167✔
4181

4182
void read_meshes(hid_t group)
22✔
4183
{
4184
  std::unordered_set<int> mesh_ids;
22✔
4185

4186
  std::vector<int> ids;
22✔
4187
  read_attribute(group, "ids", ids);
22✔
4188

4189
  for (auto id : ids) {
55✔
4190

4191
    // Check to make sure multiple meshes in the same file don't share IDs
4192
    if (contains(mesh_ids, id)) {
66!
4193
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4194
                              "'{}' in the same HDF5 input file",
4195
        id));
4196
    }
4197
    mesh_ids.insert(id);
33✔
4198

4199
    // If we've already read a mesh with the same ID in a *different* file,
4200
    // assume it is the same here
4201
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
33!
4202
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
33✔
4203
      continue;
33✔
4204
    }
4205

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

4209
    std::string mesh_type;
×
UNCOV
4210
    if (object_exists(mesh_group, "type")) {
×
UNCOV
4211
      read_dataset(mesh_group, "type", mesh_type);
×
4212
    } else {
UNCOV
4213
      mesh_type = "regular";
×
4214
    }
4215

4216
    // determine the mesh library to use
UNCOV
4217
    std::string mesh_lib;
×
UNCOV
4218
    if (object_exists(mesh_group, "library")) {
×
UNCOV
4219
      read_dataset(mesh_group, "library", mesh_lib);
×
4220
    }
4221

UNCOV
4222
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
UNCOV
4223
  }
×
4224
}
44✔
4225

4226
void meshes_to_hdf5(hid_t group)
7,860✔
4227
{
4228
  // Write number of meshes
4229
  hid_t meshes_group = create_group(group, "meshes");
7,860✔
4230
  int32_t n_meshes = model::meshes.size();
7,860✔
4231
  write_attribute(meshes_group, "n_meshes", n_meshes);
7,860✔
4232

4233
  if (n_meshes > 0) {
7,860✔
4234
    // Write IDs of meshes
4235
    vector<int> ids;
2,484✔
4236
    for (const auto& m : model::meshes) {
5,657✔
4237
      m->to_hdf5(meshes_group);
3,173✔
4238
      ids.push_back(m->id_);
3,173✔
4239
    }
4240
    write_attribute(meshes_group, "ids", ids);
2,484✔
4241
  }
2,484✔
4242

4243
  close_group(meshes_group);
7,860✔
4244
}
7,860✔
4245

4246
void free_memory_mesh()
9,207✔
4247
{
4248
  model::meshes.clear();
9,207✔
4249
  model::mesh_map.clear();
9,207✔
4250
}
9,207✔
4251

4252
extern "C" int n_meshes()
308✔
4253
{
4254
  return model::meshes.size();
308✔
4255
}
4256

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