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

openmc-dev / openmc / 21043561037

15 Jan 2026 07:23PM UTC coverage: 81.388% (-0.7%) from 82.044%
21043561037

Pull #3734

github

web-flow
Merge 5e79b7b6f into 179048b80
Pull Request #3734: Specify temperature from a field (structured mesh only)

16703 of 22995 branches covered (72.64%)

Branch coverage included in aggregate %.

156 of 179 new or added lines in 12 files covered. (87.15%)

843 existing lines in 36 files now uncovered.

54487 of 64475 relevant lines covered (84.51%)

27592585.27 hits per line

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

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

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

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

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

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

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

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

61
namespace openmc {
62

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

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

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

77
namespace model {
78

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

82
} // namespace model
83

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

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

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

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

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

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

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

143
namespace detail {
144

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

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

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

166
    // Non-atomic read of current material
167
    int32_t current_val = *slot_ptr;
1,273,365✔
168

169
    // Found the desired material; accumulate volume
170
    if (current_val == index_material) {
1,273,365✔
171
#pragma omp atomic
509,258✔
172
      this->volumes(index_elem, slot) += volume;
1,272,720✔
173
      return;
1,272,720✔
174
    }
175

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

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

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

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

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

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

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

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

228
} // namespace detail
229

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

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

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

267
  return model::meshes.back();
1,314✔
268
}
269

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

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

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

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

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

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

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

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

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

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

325
vector<double> Mesh::volumes() const
112✔
326
{
327
  vector<double> volumes(n_bins());
112✔
328
  for (int i = 0; i < n_bins(); i++) {
507,561✔
329
    volumes[i] = this->volume(i);
507,449✔
330
  }
331
  return volumes;
112✔
332
}
×
333

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

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

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

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

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

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

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

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

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

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

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

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

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

416
          p.from_source(&site);
337,641✔
417

418
          // Determine particle's location
419
          if (!exhaustive_find_cell(p)) {
337,641✔
420
            out_of_model = true;
23,958✔
421
            continue;
23,958✔
422
          }
423

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

428
          // Initialize last cells from current cell
429
          for (int j = 0; j < p.n_coord(); ++j) {
627,366✔
430
            p.cell_last(j) = p.coord(j).cell();
313,683✔
431
          }
432
          p.n_coord_last() = p.n_coord();
313,683✔
433

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

439
            // Find the distance to the nearest boundary
440
            BoundaryInfo boundary = distance_to_boundary(p);
632,829✔
441

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

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

451
            // Add volumes to any mesh elements that were crossed
452
            int i_material = p.material();
632,829✔
453
            if (i_material != C_NONE) {
632,829✔
454
              i_material = model::materials[i_material]->id();
569,415✔
455
            }
456
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
1,396,848✔
457
              int mesh_index = bins[i_bin];
764,019✔
458
              double length = distance * length_fractions[i_bin];
764,019✔
459

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

464
            if (distance == max_distance)
632,829✔
465
              break;
313,683✔
466

467
            // cross next geometric surface
468
            for (int j = 0; j < p.n_coord(); ++j) {
638,292✔
469
              p.cell_last(j) = p.coord(j).cell();
319,146✔
470
            }
471
            p.n_coord_last() = p.n_coord();
319,146✔
472

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

597
  // Close group
598
  close_group(mesh_group);
1,298✔
599
}
1,298✔
600

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

605
std::string StructuredMesh::bin_label(int bin) const
2,333,785✔
606
{
607
  MeshIndex ijk = get_indices_from_bin(bin);
2,333,785✔
608

609
  if (n_dimension_ > 2) {
2,333,785✔
610
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
4,653,120✔
611
  } else if (n_dimension_ > 1) {
7,225✔
612
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
14,200✔
613
  } else {
614
    return fmt::format("Mesh Index ({})", ijk[0]);
250✔
615
  }
616
}
617

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

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

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

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

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

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

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

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

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

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

674
  if (check_for_node(node, "options")) {
23!
UNCOV
675
    options_ = get_node_value(node, "options");
×
676
  }
677

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

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

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

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

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

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

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

NEW
725
double UnstructuredMesh::distance_to_next_boundary(Position r, Direction u) const
×
726
{
NEW
727
  fatal_error("Not implemented");
×
728
  return -1.0;
729
}
730

UNCOV
731
void UnstructuredMesh::determine_bounds()
×
732
{
UNCOV
733
  double xmin = INFTY;
×
UNCOV
734
  double ymin = INFTY;
×
UNCOV
735
  double zmin = INFTY;
×
UNCOV
736
  double xmax = -INFTY;
×
UNCOV
737
  double ymax = -INFTY;
×
UNCOV
738
  double zmax = -INFTY;
×
UNCOV
739
  int n = this->n_vertices();
×
UNCOV
740
  for (int i = 0; i < n; ++i) {
×
UNCOV
741
    auto v = this->vertex(i);
×
UNCOV
742
    xmin = std::min(v.x, xmin);
×
UNCOV
743
    ymin = std::min(v.y, ymin);
×
UNCOV
744
    zmin = std::min(v.z, zmin);
×
UNCOV
745
    xmax = std::max(v.x, xmax);
×
UNCOV
746
    ymax = std::max(v.y, ymax);
×
UNCOV
747
    zmax = std::max(v.z, zmax);
×
748
  }
UNCOV
749
  lower_left_ = {xmin, ymin, zmin};
×
UNCOV
750
  upper_right_ = {xmax, ymax, zmax};
×
UNCOV
751
}
×
752

753
Position UnstructuredMesh::sample_tet(
400,820✔
754
  std::array<Position, 4> coords, uint64_t* seed) const
755
{
756
  // Uniform distribution
757
  double s = prn(seed);
400,820✔
758
  double t = prn(seed);
400,820✔
759
  double u = prn(seed);
400,820✔
760

761
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
762
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
763
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
764
  if (s + t > 1) {
400,820✔
765
    s = 1.0 - s;
200,182✔
766
    t = 1.0 - t;
200,182✔
767
  }
768
  if (s + t + u > 1) {
400,820✔
769
    if (t + u > 1) {
267,376✔
770
      double old_t = t;
133,723✔
771
      t = 1.0 - u;
133,723✔
772
      u = 1.0 - s - old_t;
133,723✔
773
    } else if (t + u <= 1) {
133,653!
774
      double old_s = s;
133,653✔
775
      s = 1.0 - t - u;
133,653✔
776
      u = old_s + t + u - 1;
133,653✔
777
    }
778
  }
779
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
801,640✔
780
         u * (coords[3] - coords[0]) + coords[0];
1,202,460✔
781
}
782

783
const std::string UnstructuredMesh::mesh_type = "unstructured";
784

785
std::string UnstructuredMesh::get_mesh_type() const
18✔
786
{
787
  return mesh_type;
18✔
788
}
789

UNCOV
790
void UnstructuredMesh::surface_bins_crossed(
×
791
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
792
{
UNCOV
793
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
794
}
795

796
std::string UnstructuredMesh::bin_label(int bin) const
97,856✔
797
{
798
  return fmt::format("Mesh Index ({})", bin);
97,856!
799
};
800

801
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
18✔
802
{
803
  write_dataset(mesh_group, "filename", filename_);
18!
804
  write_dataset(mesh_group, "library", this->library());
18!
805
  if (!options_.empty()) {
18!
UNCOV
806
    write_attribute(mesh_group, "options", options_);
×
807
  }
808

809
  if (length_multiplier_ > 0.0)
18!
UNCOV
810
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
811

812
  // write vertex coordinates
813
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
18!
814
  for (int i = 0; i < this->n_vertices(); i++) {
39,960!
815
    auto v = this->vertex(i);
39,942!
816
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
39,942!
817
  }
818
  write_dataset(mesh_group, "vertices", vertices);
18!
819

820
  int num_elem_skipped = 0;
18✔
821

822
  // write element types and connectivity
823
  vector<double> volumes;
18✔
824
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
18!
825
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
18!
826
  for (int i = 0; i < this->n_bins(); i++) {
193,874!
827
    auto conn = this->connectivity(i);
193,856!
828

829
    volumes.emplace_back(this->volume(i));
193,856!
830

831
    // write linear tet element
832
    if (conn.size() == 4) {
193,856✔
833
      xt::view(elem_types, i, xt::all()) =
383,712!
834
        static_cast<int>(ElementType::LINEAR_TET);
383,712!
835
      xt::view(connectivity, i, xt::all()) =
383,712!
836
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
575,568!
837
      // write linear hex element
838
    } else if (conn.size() == 8) {
2,000!
839
      xt::view(elem_types, i, xt::all()) =
4,000!
840
        static_cast<int>(ElementType::LINEAR_HEX);
4,000!
841
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000!
842
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000!
843
    } else {
UNCOV
844
      num_elem_skipped++;
×
845
      xt::view(elem_types, i, xt::all()) =
×
846
        static_cast<int>(ElementType::UNSUPPORTED);
×
847
      xt::view(connectivity, i, xt::all()) = -1;
×
848
    }
849
  }
193,856✔
850

851
  // warn users that some elements were skipped
852
  if (num_elem_skipped > 0) {
18!
UNCOV
853
    warning(fmt::format("The connectivity of {} elements "
×
854
                        "on mesh {} were not written "
855
                        "because they are not of type linear tet/hex.",
UNCOV
856
      num_elem_skipped, this->id_));
×
857
  }
858

859
  write_dataset(mesh_group, "volumes", volumes);
18!
860
  write_dataset(mesh_group, "connectivity", connectivity);
18!
861
  write_dataset(mesh_group, "element_types", elem_types);
18!
862
}
18✔
863

864
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
865
{
866
  length_multiplier_ = length_multiplier;
23✔
867
}
23✔
868

869
ElementType UnstructuredMesh::element_type(int bin) const
72,000✔
870
{
871
  auto conn = connectivity(bin);
72,000!
872

873
  if (conn.size() == 4)
72,000!
874
    return ElementType::LINEAR_TET;
72,000✔
UNCOV
875
  else if (conn.size() == 8)
×
876
    return ElementType::LINEAR_HEX;
×
877
  else
UNCOV
878
    return ElementType::UNSUPPORTED;
×
879
}
72,000✔
880

881
StructuredMesh::MeshIndex StructuredMesh::get_indices(
519,968,460✔
882
  Position r, bool& in_mesh) const
883
{
884
  MeshIndex ijk;
885
  in_mesh = true;
519,968,460✔
886
  for (int i = 0; i < n_dimension_; ++i) {
2,049,496,840✔
887
    ijk[i] = get_index_in_direction(r[i], i);
1,529,528,380✔
888

889
    if (ijk[i] < 1 || ijk[i] > shape_[i])
1,529,528,380✔
890
      in_mesh = false;
46,707,528✔
891
  }
892
  return ijk;
519,968,460✔
893
}
894

895
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
768,296,448✔
896
{
897
  switch (n_dimension_) {
768,296,448!
898
  case 1:
400,275✔
899
    return ijk[0] - 1;
400,275✔
900
  case 2:
44,275,100✔
901
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
44,275,100✔
902
  case 3:
723,621,073✔
903
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
723,621,073✔
UNCOV
904
  default:
×
905
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
906
  }
907
}
908

909
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
3,575,088✔
910
{
911
  MeshIndex ijk;
912
  if (n_dimension_ == 1) {
3,575,088✔
913
    ijk[0] = bin + 1;
125✔
914
  } else if (n_dimension_ == 2) {
3,574,963✔
915
    ijk[0] = bin % shape_[0] + 1;
7,100✔
916
    ijk[1] = bin / shape_[0] + 1;
7,100✔
917
  } else if (n_dimension_ == 3) {
3,567,863!
918
    ijk[0] = bin % shape_[0] + 1;
3,567,863✔
919
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
3,567,863✔
920
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
3,567,863✔
921
  }
922
  return ijk;
3,575,088✔
923
}
924

925
int StructuredMesh::get_bin(Position r) const
113,699,537✔
926
{
927
  // Determine indices
928
  bool in_mesh;
929
  MeshIndex ijk = get_indices(r, in_mesh);
113,699,537✔
930
  if (!in_mesh)
113,699,537✔
931
    return -1;
9,833,386✔
932

933
  // Convert indices to bin
934
  return get_bin_from_indices(ijk);
103,866,151✔
935
}
936

937
int StructuredMesh::n_bins() const
514,046✔
938
{
939
  return std::accumulate(
514,046✔
940
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
1,028,092✔
941
}
942

943
int StructuredMesh::n_surface_bins() const
170✔
944
{
945
  return 4 * n_dimension_ * n_bins();
170✔
946
}
947

UNCOV
948
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
949
  const SourceSite* bank, int64_t length, bool* outside) const
950
{
951
  // Determine shape of array for counts
UNCOV
952
  std::size_t m = this->n_bins();
×
953
  vector<std::size_t> shape = {m};
×
954

955
  // Create array of zeros
UNCOV
956
  xt::xarray<double> cnt {shape, 0.0};
×
957
  bool outside_ = false;
×
958

UNCOV
959
  for (int64_t i = 0; i < length; i++) {
×
960
    const auto& site = bank[i];
×
961

962
    // determine scoring bin for entropy mesh
UNCOV
963
    int mesh_bin = get_bin(site.r);
×
964

965
    // if outside mesh, skip particle
UNCOV
966
    if (mesh_bin < 0) {
×
967
      outside_ = true;
×
968
      continue;
×
969
    }
970

971
    // Add to appropriate bin
UNCOV
972
    cnt(mesh_bin) += site.wgt;
×
973
  }
974

975
  // Create copy of count data. Since ownership will be acquired by xtensor,
976
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
977
  // warnings.
UNCOV
978
  int total = cnt.size();
×
979
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
980

981
#ifdef OPENMC_MPI
982
  // collect values from all processors
983
  MPI_Reduce(
×
984
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
985

986
  // Check if there were sites outside the mesh for any processor
987
  if (outside) {
×
988
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
×
989
  }
990
#else
991
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
×
992
  if (outside)
×
993
    *outside = outside_;
994
#endif
995

996
  // Adapt reduced values in array back into an xarray
UNCOV
997
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
998
  xt::xarray<double> counts = arr;
×
999

UNCOV
1000
  return counts;
×
1001
}
×
1002

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

1016
  // Compute the length of the entire track.
1017
  double total_distance = (r1 - r0).norm();
407,887,360✔
1018
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
407,887,360✔
1019
    return;
5,288,682✔
1020

1021
  // keep a copy of the original global position to pass to get_indices,
1022
  // which performs its own transformation to local coordinates
1023
  Position global_r = r0;
402,598,678✔
1024
  Position local_r = local_coords(r0);
402,598,678✔
1025

1026
  const int n = n_dimension_;
402,598,678✔
1027

1028
  // Flag if position is inside the mesh
1029
  bool in_mesh;
1030

1031
  // Position is r = r0 + u * traveled_distance, start at r0
1032
  double traveled_distance {0.0};
402,598,678✔
1033

1034
  // Calculate index of current cell. Offset the position a tiny bit in
1035
  // direction of flight
1036
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
402,598,678✔
1037

1038
  // if track is very short, assume that it is completely inside one cell.
1039
  // Only the current cell will score and no surfaces
1040
  if (total_distance < 2 * TINY_BIT) {
402,598,678✔
1041
    if (in_mesh) {
150,615✔
1042
      tally.track(ijk, 1.0);
150,395✔
1043
    }
1044
    return;
150,615✔
1045
  }
1046

1047
  // Calculate initial distances to next surfaces in all three dimensions
1048
  std::array<MeshDistance, 3> distances;
804,896,126✔
1049
  for (int k = 0; k < n; ++k) {
1,580,269,457✔
1050
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
1,177,821,394✔
1051
  }
1052

1053
  // Loop until r = r1 is eventually reached
1054
  while (true) {
337,988,989✔
1055

1056
    if (in_mesh) {
740,437,052✔
1057

1058
      // find surface with minimal distance to current position
1059
      const auto k = std::min_element(distances.begin(), distances.end()) -
701,500,527✔
1060
                     distances.begin();
701,500,527✔
1061

1062
      // Tally track length delta since last step
1063
      tally.track(ijk,
701,500,527✔
1064
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
701,500,527✔
1065
          total_distance);
1066

1067
      // update position and leave, if we have reached end position
1068
      traveled_distance = distances[k].distance;
701,500,527✔
1069
      if (traveled_distance >= total_distance)
701,500,527✔
1070
        return;
366,561,733✔
1071

1072
      // If we have not reached r1, we have hit a surface. Tally outward
1073
      // current
1074
      tally.surface(ijk, k, distances[k].max_surface, false);
334,938,794✔
1075

1076
      // Update cell and calculate distance to next surface in k-direction.
1077
      // The two other directions are still valid!
1078
      ijk[k] = distances[k].next_index;
334,938,794✔
1079
      distances[k] =
334,938,794✔
1080
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
334,938,794✔
1081

1082
      // Check if we have left the interior of the mesh
1083
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
334,938,794✔
1084

1085
      // If we are still inside the mesh, tally inward current for the next
1086
      // cell
1087
      if (in_mesh)
334,938,794✔
1088
        tally.surface(ijk, k, !distances[k].max_surface, true);
328,900,159✔
1089

1090
    } else { // not inside mesh
1091

1092
      // For all directions outside the mesh, find the distance that we need
1093
      // to travel to reach the next surface. Use the largest distance, as
1094
      // only this will cross all outer surfaces.
1095
      int k_max {-1};
38,936,525✔
1096
      for (int k = 0; k < n; ++k) {
155,089,570✔
1097
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
158,687,755✔
1098
            (distances[k].distance > traveled_distance)) {
42,534,710✔
1099
          traveled_distance = distances[k].distance;
40,297,550✔
1100
          k_max = k;
40,297,550✔
1101
        }
1102
      }
1103
      // Assure some distance is traveled
1104
      if (k_max == -1) {
38,936,525✔
1105
        traveled_distance += TINY_BIT;
50✔
1106
      }
1107

1108
      // If r1 is not inside the mesh, exit here
1109
      if (traveled_distance >= total_distance)
38,936,525✔
1110
        return;
35,886,330✔
1111

1112
      // Calculate the new cell index and update all distances to next
1113
      // surfaces.
1114
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
3,050,195✔
1115
      for (int k = 0; k < n; ++k) {
12,105,990✔
1116
        distances[k] =
9,055,795✔
1117
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
9,055,795✔
1118
      }
1119

1120
      // If inside the mesh, Tally inward current
1121
      if (in_mesh && k_max >= 0)
3,050,195!
1122
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
2,865,325✔
1123
    }
1124
  }
1125
}
1126

1127
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
356,920,285✔
1128
  vector<int>& bins, vector<double>& lengths) const
1129
{
1130

1131
  // Helper tally class.
1132
  // stores a pointer to the mesh class and references to bins and lengths
1133
  // parameters. Performs the actual tally through the track method.
1134
  struct TrackAggregator {
1135
    TrackAggregator(
356,920,285✔
1136
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1137
      : mesh(_mesh), bins(_bins), lengths(_lengths)
356,920,285✔
1138
    {}
356,920,285✔
1139
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
640,268,283✔
1140
    void track(const MeshIndex& ijk, double l) const
637,994,302✔
1141
    {
1142
      bins.push_back(mesh->get_bin_from_indices(ijk));
637,994,302✔
1143
      lengths.push_back(l);
637,994,302✔
1144
    }
637,994,302✔
1145

1146
    const StructuredMesh* mesh;
1147
    vector<int>& bins;
1148
    vector<double>& lengths;
1149
  };
1150

1151
  // Perform the mesh raytrace with the helper class.
1152
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
356,920,285✔
1153
}
356,920,285✔
1154

1155
void StructuredMesh::surface_bins_crossed(
50,967,075✔
1156
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1157
{
1158

1159
  // Helper tally class.
1160
  // stores a pointer to the mesh class and a reference to the bins parameter.
1161
  // Performs the actual tally through the surface method.
1162
  struct SurfaceAggregator {
1163
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
50,967,075✔
1164
      : mesh(_mesh), bins(_bins)
50,967,075✔
1165
    {}
50,967,075✔
1166
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
26,435,995✔
1167
    {
1168
      int i_bin =
1169
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
26,435,995✔
1170
      if (max)
26,435,995✔
1171
        i_bin += 2;
13,205,200✔
1172
      if (inward)
26,435,995✔
1173
        i_bin += 1;
12,991,950✔
1174
      bins.push_back(i_bin);
26,435,995✔
1175
    }
26,435,995✔
1176
    void track(const MeshIndex& idx, double l) const {}
63,656,620✔
1177

1178
    const StructuredMesh* mesh;
1179
    vector<int>& bins;
1180
  };
1181

1182
  // Perform the mesh raytrace with the helper class.
1183
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
50,967,075✔
1184
}
50,967,075✔
1185

1186
double StructuredMesh::distance_to_next_boundary(Position r, Direction u) const
620,050✔
1187
{
1188
  Position global_r = r;
620,050✔
1189
  Position local_r = local_coords(r);
620,050✔
1190

1191
  double distance = 0.0;
620,050✔
1192
  const int n = n_dimension_;
620,050✔
1193
  bool in_mesh;
1194

1195
  StructuredMesh::MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
620,050✔
1196

1197
  // Calculate initial distances to next surfaces in all three dimensions
1198
  std::array<StructuredMesh::MeshDistance, 3> distances;
1,240,100✔
1199
  for (int k = 0; k < n; ++k) {
2,480,200✔
1200
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
1,860,150✔
1201
  }
1202

1203
  if (in_mesh) {
620,050✔
1204

1205
    // Find surface with minimal distance to current position
1206
    const auto k =
1207
      std::min_element(distances.begin(), distances.end()) - distances.begin();
620,040✔
1208

1209
    distance = distances[k].distance;
620,040✔
1210

1211
  } else { // not inside mesh
1212

1213
    // For all directions outside the mesh, find the distance that we need
1214
    // to travel to reach the next surface. Use the largest distance, as
1215
    // only this will cross all outer surfaces.
1216
    int k_max {-1};
10✔
1217
    for (int k = 0; k < n; ++k) {
40✔
1218
      if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
40!
1219
          (distances[k].distance > distance)) {
10!
1220
        distance = distances[k].distance;
10✔
1221
        k_max = k;
10✔
1222
      }
1223
    }
1224
  }
1225

1226
  return distance;
620,050✔
1227
}
1228

1229
//==============================================================================
1230
// RegularMesh implementation
1231
//==============================================================================
1232

1233
int RegularMesh::set_grid()
926✔
1234
{
1235
  auto shape = xt::adapt(shape_, {n_dimension_});
926✔
1236

1237
  // Check that dimensions are all greater than zero
1238
  if (xt::any(shape <= 0)) {
926!
UNCOV
1239
    set_errmsg("All entries for a regular mesh dimensions "
×
1240
               "must be positive.");
UNCOV
1241
    return OPENMC_E_INVALID_ARGUMENT;
×
1242
  }
1243

1244
  // Make sure lower_left and dimension match
1245
  if (lower_left_.size() != n_dimension_) {
926!
UNCOV
1246
    set_errmsg("Number of entries in lower_left must be the same "
×
1247
               "as the regular mesh dimensions.");
UNCOV
1248
    return OPENMC_E_INVALID_ARGUMENT;
×
1249
  }
1250
  if (width_.size() > 0) {
926✔
1251

1252
    // Check to ensure width has same dimensions
1253
    if (width_.size() != n_dimension_) {
21!
UNCOV
1254
      set_errmsg("Number of entries on width must be the same as "
×
1255
                 "the regular mesh dimensions.");
UNCOV
1256
      return OPENMC_E_INVALID_ARGUMENT;
×
1257
    }
1258

1259
    // Check for negative widths
1260
    if (xt::any(width_ < 0.0)) {
21!
UNCOV
1261
      set_errmsg("Cannot have a negative width on a regular mesh.");
×
1262
      return OPENMC_E_INVALID_ARGUMENT;
×
1263
    }
1264

1265
    // Set width and upper right coordinate
1266
    upper_right_ = xt::eval(lower_left_ + shape * width_);
21✔
1267

1268
  } else if (upper_right_.size() > 0) {
905!
1269

1270
    // Check to ensure upper_right_ has same dimensions
1271
    if (upper_right_.size() != n_dimension_) {
905!
UNCOV
1272
      set_errmsg("Number of entries on upper_right must be the "
×
1273
                 "same as the regular mesh dimensions.");
UNCOV
1274
      return OPENMC_E_INVALID_ARGUMENT;
×
1275
    }
1276

1277
    // Check that upper-right is above lower-left
1278
    if (xt::any(upper_right_ < lower_left_)) {
905!
UNCOV
1279
      set_errmsg(
×
1280
        "The upper_right coordinates of a regular mesh must be greater than "
1281
        "the lower_left coordinates.");
UNCOV
1282
      return OPENMC_E_INVALID_ARGUMENT;
×
1283
    }
1284

1285
    // Set width
1286
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
905✔
1287
  }
1288

1289
  // Set material volumes
1290
  volume_frac_ = 1.0 / xt::prod(shape)();
926✔
1291

1292
  element_volume_ = 1.0;
926✔
1293
  for (int i = 0; i < n_dimension_; i++) {
3,498✔
1294
    element_volume_ *= width_[i];
2,572✔
1295
  }
1296
  return 0;
926✔
1297
}
926✔
1298

1299
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
921✔
1300
{
1301
  // Determine number of dimensions for mesh
1302
  if (!check_for_node(node, "dimension")) {
921!
UNCOV
1303
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1304
  }
1305

1306
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
921✔
1307
  int n = n_dimension_ = shape.size();
921✔
1308
  if (n != 1 && n != 2 && n != 3) {
921!
UNCOV
1309
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1310
  }
1311
  std::copy(shape.begin(), shape.end(), shape_.begin());
921✔
1312

1313
  // Check for lower-left coordinates
1314
  if (check_for_node(node, "lower_left")) {
921!
1315
    // Read mesh lower-left corner location
1316
    lower_left_ = get_node_xarray<double>(node, "lower_left");
921✔
1317
  } else {
UNCOV
1318
    fatal_error("Must specify <lower_left> on a mesh.");
×
1319
  }
1320

1321
  if (check_for_node(node, "width")) {
921✔
1322
    // Make sure one of upper-right or width were specified
1323
    if (check_for_node(node, "upper_right")) {
21!
UNCOV
1324
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
×
1325
    }
1326

1327
    width_ = get_node_xarray<double>(node, "width");
21✔
1328

1329
  } else if (check_for_node(node, "upper_right")) {
900!
1330

1331
    upper_right_ = get_node_xarray<double>(node, "upper_right");
900✔
1332

1333
  } else {
UNCOV
1334
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
×
1335
  }
1336

1337
  if (int err = set_grid()) {
921!
UNCOV
1338
    fatal_error(openmc_err_msg);
×
1339
  }
1340
}
921✔
1341

1342
RegularMesh::RegularMesh(hid_t group) : StructuredMesh {group}
5✔
1343
{
1344
  // Determine number of dimensions for mesh
1345
  if (!object_exists(group, "dimension")) {
5!
UNCOV
1346
    fatal_error("Must specify <dimension> on a regular mesh.");
×
1347
  }
1348

1349
  xt::xtensor<int, 1> shape;
5✔
1350
  read_dataset(group, "dimension", shape);
5✔
1351
  int n = n_dimension_ = shape.size();
5✔
1352
  if (n != 1 && n != 2 && n != 3) {
5!
UNCOV
1353
    fatal_error("Mesh must be one, two, or three dimensions.");
×
1354
  }
1355
  std::copy(shape.begin(), shape.end(), shape_.begin());
5✔
1356

1357
  // Check for lower-left coordinates
1358
  if (object_exists(group, "lower_left")) {
5!
1359
    // Read mesh lower-left corner location
1360
    read_dataset(group, "lower_left", lower_left_);
5✔
1361
  } else {
UNCOV
1362
    fatal_error("Must specify lower_left dataset on a mesh.");
×
1363
  }
1364

1365
  if (object_exists(group, "upper_right")) {
5!
1366

1367
    read_dataset(group, "upper_right", upper_right_);
5✔
1368

1369
  } else {
UNCOV
1370
    fatal_error("Must specify either upper_right dataset on a mesh.");
×
1371
  }
1372

1373
  if (int err = set_grid()) {
5!
UNCOV
1374
    fatal_error(openmc_err_msg);
×
1375
  }
1376
}
5✔
1377

1378
int RegularMesh::get_index_in_direction(double r, int i) const
1,337,794,450✔
1379
{
1380
  return std::ceil((r - lower_left_[i]) / width_[i]);
1,337,794,450✔
1381
}
1382

1383
const std::string RegularMesh::mesh_type = "regular";
1384

1385
std::string RegularMesh::get_mesh_type() const
1,425✔
1386
{
1387
  return mesh_type;
1,425✔
1388
}
1389

1390
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
652,560,955✔
1391
{
1392
  return lower_left_[i] + ijk[i] * width_[i];
652,560,955✔
1393
}
1394

1395
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
623,797,957✔
1396
{
1397
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
623,797,957✔
1398
}
1399

1400
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
1,282,555,998✔
1401
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1402
  double l) const
1403
{
1404
  MeshDistance d;
1,282,555,998✔
1405
  d.next_index = ijk[i];
1,282,555,998✔
1406
  if (std::abs(u[i]) < FP_PRECISION)
1,282,555,998✔
1407
    return d;
797,400✔
1408

1409
  d.max_surface = (u[i] > 0);
1,281,758,598✔
1410
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1,281,758,598✔
1411
    d.next_index++;
650,483,893✔
1412
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
650,483,893✔
1413
  } else if (!d.max_surface && (ijk[i] >= 1)) {
631,274,705✔
1414
    d.next_index--;
621,720,895✔
1415
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
621,720,895✔
1416
  }
1417

1418
  return d;
1,281,758,598✔
1419
}
1420

1421
std::pair<vector<double>, vector<double>> RegularMesh::plot(
10✔
1422
  Position plot_ll, Position plot_ur) const
1423
{
1424
  // Figure out which axes lie in the plane of the plot.
1425
  array<int, 2> axes {-1, -1};
10✔
1426
  if (plot_ur.z == plot_ll.z) {
10!
1427
    axes[0] = 0;
10✔
1428
    if (n_dimension_ > 1)
10!
1429
      axes[1] = 1;
10✔
UNCOV
1430
  } else if (plot_ur.y == plot_ll.y) {
×
1431
    axes[0] = 0;
×
1432
    if (n_dimension_ > 2)
×
1433
      axes[1] = 2;
×
1434
  } else if (plot_ur.x == plot_ll.x) {
×
1435
    if (n_dimension_ > 1)
×
1436
      axes[0] = 1;
×
1437
    if (n_dimension_ > 2)
×
1438
      axes[1] = 2;
×
1439
  } else {
UNCOV
1440
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1441
  }
1442

1443
  // Get the coordinates of the mesh lines along both of the axes.
1444
  array<vector<double>, 2> axis_lines;
10✔
1445
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
30✔
1446
    int axis = axes[i_ax];
20✔
1447
    if (axis == -1)
20!
UNCOV
1448
      continue;
×
1449
    auto& lines {axis_lines[i_ax]};
20✔
1450

1451
    double coord = lower_left_[axis];
20✔
1452
    for (int i = 0; i < shape_[axis] + 1; ++i) {
130✔
1453
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
110!
1454
        lines.push_back(coord);
110✔
1455
      coord += width_[axis];
110✔
1456
    }
1457
  }
1458

1459
  return {axis_lines[0], axis_lines[1]};
20✔
1460
}
10✔
1461

1462
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
915✔
1463
{
1464
  write_dataset(mesh_group, "dimension", get_x_shape());
915✔
1465
  write_dataset(mesh_group, "lower_left", lower_left_);
915✔
1466
  write_dataset(mesh_group, "upper_right", upper_right_);
915✔
1467
  write_dataset(mesh_group, "width", width_);
915✔
1468
}
915✔
1469

1470
xt::xtensor<double, 1> RegularMesh::count_sites(
3,558✔
1471
  const SourceSite* bank, int64_t length, bool* outside) const
1472
{
1473
  // Determine shape of array for counts
1474
  std::size_t m = this->n_bins();
3,558✔
1475
  vector<std::size_t> shape = {m};
3,558✔
1476

1477
  // Create array of zeros
1478
  xt::xarray<double> cnt {shape, 0.0};
3,558✔
1479
  bool outside_ = false;
3,558✔
1480

1481
  for (int64_t i = 0; i < length; i++) {
3,488,763✔
1482
    const auto& site = bank[i];
3,485,205✔
1483

1484
    // determine scoring bin for entropy mesh
1485
    int mesh_bin = get_bin(site.r);
3,485,205✔
1486

1487
    // if outside mesh, skip particle
1488
    if (mesh_bin < 0) {
3,485,205!
UNCOV
1489
      outside_ = true;
×
1490
      continue;
×
1491
    }
1492

1493
    // Add to appropriate bin
1494
    cnt(mesh_bin) += site.wgt;
3,485,205✔
1495
  }
1496

1497
  // Create copy of count data. Since ownership will be acquired by xtensor,
1498
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
1499
  // warnings.
1500
  int total = cnt.size();
3,558✔
1501
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
3,558✔
1502

1503
#ifdef OPENMC_MPI
1504
  // collect values from all processors
1505
  MPI_Reduce(
1,446✔
1506
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1,446✔
1507

1508
  // Check if there were sites outside the mesh for any processor
1509
  if (outside) {
1,446!
1510
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1,446✔
1511
  }
1512
#else
1513
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
2,112✔
1514
  if (outside)
2,112!
1515
    *outside = outside_;
2,112✔
1516
#endif
1517

1518
  // Adapt reduced values in array back into an xarray
1519
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
3,558✔
1520
  xt::xarray<double> counts = arr;
3,558✔
1521

1522
  return counts;
7,116✔
1523
}
3,558✔
1524

1525
double RegularMesh::volume(const MeshIndex& ijk) const
508,004✔
1526
{
1527
  return element_volume_;
508,004✔
1528
}
1529

1530
//==============================================================================
1531
// RectilinearMesh implementation
1532
//==============================================================================
1533

1534
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
56✔
1535
{
1536
  n_dimension_ = 3;
56✔
1537

1538
  grid_[0] = get_node_array<double>(node, "x_grid");
56✔
1539
  grid_[1] = get_node_array<double>(node, "y_grid");
56✔
1540
  grid_[2] = get_node_array<double>(node, "z_grid");
56✔
1541

1542
  if (int err = set_grid()) {
56!
UNCOV
1543
    fatal_error(openmc_err_msg);
×
1544
  }
1545
}
56✔
1546

1547
RectilinearMesh::RectilinearMesh(hid_t group) : StructuredMesh {group}
5✔
1548
{
1549
  n_dimension_ = 3;
5✔
1550

1551
  read_dataset(group, "x_grid", grid_[0]);
5✔
1552
  read_dataset(group, "y_grid", grid_[1]);
5✔
1553
  read_dataset(group, "z_grid", grid_[2]);
5✔
1554

1555
  if (int err = set_grid()) {
5!
UNCOV
1556
    fatal_error(openmc_err_msg);
×
1557
  }
1558
}
5✔
1559

1560
const std::string RectilinearMesh::mesh_type = "rectilinear";
1561

1562
std::string RectilinearMesh::get_mesh_type() const
125✔
1563
{
1564
  return mesh_type;
125✔
1565
}
1566

1567
double RectilinearMesh::positive_grid_boundary(
12,048,165✔
1568
  const MeshIndex& ijk, int i) const
1569
{
1570
  return grid_[i][ijk[i]];
12,048,165✔
1571
}
1572

1573
double RectilinearMesh::negative_grid_boundary(
11,699,730✔
1574
  const MeshIndex& ijk, int i) const
1575
{
1576
  return grid_[i][ijk[i] - 1];
11,699,730✔
1577
}
1578

1579
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
24,364,585✔
1580
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1581
  double l) const
1582
{
1583
  MeshDistance d;
24,364,585✔
1584
  d.next_index = ijk[i];
24,364,585✔
1585
  if (std::abs(u[i]) < FP_PRECISION)
24,364,585✔
1586
    return d;
259,920✔
1587

1588
  d.max_surface = (u[i] > 0);
24,104,665✔
1589
  if (d.max_surface && (ijk[i] <= shape_[i])) {
24,104,665✔
1590
    d.next_index++;
12,048,165✔
1591
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
12,048,165✔
1592
  } else if (!d.max_surface && (ijk[i] > 0)) {
12,056,500✔
1593
    d.next_index--;
11,699,730✔
1594
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
11,699,730✔
1595
  }
1596
  return d;
24,104,665✔
1597
}
1598

1599
int RectilinearMesh::set_grid()
81✔
1600
{
1601
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
81✔
1602
    static_cast<int>(grid_[1].size()) - 1,
81✔
1603
    static_cast<int>(grid_[2].size()) - 1};
81✔
1604

1605
  for (const auto& g : grid_) {
324✔
1606
    if (g.size() < 2) {
243!
UNCOV
1607
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
×
1608
                 "must each have at least 2 points");
UNCOV
1609
      return OPENMC_E_INVALID_ARGUMENT;
×
1610
    }
1611
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
243✔
1612
        g.end()) {
486!
UNCOV
1613
      set_errmsg("Values in for x-, y-, and z- grids for "
×
1614
                 "rectilinear meshes must be sorted and unique.");
UNCOV
1615
      return OPENMC_E_INVALID_ARGUMENT;
×
1616
    }
1617
  }
1618

1619
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
81✔
1620
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
81✔
1621

1622
  return 0;
81✔
1623
}
1624

1625
int RectilinearMesh::get_index_in_direction(double r, int i) const
33,685,860✔
1626
{
1627
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
33,685,860✔
1628
}
1629

1630
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
5✔
1631
  Position plot_ll, Position plot_ur) const
1632
{
1633
  // Figure out which axes lie in the plane of the plot.
1634
  array<int, 2> axes {-1, -1};
5✔
1635
  if (plot_ur.z == plot_ll.z) {
5!
UNCOV
1636
    axes = {0, 1};
×
1637
  } else if (plot_ur.y == plot_ll.y) {
5!
1638
    axes = {0, 2};
5✔
UNCOV
1639
  } else if (plot_ur.x == plot_ll.x) {
×
1640
    axes = {1, 2};
×
1641
  } else {
UNCOV
1642
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1643
  }
1644

1645
  // Get the coordinates of the mesh lines along both of the axes.
1646
  array<vector<double>, 2> axis_lines;
5✔
1647
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
15✔
1648
    int axis = axes[i_ax];
10✔
1649
    vector<double>& lines {axis_lines[i_ax]};
10✔
1650

1651
    for (auto coord : grid_[axis]) {
50✔
1652
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
40!
1653
        lines.push_back(coord);
40✔
1654
    }
1655
  }
1656

1657
  return {axis_lines[0], axis_lines[1]};
10✔
1658
}
5✔
1659

1660
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
50✔
1661
{
1662
  write_dataset(mesh_group, "x_grid", grid_[0]);
50✔
1663
  write_dataset(mesh_group, "y_grid", grid_[1]);
50✔
1664
  write_dataset(mesh_group, "z_grid", grid_[2]);
50✔
1665
}
50✔
1666

1667
double RectilinearMesh::volume(const MeshIndex& ijk) const
60✔
1668
{
1669
  double vol {1.0};
60✔
1670

1671
  for (int i = 0; i < n_dimension_; i++) {
240✔
1672
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
180✔
1673
  }
1674
  return vol;
60✔
1675
}
1676

1677
//==============================================================================
1678
// CylindricalMesh implementation
1679
//==============================================================================
1680

1681
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
182✔
1682
  : PeriodicStructuredMesh {node}
182✔
1683
{
1684
  n_dimension_ = 3;
182✔
1685
  grid_[0] = get_node_array<double>(node, "r_grid");
182✔
1686
  grid_[1] = get_node_array<double>(node, "phi_grid");
182✔
1687
  grid_[2] = get_node_array<double>(node, "z_grid");
182✔
1688
  origin_ = get_node_position(node, "origin");
182✔
1689

1690
  if (int err = set_grid()) {
182!
UNCOV
1691
    fatal_error(openmc_err_msg);
×
1692
  }
1693
}
182✔
1694

1695
CylindricalMesh::CylindricalMesh(hid_t group) : PeriodicStructuredMesh {group}
5✔
1696
{
1697
  n_dimension_ = 3;
5✔
1698
  read_dataset(group, "r_grid", grid_[0]);
5✔
1699
  read_dataset(group, "phi_grid", grid_[1]);
5✔
1700
  read_dataset(group, "z_grid", grid_[2]);
5✔
1701
  read_dataset(group, "origin", origin_);
5✔
1702

1703
  if (int err = set_grid()) {
5!
UNCOV
1704
    fatal_error(openmc_err_msg);
×
1705
  }
1706
}
5✔
1707

1708
const std::string CylindricalMesh::mesh_type = "cylindrical";
1709

1710
std::string CylindricalMesh::get_mesh_type() const
220✔
1711
{
1712
  return mesh_type;
220✔
1713
}
1714

1715
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
21,693,940✔
1716
  Position r, bool& in_mesh) const
1717
{
1718
  r = local_coords(r);
21,693,940✔
1719

1720
  Position mapped_r;
21,693,940✔
1721
  mapped_r[0] = std::hypot(r.x, r.y);
21,693,940✔
1722
  mapped_r[2] = r[2];
21,693,940✔
1723

1724
  if (mapped_r[0] < FP_PRECISION) {
21,693,940!
UNCOV
1725
    mapped_r[1] = 0.0;
×
1726
  } else {
1727
    mapped_r[1] = std::atan2(r.y, r.x);
21,693,940✔
1728
    if (mapped_r[1] < 0)
21,693,940✔
1729
      mapped_r[1] += 2 * M_PI;
10,851,105✔
1730
  }
1731

1732
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
21,693,940✔
1733

1734
  idx[1] = sanitize_phi(idx[1]);
21,693,940✔
1735

1736
  return idx;
21,693,940✔
1737
}
1738

1739
Position CylindricalMesh::sample_element(
40,050✔
1740
  const MeshIndex& ijk, uint64_t* seed) const
1741
{
1742
  double r_min = this->r(ijk[0] - 1);
40,050✔
1743
  double r_max = this->r(ijk[0]);
40,050✔
1744

1745
  double phi_min = this->phi(ijk[1] - 1);
40,050✔
1746
  double phi_max = this->phi(ijk[1]);
40,050✔
1747

1748
  double z_min = this->z(ijk[2] - 1);
40,050✔
1749
  double z_max = this->z(ijk[2]);
40,050✔
1750

1751
  double r_min_sq = r_min * r_min;
40,050✔
1752
  double r_max_sq = r_max * r_max;
40,050✔
1753
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
40,050✔
1754
  double phi = uniform_distribution(phi_min, phi_max, seed);
40,050✔
1755
  double z = uniform_distribution(z_min, z_max, seed);
40,050✔
1756

1757
  double x = r * std::cos(phi);
40,050✔
1758
  double y = r * std::sin(phi);
40,050✔
1759

1760
  return origin_ + Position(x, y, z);
40,050✔
1761
}
1762

1763
double CylindricalMesh::find_r_crossing(
64,803,120✔
1764
  const Position& r, const Direction& u, double l, int shell) const
1765
{
1766

1767
  if ((shell < 0) || (shell > shape_[0]))
64,803,120!
1768
    return INFTY;
8,142,660✔
1769

1770
  // solve r.x^2 + r.y^2 == r0^2
1771
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1772
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1773

1774
  const double r0 = grid_[0][shell];
56,660,460✔
1775
  if (r0 == 0.0)
56,660,460✔
1776
    return INFTY;
3,241,205✔
1777

1778
  const double denominator = u.x * u.x + u.y * u.y;
53,419,255✔
1779

1780
  // Direction of flight is in z-direction. Will never intersect r.
1781
  if (std::abs(denominator) < FP_PRECISION)
53,419,255✔
1782
    return INFTY;
26,800✔
1783

1784
  // inverse of dominator to help the compiler to speed things up
1785
  const double inv_denominator = 1.0 / denominator;
53,392,455✔
1786

1787
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
53,392,455✔
1788
  double c = r.x * r.x + r.y * r.y - r0 * r0;
53,392,455✔
1789
  double D = p * p - c * inv_denominator;
53,392,455✔
1790

1791
  if (D < 0.0)
53,392,455✔
1792
    return INFTY;
4,424,350✔
1793

1794
  D = std::sqrt(D);
48,968,105✔
1795

1796
  // the solution -p - D is always smaller as -p + D : Check this one first
1797
  if (std::abs(c) <= RADIAL_MESH_TOL)
48,968,105✔
1798
    return INFTY;
3,005,170✔
1799

1800
  if (-p - D > l)
45,962,935✔
1801
    return -p - D;
9,184,350✔
1802
  if (-p + D > l)
36,778,585✔
1803
    return -p + D;
22,768,095✔
1804

1805
  return INFTY;
14,010,490✔
1806
}
1807

1808
double CylindricalMesh::find_phi_crossing(
33,838,890✔
1809
  const Position& r, const Direction& u, double l, int shell) const
1810
{
1811
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1812
  if (full_phi_ && (shape_[1] == 1))
33,838,890✔
1813
    return INFTY;
13,852,200✔
1814

1815
  shell = sanitize_phi(shell);
19,986,690✔
1816

1817
  const double p0 = grid_[1][shell];
19,986,690✔
1818

1819
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1820
  // => x(s) * cos(p0) = y(s) * sin(p0)
1821
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1822
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1823

1824
  const double c0 = std::cos(p0);
19,986,690✔
1825
  const double s0 = std::sin(p0);
19,986,690✔
1826

1827
  const double denominator = (u.x * s0 - u.y * c0);
19,986,690✔
1828

1829
  // Check if direction of flight is not parallel to phi surface
1830
  if (std::abs(denominator) > FP_PRECISION) {
19,986,690✔
1831
    const double s = -(r.x * s0 - r.y * c0) / denominator;
19,868,170✔
1832
    // Check if solution is in positive direction of flight and crosses the
1833
    // correct phi surface (not -phi)
1834
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
19,868,170✔
1835
      return s;
9,190,845✔
1836
  }
1837

1838
  return INFTY;
10,795,845✔
1839
}
1840

1841
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
16,677,420✔
1842
  const Position& r, const Direction& u, double l, int shell) const
1843
{
1844
  MeshDistance d;
16,677,420✔
1845
  d.next_index = shell;
16,677,420✔
1846

1847
  // Direction of flight is within xy-plane. Will never intersect z.
1848
  if (std::abs(u.z) < FP_PRECISION)
16,677,420✔
1849
    return d;
508,280✔
1850

1851
  d.max_surface = (u.z > 0.0);
16,169,140✔
1852
  if (d.max_surface && (shell <= shape_[2])) {
16,169,140✔
1853
    d.next_index += 1;
7,669,655✔
1854
    d.distance = (grid_[2][shell] - r.z) / u.z;
7,669,655✔
1855
  } else if (!d.max_surface && (shell > 0)) {
8,499,485✔
1856
    d.next_index -= 1;
7,656,115✔
1857
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
7,656,115✔
1858
  }
1859
  return d;
16,169,140✔
1860
}
1861

1862
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
65,998,425✔
1863
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1864
  double l) const
1865
{
1866
  if (i == 0) {
65,998,425✔
1867

1868
    return std::min(
32,401,560✔
1869
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
32,401,560✔
1870
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
64,803,120✔
1871

1872
  } else if (i == 1) {
33,596,865✔
1873

1874
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
16,919,445✔
1875
                      find_phi_crossing(r0, u, l, ijk[i])),
16,919,445✔
1876
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
16,919,445✔
1877
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
33,838,890✔
1878

1879
  } else {
1880
    return find_z_crossing(r0, u, l, ijk[i]);
16,677,420✔
1881
  }
1882
}
1883

1884
int CylindricalMesh::set_grid()
197✔
1885
{
1886
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
197✔
1887
    static_cast<int>(grid_[1].size()) - 1,
197✔
1888
    static_cast<int>(grid_[2].size()) - 1};
197✔
1889

1890
  for (const auto& g : grid_) {
788✔
1891
    if (g.size() < 2) {
591!
UNCOV
1892
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
×
1893
                 "must each have at least 2 points");
UNCOV
1894
      return OPENMC_E_INVALID_ARGUMENT;
×
1895
    }
1896
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
591✔
1897
        g.end()) {
1,182!
UNCOV
1898
      set_errmsg("Values in for r-, phi-, and z- grids for "
×
1899
                 "cylindrical meshes must be sorted and unique.");
UNCOV
1900
      return OPENMC_E_INVALID_ARGUMENT;
×
1901
    }
1902
  }
1903
  if (grid_[0].front() < 0.0) {
197!
UNCOV
1904
    set_errmsg("r-grid for "
×
1905
               "cylindrical meshes must start at r >= 0.");
UNCOV
1906
    return OPENMC_E_INVALID_ARGUMENT;
×
1907
  }
1908
  if (grid_[1].front() < 0.0) {
197!
UNCOV
1909
    set_errmsg("phi-grid for "
×
1910
               "cylindrical meshes must start at phi >= 0.");
UNCOV
1911
    return OPENMC_E_INVALID_ARGUMENT;
×
1912
  }
1913
  if (grid_[1].back() > 2.0 * PI) {
197!
UNCOV
1914
    set_errmsg("phi-grids for "
×
1915
               "cylindrical meshes must end with theta <= 2*pi.");
1916

UNCOV
1917
    return OPENMC_E_INVALID_ARGUMENT;
×
1918
  }
1919

1920
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
197!
1921

1922
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
394✔
1923
    origin_[2] + grid_[2].front()};
394✔
1924
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
394✔
1925
    origin_[2] + grid_[2].back()};
394✔
1926

1927
  return 0;
197✔
1928
}
1929

1930
int CylindricalMesh::get_index_in_direction(double r, int i) const
65,081,820✔
1931
{
1932
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
65,081,820✔
1933
}
1934

UNCOV
1935
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
×
1936
  Position plot_ll, Position plot_ur) const
1937
{
UNCOV
1938
  fatal_error("Plot of cylindrical Mesh not implemented");
×
1939

1940
  // Figure out which axes lie in the plane of the plot.
1941
  array<vector<double>, 2> axis_lines;
1942
  return {axis_lines[0], axis_lines[1]};
1943
}
1944

1945
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
170✔
1946
{
1947
  write_dataset(mesh_group, "r_grid", grid_[0]);
170✔
1948
  write_dataset(mesh_group, "phi_grid", grid_[1]);
170✔
1949
  write_dataset(mesh_group, "z_grid", grid_[2]);
170✔
1950
  write_dataset(mesh_group, "origin", origin_);
170✔
1951
}
170✔
1952

1953
double CylindricalMesh::volume(const MeshIndex& ijk) const
360✔
1954
{
1955
  double r_i = grid_[0][ijk[0] - 1];
360✔
1956
  double r_o = grid_[0][ijk[0]];
360✔
1957

1958
  double phi_i = grid_[1][ijk[1] - 1];
360✔
1959
  double phi_o = grid_[1][ijk[1]];
360✔
1960

1961
  double z_i = grid_[2][ijk[2] - 1];
360✔
1962
  double z_o = grid_[2][ijk[2]];
360✔
1963

1964
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
360✔
1965
}
1966

1967
//==============================================================================
1968
// SphericalMesh implementation
1969
//==============================================================================
1970

1971
SphericalMesh::SphericalMesh(pugi::xml_node node)
157✔
1972
  : PeriodicStructuredMesh {node}
157✔
1973
{
1974
  n_dimension_ = 3;
157✔
1975

1976
  grid_[0] = get_node_array<double>(node, "r_grid");
157✔
1977
  grid_[1] = get_node_array<double>(node, "theta_grid");
157✔
1978
  grid_[2] = get_node_array<double>(node, "phi_grid");
157✔
1979
  origin_ = get_node_position(node, "origin");
157✔
1980

1981
  if (int err = set_grid()) {
157!
UNCOV
1982
    fatal_error(openmc_err_msg);
×
1983
  }
1984
}
157✔
1985

1986
SphericalMesh::SphericalMesh(hid_t group) : PeriodicStructuredMesh {group}
5✔
1987
{
1988
  n_dimension_ = 3;
5✔
1989

1990
  read_dataset(group, "r_grid", grid_[0]);
5✔
1991
  read_dataset(group, "theta_grid", grid_[1]);
5✔
1992
  read_dataset(group, "phi_grid", grid_[2]);
5✔
1993
  read_dataset(group, "origin", origin_);
5✔
1994

1995
  if (int err = set_grid()) {
5!
UNCOV
1996
    fatal_error(openmc_err_msg);
×
1997
  }
1998
}
5✔
1999

2000
const std::string SphericalMesh::mesh_type = "spherical";
2001

2002
std::string SphericalMesh::get_mesh_type() const
175✔
2003
{
2004
  return mesh_type;
175✔
2005
}
2006

2007
StructuredMesh::MeshIndex SphericalMesh::get_indices(
30,988,750✔
2008
  Position r, bool& in_mesh) const
2009
{
2010
  r = local_coords(r);
30,988,750✔
2011

2012
  Position mapped_r;
30,988,750✔
2013
  mapped_r[0] = r.norm();
30,988,750✔
2014

2015
  if (mapped_r[0] < FP_PRECISION) {
30,988,750!
UNCOV
2016
    mapped_r[1] = 0.0;
×
2017
    mapped_r[2] = 0.0;
×
2018
  } else {
2019
    mapped_r[1] = std::acos(r.z / mapped_r.x);
30,988,750✔
2020
    mapped_r[2] = std::atan2(r.y, r.x);
30,988,750✔
2021
    if (mapped_r[2] < 0)
30,988,750✔
2022
      mapped_r[2] += 2 * M_PI;
15,482,855✔
2023
  }
2024

2025
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
30,988,750✔
2026

2027
  idx[1] = sanitize_theta(idx[1]);
30,988,750✔
2028
  idx[2] = sanitize_phi(idx[2]);
30,988,750✔
2029

2030
  return idx;
30,988,750✔
2031
}
2032

2033
Position SphericalMesh::sample_element(
50✔
2034
  const MeshIndex& ijk, uint64_t* seed) const
2035
{
2036
  double r_min = this->r(ijk[0] - 1);
50✔
2037
  double r_max = this->r(ijk[0]);
50✔
2038

2039
  double theta_min = this->theta(ijk[1] - 1);
50✔
2040
  double theta_max = this->theta(ijk[1]);
50✔
2041

2042
  double phi_min = this->phi(ijk[2] - 1);
50✔
2043
  double phi_max = this->phi(ijk[2]);
50✔
2044

2045
  double cos_theta =
2046
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
50✔
2047
  double sin_theta = std::sin(std::acos(cos_theta));
50✔
2048
  double phi = uniform_distribution(phi_min, phi_max, seed);
50✔
2049
  double r_min_cub = std::pow(r_min, 3);
50✔
2050
  double r_max_cub = std::pow(r_max, 3);
50✔
2051
  // might be faster to do rejection here?
2052
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
50✔
2053

2054
  double x = r * std::cos(phi) * sin_theta;
50✔
2055
  double y = r * std::sin(phi) * sin_theta;
50✔
2056
  double z = r * cos_theta;
50✔
2057

2058
  return origin_ + Position(x, y, z);
50✔
2059
}
2060

2061
double SphericalMesh::find_r_crossing(
201,403,040✔
2062
  const Position& r, const Direction& u, double l, int shell) const
2063
{
2064
  if ((shell < 0) || (shell > shape_[0]))
201,403,040✔
2065
    return INFTY;
18,009,235✔
2066

2067
  // solve |r+s*u| = r0
2068
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2069
  const double r0 = grid_[0][shell];
183,393,805✔
2070
  if (r0 == 0.0)
183,393,805✔
2071
    return INFTY;
3,300,745✔
2072
  const double p = r.dot(u);
180,093,060✔
2073
  double c = r.dot(r) - r0 * r0;
180,093,060✔
2074
  double D = p * p - c;
180,093,060✔
2075

2076
  if (std::abs(c) <= RADIAL_MESH_TOL)
180,093,060✔
2077
    return INFTY;
4,817,570✔
2078

2079
  if (D >= 0.0) {
175,275,490✔
2080
    D = std::sqrt(D);
162,604,150✔
2081
    // the solution -p - D is always smaller as -p + D : Check this one first
2082
    if (-p - D > l)
162,604,150✔
2083
      return -p - D;
29,218,580✔
2084
    if (-p + D > l)
133,385,570✔
2085
      return -p + D;
80,411,500✔
2086
  }
2087

2088
  return INFTY;
65,645,410✔
2089
}
2090

2091
double SphericalMesh::find_theta_crossing(
49,694,360✔
2092
  const Position& r, const Direction& u, double l, int shell) const
2093
{
2094
  // Theta grid is [0, π], thus there is no real surface to cross
2095
  if (full_theta_ && (shape_[1] == 1))
49,694,360✔
2096
    return INFTY;
32,258,660✔
2097

2098
  shell = sanitize_theta(shell);
17,435,700✔
2099

2100
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
2101
  // yields
2102
  // a*s^2 + 2*b*s + c == 0 with
2103
  // a = cos(theta)^2 - u.z * u.z
2104
  // b = r*u * cos(theta)^2 - u.z * r.z
2105
  // c = r*r * cos(theta)^2 - r.z^2
2106

2107
  const double cos_t = std::cos(grid_[1][shell]);
17,435,700✔
2108
  const bool sgn = std::signbit(cos_t);
17,435,700✔
2109
  const double cos_t_2 = cos_t * cos_t;
17,435,700✔
2110

2111
  const double a = cos_t_2 - u.z * u.z;
17,435,700✔
2112
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
17,435,700✔
2113
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
17,435,700✔
2114

2115
  // if factor of s^2 is zero, direction of flight is parallel to theta
2116
  // surface
2117
  if (std::abs(a) < FP_PRECISION) {
17,435,700✔
2118
    // if b vanishes, direction of flight is within theta surface and crossing
2119
    // is not possible
2120
    if (std::abs(b) < FP_PRECISION)
219,340!
2121
      return INFTY;
219,340✔
2122

UNCOV
2123
    const double s = -0.5 * c / b;
×
2124
    // Check if solution is in positive direction of flight and has correct
2125
    // sign
UNCOV
2126
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
2127
      return s;
×
2128

2129
    // no crossing is possible
UNCOV
2130
    return INFTY;
×
2131
  }
2132

2133
  const double p = b / a;
17,216,360✔
2134
  double D = p * p - c / a;
17,216,360✔
2135

2136
  if (D < 0.0)
17,216,360✔
2137
    return INFTY;
4,979,540✔
2138

2139
  D = std::sqrt(D);
12,236,820✔
2140

2141
  // the solution -p-D is always smaller as -p+D : Check this one first
2142
  double s = -p - D;
12,236,820✔
2143
  // Check if solution is in positive direction of flight and has correct sign
2144
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
12,236,820✔
2145
    return s;
2,401,185✔
2146

2147
  s = -p + D;
9,835,635✔
2148
  // Check if solution is in positive direction of flight and has correct sign
2149
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
9,835,635✔
2150
    return s;
4,619,680✔
2151

2152
  return INFTY;
5,215,955✔
2153
}
2154

2155
double SphericalMesh::find_phi_crossing(
50,416,850✔
2156
  const Position& r, const Direction& u, double l, int shell) const
2157
{
2158
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2159
  if (full_phi_ && (shape_[2] == 1))
50,416,850✔
2160
    return INFTY;
32,258,660✔
2161

2162
  shell = sanitize_phi(shell);
18,158,190✔
2163

2164
  const double p0 = grid_[2][shell];
18,158,190✔
2165

2166
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
2167
  // => x(s) * cos(p0) = y(s) * sin(p0)
2168
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
2169
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
2170

2171
  const double c0 = std::cos(p0);
18,158,190✔
2172
  const double s0 = std::sin(p0);
18,158,190✔
2173

2174
  const double denominator = (u.x * s0 - u.y * c0);
18,158,190✔
2175

2176
  // Check if direction of flight is not parallel to phi surface
2177
  if (std::abs(denominator) > FP_PRECISION) {
18,158,190✔
2178
    const double s = -(r.x * s0 - r.y * c0) / denominator;
18,051,830✔
2179
    // Check if solution is in positive direction of flight and crosses the
2180
    // correct phi surface (not -phi)
2181
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
18,051,830✔
2182
      return s;
7,990,660✔
2183
  }
2184

2185
  return INFTY;
10,167,530✔
2186
}
2187

2188
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
150,757,125✔
2189
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2190
  double l) const
2191
{
2192

2193
  if (i == 0) {
150,757,125✔
2194
    return std::min(
100,701,520✔
2195
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
100,701,520✔
2196
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
201,403,040✔
2197

2198
  } else if (i == 1) {
50,055,605✔
2199
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
24,847,180✔
2200
                      find_theta_crossing(r0, u, l, ijk[i])),
24,847,180✔
2201
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
24,847,180✔
2202
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
49,694,360✔
2203

2204
  } else {
2205
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
25,208,425✔
2206
                      find_phi_crossing(r0, u, l, ijk[i])),
25,208,425✔
2207
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
25,208,425✔
2208
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
50,416,850✔
2209
  }
2210
}
2211

2212
int SphericalMesh::set_grid()
172✔
2213
{
2214
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
172✔
2215
    static_cast<int>(grid_[1].size()) - 1,
172✔
2216
    static_cast<int>(grid_[2].size()) - 1};
172✔
2217

2218
  for (const auto& g : grid_) {
688✔
2219
    if (g.size() < 2) {
516!
UNCOV
2220
      set_errmsg("x-, y-, and z- grids for spherical meshes "
×
2221
                 "must each have at least 2 points");
UNCOV
2222
      return OPENMC_E_INVALID_ARGUMENT;
×
2223
    }
2224
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
516✔
2225
        g.end()) {
1,032!
UNCOV
2226
      set_errmsg("Values in for r-, theta-, and phi- grids for "
×
2227
                 "spherical meshes must be sorted and unique.");
UNCOV
2228
      return OPENMC_E_INVALID_ARGUMENT;
×
2229
    }
2230
    if (g.front() < 0.0) {
516!
UNCOV
2231
      set_errmsg("r-, theta-, and phi- grids for "
×
2232
                 "spherical meshes must start at v >= 0.");
UNCOV
2233
      return OPENMC_E_INVALID_ARGUMENT;
×
2234
    }
2235
  }
2236
  if (grid_[1].back() > PI) {
172!
UNCOV
2237
    set_errmsg("theta-grids for "
×
2238
               "spherical meshes must end with theta <= pi.");
2239

UNCOV
2240
    return OPENMC_E_INVALID_ARGUMENT;
×
2241
  }
2242
  if (grid_[2].back() > 2 * PI) {
172!
UNCOV
2243
    set_errmsg("phi-grids for "
×
2244
               "spherical meshes must end with phi <= 2*pi.");
UNCOV
2245
    return OPENMC_E_INVALID_ARGUMENT;
×
2246
  }
2247

2248
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
172!
2249
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
172✔
2250

2251
  double r = grid_[0].back();
172✔
2252
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
172✔
2253
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
172✔
2254

2255
  return 0;
172✔
2256
}
2257

2258
int SphericalMesh::get_index_in_direction(double r, int i) const
92,966,250✔
2259
{
2260
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
92,966,250✔
2261
}
2262

UNCOV
2263
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
×
2264
  Position plot_ll, Position plot_ur) const
2265
{
UNCOV
2266
  fatal_error("Plot of spherical Mesh not implemented");
×
2267

2268
  // Figure out which axes lie in the plane of the plot.
2269
  array<vector<double>, 2> axis_lines;
2270
  return {axis_lines[0], axis_lines[1]};
2271
}
2272

2273
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
145✔
2274
{
2275
  write_dataset(mesh_group, "r_grid", grid_[0]);
145✔
2276
  write_dataset(mesh_group, "theta_grid", grid_[1]);
145✔
2277
  write_dataset(mesh_group, "phi_grid", grid_[2]);
145✔
2278
  write_dataset(mesh_group, "origin", origin_);
145✔
2279
}
145✔
2280

2281
double SphericalMesh::volume(const MeshIndex& ijk) const
425✔
2282
{
2283
  double r_i = grid_[0][ijk[0] - 1];
425✔
2284
  double r_o = grid_[0][ijk[0]];
425✔
2285

2286
  double theta_i = grid_[1][ijk[1] - 1];
425✔
2287
  double theta_o = grid_[1][ijk[1]];
425✔
2288

2289
  double phi_i = grid_[2][ijk[2] - 1];
425✔
2290
  double phi_o = grid_[2][ijk[2]];
425✔
2291

2292
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
425✔
2293
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
425✔
2294
}
2295

2296
//==============================================================================
2297
// Helper functions for the C API
2298
//==============================================================================
2299

2300
int check_mesh(int32_t index)
2,880✔
2301
{
2302
  if (index < 0 || index >= model::meshes.size()) {
2,880!
UNCOV
2303
    set_errmsg("Index in meshes array is out of bounds.");
×
2304
    return OPENMC_E_OUT_OF_BOUNDS;
×
2305
  }
2306
  return 0;
2,880✔
2307
}
2308

2309
template<class T>
2310
int check_mesh_type(int32_t index)
500✔
2311
{
2312
  if (int err = check_mesh(index))
500!
UNCOV
2313
    return err;
×
2314

2315
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
500!
2316
  if (!mesh) {
500!
UNCOV
2317
    set_errmsg("This function is not valid for input mesh.");
×
2318
    return OPENMC_E_INVALID_TYPE;
×
2319
  }
2320
  return 0;
500✔
2321
}
2322

2323
template<class T>
2324
bool is_mesh_type(int32_t index)
2325
{
2326
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2327
  return mesh;
2328
}
2329

2330
//==============================================================================
2331
// C API functions
2332
//==============================================================================
2333

2334
// Return the type of mesh as a C string
2335
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
665✔
2336
{
2337
  if (int err = check_mesh(index))
665!
UNCOV
2338
    return err;
×
2339

2340
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
665✔
2341

2342
  return 0;
665✔
2343
}
2344

2345
//! Extend the meshes array by n elements
2346
extern "C" int openmc_extend_meshes(
115✔
2347
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2348
{
2349
  if (index_start)
115!
2350
    *index_start = model::meshes.size();
115✔
2351
  std::string mesh_type;
115✔
2352

2353
  for (int i = 0; i < n; ++i) {
230✔
2354
    if (RegularMesh::mesh_type == type) {
115✔
2355
      model::meshes.push_back(make_unique<RegularMesh>());
75✔
2356
    } else if (RectilinearMesh::mesh_type == type) {
40✔
2357
      model::meshes.push_back(make_unique<RectilinearMesh>());
20✔
2358
    } else if (CylindricalMesh::mesh_type == type) {
20✔
2359
      model::meshes.push_back(make_unique<CylindricalMesh>());
10✔
2360
    } else if (SphericalMesh::mesh_type == type) {
10!
2361
      model::meshes.push_back(make_unique<SphericalMesh>());
10✔
2362
    } else {
UNCOV
2363
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2364
    }
2365
  }
2366
  if (index_end)
115!
UNCOV
2367
    *index_end = model::meshes.size() - 1;
×
2368

2369
  return 0;
115✔
2370
}
115✔
2371

2372
//! Adds a new unstructured mesh to OpenMC
UNCOV
2373
extern "C" int openmc_add_unstructured_mesh(
×
2374
  const char filename[], const char library[], int* id)
2375
{
UNCOV
2376
  std::string lib_name(library);
×
2377
  std::string mesh_file(filename);
×
2378
  bool valid_lib = false;
×
2379

2380
#ifdef OPENMC_DAGMC_ENABLED
2381
  if (lib_name == MOABMesh::mesh_lib_type) {
2382
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
2383
    valid_lib = true;
2384
  }
2385
#endif
2386

2387
#ifdef OPENMC_LIBMESH_ENABLED
2388
  if (lib_name == LibMesh::mesh_lib_type) {
×
2389
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
×
2390
    valid_lib = true;
2391
  }
2392
#endif
2393

UNCOV
2394
  if (!valid_lib) {
×
2395
    set_errmsg(fmt::format("Mesh library {} is not supported "
×
2396
                           "by this build of OpenMC",
2397
      lib_name));
UNCOV
2398
    return OPENMC_E_INVALID_ARGUMENT;
×
2399
  }
2400

2401
  // auto-assign new ID
UNCOV
2402
  model::meshes.back()->set_id(-1);
×
2403
  *id = model::meshes.back()->id_;
×
2404

UNCOV
2405
  return 0;
×
2406
}
×
2407

2408
//! Return the index in the meshes array of a mesh with a given ID
2409
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
195✔
2410
{
2411
  auto pair = model::mesh_map.find(id);
195✔
2412
  if (pair == model::mesh_map.end()) {
195!
UNCOV
2413
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
×
2414
    return OPENMC_E_INVALID_ID;
×
2415
  }
2416
  *index = pair->second;
195✔
2417
  return 0;
195✔
2418
}
2419

2420
//! Return the ID of a mesh
2421
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
1,270✔
2422
{
2423
  if (int err = check_mesh(index))
1,270!
UNCOV
2424
    return err;
×
2425
  *id = model::meshes[index]->id_;
1,270✔
2426
  return 0;
1,270✔
2427
}
2428

2429
//! Set the ID of a mesh
2430
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
115✔
2431
{
2432
  if (int err = check_mesh(index))
115!
UNCOV
2433
    return err;
×
2434
  model::meshes[index]->id_ = id;
115✔
2435
  model::mesh_map[id] = index;
115✔
2436
  return 0;
115✔
2437
}
2438

2439
//! Get the number of elements in a mesh
2440
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
120✔
2441
{
2442
  if (int err = check_mesh(index))
120!
UNCOV
2443
    return err;
×
2444
  *n = model::meshes[index]->n_bins();
120✔
2445
  return 0;
120✔
2446
}
2447

2448
//! Get the volume of each element in the mesh
2449
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
40✔
2450
{
2451
  if (int err = check_mesh(index))
40!
UNCOV
2452
    return err;
×
2453
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
440✔
2454
    volumes[i] = model::meshes[index]->volume(i);
400✔
2455
  }
2456
  return 0;
40✔
2457
}
2458

2459
//! Get the bounding box of a mesh
2460
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
70✔
2461
{
2462
  if (int err = check_mesh(index))
70!
UNCOV
2463
    return err;
×
2464

2465
  BoundingBox bbox = model::meshes[index]->bounding_box();
70✔
2466

2467
  // set lower left corner values
2468
  ll[0] = bbox.min.x;
70✔
2469
  ll[1] = bbox.min.y;
70✔
2470
  ll[2] = bbox.min.z;
70✔
2471

2472
  // set upper right corner values
2473
  ur[0] = bbox.max.x;
70✔
2474
  ur[1] = bbox.max.y;
70✔
2475
  ur[2] = bbox.max.z;
70✔
2476
  return 0;
70✔
2477
}
2478

2479
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
80✔
2480
  int nz, int table_size, int32_t* materials, double* volumes)
2481
{
2482
  if (int err = check_mesh(index))
80!
UNCOV
2483
    return err;
×
2484

2485
  try {
2486
    model::meshes[index]->material_volumes(
80✔
2487
      nx, ny, nz, table_size, materials, volumes);
2488
  } catch (const std::exception& e) {
5!
2489
    set_errmsg(e.what());
5✔
2490
    if (starts_with(e.what(), "Mesh")) {
5!
2491
      return OPENMC_E_GEOMETRY;
5✔
2492
    } else {
UNCOV
2493
      return OPENMC_E_ALLOCATE;
×
2494
    }
2495
  }
5✔
2496

2497
  return 0;
75✔
2498
}
2499

2500
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
20✔
2501
  Position width, int basis, int* pixels, int32_t* data)
2502
{
2503
  if (int err = check_mesh(index))
20!
UNCOV
2504
    return err;
×
2505
  const auto& mesh = model::meshes[index].get();
20✔
2506

2507
  int pixel_width = pixels[0];
20✔
2508
  int pixel_height = pixels[1];
20✔
2509

2510
  // get pixel size
2511
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
20✔
2512
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
20✔
2513

2514
  // setup basis indices and initial position centered on pixel
2515
  int in_i, out_i;
2516
  Position xyz = origin;
20✔
2517
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2518
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
20✔
2519
  switch (basis_enum) {
20!
2520
  case PlotBasis::xy:
20✔
2521
    in_i = 0;
20✔
2522
    out_i = 1;
20✔
2523
    break;
20✔
UNCOV
2524
  case PlotBasis::xz:
×
2525
    in_i = 0;
×
2526
    out_i = 2;
×
2527
    break;
×
2528
  case PlotBasis::yz:
×
2529
    in_i = 1;
×
2530
    out_i = 2;
×
2531
    break;
×
2532
  default:
×
2533
    UNREACHABLE();
×
2534
  }
2535

2536
  // set initial position
2537
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
20✔
2538
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
20✔
2539

2540
#pragma omp parallel
8✔
2541
  {
2542
    Position r = xyz;
12✔
2543

2544
#pragma omp for
2545
    for (int y = 0; y < pixel_height; y++) {
252✔
2546
      r[out_i] = xyz[out_i] - out_pixel * y;
240✔
2547
      for (int x = 0; x < pixel_width; x++) {
5,040✔
2548
        r[in_i] = xyz[in_i] + in_pixel * x;
4,800✔
2549
        data[pixel_width * y + x] = mesh->get_bin(r);
4,800✔
2550
      }
2551
    }
2552
  }
2553

2554
  return 0;
20✔
2555
}
2556

2557
//! Get the dimension of a regular mesh
2558
extern "C" int openmc_regular_mesh_get_dimension(
5✔
2559
  int32_t index, int** dims, int* n)
2560
{
2561
  if (int err = check_mesh_type<RegularMesh>(index))
5!
UNCOV
2562
    return err;
×
2563
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
5!
2564
  *dims = mesh->shape_.data();
5✔
2565
  *n = mesh->n_dimension_;
5✔
2566
  return 0;
5✔
2567
}
2568

2569
//! Set the dimension of a regular mesh
2570
extern "C" int openmc_regular_mesh_set_dimension(
85✔
2571
  int32_t index, int n, const int* dims)
2572
{
2573
  if (int err = check_mesh_type<RegularMesh>(index))
85!
UNCOV
2574
    return err;
×
2575
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
85!
2576

2577
  // Copy dimension
2578
  mesh->n_dimension_ = n;
85✔
2579
  std::copy(dims, dims + n, mesh->shape_.begin());
85✔
2580
  return 0;
85✔
2581
}
2582

2583
//! Get the regular mesh parameters
2584
extern "C" int openmc_regular_mesh_get_params(
95✔
2585
  int32_t index, double** ll, double** ur, double** width, int* n)
2586
{
2587
  if (int err = check_mesh_type<RegularMesh>(index))
95!
UNCOV
2588
    return err;
×
2589
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
95!
2590

2591
  if (m->lower_left_.dimension() == 0) {
95!
UNCOV
2592
    set_errmsg("Mesh parameters have not been set.");
×
2593
    return OPENMC_E_ALLOCATE;
×
2594
  }
2595

2596
  *ll = m->lower_left_.data();
95✔
2597
  *ur = m->upper_right_.data();
95✔
2598
  *width = m->width_.data();
95✔
2599
  *n = m->n_dimension_;
95✔
2600
  return 0;
95✔
2601
}
2602

2603
//! Set the regular mesh parameters
2604
extern "C" int openmc_regular_mesh_set_params(
100✔
2605
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2606
{
2607
  if (int err = check_mesh_type<RegularMesh>(index))
100!
UNCOV
2608
    return err;
×
2609
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
100!
2610

2611
  if (m->n_dimension_ == -1) {
100!
UNCOV
2612
    set_errmsg("Need to set mesh dimension before setting parameters.");
×
2613
    return OPENMC_E_UNASSIGNED;
×
2614
  }
2615

2616
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
100✔
2617
  if (ll && ur) {
100✔
2618
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
90✔
2619
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
90✔
2620
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
90✔
2621
  } else if (ll && width) {
10!
2622
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
5✔
2623
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
5✔
2624
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
5✔
2625
  } else if (ur && width) {
5!
2626
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
5✔
2627
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
5✔
2628
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
5✔
2629
  } else {
UNCOV
2630
    set_errmsg("At least two parameters must be specified.");
×
2631
    return OPENMC_E_INVALID_ARGUMENT;
×
2632
  }
2633

2634
  // Set material volumes
2635

2636
  // TODO: incorporate this into method in RegularMesh that can be called from
2637
  // here and from constructor
2638
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
100✔
2639
  m->element_volume_ = 1.0;
100✔
2640
  for (int i = 0; i < m->n_dimension_; i++) {
400✔
2641
    m->element_volume_ *= m->width_[i];
300✔
2642
  }
2643

2644
  return 0;
100✔
2645
}
100✔
2646

2647
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
2648
template<class C>
2649
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
40✔
2650
  const int nx, const double* grid_y, const int ny, const double* grid_z,
2651
  const int nz)
2652
{
2653
  if (int err = check_mesh_type<C>(index))
40!
UNCOV
2654
    return err;
×
2655

2656
  C* m = dynamic_cast<C*>(model::meshes[index].get());
40!
2657

2658
  m->n_dimension_ = 3;
40✔
2659

2660
  m->grid_[0].reserve(nx);
40✔
2661
  m->grid_[1].reserve(ny);
40✔
2662
  m->grid_[2].reserve(nz);
40✔
2663

2664
  for (int i = 0; i < nx; i++) {
260✔
2665
    m->grid_[0].push_back(grid_x[i]);
220✔
2666
  }
2667
  for (int i = 0; i < ny; i++) {
155✔
2668
    m->grid_[1].push_back(grid_y[i]);
115✔
2669
  }
2670
  for (int i = 0; i < nz; i++) {
145✔
2671
    m->grid_[2].push_back(grid_z[i]);
105✔
2672
  }
2673

2674
  int err = m->set_grid();
40✔
2675
  return err;
40✔
2676
}
2677

2678
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2679
template<class C>
2680
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
175✔
2681
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2682
{
2683
  if (int err = check_mesh_type<C>(index))
175!
UNCOV
2684
    return err;
×
2685
  C* m = dynamic_cast<C*>(model::meshes[index].get());
175!
2686

2687
  if (m->lower_left_.dimension() == 0) {
175!
UNCOV
2688
    set_errmsg("Mesh parameters have not been set.");
×
2689
    return OPENMC_E_ALLOCATE;
×
2690
  }
2691

2692
  *grid_x = m->grid_[0].data();
175✔
2693
  *nx = m->grid_[0].size();
175✔
2694
  *grid_y = m->grid_[1].data();
175✔
2695
  *ny = m->grid_[1].size();
175✔
2696
  *grid_z = m->grid_[2].data();
175✔
2697
  *nz = m->grid_[2].size();
175✔
2698

2699
  return 0;
175✔
2700
}
2701

2702
//! Get the rectilinear mesh grid
2703
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
65✔
2704
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2705
{
2706
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
65✔
2707
    index, grid_x, nx, grid_y, ny, grid_z, nz);
65✔
2708
}
2709

2710
//! Set the rectilienar mesh parameters
2711
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
20✔
2712
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2713
  const double* grid_z, const int nz)
2714
{
2715
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
20✔
2716
    index, grid_x, nx, grid_y, ny, grid_z, nz);
20✔
2717
}
2718

2719
//! Get the cylindrical mesh grid
2720
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
55✔
2721
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2722
{
2723
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
55✔
2724
    index, grid_x, nx, grid_y, ny, grid_z, nz);
55✔
2725
}
2726

2727
//! Set the cylindrical mesh parameters
2728
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
10✔
2729
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2730
  const double* grid_z, const int nz)
2731
{
2732
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
10✔
2733
    index, grid_x, nx, grid_y, ny, grid_z, nz);
10✔
2734
}
2735

2736
//! Get the spherical mesh grid
2737
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
55✔
2738
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2739
{
2740

2741
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
55✔
2742
    index, grid_x, nx, grid_y, ny, grid_z, nz);
55✔
2743
  ;
2744
}
2745

2746
//! Set the spherical mesh parameters
2747
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
10✔
2748
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2749
  const double* grid_z, const int nz)
2750
{
2751
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
10✔
2752
    index, grid_x, nx, grid_y, ny, grid_z, nz);
10✔
2753
}
2754

2755
#ifdef OPENMC_DAGMC_ENABLED
2756

2757
const std::string MOABMesh::mesh_lib_type = "moab";
2758

2759
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
2760
{
2761
  initialize();
2762
}
2763

2764
MOABMesh::MOABMesh(hid_t group) : UnstructuredMesh(group)
2765
{
2766
  initialize();
2767
}
2768

2769
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2770
  : UnstructuredMesh()
2771
{
2772
  n_dimension_ = 3;
2773
  filename_ = filename;
2774
  set_length_multiplier(length_multiplier);
2775
  initialize();
2776
}
2777

2778
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
2779
{
2780
  mbi_ = external_mbi;
2781
  filename_ = "unknown (external file)";
2782
  this->initialize();
2783
}
2784

2785
void MOABMesh::initialize()
2786
{
2787

2788
  // Create the MOAB interface and load data from file
2789
  this->create_interface();
2790

2791
  // Initialise MOAB error code
2792
  moab::ErrorCode rval = moab::MB_SUCCESS;
2793

2794
  // Set the dimension
2795
  n_dimension_ = 3;
2796

2797
  // set member range of tetrahedral entities
2798
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
2799
  if (rval != moab::MB_SUCCESS) {
2800
    fatal_error("Failed to get all tetrahedral elements");
2801
  }
2802

2803
  if (!ehs_.all_of_type(moab::MBTET)) {
2804
    warning("Non-tetrahedral elements found in unstructured "
2805
            "mesh file: " +
2806
            filename_);
2807
  }
2808

2809
  // set member range of vertices
2810
  int vertex_dim = 0;
2811
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
2812
  if (rval != moab::MB_SUCCESS) {
2813
    fatal_error("Failed to get all vertex handles");
2814
  }
2815

2816
  // make an entity set for all tetrahedra
2817
  // this is used for convenience later in output
2818
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
2819
  if (rval != moab::MB_SUCCESS) {
2820
    fatal_error("Failed to create an entity set for the tetrahedral elements");
2821
  }
2822

2823
  rval = mbi_->add_entities(tetset_, ehs_);
2824
  if (rval != moab::MB_SUCCESS) {
2825
    fatal_error("Failed to add tetrahedra to an entity set.");
2826
  }
2827

2828
  if (length_multiplier_ > 0.0) {
2829
    // get the connectivity of all tets
2830
    moab::Range adj;
2831
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
2832
    if (rval != moab::MB_SUCCESS) {
2833
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
2834
    }
2835
    // scale all vertex coords by multiplier (done individually so not all
2836
    // coordinates are in memory twice at once)
2837
    for (auto vert : adj) {
2838
      // retrieve coords
2839
      std::array<double, 3> coord;
2840
      rval = mbi_->get_coords(&vert, 1, coord.data());
2841
      if (rval != moab::MB_SUCCESS) {
2842
        fatal_error("Could not get coordinates of vertex.");
2843
      }
2844
      // scale coords
2845
      for (auto& c : coord) {
2846
        c *= length_multiplier_;
2847
      }
2848
      // set new coords
2849
      rval = mbi_->set_coords(&vert, 1, coord.data());
2850
      if (rval != moab::MB_SUCCESS) {
2851
        fatal_error("Failed to set new vertex coordinates");
2852
      }
2853
    }
2854
  }
2855

2856
  // Determine bounds of mesh
2857
  this->determine_bounds();
2858
}
2859

2860
void MOABMesh::prepare_for_point_location()
2861
{
2862
  // if the KDTree has already been constructed, do nothing
2863
  if (kdtree_)
2864
    return;
2865

2866
  // build acceleration data structures
2867
  compute_barycentric_data(ehs_);
2868
  build_kdtree(ehs_);
2869
}
2870

2871
void MOABMesh::create_interface()
2872
{
2873
  // Do not create a MOAB instance if one is already in memory
2874
  if (mbi_)
2875
    return;
2876

2877
  // create MOAB instance
2878
  mbi_ = std::make_shared<moab::Core>();
2879

2880
  // load unstructured mesh file
2881
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2882
  if (rval != moab::MB_SUCCESS) {
2883
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
2884
  }
2885
}
2886

2887
void MOABMesh::build_kdtree(const moab::Range& all_tets)
2888
{
2889
  moab::Range all_tris;
2890
  int adj_dim = 2;
2891
  write_message("Getting tet adjacencies...", 7);
2892
  moab::ErrorCode rval = mbi_->get_adjacencies(
2893
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2894
  if (rval != moab::MB_SUCCESS) {
2895
    fatal_error("Failed to get adjacent triangles for tets");
2896
  }
2897

2898
  if (!all_tris.all_of_type(moab::MBTRI)) {
2899
    warning("Non-triangle elements found in tet adjacencies in "
2900
            "unstructured mesh file: " +
2901
            filename_);
2902
  }
2903

2904
  // combine into one range
2905
  moab::Range all_tets_and_tris;
2906
  all_tets_and_tris.merge(all_tets);
2907
  all_tets_and_tris.merge(all_tris);
2908

2909
  // create a kd-tree instance
2910
  write_message(
2911
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
2912
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
2913

2914
  // Determine what options to use
2915
  std::ostringstream options_stream;
2916
  if (options_.empty()) {
2917
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
2918
  } else {
2919
    options_stream << options_;
2920
  }
2921
  moab::FileOptions file_opts(options_stream.str().c_str());
2922

2923
  // Build the k-d tree
2924
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2925
  if (rval != moab::MB_SUCCESS) {
2926
    fatal_error("Failed to construct KDTree for the "
2927
                "unstructured mesh file: " +
2928
                filename_);
2929
  }
2930
}
2931

2932
void MOABMesh::intersect_track(const moab::CartVect& start,
2933
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2934
{
2935
  hits.clear();
2936

2937
  moab::ErrorCode rval;
2938
  vector<moab::EntityHandle> tris;
2939
  // get all intersections with triangles in the tet mesh
2940
  // (distances are relative to the start point, not the previous
2941
  // intersection)
2942
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
2943
    dir.array(), start.array(), tris, hits, 0, track_len);
2944
  if (rval != moab::MB_SUCCESS) {
2945
    fatal_error(
2946
      "Failed to compute intersections on unstructured mesh: " + filename_);
2947
  }
2948

2949
  // remove duplicate intersection distances
2950
  std::unique(hits.begin(), hits.end());
2951

2952
  // sorts by first component of std::pair by default
2953
  std::sort(hits.begin(), hits.end());
2954
}
2955

2956
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2957
  vector<int>& bins, vector<double>& lengths) const
2958
{
2959
  moab::CartVect start(r0.x, r0.y, r0.z);
2960
  moab::CartVect end(r1.x, r1.y, r1.z);
2961
  moab::CartVect dir(u.x, u.y, u.z);
2962
  dir.normalize();
2963

2964
  double track_len = (end - start).length();
2965
  if (track_len == 0.0)
2966
    return;
2967

2968
  start -= TINY_BIT * dir;
2969
  end += TINY_BIT * dir;
2970

2971
  vector<double> hits;
2972
  intersect_track(start, dir, track_len, hits);
2973

2974
  bins.clear();
2975
  lengths.clear();
2976

2977
  // if there are no intersections the track may lie entirely
2978
  // within a single tet. If this is the case, apply entire
2979
  // score to that tet and return.
2980
  if (hits.size() == 0) {
2981
    Position midpoint = r0 + u * (track_len * 0.5);
2982
    int bin = this->get_bin(midpoint);
2983
    if (bin != -1) {
2984
      bins.push_back(bin);
2985
      lengths.push_back(1.0);
2986
    }
2987
    return;
2988
  }
2989

2990
  // for each segment in the set of tracks, try to look up a tet
2991
  // at the midpoint of the segment
2992
  Position current = r0;
2993
  double last_dist = 0.0;
2994
  for (const auto& hit : hits) {
2995
    // get the segment length
2996
    double segment_length = hit - last_dist;
2997
    last_dist = hit;
2998
    // find the midpoint of this segment
2999
    Position midpoint = current + u * (segment_length * 0.5);
3000
    // try to find a tet for this position
3001
    int bin = this->get_bin(midpoint);
3002

3003
    // determine the start point for this segment
3004
    current = r0 + u * hit;
3005

3006
    if (bin == -1) {
3007
      continue;
3008
    }
3009

3010
    bins.push_back(bin);
3011
    lengths.push_back(segment_length / track_len);
3012
  }
3013

3014
  // tally remaining portion of track after last hit if
3015
  // the last segment of the track is in the mesh but doesn't
3016
  // reach the other side of the tet
3017
  if (hits.back() < track_len) {
3018
    Position segment_start = r0 + u * hits.back();
3019
    double segment_length = track_len - hits.back();
3020
    Position midpoint = segment_start + u * (segment_length * 0.5);
3021
    int bin = this->get_bin(midpoint);
3022
    if (bin != -1) {
3023
      bins.push_back(bin);
3024
      lengths.push_back(segment_length / track_len);
3025
    }
3026
  }
3027
};
3028

3029
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
3030
{
3031
  moab::CartVect pos(r.x, r.y, r.z);
3032
  // find the leaf of the kd-tree for this position
3033
  moab::AdaptiveKDTreeIter kdtree_iter;
3034
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
3035
  if (rval != moab::MB_SUCCESS) {
3036
    return 0;
3037
  }
3038

3039
  // retrieve the tet elements of this leaf
3040
  moab::EntityHandle leaf = kdtree_iter.handle();
3041
  moab::Range tets;
3042
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
3043
  if (rval != moab::MB_SUCCESS) {
3044
    warning("MOAB error finding tets.");
3045
  }
3046

3047
  // loop over the tets in this leaf, returning the containing tet if found
3048
  for (const auto& tet : tets) {
3049
    if (point_in_tet(pos, tet)) {
3050
      return tet;
3051
    }
3052
  }
3053

3054
  // if no tet is found, return an invalid handle
3055
  return 0;
3056
}
3057

3058
double MOABMesh::volume(int bin) const
3059
{
3060
  return tet_volume(get_ent_handle_from_bin(bin));
3061
}
3062

3063
std::string MOABMesh::library() const
3064
{
3065
  return mesh_lib_type;
3066
}
3067

3068
// Sample position within a tet for MOAB type tets
3069
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
3070
{
3071

3072
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
3073

3074
  // Get vertex coordinates for MOAB tet
3075
  const moab::EntityHandle* conn1;
3076
  int conn1_size;
3077
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
3078
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
3079
    fatal_error(fmt::format(
3080
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
3081
      conn1_size));
3082
  }
3083
  moab::CartVect p[4];
3084
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
3085
  if (rval != moab::MB_SUCCESS) {
3086
    fatal_error("Failed to get tet coords");
3087
  }
3088

3089
  std::array<Position, 4> tet_verts;
3090
  for (int i = 0; i < 4; i++) {
3091
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
3092
  }
3093
  // Samples position within tet using Barycentric stuff
3094
  return this->sample_tet(tet_verts, seed);
3095
}
3096

3097
double MOABMesh::tet_volume(moab::EntityHandle tet) const
3098
{
3099
  vector<moab::EntityHandle> conn;
3100
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
3101
  if (rval != moab::MB_SUCCESS) {
3102
    fatal_error("Failed to get tet connectivity");
3103
  }
3104

3105
  moab::CartVect p[4];
3106
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
3107
  if (rval != moab::MB_SUCCESS) {
3108
    fatal_error("Failed to get tet coords");
3109
  }
3110

3111
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
3112
}
3113

3114
int MOABMesh::get_bin(Position r) const
3115
{
3116
  moab::EntityHandle tet = get_tet(r);
3117
  if (tet == 0) {
3118
    return -1;
3119
  } else {
3120
    return get_bin_from_ent_handle(tet);
3121
  }
3122
}
3123

3124
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
3125
{
3126
  moab::ErrorCode rval;
3127

3128
  baryc_data_.clear();
3129
  baryc_data_.resize(tets.size());
3130

3131
  // compute the barycentric data for each tet element
3132
  // and store it as a 3x3 matrix
3133
  for (auto& tet : tets) {
3134
    vector<moab::EntityHandle> verts;
3135
    rval = mbi_->get_connectivity(&tet, 1, verts);
3136
    if (rval != moab::MB_SUCCESS) {
3137
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
3138
    }
3139

3140
    moab::CartVect p[4];
3141
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
3142
    if (rval != moab::MB_SUCCESS) {
3143
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
3144
    }
3145

3146
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
3147

3148
    // invert now to avoid this cost later
3149
    a = a.transpose().inverse();
3150
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
3151
  }
3152
}
3153

3154
bool MOABMesh::point_in_tet(
3155
  const moab::CartVect& r, moab::EntityHandle tet) const
3156
{
3157

3158
  moab::ErrorCode rval;
3159

3160
  // get tet vertices
3161
  vector<moab::EntityHandle> verts;
3162
  rval = mbi_->get_connectivity(&tet, 1, verts);
3163
  if (rval != moab::MB_SUCCESS) {
3164
    warning("Failed to get vertices of tet in umesh: " + filename_);
3165
    return false;
3166
  }
3167

3168
  // first vertex is used as a reference point for the barycentric data -
3169
  // retrieve its coordinates
3170
  moab::CartVect p_zero;
3171
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
3172
  if (rval != moab::MB_SUCCESS) {
3173
    warning("Failed to get coordinates of a vertex in "
3174
            "unstructured mesh: " +
3175
            filename_);
3176
    return false;
3177
  }
3178

3179
  // look up barycentric data
3180
  int idx = get_bin_from_ent_handle(tet);
3181
  const moab::Matrix3& a_inv = baryc_data_[idx];
3182

3183
  moab::CartVect bary_coords = a_inv * (r - p_zero);
3184

3185
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
3186
          bary_coords[2] >= 0.0 &&
3187
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
3188
}
3189

3190
int MOABMesh::get_bin_from_index(int idx) const
3191
{
3192
  if (idx >= n_bins()) {
3193
    fatal_error(fmt::format("Invalid bin index: {}", idx));
3194
  }
3195
  return ehs_[idx] - ehs_[0];
3196
}
3197

3198
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
3199
{
3200
  int bin = get_bin(r);
3201
  *in_mesh = bin != -1;
3202
  return bin;
3203
}
3204

3205
int MOABMesh::get_index_from_bin(int bin) const
3206
{
3207
  return bin;
3208
}
3209

3210
std::pair<vector<double>, vector<double>> MOABMesh::plot(
3211
  Position plot_ll, Position plot_ur) const
3212
{
3213
  // TODO: Implement mesh lines
3214
  return {};
3215
}
3216

3217
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
3218
{
3219
  int idx = vert - verts_[0];
3220
  if (idx >= n_vertices()) {
3221
    fatal_error(
3222
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
3223
  }
3224
  return idx;
3225
}
3226

3227
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
3228
{
3229
  int bin = eh - ehs_[0];
3230
  if (bin >= n_bins()) {
3231
    fatal_error(fmt::format("Invalid bin: {}", bin));
3232
  }
3233
  return bin;
3234
}
3235

3236
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
3237
{
3238
  if (bin >= n_bins()) {
3239
    fatal_error(fmt::format("Invalid bin index: ", bin));
3240
  }
3241
  return ehs_[0] + bin;
3242
}
3243

3244
int MOABMesh::n_bins() const
3245
{
3246
  return ehs_.size();
3247
}
3248

3249
int MOABMesh::n_surface_bins() const
3250
{
3251
  // collect all triangles in the set of tets for this mesh
3252
  moab::Range tris;
3253
  moab::ErrorCode rval;
3254
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
3255
  if (rval != moab::MB_SUCCESS) {
3256
    warning("Failed to get all triangles in the mesh instance");
3257
    return -1;
3258
  }
3259
  return 2 * tris.size();
3260
}
3261

3262
Position MOABMesh::centroid(int bin) const
3263
{
3264
  moab::ErrorCode rval;
3265

3266
  auto tet = this->get_ent_handle_from_bin(bin);
3267

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

3276
  // get the coordinates
3277
  vector<moab::CartVect> coords(conn.size());
3278
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
3279
  if (rval != moab::MB_SUCCESS) {
3280
    warning("Failed to get the coordinates of a mesh element.");
3281
    return {};
3282
  }
3283

3284
  // compute the centroid of the element vertices
3285
  moab::CartVect centroid(0.0, 0.0, 0.0);
3286
  for (const auto& coord : coords) {
3287
    centroid += coord;
3288
  }
3289
  centroid /= double(coords.size());
3290

3291
  return {centroid[0], centroid[1], centroid[2]};
3292
}
3293

3294
int MOABMesh::n_vertices() const
3295
{
3296
  return verts_.size();
3297
}
3298

3299
Position MOABMesh::vertex(int id) const
3300
{
3301

3302
  moab::ErrorCode rval;
3303

3304
  moab::EntityHandle vert = verts_[id];
3305

3306
  moab::CartVect coords;
3307
  rval = mbi_->get_coords(&vert, 1, coords.array());
3308
  if (rval != moab::MB_SUCCESS) {
3309
    fatal_error("Failed to get the coordinates of a vertex.");
3310
  }
3311

3312
  return {coords[0], coords[1], coords[2]};
3313
}
3314

3315
std::vector<int> MOABMesh::connectivity(int bin) const
3316
{
3317
  moab::ErrorCode rval;
3318

3319
  auto tet = get_ent_handle_from_bin(bin);
3320

3321
  // look up the tet connectivity
3322
  vector<moab::EntityHandle> conn;
3323
  rval = mbi_->get_connectivity(&tet, 1, conn);
3324
  if (rval != moab::MB_SUCCESS) {
3325
    fatal_error("Failed to get connectivity of a mesh element.");
3326
    return {};
3327
  }
3328

3329
  std::vector<int> verts(4);
3330
  for (int i = 0; i < verts.size(); i++) {
3331
    verts[i] = get_vert_idx_from_handle(conn[i]);
3332
  }
3333

3334
  return verts;
3335
}
3336

3337
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
3338
  std::string score) const
3339
{
3340
  moab::ErrorCode rval;
3341
  // add a tag to the mesh
3342
  // all scores are treated as a single value
3343
  // with an uncertainty
3344
  moab::Tag value_tag;
3345

3346
  // create the value tag if not present and get handle
3347
  double default_val = 0.0;
3348
  auto val_string = score + "_mean";
3349
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
3350
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3351
  if (rval != moab::MB_SUCCESS) {
3352
    auto msg =
3353
      fmt::format("Could not create or retrieve the value tag for the score {}"
3354
                  " on unstructured mesh {}",
3355
        score, id_);
3356
    fatal_error(msg);
3357
  }
3358

3359
  // create the std dev tag if not present and get handle
3360
  moab::Tag error_tag;
3361
  std::string err_string = score + "_std_dev";
3362
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
3363
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3364
  if (rval != moab::MB_SUCCESS) {
3365
    auto msg =
3366
      fmt::format("Could not create or retrieve the error tag for the score {}"
3367
                  " on unstructured mesh {}",
3368
        score, id_);
3369
    fatal_error(msg);
3370
  }
3371

3372
  // return the populated tag handles
3373
  return {value_tag, error_tag};
3374
}
3375

3376
void MOABMesh::add_score(const std::string& score)
3377
{
3378
  auto score_tags = get_score_tags(score);
3379
  tag_names_.push_back(score);
3380
}
3381

3382
void MOABMesh::remove_scores()
3383
{
3384
  for (const auto& name : tag_names_) {
3385
    auto value_name = name + "_mean";
3386
    moab::Tag tag;
3387
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
3388
    if (rval != moab::MB_SUCCESS)
3389
      return;
3390

3391
    rval = mbi_->tag_delete(tag);
3392
    if (rval != moab::MB_SUCCESS) {
3393
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3394
                             " on unstructured mesh {}",
3395
        name, id_);
3396
      fatal_error(msg);
3397
    }
3398

3399
    auto std_dev_name = name + "_std_dev";
3400
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
3401
    if (rval != moab::MB_SUCCESS) {
3402
      auto msg =
3403
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3404
                    " on unstructured mesh {}",
3405
          name, id_);
3406
    }
3407

3408
    rval = mbi_->tag_delete(tag);
3409
    if (rval != moab::MB_SUCCESS) {
3410
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
3411
                             " on unstructured mesh {}",
3412
        name, id_);
3413
      fatal_error(msg);
3414
    }
3415
  }
3416
  tag_names_.clear();
3417
}
3418

3419
void MOABMesh::set_score_data(const std::string& score,
3420
  const vector<double>& values, const vector<double>& std_dev)
3421
{
3422
  auto score_tags = this->get_score_tags(score);
3423

3424
  moab::ErrorCode rval;
3425
  // set the score value
3426
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
3427
  if (rval != moab::MB_SUCCESS) {
3428
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
3429
                           "on unstructured mesh {}",
3430
      score, id_);
3431
    warning(msg);
3432
  }
3433

3434
  // set the error value
3435
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3436
  if (rval != moab::MB_SUCCESS) {
3437
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
3438
                           "on unstructured mesh {}",
3439
      score, id_);
3440
    warning(msg);
3441
  }
3442
}
3443

3444
void MOABMesh::write(const std::string& base_filename) const
3445
{
3446
  // add extension to the base name
3447
  auto filename = base_filename + ".vtk";
3448
  write_message(5, "Writing unstructured mesh {}...", filename);
3449
  filename = settings::path_output + filename;
3450

3451
  // write the tetrahedral elements of the mesh only
3452
  // to avoid clutter from zero-value data on other
3453
  // elements during visualization
3454
  moab::ErrorCode rval;
3455
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
3456
  if (rval != moab::MB_SUCCESS) {
3457
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
3458
    warning(msg);
3459
  }
3460
}
3461

3462
#endif
3463

3464
#ifdef OPENMC_LIBMESH_ENABLED
3465

3466
const std::string LibMesh::mesh_lib_type = "libmesh";
3467

3468
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
23✔
3469
{
3470
  // filename_ and length_multiplier_ will already be set by the
3471
  // UnstructuredMesh constructor
3472
  set_mesh_pointer_from_filename(filename_);
23✔
3473
  set_length_multiplier(length_multiplier_);
23✔
3474
  initialize();
23✔
3475
}
23✔
3476

3477
LibMesh::LibMesh(hid_t group) : UnstructuredMesh(group)
×
3478
{
3479
  // filename_ and length_multiplier_ will already be set by the
3480
  // UnstructuredMesh constructor
3481
  set_mesh_pointer_from_filename(filename_);
×
3482
  set_length_multiplier(length_multiplier_);
×
3483
  initialize();
×
3484
}
3485

3486
// create the mesh from a pointer to a libMesh Mesh
3487
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
×
3488
{
3489
  if (!input_mesh.is_replicated()) {
×
3490
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
3491
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
3492
  }
3493

3494
  m_ = &input_mesh;
3495
  set_length_multiplier(length_multiplier);
×
3496
  initialize();
×
3497
}
3498

3499
// create the mesh from an input file
3500
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
×
3501
{
3502
  n_dimension_ = 3;
3503
  set_mesh_pointer_from_filename(filename);
×
3504
  set_length_multiplier(length_multiplier);
×
3505
  initialize();
×
3506
}
3507

3508
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
23✔
3509
{
3510
  filename_ = filename;
23✔
3511
  unique_m_ =
3512
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
23✔
3513
  m_ = unique_m_.get();
23✔
3514
  m_->read(filename_);
23✔
3515
}
23✔
3516

3517
// build a libMesh equation system for storing values
3518
void LibMesh::build_eqn_sys()
15✔
3519
{
3520
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
30✔
3521
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
15✔
3522
  libMesh::ExplicitSystem& eq_sys =
3523
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
15✔
3524
}
15✔
3525

3526
// intialize from mesh file
3527
void LibMesh::initialize()
23✔
3528
{
3529
  if (!settings::libmesh_comm) {
23!
3530
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3531
                "communicator.");
3532
  }
3533

3534
  // assuming that unstructured meshes used in OpenMC are 3D
3535
  n_dimension_ = 3;
23✔
3536

3537
  if (length_multiplier_ > 0.0) {
23!
3538
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
×
3539
  }
3540
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
3541
  // Otherwise assume that it is prepared by its owning application
3542
  if (unique_m_) {
23!
3543
    m_->prepare_for_use();
23✔
3544
  }
3545

3546
  // ensure that the loaded mesh is 3 dimensional
3547
  if (m_->mesh_dimension() != n_dimension_) {
23!
3548
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3549
                            "mesh is not a 3D mesh.",
3550
      filename_));
3551
  }
3552

3553
  for (int i = 0; i < num_threads(); i++) {
69✔
3554
    pl_.emplace_back(m_->sub_point_locator());
46✔
3555
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
46✔
3556
    pl_.back()->enable_out_of_mesh_mode();
46✔
3557
  }
3558

3559
  // store first element in the mesh to use as an offset for bin indices
3560
  auto first_elem = *m_->elements_begin();
23✔
3561
  first_element_id_ = first_elem->id();
23✔
3562

3563
  // bounding box for the mesh for quick rejection checks
3564
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
23✔
3565
  libMesh::Point ll = bbox_.min();
23✔
3566
  libMesh::Point ur = bbox_.max();
23✔
3567
  lower_left_ = {ll(0), ll(1), ll(2)};
23✔
3568
  upper_right_ = {ur(0), ur(1), ur(2)};
23✔
3569
}
23✔
3570

3571
// Sample position within a tet for LibMesh type tets
3572
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
400,820✔
3573
{
3574
  const auto& elem = get_element_from_bin(bin);
400,820✔
3575
  // Get tet vertex coordinates from LibMesh
3576
  std::array<Position, 4> tet_verts;
400,820✔
3577
  for (int i = 0; i < elem.n_nodes(); i++) {
2,004,100✔
3578
    auto node_ref = elem.node_ref(i);
1,603,280✔
3579
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,603,280✔
3580
  }
1,603,280✔
3581
  // Samples position within tet using Barycentric coordinates
3582
  return this->sample_tet(tet_verts, seed);
801,640✔
3583
}
3584

3585
Position LibMesh::centroid(int bin) const
3586
{
3587
  const auto& elem = this->get_element_from_bin(bin);
×
3588
  auto centroid = elem.vertex_average();
×
3589
  return {centroid(0), centroid(1), centroid(2)};
3590
}
3591

3592
int LibMesh::n_vertices() const
39,978✔
3593
{
3594
  return m_->n_nodes();
39,978✔
3595
}
3596

3597
Position LibMesh::vertex(int vertex_id) const
39,942✔
3598
{
3599
  const auto node_ref = m_->node_ref(vertex_id);
39,942✔
3600
  return {node_ref(0), node_ref(1), node_ref(2)};
79,884✔
3601
}
39,942✔
3602

3603
std::vector<int> LibMesh::connectivity(int elem_id) const
265,856✔
3604
{
3605
  std::vector<int> conn;
265,856✔
3606
  const auto* elem_ptr = m_->elem_ptr(elem_id);
265,856✔
3607
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
1,337,280✔
3608
    conn.push_back(elem_ptr->node_id(i));
1,071,424✔
3609
  }
3610
  return conn;
265,856✔
3611
}
3612

3613
std::string LibMesh::library() const
33✔
3614
{
3615
  return mesh_lib_type;
33✔
3616
}
3617

3618
int LibMesh::n_bins() const
1,784,287✔
3619
{
3620
  return m_->n_elem();
1,784,287✔
3621
}
3622

3623
int LibMesh::n_surface_bins() const
3624
{
3625
  int n_bins = 0;
3626
  for (int i = 0; i < this->n_bins(); i++) {
×
3627
    const libMesh::Elem& e = get_element_from_bin(i);
3628
    n_bins += e.n_faces();
3629
    // if this is a boundary element, it will only be visited once,
3630
    // the number of surface bins is incremented to
3631
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
×
3632
      // null neighbor pointer indicates a boundary face
3633
      if (!neighbor_ptr) {
×
3634
        n_bins++;
3635
      }
3636
    }
3637
  }
3638
  return n_bins;
3639
}
3640

3641
void LibMesh::add_score(const std::string& var_name)
15✔
3642
{
3643
  if (!equation_systems_) {
15!
3644
    build_eqn_sys();
15✔
3645
  }
3646

3647
  // check if this is a new variable
3648
  std::string value_name = var_name + "_mean";
15✔
3649
  if (!variable_map_.count(value_name)) {
15!
3650
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3651
    auto var_num =
3652
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3653
    variable_map_[value_name] = var_num;
15✔
3654
  }
3655

3656
  std::string std_dev_name = var_name + "_std_dev";
15✔
3657
  // check if this is a new variable
3658
  if (!variable_map_.count(std_dev_name)) {
15!
3659
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3660
    auto var_num =
3661
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
15✔
3662
    variable_map_[std_dev_name] = var_num;
15✔
3663
  }
3664
}
15✔
3665

3666
void LibMesh::remove_scores()
15✔
3667
{
3668
  if (equation_systems_) {
15!
3669
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3670
    eqn_sys.clear();
15✔
3671
    variable_map_.clear();
15✔
3672
  }
3673
}
15✔
3674

3675
void LibMesh::set_score_data(const std::string& var_name,
15✔
3676
  const vector<double>& values, const vector<double>& std_dev)
3677
{
3678
  if (!equation_systems_) {
15!
3679
    build_eqn_sys();
×
3680
  }
3681

3682
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
15✔
3683

3684
  if (!eqn_sys.is_initialized()) {
15!
3685
    equation_systems_->init();
15✔
3686
  }
3687

3688
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
15✔
3689

3690
  // look up the value variable
3691
  std::string value_name = var_name + "_mean";
15✔
3692
  unsigned int value_num = variable_map_.at(value_name);
15✔
3693
  // look up the std dev variable
3694
  std::string std_dev_name = var_name + "_std_dev";
15✔
3695
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
15✔
3696

3697
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
97,871✔
3698
       it++) {
3699
    if (!(*it)->active()) {
97,856!
3700
      continue;
3701
    }
3702

3703
    auto bin = get_bin_from_element(*it);
97,856✔
3704

3705
    // set value
3706
    vector<libMesh::dof_id_type> value_dof_indices;
97,856✔
3707
    dof_map.dof_indices(*it, value_dof_indices, value_num);
97,856✔
3708
    assert(value_dof_indices.size() == 1);
3709
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
97,856✔
3710

3711
    // set std dev
3712
    vector<libMesh::dof_id_type> std_dev_dof_indices;
97,856✔
3713
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
97,856✔
3714
    assert(std_dev_dof_indices.size() == 1);
3715
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
97,856✔
3716
  }
97,871✔
3717
}
15✔
3718

3719
void LibMesh::write(const std::string& filename) const
15✔
3720
{
3721
  write_message(fmt::format(
15✔
3722
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
15✔
3723
  libMesh::ExodusII_IO exo(*m_);
15✔
3724
  std::set<std::string> systems_out = {eq_system_name_};
45✔
3725
  exo.write_discontinuous_exodusII(
15✔
3726
    filename + ".e", *equation_systems_, &systems_out);
30✔
3727
}
15✔
3728

3729
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3730
  vector<int>& bins, vector<double>& lengths) const
3731
{
3732
  // TODO: Implement triangle crossings here
3733
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3734
}
3735

3736
int LibMesh::get_bin(Position r) const
2,340,484✔
3737
{
3738
  // look-up a tet using the point locator
3739
  libMesh::Point p(r.x, r.y, r.z);
2,340,484✔
3740

3741
  // quick rejection check
3742
  if (!bbox_.contains_point(p)) {
2,340,484✔
3743
    return -1;
918,796✔
3744
  }
3745

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

3748
  const auto elem_ptr = (*point_locator)(p);
1,421,688✔
3749
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
1,421,688✔
3750
}
2,340,484✔
3751

3752
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
1,518,314✔
3753
{
3754
  int bin = elem->id() - first_element_id_;
1,518,314✔
3755
  if (bin >= n_bins() || bin < 0) {
1,518,314!
3756
    fatal_error(fmt::format("Invalid bin: {}", bin));
3757
  }
3758
  return bin;
1,518,314✔
3759
}
3760

3761
std::pair<vector<double>, vector<double>> LibMesh::plot(
3762
  Position plot_ll, Position plot_ur) const
3763
{
3764
  return {};
3765
}
3766

3767
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
765,460✔
3768
{
3769
  return m_->elem_ref(bin);
765,460✔
3770
}
3771

3772
double LibMesh::volume(int bin) const
364,640✔
3773
{
3774
  return this->get_element_from_bin(bin).volume();
364,640✔
3775
}
3776

3777
AdaptiveLibMesh::AdaptiveLibMesh(
3778
  libMesh::MeshBase& input_mesh, double length_multiplier)
3779
  : LibMesh(input_mesh, length_multiplier), num_active_(m_->n_active_elem())
×
3780
{
3781
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
3782
  // contiguous in ID space, so we need to map from bin indices (defined over
3783
  // active elements) to global dof ids
3784
  bin_to_elem_map_.reserve(num_active_);
×
3785
  elem_to_bin_map_.resize(m_->n_elem(), -1);
×
3786
  for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
×
3787
       it++) {
3788
    auto elem = *it;
×
3789

3790
    bin_to_elem_map_.push_back(elem->id());
×
3791
    elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
×
3792
  }
3793
}
3794

3795
int AdaptiveLibMesh::n_bins() const
3796
{
3797
  return num_active_;
3798
}
3799

3800
void AdaptiveLibMesh::add_score(const std::string& var_name)
3801
{
3802
  warning(fmt::format(
×
3803
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3804
    this->id_));
3805
}
3806

3807
void AdaptiveLibMesh::set_score_data(const std::string& var_name,
3808
  const vector<double>& values, const vector<double>& std_dev)
3809
{
3810
  warning(fmt::format(
×
3811
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3812
    this->id_));
3813
}
3814

3815
void AdaptiveLibMesh::write(const std::string& filename) const
3816
{
3817
  warning(fmt::format(
×
3818
    "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3819
    this->id_));
3820
}
3821

3822
int AdaptiveLibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3823
{
3824
  int bin = elem_to_bin_map_[elem->id()];
×
3825
  if (bin >= n_bins() || bin < 0) {
×
3826
    fatal_error(fmt::format("Invalid bin: {}", bin));
3827
  }
3828
  return bin;
3829
}
3830

3831
const libMesh::Elem& AdaptiveLibMesh::get_element_from_bin(int bin) const
3832
{
3833
  return m_->elem_ref(bin_to_elem_map_.at(bin));
3834
}
3835

3836
#endif // OPENMC_LIBMESH_ENABLED
3837

3838
//==============================================================================
3839
// Non-member functions
3840
//==============================================================================
3841

3842
void read_meshes(pugi::xml_node root)
5,411✔
3843
{
3844
  std::unordered_set<int> mesh_ids;
5,411✔
3845

3846
  for (auto node : root.children("mesh")) {
6,725✔
3847
    // Check to make sure multiple meshes in the same file don't share IDs
3848
    int id = std::stoi(get_node_value(node, "id"));
1,314✔
3849
    if (contains(mesh_ids, id)) {
1,314!
UNCOV
3850
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
3851
                              "'{}' in the same input file",
3852
        id));
3853
    }
3854
    mesh_ids.insert(id);
1,314✔
3855

3856
    // If we've already read a mesh with the same ID in a *different* file,
3857
    // assume it is the same here
3858
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
1,314!
UNCOV
3859
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
×
3860
      continue;
×
3861
    }
3862

3863
    std::string mesh_type;
1,314✔
3864
    if (check_for_node(node, "type")) {
1,314✔
3865
      mesh_type = get_node_value(node, "type", true, true);
434✔
3866
    } else {
3867
      mesh_type = "regular";
880✔
3868
    }
3869

3870
    // determine the mesh library to use
3871
    std::string mesh_lib;
1,314✔
3872
    if (check_for_node(node, "library")) {
1,314✔
3873
      mesh_lib = get_node_value(node, "library", true, true);
23!
3874
    }
3875

3876
    Mesh::create(node, mesh_type, mesh_lib);
1,314✔
3877
  }
1,314✔
3878
}
5,411✔
3879

3880
void read_meshes(hid_t group)
10✔
3881
{
3882
  std::unordered_set<int> mesh_ids;
10✔
3883

3884
  std::vector<int> ids;
10✔
3885
  read_attribute(group, "ids", ids);
10✔
3886

3887
  for (auto id : ids) {
25✔
3888

3889
    // Check to make sure multiple meshes in the same file don't share IDs
3890
    if (contains(mesh_ids, id)) {
15!
UNCOV
3891
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
×
3892
                              "'{}' in the same HDF5 input file",
3893
        id));
3894
    }
3895
    mesh_ids.insert(id);
15✔
3896

3897
    // If we've already read a mesh with the same ID in a *different* file,
3898
    // assume it is the same here
3899
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
15!
3900
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
15✔
3901
      continue;
15✔
3902
    }
3903

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

UNCOV
3907
    std::string mesh_type;
×
3908
    if (object_exists(mesh_group, "type")) {
×
3909
      read_dataset(mesh_group, "type", mesh_type);
×
3910
    } else {
UNCOV
3911
      mesh_type = "regular";
×
3912
    }
3913

3914
    // determine the mesh library to use
UNCOV
3915
    std::string mesh_lib;
×
3916
    if (object_exists(mesh_group, "library")) {
×
3917
      read_dataset(mesh_group, "library", mesh_lib);
×
3918
    }
3919

UNCOV
3920
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
3921
  }
×
3922
}
10✔
3923

3924
void meshes_to_hdf5(hid_t group)
3,050✔
3925
{
3926
  // Write number of meshes
3927
  hid_t meshes_group = create_group(group, "meshes");
3,050✔
3928
  int32_t n_meshes = model::meshes.size();
3,050✔
3929
  write_attribute(meshes_group, "n_meshes", n_meshes);
3,050✔
3930

3931
  if (n_meshes > 0) {
3,050✔
3932
    // Write IDs of meshes
3933
    vector<int> ids;
953✔
3934
    for (const auto& m : model::meshes) {
2,171✔
3935
      m->to_hdf5(meshes_group);
1,218✔
3936
      ids.push_back(m->id_);
1,218✔
3937
    }
3938
    write_attribute(meshes_group, "ids", ids);
953✔
3939
  }
953✔
3940

3941
  close_group(meshes_group);
3,050✔
3942
}
3,050✔
3943

3944
void free_memory_mesh()
3,594✔
3945
{
3946
  model::meshes.clear();
3,594✔
3947
  model::mesh_map.clear();
3,594✔
3948
}
3,594✔
3949

3950
extern "C" int n_meshes()
140✔
3951
{
3952
  return model::meshes.size();
140✔
3953
}
3954

3955
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc