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

openmc-dev / openmc / 25930342666

15 May 2026 04:56PM UTC coverage: 81.414% (+0.09%) from 81.324%
25930342666

Pull #3734

github

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

17919 of 25855 branches covered (69.31%)

Branch coverage included in aggregate %.

275 of 305 new or added lines in 18 files covered. (90.16%)

1321 existing lines in 29 files now uncovered.

58986 of 68607 relevant lines covered (85.98%)

47884537.9 hits per line

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

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

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

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

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

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

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

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

58
namespace openmc {
59

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

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

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

74
namespace model {
75

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

79
} // namespace model
80

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

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

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

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

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

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

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

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

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

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

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

187
inline void atomic_max_double(double* ptr, double value)
19,277,208✔
188
{
189
  atomic_update_double(ptr, value, false);
6,425,736✔
190
}
6,425,736✔
191

192
inline void atomic_min_double(double* ptr, double value)
19,277,208✔
193
{
194
  atomic_update_double(ptr, value, true);
6,425,736✔
195
}
196

197
namespace detail {
198

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

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

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

220
    // Non-atomic read of current material
221
    int32_t current_val = *slot_ptr;
9,079,123✔
222

223
    // Found the desired material; accumulate volume and bbox
224
    if (current_val == index_material) {
9,079,123✔
225
#pragma omp atomic
5,187,390✔
226
      this->volumes(index_elem, slot) += volume;
9,077,552✔
227
      if (bbox) {
9,077,552✔
228
        atomic_min_double(&this->bboxes(index_elem, slot, 0), bbox->min.x);
6,425,562✔
229
        atomic_min_double(&this->bboxes(index_elem, slot, 1), bbox->min.y);
6,425,562✔
230
        atomic_min_double(&this->bboxes(index_elem, slot, 2), bbox->min.z);
6,425,562✔
231
        atomic_max_double(&this->bboxes(index_elem, slot, 3), bbox->max.x);
6,425,562✔
232
        atomic_max_double(&this->bboxes(index_elem, slot, 4), bbox->max.y);
6,425,562✔
233
        atomic_max_double(&this->bboxes(index_elem, slot, 5), bbox->max.z);
6,425,562✔
234
      }
235
      return;
9,077,552✔
236
    }
237

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

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

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

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

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

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

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

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

326
} // namespace detail
327

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

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

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

365
  return model::meshes.back();
3,208✔
366
}
367

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

556
            // Add volumes to any mesh elements that were crossed
557
            int i_material = p.material();
3,914,883✔
558
            if (i_material != C_NONE) {
3,914,883✔
559
              i_material = model::materials[i_material]->id();
1,241,589✔
560
            }
561
            double cumulative_frac = 0.0;
3,914,883✔
562
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
8,048,416✔
563
              int mesh_index = bins[i_bin];
4,133,533✔
564
              double length = distance * length_fractions[i_bin];
4,133,533✔
565
              double volume = length * d1 * d2;
4,133,533✔
566

567
              if (compute_bboxes) {
4,133,533✔
568
                double axis_start = r0[axis] + distance * cumulative_frac;
2,927,448✔
569
                double axis_end = axis_start + length;
2,927,448✔
570
                cumulative_frac += length_fractions[i_bin];
2,927,448✔
571

572
                Position contrib_min = site.r;
2,927,448✔
573
                Position contrib_max = site.r;
2,927,448✔
574

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

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

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

593
            if (distance == max_distance)
3,914,883✔
594
              break;
595

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

749
  // Close group
750
  close_group(mesh_group);
3,092✔
751
}
3,092✔
752

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

757
std::string StructuredMesh::bin_label(int bin) const
5,143,043✔
758
{
759
  MeshIndex ijk = get_indices_from_bin(bin);
5,143,043✔
760

761
  if (n_dimension_ > 2) {
5,143,043✔
762
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
5,127,104✔
763
  } else if (n_dimension_ > 1) {
15,939✔
764
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
15,664✔
765
  } else {
766
    return fmt::format("Mesh Index ({})", ijk[0]);
275✔
767
  }
768
}
769

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

NEW
875
double UnstructuredMesh::distance_to_next_boundary(
×
876
  int current_bin, Position r, Direction u, int& bin_next) const
877
{
NEW
878
  fatal_error("Not implemented");
×
879
  return -1.0;
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);
79,911✔
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,602✔
917
    t = 1.0 - t;
300,602✔
918
  }
919
  if (s + t + u > 1) {
601,230✔
920
    if (t + u > 1) {
401,191✔
921
      double old_t = t;
201,074✔
922
      t = 1.0 - u;
201,074✔
923
      u = 1.0 - s - old_t;
201,074✔
924
    } else if (t + u <= 1) {
200,117!
925
      double old_s = s;
200,117✔
926
      s = 1.0 - t - u;
200,117✔
927
      u = old_s + t + u - 1;
200,117✔
928
    }
929
  }
930
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,803,690✔
931
         u * (coords[3] - coords[0]) + coords[0];
601,230✔
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

941
void UnstructuredMesh::surface_bins_crossed(
×
942
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
943
{
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
  tensor::Tensor<double> vertices(
32✔
965
    {static_cast<size_t>(this->n_vertices()), static_cast<size_t>(3)});
32✔
966
  for (int i = 0; i < this->n_vertices(); i++) {
70,275!
967
    auto v = this->vertex(i);
70,243!
968
    vertices.slice(i) = {v.x, v.y, v.z};
140,486!
969
  }
970
  write_dataset(mesh_group, "vertices", vertices);
32!
971

972
  int num_elem_skipped = 0;
32✔
973

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

983
    volumes.emplace_back(this->volume(i));
349,736!
984

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

1002
  // warn users that some elements were skipped
1003
  if (num_elem_skipped > 0) {
32!
1004
    warning(fmt::format("The connectivity of {} elements "
×
1005
                        "on mesh {} were not written "
1006
                        "because they are not of type linear tet/hex.",
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
}
96✔
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;
1026
  else if (conn.size() == 8)
×
1027
    return ElementType::LINEAR_HEX;
1028
  else
1029
    return ElementType::UNSUPPORTED;
×
1030
}
120,000✔
1031

1032
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,268,788,618✔
1033
  Position r, bool& in_mesh) const
1034
{
1035
  MeshIndex ijk;
1,268,788,618✔
1036
  in_mesh = true;
1,268,788,618✔
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;
110,514,928✔
1042
  }
1043
  return ijk;
1,268,788,618✔
1044
}
1045

1046
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,852,285,748✔
1047
{
1048
  switch (n_dimension_) {
1,852,285,748!
1049
  case 1:
880,605✔
1050
    return ijk[0] - 1;
880,605✔
1051
  case 2:
136,375,228✔
1052
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
136,375,228✔
1053
  case 3:
1,715,029,915✔
1054
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,715,029,915✔
1055
  default:
×
1056
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
1057
  }
1058
}
1059

1060
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
10,766,327✔
1061
{
1062
  MeshIndex ijk;
10,766,327✔
1063
  if (n_dimension_ == 1) {
10,766,327✔
1064
    ijk[0] = bin + 1;
275✔
1065
  } else if (n_dimension_ == 2) {
10,766,052✔
1066
    ijk[0] = bin % shape_[0] + 1;
15,664✔
1067
    ijk[1] = bin / shape_[0] + 1;
15,664✔
1068
  } else if (n_dimension_ == 3) {
10,750,388!
1069
    ijk[0] = bin % shape_[0] + 1;
10,750,388✔
1070
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
10,750,388✔
1071
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
10,750,388✔
1072
  }
1073
  return ijk;
10,766,327✔
1074
}
1075

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

1084
  // Convert indices to bin
1085
  return get_bin_from_indices(ijk);
235,401,449✔
1086
}
1087

1088
int StructuredMesh::n_bins() const
1,136,372✔
1089
{
1090
  return std::accumulate(
2,272,744✔
1091
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
1,136,372✔
1092
}
1093

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

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

1106
  // Create array of zeros
1107
  auto cnt = tensor::zeros<double>(shape);
×
1108
  bool outside_ = false;
1109

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

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

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

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

1126
  // Create reduced count data
1127
  auto counts = tensor::zeros<double>(shape);
×
1128
  int total = cnt.size();
×
1129

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

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

1145
  return counts;
×
1146
}
×
1147

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

1161
  // Compute the length of the entire track.
1162
  double total_distance = (r1 - r0).norm();
1,016,738,333✔
1163
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
1,016,738,333✔
1164
    return;
1165

1166
  // keep a copy of the original global position to pass to get_indices,
1167
  // which performs its own transformation to local coordinates
1168
  Position global_r = r0;
1,004,644,038✔
1169
  Position local_r = local_coords(r0);
1,004,644,038✔
1170

1171
  const int n = n_dimension_;
1,004,644,038✔
1172

1173
  // Flag if position is inside the mesh
1174
  bool in_mesh;
1175

1176
  // Position is r = r0 + u * traveled_distance, start at r0
1177
  double traveled_distance {0.0};
1,004,644,038✔
1178

1179
  // Calculate index of current cell. Offset the position a tiny bit in
1180
  // direction of flight
1181
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
1,004,644,038✔
1182

1183
  // if track is very short, assume that it is completely inside one cell.
1184
  // Only the current cell will score and no surfaces
1185
  if (total_distance < 2 * TINY_BIT) {
1,004,644,038✔
1186
    if (in_mesh) {
361,821✔
1187
      tally.track(ijk, 1.0);
361,337✔
1188
    }
1189
    return;
361,821✔
1190
  }
1191

1192
  // Calculate initial distances to next surfaces in all three dimensions
1193
  std::array<MeshDistance, 3> distances;
2,008,564,434✔
1194
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
1195
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
1196
  }
1197

1198
  // Loop until r = r1 is eventually reached
1199
  while (true) {
1200

1201
    if (in_mesh) {
1,788,240,523✔
1202

1203
      // find surface with minimal distance to current position
1204
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,696,726,415✔
1205
                     distances.begin();
1,696,726,415✔
1206

1207
      // Tally track length delta since last step
1208
      tally.track(ijk,
1,696,726,415✔
1209
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
2,147,483,647✔
1210
          total_distance);
1211

1212
      // update position and leave, if we have reached end position
1213
      traveled_distance = distances[k].distance;
1,696,726,415✔
1214
      if (traveled_distance >= total_distance)
1,696,726,415✔
1215
        return;
1216

1217
      // If we have not reached r1, we have hit a surface. Tally outward
1218
      // current
1219
      tally.surface(ijk, k, distances[k].max_surface, false);
776,648,927✔
1220

1221
      // Update cell and calculate distance to next surface in k-direction.
1222
      // The two other directions are still valid!
1223
      ijk[k] = distances[k].next_index;
776,648,927✔
1224
      distances[k] =
776,648,927✔
1225
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
776,648,927✔
1226

1227
      // Check if we have left the interior of the mesh
1228
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
783,661,702✔
1229

1230
      // If we are still inside the mesh, tally inward current for the next
1231
      // cell
1232
      if (in_mesh)
29,576,899✔
1233
        tally.surface(ijk, k, !distances[k].max_surface, true);
782,542,089✔
1234

1235
    } else { // not inside mesh
1236

1237
      // For all directions outside the mesh, find the distance that we need
1238
      // to travel to reach the next surface. Use the largest distance, as
1239
      // only this will cross all outer surfaces.
1240
      int k_max {-1};
1241
      for (int k = 0; k < n; ++k) {
364,612,066✔
1242
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
273,097,958✔
1243
            (distances[k].distance > traveled_distance)) {
102,028,516✔
1244
          traveled_distance = distances[k].distance;
1245
          k_max = k;
1246
        }
1247
      }
1248
      // Assure some distance is traveled
1249
      if (k_max == -1) {
91,514,108✔
1250
        traveled_distance += TINY_BIT;
110✔
1251
      }
1252

1253
      // If r1 is not inside the mesh, exit here
1254
      if (traveled_distance >= total_distance)
91,514,108✔
1255
        return;
1256

1257
      // Calculate the new cell index and update all distances to next
1258
      // surfaces.
1259
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
7,309,379✔
1260
      for (int k = 0; k < n; ++k) {
29,028,978✔
1261
        distances[k] =
21,719,599✔
1262
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
21,719,599✔
1263
      }
1264

1265
      // If inside the mesh, Tally inward current
1266
      if (in_mesh && k_max >= 0)
7,309,379!
1267
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
754,358,439✔
1268
    }
1269
  }
1270
}
1271

1272
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
904,610,768✔
1273
  vector<int>& bins, vector<double>& lengths) const
1274
{
1275

1276
  // Helper tally class.
1277
  // stores a pointer to the mesh class and references to bins and lengths
1278
  // parameters. Performs the actual tally through the track method.
1279
  struct TrackAggregator {
904,610,768✔
1280
    TrackAggregator(
904,610,768✔
1281
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1282
      : mesh(_mesh), bins(_bins), lengths(_lengths)
904,610,768✔
1283
    {}
1284
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1285
    void track(const MeshIndex& ijk, double l) const
1,557,043,188✔
1286
    {
1287
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,557,043,188✔
1288
      lengths.push_back(l);
1,557,043,188✔
1289
    }
1,557,043,188✔
1290

1291
    const StructuredMesh* mesh;
1292
    vector<int>& bins;
1293
    vector<double>& lengths;
1294
  };
1295

1296
  // Perform the mesh raytrace with the helper class.
1297
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
904,610,768✔
1298
}
904,610,768✔
1299

1300
void StructuredMesh::surface_bins_crossed(
112,127,565✔
1301
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1302
{
1303

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

1323
    const StructuredMesh* mesh;
1324
    vector<int>& bins;
1325
  };
1326

1327
  // Perform the mesh raytrace with the helper class.
1328
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
112,127,565✔
1329
}
112,127,565✔
1330

1331
double StructuredMesh::distance_to_next_boundary(
5,372,191✔
1332
  int current_bin, Position r, Direction u, int& bin_next) const
1333
{
1334
  Position global_r = r;
5,372,191✔
1335
  Position local_r = local_coords(r);
5,372,191✔
1336

1337
  double distance;
5,372,191✔
1338
  bool in_mesh;
5,372,191✔
1339

1340
  // Find the cell indices
1341
  StructuredMesh::MeshIndex ijk;
5,372,191✔
1342
  if (current_bin >= 0) {
5,372,191✔
1343

1344
    ijk = get_indices_from_bin(current_bin);
2,991,813✔
1345

1346
    // Calculate initial distances to next surfaces in all three dimensions
1347
    std::array<StructuredMesh::MeshDistance, 3> distances;
5,983,626✔
1348
    for (int k = 0; k < n_dimension_; ++k) {
11,967,252✔
1349
      distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
8,975,439✔
1350
    }
1351

1352
    // Find next ijk
1353
    auto k_min =
1354
      std::min_element(distances.begin(), distances.end()) - distances.begin();
2,991,813✔
1355
    distance = distances[k_min].distance;
2,991,813✔
1356
    ijk[k_min] = distances[k_min].next_index;
2,991,813✔
1357

1358
    // If the particle is on a surface, test using the next index
1359
    if (distance <= FP_COINCIDENT) {
2,991,813✔
1360

1361
      // Update distances
1362
      for (int k = 0; k < n_dimension_; ++k) {
176✔
1363
        distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
132✔
1364
      }
1365

1366
      k_min = std::min_element(distances.begin(), distances.end()) -
44✔
1367
              distances.begin();
44✔
1368
      distance = distances[k_min].distance;
44✔
1369
      ijk[k_min] = distances[k_min].next_index;
44✔
1370
    }
1371

1372
    // Determine next bin
1373
    in_mesh = true;
2,991,813✔
1374
    for (int k = 0; k < n_dimension_; ++k) {
11,967,252✔
1375
      if ((ijk[k] < 1) || (ijk[k] > shape_[k])) {
8,975,439✔
1376
        in_mesh = false;
1,504,206✔
1377
      }
1378
    }
1379
    if (in_mesh) {
2,991,813✔
1380
      bin_next = get_bin_from_indices(ijk);
1,487,607✔
1381
    } else {
1382
      bin_next = C_NONE;
1,504,206✔
1383
    }
1384

1385
  } else { // Outside mesh
1386

1387
    // Calculate distance to mesh from outside
1388
    distance = INFTY;
1389
    for (int k = 0; k < n_dimension_; k++) {
9,521,512✔
1390
      double d = distance_to_mesh_boundary_from_outside(k, r, u);
7,141,134✔
1391
      if (d < INFTY) {
7,141,134✔
1392
        distance = d;
194,315✔
1393
      }
1394
    }
1395

1396
    // Determine next bin
1397
    if (distance < INFTY) {
2,380,378✔
1398
      ijk = get_indices(global_r + (distance + TINY_BIT) * u, in_mesh);
194,315✔
1399
      bin_next = get_bin_from_indices(ijk);
194,315✔
1400
    } else {
1401
      bin_next = C_NONE;
2,186,063✔
1402
    }
1403
  }
1404

1405
  return distance;
5,372,191✔
1406
}
1407

1408
//==============================================================================
1409
// RegularMesh implementation
1410
//==============================================================================
1411

1412
int RegularMesh::set_grid()
2,371✔
1413
{
1414
  tensor::Tensor<int> shape(shape_.data(), static_cast<size_t>(n_dimension_));
2,371✔
1415

1416
  // Check that dimensions are all greater than zero
1417
  if ((shape <= 0).any()) {
7,113!
1418
    set_errmsg("All entries for a regular mesh dimensions "
×
1419
               "must be positive.");
1420
    return OPENMC_E_INVALID_ARGUMENT;
×
1421
  }
1422

1423
  // Make sure lower_left and dimension match
1424
  if (lower_left_.size() != n_dimension_) {
2,371!
1425
    set_errmsg("Number of entries in lower_left must be the same "
×
1426
               "as the regular mesh dimensions.");
1427
    return OPENMC_E_INVALID_ARGUMENT;
×
1428
  }
1429
  if (width_.size() > 0) {
2,371✔
1430

1431
    // Check to ensure width has same dimensions
1432
    if (width_.size() != n_dimension_) {
46!
1433
      set_errmsg("Number of entries on width must be the same as "
×
1434
                 "the regular mesh dimensions.");
1435
      return OPENMC_E_INVALID_ARGUMENT;
×
1436
    }
1437

1438
    // Check for negative widths
1439
    if ((width_ < 0.0).any()) {
138!
1440
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
1441
      return OPENMC_E_INVALID_ARGUMENT;
×
1442
    }
1443

1444
    // Set width and upper right coordinate
1445
    upper_right_ = lower_left_ + shape * width_;
138✔
1446

1447
  } else if (upper_right_.size() > 0) {
2,325!
1448

1449
    // Check to ensure upper_right_ has same dimensions
1450
    if (upper_right_.size() != n_dimension_) {
2,325!
1451
      set_errmsg("Number of entries on upper_right must be the "
×
1452
                 "same as the regular mesh dimensions.");
1453
      return OPENMC_E_INVALID_ARGUMENT;
×
1454
    }
1455

1456
    // Check that upper-right is above lower-left
1457
    if ((upper_right_ < lower_left_).any()) {
6,975!
1458
      set_errmsg(
×
1459
        "The upper_right coordinates of a regular mesh must be greater than "
1460
        "the lower_left coordinates.");
1461
      return OPENMC_E_INVALID_ARGUMENT;
×
1462
    }
1463

1464
    // Set width
1465
    width_ = (upper_right_ - lower_left_) / shape;
6,975✔
1466
  }
1467

1468
  // Set material volumes
1469
  volume_frac_ = 1.0 / shape.prod();
2,371✔
1470

1471
  element_volume_ = 1.0;
2,371✔
1472
  for (int i = 0; i < n_dimension_; i++) {
8,937✔
1473
    element_volume_ *= width_[i];
6,566✔
1474
  }
1475
  return 0;
1476
}
2,371✔
1477

1478
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
2,360✔
1479
{
1480
  // Determine number of dimensions for mesh
1481
  if (!check_for_node(node, "dimension")) {
2,360!
1482
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1483
  }
1484

1485
  tensor::Tensor<int> shape = get_node_tensor<int>(node, "dimension");
2,360✔
1486
  int n = n_dimension_ = shape.size();
2,360!
1487
  if (n != 1 && n != 2 && n != 3) {
2,360!
1488
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1489
  }
1490
  std::copy(shape.begin(), shape.end(), shape_.begin());
2,360✔
1491

1492
  // Check for lower-left coordinates
1493
  if (check_for_node(node, "lower_left")) {
2,360!
1494
    // Read mesh lower-left corner location
1495
    lower_left_ = get_node_tensor<double>(node, "lower_left");
2,360✔
1496
  } else {
1497
    fatal_error("Must specify <lower_left> on a mesh.");
×
1498
  }
1499

1500
  if (check_for_node(node, "width")) {
2,360✔
1501
    // Make sure one of upper-right or width were specified
1502
    if (check_for_node(node, "upper_right")) {
46!
1503
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1504
    }
1505

1506
    width_ = get_node_tensor<double>(node, "width");
92✔
1507

1508
  } else if (check_for_node(node, "upper_right")) {
2,314!
1509

1510
    upper_right_ = get_node_tensor<double>(node, "upper_right");
4,628✔
1511

1512
  } else {
1513
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1514
  }
1515

1516
  if (int err = set_grid()) {
2,360!
1517
    fatal_error(openmc_err_msg);
×
1518
  }
1519
}
2,360✔
1520

1521
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
11✔
1522
{
1523
  // Determine number of dimensions for mesh
1524
  if (!object_exists(group, "dimension")) {
11!
1525
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1526
  }
1527

1528
  tensor::Tensor<int> shape;
11✔
1529
  read_dataset(group, "dimension", shape);
11✔
1530
  int n = n_dimension_ = shape.size();
11!
1531
  if (n != 1 && n != 2 && n != 3) {
11!
1532
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1533
  }
1534
  std::copy(shape.begin(), shape.end(), shape_.begin());
11✔
1535

1536
  // Check for lower-left coordinates
1537
  if (object_exists(group, "lower_left")) {
11!
1538
    // Read mesh lower-left corner location
1539
    read_dataset(group, "lower_left", lower_left_);
11✔
1540
  } else {
1541
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1542
  }
1543

1544
  if (object_exists(group, "upper_right")) {
11!
1545

1546
    read_dataset(group, "upper_right", upper_right_);
11✔
1547

1548
  } else {
1549
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1550
  }
1551

1552
  if (int err = set_grid()) {
11!
1553
    fatal_error(openmc_err_msg);
×
1554
  }
1555
}
11✔
1556

1557
int RegularMesh::get_index_in_direction(double r, int i) const
2,147,483,647✔
1558
{
1559
  if (r == lower_left_[i]) {
2,147,483,647✔
1560
    return 1;
1561
  } else {
1562
    return std::ceil((r - lower_left_[i]) / width_[i]);
2,147,483,647✔
1563
  }
1564
}
1565

1566
const std::string RegularMesh::mesh_type = "regular";
1567

1568
std::string RegularMesh::get_mesh_type() const
3,412✔
1569
{
1570
  return mesh_type;
3,412✔
1571
}
1572

1573
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,615,549,854✔
1574
{
1575
  return lower_left_[i] + ijk[i] * width_[i];
1,615,549,854✔
1576
}
1577

1578
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,545,999,396✔
1579
{
1580
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,545,999,396✔
1581
}
1582

1583
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1584
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1585
  double l) const
1586
{
1587
  MeshDistance d;
2,147,483,647✔
1588
  d.next_index = ijk[i];
2,147,483,647✔
1589
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1590
    return d;
15,272,506✔
1591

1592
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1593
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1594
    d.next_index++;
1,611,293,139✔
1595
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,611,293,139✔
1596
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,567,422,847✔
1597
    d.next_index--;
1,541,742,681✔
1598
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,541,742,681✔
1599
  }
1600

1601
  return d;
2,147,483,647✔
1602
}
1603

1604
double RegularMesh::distance_to_mesh_boundary_from_outside(
7,141,068✔
1605
  int k, const Position& r, const Direction& u) const
1606
{
1607
  double t;
7,141,068✔
1608
  double distance = INFTY;
7,141,068✔
1609

1610
  if (u[k] > 0.0) {
7,141,068✔
1611
    t = (lower_left_[k] - r[k]) / u[k];
3,569,302✔
1612
  } else {
1613
    t = (upper_right_[k] - r[k]) / u[k];
3,571,766✔
1614
  }
1615

1616
  if (t > FP_COINCIDENT) {
7,141,068✔
1617
    bool reenter = true;
1618
    for (int i = 0; i < n_dimension_; i++) {
9,510,072✔
1619
      if (i != k) {
7,132,554✔
1620
        double a = r[i] + u[i] * t;
4,755,036✔
1621
        reenter &= (a >= lower_left_[i]);
4,755,036✔
1622
        reenter &= (a <= upper_right_[i]);
4,755,036✔
1623
      }
1624
    }
1625
    if (reenter) {
2,377,518✔
1626
      distance = t;
194,304✔
1627
    }
1628
  }
1629
  return distance;
7,141,068✔
1630
}
1631

1632
std::pair<vector<double>, vector<double>> RegularMesh::plot(
22✔
1633
  Position plot_ll, Position plot_ur) const
1634
{
1635
  // Figure out which axes lie in the plane of the plot.
1636
  array<int, 2> axes {-1, -1};
22✔
1637
  if (plot_ur.z == plot_ll.z) {
22!
1638
    axes[0] = 0;
22!
1639
    if (n_dimension_ > 1)
22!
1640
      axes[1] = 1;
22✔
1641
  } else if (plot_ur.y == plot_ll.y) {
×
1642
    axes[0] = 0;
×
1643
    if (n_dimension_ > 2)
×
1644
      axes[1] = 2;
×
1645
  } else if (plot_ur.x == plot_ll.x) {
×
1646
    if (n_dimension_ > 1)
×
1647
      axes[0] = 1;
×
1648
    if (n_dimension_ > 2)
×
1649
      axes[1] = 2;
×
1650
  } else {
1651
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1652
  }
1653

1654
  // Get the coordinates of the mesh lines along both of the axes.
1655
  array<vector<double>, 2> axis_lines;
1656
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
66✔
1657
    int axis = axes[i_ax];
44!
1658
    if (axis == -1)
44!
1659
      continue;
×
1660
    auto& lines {axis_lines[i_ax]};
44✔
1661

1662
    double coord = lower_left_[axis];
44✔
1663
    for (int i = 0; i < shape_[axis] + 1; ++i) {
286✔
1664
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
242!
1665
        lines.push_back(coord);
242✔
1666
      coord += width_[axis];
242✔
1667
    }
1668
  }
1669

1670
  return {axis_lines[0], axis_lines[1]};
44✔
1671
}
1672

1673
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
2,257✔
1674
{
1675
  write_dataset(mesh_group, "dimension", get_shape_tensor());
2,257✔
1676
  write_dataset(mesh_group, "lower_left", lower_left_);
2,257✔
1677
  write_dataset(mesh_group, "upper_right", upper_right_);
2,257✔
1678
  write_dataset(mesh_group, "width", width_);
2,257✔
1679
}
2,257✔
1680

1681
tensor::Tensor<double> RegularMesh::count_sites(
7,820✔
1682
  const SourceSite* bank, int64_t length, bool* outside) const
1683
{
1684
  // Determine shape of array for counts
1685
  std::size_t m = this->n_bins();
7,820✔
1686
  vector<std::size_t> shape = {m};
7,820✔
1687

1688
  // Create array of zeros
1689
  auto cnt = tensor::zeros<double>(shape);
7,820✔
1690
  bool outside_ = false;
2,892✔
1691

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

1695
    // determine scoring bin for entropy mesh
1696
    int mesh_bin = get_bin(site.r);
7,667,451✔
1697

1698
    // if outside mesh, skip particle
1699
    if (mesh_bin < 0) {
7,667,451!
1700
      outside_ = true;
×
1701
      continue;
×
1702
    }
1703

1704
    // Add to appropriate bin
1705
    cnt(mesh_bin) += site.wgt;
7,667,451✔
1706
  }
1707

1708
  // Create reduced count data
1709
  auto counts = tensor::zeros<double>(shape);
7,820✔
1710
  int total = cnt.size();
7,820✔
1711

1712
#ifdef OPENMC_MPI
1713
  // collect values from all processors
1714
  MPI_Reduce(
2,892✔
1715
    cnt.data(), counts.data(), total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
2,892✔
1716

1717
  // Check if there were sites outside the mesh for any processor
1718
  if (outside) {
2,892!
1719
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
2,892✔
1720
  }
1721
#else
1722
  std::copy(cnt.data(), cnt.data() + total, counts.data());
4,928✔
1723
  if (outside)
4,928!
1724
    *outside = outside_;
4,928✔
1725
#endif
1726

1727
  return counts;
7,820✔
1728
}
7,820✔
1729

1730
double RegularMesh::volume(const MeshIndex& ijk) const
1,122,487✔
1731
{
1732
  return element_volume_;
1,122,487✔
1733
}
1734

1735
//==============================================================================
1736
// RectilinearMesh implementation
1737
//==============================================================================
1738

1739
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
133✔
1740
{
1741
  n_dimension_ = 3;
133✔
1742

1743
  grid_[0] = get_node_array<double>(node, "x_grid");
133✔
1744
  grid_[1] = get_node_array<double>(node, "y_grid");
133✔
1745
  grid_[2] = get_node_array<double>(node, "z_grid");
133✔
1746

1747
  if (int err = set_grid()) {
133!
1748
    fatal_error(openmc_err_msg);
×
1749
  }
1750
}
133✔
1751

1752
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
11✔
1753
{
1754
  n_dimension_ = 3;
11✔
1755

1756
  read_dataset(group, "x_grid", grid_[0]);
11✔
1757
  read_dataset(group, "y_grid", grid_[1]);
11✔
1758
  read_dataset(group, "z_grid", grid_[2]);
11✔
1759

1760
  if (int err = set_grid()) {
11!
1761
    fatal_error(openmc_err_msg);
×
1762
  }
1763
}
11✔
1764

1765
const std::string RectilinearMesh::mesh_type = "rectilinear";
1766

1767
std::string RectilinearMesh::get_mesh_type() const
275✔
1768
{
1769
  return mesh_type;
275✔
1770
}
1771

1772
double RectilinearMesh::positive_grid_boundary(
26,506,007✔
1773
  const MeshIndex& ijk, int i) const
1774
{
1775
  return grid_[i][ijk[i]];
26,506,007✔
1776
}
1777

1778
double RectilinearMesh::negative_grid_boundary(
25,739,406✔
1779
  const MeshIndex& ijk, int i) const
1780
{
1781
  return grid_[i][ijk[i] - 1];
25,739,406✔
1782
}
1783

1784
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
53,602,252✔
1785
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1786
  double l) const
1787
{
1788
  MeshDistance d;
53,602,252✔
1789
  d.next_index = ijk[i];
53,602,252✔
1790
  if (std::abs(u[i]) < FP_PRECISION)
53,602,252✔
1791
    return d;
571,934✔
1792

1793
  d.max_surface = (u[i] > 0);
53,030,318✔
1794
  if (d.max_surface && (ijk[i] <= shape_[i])) {
53,030,318✔
1795
    d.next_index++;
26,506,007✔
1796
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
26,506,007✔
1797
  } else if (!d.max_surface && (ijk[i] > 0)) {
26,524,311✔
1798
    d.next_index--;
25,739,406✔
1799
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
25,739,406✔
1800
  }
1801
  return d;
53,030,318✔
1802
}
1803

1804
double RectilinearMesh::distance_to_mesh_boundary_from_outside(
66✔
1805
  int k, const Position& r, const Direction& u) const
1806
{
1807
  double t;
66✔
1808
  double distance = INFTY;
66✔
1809

1810
  if (u[k] > 0.0) {
66✔
1811
    t = (lower_left_[k] - r[k]) / u[k];
11✔
1812
  } else {
1813
    t = (upper_right_[k] - r[k]) / u[k];
55✔
1814
  }
1815

1816
  if (t > FP_COINCIDENT) {
66✔
1817
    bool reenter = true;
1818
    for (int i = 0; i < n_dimension_; i++) {
220✔
1819
      if (i != k) {
165✔
1820
        double a = r[i] + u[i] * t;
110✔
1821
        reenter &= (a >= lower_left_[i]);
110✔
1822
        reenter &= (a <= upper_right_[i]);
110✔
1823
      }
1824
    }
1825
    if (reenter) {
55✔
1826
      distance = t;
11✔
1827
    }
1828
  }
1829
  return distance;
66✔
1830
}
1831

1832
int RectilinearMesh::set_grid()
188✔
1833
{
1834
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
188✔
1835
    static_cast<int>(grid_[1].size()) - 1,
188✔
1836
    static_cast<int>(grid_[2].size()) - 1};
188✔
1837

1838
  for (const auto& g : grid_) {
752✔
1839
    if (g.size() < 2) {
564!
1840
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1841
                 "must each have at least 2 points");
1842
      return OPENMC_E_INVALID_ARGUMENT;
×
1843
    }
1844
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
564!
1845
        g.end()) {
564!
1846
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1847
                 "rectilinear meshes must be sorted and unique.");
1848
      return OPENMC_E_INVALID_ARGUMENT;
×
1849
    }
1850
  }
1851

1852
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
188✔
1853
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
188✔
1854

1855
  return 0;
188✔
1856
}
1857

1858
int RectilinearMesh::get_index_in_direction(double r, int i) const
74,108,925✔
1859
{
1860
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,108,925✔
1861
}
1862

1863
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
11✔
1864
  Position plot_ll, Position plot_ur) const
1865
{
1866
  // Figure out which axes lie in the plane of the plot.
1867
  array<int, 2> axes {-1, -1};
11✔
1868
  if (plot_ur.z == plot_ll.z) {
11!
1869
    axes = {0, 1};
×
1870
  } else if (plot_ur.y == plot_ll.y) {
11!
1871
    axes = {0, 2};
11✔
1872
  } else if (plot_ur.x == plot_ll.x) {
×
1873
    axes = {1, 2};
×
1874
  } else {
1875
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1876
  }
1877

1878
  // Get the coordinates of the mesh lines along both of the axes.
1879
  array<vector<double>, 2> axis_lines;
1880
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
33✔
1881
    int axis = axes[i_ax];
22✔
1882
    vector<double>& lines {axis_lines[i_ax]};
22✔
1883

1884
    for (auto coord : grid_[axis]) {
110✔
1885
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
88!
1886
        lines.push_back(coord);
88✔
1887
    }
1888
  }
1889

1890
  return {axis_lines[0], axis_lines[1]};
22✔
1891
}
1892

1893
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
110✔
1894
{
1895
  write_dataset(mesh_group, "x_grid", grid_[0]);
110✔
1896
  write_dataset(mesh_group, "y_grid", grid_[1]);
110✔
1897
  write_dataset(mesh_group, "z_grid", grid_[2]);
110✔
1898
}
110✔
1899

1900
double RectilinearMesh::volume(const MeshIndex& ijk) const
132✔
1901
{
1902
  double vol {1.0};
132✔
1903

1904
  for (int i = 0; i < n_dimension_; i++) {
528✔
1905
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
396✔
1906
  }
1907
  return vol;
132✔
1908
}
1909

1910
//==============================================================================
1911
// CylindricalMesh implementation
1912
//==============================================================================
1913

1914
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
400✔
1915
  : PeriodicStructuredMesh {node}
400✔
1916
{
1917
  n_dimension_ = 3;
400✔
1918
  grid_[0] = get_node_array<double>(node, "r_grid");
400✔
1919
  grid_[1] = get_node_array<double>(node, "phi_grid");
400✔
1920
  grid_[2] = get_node_array<double>(node, "z_grid");
400✔
1921
  origin_ = get_node_position(node, "origin");
400✔
1922

1923
  if (int err = set_grid()) {
400!
1924
    fatal_error(openmc_err_msg);
×
1925
  }
1926
}
400✔
1927

1928
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1929
{
1930
  n_dimension_ = 3;
11✔
1931
  read_dataset(group, "r_grid", grid_[0]);
11✔
1932
  read_dataset(group, "phi_grid", grid_[1]);
11✔
1933
  read_dataset(group, "z_grid", grid_[2]);
11✔
1934
  read_dataset(group, "origin", origin_);
11✔
1935

1936
  if (int err = set_grid()) {
11!
1937
    fatal_error(openmc_err_msg);
×
1938
  }
1939
}
11✔
1940

1941
const std::string CylindricalMesh::mesh_type = "cylindrical";
1942

1943
std::string CylindricalMesh::get_mesh_type() const
484✔
1944
{
1945
  return mesh_type;
484✔
1946
}
1947

1948
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
47,732,091✔
1949
  Position r, bool& in_mesh) const
1950
{
1951
  r = local_coords(r);
47,732,091✔
1952

1953
  Position mapped_r;
47,732,091✔
1954
  mapped_r[0] = std::hypot(r.x, r.y);
47,732,091✔
1955
  mapped_r[2] = r[2];
47,732,091✔
1956

1957
  if (mapped_r[0] < FP_PRECISION) {
47,732,091!
1958
    mapped_r[1] = 0.0;
1959
  } else {
1960
    mapped_r[1] = std::atan2(r.y, r.x);
47,732,091✔
1961
    if (mapped_r[1] < 0)
47,732,091✔
1962
      mapped_r[1] += 2 * M_PI;
23,874,862✔
1963
  }
1964

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

1967
  idx[1] = sanitize_phi(idx[1]);
47,732,091✔
1968

1969
  return idx;
47,732,091✔
1970
}
1971

1972
Position CylindricalMesh::sample_element(
88,110✔
1973
  const MeshIndex& ijk, uint64_t* seed) const
1974
{
1975
  double r_min = this->r(ijk[0] - 1);
88,110✔
1976
  double r_max = this->r(ijk[0]);
88,110✔
1977

1978
  double phi_min = this->phi(ijk[1] - 1);
88,110✔
1979
  double phi_max = this->phi(ijk[1]);
88,110✔
1980

1981
  double z_min = this->z(ijk[2] - 1);
88,110✔
1982
  double z_max = this->z(ijk[2]);
88,110✔
1983

1984
  double r_min_sq = r_min * r_min;
88,110✔
1985
  double r_max_sq = r_max * r_max;
88,110✔
1986
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
88,110✔
1987
  double phi = uniform_distribution(phi_min, phi_max, seed);
88,110✔
1988
  double z = uniform_distribution(z_min, z_max, seed);
88,110✔
1989

1990
  double x = r * std::cos(phi);
88,110✔
1991
  double y = r * std::sin(phi);
88,110✔
1992

1993
  return origin_ + Position(x, y, z);
88,110✔
1994
}
1995

1996
double CylindricalMesh::find_r_crossing(
142,588,486✔
1997
  const Position& r, const Direction& u, double l, int shell) const
1998
{
1999

2000
  if ((shell < 0) || (shell > shape_[0]))
142,588,486!
2001
    return INFTY;
2002

2003
  // solve r.x^2 + r.y^2 == r0^2
2004
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
2005
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
2006

2007
  const double r0 = grid_[0][shell];
124,674,511✔
2008
  if (r0 == 0.0)
124,674,511✔
2009
    return INFTY;
2010

2011
  const double denominator = u.x * u.x + u.y * u.y;
117,538,437✔
2012

2013
  // Direction of flight is in z-direction. Will never intersect r.
2014
  if (std::abs(denominator) < FP_PRECISION)
117,538,437✔
2015
    return INFTY;
2016

2017
  // inverse of dominator to help the compiler to speed things up
2018
  const double inv_denominator = 1.0 / denominator;
117,479,477✔
2019

2020
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,479,477✔
2021
  double R = std::sqrt(r.x * r.x + r.y * r.y);
117,479,477✔
2022
  double D = p * p - (R - r0) * (R + r0) * inv_denominator;
117,479,477✔
2023

2024
  if (D < 0.0)
117,479,477✔
2025
    return INFTY;
2026

2027
  D = std::sqrt(D);
107,743,355✔
2028

2029
  // Particle is already on the shell surface; avoid spurious crossing
2030
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
107,743,355✔
2031
    return INFTY;
2032

2033
  // Check -p - D first because it is always smaller as -p + D
2034
  if (-p - D > l)
101,109,981✔
2035
    return -p - D;
2036
  if (-p + D > l)
80,902,376✔
2037
    return -p + D;
50,078,497✔
2038

2039
  return INFTY;
2040
}
2041

2042
double CylindricalMesh::find_phi_crossing(
74,456,404✔
2043
  const Position& r, const Direction& u, double l, int shell) const
2044
{
2045
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2046
  if (full_phi_ && (shape_[1] == 1))
74,456,404✔
2047
    return INFTY;
2048

2049
  shell = sanitize_phi(shell);
43,970,718✔
2050

2051
  const double p0 = grid_[1][shell];
43,970,718✔
2052

2053
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2054
  // => x(s) * cos(p0) = y(s) * sin(p0)
2055
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2056
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2057

2058
  const double c0 = std::cos(p0);
43,970,718✔
2059
  const double s0 = std::sin(p0);
43,970,718✔
2060

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

2063
  // Check if direction of flight is not parallel to phi surface
2064
  if (std::abs(denominator) > FP_PRECISION) {
43,970,718✔
2065
    const double s = -(r.x * s0 - r.y * c0) / denominator;
43,709,974✔
2066
    // Check if solution is in positive direction of flight and crosses the
2067
    // correct phi surface (not -phi)
2068
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
43,709,974✔
2069
      return s;
20,219,859✔
2070
  }
2071

2072
  return INFTY;
2073
}
2074

2075
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
36,695,747✔
2076
  const Position& r, const Direction& u, double l, int shell) const
2077
{
2078
  MeshDistance d;
36,695,747✔
2079
  d.next_index = shell;
36,695,747✔
2080

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

2085
  d.max_surface = (u.z > 0.0);
35,577,531✔
2086
  if (d.max_surface && (shell <= shape_[2])) {
35,577,531✔
2087
    d.next_index += 1;
16,875,892✔
2088
    d.distance = (grid_[2][shell] - r.z) / u.z;
16,875,892✔
2089
  } else if (!d.max_surface && (shell > 0)) {
18,701,639✔
2090
    d.next_index -= 1;
16,846,225✔
2091
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
16,846,225✔
2092
  }
2093
  return d;
35,577,531✔
2094
}
2095

2096
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,218,192✔
2097
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2098
  double l) const
2099
{
2100
  if (i == 0) {
145,218,192✔
2101

2102
    return std::min(
142,588,486✔
2103
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
71,294,243✔
2104
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,588,486✔
2105

2106
  } else if (i == 1) {
73,923,949✔
2107

2108
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
37,228,202✔
2109
                      find_phi_crossing(r0, u, l, ijk[i])),
37,228,202✔
2110
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
37,228,202✔
2111
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
74,456,404✔
2112

2113
  } else {
2114
    return find_z_crossing(r0, u, l, ijk[i]);
36,695,747✔
2115
  }
2116
}
2117

2118
int CylindricalMesh::set_grid()
433✔
2119
{
2120
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
433✔
2121
    static_cast<int>(grid_[1].size()) - 1,
433✔
2122
    static_cast<int>(grid_[2].size()) - 1};
433✔
2123

2124
  for (const auto& g : grid_) {
1,732✔
2125
    if (g.size() < 2) {
1,299!
2126
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
2127
                 "must each have at least 2 points");
2128
      return OPENMC_E_INVALID_ARGUMENT;
×
2129
    }
2130
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,299!
2131
        g.end()) {
1,299!
2132
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
2133
                 "cylindrical meshes must be sorted and unique.");
2134
      return OPENMC_E_INVALID_ARGUMENT;
×
2135
    }
2136
  }
2137
  if (grid_[0].front() < 0.0) {
433!
2138
    set_errmsg("r-grid for "
×
2139
               "cylindrical meshes must start at r >= 0.");
2140
    return OPENMC_E_INVALID_ARGUMENT;
×
2141
  }
2142
  if (grid_[1].front() < 0.0) {
433!
2143
    set_errmsg("phi-grid for "
×
2144
               "cylindrical meshes must start at phi >= 0.");
2145
    return OPENMC_E_INVALID_ARGUMENT;
×
2146
  }
2147
  if (grid_[1].back() > 2.0 * PI) {
433!
2148
    set_errmsg("phi-grids for "
×
2149
               "cylindrical meshes must end with theta <= 2*pi.");
2150

2151
    return OPENMC_E_INVALID_ARGUMENT;
×
2152
  }
2153

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

2156
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
433✔
2157
    origin_[2] + grid_[2].front()};
433✔
2158
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
433✔
2159
    origin_[2] + grid_[2].back()};
433✔
2160

2161
  return 0;
433✔
2162
}
2163

2164
int CylindricalMesh::get_index_in_direction(double r, int i) const
143,196,273✔
2165
{
2166
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
143,196,273✔
2167
}
2168

2169
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
2170
  Position plot_ll, Position plot_ur) const
2171
{
2172
  fatal_error("Plot of cylindrical Mesh not implemented");
×
2173

2174
  // Figure out which axes lie in the plane of the plot.
2175
  array<vector<double>, 2> axis_lines;
2176
  return {axis_lines[0], axis_lines[1]};
2177
}
2178

2179
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
374✔
2180
{
2181
  write_dataset(mesh_group, "r_grid", grid_[0]);
374✔
2182
  write_dataset(mesh_group, "phi_grid", grid_[1]);
374✔
2183
  write_dataset(mesh_group, "z_grid", grid_[2]);
374✔
2184
  write_dataset(mesh_group, "origin", origin_);
374✔
2185
}
374✔
2186

2187
double CylindricalMesh::volume(const MeshIndex& ijk) const
792✔
2188
{
2189
  double r_i = grid_[0][ijk[0] - 1];
792✔
2190
  double r_o = grid_[0][ijk[0]];
792✔
2191

2192
  double phi_i = grid_[1][ijk[1] - 1];
792✔
2193
  double phi_o = grid_[1][ijk[1]];
792✔
2194

2195
  double z_i = grid_[2][ijk[2] - 1];
792✔
2196
  double z_o = grid_[2][ijk[2]];
792✔
2197

2198
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
792✔
2199
}
2200

2201
//==============================================================================
2202
// SphericalMesh implementation
2203
//==============================================================================
2204

2205
SphericalMesh::SphericalMesh(pugi::xml_node node)
345✔
2206
  : PeriodicStructuredMesh {node}
345✔
2207
{
2208
  n_dimension_ = 3;
345✔
2209

2210
  grid_[0] = get_node_array<double>(node, "r_grid");
345✔
2211
  grid_[1] = get_node_array<double>(node, "theta_grid");
345✔
2212
  grid_[2] = get_node_array<double>(node, "phi_grid");
345✔
2213
  origin_ = get_node_position(node, "origin");
345✔
2214

2215
  if (int err = set_grid()) {
345!
2216
    fatal_error(openmc_err_msg);
×
2217
  }
2218
}
345✔
2219

2220
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
2221
{
2222
  n_dimension_ = 3;
11✔
2223

2224
  read_dataset(group, "r_grid", grid_[0]);
11✔
2225
  read_dataset(group, "theta_grid", grid_[1]);
11✔
2226
  read_dataset(group, "phi_grid", grid_[2]);
11✔
2227
  read_dataset(group, "origin", origin_);
11✔
2228

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

2234
const std::string SphericalMesh::mesh_type = "spherical";
2235

2236
std::string SphericalMesh::get_mesh_type() const
385✔
2237
{
2238
  return mesh_type;
385✔
2239
}
2240

2241
StructuredMesh::MeshIndex SphericalMesh::get_indices(
68,592,128✔
2242
  Position r, bool& in_mesh) const
2243
{
2244
  r = local_coords(r);
68,592,128✔
2245

2246
  Position mapped_r;
68,592,128✔
2247
  mapped_r[0] = r.norm();
68,592,128✔
2248

2249
  if (mapped_r[0] < FP_PRECISION) {
68,592,128!
2250
    mapped_r[1] = 0.0;
2251
    mapped_r[2] = 0.0;
2252
  } else {
2253
    mapped_r[1] = std::acos(r.z / mapped_r.x);
68,592,128✔
2254
    mapped_r[2] = std::atan2(r.y, r.x);
68,592,128✔
2255
    if (mapped_r[2] < 0)
68,592,128✔
2256
      mapped_r[2] += 2 * M_PI;
34,268,685✔
2257
  }
2258

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

2261
  idx[1] = sanitize_theta(idx[1]);
68,592,128✔
2262
  idx[2] = sanitize_phi(idx[2]);
68,592,128✔
2263

2264
  return idx;
68,592,128✔
2265
}
2266

2267
Position SphericalMesh::sample_element(
110✔
2268
  const MeshIndex& ijk, uint64_t* seed) const
2269
{
2270
  double r_min = this->r(ijk[0] - 1);
110✔
2271
  double r_max = this->r(ijk[0]);
110✔
2272

2273
  double theta_min = this->theta(ijk[1] - 1);
110✔
2274
  double theta_max = this->theta(ijk[1]);
110✔
2275

2276
  double phi_min = this->phi(ijk[2] - 1);
110✔
2277
  double phi_max = this->phi(ijk[2]);
110✔
2278

2279
  double cos_theta =
110✔
2280
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
110✔
2281
  double sin_theta = std::sin(std::acos(cos_theta));
110✔
2282
  double phi = uniform_distribution(phi_min, phi_max, seed);
110✔
2283
  double r_min_cub = std::pow(r_min, 3);
110✔
2284
  double r_max_cub = std::pow(r_max, 3);
110✔
2285
  // might be faster to do rejection here?
2286
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110✔
2287

2288
  double x = r * std::cos(phi) * sin_theta;
110✔
2289
  double y = r * std::sin(phi) * sin_theta;
110✔
2290
  double z = r * cos_theta;
110✔
2291

2292
  return origin_ + Position(x, y, z);
110✔
2293
}
2294

2295
double SphericalMesh::find_r_crossing(
443,981,868✔
2296
  const Position& r, const Direction& u, double l, int shell) const
2297
{
2298
  if ((shell < 0) || (shell > shape_[0]))
443,981,868✔
2299
    return INFTY;
2300

2301
  // solve |r+s*u| = r0
2302
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2303
  const double r0 = grid_[0][shell];
404,360,781✔
2304
  if (r0 == 0.0)
404,360,781✔
2305
    return INFTY;
2306
  const double p = r.dot(u);
396,682,264✔
2307
  double R = r.norm();
396,682,264✔
2308
  double D = p * p - (R - r0) * (R + r0);
396,682,264✔
2309

2310
  // Particle is already on the shell surface; avoid spurious crossing
2311
  if (std::abs(R - r0) <= RADIAL_MESH_TOL * (1.0 + std::abs(r0)))
396,682,264✔
2312
    return INFTY;
2313

2314
  if (D >= 0.0) {
385,973,610✔
2315
    D = std::sqrt(D);
358,096,662✔
2316
    // Check -p - D first because it is always smaller as -p + D
2317
    if (-p - D > l)
358,096,662✔
2318
      return -p - D;
2319
    if (-p + D > l)
293,782,962✔
2320
      return -p + D;
177,242,120✔
2321
  }
2322

2323
  return INFTY;
2324
}
2325

2326
double SphericalMesh::find_theta_crossing(
110,161,348✔
2327
  const Position& r, const Direction& u, double l, int shell) const
2328
{
2329
  // Theta grid is [0, π], thus there is no real surface to cross
2330
  if (full_theta_ && (shape_[1] == 1))
110,161,348✔
2331
    return INFTY;
2332

2333
  shell = sanitize_theta(shell);
38,358,540✔
2334

2335
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2336
  // yields
2337
  // a*s^2 + 2*b*s + c == 0 with
2338
  // a = cos(theta)^2 - u.z * u.z
2339
  // b = r*u * cos(theta)^2 - u.z * r.z
2340
  // c = r*r * cos(theta)^2 - r.z^2
2341

2342
  const double cos_t = std::cos(grid_[1][shell]);
38,358,540✔
2343
  const bool sgn = std::signbit(cos_t);
38,358,540✔
2344
  const double cos_t_2 = cos_t * cos_t;
38,358,540✔
2345

2346
  const double a = cos_t_2 - u.z * u.z;
38,358,540✔
2347
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
38,358,540✔
2348
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
38,358,540✔
2349

2350
  // if factor of s^2 is zero, direction of flight is parallel to theta
2351
  // surface
2352
  if (std::abs(a) < FP_PRECISION) {
38,358,540✔
2353
    // if b vanishes, direction of flight is within theta surface and crossing
2354
    // is not possible
2355
    if (std::abs(b) < FP_PRECISION)
482,548!
2356
      return INFTY;
2357

2358
    const double s = -0.5 * c / b;
×
2359
    // Check if solution is in positive direction of flight and has correct
2360
    // sign
2361
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
2362
      return s;
×
2363

2364
    // no crossing is possible
2365
    return INFTY;
2366
  }
2367

2368
  const double p = b / a;
37,875,992✔
2369
  double D = p * p - c / a;
37,875,992✔
2370

2371
  if (D < 0.0)
37,875,992✔
2372
    return INFTY;
2373

2374
  D = std::sqrt(D);
26,921,004✔
2375

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

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

2387
  return INFTY;
2388
}
2389

2390
double SphericalMesh::find_phi_crossing(
111,750,826✔
2391
  const Position& r, const Direction& u, double l, int shell) const
2392
{
2393
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2394
  if (full_phi_ && (shape_[2] == 1))
111,750,826✔
2395
    return INFTY;
2396

2397
  shell = sanitize_phi(shell);
39,948,018✔
2398

2399
  const double p0 = grid_[2][shell];
39,948,018✔
2400

2401
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2402
  // => x(s) * cos(p0) = y(s) * sin(p0)
2403
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2404
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2405

2406
  const double c0 = std::cos(p0);
39,948,018✔
2407
  const double s0 = std::sin(p0);
39,948,018✔
2408

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

2411
  // Check if direction of flight is not parallel to phi surface
2412
  if (std::abs(denominator) > FP_PRECISION) {
39,948,018✔
2413
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,714,026✔
2414
    // Check if solution is in positive direction of flight and crosses the
2415
    // correct phi surface (not -phi)
2416
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,714,026✔
2417
      return s;
17,579,452✔
2418
  }
2419

2420
  return INFTY;
2421
}
2422

2423
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
332,947,021✔
2424
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2425
  double l) const
2426
{
2427

2428
  if (i == 0) {
332,947,021✔
2429
    return std::min(
443,981,868✔
2430
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
221,990,934✔
2431
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
443,981,868✔
2432

2433
  } else if (i == 1) {
110,956,087✔
2434
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
55,080,674✔
2435
                      find_theta_crossing(r0, u, l, ijk[i])),
55,080,674✔
2436
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
55,080,674✔
2437
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
110,161,348✔
2438

2439
  } else {
2440
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
55,875,413✔
2441
                      find_phi_crossing(r0, u, l, ijk[i])),
55,875,413✔
2442
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
55,875,413✔
2443
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
111,750,826✔
2444
  }
2445
}
2446

2447
int SphericalMesh::set_grid()
378✔
2448
{
2449
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
378✔
2450
    static_cast<int>(grid_[1].size()) - 1,
378✔
2451
    static_cast<int>(grid_[2].size()) - 1};
378✔
2452

2453
  for (const auto& g : grid_) {
1,512✔
2454
    if (g.size() < 2) {
1,134!
2455
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2456
                 "must each have at least 2 points");
2457
      return OPENMC_E_INVALID_ARGUMENT;
×
2458
    }
2459
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,134!
2460
        g.end()) {
1,134!
2461
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2462
                 "spherical meshes must be sorted and unique.");
2463
      return OPENMC_E_INVALID_ARGUMENT;
×
2464
    }
2465
    if (g.front() < 0.0) {
1,134!
2466
      set_errmsg("r-, theta-, and phi- grids for "
×
2467
                 "spherical meshes must start at v >= 0.");
2468
      return OPENMC_E_INVALID_ARGUMENT;
×
2469
    }
2470
  }
2471
  if (grid_[1].back() > PI) {
378!
2472
    set_errmsg("theta-grids for "
×
2473
               "spherical meshes must end with theta <= pi.");
2474

2475
    return OPENMC_E_INVALID_ARGUMENT;
×
2476
  }
2477
  if (grid_[2].back() > 2 * PI) {
378!
2478
    set_errmsg("phi-grids for "
×
2479
               "spherical meshes must end with phi <= 2*pi.");
2480
    return OPENMC_E_INVALID_ARGUMENT;
×
2481
  }
2482

2483
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
378!
2484
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
378✔
2485

2486
  double r = grid_[0].back();
378✔
2487
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
378✔
2488
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
378✔
2489

2490
  return 0;
378✔
2491
}
2492

2493
int SphericalMesh::get_index_in_direction(double r, int i) const
205,776,384✔
2494
{
2495
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
205,776,384✔
2496
}
2497

2498
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2499
  Position plot_ll, Position plot_ur) const
2500
{
2501
  fatal_error("Plot of spherical Mesh not implemented");
×
2502

2503
  // Figure out which axes lie in the plane of the plot.
2504
  array<vector<double>, 2> axis_lines;
2505
  return {axis_lines[0], axis_lines[1]};
2506
}
2507

2508
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
319✔
2509
{
2510
  write_dataset(mesh_group, "r_grid", grid_[0]);
319✔
2511
  write_dataset(mesh_group, "theta_grid", grid_[1]);
319✔
2512
  write_dataset(mesh_group, "phi_grid", grid_[2]);
319✔
2513
  write_dataset(mesh_group, "origin", origin_);
319✔
2514
}
319✔
2515

2516
double SphericalMesh::volume(const MeshIndex& ijk) const
935✔
2517
{
2518
  double r_i = grid_[0][ijk[0] - 1];
935✔
2519
  double r_o = grid_[0][ijk[0]];
935✔
2520

2521
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2522
  double theta_o = grid_[1][ijk[1]];
935✔
2523

2524
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2525
  double phi_o = grid_[2][ijk[2]];
935✔
2526

2527
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
1,870✔
2528
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
935✔
2529
}
2530

2531
//==============================================================================
2532
// Helper functions for the C API
2533
//==============================================================================
2534

2535
int check_mesh(int32_t index)
6,490✔
2536
{
2537
  if (index < 0 || index >= model::meshes.size()) {
6,490!
2538
    set_errmsg("Index in meshes array is out of bounds.");
×
2539
    return OPENMC_E_OUT_OF_BOUNDS;
×
2540
  }
2541
  return 0;
2542
}
2543

2544
template<class T>
2545
int check_mesh_type(int32_t index)
1,100✔
2546
{
2547
  if (int err = check_mesh(index))
1,100!
2548
    return err;
2549

2550
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,100!
2551
  if (!mesh) {
1,100!
2552
    set_errmsg("This function is not valid for input mesh.");
×
2553
    return OPENMC_E_INVALID_TYPE;
×
2554
  }
2555
  return 0;
2556
}
2557

2558
template<class T>
2559
bool is_mesh_type(int32_t index)
2560
{
2561
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2562
  return mesh;
2563
}
2564

2565
//==============================================================================
2566
// C API functions
2567
//==============================================================================
2568

2569
// Return the type of mesh as a C string
2570
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,496✔
2571
{
2572
  if (int err = check_mesh(index))
1,496!
2573
    return err;
2574

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

2577
  return 0;
1,496✔
2578
}
2579

2580
//! Extend the meshes array by n elements
2581
extern "C" int openmc_extend_meshes(
253✔
2582
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2583
{
2584
  if (index_start)
253!
2585
    *index_start = model::meshes.size();
253✔
2586
  std::string mesh_type;
253✔
2587

2588
  for (int i = 0; i < n; ++i) {
506✔
2589
    if (RegularMesh::mesh_type == type) {
253✔
2590
      model::meshes.push_back(make_unique<RegularMesh>());
165✔
2591
    } else if (RectilinearMesh::mesh_type == type) {
88✔
2592
      model::meshes.push_back(make_unique<RectilinearMesh>());
44✔
2593
    } else if (CylindricalMesh::mesh_type == type) {
44✔
2594
      model::meshes.push_back(make_unique<CylindricalMesh>());
22✔
2595
    } else if (SphericalMesh::mesh_type == type) {
22!
2596
      model::meshes.push_back(make_unique<SphericalMesh>());
22✔
2597
    } else {
2598
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2599
    }
2600
  }
2601
  if (index_end)
253!
2602
    *index_end = model::meshes.size() - 1;
×
2603

2604
  return 0;
253✔
2605
}
253✔
2606

2607
//! Adds a new unstructured mesh to OpenMC
2608
extern "C" int openmc_add_unstructured_mesh(
×
2609
  const char filename[], const char library[], int* id)
2610
{
2611
  std::string lib_name(library);
×
2612
  std::string mesh_file(filename);
×
2613
  bool valid_lib = false;
×
2614

2615
#ifdef OPENMC_DAGMC_ENABLED
2616
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2617
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2618
    valid_lib = true;
2619
  }
2620
#endif
2621

2622
#ifdef OPENMC_LIBMESH_ENABLED
2623
  if (lib_name == LibMesh::mesh_lib_type) {
×
2624
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2625
    valid_lib = true;
2626
  }
2627
#endif
2628

2629
  if (!valid_lib) {
×
2630
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2631
                           "by this build of OpenMC",
2632
      lib_name));
2633
    return OPENMC_E_INVALID_ARGUMENT;
×
2634
  }
2635

2636
  // auto-assign new ID
2637
  model::meshes.back()->set_id(-1);
×
2638
  *id = model::meshes.back()->id_;
2639

2640
  return 0;
2641
}
×
2642

2643
//! Return the index in the meshes array of a mesh with a given ID
2644
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
429✔
2645
{
2646
  auto pair = model::mesh_map.find(id);
429!
2647
  if (pair == model::mesh_map.end()) {
429!
2648
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
2649
    return OPENMC_E_INVALID_ID;
×
2650
  }
2651
  *index = pair->second;
429✔
2652
  return 0;
429✔
2653
}
2654

2655
//! Return the ID of a mesh
2656
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,827✔
2657
{
2658
  if (int err = check_mesh(index))
2,827!
2659
    return err;
2660
  *id = model::meshes[index]->id_;
2,827✔
2661
  return 0;
2,827✔
2662
}
2663

2664
//! Set the ID of a mesh
2665
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
253✔
2666
{
2667
  if (int err = check_mesh(index))
253!
2668
    return err;
2669
  model::meshes[index]->id_ = id;
253✔
2670
  model::mesh_map[id] = index;
253✔
2671
  return 0;
253✔
2672
}
2673

2674
//! Get the number of elements in a mesh
2675
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
297✔
2676
{
2677
  if (int err = check_mesh(index))
297!
2678
    return err;
2679
  *n = model::meshes[index]->n_bins();
297✔
2680
  return 0;
297✔
2681
}
2682

2683
//! Get the volume of each element in the mesh
2684
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
88✔
2685
{
2686
  if (int err = check_mesh(index))
88!
2687
    return err;
2688
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
968✔
2689
    volumes[i] = model::meshes[index]->volume(i);
880✔
2690
  }
2691
  return 0;
2692
}
2693

2694
//! Get the bounding box of a mesh
2695
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
176✔
2696
{
2697
  if (int err = check_mesh(index))
176!
2698
    return err;
2699

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

2702
  // set lower left corner values
2703
  ll[0] = bbox.min.x;
176✔
2704
  ll[1] = bbox.min.y;
176✔
2705
  ll[2] = bbox.min.z;
176✔
2706

2707
  // set upper right corner values
2708
  ur[0] = bbox.max.x;
176✔
2709
  ur[1] = bbox.max.y;
176✔
2710
  ur[2] = bbox.max.z;
176✔
2711
  return 0;
176✔
2712
}
2713

2714
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
209✔
2715
  int nz, int table_size, int32_t* materials, double* volumes, double* bboxes)
2716
{
2717
  if (int err = check_mesh(index))
209!
2718
    return err;
2719

2720
  try {
209✔
2721
    model::meshes[index]->material_volumes(
209✔
2722
      nx, ny, nz, table_size, materials, volumes, bboxes);
2723
  } catch (const std::exception& e) {
11!
2724
    set_errmsg(e.what());
11✔
2725
    if (starts_with(e.what(), "Mesh")) {
11!
2726
      return OPENMC_E_GEOMETRY;
11✔
2727
    } else {
2728
      return OPENMC_E_ALLOCATE;
×
2729
    }
2730
  }
11✔
2731

2732
  return 0;
2733
}
2734

2735
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
44✔
2736
  Position width, int basis, int* pixels, int32_t* data)
2737
{
2738
  if (int err = check_mesh(index))
44!
2739
    return err;
2740
  const auto& mesh = model::meshes[index].get();
44!
2741

2742
  int pixel_width = pixels[0];
44✔
2743
  int pixel_height = pixels[1];
44✔
2744

2745
  // get pixel size
2746
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
44✔
2747
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
44✔
2748

2749
  // setup basis indices and initial position centered on pixel
2750
  int in_i, out_i;
44✔
2751
  Position xyz = origin;
44✔
2752
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
44✔
2753
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
44✔
2754
  switch (basis_enum) {
44!
2755
  case PlotBasis::xy:
2756
    in_i = 0;
2757
    out_i = 1;
2758
    break;
2759
  case PlotBasis::xz:
2760
    in_i = 0;
2761
    out_i = 2;
2762
    break;
2763
  case PlotBasis::yz:
2764
    in_i = 1;
2765
    out_i = 2;
2766
    break;
2767
  default:
×
2768
    UNREACHABLE();
×
2769
  }
2770

2771
  // set initial position
2772
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
44✔
2773
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
44✔
2774

2775
#pragma omp parallel
24✔
2776
  {
20✔
2777
    Position r = xyz;
20✔
2778

2779
#pragma omp for
2780
    for (int y = 0; y < pixel_height; y++) {
420✔
2781
      r[out_i] = xyz[out_i] - out_pixel * y;
400✔
2782
      for (int x = 0; x < pixel_width; x++) {
8,400✔
2783
        r[in_i] = xyz[in_i] + in_pixel * x;
8,000✔
2784
        data[pixel_width * y + x] = mesh->get_bin(r);
8,000✔
2785
      }
2786
    }
2787
  }
2788

2789
  return 0;
44✔
2790
}
2791

2792
//! Get the dimension of a regular mesh
2793
extern "C" int openmc_regular_mesh_get_dimension(
11✔
2794
  int32_t index, int** dims, int* n)
2795
{
2796
  if (int err = check_mesh_type<RegularMesh>(index))
11!
2797
    return err;
2798
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
11!
2799
  *dims = mesh->shape_.data();
11✔
2800
  *n = mesh->n_dimension_;
11✔
2801
  return 0;
11✔
2802
}
2803

2804
//! Set the dimension of a regular mesh
2805
extern "C" int openmc_regular_mesh_set_dimension(
187✔
2806
  int32_t index, int n, const int* dims)
2807
{
2808
  if (int err = check_mesh_type<RegularMesh>(index))
187!
2809
    return err;
2810
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
187!
2811

2812
  // Copy dimension
2813
  mesh->n_dimension_ = n;
187✔
2814
  std::copy(dims, dims + n, mesh->shape_.begin());
187✔
2815
  return 0;
187✔
2816
}
2817

2818
//! Get the regular mesh parameters
2819
extern "C" int openmc_regular_mesh_get_params(
209✔
2820
  int32_t index, double** ll, double** ur, double** width, int* n)
2821
{
2822
  if (int err = check_mesh_type<RegularMesh>(index))
209!
2823
    return err;
2824
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
209!
2825

2826
  if (m->lower_left_.empty()) {
209!
2827
    set_errmsg("Mesh parameters have not been set.");
×
2828
    return OPENMC_E_ALLOCATE;
×
2829
  }
2830

2831
  *ll = m->lower_left_.data();
209✔
2832
  *ur = m->upper_right_.data();
209✔
2833
  *width = m->width_.data();
209✔
2834
  *n = m->n_dimension_;
209✔
2835
  return 0;
209✔
2836
}
2837

2838
//! Set the regular mesh parameters
2839
extern "C" int openmc_regular_mesh_set_params(
220✔
2840
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2841
{
2842
  if (int err = check_mesh_type<RegularMesh>(index))
220!
2843
    return err;
2844
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
220!
2845

2846
  if (m->n_dimension_ == -1) {
220!
2847
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
2848
    return OPENMC_E_UNASSIGNED;
×
2849
  }
2850

2851
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
220✔
2852
  if (ll && ur) {
220✔
2853
    m->lower_left_ = tensor::Tensor<double>(ll, n);
198✔
2854
    m->upper_right_ = tensor::Tensor<double>(ur, n);
198✔
2855
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_shape_tensor();
792✔
2856
  } else if (ll && width) {
22✔
2857
    m->lower_left_ = tensor::Tensor<double>(ll, n);
11✔
2858
    m->width_ = tensor::Tensor<double>(width, n);
11✔
2859
    m->upper_right_ = m->lower_left_ + m->get_shape_tensor() * m->width_;
44✔
2860
  } else if (ur && width) {
11!
2861
    m->upper_right_ = tensor::Tensor<double>(ur, n);
11✔
2862
    m->width_ = tensor::Tensor<double>(width, n);
11✔
2863
    m->lower_left_ = m->upper_right_ - m->get_shape_tensor() * m->width_;
44✔
2864
  } else {
2865
    set_errmsg("At least two parameters must be specified.");
×
2866
    return OPENMC_E_INVALID_ARGUMENT;
×
2867
  }
2868

2869
  // Set material volumes
2870

2871
  // TODO: incorporate this into method in RegularMesh that can be called from
2872
  // here and from constructor
2873
  m->volume_frac_ = 1.0 / m->get_shape_tensor().prod();
220✔
2874
  m->element_volume_ = 1.0;
220✔
2875
  for (int i = 0; i < m->n_dimension_; i++) {
880✔
2876
    m->element_volume_ *= m->width_[i];
660✔
2877
  }
2878

2879
  return 0;
2880
}
220✔
2881

2882
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2883
template<class C>
2884
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
88✔
2885
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2886
  const int nz)
2887
{
2888
  if (int err = check_mesh_type<C>(index))
88!
2889
    return err;
2890

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

2893
  m->n_dimension_ = 3;
88✔
2894

2895
  m->grid_[0].reserve(nx);
88✔
2896
  m->grid_[1].reserve(ny);
88✔
2897
  m->grid_[2].reserve(nz);
88✔
2898

2899
  for (int i = 0; i < nx; i++) {
572✔
2900
    m->grid_[0].push_back(grid_x[i]);
484✔
2901
  }
2902
  for (int i = 0; i < ny; i++) {
341✔
2903
    m->grid_[1].push_back(grid_y[i]);
253✔
2904
  }
2905
  for (int i = 0; i < nz; i++) {
319✔
2906
    m->grid_[2].push_back(grid_z[i]);
231✔
2907
  }
2908

2909
  int err = m->set_grid();
88✔
2910
  return err;
88✔
2911
}
2912

2913
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2914
template<class C>
2915
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
385✔
2916
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2917
{
2918
  if (int err = check_mesh_type<C>(index))
385!
2919
    return err;
2920
  C* m = dynamic_cast<C*>(model::meshes[index].get());
385!
2921

2922
  if (m->lower_left_.empty()) {
385!
2923
    set_errmsg("Mesh parameters have not been set.");
×
2924
    return OPENMC_E_ALLOCATE;
×
2925
  }
2926

2927
  *grid_x = m->grid_[0].data();
385✔
2928
  *nx = m->grid_[0].size();
385✔
2929
  *grid_y = m->grid_[1].data();
385✔
2930
  *ny = m->grid_[1].size();
385✔
2931
  *grid_z = m->grid_[2].data();
385✔
2932
  *nz = m->grid_[2].size();
385✔
2933

2934
  return 0;
385✔
2935
}
2936

2937
//! Get the rectilinear mesh grid
2938
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
143✔
2939
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2940
{
2941
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
143✔
2942
    index, grid_x, nx, grid_y, ny, grid_z, nz);
143✔
2943
}
2944

2945
//! Set the rectilienar mesh parameters
2946
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
44✔
2947
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2948
  const double* grid_z, const int nz)
2949
{
2950
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
44✔
2951
    index, grid_x, nx, grid_y, ny, grid_z, nz);
44✔
2952
}
2953

2954
//! Get the cylindrical mesh grid
2955
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2956
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2957
{
2958
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
121✔
2959
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2960
}
2961

2962
//! Set the cylindrical mesh parameters
2963
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
22✔
2964
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2965
  const double* grid_z, const int nz)
2966
{
2967
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
22✔
2968
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2969
}
2970

2971
//! Get the spherical mesh grid
2972
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2973
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2974
{
2975

2976
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
121✔
2977
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2978
  ;
121✔
2979
}
2980

2981
//! Set the spherical mesh parameters
2982
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
22✔
2983
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2984
  const double* grid_z, const int nz)
2985
{
2986
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
22✔
2987
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2988
}
2989

2990
#ifdef OPENMC_DAGMC_ENABLED
2991

2992
const std::string MOABMesh::mesh_lib_type = "moab";
2993

2994
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
2995
{
2996
  initialize();
24✔
2997
}
24!
2998

2999
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
3000
{
3001
  initialize();
×
3002
}
×
3003

3004
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
3005
  : UnstructuredMesh()
×
3006
{
3007
  n_dimension_ = 3;
3008
  filename_ = filename;
×
3009
  set_length_multiplier(length_multiplier);
×
3010
  initialize();
×
3011
}
×
3012

3013
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
3014
{
3015
  mbi_ = external_mbi;
1✔
3016
  filename_ = "unknown (external file)";
1✔
3017
  this->initialize();
1✔
3018
}
1!
3019

3020
void MOABMesh::initialize()
25✔
3021
{
3022

3023
  // Create the MOAB interface and load data from file
3024
  this->create_interface();
25✔
3025

3026
  // Initialise MOAB error code
3027
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
3028

3029
  // Set the dimension
3030
  n_dimension_ = 3;
25✔
3031

3032
  // set member range of tetrahedral entities
3033
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
3034
  if (rval != moab::MB_SUCCESS) {
25!
3035
    fatal_error("Failed to get all tetrahedral elements");
3036
  }
3037

3038
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
3039
    warning("Non-tetrahedral elements found in unstructured "
×
3040
            "mesh file: " +
3041
            filename_);
3042
  }
3043

3044
  // set member range of vertices
3045
  int vertex_dim = 0;
25✔
3046
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
3047
  if (rval != moab::MB_SUCCESS) {
25!
3048
    fatal_error("Failed to get all vertex handles");
3049
  }
3050

3051
  // make an entity set for all tetrahedra
3052
  // this is used for convenience later in output
3053
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
3054
  if (rval != moab::MB_SUCCESS) {
25!
3055
    fatal_error("Failed to create an entity set for the tetrahedral elements");
3056
  }
3057

3058
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
3059
  if (rval != moab::MB_SUCCESS) {
25!
3060
    fatal_error("Failed to add tetrahedra to an entity set.");
3061
  }
3062

3063
  if (length_multiplier_ > 0.0) {
25!
3064
    // get the connectivity of all tets
3065
    moab::Range adj;
×
3066
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
3067
    if (rval != moab::MB_SUCCESS) {
×
3068
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
3069
    }
3070
    // scale all vertex coords by multiplier (done individually so not all
3071
    // coordinates are in memory twice at once)
3072
    for (auto vert : adj) {
×
3073
      // retrieve coords
3074
      std::array<double, 3> coord;
3075
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
3076
      if (rval != moab::MB_SUCCESS) {
×
3077
        fatal_error("Could not get coordinates of vertex.");
3078
      }
3079
      // scale coords
3080
      for (auto& c : coord) {
×
3081
        c *= length_multiplier_;
3082
      }
3083
      // set new coords
3084
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
3085
      if (rval != moab::MB_SUCCESS) {
×
3086
        fatal_error("Failed to set new vertex coordinates");
3087
      }
3088
    }
3089
  }
3090

3091
  // Determine bounds of mesh
3092
  this->determine_bounds();
25✔
3093
}
25✔
3094

3095
void MOABMesh::prepare_for_point_location()
21✔
3096
{
3097
  // if the KDTree has already been constructed, do nothing
3098
  if (kdtree_)
21!
3099
    return;
3100

3101
  // build acceleration data structures
3102
  compute_barycentric_data(ehs_);
21✔
3103
  build_kdtree(ehs_);
21✔
3104
}
3105

3106
void MOABMesh::create_interface()
25✔
3107
{
3108
  // Do not create a MOAB instance if one is already in memory
3109
  if (mbi_)
25✔
3110
    return;
3111

3112
  // create MOAB instance
3113
  mbi_ = std::make_shared<moab::Core>();
24!
3114

3115
  // load unstructured mesh file
3116
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
3117
  if (rval != moab::MB_SUCCESS) {
24!
3118
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
3119
  }
3120
}
3121

3122
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
3123
{
3124
  moab::Range all_tris;
21✔
3125
  int adj_dim = 2;
21✔
3126
  write_message("Getting tet adjacencies...", 7);
21✔
3127
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
3128
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
3129
  if (rval != moab::MB_SUCCESS) {
21!
3130
    fatal_error("Failed to get adjacent triangles for tets");
3131
  }
3132

3133
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
3134
    warning("Non-triangle elements found in tet adjacencies in "
×
3135
            "unstructured mesh file: " +
3136
            filename_);
×
3137
  }
3138

3139
  // combine into one range
3140
  moab::Range all_tets_and_tris;
21✔
3141
  all_tets_and_tris.merge(all_tets);
21✔
3142
  all_tets_and_tris.merge(all_tris);
21✔
3143

3144
  // create a kd-tree instance
3145
  write_message(
21✔
3146
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
3147
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
3148

3149
  // Determine what options to use
3150
  std::ostringstream options_stream;
21✔
3151
  if (options_.empty()) {
21✔
3152
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
3153
  } else {
3154
    options_stream << options_;
16✔
3155
  }
3156
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
3157

3158
  // Build the k-d tree
3159
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
3160
  if (rval != moab::MB_SUCCESS) {
21!
3161
    fatal_error("Failed to construct KDTree for the "
3162
                "unstructured mesh file: " +
3163
                filename_);
×
3164
  }
3165
}
21✔
3166

3167
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
3168
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
3169
{
3170
  hits.clear();
1,543,584!
3171

3172
  moab::ErrorCode rval;
1,543,584✔
3173
  vector<moab::EntityHandle> tris;
1,543,584✔
3174
  // get all intersections with triangles in the tet mesh
3175
  // (distances are relative to the start point, not the previous
3176
  // intersection)
3177
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
3178
    dir.array(), start.array(), tris, hits, 0, track_len);
3179
  if (rval != moab::MB_SUCCESS) {
1,543,584!
3180
    fatal_error(
3181
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
3182
  }
3183

3184
  // remove duplicate intersection distances
3185
  std::unique(hits.begin(), hits.end());
1,543,584✔
3186

3187
  // sorts by first component of std::pair by default
3188
  std::sort(hits.begin(), hits.end());
1,543,584✔
3189
}
1,543,584✔
3190

3191
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
3192
  vector<int>& bins, vector<double>& lengths) const
3193
{
3194
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
3195
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
3196
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
3197
  dir.normalize();
1,543,584✔
3198

3199
  double track_len = (end - start).length();
1,543,584✔
3200
  if (track_len == 0.0)
1,543,584!
3201
    return;
721,692✔
3202

3203
  start -= TINY_BIT * dir;
1,543,584✔
3204
  end += TINY_BIT * dir;
1,543,584✔
3205

3206
  vector<double> hits;
1,543,584✔
3207
  intersect_track(start, dir, track_len, hits);
1,543,584✔
3208

3209
  bins.clear();
1,543,584!
3210
  lengths.clear();
1,543,584!
3211

3212
  // if there are no intersections the track may lie entirely
3213
  // within a single tet. If this is the case, apply entire
3214
  // score to that tet and return.
3215
  if (hits.size() == 0) {
1,543,584✔
3216
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
3217
    int bin = this->get_bin(midpoint);
721,692✔
3218
    if (bin != -1) {
721,692✔
3219
      bins.push_back(bin);
242,866✔
3220
      lengths.push_back(1.0);
242,866✔
3221
    }
3222
    return;
721,692✔
3223
  }
3224

3225
  // for each segment in the set of tracks, try to look up a tet
3226
  // at the midpoint of the segment
3227
  Position current = r0;
3228
  double last_dist = 0.0;
3229
  for (const auto& hit : hits) {
5,516,161✔
3230
    // get the segment length
3231
    double segment_length = hit - last_dist;
4,694,269✔
3232
    last_dist = hit;
4,694,269✔
3233
    // find the midpoint of this segment
3234
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
3235
    // try to find a tet for this position
3236
    int bin = this->get_bin(midpoint);
4,694,269✔
3237

3238
    // determine the start point for this segment
3239
    current = r0 + u * hit;
4,694,269✔
3240

3241
    if (bin == -1) {
4,694,269✔
3242
      continue;
20,522✔
3243
    }
3244

3245
    bins.push_back(bin);
4,673,747✔
3246
    lengths.push_back(segment_length / track_len);
4,673,747✔
3247
  }
3248

3249
  // tally remaining portion of track after last hit if
3250
  // the last segment of the track is in the mesh but doesn't
3251
  // reach the other side of the tet
3252
  if (hits.back() < track_len) {
821,892!
3253
    Position segment_start = r0 + u * hits.back();
821,892✔
3254
    double segment_length = track_len - hits.back();
821,892✔
3255
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
3256
    int bin = this->get_bin(midpoint);
821,892✔
3257
    if (bin != -1) {
821,892✔
3258
      bins.push_back(bin);
766,509✔
3259
      lengths.push_back(segment_length / track_len);
766,509✔
3260
    }
3261
  }
3262
};
1,543,584✔
3263

3264
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,232✔
3265
{
3266
  moab::CartVect pos(r.x, r.y, r.z);
7,317,232✔
3267
  // find the leaf of the kd-tree for this position
3268
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,232✔
3269
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,232✔
3270
  if (rval != moab::MB_SUCCESS) {
7,317,232✔
3271
    return 0;
3272
  }
3273

3274
  // retrieve the tet elements of this leaf
3275
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,335✔
3276
  moab::Range tets;
6,305,335✔
3277
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,335✔
3278
  if (rval != moab::MB_SUCCESS) {
6,305,335!
3279
    warning("MOAB error finding tets.");
×
3280
  }
3281

3282
  // loop over the tets in this leaf, returning the containing tet if found
3283
  for (const auto& tet : tets) {
260,211,273✔
3284
    if (point_in_tet(pos, tet)) {
260,208,426✔
3285
      return tet;
6,302,488✔
3286
    }
3287
  }
3288

3289
  // if no tet is found, return an invalid handle
3290
  return 0;
2,847✔
3291
}
14,634,464✔
3292

3293
double MOABMesh::volume(int bin) const
167,880✔
3294
{
3295
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3296
}
3297

3298
std::string MOABMesh::library() const
34✔
3299
{
3300
  return mesh_lib_type;
34✔
3301
}
3302

3303
// Sample position within a tet for MOAB type tets
3304
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3305
{
3306

3307
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3308

3309
  // Get vertex coordinates for MOAB tet
3310
  const moab::EntityHandle* conn1;
200,410✔
3311
  int conn1_size;
200,410✔
3312
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3313
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3314
    fatal_error(fmt::format(
3315
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3316
      conn1_size));
3317
  }
3318
  moab::CartVect p[4];
200,410✔
3319
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3320
  if (rval != moab::MB_SUCCESS) {
200,410!
3321
    fatal_error("Failed to get tet coords");
3322
  }
3323

3324
  std::array<Position, 4> tet_verts;
200,410✔
3325
  for (int i = 0; i < 4; i++) {
1,002,050✔
3326
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3327
  }
3328
  // Samples position within tet using Barycentric stuff
3329
  return this->sample_tet(tet_verts, seed);
200,410✔
3330
}
3331

3332
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3333
{
3334
  vector<moab::EntityHandle> conn;
167,880✔
3335
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3336
  if (rval != moab::MB_SUCCESS) {
167,880!
3337
    fatal_error("Failed to get tet connectivity");
3338
  }
3339

3340
  moab::CartVect p[4];
167,880✔
3341
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3342
  if (rval != moab::MB_SUCCESS) {
167,880!
3343
    fatal_error("Failed to get tet coords");
3344
  }
3345

3346
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
167,880✔
3347
}
167,880✔
3348

3349
int MOABMesh::get_bin(Position r) const
7,317,232✔
3350
{
3351
  moab::EntityHandle tet = get_tet(r);
7,317,232✔
3352
  if (tet == 0) {
7,317,232✔
3353
    return -1;
3354
  } else {
3355
    return get_bin_from_ent_handle(tet);
6,302,488✔
3356
  }
3357
}
3358

3359
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3360
{
3361
  moab::ErrorCode rval;
21✔
3362

3363
  baryc_data_.clear();
21!
3364
  baryc_data_.resize(tets.size());
21✔
3365

3366
  // compute the barycentric data for each tet element
3367
  // and store it as a 3x3 matrix
3368
  for (auto& tet : tets) {
239,757✔
3369
    vector<moab::EntityHandle> verts;
239,736✔
3370
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3371
    if (rval != moab::MB_SUCCESS) {
239,736!
3372
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3373
    }
3374

3375
    moab::CartVect p[4];
239,736✔
3376
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
239,736✔
3377
    if (rval != moab::MB_SUCCESS) {
239,736!
3378
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
×
3379
    }
3380

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

3383
    // invert now to avoid this cost later
3384
    a = a.transpose().inverse();
239,736✔
3385
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3386
  }
239,736✔
3387
}
21✔
3388

3389
bool MOABMesh::point_in_tet(
260,208,426✔
3390
  const moab::CartVect& r, moab::EntityHandle tet) const
3391
{
3392

3393
  moab::ErrorCode rval;
260,208,426✔
3394

3395
  // get tet vertices
3396
  vector<moab::EntityHandle> verts;
260,208,426✔
3397
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,208,426✔
3398
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3399
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3400
    return false;
3401
  }
3402

3403
  // first vertex is used as a reference point for the barycentric data -
3404
  // retrieve its coordinates
3405
  moab::CartVect p_zero;
260,208,426✔
3406
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,208,426✔
3407
  if (rval != moab::MB_SUCCESS) {
260,208,426!
3408
    warning("Failed to get coordinates of a vertex in "
×
3409
            "unstructured mesh: " +
3410
            filename_);
×
3411
    return false;
3412
  }
3413

3414
  // look up barycentric data
3415
  int idx = get_bin_from_ent_handle(tet);
260,208,426✔
3416
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,208,426✔
3417

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

3420
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
161,208,987✔
3421
          bary_coords[2] >= 0.0 &&
318,957,185✔
3422
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
21,688,225✔
3423
}
260,208,426✔
3424

3425
int MOABMesh::get_bin_from_index(int idx) const
3426
{
3427
  if (idx >= n_bins()) {
×
3428
    fatal_error(fmt::format("Invalid bin index: {}", idx));
3429
  }
3430
  return ehs_[idx] - ehs_[0];
3431
}
3432

3433
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3434
{
3435
  int bin = get_bin(r);
3436
  *in_mesh = bin != -1;
3437
  return bin;
3438
}
3439

3440
int MOABMesh::get_index_from_bin(int bin) const
3441
{
3442
  return bin;
3443
}
3444

3445
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3446
  Position plot_ll, Position plot_ur) const
3447
{
3448
  // TODO: Implement mesh lines
3449
  return {};
3450
}
3451

3452
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3453
{
3454
  int idx = vert - verts_[0];
815,520✔
3455
  if (idx >= n_vertices()) {
815,520!
3456
    fatal_error(
3457
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3458
  }
3459
  return idx;
815,520✔
3460
}
3461

3462
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,750,650✔
3463
{
3464
  int bin = eh - ehs_[0];
266,750,650✔
3465
  if (bin >= n_bins()) {
266,750,650!
3466
    fatal_error(fmt::format("Invalid bin: {}", bin));
3467
  }
3468
  return bin;
266,750,650✔
3469
}
3470

3471
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3472
{
3473
  if (bin >= n_bins()) {
572,170!
3474
    fatal_error(fmt::format("Invalid bin index: ", bin));
3475
  }
3476
  return ehs_[0] + bin;
572,170✔
3477
}
3478

3479
int MOABMesh::n_bins() const
267,526,773✔
3480
{
3481
  return ehs_.size();
267,526,773✔
3482
}
3483

3484
int MOABMesh::n_surface_bins() const
3485
{
3486
  // collect all triangles in the set of tets for this mesh
3487
  moab::Range tris;
×
3488
  moab::ErrorCode rval;
3489
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3490
  if (rval != moab::MB_SUCCESS) {
×
3491
    warning("Failed to get all triangles in the mesh instance");
×
3492
    return -1;
3493
  }
3494
  return 2 * tris.size();
×
3495
}
3496

3497
Position MOABMesh::centroid(int bin) const
3498
{
3499
  moab::ErrorCode rval;
3500

3501
  auto tet = this->get_ent_handle_from_bin(bin);
3502

3503
  // look up the tet connectivity
3504
  vector<moab::EntityHandle> conn;
×
3505
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3506
  if (rval != moab::MB_SUCCESS) {
×
3507
    warning("Failed to get connectivity of a mesh element.");
×
3508
    return {};
3509
  }
3510

3511
  // get the coordinates
3512
  vector<moab::CartVect> coords(conn.size());
×
3513
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3514
  if (rval != moab::MB_SUCCESS) {
×
3515
    warning("Failed to get the coordinates of a mesh element.");
×
3516
    return {};
3517
  }
3518

3519
  // compute the centroid of the element vertices
3520
  moab::CartVect centroid(0.0, 0.0, 0.0);
3521
  for (const auto& coord : coords) {
×
3522
    centroid += coord;
3523
  }
3524
  centroid /= double(coords.size());
3525

3526
  return {centroid[0], centroid[1], centroid[2]};
3527
}
3528

3529
int MOABMesh::n_vertices() const
845,874✔
3530
{
3531
  return verts_.size();
845,874✔
3532
}
3533

3534
Position MOABMesh::vertex(int id) const
86,227✔
3535
{
3536

3537
  moab::ErrorCode rval;
86,227✔
3538

3539
  moab::EntityHandle vert = verts_[id];
86,227✔
3540

3541
  moab::CartVect coords;
86,227✔
3542
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3543
  if (rval != moab::MB_SUCCESS) {
86,227!
3544
    fatal_error("Failed to get the coordinates of a vertex.");
3545
  }
3546

3547
  return {coords[0], coords[1], coords[2]};
86,227✔
3548
}
3549

3550
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3551
{
3552
  moab::ErrorCode rval;
203,880✔
3553

3554
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3555

3556
  // look up the tet connectivity
3557
  vector<moab::EntityHandle> conn;
203,880✔
3558
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3559
  if (rval != moab::MB_SUCCESS) {
203,880!
3560
    fatal_error("Failed to get connectivity of a mesh element.");
3561
    return {};
3562
  }
3563

3564
  std::vector<int> verts(4);
203,880✔
3565
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3566
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3567
  }
3568

3569
  return verts;
203,880✔
3570
}
203,880✔
3571

3572
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3573
  std::string score) const
3574
{
3575
  moab::ErrorCode rval;
3576
  // add a tag to the mesh
3577
  // all scores are treated as a single value
3578
  // with an uncertainty
3579
  moab::Tag value_tag;
3580

3581
  // create the value tag if not present and get handle
3582
  double default_val = 0.0;
3583
  auto val_string = score + "_mean";
3584
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3585
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3586
  if (rval != moab::MB_SUCCESS) {
×
3587
    auto msg =
3588
      fmt::format("Could not create or retrieve the value tag for the score {}"
3589
                  " on unstructured mesh {}",
3590
        score, id_);
×
3591
    fatal_error(msg);
3592
  }
3593

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

3607
  // return the populated tag handles
3608
  return {value_tag, error_tag};
3609
}
3610

3611
void MOABMesh::add_score(const std::string& score)
3612
{
3613
  auto score_tags = get_score_tags(score);
×
3614
  tag_names_.push_back(score);
3615
}
3616

3617
void MOABMesh::remove_scores()
3618
{
3619
  for (const auto& name : tag_names_) {
×
3620
    auto value_name = name + "_mean";
3621
    moab::Tag tag;
3622
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3623
    if (rval != moab::MB_SUCCESS)
×
3624
      return;
3625

3626
    rval = mbi_->tag_delete(tag);
×
3627
    if (rval != moab::MB_SUCCESS) {
×
3628
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3629
                             " on unstructured mesh {}",
3630
        name, id_);
×
3631
      fatal_error(msg);
3632
    }
3633

3634
    auto std_dev_name = name + "_std_dev";
×
3635
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3636
    if (rval != moab::MB_SUCCESS) {
×
3637
      auto msg =
3638
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3639
                    " on unstructured mesh {}",
3640
          name, id_);
×
3641
    }
3642

3643
    rval = mbi_->tag_delete(tag);
×
3644
    if (rval != moab::MB_SUCCESS) {
×
3645
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3646
                             " on unstructured mesh {}",
3647
        name, id_);
×
3648
      fatal_error(msg);
3649
    }
3650
  }
3651
  tag_names_.clear();
3652
}
3653

3654
void MOABMesh::set_score_data(const std::string& score,
3655
  const vector<double>& values, const vector<double>& std_dev)
3656
{
3657
  auto score_tags = this->get_score_tags(score);
×
3658

3659
  moab::ErrorCode rval;
3660
  // set the score value
3661
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
3662
  if (rval != moab::MB_SUCCESS) {
×
3663
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3664
                           "on unstructured mesh {}",
3665
      score, id_);
3666
    warning(msg);
×
3667
  }
3668

3669
  // set the error value
3670
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3671
  if (rval != moab::MB_SUCCESS) {
×
3672
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3673
                           "on unstructured mesh {}",
3674
      score, id_);
3675
    warning(msg);
×
3676
  }
3677
}
3678

3679
void MOABMesh::write(const std::string& base_filename) const
3680
{
3681
  // add extension to the base name
3682
  auto filename = base_filename + ".vtk";
3683
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3684
  filename = settings::path_output + filename;
×
3685

3686
  // write the tetrahedral elements of the mesh only
3687
  // to avoid clutter from zero-value data on other
3688
  // elements during visualization
3689
  moab::ErrorCode rval;
3690
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3691
  if (rval != moab::MB_SUCCESS) {
×
3692
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3693
    warning(msg);
×
3694
  }
3695
}
3696

3697
#endif
3698

3699
#ifdef OPENMC_LIBMESH_ENABLED
3700

3701
const std::string LibMesh::mesh_lib_type = "libmesh";
3702

3703
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3704
{
3705
  // filename_ and length_multiplier_ will already be set by the
3706
  // UnstructuredMesh constructor
3707
  set_mesh_pointer_from_filename(filename_);
23✔
3708
  set_length_multiplier(length_multiplier_);
23✔
3709
  initialize();
23✔
3710
}
23✔
3711

3712
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3713
{
3714
  // filename_ and length_multiplier_ will already be set by the
3715
  // UnstructuredMesh constructor
3716
  set_mesh_pointer_from_filename(filename_);
×
3717
  set_length_multiplier(length_multiplier_);
×
3718
  initialize();
×
3719
}
3720

3721
// create the mesh from a pointer to a libMesh Mesh
3722
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3723
{
3724
  if (!input_mesh.is_replicated()) {
×
3725
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3726
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3727
  }
3728

3729
  m_ = &input_mesh;
3730
  set_length_multiplier(length_multiplier);
×
3731
  initialize();
×
3732
}
3733

3734
// create the mesh from an input file
3735
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3736
{
3737
  n_dimension_ = 3;
3738
  set_mesh_pointer_from_filename(filename);
×
3739
  set_length_multiplier(length_multiplier);
×
3740
  initialize();
×
3741
}
3742

3743
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3744
{
3745
  filename_ = filename;
23✔
3746
  unique_m_ =
23✔
3747
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3748
  m_ = unique_m_.get();
23✔
3749
  m_->read(filename_);
23✔
3750
}
23✔
3751

3752
// build a libMesh equation system for storing values
3753
void LibMesh::build_eqn_sys()
15✔
3754
{
3755
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
15✔
3756
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3757
  libMesh::ExplicitSystem& eq_sys =
15✔
3758
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3759
}
15✔
3760

3761
// intialize from mesh file
3762
void LibMesh::initialize()
23✔
3763
{
3764
  if (!settings::libmesh_comm) {
23!
3765
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3766
                "communicator.");
3767
  }
3768

3769
  // assuming that unstructured meshes used in OpenMC are 3D
3770
  n_dimension_ = 3;
23✔
3771

3772
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3773
  // Otherwise assume that it is prepared by its owning application
3774
  if (unique_m_) {
23!
3775
    m_->prepare_for_use();
23✔
3776
  }
3777

3778
  // ensure that the loaded mesh is 3 dimensional
3779
  if (m_->mesh_dimension() != n_dimension_) {
23!
3780
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3781
                            "mesh is not a 3D mesh.",
3782
      filename_));
3783
  }
3784

3785
  for (int i = 0; i < num_threads(); i++) {
69✔
3786
    pl_.emplace_back(m_->sub_point_locator());
46✔
3787
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
3788
    pl_.back()->enable_out_of_mesh_mode();
46✔
3789
  }
3790

3791
  // store first element in the mesh to use as an offset for bin indices
3792
  auto first_elem = *m_->elements_begin();
46✔
3793
  first_element_id_ = first_elem->id();
23✔
3794

3795
  // bounding box for the mesh for quick rejection checks
3796
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23!
3797
  libMesh::Point ll = bbox_.min();
23!
3798
  libMesh::Point ur = bbox_.max();
23!
3799
  if (length_multiplier_ > 0.0) {
23!
3800
    lower_left_ = {length_multiplier_ * ll(0), length_multiplier_ * ll(1),
3801
      length_multiplier_ * ll(2)};
3802
    upper_right_ = {length_multiplier_ * ur(0), length_multiplier_ * ur(1),
3803
      length_multiplier_ * ur(2)};
3804
  } else {
3805
    lower_left_ = {ll(0), ll(1), ll(2)};
23✔
3806
    upper_right_ = {ur(0), ur(1), ur(2)};
23✔
3807
  }
3808
}
23✔
3809

3810
// Sample position within a tet for LibMesh type tets
3811
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3812
{
3813
  const auto& elem = get_element_from_bin(bin);
400,820✔
3814
  // Get tet vertex coordinates from LibMesh
3815
  std::array<Position, 4> tet_verts;
400,820✔
3816
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3817
    auto node_ref = elem.node_ref(i);
1,603,280✔
3818
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3819
  }
1,603,280✔
3820
  // Samples position within tet using Barycentric coordinates
3821
  Position sampled_position = this->sample_tet(tet_verts, seed);
400,820✔
3822
  if (length_multiplier_ > 0.0) {
400,820!
3823
    return length_multiplier_ * sampled_position;
3824
  } else {
3825
    return sampled_position;
400,820✔
3826
  }
3827
}
3828

3829
Position LibMesh::centroid(int bin) const
3830
{
3831
  const auto& elem = this->get_element_from_bin(bin);
3832
  auto centroid = elem.vertex_average();
3833
  if (length_multiplier_ > 0.0) {
×
3834
    return length_multiplier_ * Position(centroid(0), centroid(1), centroid(2));
3835
  } else {
3836
    return {centroid(0), centroid(1), centroid(2)};
3837
  }
3838
}
3839

3840
int LibMesh::n_vertices() const
39,978✔
3841
{
3842
  return m_->n_nodes();
39,978✔
3843
}
3844

3845
Position LibMesh::vertex(int vertex_id) const
39,942✔
3846
{
3847
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
3848
  if (length_multiplier_ > 0.0) {
39,942!
3849
    return length_multiplier_ * Position(node_ref(0), node_ref(1), node_ref(2));
×
3850
  } else {
3851
    return {node_ref(0), node_ref(1), node_ref(2)};
39,942✔
3852
  }
3853
}
39,942✔
3854

3855
std::vector<int> LibMesh::connectivity(int elem_id) const
265,856✔
3856
{
3857
  std::vector<int> conn;
265,856✔
3858
  const auto* elem_ptr = m_->elem_ptr(elem_id);
265,856✔
3859
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,337,280✔
3860
    conn.push_back(elem_ptr->node_id(i));
1,071,424✔
3861
  }
3862
  return conn;
265,856✔
3863
}
3864

3865
std::string LibMesh::library() const
33✔
3866
{
3867
  return mesh_lib_type;
33✔
3868
}
3869

3870
int LibMesh::n_bins() const
1,784,407✔
3871
{
3872
  return m_->n_elem();
1,784,407✔
3873
}
3874

3875
int LibMesh::n_surface_bins() const
3876
{
3877
  int n_bins = 0;
3878
  for (int i = 0; i < this->n_bins(); i++) {
×
3879
    const libMesh::Elem& e = get_element_from_bin(i);
3880
    n_bins += e.n_faces();
3881
    // if this is a boundary element, it will only be visited once,
3882
    // the number of surface bins is incremented to
3883
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
3884
      // null neighbor pointer indicates a boundary face
3885
      if (!neighbor_ptr) {
×
3886
        n_bins++;
3887
      }
3888
    }
3889
  }
3890
  return n_bins;
3891
}
3892

3893
void LibMesh::add_score(const std::string& var_name)
15✔
3894
{
3895
  if (!equation_systems_) {
15!
3896
    build_eqn_sys();
15✔
3897
  }
3898

3899
  // check if this is a new variable
3900
  std::string value_name = var_name + "_mean";
15✔
3901
  if (!variable_map_.count(value_name)) {
15✔
3902
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3903
    auto var_num =
15✔
3904
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3905
    variable_map_[value_name] = var_num;
15✔
3906
  }
3907

3908
  std::string std_dev_name = var_name + "_std_dev";
15✔
3909
  // check if this is a new variable
3910
  if (!variable_map_.count(std_dev_name)) {
15✔
3911
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3912
    auto var_num =
15✔
3913
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3914
    variable_map_[std_dev_name] = var_num;
15✔
3915
  }
3916
}
15✔
3917

3918
void LibMesh::remove_scores()
15✔
3919
{
3920
  if (equation_systems_) {
15!
3921
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3922
    eqn_sys.clear();
15✔
3923
    variable_map_.clear();
15✔
3924
  }
3925
}
15✔
3926

3927
void LibMesh::set_score_data(const std::string& var_name,
15✔
3928
  const vector<double>& values, const vector<double>& std_dev)
3929
{
3930
  if (!equation_systems_) {
15!
3931
    build_eqn_sys();
3932
  }
3933

3934
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3935

3936
  if (!eqn_sys.is_initialized()) {
15!
3937
    equation_systems_->init();
15✔
3938
  }
3939

3940
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3941

3942
  // look up the value variable
3943
  std::string value_name = var_name + "_mean";
15✔
3944
  unsigned int value_num = variable_map_.at(value_name);
15✔
3945
  // look up the std dev variable
3946
  std::string std_dev_name = var_name + "_std_dev";
15✔
3947
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
3948

3949
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
195,757✔
3950
       it++) {
3951
    if (!(*it)->active()) {
97,856!
3952
      continue;
3953
    }
3954

3955
    auto bin = get_bin_from_element(*it);
97,856✔
3956

3957
    // set value
3958
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
3959
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
3960
    assert(value_dof_indices.size() == 1);
97,856✔
3961
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
3962

3963
    // set std dev
3964
    vector<libMesh::dof_id_type> std_dev_dof_indices;
97,856✔
3965
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
97,856✔
3966
    assert(std_dev_dof_indices.size() == 1);
97,856✔
3967
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
97,856✔
3968
  }
97,871✔
3969
}
15✔
3970

3971
void LibMesh::write(const std::string& filename) const
15✔
3972
{
3973
  write_message(fmt::format(
15✔
3974
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
15✔
3975
  libMesh::ExodusII_IO exo(*m_);
15✔
3976
  std::set<std::string> systems_out = {eq_system_name_};
30!
3977
  exo.write_discontinuous_exodusII(
15✔
3978
    filename + ".e", *equation_systems_, &systems_out);
30✔
3979
}
15✔
3980

3981
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3982
  vector<int>& bins, vector<double>& lengths) const
3983
{
3984
  // TODO: Implement triangle crossings here
3985
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3986
}
3987

3988
int LibMesh::get_bin(Position r) const
2,340,604✔
3989
{
3990
  // look-up a tet using the point locator
3991
  libMesh::Point p(r.x, r.y, r.z);
2,340,604!
3992

3993
  if (length_multiplier_ > 0.0) {
2,340,604!
3994
    // Scale the point down
3995
    p /= length_multiplier_;
2,340,604✔
3996
  }
3997

3998
  // quick rejection check
3999
  if (!bbox_.contains_point(p)) {
2,340,604✔
4000
    return -1;
4001
  }
4002

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

4005
  const auto elem_ptr = (*point_locator)(p);
1,421,808✔
4006
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,808✔
4007
}
2,340,604✔
4008

4009
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,518,434✔
4010
{
4011
  int bin = elem->id() - first_element_id_;
1,518,434✔
4012
  if (bin >= n_bins() || bin < 0) {
1,518,434!
4013
    fatal_error(fmt::format("Invalid bin: {}", bin));
4014
  }
4015
  return bin;
1,518,434✔
4016
}
4017

4018
std::pair<vector<double>, vector<double>> LibMesh::plot(
4019
  Position plot_ll, Position plot_ur) const
4020
{
4021
  return {};
4022
}
4023

4024
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
4025
{
4026
  return m_->elem_ref(bin);
765,460✔
4027
}
4028

4029
double LibMesh::volume(int bin) const
364,640✔
4030
{
4031
  return this->get_element_from_bin(bin).volume() * length_multiplier_ *
364,640✔
4032
         length_multiplier_ * length_multiplier_;
364,640✔
4033
}
4034

4035
AdaptiveLibMesh::AdaptiveLibMesh(libMesh::MeshBase& input_mesh,
4036
  double length_multiplier,
4037
  const std::set<libMesh::subdomain_id_type>& block_ids)
4038
  : LibMesh(input_mesh, length_multiplier), block_ids_(block_ids),
4039
    block_restrict_(!block_ids_.empty()),
×
4040
    num_active_(
×
4041
      block_restrict_
4042
        ? std::distance(m_->active_subdomain_set_elements_begin(block_ids_),
×
4043
            m_->active_subdomain_set_elements_end(block_ids_))
×
4044
        : m_->n_active_elem())
×
4045
{
4046
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
4047
  // contiguous in ID space, so we need to map from bin indices (defined over
4048
  // active elements) to global dof ids
4049
  bin_to_elem_map_.reserve(num_active_);
×
4050
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
4051
  auto begin = block_restrict_
4052
                 ? m_->active_subdomain_set_elements_begin(block_ids_)
×
4053
                 : m_->active_elements_begin();
×
4054
  auto end = block_restrict_ ? m_->active_subdomain_set_elements_end(block_ids_)
×
4055
                             : m_->active_elements_end();
×
4056
  for (const auto& elem : libMesh::as_range(begin, end)) {
×
4057
    bin_to_elem_map_.push_back(elem->id());
×
4058
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
4059
  }
4060
}
4061

4062
int AdaptiveLibMesh::n_bins() const
4063
{
4064
  return num_active_;
4065
}
4066

4067
void AdaptiveLibMesh::add_score(const std::string& var_name)
4068
{
4069
  warning(fmt::format(
×
4070
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4071
    this->id_));
4072
}
4073

4074
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
4075
  const vector<double>& values, const vector<double>& std_dev)
4076
{
4077
  warning(fmt::format(
×
4078
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
4079
    this->id_));
4080
}
4081

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

4089
int AdaptiveLibMesh::get_bin(Position r) const
4090
{
4091
  // look-up a tet using the point locator
4092
  libMesh::Point p(r.x, r.y, r.z);
×
4093

4094
  if (length_multiplier_ > 0.0) {
×
4095
    // Scale the point down
4096
    p /= length_multiplier_;
4097
  }
4098

4099
  // quick rejection check
4100
  if (!bbox_.contains_point(p)) {
×
4101
    return -1;
4102
  }
4103

4104
  const auto& point_locator = pl_.at(thread_num());
×
4105

4106
  const auto elem_ptr = (*point_locator)(p, &block_ids_);
×
4107
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
×
4108
}
4109

4110
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
4111
{
4112
  int bin = elem_to_bin_map_[elem->id()];
4113
  if (bin >= n_bins() || bin < 0) {
×
4114
    fatal_error(fmt::format("Invalid bin: {}", bin));
4115
  }
4116
  return bin;
4117
}
4118

4119
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
4120
{
4121
  return m_->elem_ref(bin_to_elem_map_.at(bin));
4122
}
4123

4124
#endif // OPENMC_LIBMESH_ENABLED
4125

4126
//==============================================================================
4127
// Non-member functions
4128
//==============================================================================
4129

4130
void read_meshes(pugi::xml_node root)
13,154✔
4131
{
4132
  std::unordered_set<int> mesh_ids;
13,154✔
4133

4134
  for (auto node : root.children("mesh")) {
16,362✔
4135
    // Check to make sure multiple meshes in the same file don't share IDs
4136
    int id = std::stoi(get_node_value(node, "id"));
6,416✔
4137
    if (contains(mesh_ids, id)) {
6,416!
4138
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4139
                              "'{}' in the same input file",
4140
        id));
4141
    }
4142
    mesh_ids.insert(id);
3,208✔
4143

4144
    // If we've already read a mesh with the same ID in a *different* file,
4145
    // assume it is the same here
4146
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3,208!
4147
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
4148
      continue;
×
4149
    }
4150

4151
    std::string mesh_type;
3,208✔
4152
    if (check_for_node(node, "type")) {
3,208✔
4153
      mesh_type = get_node_value(node, "type", true, true);
948✔
4154
    } else {
4155
      mesh_type = "regular";
2,260✔
4156
    }
4157

4158
    // determine the mesh library to use
4159
    std::string mesh_lib;
3,208✔
4160
    if (check_for_node(node, "library")) {
3,208✔
4161
      mesh_lib = get_node_value(node, "library", true, true);
47!
4162
    }
4163

4164
    Mesh::create(node, mesh_type, mesh_lib);
3,208✔
4165
  }
3,208✔
4166
}
13,154✔
4167

4168
void read_meshes(hid_t group)
22✔
4169
{
4170
  std::unordered_set<int> mesh_ids;
22✔
4171

4172
  std::vector<int> ids;
22✔
4173
  read_attribute(group, "ids", ids);
22✔
4174

4175
  for (auto id : ids) {
55✔
4176

4177
    // Check to make sure multiple meshes in the same file don't share IDs
4178
    if (contains(mesh_ids, id)) {
66!
4179
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
4180
                              "'{}' in the same HDF5 input file",
4181
        id));
4182
    }
4183
    mesh_ids.insert(id);
33✔
4184

4185
    // If we've already read a mesh with the same ID in a *different* file,
4186
    // assume it is the same here
4187
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
33!
4188
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
33✔
4189
      continue;
33✔
4190
    }
4191

4192
    std::string name = fmt::format("mesh {}", id);
×
4193
    hid_t mesh_group = open_group(group, name.c_str());
×
4194

4195
    std::string mesh_type;
×
4196
    if (object_exists(mesh_group, "type")) {
×
4197
      read_dataset(mesh_group, "type", mesh_type);
×
4198
    } else {
4199
      mesh_type = "regular";
×
4200
    }
4201

4202
    // determine the mesh library to use
4203
    std::string mesh_lib;
×
4204
    if (object_exists(mesh_group, "library")) {
×
4205
      read_dataset(mesh_group, "library", mesh_lib);
×
4206
    }
4207

4208
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
4209
  }
×
4210
}
44✔
4211

4212
void meshes_to_hdf5(hid_t group)
7,500✔
4213
{
4214
  // Write number of meshes
4215
  hid_t meshes_group = create_group(group, "meshes");
7,500✔
4216
  int32_t n_meshes = model::meshes.size();
7,500✔
4217
  write_attribute(meshes_group, "n_meshes", n_meshes);
7,500✔
4218

4219
  if (n_meshes > 0) {
7,500✔
4220
    // Write IDs of meshes
4221
    vector<int> ids;
2,280✔
4222
    for (const auto& m : model::meshes) {
5,172✔
4223
      m->to_hdf5(meshes_group);
2,892✔
4224
      ids.push_back(m->id_);
2,892✔
4225
    }
4226
    write_attribute(meshes_group, "ids", ids);
2,280✔
4227
  }
2,280✔
4228

4229
  close_group(meshes_group);
7,500✔
4230
}
7,500✔
4231

4232
void free_memory_mesh()
8,605✔
4233
{
4234
  model::meshes.clear();
8,605✔
4235
  model::mesh_map.clear();
8,605✔
4236
}
8,605✔
4237

4238
extern "C" int n_meshes()
308✔
4239
{
4240
  return model::meshes.size();
308✔
4241
}
4242

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