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

openmc-dev / openmc / 21497172691

29 Jan 2026 10:34PM UTC coverage: 81.959% (-0.04%) from 82.001%
21497172691

Pull #3748

github

web-flow
Merge ed3ccb631 into f7a734189
Pull Request #3748: Fix for plotting model with multi-group cross sections

17245 of 24021 branches covered (71.79%)

Branch coverage included in aggregate %.

3 of 6 new or added lines in 1 file covered. (50.0%)

779 existing lines in 15 files now uncovered.

55797 of 65099 relevant lines covered (85.71%)

43965072.54 hits per line

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

70.49
/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 <string>
10

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

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

19
#include "xtensor/xadapt.hpp"
20
#include "xtensor/xbuilder.hpp"
21
#include "xtensor/xeval.hpp"
22
#include "xtensor/xmath.hpp"
23
#include "xtensor/xsort.hpp"
24
#include "xtensor/xtensor.hpp"
25
#include "xtensor/xview.hpp"
26
#include <fmt/core.h> // for fmt
27

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

52
#ifdef OPENMC_LIBMESH_ENABLED
53
#include "libmesh/mesh_modification.h"
54
#include "libmesh/mesh_tools.h"
55
#include "libmesh/numeric_vector.h"
56
#include "libmesh/replicated_mesh.h"
57
#endif
58

59
#ifdef OPENMC_DAGMC_ENABLED
60
#include "moab/FileOptions.hpp"
61
#endif
62

63
namespace openmc {
64

65
//==============================================================================
66
// Global variables
67
//==============================================================================
68

69
#ifdef OPENMC_LIBMESH_ENABLED
70
const bool LIBMESH_ENABLED = true;
71
#else
72
const bool LIBMESH_ENABLED = false;
73
#endif
74

75
// Value used to indicate an empty slot in the hash table. We use -2 because
76
// the value -1 is used to indicate a void material.
77
constexpr int32_t EMPTY = -2;
78

79
namespace model {
80

81
std::unordered_map<int32_t, int32_t> mesh_map;
82
vector<unique_ptr<Mesh>> meshes;
83

84
} // namespace model
85

86
#ifdef OPENMC_LIBMESH_ENABLED
87
namespace settings {
88
unique_ptr<libMesh::LibMeshInit> libmesh_init;
89
const libMesh::Parallel::Communicator* libmesh_comm {nullptr};
90
} // namespace settings
91
#endif
92

93
//==============================================================================
94
// Helper functions
95
//==============================================================================
96

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

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

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

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

140
#else
141
#error "No compare-and-swap implementation available for this compiler."
142
#endif
143
}
144

145
// Helper function equivalent to std::bit_cast in C++20
146
template<typename To, typename From>
147
inline To bit_cast_value(const From& value)
36,529,976✔
148
{
149
  To out;
150
  std::memcpy(&out, &value, sizeof(To));
36,529,976✔
151
  return out;
36,529,976✔
152
}
153

154
inline void atomic_update_double(double* ptr, double value, bool is_min)
36,498,144✔
155
{
156
#if defined(__GNUC__) || defined(__clang__)
157
  using may_alias_uint64_t [[gnu::may_alias]] = uint64_t;
158
  auto* bits_ptr = reinterpret_cast<may_alias_uint64_t*>(ptr);
36,498,144✔
159
  uint64_t current_bits = __atomic_load_n(bits_ptr, __ATOMIC_SEQ_CST);
36,498,144✔
160
  double current = bit_cast_value<double>(current_bits);
36,498,144✔
161
  while (is_min ? (value < current) : (value > current)) {
36,498,281✔
162
    uint64_t desired_bits = bit_cast_value<uint64_t>(value);
31,695✔
163
    uint64_t expected_bits = current_bits;
31,695✔
164
    if (__atomic_compare_exchange_n(bits_ptr, &expected_bits, desired_bits,
31,695✔
165
          false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
166
      return;
31,558✔
167
    }
168
    current_bits = expected_bits;
137✔
169
    current = bit_cast_value<double>(current_bits);
137✔
170
  }
171

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

187
#else
188
#error "No compare-and-swap implementation available for this compiler."
189
#endif
190
}
191

192
inline void atomic_max_double(double* ptr, double value)
18,249,072✔
193
{
194
  atomic_update_double(ptr, value, false);
18,249,072✔
195
}
18,249,072✔
196

197
inline void atomic_min_double(double* ptr, double value)
18,249,072✔
198
{
199
  atomic_update_double(ptr, value, true);
18,249,072✔
200
}
18,249,072✔
201

202
namespace detail {
203

204
//==============================================================================
205
// MaterialVolumes implementation
206
//==============================================================================
207

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

217
  // Loop for linear probing
218
  for (int attempt = 0; attempt < table_size_; ++attempt) {
8,783,305!
219
    // Determine slot to check, making sure it is positive
220
    int slot = (index_material + attempt) % table_size_;
8,783,305✔
221
    if (slot < 0)
8,783,305✔
222
      slot += table_size_;
5,816,482✔
223
    int32_t* slot_ptr = &this->materials(index_elem, slot);
8,783,305✔
224

225
    // Non-atomic read of current material
226
    int32_t current_val = *slot_ptr;
8,783,305✔
227

228
    // Found the desired material; accumulate volume and bbox
229
    if (current_val == index_material) {
8,783,305✔
230
#pragma omp atomic
4,765,347✔
231
      this->volumes(index_elem, slot) += volume;
8,734,882✔
232
      if (bbox) {
8,734,882✔
233
        atomic_min_double(&this->bboxes(index_elem, slot, 0), bbox->min.x);
6,082,896✔
234
        atomic_min_double(&this->bboxes(index_elem, slot, 1), bbox->min.y);
6,082,896✔
235
        atomic_min_double(&this->bboxes(index_elem, slot, 2), bbox->min.z);
6,082,896✔
236
        atomic_max_double(&this->bboxes(index_elem, slot, 3), bbox->max.x);
6,082,896✔
237
        atomic_max_double(&this->bboxes(index_elem, slot, 4), bbox->max.y);
6,082,896✔
238
        atomic_max_double(&this->bboxes(index_elem, slot, 5), bbox->max.z);
6,082,896✔
239
      }
240
      return;
8,734,882✔
241
    }
242

243
    // Slot appears to be empty; attempt to claim
244
    if (current_val == EMPTY) {
48,423✔
245
      // Attempt compare-and-swap from EMPTY to index_material
246
      int32_t expected_val = EMPTY;
1,529✔
247
      bool claimed_slot =
248
        atomic_cas_int32(slot_ptr, expected_val, index_material);
1,529✔
249

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

268
  // If table is full, set a flag that can be checked later
UNCOV
269
  table_full_ = true;
×
270
}
271

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

282
    // Read current material
UNCOV
283
    int32_t current_val = this->materials(index_elem, slot);
×
284

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

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

327
  // If table is full, set a flag that can be checked later
UNCOV
328
  table_full_ = true;
×
329
}
330

331
} // namespace detail
332

333
//==============================================================================
334
// Mesh implementation
335
//==============================================================================
336

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

367
  // Map ID to position in vector
368
  model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
2,955✔
369

370
  return model::meshes.back();
2,955✔
371
}
372

373
Mesh::Mesh(pugi::xml_node node)
2,999✔
374
{
375
  // Read mesh id
376
  id_ = std::stoi(get_node_value(node, "id"));
2,999✔
377
  if (check_for_node(node, "name"))
2,999✔
378
    name_ = get_node_value(node, "name");
16✔
379
}
2,999✔
380

381
Mesh::Mesh(hid_t group)
44✔
382
{
383
  // Read mesh ID
384
  read_attribute(group, "id", id_);
44✔
385

386
  // Read mesh name
387
  if (object_exists(group, "name")) {
44!
UNCOV
388
    read_dataset(group, "name", name_);
×
389
  }
390
}
44✔
391

392
void Mesh::set_id(int32_t id)
23✔
393
{
394
  assert(id >= 0 || id == C_NONE);
19!
395

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

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

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

417
  // Update ID and entry in the mesh map
418
  id_ = id;
23✔
419

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

425
  model::mesh_map[id] = std::distance(model::meshes.begin(), it.base()) - 1;
23✔
426
}
23✔
427

428
vector<double> Mesh::volumes() const
252✔
429
{
430
  vector<double> volumes(n_bins());
252✔
431
  for (int i = 0; i < n_bins(); i++) {
1,125,127✔
432
    volumes[i] = this->volume(i);
1,124,875✔
433
  }
434
  return volumes;
252✔
UNCOV
435
}
×
436

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

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

459
  Timer timer;
187✔
460
  timer.start();
187✔
461

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

466
  // Determine bounding box
467
  auto bbox = this->bounding_box();
187✔
468

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

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

477
  // Set flag for mesh being contained within model
478
  bool out_of_model = false;
187✔
479

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

487
    SourceSite site;
85✔
488
    site.E = 1.0;
85✔
489
    site.particle = ParticleType::neutron;
85✔
490

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

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

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

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

526
          p.from_source(&site);
2,962,735✔
527

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

534
          // Set birth cell attribute
535
          if (p.cell_born() == C_NONE)
2,922,805!
536
            p.cell_born() = p.lowest_coord().cell();
2,922,805✔
537

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

544
          while (true) {
545
            // Ray trace from r_start to r_end
546
            Position r0 = p.r();
3,757,715✔
547
            double max_distance = bbox.max[axis] - r0[axis];
3,757,715✔
548

549
            // Find the distance to the nearest boundary
550
            BoundaryInfo boundary = distance_to_boundary(p);
3,757,715✔
551

552
            // Advance particle forward
553
            double distance = std::min(boundary.distance(), max_distance);
3,757,715✔
554
            p.move_distance(distance);
3,757,715✔
555

556
            // Determine what mesh elements were crossed by particle
557
            bins.clear();
3,757,715✔
558
            length_fractions.clear();
3,757,715✔
559
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
3,757,715✔
560

561
            // Add volumes to any mesh elements that were crossed
562
            int i_material = p.material();
3,757,715✔
563
            if (i_material != C_NONE) {
3,757,715✔
564
              i_material = model::materials[i_material]->id();
1,100,525✔
565
            }
566
            double cumulative_frac = 0.0;
3,757,715✔
567
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
7,734,080✔
568
              int mesh_index = bins[i_bin];
3,976,365✔
569
              double length = distance * length_fractions[i_bin];
3,976,365✔
570
              double volume = length * d1 * d2;
3,976,365✔
571

572
              if (compute_bboxes) {
3,976,365✔
573
                double axis_start = r0[axis] + distance * cumulative_frac;
2,770,280✔
574
                double axis_end = axis_start + length;
2,770,280✔
575
                cumulative_frac += length_fractions[i_bin];
2,770,280✔
576

577
                Position contrib_min = site.r;
2,770,280✔
578
                Position contrib_max = site.r;
2,770,280✔
579

580
                contrib_min[ax1] = site.r[ax1] - 0.5 * d1;
2,770,280✔
581
                contrib_max[ax1] = site.r[ax1] + 0.5 * d1;
2,770,280✔
582
                contrib_min[ax2] = site.r[ax2] - 0.5 * d2;
2,770,280✔
583
                contrib_max[ax2] = site.r[ax2] + 0.5 * d2;
2,770,280✔
584
                contrib_min[axis] = std::min(axis_start, axis_end);
2,770,280✔
585
                contrib_max[axis] = std::max(axis_start, axis_end);
2,770,280✔
586

587
                BoundingBox contrib_bbox {contrib_min, contrib_max};
2,770,280✔
588
                contrib_bbox &= bbox;
2,770,280✔
589

590
                result.add_volume(
2,770,280✔
591
                  mesh_index, i_material, volume, &contrib_bbox);
592
              } else {
593
                // Add volume to result
594
                result.add_volume(mesh_index, i_material, volume);
1,206,085✔
595
              }
596
            }
597

598
            if (distance == max_distance)
3,757,715✔
599
              break;
2,922,805✔
600

601
            // cross next geometric surface
602
            for (int j = 0; j < p.n_coord(); ++j) {
1,669,820✔
603
              p.cell_last(j) = p.coord(j).cell();
834,910✔
604
            }
605
            p.n_coord_last() = p.n_coord();
834,910✔
606

607
            // Set surface that particle is on and adjust coordinate levels
608
            p.surface() = boundary.surface();
834,910✔
609
            p.n_coord() = boundary.coord_level();
834,910✔
610

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

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

635
  // Compute time for raytracing
636
  double t_raytrace = timer.elapsed();
176✔
637

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

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

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

696
  // Report time for MPI communication
697
  double t_mpi = timer.elapsed() - t_raytrace;
80✔
698
#else
699
  double t_mpi = 0.0;
96✔
700
#endif
701

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

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

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

736
void Mesh::to_hdf5(hid_t group) const
2,861✔
737
{
738
  // Create group for mesh
739
  std::string group_name = fmt::format("mesh {}", id_);
5,184✔
740
  hid_t mesh_group = create_group(group, group_name.c_str());
2,861✔
741

742
  // Write mesh type
743
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,861✔
744

745
  // Write mesh ID
746
  write_attribute(mesh_group, "id", id_);
2,861✔
747

748
  // Write mesh name
749
  write_dataset(mesh_group, "name", name_);
2,861✔
750

751
  // Write mesh data
752
  this->to_hdf5_inner(mesh_group);
2,861✔
753

754
  // Close group
755
  close_group(mesh_group);
2,861✔
756
}
2,861✔
757

758
//==============================================================================
759
// Structured Mesh implementation
760
//==============================================================================
761

762
std::string StructuredMesh::bin_label(int bin) const
5,130,327✔
763
{
764
  MeshIndex ijk = get_indices_from_bin(bin);
5,130,327✔
765

766
  if (n_dimension_ > 2) {
5,130,327✔
767
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,228,864✔
768
  } else if (n_dimension_ > 1) {
15,895✔
769
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,240✔
770
  } else {
771
    return fmt::format("Mesh Index ({})", ijk[0]);
550✔
772
  }
773
}
774

775
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,466✔
776
{
777
  // because method is const, shape_ is const as well and can't be adapted
778
  auto tmp_shape = shape_;
2,466✔
779
  return xt::adapt(tmp_shape, {n_dimension_});
4,932✔
780
}
781

782
Position StructuredMesh::sample_element(
1,425,033✔
783
  const MeshIndex& ijk, uint64_t* seed) const
784
{
785
  // lookup the lower/upper bounds for the mesh element
786
  double x_min = negative_grid_boundary(ijk, 0);
1,425,033✔
787
  double x_max = positive_grid_boundary(ijk, 0);
1,425,033✔
788

789
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,425,033!
790
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,425,033!
791

792
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,425,033!
793
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,425,033!
794

795
  return {x_min + (x_max - x_min) * prn(seed),
1,425,033✔
796
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,425,033✔
797
}
798

799
//==============================================================================
800
// Unstructured Mesh implementation
801
//==============================================================================
802

803
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
47✔
804
{
805
  n_dimension_ = 3;
47✔
806

807
  // check the mesh type
808
  if (check_for_node(node, "type")) {
47!
809
    auto temp = get_node_value(node, "type", true, true);
47!
810
    if (temp != mesh_type) {
47!
UNCOV
811
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
812
    }
813
  }
47✔
814

815
  // check if a length unit multiplier was specified
816
  if (check_for_node(node, "length_multiplier")) {
47!
UNCOV
817
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
818
  }
819

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

831
  if (check_for_node(node, "options")) {
47!
832
    options_ = get_node_value(node, "options");
16!
833
  }
834

835
  // check if mesh tally data should be written with
836
  // statepoint files
837
  if (check_for_node(node, "output")) {
47!
838
    output_ = get_node_value_bool(node, "output");
×
839
  }
840
}
47✔
841

UNCOV
842
UnstructuredMesh::UnstructuredMesh(hid_t group) : Mesh(group)
×
843
{
UNCOV
844
  n_dimension_ = 3;
×
845

846
  // check the mesh type
847
  if (object_exists(group, "type")) {
×
UNCOV
848
    std::string temp;
×
UNCOV
849
    read_dataset(group, "type", temp);
×
850
    if (temp != mesh_type) {
×
UNCOV
851
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
852
    }
UNCOV
853
  }
×
854

855
  // check if a length unit multiplier was specified
UNCOV
856
  if (object_exists(group, "length_multiplier")) {
×
UNCOV
857
    read_dataset(group, "length_multiplier", length_multiplier_);
×
858
  }
859

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

UNCOV
871
  if (attribute_exists(group, "options")) {
×
872
    read_attribute(group, "options", options_);
×
873
  }
874

875
  // check if mesh tally data should be written with
876
  // statepoint files
UNCOV
877
  if (attribute_exists(group, "output")) {
×
UNCOV
878
    read_attribute(group, "output", output_);
×
879
  }
UNCOV
880
}
×
881

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

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

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

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

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

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

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

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

960
  if (length_multiplier_ > 0.0)
32!
961
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
962

963
  // write vertex coordinates
964
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
32!
965
  for (int i = 0; i < this->n_vertices(); i++) {
70,275!
966
    auto v = this->vertex(i);
70,243!
967
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
70,243!
968
  }
969
  write_dataset(mesh_group, "vertices", vertices);
32!
970

971
  int num_elem_skipped = 0;
32✔
972

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

980
    volumes.emplace_back(this->volume(i));
349,736!
981

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

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

1010
  write_dataset(mesh_group, "volumes", volumes);
32!
1011
  write_dataset(mesh_group, "connectivity", connectivity);
32!
1012
  write_dataset(mesh_group, "element_types", elem_types);
32!
1013
}
32✔
1014

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

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

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

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

1040
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
1041
      in_mesh = false;
102,612,478✔
1042
  }
1043
  return ijk;
1,162,608,336✔
1044
}
1045

1046
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,716,013,954✔
1047
{
1048
  switch (n_dimension_) {
1,716,013,954!
1049
  case 1:
880,605✔
1050
    return ijk[0] - 1;
880,605✔
1051
  case 2:
116,880,830✔
1052
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
116,880,830✔
1053
  case 3:
1,598,252,519✔
1054
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,598,252,519✔
UNCOV
1055
  default:
×
UNCOV
1056
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
1057
  }
1058
}
1059

1060
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
7,771,546✔
1061
{
1062
  MeshIndex ijk;
1063
  if (n_dimension_ == 1) {
7,771,546✔
1064
    ijk[0] = bin + 1;
275✔
1065
  } else if (n_dimension_ == 2) {
7,771,271✔
1066
    ijk[0] = bin % shape_[0] + 1;
15,620✔
1067
    ijk[1] = bin / shape_[0] + 1;
15,620✔
1068
  } else if (n_dimension_ == 3) {
7,755,651!
1069
    ijk[0] = bin % shape_[0] + 1;
7,755,651✔
1070
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
7,755,651✔
1071
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
7,755,651✔
1072
  }
1073
  return ijk;
7,771,546✔
1074
}
1075

1076
int StructuredMesh::get_bin(Position r) const
247,959,869✔
1077
{
1078
  // Determine indices
1079
  bool in_mesh;
1080
  MeshIndex ijk = get_indices(r, in_mesh);
247,959,869✔
1081
  if (!in_mesh)
247,959,869✔
1082
    return -1;
21,004,880✔
1083

1084
  // Convert indices to bin
1085
  return get_bin_from_indices(ijk);
226,954,989✔
1086
}
1087

1088
int StructuredMesh::n_bins() const
1,139,505✔
1089
{
1090
  return std::accumulate(
1,139,505✔
1091
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,279,010✔
1092
}
1093

1094
int StructuredMesh::n_surface_bins() const
380✔
1095
{
1096
  return 4 * n_dimension_ * n_bins();
380✔
1097
}
1098

UNCOV
1099
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
1100
  const SourceSite* bank, int64_t length, bool* outside) const
1101
{
1102
  // Determine shape of array for counts
UNCOV
1103
  std::size_t m = this->n_bins();
×
UNCOV
1104
  vector<std::size_t> shape = {m};
×
1105

1106
  // Create array of zeros
UNCOV
1107
  xt::xarray<double> cnt {shape, 0.0};
×
UNCOV
1108
  bool outside_ = false;
×
1109

UNCOV
1110
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
1111
    const auto& site = bank[i];
×
1112

1113
    // determine scoring bin for entropy mesh
UNCOV
1114
    int mesh_bin = get_bin(site.r);
×
1115

1116
    // if outside mesh, skip particle
UNCOV
1117
    if (mesh_bin < 0) {
×
UNCOV
1118
      outside_ = true;
×
UNCOV
1119
      continue;
×
1120
    }
1121

1122
    // Add to appropriate bin
UNCOV
1123
    cnt(mesh_bin) += site.wgt;
×
1124
  }
1125

1126
  // Create copy of count data. Since ownership will be acquired by xtensor,
1127
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
1128
  // warnings.
UNCOV
1129
  int total = cnt.size();
×
UNCOV
1130
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
1131

1132
#ifdef OPENMC_MPI
1133
  // collect values from all processors
1134
  MPI_Reduce(
×
1135
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1136

1137
  // Check if there were sites outside the mesh for any processor
1138
  if (outside) {
×
1139
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
×
1140
  }
1141
#else
1142
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
×
1143
  if (outside)
×
1144
    *outside = outside_;
1145
#endif
1146

1147
  // Adapt reduced values in array back into an xarray
UNCOV
1148
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
UNCOV
1149
  xt::xarray<double> counts = arr;
×
1150

UNCOV
1151
  return counts;
×
UNCOV
1152
}
×
1153

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

1167
  // Compute the length of the entire track.
1168
  double total_distance = (r1 - r0).norm();
919,531,505✔
1169
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
919,531,505✔
1170
    return;
11,630,955✔
1171

1172
  // keep a copy of the original global position to pass to get_indices,
1173
  // which performs its own transformation to local coordinates
1174
  Position global_r = r0;
907,900,550✔
1175
  Position local_r = local_coords(r0);
907,900,550✔
1176

1177
  const int n = n_dimension_;
907,900,550✔
1178

1179
  // Flag if position is inside the mesh
1180
  bool in_mesh;
1181

1182
  // Position is r = r0 + u * traveled_distance, start at r0
1183
  double traveled_distance {0.0};
907,900,550✔
1184

1185
  // Calculate index of current cell. Offset the position a tiny bit in
1186
  // direction of flight
1187
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
907,900,550✔
1188

1189
  // if track is very short, assume that it is completely inside one cell.
1190
  // Only the current cell will score and no surfaces
1191
  if (total_distance < 2 * TINY_BIT) {
907,900,550✔
1192
    if (in_mesh) {
331,527✔
1193
      tally.track(ijk, 1.0);
331,043✔
1194
    }
1195
    return;
331,527✔
1196
  }
1197

1198
  // Calculate initial distances to next surfaces in all three dimensions
1199
  std::array<MeshDistance, 3> distances;
1,815,138,046✔
1200
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
1201
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
1202
  }
1203

1204
  // Loop until r = r1 is eventually reached
1205
  while (true) {
749,284,386✔
1206

1207
    if (in_mesh) {
1,656,853,409✔
1208

1209
      // find surface with minimal distance to current position
1210
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,570,613,297✔
1211
                     distances.begin();
1,570,613,297✔
1212

1213
      // Tally track length delta since last step
1214
      tally.track(ijk,
1,570,613,297✔
1215
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,570,613,297✔
1216
          total_distance);
1217

1218
      // update position and leave, if we have reached end position
1219
      traveled_distance = distances[k].distance;
1,570,613,297✔
1220
      if (traveled_distance >= total_distance)
1,570,613,297✔
1221
        return;
828,076,828✔
1222

1223
      // If we have not reached r1, we have hit a surface. Tally outward
1224
      // current
1225
      tally.surface(ijk, k, distances[k].max_surface, false);
742,536,469✔
1226

1227
      // Update cell and calculate distance to next surface in k-direction.
1228
      // The two other directions are still valid!
1229
      ijk[k] = distances[k].next_index;
742,536,469✔
1230
      distances[k] =
742,536,469✔
1231
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
742,536,469✔
1232

1233
      // Check if we have left the interior of the mesh
1234
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
742,536,469✔
1235

1236
      // If we are still inside the mesh, tally inward current for the next
1237
      // cell
1238
      if (in_mesh)
742,536,469✔
1239
        tally.surface(ijk, k, !distances[k].max_surface, true);
729,187,591✔
1240

1241
    } else { // not inside mesh
1242

1243
      // For all directions outside the mesh, find the distance that we need
1244
      // to travel to reach the next surface. Use the largest distance, as
1245
      // only this will cross all outer surfaces.
1246
      int k_max {-1};
86,240,112✔
1247
      for (int k = 0; k < n; ++k) {
343,516,082✔
1248
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
351,523,997✔
1249
            (distances[k].distance > traveled_distance)) {
94,248,027✔
1250
          traveled_distance = distances[k].distance;
89,264,503✔
1251
          k_max = k;
89,264,503✔
1252
        }
1253
      }
1254
      // Assure some distance is traveled
1255
      if (k_max == -1) {
86,240,112✔
1256
        traveled_distance += TINY_BIT;
110✔
1257
      }
1258

1259
      // If r1 is not inside the mesh, exit here
1260
      if (traveled_distance >= total_distance)
86,240,112✔
1261
        return;
79,492,195✔
1262

1263
      // Calculate the new cell index and update all distances to next
1264
      // surfaces.
1265
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,747,917✔
1266
      for (int k = 0; k < n; ++k) {
26,783,130✔
1267
        distances[k] =
20,035,213✔
1268
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
20,035,213✔
1269
      }
1270

1271
      // If inside the mesh, Tally inward current
1272
      if (in_mesh && k_max >= 0)
6,747,917!
1273
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,322,871✔
1274
    }
1275
  }
1276
}
1277

1278
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
807,403,940✔
1279
  vector<int>& bins, vector<double>& lengths) const
1280
{
1281

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

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

1302
  // Perform the mesh raytrace with the helper class.
1303
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
807,403,940✔
1304
}
807,403,940✔
1305

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

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

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

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

1337
//==============================================================================
1338
// RegularMesh implementation
1339
//==============================================================================
1340

1341
int RegularMesh::set_grid()
2,091✔
1342
{
1343
  auto shape = xt::adapt(shape_, {n_dimension_});
2,091✔
1344

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

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

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

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

1373
    // Set width and upper right coordinate
1374
    upper_right_ = xt::eval(lower_left_ + shape * width_);
49✔
1375

1376
  } else if (upper_right_.size() > 0) {
2,042!
1377

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

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

1393
    // Set width
1394
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
2,042✔
1395
  }
1396

1397
  // Set material volumes
1398
  volume_frac_ = 1.0 / xt::prod(shape)();
2,091✔
1399

1400
  element_volume_ = 1.0;
2,091✔
1401
  for (int i = 0; i < n_dimension_; i++) {
7,846✔
1402
    element_volume_ *= width_[i];
5,755✔
1403
  }
1404
  return 0;
2,091✔
1405
}
2,091✔
1406

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

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

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

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

1435
    width_ = get_node_xarray<double>(node, "width");
49✔
1436

1437
  } else if (check_for_node(node, "upper_right")) {
2,031!
1438

1439
    upper_right_ = get_node_xarray<double>(node, "upper_right");
2,031✔
1440

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

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

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

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

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

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

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

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

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

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

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

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

1498
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,458,903,550✔
1499
{
1500
  return lower_left_[i] + ijk[i] * width_[i];
1,458,903,550✔
1501
}
1502

1503
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,389,728,055✔
1504
{
1505
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,389,728,055✔
1506
}
1507

1508
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1509
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1510
  double l) const
1511
{
1512
  MeshDistance d;
2,147,483,647✔
1513
  d.next_index = ijk[i];
2,147,483,647✔
1514
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1515
    return d;
13,624,230✔
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.next_index++;
1,454,628,451✔
1520
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,454,628,451✔
1521
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,406,871,579✔
1522
    d.next_index--;
1,385,452,956✔
1523
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,385,452,956✔
1524
  }
1525

1526
  return d;
2,147,483,647✔
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✔
UNCOV
1538
  } else if (plot_ur.y == plot_ll.y) {
×
UNCOV
1539
    axes[0] = 0;
×
UNCOV
1540
    if (n_dimension_ > 2)
×
UNCOV
1541
      axes[1] = 2;
×
UNCOV
1542
  } else if (plot_ur.x == plot_ll.x) {
×
UNCOV
1543
    if (n_dimension_ > 1)
×
UNCOV
1544
      axes[0] = 1;
×
UNCOV
1545
    if (n_dimension_ > 2)
×
UNCOV
1546
      axes[1] = 2;
×
1547
  } else {
UNCOV
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;
22✔
1553
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
66✔
1554
    int axis = axes[i_ax];
44✔
1555
    if (axis == -1)
44!
UNCOV
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
}
22✔
1569

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

1578
xt::xtensor<double, 1> RegularMesh::count_sites(
7,839✔
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,839✔
1583
  vector<std::size_t> shape = {m};
7,839✔
1584

1585
  // Create array of zeros
1586
  xt::xarray<double> cnt {shape, 0.0};
7,839✔
1587
  bool outside_ = false;
7,839✔
1588

1589
  for (int64_t i = 0; i < length; i++) {
7,675,290✔
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!
UNCOV
1597
      outside_ = true;
×
UNCOV
1598
      continue;
×
1599
    }
1600

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

1605
  // Create copy of count data. Since ownership will be acquired by xtensor,
1606
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
1607
  // warnings.
1608
  int total = cnt.size();
7,839✔
1609
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
7,839✔
1610

1611
#ifdef OPENMC_MPI
1612
  // collect values from all processors
1613
  MPI_Reduce(
3,615✔
1614
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
3,615✔
1615

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

1626
  // Adapt reduced values in array back into an xarray
1627
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
7,839✔
1628
  xt::xarray<double> counts = arr;
7,839✔
1629

1630
  return counts;
15,678✔
1631
}
7,839✔
1632

1633
double RegularMesh::volume(const MeshIndex& ijk) const
1,126,107✔
1634
{
1635
  return element_volume_;
1,126,107✔
1636
}
1637

1638
//==============================================================================
1639
// RectilinearMesh implementation
1640
//==============================================================================
1641

1642
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
125✔
1643
{
1644
  n_dimension_ = 3;
125✔
1645

1646
  grid_[0] = get_node_array<double>(node, "x_grid");
125✔
1647
  grid_[1] = get_node_array<double>(node, "y_grid");
125✔
1648
  grid_[2] = get_node_array<double>(node, "z_grid");
125✔
1649

1650
  if (int err = set_grid()) {
125!
UNCOV
1651
    fatal_error(openmc_err_msg);
×
1652
  }
1653
}
125✔
1654

1655
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
11✔
1656
{
1657
  n_dimension_ = 3;
11✔
1658

1659
  read_dataset(group, "x_grid", grid_[0]);
11✔
1660
  read_dataset(group, "y_grid", grid_[1]);
11✔
1661
  read_dataset(group, "z_grid", grid_[2]);
11✔
1662

1663
  if (int err = set_grid()) {
11!
UNCOV
1664
    fatal_error(openmc_err_msg);
×
1665
  }
1666
}
11✔
1667

1668
const std::string RectilinearMesh::mesh_type = "rectilinear";
1669

1670
std::string RectilinearMesh::get_mesh_type() const
275✔
1671
{
1672
  return mesh_type;
275✔
1673
}
1674

1675
double RectilinearMesh::positive_grid_boundary(
26,505,963✔
1676
  const MeshIndex& ijk, int i) const
1677
{
1678
  return grid_[i][ijk[i]];
26,505,963✔
1679
}
1680

1681
double RectilinearMesh::negative_grid_boundary(
25,739,406✔
1682
  const MeshIndex& ijk, int i) const
1683
{
1684
  return grid_[i][ijk[i] - 1];
25,739,406✔
1685
}
1686

1687
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
53,602,087✔
1688
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1689
  double l) const
1690
{
1691
  MeshDistance d;
53,602,087✔
1692
  d.next_index = ijk[i];
53,602,087✔
1693
  if (std::abs(u[i]) < FP_PRECISION)
53,602,087✔
1694
    return d;
571,824✔
1695

1696
  d.max_surface = (u[i] > 0);
53,030,263✔
1697
  if (d.max_surface && (ijk[i] <= shape_[i])) {
53,030,263✔
1698
    d.next_index++;
26,505,963✔
1699
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
26,505,963✔
1700
  } else if (!d.max_surface && (ijk[i] > 0)) {
26,524,300✔
1701
    d.next_index--;
25,739,406✔
1702
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
25,739,406✔
1703
  }
1704
  return d;
53,030,263✔
1705
}
1706

1707
int RectilinearMesh::set_grid()
180✔
1708
{
1709
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
180✔
1710
    static_cast<int>(grid_[1].size()) - 1,
180✔
1711
    static_cast<int>(grid_[2].size()) - 1};
180✔
1712

1713
  for (const auto& g : grid_) {
720✔
1714
    if (g.size() < 2) {
540!
UNCOV
1715
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1716
                 "must each have at least 2 points");
UNCOV
1717
      return OPENMC_E_INVALID_ARGUMENT;
×
1718
    }
1719
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
540✔
1720
        g.end()) {
1,080!
UNCOV
1721
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1722
                 "rectilinear meshes must be sorted and unique.");
UNCOV
1723
      return OPENMC_E_INVALID_ARGUMENT;
×
1724
    }
1725
  }
1726

1727
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
180✔
1728
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
180✔
1729

1730
  return 0;
180✔
1731
}
1732

1733
int RectilinearMesh::get_index_in_direction(double r, int i) const
74,108,892✔
1734
{
1735
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,108,892✔
1736
}
1737

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

1753
  // Get the coordinates of the mesh lines along both of the axes.
1754
  array<vector<double>, 2> axis_lines;
11✔
1755
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
33✔
1756
    int axis = axes[i_ax];
22✔
1757
    vector<double>& lines {axis_lines[i_ax]};
22✔
1758

1759
    for (auto coord : grid_[axis]) {
110✔
1760
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
88!
1761
        lines.push_back(coord);
88✔
1762
    }
1763
  }
1764

1765
  return {axis_lines[0], axis_lines[1]};
22✔
1766
}
11✔
1767

1768
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
110✔
1769
{
1770
  write_dataset(mesh_group, "x_grid", grid_[0]);
110✔
1771
  write_dataset(mesh_group, "y_grid", grid_[1]);
110✔
1772
  write_dataset(mesh_group, "z_grid", grid_[2]);
110✔
1773
}
110✔
1774

1775
double RectilinearMesh::volume(const MeshIndex& ijk) const
132✔
1776
{
1777
  double vol {1.0};
132✔
1778

1779
  for (int i = 0; i < n_dimension_; i++) {
528✔
1780
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
396✔
1781
  }
1782
  return vol;
132✔
1783
}
1784

1785
//==============================================================================
1786
// CylindricalMesh implementation
1787
//==============================================================================
1788

1789
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
401✔
1790
  : PeriodicStructuredMesh {node}
401✔
1791
{
1792
  n_dimension_ = 3;
401✔
1793
  grid_[0] = get_node_array<double>(node, "r_grid");
401✔
1794
  grid_[1] = get_node_array<double>(node, "phi_grid");
401✔
1795
  grid_[2] = get_node_array<double>(node, "z_grid");
401✔
1796
  origin_ = get_node_position(node, "origin");
401✔
1797

1798
  if (int err = set_grid()) {
401!
UNCOV
1799
    fatal_error(openmc_err_msg);
×
1800
  }
1801
}
401✔
1802

1803
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1804
{
1805
  n_dimension_ = 3;
11✔
1806
  read_dataset(group, "r_grid", grid_[0]);
11✔
1807
  read_dataset(group, "phi_grid", grid_[1]);
11✔
1808
  read_dataset(group, "z_grid", grid_[2]);
11✔
1809
  read_dataset(group, "origin", origin_);
11✔
1810

1811
  if (int err = set_grid()) {
11!
UNCOV
1812
    fatal_error(openmc_err_msg);
×
1813
  }
1814
}
11✔
1815

1816
const std::string CylindricalMesh::mesh_type = "cylindrical";
1817

1818
std::string CylindricalMesh::get_mesh_type() const
484✔
1819
{
1820
  return mesh_type;
484✔
1821
}
1822

1823
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
47,726,668✔
1824
  Position r, bool& in_mesh) const
1825
{
1826
  r = local_coords(r);
47,726,668✔
1827

1828
  Position mapped_r;
47,726,668✔
1829
  mapped_r[0] = std::hypot(r.x, r.y);
47,726,668✔
1830
  mapped_r[2] = r[2];
47,726,668✔
1831

1832
  if (mapped_r[0] < FP_PRECISION) {
47,726,668!
UNCOV
1833
    mapped_r[1] = 0.0;
×
1834
  } else {
1835
    mapped_r[1] = std::atan2(r.y, r.x);
47,726,668✔
1836
    if (mapped_r[1] < 0)
47,726,668✔
1837
      mapped_r[1] += 2 * M_PI;
23,872,431✔
1838
  }
1839

1840
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
47,726,668✔
1841

1842
  idx[1] = sanitize_phi(idx[1]);
47,726,668✔
1843

1844
  return idx;
47,726,668✔
1845
}
1846

1847
Position CylindricalMesh::sample_element(
88,110✔
1848
  const MeshIndex& ijk, uint64_t* seed) const
1849
{
1850
  double r_min = this->r(ijk[0] - 1);
88,110✔
1851
  double r_max = this->r(ijk[0]);
88,110✔
1852

1853
  double phi_min = this->phi(ijk[1] - 1);
88,110✔
1854
  double phi_max = this->phi(ijk[1]);
88,110✔
1855

1856
  double z_min = this->z(ijk[2] - 1);
88,110✔
1857
  double z_max = this->z(ijk[2]);
88,110✔
1858

1859
  double r_min_sq = r_min * r_min;
88,110✔
1860
  double r_max_sq = r_max * r_max;
88,110✔
1861
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
88,110✔
1862
  double phi = uniform_distribution(phi_min, phi_max, seed);
88,110✔
1863
  double z = uniform_distribution(z_min, z_max, seed);
88,110✔
1864

1865
  double x = r * std::cos(phi);
88,110✔
1866
  double y = r * std::sin(phi);
88,110✔
1867

1868
  return origin_ + Position(x, y, z);
88,110✔
1869
}
1870

1871
double CylindricalMesh::find_r_crossing(
142,568,516✔
1872
  const Position& r, const Direction& u, double l, int shell) const
1873
{
1874

1875
  if ((shell < 0) || (shell > shape_[0]))
142,568,516!
1876
    return INFTY;
17,913,907✔
1877

1878
  // solve r.x^2 + r.y^2 == r0^2
1879
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1880
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1881

1882
  const double r0 = grid_[0][shell];
124,654,609✔
1883
  if (r0 == 0.0)
124,654,609✔
1884
    return INFTY;
7,130,651✔
1885

1886
  const double denominator = u.x * u.x + u.y * u.y;
117,523,958✔
1887

1888
  // Direction of flight is in z-direction. Will never intersect r.
1889
  if (std::abs(denominator) < FP_PRECISION)
117,523,958✔
1890
    return INFTY;
58,960✔
1891

1892
  // inverse of dominator to help the compiler to speed things up
1893
  const double inv_denominator = 1.0 / denominator;
117,464,998✔
1894

1895
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,464,998✔
1896
  double c = r.x * r.x + r.y * r.y - r0 * r0;
117,464,998✔
1897
  double D = p * p - c * inv_denominator;
117,464,998✔
1898

1899
  if (D < 0.0)
117,464,998✔
1900
    return INFTY;
9,733,570✔
1901

1902
  D = std::sqrt(D);
107,731,428✔
1903

1904
  // the solution -p - D is always smaller as -p + D : Check this one first
1905
  if (std::abs(c) <= RADIAL_MESH_TOL)
107,731,428✔
1906
    return INFTY;
6,611,374✔
1907

1908
  if (-p - D > l)
101,120,054✔
1909
    return -p - D;
20,205,998✔
1910
  if (-p + D > l)
80,914,056✔
1911
    return -p + D;
50,090,580✔
1912

1913
  return INFTY;
30,823,476✔
1914
}
1915

1916
double CylindricalMesh::find_phi_crossing(
74,445,558✔
1917
  const Position& r, const Direction& u, double l, int shell) const
1918
{
1919
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1920
  if (full_phi_ && (shape_[1] == 1))
74,445,558✔
1921
    return INFTY;
30,474,840✔
1922

1923
  shell = sanitize_phi(shell);
43,970,718✔
1924

1925
  const double p0 = grid_[1][shell];
43,970,718✔
1926

1927
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1928
  // => x(s) * cos(p0) = y(s) * sin(p0)
1929
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1930
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1931

1932
  const double c0 = std::cos(p0);
43,970,718✔
1933
  const double s0 = std::sin(p0);
43,970,718✔
1934

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

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

1946
  return INFTY;
23,750,859✔
1947
}
1948

1949
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
36,690,324✔
1950
  const Position& r, const Direction& u, double l, int shell) const
1951
{
1952
  MeshDistance d;
36,690,324✔
1953
  d.next_index = shell;
36,690,324✔
1954

1955
  // Direction of flight is within xy-plane. Will never intersect z.
1956
  if (std::abs(u.z) < FP_PRECISION)
36,690,324✔
1957
    return d;
1,118,216✔
1958

1959
  d.max_surface = (u.z > 0.0);
35,572,108✔
1960
  if (d.max_surface && (shell <= shape_[2])) {
35,572,108✔
1961
    d.next_index += 1;
16,873,241✔
1962
    d.distance = (grid_[2][shell] - r.z) / u.z;
16,873,241✔
1963
  } else if (!d.max_surface && (shell > 0)) {
18,698,867✔
1964
    d.next_index -= 1;
16,843,453✔
1965
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
16,843,453✔
1966
  }
1967
  return d;
35,572,108✔
1968
}
1969

1970
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,197,361✔
1971
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1972
  double l) const
1973
{
1974
  if (i == 0) {
145,197,361✔
1975

1976
    return std::min(
71,284,258✔
1977
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
71,284,258✔
1978
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,568,516✔
1979

1980
  } else if (i == 1) {
73,913,103✔
1981

1982
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
37,222,779✔
1983
                      find_phi_crossing(r0, u, l, ijk[i])),
37,222,779✔
1984
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
37,222,779✔
1985
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
74,445,558✔
1986

1987
  } else {
1988
    return find_z_crossing(r0, u, l, ijk[i]);
36,690,324✔
1989
  }
1990
}
1991

1992
int CylindricalMesh::set_grid()
434✔
1993
{
1994
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
434✔
1995
    static_cast<int>(grid_[1].size()) - 1,
434✔
1996
    static_cast<int>(grid_[2].size()) - 1};
434✔
1997

1998
  for (const auto& g : grid_) {
1,736✔
1999
    if (g.size() < 2) {
1,302!
UNCOV
2000
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
2001
                 "must each have at least 2 points");
UNCOV
2002
      return OPENMC_E_INVALID_ARGUMENT;
×
2003
    }
2004
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,302✔
2005
        g.end()) {
2,604!
UNCOV
2006
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
2007
                 "cylindrical meshes must be sorted and unique.");
UNCOV
2008
      return OPENMC_E_INVALID_ARGUMENT;
×
2009
    }
2010
  }
2011
  if (grid_[0].front() < 0.0) {
434!
UNCOV
2012
    set_errmsg("r-grid for "
×
2013
               "cylindrical meshes must start at r >= 0.");
UNCOV
2014
    return OPENMC_E_INVALID_ARGUMENT;
×
2015
  }
2016
  if (grid_[1].front() < 0.0) {
434!
UNCOV
2017
    set_errmsg("phi-grid for "
×
2018
               "cylindrical meshes must start at phi >= 0.");
UNCOV
2019
    return OPENMC_E_INVALID_ARGUMENT;
×
2020
  }
2021
  if (grid_[1].back() > 2.0 * PI) {
434!
UNCOV
2022
    set_errmsg("phi-grids for "
×
2023
               "cylindrical meshes must end with theta <= 2*pi.");
2024

UNCOV
2025
    return OPENMC_E_INVALID_ARGUMENT;
×
2026
  }
2027

2028
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
434!
2029

2030
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
868✔
2031
    origin_[2] + grid_[2].front()};
868✔
2032
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
868✔
2033
    origin_[2] + grid_[2].back()};
868✔
2034

2035
  return 0;
434✔
2036
}
2037

2038
int CylindricalMesh::get_index_in_direction(double r, int i) const
143,180,004✔
2039
{
2040
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
143,180,004✔
2041
}
2042

UNCOV
2043
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
2044
  Position plot_ll, Position plot_ur) const
2045
{
UNCOV
2046
  fatal_error("Plot of cylindrical Mesh not implemented");
×
2047

2048
  // Figure out which axes lie in the plane of the plot.
2049
  array<vector<double>, 2> axis_lines;
2050
  return {axis_lines[0], axis_lines[1]};
2051
}
2052

2053
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
374✔
2054
{
2055
  write_dataset(mesh_group, "r_grid", grid_[0]);
374✔
2056
  write_dataset(mesh_group, "phi_grid", grid_[1]);
374✔
2057
  write_dataset(mesh_group, "z_grid", grid_[2]);
374✔
2058
  write_dataset(mesh_group, "origin", origin_);
374✔
2059
}
374✔
2060

2061
double CylindricalMesh::volume(const MeshIndex& ijk) const
792✔
2062
{
2063
  double r_i = grid_[0][ijk[0] - 1];
792✔
2064
  double r_o = grid_[0][ijk[0]];
792✔
2065

2066
  double phi_i = grid_[1][ijk[1] - 1];
792✔
2067
  double phi_o = grid_[1][ijk[1]];
792✔
2068

2069
  double z_i = grid_[2][ijk[2] - 1];
792✔
2070
  double z_o = grid_[2][ijk[2]];
792✔
2071

2072
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
792✔
2073
}
2074

2075
//==============================================================================
2076
// SphericalMesh implementation
2077
//==============================================================================
2078

2079
SphericalMesh::SphericalMesh(pugi::xml_node node)
346✔
2080
  : PeriodicStructuredMesh {node}
346✔
2081
{
2082
  n_dimension_ = 3;
346✔
2083

2084
  grid_[0] = get_node_array<double>(node, "r_grid");
346✔
2085
  grid_[1] = get_node_array<double>(node, "theta_grid");
346✔
2086
  grid_[2] = get_node_array<double>(node, "phi_grid");
346✔
2087
  origin_ = get_node_position(node, "origin");
346✔
2088

2089
  if (int err = set_grid()) {
346!
UNCOV
2090
    fatal_error(openmc_err_msg);
×
2091
  }
2092
}
346✔
2093

2094
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
2095
{
2096
  n_dimension_ = 3;
11✔
2097

2098
  read_dataset(group, "r_grid", grid_[0]);
11✔
2099
  read_dataset(group, "theta_grid", grid_[1]);
11✔
2100
  read_dataset(group, "phi_grid", grid_[2]);
11✔
2101
  read_dataset(group, "origin", origin_);
11✔
2102

2103
  if (int err = set_grid()) {
11!
UNCOV
2104
    fatal_error(openmc_err_msg);
×
2105
  }
2106
}
11✔
2107

2108
const std::string SphericalMesh::mesh_type = "spherical";
2109

2110
std::string SphericalMesh::get_mesh_type() const
385✔
2111
{
2112
  return mesh_type;
385✔
2113
}
2114

2115
StructuredMesh::MeshIndex SphericalMesh::get_indices(
68,175,250✔
2116
  Position r, bool& in_mesh) const
2117
{
2118
  r = local_coords(r);
68,175,250✔
2119

2120
  Position mapped_r;
68,175,250✔
2121
  mapped_r[0] = r.norm();
68,175,250✔
2122

2123
  if (mapped_r[0] < FP_PRECISION) {
68,175,250!
UNCOV
2124
    mapped_r[1] = 0.0;
×
UNCOV
2125
    mapped_r[2] = 0.0;
×
2126
  } else {
2127
    mapped_r[1] = std::acos(r.z / mapped_r.x);
68,175,250✔
2128
    mapped_r[2] = std::atan2(r.y, r.x);
68,175,250✔
2129
    if (mapped_r[2] < 0)
68,175,250✔
2130
      mapped_r[2] += 2 * M_PI;
34,062,281✔
2131
  }
2132

2133
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
68,175,250✔
2134

2135
  idx[1] = sanitize_theta(idx[1]);
68,175,250✔
2136
  idx[2] = sanitize_phi(idx[2]);
68,175,250✔
2137

2138
  return idx;
68,175,250✔
2139
}
2140

2141
Position SphericalMesh::sample_element(
110✔
2142
  const MeshIndex& ijk, uint64_t* seed) const
2143
{
2144
  double r_min = this->r(ijk[0] - 1);
110✔
2145
  double r_max = this->r(ijk[0]);
110✔
2146

2147
  double theta_min = this->theta(ijk[1] - 1);
110✔
2148
  double theta_max = this->theta(ijk[1]);
110✔
2149

2150
  double phi_min = this->phi(ijk[2] - 1);
110✔
2151
  double phi_max = this->phi(ijk[2]);
110✔
2152

2153
  double cos_theta =
2154
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
110✔
2155
  double sin_theta = std::sin(std::acos(cos_theta));
110✔
2156
  double phi = uniform_distribution(phi_min, phi_max, seed);
110✔
2157
  double r_min_cub = std::pow(r_min, 3);
110✔
2158
  double r_max_cub = std::pow(r_max, 3);
110✔
2159
  // might be faster to do rejection here?
2160
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110✔
2161

2162
  double x = r * std::cos(phi) * sin_theta;
110✔
2163
  double y = r * std::sin(phi) * sin_theta;
110✔
2164
  double z = r * cos_theta;
110✔
2165

2166
  return origin_ + Position(x, y, z);
110✔
2167
}
2168

2169
double SphericalMesh::find_r_crossing(
443,080,484✔
2170
  const Position& r, const Direction& u, double l, int shell) const
2171
{
2172
  if ((shell < 0) || (shell > shape_[0]))
443,080,484✔
2173
    return INFTY;
39,620,317✔
2174

2175
  // solve |r+s*u| = r0
2176
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2177
  const double r0 = grid_[0][shell];
403,460,167✔
2178
  if (r0 == 0.0)
403,460,167✔
2179
    return INFTY;
7,261,639✔
2180
  const double p = r.dot(u);
396,198,528✔
2181
  double c = r.dot(r) - r0 * r0;
396,198,528✔
2182
  double D = p * p - c;
396,198,528✔
2183

2184
  if (std::abs(c) <= RADIAL_MESH_TOL)
396,198,528✔
2185
    return INFTY;
10,598,654✔
2186

2187
  if (D >= 0.0) {
385,599,874✔
2188
    D = std::sqrt(D);
357,722,926✔
2189
    // the solution -p - D is always smaller as -p + D : Check this one first
2190
    if (-p - D > l)
357,722,926✔
2191
      return -p - D;
64,279,325✔
2192
    if (-p + D > l)
293,443,601✔
2193
      return -p + D;
176,902,198✔
2194
  }
2195

2196
  return INFTY;
144,418,351✔
2197
}
2198

2199
double SphericalMesh::find_theta_crossing(
109,327,592✔
2200
  const Position& r, const Direction& u, double l, int shell) const
2201
{
2202
  // Theta grid is [0, π], thus there is no real surface to cross
2203
  if (full_theta_ && (shape_[1] == 1))
109,327,592✔
2204
    return INFTY;
70,969,052✔
2205

2206
  shell = sanitize_theta(shell);
38,358,540✔
2207

2208
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2209
  // yields
2210
  // a*s^2 + 2*b*s + c == 0 with
2211
  // a = cos(theta)^2 - u.z * u.z
2212
  // b = r*u * cos(theta)^2 - u.z * r.z
2213
  // c = r*r * cos(theta)^2 - r.z^2
2214

2215
  const double cos_t = std::cos(grid_[1][shell]);
38,358,540✔
2216
  const bool sgn = std::signbit(cos_t);
38,358,540✔
2217
  const double cos_t_2 = cos_t * cos_t;
38,358,540✔
2218

2219
  const double a = cos_t_2 - u.z * u.z;
38,358,540✔
2220
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
38,358,540✔
2221
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
38,358,540✔
2222

2223
  // if factor of s^2 is zero, direction of flight is parallel to theta
2224
  // surface
2225
  if (std::abs(a) < FP_PRECISION) {
38,358,540✔
2226
    // if b vanishes, direction of flight is within theta surface and crossing
2227
    // is not possible
2228
    if (std::abs(b) < FP_PRECISION)
482,548!
2229
      return INFTY;
482,548✔
2230

UNCOV
2231
    const double s = -0.5 * c / b;
×
2232
    // Check if solution is in positive direction of flight and has correct
2233
    // sign
UNCOV
2234
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
UNCOV
2235
      return s;
×
2236

2237
    // no crossing is possible
UNCOV
2238
    return INFTY;
×
2239
  }
2240

2241
  const double p = b / a;
37,875,992✔
2242
  double D = p * p - c / a;
37,875,992✔
2243

2244
  if (D < 0.0)
37,875,992✔
2245
    return INFTY;
10,954,988✔
2246

2247
  D = std::sqrt(D);
26,921,004✔
2248

2249
  // the solution -p-D is always smaller as -p+D : Check this one first
2250
  double s = -p - D;
26,921,004✔
2251
  // Check if solution is in positive direction of flight and has correct sign
2252
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
26,921,004✔
2253
    return s;
5,282,607✔
2254

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

2260
  return INFTY;
11,475,101✔
2261
}
2262

2263
double SphericalMesh::find_phi_crossing(
110,917,070✔
2264
  const Position& r, const Direction& u, double l, int shell) const
2265
{
2266
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2267
  if (full_phi_ && (shape_[2] == 1))
110,917,070✔
2268
    return INFTY;
70,969,052✔
2269

2270
  shell = sanitize_phi(shell);
39,948,018✔
2271

2272
  const double p0 = grid_[2][shell];
39,948,018✔
2273

2274
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2275
  // => x(s) * cos(p0) = y(s) * sin(p0)
2276
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2277
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2278

2279
  const double c0 = std::cos(p0);
39,948,018✔
2280
  const double s0 = std::sin(p0);
39,948,018✔
2281

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

2284
  // Check if direction of flight is not parallel to phi surface
2285
  if (std::abs(denominator) > FP_PRECISION) {
39,948,018✔
2286
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,714,026✔
2287
    // Check if solution is in positive direction of flight and crosses the
2288
    // correct phi surface (not -phi)
2289
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,714,026✔
2290
      return s;
17,579,452✔
2291
  }
2292

2293
  return INFTY;
22,368,566✔
2294
}
2295

2296
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
331,662,573✔
2297
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2298
  double l) const
2299
{
2300

2301
  if (i == 0) {
331,662,573✔
2302
    return std::min(
221,540,242✔
2303
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
221,540,242✔
2304
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
443,080,484✔
2305

2306
  } else if (i == 1) {
110,122,331✔
2307
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
54,663,796✔
2308
                      find_theta_crossing(r0, u, l, ijk[i])),
54,663,796✔
2309
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
54,663,796✔
2310
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
109,327,592✔
2311

2312
  } else {
2313
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
55,458,535✔
2314
                      find_phi_crossing(r0, u, l, ijk[i])),
55,458,535✔
2315
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
55,458,535✔
2316
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
110,917,070✔
2317
  }
2318
}
2319

2320
int SphericalMesh::set_grid()
379✔
2321
{
2322
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
379✔
2323
    static_cast<int>(grid_[1].size()) - 1,
379✔
2324
    static_cast<int>(grid_[2].size()) - 1};
379✔
2325

2326
  for (const auto& g : grid_) {
1,516✔
2327
    if (g.size() < 2) {
1,137!
2328
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2329
                 "must each have at least 2 points");
UNCOV
2330
      return OPENMC_E_INVALID_ARGUMENT;
×
2331
    }
2332
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,137✔
2333
        g.end()) {
2,274!
UNCOV
2334
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2335
                 "spherical meshes must be sorted and unique.");
UNCOV
2336
      return OPENMC_E_INVALID_ARGUMENT;
×
2337
    }
2338
    if (g.front() < 0.0) {
1,137!
UNCOV
2339
      set_errmsg("r-, theta-, and phi- grids for "
×
2340
                 "spherical meshes must start at v >= 0.");
UNCOV
2341
      return OPENMC_E_INVALID_ARGUMENT;
×
2342
    }
2343
  }
2344
  if (grid_[1].back() > PI) {
379!
2345
    set_errmsg("theta-grids for "
×
2346
               "spherical meshes must end with theta <= pi.");
2347

UNCOV
2348
    return OPENMC_E_INVALID_ARGUMENT;
×
2349
  }
2350
  if (grid_[2].back() > 2 * PI) {
379!
UNCOV
2351
    set_errmsg("phi-grids for "
×
2352
               "spherical meshes must end with phi <= 2*pi.");
2353
    return OPENMC_E_INVALID_ARGUMENT;
×
2354
  }
2355

2356
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
379!
2357
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
379✔
2358

2359
  double r = grid_[0].back();
379✔
2360
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
379✔
2361
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
379✔
2362

2363
  return 0;
379✔
2364
}
2365

2366
int SphericalMesh::get_index_in_direction(double r, int i) const
204,525,750✔
2367
{
2368
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
204,525,750✔
2369
}
2370

UNCOV
2371
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2372
  Position plot_ll, Position plot_ur) const
2373
{
UNCOV
2374
  fatal_error("Plot of spherical Mesh not implemented");
×
2375

2376
  // Figure out which axes lie in the plane of the plot.
2377
  array<vector<double>, 2> axis_lines;
2378
  return {axis_lines[0], axis_lines[1]};
2379
}
2380

2381
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
319✔
2382
{
2383
  write_dataset(mesh_group, "r_grid", grid_[0]);
319✔
2384
  write_dataset(mesh_group, "theta_grid", grid_[1]);
319✔
2385
  write_dataset(mesh_group, "phi_grid", grid_[2]);
319✔
2386
  write_dataset(mesh_group, "origin", origin_);
319✔
2387
}
319✔
2388

2389
double SphericalMesh::volume(const MeshIndex& ijk) const
935✔
2390
{
2391
  double r_i = grid_[0][ijk[0] - 1];
935✔
2392
  double r_o = grid_[0][ijk[0]];
935✔
2393

2394
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2395
  double theta_o = grid_[1][ijk[1]];
935✔
2396

2397
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2398
  double phi_o = grid_[2][ijk[2]];
935✔
2399

2400
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
935✔
2401
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
935✔
2402
}
2403

2404
//==============================================================================
2405
// Helper functions for the C API
2406
//==============================================================================
2407

2408
int check_mesh(int32_t index)
6,380✔
2409
{
2410
  if (index < 0 || index >= model::meshes.size()) {
6,380!
UNCOV
2411
    set_errmsg("Index in meshes array is out of bounds.");
×
UNCOV
2412
    return OPENMC_E_OUT_OF_BOUNDS;
×
2413
  }
2414
  return 0;
6,380✔
2415
}
2416

2417
template<class T>
2418
int check_mesh_type(int32_t index)
1,100✔
2419
{
2420
  if (int err = check_mesh(index))
1,100!
UNCOV
2421
    return err;
×
2422

2423
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,100!
2424
  if (!mesh) {
1,100!
UNCOV
2425
    set_errmsg("This function is not valid for input mesh.");
×
UNCOV
2426
    return OPENMC_E_INVALID_TYPE;
×
2427
  }
2428
  return 0;
1,100✔
2429
}
2430

2431
template<class T>
2432
bool is_mesh_type(int32_t index)
2433
{
2434
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2435
  return mesh;
2436
}
2437

2438
//==============================================================================
2439
// C API functions
2440
//==============================================================================
2441

2442
// Return the type of mesh as a C string
2443
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,474✔
2444
{
2445
  if (int err = check_mesh(index))
1,474!
UNCOV
2446
    return err;
×
2447

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

2450
  return 0;
1,474✔
2451
}
2452

2453
//! Extend the meshes array by n elements
2454
extern "C" int openmc_extend_meshes(
253✔
2455
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2456
{
2457
  if (index_start)
253!
2458
    *index_start = model::meshes.size();
253✔
2459
  std::string mesh_type;
253✔
2460

2461
  for (int i = 0; i < n; ++i) {
506✔
2462
    if (RegularMesh::mesh_type == type) {
253✔
2463
      model::meshes.push_back(make_unique<RegularMesh>());
165✔
2464
    } else if (RectilinearMesh::mesh_type == type) {
88✔
2465
      model::meshes.push_back(make_unique<RectilinearMesh>());
44✔
2466
    } else if (CylindricalMesh::mesh_type == type) {
44✔
2467
      model::meshes.push_back(make_unique<CylindricalMesh>());
22✔
2468
    } else if (SphericalMesh::mesh_type == type) {
22!
2469
      model::meshes.push_back(make_unique<SphericalMesh>());
22✔
2470
    } else {
UNCOV
2471
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2472
    }
2473
  }
2474
  if (index_end)
253!
2475
    *index_end = model::meshes.size() - 1;
×
2476

2477
  return 0;
253✔
2478
}
253✔
2479

2480
//! Adds a new unstructured mesh to OpenMC
2481
extern "C" int openmc_add_unstructured_mesh(
×
2482
  const char filename[], const char library[], int* id)
2483
{
2484
  std::string lib_name(library);
×
UNCOV
2485
  std::string mesh_file(filename);
×
UNCOV
2486
  bool valid_lib = false;
×
2487

2488
#ifdef OPENMC_DAGMC_ENABLED
2489
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2490
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2491
    valid_lib = true;
2492
  }
2493
#endif
2494

2495
#ifdef OPENMC_LIBMESH_ENABLED
2496
  if (lib_name == LibMesh::mesh_lib_type) {
×
2497
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2498
    valid_lib = true;
2499
  }
2500
#endif
2501

UNCOV
2502
  if (!valid_lib) {
×
UNCOV
2503
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2504
                           "by this build of OpenMC",
2505
      lib_name));
UNCOV
2506
    return OPENMC_E_INVALID_ARGUMENT;
×
2507
  }
2508

2509
  // auto-assign new ID
UNCOV
2510
  model::meshes.back()->set_id(-1);
×
UNCOV
2511
  *id = model::meshes.back()->id_;
×
2512

2513
  return 0;
×
UNCOV
2514
}
×
2515

2516
//! Return the index in the meshes array of a mesh with a given ID
2517
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
429✔
2518
{
2519
  auto pair = model::mesh_map.find(id);
429✔
2520
  if (pair == model::mesh_map.end()) {
429!
UNCOV
2521
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
UNCOV
2522
    return OPENMC_E_INVALID_ID;
×
2523
  }
2524
  *index = pair->second;
429✔
2525
  return 0;
429✔
2526
}
2527

2528
//! Return the ID of a mesh
2529
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,805✔
2530
{
2531
  if (int err = check_mesh(index))
2,805!
UNCOV
2532
    return err;
×
2533
  *id = model::meshes[index]->id_;
2,805✔
2534
  return 0;
2,805✔
2535
}
2536

2537
//! Set the ID of a mesh
2538
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
253✔
2539
{
2540
  if (int err = check_mesh(index))
253!
UNCOV
2541
    return err;
×
2542
  model::meshes[index]->id_ = id;
253✔
2543
  model::mesh_map[id] = index;
253✔
2544
  return 0;
253✔
2545
}
2546

2547
//! Get the number of elements in a mesh
2548
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
275✔
2549
{
2550
  if (int err = check_mesh(index))
275!
UNCOV
2551
    return err;
×
2552
  *n = model::meshes[index]->n_bins();
275✔
2553
  return 0;
275✔
2554
}
2555

2556
//! Get the volume of each element in the mesh
2557
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
88✔
2558
{
2559
  if (int err = check_mesh(index))
88!
UNCOV
2560
    return err;
×
2561
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
968✔
2562
    volumes[i] = model::meshes[index]->volume(i);
880✔
2563
  }
2564
  return 0;
88✔
2565
}
2566

2567
//! Get the bounding box of a mesh
2568
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
154✔
2569
{
2570
  if (int err = check_mesh(index))
154!
UNCOV
2571
    return err;
×
2572

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

2575
  // set lower left corner values
2576
  ll[0] = bbox.min.x;
154✔
2577
  ll[1] = bbox.min.y;
154✔
2578
  ll[2] = bbox.min.z;
154✔
2579

2580
  // set upper right corner values
2581
  ur[0] = bbox.max.x;
154✔
2582
  ur[1] = bbox.max.y;
154✔
2583
  ur[2] = bbox.max.z;
154✔
2584
  return 0;
154✔
2585
}
2586

2587
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
187✔
2588
  int nz, int table_size, int32_t* materials, double* volumes, double* bboxes)
2589
{
2590
  if (int err = check_mesh(index))
187!
UNCOV
2591
    return err;
×
2592

2593
  try {
2594
    model::meshes[index]->material_volumes(
187✔
2595
      nx, ny, nz, table_size, materials, volumes, bboxes);
2596
  } catch (const std::exception& e) {
11!
2597
    set_errmsg(e.what());
11✔
2598
    if (starts_with(e.what(), "Mesh")) {
11!
2599
      return OPENMC_E_GEOMETRY;
11✔
2600
    } else {
UNCOV
2601
      return OPENMC_E_ALLOCATE;
×
2602
    }
2603
  }
11✔
2604

2605
  return 0;
176✔
2606
}
2607

2608
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
44✔
2609
  Position width, int basis, int* pixels, int32_t* data)
2610
{
2611
  if (int err = check_mesh(index))
44!
UNCOV
2612
    return err;
×
2613
  const auto& mesh = model::meshes[index].get();
44✔
2614

2615
  int pixel_width = pixels[0];
44✔
2616
  int pixel_height = pixels[1];
44✔
2617

2618
  // get pixel size
2619
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
44✔
2620
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
44✔
2621

2622
  // setup basis indices and initial position centered on pixel
2623
  int in_i, out_i;
2624
  Position xyz = origin;
44✔
2625
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2626
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
44✔
2627
  switch (basis_enum) {
44!
2628
  case PlotBasis::xy:
44✔
2629
    in_i = 0;
44✔
2630
    out_i = 1;
44✔
2631
    break;
44✔
UNCOV
2632
  case PlotBasis::xz:
×
UNCOV
2633
    in_i = 0;
×
UNCOV
2634
    out_i = 2;
×
2635
    break;
×
UNCOV
2636
  case PlotBasis::yz:
×
UNCOV
2637
    in_i = 1;
×
UNCOV
2638
    out_i = 2;
×
2639
    break;
×
2640
  default:
×
UNCOV
2641
    UNREACHABLE();
×
2642
  }
2643

2644
  // set initial position
2645
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
44✔
2646
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
44✔
2647

2648
#pragma omp parallel
24✔
2649
  {
2650
    Position r = xyz;
20✔
2651

2652
#pragma omp for
2653
    for (int y = 0; y < pixel_height; y++) {
420✔
2654
      r[out_i] = xyz[out_i] - out_pixel * y;
400✔
2655
      for (int x = 0; x < pixel_width; x++) {
8,400✔
2656
        r[in_i] = xyz[in_i] + in_pixel * x;
8,000✔
2657
        data[pixel_width * y + x] = mesh->get_bin(r);
8,000✔
2658
      }
2659
    }
2660
  }
2661

2662
  return 0;
44✔
2663
}
2664

2665
//! Get the dimension of a regular mesh
2666
extern "C" int openmc_regular_mesh_get_dimension(
11✔
2667
  int32_t index, int** dims, int* n)
2668
{
2669
  if (int err = check_mesh_type<RegularMesh>(index))
11!
UNCOV
2670
    return err;
×
2671
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
11!
2672
  *dims = mesh->shape_.data();
11✔
2673
  *n = mesh->n_dimension_;
11✔
2674
  return 0;
11✔
2675
}
2676

2677
//! Set the dimension of a regular mesh
2678
extern "C" int openmc_regular_mesh_set_dimension(
187✔
2679
  int32_t index, int n, const int* dims)
2680
{
2681
  if (int err = check_mesh_type<RegularMesh>(index))
187!
UNCOV
2682
    return err;
×
2683
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
187!
2684

2685
  // Copy dimension
2686
  mesh->n_dimension_ = n;
187✔
2687
  std::copy(dims, dims + n, mesh->shape_.begin());
187✔
2688
  return 0;
187✔
2689
}
2690

2691
//! Get the regular mesh parameters
2692
extern "C" int openmc_regular_mesh_get_params(
209✔
2693
  int32_t index, double** ll, double** ur, double** width, int* n)
2694
{
2695
  if (int err = check_mesh_type<RegularMesh>(index))
209!
UNCOV
2696
    return err;
×
2697
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
209!
2698

2699
  if (m->lower_left_.dimension() == 0) {
209!
UNCOV
2700
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2701
    return OPENMC_E_ALLOCATE;
×
2702
  }
2703

2704
  *ll = m->lower_left_.data();
209✔
2705
  *ur = m->upper_right_.data();
209✔
2706
  *width = m->width_.data();
209✔
2707
  *n = m->n_dimension_;
209✔
2708
  return 0;
209✔
2709
}
2710

2711
//! Set the regular mesh parameters
2712
extern "C" int openmc_regular_mesh_set_params(
220✔
2713
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2714
{
2715
  if (int err = check_mesh_type<RegularMesh>(index))
220!
UNCOV
2716
    return err;
×
2717
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
220!
2718

2719
  if (m->n_dimension_ == -1) {
220!
UNCOV
2720
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
UNCOV
2721
    return OPENMC_E_UNASSIGNED;
×
2722
  }
2723

2724
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
220✔
2725
  if (ll && ur) {
220✔
2726
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
198✔
2727
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
198✔
2728
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
198✔
2729
  } else if (ll && width) {
22!
2730
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
11✔
2731
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
11✔
2732
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
11✔
2733
  } else if (ur && width) {
11!
2734
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
11✔
2735
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
11✔
2736
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
11✔
2737
  } else {
UNCOV
2738
    set_errmsg("At least two parameters must be specified.");
×
UNCOV
2739
    return OPENMC_E_INVALID_ARGUMENT;
×
2740
  }
2741

2742
  // Set material volumes
2743

2744
  // TODO: incorporate this into method in RegularMesh that can be called from
2745
  // here and from constructor
2746
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
220✔
2747
  m->element_volume_ = 1.0;
220✔
2748
  for (int i = 0; i < m->n_dimension_; i++) {
880✔
2749
    m->element_volume_ *= m->width_[i];
660✔
2750
  }
2751

2752
  return 0;
220✔
2753
}
220✔
2754

2755
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2756
template<class C>
2757
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
88✔
2758
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2759
  const int nz)
2760
{
2761
  if (int err = check_mesh_type<C>(index))
88!
UNCOV
2762
    return err;
×
2763

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

2766
  m->n_dimension_ = 3;
88✔
2767

2768
  m->grid_[0].reserve(nx);
88✔
2769
  m->grid_[1].reserve(ny);
88✔
2770
  m->grid_[2].reserve(nz);
88✔
2771

2772
  for (int i = 0; i < nx; i++) {
572✔
2773
    m->grid_[0].push_back(grid_x[i]);
484✔
2774
  }
2775
  for (int i = 0; i < ny; i++) {
341✔
2776
    m->grid_[1].push_back(grid_y[i]);
253✔
2777
  }
2778
  for (int i = 0; i < nz; i++) {
319✔
2779
    m->grid_[2].push_back(grid_z[i]);
231✔
2780
  }
2781

2782
  int err = m->set_grid();
88✔
2783
  return err;
88✔
2784
}
2785

2786
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2787
template<class C>
2788
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
385✔
2789
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2790
{
2791
  if (int err = check_mesh_type<C>(index))
385!
UNCOV
2792
    return err;
×
2793
  C* m = dynamic_cast<C*>(model::meshes[index].get());
385!
2794

2795
  if (m->lower_left_.dimension() == 0) {
385!
UNCOV
2796
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2797
    return OPENMC_E_ALLOCATE;
×
2798
  }
2799

2800
  *grid_x = m->grid_[0].data();
385✔
2801
  *nx = m->grid_[0].size();
385✔
2802
  *grid_y = m->grid_[1].data();
385✔
2803
  *ny = m->grid_[1].size();
385✔
2804
  *grid_z = m->grid_[2].data();
385✔
2805
  *nz = m->grid_[2].size();
385✔
2806

2807
  return 0;
385✔
2808
}
2809

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

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

2827
//! Get the cylindrical mesh grid
2828
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2829
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2830
{
2831
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
121✔
2832
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2833
}
2834

2835
//! Set the cylindrical mesh parameters
2836
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
22✔
2837
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2838
  const double* grid_z, const int nz)
2839
{
2840
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
22✔
2841
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2842
}
2843

2844
//! Get the spherical mesh grid
2845
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2846
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2847
{
2848

2849
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
121✔
2850
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2851
  ;
2852
}
2853

2854
//! Set the spherical mesh parameters
2855
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
22✔
2856
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2857
  const double* grid_z, const int nz)
2858
{
2859
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
22✔
2860
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2861
}
2862

2863
#ifdef OPENMC_DAGMC_ENABLED
2864

2865
const std::string MOABMesh::mesh_lib_type = "moab";
2866

2867
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
2868
{
2869
  initialize();
24✔
2870
}
24✔
2871

2872
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
2873
{
2874
  initialize();
×
2875
}
2876

2877
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2878
  : UnstructuredMesh()
×
2879
{
2880
  n_dimension_ = 3;
2881
  filename_ = filename;
×
2882
  set_length_multiplier(length_multiplier);
×
2883
  initialize();
×
2884
}
2885

2886
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
2887
{
2888
  mbi_ = external_mbi;
1✔
2889
  filename_ = "unknown (external file)";
1✔
2890
  this->initialize();
1✔
2891
}
1✔
2892

2893
void MOABMesh::initialize()
25✔
2894
{
2895

2896
  // Create the MOAB interface and load data from file
2897
  this->create_interface();
25✔
2898

2899
  // Initialise MOAB error code
2900
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
2901

2902
  // Set the dimension
2903
  n_dimension_ = 3;
25✔
2904

2905
  // set member range of tetrahedral entities
2906
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
2907
  if (rval != moab::MB_SUCCESS) {
25!
2908
    fatal_error("Failed to get all tetrahedral elements");
2909
  }
2910

2911
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
2912
    warning("Non-tetrahedral elements found in unstructured "
×
2913
            "mesh file: " +
2914
            filename_);
2915
  }
2916

2917
  // set member range of vertices
2918
  int vertex_dim = 0;
25✔
2919
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
2920
  if (rval != moab::MB_SUCCESS) {
25!
2921
    fatal_error("Failed to get all vertex handles");
2922
  }
2923

2924
  // make an entity set for all tetrahedra
2925
  // this is used for convenience later in output
2926
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
2927
  if (rval != moab::MB_SUCCESS) {
25!
2928
    fatal_error("Failed to create an entity set for the tetrahedral elements");
2929
  }
2930

2931
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
2932
  if (rval != moab::MB_SUCCESS) {
25!
2933
    fatal_error("Failed to add tetrahedra to an entity set.");
2934
  }
2935

2936
  if (length_multiplier_ > 0.0) {
25!
2937
    // get the connectivity of all tets
2938
    moab::Range adj;
×
2939
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
2940
    if (rval != moab::MB_SUCCESS) {
×
2941
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
2942
    }
2943
    // scale all vertex coords by multiplier (done individually so not all
2944
    // coordinates are in memory twice at once)
2945
    for (auto vert : adj) {
×
2946
      // retrieve coords
2947
      std::array<double, 3> coord;
2948
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
2949
      if (rval != moab::MB_SUCCESS) {
×
2950
        fatal_error("Could not get coordinates of vertex.");
2951
      }
2952
      // scale coords
2953
      for (auto& c : coord) {
×
2954
        c *= length_multiplier_;
2955
      }
2956
      // set new coords
2957
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2958
      if (rval != moab::MB_SUCCESS) {
×
2959
        fatal_error("Failed to set new vertex coordinates");
2960
      }
2961
    }
2962
  }
2963

2964
  // Determine bounds of mesh
2965
  this->determine_bounds();
25✔
2966
}
25✔
2967

2968
void MOABMesh::prepare_for_point_location()
21✔
2969
{
2970
  // if the KDTree has already been constructed, do nothing
2971
  if (kdtree_)
21!
2972
    return;
2973

2974
  // build acceleration data structures
2975
  compute_barycentric_data(ehs_);
21✔
2976
  build_kdtree(ehs_);
21✔
2977
}
2978

2979
void MOABMesh::create_interface()
25✔
2980
{
2981
  // Do not create a MOAB instance if one is already in memory
2982
  if (mbi_)
25✔
2983
    return;
1✔
2984

2985
  // create MOAB instance
2986
  mbi_ = std::make_shared<moab::Core>();
24✔
2987

2988
  // load unstructured mesh file
2989
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
2990
  if (rval != moab::MB_SUCCESS) {
24!
2991
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
2992
  }
2993
}
2994

2995
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
2996
{
2997
  moab::Range all_tris;
21✔
2998
  int adj_dim = 2;
21✔
2999
  write_message("Getting tet adjacencies...", 7);
21✔
3000
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
3001
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
3002
  if (rval != moab::MB_SUCCESS) {
21!
3003
    fatal_error("Failed to get adjacent triangles for tets");
3004
  }
3005

3006
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
3007
    warning("Non-triangle elements found in tet adjacencies in "
×
3008
            "unstructured mesh file: " +
3009
            filename_);
×
3010
  }
3011

3012
  // combine into one range
3013
  moab::Range all_tets_and_tris;
21✔
3014
  all_tets_and_tris.merge(all_tets);
21✔
3015
  all_tets_and_tris.merge(all_tris);
21✔
3016

3017
  // create a kd-tree instance
3018
  write_message(
21✔
3019
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
3020
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
3021

3022
  // Determine what options to use
3023
  std::ostringstream options_stream;
21✔
3024
  if (options_.empty()) {
21✔
3025
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
3026
  } else {
3027
    options_stream << options_;
16✔
3028
  }
3029
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
3030

3031
  // Build the k-d tree
3032
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
3033
  if (rval != moab::MB_SUCCESS) {
21!
3034
    fatal_error("Failed to construct KDTree for the "
3035
                "unstructured mesh file: " +
3036
                filename_);
×
3037
  }
3038
}
21✔
3039

3040
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
3041
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
3042
{
3043
  hits.clear();
1,543,584✔
3044

3045
  moab::ErrorCode rval;
3046
  vector<moab::EntityHandle> tris;
1,543,584✔
3047
  // get all intersections with triangles in the tet mesh
3048
  // (distances are relative to the start point, not the previous
3049
  // intersection)
3050
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
3051
    dir.array(), start.array(), tris, hits, 0, track_len);
3052
  if (rval != moab::MB_SUCCESS) {
1,543,584!
3053
    fatal_error(
3054
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
3055
  }
3056

3057
  // remove duplicate intersection distances
3058
  std::unique(hits.begin(), hits.end());
1,543,584✔
3059

3060
  // sorts by first component of std::pair by default
3061
  std::sort(hits.begin(), hits.end());
1,543,584✔
3062
}
1,543,584✔
3063

3064
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
3065
  vector<int>& bins, vector<double>& lengths) const
3066
{
3067
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
3068
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
3069
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
3070
  dir.normalize();
1,543,584✔
3071

3072
  double track_len = (end - start).length();
1,543,584✔
3073
  if (track_len == 0.0)
1,543,584!
3074
    return;
721,692✔
3075

3076
  start -= TINY_BIT * dir;
1,543,584✔
3077
  end += TINY_BIT * dir;
1,543,584✔
3078

3079
  vector<double> hits;
1,543,584✔
3080
  intersect_track(start, dir, track_len, hits);
1,543,584✔
3081

3082
  bins.clear();
1,543,584✔
3083
  lengths.clear();
1,543,584✔
3084

3085
  // if there are no intersections the track may lie entirely
3086
  // within a single tet. If this is the case, apply entire
3087
  // score to that tet and return.
3088
  if (hits.size() == 0) {
1,543,584✔
3089
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
3090
    int bin = this->get_bin(midpoint);
721,692✔
3091
    if (bin != -1) {
721,692✔
3092
      bins.push_back(bin);
242,866✔
3093
      lengths.push_back(1.0);
242,866✔
3094
    }
3095
    return;
721,692✔
3096
  }
3097

3098
  // for each segment in the set of tracks, try to look up a tet
3099
  // at the midpoint of the segment
3100
  Position current = r0;
821,892✔
3101
  double last_dist = 0.0;
821,892✔
3102
  for (const auto& hit : hits) {
5,516,161✔
3103
    // get the segment length
3104
    double segment_length = hit - last_dist;
4,694,269✔
3105
    last_dist = hit;
4,694,269✔
3106
    // find the midpoint of this segment
3107
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
3108
    // try to find a tet for this position
3109
    int bin = this->get_bin(midpoint);
4,694,269✔
3110

3111
    // determine the start point for this segment
3112
    current = r0 + u * hit;
4,694,269✔
3113

3114
    if (bin == -1) {
4,694,269✔
3115
      continue;
20,522✔
3116
    }
3117

3118
    bins.push_back(bin);
4,673,747✔
3119
    lengths.push_back(segment_length / track_len);
4,673,747✔
3120
  }
3121

3122
  // tally remaining portion of track after last hit if
3123
  // the last segment of the track is in the mesh but doesn't
3124
  // reach the other side of the tet
3125
  if (hits.back() < track_len) {
821,892!
3126
    Position segment_start = r0 + u * hits.back();
821,892✔
3127
    double segment_length = track_len - hits.back();
821,892✔
3128
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
3129
    int bin = this->get_bin(midpoint);
821,892✔
3130
    if (bin != -1) {
821,892✔
3131
      bins.push_back(bin);
766,509✔
3132
      lengths.push_back(segment_length / track_len);
766,509✔
3133
    }
3134
  }
3135
};
1,543,584✔
3136

3137
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,172✔
3138
{
3139
  moab::CartVect pos(r.x, r.y, r.z);
7,317,172✔
3140
  // find the leaf of the kd-tree for this position
3141
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,172✔
3142
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,172✔
3143
  if (rval != moab::MB_SUCCESS) {
7,317,172✔
3144
    return 0;
1,011,897✔
3145
  }
3146

3147
  // retrieve the tet elements of this leaf
3148
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,275✔
3149
  moab::Range tets;
6,305,275✔
3150
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,275✔
3151
  if (rval != moab::MB_SUCCESS) {
6,305,275!
3152
    warning("MOAB error finding tets.");
×
3153
  }
3154

3155
  // loop over the tets in this leaf, returning the containing tet if found
3156
  for (const auto& tet : tets) {
260,209,886✔
3157
    if (point_in_tet(pos, tet)) {
260,207,039✔
3158
      return tet;
6,302,428✔
3159
    }
3160
  }
3161

3162
  // if no tet is found, return an invalid handle
3163
  return 0;
2,847✔
3164
}
7,317,172✔
3165

3166
double MOABMesh::volume(int bin) const
167,880✔
3167
{
3168
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3169
}
3170

3171
std::string MOABMesh::library() const
34✔
3172
{
3173
  return mesh_lib_type;
34✔
3174
}
3175

3176
// Sample position within a tet for MOAB type tets
3177
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3178
{
3179

3180
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3181

3182
  // Get vertex coordinates for MOAB tet
3183
  const moab::EntityHandle* conn1;
3184
  int conn1_size;
3185
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3186
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3187
    fatal_error(fmt::format(
×
3188
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3189
      conn1_size));
3190
  }
3191
  moab::CartVect p[4];
1,002,050✔
3192
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3193
  if (rval != moab::MB_SUCCESS) {
200,410!
3194
    fatal_error("Failed to get tet coords");
3195
  }
3196

3197
  std::array<Position, 4> tet_verts;
200,410✔
3198
  for (int i = 0; i < 4; i++) {
1,002,050✔
3199
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3200
  }
3201
  // Samples position within tet using Barycentric stuff
3202
  return this->sample_tet(tet_verts, seed);
400,820✔
3203
}
3204

3205
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3206
{
3207
  vector<moab::EntityHandle> conn;
167,880✔
3208
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3209
  if (rval != moab::MB_SUCCESS) {
167,880!
3210
    fatal_error("Failed to get tet connectivity");
3211
  }
3212

3213
  moab::CartVect p[4];
839,400✔
3214
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3215
  if (rval != moab::MB_SUCCESS) {
167,880!
3216
    fatal_error("Failed to get tet coords");
3217
  }
3218

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

3222
int MOABMesh::get_bin(Position r) const
7,317,172✔
3223
{
3224
  moab::EntityHandle tet = get_tet(r);
7,317,172✔
3225
  if (tet == 0) {
7,317,172✔
3226
    return -1;
1,014,744✔
3227
  } else {
3228
    return get_bin_from_ent_handle(tet);
6,302,428✔
3229
  }
3230
}
3231

3232
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3233
{
3234
  moab::ErrorCode rval;
3235

3236
  baryc_data_.clear();
21✔
3237
  baryc_data_.resize(tets.size());
21✔
3238

3239
  // compute the barycentric data for each tet element
3240
  // and store it as a 3x3 matrix
3241
  for (auto& tet : tets) {
239,757✔
3242
    vector<moab::EntityHandle> verts;
239,736✔
3243
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3244
    if (rval != moab::MB_SUCCESS) {
239,736!
3245
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3246
    }
3247

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

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

3256
    // invert now to avoid this cost later
3257
    a = a.transpose().inverse();
239,736✔
3258
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3259
  }
239,736✔
3260
}
21✔
3261

3262
bool MOABMesh::point_in_tet(
260,207,039✔
3263
  const moab::CartVect& r, moab::EntityHandle tet) const
3264
{
3265

3266
  moab::ErrorCode rval;
3267

3268
  // get tet vertices
3269
  vector<moab::EntityHandle> verts;
260,207,039✔
3270
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,207,039✔
3271
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3272
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3273
    return false;
3274
  }
3275

3276
  // first vertex is used as a reference point for the barycentric data -
3277
  // retrieve its coordinates
3278
  moab::CartVect p_zero;
260,207,039✔
3279
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,207,039✔
3280
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3281
    warning("Failed to get coordinates of a vertex in "
×
3282
            "unstructured mesh: " +
3283
            filename_);
×
3284
    return false;
3285
  }
3286

3287
  // look up barycentric data
3288
  int idx = get_bin_from_ent_handle(tet);
260,207,039✔
3289
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,207,039✔
3290

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

3293
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
421,415,065✔
3294
          bary_coords[2] >= 0.0 &&
443,103,099✔
3295
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
281,895,073✔
3296
}
260,207,039✔
3297

3298
int MOABMesh::get_bin_from_index(int idx) const
3299
{
3300
  if (idx >= n_bins()) {
×
3301
    fatal_error(fmt::format("Invalid bin index: {}", idx));
×
3302
  }
3303
  return ehs_[idx] - ehs_[0];
3304
}
3305

3306
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3307
{
3308
  int bin = get_bin(r);
3309
  *in_mesh = bin != -1;
3310
  return bin;
3311
}
3312

3313
int MOABMesh::get_index_from_bin(int bin) const
3314
{
3315
  return bin;
3316
}
3317

3318
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3319
  Position plot_ll, Position plot_ur) const
3320
{
3321
  // TODO: Implement mesh lines
3322
  return {};
3323
}
3324

3325
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3326
{
3327
  int idx = vert - verts_[0];
815,520✔
3328
  if (idx >= n_vertices()) {
815,520!
3329
    fatal_error(
3330
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3331
  }
3332
  return idx;
815,520✔
3333
}
3334

3335
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,749,203✔
3336
{
3337
  int bin = eh - ehs_[0];
266,749,203✔
3338
  if (bin >= n_bins()) {
266,749,203!
3339
    fatal_error(fmt::format("Invalid bin: {}", bin));
×
3340
  }
3341
  return bin;
266,749,203✔
3342
}
3343

3344
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3345
{
3346
  if (bin >= n_bins()) {
572,170!
3347
    fatal_error(fmt::format("Invalid bin index: ", bin));
×
3348
  }
3349
  return ehs_[0] + bin;
572,170✔
3350
}
3351

3352
int MOABMesh::n_bins() const
267,525,326✔
3353
{
3354
  return ehs_.size();
267,525,326✔
3355
}
3356

3357
int MOABMesh::n_surface_bins() const
3358
{
3359
  // collect all triangles in the set of tets for this mesh
3360
  moab::Range tris;
×
3361
  moab::ErrorCode rval;
3362
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3363
  if (rval != moab::MB_SUCCESS) {
×
3364
    warning("Failed to get all triangles in the mesh instance");
×
3365
    return -1;
3366
  }
3367
  return 2 * tris.size();
×
3368
}
3369

3370
Position MOABMesh::centroid(int bin) const
3371
{
3372
  moab::ErrorCode rval;
3373

3374
  auto tet = this->get_ent_handle_from_bin(bin);
×
3375

3376
  // look up the tet connectivity
3377
  vector<moab::EntityHandle> conn;
3378
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3379
  if (rval != moab::MB_SUCCESS) {
×
3380
    warning("Failed to get connectivity of a mesh element.");
×
3381
    return {};
3382
  }
3383

3384
  // get the coordinates
3385
  vector<moab::CartVect> coords(conn.size());
×
3386
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3387
  if (rval != moab::MB_SUCCESS) {
×
3388
    warning("Failed to get the coordinates of a mesh element.");
×
3389
    return {};
3390
  }
3391

3392
  // compute the centroid of the element vertices
3393
  moab::CartVect centroid(0.0, 0.0, 0.0);
3394
  for (const auto& coord : coords) {
×
3395
    centroid += coord;
3396
  }
3397
  centroid /= double(coords.size());
3398

3399
  return {centroid[0], centroid[1], centroid[2]};
3400
}
3401

3402
int MOABMesh::n_vertices() const
845,874✔
3403
{
3404
  return verts_.size();
845,874✔
3405
}
3406

3407
Position MOABMesh::vertex(int id) const
86,227✔
3408
{
3409

3410
  moab::ErrorCode rval;
3411

3412
  moab::EntityHandle vert = verts_[id];
86,227✔
3413

3414
  moab::CartVect coords;
86,227✔
3415
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3416
  if (rval != moab::MB_SUCCESS) {
86,227!
3417
    fatal_error("Failed to get the coordinates of a vertex.");
3418
  }
3419

3420
  return {coords[0], coords[1], coords[2]};
172,454✔
3421
}
3422

3423
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3424
{
3425
  moab::ErrorCode rval;
3426

3427
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3428

3429
  // look up the tet connectivity
3430
  vector<moab::EntityHandle> conn;
203,880✔
3431
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3432
  if (rval != moab::MB_SUCCESS) {
203,880!
3433
    fatal_error("Failed to get connectivity of a mesh element.");
3434
    return {};
3435
  }
3436

3437
  std::vector<int> verts(4);
203,880✔
3438
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3439
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3440
  }
3441

3442
  return verts;
203,880✔
3443
}
203,880✔
3444

3445
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3446
  std::string score) const
3447
{
3448
  moab::ErrorCode rval;
3449
  // add a tag to the mesh
3450
  // all scores are treated as a single value
3451
  // with an uncertainty
3452
  moab::Tag value_tag;
3453

3454
  // create the value tag if not present and get handle
3455
  double default_val = 0.0;
3456
  auto val_string = score + "_mean";
×
3457
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3458
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3459
  if (rval != moab::MB_SUCCESS) {
×
3460
    auto msg =
3461
      fmt::format("Could not create or retrieve the value tag for the score {}"
3462
                  " on unstructured mesh {}",
3463
        score, id_);
×
3464
    fatal_error(msg);
3465
  }
3466

3467
  // create the std dev tag if not present and get handle
3468
  moab::Tag error_tag;
3469
  std::string err_string = score + "_std_dev";
×
3470
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3471
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3472
  if (rval != moab::MB_SUCCESS) {
×
3473
    auto msg =
3474
      fmt::format("Could not create or retrieve the error tag for the score {}"
3475
                  " on unstructured mesh {}",
3476
        score, id_);
×
3477
    fatal_error(msg);
3478
  }
3479

3480
  // return the populated tag handles
3481
  return {value_tag, error_tag};
3482
}
3483

3484
void MOABMesh::add_score(const std::string& score)
3485
{
3486
  auto score_tags = get_score_tags(score);
×
3487
  tag_names_.push_back(score);
×
3488
}
3489

3490
void MOABMesh::remove_scores()
3491
{
3492
  for (const auto& name : tag_names_) {
×
3493
    auto value_name = name + "_mean";
×
3494
    moab::Tag tag;
3495
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3496
    if (rval != moab::MB_SUCCESS)
×
3497
      return;
3498

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

3507
    auto std_dev_name = name + "_std_dev";
×
3508
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3509
    if (rval != moab::MB_SUCCESS) {
×
3510
      auto msg =
3511
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3512
                    " on unstructured mesh {}",
3513
          name, id_);
×
3514
    }
3515

3516
    rval = mbi_->tag_delete(tag);
×
3517
    if (rval != moab::MB_SUCCESS) {
×
3518
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3519
                             " on unstructured mesh {}",
3520
        name, id_);
×
3521
      fatal_error(msg);
3522
    }
3523
  }
×
3524
  tag_names_.clear();
3525
}
3526

3527
void MOABMesh::set_score_data(const std::string& score,
3528
  const vector<double>& values, const vector<double>& std_dev)
3529
{
3530
  auto score_tags = this->get_score_tags(score);
×
3531

3532
  moab::ErrorCode rval;
3533
  // set the score value
3534
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
×
3535
  if (rval != moab::MB_SUCCESS) {
×
3536
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3537
                           "on unstructured mesh {}",
3538
      score, id_);
×
3539
    warning(msg);
×
3540
  }
3541

3542
  // set the error value
3543
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
×
3544
  if (rval != moab::MB_SUCCESS) {
×
3545
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3546
                           "on unstructured mesh {}",
3547
      score, id_);
×
3548
    warning(msg);
×
3549
  }
3550
}
3551

3552
void MOABMesh::write(const std::string& base_filename) const
3553
{
3554
  // add extension to the base name
3555
  auto filename = base_filename + ".vtk";
×
3556
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3557
  filename = settings::path_output + filename;
×
3558

3559
  // write the tetrahedral elements of the mesh only
3560
  // to avoid clutter from zero-value data on other
3561
  // elements during visualization
3562
  moab::ErrorCode rval;
3563
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3564
  if (rval != moab::MB_SUCCESS) {
×
3565
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3566
    warning(msg);
×
3567
  }
3568
}
3569

3570
#endif
3571

3572
#ifdef OPENMC_LIBMESH_ENABLED
3573

3574
const std::string LibMesh::mesh_lib_type = "libmesh";
3575

3576
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3577
{
3578
  // filename_ and length_multiplier_ will already be set by the
3579
  // UnstructuredMesh constructor
3580
  set_mesh_pointer_from_filename(filename_);
23✔
3581
  set_length_multiplier(length_multiplier_);
23✔
3582
  initialize();
23✔
3583
}
23✔
3584

3585
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3586
{
3587
  // filename_ and length_multiplier_ will already be set by the
3588
  // UnstructuredMesh constructor
3589
  set_mesh_pointer_from_filename(filename_);
×
3590
  set_length_multiplier(length_multiplier_);
×
3591
  initialize();
×
3592
}
3593

3594
// create the mesh from a pointer to a libMesh Mesh
3595
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3596
{
3597
  if (!input_mesh.is_replicated()) {
×
3598
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3599
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3600
  }
3601

3602
  m_ = &input_mesh;
3603
  set_length_multiplier(length_multiplier);
×
3604
  initialize();
×
3605
}
3606

3607
// create the mesh from an input file
3608
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3609
{
3610
  n_dimension_ = 3;
3611
  set_mesh_pointer_from_filename(filename);
×
3612
  set_length_multiplier(length_multiplier);
×
3613
  initialize();
×
3614
}
3615

3616
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3617
{
3618
  filename_ = filename;
23✔
3619
  unique_m_ =
3620
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3621
  m_ = unique_m_.get();
23✔
3622
  m_->read(filename_);
23✔
3623
}
23✔
3624

3625
// build a libMesh equation system for storing values
3626
void LibMesh::build_eqn_sys()
15✔
3627
{
3628
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
30✔
3629
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3630
  libMesh::ExplicitSystem& eq_sys =
3631
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3632
}
15✔
3633

3634
// intialize from mesh file
3635
void LibMesh::initialize()
23✔
3636
{
3637
  if (!settings::libmesh_comm) {
23!
3638
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3639
                "communicator.");
3640
  }
3641

3642
  // assuming that unstructured meshes used in OpenMC are 3D
3643
  n_dimension_ = 3;
23✔
3644

3645
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3646
  // Otherwise assume that it is prepared by its owning application
3647
  if (unique_m_) {
23!
3648
    m_->prepare_for_use();
23✔
3649
  }
3650

3651
  // ensure that the loaded mesh is 3 dimensional
3652
  if (m_->mesh_dimension() != n_dimension_) {
23!
3653
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3654
                            "mesh is not a 3D mesh.",
3655
      filename_));
3656
  }
3657

3658
  for (int i = 0; i < num_threads(); i++) {
69✔
3659
    pl_.emplace_back(m_->sub_point_locator());
46✔
3660
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
3661
    pl_.back()->enable_out_of_mesh_mode();
46✔
3662
  }
3663

3664
  // store first element in the mesh to use as an offset for bin indices
3665
  auto first_elem = *m_->elements_begin();
23✔
3666
  first_element_id_ = first_elem->id();
23✔
3667

3668
  // bounding box for the mesh for quick rejection checks
3669
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23✔
3670
  libMesh::Point ll = bbox_.min();
23✔
3671
  libMesh::Point ur = bbox_.max();
23✔
3672
  lower_left_ = {ll(0), ll(1), ll(2)};
23✔
3673
  upper_right_ = {ur(0), ur(1), ur(2)};
23✔
3674
}
23✔
3675

3676
// Sample position within a tet for LibMesh type tets
3677
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3678
{
3679
  const auto& elem = get_element_from_bin(bin);
400,820✔
3680
  // Get tet vertex coordinates from LibMesh
3681
  std::array<Position, 4> tet_verts;
400,820✔
3682
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3683
    auto node_ref = elem.node_ref(i);
1,603,280✔
3684
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3685
  }
1,603,280✔
3686
  // Samples position within tet using Barycentric coordinates
3687
  return this->sample_tet(tet_verts, seed);
801,640✔
3688
}
3689

3690
Position LibMesh::centroid(int bin) const
3691
{
3692
  const auto& elem = this->get_element_from_bin(bin);
×
3693
  auto centroid = elem.vertex_average();
×
3694
  if (length_multiplier_ > 0.0) {
×
3695
    return length_multiplier_ * Position(centroid(0), centroid(1), centroid(2));
×
3696
  } else {
3697
    return {centroid(0), centroid(1), centroid(2)};
3698
  }
3699
}
3700

3701
int LibMesh::n_vertices() const
39,978✔
3702
{
3703
  return m_->n_nodes();
39,978✔
3704
}
3705

3706
Position LibMesh::vertex(int vertex_id) const
39,942✔
3707
{
3708
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
3709
  if (length_multiplier_ > 0.0) {
39,942!
3710
    return length_multiplier_ * Position(node_ref(0), node_ref(1), node_ref(2));
×
3711
  } else {
3712
    return {node_ref(0), node_ref(1), node_ref(2)};
39,942✔
3713
  }
3714
}
39,942✔
3715

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

3726
std::string LibMesh::library() const
33✔
3727
{
3728
  return mesh_lib_type;
33✔
3729
}
3730

3731
int LibMesh::n_bins() const
1,784,287✔
3732
{
3733
  return m_->n_elem();
1,784,287✔
3734
}
3735

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

3754
void LibMesh::add_score(const std::string& var_name)
15✔
3755
{
3756
  if (!equation_systems_) {
15!
3757
    build_eqn_sys();
15✔
3758
  }
3759

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

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

3779
void LibMesh::remove_scores()
15✔
3780
{
3781
  if (equation_systems_) {
15!
3782
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3783
    eqn_sys.clear();
15✔
3784
    variable_map_.clear();
15✔
3785
  }
3786
}
15✔
3787

3788
void LibMesh::set_score_data(const std::string& var_name,
15✔
3789
  const vector<double>& values, const vector<double>& std_dev)
3790
{
3791
  if (!equation_systems_) {
15!
3792
    build_eqn_sys();
×
3793
  }
3794

3795
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3796

3797
  if (!eqn_sys.is_initialized()) {
15!
3798
    equation_systems_->init();
15✔
3799
  }
3800

3801
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3802

3803
  // look up the value variable
3804
  std::string value_name = var_name + "_mean";
15✔
3805
  unsigned int value_num = variable_map_.at(value_name);
15✔
3806
  // look up the std dev variable
3807
  std::string std_dev_name = var_name + "_std_dev";
15✔
3808
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
3809

3810
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
97,871✔
3811
       it++) {
3812
    if (!(*it)->active()) {
97,856!
3813
      continue;
3814
    }
3815

3816
    auto bin = get_bin_from_element(*it);
97,856✔
3817

3818
    // set value
3819
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
3820
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
3821
    assert(value_dof_indices.size() == 1);
3822
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
3823

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

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

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

3849
int LibMesh::get_bin(Position r) const
2,340,484✔
3850
{
3851
  // look-up a tet using the point locator
3852
  libMesh::Point p(r.x, r.y, r.z);
2,340,484✔
3853

3854
  if (length_multiplier_ > 0.0) {
2,340,484!
3855
    // Scale the point down
3856
    p /= length_multiplier_;
3857
  }
3858

3859
  // quick rejection check
3860
  if (!bbox_.contains_point(p)) {
2,340,484✔
3861
    return -1;
918,796✔
3862
  }
3863

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

3866
  const auto elem_ptr = (*point_locator)(p);
1,421,688✔
3867
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,688✔
3868
}
2,340,484✔
3869

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

3879
std::pair<vector<double>, vector<double>> LibMesh::plot(
3880
  Position plot_ll, Position plot_ur) const
3881
{
3882
  return {};
3883
}
3884

3885
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
3886
{
3887
  return m_->elem_ref(bin);
765,460✔
3888
}
3889

3890
double LibMesh::volume(int bin) const
364,640✔
3891
{
3892
  return this->get_element_from_bin(bin).volume() * length_multiplier_ *
364,640✔
3893
         length_multiplier_ * length_multiplier_;
364,640✔
3894
}
3895

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

3923
int AdaptiveLibMesh::n_bins() const
3924
{
3925
  return num_active_;
3926
}
3927

3928
void AdaptiveLibMesh::add_score(const std::string& var_name)
3929
{
3930
  warning(fmt::format(
×
3931
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3932
    this->id_));
3933
}
3934

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

3943
void AdaptiveLibMesh::write(const std::string& filename) const
3944
{
3945
  warning(fmt::format(
×
3946
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3947
    this->id_));
3948
}
3949

3950
int AdaptiveLibMesh::get_bin(Position r) const
3951
{
3952
  // look-up a tet using the point locator
3953
  libMesh::Point p(r.x, r.y, r.z);
×
3954

3955
  if (length_multiplier_ > 0.0) {
×
3956
    // Scale the point down
3957
    p /= length_multiplier_;
3958
  }
3959

3960
  // quick rejection check
3961
  if (!bbox_.contains_point(p)) {
×
3962
    return -1;
3963
  }
3964

3965
  const auto& point_locator = pl_.at(thread_num());
×
3966

3967
  const auto elem_ptr = (*point_locator)(p, &block_ids_);
×
3968
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
×
3969
}
3970

3971
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3972
{
3973
  int bin = elem_to_bin_map_[elem->id()];
×
3974
  if (bin >= n_bins() || bin < 0) {
×
3975
    fatal_error(fmt::format("Invalid bin: {}", bin));
3976
  }
3977
  return bin;
3978
}
3979

3980
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
3981
{
3982
  return m_->elem_ref(bin_to_elem_map_.at(bin));
3983
}
3984

3985
#endif // OPENMC_LIBMESH_ENABLED
3986

3987
//==============================================================================
3988
// Non-member functions
3989
//==============================================================================
3990

3991
void read_meshes(pugi::xml_node root)
12,406✔
3992
{
3993
  std::unordered_set<int> mesh_ids;
12,406✔
3994

3995
  for (auto node : root.children("mesh")) {
15,361✔
3996
    // Check to make sure multiple meshes in the same file don't share IDs
3997
    int id = std::stoi(get_node_value(node, "id"));
2,955✔
3998
    if (contains(mesh_ids, id)) {
2,955!
UNCOV
3999
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4000
                              "'{}' in the same input file",
4001
        id));
4002
    }
4003
    mesh_ids.insert(id);
2,955✔
4004

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

4012
    std::string mesh_type;
2,955✔
4013
    if (check_for_node(node, "type")) {
2,955✔
4014
      mesh_type = get_node_value(node, "type", true, true);
956✔
4015
    } else {
4016
      mesh_type = "regular";
1,999✔
4017
    }
4018

4019
    // determine the mesh library to use
4020
    std::string mesh_lib;
2,955✔
4021
    if (check_for_node(node, "library")) {
2,955✔
4022
      mesh_lib = get_node_value(node, "library", true, true);
47!
4023
    }
4024

4025
    Mesh::create(node, mesh_type, mesh_lib);
2,955✔
4026
  }
2,955✔
4027
}
12,406✔
4028

4029
void read_meshes(hid_t group)
22✔
4030
{
4031
  std::unordered_set<int> mesh_ids;
22✔
4032

4033
  std::vector<int> ids;
22✔
4034
  read_attribute(group, "ids", ids);
22✔
4035

4036
  for (auto id : ids) {
55✔
4037

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

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

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

UNCOV
4056
    std::string mesh_type;
×
UNCOV
4057
    if (object_exists(mesh_group, "type")) {
×
UNCOV
4058
      read_dataset(mesh_group, "type", mesh_type);
×
4059
    } else {
UNCOV
4060
      mesh_type = "regular";
×
4061
    }
4062

4063
    // determine the mesh library to use
UNCOV
4064
    std::string mesh_lib;
×
UNCOV
4065
    if (object_exists(mesh_group, "library")) {
×
UNCOV
4066
      read_dataset(mesh_group, "library", mesh_lib);
×
4067
    }
4068

UNCOV
4069
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
UNCOV
4070
  }
×
4071
}
22✔
4072

4073
void meshes_to_hdf5(hid_t group)
6,775✔
4074
{
4075
  // Write number of meshes
4076
  hid_t meshes_group = create_group(group, "meshes");
6,775✔
4077
  int32_t n_meshes = model::meshes.size();
6,775✔
4078
  write_attribute(meshes_group, "n_meshes", n_meshes);
6,775✔
4079

4080
  if (n_meshes > 0) {
6,775✔
4081
    // Write IDs of meshes
4082
    vector<int> ids;
2,104✔
4083
    for (const auto& m : model::meshes) {
4,787✔
4084
      m->to_hdf5(meshes_group);
2,683✔
4085
      ids.push_back(m->id_);
2,683✔
4086
    }
4087
    write_attribute(meshes_group, "ids", ids);
2,104✔
4088
  }
2,104✔
4089

4090
  close_group(meshes_group);
6,775✔
4091
}
6,775✔
4092

4093
void free_memory_mesh()
8,186✔
4094
{
4095
  model::meshes.clear();
8,186✔
4096
  model::mesh_map.clear();
8,186✔
4097
}
8,186✔
4098

4099
extern "C" int n_meshes()
308✔
4100
{
4101
  return model::meshes.size();
308✔
4102
}
4103

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