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

openmc-dev / openmc / 20143336716

11 Dec 2025 06:28PM UTC coverage: 82.149% (-0.02%) from 82.166%
20143336716

Pull #3680

github

web-flow
Merge c5699be7a into a62e754bb
Pull Request #3680: Add a command-line argument for output verbosity

17011 of 23571 branches covered (72.17%)

Branch coverage included in aggregate %.

7 of 14 new or added lines in 2 files covered. (50.0%)

1 existing line in 1 file now uncovered.

55091 of 64199 relevant lines covered (85.81%)

43488434.42 hits per line

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

72.07
/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
#endif
55

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

60
namespace openmc {
61

62
//==============================================================================
63
// Global variables
64
//==============================================================================
65

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

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

76
namespace model {
77

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

81
} // namespace model
82

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

90
//==============================================================================
91
// Helper functions
92
//==============================================================================
93

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

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

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

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

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

142
namespace detail {
143

144
//==============================================================================
145
// MaterialVolumes implementation
146
//==============================================================================
147

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

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

165
    // Non-atomic read of current material
166
    int32_t current_val = *slot_ptr;
2,807,089✔
167

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

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

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

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

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

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

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

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

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

227
} // namespace detail
228

229
//==============================================================================
230
// Mesh implementation
231
//==============================================================================
232

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

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

266
  return model::meshes.back();
2,843✔
267
}
268

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

360
  // Determine effective width of rays
361
  Position width((nx > 0) ? (bbox.xmax - bbox.xmin) / nx : 0.0,
319✔
362
    (ny > 0) ? (bbox.ymax - bbox.ymin) / ny : 0.0,
341✔
363
    (nz > 0) ? (bbox.zmax - bbox.zmin) / nz : 0.0);
176✔
364

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

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

375
    SourceSite site;
80✔
376
    site.E = 1.0;
80✔
377
    site.particle = ParticleType::neutron;
80✔
378

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

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

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

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

414
          p.from_source(&site);
562,735✔
415

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

583
  // Write mesh type
584
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,795✔
585

586
  // Write mesh ID
587
  write_attribute(mesh_group, "id", id_);
2,795✔
588

589
  // Write mesh name
590
  write_dataset(mesh_group, "name", name_);
2,795✔
591

592
  // Write mesh data
593
  this->to_hdf5_inner(mesh_group);
2,795✔
594

595
  // Close group
596
  close_group(mesh_group);
2,795✔
597
}
2,795✔
598

599
//==============================================================================
600
// Structured Mesh implementation
601
//==============================================================================
602

603
std::string StructuredMesh::bin_label(int bin) const
5,127,863✔
604
{
605
  MeshIndex ijk = get_indices_from_bin(bin);
5,127,863✔
606

607
  if (n_dimension_ > 2) {
5,127,863✔
608
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,226,224✔
609
  } else if (n_dimension_ > 1) {
14,751✔
610
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
28,952✔
611
  } else {
612
    return fmt::format("Mesh Index ({})", ijk[0]);
550✔
613
  }
614
}
615

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

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

630
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,533,623!
631
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,533,623!
632

633
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,533,623!
634
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,533,623!
635

636
  return {x_min + (x_max - x_min) * prn(seed),
1,533,623✔
637
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,533,623✔
638
}
639

640
//==============================================================================
641
// Unstructured Mesh implementation
642
//==============================================================================
643

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

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

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

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

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

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

683
UnstructuredMesh::UnstructuredMesh(hid_t group) : Mesh(group)
×
684
{
685
  n_dimension_ = 3;
×
686

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

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

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

712
  if (attribute_exists(group, "options")) {
×
713
    read_attribute(group, "options", options_);
×
714
  }
715

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

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

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

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

775
const std::string UnstructuredMesh::mesh_type = "unstructured";
776

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

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

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

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

801
  if (length_multiplier_ > 0.0)
32!
802
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
803

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

812
  int num_elem_skipped = 0;
32✔
813

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

821
    volumes.emplace_back(this->volume(i));
349,736!
822

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

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

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

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

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

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

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

881
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
882
      in_mesh = false;
102,400,563✔
883
  }
884
  return ijk;
1,140,693,705✔
885
}
886

887
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,688,165,419✔
888
{
889
  switch (n_dimension_) {
1,688,165,419!
890
  case 1:
880,605✔
891
    return ijk[0] - 1;
880,605✔
892
  case 2:
96,174,804✔
893
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
96,174,804✔
894
  case 3:
1,591,110,010✔
895
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,591,110,010✔
896
  default:
×
897
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
898
  }
899
}
900

901
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
7,877,661✔
902
{
903
  MeshIndex ijk;
904
  if (n_dimension_ == 1) {
7,877,661✔
905
    ijk[0] = bin + 1;
275✔
906
  } else if (n_dimension_ == 2) {
7,877,386✔
907
    ijk[0] = bin % shape_[0] + 1;
14,476✔
908
    ijk[1] = bin / shape_[0] + 1;
14,476✔
909
  } else if (n_dimension_ == 3) {
7,862,910!
910
    ijk[0] = bin % shape_[0] + 1;
7,862,910✔
911
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
7,862,910✔
912
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
7,862,910✔
913
  }
914
  return ijk;
7,877,661✔
915
}
916

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

925
  // Convert indices to bin
926
  return get_bin_from_indices(ijk);
226,907,966✔
927
}
928

929
int StructuredMesh::n_bins() const
1,139,375✔
930
{
931
  return std::accumulate(
1,139,375✔
932
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,278,750✔
933
}
934

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

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

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

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

954
    // determine scoring bin for entropy mesh
955
    int mesh_bin = get_bin(site.r);
×
956

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

963
    // Add to appropriate bin
964
    cnt(mesh_bin) += site.wgt;
×
965
  }
966

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

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

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

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

992
  return counts;
×
993
}
×
994

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

1008
  // Compute the length of the entire track.
1009
  double total_distance = (r1 - r0).norm();
897,770,479✔
1010
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
897,770,479✔
1011
    return;
11,632,190✔
1012

1013
  // keep a copy of the original global position to pass to get_indices,
1014
  // which performs its own transformation to local coordinates
1015
  Position global_r = r0;
886,138,289✔
1016
  Position local_r = local_coords(r0);
886,138,289✔
1017

1018
  const int n = n_dimension_;
886,138,289✔
1019

1020
  // Flag if position is inside the mesh
1021
  bool in_mesh;
1022

1023
  // Position is r = r0 + u * traveled_distance, start at r0
1024
  double traveled_distance {0.0};
886,138,289✔
1025

1026
  // Calculate index of current cell. Offset the position a tiny bit in
1027
  // direction of flight
1028
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
886,138,289✔
1029

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

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

1045
  // Loop until r = r1 is eventually reached
1046
  while (true) {
742,877,867✔
1047

1048
    if (in_mesh) {
1,628,684,629✔
1049

1050
      // find surface with minimal distance to current position
1051
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,542,811,785✔
1052
                     distances.begin();
1,542,811,785✔
1053

1054
      // Tally track length delta since last step
1055
      tally.track(ijk,
1,542,811,785✔
1056
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,542,811,785✔
1057
          total_distance);
1058

1059
      // update position and leave, if we have reached end position
1060
      traveled_distance = distances[k].distance;
1,542,811,785✔
1061
      if (traveled_distance >= total_distance)
1,542,811,785✔
1062
        return;
806,576,488✔
1063

1064
      // If we have not reached r1, we have hit a surface. Tally outward
1065
      // current
1066
      tally.surface(ijk, k, distances[k].max_surface, false);
736,235,297✔
1067

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

1074
      // Check if we have left the interior of the mesh
1075
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
736,235,297✔
1076

1077
      // If we are still inside the mesh, tally inward current for the next
1078
      // cell
1079
      if (in_mesh)
736,235,297✔
1080
        tally.surface(ijk, k, !distances[k].max_surface, true);
723,058,470✔
1081

1082
    } else { // not inside mesh
1083

1084
      // For all directions outside the mesh, find the distance that we need
1085
      // to travel to reach the next surface. Use the largest distance, as
1086
      // only this will cross all outer surfaces.
1087
      int k_max {-1};
85,872,844✔
1088
      for (int k = 0; k < n; ++k) {
342,047,010✔
1089
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
350,038,227✔
1090
            (distances[k].distance > traveled_distance)) {
93,864,061✔
1091
          traveled_distance = distances[k].distance;
88,885,949✔
1092
          k_max = k;
88,885,949✔
1093
        }
1094
      }
1095
      // Assure some distance is traveled
1096
      if (k_max == -1) {
85,872,844✔
1097
        traveled_distance += TINY_BIT;
110✔
1098
      }
1099

1100
      // If r1 is not inside the mesh, exit here
1101
      if (traveled_distance >= total_distance)
85,872,844✔
1102
        return;
79,230,274✔
1103

1104
      // Calculate the new cell index and update all distances to next
1105
      // surfaces.
1106
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,642,570✔
1107
      for (int k = 0; k < n; ++k) {
26,361,742✔
1108
        distances[k] =
19,719,172✔
1109
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
19,719,172✔
1110
      }
1111

1112
      // If inside the mesh, Tally inward current
1113
      if (in_mesh && k_max >= 0)
6,642,570!
1114
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,223,255✔
1115
    }
1116
  }
1117
}
1118

1119
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
785,642,914✔
1120
  vector<int>& bins, vector<double>& lengths) const
1121
{
1122

1123
  // Helper tally class.
1124
  // stores a pointer to the mesh class and references to bins and lengths
1125
  // parameters. Performs the actual tally through the track method.
1126
  struct TrackAggregator {
1127
    TrackAggregator(
785,642,914✔
1128
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1129
      : mesh(_mesh), bins(_bins), lengths(_lengths)
785,642,914✔
1130
    {}
785,642,914✔
1131
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1,407,357,833✔
1132
    void track(const MeshIndex& ijk, double l) const
1,403,098,264✔
1133
    {
1134
      bins.push_back(mesh->get_bin_from_indices(ijk));
1,403,098,264✔
1135
      lengths.push_back(l);
1,403,098,264✔
1136
    }
1,403,098,264✔
1137

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

1143
  // Perform the mesh raytrace with the helper class.
1144
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
785,642,914✔
1145
}
785,642,914✔
1146

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

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

1170
    const StructuredMesh* mesh;
1171
    vector<int>& bins;
1172
  };
1173

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

1178
//==============================================================================
1179
// RegularMesh implementation
1180
//==============================================================================
1181

1182
int RegularMesh::set_grid()
1,979✔
1183
{
1184
  auto shape = xt::adapt(shape_, {n_dimension_});
1,979✔
1185

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

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

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

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

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

1217
  } else if (upper_right_.size() > 0) {
1,930!
1218

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

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

1234
    // Set width
1235
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1,930✔
1236
  }
1237

1238
  // Set material volumes
1239
  volume_frac_ = 1.0 / xt::prod(shape)();
1,979✔
1240

1241
  element_volume_ = 1.0;
1,979✔
1242
  for (int i = 0; i < n_dimension_; i++) {
7,478✔
1243
    element_volume_ *= width_[i];
5,499✔
1244
  }
1245
  return 0;
1,979✔
1246
}
1,979✔
1247

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

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

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

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

1276
    width_ = get_node_xarray<double>(node, "width");
49✔
1277

1278
  } else if (check_for_node(node, "upper_right")) {
1,919!
1279

1280
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1,919✔
1281

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

1286
  if (int err = set_grid()) {
1,968!
1287
    fatal_error(openmc_err_msg);
×
1288
  }
1289
}
1,968✔
1290

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

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

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

1314
  if (object_exists(group, "upper_right")) {
11!
1315

1316
    read_dataset(group, "upper_right", upper_right_);
11✔
1317

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

1322
  if (int err = set_grid()) {
11!
1323
    fatal_error(openmc_err_msg);
×
1324
  }
1325
}
11✔
1326

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

1332
const std::string RegularMesh::mesh_type = "regular";
1333

1334
std::string RegularMesh::get_mesh_type() const
3,071✔
1335
{
1336
  return mesh_type;
3,071✔
1337
}
1338

1339
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1,433,144,668✔
1340
{
1341
  return lower_left_[i] + ijk[i] * width_[i];
1,433,144,668✔
1342
}
1343

1344
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,371,412,516✔
1345
{
1346
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,371,412,516✔
1347
}
1348

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

1358
  d.max_surface = (u[i] > 0);
2,147,483,647✔
1359
  if (d.max_surface && (ijk[i] <= shape_[i])) {
2,147,483,647✔
1360
    d.next_index++;
1,428,543,799✔
1361
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,428,543,799✔
1362
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1,387,993,396✔
1363
    d.next_index--;
1,366,811,647✔
1364
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,366,811,647✔
1365
  }
1366

1367
  return d;
2,147,483,647✔
1368
}
1369

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

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

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

1408
  return {axis_lines[0], axis_lines[1]};
44✔
1409
}
22✔
1410

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

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

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

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

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

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

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

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

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

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

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

1471
  return counts;
15,678✔
1472
}
7,839✔
1473

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

1479
//==============================================================================
1480
// RectilinearMesh implementation
1481
//==============================================================================
1482

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

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

1491
  if (int err = set_grid()) {
125!
1492
    fatal_error(openmc_err_msg);
×
1493
  }
1494
}
125✔
1495

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

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

1504
  if (int err = set_grid()) {
11!
1505
    fatal_error(openmc_err_msg);
×
1506
  }
1507
}
11✔
1508

1509
const std::string RectilinearMesh::mesh_type = "rectilinear";
1510

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

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

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

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

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

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

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

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

1571
  return 0;
180✔
1572
}
1573

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

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

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

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

1606
  return {axis_lines[0], axis_lines[1]};
22✔
1607
}
11✔
1608

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

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

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

1626
//==============================================================================
1627
// CylindricalMesh implementation
1628
//==============================================================================
1629

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

1639
  if (int err = set_grid()) {
401!
1640
    fatal_error(openmc_err_msg);
×
1641
  }
1642
}
401✔
1643

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

1652
  if (int err = set_grid()) {
11!
1653
    fatal_error(openmc_err_msg);
×
1654
  }
1655
}
11✔
1656

1657
const std::string CylindricalMesh::mesh_type = "cylindrical";
1658

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

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

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

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

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

1683
  idx[1] = sanitize_phi(idx[1]);
47,726,668✔
1684

1685
  return idx;
47,726,668✔
1686
}
1687

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

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

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

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

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

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

1712
double CylindricalMesh::find_r_crossing(
142,566,864✔
1713
  const Position& r, const Direction& u, double l, int shell) const
1714
{
1715

1716
  if ((shell < 0) || (shell > shape_[0]))
142,566,864!
1717
    return INFTY;
17,913,852✔
1718

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

1723
  const double r0 = grid_[0][shell];
124,653,012✔
1724
  if (r0 == 0.0)
124,653,012✔
1725
    return INFTY;
7,130,651✔
1726

1727
  const double denominator = u.x * u.x + u.y * u.y;
117,522,361✔
1728

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

1733
  // inverse of dominator to help the compiler to speed things up
1734
  const double inv_denominator = 1.0 / denominator;
117,463,401✔
1735

1736
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
117,463,401✔
1737
  double c = r.x * r.x + r.y * r.y - r0 * r0;
117,463,401✔
1738
  double D = p * p - c * inv_denominator;
117,463,401✔
1739

1740
  if (D < 0.0)
117,463,401✔
1741
    return INFTY;
9,733,570✔
1742

1743
  D = std::sqrt(D);
107,729,831✔
1744

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

1749
  if (-p - D > l)
101,118,457✔
1750
    return -p - D;
20,205,570✔
1751
  if (-p + D > l)
80,912,887✔
1752
    return -p + D;
50,089,809✔
1753

1754
  return INFTY;
30,823,078✔
1755
}
1756

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

1764
  shell = sanitize_phi(shell);
43,970,718✔
1765

1766
  const double p0 = grid_[1][shell];
43,970,718✔
1767

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

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

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

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

1787
  return INFTY;
23,750,859✔
1788
}
1789

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

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

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

1811
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
145,196,535✔
1812
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1813
  double l) const
1814
{
1815
  if (i == 0) {
145,196,535✔
1816

1817
    return std::min(
71,283,432✔
1818
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
71,283,432✔
1819
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
142,566,864✔
1820

1821
  } else if (i == 1) {
73,913,103✔
1822

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

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

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

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

1866
    return OPENMC_E_INVALID_ARGUMENT;
×
1867
  }
1868

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

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

1876
  return 0;
434✔
1877
}
1878

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

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

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

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

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

1907
  double phi_i = grid_[1][ijk[1] - 1];
792✔
1908
  double phi_o = grid_[1][ijk[1]];
792✔
1909

1910
  double z_i = grid_[2][ijk[2] - 1];
792✔
1911
  double z_o = grid_[2][ijk[2]];
792✔
1912

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

1916
//==============================================================================
1917
// SphericalMesh implementation
1918
//==============================================================================
1919

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

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

1930
  if (int err = set_grid()) {
346!
1931
    fatal_error(openmc_err_msg);
×
1932
  }
1933
}
346✔
1934

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

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

1944
  if (int err = set_grid()) {
11!
1945
    fatal_error(openmc_err_msg);
×
1946
  }
1947
}
11✔
1948

1949
const std::string SphericalMesh::mesh_type = "spherical";
1950

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

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

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

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

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

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

1979
  return idx;
68,175,250✔
1980
}
1981

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

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

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

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

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

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

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

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

2025
  if (std::abs(c) <= RADIAL_MESH_TOL)
396,204,732✔
2026
    return INFTY;
10,598,654✔
2027

2028
  if (D >= 0.0) {
385,606,078✔
2029
    D = std::sqrt(D);
357,729,130✔
2030
    // the solution -p - D is always smaller as -p + D : Check this one first
2031
    if (-p - D > l)
357,729,130✔
2032
      return -p - D;
64,280,876✔
2033
    if (-p + D > l)
293,448,254✔
2034
      return -p + D;
176,905,300✔
2035
  }
2036

2037
  return INFTY;
144,419,902✔
2038
}
2039

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

2047
  shell = sanitize_theta(shell);
38,358,540✔
2048

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

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

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

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

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

2078
    // no crossing is possible
2079
    return INFTY;
×
2080
  }
2081

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

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

2088
  D = std::sqrt(D);
26,921,004✔
2089

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

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

2101
  return INFTY;
11,475,101✔
2102
}
2103

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

2111
  shell = sanitize_phi(shell);
39,948,018✔
2112

2113
  const double p0 = grid_[2][shell];
39,948,018✔
2114

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

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

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

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

2134
  return INFTY;
22,368,566✔
2135
}
2136

2137
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
331,665,675✔
2138
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2139
  double l) const
2140
{
2141

2142
  if (i == 0) {
331,665,675✔
2143
    return std::min(
221,543,344✔
2144
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
221,543,344✔
2145
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
443,086,688✔
2146

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

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

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

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

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

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

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

2204
  return 0;
379✔
2205
}
2206

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

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

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

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

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

2235
  double theta_i = grid_[1][ijk[1] - 1];
935✔
2236
  double theta_o = grid_[1][ijk[1]];
935✔
2237

2238
  double phi_i = grid_[2][ijk[2] - 1];
935✔
2239
  double phi_o = grid_[2][ijk[2]];
935✔
2240

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

2245
//==============================================================================
2246
// Helper functions for the C API
2247
//==============================================================================
2248

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

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

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

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

2279
//==============================================================================
2280
// C API functions
2281
//==============================================================================
2282

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

2289
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
1,452✔
2290

2291
  return 0;
1,452✔
2292
}
2293

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

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

2318
  return 0;
253✔
2319
}
253✔
2320

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

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

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

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

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

2354
  return 0;
×
2355
}
×
2356

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

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

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

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

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

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

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

2416
  // set lower left corner values
2417
  ll[0] = bbox.xmin;
154✔
2418
  ll[1] = bbox.ymin;
154✔
2419
  ll[2] = bbox.zmin;
154✔
2420

2421
  // set upper right corner values
2422
  ur[0] = bbox.xmax;
154✔
2423
  ur[1] = bbox.ymax;
154✔
2424
  ur[2] = bbox.zmax;
154✔
2425
  return 0;
154✔
2426
}
2427

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

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

2446
  return 0;
165✔
2447
}
2448

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

2456
  int pixel_width = pixels[0];
44✔
2457
  int pixel_height = pixels[1];
44✔
2458

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

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

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

2489
#pragma omp parallel
24✔
2490
  {
2491
    Position r = xyz;
20✔
2492

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

2503
  return 0;
44✔
2504
}
2505

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

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

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

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

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

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

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

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

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

2583
  // Set material volumes
2584

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

2593
  return 0;
220✔
2594
}
220✔
2595

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

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

2607
  m->n_dimension_ = 3;
88✔
2608

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

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

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

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

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

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

2648
  return 0;
385✔
2649
}
2650

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

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

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

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

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

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

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

2704
#ifdef OPENMC_DAGMC_ENABLED
2705

2706
const std::string MOABMesh::mesh_lib_type = "moab";
2707

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

2713
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
×
2714
{
2715
  initialize();
×
2716
}
2717

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

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

2734
void MOABMesh::initialize()
25✔
2735
{
2736

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

2740
  // Initialise MOAB error code
2741
  moab::ErrorCode rval = moab::MB_SUCCESS;
25✔
2742

2743
  // Set the dimension
2744
  n_dimension_ = 3;
25✔
2745

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2917
  start -= TINY_BIT * dir;
1,543,584✔
2918
  end += TINY_BIT * dir;
1,543,584✔
2919

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

2923
  bins.clear();
1,543,584✔
2924
  lengths.clear();
1,543,584✔
2925

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

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

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

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

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

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

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

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

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

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

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

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

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

3021
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
200,410✔
3022

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

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

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

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

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

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

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

3077
  baryc_data_.clear();
21✔
3078
  baryc_data_.resize(tets.size());
21✔
3079

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

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

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

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

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

3107
  moab::ErrorCode rval;
3108

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

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

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

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

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

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

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

3154
int MOABMesh::get_index_from_bin(int bin) const
3155
{
3156
  return bin;
3157
}
3158

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

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

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

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

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

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

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

3215
  auto tet = this->get_ent_handle_from_bin(bin);
×
3216

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

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

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

3240
  return {centroid[0], centroid[1], centroid[2]};
3241
}
3242

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

3248
Position MOABMesh::vertex(int id) const
86,227✔
3249
{
3250

3251
  moab::ErrorCode rval;
3252

3253
  moab::EntityHandle vert = verts_[id];
86,227✔
3254

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

3261
  return {coords[0], coords[1], coords[2]};
172,454✔
3262
}
3263

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

3268
  auto tet = get_ent_handle_from_bin(bin);
203,880✔
3269

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

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

3283
  return verts;
203,880✔
3284
}
203,880✔
3285

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

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

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

3321
  // return the populated tag handles
3322
  return {value_tag, error_tag};
3323
}
3324

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

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

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

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

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

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

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

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

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

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

3411
#endif
3412

3413
#ifdef OPENMC_LIBMESH_ENABLED
3414

3415
const std::string LibMesh::mesh_lib_type = "libmesh";
3416

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

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

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

3443
  m_ = &input_mesh;
3444
  set_length_multiplier(length_multiplier);
×
3445
  initialize();
×
3446
}
3447

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3631
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3632

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

3637
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3638

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

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

3652
    auto bin = get_bin_from_element(*it);
97,856✔
3653

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3744
int AdaptiveLibMesh::n_bins() const
3745
{
3746
  return num_active_;
3747
}
3748

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

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

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

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

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

3785
#endif // OPENMC_LIBMESH_ENABLED
3786

3787
//==============================================================================
3788
// Non-member functions
3789
//==============================================================================
3790

3791
void read_meshes(pugi::xml_node root)
11,749✔
3792
{
3793
  std::unordered_set<int> mesh_ids;
11,749✔
3794

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

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

3812
    std::string mesh_type;
2,843✔
3813
    if (check_for_node(node, "type")) {
2,843✔
3814
      mesh_type = get_node_value(node, "type", true, true);
956✔
3815
    } else {
3816
      mesh_type = "regular";
1,887✔
3817
    }
3818

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

3825
    Mesh::create(node, mesh_type, mesh_lib);
2,843✔
3826
  }
2,843✔
3827
}
11,749✔
3828

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

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

3836
  for (auto id : ids) {
55✔
3837

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

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

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

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

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

3869
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
3870
  }
×
3871
}
22✔
3872

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

3880
  if (n_meshes > 0) {
6,555✔
3881
    // Write IDs of meshes
3882
    vector<int> ids;
2,049✔
3883
    for (const auto& m : model::meshes) {
4,666✔
3884
      m->to_hdf5(meshes_group);
2,617✔
3885
      ids.push_back(m->id_);
2,617✔
3886
    }
3887
    write_attribute(meshes_group, "ids", ids);
2,049✔
3888
  }
2,049✔
3889

3890
  close_group(meshes_group);
6,555✔
3891
}
6,555✔
3892

3893
void free_memory_mesh()
7,707✔
3894
{
3895
  model::meshes.clear();
7,707✔
3896
  model::mesh_map.clear();
7,707✔
3897
}
7,707✔
3898

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

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

© 2025 Coveralls, Inc