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

openmc-dev / openmc / 13212514097

08 Feb 2025 05:00AM UTC coverage: 84.874% (+0.009%) from 84.865%
13212514097

Pull #3129

github

web-flow
Merge ccc8376a9 into 6e0f156d3
Pull Request #3129: Compute material volumes in mesh elements based on raytracing

205 of 231 new or added lines in 3 files covered. (88.74%)

1126 existing lines in 43 files now uncovered.

50270 of 59229 relevant lines covered (84.87%)

34951789.18 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
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
4
#include <cmath>          // for ceil
5
#include <cstddef>        // for size_t
6
#include <gsl/gsl-lite.hpp>
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,369✔
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,369✔
124
    ptr, &expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
1,369✔
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

158
    // Non-atomic read of current material
159
    int32_t& current_val = this->materials(index_elem, slot);
2,543,388✔
160

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

168
    // Slot appears to be empty; attempt to claim
169
    if (current_val == EMPTY) {
69,601✔
170
      // Attempt compare-and-swap from EMPTY to index_material
171
      bool claimed_slot = atomic_cas_int32(&current_val, EMPTY, index_material);
1,369✔
172

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

183
  // If table is full, set a flag that can be checked later
NEW
184
  table_full_ = true;
×
185
}
186

NEW
187
void MaterialVolumes::add_volume_unsafe(
×
188
  int index_elem, int index_material, double volume)
189
{
190
  // Linear probe
NEW
191
  for (int attempt = 0; attempt < table_size_; ++attempt) {
×
NEW
192
    int slot = (index_material + attempt) % table_size_;
×
193

194
    // Read current material
NEW
195
    int32_t current_val = this->materials(index_elem, slot);
×
196

197
    // Found the desired material; accumulate volume
NEW
198
    if (current_val == index_material) {
×
NEW
199
      this->volumes(index_elem, slot) += volume;
×
NEW
200
      return;
×
201
    }
202

203
    // Claim empty slot
NEW
204
    if (current_val == EMPTY) {
×
NEW
205
      this->materials(index_elem, slot) = index_material;
×
NEW
206
      this->volumes(index_elem, slot) += volume;
×
NEW
207
      return;
×
208
    }
209
  }
210

211
  // If table is full, set a flag that can be checked later
NEW
212
  table_full_ = true;
×
213
}
214

215
} // namespace detail
216

217
//==============================================================================
218
// Mesh implementation
219
//==============================================================================
220

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

229
void Mesh::set_id(int32_t id)
1✔
230
{
231
  Expects(id >= 0 || id == C_NONE);
1✔
232

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

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

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

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

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

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

282
  Timer timer;
156✔
283
  timer.start();
156✔
284

285
  // Create object for keeping track of materials/volumes
286
  detail::MaterialVolumes result(materials, volumes, table_size);
156✔
287

288
  // Determine bounding box
289
  auto bbox = this->bounding_box();
156✔
290

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

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

297
  // Set flag for mesh being contained within model
298
  bool out_of_model = false;
156✔
299

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

307
    SourceSite site;
78✔
308
    site.E = 1.0;
78✔
309
    site.particle = ParticleType::neutron;
78✔
310

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

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

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

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

343
          p.from_source(&site);
494,274✔
344

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

351
          // Set birth cell attribute
352
          if (p.cell_born() == C_NONE)
446,358✔
353
            p.cell_born() = p.lowest_coord().cell;
446,358✔
354

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

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

366
            // Find the distance to the nearest boundary
367
            BoundaryInfo boundary = distance_to_boundary(p);
973,974✔
368

369
            // Advance particle forward
370
            double distance = std::min(boundary.distance, max_distance);
973,974✔
371
            p.move_distance(distance);
973,974✔
372

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

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

387
              // Add volume to result
388
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,237,578✔
389
            }
390

391
            if (distance == max_distance)
973,974✔
392
              break;
446,358✔
393

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

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

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

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

428
  // Compute time for raytracing
429
  double t_raytrace = timer.elapsed();
144✔
430

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

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

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

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

470
  // Normalize based on known volumes of elements
471
  for (int i = 0; i < this->n_bins(); ++i) {
1,020✔
472
    // Estimated total volume in element i
473
    double volume = 0.0;
876✔
474
    for (int j = 0; j < table_size; ++j) {
7,884✔
475
      volume += result.volumes(i, j);
7,008✔
476
    }
477

478
    // Renormalize volumes based on known volume of element i
479
    double norm = this->volume(i) / volume;
876✔
480
    for (int j = 0; j < table_size; ++j) {
7,884✔
481
      result.volumes(i, j) *= norm;
7,008✔
482
    }
483
  }
484

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

500
void Mesh::to_hdf5(hid_t group) const
3,144✔
501
{
502
  // Create group for mesh
503
  std::string group_name = fmt::format("mesh {}", id_);
5,752✔
504
  hid_t mesh_group = create_group(group, group_name.c_str());
3,144✔
505

506
  // Write mesh type
507
  write_dataset(mesh_group, "type", this->get_mesh_type());
3,144✔
508

509
  // Write mesh ID
510
  write_attribute(mesh_group, "id", id_);
3,144✔
511

512
  // Write mesh name
513
  write_dataset(mesh_group, "name", name_);
3,144✔
514

515
  // Write mesh data
516
  this->to_hdf5_inner(mesh_group);
3,144✔
517

518
  // Close group
519
  close_group(mesh_group);
3,144✔
520
}
3,144✔
521

522
//==============================================================================
523
// Structured Mesh implementation
524
//==============================================================================
525

526
std::string StructuredMesh::bin_label(int bin) const
5,446,464✔
527
{
528
  MeshIndex ijk = get_indices_from_bin(bin);
5,446,464✔
529

530
  if (n_dimension_ > 2) {
5,446,464✔
531
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,860,576✔
532
  } else if (n_dimension_ > 1) {
16,176✔
533
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,752✔
534
  } else {
535
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
536
  }
537
}
538

539
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
3,102✔
540
{
541
  // because method is const, shape_ is const as well and can't be adapted
542
  auto tmp_shape = shape_;
3,102✔
543
  return xt::adapt(tmp_shape, {n_dimension_});
6,204✔
544
}
545

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

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

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

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

563
//==============================================================================
564
// Unstructured Mesh implementation
565
//==============================================================================
566

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

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

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

593
  if (check_for_node(node, "options")) {
45✔
594
    options_ = get_node_value(node, "options");
16✔
595
  }
596

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

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

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

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

656
const std::string UnstructuredMesh::mesh_type = "unstructured";
657

658
std::string UnstructuredMesh::get_mesh_type() const
30✔
659
{
660
  return mesh_type;
30✔
661
}
662

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

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

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

682
  if (length_multiplier_ > 0.0)
30✔
683
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
684

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

693
  int num_elem_skipped = 0;
30✔
694

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

702
    volumes.emplace_back(this->volume(i));
337,712✔
703

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

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

732
  write_dataset(mesh_group, "volumes", volumes);
30✔
733
  write_dataset(mesh_group, "connectivity", connectivity);
30✔
734
  write_dataset(mesh_group, "element_types", elem_types);
30✔
735
}
30✔
736

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

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

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

754
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,036,458,918✔
755
  Position r, bool& in_mesh) const
756
{
757
  MeshIndex ijk;
758
  in_mesh = true;
1,036,458,918✔
759
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
760
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
761

762
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
763
      in_mesh = false;
91,752,208✔
764
  }
765
  return ijk;
1,036,458,918✔
766
}
767

768
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,090,129,767✔
769
{
770
  switch (n_dimension_) {
1,090,129,767✔
771
  case 1:
956,736✔
772
    return ijk[0] - 1;
956,736✔
773
  case 2:
21,602,040✔
774
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,602,040✔
775
  case 3:
1,067,570,991✔
776
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,067,570,991✔
777
  default:
×
778
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
779
  }
780
}
781

782
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,143,932✔
783
{
784
  MeshIndex ijk;
785
  if (n_dimension_ == 1) {
8,143,932✔
786
    ijk[0] = bin + 1;
300✔
787
  } else if (n_dimension_ == 2) {
8,143,632✔
788
    ijk[0] = bin % shape_[0] + 1;
15,876✔
789
    ijk[1] = bin / shape_[0] + 1;
15,876✔
790
  } else if (n_dimension_ == 3) {
8,127,756✔
791
    ijk[0] = bin % shape_[0] + 1;
8,127,756✔
792
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,127,756✔
793
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,127,756✔
794
  }
795
  return ijk;
8,143,932✔
796
}
797

798
int StructuredMesh::get_bin(Position r) const
333,109,443✔
799
{
800
  // Determine indices
801
  bool in_mesh;
802
  MeshIndex ijk = get_indices(r, in_mesh);
333,109,443✔
803
  if (!in_mesh)
333,109,443✔
804
    return -1;
22,164,978✔
805

806
  // Convert indices to bin
807
  return get_bin_from_indices(ijk);
310,944,465✔
808
}
809

810
int StructuredMesh::n_bins() const
1,077,079✔
811
{
812
  return std::accumulate(
1,077,079✔
813
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,154,158✔
814
}
815

816
int StructuredMesh::n_surface_bins() const
593✔
817
{
818
  return 4 * n_dimension_ * n_bins();
593✔
819
}
820

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

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

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

835
    // determine scoring bin for entropy mesh
836
    int mesh_bin = get_bin(site.r);
×
837

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

844
    // Add to appropriate bin
845
    cnt(mesh_bin) += site.wgt;
×
846
  }
847

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

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

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

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

873
  return counts;
×
874
}
875

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

889
  // Compute the length of the entire track.
890
  double total_distance = (r1 - r0).norm();
707,609,395✔
891
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
707,609,395✔
892
    return;
9,891,964✔
893

894
  const int n = n_dimension_;
697,717,431✔
895

896
  // Flag if position is inside the mesh
897
  bool in_mesh;
898

899
  // Position is r = r0 + u * traveled_distance, start at r0
900
  double traveled_distance {0.0};
697,717,431✔
901

902
  // Calculate index of current cell. Offset the position a tiny bit in
903
  // direction of flight
904
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
697,717,431✔
905

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

915
  // translate start and end positions,
916
  // this needs to come after the get_indices call because it does its own
917
  // translation
918
  local_coords(r0);
697,675,947✔
919
  local_coords(r1);
697,675,947✔
920

921
  // Calculate initial distances to next surfaces in all three dimensions
922
  std::array<MeshDistance, 3> distances;
1,395,351,894✔
923
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
924
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
2,074,995,801✔
925
  }
926

927
  // Loop until r = r1 is eventually reached
928
  while (true) {
362,793,298✔
929

930
    if (in_mesh) {
1,060,469,245✔
931

932
      // find surface with minimal distance to current position
933
      const auto k = std::min_element(distances.begin(), distances.end()) -
967,502,619✔
934
                     distances.begin();
967,502,619✔
935

936
      // Tally track length delta since last step
937
      tally.track(ijk,
967,502,619✔
938
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
967,502,619✔
939
          total_distance);
940

941
      // update position and leave, if we have reached end position
942
      traveled_distance = distances[k].distance;
967,502,619✔
943
      if (traveled_distance >= total_distance)
967,502,619✔
944
        return;
610,341,365✔
945

946
      // If we have not reached r1, we have hit a surface. Tally outward
947
      // current
948
      tally.surface(ijk, k, distances[k].max_surface, false);
357,161,254✔
949

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

956
      // Check if we have left the interior of the mesh
957
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
357,161,254✔
958

959
      // If we are still inside the mesh, tally inward current for the next
960
      // cell
961
      if (in_mesh)
357,161,254✔
962
        tally.surface(ijk, k, !distances[k].max_surface, true);
331,630,694✔
963

964
    } else { // not inside mesh
965

966
      // For all directions outside the mesh, find the distance that we need
967
      // to travel to reach the next surface. Use the largest distance, as
968
      // only this will cross all outer surfaces.
969
      int k_max {0};
92,966,626✔
970
      for (int k = 0; k < n; ++k) {
369,525,118✔
971
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
370,867,692✔
972
            (distances[k].distance > traveled_distance)) {
94,309,200✔
973
          traveled_distance = distances[k].distance;
93,523,440✔
974
          k_max = k;
93,523,440✔
975
        }
976
      }
977

978
      // If r1 is not inside the mesh, exit here
979
      if (traveled_distance >= total_distance)
92,966,626✔
980
        return;
87,334,582✔
981

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

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

250,709,558✔
997
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
998
  vector<int>& bins, vector<double>& lengths) const
999
{
1000

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

1016
    const StructuredMesh* mesh;
250,709,558✔
1017
    vector<int>& bins;
1018
    vector<double>& lengths;
1019
  };
1020

250,709,558✔
1021
  // Perform the mesh raytrace with the helper class.
1022
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1023
}
1024

250,709,558✔
1025
void StructuredMesh::surface_bins_crossed(
×
1026
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
1027
{
1028

×
1029
  // Helper tally class.
1030
  // stores a pointer to the mesh class and a reference to the bins parameter.
1031
  // Performs the actual tally through the surface method.
1032
  struct SurfaceAggregator {
1033
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
1034
      : mesh(_mesh), bins(_bins)
250,709,558✔
1035
    {}
250,709,558✔
1036
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
1037
    {
1038
      int i_bin =
501,419,116✔
1039
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,001,002,088✔
1040
      if (max)
750,292,530✔
1041
        i_bin += 2;
1042
      if (inward)
1043
        i_bin += 1;
1044
      bins.push_back(i_bin);
61,870,061✔
1045
    }
1046
    void track(const MeshIndex& idx, double l) const {}
312,579,619✔
1047

1048
    const StructuredMesh* mesh;
1049
    vector<int>& bins;
309,532,184✔
1050
  };
309,532,184✔
1051

1052
  // Perform the mesh raytrace with the helper class.
1053
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
309,532,184✔
1054
}
309,532,184✔
1055

1056
//==============================================================================
1057
// RegularMesh implementation
1058
//==============================================================================
309,532,184✔
1059

309,532,184✔
1060
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
247,905,711✔
1061
{
1062
  // Determine number of dimensions for mesh
1063
  if (!check_for_node(node, "dimension")) {
1064
    fatal_error("Must specify <dimension> on a regular mesh.");
61,626,473✔
1065
  }
1066

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

61,626,473✔
1074
  // Check that dimensions are all greater than zero
1075
  if (xt::any(shape <= 0)) {
1076
    fatal_error("All entries on the <dimension> element for a tally "
1077
                "mesh must be positive.");
61,626,473✔
1078
  }
59,328,330✔
1079

1080
  // Check for lower-left coordinates
1081
  if (check_for_node(node, "lower_left")) {
1082
    // Read mesh lower-left corner location
1083
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1084
  } else {
1085
    fatal_error("Must specify <lower_left> on a mesh.");
3,047,435✔
1086
  }
11,838,440✔
1087

11,994,992✔
1088
  // Make sure lower_left and dimension match
3,203,987✔
1089
  if (shape.size() != lower_left_.size()) {
3,139,523✔
1090
    fatal_error("Number of entries on <lower_left> must be the same "
3,139,523✔
1091
                "as the number of entries on <dimension>.");
1092
  }
1093

1094
  if (check_for_node(node, "width")) {
1095
    // Make sure one of upper-right or width were specified
3,047,435✔
1096
    if (check_for_node(node, "upper_right")) {
2,803,847✔
1097
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1098
    }
1099

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

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

218,592✔
1109
    // Check for negative widths
1110
    if (xt::any(width_ < 0.0)) {
1111
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1112
    }
456,899,837✔
1113

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

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

1120
    // Check to ensure width has same dimensions
1121
    auto n = upper_right_.size();
1122
    if (n != lower_left_.size()) {
456,899,837✔
1123
      fatal_error("Number of entries on <upper_right> must be the "
456,899,837✔
1124
                  "same as the number of entries on <lower_left>.");
9,891,964✔
1125
    }
1126

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

447,007,873✔
1133
    // Set width
1134
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1135
  } else {
1136
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
447,007,873✔
1137
  }
1138

1139
  // Set material volumes
1140
  volume_frac_ = 1.0 / xt::prod(shape)();
447,007,873✔
1141

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

1148
int RegularMesh::get_index_in_direction(double r, int i) const
1149
{
1150
  return std::ceil((r - lower_left_[i]) / width_[i]);
446,966,389✔
1151
}
446,966,389✔
1152

1153
const std::string RegularMesh::mesh_type = "regular";
1154

893,932,778✔
1155
std::string RegularMesh::get_mesh_type() const
1,771,669,660✔
1156
{
1,324,703,271✔
1157
  return mesh_type;
1158
}
1159

1160
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
300,923,237✔
1161
{
1162
  return lower_left_[i] + ijk[i] * width_[i];
747,889,626✔
1163
}
1164

1165
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
657,970,435✔
1166
{
657,970,435✔
1167
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1168
}
1169

657,970,435✔
1170
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
657,970,435✔
1171
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1172
  double l) const
1173
{
1174
  MeshDistance d;
657,970,435✔
1175
  d.next_index = ijk[i];
657,970,435✔
1176
  if (std::abs(u[i]) < FP_PRECISION)
362,435,654✔
1177
    return d;
1178

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

295,534,781✔
1190
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1191
  Position plot_ll, Position plot_ur) const
1192
{
1193
  // Figure out which axes lie in the plane of the plot.
295,534,781✔
1194
  array<int, 2> axes {-1, -1};
272,302,364✔
1195
  if (plot_ur.z == plot_ll.z) {
1196
    axes[0] = 0;
1197
    if (n_dimension_ > 1)
1198
      axes[1] = 1;
1199
  } else if (plot_ur.y == plot_ll.y) {
1200
    axes[0] = 0;
1201
    if (n_dimension_ > 2)
89,919,191✔
1202
      axes[1] = 2;
357,686,678✔
1203
  } else if (plot_ur.x == plot_ll.x) {
358,872,700✔
1204
    if (n_dimension_ > 1)
91,105,213✔
1205
      axes[0] = 1;
90,383,917✔
1206
    if (n_dimension_ > 2)
90,383,917✔
1207
      axes[1] = 2;
1208
  } else {
1209
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1210
  }
1211

89,919,191✔
1212
  // Get the coordinates of the mesh lines along both of the axes.
84,530,735✔
1213
  array<vector<double>, 2> axis_lines;
1214
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1215
    int axis = axes[i_ax];
1216
    if (axis == -1)
5,388,456✔
1217
      continue;
21,438,660✔
1218
    auto& lines {axis_lines[i_ax]};
16,050,204✔
1219

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

1228
  return {axis_lines[0], axis_lines[1]};
1229
}
456,899,837✔
1230

1231
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1232
{
1233
  write_dataset(mesh_group, "dimension", get_x_shape());
1234
  write_dataset(mesh_group, "lower_left", lower_left_);
1235
  write_dataset(mesh_group, "upper_right", upper_right_);
1236
  write_dataset(mesh_group, "width", width_);
1237
}
456,899,837✔
1238

1239
xt::xtensor<double, 1> RegularMesh::count_sites(
456,899,837✔
1240
  const SourceSite* bank, int64_t length, bool* outside) const
456,899,837✔
1241
{
571,873,537✔
1242
  // Determine shape of array for counts
658,011,907✔
1243
  std::size_t m = this->n_bins();
1244
  vector<std::size_t> shape = {m};
658,011,907✔
1245

658,011,907✔
1246
  // Create array of zeros
658,011,907✔
1247
  xt::xarray<double> cnt {shape, 0.0};
1248
  bool outside_ = false;
1249

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

1253
    // determine scoring bin for entropy mesh
1254
    int mesh_bin = get_bin(site.r);
456,899,837✔
1255

456,899,837✔
1256
    // if outside mesh, skip particle
1257
    if (mesh_bin < 0) {
250,709,558✔
1258
      outside_ = true;
1259
      continue;
1260
    }
1261

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

250,709,558✔
1266
  // Create copy of count data. Since ownership will be acquired by xtensor,
250,709,558✔
1267
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
250,709,558✔
1268
  // warnings.
121,173,395✔
1269
  int total = cnt.size();
1270
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
1271

121,173,395✔
1272
#ifdef OPENMC_MPI
121,173,395✔
1273
  // collect values from all processors
60,541,998✔
1274
  MPI_Reduce(
121,173,395✔
1275
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
59,546,922✔
1276

121,173,395✔
1277
  // Check if there were sites outside the mesh for any processor
121,173,395✔
1278
  if (outside) {
309,532,184✔
1279
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1280
  }
1281
#else
1282
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1283
  if (outside)
1284
    *outside = outside_;
1285
#endif
250,709,558✔
1286

250,709,558✔
1287
  // Adapt reduced values in array back into an xarray
1288
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1289
  xt::xarray<double> counts = arr;
1290

1291
  return counts;
1292
}
2,001✔
1293

1294
double RegularMesh::volume(const MeshIndex& ijk) const
1295
{
2,001✔
1296
  return element_volume_;
×
1297
}
1298

1299
//==============================================================================
2,001✔
1300
// RectilinearMesh implementation
2,001✔
1301
//==============================================================================
2,001✔
1302

×
1303
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1304
{
2,001✔
1305
  n_dimension_ = 3;
1306

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

1311
  if (int err = set_grid()) {
1312
    fatal_error(openmc_err_msg);
1313
  }
2,001✔
1314
}
1315

2,001✔
1316
const std::string RectilinearMesh::mesh_type = "rectilinear";
UNCOV
1317

×
1318
std::string RectilinearMesh::get_mesh_type() const
1319
{
1320
  return mesh_type;
1321
}
2,001✔
UNCOV
1322

×
1323
double RectilinearMesh::positive_grid_boundary(
1324
  const MeshIndex& ijk, int i) const
1325
{
1326
  return grid_[i][ijk[i]];
2,001✔
1327
}
1328

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

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

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

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

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

2,001✔
1375
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
7,737✔
1376
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
5,736✔
1377

1378
  return 0;
2,001✔
1379
}
1380

2,147,483,647✔
1381
int RectilinearMesh::get_index_in_direction(double r, int i) const
1382
{
2,147,483,647✔
1383
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1384
}
1385

1386
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1387
  Position plot_ll, Position plot_ur) const
4,031✔
1388
{
1389
  // Figure out which axes lie in the plane of the plot.
4,031✔
1390
  array<int, 2> axes {-1, -1};
1391
  if (plot_ur.z == plot_ll.z) {
1392
    axes = {0, 1};
938,582,646✔
1393
  } else if (plot_ur.y == plot_ll.y) {
1394
    axes = {0, 2};
938,582,646✔
1395
  } else if (plot_ur.x == plot_ll.x) {
1396
    axes = {1, 2};
1397
  } else {
870,016,994✔
1398
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1399
  }
870,016,994✔
1400

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

1,824,763,255✔
1407
    for (auto coord : grid_[axis]) {
1,824,763,255✔
1408
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,824,763,255✔
1409
        lines.push_back(coord);
1,314,072✔
1410
    }
1411
  }
1,823,449,183✔
1412

1,823,449,183✔
1413
  return {axis_lines[0], axis_lines[1]};
933,955,530✔
1414
}
933,955,530✔
1415

889,493,653✔
1416
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
865,389,878✔
1417
{
865,389,878✔
1418
  write_dataset(mesh_group, "x_grid", grid_[0]);
1419
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,823,449,183✔
1420
  write_dataset(mesh_group, "z_grid", grid_[2]);
1421
}
1422

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

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

×
1433
//==============================================================================
×
UNCOV
1434
// CylindricalMesh implementation
×
1435
//==============================================================================
×
UNCOV
1436

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

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

48✔
1451
const std::string CylindricalMesh::mesh_type = "cylindrical";
1452

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

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

1463
  Position mapped_r;
2,284✔
1464
  mapped_r[0] = std::hypot(r.x, r.y);
1465
  mapped_r[2] = r[2];
2,284✔
1466

2,284✔
1467
  if (mapped_r[0] < FP_PRECISION) {
2,284✔
1468
    mapped_r[1] = 0.0;
2,284✔
1469
  } else {
2,284✔
1470
    mapped_r[1] = std::atan2(r.y, r.x);
1471
    if (mapped_r[1] < 0)
12,248✔
1472
      mapped_r[1] += 2 * M_PI;
1473
  }
1474

1475
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,248✔
1476

12,248✔
1477
  idx[1] = sanitize_phi(idx[1]);
1478

1479
  return idx;
12,248✔
1480
}
12,248✔
1481

1482
Position CylindricalMesh::sample_element(
12,019,257✔
1483
  const MeshIndex& ijk, uint64_t* seed) const
12,007,009✔
1484
{
1485
  double r_min = this->r(ijk[0] - 1);
1486
  double r_max = this->r(ijk[0]);
12,007,009✔
1487

1488
  double phi_min = this->phi(ijk[1] - 1);
1489
  double phi_max = this->phi(ijk[1]);
12,007,009✔
UNCOV
1490

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

1494
  double r_min_sq = r_min * r_min;
1495
  double r_max_sq = r_max * r_max;
12,007,009✔
1496
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1497
  double phi = uniform_distribution(phi_min, phi_max, seed);
1498
  double z = uniform_distribution(z_min, z_max, seed);
1499

1500
  double x = r * std::cos(phi);
1501
  double y = r * std::sin(phi);
12,248✔
1502

12,248✔
1503
  return origin_ + Position(x, y, z);
1504
}
1505

1506
double CylindricalMesh::find_r_crossing(
7,320✔
1507
  const Position& r, const Direction& u, double l, int shell) const
7,320✔
1508
{
1509

1510
  if ((shell < 0) || (shell > shape_[0]))
7,320✔
1511
    return INFTY;
7,320✔
1512

1513
  // solve r.x^2 + r.y^2 == r0^2
1514
  // 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✔
1515
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,928✔
1516

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

12,248✔
1521
  const double denominator = u.x * u.x + u.y * u.y;
12,248✔
1522

1523
  // Direction of flight is in z-direction. Will never intersect r.
24,496✔
1524
  if (std::abs(denominator) < FP_PRECISION)
12,248✔
1525
    return INFTY;
1526

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

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

1534
  if (D < 0.0)
1535
    return INFTY;
111✔
1536

1537
  D = std::sqrt(D);
111✔
1538

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

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

1548
  return INFTY;
1549
}
1550

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

1558
  shell = sanitize_phi(shell);
55,925,134✔
1559

1560
  const double p0 = grid_[1][shell];
1561

55,078,367✔
1562
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1563
  // => x(s) * cos(p0) = y(s) * sin(p0)
1564
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
55,078,367✔
1565
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1566

1567
  const double c0 = std::cos(p0);
112,648,356✔
1568
  const double s0 = std::sin(p0);
1569

1570
  const double denominator = (u.x * s0 - u.y * c0);
1571

112,648,356✔
1572
  // Check if direction of flight is not parallel to phi surface
112,648,356✔
1573
  if (std::abs(denominator) > FP_PRECISION) {
112,648,356✔
1574
    const double s = -(r.x * s0 - r.y * c0) / denominator;
623,808✔
1575
    // Check if solution is in positive direction of flight and crosses the
1576
    // correct phi surface (not -phi)
112,024,548✔
1577
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
112,024,548✔
1578
      return s;
55,925,134✔
1579
  }
55,925,134✔
1580

56,099,414✔
1581
  return INFTY;
55,078,367✔
1582
}
55,078,367✔
1583

1584
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
112,024,548✔
1585
  const Position& r, const Direction& u, double l, int shell) const
1586
{
1587
  MeshDistance d;
185✔
1588
  d.next_index = shell;
1589

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

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

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

185✔
1611
  if (i == 0) {
1612

1613
    return std::min(
161,323,602✔
1614
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1615
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
161,323,602✔
1616

1617
  } else if (i == 1) {
1618

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

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

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

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

1662
    return OPENMC_E_INVALID_ARGUMENT;
144✔
1663
  }
1664

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

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

1672
  return 0;
413✔
1673
}
413✔
1674

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

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

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

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

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

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

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

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

50,568,432✔
1712
//==============================================================================
1713
// SphericalMesh implementation
1714
//==============================================================================
96,000✔
1715

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

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

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

96,000✔
1731
const std::string SphericalMesh::mesh_type = "spherical";
1732

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

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

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

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

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

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

123,159,468✔
1761
  return idx;
1762
}
123,159,468✔
1763

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

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

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

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

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

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

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

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

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

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

1818
  return INFTY;
1819
}
39,992,448✔
1820

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

38,772,576✔
1828
  shell = sanitize_theta(shell);
17,769,456✔
1829

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

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

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

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

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

1859
    // no crossing is possible
1860
    return INFTY;
1861
  }
437✔
1862

1863
  const double p = b / a;
437✔
1864
  double D = p * p - c / a;
437✔
1865

437✔
1866
  if (D < 0.0)
1867
    return INFTY;
1,748✔
1868

1,311✔
UNCOV
1869
  D = std::sqrt(D);
×
1870

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

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

×
1882
  return INFTY;
UNCOV
1883
}
×
1884

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

×
1892
  shell = sanitize_phi(shell);
1893

UNCOV
1894
  const double p0 = grid_[2][shell];
×
1895

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

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

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

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

UNCOV
1915
  return INFTY;
×
1916
}
1917

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

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

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

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

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

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

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

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

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

73,752,732✔
1985
  return 0;
36,882,336✔
1986
}
1987

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

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

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

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

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

UNCOV
2016
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
2017
  double theta_o = grid_[1][ijk[1]];
×
UNCOV
2018

×
2019
  double phi_i = grid_[2][ijk[2] - 1];
UNCOV
2020
  double phi_o = grid_[2][ijk[2]];
×
2021

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

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

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

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

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

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

2060
//==============================================================================
41,719,248✔
2061
// C API functions
2062
//==============================================================================
2063

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

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

41,719,248✔
2072
  return 0;
2073
}
41,719,248✔
2074

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

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

41,192,832✔
2099
  return 0;
11,955,456✔
2100
}
2101

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

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

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

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

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

43,467,960✔
2135
  return 0;
2136
}
43,467,960✔
2137

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

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

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

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

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

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

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

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

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

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

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

2227
  return 0;
UNCOV
2228
}
×
2229

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

2237
  int pixel_width = pixels[0];
312✔
2238
  int pixel_height = pixels[1];
312✔
2239

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

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

×
UNCOV
2266
  // set initial position
×
2267
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2268
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
9,424✔
2269

2270
#pragma omp parallel
2271
  {
2272
    Position r = xyz;
1,759✔
2273

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

2284
  return 0;
156✔
2285
}
2286

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

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

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

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

1,191✔
2321
  if (m->lower_left_.dimension() == 0) {
2322
    set_errmsg("Mesh parameters have not been set.");
1,191✔
UNCOV
2323
    return OPENMC_E_ALLOCATE;
×
2324
  }
2325

1,191✔
2326
  *ll = m->lower_left_.data();
1,191✔
UNCOV
2327
  *ur = m->upper_right_.data();
×
UNCOV
2328
  *width = m->width_.data();
×
2329
  *n = m->n_dimension_;
2330
  return 0;
1,191✔
2331
}
2332

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

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

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

942✔
2364
  // Set material volumes
471✔
2365

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

×
2374
  return 0;
2375
}
2376

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

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

×
UNCOV
2388
  m->n_dimension_ = 3;
×
2389

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

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

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

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

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

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

2429
  return 0;
2430
}
2431

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

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

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

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

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

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

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

144✔
2485
#ifdef DAGMC
144✔
2486

144✔
2487
const std::string MOABMesh::mesh_lib_type = "moab";
2488

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

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

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

144✔
2508
void MOABMesh::initialize()
2509
{
2510

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

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

2517
  // Set the dimension
48✔
2518
  n_dimension_ = 3;
48✔
2519

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

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

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

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

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

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

2579
  // Determine bounds of mesh
2580
  this->determine_bounds();
373✔
2581
}
2582

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

373✔
2589
  // build acceleration data structures
373✔
2590
  compute_barycentric_data(ehs_);
373✔
2591
  build_kdtree(ehs_);
2592
}
2593

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

397✔
2600
  // create MOAB instance
2601
  mbi_ = std::make_shared<moab::Core>();
397✔
UNCOV
2602

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

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

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

409✔
2627
  // combine into one range
409✔
2628
  moab::Range all_tets_and_tris;
385✔
2629
  all_tets_and_tris.merge(all_tets);
385✔
2630
  all_tets_and_tris.merge(all_tris);
385✔
2631

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

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

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

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

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

122✔
2672
  // remove duplicate intersection distances
122✔
2673
  std::unique(hits.begin(), hits.end());
2674

988✔
2675
  // sorts by first component of std::pair by default
866✔
2676
  std::sort(hits.begin(), hits.end());
2677
}
450✔
2678

328✔
2679
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2680
  vector<int>& bins, vector<double>& lengths) const
426✔
2681
{
304✔
2682
  moab::CartVect start(r0.x, r0.y, r0.z);
2683
  moab::CartVect end(r1.x, r1.y, r1.z);
2684
  moab::CartVect dir(u.x, u.y, u.z);
122✔
2685
  dir.normalize();
122✔
2686

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

2691
  start -= TINY_BIT * dir;
24✔
UNCOV
2692
  end += TINY_BIT * dir;
×
2693

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

24✔
2697
  bins.clear();
2698
  lengths.clear();
24✔
2699

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

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

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

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

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

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

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

246✔
2762
  // retrieve the tet elements of this leaf
172✔
2763
  moab::EntityHandle leaf = kdtree_iter.handle();
2764
  moab::Range tets;
234✔
2765
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
160✔
2766
  if (rval != moab::MB_SUCCESS) {
2767
    warning("MOAB error finding tets.");
2768
  }
74✔
2769

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

2777
  // if no tet is found, return an invalid handle
446✔
2778
  return 0;
×
2779
}
446✔
2780

2781
double MOABMesh::volume(int bin) const
446✔
UNCOV
2782
{
×
UNCOV
2783
  return tet_volume(get_ent_handle_from_bin(bin));
×
2784
}
2785

2786
std::string MOABMesh::library() const
446✔
2787
{
446✔
2788
  return mesh_lib_type;
446✔
2789
}
446✔
2790

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

2795
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2796

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

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

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

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

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

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

×
2847
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2848
{
2849
  moab::ErrorCode rval;
182✔
2850

182✔
2851
  baryc_data_.clear();
182✔
2852
  baryc_data_.resize(tets.size());
182✔
2853

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

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

74✔
2869
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2870

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

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

132✔
2881
  moab::ErrorCode rval;
132✔
2882

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

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

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

2906
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2907

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

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

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

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

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

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

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

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

2967
int MOABMesh::n_bins() const
2968
{
23✔
2969
  return ehs_.size();
23✔
2970
}
2971

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

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

2989
  auto tet = this->get_ent_handle_from_bin(bin);
2990

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

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

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

19✔
3014
  return {centroid[0], centroid[1], centroid[2]};
3015
}
3016

3017
int MOABMesh::n_vertices() const
19✔
3018
{
19✔
3019
  return verts_.size();
3020
}
3021

23✔
3022
Position MOABMesh::vertex(int id) const
3023
{
3024

23✔
3025
  moab::ErrorCode rval;
1✔
3026

3027
  moab::EntityHandle vert = verts_[id];
3028

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

3035
  return {coords[0], coords[1], coords[2]};
3036
}
3037

19✔
3038
std::vector<int> MOABMesh::connectivity(int bin) const
3039
{
19✔
3040
  moab::ErrorCode rval;
19✔
3041

19✔
3042
  auto tet = get_ent_handle_from_bin(bin);
19✔
3043

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

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

19✔
3057
  return verts;
19✔
3058
}
3059

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

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

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

1,519,602✔
3095
  // return the populated tag handles
3096
  return {value_tag, error_tag};
3097
}
3098

3099
void MOABMesh::add_score(const std::string& score)
3100
{
1,519,602✔
3101
  auto score_tags = get_score_tags(score);
3102
  tag_names_.push_back(score);
3103
}
1,519,602✔
3104

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

3114
    rval = mbi_->tag_delete(tag);
1,519,602✔
3115
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
3116
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3117
                             " on unstructured mesh {}",
3118
        name, id_);
1,519,602✔
3119
      fatal_error(msg);
1,519,602✔
3120
    }
3121

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

1,519,602✔
3131
    rval = mbi_->tag_delete(tag);
701,483✔
3132
    if (rval != moab::MB_SUCCESS) {
701,483✔
3133
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3134
                             " on unstructured mesh {}",
223,515✔
3135
        name, id_);
223,515✔
3136
      fatal_error(msg);
3137
    }
701,483✔
3138
  }
3139
  tag_names_.clear();
3140
}
3141

3142
void MOABMesh::set_score_data(const std::string& score,
818,119✔
3143
  const vector<double>& values, const vector<double>& std_dev)
818,119✔
3144
{
5,506,248✔
3145
  auto score_tags = this->get_score_tags(score);
3146

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

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

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

762,819✔
3174
  // write the tetrahedral elements of the mesh only
762,819✔
3175
  // to avoid clutter from zero-value data on other
3176
  // elements during visualization
3177
  moab::ErrorCode rval;
1,519,602✔
3178
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
3179
  if (rval != moab::MB_SUCCESS) {
7,279,728✔
3180
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
3181
    warning(msg);
7,279,728✔
3182
  }
3183
}
7,279,728✔
3184

7,279,728✔
3185
#endif
7,279,728✔
3186

1,010,077✔
3187
#ifdef LIBMESH
3188

3189
const std::string LibMesh::mesh_lib_type = "libmesh";
3190

6,269,651✔
3191
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,269,651✔
3192
{
6,269,651✔
3193
  // filename_ and length_multiplier_ will already be set by the
6,269,651✔
3194
  // UnstructuredMesh constructor
3195
  set_mesh_pointer_from_filename(filename_);
3196
  set_length_multiplier(length_multiplier_);
3197
  initialize();
3198
}
259,367,091✔
3199

259,364,245✔
3200
// create the mesh from a pointer to a libMesh Mesh
6,266,805✔
3201
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
3202
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
3203
{
3204
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
3205
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
2,846✔
3206
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
7,279,728✔
3207
  }
3208

155,856✔
3209
  m_ = &input_mesh;
3210
  set_length_multiplier(length_multiplier);
155,856✔
3211
  initialize();
3212
}
3213

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

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

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

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

3249
  // assuming that unstructured meshes used in OpenMC are 3D
155,856✔
3250
  n_dimension_ = 3;
155,856✔
3251

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

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

7,279,728✔
3268
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3269
    pl_.emplace_back(m_->sub_point_locator());
3270
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,266,805✔
3271
    pl_.back()->enable_out_of_mesh_mode();
3272
  }
3273

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

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

3288
      bin_to_elem_map_.push_back(elem->id());
3289
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3290
    }
1,138,560✔
3291
  }
227,712✔
3292

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

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

3315
Position LibMesh::centroid(int bin) const
3316
{
3317
  const auto& elem = this->get_element_from_bin(bin);
3318
  auto centroid = elem.vertex_average();
3319
  return {centroid(0), centroid(1), centroid(2)};
3320
}
259,364,245✔
3321

259,364,245✔
3322
int LibMesh::n_vertices() const
259,364,245✔
3323
{
3324
  return m_->n_nodes();
3325
}
3326

3327
Position LibMesh::vertex(int vertex_id) const
3328
{
3329
  const auto node_ref = m_->node_ref(vertex_id);
3330
  return {node_ref(0), node_ref(1), node_ref(2)};
259,364,245✔
3331
}
259,364,245✔
3332

3333
std::vector<int> LibMesh::connectivity(int elem_id) const
259,364,245✔
3334
{
3335
  std::vector<int> conn;
420,046,390✔
3336
  const auto* elem_ptr = m_->elem_ptr(elem_id);
441,624,143✔
3337
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
280,941,998✔
3338
    conn.push_back(elem_ptr->node_id(i));
259,364,245✔
3339
  }
3340
  return conn;
3341
}
3342

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

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

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

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

265,858,762✔
3378
    return;
3379
  }
265,858,762✔
3380

265,858,762✔
3381
  if (!equation_systems_) {
3382
    build_eqn_sys();
3383
  }
265,858,762✔
3384

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

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

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

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

3421
    return;
3422
  }
3423

3424
  if (!equation_systems_) {
3425
    build_eqn_sys();
3426
  }
3427

3428
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3429

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

3434
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3435

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

3443
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3444
       it++) {
795,427✔
3445
    if (!(*it)->active()) {
3446
      continue;
795,427✔
3447
    }
3448

3449
    auto bin = get_bin_from_element(*it);
81,537✔
3450

3451
    // set value
3452
    vector<libMesh::dof_id_type> value_dof_indices;
3453
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3454
    Ensures(value_dof_indices.size() == 1);
81,537✔
3455
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3456

81,537✔
3457
    // set std dev
81,537✔
3458
    vector<libMesh::dof_id_type> std_dev_dof_indices;
81,537✔
3459
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3460
    Ensures(std_dev_dof_indices.size() == 1);
3461
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3462
  }
163,074✔
3463
}
3464

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

3472
    return;
191,856✔
3473
  }
191,856✔
3474

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

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

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

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

3500
  const auto& point_locator = pl_.at(thread_num());
3501

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

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

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

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

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

3532
#endif // LIBMESH
3533

3534
//==============================================================================
3535
// Non-member functions
3536
//==============================================================================
3537

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

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

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

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

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

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

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

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

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

3620
  close_group(meshes_group);
3621
}
3622

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

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

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

© 2026 Coveralls, Inc