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

openmc-dev / openmc / 13591584831

28 Feb 2025 03:46PM UTC coverage: 85.051% (+0.3%) from 84.722%
13591584831

Pull #3067

github

web-flow
Merge 08055e996 into c26fde666
Pull Request #3067: Implement user-configurable random number stride

36 of 44 new or added lines in 8 files covered. (81.82%)

3588 existing lines in 111 files now uncovered.

51062 of 60037 relevant lines covered (85.05%)

32650986.73 hits per line

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

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

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

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

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

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

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

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

60
namespace openmc {
61

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

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

72
namespace model {
73

74
std::unordered_map<int32_t, int32_t> mesh_map;
75
vector<unique_ptr<Mesh>> meshes;
76

77
} // namespace model
78

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

86
//==============================================================================
87
// Helper functions
88
//==============================================================================
89

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

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

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

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

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

138
namespace detail {
139

140
//==============================================================================
141
// MaterialVolumes implementation
142
//==============================================================================
143

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

153
  // Loop for linear probing
154
  for (int attempt = 0; attempt < table_size_; ++attempt) {
2,543,388✔
155
    // Determine slot to check
156
    int slot = (index_material + attempt) % table_size_;
2,543,388✔
157
    int32_t* slot_ptr = &this->materials(index_elem, slot);
2,543,388✔
158

159
    // Non-atomic read of current material
160
    int32_t current_val = *slot_ptr;
2,543,388✔
161

162
    // Found the desired material; accumulate volume
163
    if (current_val == index_material) {
2,543,388✔
164
#pragma omp atomic
1,237,161✔
165
      this->volumes(index_elem, slot) += volume;
2,473,783✔
166
      return;
2,473,783✔
167
    }
168

169
    // Slot appears to be empty; attempt to claim
170
    if (current_val == EMPTY) {
69,605✔
171
      // Attempt compare-and-swap from EMPTY to index_material
172
      int32_t expected_val = EMPTY;
1,373✔
173
      bool claimed_slot =
174
        atomic_cas_int32(slot_ptr, expected_val, index_material);
1,373✔
175

176
      // If we claimed the slot or another thread claimed it but the same
177
      // material was inserted, proceed to accumulate
178
      if (claimed_slot || (expected_val == index_material)) {
1,373✔
179
#pragma omp atomic
690✔
180
        this->volumes(index_elem, slot) += volume;
1,373✔
181
        return;
1,373✔
182
      }
183
    }
184
  }
185

186
  // If table is full, set a flag that can be checked later
UNCOV
187
  table_full_ = true;
×
188
}
189

UNCOV
190
void MaterialVolumes::add_volume_unsafe(
×
191
  int index_elem, int index_material, double volume)
192
{
193
  // Linear probe
UNCOV
194
  for (int attempt = 0; attempt < table_size_; ++attempt) {
×
UNCOV
195
    int slot = (index_material + attempt) % table_size_;
×
196

197
    // Read current material
UNCOV
198
    int32_t current_val = this->materials(index_elem, slot);
×
199

200
    // Found the desired material; accumulate volume
201
    if (current_val == index_material) {
×
UNCOV
202
      this->volumes(index_elem, slot) += volume;
×
UNCOV
203
      return;
×
204
    }
205

206
    // Claim empty slot
UNCOV
207
    if (current_val == EMPTY) {
×
UNCOV
208
      this->materials(index_elem, slot) = index_material;
×
UNCOV
209
      this->volumes(index_elem, slot) += volume;
×
UNCOV
210
      return;
×
211
    }
212
  }
213

214
  // If table is full, set a flag that can be checked later
UNCOV
215
  table_full_ = true;
×
216
}
217

218
} // namespace detail
219

220
//==============================================================================
221
// Mesh implementation
222
//==============================================================================
223

224
Mesh::Mesh(pugi::xml_node node)
2,557✔
225
{
226
  // Read mesh id
227
  id_ = std::stoi(get_node_value(node, "id"));
2,557✔
228
  if (check_for_node(node, "name"))
2,557✔
229
    name_ = get_node_value(node, "name");
17✔
230
}
2,557✔
231

232
void Mesh::set_id(int32_t id)
1✔
233
{
234
  assert(id >= 0 || id == C_NONE);
1✔
235

236
  // Clear entry in mesh map in case one was already assigned
237
  if (id_ != C_NONE) {
1✔
UNCOV
238
    model::mesh_map.erase(id_);
×
UNCOV
239
    id_ = C_NONE;
×
240
  }
241

242
  // Ensure no other mesh has the same ID
243
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
1✔
UNCOV
244
    throw std::runtime_error {
×
UNCOV
245
      fmt::format("Two meshes have the same ID: {}", id)};
×
246
  }
247

248
  // If no ID is specified, auto-assign the next ID in the sequence
249
  if (id == C_NONE) {
1✔
250
    id = 0;
1✔
251
    for (const auto& m : model::meshes) {
3✔
252
      id = std::max(id, m->id_);
2✔
253
    }
254
    ++id;
1✔
255
  }
256

257
  // Update ID and entry in the mesh map
258
  id_ = id;
1✔
259
  model::mesh_map[id] = model::meshes.size() - 1;
1✔
260
}
1✔
261

262
vector<double> Mesh::volumes() const
508✔
263
{
264
  vector<double> volumes(n_bins());
508✔
265
  for (int i = 0; i < n_bins(); i++) {
1,057,768✔
266
    volumes[i] = this->volume(i);
1,057,260✔
267
  }
268
  return volumes;
508✔
UNCOV
269
}
×
270

271
void Mesh::material_volumes(int nx, int ny, int nz, int table_size,
156✔
272
  int32_t* materials, double* volumes) const
273
{
274
  if (mpi::master) {
156✔
275
    header("MESH MATERIAL VOLUMES CALCULATION", 7);
156✔
276
  }
277
  write_message(7, "Number of rays (x) = {}", nx);
156✔
278
  write_message(7, "Number of rays (y) = {}", ny);
156✔
279
  write_message(7, "Number of rays (z) = {}", nz);
156✔
280
  int64_t n_total = nx * ny + ny * nz + nx * nz;
156✔
281
  write_message(7, "Total number of rays = {}", n_total);
156✔
282
  write_message(
156✔
283
    7, "Maximum number of materials per mesh element = {}", table_size);
284

285
  Timer timer;
156✔
286
  timer.start();
156✔
287

288
  // Create object for keeping track of materials/volumes
289
  detail::MaterialVolumes result(materials, volumes, table_size);
156✔
290

291
  // Determine bounding box
292
  auto bbox = this->bounding_box();
156✔
293

294
  std::array<int, 3> n_rays = {nx, ny, nz};
156✔
295

296
  // Determine effective width of rays
297
  Position width((bbox.xmax - bbox.xmin) / nx, (bbox.ymax - bbox.ymin) / ny,
156✔
298
    (bbox.zmax - bbox.zmin) / nz);
156✔
299

300
  // Set flag for mesh being contained within model
301
  bool out_of_model = false;
156✔
302

303
#pragma omp parallel
78✔
304
  {
305
    // Preallocate vector for mesh indices and length fractions and particle
306
    std::vector<int> bins;
78✔
307
    std::vector<double> length_fractions;
78✔
308
    Particle p;
78✔
309

310
    SourceSite site;
78✔
311
    site.E = 1.0;
78✔
312
    site.particle = ParticleType::neutron;
78✔
313

314
    for (int axis = 0; axis < 3; ++axis) {
312✔
315
      // Set starting position and direction
316
      site.r = {0.0, 0.0, 0.0};
234✔
317
      site.r[axis] = bbox.min()[axis];
234✔
318
      site.u = {0.0, 0.0, 0.0};
234✔
319
      site.u[axis] = 1.0;
234✔
320

321
      // Determine width of rays and number of rays in other directions
322
      int ax1 = (axis + 1) % 3;
234✔
323
      int ax2 = (axis + 2) % 3;
234✔
324
      double min1 = bbox.min()[ax1];
234✔
325
      double min2 = bbox.min()[ax2];
234✔
326
      double d1 = width[ax1];
234✔
327
      double d2 = width[ax2];
234✔
328
      int n1 = n_rays[ax1];
234✔
329
      int n2 = n_rays[ax2];
234✔
330

331
      // Divide rays in first direction over MPI processes by computing starting
332
      // and ending indices
333
      int min_work = n1 / mpi::n_procs;
234✔
334
      int remainder = n1 % mpi::n_procs;
234✔
335
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
234✔
336
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
234✔
337
      int i1_end = i1_start + n1_local;
234✔
338

339
      // Loop over rays on face of bounding box
340
#pragma omp for collapse(2)
341
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
9,924✔
342
        for (int i2 = 0; i2 < n2; ++i2) {
503,964✔
343
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
494,274✔
344
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
494,274✔
345

346
          p.from_source(&site);
494,274✔
347

348
          // Determine particle's location
349
          if (!exhaustive_find_cell(p)) {
494,274✔
350
            out_of_model = true;
47,916✔
351
            continue;
47,916✔
352
          }
353

354
          // Set birth cell attribute
355
          if (p.cell_born() == C_NONE)
446,358✔
356
            p.cell_born() = p.lowest_coord().cell;
446,358✔
357

358
          // Initialize last cells from current cell
359
          for (int j = 0; j < p.n_coord(); ++j) {
892,716✔
360
            p.cell_last(j) = p.coord(j).cell;
446,358✔
361
          }
362
          p.n_coord_last() = p.n_coord();
446,358✔
363

364
          while (true) {
365
            // Ray trace from r_start to r_end
366
            Position r0 = p.r();
973,974✔
367
            double max_distance = bbox.max()[axis] - r0[axis];
973,974✔
368

369
            // Find the distance to the nearest boundary
370
            BoundaryInfo boundary = distance_to_boundary(p);
973,974✔
371

372
            // Advance particle forward
373
            double distance = std::min(boundary.distance, max_distance);
973,974✔
374
            p.move_distance(distance);
973,974✔
375

376
            // Determine what mesh elements were crossed by particle
377
            bins.clear();
973,974✔
378
            length_fractions.clear();
973,974✔
379
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
973,974✔
380

381
            // Add volumes to any mesh elements that were crossed
382
            int i_material = p.material();
973,974✔
383
            if (i_material != C_NONE) {
973,974✔
384
              i_material = model::materials[i_material]->id();
939,858✔
385
            }
386
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
2,211,552✔
387
              int mesh_index = bins[i_bin];
1,237,578✔
388
              double length = distance * length_fractions[i_bin];
1,237,578✔
389

390
              // Add volume to result
391
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,237,578✔
392
            }
393

394
            if (distance == max_distance)
973,974✔
395
              break;
446,358✔
396

397
            // cross next geometric surface
398
            for (int j = 0; j < p.n_coord(); ++j) {
1,055,232✔
399
              p.cell_last(j) = p.coord(j).cell;
527,616✔
400
            }
401
            p.n_coord_last() = p.n_coord();
527,616✔
402

403
            // Set surface that particle is on and adjust coordinate levels
404
            p.surface() = boundary.surface;
527,616✔
405
            p.n_coord() = boundary.coord_level;
527,616✔
406

407
            if (boundary.lattice_translation[0] != 0 ||
527,616✔
408
                boundary.lattice_translation[1] != 0 ||
1,055,232✔
409
                boundary.lattice_translation[2] != 0) {
527,616✔
410
              // Particle crosses lattice boundary
411
              cross_lattice(p, boundary);
412
            } else {
413
              // Particle crosses surface
414
              const auto& surf {model::surfaces[p.surface_index()].get()};
527,616✔
415
              p.cross_surface(*surf);
527,616✔
416
            }
417
          }
527,616✔
418
        }
419
      }
420
    }
421
  }
78✔
422

423
  // Check for errors
424
  if (out_of_model) {
156✔
425
    throw std::runtime_error("Mesh not fully contained in geometry.");
12✔
426
  } else if (result.table_full()) {
144✔
UNCOV
427
    throw std::runtime_error("Maximum number of materials for mesh material "
×
428
                             "volume calculation insufficient.");
×
429
  }
430

431
  // Compute time for raytracing
432
  double t_raytrace = timer.elapsed();
144✔
433

434
#ifdef OPENMC_MPI
435
  // Combine results from multiple MPI processes
436
  if (mpi::n_procs > 1) {
60✔
437
    int total = this->n_bins() * table_size;
438
    if (mpi::master) {
439
      // Allocate temporary buffer for receiving data
440
      std::vector<int32_t> mats(total);
441
      std::vector<double> vols(total);
442

443
      for (int i = 1; i < mpi::n_procs; ++i) {
444
        // Receive material indices and volumes from process i
445
        MPI_Recv(
446
          mats.data(), total, MPI_INT, i, i, mpi::intracomm, MPI_STATUS_IGNORE);
447
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
448
          MPI_STATUS_IGNORE);
449

450
        // Combine with existing results; we can call thread unsafe version of
451
        // add_volume because each thread is operating on a different element
452
#pragma omp for
453
        for (int index_elem = 0; index_elem < n_bins(); ++index_elem) {
454
          for (int k = 0; k < table_size; ++k) {
455
            int index = index_elem * table_size + k;
456
            result.add_volume_unsafe(index_elem, mats[index], vols[index]);
457
          }
458
        }
459
      }
460
    } else {
461
      // Send material indices and volumes to process 0
462
      MPI_Send(materials, total, MPI_INT, 0, mpi::rank, mpi::intracomm);
463
      MPI_Send(volumes, total, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
464
    }
465
  }
466

467
  // Report time for MPI communication
468
  double t_mpi = timer.elapsed() - t_raytrace;
60✔
469
#else
470
  double t_mpi = 0.0;
84✔
471
#endif
472

473
  // Normalize based on known volumes of elements
474
  for (int i = 0; i < this->n_bins(); ++i) {
1,020✔
475
    // Estimated total volume in element i
476
    double volume = 0.0;
876✔
477
    for (int j = 0; j < table_size; ++j) {
7,884✔
478
      volume += result.volumes(i, j);
7,008✔
479
    }
480
    // Renormalize volumes based on known volume of element i
481
    double norm = this->volume(i) / volume;
876✔
482
    for (int j = 0; j < table_size; ++j) {
7,884✔
483
      result.volumes(i, j) *= norm;
7,008✔
484
    }
485
  }
486

487
  // Show elapsed time
488
  timer.stop();
144✔
489
  double t_total = timer.elapsed();
144✔
490
  double t_normalize = t_total - t_raytrace - t_mpi;
144✔
491
  if (mpi::master) {
144✔
492
    header("Timing Statistics", 7);
144✔
493
    show_time("Total time elapsed", t_total);
144✔
494
    show_time("Ray tracing", t_raytrace, 1);
144✔
495
    show_time("Ray tracing (per ray)", t_raytrace / n_total, 1);
144✔
496
    show_time("MPI communication", t_mpi, 1);
144✔
497
    show_time("Normalization", t_normalize, 1);
144✔
498
    std::fflush(stdout);
144✔
499
  }
500
}
144✔
501

502
void Mesh::to_hdf5(hid_t group) const
2,593✔
503
{
504
  // Create group for mesh
505
  std::string group_name = fmt::format("mesh {}", id_);
4,694✔
506
  hid_t mesh_group = create_group(group, group_name.c_str());
2,593✔
507

508
  // Write mesh type
509
  write_dataset(mesh_group, "type", this->get_mesh_type());
2,593✔
510

511
  // Write mesh ID
512
  write_attribute(mesh_group, "id", id_);
2,593✔
513

514
  // Write mesh name
515
  write_dataset(mesh_group, "name", name_);
2,593✔
516

517
  // Write mesh data
518
  this->to_hdf5_inner(mesh_group);
2,593✔
519

520
  // Close group
521
  close_group(mesh_group);
2,593✔
522
}
2,593✔
523

524
//==============================================================================
525
// Structured Mesh implementation
526
//==============================================================================
527

528
std::string StructuredMesh::bin_label(int bin) const
5,419,584✔
529
{
530
  MeshIndex ijk = get_indices_from_bin(bin);
5,419,584✔
531

532
  if (n_dimension_ > 2) {
5,419,584✔
533
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,806,720✔
534
  } else if (n_dimension_ > 1) {
16,224✔
535
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,848✔
536
  } else {
537
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
538
  }
539
}
540

541
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,262✔
542
{
543
  // because method is const, shape_ is const as well and can't be adapted
544
  auto tmp_shape = shape_;
2,262✔
545
  return xt::adapt(tmp_shape, {n_dimension_});
4,524✔
546
}
547

548
Position StructuredMesh::sample_element(
1,542,372✔
549
  const MeshIndex& ijk, uint64_t* seed) const
550
{
551
  // lookup the lower/upper bounds for the mesh element
552
  double x_min = negative_grid_boundary(ijk, 0);
1,542,372✔
553
  double x_max = positive_grid_boundary(ijk, 0);
1,542,372✔
554

555
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
556
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
557

558
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
559
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
560

561
  return {x_min + (x_max - x_min) * prn(seed),
1,542,372✔
562
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,542,372✔
563
}
564

565
//==============================================================================
566
// Unstructured Mesh implementation
567
//==============================================================================
568

569
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
46✔
570
{
571
  // check the mesh type
572
  if (check_for_node(node, "type")) {
46✔
573
    auto temp = get_node_value(node, "type", true, true);
46✔
574
    if (temp != mesh_type) {
46✔
UNCOV
575
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
576
    }
577
  }
46✔
578

579
  // check if a length unit multiplier was specified
580
  if (check_for_node(node, "length_multiplier")) {
46✔
UNCOV
581
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
582
  }
583

584
  // get the filename of the unstructured mesh to load
585
  if (check_for_node(node, "filename")) {
46✔
586
    filename_ = get_node_value(node, "filename");
46✔
587
    if (!file_exists(filename_)) {
46✔
UNCOV
588
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
589
    }
590
  } else {
UNCOV
591
    fatal_error(fmt::format(
×
UNCOV
592
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
593
  }
594

595
  if (check_for_node(node, "options")) {
46✔
596
    options_ = get_node_value(node, "options");
16✔
597
  }
598

599
  // check if mesh tally data should be written with
600
  // statepoint files
601
  if (check_for_node(node, "output")) {
46✔
UNCOV
602
    output_ = get_node_value_bool(node, "output");
×
603
  }
604
}
46✔
605

606
void UnstructuredMesh::determine_bounds()
24✔
607
{
608
  double xmin = INFTY;
24✔
609
  double ymin = INFTY;
24✔
610
  double zmin = INFTY;
24✔
611
  double xmax = -INFTY;
24✔
612
  double ymax = -INFTY;
24✔
613
  double zmax = -INFTY;
24✔
614
  int n = this->n_vertices();
24✔
615
  for (int i = 0; i < n; ++i) {
55,936✔
616
    auto v = this->vertex(i);
55,912✔
617
    xmin = std::min(v.x, xmin);
55,912✔
618
    ymin = std::min(v.y, ymin);
55,912✔
619
    zmin = std::min(v.z, zmin);
55,912✔
620
    xmax = std::max(v.x, xmax);
55,912✔
621
    ymax = std::max(v.y, ymax);
55,912✔
622
    zmax = std::max(v.z, zmax);
55,912✔
623
  }
624
  lower_left_ = {xmin, ymin, zmin};
24✔
625
  upper_right_ = {xmax, ymax, zmax};
24✔
626
}
24✔
627

628
Position UnstructuredMesh::sample_tet(
601,230✔
629
  std::array<Position, 4> coords, uint64_t* seed) const
630
{
631
  // Uniform distribution
632
  double s = prn(seed);
601,230✔
633
  double t = prn(seed);
601,230✔
634
  double u = prn(seed);
601,230✔
635

636
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
637
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
638
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
639
  if (s + t > 1) {
601,230✔
640
    s = 1.0 - s;
301,245✔
641
    t = 1.0 - t;
301,245✔
642
  }
643
  if (s + t + u > 1) {
601,230✔
644
    if (t + u > 1) {
400,908✔
645
      double old_t = t;
199,920✔
646
      t = 1.0 - u;
199,920✔
647
      u = 1.0 - s - old_t;
199,920✔
648
    } else if (t + u <= 1) {
200,988✔
649
      double old_s = s;
200,988✔
650
      s = 1.0 - t - u;
200,988✔
651
      u = old_s + t + u - 1;
200,988✔
652
    }
653
  }
654
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
655
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
656
}
657

658
const std::string UnstructuredMesh::mesh_type = "unstructured";
659

660
std::string UnstructuredMesh::get_mesh_type() const
31✔
661
{
662
  return mesh_type;
31✔
663
}
664

UNCOV
665
void UnstructuredMesh::surface_bins_crossed(
×
666
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
667
{
UNCOV
668
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
669
}
670

671
std::string UnstructuredMesh::bin_label(int bin) const
205,712✔
672
{
673
  return fmt::format("Mesh Index ({})", bin);
205,712✔
674
};
675

676
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
31✔
677
{
678
  write_dataset(mesh_group, "filename", filename_);
31✔
679
  write_dataset(mesh_group, "library", this->library());
31✔
680
  if (!options_.empty()) {
31✔
681
    write_attribute(mesh_group, "options", options_);
8✔
682
  }
683

684
  if (length_multiplier_ > 0.0)
31✔
UNCOV
685
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
686

687
  // write vertex coordinates
688
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
31✔
689
  for (int i = 0; i < this->n_vertices(); i++) {
70,260✔
690
    auto v = this->vertex(i);
70,229✔
691
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
70,229✔
692
  }
693
  write_dataset(mesh_group, "vertices", vertices);
31✔
694

695
  int num_elem_skipped = 0;
31✔
696

697
  // write element types and connectivity
698
  vector<double> volumes;
31✔
699
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
31✔
700
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
31✔
701
  for (int i = 0; i < this->n_bins(); i++) {
349,743✔
702
    auto conn = this->connectivity(i);
349,712✔
703

704
    volumes.emplace_back(this->volume(i));
349,712✔
705

706
    // write linear tet element
707
    if (conn.size() == 4) {
349,712✔
708
      xt::view(elem_types, i, xt::all()) =
695,424✔
709
        static_cast<int>(ElementType::LINEAR_TET);
695,424✔
710
      xt::view(connectivity, i, xt::all()) =
695,424✔
711
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
1,043,136✔
712
      // write linear hex element
713
    } else if (conn.size() == 8) {
2,000✔
714
      xt::view(elem_types, i, xt::all()) =
4,000✔
715
        static_cast<int>(ElementType::LINEAR_HEX);
4,000✔
716
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000✔
717
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000✔
718
    } else {
UNCOV
719
      num_elem_skipped++;
×
UNCOV
720
      xt::view(elem_types, i, xt::all()) =
×
721
        static_cast<int>(ElementType::UNSUPPORTED);
UNCOV
722
      xt::view(connectivity, i, xt::all()) = -1;
×
723
    }
724
  }
349,712✔
725

726
  // warn users that some elements were skipped
727
  if (num_elem_skipped > 0) {
31✔
UNCOV
728
    warning(fmt::format("The connectivity of {} elements "
×
729
                        "on mesh {} were not written "
730
                        "because they are not of type linear tet/hex.",
UNCOV
731
      num_elem_skipped, this->id_));
×
732
  }
733

734
  write_dataset(mesh_group, "volumes", volumes);
31✔
735
  write_dataset(mesh_group, "connectivity", connectivity);
31✔
736
  write_dataset(mesh_group, "element_types", elem_types);
31✔
737
}
31✔
738

739
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
740
{
741
  length_multiplier_ = length_multiplier;
23✔
742
}
23✔
743

744
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
745
{
746
  auto conn = connectivity(bin);
120,000✔
747

748
  if (conn.size() == 4)
120,000✔
749
    return ElementType::LINEAR_TET;
120,000✔
UNCOV
750
  else if (conn.size() == 8)
×
UNCOV
751
    return ElementType::LINEAR_HEX;
×
752
  else
UNCOV
753
    return ElementType::UNSUPPORTED;
×
754
}
120,000✔
755

756
StructuredMesh::MeshIndex StructuredMesh::get_indices(
757,167,489✔
757
  Position r, bool& in_mesh) const
758
{
759
  MeshIndex ijk;
760
  in_mesh = true;
757,167,489✔
761
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
762
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
763

764
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
765
      in_mesh = false;
91,752,208✔
766
  }
767
  return ijk;
757,167,489✔
768
}
769

770
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
865,291,735✔
771
{
772
  switch (n_dimension_) {
865,291,735✔
773
  case 1:
956,736✔
774
    return ijk[0] - 1;
956,736✔
775
  case 2:
21,622,536✔
776
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,622,536✔
777
  case 3:
842,712,463✔
778
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
842,712,463✔
UNCOV
779
  default:
×
UNCOV
780
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
781
  }
782
}
783

784
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,117,052✔
785
{
786
  MeshIndex ijk;
787
  if (n_dimension_ == 1) {
8,117,052✔
788
    ijk[0] = bin + 1;
300✔
789
  } else if (n_dimension_ == 2) {
8,116,752✔
790
    ijk[0] = bin % shape_[0] + 1;
15,924✔
791
    ijk[1] = bin / shape_[0] + 1;
15,924✔
792
  } else if (n_dimension_ == 3) {
8,100,828✔
793
    ijk[0] = bin % shape_[0] + 1;
8,100,828✔
794
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,100,828✔
795
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,100,828✔
796
  }
797
  return ijk;
8,117,052✔
798
}
799

800
int StructuredMesh::get_bin(Position r) const
259,497,320✔
801
{
802
  // Determine indices
803
  bool in_mesh;
804
  MeshIndex ijk = get_indices(r, in_mesh);
259,497,320✔
805
  if (!in_mesh)
259,497,320✔
806
    return -1;
22,164,978✔
807

808
  // Convert indices to bin
809
  return get_bin_from_indices(ijk);
237,332,342✔
810
}
811

812
int StructuredMesh::n_bins() const
1,073,148✔
813
{
814
  return std::accumulate(
1,073,148✔
815
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,146,296✔
816
}
817

818
int StructuredMesh::n_surface_bins() const
413✔
819
{
820
  return 4 * n_dimension_ * n_bins();
413✔
821
}
822

UNCOV
823
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
824
  const SourceSite* bank, int64_t length, bool* outside) const
825
{
826
  // Determine shape of array for counts
UNCOV
827
  std::size_t m = this->n_bins();
×
UNCOV
828
  vector<std::size_t> shape = {m};
×
829

830
  // Create array of zeros
UNCOV
831
  xt::xarray<double> cnt {shape, 0.0};
×
UNCOV
832
  bool outside_ = false;
×
833

UNCOV
834
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
835
    const auto& site = bank[i];
×
836

837
    // determine scoring bin for entropy mesh
UNCOV
838
    int mesh_bin = get_bin(site.r);
×
839

840
    // if outside mesh, skip particle
UNCOV
841
    if (mesh_bin < 0) {
×
UNCOV
842
      outside_ = true;
×
UNCOV
843
      continue;
×
844
    }
845

846
    // Add to appropriate bin
UNCOV
847
    cnt(mesh_bin) += site.wgt;
×
848
  }
849

850
  // Create copy of count data. Since ownership will be acquired by xtensor,
851
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
852
  // warnings.
UNCOV
853
  int total = cnt.size();
×
UNCOV
854
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
855

856
#ifdef OPENMC_MPI
857
  // collect values from all processors
858
  MPI_Reduce(
859
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
860

861
  // Check if there were sites outside the mesh for any processor
862
  if (outside) {
863
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
864
  }
865
#else
866
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
867
  if (outside)
868
    *outside = outside_;
869
#endif
870

871
  // Adapt reduced values in array back into an xarray
UNCOV
872
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
UNCOV
873
  xt::xarray<double> counts = arr;
×
874

UNCOV
875
  return counts;
×
876
}
877

878
// raytrace through the mesh. The template class T will do the tallying.
879
// A modern optimizing compiler can recognize the noop method of T and
880
// eliminate that call entirely.
881
template<class T>
882
void StructuredMesh::raytrace_mesh(
501,925,730✔
883
  Position r0, Position r1, const Direction& u, T tally) const
884
{
885
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
886
  // enable/disable tally calls for now, T template type needs to provide both
887
  // surface and track methods, which might be empty. modern optimizing
888
  // compilers will (hopefully) eliminate the complete code (including
889
  // calculation of parameters) but for the future: be explicit
890

891
  // Compute the length of the entire track.
892
  double total_distance = (r1 - r0).norm();
501,925,730✔
893
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
501,925,730✔
894
    return;
9,887,605✔
895

896
  const int n = n_dimension_;
492,038,125✔
897

898
  // Flag if position is inside the mesh
899
  bool in_mesh;
900

901
  // Position is r = r0 + u * traveled_distance, start at r0
902
  double traveled_distance {0.0};
492,038,125✔
903

904
  // Calculate index of current cell. Offset the position a tiny bit in
905
  // direction of flight
906
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
492,038,125✔
907

908
  // if track is very short, assume that it is completely inside one cell.
909
  // Only the current cell will score and no surfaces
910
  if (total_distance < 2 * TINY_BIT) {
492,038,125✔
911
    if (in_mesh) {
41,484✔
912
      tally.track(ijk, 1.0);
41,472✔
913
    }
914
    return;
41,484✔
915
  }
916

917
  // translate start and end positions,
918
  // this needs to come after the get_indices call because it does its own
919
  // translation
920
  local_coords(r0);
491,996,641✔
921
  local_coords(r1);
491,996,641✔
922

923
  // Calculate initial distances to next surfaces in all three dimensions
924
  std::array<MeshDistance, 3> distances;
983,993,282✔
925
  for (int k = 0; k < n; ++k) {
1,949,954,524✔
926
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
1,457,957,883✔
927
  }
928

929
  // Loop until r = r1 is eventually reached
930
  while (true) {
315,822,995✔
931

932
    if (in_mesh) {
807,819,636✔
933

934
      // find surface with minimal distance to current position
935
      const auto k = std::min_element(distances.begin(), distances.end()) -
724,461,914✔
936
                     distances.begin();
724,461,914✔
937

938
      // Tally track length delta since last step
939
      tally.track(ijk,
724,461,914✔
940
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
724,461,914✔
941
          total_distance);
942

943
      // update position and leave, if we have reached end position
944
      traveled_distance = distances[k].distance;
724,461,914✔
945
      if (traveled_distance >= total_distance)
724,461,914✔
946
        return;
414,270,963✔
947

948
      // If we have not reached r1, we have hit a surface. Tally outward
949
      // current
950
      tally.surface(ijk, k, distances[k].max_surface, false);
310,190,951✔
951

952
      // Update cell and calculate distance to next surface in k-direction.
953
      // The two other directions are still valid!
954
      ijk[k] = distances[k].next_index;
310,190,951✔
955
      distances[k] =
310,190,951✔
956
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
310,190,951✔
957

958
      // Check if we have left the interior of the mesh
959
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
310,190,951✔
960

961
      // If we are still inside the mesh, tally inward current for the next
962
      // cell
963
      if (in_mesh)
310,190,951✔
964
        tally.surface(ijk, k, !distances[k].max_surface, true);
294,269,295✔
965

966
    } else { // not inside mesh
967

968
      // For all directions outside the mesh, find the distance that we need
969
      // to travel to reach the next surface. Use the largest distance, as
970
      // only this will cross all outer surfaces.
971
      int k_max {0};
83,357,722✔
972
      for (int k = 0; k < n; ++k) {
331,089,502✔
973
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
332,432,076✔
974
            (distances[k].distance > traveled_distance)) {
84,700,296✔
975
          traveled_distance = distances[k].distance;
83,914,536✔
976
          k_max = k;
83,914,536✔
977
        }
978
      }
979

980
      // If r1 is not inside the mesh, exit here
981
      if (traveled_distance >= total_distance)
83,357,722✔
982
        return;
77,725,678✔
983

984
      // Calculate the new cell index and update all distances to next
985
      // surfaces.
986
      ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
5,632,044✔
987
      for (int k = 0; k < n; ++k) {
22,298,916✔
988
        distances[k] =
16,666,872✔
989
          distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
16,666,872✔
990
      }
991

992
      // If inside the mesh, Tally inward current
993
      if (in_mesh)
5,632,044✔
994
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
4,254,984✔
995
    }
996
  }
997
}
998

131,753,582✔
999
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1000
  vector<int>& bins, vector<double>& lengths) const
1001
{
1002

1003
  // Helper tally class.
1004
  // stores a pointer to the mesh class and references to bins and lengths
1005
  // parameters. Performs the actual tally through the track method.
1006
  struct TrackAggregator {
1007
    TrackAggregator(
1008
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
131,753,582✔
1009
      : mesh(_mesh), bins(_bins), lengths(_lengths)
131,753,582✔
UNCOV
1010
    {}
×
1011
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1012
    void track(const MeshIndex& ijk, double l) const
131,753,582✔
1013
    {
1014
      bins.push_back(mesh->get_bin_from_indices(ijk));
1015
      lengths.push_back(l);
1016
    }
1017

1018
    const StructuredMesh* mesh;
131,753,582✔
1019
    vector<int>& bins;
1020
    vector<double>& lengths;
1021
  };
1022

131,753,582✔
1023
  // Perform the mesh raytrace with the helper class.
1024
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1025
}
1026

131,753,582✔
UNCOV
1027
void StructuredMesh::surface_bins_crossed(
×
UNCOV
1028
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
1029
{
UNCOV
1030

×
1031
  // Helper tally class.
1032
  // stores a pointer to the mesh class and a reference to the bins parameter.
1033
  // Performs the actual tally through the surface method.
1034
  struct SurfaceAggregator {
1035
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
1036
      : mesh(_mesh), bins(_bins)
131,753,582✔
1037
    {}
131,753,582✔
1038
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
1039
    {
1040
      int i_bin =
263,507,164✔
1041
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
525,178,184✔
1042
      if (max)
393,424,602✔
1043
        i_bin += 2;
1044
      if (inward)
1045
        i_bin += 1;
1046
      bins.push_back(i_bin);
34,728,881✔
1047
    }
1048
    void track(const MeshIndex& idx, double l) const {}
166,482,463✔
1049

1050
    const StructuredMesh* mesh;
1051
    vector<int>& bins;
164,344,808✔
1052
  };
164,344,808✔
1053

1054
  // Perform the mesh raytrace with the helper class.
1055
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
164,344,808✔
1056
}
164,344,808✔
1057

1058
//==============================================================================
1059
// RegularMesh implementation
1060
//==============================================================================
164,344,808✔
1061

164,344,808✔
1062
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
129,859,515✔
1063
{
1064
  // Determine number of dimensions for mesh
1065
  if (!check_for_node(node, "dimension")) {
1066
    fatal_error("Must specify <dimension> on a regular mesh.");
34,485,293✔
1067
  }
1068

1069
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
1070
  int n = n_dimension_ = shape.size();
34,485,293✔
1071
  if (n != 1 && n != 2 && n != 3) {
34,485,293✔
1072
    fatal_error("Mesh must be one, two, or three dimensions.");
34,485,293✔
1073
  }
1074
  std::copy(shape.begin(), shape.end(), shape_.begin());
1075

34,485,293✔
1076
  // Check that dimensions are all greater than zero
1077
  if (xt::any(shape <= 0)) {
1078
    fatal_error("All entries on the <dimension> element for a tally "
1079
                "mesh must be positive.");
34,485,293✔
1080
  }
33,096,930✔
1081

1082
  // Check for lower-left coordinates
1083
  if (check_for_node(node, "lower_left")) {
1084
    // Read mesh lower-left corner location
1085
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1086
  } else {
1087
    fatal_error("Must specify <lower_left> on a mesh.");
2,137,655✔
1088
  }
8,199,320✔
1089

8,355,872✔
1090
  // Make sure lower_left and dimension match
2,294,207✔
1091
  if (shape.size() != lower_left_.size()) {
2,229,743✔
1092
    fatal_error("Number of entries on <lower_left> must be the same "
2,229,743✔
1093
                "as the number of entries on <dimension>.");
1094
  }
1095

1096
  if (check_for_node(node, "width")) {
1097
    // Make sure one of upper-right or width were specified
2,137,655✔
1098
    if (check_for_node(node, "upper_right")) {
1,894,067✔
1099
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1100
    }
1101

1102
    width_ = get_node_xarray<double>(node, "width");
243,588✔
1103

860,256✔
1104
    // Check to ensure width has same dimensions
616,668✔
1105
    auto n = width_.size();
616,668✔
1106
    if (n != lower_left_.size()) {
1107
      fatal_error("Number of entries on <width> must be the same as "
1108
                  "the number of entries on <lower_left>.");
1109
    }
243,588✔
1110

218,592✔
1111
    // Check for negative widths
1112
    if (xt::any(width_ < 0.0)) {
1113
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1114
    }
370,172,148✔
1115

1116
    // Set width and upper right coordinate
1117
    upper_right_ = xt::eval(lower_left_ + shape * width_);
1118

1119
  } else if (check_for_node(node, "upper_right")) {
1120
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1121

1122
    // Check to ensure width has same dimensions
1123
    auto n = upper_right_.size();
1124
    if (n != lower_left_.size()) {
370,172,148✔
1125
      fatal_error("Number of entries on <upper_right> must be the "
370,172,148✔
1126
                  "same as the number of entries on <lower_left>.");
9,887,605✔
1127
    }
1128

360,284,543✔
1129
    // Check that upper-right is above lower-left
1130
    if (xt::any(upper_right_ < lower_left_)) {
1131
      fatal_error("The <upper_right> coordinates must be greater than "
1132
                  "the <lower_left> coordinates on a tally mesh.");
1133
    }
1134

360,284,543✔
1135
    // Set width
1136
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1137
  } else {
1138
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
360,284,543✔
1139
  }
1140

1141
  // Set material volumes
1142
  volume_frac_ = 1.0 / xt::prod(shape)();
360,284,543✔
1143

41,484✔
1144
  element_volume_ = 1.0;
41,472✔
1145
  for (int i = 0; i < n_dimension_; i++) {
1146
    element_volume_ *= width_[i];
41,484✔
1147
  }
1148
}
1149

1150
int RegularMesh::get_index_in_direction(double r, int i) const
1151
{
1152
  return std::ceil((r - lower_left_[i]) / width_[i]);
360,243,059✔
1153
}
360,243,059✔
1154

1155
const std::string RegularMesh::mesh_type = "regular";
1156

720,486,118✔
1157
std::string RegularMesh::get_mesh_type() const
1,424,776,340✔
1158
{
1,064,533,281✔
1159
  return mesh_type;
1160
}
1161

1162
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
281,094,114✔
1163
{
1164
  return lower_left_[i] + ijk[i] * width_[i];
641,337,173✔
1165
}
1166

1167
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
560,117,106✔
1168
{
560,117,106✔
1169
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1170
}
1171

560,117,106✔
1172
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
560,117,106✔
1173
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1174
  double l) const
1175
{
1176
  MeshDistance d;
560,117,106✔
1177
  d.next_index = ijk[i];
560,117,106✔
1178
  if (std::abs(u[i]) < FP_PRECISION)
284,411,448✔
1179
    return d;
1180

1181
  d.max_surface = (u[i] > 0);
1182
  if (d.max_surface && (ijk[i] <= shape_[i])) {
275,705,658✔
1183
    d.next_index++;
1184
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1185
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1186
    d.next_index--;
275,705,658✔
1187
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
275,705,658✔
1188
  }
275,705,658✔
1189
  return d;
1190
}
1191

275,705,658✔
1192
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1193
  Position plot_ll, Position plot_ur) const
1194
{
1195
  // Figure out which axes lie in the plane of the plot.
275,705,658✔
1196
  array<int, 2> axes {-1, -1};
261,172,365✔
1197
  if (plot_ur.z == plot_ll.z) {
1198
    axes[0] = 0;
1199
    if (n_dimension_ > 1)
1200
      axes[1] = 1;
1201
  } else if (plot_ur.y == plot_ll.y) {
1202
    axes[0] = 0;
1203
    if (n_dimension_ > 2)
81,220,067✔
1204
      axes[1] = 2;
322,890,182✔
1205
  } else if (plot_ur.x == plot_ll.x) {
324,076,204✔
1206
    if (n_dimension_ > 1)
82,406,089✔
1207
      axes[0] = 1;
81,684,793✔
1208
    if (n_dimension_ > 2)
81,684,793✔
1209
      axes[1] = 2;
1210
  } else {
1211
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1212
  }
1213

81,220,067✔
1214
  // Get the coordinates of the mesh lines along both of the axes.
75,831,611✔
1215
  array<vector<double>, 2> axis_lines;
1216
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1217
    int axis = axes[i_ax];
1218
    if (axis == -1)
5,388,456✔
1219
      continue;
21,438,660✔
1220
    auto& lines {axis_lines[i_ax]};
16,050,204✔
1221

16,050,204✔
1222
    double coord = lower_left_[axis];
1223
    for (int i = 0; i < shape_[axis] + 1; ++i) {
1224
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1225
        lines.push_back(coord);
5,388,456✔
1226
      coord += width_[axis];
4,036,392✔
1227
    }
1228
  }
1229

1230
  return {axis_lines[0], axis_lines[1]};
1231
}
370,172,148✔
1232

1233
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1234
{
1235
  write_dataset(mesh_group, "dimension", get_x_shape());
1236
  write_dataset(mesh_group, "lower_left", lower_left_);
1237
  write_dataset(mesh_group, "upper_right", upper_right_);
1238
  write_dataset(mesh_group, "width", width_);
1239
}
370,172,148✔
1240

1241
xt::xtensor<double, 1> RegularMesh::count_sites(
370,172,148✔
1242
  const SourceSite* bank, int64_t length, bool* outside) const
370,172,148✔
1243
{
540,914,415✔
1244
  // Determine shape of array for counts
560,158,578✔
1245
  std::size_t m = this->n_bins();
1246
  vector<std::size_t> shape = {m};
560,158,578✔
1247

560,158,578✔
1248
  // Create array of zeros
560,158,578✔
1249
  xt::xarray<double> cnt {shape, 0.0};
1250
  bool outside_ = false;
1251

1252
  for (int64_t i = 0; i < length; i++) {
1253
    const auto& site = bank[i];
1254

1255
    // determine scoring bin for entropy mesh
1256
    int mesh_bin = get_bin(site.r);
370,172,148✔
1257

370,172,148✔
1258
    // if outside mesh, skip particle
1259
    if (mesh_bin < 0) {
131,753,582✔
1260
      outside_ = true;
1261
      continue;
1262
    }
1263

1264
    // Add to appropriate bin
1265
    cnt(mesh_bin) += site.wgt;
1266
  }
1267

131,753,582✔
1268
  // Create copy of count data. Since ownership will be acquired by xtensor,
131,753,582✔
1269
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
131,753,582✔
1270
  // warnings.
67,800,815✔
1271
  int total = cnt.size();
1272
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
1273

67,800,815✔
1274
#ifdef OPENMC_MPI
67,800,815✔
1275
  // collect values from all processors
33,862,038✔
1276
  MPI_Reduce(
67,800,815✔
1277
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
33,315,522✔
1278

67,800,815✔
1279
  // Check if there were sites outside the mesh for any processor
67,800,815✔
1280
  if (outside) {
164,344,808✔
1281
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1282
  }
1283
#else
1284
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1285
  if (outside)
1286
    *outside = outside_;
1287
#endif
131,753,582✔
1288

131,753,582✔
1289
  // Adapt reduced values in array back into an xarray
1290
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1291
  xt::xarray<double> counts = arr;
1292

1293
  return counts;
1294
}
1,670✔
1295

1296
double RegularMesh::volume(const MeshIndex& ijk) const
1297
{
1,670✔
1298
  return element_volume_;
×
1299
}
1300

1301
//==============================================================================
1,670✔
1302
// RectilinearMesh implementation
1,670✔
1303
//==============================================================================
1,670✔
UNCOV
1304

×
1305
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1306
{
1,670✔
1307
  n_dimension_ = 3;
1308

1309
  grid_[0] = get_node_array<double>(node, "x_grid");
1,670✔
UNCOV
1310
  grid_[1] = get_node_array<double>(node, "y_grid");
×
1311
  grid_[2] = get_node_array<double>(node, "z_grid");
1312

1313
  if (int err = set_grid()) {
1314
    fatal_error(openmc_err_msg);
1315
  }
1,670✔
1316
}
1317

1,670✔
1318
const std::string RectilinearMesh::mesh_type = "rectilinear";
UNCOV
1319

×
1320
std::string RectilinearMesh::get_mesh_type() const
1321
{
1322
  return mesh_type;
1323
}
1,670✔
1324

×
1325
double RectilinearMesh::positive_grid_boundary(
1326
  const MeshIndex& ijk, int i) const
1327
{
1328
  return grid_[i][ijk[i]];
1,670✔
1329
}
1330

51✔
UNCOV
1331
double RectilinearMesh::negative_grid_boundary(
×
1332
  const MeshIndex& ijk, int i) const
1333
{
1334
  return grid_[i][ijk[i] - 1];
51✔
1335
}
1336

1337
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
51✔
1338
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
51✔
UNCOV
1339
  double l) const
×
1340
{
1341
  MeshDistance d;
1342
  d.next_index = ijk[i];
1343
  if (std::abs(u[i]) < FP_PRECISION)
1344
    return d;
51✔
UNCOV
1345

×
1346
  d.max_surface = (u[i] > 0);
1347
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1348
    d.next_index++;
1349
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
51✔
1350
  } else if (!d.max_surface && (ijk[i] > 0)) {
1351
    d.next_index--;
1,619✔
1352
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,619✔
1353
  }
1354
  return d;
1355
}
1,619✔
1356

1,619✔
UNCOV
1357
int RectilinearMesh::set_grid()
×
1358
{
1359
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1360
    static_cast<int>(grid_[1].size()) - 1,
1361
    static_cast<int>(grid_[2].size()) - 1};
1362

1,619✔
UNCOV
1363
  for (const auto& g : grid_) {
×
1364
    if (g.size() < 2) {
1365
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1366
                 "must each have at least 2 points");
1367
      return OPENMC_E_INVALID_ARGUMENT;
1368
    }
1,619✔
1369
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
UNCOV
1370
        g.end()) {
×
1371
      set_errmsg("Values in for x-, y-, and z- grids for "
1372
                 "rectilinear meshes must be sorted and unique.");
1373
      return OPENMC_E_INVALID_ARGUMENT;
1374
    }
1,670✔
1375
  }
1376

1,670✔
1377
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
6,396✔
1378
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
4,726✔
1379

1380
  return 0;
1,670✔
1381
}
1382

1,792,011,885✔
1383
int RectilinearMesh::get_index_in_direction(double r, int i) const
1384
{
1,792,011,885✔
1385
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1386
}
1387

1388
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1389
  Position plot_ll, Position plot_ur) const
2,987✔
1390
{
1391
  // Figure out which axes lie in the plane of the plot.
2,987✔
1392
  array<int, 2> axes {-1, -1};
1393
  if (plot_ur.z == plot_ll.z) {
1394
    axes = {0, 1};
636,485,419✔
1395
  } else if (plot_ur.y == plot_ll.y) {
1396
    axes = {0, 2};
636,485,419✔
1397
  } else if (plot_ur.x == plot_ll.x) {
1398
    axes = {1, 2};
1399
  } else {
567,760,772✔
1400
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1401
  }
567,760,772✔
1402

1403
  // Get the coordinates of the mesh lines along both of the axes.
1404
  array<vector<double>, 2> axis_lines;
1,210,950,122✔
1405
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1406
    int axis = axes[i_ax];
1407
    vector<double>& lines {axis_lines[i_ax]};
1408

1,210,950,122✔
1409
    for (auto coord : grid_[axis]) {
1,210,950,122✔
1410
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,210,950,122✔
1411
        lines.push_back(coord);
1,314,072✔
1412
    }
1413
  }
1,209,636,050✔
1414

1,209,636,050✔
1415
  return {axis_lines[0], axis_lines[1]};
631,858,303✔
1416
}
631,858,303✔
1417

577,777,747✔
1418
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
563,133,656✔
1419
{
563,133,656✔
1420
  write_dataset(mesh_group, "x_grid", grid_[0]);
1421
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,209,636,050✔
1422
  write_dataset(mesh_group, "z_grid", grid_[2]);
1423
}
1424

24✔
1425
double RectilinearMesh::volume(const MeshIndex& ijk) const
1426
{
1427
  double vol {1.0};
1428

24✔
1429
  for (int i = 0; i < n_dimension_; i++) {
24✔
1430
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
24✔
1431
  }
24✔
1432
  return vol;
24✔
UNCOV
1433
}
×
UNCOV
1434

×
UNCOV
1435
//==============================================================================
×
UNCOV
1436
// CylindricalMesh implementation
×
UNCOV
1437
//==============================================================================
×
UNCOV
1438

×
UNCOV
1439
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
×
UNCOV
1440
  : PeriodicStructuredMesh {node}
×
UNCOV
1441
{
×
1442
  n_dimension_ = 3;
UNCOV
1443
  grid_[0] = get_node_array<double>(node, "r_grid");
×
1444
  grid_[1] = get_node_array<double>(node, "phi_grid");
1445
  grid_[2] = get_node_array<double>(node, "z_grid");
1446
  origin_ = get_node_position(node, "origin");
1447

24✔
1448
  if (int err = set_grid()) {
72✔
1449
    fatal_error(openmc_err_msg);
48✔
1450
  }
48✔
UNCOV
1451
}
×
1452

48✔
1453
const std::string CylindricalMesh::mesh_type = "cylindrical";
1454

48✔
1455
std::string CylindricalMesh::get_mesh_type() const
312✔
1456
{
264✔
1457
  return mesh_type;
264✔
1458
}
264✔
1459

1460
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
1461
  Position r, bool& in_mesh) const
1462
{
48✔
1463
  local_coords(r);
24✔
1464

1465
  Position mapped_r;
1,756✔
1466
  mapped_r[0] = std::hypot(r.x, r.y);
1467
  mapped_r[2] = r[2];
1,756✔
1468

1,756✔
1469
  if (mapped_r[0] < FP_PRECISION) {
1,756✔
1470
    mapped_r[1] = 0.0;
1,756✔
1471
  } else {
1,756✔
1472
    mapped_r[1] = std::atan2(r.y, r.x);
1473
    if (mapped_r[1] < 0)
8,828✔
1474
      mapped_r[1] += 2 * M_PI;
1475
  }
1476

1477
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
8,828✔
1478

8,828✔
1479
  idx[1] = sanitize_phi(idx[1]);
1480

1481
  return idx;
8,828✔
1482
}
8,828✔
1483

1484
Position CylindricalMesh::sample_element(
8,667,105✔
1485
  const MeshIndex& ijk, uint64_t* seed) const
8,658,277✔
1486
{
1487
  double r_min = this->r(ijk[0] - 1);
1488
  double r_max = this->r(ijk[0]);
8,658,277✔
1489

1490
  double phi_min = this->phi(ijk[1] - 1);
1491
  double phi_max = this->phi(ijk[1]);
8,658,277✔
UNCOV
1492

×
UNCOV
1493
  double z_min = this->z(ijk[2] - 1);
×
1494
  double z_max = this->z(ijk[2]);
1495

1496
  double r_min_sq = r_min * r_min;
1497
  double r_max_sq = r_max * r_max;
8,658,277✔
1498
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1499
  double phi = uniform_distribution(phi_min, phi_max, seed);
1500
  double z = uniform_distribution(z_min, z_max, seed);
1501

1502
  double x = r * std::cos(phi);
1503
  double y = r * std::sin(phi);
8,828✔
1504

8,828✔
1505
  return origin_ + Position(x, y, z);
1506
}
1507

1508
double CylindricalMesh::find_r_crossing(
3,900✔
1509
  const Position& r, const Direction& u, double l, int shell) const
3,900✔
1510
{
1511

1512
  if ((shell < 0) || (shell > shape_[0]))
3,900✔
1513
    return INFTY;
3,900✔
1514

1515
  // solve r.x^2 + r.y^2 == r0^2
1516
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
4,928✔
1517
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,928✔
1518

4,928✔
1519
  const double r0 = grid_[0][shell];
1520
  if (r0 == 0.0)
1521
    return INFTY;
1522

8,828✔
1523
  const double denominator = u.x * u.x + u.y * u.y;
8,828✔
1524

1525
  // Direction of flight is in z-direction. Will never intersect r.
17,656✔
1526
  if (std::abs(denominator) < FP_PRECISION)
8,828✔
1527
    return INFTY;
1528

1,058,040✔
1529
  // inverse of dominator to help the compiler to speed things up
1530
  const double inv_denominator = 1.0 / denominator;
1,058,040✔
1531

1532
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
1533
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1534
  double D = p * p - c * inv_denominator;
1535

1536
  if (D < 0.0)
1537
    return INFTY;
111✔
1538

1539
  D = std::sqrt(D);
111✔
1540

1541
  // the solution -p - D is always smaller as -p + D : Check this one first
111✔
1542
  if (std::abs(c) <= RADIAL_MESH_TOL)
111✔
1543
    return INFTY;
111✔
1544

1545
  if (-p - D > l)
111✔
UNCOV
1546
    return -p - D;
×
1547
  if (-p + D > l)
1548
    return -p + D;
111✔
1549

1550
  return INFTY;
1551
}
1552

280✔
1553
double CylindricalMesh::find_phi_crossing(
1554
  const Position& r, const Direction& u, double l, int shell) const
280✔
1555
{
1556
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1557
  if (full_phi_ && (shape_[1] == 1))
30,886,582✔
1558
    return INFTY;
1559

1560
  shell = sanitize_phi(shell);
30,886,582✔
1561

1562
  const double p0 = grid_[1][shell];
1563

30,071,051✔
1564
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1565
  // => x(s) * cos(p0) = y(s) * sin(p0)
1566
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
30,071,051✔
1567
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1568

1569
  const double c0 = std::cos(p0);
62,453,268✔
1570
  const double s0 = std::sin(p0);
1571

1572
  const double denominator = (u.x * s0 - u.y * c0);
1573

62,453,268✔
1574
  // Check if direction of flight is not parallel to phi surface
62,453,268✔
1575
  if (std::abs(denominator) > FP_PRECISION) {
62,453,268✔
1576
    const double s = -(r.x * s0 - r.y * c0) / denominator;
623,808✔
1577
    // Check if solution is in positive direction of flight and crosses the
1578
    // correct phi surface (not -phi)
61,829,460✔
1579
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
61,829,460✔
1580
      return s;
30,886,582✔
1581
  }
30,886,582✔
1582

30,942,878✔
1583
  return INFTY;
30,071,051✔
1584
}
30,071,051✔
1585

1586
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
61,829,460✔
1587
  const Position& r, const Direction& u, double l, int shell) const
1588
{
1589
  MeshDistance d;
161✔
1590
  d.next_index = shell;
1591

161✔
1592
  // Direction of flight is within xy-plane. Will never intersect z.
161✔
1593
  if (std::abs(u.z) < FP_PRECISION)
161✔
1594
    return d;
1595

644✔
1596
  d.max_surface = (u.z > 0.0);
483✔
UNCOV
1597
  if (d.max_surface && (shell <= shape_[2])) {
×
1598
    d.next_index += 1;
UNCOV
1599
    d.distance = (grid_[2][shell] - r.z) / u.z;
×
1600
  } else if (!d.max_surface && (shell > 0)) {
1601
    d.next_index -= 1;
483✔
1602
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
966✔
UNCOV
1603
  }
×
1604
  return d;
UNCOV
1605
}
×
1606

1607
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1608
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1609
  double l) const
161✔
1610
{
161✔
1611
  Position r = r0 - origin_;
1612

161✔
1613
  if (i == 0) {
1614

1615
    return std::min(
86,818,218✔
1616
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1617
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
86,818,218✔
1618

1619
  } else if (i == 1) {
1620

12✔
1621
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1622
                      find_phi_crossing(r, u, l, ijk[i])),
1623
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1624
        find_phi_crossing(r, u, l, ijk[i] - 1)));
12✔
1625

12✔
UNCOV
1626
  } else {
×
1627
    return find_z_crossing(r, u, l, ijk[i]);
12✔
1628
  }
12✔
UNCOV
1629
}
×
UNCOV
1630

×
1631
int CylindricalMesh::set_grid()
UNCOV
1632
{
×
1633
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1634
    static_cast<int>(grid_[1].size()) - 1,
1635
    static_cast<int>(grid_[2].size()) - 1};
1636

12✔
1637
  for (const auto& g : grid_) {
36✔
1638
    if (g.size() < 2) {
24✔
1639
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
24✔
1640
                 "must each have at least 2 points");
1641
      return OPENMC_E_INVALID_ARGUMENT;
120✔
1642
    }
96✔
1643
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
96✔
1644
        g.end()) {
1645
      set_errmsg("Values in for r-, phi-, and z- grids for "
1646
                 "cylindrical meshes must be sorted and unique.");
1647
      return OPENMC_E_INVALID_ARGUMENT;
24✔
1648
    }
12✔
1649
  }
1650
  if (grid_[0].front() < 0.0) {
98✔
1651
    set_errmsg("r-grid for "
1652
               "cylindrical meshes must start at r >= 0.");
98✔
1653
    return OPENMC_E_INVALID_ARGUMENT;
98✔
1654
  }
98✔
1655
  if (grid_[1].front() < 0.0) {
98✔
1656
    set_errmsg("phi-grid for "
1657
               "cylindrical meshes must start at phi >= 0.");
144✔
1658
    return OPENMC_E_INVALID_ARGUMENT;
1659
  }
144✔
1660
  if (grid_[1].back() > 2.0 * PI) {
1661
    set_errmsg("phi-grids for "
576✔
1662
               "cylindrical meshes must end with theta <= 2*pi.");
432✔
1663

1664
    return OPENMC_E_INVALID_ARGUMENT;
144✔
1665
  }
1666

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

1669
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
1670
    origin_[2] + grid_[2].front()};
1671
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
413✔
1672
    origin_[2] + grid_[2].back()};
413✔
1673

1674
  return 0;
413✔
1675
}
413✔
1676

413✔
1677
int CylindricalMesh::get_index_in_direction(double r, int i) const
413✔
1678
{
413✔
1679
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1680
}
413✔
UNCOV
1681

×
1682
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1683
  Position plot_ll, Position plot_ur) const
413✔
1684
{
1685
  fatal_error("Plot of cylindrical Mesh not implemented");
1686

1687
  // Figure out which axes lie in the plane of the plot.
516✔
1688
  array<vector<double>, 2> axis_lines;
1689
  return {axis_lines[0], axis_lines[1]};
516✔
1690
}
1691

1692
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
50,568,432✔
1693
{
1694
  write_dataset(mesh_group, "r_grid", grid_[0]);
1695
  write_dataset(mesh_group, "phi_grid", grid_[1]);
50,568,432✔
1696
  write_dataset(mesh_group, "z_grid", grid_[2]);
1697
  write_dataset(mesh_group, "origin", origin_);
50,568,432✔
1698
}
50,568,432✔
1699

50,568,432✔
1700
double CylindricalMesh::volume(const MeshIndex& ijk) const
1701
{
50,568,432✔
UNCOV
1702
  double r_i = grid_[0][ijk[0] - 1];
×
1703
  double r_o = grid_[0][ijk[0]];
1704

50,568,432✔
1705
  double phi_i = grid_[1][ijk[1] - 1];
50,568,432✔
1706
  double phi_o = grid_[1][ijk[1]];
25,300,356✔
1707

1708
  double z_i = grid_[2][ijk[2] - 1];
1709
  double z_o = grid_[2][ijk[2]];
50,568,432✔
1710

1711
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
50,568,432✔
1712
}
1713

50,568,432✔
1714
//==============================================================================
1715
// SphericalMesh implementation
1716
//==============================================================================
96,000✔
1717

1718
SphericalMesh::SphericalMesh(pugi::xml_node node)
1719
  : PeriodicStructuredMesh {node}
96,000✔
1720
{
96,000✔
1721
  n_dimension_ = 3;
1722

96,000✔
1723
  grid_[0] = get_node_array<double>(node, "r_grid");
96,000✔
1724
  grid_[1] = get_node_array<double>(node, "theta_grid");
1725
  grid_[2] = get_node_array<double>(node, "phi_grid");
96,000✔
1726
  origin_ = get_node_position(node, "origin");
96,000✔
1727

1728
  if (int err = set_grid()) {
96,000✔
1729
    fatal_error(openmc_err_msg);
96,000✔
1730
  }
96,000✔
1731
}
96,000✔
1732

96,000✔
1733
const std::string SphericalMesh::mesh_type = "spherical";
1734

96,000✔
1735
std::string SphericalMesh::get_mesh_type() const
96,000✔
1736
{
1737
  return mesh_type;
96,000✔
1738
}
1739

1740
StructuredMesh::MeshIndex SphericalMesh::get_indices(
148,757,592✔
1741
  Position r, bool& in_mesh) const
1742
{
1743
  local_coords(r);
1744

148,757,592✔
1745
  Position mapped_r;
18,260,184✔
1746
  mapped_r[0] = r.norm();
1747

1748
  if (mapped_r[0] < FP_PRECISION) {
1749
    mapped_r[1] = 0.0;
1750
    mapped_r[2] = 0.0;
1751
  } else {
130,497,408✔
1752
    mapped_r[1] = std::acos(r.z / mapped_r.x);
130,497,408✔
1753
    mapped_r[2] = std::atan2(r.y, r.x);
7,273,620✔
1754
    if (mapped_r[2] < 0)
1755
      mapped_r[2] += 2 * M_PI;
123,223,788✔
1756
  }
1757

1758
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
123,223,788✔
1759

64,320✔
1760
  idx[1] = sanitize_theta(idx[1]);
1761
  idx[2] = sanitize_phi(idx[2]);
1762

123,159,468✔
1763
  return idx;
1764
}
123,159,468✔
1765

123,159,468✔
1766
Position SphericalMesh::sample_element(
123,159,468✔
1767
  const MeshIndex& ijk, uint64_t* seed) const
1768
{
123,159,468✔
1769
  double r_min = this->r(ijk[0] - 1);
17,009,892✔
1770
  double r_max = this->r(ijk[0]);
1771

106,149,576✔
1772
  double theta_min = this->theta(ijk[1] - 1);
1773
  double theta_max = this->theta(ijk[1]);
1774

106,149,576✔
1775
  double phi_min = this->phi(ijk[2] - 1);
7,057,536✔
1776
  double phi_max = this->phi(ijk[2]);
1777

99,092,040✔
1778
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
20,246,952✔
1779
  double sin_theta = std::sin(std::acos(cos_theta));
78,845,088✔
1780
  double phi = uniform_distribution(phi_min, phi_max, seed);
47,862,120✔
1781
  double r_min_cub = std::pow(r_min, 3);
1782
  double r_max_cub = std::pow(r_max, 3);
30,982,968✔
1783
  // might be faster to do rejection here?
1784
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
1785

74,283,288✔
1786
  double x = r * std::cos(phi) * sin_theta;
1787
  double y = r * std::sin(phi) * sin_theta;
1788
  double z = r * cos_theta;
1789

74,283,288✔
1790
  return origin_ + Position(x, y, z);
32,466,432✔
1791
}
1792

41,816,856✔
1793
double SphericalMesh::find_r_crossing(
1794
  const Position& r, const Direction& u, double l, int shell) const
41,816,856✔
1795
{
1796
  if ((shell < 0) || (shell > shape_[0]))
1797
    return INFTY;
1798

1799
  // solve |r+s*u| = r0
1800
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
1801
  const double r0 = grid_[0][shell];
41,816,856✔
1802
  if (r0 == 0.0)
41,816,856✔
1803
    return INFTY;
1804
  const double p = r.dot(u);
41,816,856✔
1805
  double c = r.dot(r) - r0 * r0;
1806
  double D = p * p - c;
1807

41,816,856✔
1808
  if (std::abs(c) <= RADIAL_MESH_TOL)
41,532,408✔
1809
    return INFTY;
1810

1811
  if (D >= 0.0) {
41,532,408✔
1812
    D = std::sqrt(D);
19,437,552✔
1813
    // the solution -p - D is always smaller as -p + D : Check this one first
1814
    if (-p - D > l)
1815
      return -p - D;
22,379,304✔
1816
    if (-p + D > l)
1817
      return -p + D;
1818
  }
39,992,448✔
1819

1820
  return INFTY;
1821
}
39,992,448✔
1822

39,992,448✔
1823
double SphericalMesh::find_theta_crossing(
1824
  const Position& r, const Direction& u, double l, int shell) const
1825
{
39,992,448✔
1826
  // Theta grid is [0, π], thus there is no real surface to cross
1,219,872✔
1827
  if (full_theta_ && (shape_[1] == 1))
1828
    return INFTY;
38,772,576✔
1829

38,772,576✔
1830
  shell = sanitize_theta(shell);
17,769,456✔
1831

17,769,456✔
1832
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
21,003,120✔
1833
  // yields
18,037,548✔
1834
  // a*s^2 + 2*b*s + c == 0 with
18,037,548✔
1835
  // a = cos(theta)^2 - u.z * u.z
1836
  // b = r*u * cos(theta)^2 - u.z * r.z
38,772,576✔
1837
  // c = r*r * cos(theta)^2 - r.z^2
1838

1839
  const double cos_t = std::cos(grid_[1][shell]);
151,512,888✔
1840
  const bool sgn = std::signbit(cos_t);
1841
  const double cos_t_2 = cos_t * cos_t;
1842

1843
  const double a = cos_t_2 - u.z * u.z;
151,512,888✔
1844
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1845
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
151,512,888✔
1846

1847
  // if factor of s^2 is zero, direction of flight is parallel to theta
74,378,796✔
1848
  // surface
74,378,796✔
1849
  if (std::abs(a) < FP_PRECISION) {
148,757,592✔
1850
    // if b vanishes, direction of flight is within theta surface and crossing
1851
    // is not possible
77,134,092✔
1852
    if (std::abs(b) < FP_PRECISION)
1853
      return INFTY;
37,141,644✔
1854

37,141,644✔
1855
    const double s = -0.5 * c / b;
37,141,644✔
1856
    // Check if solution is in positive direction of flight and has correct
74,283,288✔
1857
    // sign
1858
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1859
      return s;
39,992,448✔
1860

1861
    // no crossing is possible
1862
    return INFTY;
1863
  }
437✔
1864

1865
  const double p = b / a;
437✔
1866
  double D = p * p - c / a;
437✔
1867

437✔
1868
  if (D < 0.0)
1869
    return INFTY;
1,748✔
1870

1,311✔
UNCOV
1871
  D = std::sqrt(D);
×
1872

UNCOV
1873
  // the solution -p-D is always smaller as -p+D : Check this one first
×
1874
  double s = -p - D;
1875
  // Check if solution is in positive direction of flight and has correct sign
1,311✔
1876
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
2,622✔
UNCOV
1877
    return s;
×
1878

UNCOV
1879
  s = -p + D;
×
1880
  // Check if solution is in positive direction of flight and has correct sign
1881
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1882
    return s;
437✔
UNCOV
1883

×
1884
  return INFTY;
1885
}
×
1886

1887
double SphericalMesh::find_phi_crossing(
437✔
UNCOV
1888
  const Position& r, const Direction& u, double l, int shell) const
×
1889
{
UNCOV
1890
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
×
1891
  if (full_phi_ && (shape_[2] == 1))
1892
    return INFTY;
437✔
1893

×
1894
  shell = sanitize_phi(shell);
1895

1896
  const double p0 = grid_[2][shell];
×
1897

1898
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1899
  // => x(s) * cos(p0) = y(s) * sin(p0)
437✔
1900
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1901
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
874✔
1902

874✔
1903
  const double c0 = std::cos(p0);
874✔
1904
  const double s0 = std::sin(p0);
874✔
1905

1906
  const double denominator = (u.x * s0 - u.y * c0);
437✔
1907

1908
  // Check if direction of flight is not parallel to phi surface
1909
  if (std::abs(denominator) > FP_PRECISION) {
151,705,296✔
1910
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1911
    // Check if solution is in positive direction of flight and crosses the
151,705,296✔
1912
    // correct phi surface (not -phi)
1913
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
UNCOV
1914
      return s;
×
1915
  }
1916

UNCOV
1917
  return INFTY;
×
1918
}
1919

1920
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1921
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1922
  double l) const
1923
{
1924

396✔
1925
  if (i == 0) {
1926
    return std::min(
396✔
1927
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
396✔
1928
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
396✔
1929

396✔
1930
  } else if (i == 1) {
396✔
1931
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
1932
                      find_theta_crossing(r0, u, l, ijk[i])),
384✔
1933
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
1934
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
384✔
1935

384✔
1936
  } else {
1937
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
384✔
1938
                      find_phi_crossing(r0, u, l, ijk[i])),
384✔
1939
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1940
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
384✔
1941
  }
384✔
1942
}
1943

384✔
1944
int SphericalMesh::set_grid()
1945
{
1946
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1947
    static_cast<int>(grid_[1].size()) - 1,
1948
    static_cast<int>(grid_[2].size()) - 1};
1949

1950
  for (const auto& g : grid_) {
317✔
1951
    if (g.size() < 2) {
317✔
1952
      set_errmsg("x-, y-, and z- grids for spherical meshes "
1953
                 "must each have at least 2 points");
317✔
1954
      return OPENMC_E_INVALID_ARGUMENT;
1955
    }
317✔
1956
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
317✔
1957
        g.end()) {
317✔
1958
      set_errmsg("Values in for r-, theta-, and phi- grids for "
317✔
1959
                 "spherical meshes must be sorted and unique.");
1960
      return OPENMC_E_INVALID_ARGUMENT;
317✔
UNCOV
1961
    }
×
1962
    if (g.front() < 0.0) {
1963
      set_errmsg("r-, theta-, and phi- grids for "
317✔
1964
                 "spherical meshes must start at v >= 0.");
1965
      return OPENMC_E_INVALID_ARGUMENT;
1966
    }
1967
  }
372✔
1968
  if (grid_[1].back() > PI) {
1969
    set_errmsg("theta-grids for "
372✔
1970
               "spherical meshes must end with theta <= pi.");
1971

1972
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1973
  }
1974
  if (grid_[2].back() > 2 * PI) {
1975
    set_errmsg("phi-grids for "
73,752,732✔
1976
               "spherical meshes must end with phi <= 2*pi.");
1977
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1978
  }
73,752,732✔
1979

1980
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
73,752,732✔
UNCOV
1981
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
×
1982

×
1983
  double r = grid_[0].back();
1984
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
73,752,732✔
1985
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
73,752,732✔
1986

73,752,732✔
1987
  return 0;
36,882,336✔
1988
}
1989

1990
int SphericalMesh::get_index_in_direction(double r, int i) const
73,752,732✔
1991
{
1992
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
73,752,732✔
1993
}
73,752,732✔
1994

1995
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
73,752,732✔
1996
  Position plot_ll, Position plot_ur) const
1997
{
1998
  fatal_error("Plot of spherical Mesh not implemented");
×
1999

2000
  // Figure out which axes lie in the plane of the plot.
UNCOV
2001
  array<vector<double>, 2> axis_lines;
×
UNCOV
2002
  return {axis_lines[0], axis_lines[1]};
×
2003
}
UNCOV
2004

×
UNCOV
2005
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
×
2006
{
UNCOV
2007
  write_dataset(mesh_group, "r_grid", grid_[0]);
×
UNCOV
2008
  write_dataset(mesh_group, "theta_grid", grid_[1]);
×
2009
  write_dataset(mesh_group, "phi_grid", grid_[2]);
2010
  write_dataset(mesh_group, "origin", origin_);
×
2011
}
×
UNCOV
2012

×
UNCOV
2013
double SphericalMesh::volume(const MeshIndex& ijk) const
×
UNCOV
2014
{
×
2015
  double r_i = grid_[0][ijk[0] - 1];
UNCOV
2016
  double r_o = grid_[0][ijk[0]];
×
2017

2018
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
2019
  double theta_o = grid_[1][ijk[1]];
×
UNCOV
2020

×
2021
  double phi_i = grid_[2][ijk[2] - 1];
2022
  double phi_o = grid_[2][ijk[2]];
×
2023

2024
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
2025
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
481,615,104✔
2026
}
2027

2028
//==============================================================================
481,615,104✔
2029
// Helper functions for the C API
44,030,820✔
2030
//==============================================================================
2031

2032
int check_mesh(int32_t index)
2033
{
437,584,284✔
2034
  if (index < 0 || index >= model::meshes.size()) {
437,584,284✔
2035
    set_errmsg("Index in meshes array is out of bounds.");
7,601,184✔
2036
    return OPENMC_E_OUT_OF_BOUNDS;
429,983,100✔
2037
  }
429,983,100✔
2038
  return 0;
429,983,100✔
2039
}
2040

429,983,100✔
2041
template<class T>
11,548,560✔
2042
int check_mesh_type(int32_t index)
2043
{
418,434,540✔
2044
  if (int err = check_mesh(index))
388,836,240✔
2045
    return err;
2046

388,836,240✔
2047
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
69,512,340✔
2048
  if (!mesh) {
319,323,900✔
2049
    set_errmsg("This function is not valid for input mesh.");
192,371,628✔
2050
    return OPENMC_E_INVALID_TYPE;
2051
  }
2052
  return 0;
156,550,572✔
2053
}
2054

2055
template<class T>
118,217,520✔
2056
bool is_mesh_type(int32_t index)
2057
{
2058
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2059
  return mesh;
118,217,520✔
2060
}
76,498,272✔
2061

2062
//==============================================================================
41,719,248✔
2063
// C API functions
2064
//==============================================================================
2065

2066
// Return the type of mesh as a C string
2067
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2068
{
2069
  if (int err = check_mesh(index))
2070
    return err;
2071

41,719,248✔
2072
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
41,719,248✔
2073

41,719,248✔
2074
  return 0;
2075
}
41,719,248✔
2076

41,719,248✔
2077
//! Extend the meshes array by n elements
41,719,248✔
2078
extern "C" int openmc_extend_meshes(
2079
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2080
{
2081
  if (index_start)
41,719,248✔
2082
    *index_start = model::meshes.size();
2083
  std::string mesh_type;
2084

526,416✔
2085
  for (int i = 0; i < n; ++i) {
526,416✔
2086
    if (RegularMesh::mesh_type == type) {
UNCOV
2087
      model::meshes.push_back(make_unique<RegularMesh>());
×
2088
    } else if (RectilinearMesh::mesh_type == type) {
2089
      model::meshes.push_back(make_unique<RectilinearMesh>());
2090
    } else if (CylindricalMesh::mesh_type == type) {
×
UNCOV
2091
      model::meshes.push_back(make_unique<CylindricalMesh>());
×
2092
    } else if (SphericalMesh::mesh_type == type) {
2093
      model::meshes.push_back(make_unique<SphericalMesh>());
2094
    } else {
×
2095
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
2096
    }
2097
  }
41,192,832✔
2098
  if (index_end)
41,192,832✔
2099
    *index_end = model::meshes.size() - 1;
2100

41,192,832✔
2101
  return 0;
11,955,456✔
2102
}
2103

29,237,376✔
2104
//! Adds a new unstructured mesh to OpenMC
2105
extern "C" int openmc_add_unstructured_mesh(
2106
  const char filename[], const char library[], int* id)
29,237,376✔
2107
{
2108
  std::string lib_name(library);
29,237,376✔
2109
  std::string mesh_file(filename);
5,599,656✔
2110
  bool valid_lib = false;
2111

23,637,720✔
2112
#ifdef DAGMC
2113
  if (lib_name == MOABMesh::mesh_lib_type) {
23,637,720✔
2114
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
11,064,504✔
2115
    valid_lib = true;
2116
  }
12,573,216✔
2117
#endif
2118

2119
#ifdef LIBMESH
119,966,232✔
2120
  if (lib_name == LibMesh::mesh_lib_type) {
2121
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2122
    valid_lib = true;
2123
  }
119,966,232✔
2124
#endif
76,498,272✔
2125

2126
  if (!valid_lib) {
43,467,960✔
2127
    set_errmsg(fmt::format("Mesh library {} is not supported "
2128
                           "by this build of OpenMC",
43,467,960✔
2129
      lib_name));
2130
    return OPENMC_E_INVALID_ARGUMENT;
2131
  }
2132

2133
  // auto-assign new ID
2134
  model::meshes.back()->set_id(-1);
2135
  *id = model::meshes.back()->id_;
43,467,960✔
2136

43,467,960✔
2137
  return 0;
2138
}
43,467,960✔
2139

2140
//! Return the index in the meshes array of a mesh with a given ID
2141
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
43,467,960✔
2142
{
43,212,696✔
2143
  auto pair = model::mesh_map.find(id);
2144
  if (pair == model::mesh_map.end()) {
2145
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
43,212,696✔
2146
    return OPENMC_E_INVALID_ID;
19,003,152✔
2147
  }
2148
  *index = pair->second;
2149
  return 0;
24,464,808✔
2150
}
2151

2152
//! Return the ID of a mesh
359,899,428✔
2153
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2154
{
2155
  if (int err = check_mesh(index))
2156
    return err;
2157
  *id = model::meshes[index]->id_;
359,899,428✔
2158
  return 0;
240,807,552✔
2159
}
240,807,552✔
2160

481,615,104✔
2161
//! Set the ID of a mesh
2162
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
119,091,876✔
2163
{
59,108,760✔
2164
  if (int err = check_mesh(index))
59,108,760✔
2165
    return err;
59,108,760✔
2166
  model::meshes[index]->id_ = id;
118,217,520✔
2167
  model::mesh_map[id] = index;
2168
  return 0;
2169
}
59,983,116✔
2170

59,983,116✔
2171
//! Get the number of elements in a mesh
59,983,116✔
2172
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
119,966,232✔
2173
{
2174
  if (int err = check_mesh(index))
2175
    return err;
2176
  *n = model::meshes[index]->n_bins();
341✔
2177
  return 0;
2178
}
341✔
2179

341✔
2180
//! Get the volume of each element in the mesh
341✔
2181
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
2182
{
1,364✔
2183
  if (int err = check_mesh(index))
1,023✔
UNCOV
2184
    return err;
×
2185
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
2186
    volumes[i] = model::meshes[index]->volume(i);
×
2187
  }
2188
  return 0;
1,023✔
2189
}
2,046✔
UNCOV
2190

×
2191
//! Get the bounding box of a mesh
UNCOV
2192
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
×
2193
{
2194
  if (int err = check_mesh(index))
1,023✔
UNCOV
2195
    return err;
×
2196

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

2199
  // set lower left corner values
2200
  ll[0] = bbox.xmin;
341✔
UNCOV
2201
  ll[1] = bbox.ymin;
×
2202
  ll[2] = bbox.zmin;
2203

UNCOV
2204
  // set upper right corner values
×
2205
  ur[0] = bbox.xmax;
2206
  ur[1] = bbox.ymax;
341✔
UNCOV
2207
  ur[2] = bbox.zmax;
×
2208
  return 0;
UNCOV
2209
}
×
2210

2211
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
2212
  int nz, int table_size, int32_t* materials, double* volumes)
341✔
2213
{
341✔
2214
  if (int err = check_mesh(index))
2215
    return err;
341✔
2216

341✔
2217
  try {
341✔
2218
    model::meshes[index]->material_volumes(
2219
      nx, ny, nz, table_size, materials, volumes);
341✔
2220
  } catch (const std::exception& e) {
2221
    set_errmsg(e.what());
2222
    if (starts_with(e.what(), "Mesh")) {
221,258,196✔
2223
      return OPENMC_E_GEOMETRY;
2224
    } else {
221,258,196✔
2225
      return OPENMC_E_ALLOCATE;
2226
    }
2227
  }
×
2228

2229
  return 0;
UNCOV
2230
}
×
2231

2232
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
2233
  Position width, int basis, int* pixels, int32_t* data)
2234
{
2235
  if (int err = check_mesh(index))
2236
    return err;
2237
  const auto& mesh = model::meshes[index].get();
312✔
2238

2239
  int pixel_width = pixels[0];
312✔
2240
  int pixel_height = pixels[1];
312✔
2241

312✔
2242
  // get pixel size
312✔
2243
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
312✔
2244
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
2245

528✔
2246
  // setup basis indices and initial position centered on pixel
2247
  int in_i, out_i;
528✔
2248
  Position xyz = origin;
528✔
2249
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2250
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
528✔
2251
  switch (basis_enum) {
528✔
2252
  case PlotBasis::xy:
2253
    in_i = 0;
528✔
2254
    out_i = 1;
528✔
2255
    break;
2256
  case PlotBasis::xz:
528✔
2257
    in_i = 0;
528✔
2258
    out_i = 2;
2259
    break;
2260
  case PlotBasis::yz:
2261
    in_i = 1;
2262
    out_i = 2;
2263
    break;
2264
  default:
6,928✔
2265
    UNREACHABLE();
2266
  }
6,928✔
UNCOV
2267

×
2268
  // set initial position
×
2269
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2270
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
6,928✔
2271

2272
#pragma omp parallel
2273
  {
2274
    Position r = xyz;
1,243✔
2275

2276
#pragma omp for
1,243✔
UNCOV
2277
    for (int y = 0; y < pixel_height; y++) {
×
2278
      r[out_i] = xyz[out_i] - out_pixel * y;
2279
      for (int x = 0; x < pixel_width; x++) {
1,243✔
2280
        r[in_i] = xyz[in_i] + in_pixel * x;
1,243✔
UNCOV
2281
        data[pixel_width * y + x] = mesh->get_bin(r);
×
2282
      }
×
2283
    }
2284
  }
1,243✔
2285

2286
  return 0;
156✔
2287
}
2288

156✔
UNCOV
2289
//! Get the dimension of a regular mesh
×
2290
extern "C" int openmc_regular_mesh_get_dimension(
2291
  int32_t index, int** dims, int* n)
156✔
2292
{
156✔
UNCOV
2293
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2294
    return err;
×
2295
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2296
  *dims = mesh->shape_.data();
156✔
2297
  *n = mesh->n_dimension_;
2298
  return 0;
156✔
2299
}
2300

156✔
UNCOV
2301
//! Set the dimension of a regular mesh
×
2302
extern "C" int openmc_regular_mesh_set_dimension(
2303
  int32_t index, int n, const int* dims)
156✔
2304
{
156✔
UNCOV
2305
  if (int err = check_mesh_type<RegularMesh>(index))
×
2306
    return err;
×
2307
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2308

156✔
2309
  // Copy dimension
2310
  mesh->n_dimension_ = n;
208✔
2311
  std::copy(dims, dims + n, mesh->shape_.begin());
2312
  return 0;
208✔
UNCOV
2313
}
×
2314

2315
//! Get the regular mesh parameters
208✔
2316
extern "C" int openmc_regular_mesh_get_params(
208✔
UNCOV
2317
  int32_t index, double** ll, double** ur, double** width, int* n)
×
UNCOV
2318
{
×
2319
  if (int err = check_mesh_type<RegularMesh>(index))
2320
    return err;
208✔
2321
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2322

723✔
2323
  if (m->lower_left_.dimension() == 0) {
2324
    set_errmsg("Mesh parameters have not been set.");
723✔
2325
    return OPENMC_E_ALLOCATE;
×
2326
  }
2327

723✔
2328
  *ll = m->lower_left_.data();
723✔
UNCOV
2329
  *ur = m->upper_right_.data();
×
UNCOV
2330
  *width = m->width_.data();
×
2331
  *n = m->n_dimension_;
2332
  return 0;
723✔
2333
}
2334

2335
//! Set the regular mesh parameters
2336
extern "C" int openmc_regular_mesh_set_params(
2337
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2338
{
2339
  if (int err = check_mesh_type<RegularMesh>(index))
2340
    return err;
2341
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2342

2343
  if (m->n_dimension_ == -1) {
2344
    set_errmsg("Need to set mesh dimension before setting parameters.");
2345
    return OPENMC_E_UNASSIGNED;
2346
  }
2347

1,593✔
2348
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2349
  if (ll && ur) {
1,593✔
UNCOV
2350
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
×
2351
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2352
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
1,593✔
2353
  } else if (ll && width) {
2354
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
1,593✔
2355
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2356
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2357
  } else if (ur && width) {
2358
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
291✔
2359
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2360
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
2361
  } else {
291✔
2362
    set_errmsg("At least two parameters must be specified.");
291✔
2363
    return OPENMC_E_INVALID_ARGUMENT;
291✔
2364
  }
2365

582✔
2366
  // Set material volumes
291✔
2367

193✔
2368
  // TODO: incorporate this into method in RegularMesh that can be called from
98✔
2369
  // here and from constructor
50✔
2370
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
48✔
2371
  m->element_volume_ = 1.0;
24✔
2372
  for (int i = 0; i < m->n_dimension_; i++) {
24✔
2373
    m->element_volume_ *= m->width_[i];
24✔
2374
  }
UNCOV
2375

×
2376
  return 0;
2377
}
2378

291✔
UNCOV
2379
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
×
2380
template<class C>
2381
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
291✔
2382
  const int nx, const double* grid_y, const int ny, const double* grid_z,
291✔
2383
  const int nz)
2384
{
UNCOV
2385
  if (int err = check_mesh_type<C>(index))
×
2386
    return err;
2387

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

×
UNCOV
2390
  m->n_dimension_ = 3;
×
2391

2392
  m->grid_[0].reserve(nx);
2393
  m->grid_[1].reserve(ny);
2394
  m->grid_[2].reserve(nz);
2395

2396
  for (int i = 0; i < nx; i++) {
2397
    m->grid_[0].push_back(grid_x[i]);
2398
  }
2399
  for (int i = 0; i < ny; i++) {
2400
    m->grid_[1].push_back(grid_y[i]);
2401
  }
2402
  for (int i = 0; i < nz; i++) {
2403
    m->grid_[2].push_back(grid_z[i]);
2404
  }
2405

UNCOV
2406
  int err = m->set_grid();
×
UNCOV
2407
  return err;
×
2408
}
2409

UNCOV
2410
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
×
2411
template<class C>
2412
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
2413
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
UNCOV
2414
{
×
UNCOV
2415
  if (int err = check_mesh_type<C>(index))
×
2416
    return err;
UNCOV
2417
  C* m = dynamic_cast<C*>(model::meshes[index].get());
×
2418

2419
  if (m->lower_left_.dimension() == 0) {
2420
    set_errmsg("Mesh parameters have not been set.");
2421
    return OPENMC_E_ALLOCATE;
483✔
2422
  }
2423

483✔
2424
  *grid_x = m->grid_[0].data();
483✔
UNCOV
2425
  *nx = m->grid_[0].size();
×
UNCOV
2426
  *grid_y = m->grid_[1].data();
×
2427
  *ny = m->grid_[1].size();
2428
  *grid_z = m->grid_[2].data();
483✔
2429
  *nz = m->grid_[2].size();
483✔
2430

2431
  return 0;
2432
}
2433

3,105✔
2434
//! Get the rectilinear mesh grid
2435
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
3,105✔
UNCOV
2436
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2437
{
3,105✔
2438
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
3,105✔
2439
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2440
}
2441

2442
//! Set the rectilienar mesh parameters
291✔
2443
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
2444
  const double* grid_x, const int nx, const double* grid_y, const int ny,
291✔
UNCOV
2445
  const double* grid_z, const int nz)
×
2446
{
291✔
2447
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
291✔
2448
    index, grid_x, nx, grid_y, ny, grid_z, nz);
291✔
2449
}
2450

2451
//! Get the cylindrical mesh grid
2452
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
252✔
2453
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2454
{
252✔
UNCOV
2455
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
×
2456
    index, grid_x, nx, grid_y, ny, grid_z, nz);
252✔
2457
}
252✔
2458

2459
//! Set the cylindrical mesh parameters
2460
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
2461
  const double* grid_x, const int nx, const double* grid_y, const int ny,
96✔
2462
  const double* grid_z, const int nz)
2463
{
96✔
UNCOV
2464
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
×
2465
    index, grid_x, nx, grid_y, ny, grid_z, nz);
1,056✔
2466
}
960✔
2467

2468
//! Get the spherical mesh grid
96✔
2469
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
2470
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2471
{
2472

144✔
2473
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2474
    index, grid_x, nx, grid_y, ny, grid_z, nz);
144✔
UNCOV
2475
  ;
×
2476
}
2477

144✔
2478
//! Set the spherical mesh parameters
2479
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
2480
  const double* grid_x, const int nx, const double* grid_y, const int ny,
144✔
2481
  const double* grid_z, const int nz)
144✔
2482
{
144✔
2483
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
2484
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2485
}
144✔
2486

144✔
2487
#ifdef DAGMC
144✔
2488

144✔
2489
const std::string MOABMesh::mesh_lib_type = "moab";
2490

2491
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
156✔
2492
{
2493
  initialize();
2494
}
156✔
UNCOV
2495

×
2496
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2497
{
2498
  filename_ = filename;
156✔
2499
  set_length_multiplier(length_multiplier);
2500
  initialize();
12✔
2501
}
12✔
2502

12✔
2503
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
12✔
2504
{
UNCOV
2505
  mbi_ = external_mbi;
×
2506
  filename_ = "unknown (external file)";
2507
  this->initialize();
12✔
2508
}
2509

144✔
2510
void MOABMesh::initialize()
2511
{
2512

48✔
2513
  // Create the MOAB interface and load data from file
2514
  this->create_interface();
2515

48✔
UNCOV
2516
  // Initialise MOAB error code
×
2517
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2518

2519
  // Set the dimension
48✔
2520
  n_dimension_ = 3;
48✔
2521

2522
  // set member range of tetrahedral entities
2523
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
48✔
2524
  if (rval != moab::MB_SUCCESS) {
48✔
2525
    fatal_error("Failed to get all tetrahedral elements");
2526
  }
2527

2528
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2529
    warning("Non-tetrahedral elements found in unstructured "
2530
            "mesh file: " +
48✔
2531
            filename_);
48✔
2532
  }
48✔
2533

48✔
2534
  // set member range of vertices
48✔
2535
  int vertex_dim = 0;
48✔
UNCOV
2536
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
UNCOV
2537
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2538
    fatal_error("Failed to get all vertex handles");
×
UNCOV
2539
  }
×
UNCOV
2540

×
UNCOV
2541
  // make an entity set for all tetrahedra
×
UNCOV
2542
  // this is used for convenience later in output
×
UNCOV
2543
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
×
UNCOV
2544
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2545
    fatal_error("Failed to create an entity set for the tetrahedral elements");
×
2546
  }
2547

2548
  rval = mbi_->add_entities(tetset_, ehs_);
2549
  if (rval != moab::MB_SUCCESS) {
48✔
2550
    fatal_error("Failed to add tetrahedra to an entity set.");
48✔
2551
  }
2552

24✔
2553
  if (length_multiplier_ > 0.0) {
2554
    // get the connectivity of all tets
24✔
2555
    moab::Range adj;
2556
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
2557
    if (rval != moab::MB_SUCCESS) {
504✔
2558
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
480✔
2559
    }
10,080✔
2560
    // scale all vertex coords by multiplier (done individually so not all
9,600✔
2561
    // coordinates are in memory twice at once)
9,600✔
2562
    for (auto vert : adj) {
2563
      // retrieve coords
2564
      std::array<double, 3> coord;
2565
      rval = mbi_->get_coords(&vert, 1, coord.data());
2566
      if (rval != moab::MB_SUCCESS) {
48✔
2567
        fatal_error("Could not get coordinates of vertex.");
2568
      }
2569
      // scale coords
2570
      for (auto& c : coord) {
12✔
2571
        c *= length_multiplier_;
2572
      }
2573
      // set new coords
12✔
UNCOV
2574
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2575
      if (rval != moab::MB_SUCCESS) {
12✔
2576
        fatal_error("Failed to set new vertex coordinates");
12✔
2577
      }
12✔
2578
    }
12✔
2579
  }
2580

2581
  // Determine bounds of mesh
2582
  this->determine_bounds();
217✔
2583
}
2584

2585
void MOABMesh::prepare_for_point_location()
217✔
UNCOV
2586
{
×
2587
  // if the KDTree has already been constructed, do nothing
217✔
2588
  if (kdtree_)
2589
    return;
2590

217✔
2591
  // build acceleration data structures
217✔
2592
  compute_barycentric_data(ehs_);
217✔
2593
  build_kdtree(ehs_);
2594
}
2595

2596
void MOABMesh::create_interface()
241✔
2597
{
2598
  // Do not create a MOAB instance if one is already in memory
2599
  if (mbi_)
241✔
UNCOV
2600
    return;
×
2601

241✔
2602
  // create MOAB instance
2603
  mbi_ = std::make_shared<moab::Core>();
241✔
UNCOV
2604

×
UNCOV
2605
  // load unstructured mesh file
×
2606
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2607
  if (rval != moab::MB_SUCCESS) {
2608
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
241✔
2609
  }
241✔
2610
}
241✔
2611

241✔
2612
void MOABMesh::build_kdtree(const moab::Range& all_tets)
241✔
2613
{
2614
  moab::Range all_tris;
2615
  int adj_dim = 2;
2616
  write_message("Getting tet adjacencies...", 7);
253✔
2617
  moab::ErrorCode rval = mbi_->get_adjacencies(
2618
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2619
  if (rval != moab::MB_SUCCESS) {
253✔
UNCOV
2620
    fatal_error("Failed to get adjacent triangles for tets");
×
2621
  }
253✔
2622

2623
  if (!all_tris.all_of_type(moab::MBTRI)) {
253✔
UNCOV
2624
    warning("Non-triangle elements found in tet adjacencies in "
×
UNCOV
2625
            "unstructured mesh file: " +
×
2626
            filename_);
2627
  }
2628

253✔
2629
  // combine into one range
253✔
2630
  moab::Range all_tets_and_tris;
229✔
2631
  all_tets_and_tris.merge(all_tets);
229✔
2632
  all_tets_and_tris.merge(all_tris);
229✔
2633

24✔
2634
  // create a kd-tree instance
12✔
2635
  write_message(
12✔
2636
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
12✔
2637
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
12✔
2638

12✔
2639
  // Determine what options to use
12✔
2640
  std::ostringstream options_stream;
12✔
2641
  if (options_.empty()) {
UNCOV
2642
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
×
UNCOV
2643
  } else {
×
2644
    options_stream << options_;
2645
  }
2646
  moab::FileOptions file_opts(options_stream.str().c_str());
2647

2648
  // Build the k-d tree
2649
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2650
  if (rval != moab::MB_SUCCESS) {
253✔
2651
    fatal_error("Failed to construct KDTree for the "
253✔
2652
                "unstructured mesh file: " +
1,012✔
2653
                filename_);
759✔
2654
  }
2655
}
2656

253✔
2657
void MOABMesh::intersect_track(const moab::CartVect& start,
253✔
2658
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2659
{
2660
  hits.clear();
2661

98✔
2662
  moab::ErrorCode rval;
2663
  vector<moab::EntityHandle> tris;
2664
  // get all intersections with triangles in the tet mesh
2665
  // (distances are relative to the start point, not the previous
98✔
UNCOV
2666
  // intersection)
×
2667
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
2668
    dir.array(), start.array(), tris, hits, 0, track_len);
98✔
2669
  if (rval != moab::MB_SUCCESS) {
2670
    fatal_error(
98✔
2671
      "Failed to compute intersections on unstructured mesh: " + filename_);
2672
  }
98✔
2673

98✔
2674
  // remove duplicate intersection distances
98✔
2675
  std::unique(hits.begin(), hits.end());
2676

652✔
2677
  // sorts by first component of std::pair by default
554✔
2678
  std::sort(hits.begin(), hits.end());
2679
}
378✔
2680

280✔
2681
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2682
  vector<int>& bins, vector<double>& lengths) const
354✔
2683
{
256✔
2684
  moab::CartVect start(r0.x, r0.y, r0.z);
2685
  moab::CartVect end(r1.x, r1.y, r1.z);
2686
  moab::CartVect dir(u.x, u.y, u.z);
98✔
2687
  dir.normalize();
98✔
2688

2689
  double track_len = (end - start).length();
24✔
2690
  if (track_len == 0.0)
2691
    return;
2692

2693
  start -= TINY_BIT * dir;
24✔
UNCOV
2694
  end += TINY_BIT * dir;
×
2695

2696
  vector<double> hits;
24✔
2697
  intersect_track(start, dir, track_len, hits);
2698

24✔
2699
  bins.clear();
2700
  lengths.clear();
24✔
2701

24✔
2702
  // if there are no intersections the track may lie entirely
24✔
2703
  // within a single tet. If this is the case, apply entire
2704
  // score to that tet and return.
96✔
2705
  if (hits.size() == 0) {
72✔
2706
    Position midpoint = r0 + u * (track_len * 0.5);
2707
    int bin = this->get_bin(midpoint);
96✔
2708
    if (bin != -1) {
72✔
2709
      bins.push_back(bin);
2710
      lengths.push_back(1.0);
108✔
2711
    }
84✔
2712
    return;
2713
  }
2714

24✔
2715
  // for each segment in the set of tracks, try to look up a tet
24✔
2716
  // at the midpoint of the segment
2717
  Position current = r0;
24✔
2718
  double last_dist = 0.0;
2719
  for (const auto& hit : hits) {
2720
    // get the segment length
2721
    double segment_length = hit - last_dist;
24✔
UNCOV
2722
    last_dist = hit;
×
2723
    // find the midpoint of this segment
2724
    Position midpoint = current + u * (segment_length * 0.5);
24✔
2725
    // try to find a tet for this position
2726
    int bin = this->get_bin(midpoint);
24✔
2727

2728
    // determine the start point for this segment
24✔
2729
    current = r0 + u * hit;
24✔
2730

24✔
2731
    if (bin == -1) {
2732
      continue;
96✔
2733
    }
72✔
2734

2735
    bins.push_back(bin);
108✔
2736
    lengths.push_back(segment_length / track_len);
84✔
2737
  }
2738

84✔
2739
  // tally remaining portion of track after last hit if
60✔
2740
  // the last segment of the track is in the mesh but doesn't
2741
  // reach the other side of the tet
2742
  if (hits.back() < track_len) {
24✔
2743
    Position segment_start = r0 + u * hits.back();
24✔
2744
    double segment_length = track_len - hits.back();
2745
    Position midpoint = segment_start + u * (segment_length * 0.5);
50✔
2746
    int bin = this->get_bin(midpoint);
2747
    if (bin != -1) {
2748
      bins.push_back(bin);
2749
      lengths.push_back(segment_length / track_len);
50✔
UNCOV
2750
    }
×
2751
  }
2752
};
50✔
2753

2754
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
50✔
2755
{
2756
  moab::CartVect pos(r.x, r.y, r.z);
50✔
2757
  // find the leaf of the kd-tree for this position
50✔
2758
  moab::AdaptiveKDTreeIter kdtree_iter;
50✔
2759
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
2760
  if (rval != moab::MB_SUCCESS) {
460✔
2761
    return 0;
410✔
2762
  }
2763

174✔
2764
  // retrieve the tet elements of this leaf
124✔
2765
  moab::EntityHandle leaf = kdtree_iter.handle();
2766
  moab::Range tets;
162✔
2767
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
112✔
2768
  if (rval != moab::MB_SUCCESS) {
2769
    warning("MOAB error finding tets.");
2770
  }
50✔
2771

50✔
2772
  // loop over the tets in this leaf, returning the containing tet if found
2773
  for (const auto& tet : tets) {
2774
    if (point_in_tet(pos, tet)) {
2775
      return tet;
2776
    }
422✔
2777
  }
2778

2779
  // if no tet is found, return an invalid handle
422✔
UNCOV
2780
  return 0;
×
2781
}
422✔
2782

2783
double MOABMesh::volume(int bin) const
422✔
UNCOV
2784
{
×
UNCOV
2785
  return tet_volume(get_ent_handle_from_bin(bin));
×
2786
}
2787

2788
std::string MOABMesh::library() const
422✔
2789
{
422✔
2790
  return mesh_lib_type;
422✔
2791
}
422✔
2792

422✔
2793
// Sample position within a tet for MOAB type tets
422✔
2794
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2795
{
422✔
2796

2797
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2798

2799
  // Get vertex coordinates for MOAB tet
2800
  const moab::EntityHandle* conn1;
132✔
UNCOV
2801
  int conn1_size;
×
2802
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
132✔
2803
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
2804
    fatal_error(fmt::format(
132✔
UNCOV
2805
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
×
UNCOV
2806
      conn1_size));
×
2807
  }
2808
  moab::CartVect p[4];
2809
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
132✔
2810
  if (rval != moab::MB_SUCCESS) {
132✔
2811
    fatal_error("Failed to get tet coords");
132✔
2812
  }
132✔
2813

132✔
2814
  std::array<Position, 4> tet_verts;
132✔
2815
  for (int i = 0; i < 4; i++) {
2816
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
132✔
2817
  }
2818
  // Samples position within tet using Barycentric stuff
132✔
2819
  return this->sample_tet(tet_verts, seed);
2820
}
2821

132✔
UNCOV
2822
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2823
{
132✔
2824
  vector<moab::EntityHandle> conn;
2825
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
132✔
UNCOV
2826
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2827
    fatal_error("Failed to get tet connectivity");
×
2828
  }
2829

2830
  moab::CartVect p[4];
132✔
2831
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
132✔
2832
  if (rval != moab::MB_SUCCESS) {
132✔
2833
    fatal_error("Failed to get tet coords");
132✔
2834
  }
132✔
2835

132✔
2836
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2837
}
132✔
2838

2839
int MOABMesh::get_bin(Position r) const
158✔
2840
{
2841
  moab::EntityHandle tet = get_tet(r);
2842
  if (tet == 0) {
158✔
UNCOV
2843
    return -1;
×
2844
  } else {
158✔
2845
    return get_bin_from_ent_handle(tet);
2846
  }
158✔
UNCOV
2847
}
×
UNCOV
2848

×
2849
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2850
{
2851
  moab::ErrorCode rval;
158✔
2852

158✔
2853
  baryc_data_.clear();
158✔
2854
  baryc_data_.resize(tets.size());
158✔
2855

158✔
2856
  // compute the barycentric data for each tet element
158✔
2857
  // and store it as a 3x3 matrix
2858
  for (auto& tet : tets) {
158✔
2859
    vector<moab::EntityHandle> verts;
2860
    rval = mbi_->get_connectivity(&tet, 1, verts);
2861
    if (rval != moab::MB_SUCCESS) {
2862
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
158✔
2863
    }
2864

2865
    moab::CartVect p[4];
158✔
2866
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
158✔
2867
    if (rval != moab::MB_SUCCESS) {
2868
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2869
    }
2870

50✔
2871
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2872

2873
    // invert now to avoid this cost later
2874
    a = a.transpose().inverse();
50✔
2875
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
50✔
2876
  }
2877
}
2878

2879
bool MOABMesh::point_in_tet(
132✔
2880
  const moab::CartVect& r, moab::EntityHandle tet) const
2881
{
2882

132✔
2883
  moab::ErrorCode rval;
132✔
2884

2885
  // get tet vertices
2886
  vector<moab::EntityHandle> verts;
2887
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2888
  if (rval != moab::MB_SUCCESS) {
2889
    warning("Failed to get vertices of tet in umesh: " + filename_);
2890
    return false;
2891
  }
24✔
2892

24✔
2893
  // first vertex is used as a reference point for the barycentric data -
2894
  // retrieve its coordinates
2895
  moab::CartVect p_zero;
2896
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
132✔
2897
  if (rval != moab::MB_SUCCESS) {
2898
    warning("Failed to get coordinates of a vertex in "
2899
            "unstructured mesh: " +
2900
            filename_);
132✔
2901
    return false;
132✔
2902
  }
2903

2904
  // look up barycentric data
2905
  int idx = get_bin_from_ent_handle(tet);
2906
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2907

2908
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2909

2910
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
24✔
2911
          bary_coords[2] >= 0.0 &&
24✔
2912
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2913
}
2914

2915
int MOABMesh::get_bin_from_index(int idx) const
2916
{
2917
  if (idx >= n_bins()) {
2918
    fatal_error(fmt::format("Invalid bin index: {}", idx));
23✔
2919
  }
2920
  return ehs_[idx] - ehs_[0];
23✔
2921
}
23✔
2922

2923
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2924
{
2925
  int bin = get_bin(r);
2926
  *in_mesh = bin != -1;
2927
  return bin;
2928
}
2929

2930
int MOABMesh::get_index_from_bin(int bin) const
1✔
2931
{
2932
  return bin;
1✔
2933
}
1✔
2934

1✔
2935
std::pair<vector<double>, vector<double>> MOABMesh::plot(
1✔
2936
  Position plot_ll, Position plot_ur) const
2937
{
24✔
2938
  // TODO: Implement mesh lines
2939
  return {};
2940
}
2941

24✔
2942
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2943
{
2944
  int idx = vert - verts_[0];
24✔
2945
  if (idx >= n_vertices()) {
2946
    fatal_error(
2947
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
24✔
2948
  }
2949
  return idx;
2950
}
24✔
2951

24✔
2952
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2953
{
2954
  int bin = eh - ehs_[0];
2955
  if (bin >= n_bins()) {
24✔
2956
    fatal_error(fmt::format("Invalid bin: {}", bin));
2957
  }
2958
  return bin;
2959
}
2960

2961
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2962
{
24✔
2963
  if (bin >= n_bins()) {
24✔
2964
    fatal_error(fmt::format("Invalid bin index: ", bin));
24✔
2965
  }
2966
  return ehs_[0] + bin;
2967
}
2968

2969
int MOABMesh::n_bins() const
2970
{
24✔
2971
  return ehs_.size();
24✔
2972
}
2973

2974
int MOABMesh::n_surface_bins() const
2975
{
24✔
2976
  // collect all triangles in the set of tets for this mesh
24✔
2977
  moab::Range tris;
2978
  moab::ErrorCode rval;
2979
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
2980
  if (rval != moab::MB_SUCCESS) {
24✔
2981
    warning("Failed to get all triangles in the mesh instance");
2982
    return -1;
2983
  }
2984
  return 2 * tris.size();
2985
}
2986

2987
Position MOABMesh::centroid(int bin) const
2988
{
2989
  moab::ErrorCode rval;
2990

2991
  auto tet = this->get_ent_handle_from_bin(bin);
2992

2993
  // look up the tet connectivity
2994
  vector<moab::EntityHandle> conn;
2995
  rval = mbi_->get_connectivity(&tet, 1, conn);
2996
  if (rval != moab::MB_SUCCESS) {
2997
    warning("Failed to get connectivity of a mesh element.");
2998
    return {};
2999
  }
3000

3001
  // get the coordinates
3002
  vector<moab::CartVect> coords(conn.size());
3003
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
3004
  if (rval != moab::MB_SUCCESS) {
3005
    warning("Failed to get the coordinates of a mesh element.");
3006
    return {};
3007
  }
3008

3009
  // compute the centroid of the element vertices
24✔
3010
  moab::CartVect centroid(0.0, 0.0, 0.0);
24✔
3011
  for (const auto& coord : coords) {
3012
    centroid += coord;
20✔
3013
  }
3014
  centroid /= double(coords.size());
3015

20✔
3016
  return {centroid[0], centroid[1], centroid[2]};
3017
}
3018

3019
int MOABMesh::n_vertices() const
20✔
3020
{
20✔
3021
  return verts_.size();
3022
}
3023

24✔
3024
Position MOABMesh::vertex(int id) const
3025
{
3026

24✔
3027
  moab::ErrorCode rval;
1✔
3028

3029
  moab::EntityHandle vert = verts_[id];
3030

23✔
3031
  moab::CartVect coords;
3032
  rval = mbi_->get_coords(&vert, 1, coords.array());
3033
  if (rval != moab::MB_SUCCESS) {
23✔
3034
    fatal_error("Failed to get the coordinates of a vertex.");
23✔
3035
  }
3036

3037
  return {coords[0], coords[1], coords[2]};
3038
}
3039

20✔
3040
std::vector<int> MOABMesh::connectivity(int bin) const
3041
{
20✔
3042
  moab::ErrorCode rval;
20✔
3043

20✔
3044
  auto tet = get_ent_handle_from_bin(bin);
20✔
3045

3046
  // look up the tet connectivity
20✔
3047
  vector<moab::EntityHandle> conn;
3048
  rval = mbi_->get_connectivity(&tet, 1, conn);
3049
  if (rval != moab::MB_SUCCESS) {
3050
    fatal_error("Failed to get connectivity of a mesh element.");
20✔
3051
    return {};
3052
  }
3053

3054
  std::vector<int> verts(4);
3055
  for (int i = 0; i < verts.size(); i++) {
3056
    verts[i] = get_vert_idx_from_handle(conn[i]);
3057
  }
20✔
3058

20✔
3059
  return verts;
20✔
3060
}
3061

3062
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
20✔
3063
  std::string score) const
20✔
3064
{
20✔
3065
  moab::ErrorCode rval;
3066
  // add a tag to the mesh
3067
  // all scores are treated as a single value
20✔
3068
  // with an uncertainty
20✔
3069
  moab::Tag value_tag;
4✔
3070

3071
  // create the value tag if not present and get handle
16✔
3072
  double default_val = 0.0;
3073
  auto val_string = score + "_mean";
20✔
3074
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
3075
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3076
  if (rval != moab::MB_SUCCESS) {
20✔
3077
    auto msg =
20✔
3078
      fmt::format("Could not create or retrieve the value tag for the score {}"
3079
                  " on unstructured mesh {}",
3080
        score, id_);
3081
    fatal_error(msg);
3082
  }
20✔
3083

3084
  // create the std dev tag if not present and get handle
1,542,122✔
3085
  moab::Tag error_tag;
3086
  std::string err_string = score + "_std_dev";
3087
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
1,542,122✔
3088
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3089
  if (rval != moab::MB_SUCCESS) {
3090
    auto msg =
1,542,122✔
3091
      fmt::format("Could not create or retrieve the error tag for the score {}"
3092
                  " on unstructured mesh {}",
3093
        score, id_);
3094
    fatal_error(msg);
1,542,122✔
3095
  }
3096

1,542,122✔
3097
  // return the populated tag handles
3098
  return {value_tag, error_tag};
3099
}
3100

3101
void MOABMesh::add_score(const std::string& score)
3102
{
1,542,122✔
3103
  auto score_tags = get_score_tags(score);
3104
  tag_names_.push_back(score);
3105
}
1,542,122✔
3106

1,542,122✔
3107
void MOABMesh::remove_scores()
3108
{
1,542,122✔
3109
  for (const auto& name : tag_names_) {
3110
    auto value_name = name + "_mean";
3111
    moab::Tag tag;
1,542,122✔
3112
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
1,542,122✔
3113
    if (rval != moab::MB_SUCCESS)
1,542,122✔
3114
      return;
1,542,122✔
3115

3116
    rval = mbi_->tag_delete(tag);
1,542,122✔
3117
    if (rval != moab::MB_SUCCESS) {
1,542,122✔
3118
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
720,627✔
3119
                             " on unstructured mesh {}",
3120
        name, id_);
1,542,122✔
3121
      fatal_error(msg);
1,542,122✔
3122
    }
3123

1,542,122✔
3124
    auto std_dev_name = name + "_std_dev";
1,542,122✔
3125
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
3126
    if (rval != moab::MB_SUCCESS) {
1,542,122✔
3127
      auto msg =
1,542,122✔
3128
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3129
                    " on unstructured mesh {}",
3130
          name, id_);
3131
    }
3132

1,542,122✔
3133
    rval = mbi_->tag_delete(tag);
720,627✔
3134
    if (rval != moab::MB_SUCCESS) {
720,627✔
3135
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
720,627✔
3136
                             " on unstructured mesh {}",
242,659✔
3137
        name, id_);
242,659✔
3138
      fatal_error(msg);
3139
    }
720,627✔
3140
  }
3141
  tag_names_.clear();
3142
}
3143

3144
void MOABMesh::set_score_data(const std::string& score,
821,495✔
3145
  const vector<double>& values, const vector<double>& std_dev)
821,495✔
3146
{
5,514,377✔
3147
  auto score_tags = this->get_score_tags(score);
3148

4,692,882✔
3149
  moab::ErrorCode rval;
4,692,882✔
3150
  // set the score value
3151
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
4,692,882✔
3152
  if (rval != moab::MB_SUCCESS) {
3153
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
4,692,882✔
3154
                           "on unstructured mesh {}",
3155
      score, id_);
3156
    warning(msg);
4,692,882✔
3157
  }
3158

4,692,882✔
3159
  // set the error value
20,480✔
3160
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3161
  if (rval != moab::MB_SUCCESS) {
3162
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
4,672,402✔
3163
                           "on unstructured mesh {}",
4,672,402✔
3164
      score, id_);
3165
    warning(msg);
3166
  }
3167
}
3168

3169
void MOABMesh::write(const std::string& base_filename) const
821,495✔
3170
{
821,495✔
3171
  // add extension to the base name
821,495✔
3172
  auto filename = base_filename + ".vtk";
821,495✔
3173
  write_message(5, "Writing unstructured mesh {}...", filename);
821,495✔
3174
  filename = settings::path_output + filename;
821,495✔
3175

766,195✔
3176
  // write the tetrahedral elements of the mesh only
766,195✔
3177
  // to avoid clutter from zero-value data on other
3178
  // elements during visualization
3179
  moab::ErrorCode rval;
1,542,122✔
3180
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
3181
  if (rval != moab::MB_SUCCESS) {
7,307,001✔
3182
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
3183
    warning(msg);
7,307,001✔
3184
  }
3185
}
7,307,001✔
3186

7,307,001✔
3187
#endif
7,307,001✔
3188

1,010,077✔
3189
#ifdef LIBMESH
3190

3191
const std::string LibMesh::mesh_lib_type = "libmesh";
3192

6,296,924✔
3193
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,296,924✔
3194
{
6,296,924✔
3195
  // filename_ and length_multiplier_ will already be set by the
6,296,924✔
3196
  // UnstructuredMesh constructor
3197
  set_mesh_pointer_from_filename(filename_);
3198
  set_length_multiplier(length_multiplier_);
3199
  initialize();
3200
}
260,010,824✔
3201

260,007,978✔
3202
// create the mesh from a pointer to a libMesh Mesh
6,294,078✔
3203
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
3204
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
3205
{
3206
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
3207
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
2,846✔
3208
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
7,307,001✔
3209
  }
3210

167,856✔
3211
  m_ = &input_mesh;
3212
  set_length_multiplier(length_multiplier);
167,856✔
3213
  initialize();
3214
}
3215

32✔
3216
// create the mesh from an input file
3217
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
32✔
3218
  : adaptive_(false)
3219
{
3220
  set_mesh_pointer_from_filename(filename);
3221
  set_length_multiplier(length_multiplier);
200,410✔
3222
  initialize();
3223
}
3224

200,410✔
3225
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
3226
{
3227
  filename_ = filename;
3228
  unique_m_ =
3229
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
200,410✔
3230
  m_ = unique_m_.get();
200,410✔
3231
  m_->read(filename_);
3232
}
3233

3234
// build a libMesh equation system for storing values
3235
void LibMesh::build_eqn_sys()
1,002,050✔
3236
{
200,410✔
3237
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
200,410✔
3238
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
3239
  libMesh::ExplicitSystem& eq_sys =
3240
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
3241
}
200,410✔
3242

1,002,050✔
3243
// intialize from mesh file
801,640✔
3244
void LibMesh::initialize()
3245
{
3246
  if (!settings::libmesh_comm) {
400,820✔
3247
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3248
                "communicator.");
3249
  }
167,856✔
3250

3251
  // assuming that unstructured meshes used in OpenMC are 3D
167,856✔
3252
  n_dimension_ = 3;
167,856✔
3253

167,856✔
3254
  if (length_multiplier_ > 0.0) {
3255
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
3256
  }
3257
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
839,280✔
3258
  // Otherwise assume that it is prepared by its owning application
167,856✔
3259
  if (unique_m_) {
167,856✔
3260
    m_->prepare_for_use();
3261
  }
3262

3263
  // ensure that the loaded mesh is 3 dimensional
335,712✔
3264
  if (m_->mesh_dimension() != n_dimension_) {
167,856✔
3265
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3266
                            "mesh is not a 3D mesh.",
7,307,001✔
3267
      filename_));
3268
  }
7,307,001✔
3269

7,307,001✔
3270
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3271
    pl_.emplace_back(m_->sub_point_locator());
3272
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,294,078✔
3273
    pl_.back()->enable_out_of_mesh_mode();
3274
  }
3275

3276
  // store first element in the mesh to use as an offset for bin indices
20✔
3277
  auto first_elem = *m_->elements_begin();
3278
  first_element_id_ = first_elem->id();
3279

3280
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
20✔
3281
  // contiguous in ID space, so we need to map from bin indices (defined over
20✔
3282
  // active elements) to global dof ids
3283
  if (adaptive_) {
3284
    bin_to_elem_map_.reserve(m_->n_active_elem());
3285
    elem_to_bin_map_.resize(m_->n_elem(), -1);
239,732✔
3286
    for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
239,712✔
3287
         it++) {
239,712✔
3288
      auto elem = *it;
239,712✔
3289

3290
      bin_to_elem_map_.push_back(elem->id());
3291
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3292
    }
1,198,560✔
3293
  }
239,712✔
3294

239,712✔
3295
  // bounding box for the mesh for quick rejection checks
3296
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3297
  libMesh::Point ll = bbox_.min();
3298
  libMesh::Point ur = bbox_.max();
239,712✔
3299
  lower_left_ = {ll(0), ll(1), ll(2)};
3300
  upper_right_ = {ur(0), ur(1), ur(2)};
3301
}
239,712✔
3302

239,712✔
3303
// Sample position within a tet for LibMesh type tets
239,712✔
3304
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
20✔
3305
{
3306
  const auto& elem = get_element_from_bin(bin);
260,007,978✔
3307
  // Get tet vertex coordinates from LibMesh
3308
  std::array<Position, 4> tet_verts;
3309
  for (int i = 0; i < elem.n_nodes(); i++) {
3310
    auto node_ref = elem.node_ref(i);
3311
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
3312
  }
3313
  // Samples position within tet using Barycentric coordinates
260,007,978✔
3314
  return this->sample_tet(tet_verts, seed);
260,007,978✔
3315
}
260,007,978✔
3316

3317
Position LibMesh::centroid(int bin) const
3318
{
3319
  const auto& elem = this->get_element_from_bin(bin);
3320
  auto centroid = elem.vertex_average();
3321
  return {centroid(0), centroid(1), centroid(2)};
3322
}
260,007,978✔
3323

260,007,978✔
3324
int LibMesh::n_vertices() const
260,007,978✔
3325
{
3326
  return m_->n_nodes();
3327
}
3328

3329
Position LibMesh::vertex(int vertex_id) const
3330
{
3331
  const auto node_ref = m_->node_ref(vertex_id);
3332
  return {node_ref(0), node_ref(1), node_ref(2)};
260,007,978✔
3333
}
260,007,978✔
3334

3335
std::vector<int> LibMesh::connectivity(int elem_id) const
260,007,978✔
3336
{
3337
  std::vector<int> conn;
421,084,473✔
3338
  const auto* elem_ptr = m_->elem_ptr(elem_id);
442,748,125✔
3339
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
281,671,630✔
3340
    conn.push_back(elem_ptr->node_id(i));
260,007,978✔
3341
  }
3342
  return conn;
3343
}
3344

3345
std::string LibMesh::library() const
3346
{
3347
  return mesh_lib_type;
3348
}
3349

3350
int LibMesh::n_bins() const
3351
{
3352
  return m_->n_active_elem();
3353
}
3354

3355
int LibMesh::n_surface_bins() const
3356
{
3357
  int n_bins = 0;
3358
  for (int i = 0; i < this->n_bins(); i++) {
3359
    const libMesh::Elem& e = get_element_from_bin(i);
3360
    n_bins += e.n_faces();
3361
    // if this is a boundary element, it will only be visited once,
3362
    // the number of surface bins is incremented to
3363
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
3364
      // null neighbor pointer indicates a boundary face
3365
      if (!neighbor_ptr) {
3366
        n_bins++;
3367
      }
3368
    }
3369
  }
815,424✔
3370
  return n_bins;
3371
}
815,424✔
3372

815,424✔
3373
void LibMesh::add_score(const std::string& var_name)
3374
{
3375
  if (adaptive_) {
3376
    warning(fmt::format(
815,424✔
3377
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3378
      this->id_));
3379

266,541,768✔
3380
    return;
3381
  }
266,541,768✔
3382

266,541,768✔
3383
  if (!equation_systems_) {
3384
    build_eqn_sys();
3385
  }
266,541,768✔
3386

3387
  // check if this is a new variable
3388
  std::string value_name = var_name + "_mean";
572,122✔
3389
  if (!variable_map_.count(value_name)) {
3390
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
572,122✔
3391
    auto var_num =
3392
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3393
    variable_map_[value_name] = var_num;
572,122✔
3394
  }
3395

3396
  std::string std_dev_name = var_name + "_std_dev";
267,317,815✔
3397
  // check if this is a new variable
3398
  if (!variable_map_.count(std_dev_name)) {
267,317,815✔
3399
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3400
    auto var_num =
3401
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3402
    variable_map_[std_dev_name] = var_num;
3403
  }
3404
}
3405

3406
void LibMesh::remove_scores()
3407
{
3408
  if (equation_systems_) {
3409
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3410
    eqn_sys.clear();
3411
    variable_map_.clear();
3412
  }
3413
}
3414

3415
void LibMesh::set_score_data(const std::string& var_name,
3416
  const vector<double>& values, const vector<double>& std_dev)
3417
{
3418
  if (adaptive_) {
3419
    warning(fmt::format(
3420
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3421
      this->id_));
3422

3423
    return;
3424
  }
3425

3426
  if (!equation_systems_) {
3427
    build_eqn_sys();
3428
  }
3429

3430
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3431

3432
  if (!eqn_sys.is_initialized()) {
3433
    equation_systems_->init();
3434
  }
3435

3436
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3437

3438
  // look up the value variable
3439
  std::string value_name = var_name + "_mean";
3440
  unsigned int value_num = variable_map_.at(value_name);
3441
  // look up the std dev variable
3442
  std::string std_dev_name = var_name + "_std_dev";
3443
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3444

3445
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3446
       it++) {
845,761✔
3447
    if (!(*it)->active()) {
3448
      continue;
845,761✔
3449
    }
3450

3451
    auto bin = get_bin_from_element(*it);
86,199✔
3452

3453
    // set value
3454
    vector<libMesh::dof_id_type> value_dof_indices;
3455
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3456
    assert(value_dof_indices.size() == 1);
86,199✔
3457
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3458

86,199✔
3459
    // set std dev
86,199✔
3460
    vector<libMesh::dof_id_type> std_dev_dof_indices;
86,199✔
3461
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3462
    assert(std_dev_dof_indices.size() == 1);
3463
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3464
  }
172,398✔
3465
}
3466

3467
void LibMesh::write(const std::string& filename) const
203,856✔
3468
{
3469
  if (adaptive_) {
3470
    warning(fmt::format(
3471
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
203,856✔
3472
      this->id_));
3473

3474
    return;
203,856✔
3475
  }
203,856✔
3476

203,856✔
3477
  write_message(fmt::format(
3478
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
3479
  libMesh::ExodusII_IO exo(*m_);
3480
  std::set<std::string> systems_out = {eq_system_name_};
3481
  exo.write_discontinuous_exodusII(
203,856✔
3482
    filename + ".e", *equation_systems_, &systems_out);
1,019,280✔
3483
}
815,424✔
3484

3485
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3486
  vector<int>& bins, vector<double>& lengths) const
203,856✔
3487
{
203,856✔
3488
  // TODO: Implement triangle crossings here
3489
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3490
}
3491

3492
int LibMesh::get_bin(Position r) const
3493
{
3494
  // look-up a tet using the point locator
3495
  libMesh::Point p(r.x, r.y, r.z);
3496

3497
  // quick rejection check
3498
  if (!bbox_.contains_point(p)) {
3499
    return -1;
3500
  }
3501

3502
  const auto& point_locator = pl_.at(thread_num());
3503

3504
  const auto elem_ptr = (*point_locator)(p);
3505
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3506
}
3507

3508
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3509
{
3510
  int bin =
3511
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3512
  if (bin >= n_bins() || bin < 0) {
3513
    fatal_error(fmt::format("Invalid bin: {}", bin));
3514
  }
3515
  return bin;
3516
}
3517

3518
std::pair<vector<double>, vector<double>> LibMesh::plot(
3519
  Position plot_ll, Position plot_ur) const
3520
{
3521
  return {};
3522
}
3523

3524
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3525
{
3526
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3527
}
3528

3529
double LibMesh::volume(int bin) const
3530
{
3531
  return this->get_element_from_bin(bin).volume();
3532
}
3533

3534
#endif // LIBMESH
3535

3536
//==============================================================================
3537
// Non-member functions
3538
//==============================================================================
3539

3540
void read_meshes(pugi::xml_node root)
3541
{
3542
  std::unordered_set<int> mesh_ids;
3543

3544
  for (auto node : root.children("mesh")) {
3545
    // Check to make sure multiple meshes in the same file don't share IDs
3546
    int id = std::stoi(get_node_value(node, "id"));
3547
    if (contains(mesh_ids, id)) {
3548
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
3549
                              "'{}' in the same input file",
3550
        id));
3551
    }
3552
    mesh_ids.insert(id);
3553

3554
    // If we've already read a mesh with the same ID in a *different* file,
3555
    // assume it is the same here
3556
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3557
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3558
      continue;
3559
    }
3560

3561
    std::string mesh_type;
3562
    if (check_for_node(node, "type")) {
3563
      mesh_type = get_node_value(node, "type", true, true);
3564
    } else {
3565
      mesh_type = "regular";
3566
    }
3567

3568
    // determine the mesh library to use
3569
    std::string mesh_lib;
3570
    if (check_for_node(node, "library")) {
3571
      mesh_lib = get_node_value(node, "library", true, true);
3572
    }
3573

3574
    // Read mesh and add to vector
3575
    if (mesh_type == RegularMesh::mesh_type) {
3576
      model::meshes.push_back(make_unique<RegularMesh>(node));
3577
    } else if (mesh_type == RectilinearMesh::mesh_type) {
3578
      model::meshes.push_back(make_unique<RectilinearMesh>(node));
3579
    } else if (mesh_type == CylindricalMesh::mesh_type) {
3580
      model::meshes.push_back(make_unique<CylindricalMesh>(node));
3581
    } else if (mesh_type == SphericalMesh::mesh_type) {
3582
      model::meshes.push_back(make_unique<SphericalMesh>(node));
3583
#ifdef DAGMC
3584
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3585
               mesh_lib == MOABMesh::mesh_lib_type) {
3586
      model::meshes.push_back(make_unique<MOABMesh>(node));
3587
#endif
3588
#ifdef LIBMESH
3589
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3590
               mesh_lib == LibMesh::mesh_lib_type) {
3591
      model::meshes.push_back(make_unique<LibMesh>(node));
3592
#endif
3593
    } else if (mesh_type == UnstructuredMesh::mesh_type) {
3594
      fatal_error("Unstructured mesh support is not enabled or the mesh "
3595
                  "library is invalid.");
3596
    } else {
3597
      fatal_error("Invalid mesh type: " + mesh_type);
3598
    }
3599

3600
    // Map ID to position in vector
3601
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3602
  }
3603
}
3604

3605
void meshes_to_hdf5(hid_t group)
3606
{
3607
  // Write number of meshes
3608
  hid_t meshes_group = create_group(group, "meshes");
3609
  int32_t n_meshes = model::meshes.size();
3610
  write_attribute(meshes_group, "n_meshes", n_meshes);
3611

3612
  if (n_meshes > 0) {
3613
    // Write IDs of meshes
3614
    vector<int> ids;
3615
    for (const auto& m : model::meshes) {
3616
      m->to_hdf5(meshes_group);
3617
      ids.push_back(m->id_);
3618
    }
3619
    write_attribute(meshes_group, "ids", ids);
3620
  }
23✔
3621

3622
  close_group(meshes_group);
3623
}
3624

23✔
3625
void free_memory_mesh()
23✔
3626
{
23✔
3627
  model::meshes.clear();
23✔
3628
  model::mesh_map.clear();
3629
}
3630

3631
extern "C" int n_meshes()
3632
{
3633
  return model::meshes.size();
3634
}
3635

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

© 2025 Coveralls, Inc