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

openmc-dev / openmc / 14260484407

04 Apr 2025 07:41AM UTC coverage: 84.589% (-0.3%) from 84.851%
14260484407

Pull #3279

github

web-flow
Merge 33e03305e into 07f533461
Pull Request #3279: Hexagonal mesh model

2 of 332 new or added lines in 3 files covered. (0.6%)

2798 existing lines in 84 files now uncovered.

51799 of 61236 relevant lines covered (84.59%)

38650400.15 hits per line

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

87.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/hex_mesh.h"
34
#include "openmc/material.h"
35
#include "openmc/memory.h"
36
#include "openmc/message_passing.h"
37
#include "openmc/openmp_interface.h"
38
#include "openmc/output.h"
39
#include "openmc/particle_data.h"
40
#include "openmc/plot.h"
41
#include "openmc/random_dist.h"
42
#include "openmc/search.h"
43
#include "openmc/settings.h"
44
#include "openmc/string_utils.h"
45
#include "openmc/tallies/filter.h"
46
#include "openmc/tallies/tally.h"
47
#include "openmc/timer.h"
48
#include "openmc/volume_calc.h"
49
#include "openmc/xml_interface.h"
50

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

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

61
namespace openmc {
62

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

67
#ifdef LIBMESH
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 LIBMESH
85
namespace settings {
86
unique_ptr<libMesh::LibMeshInit> libmesh_init;
87
const libMesh::Parallel::Communicator* libmesh_comm {nullptr};
88
} // namespace settings
89
#endif
90

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

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

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

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

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

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

143
namespace detail {
144

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

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

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

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

169
    // Found the desired material; accumulate volume
170
    if (current_val == index_material) {
2,268,893✔
171
#pragma omp atomic
1,237,118✔
172
      this->volumes(index_elem, slot) += volume;
2,267,637✔
173
      return;
2,267,637✔
174
    }
175

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

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

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

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

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

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

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

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

228
} // namespace detail
229

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

234
Mesh::Mesh(pugi::xml_node node)
2,637✔
235
{
236
  // Read mesh id
237
  id_ = std::stoi(get_node_value(node, "id"));
2,637✔
238
  if (check_for_node(node, "name"))
2,637✔
239
    name_ = get_node_value(node, "name");
16✔
240
}
2,637✔
241

242
void Mesh::set_id(int32_t id)
1✔
243
{
244
  assert(id >= 0 || id == C_NONE);
1✔
245

246
  // Clear entry in mesh map in case one was already assigned
247
  if (id_ != C_NONE) {
1✔
UNCOV
248
    model::mesh_map.erase(id_);
×
UNCOV
249
    id_ = C_NONE;
×
250
  }
251

252
  // Ensure no other mesh has the same ID
253
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
1✔
UNCOV
254
    throw std::runtime_error {
×
UNCOV
255
      fmt::format("Two meshes have the same ID: {}", id)};
×
256
  }
257

258
  // If no ID is specified, auto-assign the next ID in the sequence
259
  if (id == C_NONE) {
1✔
260
    id = 0;
1✔
261
    for (const auto& m : model::meshes) {
3✔
262
      id = std::max(id, m->id_);
2✔
263
    }
264
    ++id;
1✔
265
  }
266

267
  // Update ID and entry in the mesh map
268
  id_ = id;
1✔
269
  model::mesh_map[id] = model::meshes.size() - 1;
1✔
270
}
1✔
271

272
vector<double> Mesh::volumes() const
2,394✔
273
{
274
  vector<double> volumes(n_bins());
2,394✔
275
  for (int i = 0; i < n_bins(); i++) {
7,453,349✔
276
    volumes[i] = this->volume(i);
7,450,955✔
277
  }
278
  return volumes;
2,394✔
UNCOV
279
}
×
280

281
void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
143✔
282
  int32_t* materials, double* volumes) const
283
{
284
  if (mpi::master) {
143✔
285
    header("MESH MATERIAL VOLUMES CALCULATION", 7);
143✔
286
  }
287
  write_message(7, "Number of mesh elements = {}", n_bins());
143✔
288
  write_message(7, "Number of rays (x) = {}", nx);
143✔
289
  write_message(7, "Number of rays (y) = {}", ny);
143✔
290
  write_message(7, "Number of rays (z) = {}", nz);
143✔
291
  int64_t n_total = static_cast<int64_t>(nx) * ny +
143✔
292
                    static_cast<int64_t>(ny) * nz +
143✔
293
                    static_cast<int64_t>(nx) * nz;
143✔
294
  write_message(7, "Total number of rays = {}", n_total);
143✔
295
  write_message(7, "Table size per mesh element = {}", table_size);
143✔
296

297
  Timer timer;
143✔
298
  timer.start();
143✔
299

300
  // Create object for keeping track of materials/volumes
301
  detail::MaterialVolumes result(materials, volumes, table_size);
143✔
302

303
  // Determine bounding box
304
  auto bbox = this->bounding_box();
143✔
305

306
  std::array<int, 3> n_rays = {nx, ny, nz};
143✔
307

308
  // Determine effective width of rays
309
  Position width((nx > 0) ? (bbox.xmax - bbox.xmin) / nx : 0.0,
264✔
310
    (ny > 0) ? (bbox.ymax - bbox.ymin) / ny : 0.0,
275✔
311
    (nz > 0) ? (bbox.zmax - bbox.zmin) / nz : 0.0);
143✔
312

313
  // Set flag for mesh being contained within model
314
  bool out_of_model = false;
143✔
315

316
#pragma omp parallel
78✔
317
  {
318
    // Preallocate vector for mesh indices and length fractions and particle
319
    std::vector<int> bins;
65✔
320
    std::vector<double> length_fractions;
65✔
321
    Particle p;
65✔
322

323
    SourceSite site;
65✔
324
    site.E = 1.0;
65✔
325
    site.particle = ParticleType::neutron;
65✔
326

327
    for (int axis = 0; axis < 3; ++axis) {
260✔
328
      // Set starting position and direction
329
      site.r = {0.0, 0.0, 0.0};
195✔
330
      site.r[axis] = bbox.min()[axis];
195✔
331
      site.u = {0.0, 0.0, 0.0};
195✔
332
      site.u[axis] = 1.0;
195✔
333

334
      // Determine width of rays and number of rays in other directions
335
      int ax1 = (axis + 1) % 3;
195✔
336
      int ax2 = (axis + 2) % 3;
195✔
337
      double min1 = bbox.min()[ax1];
195✔
338
      double min2 = bbox.min()[ax2];
195✔
339
      double d1 = width[ax1];
195✔
340
      double d2 = width[ax2];
195✔
341
      int n1 = n_rays[ax1];
195✔
342
      int n2 = n_rays[ax2];
195✔
343
      if (n1 == 0 || n2 == 0) {
195✔
344
        continue;
50✔
345
      }
346

347
      // Divide rays in first direction over MPI processes by computing starting
348
      // and ending indices
349
      int min_work = n1 / mpi::n_procs;
145✔
350
      int remainder = n1 % mpi::n_procs;
145✔
351
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
145✔
352
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
145✔
353
      int i1_end = i1_start + n1_local;
145✔
354

355
      // Loop over rays on face of bounding box
356
#pragma omp for collapse(2)
357
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
7,070✔
358
        for (int i2 = 0; i2 < n2; ++i2) {
418,820✔
359
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
411,895✔
360
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
411,895✔
361

362
          p.from_source(&site);
411,895✔
363

364
          // Determine particle's location
365
          if (!exhaustive_find_cell(p)) {
411,895✔
366
            out_of_model = true;
39,930✔
367
            continue;
39,930✔
368
          }
369

370
          // Set birth cell attribute
371
          if (p.cell_born() == C_NONE)
371,965✔
372
            p.cell_born() = p.lowest_coord().cell;
371,965✔
373

374
          // Initialize last cells from current cell
375
          for (int j = 0; j < p.n_coord(); ++j) {
743,930✔
376
            p.cell_last(j) = p.coord(j).cell;
371,965✔
377
          }
378
          p.n_coord_last() = p.n_coord();
371,965✔
379

380
          while (true) {
381
            // Ray trace from r_start to r_end
382
            Position r0 = p.r();
811,645✔
383
            double max_distance = bbox.max()[axis] - r0[axis];
811,645✔
384

385
            // Find the distance to the nearest boundary
386
            BoundaryInfo boundary = distance_to_boundary(p);
811,645✔
387

388
            // Advance particle forward
389
            double distance = std::min(boundary.distance, max_distance);
811,645✔
390
            p.move_distance(distance);
811,645✔
391

392
            // Determine what mesh elements were crossed by particle
393
            bins.clear();
811,645✔
394
            length_fractions.clear();
811,645✔
395
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
811,645✔
396

397
            // Add volumes to any mesh elements that were crossed
398
            int i_material = p.material();
811,645✔
399
            if (i_material != C_NONE) {
811,645✔
400
              i_material = model::materials[i_material]->id();
783,215✔
401
            }
402
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
1,842,960✔
403
              int mesh_index = bins[i_bin];
1,031,315✔
404
              double length = distance * length_fractions[i_bin];
1,031,315✔
405

406
              // Add volume to result
407
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,031,315✔
408
            }
409

410
            if (distance == max_distance)
811,645✔
411
              break;
371,965✔
412

413
            // cross next geometric surface
414
            for (int j = 0; j < p.n_coord(); ++j) {
879,360✔
415
              p.cell_last(j) = p.coord(j).cell;
439,680✔
416
            }
417
            p.n_coord_last() = p.n_coord();
439,680✔
418

419
            // Set surface that particle is on and adjust coordinate levels
420
            p.surface() = boundary.surface;
439,680✔
421
            p.n_coord() = boundary.coord_level;
439,680✔
422

423
            if (boundary.lattice_translation[0] != 0 ||
439,680✔
424
                boundary.lattice_translation[1] != 0 ||
879,360✔
425
                boundary.lattice_translation[2] != 0) {
439,680✔
426
              // Particle crosses lattice boundary
427
              cross_lattice(p, boundary);
428
            } else {
429
              // Particle crosses surface
430
              const auto& surf {model::surfaces[p.surface_index()].get()};
439,680✔
431
              p.cross_surface(*surf);
439,680✔
432
            }
433
          }
439,680✔
434
        }
435
      }
436
    }
437
  }
65✔
438

439
  // Check for errors
440
  if (out_of_model) {
143✔
441
    throw std::runtime_error("Mesh not fully contained in geometry.");
11✔
442
  } else if (result.table_full()) {
132✔
UNCOV
443
    throw std::runtime_error("Maximum number of materials for mesh material "
×
UNCOV
444
                             "volume calculation insufficient.");
×
445
  }
446

447
  // Compute time for raytracing
448
  double t_raytrace = timer.elapsed();
132✔
449

450
#ifdef OPENMC_MPI
451
  // Combine results from multiple MPI processes
452
  if (mpi::n_procs > 1) {
60✔
453
    int total = this->n_bins() * table_size;
454
    if (mpi::master) {
455
      // Allocate temporary buffer for receiving data
456
      std::vector<int32_t> mats(total);
457
      std::vector<double> vols(total);
458

459
      for (int i = 1; i < mpi::n_procs; ++i) {
460
        // Receive material indices and volumes from process i
461
        MPI_Recv(mats.data(), total, MPI_INT32_T, i, i, mpi::intracomm,
462
          MPI_STATUS_IGNORE);
463
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
464
          MPI_STATUS_IGNORE);
465

466
        // Combine with existing results; we can call thread unsafe version of
467
        // add_volume because each thread is operating on a different element
468
#pragma omp for
469
        for (int index_elem = 0; index_elem < n_bins(); ++index_elem) {
470
          for (int k = 0; k < table_size; ++k) {
471
            int index = index_elem * table_size + k;
472
            if (mats[index] != EMPTY) {
473
              result.add_volume_unsafe(index_elem, mats[index], vols[index]);
474
            }
475
          }
476
        }
477
      }
478
    } else {
479
      // Send material indices and volumes to process 0
480
      MPI_Send(materials, total, MPI_INT32_T, 0, mpi::rank, mpi::intracomm);
481
      MPI_Send(volumes, total, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
482
    }
483
  }
484

485
  // Report time for MPI communication
486
  double t_mpi = timer.elapsed() - t_raytrace;
60✔
487
#else
488
  double t_mpi = 0.0;
72✔
489
#endif
490

491
  // Normalize based on known volumes of elements
492
  for (int i = 0; i < this->n_bins(); ++i) {
935✔
493
    // Estimated total volume in element i
494
    double volume = 0.0;
803✔
495
    for (int j = 0; j < table_size; ++j) {
7,227✔
496
      volume += result.volumes(i, j);
6,424✔
497
    }
498
    // Renormalize volumes based on known volume of element i
499
    double norm = this->volume(i) / volume;
803✔
500
    for (int j = 0; j < table_size; ++j) {
7,227✔
501
      result.volumes(i, j) *= norm;
6,424✔
502
    }
503
  }
504

505
  // Get total time and normalization time
506
  timer.stop();
132✔
507
  double t_total = timer.elapsed();
132✔
508
  double t_norm = t_total - t_raytrace - t_mpi;
132✔
509

510
  // Show timing statistics
511
  if (settings::verbosity < 7 || !mpi::master)
132✔
512
    return;
44✔
513
  header("Timing Statistics", 7);
88✔
514
  fmt::print(" Total time elapsed            = {:.4e} seconds\n", t_total);
88✔
515
  fmt::print("   Ray tracing                 = {:.4e} seconds\n", t_raytrace);
88✔
516
  fmt::print("   MPI communication           = {:.4e} seconds\n", t_mpi);
88✔
517
  fmt::print("   Normalization               = {:.4e} seconds\n", t_norm);
72✔
518
  fmt::print(" Calculation rate              = {:.4e} rays/seconds\n",
72✔
519
    n_total / t_raytrace);
88✔
520
  fmt::print(" Calculation rate (per thread) = {:.4e} rays/seconds\n",
72✔
521
    n_total / (t_raytrace * mpi::n_procs * num_threads()));
88✔
522
  std::fflush(stdout);
88✔
523
}
524

525
void Mesh::to_hdf5(hid_t group) const
2,664✔
526
{
527
  // Create group for mesh
528
  std::string group_name = fmt::format("mesh {}", id_);
4,746✔
529
  hid_t mesh_group = create_group(group, group_name.c_str());
2,664✔
530

531
  // Write mesh type
532
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,664✔
533

534
  // Write mesh ID
535
  write_attribute(mesh_group, "id", id_);
2,664✔
536

537
  // Write mesh name
538
  write_dataset(mesh_group, "name", name_);
2,664✔
539

540
  // Write mesh data
541
  this->to_hdf5_inner(mesh_group);
2,664✔
542

543
  // Close group
544
  close_group(mesh_group);
2,664✔
545
}
2,664✔
546

547
//==============================================================================
548
// Structured Mesh implementation
549
//==============================================================================
550

551
std::string StructuredMesh::bin_label(int bin) const
5,120,453✔
552
{
553
  MeshIndex ijk = get_indices_from_bin(bin);
5,120,453✔
554

555
  if (n_dimension_ > 2) {
5,120,453✔
556
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,211,074✔
557
  } else if (n_dimension_ > 1) {
14,916✔
558
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
29,282✔
559
  } else {
560
    return fmt::format("Mesh Index ({})", ijk[0]);
550✔
561
  }
562
}
563

564
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,384✔
565
{
566
  // because method is const, shape_ is const as well and can't be adapted
567
  auto tmp_shape = shape_;
2,384✔
568
  return xt::adapt(tmp_shape, {n_dimension_});
4,768✔
569
}
570

571
Position StructuredMesh::sample_element(
1,413,841✔
572
  const MeshIndex& ijk, uint64_t* seed) const
573
{
574
  // lookup the lower/upper bounds for the mesh element
575
  double x_min = negative_grid_boundary(ijk, 0);
1,413,841✔
576
  double x_max = positive_grid_boundary(ijk, 0);
1,413,841✔
577

578
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,413,841✔
579
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,413,841✔
580

581
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,413,841✔
582
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,413,841✔
583

584
  return {x_min + (x_max - x_min) * prn(seed),
1,413,841✔
585
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,413,841✔
586
}
587

588
//==============================================================================
589
// Unstructured Mesh implementation
590
//==============================================================================
591

592
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
46✔
593
{
594
  // check the mesh type
595
  if (check_for_node(node, "type")) {
46✔
596
    auto temp = get_node_value(node, "type", true, true);
46✔
597
    if (temp != mesh_type) {
46✔
UNCOV
598
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
599
    }
600
  }
46✔
601

602
  // check if a length unit multiplier was specified
603
  if (check_for_node(node, "length_multiplier")) {
46✔
UNCOV
604
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
605
  }
606

607
  // get the filename of the unstructured mesh to load
608
  if (check_for_node(node, "filename")) {
46✔
609
    filename_ = get_node_value(node, "filename");
46✔
610
    if (!file_exists(filename_)) {
46✔
UNCOV
611
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
612
    }
613
  } else {
614
    fatal_error(fmt::format(
×
UNCOV
615
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
616
  }
617

618
  if (check_for_node(node, "options")) {
46✔
619
    options_ = get_node_value(node, "options");
16✔
620
  }
621

622
  // check if mesh tally data should be written with
623
  // statepoint files
624
  if (check_for_node(node, "output")) {
46✔
UNCOV
625
    output_ = get_node_value_bool(node, "output");
×
626
  }
627
}
46✔
628

629
void UnstructuredMesh::determine_bounds()
24✔
630
{
631
  double xmin = INFTY;
24✔
632
  double ymin = INFTY;
24✔
633
  double zmin = INFTY;
24✔
634
  double xmax = -INFTY;
24✔
635
  double ymax = -INFTY;
24✔
636
  double zmax = -INFTY;
24✔
637
  int n = this->n_vertices();
24✔
638
  for (int i = 0; i < n; ++i) {
55,936✔
639
    auto v = this->vertex(i);
55,912✔
640
    xmin = std::min(v.x, xmin);
55,912✔
641
    ymin = std::min(v.y, ymin);
55,912✔
642
    zmin = std::min(v.z, zmin);
55,912✔
643
    xmax = std::max(v.x, xmax);
55,912✔
644
    ymax = std::max(v.y, ymax);
55,912✔
645
    zmax = std::max(v.z, zmax);
55,912✔
646
  }
647
  lower_left_ = {xmin, ymin, zmin};
24✔
648
  upper_right_ = {xmax, ymax, zmax};
24✔
649
}
24✔
650

651
Position UnstructuredMesh::sample_tet(
601,230✔
652
  std::array<Position, 4> coords, uint64_t* seed) const
653
{
654
  // Uniform distribution
655
  double s = prn(seed);
601,230✔
656
  double t = prn(seed);
601,230✔
657
  double u = prn(seed);
601,230✔
658

659
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
660
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
661
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
662
  if (s + t > 1) {
601,230✔
663
    s = 1.0 - s;
301,245✔
664
    t = 1.0 - t;
301,245✔
665
  }
666
  if (s + t + u > 1) {
601,230✔
667
    if (t + u > 1) {
400,908✔
668
      double old_t = t;
199,920✔
669
      t = 1.0 - u;
199,920✔
670
      u = 1.0 - s - old_t;
199,920✔
671
    } else if (t + u <= 1) {
200,988✔
672
      double old_s = s;
200,988✔
673
      s = 1.0 - t - u;
200,988✔
674
      u = old_s + t + u - 1;
200,988✔
675
    }
676
  }
677
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
678
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
679
}
680

681
const std::string UnstructuredMesh::mesh_type = "unstructured";
682

683
std::string UnstructuredMesh::get_mesh_type() const
31✔
684
{
685
  return mesh_type;
31✔
686
}
687

UNCOV
688
void UnstructuredMesh::surface_bins_crossed(
×
689
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
690
{
UNCOV
691
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
692
}
693

694
std::string UnstructuredMesh::bin_label(int bin) const
205,712✔
695
{
696
  return fmt::format("Mesh Index ({})", bin);
205,712✔
697
};
698

699
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
31✔
700
{
701
  write_dataset(mesh_group, "filename", filename_);
31✔
702
  write_dataset(mesh_group, "library", this->library());
31✔
703
  if (!options_.empty()) {
31✔
704
    write_attribute(mesh_group, "options", options_);
8✔
705
  }
706

707
  if (length_multiplier_ > 0.0)
31✔
UNCOV
708
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
709

710
  // write vertex coordinates
711
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
31✔
712
  for (int i = 0; i < this->n_vertices(); i++) {
70,260✔
713
    auto v = this->vertex(i);
70,229✔
714
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
70,229✔
715
  }
716
  write_dataset(mesh_group, "vertices", vertices);
31✔
717

718
  int num_elem_skipped = 0;
31✔
719

720
  // write element types and connectivity
721
  vector<double> volumes;
31✔
722
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
31✔
723
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
31✔
724
  for (int i = 0; i < this->n_bins(); i++) {
349,743✔
725
    auto conn = this->connectivity(i);
349,712✔
726

727
    volumes.emplace_back(this->volume(i));
349,712✔
728

729
    // write linear tet element
730
    if (conn.size() == 4) {
349,712✔
731
      xt::view(elem_types, i, xt::all()) =
695,424✔
732
        static_cast<int>(ElementType::LINEAR_TET);
695,424✔
733
      xt::view(connectivity, i, xt::all()) =
695,424✔
734
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
1,043,136✔
735
      // write linear hex element
736
    } else if (conn.size() == 8) {
2,000✔
737
      xt::view(elem_types, i, xt::all()) =
4,000✔
738
        static_cast<int>(ElementType::LINEAR_HEX);
4,000✔
739
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000✔
740
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000✔
741
    } else {
UNCOV
742
      num_elem_skipped++;
×
UNCOV
743
      xt::view(elem_types, i, xt::all()) =
×
744
        static_cast<int>(ElementType::UNSUPPORTED);
UNCOV
745
      xt::view(connectivity, i, xt::all()) = -1;
×
746
    }
747
  }
349,712✔
748

749
  // warn users that some elements were skipped
750
  if (num_elem_skipped > 0) {
31✔
UNCOV
751
    warning(fmt::format("The connectivity of {} elements "
×
752
                        "on mesh {} were not written "
753
                        "because they are not of type linear tet/hex.",
UNCOV
754
      num_elem_skipped, this->id_));
×
755
  }
756

757
  write_dataset(mesh_group, "volumes", volumes);
31✔
758
  write_dataset(mesh_group, "connectivity", connectivity);
31✔
759
  write_dataset(mesh_group, "element_types", elem_types);
31✔
760
}
31✔
761

762
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
763
{
764
  length_multiplier_ = length_multiplier;
23✔
765
}
23✔
766

767
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
768
{
769
  auto conn = connectivity(bin);
120,000✔
770

771
  if (conn.size() == 4)
120,000✔
772
    return ElementType::LINEAR_TET;
120,000✔
UNCOV
773
  else if (conn.size() == 8)
×
UNCOV
774
    return ElementType::LINEAR_HEX;
×
775
  else
UNCOV
776
    return ElementType::UNSUPPORTED;
×
777
}
120,000✔
778

779
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,148,741,975✔
780
  Position r, bool& in_mesh) const
781
{
782
  MeshIndex ijk;
783
  in_mesh = true;
1,148,741,975✔
784
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
785
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
786

787
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
788
      in_mesh = false;
84,293,356✔
789
  }
790
  return ijk;
1,148,741,975✔
791
}
792

793
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,688,102,459✔
794
{
795
  switch (n_dimension_) {
1,688,102,459✔
796
  case 1:
877,008✔
797
    return ijk[0] - 1;
877,008✔
798
  case 2:
78,932,062✔
799
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
78,932,062✔
800
  case 3:
1,608,293,389✔
801
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,608,293,389✔
UNCOV
802
  default:
×
UNCOV
803
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
804
  }
805
}
806

807
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
14,074,932✔
808
{
809
  MeshIndex ijk;
810
  if (n_dimension_ == 1) {
14,074,932✔
811
    ijk[0] = bin + 1;
275✔
812
  } else if (n_dimension_ == 2) {
14,074,657✔
813
    ijk[0] = bin % shape_[0] + 1;
14,641✔
814
    ijk[1] = bin / shape_[0] + 1;
14,641✔
815
  } else if (n_dimension_ == 3) {
14,060,016✔
816
    ijk[0] = bin % shape_[0] + 1;
14,060,016✔
817
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
14,060,016✔
818
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
14,060,016✔
819
  }
820
  return ijk;
14,074,932✔
821
}
822

823
int StructuredMesh::get_bin(Position r) const
248,118,914✔
824
{
825
  // Determine indices
826
  bool in_mesh;
827
  MeshIndex ijk = get_indices(r, in_mesh);
248,118,914✔
828
  if (!in_mesh)
248,118,914✔
829
    return -1;
20,432,536✔
830

831
  // Convert indices to bin
832
  return get_bin_from_indices(ijk);
227,686,378✔
833
}
834

835
int StructuredMesh::n_bins() const
7,470,027✔
836
{
837
  return std::accumulate(
7,470,027✔
838
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
14,940,054✔
839
}
840

841
int StructuredMesh::n_surface_bins() const
399✔
842
{
843
  return 4 * n_dimension_ * n_bins();
399✔
844
}
845

UNCOV
846
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
847
  const SourceSite* bank, int64_t length, bool* outside) const
848
{
849
  // Determine shape of array for counts
UNCOV
850
  std::size_t m = this->n_bins();
×
UNCOV
851
  vector<std::size_t> shape = {m};
×
852

853
  // Create array of zeros
UNCOV
854
  xt::xarray<double> cnt {shape, 0.0};
×
UNCOV
855
  bool outside_ = false;
×
856

UNCOV
857
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
858
    const auto& site = bank[i];
×
859

860
    // determine scoring bin for entropy mesh
UNCOV
861
    int mesh_bin = get_bin(site.r);
×
862

863
    // if outside mesh, skip particle
UNCOV
864
    if (mesh_bin < 0) {
×
UNCOV
865
      outside_ = true;
×
UNCOV
866
      continue;
×
867
    }
868

869
    // Add to appropriate bin
UNCOV
870
    cnt(mesh_bin) += site.wgt;
×
871
  }
872

873
  // Create copy of count data. Since ownership will be acquired by xtensor,
874
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
875
  // warnings.
UNCOV
876
  int total = cnt.size();
×
UNCOV
877
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
878

879
#ifdef OPENMC_MPI
880
  // collect values from all processors
881
  MPI_Reduce(
882
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
883

884
  // Check if there were sites outside the mesh for any processor
885
  if (outside) {
886
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
887
  }
888
#else
889
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
890
  if (outside)
891
    *outside = outside_;
892
#endif
893

894
  // Adapt reduced values in array back into an xarray
UNCOV
895
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
UNCOV
896
  xt::xarray<double> counts = arr;
×
897

UNCOV
898
  return counts;
×
899
}
900

901
// raytrace through the mesh. The template class T will do the tallying.
902
// A modern optimizing compiler can recognize the noop method of T and
903
// eliminate that call entirely.
904
template<class T>
905
void StructuredMesh::raytrace_mesh(
904,518,467✔
906
  Position r0, Position r1, const Direction& u, T tally) const
907
{
908
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
909
  // enable/disable tally calls for now, T template type needs to provide both
910
  // surface and track methods, which might be empty. modern optimizing
911
  // compilers will (hopefully) eliminate the complete code (including
912
  // calculation of parameters) but for the future: be explicit
913

914
  // Compute the length of the entire track.
915
  double total_distance = (r1 - r0).norm();
904,518,467✔
916
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
904,518,467✔
917
    return;
9,061,235✔
918

919
  const int n = n_dimension_;
895,457,232✔
920

921
  // Flag if position is inside the mesh
922
  bool in_mesh;
923

924
  // Position is r = r0 + u * traveled_distance, start at r0
925
  double traveled_distance {0.0};
895,457,232✔
926

927
  // Calculate index of current cell. Offset the position a tiny bit in
928
  // direction of flight
929
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
895,457,232✔
930

931
  // if track is very short, assume that it is completely inside one cell.
932
  // Only the current cell will score and no surfaces
933
  if (total_distance < 2 * TINY_BIT) {
895,457,232✔
934
    if (in_mesh) {
646,283✔
935
      tally.track(ijk, 1.0);
646,272✔
936
    }
937
    return;
646,283✔
938
  }
939

940
  // translate start and end positions,
941
  // this needs to come after the get_indices call because it does its own
942
  // translation
943
  local_coords(r0);
894,810,949✔
944
  local_coords(r1);
894,810,949✔
945

946
  // Calculate initial distances to next surfaces in all three dimensions
947
  std::array<MeshDistance, 3> distances;
1,789,621,898✔
948
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
949
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
2,147,483,647✔
950
  }
951

952
  // Loop until r = r1 is eventually reached
953
  while (true) {
739,073,288✔
954

955
    if (in_mesh) {
1,633,884,237✔
956

957
      // find surface with minimal distance to current position
958
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,556,557,305✔
959
                     distances.begin();
1,556,557,305✔
960

961
      // Tally track length delta since last step
962
      tally.track(ijk,
1,556,557,305✔
963
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,556,557,305✔
964
          total_distance);
965

966
      // update position and leave, if we have reached end position
967
      traveled_distance = distances[k].distance;
1,556,557,305✔
968
      if (traveled_distance >= total_distance)
1,556,557,305✔
969
        return;
822,649,846✔
970

971
      // If we have not reached r1, we have hit a surface. Tally outward
972
      // current
973
      tally.surface(ijk, k, distances[k].max_surface, false);
733,907,459✔
974

975
      // Update cell and calculate distance to next surface in k-direction.
976
      // The two other directions are still valid!
977
      ijk[k] = distances[k].next_index;
733,907,459✔
978
      distances[k] =
733,907,459✔
979
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
733,907,459✔
980

981
      // Check if we have left the interior of the mesh
982
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
733,907,459✔
983

984
      // If we are still inside the mesh, tally inward current for the next
985
      // cell
986
      if (in_mesh)
733,907,459✔
987
        tally.surface(ijk, k, !distances[k].max_surface, true);
718,439,837✔
988

989
    } else { // not inside mesh
990

991
      // For all directions outside the mesh, find the distance that we need
992
      // to travel to reach the next surface. Use the largest distance, as
993
      // only this will cross all outer surfaces.
994
      int k_max {0};
77,326,932✔
995
      for (int k = 0; k < n; ++k) {
307,161,472✔
996
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
308,399,804✔
997
            (distances[k].distance > traveled_distance)) {
78,565,264✔
998
          traveled_distance = distances[k].distance;
77,839,850✔
999
          k_max = k;
77,839,850✔
1000
        }
1001
      }
1002

1003
      // If r1 is not inside the mesh, exit here
1004
      if (traveled_distance >= total_distance)
77,326,932✔
1005
        return;
72,161,103✔
1006

1007
      // Calculate the new cell index and update all distances to next
1008
      // surfaces.
1009
      ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
5,165,829✔
1010
      for (int k = 0; k < n; ++k) {
20,453,161✔
1011
        distances[k] =
15,287,332✔
1012
          distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
15,287,332✔
1013
      }
1014

1015
      // If inside the mesh, Tally inward current
1016
      if (in_mesh)
5,165,829✔
1017
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
3,901,995✔
1018
    }
1019
  }
1020
}
1021

131,513,198✔
1022
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1023
  vector<int>& bins, vector<double>& lengths) const
1024
{
1025

1026
  // Helper tally class.
1027
  // stores a pointer to the mesh class and references to bins and lengths
1028
  // parameters. Performs the actual tally through the track method.
1029
  struct TrackAggregator {
1030
    TrackAggregator(
1031
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
131,513,198✔
1032
      : mesh(_mesh), bins(_bins), lengths(_lengths)
131,513,198✔
UNCOV
1033
    {}
×
1034
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1035
    void track(const MeshIndex& ijk, double l) const
131,513,198✔
1036
    {
1037
      bins.push_back(mesh->get_bin_from_indices(ijk));
1038
      lengths.push_back(l);
1039
    }
1040

1041
    const StructuredMesh* mesh;
131,513,198✔
1042
    vector<int>& bins;
1043
    vector<double>& lengths;
1044
  };
1045

131,513,198✔
1046
  // Perform the mesh raytrace with the helper class.
1047
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1048
}
1049

131,513,198✔
UNCOV
1050
void StructuredMesh::surface_bins_crossed(
×
1051
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
1052
{
UNCOV
1053

×
1054
  // Helper tally class.
1055
  // stores a pointer to the mesh class and a reference to the bins parameter.
1056
  // Performs the actual tally through the surface method.
1057
  struct SurfaceAggregator {
1058
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
1059
      : mesh(_mesh), bins(_bins)
131,513,198✔
1060
    {}
131,513,198✔
1061
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
1062
    {
1063
      int i_bin =
263,026,396✔
1064
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
524,369,660✔
1065
      if (max)
392,856,462✔
1066
        i_bin += 2;
1067
      if (inward)
1068
        i_bin += 1;
1069
      bins.push_back(i_bin);
34,285,053✔
1070
    }
1071
    void track(const MeshIndex& idx, double l) const {}
165,798,251✔
1072

1073
    const StructuredMesh* mesh;
1074
    vector<int>& bins;
163,756,601✔
1075
  };
163,756,601✔
1076

1077
  // Perform the mesh raytrace with the helper class.
1078
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
163,756,601✔
1079
}
163,756,601✔
1080

1081
//==============================================================================
1082
// RegularMesh implementation
1083
//==============================================================================
163,756,601✔
1084

163,756,601✔
1085
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
129,694,837✔
1086
{
1087
  // Determine number of dimensions for mesh
1088
  if (!check_for_node(node, "dimension")) {
1089
    fatal_error("Must specify <dimension> on a regular mesh.");
34,061,764✔
1090
  }
1091

1092
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
1093
  int n = n_dimension_ = shape.size();
34,061,764✔
1094
  if (n != 1 && n != 2 && n != 3) {
34,061,764✔
1095
    fatal_error("Mesh must be one, two, or three dimensions.");
34,061,764✔
1096
  }
1097
  std::copy(shape.begin(), shape.end(), shape_.begin());
1098

34,061,764✔
1099
  // Check that dimensions are all greater than zero
1100
  if (xt::any(shape <= 0)) {
1101
    fatal_error("All entries on the <dimension> element for a tally "
1102
                "mesh must be positive.");
34,061,764✔
1103
  }
32,706,965✔
1104

1105
  // Check for lower-left coordinates
1106
  if (check_for_node(node, "lower_left")) {
1107
    // Read mesh lower-left corner location
1108
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1109
  } else {
1110
    fatal_error("Must specify <lower_left> on a mesh.");
2,041,650✔
1111
  }
7,844,575✔
1112

7,988,081✔
1113
  // Make sure lower_left and dimension match
2,185,156✔
1114
  if (shape.size() != lower_left_.size()) {
2,126,064✔
1115
    fatal_error("Number of entries on <lower_left> must be the same "
2,126,064✔
1116
                "as the number of entries on <dimension>.");
1117
  }
1118

1119
  if (check_for_node(node, "width")) {
1120
    // Make sure one of upper-right or width were specified
2,041,650✔
1121
    if (check_for_node(node, "upper_right")) {
1,818,361✔
1122
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1123
    }
1124

1125
    width_ = get_node_xarray<double>(node, "width");
223,289✔
1126

788,568✔
1127
    // Check to ensure width has same dimensions
565,279✔
1128
    auto n = width_.size();
565,279✔
1129
    if (n != lower_left_.size()) {
1130
      fatal_error("Number of entries on <width> must be the same as "
1131
                  "the number of entries on <lower_left>.");
1132
    }
223,289✔
1133

200,376✔
1134
    // Check for negative widths
1135
    if (xt::any(width_ < 0.0)) {
1136
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1137
    }
773,005,269✔
1138

1139
    // Set width and upper right coordinate
1140
    upper_right_ = xt::eval(lower_left_ + shape * width_);
1141

1142
  } else if (check_for_node(node, "upper_right")) {
1143
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1144

1145
    // Check to ensure width has same dimensions
1146
    auto n = upper_right_.size();
1147
    if (n != lower_left_.size()) {
773,005,269✔
1148
      fatal_error("Number of entries on <upper_right> must be the "
773,005,269✔
1149
                  "same as the number of entries on <lower_left>.");
9,061,235✔
1150
    }
1151

763,944,034✔
1152
    // Check that upper-right is above lower-left
1153
    if (xt::any(upper_right_ < lower_left_)) {
1154
      fatal_error("The <upper_right> coordinates must be greater than "
1155
                  "the <lower_left> coordinates on a tally mesh.");
1156
    }
1157

763,944,034✔
1158
    // Set width
1159
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1160
  } else {
1161
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
763,944,034✔
1162
  }
1163

1164
  // Set material volumes
1165
  volume_frac_ = 1.0 / xt::prod(shape)();
763,944,034✔
1166

646,283✔
1167
  element_volume_ = 1.0;
646,272✔
1168
  for (int i = 0; i < n_dimension_; i++) {
1169
    element_volume_ *= width_[i];
646,283✔
1170
  }
1171
}
1172

1173
int RegularMesh::get_index_in_direction(double r, int i) const
1174
{
1175
  return std::ceil((r - lower_left_[i]) / width_[i]);
763,297,751✔
1176
}
763,297,751✔
1177

1178
const std::string RegularMesh::mesh_type = "regular";
1179

1,526,595,502✔
1180
std::string RegularMesh::get_mesh_type() const
2,147,483,647✔
1181
{
2,147,483,647✔
1182
  return mesh_type;
1183
}
1184

1185
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
704,788,235✔
1186
{
1187
  return lower_left_[i] + ijk[i] * width_[i];
1,468,085,986✔
1188
}
1189

1190
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1,392,800,704✔
1191
{
1,392,800,704✔
1192
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1193
}
1194

1,392,800,704✔
1195
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
1,392,800,704✔
1196
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1197
  double l) const
1198
{
1199
  MeshDistance d;
1,392,800,704✔
1200
  d.next_index = ijk[i];
1,392,800,704✔
1201
  if (std::abs(u[i]) < FP_PRECISION)
692,955,009✔
1202
    return d;
1203

1204
  d.max_surface = (u[i] > 0);
1205
  if (d.max_surface && (ijk[i] <= shape_[i])) {
699,845,695✔
1206
    d.next_index++;
1207
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1208
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1209
    d.next_index--;
699,845,695✔
1210
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
699,845,695✔
1211
  }
699,845,695✔
1212
  return d;
1213
}
1214

699,845,695✔
1215
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1216
  Position plot_ll, Position plot_ur) const
1217
{
1218
  // Figure out which axes lie in the plane of the plot.
699,845,695✔
1219
  array<int, 2> axes {-1, -1};
685,732,872✔
1220
  if (plot_ur.z == plot_ll.z) {
1221
    axes[0] = 0;
1222
    if (n_dimension_ > 1)
1223
      axes[1] = 1;
1224
  } else if (plot_ur.y == plot_ll.y) {
1225
    axes[0] = 0;
1226
    if (n_dimension_ > 2)
75,285,282✔
1227
      axes[1] = 2;
299,316,897✔
1228
  } else if (plot_ur.x == plot_ll.x) {
300,411,723✔
1229
    if (n_dimension_ > 1)
76,380,108✔
1230
      axes[0] = 1;
75,713,786✔
1231
    if (n_dimension_ > 2)
75,713,786✔
1232
      axes[1] = 2;
1233
  } else {
1234
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1235
  }
1236

75,285,282✔
1237
  // Get the coordinates of the mesh lines along both of the axes.
70,342,742✔
1238
  array<vector<double>, 2> axis_lines;
1239
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1240
    int axis = axes[i_ax];
1241
    if (axis == -1)
4,942,540✔
1242
      continue;
19,664,593✔
1243
    auto& lines {axis_lines[i_ax]};
14,722,053✔
1244

14,722,053✔
1245
    double coord = lower_left_[axis];
1246
    for (int i = 0; i < shape_[axis] + 1; ++i) {
1247
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1248
        lines.push_back(coord);
4,942,540✔
1249
      coord += width_[axis];
3,701,619✔
1250
    }
1251
  }
1252

1253
  return {axis_lines[0], axis_lines[1]};
1254
}
773,005,269✔
1255

1256
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1257
{
1258
  write_dataset(mesh_group, "dimension", get_x_shape());
1259
  write_dataset(mesh_group, "lower_left", lower_left_);
1260
  write_dataset(mesh_group, "upper_right", upper_right_);
1261
  write_dataset(mesh_group, "width", width_);
1262
}
773,005,269✔
1263

1264
xt::xtensor<double, 1> RegularMesh::count_sites(
773,005,269✔
1265
  const SourceSite* bank, int64_t length, bool* outside) const
773,005,269✔
1266
{
1,389,280,186✔
1267
  // Determine shape of array for counts
1,393,446,976✔
1268
  std::size_t m = this->n_bins();
1269
  vector<std::size_t> shape = {m};
1,393,446,976✔
1270

1,393,446,976✔
1271
  // Create array of zeros
1,393,446,976✔
1272
  xt::xarray<double> cnt {shape, 0.0};
1273
  bool outside_ = false;
1274

1275
  for (int64_t i = 0; i < length; i++) {
1276
    const auto& site = bank[i];
1277

1278
    // determine scoring bin for entropy mesh
1279
    int mesh_bin = get_bin(site.r);
773,005,269✔
1280

773,005,269✔
1281
    // if outside mesh, skip particle
1282
    if (mesh_bin < 0) {
131,513,198✔
1283
      outside_ = true;
1284
      continue;
1285
    }
1286

1287
    // Add to appropriate bin
1288
    cnt(mesh_bin) += site.wgt;
1289
  }
1290

131,513,198✔
1291
  // Create copy of count data. Since ownership will be acquired by xtensor,
131,513,198✔
1292
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
131,513,198✔
1293
  // warnings.
66,969,105✔
1294
  int total = cnt.size();
1295
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
1296

66,969,105✔
1297
#ifdef OPENMC_MPI
66,969,105✔
1298
  // collect values from all processors
33,448,809✔
1299
  MPI_Reduce(
66,969,105✔
1300
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
32,907,341✔
1301

66,969,105✔
1302
  // Check if there were sites outside the mesh for any processor
66,969,105✔
1303
  if (outside) {
163,756,601✔
1304
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1305
  }
1306
#else
1307
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1308
  if (outside)
1309
    *outside = outside_;
1310
#endif
131,513,198✔
1311

131,513,198✔
1312
  // Adapt reduced values in array back into an xarray
1313
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1314
  xt::xarray<double> counts = arr;
1315

1316
  return counts;
1317
}
1,818✔
1318

1319
double RegularMesh::volume(const MeshIndex& ijk) const
1320
{
1,818✔
UNCOV
1321
  return element_volume_;
×
1322
}
1323

1324
//==============================================================================
1,818✔
1325
// RectilinearMesh implementation
1,818✔
1326
//==============================================================================
1,818✔
UNCOV
1327

×
1328
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1329
{
1,818✔
1330
  n_dimension_ = 3;
1331

1332
  grid_[0] = get_node_array<double>(node, "x_grid");
1,818✔
UNCOV
1333
  grid_[1] = get_node_array<double>(node, "y_grid");
×
1334
  grid_[2] = get_node_array<double>(node, "z_grid");
1335

1336
  if (int err = set_grid()) {
1337
    fatal_error(openmc_err_msg);
1338
  }
1,818✔
1339
}
1340

1,818✔
1341
const std::string RectilinearMesh::mesh_type = "rectilinear";
UNCOV
1342

×
1343
std::string RectilinearMesh::get_mesh_type() const
1344
{
1345
  return mesh_type;
1346
}
1,818✔
UNCOV
1347

×
1348
double RectilinearMesh::positive_grid_boundary(
1349
  const MeshIndex& ijk, int i) const
1350
{
1351
  return grid_[i][ijk[i]];
1,818✔
1352
}
1353

48✔
UNCOV
1354
double RectilinearMesh::negative_grid_boundary(
×
1355
  const MeshIndex& ijk, int i) const
1356
{
1357
  return grid_[i][ijk[i] - 1];
48✔
1358
}
1359

1360
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
48✔
1361
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
48✔
1362
  double l) const
×
1363
{
1364
  MeshDistance d;
1365
  d.next_index = ijk[i];
1366
  if (std::abs(u[i]) < FP_PRECISION)
1367
    return d;
48✔
UNCOV
1368

×
1369
  d.max_surface = (u[i] > 0);
1370
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1371
    d.next_index++;
1372
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
48✔
1373
  } else if (!d.max_surface && (ijk[i] > 0)) {
1374
    d.next_index--;
1,770✔
1375
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,770✔
1376
  }
1377
  return d;
1378
}
1,770✔
1379

1,770✔
UNCOV
1380
int RectilinearMesh::set_grid()
×
1381
{
1382
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1383
    static_cast<int>(grid_[1].size()) - 1,
1384
    static_cast<int>(grid_[2].size()) - 1};
1385

1,770✔
UNCOV
1386
  for (const auto& g : grid_) {
×
1387
    if (g.size() < 2) {
1388
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1389
                 "must each have at least 2 points");
1390
      return OPENMC_E_INVALID_ARGUMENT;
1391
    }
1,770✔
1392
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
UNCOV
1393
        g.end()) {
×
1394
      set_errmsg("Values in for x-, y-, and z- grids for "
1395
                 "rectilinear meshes must be sorted and unique.");
1396
      return OPENMC_E_INVALID_ARGUMENT;
1397
    }
1,818✔
1398
  }
1399

1,818✔
1400
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
6,909✔
1401
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
5,091✔
1402

1403
  return 0;
1,818✔
1404
}
1405

2,147,483,647✔
1406
int RectilinearMesh::get_index_in_direction(double r, int i) const
1407
{
2,147,483,647✔
1408
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1409
}
1410

1411
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1412
  Position plot_ll, Position plot_ur) const
3,067✔
1413
{
1414
  // Figure out which axes lie in the plane of the plot.
3,067✔
1415
  array<int, 2> axes {-1, -1};
1416
  if (plot_ur.z == plot_ll.z) {
1417
    axes = {0, 1};
1,454,045,654✔
1418
  } else if (plot_ur.y == plot_ll.y) {
1419
    axes = {0, 2};
1,454,045,654✔
1420
  } else if (plot_ur.x == plot_ll.x) {
1421
    axes = {1, 2};
1422
  } else {
1,391,184,064✔
1423
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1424
  }
1,391,184,064✔
1425

1426
  // Get the coordinates of the mesh lines along both of the axes.
1427
  array<vector<double>, 2> axis_lines;
2,147,483,647✔
1428
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1429
    int axis = axes[i_ax];
1430
    vector<double>& lines {axis_lines[i_ax]};
1431

2,147,483,647✔
1432
    for (auto coord : grid_[axis]) {
2,147,483,647✔
1433
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
2,147,483,647✔
1434
        lines.push_back(coord);
1,204,566✔
1435
    }
1436
  }
2,147,483,647✔
1437

2,147,483,647✔
1438
  return {axis_lines[0], axis_lines[1]};
1,449,804,131✔
1439
}
1,449,804,131✔
1440

1,401,253,567✔
1441
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
1,386,942,541✔
1442
{
1,386,942,541✔
1443
  write_dataset(mesh_group, "x_grid", grid_[0]);
1444
  write_dataset(mesh_group, "y_grid", grid_[1]);
2,147,483,647✔
1445
  write_dataset(mesh_group, "z_grid", grid_[2]);
1446
}
1447

22✔
1448
double RectilinearMesh::volume(const MeshIndex& ijk) const
1449
{
1450
  double vol {1.0};
1451

22✔
1452
  for (int i = 0; i < n_dimension_; i++) {
22✔
1453
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
22✔
1454
  }
22✔
1455
  return vol;
22✔
UNCOV
1456
}
×
UNCOV
1457

×
UNCOV
1458
//==============================================================================
×
UNCOV
1459
// CylindricalMesh implementation
×
UNCOV
1460
//==============================================================================
×
UNCOV
1461

×
UNCOV
1462
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
×
UNCOV
1463
  : PeriodicStructuredMesh {node}
×
UNCOV
1464
{
×
1465
  n_dimension_ = 3;
UNCOV
1466
  grid_[0] = get_node_array<double>(node, "r_grid");
×
1467
  grid_[1] = get_node_array<double>(node, "phi_grid");
1468
  grid_[2] = get_node_array<double>(node, "z_grid");
1469
  origin_ = get_node_position(node, "origin");
1470

22✔
1471
  if (int err = set_grid()) {
66✔
1472
    fatal_error(openmc_err_msg);
44✔
1473
  }
44✔
UNCOV
1474
}
×
1475

44✔
1476
const std::string CylindricalMesh::mesh_type = "cylindrical";
1477

44✔
1478
std::string CylindricalMesh::get_mesh_type() const
286✔
1479
{
242✔
1480
  return mesh_type;
242✔
1481
}
242✔
1482

1483
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
1484
  Position r, bool& in_mesh) const
1485
{
44✔
1486
  local_coords(r);
22✔
1487

1488
  Position mapped_r;
1,892✔
1489
  mapped_r[0] = std::hypot(r.x, r.y);
1490
  mapped_r[2] = r[2];
1,892✔
1491

1,892✔
1492
  if (mapped_r[0] < FP_PRECISION) {
1,892✔
1493
    mapped_r[1] = 0.0;
1,892✔
1494
  } else {
1,892✔
1495
    mapped_r[1] = std::atan2(r.y, r.x);
1496
    if (mapped_r[1] < 0)
8,409✔
1497
      mapped_r[1] += 2 * M_PI;
1498
  }
1499

1500
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
8,409✔
1501

8,409✔
1502
  idx[1] = sanitize_phi(idx[1]);
1503

1504
  return idx;
8,409✔
1505
}
8,409✔
1506

1507
Position CylindricalMesh::sample_element(
8,247,479✔
1508
  const MeshIndex& ijk, uint64_t* seed) const
8,239,070✔
1509
{
1510
  double r_min = this->r(ijk[0] - 1);
1511
  double r_max = this->r(ijk[0]);
8,239,070✔
1512

1513
  double phi_min = this->phi(ijk[1] - 1);
1514
  double phi_max = this->phi(ijk[1]);
8,239,070✔
UNCOV
1515

×
UNCOV
1516
  double z_min = this->z(ijk[2] - 1);
×
1517
  double z_max = this->z(ijk[2]);
1518

1519
  double r_min_sq = r_min * r_min;
1520
  double r_max_sq = r_max * r_max;
8,239,070✔
1521
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1522
  double phi = uniform_distribution(phi_min, phi_max, seed);
1523
  double z = uniform_distribution(z_min, z_max, seed);
1524

1525
  double x = r * std::cos(phi);
1526
  double y = r * std::sin(phi);
8,409✔
1527

8,409✔
1528
  return origin_ + Position(x, y, z);
1529
}
1530

1531
double CylindricalMesh::find_r_crossing(
4,185✔
1532
  const Position& r, const Direction& u, double l, int shell) const
4,185✔
1533
{
1534

1535
  if ((shell < 0) || (shell > shape_[0]))
4,185✔
1536
    return INFTY;
4,185✔
1537

1538
  // solve r.x^2 + r.y^2 == r0^2
1539
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
4,224✔
1540
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,224✔
1541

4,224✔
1542
  const double r0 = grid_[0][shell];
1543
  if (r0 == 0.0)
1544
    return INFTY;
1545

8,409✔
1546
  const double denominator = u.x * u.x + u.y * u.y;
8,409✔
1547

1548
  // Direction of flight is in z-direction. Will never intersect r.
16,818✔
1549
  if (std::abs(denominator) < FP_PRECISION)
8,409✔
1550
    return INFTY;
1551

7,451,670✔
1552
  // inverse of dominator to help the compiler to speed things up
1553
  const double inv_denominator = 1.0 / denominator;
7,451,670✔
1554

1555
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
1556
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1557
  double D = p * p - c * inv_denominator;
1558

1559
  if (D < 0.0)
1560
    return INFTY;
103✔
1561

1562
  D = std::sqrt(D);
103✔
1563

1564
  // the solution -p - D is always smaller as -p + D : Check this one first
103✔
1565
  if (std::abs(c) <= RADIAL_MESH_TOL)
103✔
1566
    return INFTY;
103✔
1567

1568
  if (-p - D > l)
103✔
UNCOV
1569
    return -p - D;
×
1570
  if (-p + D > l)
1571
    return -p + D;
103✔
1572

1573
  return INFTY;
1574
}
1575

261✔
1576
double CylindricalMesh::find_phi_crossing(
1577
  const Position& r, const Direction& u, double l, int shell) const
261✔
1578
{
1579
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1580
  if (full_phi_ && (shape_[1] == 1))
30,573,125✔
1581
    return INFTY;
1582

1583
  shell = sanitize_phi(shell);
30,573,125✔
1584

1585
  const double p0 = grid_[1][shell];
1586

29,822,735✔
1587
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1588
  // => x(s) * cos(p0) = y(s) * sin(p0)
1589
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
29,822,735✔
1590
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1591

1592
  const double c0 = std::cos(p0);
61,780,330✔
1593
  const double s0 = std::sin(p0);
1594

1595
  const double denominator = (u.x * s0 - u.y * c0);
1596

61,780,330✔
1597
  // Check if direction of flight is not parallel to phi surface
61,780,330✔
1598
  if (std::abs(denominator) > FP_PRECISION) {
61,780,330✔
1599
    const double s = -(r.x * s0 - r.y * c0) / denominator;
571,824✔
1600
    // Check if solution is in positive direction of flight and crosses the
1601
    // correct phi surface (not -phi)
61,208,506✔
1602
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
61,208,506✔
1603
      return s;
30,573,125✔
1604
  }
30,573,125✔
1605

30,635,381✔
1606
  return INFTY;
29,822,735✔
1607
}
29,822,735✔
1608

1609
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
61,208,506✔
1610
  const Position& r, const Direction& u, double l, int shell) const
1611
{
1612
  MeshDistance d;
151✔
1613
  d.next_index = shell;
1614

151✔
1615
  // Direction of flight is within xy-plane. Will never intersect z.
151✔
1616
  if (std::abs(u.z) < FP_PRECISION)
151✔
1617
    return d;
1618

604✔
1619
  d.max_surface = (u.z > 0.0);
453✔
1620
  if (d.max_surface && (shell <= shape_[2])) {
×
1621
    d.next_index += 1;
1622
    d.distance = (grid_[2][shell] - r.z) / u.z;
×
1623
  } else if (!d.max_surface && (shell > 0)) {
1624
    d.next_index -= 1;
453✔
1625
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
906✔
UNCOV
1626
  }
×
1627
  return d;
1628
}
×
1629

1630
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1631
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1632
  double l) const
151✔
1633
{
151✔
1634
  Position r = r0 - origin_;
1635

151✔
1636
  if (i == 0) {
1637

1638
    return std::min(
86,309,547✔
1639
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1640
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
86,309,547✔
1641

1642
  } else if (i == 1) {
1643

11✔
1644
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1645
                      find_phi_crossing(r, u, l, ijk[i])),
1646
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1647
        find_phi_crossing(r, u, l, ijk[i] - 1)));
11✔
1648

11✔
1649
  } else {
×
1650
    return find_z_crossing(r, u, l, ijk[i]);
11✔
1651
  }
11✔
UNCOV
1652
}
×
UNCOV
1653

×
1654
int CylindricalMesh::set_grid()
UNCOV
1655
{
×
1656
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1657
    static_cast<int>(grid_[1].size()) - 1,
1658
    static_cast<int>(grid_[2].size()) - 1};
1659

11✔
1660
  for (const auto& g : grid_) {
33✔
1661
    if (g.size() < 2) {
22✔
1662
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
22✔
1663
                 "must each have at least 2 points");
1664
      return OPENMC_E_INVALID_ARGUMENT;
110✔
1665
    }
88✔
1666
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
88✔
1667
        g.end()) {
1668
      set_errmsg("Values in for r-, phi-, and z- grids for "
1669
                 "cylindrical meshes must be sorted and unique.");
1670
      return OPENMC_E_INVALID_ARGUMENT;
22✔
1671
    }
11✔
1672
  }
1673
  if (grid_[0].front() < 0.0) {
92✔
1674
    set_errmsg("r-grid for "
1675
               "cylindrical meshes must start at r >= 0.");
92✔
1676
    return OPENMC_E_INVALID_ARGUMENT;
92✔
1677
  }
92✔
1678
  if (grid_[1].front() < 0.0) {
92✔
1679
    set_errmsg("phi-grid for "
1680
               "cylindrical meshes must start at phi >= 0.");
132✔
1681
    return OPENMC_E_INVALID_ARGUMENT;
1682
  }
132✔
1683
  if (grid_[1].back() > 2.0 * PI) {
1684
    set_errmsg("phi-grids for "
528✔
1685
               "cylindrical meshes must end with theta <= 2*pi.");
396✔
1686

1687
    return OPENMC_E_INVALID_ARGUMENT;
132✔
1688
  }
1689

1690
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
1691

1692
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
1693
    origin_[2] + grid_[2].front()};
1694
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
379✔
1695
    origin_[2] + grid_[2].back()};
379✔
1696

1697
  return 0;
379✔
1698
}
379✔
1699

379✔
1700
int CylindricalMesh::get_index_in_direction(double r, int i) const
379✔
1701
{
379✔
1702
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1703
}
379✔
UNCOV
1704

×
1705
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1706
  Position plot_ll, Position plot_ur) const
379✔
1707
{
1708
  fatal_error("Plot of cylindrical Mesh not implemented");
1709

1710
  // Figure out which axes lie in the plane of the plot.
473✔
1711
  array<vector<double>, 2> axis_lines;
1712
  return {axis_lines[0], axis_lines[1]};
473✔
1713
}
1714

1715
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
46,354,396✔
1716
{
1717
  write_dataset(mesh_group, "r_grid", grid_[0]);
1718
  write_dataset(mesh_group, "phi_grid", grid_[1]);
46,354,396✔
1719
  write_dataset(mesh_group, "z_grid", grid_[2]);
1720
  write_dataset(mesh_group, "origin", origin_);
46,354,396✔
1721
}
46,354,396✔
1722

46,354,396✔
1723
double CylindricalMesh::volume(const MeshIndex& ijk) const
1724
{
46,354,396✔
UNCOV
1725
  double r_i = grid_[0][ijk[0] - 1];
×
1726
  double r_o = grid_[0][ijk[0]];
1727

46,354,396✔
1728
  double phi_i = grid_[1][ijk[1] - 1];
46,354,396✔
1729
  double phi_o = grid_[1][ijk[1]];
23,191,993✔
1730

1731
  double z_i = grid_[2][ijk[2] - 1];
1732
  double z_o = grid_[2][ijk[2]];
46,354,396✔
1733

1734
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
46,354,396✔
1735
}
1736

46,354,396✔
1737
//==============================================================================
1738
// SphericalMesh implementation
1739
//==============================================================================
88,000✔
1740

1741
SphericalMesh::SphericalMesh(pugi::xml_node node)
1742
  : PeriodicStructuredMesh {node}
88,000✔
1743
{
88,000✔
1744
  n_dimension_ = 3;
1745

88,000✔
1746
  grid_[0] = get_node_array<double>(node, "r_grid");
88,000✔
1747
  grid_[1] = get_node_array<double>(node, "theta_grid");
1748
  grid_[2] = get_node_array<double>(node, "phi_grid");
88,000✔
1749
  origin_ = get_node_position(node, "origin");
88,000✔
1750

1751
  if (int err = set_grid()) {
88,000✔
1752
    fatal_error(openmc_err_msg);
88,000✔
1753
  }
88,000✔
1754
}
88,000✔
1755

88,000✔
1756
const std::string SphericalMesh::mesh_type = "spherical";
1757

88,000✔
1758
std::string SphericalMesh::get_mesh_type() const
88,000✔
1759
{
1760
  return mesh_type;
88,000✔
1761
}
1762

1763
StructuredMesh::MeshIndex SphericalMesh::get_indices(
136,361,126✔
1764
  Position r, bool& in_mesh) const
1765
{
1766
  local_coords(r);
1767

136,361,126✔
1768
  Position mapped_r;
16,738,502✔
1769
  mapped_r[0] = r.norm();
1770

1771
  if (mapped_r[0] < FP_PRECISION) {
1772
    mapped_r[1] = 0.0;
1773
    mapped_r[2] = 0.0;
1774
  } else {
119,622,624✔
1775
    mapped_r[1] = std::acos(r.z / mapped_r.x);
119,622,624✔
1776
    mapped_r[2] = std::atan2(r.y, r.x);
6,667,485✔
1777
    if (mapped_r[2] < 0)
1778
      mapped_r[2] += 2 * M_PI;
112,955,139✔
1779
  }
1780

1781
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
112,955,139✔
1782

58,960✔
1783
  idx[1] = sanitize_theta(idx[1]);
1784
  idx[2] = sanitize_phi(idx[2]);
1785

112,896,179✔
1786
  return idx;
1787
}
112,896,179✔
1788

112,896,179✔
1789
Position SphericalMesh::sample_element(
112,896,179✔
1790
  const MeshIndex& ijk, uint64_t* seed) const
1791
{
112,896,179✔
1792
  double r_min = this->r(ijk[0] - 1);
15,592,401✔
1793
  double r_max = this->r(ijk[0]);
1794

97,303,778✔
1795
  double theta_min = this->theta(ijk[1] - 1);
1796
  double theta_max = this->theta(ijk[1]);
1797

97,303,778✔
1798
  double phi_min = this->phi(ijk[2] - 1);
6,469,408✔
1799
  double phi_max = this->phi(ijk[2]);
1800

90,834,370✔
1801
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
18,559,706✔
1802
  double sin_theta = std::sin(std::acos(cos_theta));
72,274,664✔
1803
  double phi = uniform_distribution(phi_min, phi_max, seed);
43,873,610✔
1804
  double r_min_cub = std::pow(r_min, 3);
1805
  double r_max_cub = std::pow(r_max, 3);
28,401,054✔
1806
  // might be faster to do rejection here?
1807
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
1808

68,093,014✔
1809
  double x = r * std::cos(phi) * sin_theta;
1810
  double y = r * std::sin(phi) * sin_theta;
1811
  double z = r * cos_theta;
1812

68,093,014✔
1813
  return origin_ + Position(x, y, z);
29,760,896✔
1814
}
1815

38,332,118✔
1816
double SphericalMesh::find_r_crossing(
1817
  const Position& r, const Direction& u, double l, int shell) const
38,332,118✔
1818
{
1819
  if ((shell < 0) || (shell > shape_[0]))
1820
    return INFTY;
1821

1822
  // solve |r+s*u| = r0
1823
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
1824
  const double r0 = grid_[0][shell];
38,332,118✔
1825
  if (r0 == 0.0)
38,332,118✔
1826
    return INFTY;
1827
  const double p = r.dot(u);
38,332,118✔
1828
  double c = r.dot(r) - r0 * r0;
1829
  double D = p * p - c;
1830

38,332,118✔
1831
  if (std::abs(c) <= RADIAL_MESH_TOL)
38,071,374✔
1832
    return INFTY;
1833

1834
  if (D >= 0.0) {
38,071,374✔
1835
    D = std::sqrt(D);
17,817,756✔
1836
    // the solution -p - D is always smaller as -p + D : Check this one first
1837
    if (-p - D > l)
1838
      return -p - D;
20,514,362✔
1839
    if (-p + D > l)
1840
      return -p + D;
1841
  }
36,659,744✔
1842

1843
  return INFTY;
1844
}
36,659,744✔
1845

36,659,744✔
1846
double SphericalMesh::find_theta_crossing(
1847
  const Position& r, const Direction& u, double l, int shell) const
1848
{
36,659,744✔
1849
  // Theta grid is [0, π], thus there is no real surface to cross
1,118,216✔
1850
  if (full_theta_ && (shape_[1] == 1))
1851
    return INFTY;
35,541,528✔
1852

35,541,528✔
1853
  shell = sanitize_theta(shell);
16,288,668✔
1854

16,288,668✔
1855
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
19,252,860✔
1856
  // yields
16,534,419✔
1857
  // a*s^2 + 2*b*s + c == 0 with
16,534,419✔
1858
  // a = cos(theta)^2 - u.z * u.z
1859
  // b = r*u * cos(theta)^2 - u.z * r.z
35,541,528✔
1860
  // c = r*r * cos(theta)^2 - r.z^2
1861

1862
  const double cos_t = std::cos(grid_[1][shell]);
138,886,814✔
1863
  const bool sgn = std::signbit(cos_t);
1864
  const double cos_t_2 = cos_t * cos_t;
1865

1866
  const double a = cos_t_2 - u.z * u.z;
138,886,814✔
1867
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1868
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
138,886,814✔
1869

1870
  // if factor of s^2 is zero, direction of flight is parallel to theta
68,180,563✔
1871
  // surface
68,180,563✔
1872
  if (std::abs(a) < FP_PRECISION) {
136,361,126✔
1873
    // if b vanishes, direction of flight is within theta surface and crossing
1874
    // is not possible
70,706,251✔
1875
    if (std::abs(b) < FP_PRECISION)
1876
      return INFTY;
34,046,507✔
1877

34,046,507✔
1878
    const double s = -0.5 * c / b;
34,046,507✔
1879
    // Check if solution is in positive direction of flight and has correct
68,093,014✔
1880
    // sign
1881
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1882
      return s;
36,659,744✔
1883

1884
    // no crossing is possible
1885
    return INFTY;
1886
  }
401✔
1887

1888
  const double p = b / a;
401✔
1889
  double D = p * p - c / a;
401✔
1890

401✔
1891
  if (D < 0.0)
1892
    return INFTY;
1,604✔
1893

1,203✔
UNCOV
1894
  D = std::sqrt(D);
×
1895

UNCOV
1896
  // the solution -p-D is always smaller as -p+D : Check this one first
×
1897
  double s = -p - D;
1898
  // Check if solution is in positive direction of flight and has correct sign
1,203✔
1899
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
2,406✔
UNCOV
1900
    return s;
×
1901

UNCOV
1902
  s = -p + D;
×
1903
  // Check if solution is in positive direction of flight and has correct sign
1904
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1905
    return s;
401✔
UNCOV
1906

×
1907
  return INFTY;
UNCOV
1908
}
×
1909

1910
double SphericalMesh::find_phi_crossing(
401✔
UNCOV
1911
  const Position& r, const Direction& u, double l, int shell) const
×
1912
{
UNCOV
1913
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
×
1914
  if (full_phi_ && (shape_[2] == 1))
1915
    return INFTY;
401✔
1916

×
1917
  shell = sanitize_phi(shell);
1918

UNCOV
1919
  const double p0 = grid_[2][shell];
×
1920

1921
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1922
  // => x(s) * cos(p0) = y(s) * sin(p0)
401✔
1923
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1924
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
802✔
1925

802✔
1926
  const double c0 = std::cos(p0);
802✔
1927
  const double s0 = std::sin(p0);
802✔
1928

1929
  const double denominator = (u.x * s0 - u.y * c0);
401✔
1930

1931
  // Check if direction of flight is not parallel to phi surface
1932
  if (std::abs(denominator) > FP_PRECISION) {
139,063,188✔
1933
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1934
    // Check if solution is in positive direction of flight and crosses the
139,063,188✔
1935
    // correct phi surface (not -phi)
1936
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
1937
      return s;
×
1938
  }
1939

UNCOV
1940
  return INFTY;
×
1941
}
1942

1943
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1944
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1945
  double l) const
1946
{
1947

363✔
1948
  if (i == 0) {
1949
    return std::min(
363✔
1950
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
363✔
1951
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
363✔
1952

363✔
1953
  } else if (i == 1) {
363✔
1954
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
1955
                      find_theta_crossing(r0, u, l, ijk[i])),
352✔
1956
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
1957
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
352✔
1958

352✔
1959
  } else {
1960
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
352✔
1961
                      find_phi_crossing(r0, u, l, ijk[i])),
352✔
1962
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1963
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
352✔
1964
  }
352✔
1965
}
1966

352✔
1967
int SphericalMesh::set_grid()
1968
{
1969
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1970
    static_cast<int>(grid_[1].size()) - 1,
1971
    static_cast<int>(grid_[2].size()) - 1};
1972

1973
  for (const auto& g : grid_) {
291✔
1974
    if (g.size() < 2) {
291✔
1975
      set_errmsg("x-, y-, and z- grids for spherical meshes "
1976
                 "must each have at least 2 points");
291✔
1977
      return OPENMC_E_INVALID_ARGUMENT;
1978
    }
291✔
1979
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
291✔
1980
        g.end()) {
291✔
1981
      set_errmsg("Values in for r-, theta-, and phi- grids for "
291✔
1982
                 "spherical meshes must be sorted and unique.");
1983
      return OPENMC_E_INVALID_ARGUMENT;
291✔
UNCOV
1984
    }
×
1985
    if (g.front() < 0.0) {
1986
      set_errmsg("r-, theta-, and phi- grids for "
291✔
1987
                 "spherical meshes must start at v >= 0.");
1988
      return OPENMC_E_INVALID_ARGUMENT;
1989
    }
1990
  }
341✔
1991
  if (grid_[1].back() > PI) {
1992
    set_errmsg("theta-grids for "
341✔
1993
               "spherical meshes must end with theta <= pi.");
1994

1995
    return OPENMC_E_INVALID_ARGUMENT;
67,606,671✔
1996
  }
1997
  if (grid_[2].back() > 2 * PI) {
1998
    set_errmsg("phi-grids for "
67,606,671✔
1999
               "spherical meshes must end with phi <= 2*pi.");
2000
    return OPENMC_E_INVALID_ARGUMENT;
67,606,671✔
2001
  }
67,606,671✔
2002

2003
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
67,606,671✔
UNCOV
2004
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
×
UNCOV
2005

×
2006
  double r = grid_[0].back();
2007
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
67,606,671✔
2008
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
67,606,671✔
2009

67,606,671✔
2010
  return 0;
33,808,808✔
2011
}
2012

2013
int SphericalMesh::get_index_in_direction(double r, int i) const
67,606,671✔
2014
{
2015
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
67,606,671✔
2016
}
67,606,671✔
2017

2018
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
67,606,671✔
2019
  Position plot_ll, Position plot_ur) const
2020
{
UNCOV
2021
  fatal_error("Plot of spherical Mesh not implemented");
×
2022

2023
  // Figure out which axes lie in the plane of the plot.
2024
  array<vector<double>, 2> axis_lines;
×
UNCOV
2025
  return {axis_lines[0], axis_lines[1]};
×
2026
}
UNCOV
2027

×
UNCOV
2028
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
×
2029
{
UNCOV
2030
  write_dataset(mesh_group, "r_grid", grid_[0]);
×
2031
  write_dataset(mesh_group, "theta_grid", grid_[1]);
×
2032
  write_dataset(mesh_group, "phi_grid", grid_[2]);
UNCOV
2033
  write_dataset(mesh_group, "origin", origin_);
×
UNCOV
2034
}
×
2035

×
2036
double SphericalMesh::volume(const MeshIndex& ijk) const
×
UNCOV
2037
{
×
2038
  double r_i = grid_[0][ijk[0] - 1];
UNCOV
2039
  double r_o = grid_[0][ijk[0]];
×
2040

UNCOV
2041
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
2042
  double theta_o = grid_[1][ijk[1]];
×
2043

×
2044
  double phi_i = grid_[2][ijk[2] - 1];
UNCOV
2045
  double phi_o = grid_[2][ijk[2]];
×
2046

2047
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
2048
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
441,480,512✔
2049
}
2050

2051
//==============================================================================
441,480,512✔
2052
// Helper functions for the C API
40,361,585✔
2053
//==============================================================================
2054

2055
int check_mesh(int32_t index)
2056
{
401,118,927✔
2057
  if (index < 0 || index >= model::meshes.size()) {
401,118,927✔
2058
    set_errmsg("Index in meshes array is out of bounds.");
6,967,752✔
2059
    return OPENMC_E_OUT_OF_BOUNDS;
394,151,175✔
2060
  }
394,151,175✔
2061
  return 0;
394,151,175✔
2062
}
2063

394,151,175✔
2064
template<class T>
10,586,180✔
2065
int check_mesh_type(int32_t index)
2066
{
383,564,995✔
2067
  if (int err = check_mesh(index))
356,433,220✔
2068
    return err;
2069

356,433,220✔
2070
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
63,719,645✔
2071
  if (!mesh) {
292,713,575✔
2072
    set_errmsg("This function is not valid for input mesh.");
176,340,659✔
2073
    return OPENMC_E_INVALID_TYPE;
2074
  }
2075
  return 0;
143,504,691✔
2076
}
2077

2078
template<class T>
108,366,060✔
2079
bool is_mesh_type(int32_t index)
2080
{
2081
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2082
  return mesh;
108,366,060✔
2083
}
70,123,416✔
2084

2085
//==============================================================================
38,242,644✔
2086
// C API functions
2087
//==============================================================================
2088

2089
// Return the type of mesh as a C string
2090
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2091
{
2092
  if (int err = check_mesh(index))
2093
    return err;
2094

38,242,644✔
2095
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
38,242,644✔
2096

38,242,644✔
2097
  return 0;
2098
}
38,242,644✔
2099

38,242,644✔
2100
//! Extend the meshes array by n elements
38,242,644✔
2101
extern "C" int openmc_extend_meshes(
2102
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2103
{
2104
  if (index_start)
38,242,644✔
2105
    *index_start = model::meshes.size();
2106
  std::string mesh_type;
2107

482,548✔
2108
  for (int i = 0; i < n; ++i) {
482,548✔
2109
    if (RegularMesh::mesh_type == type) {
UNCOV
2110
      model::meshes.push_back(make_unique<RegularMesh>());
×
2111
    } else if (RectilinearMesh::mesh_type == type) {
2112
      model::meshes.push_back(make_unique<RectilinearMesh>());
UNCOV
2113
    } else if (CylindricalMesh::mesh_type == type) {
×
UNCOV
2114
      model::meshes.push_back(make_unique<CylindricalMesh>());
×
2115
    } else if (SphericalMesh::mesh_type == type) {
2116
      model::meshes.push_back(make_unique<SphericalMesh>());
UNCOV
2117
    } else {
×
2118
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
2119
    }
2120
  }
37,760,096✔
2121
  if (index_end)
37,760,096✔
2122
    *index_end = model::meshes.size() - 1;
2123

37,760,096✔
2124
  return 0;
10,959,168✔
2125
}
2126

26,800,928✔
2127
//! Adds a new unstructured mesh to OpenMC
2128
extern "C" int openmc_add_unstructured_mesh(
2129
  const char filename[], const char library[], int* id)
26,800,928✔
2130
{
2131
  std::string lib_name(library);
26,800,928✔
2132
  std::string mesh_file(filename);
5,133,018✔
2133
  bool valid_lib = false;
2134

21,667,910✔
2135
#ifdef DAGMC
2136
  if (lib_name == MOABMesh::mesh_lib_type) {
21,667,910✔
2137
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
10,142,462✔
2138
    valid_lib = true;
2139
  }
11,525,448✔
2140
#endif
2141

2142
#ifdef LIBMESH
109,969,046✔
2143
  if (lib_name == LibMesh::mesh_lib_type) {
2144
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2145
    valid_lib = true;
2146
  }
109,969,046✔
2147
#endif
70,123,416✔
2148

2149
  if (!valid_lib) {
39,845,630✔
2150
    set_errmsg(fmt::format("Mesh library {} is not supported "
2151
                           "by this build of OpenMC",
39,845,630✔
2152
      lib_name));
2153
    return OPENMC_E_INVALID_ARGUMENT;
2154
  }
2155

2156
  // auto-assign new ID
2157
  model::meshes.back()->set_id(-1);
2158
  *id = model::meshes.back()->id_;
39,845,630✔
2159

39,845,630✔
2160
  return 0;
2161
}
39,845,630✔
2162

2163
//! Return the index in the meshes array of a mesh with a given ID
2164
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
39,845,630✔
2165
{
39,611,638✔
2166
  auto pair = model::mesh_map.find(id);
2167
  if (pair == model::mesh_map.end()) {
2168
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
39,611,638✔
2169
    return OPENMC_E_INVALID_ID;
17,419,556✔
2170
  }
2171
  *index = pair->second;
2172
  return 0;
22,426,074✔
2173
}
2174

2175
//! Return the ID of a mesh
329,907,809✔
2176
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2177
{
2178
  if (int err = check_mesh(index))
2179
    return err;
2180
  *id = model::meshes[index]->id_;
329,907,809✔
2181
  return 0;
220,740,256✔
2182
}
220,740,256✔
2183

441,480,512✔
2184
//! Set the ID of a mesh
2185
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
109,167,553✔
2186
{
54,183,030✔
2187
  if (int err = check_mesh(index))
54,183,030✔
2188
    return err;
54,183,030✔
2189
  model::meshes[index]->id_ = id;
108,366,060✔
2190
  model::mesh_map[id] = index;
2191
  return 0;
2192
}
54,984,523✔
2193

54,984,523✔
2194
//! Get the number of elements in a mesh
54,984,523✔
2195
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
109,969,046✔
2196
{
2197
  if (int err = check_mesh(index))
2198
    return err;
2199
  *n = model::meshes[index]->n_bins();
313✔
2200
  return 0;
2201
}
313✔
2202

313✔
2203
//! Get the volume of each element in the mesh
313✔
2204
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
2205
{
1,252✔
2206
  if (int err = check_mesh(index))
939✔
UNCOV
2207
    return err;
×
2208
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
UNCOV
2209
    volumes[i] = model::meshes[index]->volume(i);
×
2210
  }
2211
  return 0;
939✔
2212
}
1,878✔
UNCOV
2213

×
2214
//! Get the bounding box of a mesh
UNCOV
2215
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
×
2216
{
2217
  if (int err = check_mesh(index))
939✔
UNCOV
2218
    return err;
×
2219

UNCOV
2220
  BoundingBox bbox = model::meshes[index]->bounding_box();
×
2221

2222
  // set lower left corner values
2223
  ll[0] = bbox.xmin;
313✔
UNCOV
2224
  ll[1] = bbox.ymin;
×
2225
  ll[2] = bbox.zmin;
2226

2227
  // set upper right corner values
×
2228
  ur[0] = bbox.xmax;
2229
  ur[1] = bbox.ymax;
313✔
UNCOV
2230
  ur[2] = bbox.zmax;
×
2231
  return 0;
UNCOV
2232
}
×
2233

2234
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
2235
  int nz, int table_size, int32_t* materials, double* volumes)
313✔
2236
{
313✔
2237
  if (int err = check_mesh(index))
2238
    return err;
313✔
2239

313✔
2240
  try {
313✔
2241
    model::meshes[index]->material_volumes(
2242
      nx, ny, nz, table_size, materials, volumes);
313✔
2243
  } catch (const std::exception& e) {
2244
    set_errmsg(e.what());
2245
    if (starts_with(e.what(), "Mesh")) {
202,820,013✔
2246
      return OPENMC_E_GEOMETRY;
2247
    } else {
202,820,013✔
2248
      return OPENMC_E_ALLOCATE;
2249
    }
UNCOV
2250
  }
×
2251

2252
  return 0;
UNCOV
2253
}
×
2254

2255
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
2256
  Position width, int basis, int* pixels, int32_t* data)
2257
{
2258
  if (int err = check_mesh(index))
2259
    return err;
2260
  const auto& mesh = model::meshes[index].get();
286✔
2261

2262
  int pixel_width = pixels[0];
286✔
2263
  int pixel_height = pixels[1];
286✔
2264

286✔
2265
  // get pixel size
286✔
2266
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
286✔
2267
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
2268

484✔
2269
  // setup basis indices and initial position centered on pixel
2270
  int in_i, out_i;
484✔
2271
  Position xyz = origin;
484✔
2272
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2273
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
484✔
2274
  switch (basis_enum) {
484✔
2275
  case PlotBasis::xy:
2276
    in_i = 0;
484✔
2277
    out_i = 1;
484✔
2278
    break;
2279
  case PlotBasis::xz:
484✔
2280
    in_i = 0;
484✔
2281
    out_i = 2;
2282
    break;
2283
  case PlotBasis::yz:
2284
    in_i = 1;
2285
    out_i = 2;
2286
    break;
2287
  default:
6,576✔
2288
    UNREACHABLE();
2289
  }
6,576✔
UNCOV
2290

×
UNCOV
2291
  // set initial position
×
2292
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2293
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
6,576✔
2294

2295
#pragma omp parallel
2296
  {
2297
    Position r = xyz;
1,186✔
2298

2299
#pragma omp for
1,186✔
UNCOV
2300
    for (int y = 0; y < pixel_height; y++) {
×
2301
      r[out_i] = xyz[out_i] - out_pixel * y;
2302
      for (int x = 0; x < pixel_width; x++) {
1,186✔
2303
        r[in_i] = xyz[in_i] + in_pixel * x;
1,186✔
UNCOV
2304
        data[pixel_width * y + x] = mesh->get_bin(r);
×
UNCOV
2305
      }
×
2306
    }
2307
  }
1,186✔
2308

2309
  return 0;
143✔
2310
}
2311

143✔
UNCOV
2312
//! Get the dimension of a regular mesh
×
2313
extern "C" int openmc_regular_mesh_get_dimension(
2314
  int32_t index, int** dims, int* n)
143✔
2315
{
143✔
UNCOV
2316
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2317
    return err;
×
2318
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2319
  *dims = mesh->shape_.data();
143✔
2320
  *n = mesh->n_dimension_;
2321
  return 0;
143✔
2322
}
2323

143✔
UNCOV
2324
//! Set the dimension of a regular mesh
×
2325
extern "C" int openmc_regular_mesh_set_dimension(
2326
  int32_t index, int n, const int* dims)
143✔
2327
{
143✔
UNCOV
2328
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2329
    return err;
×
2330
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2331

143✔
2332
  // Copy dimension
2333
  mesh->n_dimension_ = n;
195✔
2334
  std::copy(dims, dims + n, mesh->shape_.begin());
2335
  return 0;
195✔
UNCOV
2336
}
×
2337

2338
//! Get the regular mesh parameters
195✔
2339
extern "C" int openmc_regular_mesh_get_params(
195✔
UNCOV
2340
  int32_t index, double** ll, double** ur, double** width, int* n)
×
UNCOV
2341
{
×
2342
  if (int err = check_mesh_type<RegularMesh>(index))
2343
    return err;
195✔
2344
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2345

705✔
2346
  if (m->lower_left_.dimension() == 0) {
2347
    set_errmsg("Mesh parameters have not been set.");
705✔
UNCOV
2348
    return OPENMC_E_ALLOCATE;
×
2349
  }
2350

705✔
2351
  *ll = m->lower_left_.data();
705✔
2352
  *ur = m->upper_right_.data();
×
UNCOV
2353
  *width = m->width_.data();
×
2354
  *n = m->n_dimension_;
2355
  return 0;
705✔
2356
}
2357

2358
//! Set the regular mesh parameters
2359
extern "C" int openmc_regular_mesh_set_params(
2360
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2361
{
2362
  if (int err = check_mesh_type<RegularMesh>(index))
2363
    return err;
2364
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2365

2366
  if (m->n_dimension_ == -1) {
2367
    set_errmsg("Need to set mesh dimension before setting parameters.");
2368
    return OPENMC_E_UNASSIGNED;
2369
  }
2370

1,509✔
2371
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2372
  if (ll && ur) {
1,509✔
UNCOV
2373
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
×
2374
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2375
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
1,509✔
2376
  } else if (ll && width) {
2377
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
1,509✔
2378
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2379
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2380
  } else if (ur && width) {
2381
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
283✔
2382
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2383
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
2384
  } else {
283✔
2385
    set_errmsg("At least two parameters must be specified.");
283✔
2386
    return OPENMC_E_INVALID_ARGUMENT;
283✔
2387
  }
2388

566✔
2389
  // Set material volumes
283✔
2390

191✔
2391
  // TODO: incorporate this into method in RegularMesh that can be called from
92✔
2392
  // here and from constructor
48✔
2393
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
44✔
2394
  m->element_volume_ = 1.0;
22✔
2395
  for (int i = 0; i < m->n_dimension_; i++) {
22✔
2396
    m->element_volume_ *= m->width_[i];
22✔
2397
  }
UNCOV
2398

×
2399
  return 0;
2400
}
2401

283✔
UNCOV
2402
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
×
2403
template<class C>
2404
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
283✔
2405
  const int nx, const double* grid_y, const int ny, const double* grid_z,
283✔
2406
  const int nz)
2407
{
UNCOV
2408
  if (int err = check_mesh_type<C>(index))
×
2409
    return err;
2410

UNCOV
2411
  C* m = dynamic_cast<C*>(model::meshes[index].get());
×
UNCOV
2412

×
UNCOV
2413
  m->n_dimension_ = 3;
×
2414

2415
  m->grid_[0].reserve(nx);
2416
  m->grid_[1].reserve(ny);
2417
  m->grid_[2].reserve(nz);
2418

2419
  for (int i = 0; i < nx; i++) {
2420
    m->grid_[0].push_back(grid_x[i]);
2421
  }
2422
  for (int i = 0; i < ny; i++) {
2423
    m->grid_[1].push_back(grid_y[i]);
2424
  }
2425
  for (int i = 0; i < nz; i++) {
2426
    m->grid_[2].push_back(grid_z[i]);
2427
  }
2428

UNCOV
2429
  int err = m->set_grid();
×
UNCOV
2430
  return err;
×
2431
}
2432

UNCOV
2433
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
×
2434
template<class C>
2435
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
2436
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
UNCOV
2437
{
×
UNCOV
2438
  if (int err = check_mesh_type<C>(index))
×
2439
    return err;
UNCOV
2440
  C* m = dynamic_cast<C*>(model::meshes[index].get());
×
2441

2442
  if (m->lower_left_.dimension() == 0) {
2443
    set_errmsg("Mesh parameters have not been set.");
2444
    return OPENMC_E_ALLOCATE;
459✔
2445
  }
2446

459✔
2447
  *grid_x = m->grid_[0].data();
459✔
UNCOV
2448
  *nx = m->grid_[0].size();
×
2449
  *grid_y = m->grid_[1].data();
×
2450
  *ny = m->grid_[1].size();
2451
  *grid_z = m->grid_[2].data();
459✔
2452
  *nz = m->grid_[2].size();
459✔
2453

2454
  return 0;
2455
}
2456

2,960✔
2457
//! Get the rectilinear mesh grid
2458
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
2,960✔
UNCOV
2459
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2460
{
2,960✔
2461
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
2,960✔
2462
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2463
}
2464

2465
//! Set the rectilienar mesh parameters
283✔
2466
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
2467
  const double* grid_x, const int nx, const double* grid_y, const int ny,
283✔
UNCOV
2468
  const double* grid_z, const int nz)
×
2469
{
283✔
2470
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
283✔
2471
    index, grid_x, nx, grid_y, ny, grid_z, nz);
283✔
2472
}
2473

2474
//! Get the cylindrical mesh grid
2475
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
231✔
2476
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2477
{
231✔
UNCOV
2478
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
×
2479
    index, grid_x, nx, grid_y, ny, grid_z, nz);
231✔
2480
}
231✔
2481

2482
//! Set the cylindrical mesh parameters
2483
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
2484
  const double* grid_x, const int nx, const double* grid_y, const int ny,
88✔
2485
  const double* grid_z, const int nz)
2486
{
88✔
UNCOV
2487
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
×
2488
    index, grid_x, nx, grid_y, ny, grid_z, nz);
968✔
2489
}
880✔
2490

2491
//! Get the spherical mesh grid
88✔
2492
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
2493
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2494
{
2495

132✔
2496
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2497
    index, grid_x, nx, grid_y, ny, grid_z, nz);
132✔
UNCOV
2498
  ;
×
2499
}
2500

132✔
2501
//! Set the spherical mesh parameters
2502
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
2503
  const double* grid_x, const int nx, const double* grid_y, const int ny,
132✔
2504
  const double* grid_z, const int nz)
132✔
2505
{
132✔
2506
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
2507
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2508
}
132✔
2509

132✔
2510
#ifdef DAGMC
132✔
2511

132✔
2512
const std::string MOABMesh::mesh_lib_type = "moab";
2513

2514
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
143✔
2515
{
2516
  initialize();
2517
}
143✔
UNCOV
2518

×
2519
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2520
{
2521
  filename_ = filename;
143✔
2522
  set_length_multiplier(length_multiplier);
2523
  initialize();
11✔
2524
}
11✔
2525

11✔
2526
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
11✔
2527
{
2528
  mbi_ = external_mbi;
×
2529
  filename_ = "unknown (external file)";
2530
  this->initialize();
11✔
2531
}
2532

132✔
2533
void MOABMesh::initialize()
2534
{
2535

44✔
2536
  // Create the MOAB interface and load data from file
2537
  this->create_interface();
2538

44✔
UNCOV
2539
  // Initialise MOAB error code
×
2540
  moab::ErrorCode rval = moab::MB_SUCCESS;
44✔
2541

2542
  // Set the dimension
44✔
2543
  n_dimension_ = 3;
44✔
2544

2545
  // set member range of tetrahedral entities
2546
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
44✔
2547
  if (rval != moab::MB_SUCCESS) {
44✔
2548
    fatal_error("Failed to get all tetrahedral elements");
2549
  }
2550

2551
  if (!ehs_.all_of_type(moab::MBTET)) {
44✔
2552
    warning("Non-tetrahedral elements found in unstructured "
2553
            "mesh file: " +
44✔
2554
            filename_);
44✔
2555
  }
44✔
2556

44✔
2557
  // set member range of vertices
44✔
2558
  int vertex_dim = 0;
44✔
UNCOV
2559
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
UNCOV
2560
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2561
    fatal_error("Failed to get all vertex handles");
×
UNCOV
2562
  }
×
UNCOV
2563

×
UNCOV
2564
  // make an entity set for all tetrahedra
×
UNCOV
2565
  // this is used for convenience later in output
×
UNCOV
2566
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
×
UNCOV
2567
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2568
    fatal_error("Failed to create an entity set for the tetrahedral elements");
×
2569
  }
2570

2571
  rval = mbi_->add_entities(tetset_, ehs_);
2572
  if (rval != moab::MB_SUCCESS) {
44✔
2573
    fatal_error("Failed to add tetrahedra to an entity set.");
44✔
2574
  }
2575

24✔
2576
  if (length_multiplier_ > 0.0) {
2577
    // get the connectivity of all tets
20✔
2578
    moab::Range adj;
2579
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
2580
    if (rval != moab::MB_SUCCESS) {
420✔
2581
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
400✔
2582
    }
8,400✔
2583
    // scale all vertex coords by multiplier (done individually so not all
8,000✔
2584
    // coordinates are in memory twice at once)
8,000✔
2585
    for (auto vert : adj) {
2586
      // retrieve coords
2587
      std::array<double, 3> coord;
2588
      rval = mbi_->get_coords(&vert, 1, coord.data());
2589
      if (rval != moab::MB_SUCCESS) {
44✔
2590
        fatal_error("Could not get coordinates of vertex.");
2591
      }
2592
      // scale coords
2593
      for (auto& c : coord) {
11✔
2594
        c *= length_multiplier_;
2595
      }
2596
      // set new coords
11✔
UNCOV
2597
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2598
      if (rval != moab::MB_SUCCESS) {
11✔
2599
        fatal_error("Failed to set new vertex coordinates");
11✔
2600
      }
11✔
2601
    }
11✔
2602
  }
2603

2604
  // Determine bounds of mesh
2605
  this->determine_bounds();
213✔
2606
}
2607

2608
void MOABMesh::prepare_for_point_location()
213✔
UNCOV
2609
{
×
2610
  // if the KDTree has already been constructed, do nothing
213✔
2611
  if (kdtree_)
2612
    return;
2613

213✔
2614
  // build acceleration data structures
213✔
2615
  compute_barycentric_data(ehs_);
213✔
2616
  build_kdtree(ehs_);
2617
}
2618

2619
void MOABMesh::create_interface()
235✔
2620
{
2621
  // Do not create a MOAB instance if one is already in memory
2622
  if (mbi_)
235✔
UNCOV
2623
    return;
×
2624

235✔
2625
  // create MOAB instance
2626
  mbi_ = std::make_shared<moab::Core>();
235✔
UNCOV
2627

×
UNCOV
2628
  // load unstructured mesh file
×
2629
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2630
  if (rval != moab::MB_SUCCESS) {
2631
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
235✔
2632
  }
235✔
2633
}
235✔
2634

235✔
2635
void MOABMesh::build_kdtree(const moab::Range& all_tets)
235✔
2636
{
2637
  moab::Range all_tris;
2638
  int adj_dim = 2;
2639
  write_message("Getting tet adjacencies...", 7);
246✔
2640
  moab::ErrorCode rval = mbi_->get_adjacencies(
2641
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2642
  if (rval != moab::MB_SUCCESS) {
246✔
UNCOV
2643
    fatal_error("Failed to get adjacent triangles for tets");
×
2644
  }
246✔
2645

2646
  if (!all_tris.all_of_type(moab::MBTRI)) {
246✔
UNCOV
2647
    warning("Non-triangle elements found in tet adjacencies in "
×
UNCOV
2648
            "unstructured mesh file: " +
×
2649
            filename_);
2650
  }
2651

246✔
2652
  // combine into one range
246✔
2653
  moab::Range all_tets_and_tris;
224✔
2654
  all_tets_and_tris.merge(all_tets);
224✔
2655
  all_tets_and_tris.merge(all_tris);
224✔
2656

22✔
2657
  // create a kd-tree instance
11✔
2658
  write_message(
11✔
2659
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
11✔
2660
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
11✔
2661

11✔
2662
  // Determine what options to use
11✔
2663
  std::ostringstream options_stream;
11✔
2664
  if (options_.empty()) {
UNCOV
2665
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
×
UNCOV
2666
  } else {
×
2667
    options_stream << options_;
2668
  }
2669
  moab::FileOptions file_opts(options_stream.str().c_str());
2670

2671
  // Build the k-d tree
2672
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2673
  if (rval != moab::MB_SUCCESS) {
246✔
2674
    fatal_error("Failed to construct KDTree for the "
246✔
2675
                "unstructured mesh file: " +
984✔
2676
                filename_);
738✔
2677
  }
2678
}
2679

246✔
2680
void MOABMesh::intersect_track(const moab::CartVect& start,
246✔
2681
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2682
{
2683
  hits.clear();
2684

92✔
2685
  moab::ErrorCode rval;
2686
  vector<moab::EntityHandle> tris;
2687
  // get all intersections with triangles in the tet mesh
2688
  // (distances are relative to the start point, not the previous
92✔
UNCOV
2689
  // intersection)
×
2690
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
2691
    dir.array(), start.array(), tris, hits, 0, track_len);
92✔
2692
  if (rval != moab::MB_SUCCESS) {
2693
    fatal_error(
92✔
2694
      "Failed to compute intersections on unstructured mesh: " + filename_);
2695
  }
92✔
2696

92✔
2697
  // remove duplicate intersection distances
92✔
2698
  std::unique(hits.begin(), hits.end());
2699

628✔
2700
  // sorts by first component of std::pair by default
536✔
2701
  std::sort(hits.begin(), hits.end());
2702
}
353✔
2703

261✔
2704
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2705
  vector<int>& bins, vector<double>& lengths) const
331✔
2706
{
239✔
2707
  moab::CartVect start(r0.x, r0.y, r0.z);
2708
  moab::CartVect end(r1.x, r1.y, r1.z);
2709
  moab::CartVect dir(u.x, u.y, u.z);
92✔
2710
  dir.normalize();
92✔
2711

2712
  double track_len = (end - start).length();
22✔
2713
  if (track_len == 0.0)
2714
    return;
2715

2716
  start -= TINY_BIT * dir;
22✔
UNCOV
2717
  end += TINY_BIT * dir;
×
2718

2719
  vector<double> hits;
22✔
2720
  intersect_track(start, dir, track_len, hits);
2721

22✔
2722
  bins.clear();
2723
  lengths.clear();
22✔
2724

22✔
2725
  // if there are no intersections the track may lie entirely
22✔
2726
  // within a single tet. If this is the case, apply entire
2727
  // score to that tet and return.
88✔
2728
  if (hits.size() == 0) {
66✔
2729
    Position midpoint = r0 + u * (track_len * 0.5);
2730
    int bin = this->get_bin(midpoint);
88✔
2731
    if (bin != -1) {
66✔
2732
      bins.push_back(bin);
2733
      lengths.push_back(1.0);
99✔
2734
    }
77✔
2735
    return;
2736
  }
2737

22✔
2738
  // for each segment in the set of tracks, try to look up a tet
22✔
2739
  // at the midpoint of the segment
2740
  Position current = r0;
22✔
2741
  double last_dist = 0.0;
2742
  for (const auto& hit : hits) {
2743
    // get the segment length
2744
    double segment_length = hit - last_dist;
22✔
UNCOV
2745
    last_dist = hit;
×
2746
    // find the midpoint of this segment
2747
    Position midpoint = current + u * (segment_length * 0.5);
22✔
2748
    // try to find a tet for this position
2749
    int bin = this->get_bin(midpoint);
22✔
2750

2751
    // determine the start point for this segment
22✔
2752
    current = r0 + u * hit;
22✔
2753

22✔
2754
    if (bin == -1) {
2755
      continue;
88✔
2756
    }
66✔
2757

2758
    bins.push_back(bin);
99✔
2759
    lengths.push_back(segment_length / track_len);
77✔
2760
  }
2761

77✔
2762
  // tally remaining portion of track after last hit if
55✔
2763
  // the last segment of the track is in the mesh but doesn't
2764
  // reach the other side of the tet
2765
  if (hits.back() < track_len) {
22✔
2766
    Position segment_start = r0 + u * hits.back();
22✔
2767
    double segment_length = track_len - hits.back();
2768
    Position midpoint = segment_start + u * (segment_length * 0.5);
48✔
2769
    int bin = this->get_bin(midpoint);
2770
    if (bin != -1) {
2771
      bins.push_back(bin);
2772
      lengths.push_back(segment_length / track_len);
48✔
UNCOV
2773
    }
×
2774
  }
2775
};
48✔
2776

2777
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
48✔
2778
{
2779
  moab::CartVect pos(r.x, r.y, r.z);
48✔
2780
  // find the leaf of the kd-tree for this position
48✔
2781
  moab::AdaptiveKDTreeIter kdtree_iter;
48✔
2782
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
2783
  if (rval != moab::MB_SUCCESS) {
452✔
2784
    return 0;
404✔
2785
  }
2786

166✔
2787
  // retrieve the tet elements of this leaf
118✔
2788
  moab::EntityHandle leaf = kdtree_iter.handle();
2789
  moab::Range tets;
155✔
2790
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
107✔
2791
  if (rval != moab::MB_SUCCESS) {
2792
    warning("MOAB error finding tets.");
2793
  }
48✔
2794

48✔
2795
  // loop over the tets in this leaf, returning the containing tet if found
2796
  for (const auto& tet : tets) {
2797
    if (point_in_tet(pos, tet)) {
2798
      return tet;
2799
    }
389✔
2800
  }
2801

2802
  // if no tet is found, return an invalid handle
389✔
UNCOV
2803
  return 0;
×
2804
}
389✔
2805

2806
double MOABMesh::volume(int bin) const
389✔
UNCOV
2807
{
×
UNCOV
2808
  return tet_volume(get_ent_handle_from_bin(bin));
×
2809
}
2810

2811
std::string MOABMesh::library() const
389✔
2812
{
389✔
2813
  return mesh_lib_type;
389✔
2814
}
389✔
2815

389✔
2816
// Sample position within a tet for MOAB type tets
389✔
2817
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2818
{
389✔
2819

2820
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
121✔
2821

2822
  // Get vertex coordinates for MOAB tet
2823
  const moab::EntityHandle* conn1;
121✔
UNCOV
2824
  int conn1_size;
×
2825
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
121✔
2826
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
2827
    fatal_error(fmt::format(
121✔
UNCOV
2828
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
×
UNCOV
2829
      conn1_size));
×
2830
  }
2831
  moab::CartVect p[4];
2832
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
121✔
2833
  if (rval != moab::MB_SUCCESS) {
121✔
2834
    fatal_error("Failed to get tet coords");
121✔
2835
  }
121✔
2836

121✔
2837
  std::array<Position, 4> tet_verts;
121✔
2838
  for (int i = 0; i < 4; i++) {
2839
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
121✔
2840
  }
2841
  // Samples position within tet using Barycentric stuff
121✔
2842
  return this->sample_tet(tet_verts, seed);
2843
}
2844

121✔
UNCOV
2845
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2846
{
121✔
2847
  vector<moab::EntityHandle> conn;
2848
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
121✔
UNCOV
2849
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2850
    fatal_error("Failed to get tet connectivity");
×
2851
  }
2852

2853
  moab::CartVect p[4];
121✔
2854
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
121✔
2855
  if (rval != moab::MB_SUCCESS) {
121✔
2856
    fatal_error("Failed to get tet coords");
121✔
2857
  }
121✔
2858

121✔
2859
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2860
}
121✔
2861

2862
int MOABMesh::get_bin(Position r) const
147✔
2863
{
2864
  moab::EntityHandle tet = get_tet(r);
2865
  if (tet == 0) {
147✔
UNCOV
2866
    return -1;
×
2867
  } else {
147✔
2868
    return get_bin_from_ent_handle(tet);
2869
  }
147✔
UNCOV
2870
}
×
UNCOV
2871

×
2872
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2873
{
2874
  moab::ErrorCode rval;
147✔
2875

147✔
2876
  baryc_data_.clear();
147✔
2877
  baryc_data_.resize(tets.size());
147✔
2878

147✔
2879
  // compute the barycentric data for each tet element
147✔
2880
  // and store it as a 3x3 matrix
2881
  for (auto& tet : tets) {
147✔
2882
    vector<moab::EntityHandle> verts;
2883
    rval = mbi_->get_connectivity(&tet, 1, verts);
2884
    if (rval != moab::MB_SUCCESS) {
2885
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
147✔
2886
    }
2887

2888
    moab::CartVect p[4];
147✔
2889
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
147✔
2890
    if (rval != moab::MB_SUCCESS) {
2891
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2892
    }
2893

48✔
2894
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2895

2896
    // invert now to avoid this cost later
2897
    a = a.transpose().inverse();
48✔
2898
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
48✔
2899
  }
2900
}
2901

2902
bool MOABMesh::point_in_tet(
121✔
2903
  const moab::CartVect& r, moab::EntityHandle tet) const
2904
{
2905

121✔
2906
  moab::ErrorCode rval;
121✔
2907

2908
  // get tet vertices
2909
  vector<moab::EntityHandle> verts;
2910
  rval = mbi_->get_connectivity(&tet, 1, verts);
22✔
2911
  if (rval != moab::MB_SUCCESS) {
2912
    warning("Failed to get vertices of tet in umesh: " + filename_);
2913
    return false;
2914
  }
22✔
2915

22✔
2916
  // first vertex is used as a reference point for the barycentric data -
2917
  // retrieve its coordinates
2918
  moab::CartVect p_zero;
2919
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
121✔
2920
  if (rval != moab::MB_SUCCESS) {
2921
    warning("Failed to get coordinates of a vertex in "
2922
            "unstructured mesh: " +
2923
            filename_);
121✔
2924
    return false;
121✔
2925
  }
2926

2927
  // look up barycentric data
2928
  int idx = get_bin_from_ent_handle(tet);
2929
  const moab::Matrix3& a_inv = baryc_data_[idx];
22✔
2930

2931
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2932

2933
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
22✔
2934
          bary_coords[2] >= 0.0 &&
22✔
2935
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2936
}
2937

2938
int MOABMesh::get_bin_from_index(int idx) const
2939
{
2940
  if (idx >= n_bins()) {
2941
    fatal_error(fmt::format("Invalid bin index: {}", idx));
23✔
2942
  }
2943
  return ehs_[idx] - ehs_[0];
23✔
2944
}
23✔
2945

2946
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2947
{
2948
  int bin = get_bin(r);
2949
  *in_mesh = bin != -1;
2950
  return bin;
2951
}
2952

2953
int MOABMesh::get_index_from_bin(int bin) const
1✔
2954
{
2955
  return bin;
1✔
2956
}
1✔
2957

1✔
2958
std::pair<vector<double>, vector<double>> MOABMesh::plot(
1✔
2959
  Position plot_ll, Position plot_ur) const
2960
{
24✔
2961
  // TODO: Implement mesh lines
2962
  return {};
2963
}
2964

24✔
2965
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2966
{
2967
  int idx = vert - verts_[0];
24✔
2968
  if (idx >= n_vertices()) {
2969
    fatal_error(
2970
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
24✔
2971
  }
2972
  return idx;
2973
}
24✔
2974

24✔
2975
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2976
{
2977
  int bin = eh - ehs_[0];
2978
  if (bin >= n_bins()) {
24✔
2979
    fatal_error(fmt::format("Invalid bin: {}", bin));
2980
  }
2981
  return bin;
2982
}
2983

2984
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2985
{
24✔
2986
  if (bin >= n_bins()) {
24✔
2987
    fatal_error(fmt::format("Invalid bin index: ", bin));
24✔
2988
  }
2989
  return ehs_[0] + bin;
2990
}
2991

2992
int MOABMesh::n_bins() const
2993
{
24✔
2994
  return ehs_.size();
24✔
2995
}
2996

2997
int MOABMesh::n_surface_bins() const
2998
{
24✔
2999
  // collect all triangles in the set of tets for this mesh
24✔
3000
  moab::Range tris;
3001
  moab::ErrorCode rval;
3002
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
3003
  if (rval != moab::MB_SUCCESS) {
24✔
3004
    warning("Failed to get all triangles in the mesh instance");
3005
    return -1;
3006
  }
3007
  return 2 * tris.size();
3008
}
3009

3010
Position MOABMesh::centroid(int bin) const
3011
{
3012
  moab::ErrorCode rval;
3013

3014
  auto tet = this->get_ent_handle_from_bin(bin);
3015

3016
  // look up the tet connectivity
3017
  vector<moab::EntityHandle> conn;
3018
  rval = mbi_->get_connectivity(&tet, 1, conn);
3019
  if (rval != moab::MB_SUCCESS) {
3020
    warning("Failed to get connectivity of a mesh element.");
3021
    return {};
3022
  }
3023

3024
  // get the coordinates
3025
  vector<moab::CartVect> coords(conn.size());
3026
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
3027
  if (rval != moab::MB_SUCCESS) {
3028
    warning("Failed to get the coordinates of a mesh element.");
3029
    return {};
3030
  }
3031

3032
  // compute the centroid of the element vertices
24✔
3033
  moab::CartVect centroid(0.0, 0.0, 0.0);
24✔
3034
  for (const auto& coord : coords) {
3035
    centroid += coord;
20✔
3036
  }
3037
  centroid /= double(coords.size());
3038

20✔
3039
  return {centroid[0], centroid[1], centroid[2]};
3040
}
3041

3042
int MOABMesh::n_vertices() const
20✔
3043
{
20✔
3044
  return verts_.size();
3045
}
3046

24✔
3047
Position MOABMesh::vertex(int id) const
3048
{
3049

24✔
3050
  moab::ErrorCode rval;
1✔
3051

3052
  moab::EntityHandle vert = verts_[id];
3053

23✔
3054
  moab::CartVect coords;
3055
  rval = mbi_->get_coords(&vert, 1, coords.array());
3056
  if (rval != moab::MB_SUCCESS) {
23✔
3057
    fatal_error("Failed to get the coordinates of a vertex.");
23✔
3058
  }
3059

3060
  return {coords[0], coords[1], coords[2]};
3061
}
3062

20✔
3063
std::vector<int> MOABMesh::connectivity(int bin) const
3064
{
20✔
3065
  moab::ErrorCode rval;
20✔
3066

20✔
3067
  auto tet = get_ent_handle_from_bin(bin);
20✔
3068

3069
  // look up the tet connectivity
20✔
3070
  vector<moab::EntityHandle> conn;
3071
  rval = mbi_->get_connectivity(&tet, 1, conn);
3072
  if (rval != moab::MB_SUCCESS) {
3073
    fatal_error("Failed to get connectivity of a mesh element.");
20✔
3074
    return {};
3075
  }
3076

3077
  std::vector<int> verts(4);
3078
  for (int i = 0; i < verts.size(); i++) {
3079
    verts[i] = get_vert_idx_from_handle(conn[i]);
3080
  }
20✔
3081

20✔
3082
  return verts;
20✔
3083
}
3084

3085
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
20✔
3086
  std::string score) const
20✔
3087
{
20✔
3088
  moab::ErrorCode rval;
3089
  // add a tag to the mesh
3090
  // all scores are treated as a single value
20✔
3091
  // with an uncertainty
20✔
3092
  moab::Tag value_tag;
4✔
3093

3094
  // create the value tag if not present and get handle
16✔
3095
  double default_val = 0.0;
3096
  auto val_string = score + "_mean";
20✔
3097
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
3098
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3099
  if (rval != moab::MB_SUCCESS) {
20✔
3100
    auto msg =
20✔
3101
      fmt::format("Could not create or retrieve the value tag for the score {}"
3102
                  " on unstructured mesh {}",
3103
        score, id_);
3104
    fatal_error(msg);
3105
  }
20✔
3106

3107
  // create the std dev tag if not present and get handle
1,542,122✔
3108
  moab::Tag error_tag;
3109
  std::string err_string = score + "_std_dev";
3110
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
1,542,122✔
3111
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3112
  if (rval != moab::MB_SUCCESS) {
3113
    auto msg =
1,542,122✔
3114
      fmt::format("Could not create or retrieve the error tag for the score {}"
3115
                  " on unstructured mesh {}",
3116
        score, id_);
3117
    fatal_error(msg);
1,542,122✔
3118
  }
3119

1,542,122✔
3120
  // return the populated tag handles
3121
  return {value_tag, error_tag};
3122
}
3123

3124
void MOABMesh::add_score(const std::string& score)
3125
{
1,542,122✔
3126
  auto score_tags = get_score_tags(score);
3127
  tag_names_.push_back(score);
3128
}
1,542,122✔
3129

1,542,122✔
3130
void MOABMesh::remove_scores()
3131
{
1,542,122✔
3132
  for (const auto& name : tag_names_) {
3133
    auto value_name = name + "_mean";
3134
    moab::Tag tag;
1,542,122✔
3135
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
1,542,122✔
3136
    if (rval != moab::MB_SUCCESS)
1,542,122✔
3137
      return;
1,542,122✔
3138

3139
    rval = mbi_->tag_delete(tag);
1,542,122✔
3140
    if (rval != moab::MB_SUCCESS) {
1,542,122✔
3141
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
720,627✔
3142
                             " on unstructured mesh {}",
3143
        name, id_);
1,542,122✔
3144
      fatal_error(msg);
1,542,122✔
3145
    }
3146

1,542,122✔
3147
    auto std_dev_name = name + "_std_dev";
1,542,122✔
3148
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
3149
    if (rval != moab::MB_SUCCESS) {
1,542,122✔
3150
      auto msg =
1,542,122✔
3151
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3152
                    " on unstructured mesh {}",
3153
          name, id_);
3154
    }
3155

1,542,122✔
3156
    rval = mbi_->tag_delete(tag);
720,627✔
3157
    if (rval != moab::MB_SUCCESS) {
720,627✔
3158
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
720,627✔
3159
                             " on unstructured mesh {}",
242,659✔
3160
        name, id_);
242,659✔
3161
      fatal_error(msg);
3162
    }
720,627✔
3163
  }
3164
  tag_names_.clear();
3165
}
3166

3167
void MOABMesh::set_score_data(const std::string& score,
821,495✔
3168
  const vector<double>& values, const vector<double>& std_dev)
821,495✔
3169
{
5,514,377✔
3170
  auto score_tags = this->get_score_tags(score);
3171

4,692,882✔
3172
  moab::ErrorCode rval;
4,692,882✔
3173
  // set the score value
3174
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
4,692,882✔
3175
  if (rval != moab::MB_SUCCESS) {
3176
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
4,692,882✔
3177
                           "on unstructured mesh {}",
3178
      score, id_);
3179
    warning(msg);
4,692,882✔
3180
  }
3181

4,692,882✔
3182
  // set the error value
20,480✔
3183
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3184
  if (rval != moab::MB_SUCCESS) {
3185
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
4,672,402✔
3186
                           "on unstructured mesh {}",
4,672,402✔
3187
      score, id_);
3188
    warning(msg);
3189
  }
3190
}
3191

3192
void MOABMesh::write(const std::string& base_filename) const
821,495✔
3193
{
821,495✔
3194
  // add extension to the base name
821,495✔
3195
  auto filename = base_filename + ".vtk";
821,495✔
3196
  write_message(5, "Writing unstructured mesh {}...", filename);
821,495✔
3197
  filename = settings::path_output + filename;
821,495✔
3198

766,195✔
3199
  // write the tetrahedral elements of the mesh only
766,195✔
3200
  // to avoid clutter from zero-value data on other
3201
  // elements during visualization
3202
  moab::ErrorCode rval;
1,542,122✔
3203
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
3204
  if (rval != moab::MB_SUCCESS) {
7,307,001✔
3205
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
3206
    warning(msg);
7,307,001✔
3207
  }
3208
}
7,307,001✔
3209

7,307,001✔
3210
#endif
7,307,001✔
3211

1,010,077✔
3212
#ifdef LIBMESH
3213

3214
const std::string LibMesh::mesh_lib_type = "libmesh";
3215

6,296,924✔
3216
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,296,924✔
3217
{
6,296,924✔
3218
  // filename_ and length_multiplier_ will already be set by the
6,296,924✔
3219
  // UnstructuredMesh constructor
3220
  set_mesh_pointer_from_filename(filename_);
3221
  set_length_multiplier(length_multiplier_);
3222
  initialize();
3223
}
260,010,824✔
3224

260,007,978✔
3225
// create the mesh from a pointer to a libMesh Mesh
6,294,078✔
3226
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
3227
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
3228
{
3229
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
3230
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
2,846✔
3231
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
7,307,001✔
3232
  }
3233

167,856✔
3234
  m_ = &input_mesh;
3235
  set_length_multiplier(length_multiplier);
167,856✔
3236
  initialize();
3237
}
3238

32✔
3239
// create the mesh from an input file
3240
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
32✔
3241
  : adaptive_(false)
3242
{
3243
  set_mesh_pointer_from_filename(filename);
3244
  set_length_multiplier(length_multiplier);
200,410✔
3245
  initialize();
3246
}
3247

200,410✔
3248
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
3249
{
3250
  filename_ = filename;
3251
  unique_m_ =
3252
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
200,410✔
3253
  m_ = unique_m_.get();
200,410✔
3254
  m_->read(filename_);
3255
}
3256

3257
// build a libMesh equation system for storing values
3258
void LibMesh::build_eqn_sys()
1,002,050✔
3259
{
200,410✔
3260
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
200,410✔
3261
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
3262
  libMesh::ExplicitSystem& eq_sys =
3263
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
3264
}
200,410✔
3265

1,002,050✔
3266
// intialize from mesh file
801,640✔
3267
void LibMesh::initialize()
3268
{
3269
  if (!settings::libmesh_comm) {
400,820✔
3270
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3271
                "communicator.");
3272
  }
167,856✔
3273

3274
  // assuming that unstructured meshes used in OpenMC are 3D
167,856✔
3275
  n_dimension_ = 3;
167,856✔
3276

167,856✔
3277
  if (length_multiplier_ > 0.0) {
3278
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
3279
  }
3280
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
839,280✔
3281
  // Otherwise assume that it is prepared by its owning application
167,856✔
3282
  if (unique_m_) {
167,856✔
3283
    m_->prepare_for_use();
3284
  }
3285

3286
  // ensure that the loaded mesh is 3 dimensional
335,712✔
3287
  if (m_->mesh_dimension() != n_dimension_) {
167,856✔
3288
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3289
                            "mesh is not a 3D mesh.",
7,307,001✔
3290
      filename_));
3291
  }
7,307,001✔
3292

7,307,001✔
3293
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3294
    pl_.emplace_back(m_->sub_point_locator());
3295
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,294,078✔
3296
    pl_.back()->enable_out_of_mesh_mode();
3297
  }
3298

3299
  // store first element in the mesh to use as an offset for bin indices
20✔
3300
  auto first_elem = *m_->elements_begin();
3301
  first_element_id_ = first_elem->id();
3302

3303
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
20✔
3304
  // contiguous in ID space, so we need to map from bin indices (defined over
20✔
3305
  // active elements) to global dof ids
3306
  if (adaptive_) {
3307
    bin_to_elem_map_.reserve(m_->n_active_elem());
3308
    elem_to_bin_map_.resize(m_->n_elem(), -1);
239,732✔
3309
    for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
239,712✔
3310
         it++) {
239,712✔
3311
      auto elem = *it;
239,712✔
3312

3313
      bin_to_elem_map_.push_back(elem->id());
3314
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3315
    }
1,198,560✔
3316
  }
239,712✔
3317

239,712✔
3318
  // bounding box for the mesh for quick rejection checks
3319
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3320
  libMesh::Point ll = bbox_.min();
3321
  libMesh::Point ur = bbox_.max();
239,712✔
3322
  lower_left_ = {ll(0), ll(1), ll(2)};
3323
  upper_right_ = {ur(0), ur(1), ur(2)};
3324
}
239,712✔
3325

239,712✔
3326
// Sample position within a tet for LibMesh type tets
239,712✔
3327
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
20✔
3328
{
3329
  const auto& elem = get_element_from_bin(bin);
260,007,978✔
3330
  // Get tet vertex coordinates from LibMesh
3331
  std::array<Position, 4> tet_verts;
3332
  for (int i = 0; i < elem.n_nodes(); i++) {
3333
    auto node_ref = elem.node_ref(i);
3334
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
3335
  }
3336
  // Samples position within tet using Barycentric coordinates
260,007,978✔
3337
  return this->sample_tet(tet_verts, seed);
260,007,978✔
3338
}
260,007,978✔
3339

3340
Position LibMesh::centroid(int bin) const
3341
{
3342
  const auto& elem = this->get_element_from_bin(bin);
3343
  auto centroid = elem.vertex_average();
3344
  return {centroid(0), centroid(1), centroid(2)};
3345
}
260,007,978✔
3346

260,007,978✔
3347
int LibMesh::n_vertices() const
260,007,978✔
3348
{
3349
  return m_->n_nodes();
3350
}
3351

3352
Position LibMesh::vertex(int vertex_id) const
3353
{
3354
  const auto node_ref = m_->node_ref(vertex_id);
3355
  return {node_ref(0), node_ref(1), node_ref(2)};
260,007,978✔
3356
}
260,007,978✔
3357

3358
std::vector<int> LibMesh::connectivity(int elem_id) const
260,007,978✔
3359
{
3360
  std::vector<int> conn;
421,084,473✔
3361
  const auto* elem_ptr = m_->elem_ptr(elem_id);
442,748,125✔
3362
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
281,671,630✔
3363
    conn.push_back(elem_ptr->node_id(i));
260,007,978✔
3364
  }
3365
  return conn;
3366
}
3367

3368
std::string LibMesh::library() const
3369
{
3370
  return mesh_lib_type;
3371
}
3372

3373
int LibMesh::n_bins() const
3374
{
3375
  return m_->n_active_elem();
3376
}
3377

3378
int LibMesh::n_surface_bins() const
3379
{
3380
  int n_bins = 0;
3381
  for (int i = 0; i < this->n_bins(); i++) {
3382
    const libMesh::Elem& e = get_element_from_bin(i);
3383
    n_bins += e.n_faces();
3384
    // if this is a boundary element, it will only be visited once,
3385
    // the number of surface bins is incremented to
3386
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
3387
      // null neighbor pointer indicates a boundary face
3388
      if (!neighbor_ptr) {
3389
        n_bins++;
3390
      }
3391
    }
3392
  }
815,424✔
3393
  return n_bins;
3394
}
815,424✔
3395

815,424✔
3396
void LibMesh::add_score(const std::string& var_name)
3397
{
3398
  if (adaptive_) {
3399
    warning(fmt::format(
815,424✔
3400
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3401
      this->id_));
3402

266,541,768✔
3403
    return;
3404
  }
266,541,768✔
3405

266,541,768✔
3406
  if (!equation_systems_) {
3407
    build_eqn_sys();
3408
  }
266,541,768✔
3409

3410
  // check if this is a new variable
3411
  std::string value_name = var_name + "_mean";
572,122✔
3412
  if (!variable_map_.count(value_name)) {
3413
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
572,122✔
3414
    auto var_num =
3415
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3416
    variable_map_[value_name] = var_num;
572,122✔
3417
  }
3418

3419
  std::string std_dev_name = var_name + "_std_dev";
267,317,815✔
3420
  // check if this is a new variable
3421
  if (!variable_map_.count(std_dev_name)) {
267,317,815✔
3422
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3423
    auto var_num =
3424
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3425
    variable_map_[std_dev_name] = var_num;
3426
  }
3427
}
3428

3429
void LibMesh::remove_scores()
3430
{
3431
  if (equation_systems_) {
3432
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3433
    eqn_sys.clear();
3434
    variable_map_.clear();
3435
  }
3436
}
3437

3438
void LibMesh::set_score_data(const std::string& var_name,
3439
  const vector<double>& values, const vector<double>& std_dev)
3440
{
3441
  if (adaptive_) {
3442
    warning(fmt::format(
3443
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3444
      this->id_));
3445

3446
    return;
3447
  }
3448

3449
  if (!equation_systems_) {
3450
    build_eqn_sys();
3451
  }
3452

3453
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3454

3455
  if (!eqn_sys.is_initialized()) {
3456
    equation_systems_->init();
3457
  }
3458

3459
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3460

3461
  // look up the value variable
3462
  std::string value_name = var_name + "_mean";
3463
  unsigned int value_num = variable_map_.at(value_name);
3464
  // look up the std dev variable
3465
  std::string std_dev_name = var_name + "_std_dev";
3466
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3467

3468
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3469
       it++) {
845,761✔
3470
    if (!(*it)->active()) {
3471
      continue;
845,761✔
3472
    }
3473

3474
    auto bin = get_bin_from_element(*it);
86,199✔
3475

3476
    // set value
3477
    vector<libMesh::dof_id_type> value_dof_indices;
3478
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3479
    assert(value_dof_indices.size() == 1);
86,199✔
3480
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3481

86,199✔
3482
    // set std dev
86,199✔
3483
    vector<libMesh::dof_id_type> std_dev_dof_indices;
86,199✔
3484
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3485
    assert(std_dev_dof_indices.size() == 1);
3486
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3487
  }
172,398✔
3488
}
3489

3490
void LibMesh::write(const std::string& filename) const
203,856✔
3491
{
3492
  if (adaptive_) {
3493
    warning(fmt::format(
3494
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
203,856✔
3495
      this->id_));
3496

3497
    return;
203,856✔
3498
  }
203,856✔
3499

203,856✔
3500
  write_message(fmt::format(
3501
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
3502
  libMesh::ExodusII_IO exo(*m_);
3503
  std::set<std::string> systems_out = {eq_system_name_};
3504
  exo.write_discontinuous_exodusII(
203,856✔
3505
    filename + ".e", *equation_systems_, &systems_out);
1,019,280✔
3506
}
815,424✔
3507

3508
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3509
  vector<int>& bins, vector<double>& lengths) const
203,856✔
3510
{
203,856✔
3511
  // TODO: Implement triangle crossings here
3512
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3513
}
3514

3515
int LibMesh::get_bin(Position r) const
3516
{
3517
  // look-up a tet using the point locator
3518
  libMesh::Point p(r.x, r.y, r.z);
3519

3520
  // quick rejection check
3521
  if (!bbox_.contains_point(p)) {
3522
    return -1;
3523
  }
3524

3525
  const auto& point_locator = pl_.at(thread_num());
3526

3527
  const auto elem_ptr = (*point_locator)(p);
3528
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3529
}
3530

3531
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3532
{
3533
  int bin =
3534
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3535
  if (bin >= n_bins() || bin < 0) {
3536
    fatal_error(fmt::format("Invalid bin: {}", bin));
3537
  }
3538
  return bin;
3539
}
3540

3541
std::pair<vector<double>, vector<double>> LibMesh::plot(
3542
  Position plot_ll, Position plot_ur) const
3543
{
3544
  return {};
3545
}
3546

3547
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3548
{
3549
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3550
}
3551

3552
double LibMesh::volume(int bin) const
3553
{
3554
  return this->get_element_from_bin(bin).volume();
3555
}
3556

3557
#endif // LIBMESH
3558

3559
//==============================================================================
3560
// Non-member functions
3561
//==============================================================================
3562

3563
void read_meshes(pugi::xml_node root)
3564
{
3565
  std::unordered_set<int> mesh_ids;
3566

3567
  for (auto node : root.children("mesh")) {
3568
    // Check to make sure multiple meshes in the same file don't share IDs
3569
    int id = std::stoi(get_node_value(node, "id"));
3570
    if (contains(mesh_ids, id)) {
3571
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
3572
                              "'{}' in the same input file",
3573
        id));
3574
    }
3575
    mesh_ids.insert(id);
3576

3577
    // If we've already read a mesh with the same ID in a *different* file,
3578
    // assume it is the same here
3579
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3580
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3581
      continue;
3582
    }
3583

3584
    std::string mesh_type;
3585
    if (check_for_node(node, "type")) {
3586
      mesh_type = get_node_value(node, "type", true, true);
3587
    } else {
3588
      mesh_type = "regular";
3589
    }
3590

3591
    // determine the mesh library to use
3592
    std::string mesh_lib;
3593
    if (check_for_node(node, "library")) {
3594
      mesh_lib = get_node_value(node, "library", true, true);
3595
    }
3596

3597
    // Read mesh and add to vector
3598
    if (mesh_type == RegularMesh::mesh_type) {
3599
      model::meshes.push_back(make_unique<RegularMesh>(node));
3600
    } else if (mesh_type == RectilinearMesh::mesh_type) {
3601
      model::meshes.push_back(make_unique<RectilinearMesh>(node));
3602
    } else if (mesh_type == CylindricalMesh::mesh_type) {
3603
      model::meshes.push_back(make_unique<CylindricalMesh>(node));
3604
    } else if (mesh_type == SphericalMesh::mesh_type) {
3605
      model::meshes.push_back(make_unique<SphericalMesh>(node));
3606
    } else if (mesh_type == HexagonalMesh::mesh_type) {
3607
      model::meshes.push_back(make_unique<HexagonalMesh>(node));
3608
#ifdef DAGMC
3609
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3610
               mesh_lib == MOABMesh::mesh_lib_type) {
3611
      model::meshes.push_back(make_unique<MOABMesh>(node));
3612
#endif
3613
#ifdef LIBMESH
3614
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3615
               mesh_lib == LibMesh::mesh_lib_type) {
3616
      model::meshes.push_back(make_unique<LibMesh>(node));
3617
#endif
3618
    } else if (mesh_type == UnstructuredMesh::mesh_type) {
3619
      fatal_error("Unstructured mesh support is not enabled or the mesh "
3620
                  "library is invalid.");
3621
    } else {
3622
      fatal_error("Invalid mesh type: " + mesh_type);
3623
    }
3624

3625
    // Map ID to position in vector
3626
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3627
  }
3628
}
3629

3630
void meshes_to_hdf5(hid_t group)
3631
{
3632
  // Write number of meshes
3633
  hid_t meshes_group = create_group(group, "meshes");
3634
  int32_t n_meshes = model::meshes.size();
3635
  write_attribute(meshes_group, "n_meshes", n_meshes);
3636

3637
  if (n_meshes > 0) {
3638
    // Write IDs of meshes
3639
    vector<int> ids;
3640
    for (const auto& m : model::meshes) {
3641
      m->to_hdf5(meshes_group);
3642
      ids.push_back(m->id_);
3643
    }
23✔
3644
    write_attribute(meshes_group, "ids", ids);
3645
  }
3646

3647
  close_group(meshes_group);
23✔
3648
}
23✔
3649

23✔
3650
void free_memory_mesh()
23✔
3651
{
3652
  model::meshes.clear();
3653
  model::mesh_map.clear();
3654
}
3655

3656
extern "C" int n_meshes()
3657
{
3658
  return model::meshes.size();
3659
}
3660

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