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

openmc-dev / openmc / 22885916129

10 Mar 2026 03:37AM UTC coverage: 81.358%. First build
22885916129

Pull #3857

github

web-flow
Merge c0e3f064e into 908e63115
Pull Request #3857: Minimal implementation of hexagonal mesh

17621 of 25466 branches covered (69.19%)

Branch coverage included in aggregate %.

265 of 433 new or added lines in 4 files covered. (61.2%)

58185 of 67710 relevant lines covered (85.93%)

44685965.52 hits per line

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

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

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

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

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

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

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

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

58
namespace openmc {
59

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

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

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

74
namespace model {
75

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

79
} // namespace model
80

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

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

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

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

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

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

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

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

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

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

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

187
inline void atomic_max_double(double* ptr, double value)
18,558,600✔
188
{
189
  atomic_update_double(ptr, value, false);
6,186,200✔
190
}
6,186,200✔
191

192
inline void atomic_min_double(double* ptr, double value)
18,558,600✔
193
{
194
  atomic_update_double(ptr, value, true);
6,186,200✔
195
}
196

197
namespace detail {
198

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

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

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

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

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

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

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

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

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

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

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

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

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

326
} // namespace detail
327

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

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

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

367
  return model::meshes.back();
3,007✔
368
}
369

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

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

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

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

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

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

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

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

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

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

425
vector<double> Mesh::volumes() const
246✔
426
{
427
  vector<double> volumes(n_bins());
246✔
428
  for (int i = 0; i < n_bins(); i++) {
1,111,189✔
429
    volumes[i] = this->volume(i);
1,110,943✔
430
  }
431
  return volumes;
246✔
432
}
×
433

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

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

456
  Timer timer;
187✔
457
  timer.start();
187✔
458

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

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

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

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

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

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

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

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

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

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

516
      // Loop over rays on face of bounding box
517
#pragma omp for collapse(2)
518
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
15,800✔
519
        for (int i2 = 0; i2 < n2; ++i2) {
2,978,340✔
520
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
2,962,735✔
521
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
2,962,735✔
522

523
          p.from_source(&site);
2,962,735✔
524

525
          // Determine particle's location
526
          if (!exhaustive_find_cell(p)) {
2,962,735✔
527
            out_of_model = true;
39,930✔
528
            continue;
39,930✔
529
          }
530

531
          // Set birth cell attribute
532
          if (p.cell_born() == C_NONE)
2,922,805!
533
            p.cell_born() = p.lowest_coord().cell();
2,922,805✔
534

535
          // Initialize last cells from current cell
536
          for (int j = 0; j < p.n_coord(); ++j) {
5,845,610✔
537
            p.cell_last(j) = p.coord(j).cell();
2,922,805✔
538
          }
539
          p.n_coord_last() = p.n_coord();
2,922,805✔
540

541
          while (true) {
4,697,649✔
542
            // Ray trace from r_start to r_end
543
            Position r0 = p.r();
3,810,227✔
544
            double max_distance = bbox.max[axis] - r0[axis];
3,810,227✔
545

546
            // Find the distance to the nearest boundary
547
            BoundaryInfo boundary = distance_to_boundary(p);
3,810,227✔
548

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

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

558
            // Add volumes to any mesh elements that were crossed
559
            int i_material = p.material();
3,810,227✔
560
            if (i_material != C_NONE) {
3,810,227✔
561
              i_material = model::materials[i_material]->id();
1,126,781✔
562
            }
563
            double cumulative_frac = 0.0;
3,810,227✔
564
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
7,839,104✔
565
              int mesh_index = bins[i_bin];
4,028,877✔
566
              double length = distance * length_fractions[i_bin];
4,028,877✔
567
              double volume = length * d1 * d2;
4,028,877✔
568

569
              if (compute_bboxes) {
4,028,877✔
570
                double axis_start = r0[axis] + distance * cumulative_frac;
2,822,792✔
571
                double axis_end = axis_start + length;
2,822,792✔
572
                cumulative_frac += length_fractions[i_bin];
2,822,792✔
573

574
                Position contrib_min = site.r;
2,822,792✔
575
                Position contrib_max = site.r;
2,822,792✔
576

577
                contrib_min[ax1] = site.r[ax1] - 0.5 * d1;
2,822,792✔
578
                contrib_max[ax1] = site.r[ax1] + 0.5 * d1;
2,822,792✔
579
                contrib_min[ax2] = site.r[ax2] - 0.5 * d2;
2,822,792✔
580
                contrib_max[ax2] = site.r[ax2] + 0.5 * d2;
2,822,792✔
581
                contrib_min[axis] = std::min(axis_start, axis_end);
2,822,792!
582
                contrib_max[axis] = std::max(axis_start, axis_end);
5,645,584!
583

584
                BoundingBox contrib_bbox {contrib_min, contrib_max};
2,822,792✔
585
                contrib_bbox &= bbox;
2,822,792✔
586

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

595
            if (distance == max_distance)
3,810,227✔
596
              break;
597

598
            // cross next geometric surface
599
            for (int j = 0; j < p.n_coord(); ++j) {
1,774,844✔
600
              p.cell_last(j) = p.coord(j).cell();
887,422✔
601
            }
602
            p.n_coord_last() = p.n_coord();
887,422✔
603

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

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

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

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

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

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

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

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

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

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

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

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

739
  // Write mesh type
740
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,949✔
741

742
  // Write mesh ID
743
  write_attribute(mesh_group, "id", id_);
2,949✔
744

745
  // Write mesh name
746
  write_dataset(mesh_group, "name", name_);
2,949✔
747

748
  // Write mesh data
749
  this->to_hdf5_inner(mesh_group);
2,949✔
750

751
  // Close group
752
  close_group(mesh_group);
2,949✔
753
}
2,949✔
754

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

759
std::string StructuredMesh::bin_label(int bin) const
5,135,299✔
760
{
761
  MeshIndex ijk = get_indices_from_bin(bin);
5,135,299✔
762

763
  if (n_dimension_ > 2) {
5,135,299✔
764
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
5,119,360✔
765
  } else if (n_dimension_ > 1) {
15,939✔
766
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
15,664✔
767
  } else {
768
    return fmt::format("Mesh Index ({})", ijk[0]);
275✔
769
  }
770
}
771

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

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

784
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,417,377!
785
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,417,377!
786

787
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,417,377!
788
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,417,377!
789

790
  return {x_min + (x_max - x_min) * prn(seed),
1,417,377✔
791
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,417,377✔
792
}
793

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

929
const std::string UnstructuredMesh::mesh_type = "unstructured";
930

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

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

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

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

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

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

967
  int num_elem_skipped = 0;
32✔
968

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

978
    volumes.emplace_back(this->volume(i));
349,736!
979

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

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

1005
  write_dataset(mesh_group, "volumes", volumes);
32!
1006
  write_dataset(mesh_group, "connectivity", connectivity);
32!
1007
  write_dataset(mesh_group, "element_types", elem_types);
32!
1008
}
96✔
1009

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

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

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

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

1035
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
1036
      in_mesh = false;
102,653,580✔
1037
  }
1038
  return ijk;
1,191,158,769✔
1039
}
1040

1041
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,754,066,970✔
1042
{
1043
  switch (n_dimension_) {
1,754,066,970!
1044
  case 1:
880,605✔
1045
    return ijk[0] - 1;
880,605✔
1046
  case 2:
136,375,228✔
1047
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
136,375,228✔
1048
  case 3:
1,616,811,137✔
1049
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,616,811,137✔
1050
  default:
×
1051
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
1052
  }
1053
}
1054

1055
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
7,754,930✔
1056
{
1057
  MeshIndex ijk;
7,754,930✔
1058
  if (n_dimension_ == 1) {
7,754,930✔
1059
    ijk[0] = bin + 1;
275✔
1060
  } else if (n_dimension_ == 2) {
7,754,655✔
1061
    ijk[0] = bin % shape_[0] + 1;
15,664✔
1062
    ijk[1] = bin / shape_[0] + 1;
15,664✔
1063
  } else if (n_dimension_ == 3) {
7,738,991!
1064
    ijk[0] = bin % shape_[0] + 1;
7,738,991✔
1065
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
7,738,991✔
1066
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
7,738,991✔
1067
  }
1068
  return ijk;
7,754,930✔
1069
}
1070

1071
int StructuredMesh::get_bin(Position r) const
255,625,133✔
1072
{
1073
  // Determine indices
1074
  bool in_mesh;
255,625,133✔
1075
  MeshIndex ijk = get_indices(r, in_mesh);
255,625,133✔
1076
  if (!in_mesh)
255,625,133✔
1077
    return -1;
1078

1079
  // Convert indices to bin
1080
  return get_bin_from_indices(ijk);
234,579,285✔
1081
}
1082

1083
int StructuredMesh::n_bins() const
1,125,575✔
1084
{
1085
  return std::accumulate(
2,251,150✔
1086
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
1,125,575✔
1087
}
1088

1089
int StructuredMesh::n_surface_bins() const
370✔
1090
{
1091
  return 4 * n_dimension_ * n_bins();
370✔
1092
}
1093

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

1101
  // Create array of zeros
1102
  auto cnt = tensor::zeros<double>(shape);
×
1103
  bool outside_ = false;
1104

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

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

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

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

1121
  // Create reduced count data
1122
  auto counts = tensor::zeros<double>(shape);
×
1123
  int total = cnt.size();
×
1124

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

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

1140
  return counts;
×
1141
}
×
1142

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

1156
  // Compute the length of the entire track.
1157
  double total_distance = (r1 - r0).norm();
940,456,280✔
1158
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
940,456,280✔
1159
    return;
1160

1161
  // keep a copy of the original global position to pass to get_indices,
1162
  // which performs its own transformation to local coordinates
1163
  Position global_r = r0;
928,363,418✔
1164
  Position local_r = local_coords(r0);
928,363,418✔
1165

1166
  const int n = n_neighbors();
928,363,418✔
1167

1168
  // Flag if position is inside the mesh
1169
  bool inside_mesh;
1170

1171
  // Position is r = r0 + u * traveled_distance, start at r0
1172
  double traveled_distance {0.0};
928,363,418✔
1173

1174
  // Calculate index of current cell. Offset the position a tiny bit in
1175
  // direction of flight
1176
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, inside_mesh);
928,363,418✔
1177

1178
  // if track is very short, assume that it is completely inside one cell.
1179
  // Only the current cell will score and no surfaces
1180
  if (total_distance < 2 * TINY_BIT) {
928,363,418✔
1181
    if (inside_mesh) {
331,923✔
1182
      tally.track(ijk, 1.0);
331,307✔
1183
    }
1184
    return;
331,923✔
1185
  }
1186

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

1193
  // Loop until r = r1 is eventually reached
1194
  while (true) {
1195
    if (inside_mesh) {
1,687,312,326✔
1196

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

1201
      // Tally track length delta since last step
1202
      tally.track(ijk,
1,601,041,753✔
1203
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
2,147,483,647✔
1204
          total_distance);
1205

1206
      // update position and leave, if we have reached end position
1207
      traveled_distance = distances[k].distance;
1,601,041,753✔
1208
      if (traveled_distance >= total_distance)
1,601,041,753✔
1209
        return;
1210

1211
      // If we have not reached r1, we have hit a surface. Tally outward
1212
      // current
1213
      tally.surface(ijk, k, distances[k].max_surface, false);
752,110,613✔
1214

1215
      // Update cell and calculate distance to next surface in correlated
1216
      // directions.
1217
      for (int j = 0; j < 3; ++j)
2,147,483,647✔
1218
        ijk[j] += distances[k].offset[j];
2,147,483,647✔
1219
      sanitize_index(ijk);
752,110,613✔
1220
      for (auto j : correlated_axes_[k])
1,504,221,226✔
1221
        distances[j] =
752,110,613✔
1222
          distance_to_grid_boundary(ijk, j, local_r, u, traveled_distance);
752,110,613✔
1223

1224
      // Check if we have left the interior of the mesh
1225
      inside_mesh = index_inside_mesh(ijk, k);
752,110,613✔
1226

1227
      // If we are still inside the mesh, tally inward current for the next
1228
      // cell
1229
      if (inside_mesh)
29,576,899✔
1230
        tally.surface(ijk, k, !distances[k].max_surface, true);
757,864,614✔
1231

1232
    } else { // not inside mesh
1233
      int k_max;
1234
      traveled_distance =
1235
        distance_to_mesh(ijk, distances, traveled_distance, k_max);
86,270,573✔
1236
      // If r1 is not inside the mesh, exit here
1237
      if (traveled_distance >= total_distance)
86,270,573✔
1238
        return;
79,100,355✔
1239

1240
      // Calculate the new cell index and update all distances to next
1241
      // surfaces.
1242
      ijk =
1243
        get_indices(global_r + (traveled_distance + TINY_BIT) * u, inside_mesh);
7,170,218✔
1244
      for (int k = 0; k < n; ++k) {
28,472,334✔
1245
        distances[k] =
21,302,116✔
1246
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
21,302,116✔
1247
      }
1248

1249
      // If inside the mesh, Tally inward current
1250
      if (inside_mesh && k_max >= 0)
222,288!
1251
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
199,320✔
1252
    }
1253
  }
1254
}
1255

1256
double StructuredMesh::distance_to_mesh(const MeshIndex& ijk,
86,270,573✔
1257
  const std::array<MeshDistance, 4>& distances, double traveled_distance,
1258
  int& k_max) const
1259
{
1260
  // For all directions outside the mesh, find the distance that we need
1261
  // to travel to reach the next surface. Use the largest distance, as
1262
  // only this will cross all outer surfaces.
1263
  const int n = n_neighbors();
86,270,573✔
1264
  k_max = -1;
86,270,573✔
1265
  for (int k = 0; k < n; ++k) {
343,637,926✔
1266
    if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
257,367,353✔
1267
        (distances[k].distance > traveled_distance)) {
94,278,488✔
1268
      traveled_distance = distances[k].distance;
89,294,964✔
1269
      k_max = k;
89,294,964✔
1270
    }
1271
  }
1272
  // Assure some distance is traveled
1273
  if (k_max == -1) {
86,270,573✔
1274
    traveled_distance += TINY_BIT;
110✔
1275
  }
1276
  return traveled_distance;
86,270,573✔
1277
}
1278

1279
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
828,328,715✔
1280
  vector<int>& bins, vector<double>& lengths) const
1281
{
1282

1283
  // Helper tally class.
1284
  // stores a pointer to the mesh class and references to bins and lengths
1285
  // parameters. Performs the actual tally through the track method.
1286
  struct TrackAggregator {
828,328,715✔
1287
    TrackAggregator(
828,328,715✔
1288
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1289
      : mesh(_mesh), bins(_bins), lengths(_lengths)
828,328,715✔
1290
    {}
1291
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1292
    void track(const MeshIndex& ijk, double l) const
1,461,328,496✔
1293
    {
1294
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,461,328,496✔
1295
      lengths.push_back(l);
1,461,328,496✔
1296
    }
1,461,328,496✔
1297

1298
    const StructuredMesh* mesh;
1299
    vector<int>& bins;
1300
    vector<double>& lengths;
1301
  };
1302

1303
  // Perform the mesh raytrace with the helper class.
1304
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
828,328,715✔
1305
}
828,328,715✔
1306

1307
void StructuredMesh::surface_bins_crossed(
112,127,565✔
1308
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1309
{
1310

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

1330
    const StructuredMesh* mesh;
1331
    vector<int>& bins;
1332
  };
1333

1334
  // Perform the mesh raytrace with the helper class.
1335
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
112,127,565✔
1336
}
112,127,565✔
1337

1338
//==============================================================================
1339
// RegularMesh implementation
1340
//==============================================================================
1341

1342
int RegularMesh::set_grid()
2,126✔
1343
{
1344
  tensor::Tensor<int> shape(shape_.data(), static_cast<size_t>(n_dimension_));
2,126✔
1345

1346
  // Check that dimensions are all greater than zero
1347
  if ((shape <= 0).any()) {
6,378!
1348
    set_errmsg("All entries for a regular mesh dimensions "
×
1349
               "must be positive.");
1350
    return OPENMC_E_INVALID_ARGUMENT;
×
1351
  }
1352

1353
  // Make sure lower_left and dimension match
1354
  if (lower_left_.size() != n_dimension_) {
2,126!
1355
    set_errmsg("Number of entries in lower_left must be the same "
×
1356
               "as the regular mesh dimensions.");
1357
    return OPENMC_E_INVALID_ARGUMENT;
×
1358
  }
1359
  if (width_.size() > 0) {
2,126✔
1360

1361
    // Check to ensure width has same dimensions
1362
    if (width_.size() != n_dimension_) {
46!
1363
      set_errmsg("Number of entries on width must be the same as "
×
1364
                 "the regular mesh dimensions.");
1365
      return OPENMC_E_INVALID_ARGUMENT;
×
1366
    }
1367

1368
    // Check for negative widths
1369
    if ((width_ < 0.0).any()) {
138!
1370
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
1371
      return OPENMC_E_INVALID_ARGUMENT;
×
1372
    }
1373

1374
    // Set width and upper right coordinate
1375
    upper_right_ = lower_left_ + shape * width_;
138✔
1376

1377
  } else if (upper_right_.size() > 0) {
2,080!
1378

1379
    // Check to ensure upper_right_ has same dimensions
1380
    if (upper_right_.size() != n_dimension_) {
2,080!
1381
      set_errmsg("Number of entries on upper_right must be the "
×
1382
                 "same as the regular mesh dimensions.");
1383
      return OPENMC_E_INVALID_ARGUMENT;
×
1384
    }
1385

1386
    // Check that upper-right is above lower-left
1387
    if ((upper_right_ < lower_left_).any()) {
6,240!
1388
      set_errmsg(
×
1389
        "The upper_right coordinates of a regular mesh must be greater than "
1390
        "the lower_left coordinates.");
1391
      return OPENMC_E_INVALID_ARGUMENT;
×
1392
    }
1393

1394
    // Set width
1395
    width_ = (upper_right_ - lower_left_) / shape;
6,240✔
1396
  }
1397

1398
  // Set material volumes
1399
  volume_frac_ = 1.0 / shape.prod();
2,126✔
1400

1401
  element_volume_ = 1.0;
2,126✔
1402
  for (int i = 0; i < n_dimension_; i++) {
7,957✔
1403
    element_volume_ *= width_[i];
5,831✔
1404
  }
1405
  return 0;
1406
}
2,126✔
1407

1408
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
2,115✔
1409
{
1410
  // Determine number of dimensions for mesh
1411
  if (!check_for_node(node, "dimension")) {
2,115!
1412
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1413
  }
1414

1415
  tensor::Tensor<int> shape = get_node_tensor<int>(node, "dimension");
2,115✔
1416
  int n = n_dimension_ = shape.size();
2,115!
1417
  if (n != 1 && n != 2 && n != 3) {
2,115!
1418
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1419
  }
1420
  std::copy(shape.begin(), shape.end(), shape_.begin());
2,115✔
1421

1422
  // Check for lower-left coordinates
1423
  if (check_for_node(node, "lower_left")) {
2,115!
1424
    // Read mesh lower-left corner location
1425
    lower_left_ = get_node_tensor<double>(node, "lower_left");
2,115✔
1426
  } else {
1427
    fatal_error("Must specify <lower_left> on a mesh.");
×
1428
  }
1429

1430
  if (check_for_node(node, "width")) {
2,115✔
1431
    // Make sure one of upper-right or width were specified
1432
    if (check_for_node(node, "upper_right")) {
46!
1433
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1434
    }
1435

1436
    width_ = get_node_tensor<double>(node, "width");
92✔
1437

1438
  } else if (check_for_node(node, "upper_right")) {
2,069!
1439

1440
    upper_right_ = get_node_tensor<double>(node, "upper_right");
4,138✔
1441

1442
  } else {
1443
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1444
  }
1445

1446
  if (int err = set_grid()) {
2,115!
1447
    fatal_error(openmc_err_msg);
×
1448
  }
1449
}
2,115✔
1450

1451
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
11✔
1452
{
1453
  // Determine number of dimensions for mesh
1454
  if (!object_exists(group, "dimension")) {
11!
1455
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1456
  }
1457

1458
  tensor::Tensor<int> shape;
11✔
1459
  read_dataset(group, "dimension", shape);
11✔
1460
  int n = n_dimension_ = shape.size();
11!
1461
  if (n != 1 && n != 2 && n != 3) {
11!
1462
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1463
  }
1464
  std::copy(shape.begin(), shape.end(), shape_.begin());
11✔
1465

1466
  // Check for lower-left coordinates
1467
  if (object_exists(group, "lower_left")) {
11!
1468
    // Read mesh lower-left corner location
1469
    read_dataset(group, "lower_left", lower_left_);
11✔
1470
  } else {
1471
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1472
  }
1473

1474
  if (object_exists(group, "upper_right")) {
11!
1475

1476
    read_dataset(group, "upper_right", upper_right_);
11✔
1477

1478
  } else {
1479
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1480
  }
1481

1482
  if (int err = set_grid()) {
11!
1483
    fatal_error(openmc_err_msg);
×
1484
  }
1485
}
11✔
1486

1487
int RegularMesh::get_index_in_direction(double r, int i) const
2,147,483,647✔
1488
{
1489
  return std::ceil((r - lower_left_[i]) / width_[i]);
2,147,483,647✔
1490
}
1491

1492
const std::string RegularMesh::mesh_type = "regular";
1493

1494
std::string RegularMesh::get_mesh_type() const
3,225✔
1495
{
1496
  return mesh_type;
3,225✔
1497
}
1498

1499
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,486,377,415✔
1500
{
1501
  return lower_left_[i] + ijk[i] * width_[i];
1,486,377,415✔
1502
}
1503

1504
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,417,165,179✔
1505
{
1506
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,417,165,179✔
1507
}
1508

1509
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1510
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1511
  double l) const
1512
{
1513
  MeshDistance d;
2,147,483,647✔
1514
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1515
    return d;
1516

1517
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1518
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1519
    ++d.offset[i];
1,482,125,284✔
1520
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,482,125,284✔
1521
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,434,361,360✔
1522
    --d.offset[i];
1,412,913,048✔
1523
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,412,913,048✔
1524
  }
1525

1526
  return d;
1527
}
1528

1529
std::pair<vector<double>, vector<double>> RegularMesh::plot(
22✔
1530
  Position plot_ll, Position plot_ur) const
1531
{
1532
  // Figure out which axes lie in the plane of the plot.
1533
  array<int, 2> axes {-1, -1};
22✔
1534
  if (plot_ur.z == plot_ll.z) {
22!
1535
    axes[0] = 0;
22!
1536
    if (n_dimension_ > 1)
22!
1537
      axes[1] = 1;
22✔
1538
  } else if (plot_ur.y == plot_ll.y) {
×
1539
    axes[0] = 0;
×
1540
    if (n_dimension_ > 2)
×
1541
      axes[1] = 2;
×
1542
  } else if (plot_ur.x == plot_ll.x) {
×
1543
    if (n_dimension_ > 1)
×
1544
      axes[0] = 1;
×
1545
    if (n_dimension_ > 2)
×
1546
      axes[1] = 2;
×
1547
  } else {
1548
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1549
  }
1550

1551
  // Get the coordinates of the mesh lines along both of the axes.
1552
  array<vector<double>, 2> axis_lines;
1553
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
66✔
1554
    int axis = axes[i_ax];
44!
1555
    if (axis == -1)
44!
1556
      continue;
×
1557
    auto& lines {axis_lines[i_ax]};
44✔
1558

1559
    double coord = lower_left_[axis];
44✔
1560
    for (int i = 0; i < shape_[axis] + 1; ++i) {
286✔
1561
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
242!
1562
        lines.push_back(coord);
242✔
1563
      coord += width_[axis];
242✔
1564
    }
1565
  }
1566

1567
  return {axis_lines[0], axis_lines[1]};
44✔
1568
}
1569

1570
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
2,092✔
1571
{
1572
  write_dataset(mesh_group, "dimension", get_shape_tensor());
2,092✔
1573
  write_dataset(mesh_group, "lower_left", lower_left_);
2,092✔
1574
  write_dataset(mesh_group, "upper_right", upper_right_);
2,092✔
1575
  write_dataset(mesh_group, "width", width_);
2,092✔
1576
}
2,092✔
1577

1578
tensor::Tensor<double> RegularMesh::count_sites(
7,820✔
1579
  const SourceSite* bank, int64_t length, bool* outside) const
1580
{
1581
  // Determine shape of array for counts
1582
  std::size_t m = this->n_bins();
7,820✔
1583
  vector<std::size_t> shape = {m};
7,820✔
1584

1585
  // Create array of zeros
1586
  auto cnt = tensor::zeros<double>(shape);
7,820✔
1587
  bool outside_ = false;
2,892✔
1588

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

1592
    // determine scoring bin for entropy mesh
1593
    int mesh_bin = get_bin(site.r);
7,667,451✔
1594

1595
    // if outside mesh, skip particle
1596
    if (mesh_bin < 0) {
7,667,451!
1597
      outside_ = true;
×
1598
      continue;
×
1599
    }
1600

1601
    // Add to appropriate bin
1602
    cnt(mesh_bin) += site.wgt;
7,667,451✔
1603
  }
1604

1605
  // Create reduced count data
1606
  auto counts = tensor::zeros<double>(shape);
7,820✔
1607
  int total = cnt.size();
7,820✔
1608

1609
#ifdef OPENMC_MPI
1610
  // collect values from all processors
1611
  MPI_Reduce(
2,892✔
1612
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
2,892✔
1613

1614
  // Check if there were sites outside the mesh for any processor
1615
  if (outside) {
2,892!
1616
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
2,892✔
1617
  }
1618
#else
1619
  std::copy(cnt.data(), cnt.data() + total, counts.data());
4,928✔
1620
  if (outside)
4,928!
1621
    *outside = outside_;
4,928✔
1622
#endif
1623

1624
  return counts;
7,820✔
1625
}
7,820✔
1626

1627
double RegularMesh::volume(const MeshIndex& ijk) const
1,112,175✔
1628
{
1629
  return element_volume_;
1,112,175✔
1630
}
1631

1632
//==============================================================================
1633
// RectilinearMesh implementation
1634
//==============================================================================
1635

1636
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
122✔
1637
{
1638
  n_dimension_ = 3;
122✔
1639

1640
  grid_[0] = get_node_array<double>(node, "x_grid");
122✔
1641
  grid_[1] = get_node_array<double>(node, "y_grid");
122✔
1642
  grid_[2] = get_node_array<double>(node, "z_grid");
122✔
1643

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

1649
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
11✔
1650
{
1651
  n_dimension_ = 3;
11✔
1652

1653
  read_dataset(group, "x_grid", grid_[0]);
11✔
1654
  read_dataset(group, "y_grid", grid_[1]);
11✔
1655
  read_dataset(group, "z_grid", grid_[2]);
11✔
1656

1657
  if (int err = set_grid()) {
11!
1658
    fatal_error(openmc_err_msg);
×
1659
  }
1660
}
11✔
1661

1662
const std::string RectilinearMesh::mesh_type = "rectilinear";
1663

1664
std::string RectilinearMesh::get_mesh_type() const
275✔
1665
{
1666
  return mesh_type;
275✔
1667
}
1668

1669
double RectilinearMesh::positive_grid_boundary(
26,505,963✔
1670
  const MeshIndex& ijk, int i) const
1671
{
1672
  return grid_[i][ijk[i]];
26,505,963✔
1673
}
1674

1675
double RectilinearMesh::negative_grid_boundary(
25,739,406✔
1676
  const MeshIndex& ijk, int i) const
1677
{
1678
  return grid_[i][ijk[i] - 1];
25,739,406✔
1679
}
1680

1681
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
53,602,087✔
1682
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1683
  double l) const
1684
{
1685
  MeshDistance d;
53,602,087✔
1686
  if (std::abs(u[i]) < FP_PRECISION)
53,602,087✔
1687
    return d;
1688

1689
  d.max_surface = (u[i] > 0);
53,030,263✔
1690
  if (d.max_surface && (ijk[i] <= shape_[i])) {
53,030,263✔
1691
    ++d.offset[i];
26,505,963✔
1692
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
26,505,963✔
1693
  } else if (!d.max_surface && (ijk[i] > 0)) {
26,524,300✔
1694
    --d.offset[i];
25,739,406✔
1695
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
25,739,406✔
1696
  }
1697
  return d;
1698
}
1699

1700
int RectilinearMesh::set_grid()
177✔
1701
{
1702
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
177✔
1703
    static_cast<int>(grid_[1].size()) - 1,
177✔
1704
    static_cast<int>(grid_[2].size()) - 1};
177✔
1705

1706
  for (const auto& g : grid_) {
708✔
1707
    if (g.size() < 2) {
531!
1708
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1709
                 "must each have at least 2 points");
1710
      return OPENMC_E_INVALID_ARGUMENT;
×
1711
    }
1712
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
531!
1713
        g.end()) {
531!
1714
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1715
                 "rectilinear meshes must be sorted and unique.");
1716
      return OPENMC_E_INVALID_ARGUMENT;
×
1717
    }
1718
  }
1719

1720
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
177✔
1721
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
177✔
1722

1723
  return 0;
177✔
1724
}
1725

1726
int RectilinearMesh::get_index_in_direction(double r, int i) const
74,108,892✔
1727
{
1728
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,108,892✔
1729
}
1730

1731
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
11✔
1732
  Position plot_ll, Position plot_ur) const
1733
{
1734
  // Figure out which axes lie in the plane of the plot.
1735
  array<int, 2> axes {-1, -1};
11✔
1736
  if (plot_ur.z == plot_ll.z) {
11!
1737
    axes = {0, 1};
×
1738
  } else if (plot_ur.y == plot_ll.y) {
11!
1739
    axes = {0, 2};
11✔
1740
  } else if (plot_ur.x == plot_ll.x) {
×
1741
    axes = {1, 2};
×
1742
  } else {
1743
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1744
  }
1745

1746
  // Get the coordinates of the mesh lines along both of the axes.
1747
  array<vector<double>, 2> axis_lines;
1748
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
33✔
1749
    int axis = axes[i_ax];
22✔
1750
    vector<double>& lines {axis_lines[i_ax]};
22✔
1751

1752
    for (auto coord : grid_[axis]) {
110✔
1753
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
88!
1754
        lines.push_back(coord);
88✔
1755
    }
1756
  }
1757

1758
  return {axis_lines[0], axis_lines[1]};
22✔
1759
}
1760

1761
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
110✔
1762
{
1763
  write_dataset(mesh_group, "x_grid", grid_[0]);
110✔
1764
  write_dataset(mesh_group, "y_grid", grid_[1]);
110✔
1765
  write_dataset(mesh_group, "z_grid", grid_[2]);
110✔
1766
}
110✔
1767

1768
double RectilinearMesh::volume(const MeshIndex& ijk) const
132✔
1769
{
1770
  double vol {1.0};
132✔
1771

1772
  for (int i = 0; i < n_dimension_; i++) {
528✔
1773
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
396✔
1774
  }
1775
  return vol;
132✔
1776
}
1777

1778
//==============================================================================
1779
// CylindricalMesh implementation
1780
//==============================================================================
1781

1782
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
400✔
1783
  : PeriodicStructuredMesh {node}
400✔
1784
{
1785
  n_dimension_ = 3;
400✔
1786
  grid_[0] = get_node_array<double>(node, "r_grid");
400✔
1787
  grid_[1] = get_node_array<double>(node, "phi_grid");
400✔
1788
  grid_[2] = get_node_array<double>(node, "z_grid");
400✔
1789
  origin_ = get_node_position(node, "origin");
400✔
1790

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

1796
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1797
{
1798
  n_dimension_ = 3;
11✔
1799
  read_dataset(group, "r_grid", grid_[0]);
11✔
1800
  read_dataset(group, "phi_grid", grid_[1]);
11✔
1801
  read_dataset(group, "z_grid", grid_[2]);
11✔
1802
  read_dataset(group, "origin", origin_);
11✔
1803

1804
  if (int err = set_grid()) {
11!
1805
    fatal_error(openmc_err_msg);
×
1806
  }
1807
}
11✔
1808

1809
const std::string CylindricalMesh::mesh_type = "cylindrical";
1810

1811
std::string CylindricalMesh::get_mesh_type() const
484✔
1812
{
1813
  return mesh_type;
484✔
1814
}
1815

1816
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
47,732,091✔
1817
  Position r, bool& in_mesh) const
1818
{
1819
  r = local_coords(r);
47,732,091✔
1820

1821
  Position mapped_r;
47,732,091✔
1822
  mapped_r[0] = std::hypot(r.x, r.y);
47,732,091✔
1823
  mapped_r[2] = r[2];
47,732,091✔
1824

1825
  if (mapped_r[0] < FP_PRECISION) {
47,732,091!
1826
    mapped_r[1] = 0.0;
1827
  } else {
1828
    mapped_r[1] = std::atan2(r.y, r.x);
47,732,091✔
1829
    if (mapped_r[1] < 0)
47,732,091✔
1830
      mapped_r[1] += 2 * M_PI;
23,874,862✔
1831
  }
1832

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

1835
  idx[1] = sanitize_phi(idx[1]);
47,732,091✔
1836

1837
  return idx;
47,732,091✔
1838
}
1839

1840
Position CylindricalMesh::sample_element(
88,110✔
1841
  const MeshIndex& ijk, uint64_t* seed) const
1842
{
1843
  double r_min = this->r(ijk[0] - 1);
88,110✔
1844
  double r_max = this->r(ijk[0]);
88,110✔
1845

1846
  double phi_min = this->phi(ijk[1] - 1);
88,110✔
1847
  double phi_max = this->phi(ijk[1]);
88,110✔
1848

1849
  double z_min = this->z(ijk[2] - 1);
88,110✔
1850
  double z_max = this->z(ijk[2]);
88,110✔
1851

1852
  double r_min_sq = r_min * r_min;
88,110✔
1853
  double r_max_sq = r_max * r_max;
88,110✔
1854
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
88,110✔
1855
  double phi = uniform_distribution(phi_min, phi_max, seed);
88,110✔
1856
  double z = uniform_distribution(z_min, z_max, seed);
88,110✔
1857

1858
  double x = r * std::cos(phi);
88,110✔
1859
  double y = r * std::sin(phi);
88,110✔
1860

1861
  return origin_ + Position(x, y, z);
88,110✔
1862
}
1863

1864
double CylindricalMesh::find_r_crossing(
142,588,130✔
1865
  const Position& r, const Direction& u, double l, int shell) const
1866
{
1867

1868
  if ((shell < 0) || (shell > shape_[0]))
142,588,130!
1869
    return INFTY;
1870

1871
  // solve r.x^2 + r.y^2 == r0^2
1872
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1873
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1874

1875
  const double r0 = grid_[0][shell];
124,674,221✔
1876
  if (r0 == 0.0)
124,674,221✔
1877
    return INFTY;
1878

1879
  const double denominator = u.x * u.x + u.y * u.y;
117,538,147✔
1880

1881
  // Direction of flight is in z-direction. Will never intersect r.
1882
  if (std::abs(denominator) < FP_PRECISION)
117,538,147✔
1883
    return INFTY;
1884

1885
  // inverse of dominator to help the compiler to speed things up
1886
  const double inv_denominator = 1.0 / denominator;
117,479,187✔
1887

1888
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,479,187✔
1889
  double R = std::sqrt(r.x * r.x + r.y * r.y);
117,479,187✔
1890
  double D = p * p - (R - r0) * (R + r0) * inv_denominator;
117,479,187✔
1891

1892
  if (D < 0.0)
117,479,187✔
1893
    return INFTY;
1894

1895
  D = std::sqrt(D);
107,743,065✔
1896

1897
  // Particle is already on the shell surface; avoid spurious crossing
1898
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
107,743,065✔
1899
    return INFTY;
1900

1901
  // Check -p - D first because it is always smaller as -p + D
1902
  if (-p - D > l)
101,109,691✔
1903
    return -p - D;
1904
  if (-p + D > l)
80,901,724✔
1905
    return -p + D;
50,078,385✔
1906

1907
  return INFTY;
1908
}
1909

1910
double CylindricalMesh::find_phi_crossing(
74,456,404✔
1911
  const Position& r, const Direction& u, double l, int shell) const
1912
{
1913
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1914
  if (full_phi_ && (shape_[1] == 1))
74,456,404✔
1915
    return INFTY;
1916

1917
  shell = sanitize_phi(shell);
43,970,718✔
1918

1919
  const double p0 = grid_[1][shell];
43,970,718✔
1920

1921
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1922
  // => x(s) * cos(p0) = y(s) * sin(p0)
1923
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1924
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1925

1926
  const double c0 = std::cos(p0);
43,970,718✔
1927
  const double s0 = std::sin(p0);
43,970,718✔
1928

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

1931
  // Check if direction of flight is not parallel to phi surface
1932
  if (std::abs(denominator) > FP_PRECISION) {
43,970,718✔
1933
    const double s = -(r.x * s0 - r.y * c0) / denominator;
43,709,974✔
1934
    // Check if solution is in positive direction of flight and crosses the
1935
    // correct phi surface (not -phi)
1936
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
43,709,974✔
1937
      return s;
20,219,859✔
1938
  }
1939

1940
  return INFTY;
1941
}
1942

1943
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
36,695,747✔
1944
  const Position& r, const Direction& u, double l, int shell) const
1945
{
1946
  MeshDistance d;
36,695,747✔
1947

1948
  // Direction of flight is within xy-plane. Will never intersect z.
1949
  if (std::abs(u.z) < FP_PRECISION)
36,695,747✔
1950
    return d;
1951

1952
  d.max_surface = (u.z > 0.0);
35,577,531✔
1953
  if (d.max_surface && (shell <= shape_[2])) {
35,577,531✔
1954
    ++d.offset[2];
16,875,892✔
1955
    d.distance = (grid_[2][shell] - r.z) / u.z;
16,875,892✔
1956
  } else if (!d.max_surface && (shell > 0)) {
18,701,639✔
1957
    --d.offset[2];
16,846,225✔
1958
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
16,846,225✔
1959
  }
1960
  return d;
1961
}
1962

1963
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,218,014✔
1964
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1965
  double l) const
1966
{
1967
  if (i == 0) {
145,218,014✔
1968
    return std::min(
142,588,130✔
1969
      MeshDistance({1, 0, 0}, true, find_r_crossing(r0, u, l, ijk[i])),
71,294,065✔
1970
      MeshDistance({-1, 0, 0}, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,588,130✔
1971

1972
  } else if (i == 1) {
73,923,949✔
1973

1974
    return std::min(
74,456,404✔
1975
      MeshDistance({0, 1, 0}, true, find_phi_crossing(r0, u, l, ijk[i])),
37,228,202✔
1976
      MeshDistance({0, -1, 0}, false, find_phi_crossing(r0, u, l, ijk[i] - 1)));
74,456,404✔
1977

1978
  } else {
1979
    return find_z_crossing(r0, u, l, ijk[i]);
36,695,747✔
1980
  }
1981
}
1982

1983
int CylindricalMesh::set_grid()
433✔
1984
{
1985
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
433✔
1986
    static_cast<int>(grid_[1].size()) - 1,
433✔
1987
    static_cast<int>(grid_[2].size()) - 1};
433✔
1988

1989
  for (const auto& g : grid_) {
1,732✔
1990
    if (g.size() < 2) {
1,299!
1991
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
1992
                 "must each have at least 2 points");
1993
      return OPENMC_E_INVALID_ARGUMENT;
×
1994
    }
1995
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,299!
1996
        g.end()) {
1,299!
1997
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
1998
                 "cylindrical meshes must be sorted and unique.");
1999
      return OPENMC_E_INVALID_ARGUMENT;
×
2000
    }
2001
  }
2002
  if (grid_[0].front() < 0.0) {
433!
2003
    set_errmsg("r-grid for "
×
2004
               "cylindrical meshes must start at r >= 0.");
2005
    return OPENMC_E_INVALID_ARGUMENT;
×
2006
  }
2007
  if (grid_[1].front() < 0.0) {
433!
2008
    set_errmsg("phi-grid for "
×
2009
               "cylindrical meshes must start at phi >= 0.");
2010
    return OPENMC_E_INVALID_ARGUMENT;
×
2011
  }
2012
  if (grid_[1].back() > 2.0 * PI) {
433!
2013
    set_errmsg("phi-grids for "
×
2014
               "cylindrical meshes must end with theta <= 2*pi.");
2015

2016
    return OPENMC_E_INVALID_ARGUMENT;
×
2017
  }
2018

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

2021
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
433✔
2022
    origin_[2] + grid_[2].front()};
433✔
2023
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
433✔
2024
    origin_[2] + grid_[2].back()};
433✔
2025

2026
  return 0;
433✔
2027
}
2028

2029
int CylindricalMesh::get_index_in_direction(double r, int i) const
143,196,273✔
2030
{
2031
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
143,196,273✔
2032
}
2033

2034
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
2035
  Position plot_ll, Position plot_ur) const
2036
{
2037
  fatal_error("Plot of cylindrical Mesh not implemented");
×
2038

2039
  // Figure out which axes lie in the plane of the plot.
2040
  array<vector<double>, 2> axis_lines;
2041
  return {axis_lines[0], axis_lines[1]};
2042
}
2043

2044
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
374✔
2045
{
2046
  write_dataset(mesh_group, "r_grid", grid_[0]);
374✔
2047
  write_dataset(mesh_group, "phi_grid", grid_[1]);
374✔
2048
  write_dataset(mesh_group, "z_grid", grid_[2]);
374✔
2049
  write_dataset(mesh_group, "origin", origin_);
374✔
2050
}
374✔
2051

2052
double CylindricalMesh::volume(const MeshIndex& ijk) const
792✔
2053
{
2054
  double r_i = grid_[0][ijk[0] - 1];
792✔
2055
  double r_o = grid_[0][ijk[0]];
792✔
2056

2057
  double phi_i = grid_[1][ijk[1] - 1];
792✔
2058
  double phi_o = grid_[1][ijk[1]];
792✔
2059

2060
  double z_i = grid_[2][ijk[2] - 1];
792✔
2061
  double z_o = grid_[2][ijk[2]];
792✔
2062

2063
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
792✔
2064
}
2065

2066
//==============================================================================
2067
// SphericalMesh implementation
2068
//==============================================================================
2069

2070
SphericalMesh::SphericalMesh(pugi::xml_node node)
345✔
2071
  : PeriodicStructuredMesh {node}
345✔
2072
{
2073
  n_dimension_ = 3;
345✔
2074
  grid_[0] = get_node_array<double>(node, "r_grid");
345✔
2075
  grid_[1] = get_node_array<double>(node, "theta_grid");
345✔
2076
  grid_[2] = get_node_array<double>(node, "phi_grid");
345✔
2077
  origin_ = get_node_position(node, "origin");
345✔
2078

2079
  if (int err = set_grid()) {
345!
2080
    fatal_error(openmc_err_msg);
×
2081
  }
2082
}
345✔
2083

2084
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
2085
{
2086
  n_dimension_ = 3;
11✔
2087
  read_dataset(group, "r_grid", grid_[0]);
11✔
2088
  read_dataset(group, "theta_grid", grid_[1]);
11✔
2089
  read_dataset(group, "phi_grid", grid_[2]);
11✔
2090
  read_dataset(group, "origin", origin_);
11✔
2091

2092
  if (int err = set_grid()) {
11!
2093
    fatal_error(openmc_err_msg);
×
2094
  }
2095
}
11✔
2096

2097
const std::string SphericalMesh::mesh_type = "spherical";
2098

2099
std::string SphericalMesh::get_mesh_type() const
385✔
2100
{
2101
  return mesh_type;
385✔
2102
}
2103

2104
StructuredMesh::MeshIndex SphericalMesh::get_indices(
68,592,128✔
2105
  Position r, bool& in_mesh) const
2106
{
2107
  r = local_coords(r);
68,592,128✔
2108

2109
  Position mapped_r;
68,592,128✔
2110
  mapped_r[0] = r.norm();
68,592,128✔
2111

2112
  if (mapped_r[0] < FP_PRECISION) {
68,592,128!
2113
    mapped_r[1] = 0.0;
2114
    mapped_r[2] = 0.0;
2115
  } else {
2116
    mapped_r[1] = std::acos(r.z / mapped_r.x);
68,592,128✔
2117
    mapped_r[2] = std::atan2(r.y, r.x);
68,592,128✔
2118
    if (mapped_r[2] < 0)
68,592,128✔
2119
      mapped_r[2] += 2 * M_PI;
34,268,685✔
2120
  }
2121

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

2124
  idx[1] = sanitize_theta(idx[1]);
68,592,128✔
2125
  idx[2] = sanitize_phi(idx[2]);
68,592,128✔
2126

2127
  return idx;
68,592,128✔
2128
}
2129

2130
Position SphericalMesh::sample_element(
110✔
2131
  const MeshIndex& ijk, uint64_t* seed) const
2132
{
2133
  double r_min = this->r(ijk[0] - 1);
110✔
2134
  double r_max = this->r(ijk[0]);
110✔
2135

2136
  double theta_min = this->theta(ijk[1] - 1);
110✔
2137
  double theta_max = this->theta(ijk[1]);
110✔
2138

2139
  double phi_min = this->phi(ijk[2] - 1);
110✔
2140
  double phi_max = this->phi(ijk[2]);
110✔
2141

2142
  double cos_theta =
110✔
2143
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
110✔
2144
  double sin_theta = std::sin(std::acos(cos_theta));
110✔
2145
  double phi = uniform_distribution(phi_min, phi_max, seed);
110✔
2146
  double r_min_cub = std::pow(r_min, 3);
110✔
2147
  double r_max_cub = std::pow(r_max, 3);
110✔
2148
  // might be faster to do rejection here?
2149
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110✔
2150

2151
  double x = r * std::cos(phi) * sin_theta;
110✔
2152
  double y = r * std::sin(phi) * sin_theta;
110✔
2153
  double z = r * cos_theta;
110✔
2154

2155
  return origin_ + Position(x, y, z);
110✔
2156
}
2157

2158
double SphericalMesh::find_r_crossing(
444,032,534✔
2159
  const Position& r, const Direction& u, double l, int shell) const
2160
{
2161
  if ((shell < 0) || (shell > shape_[0]))
444,032,534✔
2162
    return INFTY;
2163

2164
  // solve |r+s*u| = r0
2165
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2166
  const double r0 = grid_[0][shell];
404,411,447✔
2167
  if (r0 == 0.0)
404,411,447✔
2168
    return INFTY;
2169
  const double p = r.dot(u);
396,732,930✔
2170
  double R = r.norm();
396,732,930✔
2171
  double D = p * p - (R - r0) * (R + r0);
396,732,930✔
2172

2173
  // Particle is already on the shell surface; avoid spurious crossing
2174
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
396,732,930✔
2175
    return INFTY;
2176

2177
  if (D >= 0.0) {
386,024,276✔
2178
    D = std::sqrt(D);
358,147,328✔
2179
    // Check -p - D first because it is always smaller as -p + D
2180
    if (-p - D > l)
358,147,328✔
2181
      return -p - D;
2182
    if (-p + D > l)
293,819,152✔
2183
      return -p + D;
177,267,453✔
2184
  }
2185

2186
  return INFTY;
2187
}
2188

2189
double SphericalMesh::find_theta_crossing(
110,161,348✔
2190
  const Position& r, const Direction& u, double l, int shell) const
2191
{
2192
  // Theta grid is [0, π], thus there is no real surface to cross
2193
  if (full_theta_ && (shape_[1] == 1))
110,161,348✔
2194
    return INFTY;
2195

2196
  shell = sanitize_theta(shell);
38,358,540✔
2197

2198
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2199
  // yields
2200
  // a*s^2 + 2*b*s + c == 0 with
2201
  // a = cos(theta)^2 - u.z * u.z
2202
  // b = r*u * cos(theta)^2 - u.z * r.z
2203
  // c = r*r * cos(theta)^2 - r.z^2
2204

2205
  const double cos_t = std::cos(grid_[1][shell]);
38,358,540✔
2206
  const bool sgn = std::signbit(cos_t);
38,358,540✔
2207
  const double cos_t_2 = cos_t * cos_t;
38,358,540✔
2208

2209
  const double a = cos_t_2 - u.z * u.z;
38,358,540✔
2210
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
38,358,540✔
2211
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
38,358,540✔
2212

2213
  // if factor of s^2 is zero, direction of flight is parallel to theta
2214
  // surface
2215
  if (std::abs(a) < FP_PRECISION) {
38,358,540✔
2216
    // if b vanishes, direction of flight is within theta surface and crossing
2217
    // is not possible
2218
    if (std::abs(b) < FP_PRECISION)
482,548!
2219
      return INFTY;
2220

2221
    const double s = -0.5 * c / b;
×
2222
    // Check if solution is in positive direction of flight and has correct
2223
    // sign
2224
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
2225
      return s;
×
2226

2227
    // no crossing is possible
2228
    return INFTY;
2229
  }
2230

2231
  const double p = b / a;
37,875,992✔
2232
  double D = p * p - c / a;
37,875,992✔
2233

2234
  if (D < 0.0)
37,875,992✔
2235
    return INFTY;
2236

2237
  D = std::sqrt(D);
26,921,004✔
2238

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

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

2250
  return INFTY;
2251
}
2252

2253
double SphericalMesh::find_phi_crossing(
111,750,826✔
2254
  const Position& r, const Direction& u, double l, int shell) const
2255
{
2256
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2257
  if (full_phi_ && (shape_[2] == 1))
111,750,826✔
2258
    return INFTY;
2259

2260
  shell = sanitize_phi(shell);
39,948,018✔
2261

2262
  const double p0 = grid_[2][shell];
39,948,018✔
2263

2264
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2265
  // => x(s) * cos(p0) = y(s) * sin(p0)
2266
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2267
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2268

2269
  const double c0 = std::cos(p0);
39,948,018✔
2270
  const double s0 = std::sin(p0);
39,948,018✔
2271

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

2274
  // Check if direction of flight is not parallel to phi surface
2275
  if (std::abs(denominator) > FP_PRECISION) {
39,948,018✔
2276
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,714,026✔
2277
    // Check if solution is in positive direction of flight and crosses the
2278
    // correct phi surface (not -phi)
2279
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,714,026✔
2280
      return s;
17,579,452✔
2281
  }
2282

2283
  return INFTY;
2284
}
2285

2286
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
332,972,354✔
2287
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2288
  double l) const
2289
{
2290

2291
  if (i == 0) {
332,972,354✔
2292
    return std::min(
444,032,534✔
2293
      MeshDistance({1, 0, 0}, true, find_r_crossing(r0, u, l, ijk[i])),
222,016,267✔
2294
      MeshDistance({-1, 0, 0}, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
444,032,534✔
2295

2296
  } else if (i == 1) {
110,956,087✔
2297
    return std::min(
110,161,348✔
2298
      MeshDistance({0, 1, 0}, true, find_theta_crossing(r0, u, l, ijk[i])),
55,080,674✔
2299
      MeshDistance(
55,080,674✔
2300
        {0, -1, 0}, false, find_theta_crossing(r0, u, l, ijk[i] - 1)));
110,161,348✔
2301

2302
  } else {
2303
    return std::min(
111,750,826✔
2304
      MeshDistance({0, 0, 1}, true, find_phi_crossing(r0, u, l, ijk[i])),
55,875,413✔
2305
      MeshDistance({0, 0, -1}, false, find_phi_crossing(r0, u, l, ijk[i] - 1)));
111,750,826✔
2306
  }
2307
}
2308

2309
int SphericalMesh::set_grid()
378✔
2310
{
2311
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
378✔
2312
    static_cast<int>(grid_[1].size()) - 1,
378✔
2313
    static_cast<int>(grid_[2].size()) - 1};
378✔
2314

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

2337
    return OPENMC_E_INVALID_ARGUMENT;
×
2338
  }
2339
  if (grid_[2].back() > 2 * PI) {
378!
2340
    set_errmsg("phi-grids for "
×
2341
               "spherical meshes must end with phi <= 2*pi.");
2342
    return OPENMC_E_INVALID_ARGUMENT;
×
2343
  }
2344

2345
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
378!
2346
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
378✔
2347

2348
  double r = grid_[0].back();
378✔
2349
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
378✔
2350
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
378✔
2351

2352
  return 0;
378✔
2353
}
2354

2355
int SphericalMesh::get_index_in_direction(double r, int i) const
205,776,384✔
2356
{
2357
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
205,776,384✔
2358
}
2359

2360
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2361
  Position plot_ll, Position plot_ur) const
2362
{
2363
  fatal_error("Plot of spherical Mesh not implemented");
×
2364

2365
  // Figure out which axes lie in the plane of the plot.
2366
  array<vector<double>, 2> axis_lines;
2367
  return {axis_lines[0], axis_lines[1]};
2368
}
2369

2370
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
319✔
2371
{
2372
  write_dataset(mesh_group, "r_grid", grid_[0]);
319✔
2373
  write_dataset(mesh_group, "theta_grid", grid_[1]);
319✔
2374
  write_dataset(mesh_group, "phi_grid", grid_[2]);
319✔
2375
  write_dataset(mesh_group, "origin", origin_);
319✔
2376
}
319✔
2377

2378
double SphericalMesh::volume(const MeshIndex& ijk) const
935✔
2379
{
2380
  double r_i = grid_[0][ijk[0] - 1];
935✔
2381
  double r_o = grid_[0][ijk[0]];
935✔
2382

2383
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2384
  double theta_o = grid_[1][ijk[1]];
935✔
2385

2386
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2387
  double phi_o = grid_[2][ijk[2]];
935✔
2388

2389
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
1,870✔
2390
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
935✔
2391
}
2392

2393
//==============================================================================
2394
// HexagonalMesh implementation
2395
//==============================================================================
2396

2397
HexagonalMesh::HexagonalMesh(pugi::xml_node node)
22✔
2398
  : PeriodicStructuredMesh {node}
22✔
2399
{
2400
  n_dimension_ = 3;
22✔
2401
  correlated_axes_ = {{0, 1, 2}, {0, 1, 2}, {0, 1, 2}, {3}};
132!
2402
  grid_ = get_node_array<double>(node, "grid");
22✔
2403
  radius_ = std::stoi(get_node_value(node, "radius"));
44✔
2404
  size_ = std::stod(get_node_value(node, "size"));
44✔
2405
  origin_ = get_node_position(node, "origin");
22✔
2406

2407
  // Read the orientation.  Default to 'y'.
2408
  if (check_for_node(node, "orientation")) {
22!
2409
    std::string orientation = get_node_value(node, "orientation");
22✔
2410
    if (orientation == "x") {
22✔
2411
      orientation_ = Orientation::x;
11✔
2412
    } else if (orientation != "y") {
11!
NEW
2413
      fatal_error("Unrecognized orientation '" + orientation + "'");
×
2414
    }
2415
  }
22✔
2416

2417
  if (int err = set_grid()) {
22!
NEW
2418
    fatal_error(openmc_err_msg);
×
2419
  }
2420
}
22✔
2421

NEW
2422
HexagonalMesh::HexagonalMesh(hid_t group) : PeriodicStructuredMesh {group}
×
2423
{
NEW
2424
  n_dimension_ = 3;
×
NEW
2425
  correlated_axes_ = {{0, 1, 2}, {0, 1, 2}, {0, 1, 2}, {3}};
×
NEW
2426
  read_dataset(group, "grid", grid_);
×
NEW
2427
  read_attribute(group, "radius", radius_);
×
NEW
2428
  read_attribute(group, "size", size_);
×
NEW
2429
  read_dataset(group, "origin", origin_);
×
2430

NEW
2431
  if (attribute_exists(group, "orientation")) {
×
NEW
2432
    std::string orientation;
×
NEW
2433
    read_attribute(group, "orientation", orientation);
×
NEW
2434
    if (orientation == "x") {
×
NEW
2435
      orientation_ = Orientation::x;
×
NEW
2436
    } else if (orientation != "y") {
×
NEW
2437
      fatal_error("Unrecognized orientation '" + orientation + "'");
×
2438
    }
NEW
2439
  }
×
NEW
2440
  if (int err = set_grid()) {
×
NEW
2441
    fatal_error(openmc_err_msg);
×
2442
  }
NEW
2443
}
×
2444

2445
const std::string HexagonalMesh::mesh_type = "hexagonal";
2446

2447
std::string HexagonalMesh::get_mesh_type() const
22✔
2448
{
2449
  return mesh_type;
22✔
2450
}
2451

2452
int HexagonalMesh::get_bin(Position r) const
1,049,961✔
2453
{
2454
  // Determine indices
2455
  bool in_mesh;
1,049,961✔
2456
  MeshIndex ijk = get_indices(r, in_mesh);
1,049,961✔
2457

2458
  if (!in_mesh)
1,049,961!
2459
    return -1;
2460

2461
  // Convert indices to bin
2462
  return get_bin_from_indices(ijk);
1,049,961✔
2463
}
2464

2465
int HexagonalMesh::n_bins() const
22✔
2466
{
2467
  return (1 + 3 * (radius_ + 1) * radius_) * (grid_.size() - 1);
22✔
2468
}
2469

NEW
2470
int HexagonalMesh::n_surface_bins() const
×
2471
{
NEW
2472
  return 4 * 4 * n_bins();
×
2473
}
2474

2475
int HexagonalMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,049,961✔
2476
{
2477
  int r = ijk[0];
1,049,961✔
2478
  int q = ijk[1];
1,049,961✔
2479
  int s = -r - q;
1,049,961✔
2480
  int k = ijk[2];
1,049,961✔
2481
  int rad = std::max({std::abs(r), std::abs(q), std::abs(s)});
1,049,961✔
2482
  int offset = 3 * (radius_ * (radius_ + 1) - rad * (rad + 1));
1,049,961✔
2483
  int side, pos;
1,049,961✔
2484
  if ((q == rad) && (s == -rad)) {
1,049,961✔
2485
    side = 0;
2486
    pos = r;
2487
  } else if ((s == -rad) && (r == rad)) {
707,465✔
2488
    side = 1;
107,140✔
2489
    pos = -q;
107,140✔
2490
  } else if ((r == rad) && (q == -rad)) {
600,325✔
2491
    side = 2;
108,097✔
2492
    pos = -s;
108,097✔
2493
  } else if ((q == -rad) && (s == rad)) {
492,228✔
2494
    side = 3;
2495
    pos = -r;
2496
  } else if ((s == rad) && (r == -rad)) {
383,823✔
2497
    side = 4;
2498
    pos = q;
2499
  } else {
2500
    side = 5;
275,352✔
2501
    pos = s;
275,352✔
2502
  }
2503
  return (k - 1) * (grid_.size() - 1) + offset + side * rad + pos;
1,049,961✔
2504
}
2505

2506
StructuredMesh::MeshIndex HexagonalMesh::get_indices(
1,049,961✔
2507
  Position r, bool& in_mesh) const
2508
{
2509
  r = local_coords(r);
1,049,961✔
2510

2511
  double q = q_dual_.dot(r);
1,049,961✔
2512
  double r0 = r_dual_.dot(r);
1,049,961✔
2513
  double s = -q - r0;
1,049,961✔
2514

2515
  double q_r = std::round(q);
1,049,961✔
2516
  double r_r = std::round(r0);
1,049,961✔
2517
  double s_r = std::round(s);
1,049,961✔
2518
  std::array<double, 3> diff = {
1,049,961✔
2519
    std::abs(q - q_r), std::abs(r0 - r_r), std::abs(s - s_r)};
1,049,961✔
2520
  auto max_it = std::max_element(diff.begin(), diff.end());
1,049,961✔
2521
  int i = static_cast<int>(std::distance(diff.begin(), max_it));
1,049,961✔
2522

2523
  if (i == 0)
1,049,961✔
2524
    q_r = -s_r - r_r;
348,524✔
2525
  else if (i == 1)
701,437✔
2526
    r_r = -q_r - s_r;
348,788✔
2527

2528
  int z_i = get_index_in_z_direction(r.z);
1,049,961✔
2529
  int q_i = static_cast<int>(q_r);
1,049,961✔
2530
  int r_i = static_cast<int>(r_r);
1,049,961✔
2531
  int s_i = -q_i - r_i;
1,049,961✔
2532

2533
  MeshIndex ijk;
1,049,961✔
2534
  in_mesh = true;
1,049,961✔
2535

2536
  ijk[0] = q_i;
1,049,961!
2537
  ijk[1] = r_i;
1,049,961✔
2538
  ijk[2] = z_i;
1,049,961✔
2539

2540
  if (std::max({std::abs(q_i), std::abs(r_i), std::abs(s_i)}) > radius_)
1,049,961!
NEW
2541
    in_mesh = false;
×
2542
  if (ijk[2] < 1 || ijk[2] > grid_.size() - 1)
1,049,961!
NEW
2543
    in_mesh = false;
×
2544

2545
  return ijk;
1,049,961✔
2546
}
2547

NEW
2548
Position HexagonalMesh::sample_element(
×
2549
  const MeshIndex& ijk, uint64_t* seed) const
2550
{
NEW
2551
  double q0 = ijk[0];
×
NEW
2552
  double r0 = ijk[1];
×
NEW
2553
  double z = uniform_distribution(grid_[ijk[2] - 1], grid_[ijk[2]], seed);
×
NEW
2554
  double q, r, s;
×
NEW
2555
  do {
×
NEW
2556
    double rad = std::cbrt(uniform_distribution(0.0, std::pow(size_, 3), seed));
×
NEW
2557
    double phi = uniform_distribution(0.0, 2 * PI, seed);
×
NEW
2558
    double x = rad * std::cos(phi);
×
NEW
2559
    double y = rad * std::sin(phi);
×
NEW
2560
    q = q_dual_[0] * x + q_dual_[1] * y;
×
NEW
2561
    r = r_dual_[0] * x + r_dual_[1] * y;
×
NEW
2562
    s = -q - r;
×
NEW
2563
  } while (std::max({std::abs(q), std::abs(r), std::abs(s)}) > 1);
×
2564

NEW
2565
  return origin_ + size_ * ((q + q0) * q_ + (r + r0) * r_) + z;
×
2566
}
2567

NEW
2568
StructuredMesh::MeshDistance HexagonalMesh::find_z_crossing(
×
2569
  const Position& r, const Direction& u, double l, int shell) const
2570
{
NEW
2571
  MeshDistance d;
×
2572

2573
  // Direction of flight is within xy-plane. Will never intersect z.
NEW
2574
  if (std::abs(u.z) < FP_PRECISION)
×
2575
    return d;
2576

NEW
2577
  d.max_surface = (u.z > 0.0);
×
NEW
2578
  if (d.max_surface && (shell < grid_.size())) {
×
NEW
2579
    ++d.offset[2];
×
NEW
2580
    d.distance = (grid_[shell] - r.z) / u.z;
×
NEW
2581
  } else if (!d.max_surface && (shell > 0)) {
×
NEW
2582
    --d.offset[2];
×
NEW
2583
    d.distance = (grid_[shell - 1] - r.z) / u.z;
×
2584
  }
2585
  return d;
2586
}
2587

NEW
2588
StructuredMesh::MeshDistance HexagonalMesh::distance_to_grid_boundary(
×
2589
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2590
  double l) const
2591
{
NEW
2592
  auto r = local_coords(r0);
×
NEW
2593
  if (i == 3)
×
NEW
2594
    return find_z_crossing(r, u, l, ijk[2]);
×
NEW
2595
  r.z = 0.0;
×
2596

NEW
2597
  MeshDistance d;
×
NEW
2598
  Direction s_ = -q_ - r_;
×
NEW
2599
  Direction dir;
×
NEW
2600
  MeshIndex idx = {ijk[0], ijk[1], -ijk[0] - ijk[1]};
×
NEW
2601
  MeshIndex offset = {0, 0, 0};
×
NEW
2602
  double distance;
×
NEW
2603
  if (i == 0) {
×
NEW
2604
    dir = 0.5 * (q_ - r_);
×
NEW
2605
    ++offset[0];
×
NEW
2606
    --offset[1];
×
NEW
2607
  } else if (i == 1) {
×
NEW
2608
    dir = 0.5 * (r_ - s_);
×
NEW
2609
    ++offset[1];
×
NEW
2610
    --offset[2];
×
2611
  } else {
NEW
2612
    dir = 0.5 * (s_ - q_);
×
NEW
2613
    --offset[0];
×
NEW
2614
    ++offset[2];
×
2615
  }
NEW
2616
  dir /= dir.norm();
×
NEW
2617
  if (std::abs(u.dot(dir)) < FP_PRECISION)
×
NEW
2618
    return d;
×
2619

NEW
2620
  d.max_surface = (u.dot(dir) > 0.0);
×
NEW
2621
  if (d.max_surface) {
×
NEW
2622
    distance = (idx[0] * q_ + idx[1] * r_ + dir - r).norm() / u.dot(dir);
×
NEW
2623
    idx[0] += offset[0];
×
NEW
2624
    idx[1] += offset[1];
×
NEW
2625
    idx[2] += offset[2];
×
2626
  } else {
NEW
2627
    distance = -(idx[0] * q_ + idx[1] * r_ - dir - r).norm() / u.dot(dir);
×
NEW
2628
    idx[0] -= offset[0];
×
NEW
2629
    idx[1] -= offset[1];
×
NEW
2630
    idx[2] -= offset[2];
×
2631
  }
NEW
2632
  int radius = std::max({std::abs(idx[0]), std::abs(idx[1]), std::abs(idx[2])});
×
NEW
2633
  if (d.max_surface && radius <= radius_) {
×
2634
    d.offset[0] += offset[0];
2635
    d.offset[1] += offset[1];
2636
    d.distance = distance;
NEW
2637
  } else if (!d.max_surface && radius <= radius_) {
×
NEW
2638
    d.offset[0] -= offset[0];
×
NEW
2639
    d.offset[1] -= offset[1];
×
NEW
2640
    d.distance = distance;
×
2641
  }
NEW
2642
  return d;
×
2643
}
2644

NEW
2645
double HexagonalMesh::distance_to_mesh(const MeshIndex& ijk,
×
2646
  const std::array<MeshDistance, 4>& distances, double traveled_distance,
2647
  int& k_max) const
2648
{
2649
  // For all directions outside the mesh, find the distance that we need
2650
  // to travel to reach the next surface. Use the largest distance, as
2651
  // only this will cross all outer surfaces.
NEW
2652
  const int n = n_neighbors();
×
NEW
2653
  k_max = -1;
×
2654

NEW
2655
  for (int k = 0; k < n; ++k) {
×
NEW
2656
    if (distances[k].distance <= traveled_distance)
×
NEW
2657
      continue;
×
NEW
2658
    if ((k < 2) && (std::abs(ijk[k]) <= radius_))
×
NEW
2659
      continue;
×
NEW
2660
    if ((k == 2) && std::abs(ijk[0] + ijk[1]) <= radius_)
×
NEW
2661
      continue;
×
NEW
2662
    if ((k == 3) && ijk[2] >= 1 && ijk[2] < grid_.size())
×
NEW
2663
      continue;
×
NEW
2664
    traveled_distance = distances[k].distance;
×
NEW
2665
    k_max = k;
×
2666
  }
2667
  // Assure some distance is traveled
NEW
2668
  if (k_max == -1) {
×
NEW
2669
    traveled_distance += TINY_BIT;
×
2670
  }
NEW
2671
  return traveled_distance;
×
2672
}
2673

2674
int HexagonalMesh::set_grid()
22✔
2675
{
2676

2677
  if (orientation_ == Orientation::x) {
22✔
2678
    q_ = {std::sqrt(3.0), 0.0, 0.0};
11✔
2679
    r_ = {0.5 * std::sqrt(3), 1.5, 0.0};
11✔
2680
    q_dual_ = {std::sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0};
11✔
2681
    r_dual_ = {0.0, 2.0 / 3.0, 0.0};
11✔
2682
  } else {
2683
    q_ = {0.0, std::sqrt(3.0), 0.0};
11✔
2684
    r_ = {1.5, 0.5 * std::sqrt(3.0), 0.0};
11✔
2685
    q_dual_ = {-1.0 / 3.0, std::sqrt(3.0) / 3.0, 0.0};
11✔
2686
    r_dual_ = {2.0 / 3.0, 0.0, 0.0};
11✔
2687
  }
2688

2689
  q_ *= size_;
22✔
2690
  r_ *= size_;
22✔
2691
  q_dual_ /= size_;
22✔
2692
  r_dual_ /= size_;
22✔
2693

2694
  if (grid_.size() < 2) {
22!
NEW
2695
    set_errmsg("z- grid for hexagonal meshes "
×
2696
               "must each have at least 2 points");
NEW
2697
    return OPENMC_E_INVALID_ARGUMENT;
×
2698
  }
2699
  if (std::adjacent_find(grid_.begin(), grid_.end(), std::greater_equal<>()) !=
22✔
2700
      grid_.end()) {
22!
NEW
2701
    set_errmsg("Values in z- grid for "
×
2702
               "hexagonal meshes must be sorted and unique.");
NEW
2703
    return OPENMC_E_INVALID_ARGUMENT;
×
2704
  }
2705
  return 0;
2706
}
2707

2708
int HexagonalMesh::get_index_in_z_direction(double z) const
1,049,961✔
2709
{
2710
  return lower_bound_index(grid_.begin(), grid_.end(), z) + 1;
1,049,961✔
2711
}
2712

NEW
2713
std::pair<vector<double>, vector<double>> HexagonalMesh::plot(
×
2714
  Position plot_ll, Position plot_ur) const
2715
{
NEW
2716
  fatal_error("Plot of hexagonal Mesh not implemented");
×
2717

2718
  // Figure out which axes lie in the plane of the plot.
2719
  array<vector<double>, 2> axis_lines;
2720
  return {axis_lines[0], axis_lines[1]};
2721
}
2722

2723
void HexagonalMesh::to_hdf5_inner(hid_t mesh_group) const
22✔
2724
{
2725
  write_dataset(mesh_group, "grid", grid_);
22✔
2726
  if (orientation_ == Orientation::x)
22✔
2727
    write_attribute(mesh_group, "orientation", "x");
11✔
2728
  write_attribute(mesh_group, "radius", radius_);
22✔
2729
  write_attribute(mesh_group, "size", size_);
22✔
2730
  write_dataset(mesh_group, "origin", origin_);
22✔
2731
}
22✔
2732

NEW
2733
double HexagonalMesh::volume(const MeshIndex& ijk) const
×
2734
{
NEW
2735
  double f = 1.5 * std::sqrt(3.0);
×
NEW
2736
  double z_i = grid_[ijk[2] - 1];
×
NEW
2737
  double z_o = grid_[ijk[2]];
×
NEW
2738
  return f * (z_o - z_i) * size_ * size_;
×
2739
}
2740

2741
//==============================================================================
2742
// Helper functions for the C API
2743
//==============================================================================
2744

2745
int check_mesh(int32_t index)
6,380✔
2746
{
2747
  if (index < 0 || index >= model::meshes.size()) {
6,380!
2748
    set_errmsg("Index in meshes array is out of bounds.");
×
2749
    return OPENMC_E_OUT_OF_BOUNDS;
×
2750
  }
2751
  return 0;
2752
}
2753

2754
template<class T>
2755
int check_mesh_type(int32_t index)
1,100✔
2756
{
2757
  if (int err = check_mesh(index))
1,100!
2758
    return err;
2759

2760
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,100!
2761
  if (!mesh) {
1,100!
2762
    set_errmsg("This function is not valid for input mesh.");
×
2763
    return OPENMC_E_INVALID_TYPE;
×
2764
  }
2765
  return 0;
2766
}
2767

2768
template<class T>
2769
bool is_mesh_type(int32_t index)
2770
{
2771
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2772
  return mesh;
2773
}
2774

2775
//==============================================================================
2776
// C API functions
2777
//==============================================================================
2778

2779
// Return the type of mesh as a C string
2780
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,474✔
2781
{
2782
  if (int err = check_mesh(index))
1,474!
2783
    return err;
2784

2785
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
1,474✔
2786

2787
  return 0;
1,474✔
2788
}
2789

2790
//! Extend the meshes array by n elements
2791
extern "C" int openmc_extend_meshes(
253✔
2792
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2793
{
2794
  if (index_start)
253!
2795
    *index_start = model::meshes.size();
253✔
2796
  std::string mesh_type;
253✔
2797

2798
  for (int i = 0; i < n; ++i) {
506✔
2799
    if (RegularMesh::mesh_type == type) {
253✔
2800
      model::meshes.push_back(make_unique<RegularMesh>());
165✔
2801
    } else if (RectilinearMesh::mesh_type == type) {
88✔
2802
      model::meshes.push_back(make_unique<RectilinearMesh>());
44✔
2803
    } else if (CylindricalMesh::mesh_type == type) {
44✔
2804
      model::meshes.push_back(make_unique<CylindricalMesh>());
22✔
2805
    } else if (SphericalMesh::mesh_type == type) {
22!
2806
      model::meshes.push_back(make_unique<SphericalMesh>());
22✔
NEW
2807
    } else if (HexagonalMesh::mesh_type == type) {
×
NEW
2808
      model::meshes.push_back(make_unique<HexagonalMesh>());
×
2809
    } else {
2810
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2811
    }
2812
  }
2813
  if (index_end)
253!
2814
    *index_end = model::meshes.size() - 1;
×
2815

2816
  return 0;
253✔
2817
}
253✔
2818

2819
//! Adds a new unstructured mesh to OpenMC
2820
extern "C" int openmc_add_unstructured_mesh(
×
2821
  const char filename[], const char library[], int* id)
2822
{
2823
  std::string lib_name(library);
×
2824
  std::string mesh_file(filename);
×
2825
  bool valid_lib = false;
×
2826

2827
#ifdef OPENMC_DAGMC_ENABLED
2828
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2829
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2830
    valid_lib = true;
2831
  }
2832
#endif
2833

2834
#ifdef OPENMC_LIBMESH_ENABLED
2835
  if (lib_name == LibMesh::mesh_lib_type) {
×
2836
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2837
    valid_lib = true;
2838
  }
2839
#endif
2840

2841
  if (!valid_lib) {
×
2842
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2843
                           "by this build of OpenMC",
2844
      lib_name));
2845
    return OPENMC_E_INVALID_ARGUMENT;
×
2846
  }
2847

2848
  // auto-assign new ID
2849
  model::meshes.back()->set_id(-1);
×
2850
  *id = model::meshes.back()->id_;
2851

2852
  return 0;
2853
}
×
2854

2855
//! Return the index in the meshes array of a mesh with a given ID
2856
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
429✔
2857
{
2858
  auto pair = model::mesh_map.find(id);
429!
2859
  if (pair == model::mesh_map.end()) {
429!
2860
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
2861
    return OPENMC_E_INVALID_ID;
×
2862
  }
2863
  *index = pair->second;
429✔
2864
  return 0;
429✔
2865
}
2866

2867
//! Return the ID of a mesh
2868
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,805✔
2869
{
2870
  if (int err = check_mesh(index))
2,805!
2871
    return err;
2872
  *id = model::meshes[index]->id_;
2,805✔
2873
  return 0;
2,805✔
2874
}
2875

2876
//! Set the ID of a mesh
2877
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
253✔
2878
{
2879
  if (int err = check_mesh(index))
253!
2880
    return err;
2881
  model::meshes[index]->id_ = id;
253✔
2882
  model::mesh_map[id] = index;
253✔
2883
  return 0;
253✔
2884
}
2885

2886
//! Get the number of elements in a mesh
2887
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
275✔
2888
{
2889
  if (int err = check_mesh(index))
275!
2890
    return err;
2891
  *n = model::meshes[index]->n_bins();
275✔
2892
  return 0;
275✔
2893
}
2894

2895
//! Get the volume of each element in the mesh
2896
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
88✔
2897
{
2898
  if (int err = check_mesh(index))
88!
2899
    return err;
2900
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
968✔
2901
    volumes[i] = model::meshes[index]->volume(i);
880✔
2902
  }
2903
  return 0;
2904
}
2905

2906
//! Get the bounding box of a mesh
2907
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
154✔
2908
{
2909
  if (int err = check_mesh(index))
154!
2910
    return err;
2911

2912
  BoundingBox bbox = model::meshes[index]->bounding_box();
154✔
2913

2914
  // set lower left corner values
2915
  ll[0] = bbox.min.x;
154✔
2916
  ll[1] = bbox.min.y;
154✔
2917
  ll[2] = bbox.min.z;
154✔
2918

2919
  // set upper right corner values
2920
  ur[0] = bbox.max.x;
154✔
2921
  ur[1] = bbox.max.y;
154✔
2922
  ur[2] = bbox.max.z;
154✔
2923
  return 0;
154✔
2924
}
2925

2926
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
187✔
2927
  int nz, int table_size, int32_t* materials, double* volumes, double* bboxes)
2928
{
2929
  if (int err = check_mesh(index))
187!
2930
    return err;
2931

2932
  try {
187✔
2933
    model::meshes[index]->material_volumes(
187✔
2934
      nx, ny, nz, table_size, materials, volumes, bboxes);
2935
  } catch (const std::exception& e) {
11!
2936
    set_errmsg(e.what());
11✔
2937
    if (starts_with(e.what(), "Mesh")) {
11!
2938
      return OPENMC_E_GEOMETRY;
11✔
2939
    } else {
2940
      return OPENMC_E_ALLOCATE;
×
2941
    }
2942
  }
11✔
2943

2944
  return 0;
2945
}
2946

2947
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
44✔
2948
  Position width, int basis, int* pixels, int32_t* data)
2949
{
2950
  if (int err = check_mesh(index))
44!
2951
    return err;
2952
  const auto& mesh = model::meshes[index].get();
44!
2953

2954
  int pixel_width = pixels[0];
44✔
2955
  int pixel_height = pixels[1];
44✔
2956

2957
  // get pixel size
2958
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
44✔
2959
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
44✔
2960

2961
  // setup basis indices and initial position centered on pixel
2962
  int in_i, out_i;
44✔
2963
  Position xyz = origin;
44✔
2964
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
44✔
2965
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
44✔
2966
  switch (basis_enum) {
44!
2967
  case PlotBasis::xy:
2968
    in_i = 0;
2969
    out_i = 1;
2970
    break;
2971
  case PlotBasis::xz:
2972
    in_i = 0;
2973
    out_i = 2;
2974
    break;
2975
  case PlotBasis::yz:
2976
    in_i = 1;
2977
    out_i = 2;
2978
    break;
2979
  default:
×
2980
    UNREACHABLE();
×
2981
  }
2982

2983
  // set initial position
2984
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
44✔
2985
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
44✔
2986

2987
#pragma omp parallel
24✔
2988
  {
20✔
2989
    Position r = xyz;
20✔
2990

2991
#pragma omp for
2992
    for (int y = 0; y < pixel_height; y++) {
420✔
2993
      r[out_i] = xyz[out_i] - out_pixel * y;
400✔
2994
      for (int x = 0; x < pixel_width; x++) {
8,400✔
2995
        r[in_i] = xyz[in_i] + in_pixel * x;
8,000✔
2996
        data[pixel_width * y + x] = mesh->get_bin(r);
8,000✔
2997
      }
2998
    }
2999
  }
3000

3001
  return 0;
44✔
3002
}
3003

3004
//! Get the dimension of a regular mesh
3005
extern "C" int openmc_regular_mesh_get_dimension(
11✔
3006
  int32_t index, int** dims, int* n)
3007
{
3008
  if (int err = check_mesh_type<RegularMesh>(index))
11!
3009
    return err;
3010
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
11!
3011
  *dims = mesh->shape_.data();
11✔
3012
  *n = mesh->n_dimension_;
11✔
3013
  return 0;
11✔
3014
}
3015

3016
//! Set the dimension of a regular mesh
3017
extern "C" int openmc_regular_mesh_set_dimension(
187✔
3018
  int32_t index, int n, const int* dims)
3019
{
3020
  if (int err = check_mesh_type<RegularMesh>(index))
187!
3021
    return err;
3022
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
187!
3023

3024
  // Copy dimension
3025
  mesh->n_dimension_ = n;
187✔
3026
  std::copy(dims, dims + n, mesh->shape_.begin());
187✔
3027
  return 0;
187✔
3028
}
3029

3030
//! Get the regular mesh parameters
3031
extern "C" int openmc_regular_mesh_get_params(
209✔
3032
  int32_t index, double** ll, double** ur, double** width, int* n)
3033
{
3034
  if (int err = check_mesh_type<RegularMesh>(index))
209!
3035
    return err;
3036
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
209!
3037

3038
  if (m->lower_left_.empty()) {
209!
3039
    set_errmsg("Mesh parameters have not been set.");
×
3040
    return OPENMC_E_ALLOCATE;
×
3041
  }
3042

3043
  *ll = m->lower_left_.data();
209✔
3044
  *ur = m->upper_right_.data();
209✔
3045
  *width = m->width_.data();
209✔
3046
  *n = m->n_dimension_;
209✔
3047
  return 0;
209✔
3048
}
3049

3050
//! Set the regular mesh parameters
3051
extern "C" int openmc_regular_mesh_set_params(
220✔
3052
  int32_t index, int n, const double* ll, const double* ur, const double* width)
3053
{
3054
  if (int err = check_mesh_type<RegularMesh>(index))
220!
3055
    return err;
3056
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
220!
3057

3058
  if (m->n_dimension_ == -1) {
220!
3059
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
3060
    return OPENMC_E_UNASSIGNED;
×
3061
  }
3062

3063
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
220✔
3064
  if (ll && ur) {
220✔
3065
    m->lower_left_ = tensor::Tensor<double>(ll, n);
198✔
3066
    m->upper_right_ = tensor::Tensor<double>(ur, n);
198✔
3067
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_shape_tensor();
792✔
3068
  } else if (ll && width) {
22✔
3069
    m->lower_left_ = tensor::Tensor<double>(ll, n);
11✔
3070
    m->width_ = tensor::Tensor<double>(width, n);
11✔
3071
    m->upper_right_ = m->lower_left_ + m->get_shape_tensor() * m->width_;
44✔
3072
  } else if (ur && width) {
11!
3073
    m->upper_right_ = tensor::Tensor<double>(ur, n);
11✔
3074
    m->width_ = tensor::Tensor<double>(width, n);
11✔
3075
    m->lower_left_ = m->upper_right_ - m->get_shape_tensor() * m->width_;
44✔
3076
  } else {
3077
    set_errmsg("At least two parameters must be specified.");
×
3078
    return OPENMC_E_INVALID_ARGUMENT;
×
3079
  }
3080

3081
  // Set material volumes
3082

3083
  // TODO: incorporate this into method in RegularMesh that can be called from
3084
  // here and from constructor
3085
  m->volume_frac_ = 1.0 / m->get_shape_tensor().prod();
220✔
3086
  m->element_volume_ = 1.0;
220✔
3087
  for (int i = 0; i < m->n_dimension_; i++) {
880✔
3088
    m->element_volume_ *= m->width_[i];
660✔
3089
  }
3090

3091
  return 0;
3092
}
220✔
3093

3094
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
3095
template<class C>
3096
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
88✔
3097
  const int nx, const double* grid_y, const int ny, const double* grid_z,
3098
  const int nz)
3099
{
3100
  if (int err = check_mesh_type<C>(index))
88!
3101
    return err;
3102

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

3105
  m->n_dimension_ = 3;
88✔
3106

3107
  m->grid_[0].reserve(nx);
88✔
3108
  m->grid_[1].reserve(ny);
88✔
3109
  m->grid_[2].reserve(nz);
88✔
3110

3111
  for (int i = 0; i < nx; i++) {
572✔
3112
    m->grid_[0].push_back(grid_x[i]);
484✔
3113
  }
3114
  for (int i = 0; i < ny; i++) {
341✔
3115
    m->grid_[1].push_back(grid_y[i]);
253✔
3116
  }
3117
  for (int i = 0; i < nz; i++) {
319✔
3118
    m->grid_[2].push_back(grid_z[i]);
231✔
3119
  }
3120

3121
  int err = m->set_grid();
88✔
3122
  return err;
88✔
3123
}
3124

3125
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
3126
template<class C>
3127
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
385✔
3128
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
3129
{
3130
  if (int err = check_mesh_type<C>(index))
385!
3131
    return err;
3132
  C* m = dynamic_cast<C*>(model::meshes[index].get());
385!
3133

3134
  if (m->lower_left_.empty()) {
385!
3135
    set_errmsg("Mesh parameters have not been set.");
×
3136
    return OPENMC_E_ALLOCATE;
×
3137
  }
3138

3139
  *grid_x = m->grid_[0].data();
385✔
3140
  *nx = m->grid_[0].size();
385✔
3141
  *grid_y = m->grid_[1].data();
385✔
3142
  *ny = m->grid_[1].size();
385✔
3143
  *grid_z = m->grid_[2].data();
385✔
3144
  *nz = m->grid_[2].size();
385✔
3145

3146
  return 0;
385✔
3147
}
3148

3149
//! Get the rectilinear mesh grid
3150
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
143✔
3151
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
3152
{
3153
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
143✔
3154
    index, grid_x, nx, grid_y, ny, grid_z, nz);
143✔
3155
}
3156

3157
//! Set the rectilienar mesh parameters
3158
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
44✔
3159
  const double* grid_x, const int nx, const double* grid_y, const int ny,
3160
  const double* grid_z, const int nz)
3161
{
3162
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
44✔
3163
    index, grid_x, nx, grid_y, ny, grid_z, nz);
44✔
3164
}
3165

3166
//! Get the cylindrical mesh grid
3167
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
121✔
3168
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
3169
{
3170
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
121✔
3171
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
3172
}
3173

3174
//! Set the cylindrical mesh parameters
3175
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
22✔
3176
  const double* grid_x, const int nx, const double* grid_y, const int ny,
3177
  const double* grid_z, const int nz)
3178
{
3179
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
22✔
3180
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
3181
}
3182

3183
//! Get the spherical mesh grid
3184
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
121✔
3185
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
3186
{
3187

3188
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
121✔
3189
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
3190
  ;
121✔
3191
}
3192

3193
//! Set the spherical mesh parameters
3194
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
22✔
3195
  const double* grid_x, const int nx, const double* grid_y, const int ny,
3196
  const double* grid_z, const int nz)
3197
{
3198
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
22✔
3199
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
3200
}
3201

3202
#ifdef OPENMC_DAGMC_ENABLED
3203

3204
const std::string MOABMesh::mesh_lib_type = "moab";
3205

3206
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
3207
{
3208
  initialize();
24✔
3209
}
24!
3210

3211
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
3212
{
3213
  initialize();
×
3214
}
×
3215

3216
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
3217
  : UnstructuredMesh()
×
3218
{
3219
  n_dimension_ = 3;
3220
  filename_ = filename;
×
3221
  set_length_multiplier(length_multiplier);
×
3222
  initialize();
×
3223
}
×
3224

3225
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
3226
{
3227
  mbi_ = external_mbi;
1✔
3228
  filename_ = "unknown (external file)";
1✔
3229
  this->initialize();
1✔
3230
}
1!
3231

3232
void MOABMesh::initialize()
25✔
3233
{
3234

3235
  // Create the MOAB interface and load data from file
3236
  this->create_interface();
25✔
3237

3238
  // Initialise MOAB error code
3239
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
3240

3241
  // Set the dimension
3242
  n_dimension_ = 3;
25✔
3243

3244
  // set member range of tetrahedral entities
3245
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
3246
  if (rval != moab::MB_SUCCESS) {
25!
3247
    fatal_error("Failed to get all tetrahedral elements");
3248
  }
3249

3250
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
3251
    warning("Non-tetrahedral elements found in unstructured "
×
3252
            "mesh file: " +
3253
            filename_);
3254
  }
3255

3256
  // set member range of vertices
3257
  int vertex_dim = 0;
25✔
3258
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
3259
  if (rval != moab::MB_SUCCESS) {
25!
3260
    fatal_error("Failed to get all vertex handles");
3261
  }
3262

3263
  // make an entity set for all tetrahedra
3264
  // this is used for convenience later in output
3265
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
3266
  if (rval != moab::MB_SUCCESS) {
25!
3267
    fatal_error("Failed to create an entity set for the tetrahedral elements");
3268
  }
3269

3270
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
3271
  if (rval != moab::MB_SUCCESS) {
25!
3272
    fatal_error("Failed to add tetrahedra to an entity set.");
3273
  }
3274

3275
  if (length_multiplier_ > 0.0) {
25!
3276
    // get the connectivity of all tets
3277
    moab::Range adj;
×
3278
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
3279
    if (rval != moab::MB_SUCCESS) {
×
3280
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
3281
    }
3282
    // scale all vertex coords by multiplier (done individually so not all
3283
    // coordinates are in memory twice at once)
3284
    for (auto vert : adj) {
×
3285
      // retrieve coords
3286
      std::array<double, 3> coord;
3287
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
3288
      if (rval != moab::MB_SUCCESS) {
×
3289
        fatal_error("Could not get coordinates of vertex.");
3290
      }
3291
      // scale coords
3292
      for (auto& c : coord) {
×
3293
        c *= length_multiplier_;
3294
      }
3295
      // set new coords
3296
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
3297
      if (rval != moab::MB_SUCCESS) {
×
3298
        fatal_error("Failed to set new vertex coordinates");
3299
      }
3300
    }
3301
  }
3302

3303
  // Determine bounds of mesh
3304
  this->determine_bounds();
25✔
3305
}
25✔
3306

3307
void MOABMesh::prepare_for_point_location()
21✔
3308
{
3309
  // if the KDTree has already been constructed, do nothing
3310
  if (kdtree_)
21!
3311
    return;
3312

3313
  // build acceleration data structures
3314
  compute_barycentric_data(ehs_);
21✔
3315
  build_kdtree(ehs_);
21✔
3316
}
3317

3318
void MOABMesh::create_interface()
25✔
3319
{
3320
  // Do not create a MOAB instance if one is already in memory
3321
  if (mbi_)
25✔
3322
    return;
3323

3324
  // create MOAB instance
3325
  mbi_ = std::make_shared<moab::Core>();
24!
3326

3327
  // load unstructured mesh file
3328
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
3329
  if (rval != moab::MB_SUCCESS) {
24!
3330
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
3331
  }
3332
}
3333

3334
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
3335
{
3336
  moab::Range all_tris;
21✔
3337
  int adj_dim = 2;
21✔
3338
  write_message("Getting tet adjacencies...", 7);
21✔
3339
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
3340
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
3341
  if (rval != moab::MB_SUCCESS) {
21!
3342
    fatal_error("Failed to get adjacent triangles for tets");
3343
  }
3344

3345
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
3346
    warning("Non-triangle elements found in tet adjacencies in "
×
3347
            "unstructured mesh file: " +
3348
            filename_);
×
3349
  }
3350

3351
  // combine into one range
3352
  moab::Range all_tets_and_tris;
21✔
3353
  all_tets_and_tris.merge(all_tets);
21✔
3354
  all_tets_and_tris.merge(all_tris);
21✔
3355

3356
  // create a kd-tree instance
3357
  write_message(
21✔
3358
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
3359
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
3360

3361
  // Determine what options to use
3362
  std::ostringstream options_stream;
21✔
3363
  if (options_.empty()) {
21✔
3364
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
3365
  } else {
3366
    options_stream << options_;
16✔
3367
  }
3368
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
3369

3370
  // Build the k-d tree
3371
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
3372
  if (rval != moab::MB_SUCCESS) {
21!
3373
    fatal_error("Failed to construct KDTree for the "
3374
                "unstructured mesh file: " +
3375
                filename_);
×
3376
  }
3377
}
21✔
3378

3379
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
3380
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
3381
{
3382
  hits.clear();
1,543,584!
3383

3384
  moab::ErrorCode rval;
1,543,584✔
3385
  vector<moab::EntityHandle> tris;
1,543,584✔
3386
  // get all intersections with triangles in the tet mesh
3387
  // (distances are relative to the start point, not the previous
3388
  // intersection)
3389
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
3390
    dir.array(), start.array(), tris, hits, 0, track_len);
3391
  if (rval != moab::MB_SUCCESS) {
1,543,584!
3392
    fatal_error(
3393
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
3394
  }
3395

3396
  // remove duplicate intersection distances
3397
  std::unique(hits.begin(), hits.end());
1,543,584✔
3398

3399
  // sorts by first component of std::pair by default
3400
  std::sort(hits.begin(), hits.end());
1,543,584✔
3401
}
1,543,584✔
3402

3403
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
3404
  vector<int>& bins, vector<double>& lengths) const
3405
{
3406
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
3407
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
3408
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
3409
  dir.normalize();
1,543,584✔
3410

3411
  double track_len = (end - start).length();
1,543,584✔
3412
  if (track_len == 0.0)
1,543,584!
3413
    return;
721,692✔
3414

3415
  start -= TINY_BIT * dir;
1,543,584✔
3416
  end += TINY_BIT * dir;
1,543,584✔
3417

3418
  vector<double> hits;
1,543,584✔
3419
  intersect_track(start, dir, track_len, hits);
1,543,584✔
3420

3421
  bins.clear();
1,543,584!
3422
  lengths.clear();
1,543,584!
3423

3424
  // if there are no intersections the track may lie entirely
3425
  // within a single tet. If this is the case, apply entire
3426
  // score to that tet and return.
3427
  if (hits.size() == 0) {
1,543,584✔
3428
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
3429
    int bin = this->get_bin(midpoint);
721,692✔
3430
    if (bin != -1) {
721,692✔
3431
      bins.push_back(bin);
242,866✔
3432
      lengths.push_back(1.0);
242,866✔
3433
    }
3434
    return;
721,692✔
3435
  }
3436

3437
  // for each segment in the set of tracks, try to look up a tet
3438
  // at the midpoint of the segment
3439
  Position current = r0;
3440
  double last_dist = 0.0;
3441
  for (const auto& hit : hits) {
5,516,161✔
3442
    // get the segment length
3443
    double segment_length = hit - last_dist;
4,694,269✔
3444
    last_dist = hit;
4,694,269✔
3445
    // find the midpoint of this segment
3446
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
3447
    // try to find a tet for this position
3448
    int bin = this->get_bin(midpoint);
4,694,269✔
3449

3450
    // determine the start point for this segment
3451
    current = r0 + u * hit;
4,694,269✔
3452

3453
    if (bin == -1) {
4,694,269✔
3454
      continue;
20,522✔
3455
    }
3456

3457
    bins.push_back(bin);
4,673,747✔
3458
    lengths.push_back(segment_length / track_len);
4,673,747✔
3459
  }
3460

3461
  // tally remaining portion of track after last hit if
3462
  // the last segment of the track is in the mesh but doesn't
3463
  // reach the other side of the tet
3464
  if (hits.back() < track_len) {
821,892!
3465
    Position segment_start = r0 + u * hits.back();
821,892✔
3466
    double segment_length = track_len - hits.back();
821,892✔
3467
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
3468
    int bin = this->get_bin(midpoint);
821,892✔
3469
    if (bin != -1) {
821,892✔
3470
      bins.push_back(bin);
766,509✔
3471
      lengths.push_back(segment_length / track_len);
766,509✔
3472
    }
3473
  }
3474
};
1,543,584✔
3475

3476
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,232✔
3477
{
3478
  moab::CartVect pos(r.x, r.y, r.z);
7,317,232✔
3479
  // find the leaf of the kd-tree for this position
3480
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,232✔
3481
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,232✔
3482
  if (rval != moab::MB_SUCCESS) {
7,317,232✔
3483
    return 0;
3484
  }
3485

3486
  // retrieve the tet elements of this leaf
3487
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,335✔
3488
  moab::Range tets;
6,305,335✔
3489
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,335✔
3490
  if (rval != moab::MB_SUCCESS) {
6,305,335!
3491
    warning("MOAB error finding tets.");
×
3492
  }
3493

3494
  // loop over the tets in this leaf, returning the containing tet if found
3495
  for (const auto& tet : tets) {
260,211,273✔
3496
    if (point_in_tet(pos, tet)) {
260,208,426✔
3497
      return tet;
6,302,488✔
3498
    }
3499
  }
3500

3501
  // if no tet is found, return an invalid handle
3502
  return 0;
2,847✔
3503
}
14,634,464✔
3504

3505
double MOABMesh::volume(int bin) const
167,880✔
3506
{
3507
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3508
}
3509

3510
std::string MOABMesh::library() const
34✔
3511
{
3512
  return mesh_lib_type;
34✔
3513
}
3514

3515
// Sample position within a tet for MOAB type tets
3516
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3517
{
3518

3519
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3520

3521
  // Get vertex coordinates for MOAB tet
3522
  const moab::EntityHandle* conn1;
200,410✔
3523
  int conn1_size;
200,410✔
3524
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3525
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3526
    fatal_error(fmt::format(
3527
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3528
      conn1_size));
3529
  }
3530
  moab::CartVect p[4];
200,410✔
3531
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3532
  if (rval != moab::MB_SUCCESS) {
200,410!
3533
    fatal_error("Failed to get tet coords");
3534
  }
3535

3536
  std::array<Position, 4> tet_verts;
200,410✔
3537
  for (int i = 0; i < 4; i++) {
1,002,050✔
3538
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3539
  }
3540
  // Samples position within tet using Barycentric stuff
3541
  return this->sample_tet(tet_verts, seed);
200,410✔
3542
}
3543

3544
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3545
{
3546
  vector<moab::EntityHandle> conn;
167,880✔
3547
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3548
  if (rval != moab::MB_SUCCESS) {
167,880!
3549
    fatal_error("Failed to get tet connectivity");
3550
  }
3551

3552
  moab::CartVect p[4];
167,880✔
3553
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3554
  if (rval != moab::MB_SUCCESS) {
167,880!
3555
    fatal_error("Failed to get tet coords");
3556
  }
3557

3558
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
167,880✔
3559
}
167,880✔
3560

3561
int MOABMesh::get_bin(Position r) const
7,317,232✔
3562
{
3563
  moab::EntityHandle tet = get_tet(r);
7,317,232✔
3564
  if (tet == 0) {
7,317,232✔
3565
    return -1;
3566
  } else {
3567
    return get_bin_from_ent_handle(tet);
6,302,488✔
3568
  }
3569
}
3570

3571
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3572
{
3573
  moab::ErrorCode rval;
21✔
3574

3575
  baryc_data_.clear();
21!
3576
  baryc_data_.resize(tets.size());
21✔
3577

3578
  // compute the barycentric data for each tet element
3579
  // and store it as a 3x3 matrix
3580
  for (auto& tet : tets) {
239,757✔
3581
    vector<moab::EntityHandle> verts;
239,736✔
3582
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3583
    if (rval != moab::MB_SUCCESS) {
239,736!
3584
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3585
    }
3586

3587
    moab::CartVect p[4];
239,736✔
3588
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
239,736✔
3589
    if (rval != moab::MB_SUCCESS) {
239,736!
3590
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
×
3591
    }
3592

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

3595
    // invert now to avoid this cost later
3596
    a = a.transpose().inverse();
239,736✔
3597
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3598
  }
239,736✔
3599
}
21✔
3600

3601
bool MOABMesh::point_in_tet(
260,208,426✔
3602
  const moab::CartVect& r, moab::EntityHandle tet) const
3603
{
3604

3605
  moab::ErrorCode rval;
260,208,426✔
3606

3607
  // get tet vertices
3608
  vector<moab::EntityHandle> verts;
260,208,426✔
3609
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,208,426✔
3610
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3611
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3612
    return false;
3613
  }
3614

3615
  // first vertex is used as a reference point for the barycentric data -
3616
  // retrieve its coordinates
3617
  moab::CartVect p_zero;
260,208,426✔
3618
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,208,426✔
3619
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3620
    warning("Failed to get coordinates of a vertex in "
×
3621
            "unstructured mesh: " +
3622
            filename_);
×
3623
    return false;
3624
  }
3625

3626
  // look up barycentric data
3627
  int idx = get_bin_from_ent_handle(tet);
260,208,426✔
3628
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,208,426✔
3629

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

3632
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
161,208,987✔
3633
          bary_coords[2] >= 0.0 &&
318,957,185✔
3634
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
21,688,225✔
3635
}
260,208,426✔
3636

3637
int MOABMesh::get_bin_from_index(int idx) const
3638
{
3639
  if (idx >= n_bins()) {
×
3640
    fatal_error(fmt::format("Invalid bin index: {}", idx));
3641
  }
3642
  return ehs_[idx] - ehs_[0];
3643
}
3644

3645
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3646
{
3647
  int bin = get_bin(r);
3648
  *in_mesh = bin != -1;
3649
  return bin;
3650
}
3651

3652
int MOABMesh::get_index_from_bin(int bin) const
3653
{
3654
  return bin;
3655
}
3656

3657
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3658
  Position plot_ll, Position plot_ur) const
3659
{
3660
  // TODO: Implement mesh lines
3661
  return {};
3662
}
3663

3664
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3665
{
3666
  int idx = vert - verts_[0];
815,520✔
3667
  if (idx >= n_vertices()) {
815,520!
3668
    fatal_error(
3669
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3670
  }
3671
  return idx;
815,520✔
3672
}
3673

3674
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,750,650✔
3675
{
3676
  int bin = eh - ehs_[0];
266,750,650✔
3677
  if (bin >= n_bins()) {
266,750,650!
3678
    fatal_error(fmt::format("Invalid bin: {}", bin));
3679
  }
3680
  return bin;
266,750,650✔
3681
}
3682

3683
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3684
{
3685
  if (bin >= n_bins()) {
572,170!
3686
    fatal_error(fmt::format("Invalid bin index: ", bin));
3687
  }
3688
  return ehs_[0] + bin;
572,170✔
3689
}
3690

3691
int MOABMesh::n_bins() const
267,526,773✔
3692
{
3693
  return ehs_.size();
267,526,773✔
3694
}
3695

3696
int MOABMesh::n_surface_bins() const
3697
{
3698
  // collect all triangles in the set of tets for this mesh
3699
  moab::Range tris;
×
3700
  moab::ErrorCode rval;
3701
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3702
  if (rval != moab::MB_SUCCESS) {
×
3703
    warning("Failed to get all triangles in the mesh instance");
×
3704
    return -1;
3705
  }
3706
  return 2 * tris.size();
×
3707
}
3708

3709
Position MOABMesh::centroid(int bin) const
3710
{
3711
  moab::ErrorCode rval;
3712

3713
  auto tet = this->get_ent_handle_from_bin(bin);
3714

3715
  // look up the tet connectivity
3716
  vector<moab::EntityHandle> conn;
×
3717
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3718
  if (rval != moab::MB_SUCCESS) {
×
3719
    warning("Failed to get connectivity of a mesh element.");
×
3720
    return {};
3721
  }
3722

3723
  // get the coordinates
3724
  vector<moab::CartVect> coords(conn.size());
×
3725
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3726
  if (rval != moab::MB_SUCCESS) {
×
3727
    warning("Failed to get the coordinates of a mesh element.");
×
3728
    return {};
3729
  }
3730

3731
  // compute the centroid of the element vertices
3732
  moab::CartVect centroid(0.0, 0.0, 0.0);
3733
  for (const auto& coord : coords) {
×
3734
    centroid += coord;
3735
  }
3736
  centroid /= double(coords.size());
3737

3738
  return {centroid[0], centroid[1], centroid[2]};
3739
}
3740

3741
int MOABMesh::n_vertices() const
845,874✔
3742
{
3743
  return verts_.size();
845,874✔
3744
}
3745

3746
Position MOABMesh::vertex(int id) const
86,227✔
3747
{
3748

3749
  moab::ErrorCode rval;
86,227✔
3750

3751
  moab::EntityHandle vert = verts_[id];
86,227✔
3752

3753
  moab::CartVect coords;
86,227✔
3754
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3755
  if (rval != moab::MB_SUCCESS) {
86,227!
3756
    fatal_error("Failed to get the coordinates of a vertex.");
3757
  }
3758

3759
  return {coords[0], coords[1], coords[2]};
86,227✔
3760
}
3761

3762
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3763
{
3764
  moab::ErrorCode rval;
203,880✔
3765

3766
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3767

3768
  // look up the tet connectivity
3769
  vector<moab::EntityHandle> conn;
203,880✔
3770
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3771
  if (rval != moab::MB_SUCCESS) {
203,880!
3772
    fatal_error("Failed to get connectivity of a mesh element.");
3773
    return {};
3774
  }
3775

3776
  std::vector<int> verts(4);
203,880✔
3777
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3778
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3779
  }
3780

3781
  return verts;
203,880✔
3782
}
203,880✔
3783

3784
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3785
  std::string score) const
3786
{
3787
  moab::ErrorCode rval;
3788
  // add a tag to the mesh
3789
  // all scores are treated as a single value
3790
  // with an uncertainty
3791
  moab::Tag value_tag;
3792

3793
  // create the value tag if not present and get handle
3794
  double default_val = 0.0;
3795
  auto val_string = score + "_mean";
3796
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3797
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3798
  if (rval != moab::MB_SUCCESS) {
×
3799
    auto msg =
3800
      fmt::format("Could not create or retrieve the value tag for the score {}"
3801
                  " on unstructured mesh {}",
3802
        score, id_);
×
3803
    fatal_error(msg);
3804
  }
3805

3806
  // create the std dev tag if not present and get handle
3807
  moab::Tag error_tag;
3808
  std::string err_string = score + "_std_dev";
×
3809
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3810
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3811
  if (rval != moab::MB_SUCCESS) {
×
3812
    auto msg =
3813
      fmt::format("Could not create or retrieve the error tag for the score {}"
3814
                  " on unstructured mesh {}",
3815
        score, id_);
×
3816
    fatal_error(msg);
3817
  }
3818

3819
  // return the populated tag handles
3820
  return {value_tag, error_tag};
3821
}
3822

3823
void MOABMesh::add_score(const std::string& score)
3824
{
3825
  auto score_tags = get_score_tags(score);
×
3826
  tag_names_.push_back(score);
3827
}
3828

3829
void MOABMesh::remove_scores()
3830
{
3831
  for (const auto& name : tag_names_) {
×
3832
    auto value_name = name + "_mean";
3833
    moab::Tag tag;
3834
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3835
    if (rval != moab::MB_SUCCESS)
×
3836
      return;
3837

3838
    rval = mbi_->tag_delete(tag);
×
3839
    if (rval != moab::MB_SUCCESS) {
×
3840
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3841
                             " on unstructured mesh {}",
3842
        name, id_);
×
3843
      fatal_error(msg);
3844
    }
3845

3846
    auto std_dev_name = name + "_std_dev";
×
3847
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3848
    if (rval != moab::MB_SUCCESS) {
×
3849
      auto msg =
3850
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3851
                    " on unstructured mesh {}",
3852
          name, id_);
×
3853
    }
3854

3855
    rval = mbi_->tag_delete(tag);
×
3856
    if (rval != moab::MB_SUCCESS) {
×
3857
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3858
                             " on unstructured mesh {}",
3859
        name, id_);
×
3860
      fatal_error(msg);
3861
    }
3862
  }
3863
  tag_names_.clear();
3864
}
3865

3866
void MOABMesh::set_score_data(const std::string& score,
3867
  const vector<double>& values, const vector<double>& std_dev)
3868
{
3869
  auto score_tags = this->get_score_tags(score);
×
3870

3871
  moab::ErrorCode rval;
3872
  // set the score value
3873
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
3874
  if (rval != moab::MB_SUCCESS) {
×
3875
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3876
                           "on unstructured mesh {}",
3877
      score, id_);
3878
    warning(msg);
×
3879
  }
3880

3881
  // set the error value
3882
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3883
  if (rval != moab::MB_SUCCESS) {
×
3884
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3885
                           "on unstructured mesh {}",
3886
      score, id_);
3887
    warning(msg);
×
3888
  }
3889
}
3890

3891
void MOABMesh::write(const std::string& base_filename) const
3892
{
3893
  // add extension to the base name
3894
  auto filename = base_filename + ".vtk";
3895
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3896
  filename = settings::path_output + filename;
×
3897

3898
  // write the tetrahedral elements of the mesh only
3899
  // to avoid clutter from zero-value data on other
3900
  // elements during visualization
3901
  moab::ErrorCode rval;
3902
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3903
  if (rval != moab::MB_SUCCESS) {
×
3904
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3905
    warning(msg);
×
3906
  }
3907
}
3908

3909
#endif
3910

3911
#ifdef OPENMC_LIBMESH_ENABLED
3912

3913
const std::string LibMesh::mesh_lib_type = "libmesh";
3914

3915
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3916
{
3917
  // filename_ and length_multiplier_ will already be set by the
3918
  // UnstructuredMesh constructor
3919
  set_mesh_pointer_from_filename(filename_);
23✔
3920
  set_length_multiplier(length_multiplier_);
23✔
3921
  initialize();
23✔
3922
}
23✔
3923

3924
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3925
{
3926
  // filename_ and length_multiplier_ will already be set by the
3927
  // UnstructuredMesh constructor
3928
  set_mesh_pointer_from_filename(filename_);
×
3929
  set_length_multiplier(length_multiplier_);
×
3930
  initialize();
×
3931
}
3932

3933
// create the mesh from a pointer to a libMesh Mesh
3934
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3935
{
3936
  if (!input_mesh.is_replicated()) {
×
3937
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3938
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3939
  }
3940

3941
  m_ = &input_mesh;
3942
  set_length_multiplier(length_multiplier);
×
3943
  initialize();
×
3944
}
3945

3946
// create the mesh from an input file
3947
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3948
{
3949
  n_dimension_ = 3;
3950
  set_mesh_pointer_from_filename(filename);
×
3951
  set_length_multiplier(length_multiplier);
×
3952
  initialize();
×
3953
}
3954

3955
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3956
{
3957
  filename_ = filename;
23✔
3958
  unique_m_ =
23✔
3959
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3960
  m_ = unique_m_.get();
23✔
3961
  m_->read(filename_);
23✔
3962
}
23✔
3963

3964
// build a libMesh equation system for storing values
3965
void LibMesh::build_eqn_sys()
15✔
3966
{
3967
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
15✔
3968
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3969
  libMesh::ExplicitSystem& eq_sys =
15✔
3970
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3971
}
15✔
3972

3973
// intialize from mesh file
3974
void LibMesh::initialize()
23✔
3975
{
3976
  if (!settings::libmesh_comm) {
23!
3977
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3978
                "communicator.");
3979
  }
3980

3981
  // assuming that unstructured meshes used in OpenMC are 3D
3982
  n_dimension_ = 3;
23✔
3983

3984
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3985
  // Otherwise assume that it is prepared by its owning application
3986
  if (unique_m_) {
23!
3987
    m_->prepare_for_use();
23✔
3988
  }
3989

3990
  // ensure that the loaded mesh is 3 dimensional
3991
  if (m_->mesh_dimension() != n_dimension_) {
23!
3992
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3993
                            "mesh is not a 3D mesh.",
3994
      filename_));
3995
  }
3996

3997
  for (int i = 0; i < num_threads(); i++) {
69✔
3998
    pl_.emplace_back(m_->sub_point_locator());
46✔
3999
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
4000
    pl_.back()->enable_out_of_mesh_mode();
46✔
4001
  }
4002

4003
  // store first element in the mesh to use as an offset for bin indices
4004
  auto first_elem = *m_->elements_begin();
46✔
4005
  first_element_id_ = first_elem->id();
23✔
4006

4007
  // bounding box for the mesh for quick rejection checks
4008
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23!
4009
  libMesh::Point ll = bbox_.min();
23!
4010
  libMesh::Point ur = bbox_.max();
23!
4011
  if (length_multiplier_ > 0.0) {
23!
4012
    lower_left_ = {length_multiplier_ * ll(0), length_multiplier_ * ll(1),
4013
      length_multiplier_ * ll(2)};
4014
    upper_right_ = {length_multiplier_ * ur(0), length_multiplier_ * ur(1),
4015
      length_multiplier_ * ur(2)};
4016
  } else {
4017
    lower_left_ = {ll(0), ll(1), ll(2)};
23✔
4018
    upper_right_ = {ur(0), ur(1), ur(2)};
23✔
4019
  }
4020
}
23✔
4021

4022
// Sample position within a tet for LibMesh type tets
4023
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
4024
{
4025
  const auto& elem = get_element_from_bin(bin);
400,820✔
4026
  // Get tet vertex coordinates from LibMesh
4027
  std::array<Position, 4> tet_verts;
400,820✔
4028
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
4029
    auto node_ref = elem.node_ref(i);
1,603,280✔
4030
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
4031
  }
1,603,280✔
4032
  // Samples position within tet using Barycentric coordinates
4033
  Position sampled_position = this->sample_tet(tet_verts, seed);
400,820✔
4034
  if (length_multiplier_ > 0.0) {
400,820!
4035
    return length_multiplier_ * sampled_position;
4036
  } else {
4037
    return sampled_position;
400,820✔
4038
  }
4039
}
4040

4041
Position LibMesh::centroid(int bin) const
4042
{
4043
  const auto& elem = this->get_element_from_bin(bin);
4044
  auto centroid = elem.vertex_average();
4045
  if (length_multiplier_ > 0.0) {
×
4046
    return length_multiplier_ * Position(centroid(0), centroid(1), centroid(2));
4047
  } else {
4048
    return {centroid(0), centroid(1), centroid(2)};
4049
  }
4050
}
4051

4052
int LibMesh::n_vertices() const
39,978✔
4053
{
4054
  return m_->n_nodes();
39,978✔
4055
}
4056

4057
Position LibMesh::vertex(int vertex_id) const
39,942✔
4058
{
4059
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
4060
  if (length_multiplier_ > 0.0) {
39,942!
4061
    return length_multiplier_ * Position(node_ref(0), node_ref(1), node_ref(2));
×
4062
  } else {
4063
    return {node_ref(0), node_ref(1), node_ref(2)};
39,942✔
4064
  }
4065
}
39,942✔
4066

4067
std::vector<int> LibMesh::connectivity(int elem_id) const
265,856✔
4068
{
4069
  std::vector<int> conn;
265,856✔
4070
  const auto* elem_ptr = m_->elem_ptr(elem_id);
265,856✔
4071
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,337,280✔
4072
    conn.push_back(elem_ptr->node_id(i));
1,071,424✔
4073
  }
4074
  return conn;
265,856✔
4075
}
4076

4077
std::string LibMesh::library() const
33✔
4078
{
4079
  return mesh_lib_type;
33✔
4080
}
4081

4082
int LibMesh::n_bins() const
1,784,407✔
4083
{
4084
  return m_->n_elem();
1,784,407✔
4085
}
4086

4087
int LibMesh::n_surface_bins() const
4088
{
4089
  int n_bins = 0;
4090
  for (int i = 0; i < this->n_bins(); i++) {
×
4091
    const libMesh::Elem& e = get_element_from_bin(i);
4092
    n_bins += e.n_faces();
4093
    // if this is a boundary element, it will only be visited once,
4094
    // the number of surface bins is incremented to
4095
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
4096
      // null neighbor pointer indicates a boundary face
4097
      if (!neighbor_ptr) {
×
4098
        n_bins++;
4099
      }
4100
    }
4101
  }
4102
  return n_bins;
4103
}
4104

4105
void LibMesh::add_score(const std::string& var_name)
15✔
4106
{
4107
  if (!equation_systems_) {
15!
4108
    build_eqn_sys();
15✔
4109
  }
4110

4111
  // check if this is a new variable
4112
  std::string value_name = var_name + "_mean";
15✔
4113
  if (!variable_map_.count(value_name)) {
15✔
4114
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
4115
    auto var_num =
15✔
4116
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
4117
    variable_map_[value_name] = var_num;
15✔
4118
  }
4119

4120
  std::string std_dev_name = var_name + "_std_dev";
15✔
4121
  // check if this is a new variable
4122
  if (!variable_map_.count(std_dev_name)) {
15✔
4123
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
4124
    auto var_num =
15✔
4125
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
4126
    variable_map_[std_dev_name] = var_num;
15✔
4127
  }
4128
}
15✔
4129

4130
void LibMesh::remove_scores()
15✔
4131
{
4132
  if (equation_systems_) {
15!
4133
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
4134
    eqn_sys.clear();
15✔
4135
    variable_map_.clear();
15✔
4136
  }
4137
}
15✔
4138

4139
void LibMesh::set_score_data(const std::string& var_name,
15✔
4140
  const vector<double>& values, const vector<double>& std_dev)
4141
{
4142
  if (!equation_systems_) {
15!
4143
    build_eqn_sys();
4144
  }
4145

4146
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
4147

4148
  if (!eqn_sys.is_initialized()) {
15!
4149
    equation_systems_->init();
15✔
4150
  }
4151

4152
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
4153

4154
  // look up the value variable
4155
  std::string value_name = var_name + "_mean";
15✔
4156
  unsigned int value_num = variable_map_.at(value_name);
15✔
4157
  // look up the std dev variable
4158
  std::string std_dev_name = var_name + "_std_dev";
15✔
4159
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
4160

4161
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
195,757✔
4162
       it++) {
4163
    if (!(*it)->active()) {
97,856!
4164
      continue;
4165
    }
4166

4167
    auto bin = get_bin_from_element(*it);
97,856✔
4168

4169
    // set value
4170
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
4171
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
4172
    assert(value_dof_indices.size() == 1);
97,856✔
4173
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
4174

4175
    // set std dev
4176
    vector<libMesh::dof_id_type> std_dev_dof_indices;
97,856✔
4177
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
97,856✔
4178
    assert(std_dev_dof_indices.size() == 1);
97,856✔
4179
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
97,856✔
4180
  }
97,871✔
4181
}
15✔
4182

4183
void LibMesh::write(const std::string& filename) const
15✔
4184
{
4185
  write_message(fmt::format(
15✔
4186
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
15✔
4187
  libMesh::ExodusII_IO exo(*m_);
15✔
4188
  std::set<std::string> systems_out = {eq_system_name_};
30!
4189
  exo.write_discontinuous_exodusII(
15✔
4190
    filename + ".e", *equation_systems_, &systems_out);
30✔
4191
}
15✔
4192

4193
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
4194
  vector<int>& bins, vector<double>& lengths) const
4195
{
4196
  // TODO: Implement triangle crossings here
4197
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
4198
}
4199

4200
int LibMesh::get_bin(Position r) const
2,340,604✔
4201
{
4202
  // look-up a tet using the point locator
4203
  libMesh::Point p(r.x, r.y, r.z);
2,340,604!
4204

4205
  if (length_multiplier_ > 0.0) {
2,340,604!
4206
    // Scale the point down
4207
    p /= length_multiplier_;
2,340,604✔
4208
  }
4209

4210
  // quick rejection check
4211
  if (!bbox_.contains_point(p)) {
2,340,604✔
4212
    return -1;
4213
  }
4214

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

4217
  const auto elem_ptr = (*point_locator)(p);
1,421,808✔
4218
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,808✔
4219
}
2,340,604✔
4220

4221
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,518,434✔
4222
{
4223
  int bin = elem->id() - first_element_id_;
1,518,434✔
4224
  if (bin >= n_bins() || bin < 0) {
1,518,434!
4225
    fatal_error(fmt::format("Invalid bin: {}", bin));
4226
  }
4227
  return bin;
1,518,434✔
4228
}
4229

4230
std::pair<vector<double>, vector<double>> LibMesh::plot(
4231
  Position plot_ll, Position plot_ur) const
4232
{
4233
  return {};
4234
}
4235

4236
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
4237
{
4238
  return m_->elem_ref(bin);
765,460✔
4239
}
4240

4241
double LibMesh::volume(int bin) const
364,640✔
4242
{
4243
  return this->get_element_from_bin(bin).volume() * length_multiplier_ *
364,640✔
4244
         length_multiplier_ * length_multiplier_;
364,640✔
4245
}
4246

4247
AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh,
4248
  double length_multiplier,
4249
  const std::set<libMesh::subdomain_id_type>& block_ids)
4250
  : LibMesh(input_mesh, length_multiplier), block_ids_(block_ids),
4251
    block_restrict_(!block_ids_.empty()),
×
4252
    num_active_(
×
4253
      block_restrict_
4254
        ? std::distance(m_->active_subdomain_set_elements_begin(block_ids_),
×
4255
            m_->active_subdomain_set_elements_end(block_ids_))
×
4256
        : m_->n_active_elem())
×
4257
{
4258
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
4259
  // contiguous in ID space, so we need to map from bin indices (defined over
4260
  // active elements) to global dof ids
4261
  bin_to_elem_map_.reserve(num_active_);
×
4262
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
4263
  auto begin = block_restrict_
4264
                 ? m_->active_subdomain_set_elements_begin(block_ids_)
×
4265
                 : m_->active_elements_begin();
×
4266
  auto end = block_restrict_ ? m_->active_subdomain_set_elements_end(block_ids_)
×
4267
                             : m_->active_elements_end();
×
4268
  for (const auto& elem : libMesh::as_range(begin, end)) {
×
4269
    bin_to_elem_map_.push_back(elem->id());
×
4270
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
4271
  }
4272
}
4273

4274
int AdaptiveLibMesh::n_bins() const
4275
{
4276
  return num_active_;
4277
}
4278

4279
void AdaptiveLibMesh::add_score(const std::string& var_name)
4280
{
4281
  warning(fmt::format(
×
4282
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4283
    this->id_));
4284
}
4285

4286
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
4287
  const vector<double>& values, const vector<double>& std_dev)
4288
{
4289
  warning(fmt::format(
×
4290
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4291
    this->id_));
4292
}
4293

4294
void AdaptiveLibMesh::write(const std::string& filename) const
4295
{
4296
  warning(fmt::format(
×
4297
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4298
    this->id_));
4299
}
4300

4301
int AdaptiveLibMesh::get_bin(Position r) const
4302
{
4303
  // look-up a tet using the point locator
4304
  libMesh::Point p(r.x, r.y, r.z);
×
4305

4306
  if (length_multiplier_ > 0.0) {
×
4307
    // Scale the point down
4308
    p /= length_multiplier_;
4309
  }
4310

4311
  // quick rejection check
4312
  if (!bbox_.contains_point(p)) {
×
4313
    return -1;
4314
  }
4315

4316
  const auto& point_locator = pl_.at(thread_num());
×
4317

4318
  const auto elem_ptr = (*point_locator)(p, &block_ids_);
×
4319
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
×
4320
}
4321

4322
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
4323
{
4324
  int bin = elem_to_bin_map_[elem->id()];
4325
  if (bin >= n_bins() || bin < 0) {
×
4326
    fatal_error(fmt::format("Invalid bin: {}", bin));
4327
  }
4328
  return bin;
4329
}
4330

4331
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
4332
{
4333
  return m_->elem_ref(bin_to_elem_map_.at(bin));
4334
}
4335

4336
#endif // OPENMC_LIBMESH_ENABLED
4337

4338
//==============================================================================
4339
// Non-member functions
4340
//==============================================================================
4341

4342
void read_meshes(pugi::xml_node root)
12,653✔
4343
{
4344
  std::unordered_set<int> mesh_ids;
12,653✔
4345

4346
  for (auto node : root.children("mesh")) {
15,660✔
4347
    // Check to make sure multiple meshes in the same file don't share IDs
4348
    int id = std::stoi(get_node_value(node, "id"));
6,014✔
4349
    if (contains(mesh_ids, id)) {
6,014!
4350
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4351
                              "'{}' in the same input file",
4352
        id));
4353
    }
4354
    mesh_ids.insert(id);
3,007✔
4355

4356
    // If we've already read a mesh with the same ID in a *different* file,
4357
    // assume it is the same here
4358
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3,007!
4359
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
4360
      continue;
×
4361
    }
4362

4363
    std::string mesh_type;
3,007✔
4364
    if (check_for_node(node, "type")) {
3,007✔
4365
      mesh_type = get_node_value(node, "type", true, true);
970✔
4366
    } else {
4367
      mesh_type = "regular";
2,037✔
4368
    }
4369

4370
    // determine the mesh library to use
4371
    std::string mesh_lib;
3,007✔
4372
    if (check_for_node(node, "library")) {
3,007✔
4373
      mesh_lib = get_node_value(node, "library", true, true);
47!
4374
    }
4375

4376
    Mesh::create(node, mesh_type, mesh_lib);
3,007✔
4377
  }
3,007✔
4378
}
12,653✔
4379

4380
void read_meshes(hid_t group)
22✔
4381
{
4382
  std::unordered_set<int> mesh_ids;
22✔
4383

4384
  std::vector<int> ids;
22✔
4385
  read_attribute(group, "ids", ids);
22✔
4386

4387
  for (auto id : ids) {
55✔
4388

4389
    // Check to make sure multiple meshes in the same file don't share IDs
4390
    if (contains(mesh_ids, id)) {
66!
4391
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4392
                              "'{}' in the same HDF5 input file",
4393
        id));
4394
    }
4395
    mesh_ids.insert(id);
33✔
4396

4397
    // If we've already read a mesh with the same ID in a *different* file,
4398
    // assume it is the same here
4399
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
33!
4400
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
33✔
4401
      continue;
33✔
4402
    }
4403

4404
    std::string name = fmt::format("mesh {}", id);
×
4405
    hid_t mesh_group = open_group(group, name.c_str());
×
4406

4407
    std::string mesh_type;
×
4408
    if (object_exists(mesh_group, "type")) {
×
4409
      read_dataset(mesh_group, "type", mesh_type);
×
4410
    } else {
4411
      mesh_type = "regular";
×
4412
    }
4413

4414
    // determine the mesh library to use
4415
    std::string mesh_lib;
×
4416
    if (object_exists(mesh_group, "library")) {
×
4417
      read_dataset(mesh_group, "library", mesh_lib);
×
4418
    }
4419

4420
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
4421
  }
×
4422
}
44✔
4423

4424
void meshes_to_hdf5(hid_t group)
7,060✔
4425
{
4426
  // Write number of meshes
4427
  hid_t meshes_group = create_group(group, "meshes");
7,060✔
4428
  int32_t n_meshes = model::meshes.size();
7,060✔
4429
  write_attribute(meshes_group, "n_meshes", n_meshes);
7,060✔
4430

4431
  if (n_meshes > 0) {
7,060✔
4432
    // Write IDs of meshes
4433
    vector<int> ids;
2,192✔
4434
    for (const auto& m : model::meshes) {
4,963✔
4435
      m->to_hdf5(meshes_group);
2,771✔
4436
      ids.push_back(m->id_);
2,771✔
4437
    }
4438
    write_attribute(meshes_group, "ids", ids);
2,192✔
4439
  }
2,192✔
4440

4441
  close_group(meshes_group);
7,060✔
4442
}
7,060✔
4443

4444
void free_memory_mesh()
8,294✔
4445
{
4446
  model::meshes.clear();
8,294✔
4447
  model::mesh_map.clear();
8,294✔
4448
}
8,294✔
4449

4450
extern "C" int n_meshes()
308✔
4451
{
4452
  return model::meshes.size();
308✔
4453
}
4454

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