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

openmc-dev / openmc / 20792992061

07 Jan 2026 06:56PM UTC coverage: 82.13% (+0.2%) from 81.94%
20792992061

Pull #3700

github

web-flow
Merge a389abcd9 into dfc80c706
Pull Request #3700: Gamma contact dose rate computation fispact style

17093 of 23667 branches covered (72.22%)

Branch coverage included in aggregate %.

78 of 143 new or added lines in 5 files covered. (54.55%)

728 existing lines in 16 files now uncovered.

55293 of 64469 relevant lines covered (85.77%)

43338787.02 hits per line

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

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

9
#ifdef _MSC_VER
10
#include <intrin.h> // for _InterlockedCompareExchange
11
#endif
12

13
#ifdef OPENMC_MPI
14
#include "mpi.h"
15
#endif
16

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

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

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

57
#ifdef OPENMC_DAGMC_ENABLED
58
#include "moab/FileOptions.hpp"
59
#endif
60

61
namespace openmc {
62

63
//==============================================================================
64
// Global variables
65
//==============================================================================
66

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

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

77
namespace model {
78

79
std::unordered_map<int32_t, int32_t> mesh_map;
80
vector<unique_ptr<Mesh>> meshes;
81

82
} // namespace model
83

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

91
//==============================================================================
92
// Helper functions
93
//==============================================================================
94

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

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

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

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

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

143
namespace detail {
144

145
//==============================================================================
146
// MaterialVolumes implementation
147
//==============================================================================
148

149
void MaterialVolumes::add_volume(
2,801,403✔
150
  int index_elem, int index_material, double volume)
151
{
152
  // This method handles adding elements to the materials hash table,
153
  // implementing open addressing with linear probing. Consistency across
154
  // multiple threads is handled by with an atomic compare-and-swap operation.
155
  // Ideally, we would use #pragma omp atomic compare, but it was introduced in
156
  // OpenMP 5.1 and is not widely supported yet.
157

158
  // Loop for linear probing
159
  for (int attempt = 0; attempt < table_size_; ++attempt) {
2,801,403!
160
    // Determine slot to check, making sure it is positive
161
    int slot = (index_material + attempt) % table_size_;
2,801,403✔
162
    if (slot < 0)
2,801,403✔
163
      slot += table_size_;
208,978✔
164
    int32_t* slot_ptr = &this->materials(index_elem, slot);
2,801,403✔
165

166
    // Non-atomic read of current material
167
    int32_t current_val = *slot_ptr;
2,801,403✔
168

169
    // Found the desired material; accumulate volume
170
    if (current_val == index_material) {
2,801,403✔
171
#pragma omp atomic
1,527,719✔
172
      this->volumes(index_elem, slot) += volume;
2,799,983✔
173
      return;
2,799,983✔
174
    }
175

176
    // Slot appears to be empty; attempt to claim
177
    if (current_val == EMPTY) {
1,420!
178
      // Attempt compare-and-swap from EMPTY to index_material
179
      int32_t expected_val = EMPTY;
1,420✔
180
      bool claimed_slot =
181
        atomic_cas_int32(slot_ptr, expected_val, index_material);
1,420✔
182

183
      // If we claimed the slot or another thread claimed it but the same
184
      // material was inserted, proceed to accumulate
185
      if (claimed_slot || (expected_val == index_material)) {
1,420!
186
#pragma omp atomic
782✔
187
        this->volumes(index_elem, slot) += volume;
1,420✔
188
        return;
1,420✔
189
      }
190
    }
191
  }
192

193
  // If table is full, set a flag that can be checked later
194
  table_full_ = true;
×
195
}
196

197
void MaterialVolumes::add_volume_unsafe(
×
198
  int index_elem, int index_material, double volume)
199
{
200
  // Linear probe
201
  for (int attempt = 0; attempt < table_size_; ++attempt) {
×
202
    // Determine slot to check, making sure it is positive
203
    int slot = (index_material + attempt) % table_size_;
×
204
    if (slot < 0)
×
205
      slot += table_size_;
×
206

207
    // Read current material
208
    int32_t current_val = this->materials(index_elem, slot);
×
209

210
    // Found the desired material; accumulate volume
211
    if (current_val == index_material) {
×
212
      this->volumes(index_elem, slot) += volume;
×
213
      return;
×
214
    }
215

216
    // Claim empty slot
217
    if (current_val == EMPTY) {
×
218
      this->materials(index_elem, slot) = index_material;
×
219
      this->volumes(index_elem, slot) += volume;
×
220
      return;
×
221
    }
222
  }
223

224
  // If table is full, set a flag that can be checked later
225
  table_full_ = true;
×
226
}
227

228
} // namespace detail
229

230
//==============================================================================
231
// Mesh implementation
232
//==============================================================================
233

234
template<typename T>
235
const std::unique_ptr<Mesh>& Mesh::create(
2,891✔
236
  T dataset, const std::string& mesh_type, const std::string& mesh_library)
237
{
238
  // Determine mesh type. Add to model vector and map
239
  if (mesh_type == RegularMesh::mesh_type) {
2,891✔
240
    model::meshes.push_back(make_unique<RegularMesh>(dataset));
2,005!
241
  } else if (mesh_type == RectilinearMesh::mesh_type) {
886✔
242
    model::meshes.push_back(make_unique<RectilinearMesh>(dataset));
114!
243
  } else if (mesh_type == CylindricalMesh::mesh_type) {
772✔
244
    model::meshes.push_back(make_unique<CylindricalMesh>(dataset));
390!
245
  } else if (mesh_type == SphericalMesh::mesh_type) {
382✔
246
    model::meshes.push_back(make_unique<SphericalMesh>(dataset));
335!
247
#ifdef OPENMC_DAGMC_ENABLED
248
  } else if (mesh_type == UnstructuredMesh::mesh_type &&
48!
249
             mesh_library == MOABMesh::mesh_lib_type) {
24✔
250
    model::meshes.push_back(make_unique<MOABMesh>(dataset));
24!
251
#endif
252
#ifdef OPENMC_LIBMESH_ENABLED
253
  } else if (mesh_type == UnstructuredMesh::mesh_type &&
46!
254
             mesh_library == LibMesh::mesh_lib_type) {
23✔
255
    model::meshes.push_back(make_unique<LibMesh>(dataset));
23!
256
#endif
257
  } else if (mesh_type == UnstructuredMesh::mesh_type) {
×
258
    fatal_error("Unstructured mesh support is not enabled or the mesh "
×
259
                "library is invalid.");
260
  } else {
261
    fatal_error(fmt::format("Invalid mesh type: {}", mesh_type));
×
262
  }
263

264
  // Map ID to position in vector
265
  model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
2,891✔
266

267
  return model::meshes.back();
2,891✔
268
}
269

270
Mesh::Mesh(pugi::xml_node node)
2,935✔
271
{
272
  // Read mesh id
273
  id_ = std::stoi(get_node_value(node, "id"));
2,935✔
274
  if (check_for_node(node, "name"))
2,935✔
275
    name_ = get_node_value(node, "name");
16✔
276
}
2,935✔
277

278
Mesh::Mesh(hid_t group)
44✔
279
{
280
  // Read mesh ID
281
  read_attribute(group, "id", id_);
44✔
282

283
  // Read mesh name
284
  if (object_exists(group, "name")) {
44!
285
    read_dataset(group, "name", name_);
×
286
  }
287
}
44✔
288

289
void Mesh::set_id(int32_t id)
23✔
290
{
291
  assert(id >= 0 || id == C_NONE);
19!
292

293
  // Clear entry in mesh map in case one was already assigned
294
  if (id_ != C_NONE) {
23✔
295
    model::mesh_map.erase(id_);
22✔
296
    id_ = C_NONE;
22✔
297
  }
298

299
  // Ensure no other mesh has the same ID
300
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
23!
301
    throw std::runtime_error {
×
302
      fmt::format("Two meshes have the same ID: {}", id)};
×
303
  }
304

305
  // If no ID is specified, auto-assign the next ID in the sequence
306
  if (id == C_NONE) {
23✔
307
    id = 0;
1✔
308
    for (const auto& m : model::meshes) {
3✔
309
      id = std::max(id, m->id_);
2✔
310
    }
311
    ++id;
1✔
312
  }
313

314
  // Update ID and entry in the mesh map
315
  id_ = id;
23✔
316

317
  // find the index of this mesh in the model::meshes vector
318
  // (search in reverse because this mesh was likely just added to the vector)
319
  auto it = std::find_if(model::meshes.rbegin(), model::meshes.rend(),
23✔
320
    [this](const std::unique_ptr<Mesh>& mesh) { return mesh.get() == this; });
80✔
321

322
  model::mesh_map[id] = std::distance(model::meshes.begin(), it.base()) - 1;
23✔
323
}
23✔
324

325
vector<double> Mesh::volumes() const
252✔
326
{
327
  vector<double> volumes(n_bins());
252✔
328
  for (int i = 0; i < n_bins(); i++) {
1,125,127✔
329
    volumes[i] = this->volume(i);
1,124,875✔
330
  }
331
  return volumes;
252✔
332
}
×
333

334
void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
176✔
335
  int32_t* materials, double* volumes) const
336
{
337
  if (mpi::master) {
176!
338
    header("MESH MATERIAL VOLUMES CALCULATION", 7);
176✔
339
  }
340
  write_message(7, "Number of mesh elements = {}", n_bins());
176✔
341
  write_message(7, "Number of rays (x) = {}", nx);
176✔
342
  write_message(7, "Number of rays (y) = {}", ny);
176✔
343
  write_message(7, "Number of rays (z) = {}", nz);
176✔
344
  int64_t n_total = static_cast<int64_t>(nx) * ny +
176✔
345
                    static_cast<int64_t>(ny) * nz +
176✔
346
                    static_cast<int64_t>(nx) * nz;
176✔
347
  write_message(7, "Total number of rays = {}", n_total);
176✔
348
  write_message(7, "Table size per mesh element = {}", table_size);
176✔
349

350
  Timer timer;
176✔
351
  timer.start();
176✔
352

353
  // Create object for keeping track of materials/volumes
354
  detail::MaterialVolumes result(materials, volumes, table_size);
176✔
355

356
  // Determine bounding box
357
  auto bbox = this->bounding_box();
176✔
358

359
  std::array<int, 3> n_rays = {nx, ny, nz};
176✔
360

361
  // Determine effective width of rays
362
  Position width = bbox.max - bbox.min;
176✔
363
  width.x = (nx > 0) ? width.x / nx : 0.0;
176✔
364
  width.y = (ny > 0) ? width.y / ny : 0.0;
176✔
365
  width.z = (nz > 0) ? width.z / nz : 0.0;
176✔
366

367
  // Set flag for mesh being contained within model
368
  bool out_of_model = false;
176✔
369

370
#pragma omp parallel
96✔
371
  {
372
    // Preallocate vector for mesh indices and length fractions and particle
373
    std::vector<int> bins;
80✔
374
    std::vector<double> length_fractions;
80✔
375
    Particle p;
80✔
376

377
    SourceSite site;
80✔
378
    site.E = 1.0;
80✔
379
    site.particle = ParticleType::neutron;
80✔
380

381
    for (int axis = 0; axis < 3; ++axis) {
320✔
382
      // Set starting position and direction
383
      site.r = {0.0, 0.0, 0.0};
240✔
384
      site.r[axis] = bbox.min[axis];
240✔
385
      site.u = {0.0, 0.0, 0.0};
240✔
386
      site.u[axis] = 1.0;
240✔
387

388
      // Determine width of rays and number of rays in other directions
389
      int ax1 = (axis + 1) % 3;
240✔
390
      int ax2 = (axis + 2) % 3;
240✔
391
      double min1 = bbox.min[ax1];
240✔
392
      double min2 = bbox.min[ax2];
240✔
393
      double d1 = width[ax1];
240✔
394
      double d2 = width[ax2];
240✔
395
      int n1 = n_rays[ax1];
240✔
396
      int n2 = n_rays[ax2];
240✔
397
      if (n1 == 0 || n2 == 0) {
240✔
398
        continue;
60✔
399
      }
400

401
      // Divide rays in first direction over MPI processes by computing starting
402
      // and ending indices
403
      int min_work = n1 / mpi::n_procs;
180✔
404
      int remainder = n1 % mpi::n_procs;
180✔
405
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
180!
406
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
180✔
407
      int i1_end = i1_start + n1_local;
180✔
408

409
      // Loop over rays on face of bounding box
410
#pragma omp for collapse(2)
411
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
9,785✔
412
        for (int i2 = 0; i2 < n2; ++i2) {
572,340✔
413
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
562,735✔
414
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
562,735✔
415

416
          p.from_source(&site);
562,735✔
417

418
          // Determine particle's location
419
          if (!exhaustive_find_cell(p)) {
562,735✔
420
            out_of_model = true;
39,930✔
421
            continue;
39,930✔
422
          }
423

424
          // Set birth cell attribute
425
          if (p.cell_born() == C_NONE)
522,805!
426
            p.cell_born() = p.lowest_coord().cell();
522,805✔
427

428
          // Initialize last cells from current cell
429
          for (int j = 0; j < p.n_coord(); ++j) {
1,045,610✔
430
            p.cell_last(j) = p.coord(j).cell();
522,805✔
431
          }
432
          p.n_coord_last() = p.n_coord();
522,805✔
433

434
          while (true) {
435
            // Ray trace from r_start to r_end
436
            Position r0 = p.r();
1,054,715✔
437
            double max_distance = bbox.max[axis] - r0[axis];
1,054,715✔
438

439
            // Find the distance to the nearest boundary
440
            BoundaryInfo boundary = distance_to_boundary(p);
1,054,715✔
441

442
            // Advance particle forward
443
            double distance = std::min(boundary.distance(), max_distance);
1,054,715✔
444
            p.move_distance(distance);
1,054,715✔
445

446
            // Determine what mesh elements were crossed by particle
447
            bins.clear();
1,054,715✔
448
            length_fractions.clear();
1,054,715✔
449
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
1,054,715✔
450

451
            // Add volumes to any mesh elements that were crossed
452
            int i_material = p.material();
1,054,715✔
453
            if (i_material != C_NONE) {
1,054,715✔
454
              i_material = model::materials[i_material]->id();
949,025✔
455
            }
456
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
2,328,080✔
457
              int mesh_index = bins[i_bin];
1,273,365✔
458
              double length = distance * length_fractions[i_bin];
1,273,365✔
459

460
              // Add volume to result
461
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,273,365✔
462
            }
463

464
            if (distance == max_distance)
1,054,715✔
465
              break;
522,805✔
466

467
            // cross next geometric surface
468
            for (int j = 0; j < p.n_coord(); ++j) {
1,063,820✔
469
              p.cell_last(j) = p.coord(j).cell();
531,910✔
470
            }
471
            p.n_coord_last() = p.n_coord();
531,910✔
472

473
            // Set surface that particle is on and adjust coordinate levels
474
            p.surface() = boundary.surface();
531,910✔
475
            p.n_coord() = boundary.coord_level();
531,910✔
476

477
            if (boundary.lattice_translation()[0] != 0 ||
531,910✔
478
                boundary.lattice_translation()[1] != 0 ||
1,063,820!
479
                boundary.lattice_translation()[2] != 0) {
531,910!
480
              // Particle crosses lattice boundary
481
              cross_lattice(p, boundary);
×
482
            } else {
483
              // Particle crosses surface
484
              const auto& surf {model::surfaces[p.surface_index()].get()};
531,910✔
485
              p.cross_surface(*surf);
531,910✔
486
            }
487
          }
531,910✔
488
        }
489
      }
490
    }
491
  }
80✔
492

493
  // Check for errors
494
  if (out_of_model) {
176✔
495
    throw std::runtime_error("Mesh not fully contained in geometry.");
11✔
496
  } else if (result.table_full()) {
165!
497
    throw std::runtime_error("Maximum number of materials for mesh material "
×
UNCOV
498
                             "volume calculation insufficient.");
×
499
  }
500

501
  // Compute time for raytracing
502
  double t_raytrace = timer.elapsed();
165✔
503

504
#ifdef OPENMC_MPI
505
  // Combine results from multiple MPI processes
506
  if (mpi::n_procs > 1) {
75!
507
    int total = this->n_bins() * table_size;
×
508
    if (mpi::master) {
×
509
      // Allocate temporary buffer for receiving data
510
      std::vector<int32_t> mats(total);
×
511
      std::vector<double> vols(total);
×
512

513
      for (int i = 1; i < mpi::n_procs; ++i) {
×
514
        // Receive material indices and volumes from process i
515
        MPI_Recv(mats.data(), total, MPI_INT32_T, i, i, mpi::intracomm,
×
516
          MPI_STATUS_IGNORE);
517
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
×
518
          MPI_STATUS_IGNORE);
519

520
        // Combine with existing results; we can call thread unsafe version of
521
        // add_volume because each thread is operating on a different element
522
#pragma omp for
523
        for (int index_elem = 0; index_elem < n_bins(); ++index_elem) {
×
524
          for (int k = 0; k < table_size; ++k) {
×
525
            int index = index_elem * table_size + k;
526
            if (mats[index] != EMPTY) {
×
527
              result.add_volume_unsafe(index_elem, mats[index], vols[index]);
×
528
            }
529
          }
530
        }
531
      }
532
    } else {
533
      // Send material indices and volumes to process 0
534
      MPI_Send(materials, total, MPI_INT32_T, 0, mpi::rank, mpi::intracomm);
×
535
      MPI_Send(volumes, total, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
×
536
    }
537
  }
538

539
  // Report time for MPI communication
540
  double t_mpi = timer.elapsed() - t_raytrace;
75✔
541
#else
542
  double t_mpi = 0.0;
90✔
543
#endif
544

545
  // Normalize based on known volumes of elements
546
  for (int i = 0; i < this->n_bins(); ++i) {
1,045✔
547
    // Estimated total volume in element i
548
    double volume = 0.0;
880✔
549
    for (int j = 0; j < table_size; ++j) {
7,920✔
550
      volume += result.volumes(i, j);
7,040✔
551
    }
552
    // Renormalize volumes based on known volume of element i
553
    double norm = this->volume(i) / volume;
880✔
554
    for (int j = 0; j < table_size; ++j) {
7,920✔
555
      result.volumes(i, j) *= norm;
7,040✔
556
    }
557
  }
558

559
  // Get total time and normalization time
560
  timer.stop();
165✔
561
  double t_total = timer.elapsed();
165✔
562
  double t_norm = t_total - t_raytrace - t_mpi;
165✔
563

564
  // Show timing statistics
565
  if (settings::verbosity < 7 || !mpi::master)
165!
566
    return;
44✔
567
  header("Timing Statistics", 7);
121✔
568
  fmt::print(" Total time elapsed            = {:.4e} seconds\n", t_total);
121✔
569
  fmt::print("   Ray tracing                 = {:.4e} seconds\n", t_raytrace);
121✔
570
  fmt::print("   MPI communication           = {:.4e} seconds\n", t_mpi);
121✔
571
  fmt::print("   Normalization               = {:.4e} seconds\n", t_norm);
99✔
572
  fmt::print(" Calculation rate              = {:.4e} rays/seconds\n",
99✔
573
    n_total / t_raytrace);
121✔
574
  fmt::print(" Calculation rate (per thread) = {:.4e} rays/seconds\n",
99✔
575
    n_total / (t_raytrace * mpi::n_procs * num_threads()));
121✔
576
  std::fflush(stdout);
121✔
577
}
578

579
void Mesh::to_hdf5(hid_t group) const
2,828✔
580
{
581
  // Create group for mesh
582
  std::string group_name = fmt::format("mesh {}", id_);
5,124✔
583
  hid_t mesh_group = create_group(group, group_name.c_str());
2,828✔
584

585
  // Write mesh type
586
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,828✔
587

588
  // Write mesh ID
589
  write_attribute(mesh_group, "id", id_);
2,828✔
590

591
  // Write mesh name
592
  write_dataset(mesh_group, "name", name_);
2,828✔
593

594
  // Write mesh data
595
  this->to_hdf5_inner(mesh_group);
2,828✔
596

597
  // Close group
598
  close_group(mesh_group);
2,828✔
599
}
2,828✔
600

601
//==============================================================================
602
// Structured Mesh implementation
603
//==============================================================================
604

605
std::string StructuredMesh::bin_label(int bin) const
5,130,283✔
606
{
607
  MeshIndex ijk = get_indices_from_bin(bin);
5,130,283✔
608

609
  if (n_dimension_ > 2) {
5,130,283✔
610
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,228,864✔
611
  } else if (n_dimension_ > 1) {
15,851✔
612
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,152✔
613
  } else {
614
    return fmt::format("Mesh Index ({})", ijk[0]);
550✔
615
  }
616
}
617

618
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,433✔
619
{
620
  // because method is const, shape_ is const as well and can't be adapted
621
  auto tmp_shape = shape_;
2,433✔
622
  return xt::adapt(tmp_shape, {n_dimension_});
4,866✔
623
}
624

625
Position StructuredMesh::sample_element(
1,533,348✔
626
  const MeshIndex& ijk, uint64_t* seed) const
627
{
628
  // lookup the lower/upper bounds for the mesh element
629
  double x_min = negative_grid_boundary(ijk, 0);
1,533,348✔
630
  double x_max = positive_grid_boundary(ijk, 0);
1,533,348✔
631

632
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,533,348!
633
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,533,348!
634

635
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,533,348!
636
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,533,348!
637

638
  return {x_min + (x_max - x_min) * prn(seed),
1,533,348✔
639
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,533,348✔
640
}
641

642
//==============================================================================
643
// Unstructured Mesh implementation
644
//==============================================================================
645

646
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
47✔
647
{
648
  n_dimension_ = 3;
47✔
649

650
  // check the mesh type
651
  if (check_for_node(node, "type")) {
47!
652
    auto temp = get_node_value(node, "type", true, true);
47!
653
    if (temp != mesh_type) {
47!
UNCOV
654
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
655
    }
656
  }
47✔
657

658
  // check if a length unit multiplier was specified
659
  if (check_for_node(node, "length_multiplier")) {
47!
UNCOV
660
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
661
  }
662

663
  // get the filename of the unstructured mesh to load
664
  if (check_for_node(node, "filename")) {
47!
665
    filename_ = get_node_value(node, "filename");
47!
666
    if (!file_exists(filename_)) {
47!
UNCOV
667
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
668
    }
669
  } else {
670
    fatal_error(fmt::format(
×
UNCOV
671
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
672
  }
673

674
  if (check_for_node(node, "options")) {
47!
675
    options_ = get_node_value(node, "options");
16!
676
  }
677

678
  // check if mesh tally data should be written with
679
  // statepoint files
680
  if (check_for_node(node, "output")) {
47!
UNCOV
681
    output_ = get_node_value_bool(node, "output");
×
682
  }
683
}
47✔
684

UNCOV
685
UnstructuredMesh::UnstructuredMesh(hid_t group) : Mesh(group)
×
686
{
UNCOV
687
  n_dimension_ = 3;
×
688

689
  // check the mesh type
690
  if (object_exists(group, "type")) {
×
691
    std::string temp;
×
692
    read_dataset(group, "type", temp);
×
693
    if (temp != mesh_type) {
×
UNCOV
694
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
695
    }
UNCOV
696
  }
×
697

698
  // check if a length unit multiplier was specified
699
  if (object_exists(group, "length_multiplier")) {
×
UNCOV
700
    read_dataset(group, "length_multiplier", length_multiplier_);
×
701
  }
702

703
  // get the filename of the unstructured mesh to load
704
  if (object_exists(group, "filename")) {
×
705
    read_dataset(group, "filename", filename_);
×
706
    if (!file_exists(filename_)) {
×
UNCOV
707
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
708
    }
709
  } else {
710
    fatal_error(fmt::format(
×
UNCOV
711
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
712
  }
713

714
  if (attribute_exists(group, "options")) {
×
UNCOV
715
    read_attribute(group, "options", options_);
×
716
  }
717

718
  // check if mesh tally data should be written with
719
  // statepoint files
720
  if (attribute_exists(group, "output")) {
×
UNCOV
721
    read_attribute(group, "output", output_);
×
722
  }
UNCOV
723
}
×
724

725
void UnstructuredMesh::determine_bounds()
25✔
726
{
727
  double xmin = INFTY;
25✔
728
  double ymin = INFTY;
25✔
729
  double zmin = INFTY;
25✔
730
  double xmax = -INFTY;
25✔
731
  double ymax = -INFTY;
25✔
732
  double zmax = -INFTY;
25✔
733
  int n = this->n_vertices();
25!
734
  for (int i = 0; i < n; ++i) {
55,951✔
735
    auto v = this->vertex(i);
55,926!
736
    xmin = std::min(v.x, xmin);
55,926✔
737
    ymin = std::min(v.y, ymin);
55,926✔
738
    zmin = std::min(v.z, zmin);
55,926✔
739
    xmax = std::max(v.x, xmax);
55,926✔
740
    ymax = std::max(v.y, ymax);
55,926✔
741
    zmax = std::max(v.z, zmax);
55,926✔
742
  }
743
  lower_left_ = {xmin, ymin, zmin};
25!
744
  upper_right_ = {xmax, ymax, zmax};
25!
745
}
25✔
746

747
Position UnstructuredMesh::sample_tet(
601,230✔
748
  std::array<Position, 4> coords, uint64_t* seed) const
749
{
750
  // Uniform distribution
751
  double s = prn(seed);
601,230✔
752
  double t = prn(seed);
601,230✔
753
  double u = prn(seed);
601,230✔
754

755
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
756
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
757
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
758
  if (s + t > 1) {
601,230✔
759
    s = 1.0 - s;
300,535✔
760
    t = 1.0 - t;
300,535✔
761
  }
762
  if (s + t + u > 1) {
601,230✔
763
    if (t + u > 1) {
400,935✔
764
      double old_t = t;
200,698✔
765
      t = 1.0 - u;
200,698✔
766
      u = 1.0 - s - old_t;
200,698✔
767
    } else if (t + u <= 1) {
200,237!
768
      double old_s = s;
200,237✔
769
      s = 1.0 - t - u;
200,237✔
770
      u = old_s + t + u - 1;
200,237✔
771
    }
772
  }
773
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
774
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
775
}
776

777
const std::string UnstructuredMesh::mesh_type = "unstructured";
778

779
std::string UnstructuredMesh::get_mesh_type() const
32✔
780
{
781
  return mesh_type;
32✔
782
}
783

UNCOV
784
void UnstructuredMesh::surface_bins_crossed(
×
785
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
786
{
UNCOV
787
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
788
}
789

790
std::string UnstructuredMesh::bin_label(int bin) const
205,736✔
791
{
792
  return fmt::format("Mesh Index ({})", bin);
205,736!
793
};
794

795
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
32✔
796
{
797
  write_dataset(mesh_group, "filename", filename_);
32!
798
  write_dataset(mesh_group, "library", this->library());
32!
799
  if (!options_.empty()) {
32✔
800
    write_attribute(mesh_group, "options", options_);
8!
801
  }
802

803
  if (length_multiplier_ > 0.0)
32!
UNCOV
804
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
805

806
  // write vertex coordinates
807
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
32!
808
  for (int i = 0; i < this->n_vertices(); i++) {
70,275!
809
    auto v = this->vertex(i);
70,243!
810
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
70,243!
811
  }
812
  write_dataset(mesh_group, "vertices", vertices);
32!
813

814
  int num_elem_skipped = 0;
32✔
815

816
  // write element types and connectivity
817
  vector<double> volumes;
32✔
818
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
32!
819
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
32!
820
  for (int i = 0; i < this->n_bins(); i++) {
349,768!
821
    auto conn = this->connectivity(i);
349,736!
822

823
    volumes.emplace_back(this->volume(i));
349,736!
824

825
    // write linear tet element
826
    if (conn.size() == 4) {
349,736✔
827
      xt::view(elem_types, i, xt::all()) =
695,472!
828
        static_cast<int>(ElementType::LINEAR_TET);
695,472!
829
      xt::view(connectivity, i, xt::all()) =
695,472!
830
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
1,043,208!
831
      // write linear hex element
832
    } else if (conn.size() == 8) {
2,000!
833
      xt::view(elem_types, i, xt::all()) =
4,000!
834
        static_cast<int>(ElementType::LINEAR_HEX);
4,000!
835
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000!
836
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000!
837
    } else {
838
      num_elem_skipped++;
×
839
      xt::view(elem_types, i, xt::all()) =
×
840
        static_cast<int>(ElementType::UNSUPPORTED);
×
UNCOV
841
      xt::view(connectivity, i, xt::all()) = -1;
×
842
    }
843
  }
349,736✔
844

845
  // warn users that some elements were skipped
846
  if (num_elem_skipped > 0) {
32!
UNCOV
847
    warning(fmt::format("The connectivity of {} elements "
×
848
                        "on mesh {} were not written "
849
                        "because they are not of type linear tet/hex.",
UNCOV
850
      num_elem_skipped, this->id_));
×
851
  }
852

853
  write_dataset(mesh_group, "volumes", volumes);
32!
854
  write_dataset(mesh_group, "connectivity", connectivity);
32!
855
  write_dataset(mesh_group, "element_types", elem_types);
32!
856
}
32✔
857

858
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
859
{
860
  length_multiplier_ = length_multiplier;
23✔
861
}
23✔
862

863
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
864
{
865
  auto conn = connectivity(bin);
120,000!
866

867
  if (conn.size() == 4)
120,000!
868
    return ElementType::LINEAR_TET;
120,000✔
869
  else if (conn.size() == 8)
×
UNCOV
870
    return ElementType::LINEAR_HEX;
×
871
  else
UNCOV
872
    return ElementType::UNSUPPORTED;
×
873
}
120,000✔
874

875
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,141,882,859✔
876
  Position r, bool& in_mesh) const
877
{
878
  MeshIndex ijk;
879
  in_mesh = true;
1,141,882,859✔
880
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
881
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
882

883
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
884
      in_mesh = false;
102,612,478✔
885
  }
886
  return ijk;
1,141,882,859✔
887
}
888

889
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,690,586,612✔
890
{
891
  switch (n_dimension_) {
1,690,586,612!
892
  case 1:
880,605✔
893
    return ijk[0] - 1;
880,605✔
894
  case 2:
97,386,432✔
895
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
97,386,432✔
896
  case 3:
1,592,319,575✔
897
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,592,319,575✔
898
  default:
×
UNCOV
899
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
900
  }
901
}
902

903
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
7,879,806✔
904
{
905
  MeshIndex ijk;
906
  if (n_dimension_ == 1) {
7,879,806✔
907
    ijk[0] = bin + 1;
275✔
908
  } else if (n_dimension_ == 2) {
7,879,531✔
909
    ijk[0] = bin % shape_[0] + 1;
15,576✔
910
    ijk[1] = bin / shape_[0] + 1;
15,576✔
911
  } else if (n_dimension_ == 3) {
7,863,955!
912
    ijk[0] = bin % shape_[0] + 1;
7,863,955✔
913
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
7,863,955✔
914
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
7,863,955✔
915
  }
916
  return ijk;
7,879,806✔
917
}
918

919
int StructuredMesh::get_bin(Position r) const
247,910,024✔
920
{
921
  // Determine indices
922
  bool in_mesh;
923
  MeshIndex ijk = get_indices(r, in_mesh);
247,910,024✔
924
  if (!in_mesh)
247,910,024✔
925
    return -1;
21,004,880✔
926

927
  // Convert indices to bin
928
  return get_bin_from_indices(ijk);
226,905,144✔
929
}
930

931
int StructuredMesh::n_bins() const
1,139,434✔
932
{
933
  return std::accumulate(
1,139,434✔
934
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,278,868✔
935
}
936

937
int StructuredMesh::n_surface_bins() const
380✔
938
{
939
  return 4 * n_dimension_ * n_bins();
380✔
940
}
941

UNCOV
942
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
943
  const SourceSite* bank, int64_t length, bool* outside) const
944
{
945
  // Determine shape of array for counts
946
  std::size_t m = this->n_bins();
×
UNCOV
947
  vector<std::size_t> shape = {m};
×
948

949
  // Create array of zeros
950
  xt::xarray<double> cnt {shape, 0.0};
×
UNCOV
951
  bool outside_ = false;
×
952

953
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
954
    const auto& site = bank[i];
×
955

956
    // determine scoring bin for entropy mesh
UNCOV
957
    int mesh_bin = get_bin(site.r);
×
958

959
    // if outside mesh, skip particle
960
    if (mesh_bin < 0) {
×
961
      outside_ = true;
×
UNCOV
962
      continue;
×
963
    }
964

965
    // Add to appropriate bin
UNCOV
966
    cnt(mesh_bin) += site.wgt;
×
967
  }
968

969
  // Create copy of count data. Since ownership will be acquired by xtensor,
970
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
971
  // warnings.
972
  int total = cnt.size();
×
UNCOV
973
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
974

975
#ifdef OPENMC_MPI
976
  // collect values from all processors
977
  MPI_Reduce(
×
978
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
979

980
  // Check if there were sites outside the mesh for any processor
981
  if (outside) {
×
982
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
×
983
  }
984
#else
985
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
×
986
  if (outside)
×
987
    *outside = outside_;
988
#endif
989

990
  // Adapt reduced values in array back into an xarray
991
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
UNCOV
992
  xt::xarray<double> counts = arr;
×
993

994
  return counts;
×
UNCOV
995
}
×
996

997
// raytrace through the mesh. The template class T will do the tallying.
998
// A modern optimizing compiler can recognize the noop method of T and
999
// eliminate that call entirely.
1000
template<class T>
1001
void StructuredMesh::raytrace_mesh(
898,857,725✔
1002
  Position r0, Position r1, const Direction& u, T tally) const
1003
{
1004
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
1005
  // enable/disable tally calls for now, T template type needs to provide both
1006
  // surface and track methods, which might be empty. modern optimizing
1007
  // compilers will (hopefully) eliminate the complete code (including
1008
  // calculation of parameters) but for the future: be explicit
1009

1010
  // Compute the length of the entire track.
1011
  double total_distance = (r1 - r0).norm();
898,857,725✔
1012
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
898,857,725✔
1013
    return;
11,632,807✔
1014

1015
  // keep a copy of the original global position to pass to get_indices,
1016
  // which performs its own transformation to local coordinates
1017
  Position global_r = r0;
887,224,918✔
1018
  Position local_r = local_coords(r0);
887,224,918✔
1019

1020
  const int n = n_dimension_;
887,224,918✔
1021

1022
  // Flag if position is inside the mesh
1023
  bool in_mesh;
1024

1025
  // Position is r = r0 + u * traveled_distance, start at r0
1026
  double traveled_distance {0.0};
887,224,918✔
1027

1028
  // Calculate index of current cell. Offset the position a tiny bit in
1029
  // direction of flight
1030
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
887,224,918✔
1031

1032
  // if track is very short, assume that it is completely inside one cell.
1033
  // Only the current cell will score and no surfaces
1034
  if (total_distance < 2 * TINY_BIT) {
887,224,918✔
1035
    if (in_mesh) {
331,527✔
1036
      tally.track(ijk, 1.0);
331,043✔
1037
    }
1038
    return;
331,527✔
1039
  }
1040

1041
  // Calculate initial distances to next surfaces in all three dimensions
1042
  std::array<MeshDistance, 3> distances;
1,773,786,782✔
1043
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
1044
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
1045
  }
1046

1047
  // Loop until r = r1 is eventually reached
1048
  while (true) {
744,582,576✔
1049

1050
    if (in_mesh) {
1,631,475,967✔
1051

1052
      // find surface with minimal distance to current position
1053
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,545,235,800✔
1054
                     distances.begin();
1,545,235,800✔
1055

1056
      // Tally track length delta since last step
1057
      tally.track(ijk,
1,545,235,800✔
1058
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,545,235,800✔
1059
          total_distance);
1060

1061
      // update position and leave, if we have reached end position
1062
      traveled_distance = distances[k].distance;
1,545,235,800✔
1063
      if (traveled_distance >= total_distance)
1,545,235,800✔
1064
        return;
807,401,141✔
1065

1066
      // If we have not reached r1, we have hit a surface. Tally outward
1067
      // current
1068
      tally.surface(ijk, k, distances[k].max_surface, false);
737,834,659✔
1069

1070
      // Update cell and calculate distance to next surface in k-direction.
1071
      // The two other directions are still valid!
1072
      ijk[k] = distances[k].next_index;
737,834,659✔
1073
      distances[k] =
737,834,659✔
1074
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
737,834,659✔
1075

1076
      // Check if we have left the interior of the mesh
1077
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
737,834,659✔
1078

1079
      // If we are still inside the mesh, tally inward current for the next
1080
      // cell
1081
      if (in_mesh)
737,834,659✔
1082
        tally.surface(ijk, k, !distances[k].max_surface, true);
724,485,726✔
1083

1084
    } else { // not inside mesh
1085

1086
      // For all directions outside the mesh, find the distance that we need
1087
      // to travel to reach the next surface. Use the largest distance, as
1088
      // only this will cross all outer surfaces.
1089
      int k_max {-1};
86,240,167✔
1090
      for (int k = 0; k < n; ++k) {
343,516,302✔
1091
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
351,524,217✔
1092
            (distances[k].distance > traveled_distance)) {
94,248,082✔
1093
          traveled_distance = distances[k].distance;
89,264,558✔
1094
          k_max = k;
89,264,558✔
1095
        }
1096
      }
1097
      // Assure some distance is traveled
1098
      if (k_max == -1) {
86,240,167✔
1099
        traveled_distance += TINY_BIT;
110✔
1100
      }
1101

1102
      // If r1 is not inside the mesh, exit here
1103
      if (traveled_distance >= total_distance)
86,240,167✔
1104
        return;
79,492,250✔
1105

1106
      // Calculate the new cell index and update all distances to next
1107
      // surfaces.
1108
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,747,917✔
1109
      for (int k = 0; k < n; ++k) {
26,783,130✔
1110
        distances[k] =
20,035,213✔
1111
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
20,035,213✔
1112
      }
1113

1114
      // If inside the mesh, Tally inward current
1115
      if (in_mesh && k_max >= 0)
6,747,917!
1116
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,322,871✔
1117
    }
1118
  }
1119
}
1120

1121
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
786,730,160✔
1122
  vector<int>& bins, vector<double>& lengths) const
1123
{
1124

1125
  // Helper tally class.
1126
  // stores a pointer to the mesh class and references to bins and lengths
1127
  // parameters. Performs the actual tally through the track method.
1128
  struct TrackAggregator {
1129
    TrackAggregator(
786,730,160✔
1130
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1131
      : mesh(_mesh), bins(_bins), lengths(_lengths)
786,730,160✔
1132
    {}
786,730,160✔
1133
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1,410,484,067✔
1134
    void track(const MeshIndex& ijk, double l) const
1,405,522,279✔
1135
    {
1136
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,405,522,279✔
1137
      lengths.push_back(l);
1,405,522,279✔
1138
    }
1,405,522,279✔
1139

1140
    const StructuredMesh* mesh;
1141
    vector<int>& bins;
1142
    vector<double>& lengths;
1143
  };
1144

1145
  // Perform the mesh raytrace with the helper class.
1146
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
786,730,160✔
1147
}
786,730,160✔
1148

1149
void StructuredMesh::surface_bins_crossed(
112,127,565✔
1150
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1151
{
1152

1153
  // Helper tally class.
1154
  // stores a pointer to the mesh class and a reference to the bins parameter.
1155
  // Performs the actual tally through the surface method.
1156
  struct SurfaceAggregator {
1157
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
112,127,565✔
1158
      : mesh(_mesh), bins(_bins)
112,127,565✔
1159
    {}
112,127,565✔
1160
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
58,159,189✔
1161
    {
1162
      int i_bin =
1163
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
58,159,189✔
1164
      if (max)
58,159,189✔
1165
        i_bin += 2;
29,051,440✔
1166
      if (inward)
58,159,189✔
1167
        i_bin += 1;
28,582,290✔
1168
      bins.push_back(i_bin);
58,159,189✔
1169
    }
58,159,189✔
1170
    void track(const MeshIndex& idx, double l) const {}
140,044,564✔
1171

1172
    const StructuredMesh* mesh;
1173
    vector<int>& bins;
1174
  };
1175

1176
  // Perform the mesh raytrace with the helper class.
1177
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
112,127,565✔
1178
}
112,127,565✔
1179

1180
//==============================================================================
1181
// RegularMesh implementation
1182
//==============================================================================
1183

1184
int RegularMesh::set_grid()
2,027✔
1185
{
1186
  auto shape = xt::adapt(shape_, {n_dimension_});
2,027✔
1187

1188
  // Check that dimensions are all greater than zero
1189
  if (xt::any(shape <= 0)) {
2,027!
UNCOV
1190
    set_errmsg("All entries for a regular mesh dimensions "
×
1191
               "must be positive.");
UNCOV
1192
    return OPENMC_E_INVALID_ARGUMENT;
×
1193
  }
1194

1195
  // Make sure lower_left and dimension match
1196
  if (lower_left_.size() != n_dimension_) {
2,027!
UNCOV
1197
    set_errmsg("Number of entries in lower_left must be the same "
×
1198
               "as the regular mesh dimensions.");
UNCOV
1199
    return OPENMC_E_INVALID_ARGUMENT;
×
1200
  }
1201
  if (width_.size() > 0) {
2,027✔
1202

1203
    // Check to ensure width has same dimensions
1204
    if (width_.size() != n_dimension_) {
49!
UNCOV
1205
      set_errmsg("Number of entries on width must be the same as "
×
1206
                 "the regular mesh dimensions.");
UNCOV
1207
      return OPENMC_E_INVALID_ARGUMENT;
×
1208
    }
1209

1210
    // Check for negative widths
1211
    if (xt::any(width_ < 0.0)) {
49!
1212
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
UNCOV
1213
      return OPENMC_E_INVALID_ARGUMENT;
×
1214
    }
1215

1216
    // Set width and upper right coordinate
1217
    upper_right_ = xt::eval(lower_left_ + shape * width_);
49✔
1218

1219
  } else if (upper_right_.size() > 0) {
1,978!
1220

1221
    // Check to ensure upper_right_ has same dimensions
1222
    if (upper_right_.size() != n_dimension_) {
1,978!
UNCOV
1223
      set_errmsg("Number of entries on upper_right must be the "
×
1224
                 "same as the regular mesh dimensions.");
UNCOV
1225
      return OPENMC_E_INVALID_ARGUMENT;
×
1226
    }
1227

1228
    // Check that upper-right is above lower-left
1229
    if (xt::any(upper_right_ < lower_left_)) {
1,978!
UNCOV
1230
      set_errmsg(
×
1231
        "The upper_right coordinates of a regular mesh must be greater than "
1232
        "the lower_left coordinates.");
UNCOV
1233
      return OPENMC_E_INVALID_ARGUMENT;
×
1234
    }
1235

1236
    // Set width
1237
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1,978✔
1238
  }
1239

1240
  // Set material volumes
1241
  volume_frac_ = 1.0 / xt::prod(shape)();
2,027✔
1242

1243
  element_volume_ = 1.0;
2,027✔
1244
  for (int i = 0; i < n_dimension_; i++) {
7,654✔
1245
    element_volume_ *= width_[i];
5,627✔
1246
  }
1247
  return 0;
2,027✔
1248
}
2,027✔
1249

1250
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
2,016✔
1251
{
1252
  // Determine number of dimensions for mesh
1253
  if (!check_for_node(node, "dimension")) {
2,016!
UNCOV
1254
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1255
  }
1256

1257
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
2,016✔
1258
  int n = n_dimension_ = shape.size();
2,016✔
1259
  if (n != 1 && n != 2 && n != 3) {
2,016!
UNCOV
1260
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1261
  }
1262
  std::copy(shape.begin(), shape.end(), shape_.begin());
2,016✔
1263

1264
  // Check for lower-left coordinates
1265
  if (check_for_node(node, "lower_left")) {
2,016!
1266
    // Read mesh lower-left corner location
1267
    lower_left_ = get_node_xarray<double>(node, "lower_left");
2,016✔
1268
  } else {
UNCOV
1269
    fatal_error("Must specify <lower_left> on a mesh.");
×
1270
  }
1271

1272
  if (check_for_node(node, "width")) {
2,016✔
1273
    // Make sure one of upper-right or width were specified
1274
    if (check_for_node(node, "upper_right")) {
49!
UNCOV
1275
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1276
    }
1277

1278
    width_ = get_node_xarray<double>(node, "width");
49✔
1279

1280
  } else if (check_for_node(node, "upper_right")) {
1,967!
1281

1282
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1,967✔
1283

1284
  } else {
UNCOV
1285
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1286
  }
1287

1288
  if (int err = set_grid()) {
2,016!
UNCOV
1289
    fatal_error(openmc_err_msg);
×
1290
  }
1291
}
2,016✔
1292

1293
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
11✔
1294
{
1295
  // Determine number of dimensions for mesh
1296
  if (!object_exists(group, "dimension")) {
11!
UNCOV
1297
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1298
  }
1299

1300
  xt::xtensor<int, 1> shape;
11✔
1301
  read_dataset(group, "dimension", shape);
11✔
1302
  int n = n_dimension_ = shape.size();
11✔
1303
  if (n != 1 && n != 2 && n != 3) {
11!
UNCOV
1304
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1305
  }
1306
  std::copy(shape.begin(), shape.end(), shape_.begin());
11✔
1307

1308
  // Check for lower-left coordinates
1309
  if (object_exists(group, "lower_left")) {
11!
1310
    // Read mesh lower-left corner location
1311
    read_dataset(group, "lower_left", lower_left_);
11✔
1312
  } else {
UNCOV
1313
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1314
  }
1315

1316
  if (object_exists(group, "upper_right")) {
11!
1317

1318
    read_dataset(group, "upper_right", upper_right_);
11✔
1319

1320
  } else {
UNCOV
1321
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1322
  }
1323

1324
  if (int err = set_grid()) {
11!
UNCOV
1325
    fatal_error(openmc_err_msg);
×
1326
  }
1327
}
11✔
1328

1329
int RegularMesh::get_index_in_direction(double r, int i) const
2,147,483,647✔
1330
{
1331
  return std::ceil((r - lower_left_[i]) / width_[i]);
2,147,483,647✔
1332
}
1333

1334
const std::string RegularMesh::mesh_type = "regular";
1335

1336
std::string RegularMesh::get_mesh_type() const
3,115✔
1337
{
1338
  return mesh_type;
3,115✔
1339
}
1340

1341
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,436,223,126✔
1342
{
1343
  return lower_left_[i] + ijk[i] * width_[i];
1,436,223,126✔
1344
}
1345

1346
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,372,944,276✔
1347
{
1348
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,372,944,276✔
1349
}
1350

1351
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
2,147,483,647✔
1352
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1353
  double l) const
1354
{
1355
  MeshDistance d;
2,147,483,647✔
1356
  d.next_index = ijk[i];
2,147,483,647✔
1357
  if (std::abs(u[i]) < FP_PRECISION)
2,147,483,647✔
1358
    return d;
1,754,214✔
1359

1360
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1361
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1362
    d.next_index++;
1,431,623,082✔
1363
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,431,623,082✔
1364
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,389,762,855✔
1365
    d.next_index--;
1,368,344,232✔
1366
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,368,344,232✔
1367
  }
1368

1369
  return d;
2,147,483,647✔
1370
}
1371

1372
std::pair<vector<double>, vector<double>> RegularMesh::plot(
22✔
1373
  Position plot_ll, Position plot_ur) const
1374
{
1375
  // Figure out which axes lie in the plane of the plot.
1376
  array<int, 2> axes {-1, -1};
22✔
1377
  if (plot_ur.z == plot_ll.z) {
22!
1378
    axes[0] = 0;
22✔
1379
    if (n_dimension_ > 1)
22!
1380
      axes[1] = 1;
22✔
1381
  } else if (plot_ur.y == plot_ll.y) {
×
1382
    axes[0] = 0;
×
1383
    if (n_dimension_ > 2)
×
1384
      axes[1] = 2;
×
1385
  } else if (plot_ur.x == plot_ll.x) {
×
1386
    if (n_dimension_ > 1)
×
1387
      axes[0] = 1;
×
1388
    if (n_dimension_ > 2)
×
UNCOV
1389
      axes[1] = 2;
×
1390
  } else {
UNCOV
1391
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1392
  }
1393

1394
  // Get the coordinates of the mesh lines along both of the axes.
1395
  array<vector<double>, 2> axis_lines;
22✔
1396
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
66✔
1397
    int axis = axes[i_ax];
44✔
1398
    if (axis == -1)
44!
UNCOV
1399
      continue;
×
1400
    auto& lines {axis_lines[i_ax]};
44✔
1401

1402
    double coord = lower_left_[axis];
44✔
1403
    for (int i = 0; i < shape_[axis] + 1; ++i) {
286✔
1404
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
242!
1405
        lines.push_back(coord);
242✔
1406
      coord += width_[axis];
242✔
1407
    }
1408
  }
1409

1410
  return {axis_lines[0], axis_lines[1]};
44✔
1411
}
22✔
1412

1413
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1,993✔
1414
{
1415
  write_dataset(mesh_group, "dimension", get_x_shape());
1,993✔
1416
  write_dataset(mesh_group, "lower_left", lower_left_);
1,993✔
1417
  write_dataset(mesh_group, "upper_right", upper_right_);
1,993✔
1418
  write_dataset(mesh_group, "width", width_);
1,993✔
1419
}
1,993✔
1420

1421
xt::xtensor<double, 1> RegularMesh::count_sites(
7,839✔
1422
  const SourceSite* bank, int64_t length, bool* outside) const
1423
{
1424
  // Determine shape of array for counts
1425
  std::size_t m = this->n_bins();
7,839✔
1426
  vector<std::size_t> shape = {m};
7,839✔
1427

1428
  // Create array of zeros
1429
  xt::xarray<double> cnt {shape, 0.0};
7,839✔
1430
  bool outside_ = false;
7,839✔
1431

1432
  for (int64_t i = 0; i < length; i++) {
7,675,290✔
1433
    const auto& site = bank[i];
7,667,451✔
1434

1435
    // determine scoring bin for entropy mesh
1436
    int mesh_bin = get_bin(site.r);
7,667,451✔
1437

1438
    // if outside mesh, skip particle
1439
    if (mesh_bin < 0) {
7,667,451!
1440
      outside_ = true;
×
UNCOV
1441
      continue;
×
1442
    }
1443

1444
    // Add to appropriate bin
1445
    cnt(mesh_bin) += site.wgt;
7,667,451✔
1446
  }
1447

1448
  // Create copy of count data. Since ownership will be acquired by xtensor,
1449
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
1450
  // warnings.
1451
  int total = cnt.size();
7,839✔
1452
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
7,839✔
1453

1454
#ifdef OPENMC_MPI
1455
  // collect values from all processors
1456
  MPI_Reduce(
3,615✔
1457
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
3,615✔
1458

1459
  // Check if there were sites outside the mesh for any processor
1460
  if (outside) {
3,615!
1461
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
3,615✔
1462
  }
1463
#else
1464
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
4,224✔
1465
  if (outside)
4,224!
1466
    *outside = outside_;
4,224✔
1467
#endif
1468

1469
  // Adapt reduced values in array back into an xarray
1470
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
7,839✔
1471
  xt::xarray<double> counts = arr;
7,839✔
1472

1473
  return counts;
15,678✔
1474
}
7,839✔
1475

1476
double RegularMesh::volume(const MeshIndex& ijk) const
1,126,096✔
1477
{
1478
  return element_volume_;
1,126,096✔
1479
}
1480

1481
//==============================================================================
1482
// RectilinearMesh implementation
1483
//==============================================================================
1484

1485
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
125✔
1486
{
1487
  n_dimension_ = 3;
125✔
1488

1489
  grid_[0] = get_node_array<double>(node, "x_grid");
125✔
1490
  grid_[1] = get_node_array<double>(node, "y_grid");
125✔
1491
  grid_[2] = get_node_array<double>(node, "z_grid");
125✔
1492

1493
  if (int err = set_grid()) {
125!
UNCOV
1494
    fatal_error(openmc_err_msg);
×
1495
  }
1496
}
125✔
1497

1498
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
11✔
1499
{
1500
  n_dimension_ = 3;
11✔
1501

1502
  read_dataset(group, "x_grid", grid_[0]);
11✔
1503
  read_dataset(group, "y_grid", grid_[1]);
11✔
1504
  read_dataset(group, "z_grid", grid_[2]);
11✔
1505

1506
  if (int err = set_grid()) {
11!
UNCOV
1507
    fatal_error(openmc_err_msg);
×
1508
  }
1509
}
11✔
1510

1511
const std::string RectilinearMesh::mesh_type = "rectilinear";
1512

1513
std::string RectilinearMesh::get_mesh_type() const
275✔
1514
{
1515
  return mesh_type;
275✔
1516
}
1517

1518
double RectilinearMesh::positive_grid_boundary(
26,505,963✔
1519
  const MeshIndex& ijk, int i) const
1520
{
1521
  return grid_[i][ijk[i]];
26,505,963✔
1522
}
1523

1524
double RectilinearMesh::negative_grid_boundary(
25,739,406✔
1525
  const MeshIndex& ijk, int i) const
1526
{
1527
  return grid_[i][ijk[i] - 1];
25,739,406✔
1528
}
1529

1530
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
53,602,087✔
1531
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1532
  double l) const
1533
{
1534
  MeshDistance d;
53,602,087✔
1535
  d.next_index = ijk[i];
53,602,087✔
1536
  if (std::abs(u[i]) < FP_PRECISION)
53,602,087✔
1537
    return d;
571,824✔
1538

1539
  d.max_surface = (u[i] > 0);
53,030,263✔
1540
  if (d.max_surface && (ijk[i] <= shape_[i])) {
53,030,263✔
1541
    d.next_index++;
26,505,963✔
1542
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
26,505,963✔
1543
  } else if (!d.max_surface && (ijk[i] > 0)) {
26,524,300✔
1544
    d.next_index--;
25,739,406✔
1545
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
25,739,406✔
1546
  }
1547
  return d;
53,030,263✔
1548
}
1549

1550
int RectilinearMesh::set_grid()
180✔
1551
{
1552
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
180✔
1553
    static_cast<int>(grid_[1].size()) - 1,
180✔
1554
    static_cast<int>(grid_[2].size()) - 1};
180✔
1555

1556
  for (const auto& g : grid_) {
720✔
1557
    if (g.size() < 2) {
540!
UNCOV
1558
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1559
                 "must each have at least 2 points");
UNCOV
1560
      return OPENMC_E_INVALID_ARGUMENT;
×
1561
    }
1562
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
540✔
1563
        g.end()) {
1,080!
UNCOV
1564
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1565
                 "rectilinear meshes must be sorted and unique.");
UNCOV
1566
      return OPENMC_E_INVALID_ARGUMENT;
×
1567
    }
1568
  }
1569

1570
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
180✔
1571
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
180✔
1572

1573
  return 0;
180✔
1574
}
1575

1576
int RectilinearMesh::get_index_in_direction(double r, int i) const
74,108,892✔
1577
{
1578
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,108,892✔
1579
}
1580

1581
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
11✔
1582
  Position plot_ll, Position plot_ur) const
1583
{
1584
  // Figure out which axes lie in the plane of the plot.
1585
  array<int, 2> axes {-1, -1};
11✔
1586
  if (plot_ur.z == plot_ll.z) {
11!
UNCOV
1587
    axes = {0, 1};
×
1588
  } else if (plot_ur.y == plot_ll.y) {
11!
1589
    axes = {0, 2};
11✔
1590
  } else if (plot_ur.x == plot_ll.x) {
×
UNCOV
1591
    axes = {1, 2};
×
1592
  } else {
UNCOV
1593
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1594
  }
1595

1596
  // Get the coordinates of the mesh lines along both of the axes.
1597
  array<vector<double>, 2> axis_lines;
11✔
1598
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
33✔
1599
    int axis = axes[i_ax];
22✔
1600
    vector<double>& lines {axis_lines[i_ax]};
22✔
1601

1602
    for (auto coord : grid_[axis]) {
110✔
1603
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
88!
1604
        lines.push_back(coord);
88✔
1605
    }
1606
  }
1607

1608
  return {axis_lines[0], axis_lines[1]};
22✔
1609
}
11✔
1610

1611
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
110✔
1612
{
1613
  write_dataset(mesh_group, "x_grid", grid_[0]);
110✔
1614
  write_dataset(mesh_group, "y_grid", grid_[1]);
110✔
1615
  write_dataset(mesh_group, "z_grid", grid_[2]);
110✔
1616
}
110✔
1617

1618
double RectilinearMesh::volume(const MeshIndex& ijk) const
132✔
1619
{
1620
  double vol {1.0};
132✔
1621

1622
  for (int i = 0; i < n_dimension_; i++) {
528✔
1623
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
396✔
1624
  }
1625
  return vol;
132✔
1626
}
1627

1628
//==============================================================================
1629
// CylindricalMesh implementation
1630
//==============================================================================
1631

1632
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
401✔
1633
  : PeriodicStructuredMesh {node}
401✔
1634
{
1635
  n_dimension_ = 3;
401✔
1636
  grid_[0] = get_node_array<double>(node, "r_grid");
401✔
1637
  grid_[1] = get_node_array<double>(node, "phi_grid");
401✔
1638
  grid_[2] = get_node_array<double>(node, "z_grid");
401✔
1639
  origin_ = get_node_position(node, "origin");
401✔
1640

1641
  if (int err = set_grid()) {
401!
UNCOV
1642
    fatal_error(openmc_err_msg);
×
1643
  }
1644
}
401✔
1645

1646
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1647
{
1648
  n_dimension_ = 3;
11✔
1649
  read_dataset(group, "r_grid", grid_[0]);
11✔
1650
  read_dataset(group, "phi_grid", grid_[1]);
11✔
1651
  read_dataset(group, "z_grid", grid_[2]);
11✔
1652
  read_dataset(group, "origin", origin_);
11✔
1653

1654
  if (int err = set_grid()) {
11!
UNCOV
1655
    fatal_error(openmc_err_msg);
×
1656
  }
1657
}
11✔
1658

1659
const std::string CylindricalMesh::mesh_type = "cylindrical";
1660

1661
std::string CylindricalMesh::get_mesh_type() const
484✔
1662
{
1663
  return mesh_type;
484✔
1664
}
1665

1666
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
47,726,668✔
1667
  Position r, bool& in_mesh) const
1668
{
1669
  r = local_coords(r);
47,726,668✔
1670

1671
  Position mapped_r;
47,726,668✔
1672
  mapped_r[0] = std::hypot(r.x, r.y);
47,726,668✔
1673
  mapped_r[2] = r[2];
47,726,668✔
1674

1675
  if (mapped_r[0] < FP_PRECISION) {
47,726,668!
UNCOV
1676
    mapped_r[1] = 0.0;
×
1677
  } else {
1678
    mapped_r[1] = std::atan2(r.y, r.x);
47,726,668✔
1679
    if (mapped_r[1] < 0)
47,726,668✔
1680
      mapped_r[1] += 2 * M_PI;
23,872,431✔
1681
  }
1682

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

1685
  idx[1] = sanitize_phi(idx[1]);
47,726,668✔
1686

1687
  return idx;
47,726,668✔
1688
}
1689

1690
Position CylindricalMesh::sample_element(
88,110✔
1691
  const MeshIndex& ijk, uint64_t* seed) const
1692
{
1693
  double r_min = this->r(ijk[0] - 1);
88,110✔
1694
  double r_max = this->r(ijk[0]);
88,110✔
1695

1696
  double phi_min = this->phi(ijk[1] - 1);
88,110✔
1697
  double phi_max = this->phi(ijk[1]);
88,110✔
1698

1699
  double z_min = this->z(ijk[2] - 1);
88,110✔
1700
  double z_max = this->z(ijk[2]);
88,110✔
1701

1702
  double r_min_sq = r_min * r_min;
88,110✔
1703
  double r_max_sq = r_max * r_max;
88,110✔
1704
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
88,110✔
1705
  double phi = uniform_distribution(phi_min, phi_max, seed);
88,110✔
1706
  double z = uniform_distribution(z_min, z_max, seed);
88,110✔
1707

1708
  double x = r * std::cos(phi);
88,110✔
1709
  double y = r * std::sin(phi);
88,110✔
1710

1711
  return origin_ + Position(x, y, z);
88,110✔
1712
}
1713

1714
double CylindricalMesh::find_r_crossing(
142,570,168✔
1715
  const Position& r, const Direction& u, double l, int shell) const
1716
{
1717

1718
  if ((shell < 0) || (shell > shape_[0]))
142,570,168!
1719
    return INFTY;
17,913,962✔
1720

1721
  // solve r.x^2 + r.y^2 == r0^2
1722
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1723
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1724

1725
  const double r0 = grid_[0][shell];
124,656,206✔
1726
  if (r0 == 0.0)
124,656,206✔
1727
    return INFTY;
7,130,651✔
1728

1729
  const double denominator = u.x * u.x + u.y * u.y;
117,525,555✔
1730

1731
  // Direction of flight is in z-direction. Will never intersect r.
1732
  if (std::abs(denominator) < FP_PRECISION)
117,525,555✔
1733
    return INFTY;
58,960✔
1734

1735
  // inverse of dominator to help the compiler to speed things up
1736
  const double inv_denominator = 1.0 / denominator;
117,466,595✔
1737

1738
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,466,595✔
1739
  double c = r.x * r.x + r.y * r.y - r0 * r0;
117,466,595✔
1740
  double D = p * p - c * inv_denominator;
117,466,595✔
1741

1742
  if (D < 0.0)
117,466,595✔
1743
    return INFTY;
9,733,570✔
1744

1745
  D = std::sqrt(D);
107,733,025✔
1746

1747
  // the solution -p - D is always smaller as -p + D : Check this one first
1748
  if (std::abs(c) <= RADIAL_MESH_TOL)
107,733,025✔
1749
    return INFTY;
6,611,374✔
1750

1751
  if (-p - D > l)
101,121,651✔
1752
    return -p - D;
20,206,426✔
1753
  if (-p + D > l)
80,915,225✔
1754
    return -p + D;
50,091,351✔
1755

1756
  return INFTY;
30,823,874✔
1757
}
1758

1759
double CylindricalMesh::find_phi_crossing(
74,445,558✔
1760
  const Position& r, const Direction& u, double l, int shell) const
1761
{
1762
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1763
  if (full_phi_ && (shape_[1] == 1))
74,445,558✔
1764
    return INFTY;
30,474,840✔
1765

1766
  shell = sanitize_phi(shell);
43,970,718✔
1767

1768
  const double p0 = grid_[1][shell];
43,970,718✔
1769

1770
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1771
  // => x(s) * cos(p0) = y(s) * sin(p0)
1772
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1773
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1774

1775
  const double c0 = std::cos(p0);
43,970,718✔
1776
  const double s0 = std::sin(p0);
43,970,718✔
1777

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

1780
  // Check if direction of flight is not parallel to phi surface
1781
  if (std::abs(denominator) > FP_PRECISION) {
43,970,718✔
1782
    const double s = -(r.x * s0 - r.y * c0) / denominator;
43,709,974✔
1783
    // Check if solution is in positive direction of flight and crosses the
1784
    // correct phi surface (not -phi)
1785
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
43,709,974✔
1786
      return s;
20,219,859✔
1787
  }
1788

1789
  return INFTY;
23,750,859✔
1790
}
1791

1792
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
36,690,324✔
1793
  const Position& r, const Direction& u, double l, int shell) const
1794
{
1795
  MeshDistance d;
36,690,324✔
1796
  d.next_index = shell;
36,690,324✔
1797

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

1802
  d.max_surface = (u.z > 0.0);
35,572,108✔
1803
  if (d.max_surface && (shell <= shape_[2])) {
35,572,108✔
1804
    d.next_index += 1;
16,873,241✔
1805
    d.distance = (grid_[2][shell] - r.z) / u.z;
16,873,241✔
1806
  } else if (!d.max_surface && (shell > 0)) {
18,698,867✔
1807
    d.next_index -= 1;
16,843,453✔
1808
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
16,843,453✔
1809
  }
1810
  return d;
35,572,108✔
1811
}
1812

1813
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,198,187✔
1814
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1815
  double l) const
1816
{
1817
  if (i == 0) {
145,198,187✔
1818

1819
    return std::min(
71,285,084✔
1820
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
71,285,084✔
1821
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,570,168✔
1822

1823
  } else if (i == 1) {
73,913,103✔
1824

1825
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
37,222,779✔
1826
                      find_phi_crossing(r0, u, l, ijk[i])),
37,222,779✔
1827
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
37,222,779✔
1828
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
74,445,558✔
1829

1830
  } else {
1831
    return find_z_crossing(r0, u, l, ijk[i]);
36,690,324✔
1832
  }
1833
}
1834

1835
int CylindricalMesh::set_grid()
434✔
1836
{
1837
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
434✔
1838
    static_cast<int>(grid_[1].size()) - 1,
434✔
1839
    static_cast<int>(grid_[2].size()) - 1};
434✔
1840

1841
  for (const auto& g : grid_) {
1,736✔
1842
    if (g.size() < 2) {
1,302!
UNCOV
1843
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
1844
                 "must each have at least 2 points");
UNCOV
1845
      return OPENMC_E_INVALID_ARGUMENT;
×
1846
    }
1847
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,302✔
1848
        g.end()) {
2,604!
UNCOV
1849
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
1850
                 "cylindrical meshes must be sorted and unique.");
UNCOV
1851
      return OPENMC_E_INVALID_ARGUMENT;
×
1852
    }
1853
  }
1854
  if (grid_[0].front() < 0.0) {
434!
UNCOV
1855
    set_errmsg("r-grid for "
×
1856
               "cylindrical meshes must start at r >= 0.");
UNCOV
1857
    return OPENMC_E_INVALID_ARGUMENT;
×
1858
  }
1859
  if (grid_[1].front() < 0.0) {
434!
UNCOV
1860
    set_errmsg("phi-grid for "
×
1861
               "cylindrical meshes must start at phi >= 0.");
UNCOV
1862
    return OPENMC_E_INVALID_ARGUMENT;
×
1863
  }
1864
  if (grid_[1].back() > 2.0 * PI) {
434!
UNCOV
1865
    set_errmsg("phi-grids for "
×
1866
               "cylindrical meshes must end with theta <= 2*pi.");
1867

UNCOV
1868
    return OPENMC_E_INVALID_ARGUMENT;
×
1869
  }
1870

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

1873
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
868✔
1874
    origin_[2] + grid_[2].front()};
868✔
1875
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
868✔
1876
    origin_[2] + grid_[2].back()};
868✔
1877

1878
  return 0;
434✔
1879
}
1880

1881
int CylindricalMesh::get_index_in_direction(double r, int i) const
143,180,004✔
1882
{
1883
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
143,180,004✔
1884
}
1885

UNCOV
1886
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
1887
  Position plot_ll, Position plot_ur) const
1888
{
UNCOV
1889
  fatal_error("Plot of cylindrical Mesh not implemented");
×
1890

1891
  // Figure out which axes lie in the plane of the plot.
1892
  array<vector<double>, 2> axis_lines;
1893
  return {axis_lines[0], axis_lines[1]};
1894
}
1895

1896
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
374✔
1897
{
1898
  write_dataset(mesh_group, "r_grid", grid_[0]);
374✔
1899
  write_dataset(mesh_group, "phi_grid", grid_[1]);
374✔
1900
  write_dataset(mesh_group, "z_grid", grid_[2]);
374✔
1901
  write_dataset(mesh_group, "origin", origin_);
374✔
1902
}
374✔
1903

1904
double CylindricalMesh::volume(const MeshIndex& ijk) const
792✔
1905
{
1906
  double r_i = grid_[0][ijk[0] - 1];
792✔
1907
  double r_o = grid_[0][ijk[0]];
792✔
1908

1909
  double phi_i = grid_[1][ijk[1] - 1];
792✔
1910
  double phi_o = grid_[1][ijk[1]];
792✔
1911

1912
  double z_i = grid_[2][ijk[2] - 1];
792✔
1913
  double z_o = grid_[2][ijk[2]];
792✔
1914

1915
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
792✔
1916
}
1917

1918
//==============================================================================
1919
// SphericalMesh implementation
1920
//==============================================================================
1921

1922
SphericalMesh::SphericalMesh(pugi::xml_node node)
346✔
1923
  : PeriodicStructuredMesh {node}
346✔
1924
{
1925
  n_dimension_ = 3;
346✔
1926

1927
  grid_[0] = get_node_array<double>(node, "r_grid");
346✔
1928
  grid_[1] = get_node_array<double>(node, "theta_grid");
346✔
1929
  grid_[2] = get_node_array<double>(node, "phi_grid");
346✔
1930
  origin_ = get_node_position(node, "origin");
346✔
1931

1932
  if (int err = set_grid()) {
346!
UNCOV
1933
    fatal_error(openmc_err_msg);
×
1934
  }
1935
}
346✔
1936

1937
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
11✔
1938
{
1939
  n_dimension_ = 3;
11✔
1940

1941
  read_dataset(group, "r_grid", grid_[0]);
11✔
1942
  read_dataset(group, "theta_grid", grid_[1]);
11✔
1943
  read_dataset(group, "phi_grid", grid_[2]);
11✔
1944
  read_dataset(group, "origin", origin_);
11✔
1945

1946
  if (int err = set_grid()) {
11!
UNCOV
1947
    fatal_error(openmc_err_msg);
×
1948
  }
1949
}
11✔
1950

1951
const std::string SphericalMesh::mesh_type = "spherical";
1952

1953
std::string SphericalMesh::get_mesh_type() const
385✔
1954
{
1955
  return mesh_type;
385✔
1956
}
1957

1958
StructuredMesh::MeshIndex SphericalMesh::get_indices(
68,175,250✔
1959
  Position r, bool& in_mesh) const
1960
{
1961
  r = local_coords(r);
68,175,250✔
1962

1963
  Position mapped_r;
68,175,250✔
1964
  mapped_r[0] = r.norm();
68,175,250✔
1965

1966
  if (mapped_r[0] < FP_PRECISION) {
68,175,250!
1967
    mapped_r[1] = 0.0;
×
UNCOV
1968
    mapped_r[2] = 0.0;
×
1969
  } else {
1970
    mapped_r[1] = std::acos(r.z / mapped_r.x);
68,175,250✔
1971
    mapped_r[2] = std::atan2(r.y, r.x);
68,175,250✔
1972
    if (mapped_r[2] < 0)
68,175,250✔
1973
      mapped_r[2] += 2 * M_PI;
34,062,281✔
1974
  }
1975

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

1978
  idx[1] = sanitize_theta(idx[1]);
68,175,250✔
1979
  idx[2] = sanitize_phi(idx[2]);
68,175,250✔
1980

1981
  return idx;
68,175,250✔
1982
}
1983

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

1990
  double theta_min = this->theta(ijk[1] - 1);
110✔
1991
  double theta_max = this->theta(ijk[1]);
110✔
1992

1993
  double phi_min = this->phi(ijk[2] - 1);
110✔
1994
  double phi_max = this->phi(ijk[2]);
110✔
1995

1996
  double cos_theta =
1997
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
110✔
1998
  double sin_theta = std::sin(std::acos(cos_theta));
110✔
1999
  double phi = uniform_distribution(phi_min, phi_max, seed);
110✔
2000
  double r_min_cub = std::pow(r_min, 3);
110✔
2001
  double r_max_cub = std::pow(r_max, 3);
110✔
2002
  // might be faster to do rejection here?
2003
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110✔
2004

2005
  double x = r * std::cos(phi) * sin_theta;
110✔
2006
  double y = r * std::sin(phi) * sin_theta;
110✔
2007
  double z = r * cos_theta;
110✔
2008

2009
  return origin_ + Position(x, y, z);
110✔
2010
}
2011

2012
double SphericalMesh::find_r_crossing(
443,074,280✔
2013
  const Position& r, const Direction& u, double l, int shell) const
2014
{
2015
  if ((shell < 0) || (shell > shape_[0]))
443,074,280✔
2016
    return INFTY;
39,620,317✔
2017

2018
  // solve |r+s*u| = r0
2019
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2020
  const double r0 = grid_[0][shell];
403,453,963✔
2021
  if (r0 == 0.0)
403,453,963✔
2022
    return INFTY;
7,261,639✔
2023
  const double p = r.dot(u);
396,192,324✔
2024
  double c = r.dot(r) - r0 * r0;
396,192,324✔
2025
  double D = p * p - c;
396,192,324✔
2026

2027
  if (std::abs(c) <= RADIAL_MESH_TOL)
396,192,324✔
2028
    return INFTY;
10,598,654✔
2029

2030
  if (D >= 0.0) {
385,593,670✔
2031
    D = std::sqrt(D);
357,716,722✔
2032
    // the solution -p - D is always smaller as -p + D : Check this one first
2033
    if (-p - D > l)
357,716,722✔
2034
      return -p - D;
64,277,774✔
2035
    if (-p + D > l)
293,438,948✔
2036
      return -p + D;
176,899,096✔
2037
  }
2038

2039
  return INFTY;
144,416,800✔
2040
}
2041

2042
double SphericalMesh::find_theta_crossing(
109,327,592✔
2043
  const Position& r, const Direction& u, double l, int shell) const
2044
{
2045
  // Theta grid is [0, π], thus there is no real surface to cross
2046
  if (full_theta_ && (shape_[1] == 1))
109,327,592✔
2047
    return INFTY;
70,969,052✔
2048

2049
  shell = sanitize_theta(shell);
38,358,540✔
2050

2051
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2052
  // yields
2053
  // a*s^2 + 2*b*s + c == 0 with
2054
  // a = cos(theta)^2 - u.z * u.z
2055
  // b = r*u * cos(theta)^2 - u.z * r.z
2056
  // c = r*r * cos(theta)^2 - r.z^2
2057

2058
  const double cos_t = std::cos(grid_[1][shell]);
38,358,540✔
2059
  const bool sgn = std::signbit(cos_t);
38,358,540✔
2060
  const double cos_t_2 = cos_t * cos_t;
38,358,540✔
2061

2062
  const double a = cos_t_2 - u.z * u.z;
38,358,540✔
2063
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
38,358,540✔
2064
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
38,358,540✔
2065

2066
  // if factor of s^2 is zero, direction of flight is parallel to theta
2067
  // surface
2068
  if (std::abs(a) < FP_PRECISION) {
38,358,540✔
2069
    // if b vanishes, direction of flight is within theta surface and crossing
2070
    // is not possible
2071
    if (std::abs(b) < FP_PRECISION)
482,548!
2072
      return INFTY;
482,548✔
2073

UNCOV
2074
    const double s = -0.5 * c / b;
×
2075
    // Check if solution is in positive direction of flight and has correct
2076
    // sign
2077
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
UNCOV
2078
      return s;
×
2079

2080
    // no crossing is possible
UNCOV
2081
    return INFTY;
×
2082
  }
2083

2084
  const double p = b / a;
37,875,992✔
2085
  double D = p * p - c / a;
37,875,992✔
2086

2087
  if (D < 0.0)
37,875,992✔
2088
    return INFTY;
10,954,988✔
2089

2090
  D = std::sqrt(D);
26,921,004✔
2091

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

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

2103
  return INFTY;
11,475,101✔
2104
}
2105

2106
double SphericalMesh::find_phi_crossing(
110,917,070✔
2107
  const Position& r, const Direction& u, double l, int shell) const
2108
{
2109
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2110
  if (full_phi_ && (shape_[2] == 1))
110,917,070✔
2111
    return INFTY;
70,969,052✔
2112

2113
  shell = sanitize_phi(shell);
39,948,018✔
2114

2115
  const double p0 = grid_[2][shell];
39,948,018✔
2116

2117
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2118
  // => x(s) * cos(p0) = y(s) * sin(p0)
2119
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2120
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2121

2122
  const double c0 = std::cos(p0);
39,948,018✔
2123
  const double s0 = std::sin(p0);
39,948,018✔
2124

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

2127
  // Check if direction of flight is not parallel to phi surface
2128
  if (std::abs(denominator) > FP_PRECISION) {
39,948,018✔
2129
    const double s = -(r.x * s0 - r.y * c0) / denominator;
39,714,026✔
2130
    // Check if solution is in positive direction of flight and crosses the
2131
    // correct phi surface (not -phi)
2132
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
39,714,026✔
2133
      return s;
17,579,452✔
2134
  }
2135

2136
  return INFTY;
22,368,566✔
2137
}
2138

2139
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
331,659,471✔
2140
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2141
  double l) const
2142
{
2143

2144
  if (i == 0) {
331,659,471✔
2145
    return std::min(
221,537,140✔
2146
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
221,537,140✔
2147
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
443,074,280✔
2148

2149
  } else if (i == 1) {
110,122,331✔
2150
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
54,663,796✔
2151
                      find_theta_crossing(r0, u, l, ijk[i])),
54,663,796✔
2152
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
54,663,796✔
2153
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
109,327,592✔
2154

2155
  } else {
2156
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
55,458,535✔
2157
                      find_phi_crossing(r0, u, l, ijk[i])),
55,458,535✔
2158
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
55,458,535✔
2159
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
110,917,070✔
2160
  }
2161
}
2162

2163
int SphericalMesh::set_grid()
379✔
2164
{
2165
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
379✔
2166
    static_cast<int>(grid_[1].size()) - 1,
379✔
2167
    static_cast<int>(grid_[2].size()) - 1};
379✔
2168

2169
  for (const auto& g : grid_) {
1,516✔
2170
    if (g.size() < 2) {
1,137!
UNCOV
2171
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2172
                 "must each have at least 2 points");
UNCOV
2173
      return OPENMC_E_INVALID_ARGUMENT;
×
2174
    }
2175
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,137✔
2176
        g.end()) {
2,274!
UNCOV
2177
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2178
                 "spherical meshes must be sorted and unique.");
UNCOV
2179
      return OPENMC_E_INVALID_ARGUMENT;
×
2180
    }
2181
    if (g.front() < 0.0) {
1,137!
UNCOV
2182
      set_errmsg("r-, theta-, and phi- grids for "
×
2183
                 "spherical meshes must start at v >= 0.");
UNCOV
2184
      return OPENMC_E_INVALID_ARGUMENT;
×
2185
    }
2186
  }
2187
  if (grid_[1].back() > PI) {
379!
UNCOV
2188
    set_errmsg("theta-grids for "
×
2189
               "spherical meshes must end with theta <= pi.");
2190

UNCOV
2191
    return OPENMC_E_INVALID_ARGUMENT;
×
2192
  }
2193
  if (grid_[2].back() > 2 * PI) {
379!
UNCOV
2194
    set_errmsg("phi-grids for "
×
2195
               "spherical meshes must end with phi <= 2*pi.");
UNCOV
2196
    return OPENMC_E_INVALID_ARGUMENT;
×
2197
  }
2198

2199
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
379!
2200
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
379✔
2201

2202
  double r = grid_[0].back();
379✔
2203
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
379✔
2204
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
379✔
2205

2206
  return 0;
379✔
2207
}
2208

2209
int SphericalMesh::get_index_in_direction(double r, int i) const
204,525,750✔
2210
{
2211
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
204,525,750✔
2212
}
2213

UNCOV
2214
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2215
  Position plot_ll, Position plot_ur) const
2216
{
UNCOV
2217
  fatal_error("Plot of spherical Mesh not implemented");
×
2218

2219
  // Figure out which axes lie in the plane of the plot.
2220
  array<vector<double>, 2> axis_lines;
2221
  return {axis_lines[0], axis_lines[1]};
2222
}
2223

2224
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
319✔
2225
{
2226
  write_dataset(mesh_group, "r_grid", grid_[0]);
319✔
2227
  write_dataset(mesh_group, "theta_grid", grid_[1]);
319✔
2228
  write_dataset(mesh_group, "phi_grid", grid_[2]);
319✔
2229
  write_dataset(mesh_group, "origin", origin_);
319✔
2230
}
319✔
2231

2232
double SphericalMesh::volume(const MeshIndex& ijk) const
935✔
2233
{
2234
  double r_i = grid_[0][ijk[0] - 1];
935✔
2235
  double r_o = grid_[0][ijk[0]];
935✔
2236

2237
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2238
  double theta_o = grid_[1][ijk[1]];
935✔
2239

2240
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2241
  double phi_o = grid_[2][ijk[2]];
935✔
2242

2243
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
935✔
2244
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
935✔
2245
}
2246

2247
//==============================================================================
2248
// Helper functions for the C API
2249
//==============================================================================
2250

2251
int check_mesh(int32_t index)
6,336✔
2252
{
2253
  if (index < 0 || index >= model::meshes.size()) {
6,336!
2254
    set_errmsg("Index in meshes array is out of bounds.");
×
UNCOV
2255
    return OPENMC_E_OUT_OF_BOUNDS;
×
2256
  }
2257
  return 0;
6,336✔
2258
}
2259

2260
template<class T>
2261
int check_mesh_type(int32_t index)
1,100✔
2262
{
2263
  if (int err = check_mesh(index))
1,100!
UNCOV
2264
    return err;
×
2265

2266
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1,100!
2267
  if (!mesh) {
1,100!
2268
    set_errmsg("This function is not valid for input mesh.");
×
UNCOV
2269
    return OPENMC_E_INVALID_TYPE;
×
2270
  }
2271
  return 0;
1,100✔
2272
}
2273

2274
template<class T>
2275
bool is_mesh_type(int32_t index)
2276
{
2277
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2278
  return mesh;
2279
}
2280

2281
//==============================================================================
2282
// C API functions
2283
//==============================================================================
2284

2285
// Return the type of mesh as a C string
2286
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
1,463✔
2287
{
2288
  if (int err = check_mesh(index))
1,463!
UNCOV
2289
    return err;
×
2290

2291
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
1,463✔
2292

2293
  return 0;
1,463✔
2294
}
2295

2296
//! Extend the meshes array by n elements
2297
extern "C" int openmc_extend_meshes(
253✔
2298
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2299
{
2300
  if (index_start)
253!
2301
    *index_start = model::meshes.size();
253✔
2302
  std::string mesh_type;
253✔
2303

2304
  for (int i = 0; i < n; ++i) {
506✔
2305
    if (RegularMesh::mesh_type == type) {
253✔
2306
      model::meshes.push_back(make_unique<RegularMesh>());
165✔
2307
    } else if (RectilinearMesh::mesh_type == type) {
88✔
2308
      model::meshes.push_back(make_unique<RectilinearMesh>());
44✔
2309
    } else if (CylindricalMesh::mesh_type == type) {
44✔
2310
      model::meshes.push_back(make_unique<CylindricalMesh>());
22✔
2311
    } else if (SphericalMesh::mesh_type == type) {
22!
2312
      model::meshes.push_back(make_unique<SphericalMesh>());
22✔
2313
    } else {
UNCOV
2314
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2315
    }
2316
  }
2317
  if (index_end)
253!
UNCOV
2318
    *index_end = model::meshes.size() - 1;
×
2319

2320
  return 0;
253✔
2321
}
253✔
2322

2323
//! Adds a new unstructured mesh to OpenMC
UNCOV
2324
extern "C" int openmc_add_unstructured_mesh(
×
2325
  const char filename[], const char library[], int* id)
2326
{
2327
  std::string lib_name(library);
×
2328
  std::string mesh_file(filename);
×
UNCOV
2329
  bool valid_lib = false;
×
2330

2331
#ifdef OPENMC_DAGMC_ENABLED
2332
  if (lib_name == MOABMesh::mesh_lib_type) {
×
2333
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
×
2334
    valid_lib = true;
2335
  }
2336
#endif
2337

2338
#ifdef OPENMC_LIBMESH_ENABLED
2339
  if (lib_name == LibMesh::mesh_lib_type) {
×
2340
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2341
    valid_lib = true;
2342
  }
2343
#endif
2344

2345
  if (!valid_lib) {
×
UNCOV
2346
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2347
                           "by this build of OpenMC",
2348
      lib_name));
UNCOV
2349
    return OPENMC_E_INVALID_ARGUMENT;
×
2350
  }
2351

2352
  // auto-assign new ID
2353
  model::meshes.back()->set_id(-1);
×
UNCOV
2354
  *id = model::meshes.back()->id_;
×
2355

2356
  return 0;
×
UNCOV
2357
}
×
2358

2359
//! Return the index in the meshes array of a mesh with a given ID
2360
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
429✔
2361
{
2362
  auto pair = model::mesh_map.find(id);
429✔
2363
  if (pair == model::mesh_map.end()) {
429!
2364
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
UNCOV
2365
    return OPENMC_E_INVALID_ID;
×
2366
  }
2367
  *index = pair->second;
429✔
2368
  return 0;
429✔
2369
}
2370

2371
//! Return the ID of a mesh
2372
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2,794✔
2373
{
2374
  if (int err = check_mesh(index))
2,794!
UNCOV
2375
    return err;
×
2376
  *id = model::meshes[index]->id_;
2,794✔
2377
  return 0;
2,794✔
2378
}
2379

2380
//! Set the ID of a mesh
2381
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
253✔
2382
{
2383
  if (int err = check_mesh(index))
253!
UNCOV
2384
    return err;
×
2385
  model::meshes[index]->id_ = id;
253✔
2386
  model::mesh_map[id] = index;
253✔
2387
  return 0;
253✔
2388
}
2389

2390
//! Get the number of elements in a mesh
2391
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
264✔
2392
{
2393
  if (int err = check_mesh(index))
264!
UNCOV
2394
    return err;
×
2395
  *n = model::meshes[index]->n_bins();
264✔
2396
  return 0;
264✔
2397
}
2398

2399
//! Get the volume of each element in the mesh
2400
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
88✔
2401
{
2402
  if (int err = check_mesh(index))
88!
UNCOV
2403
    return err;
×
2404
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
968✔
2405
    volumes[i] = model::meshes[index]->volume(i);
880✔
2406
  }
2407
  return 0;
88✔
2408
}
2409

2410
//! Get the bounding box of a mesh
2411
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
154✔
2412
{
2413
  if (int err = check_mesh(index))
154!
UNCOV
2414
    return err;
×
2415

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

2418
  // set lower left corner values
2419
  ll[0] = bbox.min.x;
154✔
2420
  ll[1] = bbox.min.y;
154✔
2421
  ll[2] = bbox.min.z;
154✔
2422

2423
  // set upper right corner values
2424
  ur[0] = bbox.max.x;
154✔
2425
  ur[1] = bbox.max.y;
154✔
2426
  ur[2] = bbox.max.z;
154✔
2427
  return 0;
154✔
2428
}
2429

2430
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
176✔
2431
  int nz, int table_size, int32_t* materials, double* volumes)
2432
{
2433
  if (int err = check_mesh(index))
176!
UNCOV
2434
    return err;
×
2435

2436
  try {
2437
    model::meshes[index]->material_volumes(
176✔
2438
      nx, ny, nz, table_size, materials, volumes);
2439
  } catch (const std::exception& e) {
11!
2440
    set_errmsg(e.what());
11✔
2441
    if (starts_with(e.what(), "Mesh")) {
11!
2442
      return OPENMC_E_GEOMETRY;
11✔
2443
    } else {
UNCOV
2444
      return OPENMC_E_ALLOCATE;
×
2445
    }
2446
  }
11✔
2447

2448
  return 0;
165✔
2449
}
2450

2451
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
44✔
2452
  Position width, int basis, int* pixels, int32_t* data)
2453
{
2454
  if (int err = check_mesh(index))
44!
UNCOV
2455
    return err;
×
2456
  const auto& mesh = model::meshes[index].get();
44✔
2457

2458
  int pixel_width = pixels[0];
44✔
2459
  int pixel_height = pixels[1];
44✔
2460

2461
  // get pixel size
2462
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
44✔
2463
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
44✔
2464

2465
  // setup basis indices and initial position centered on pixel
2466
  int in_i, out_i;
2467
  Position xyz = origin;
44✔
2468
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2469
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
44✔
2470
  switch (basis_enum) {
44!
2471
  case PlotBasis::xy:
44✔
2472
    in_i = 0;
44✔
2473
    out_i = 1;
44✔
2474
    break;
44✔
2475
  case PlotBasis::xz:
×
2476
    in_i = 0;
×
2477
    out_i = 2;
×
2478
    break;
×
2479
  case PlotBasis::yz:
×
2480
    in_i = 1;
×
2481
    out_i = 2;
×
2482
    break;
×
2483
  default:
×
UNCOV
2484
    UNREACHABLE();
×
2485
  }
2486

2487
  // set initial position
2488
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
44✔
2489
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
44✔
2490

2491
#pragma omp parallel
24✔
2492
  {
2493
    Position r = xyz;
20✔
2494

2495
#pragma omp for
2496
    for (int y = 0; y < pixel_height; y++) {
420✔
2497
      r[out_i] = xyz[out_i] - out_pixel * y;
400✔
2498
      for (int x = 0; x < pixel_width; x++) {
8,400✔
2499
        r[in_i] = xyz[in_i] + in_pixel * x;
8,000✔
2500
        data[pixel_width * y + x] = mesh->get_bin(r);
8,000✔
2501
      }
2502
    }
2503
  }
2504

2505
  return 0;
44✔
2506
}
2507

2508
//! Get the dimension of a regular mesh
2509
extern "C" int openmc_regular_mesh_get_dimension(
11✔
2510
  int32_t index, int** dims, int* n)
2511
{
2512
  if (int err = check_mesh_type<RegularMesh>(index))
11!
UNCOV
2513
    return err;
×
2514
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
11!
2515
  *dims = mesh->shape_.data();
11✔
2516
  *n = mesh->n_dimension_;
11✔
2517
  return 0;
11✔
2518
}
2519

2520
//! Set the dimension of a regular mesh
2521
extern "C" int openmc_regular_mesh_set_dimension(
187✔
2522
  int32_t index, int n, const int* dims)
2523
{
2524
  if (int err = check_mesh_type<RegularMesh>(index))
187!
UNCOV
2525
    return err;
×
2526
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
187!
2527

2528
  // Copy dimension
2529
  mesh->n_dimension_ = n;
187✔
2530
  std::copy(dims, dims + n, mesh->shape_.begin());
187✔
2531
  return 0;
187✔
2532
}
2533

2534
//! Get the regular mesh parameters
2535
extern "C" int openmc_regular_mesh_get_params(
209✔
2536
  int32_t index, double** ll, double** ur, double** width, int* n)
2537
{
2538
  if (int err = check_mesh_type<RegularMesh>(index))
209!
UNCOV
2539
    return err;
×
2540
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
209!
2541

2542
  if (m->lower_left_.dimension() == 0) {
209!
2543
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2544
    return OPENMC_E_ALLOCATE;
×
2545
  }
2546

2547
  *ll = m->lower_left_.data();
209✔
2548
  *ur = m->upper_right_.data();
209✔
2549
  *width = m->width_.data();
209✔
2550
  *n = m->n_dimension_;
209✔
2551
  return 0;
209✔
2552
}
2553

2554
//! Set the regular mesh parameters
2555
extern "C" int openmc_regular_mesh_set_params(
220✔
2556
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2557
{
2558
  if (int err = check_mesh_type<RegularMesh>(index))
220!
UNCOV
2559
    return err;
×
2560
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
220!
2561

2562
  if (m->n_dimension_ == -1) {
220!
2563
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
UNCOV
2564
    return OPENMC_E_UNASSIGNED;
×
2565
  }
2566

2567
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
220✔
2568
  if (ll && ur) {
220✔
2569
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
198✔
2570
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
198✔
2571
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
198✔
2572
  } else if (ll && width) {
22!
2573
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
11✔
2574
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
11✔
2575
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
11✔
2576
  } else if (ur && width) {
11!
2577
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
11✔
2578
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
11✔
2579
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
11✔
2580
  } else {
2581
    set_errmsg("At least two parameters must be specified.");
×
UNCOV
2582
    return OPENMC_E_INVALID_ARGUMENT;
×
2583
  }
2584

2585
  // Set material volumes
2586

2587
  // TODO: incorporate this into method in RegularMesh that can be called from
2588
  // here and from constructor
2589
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
220✔
2590
  m->element_volume_ = 1.0;
220✔
2591
  for (int i = 0; i < m->n_dimension_; i++) {
880✔
2592
    m->element_volume_ *= m->width_[i];
660✔
2593
  }
2594

2595
  return 0;
220✔
2596
}
220✔
2597

2598
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2599
template<class C>
2600
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
88✔
2601
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2602
  const int nz)
2603
{
2604
  if (int err = check_mesh_type<C>(index))
88!
UNCOV
2605
    return err;
×
2606

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

2609
  m->n_dimension_ = 3;
88✔
2610

2611
  m->grid_[0].reserve(nx);
88✔
2612
  m->grid_[1].reserve(ny);
88✔
2613
  m->grid_[2].reserve(nz);
88✔
2614

2615
  for (int i = 0; i < nx; i++) {
572✔
2616
    m->grid_[0].push_back(grid_x[i]);
484✔
2617
  }
2618
  for (int i = 0; i < ny; i++) {
341✔
2619
    m->grid_[1].push_back(grid_y[i]);
253✔
2620
  }
2621
  for (int i = 0; i < nz; i++) {
319✔
2622
    m->grid_[2].push_back(grid_z[i]);
231✔
2623
  }
2624

2625
  int err = m->set_grid();
88✔
2626
  return err;
88✔
2627
}
2628

2629
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2630
template<class C>
2631
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
385✔
2632
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2633
{
2634
  if (int err = check_mesh_type<C>(index))
385!
UNCOV
2635
    return err;
×
2636
  C* m = dynamic_cast<C*>(model::meshes[index].get());
385!
2637

2638
  if (m->lower_left_.dimension() == 0) {
385!
2639
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2640
    return OPENMC_E_ALLOCATE;
×
2641
  }
2642

2643
  *grid_x = m->grid_[0].data();
385✔
2644
  *nx = m->grid_[0].size();
385✔
2645
  *grid_y = m->grid_[1].data();
385✔
2646
  *ny = m->grid_[1].size();
385✔
2647
  *grid_z = m->grid_[2].data();
385✔
2648
  *nz = m->grid_[2].size();
385✔
2649

2650
  return 0;
385✔
2651
}
2652

2653
//! Get the rectilinear mesh grid
2654
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
143✔
2655
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2656
{
2657
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
143✔
2658
    index, grid_x, nx, grid_y, ny, grid_z, nz);
143✔
2659
}
2660

2661
//! Set the rectilienar mesh parameters
2662
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
44✔
2663
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2664
  const double* grid_z, const int nz)
2665
{
2666
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
44✔
2667
    index, grid_x, nx, grid_y, ny, grid_z, nz);
44✔
2668
}
2669

2670
//! Get the cylindrical mesh grid
2671
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2672
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2673
{
2674
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
121✔
2675
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2676
}
2677

2678
//! Set the cylindrical mesh parameters
2679
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
22✔
2680
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2681
  const double* grid_z, const int nz)
2682
{
2683
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
22✔
2684
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2685
}
2686

2687
//! Get the spherical mesh grid
2688
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
121✔
2689
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2690
{
2691

2692
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
121✔
2693
    index, grid_x, nx, grid_y, ny, grid_z, nz);
121✔
2694
  ;
2695
}
2696

2697
//! Set the spherical mesh parameters
2698
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
22✔
2699
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2700
  const double* grid_z, const int nz)
2701
{
2702
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
22✔
2703
    index, grid_x, nx, grid_y, ny, grid_z, nz);
22✔
2704
}
2705

2706
#ifdef OPENMC_DAGMC_ENABLED
2707

2708
const std::string MOABMesh::mesh_lib_type = "moab";
2709

2710
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
24✔
2711
{
2712
  initialize();
24✔
2713
}
24✔
2714

2715
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
2716
{
2717
  initialize();
×
2718
}
2719

2720
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2721
  : UnstructuredMesh()
×
2722
{
2723
  n_dimension_ = 3;
2724
  filename_ = filename;
×
2725
  set_length_multiplier(length_multiplier);
×
2726
  initialize();
×
2727
}
2728

2729
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
1✔
2730
{
2731
  mbi_ = external_mbi;
1✔
2732
  filename_ = "unknown (external file)";
1✔
2733
  this->initialize();
1✔
2734
}
1✔
2735

2736
void MOABMesh::initialize()
25✔
2737
{
2738

2739
  // Create the MOAB interface and load data from file
2740
  this->create_interface();
25✔
2741

2742
  // Initialise MOAB error code
2743
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
2744

2745
  // Set the dimension
2746
  n_dimension_ = 3;
25✔
2747

2748
  // set member range of tetrahedral entities
2749
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
25✔
2750
  if (rval != moab::MB_SUCCESS) {
25!
2751
    fatal_error("Failed to get all tetrahedral elements");
2752
  }
2753

2754
  if (!ehs_.all_of_type(moab::MBTET)) {
25!
2755
    warning("Non-tetrahedral elements found in unstructured "
×
2756
            "mesh file: " +
2757
            filename_);
2758
  }
2759

2760
  // set member range of vertices
2761
  int vertex_dim = 0;
25✔
2762
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
25✔
2763
  if (rval != moab::MB_SUCCESS) {
25!
2764
    fatal_error("Failed to get all vertex handles");
2765
  }
2766

2767
  // make an entity set for all tetrahedra
2768
  // this is used for convenience later in output
2769
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
25✔
2770
  if (rval != moab::MB_SUCCESS) {
25!
2771
    fatal_error("Failed to create an entity set for the tetrahedral elements");
2772
  }
2773

2774
  rval = mbi_->add_entities(tetset_, ehs_);
25✔
2775
  if (rval != moab::MB_SUCCESS) {
25!
2776
    fatal_error("Failed to add tetrahedra to an entity set.");
2777
  }
2778

2779
  if (length_multiplier_ > 0.0) {
25!
2780
    // get the connectivity of all tets
2781
    moab::Range adj;
×
2782
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
×
2783
    if (rval != moab::MB_SUCCESS) {
×
2784
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
2785
    }
2786
    // scale all vertex coords by multiplier (done individually so not all
2787
    // coordinates are in memory twice at once)
2788
    for (auto vert : adj) {
×
2789
      // retrieve coords
2790
      std::array<double, 3> coord;
2791
      rval = mbi_->get_coords(&vert, 1, coord.data());
×
2792
      if (rval != moab::MB_SUCCESS) {
×
2793
        fatal_error("Could not get coordinates of vertex.");
2794
      }
2795
      // scale coords
2796
      for (auto& c : coord) {
×
2797
        c *= length_multiplier_;
2798
      }
2799
      // set new coords
2800
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2801
      if (rval != moab::MB_SUCCESS) {
×
2802
        fatal_error("Failed to set new vertex coordinates");
2803
      }
2804
    }
2805
  }
2806

2807
  // Determine bounds of mesh
2808
  this->determine_bounds();
25✔
2809
}
25✔
2810

2811
void MOABMesh::prepare_for_point_location()
21✔
2812
{
2813
  // if the KDTree has already been constructed, do nothing
2814
  if (kdtree_)
21!
2815
    return;
2816

2817
  // build acceleration data structures
2818
  compute_barycentric_data(ehs_);
21✔
2819
  build_kdtree(ehs_);
21✔
2820
}
2821

2822
void MOABMesh::create_interface()
25✔
2823
{
2824
  // Do not create a MOAB instance if one is already in memory
2825
  if (mbi_)
25✔
2826
    return;
1✔
2827

2828
  // create MOAB instance
2829
  mbi_ = std::make_shared<moab::Core>();
24✔
2830

2831
  // load unstructured mesh file
2832
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
24✔
2833
  if (rval != moab::MB_SUCCESS) {
24!
2834
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
2835
  }
2836
}
2837

2838
void MOABMesh::build_kdtree(const moab::Range& all_tets)
21✔
2839
{
2840
  moab::Range all_tris;
21✔
2841
  int adj_dim = 2;
21✔
2842
  write_message("Getting tet adjacencies...", 7);
21✔
2843
  moab::ErrorCode rval = mbi_->get_adjacencies(
21✔
2844
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2845
  if (rval != moab::MB_SUCCESS) {
21!
2846
    fatal_error("Failed to get adjacent triangles for tets");
2847
  }
2848

2849
  if (!all_tris.all_of_type(moab::MBTRI)) {
21!
2850
    warning("Non-triangle elements found in tet adjacencies in "
×
2851
            "unstructured mesh file: " +
2852
            filename_);
×
2853
  }
2854

2855
  // combine into one range
2856
  moab::Range all_tets_and_tris;
21✔
2857
  all_tets_and_tris.merge(all_tets);
21✔
2858
  all_tets_and_tris.merge(all_tris);
21✔
2859

2860
  // create a kd-tree instance
2861
  write_message(
21✔
2862
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
21✔
2863
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
21✔
2864

2865
  // Determine what options to use
2866
  std::ostringstream options_stream;
21✔
2867
  if (options_.empty()) {
21✔
2868
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
5✔
2869
  } else {
2870
    options_stream << options_;
16✔
2871
  }
2872
  moab::FileOptions file_opts(options_stream.str().c_str());
21✔
2873

2874
  // Build the k-d tree
2875
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
21✔
2876
  if (rval != moab::MB_SUCCESS) {
21!
2877
    fatal_error("Failed to construct KDTree for the "
2878
                "unstructured mesh file: " +
2879
                filename_);
×
2880
  }
2881
}
21✔
2882

2883
void MOABMesh::intersect_track(const moab::CartVect& start,
1,543,584✔
2884
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2885
{
2886
  hits.clear();
1,543,584✔
2887

2888
  moab::ErrorCode rval;
2889
  vector<moab::EntityHandle> tris;
1,543,584✔
2890
  // get all intersections with triangles in the tet mesh
2891
  // (distances are relative to the start point, not the previous
2892
  // intersection)
2893
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
1,543,584✔
2894
    dir.array(), start.array(), tris, hits, 0, track_len);
2895
  if (rval != moab::MB_SUCCESS) {
1,543,584!
2896
    fatal_error(
2897
      "Failed to compute intersections on unstructured mesh: " + filename_);
×
2898
  }
2899

2900
  // remove duplicate intersection distances
2901
  std::unique(hits.begin(), hits.end());
1,543,584✔
2902

2903
  // sorts by first component of std::pair by default
2904
  std::sort(hits.begin(), hits.end());
1,543,584✔
2905
}
1,543,584✔
2906

2907
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1,543,584✔
2908
  vector<int>& bins, vector<double>& lengths) const
2909
{
2910
  moab::CartVect start(r0.x, r0.y, r0.z);
1,543,584✔
2911
  moab::CartVect end(r1.x, r1.y, r1.z);
1,543,584✔
2912
  moab::CartVect dir(u.x, u.y, u.z);
1,543,584✔
2913
  dir.normalize();
1,543,584✔
2914

2915
  double track_len = (end - start).length();
1,543,584✔
2916
  if (track_len == 0.0)
1,543,584!
2917
    return;
721,692✔
2918

2919
  start -= TINY_BIT * dir;
1,543,584✔
2920
  end += TINY_BIT * dir;
1,543,584✔
2921

2922
  vector<double> hits;
1,543,584✔
2923
  intersect_track(start, dir, track_len, hits);
1,543,584✔
2924

2925
  bins.clear();
1,543,584✔
2926
  lengths.clear();
1,543,584✔
2927

2928
  // if there are no intersections the track may lie entirely
2929
  // within a single tet. If this is the case, apply entire
2930
  // score to that tet and return.
2931
  if (hits.size() == 0) {
1,543,584✔
2932
    Position midpoint = r0 + u * (track_len * 0.5);
721,692✔
2933
    int bin = this->get_bin(midpoint);
721,692✔
2934
    if (bin != -1) {
721,692✔
2935
      bins.push_back(bin);
242,866✔
2936
      lengths.push_back(1.0);
242,866✔
2937
    }
2938
    return;
721,692✔
2939
  }
2940

2941
  // for each segment in the set of tracks, try to look up a tet
2942
  // at the midpoint of the segment
2943
  Position current = r0;
821,892✔
2944
  double last_dist = 0.0;
821,892✔
2945
  for (const auto& hit : hits) {
5,516,161✔
2946
    // get the segment length
2947
    double segment_length = hit - last_dist;
4,694,269✔
2948
    last_dist = hit;
4,694,269✔
2949
    // find the midpoint of this segment
2950
    Position midpoint = current + u * (segment_length * 0.5);
4,694,269✔
2951
    // try to find a tet for this position
2952
    int bin = this->get_bin(midpoint);
4,694,269✔
2953

2954
    // determine the start point for this segment
2955
    current = r0 + u * hit;
4,694,269✔
2956

2957
    if (bin == -1) {
4,694,269✔
2958
      continue;
20,522✔
2959
    }
2960

2961
    bins.push_back(bin);
4,673,747✔
2962
    lengths.push_back(segment_length / track_len);
4,673,747✔
2963
  }
2964

2965
  // tally remaining portion of track after last hit if
2966
  // the last segment of the track is in the mesh but doesn't
2967
  // reach the other side of the tet
2968
  if (hits.back() < track_len) {
821,892!
2969
    Position segment_start = r0 + u * hits.back();
821,892✔
2970
    double segment_length = track_len - hits.back();
821,892✔
2971
    Position midpoint = segment_start + u * (segment_length * 0.5);
821,892✔
2972
    int bin = this->get_bin(midpoint);
821,892✔
2973
    if (bin != -1) {
821,892✔
2974
      bins.push_back(bin);
766,509✔
2975
      lengths.push_back(segment_length / track_len);
766,509✔
2976
    }
2977
  }
2978
};
1,543,584✔
2979

2980
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
7,317,172✔
2981
{
2982
  moab::CartVect pos(r.x, r.y, r.z);
7,317,172✔
2983
  // find the leaf of the kd-tree for this position
2984
  moab::AdaptiveKDTreeIter kdtree_iter;
7,317,172✔
2985
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
7,317,172✔
2986
  if (rval != moab::MB_SUCCESS) {
7,317,172✔
2987
    return 0;
1,011,897✔
2988
  }
2989

2990
  // retrieve the tet elements of this leaf
2991
  moab::EntityHandle leaf = kdtree_iter.handle();
6,305,275✔
2992
  moab::Range tets;
6,305,275✔
2993
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
6,305,275✔
2994
  if (rval != moab::MB_SUCCESS) {
6,305,275!
2995
    warning("MOAB error finding tets.");
×
2996
  }
2997

2998
  // loop over the tets in this leaf, returning the containing tet if found
2999
  for (const auto& tet : tets) {
260,209,886✔
3000
    if (point_in_tet(pos, tet)) {
260,207,039✔
3001
      return tet;
6,302,428✔
3002
    }
3003
  }
3004

3005
  // if no tet is found, return an invalid handle
3006
  return 0;
2,847✔
3007
}
7,317,172✔
3008

3009
double MOABMesh::volume(int bin) const
167,880✔
3010
{
3011
  return tet_volume(get_ent_handle_from_bin(bin));
167,880✔
3012
}
3013

3014
std::string MOABMesh::library() const
34✔
3015
{
3016
  return mesh_lib_type;
34✔
3017
}
3018

3019
// Sample position within a tet for MOAB type tets
3020
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
200,410✔
3021
{
3022

3023
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3024

3025
  // Get vertex coordinates for MOAB tet
3026
  const moab::EntityHandle* conn1;
3027
  int conn1_size;
3028
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
200,410✔
3029
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
200,410!
3030
    fatal_error(fmt::format(
×
3031
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3032
      conn1_size));
3033
  }
3034
  moab::CartVect p[4];
1,002,050✔
3035
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
200,410✔
3036
  if (rval != moab::MB_SUCCESS) {
200,410!
3037
    fatal_error("Failed to get tet coords");
3038
  }
3039

3040
  std::array<Position, 4> tet_verts;
200,410✔
3041
  for (int i = 0; i < 4; i++) {
1,002,050✔
3042
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
801,640✔
3043
  }
3044
  // Samples position within tet using Barycentric stuff
3045
  return this->sample_tet(tet_verts, seed);
400,820✔
3046
}
3047

3048
double MOABMesh::tet_volume(moab::EntityHandle tet) const
167,880✔
3049
{
3050
  vector<moab::EntityHandle> conn;
167,880✔
3051
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
167,880✔
3052
  if (rval != moab::MB_SUCCESS) {
167,880!
3053
    fatal_error("Failed to get tet connectivity");
3054
  }
3055

3056
  moab::CartVect p[4];
839,400✔
3057
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
167,880✔
3058
  if (rval != moab::MB_SUCCESS) {
167,880!
3059
    fatal_error("Failed to get tet coords");
3060
  }
3061

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

3065
int MOABMesh::get_bin(Position r) const
7,317,172✔
3066
{
3067
  moab::EntityHandle tet = get_tet(r);
7,317,172✔
3068
  if (tet == 0) {
7,317,172✔
3069
    return -1;
1,014,744✔
3070
  } else {
3071
    return get_bin_from_ent_handle(tet);
6,302,428✔
3072
  }
3073
}
3074

3075
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
21✔
3076
{
3077
  moab::ErrorCode rval;
3078

3079
  baryc_data_.clear();
21✔
3080
  baryc_data_.resize(tets.size());
21✔
3081

3082
  // compute the barycentric data for each tet element
3083
  // and store it as a 3x3 matrix
3084
  for (auto& tet : tets) {
239,757✔
3085
    vector<moab::EntityHandle> verts;
239,736✔
3086
    rval = mbi_->get_connectivity(&tet, 1, verts);
239,736✔
3087
    if (rval != moab::MB_SUCCESS) {
239,736!
3088
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
×
3089
    }
3090

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

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

3099
    // invert now to avoid this cost later
3100
    a = a.transpose().inverse();
239,736✔
3101
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
239,736✔
3102
  }
239,736✔
3103
}
21✔
3104

3105
bool MOABMesh::point_in_tet(
260,207,039✔
3106
  const moab::CartVect& r, moab::EntityHandle tet) const
3107
{
3108

3109
  moab::ErrorCode rval;
3110

3111
  // get tet vertices
3112
  vector<moab::EntityHandle> verts;
260,207,039✔
3113
  rval = mbi_->get_connectivity(&tet, 1, verts);
260,207,039✔
3114
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3115
    warning("Failed to get vertices of tet in umesh: " + filename_);
×
3116
    return false;
3117
  }
3118

3119
  // first vertex is used as a reference point for the barycentric data -
3120
  // retrieve its coordinates
3121
  moab::CartVect p_zero;
260,207,039✔
3122
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
260,207,039✔
3123
  if (rval != moab::MB_SUCCESS) {
260,207,039!
3124
    warning("Failed to get coordinates of a vertex in "
×
3125
            "unstructured mesh: " +
3126
            filename_);
×
3127
    return false;
3128
  }
3129

3130
  // look up barycentric data
3131
  int idx = get_bin_from_ent_handle(tet);
260,207,039✔
3132
  const moab::Matrix3& a_inv = baryc_data_[idx];
260,207,039✔
3133

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

3136
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
421,415,065✔
3137
          bary_coords[2] >= 0.0 &&
443,103,099✔
3138
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
281,895,073✔
3139
}
260,207,039✔
3140

3141
int MOABMesh::get_bin_from_index(int idx) const
3142
{
3143
  if (idx >= n_bins()) {
×
3144
    fatal_error(fmt::format("Invalid bin index: {}", idx));
×
3145
  }
3146
  return ehs_[idx] - ehs_[0];
3147
}
3148

3149
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3150
{
3151
  int bin = get_bin(r);
3152
  *in_mesh = bin != -1;
3153
  return bin;
3154
}
3155

3156
int MOABMesh::get_index_from_bin(int bin) const
3157
{
3158
  return bin;
3159
}
3160

3161
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3162
  Position plot_ll, Position plot_ur) const
3163
{
3164
  // TODO: Implement mesh lines
3165
  return {};
3166
}
3167

3168
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
815,520✔
3169
{
3170
  int idx = vert - verts_[0];
815,520✔
3171
  if (idx >= n_vertices()) {
815,520!
3172
    fatal_error(
3173
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
×
3174
  }
3175
  return idx;
815,520✔
3176
}
3177

3178
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
266,749,203✔
3179
{
3180
  int bin = eh - ehs_[0];
266,749,203✔
3181
  if (bin >= n_bins()) {
266,749,203!
3182
    fatal_error(fmt::format("Invalid bin: {}", bin));
×
3183
  }
3184
  return bin;
266,749,203✔
3185
}
3186

3187
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
572,170✔
3188
{
3189
  if (bin >= n_bins()) {
572,170!
3190
    fatal_error(fmt::format("Invalid bin index: ", bin));
×
3191
  }
3192
  return ehs_[0] + bin;
572,170✔
3193
}
3194

3195
int MOABMesh::n_bins() const
267,525,326✔
3196
{
3197
  return ehs_.size();
267,525,326✔
3198
}
3199

3200
int MOABMesh::n_surface_bins() const
3201
{
3202
  // collect all triangles in the set of tets for this mesh
3203
  moab::Range tris;
×
3204
  moab::ErrorCode rval;
3205
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
×
3206
  if (rval != moab::MB_SUCCESS) {
×
3207
    warning("Failed to get all triangles in the mesh instance");
×
3208
    return -1;
3209
  }
3210
  return 2 * tris.size();
×
3211
}
3212

3213
Position MOABMesh::centroid(int bin) const
3214
{
3215
  moab::ErrorCode rval;
3216

3217
  auto tet = this->get_ent_handle_from_bin(bin);
×
3218

3219
  // look up the tet connectivity
3220
  vector<moab::EntityHandle> conn;
3221
  rval = mbi_->get_connectivity(&tet, 1, conn);
×
3222
  if (rval != moab::MB_SUCCESS) {
×
3223
    warning("Failed to get connectivity of a mesh element.");
×
3224
    return {};
3225
  }
3226

3227
  // get the coordinates
3228
  vector<moab::CartVect> coords(conn.size());
×
3229
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
×
3230
  if (rval != moab::MB_SUCCESS) {
×
3231
    warning("Failed to get the coordinates of a mesh element.");
×
3232
    return {};
3233
  }
3234

3235
  // compute the centroid of the element vertices
3236
  moab::CartVect centroid(0.0, 0.0, 0.0);
3237
  for (const auto& coord : coords) {
×
3238
    centroid += coord;
3239
  }
3240
  centroid /= double(coords.size());
3241

3242
  return {centroid[0], centroid[1], centroid[2]};
3243
}
3244

3245
int MOABMesh::n_vertices() const
845,874✔
3246
{
3247
  return verts_.size();
845,874✔
3248
}
3249

3250
Position MOABMesh::vertex(int id) const
86,227✔
3251
{
3252

3253
  moab::ErrorCode rval;
3254

3255
  moab::EntityHandle vert = verts_[id];
86,227✔
3256

3257
  moab::CartVect coords;
86,227✔
3258
  rval = mbi_->get_coords(&vert, 1, coords.array());
86,227✔
3259
  if (rval != moab::MB_SUCCESS) {
86,227!
3260
    fatal_error("Failed to get the coordinates of a vertex.");
3261
  }
3262

3263
  return {coords[0], coords[1], coords[2]};
172,454✔
3264
}
3265

3266
std::vector<int> MOABMesh::connectivity(int bin) const
203,880✔
3267
{
3268
  moab::ErrorCode rval;
3269

3270
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3271

3272
  // look up the tet connectivity
3273
  vector<moab::EntityHandle> conn;
203,880✔
3274
  rval = mbi_->get_connectivity(&tet, 1, conn);
203,880✔
3275
  if (rval != moab::MB_SUCCESS) {
203,880!
3276
    fatal_error("Failed to get connectivity of a mesh element.");
3277
    return {};
3278
  }
3279

3280
  std::vector<int> verts(4);
203,880✔
3281
  for (int i = 0; i < verts.size(); i++) {
1,019,400✔
3282
    verts[i] = get_vert_idx_from_handle(conn[i]);
815,520✔
3283
  }
3284

3285
  return verts;
203,880✔
3286
}
203,880✔
3287

3288
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3289
  std::string score) const
3290
{
3291
  moab::ErrorCode rval;
3292
  // add a tag to the mesh
3293
  // all scores are treated as a single value
3294
  // with an uncertainty
3295
  moab::Tag value_tag;
3296

3297
  // create the value tag if not present and get handle
3298
  double default_val = 0.0;
3299
  auto val_string = score + "_mean";
×
3300
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3301
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3302
  if (rval != moab::MB_SUCCESS) {
×
3303
    auto msg =
3304
      fmt::format("Could not create or retrieve the value tag for the score {}"
3305
                  " on unstructured mesh {}",
3306
        score, id_);
×
3307
    fatal_error(msg);
3308
  }
3309

3310
  // create the std dev tag if not present and get handle
3311
  moab::Tag error_tag;
3312
  std::string err_string = score + "_std_dev";
×
3313
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
×
3314
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3315
  if (rval != moab::MB_SUCCESS) {
×
3316
    auto msg =
3317
      fmt::format("Could not create or retrieve the error tag for the score {}"
3318
                  " on unstructured mesh {}",
3319
        score, id_);
×
3320
    fatal_error(msg);
3321
  }
3322

3323
  // return the populated tag handles
3324
  return {value_tag, error_tag};
3325
}
3326

3327
void MOABMesh::add_score(const std::string& score)
3328
{
3329
  auto score_tags = get_score_tags(score);
×
3330
  tag_names_.push_back(score);
×
3331
}
3332

3333
void MOABMesh::remove_scores()
3334
{
3335
  for (const auto& name : tag_names_) {
×
3336
    auto value_name = name + "_mean";
×
3337
    moab::Tag tag;
3338
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
×
3339
    if (rval != moab::MB_SUCCESS)
×
3340
      return;
3341

3342
    rval = mbi_->tag_delete(tag);
×
3343
    if (rval != moab::MB_SUCCESS) {
×
3344
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3345
                             " on unstructured mesh {}",
3346
        name, id_);
×
3347
      fatal_error(msg);
3348
    }
3349

3350
    auto std_dev_name = name + "_std_dev";
×
3351
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
×
3352
    if (rval != moab::MB_SUCCESS) {
×
3353
      auto msg =
3354
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3355
                    " on unstructured mesh {}",
3356
          name, id_);
×
3357
    }
3358

3359
    rval = mbi_->tag_delete(tag);
×
3360
    if (rval != moab::MB_SUCCESS) {
×
3361
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3362
                             " on unstructured mesh {}",
3363
        name, id_);
×
3364
      fatal_error(msg);
3365
    }
3366
  }
×
3367
  tag_names_.clear();
3368
}
3369

3370
void MOABMesh::set_score_data(const std::string& score,
3371
  const vector<double>& values, const vector<double>& std_dev)
3372
{
3373
  auto score_tags = this->get_score_tags(score);
×
3374

3375
  moab::ErrorCode rval;
3376
  // set the score value
3377
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
×
3378
  if (rval != moab::MB_SUCCESS) {
×
3379
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3380
                           "on unstructured mesh {}",
3381
      score, id_);
×
3382
    warning(msg);
×
3383
  }
3384

3385
  // set the error value
3386
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
×
3387
  if (rval != moab::MB_SUCCESS) {
×
3388
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3389
                           "on unstructured mesh {}",
3390
      score, id_);
×
3391
    warning(msg);
×
3392
  }
3393
}
3394

3395
void MOABMesh::write(const std::string& base_filename) const
3396
{
3397
  // add extension to the base name
3398
  auto filename = base_filename + ".vtk";
×
3399
  write_message(5, "Writing unstructured mesh {}...", filename);
×
3400
  filename = settings::path_output + filename;
×
3401

3402
  // write the tetrahedral elements of the mesh only
3403
  // to avoid clutter from zero-value data on other
3404
  // elements during visualization
3405
  moab::ErrorCode rval;
3406
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
×
3407
  if (rval != moab::MB_SUCCESS) {
×
3408
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
×
3409
    warning(msg);
×
3410
  }
3411
}
3412

3413
#endif
3414

3415
#ifdef OPENMC_LIBMESH_ENABLED
3416

3417
const std::string LibMesh::mesh_lib_type = "libmesh";
3418

3419
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3420
{
3421
  // filename_ and length_multiplier_ will already be set by the
3422
  // UnstructuredMesh constructor
3423
  set_mesh_pointer_from_filename(filename_);
23✔
3424
  set_length_multiplier(length_multiplier_);
23✔
3425
  initialize();
23✔
3426
}
23✔
3427

3428
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3429
{
3430
  // filename_ and length_multiplier_ will already be set by the
3431
  // UnstructuredMesh constructor
3432
  set_mesh_pointer_from_filename(filename_);
×
3433
  set_length_multiplier(length_multiplier_);
×
3434
  initialize();
×
3435
}
3436

3437
// create the mesh from a pointer to a libMesh Mesh
3438
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3439
{
3440
  if (!input_mesh.is_replicated()) {
×
3441
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3442
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3443
  }
3444

3445
  m_ = &input_mesh;
3446
  set_length_multiplier(length_multiplier);
×
3447
  initialize();
×
3448
}
3449

3450
// create the mesh from an input file
3451
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3452
{
3453
  n_dimension_ = 3;
3454
  set_mesh_pointer_from_filename(filename);
×
3455
  set_length_multiplier(length_multiplier);
×
3456
  initialize();
×
3457
}
3458

3459
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3460
{
3461
  filename_ = filename;
23✔
3462
  unique_m_ =
3463
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3464
  m_ = unique_m_.get();
23✔
3465
  m_->read(filename_);
23✔
3466
}
23✔
3467

3468
// build a libMesh equation system for storing values
3469
void LibMesh::build_eqn_sys()
15✔
3470
{
3471
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
30✔
3472
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3473
  libMesh::ExplicitSystem& eq_sys =
3474
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3475
}
15✔
3476

3477
// intialize from mesh file
3478
void LibMesh::initialize()
23✔
3479
{
3480
  if (!settings::libmesh_comm) {
23!
3481
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3482
                "communicator.");
3483
  }
3484

3485
  // assuming that unstructured meshes used in OpenMC are 3D
3486
  n_dimension_ = 3;
23✔
3487

3488
  if (length_multiplier_ > 0.0) {
23!
3489
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
×
3490
  }
3491
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3492
  // Otherwise assume that it is prepared by its owning application
3493
  if (unique_m_) {
23!
3494
    m_->prepare_for_use();
23✔
3495
  }
3496

3497
  // ensure that the loaded mesh is 3 dimensional
3498
  if (m_->mesh_dimension() != n_dimension_) {
23!
3499
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3500
                            "mesh is not a 3D mesh.",
3501
      filename_));
3502
  }
3503

3504
  for (int i = 0; i < num_threads(); i++) {
69✔
3505
    pl_.emplace_back(m_->sub_point_locator());
46✔
3506
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
3507
    pl_.back()->enable_out_of_mesh_mode();
46✔
3508
  }
3509

3510
  // store first element in the mesh to use as an offset for bin indices
3511
  auto first_elem = *m_->elements_begin();
23✔
3512
  first_element_id_ = first_elem->id();
23✔
3513

3514
  // bounding box for the mesh for quick rejection checks
3515
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23✔
3516
  libMesh::Point ll = bbox_.min();
23✔
3517
  libMesh::Point ur = bbox_.max();
23✔
3518
  lower_left_ = {ll(0), ll(1), ll(2)};
23✔
3519
  upper_right_ = {ur(0), ur(1), ur(2)};
23✔
3520
}
23✔
3521

3522
// Sample position within a tet for LibMesh type tets
3523
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3524
{
3525
  const auto& elem = get_element_from_bin(bin);
400,820✔
3526
  // Get tet vertex coordinates from LibMesh
3527
  std::array<Position, 4> tet_verts;
400,820✔
3528
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3529
    auto node_ref = elem.node_ref(i);
1,603,280✔
3530
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3531
  }
1,603,280✔
3532
  // Samples position within tet using Barycentric coordinates
3533
  return this->sample_tet(tet_verts, seed);
801,640✔
3534
}
3535

3536
Position LibMesh::centroid(int bin) const
3537
{
3538
  const auto& elem = this->get_element_from_bin(bin);
×
3539
  auto centroid = elem.vertex_average();
×
3540
  return {centroid(0), centroid(1), centroid(2)};
3541
}
3542

3543
int LibMesh::n_vertices() const
39,978✔
3544
{
3545
  return m_->n_nodes();
39,978✔
3546
}
3547

3548
Position LibMesh::vertex(int vertex_id) const
39,942✔
3549
{
3550
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
3551
  return {node_ref(0), node_ref(1), node_ref(2)};
79,884✔
3552
}
39,942✔
3553

3554
std::vector<int> LibMesh::connectivity(int elem_id) const
265,856✔
3555
{
3556
  std::vector<int> conn;
265,856✔
3557
  const auto* elem_ptr = m_->elem_ptr(elem_id);
265,856✔
3558
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,337,280✔
3559
    conn.push_back(elem_ptr->node_id(i));
1,071,424✔
3560
  }
3561
  return conn;
265,856✔
3562
}
3563

3564
std::string LibMesh::library() const
33✔
3565
{
3566
  return mesh_lib_type;
33✔
3567
}
3568

3569
int LibMesh::n_bins() const
1,784,287✔
3570
{
3571
  return m_->n_elem();
1,784,287✔
3572
}
3573

3574
int LibMesh::n_surface_bins() const
3575
{
3576
  int n_bins = 0;
3577
  for (int i = 0; i < this->n_bins(); i++) {
×
3578
    const libMesh::Elem& e = get_element_from_bin(i);
3579
    n_bins += e.n_faces();
3580
    // if this is a boundary element, it will only be visited once,
3581
    // the number of surface bins is incremented to
3582
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
3583
      // null neighbor pointer indicates a boundary face
3584
      if (!neighbor_ptr) {
×
3585
        n_bins++;
3586
      }
3587
    }
3588
  }
3589
  return n_bins;
3590
}
3591

3592
void LibMesh::add_score(const std::string& var_name)
15✔
3593
{
3594
  if (!equation_systems_) {
15!
3595
    build_eqn_sys();
15✔
3596
  }
3597

3598
  // check if this is a new variable
3599
  std::string value_name = var_name + "_mean";
15✔
3600
  if (!variable_map_.count(value_name)) {
15!
3601
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3602
    auto var_num =
3603
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3604
    variable_map_[value_name] = var_num;
15✔
3605
  }
3606

3607
  std::string std_dev_name = var_name + "_std_dev";
15✔
3608
  // check if this is a new variable
3609
  if (!variable_map_.count(std_dev_name)) {
15!
3610
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3611
    auto var_num =
3612
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3613
    variable_map_[std_dev_name] = var_num;
15✔
3614
  }
3615
}
15✔
3616

3617
void LibMesh::remove_scores()
15✔
3618
{
3619
  if (equation_systems_) {
15!
3620
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3621
    eqn_sys.clear();
15✔
3622
    variable_map_.clear();
15✔
3623
  }
3624
}
15✔
3625

3626
void LibMesh::set_score_data(const std::string& var_name,
15✔
3627
  const vector<double>& values, const vector<double>& std_dev)
3628
{
3629
  if (!equation_systems_) {
15!
3630
    build_eqn_sys();
×
3631
  }
3632

3633
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3634

3635
  if (!eqn_sys.is_initialized()) {
15!
3636
    equation_systems_->init();
15✔
3637
  }
3638

3639
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3640

3641
  // look up the value variable
3642
  std::string value_name = var_name + "_mean";
15✔
3643
  unsigned int value_num = variable_map_.at(value_name);
15✔
3644
  // look up the std dev variable
3645
  std::string std_dev_name = var_name + "_std_dev";
15✔
3646
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
3647

3648
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
97,871✔
3649
       it++) {
3650
    if (!(*it)->active()) {
97,856!
3651
      continue;
3652
    }
3653

3654
    auto bin = get_bin_from_element(*it);
97,856✔
3655

3656
    // set value
3657
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
3658
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
3659
    assert(value_dof_indices.size() == 1);
3660
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
3661

3662
    // set std dev
3663
    vector<libMesh::dof_id_type> std_dev_dof_indices;
97,856✔
3664
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
97,856✔
3665
    assert(std_dev_dof_indices.size() == 1);
3666
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
97,856✔
3667
  }
97,871✔
3668
}
15✔
3669

3670
void LibMesh::write(const std::string& filename) const
15✔
3671
{
3672
  write_message(fmt::format(
15✔
3673
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
15✔
3674
  libMesh::ExodusII_IO exo(*m_);
15✔
3675
  std::set<std::string> systems_out = {eq_system_name_};
45✔
3676
  exo.write_discontinuous_exodusII(
15✔
3677
    filename + ".e", *equation_systems_, &systems_out);
30✔
3678
}
15✔
3679

3680
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3681
  vector<int>& bins, vector<double>& lengths) const
3682
{
3683
  // TODO: Implement triangle crossings here
3684
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3685
}
3686

3687
int LibMesh::get_bin(Position r) const
2,340,484✔
3688
{
3689
  // look-up a tet using the point locator
3690
  libMesh::Point p(r.x, r.y, r.z);
2,340,484✔
3691

3692
  // quick rejection check
3693
  if (!bbox_.contains_point(p)) {
2,340,484✔
3694
    return -1;
918,796✔
3695
  }
3696

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

3699
  const auto elem_ptr = (*point_locator)(p);
1,421,688✔
3700
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,688✔
3701
}
2,340,484✔
3702

3703
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,518,314✔
3704
{
3705
  int bin = elem->id() - first_element_id_;
1,518,314✔
3706
  if (bin >= n_bins() || bin < 0) {
1,518,314!
3707
    fatal_error(fmt::format("Invalid bin: {}", bin));
3708
  }
3709
  return bin;
1,518,314✔
3710
}
3711

3712
std::pair<vector<double>, vector<double>> LibMesh::plot(
3713
  Position plot_ll, Position plot_ur) const
3714
{
3715
  return {};
3716
}
3717

3718
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
3719
{
3720
  return m_->elem_ref(bin);
765,460✔
3721
}
3722

3723
double LibMesh::volume(int bin) const
364,640✔
3724
{
3725
  return this->get_element_from_bin(bin).volume();
364,640✔
3726
}
3727

3728
AdaptiveLibMesh::AdaptiveLibMesh(
3729
  libMesh::MeshBase& input_mesh, double length_multiplier)
3730
  : LibMesh(input_mesh, length_multiplier), num_active_(m_->n_active_elem())
×
3731
{
3732
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
3733
  // contiguous in ID space, so we need to map from bin indices (defined over
3734
  // active elements) to global dof ids
3735
  bin_to_elem_map_.reserve(num_active_);
×
3736
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
3737
  for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
×
3738
       it++) {
3739
    auto elem = *it;
×
3740

3741
    bin_to_elem_map_.push_back(elem->id());
×
3742
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
3743
  }
3744
}
3745

3746
int AdaptiveLibMesh::n_bins() const
3747
{
3748
  return num_active_;
3749
}
3750

3751
void AdaptiveLibMesh::add_score(const std::string& var_name)
3752
{
3753
  warning(fmt::format(
×
3754
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3755
    this->id_));
3756
}
3757

3758
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
3759
  const vector<double>& values, const vector<double>& std_dev)
3760
{
3761
  warning(fmt::format(
×
3762
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3763
    this->id_));
3764
}
3765

3766
void AdaptiveLibMesh::write(const std::string& filename) const
3767
{
3768
  warning(fmt::format(
×
3769
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3770
    this->id_));
3771
}
3772

3773
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3774
{
3775
  int bin = elem_to_bin_map_[elem->id()];
×
3776
  if (bin >= n_bins() || bin < 0) {
×
3777
    fatal_error(fmt::format("Invalid bin: {}", bin));
3778
  }
3779
  return bin;
3780
}
3781

3782
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
3783
{
3784
  return m_->elem_ref(bin_to_elem_map_.at(bin));
3785
}
3786

3787
#endif // OPENMC_LIBMESH_ENABLED
3788

3789
//==============================================================================
3790
// Non-member functions
3791
//==============================================================================
3792

3793
void read_meshes(pugi::xml_node root)
11,916✔
3794
{
3795
  std::unordered_set<int> mesh_ids;
11,916✔
3796

3797
  for (auto node : root.children("mesh")) {
14,807✔
3798
    // Check to make sure multiple meshes in the same file don't share IDs
3799
    int id = std::stoi(get_node_value(node, "id"));
2,891✔
3800
    if (contains(mesh_ids, id)) {
2,891!
UNCOV
3801
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
3802
                              "'{}' in the same input file",
3803
        id));
3804
    }
3805
    mesh_ids.insert(id);
2,891✔
3806

3807
    // If we've already read a mesh with the same ID in a *different* file,
3808
    // assume it is the same here
3809
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
2,891!
3810
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
UNCOV
3811
      continue;
×
3812
    }
3813

3814
    std::string mesh_type;
2,891✔
3815
    if (check_for_node(node, "type")) {
2,891✔
3816
      mesh_type = get_node_value(node, "type", true, true);
956✔
3817
    } else {
3818
      mesh_type = "regular";
1,935✔
3819
    }
3820

3821
    // determine the mesh library to use
3822
    std::string mesh_lib;
2,891✔
3823
    if (check_for_node(node, "library")) {
2,891✔
3824
      mesh_lib = get_node_value(node, "library", true, true);
47!
3825
    }
3826

3827
    Mesh::create(node, mesh_type, mesh_lib);
2,891✔
3828
  }
2,891✔
3829
}
11,916✔
3830

3831
void read_meshes(hid_t group)
22✔
3832
{
3833
  std::unordered_set<int> mesh_ids;
22✔
3834

3835
  std::vector<int> ids;
22✔
3836
  read_attribute(group, "ids", ids);
22✔
3837

3838
  for (auto id : ids) {
55✔
3839

3840
    // Check to make sure multiple meshes in the same file don't share IDs
3841
    if (contains(mesh_ids, id)) {
33!
UNCOV
3842
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
3843
                              "'{}' in the same HDF5 input file",
3844
        id));
3845
    }
3846
    mesh_ids.insert(id);
33✔
3847

3848
    // If we've already read a mesh with the same ID in a *different* file,
3849
    // assume it is the same here
3850
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
33!
3851
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
33✔
3852
      continue;
33✔
3853
    }
3854

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

3858
    std::string mesh_type;
×
3859
    if (object_exists(mesh_group, "type")) {
×
UNCOV
3860
      read_dataset(mesh_group, "type", mesh_type);
×
3861
    } else {
UNCOV
3862
      mesh_type = "regular";
×
3863
    }
3864

3865
    // determine the mesh library to use
3866
    std::string mesh_lib;
×
3867
    if (object_exists(mesh_group, "library")) {
×
UNCOV
3868
      read_dataset(mesh_group, "library", mesh_lib);
×
3869
    }
3870

3871
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
UNCOV
3872
  }
×
3873
}
22✔
3874

3875
void meshes_to_hdf5(hid_t group)
6,665✔
3876
{
3877
  // Write number of meshes
3878
  hid_t meshes_group = create_group(group, "meshes");
6,665✔
3879
  int32_t n_meshes = model::meshes.size();
6,665✔
3880
  write_attribute(meshes_group, "n_meshes", n_meshes);
6,665✔
3881

3882
  if (n_meshes > 0) {
6,665✔
3883
    // Write IDs of meshes
3884
    vector<int> ids;
2,071✔
3885
    for (const auto& m : model::meshes) {
4,721✔
3886
      m->to_hdf5(meshes_group);
2,650✔
3887
      ids.push_back(m->id_);
2,650✔
3888
    }
3889
    write_attribute(meshes_group, "ids", ids);
2,071✔
3890
  }
2,071✔
3891

3892
  close_group(meshes_group);
6,665✔
3893
}
6,665✔
3894

3895
void free_memory_mesh()
7,842✔
3896
{
3897
  model::meshes.clear();
7,842✔
3898
  model::mesh_map.clear();
7,842✔
3899
}
7,842✔
3900

3901
extern "C" int n_meshes()
308✔
3902
{
3903
  return model::meshes.size();
308✔
3904
}
3905

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