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

openmc-dev / openmc / 15618759193

12 Jun 2025 06:59PM UTC coverage: 85.165% (+0.007%) from 85.158%
15618759193

Pull #3436

github

web-flow
Merge f2964f7dc into 6c9c69628
Pull Request #3436: Allowing chain_file to be chain object to save reloading time

18 of 22 new or added lines in 4 files covered. (81.82%)

374 existing lines in 7 files now uncovered.

52385 of 61510 relevant lines covered (85.17%)

36774825.05 hits per line

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

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

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

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

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

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

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

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

60
namespace openmc {
61

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

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

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

76
namespace model {
77

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

81
} // namespace model
82

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

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

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

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

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

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

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

142
namespace detail {
143

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

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

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

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

168
    // Found the desired material; accumulate volume
169
    if (current_val == index_material) {
2,416,997✔
170
#pragma omp atomic
1,318,003✔
171
      this->volumes(index_elem, slot) += volume;
2,415,650✔
172
      return;
2,415,650✔
173
    }
174

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

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

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

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

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

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

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

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

227
} // namespace detail
228

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

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

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

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

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

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

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

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

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

296
  Timer timer;
154✔
297
  timer.start();
154✔
298

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

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

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

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

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

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

322
    SourceSite site;
70✔
323
    site.E = 1.0;
70✔
324
    site.particle = ParticleType::neutron;
70✔
325

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

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

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

354
      // Loop over rays on face of bounding box
355
#pragma omp for collapse(2)
356
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
8,395✔
357
        for (int i2 = 0; i2 < n2; ++i2) {
470,510✔
358
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
462,275✔
359
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
462,275✔
360

361
          p.from_source(&site);
462,275✔
362

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

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

373
          // Initialize last cells from current cell
374
          for (int j = 0; j < p.n_coord(); ++j) {
844,690✔
375
            p.cell_last(j) = p.coord(j).cell;
422,345✔
376
          }
377
          p.n_coord_last() = p.n_coord();
422,345✔
378

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

384
            // Find the distance to the nearest boundary
385
            BoundaryInfo boundary = distance_to_boundary(p);
869,285✔
386

387
            // Advance particle forward
388
            double distance = std::min(boundary.distance, max_distance);
869,285✔
389
            p.move_distance(distance);
869,285✔
390

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

396
            // Add volumes to any mesh elements that were crossed
397
            int i_material = p.material();
869,285✔
398
            if (i_material != C_NONE) {
869,285✔
399
              i_material = model::materials[i_material]->id();
840,855✔
400
            }
401
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
1,967,920✔
402
              int mesh_index = bins[i_bin];
1,098,635✔
403
              double length = distance * length_fractions[i_bin];
1,098,635✔
404

405
              // Add volume to result
406
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,098,635✔
407
            }
408

409
            if (distance == max_distance)
869,285✔
410
              break;
422,345✔
411

412
            // cross next geometric surface
413
            for (int j = 0; j < p.n_coord(); ++j) {
893,880✔
414
              p.cell_last(j) = p.coord(j).cell;
446,940✔
415
            }
416
            p.n_coord_last() = p.n_coord();
446,940✔
417

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

542
  // Close group
543
  close_group(mesh_group);
2,697✔
544
}
2,697✔
545

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

550
std::string StructuredMesh::bin_label(int bin) const
5,121,256✔
551
{
552
  MeshIndex ijk = get_indices_from_bin(bin);
5,121,256✔
553

554
  if (n_dimension_ > 2) {
5,121,256✔
555
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,212,680✔
556
  } else if (n_dimension_ > 1) {
14,916✔
557
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
29,282✔
558
  } else {
559
    return fmt::format("Mesh Index ({})", ijk[0]);
550✔
560
  }
561
}
562

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

717
  int num_elem_skipped = 0;
31✔
718

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

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

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

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

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

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

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

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

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

786
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
787
      in_mesh = false;
99,949,084✔
788
  }
789
  return ijk;
1,160,262,635✔
790
}
791

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

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

822
int StructuredMesh::get_bin(Position r) const
248,048,863✔
823
{
824
  // Determine indices
825
  bool in_mesh;
826
  MeshIndex ijk = get_indices(r, in_mesh);
248,048,863✔
827
  if (!in_mesh)
248,048,863✔
828
    return -1;
20,441,094✔
829

830
  // Convert indices to bin
831
  return get_bin_from_indices(ijk);
227,607,769✔
832
}
833

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

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

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

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

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

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

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

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

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

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

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

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

897
  return counts;
×
898
}
899

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

913
  // Compute the length of the entire track.
914
  double total_distance = (r1 - r0).norm();
914,717,945✔
915
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
914,717,945✔
916
    return;
8,720,436✔
917

918
  // keep a copy of the original global position to pass to get_indices,
919
  // which performs its own transformation to local coordinates
920
  Position global_r = r0;
905,997,509✔
921
  Position local_r = local_coords(r0);
905,997,509✔
922

923
  const int n = n_dimension_;
905,997,509✔
924

925
  // Flag if position is inside the mesh
926
  bool in_mesh;
927

928
  // Position is r = r0 + u * traveled_distance, start at r0
929
  double traveled_distance {0.0};
905,997,509✔
930

931
  // Calculate index of current cell. Offset the position a tiny bit in
932
  // direction of flight
933
  MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
905,997,509✔
934

935
  // if track is very short, assume that it is completely inside one cell.
936
  // Only the current cell will score and no surfaces
937
  if (total_distance < 2 * TINY_BIT) {
905,997,509✔
938
    if (in_mesh) {
646,283✔
939
      tally.track(ijk, 1.0);
646,272✔
940
    }
941
    return;
646,283✔
942
  }
943

944
  // Calculate initial distances to next surfaces in all three dimensions
945
  std::array<MeshDistance, 3> distances;
1,810,702,452✔
946
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
947
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
2,147,483,647✔
948
  }
949

950
  // Loop until r = r1 is eventually reached
951
  while (true) {
743,483,430✔
952

953
    if (in_mesh) {
1,648,834,656✔
954

955
      // find surface with minimal distance to current position
956
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,562,527,533✔
957
                     distances.begin();
1,562,527,533✔
958

959
      // Tally track length delta since last step
960
      tally.track(ijk,
1,562,527,533✔
961
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
1,562,527,533✔
962
          total_distance);
963

964
      // update position and leave, if we have reached end position
965
      traveled_distance = distances[k].distance;
1,562,527,533✔
966
      if (traveled_distance >= total_distance)
1,562,527,533✔
967
        return;
825,260,366✔
968

969
      // If we have not reached r1, we have hit a surface. Tally outward
970
      // current
971
      tally.surface(ijk, k, distances[k].max_surface, false);
737,267,167✔
972

973
      // Update cell and calculate distance to next surface in k-direction.
974
      // The two other directions are still valid!
975
      ijk[k] = distances[k].next_index;
737,267,167✔
976
      distances[k] =
737,267,167✔
977
        distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
737,267,167✔
978

979
      // Check if we have left the interior of the mesh
980
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
737,267,167✔
981

982
      // If we are still inside the mesh, tally inward current for the next
983
      // cell
984
      if (in_mesh)
737,267,167✔
985
        tally.surface(ijk, k, !distances[k].max_surface, true);
721,760,374✔
986

987
    } else { // not inside mesh
988

989
      // For all directions outside the mesh, find the distance that we need
990
      // to travel to reach the next surface. Use the largest distance, as
991
      // only this will cross all outer surfaces.
992
      int k_max {-1};
86,307,123✔
993
      for (int k = 0; k < n; ++k) {
343,082,236✔
994
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
351,026,674✔
995
            (distances[k].distance > traveled_distance)) {
94,251,561✔
996
          traveled_distance = distances[k].distance;
89,304,017✔
997
          k_max = k;
89,304,017✔
998
        }
999
      }
1000
      // Assure some distance is traveled
1001
      if (k_max == -1) {
86,307,123✔
1002
        traveled_distance += TINY_BIT;
110✔
1003
      }
1004

1005
      // If r1 is not inside the mesh, exit here
1006
      if (traveled_distance >= total_distance)
86,307,123✔
1007
        return;
80,090,860✔
1008

1009
      // Calculate the new cell index and update all distances to next
1010
      // surfaces.
1011
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,216,263✔
1012
      for (int k = 0; k < n; ++k) {
24,654,897✔
1013
        distances[k] =
18,438,634✔
1014
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
18,438,634✔
1015
      }
1016

1017
      // If inside the mesh, Tally inward current
1018
      if (in_mesh && k_max >= 0)
6,216,263✔
1019
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,003,204✔
1020
    }
1021
  }
1022
}
1023

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

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

131,513,308✔
1043
    const StructuredMesh* mesh;
1044
    vector<int>& bins;
1045
    vector<double>& lengths;
1046
  };
1047

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

1052
void StructuredMesh::surface_bins_crossed(
131,513,308✔
1053
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1054
{
1055

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

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

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

1083
//==============================================================================
1084
// RegularMesh implementation
163,756,601✔
1085
//==============================================================================
163,756,601✔
1086

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

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

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

1107
  // Check for lower-left coordinates
1108
  if (check_for_node(node, "lower_left")) {
1109
    // Read mesh lower-left corner location
1110
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1111
  } else {
2,041,870✔
1112
    fatal_error("Must specify <lower_left> on a mesh.");
7,845,455✔
1113
  }
7,988,961✔
1114

2,185,376✔
1115
  // Make sure lower_left and dimension match
2,126,174✔
1116
  if (shape.size() != lower_left_.size()) {
2,126,174✔
1117
    fatal_error("Number of entries on <lower_left> must be the same "
1118
                "as the number of entries on <dimension>.");
1119
  }
1120

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

1,818,471✔
1127
    width_ = get_node_xarray<double>(node, "width");
1128

1129
    // Check to ensure width has same dimensions
1130
    auto n = width_.size();
223,399✔
1131
    if (n != lower_left_.size()) {
789,008✔
1132
      fatal_error("Number of entries on <width> must be the same as "
565,609✔
1133
                  "the number of entries on <lower_left>.");
565,609✔
1134
    }
1135

1136
    // Check for negative widths
1137
    if (xt::any(width_ < 0.0)) {
223,399✔
1138
      fatal_error("Cannot have a negative <width> on a tally mesh.");
200,376✔
1139
    }
1140

1141
    // Set width and upper right coordinate
1142
    upper_right_ = xt::eval(lower_left_ + shape * width_);
783,204,637✔
1143

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

1147
    // Check to ensure width has same dimensions
1148
    auto n = upper_right_.size();
1149
    if (n != lower_left_.size()) {
1150
      fatal_error("Number of entries on <upper_right> must be the "
1151
                  "same as the number of entries on <lower_left>.");
1152
    }
783,204,637✔
1153

783,204,637✔
1154
    // Check that upper-right is above lower-left
8,720,436✔
1155
    if (xt::any(upper_right_ < lower_left_)) {
1156
      fatal_error("The <upper_right> coordinates must be greater than "
1157
                  "the <lower_left> coordinates on a tally mesh.");
1158
    }
774,484,201✔
1159

774,484,201✔
1160
    // Set width
1161
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
774,484,201✔
1162
  } else {
1163
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
1164
  }
1165

1166
  // Set material volumes
1167
  volume_frac_ = 1.0 / xt::prod(shape)();
774,484,201✔
1168

1169
  element_volume_ = 1.0;
1170
  for (int i = 0; i < n_dimension_; i++) {
1171
    element_volume_ *= width_[i];
774,484,201✔
1172
  }
1173
}
1174

1175
int RegularMesh::get_index_in_direction(double r, int i) const
774,484,201✔
1176
{
646,283✔
1177
  return std::ceil((r - lower_left_[i]) / width_[i]);
646,272✔
1178
}
1179

646,283✔
1180
const std::string RegularMesh::mesh_type = "regular";
1181

1182
std::string RegularMesh::get_mesh_type() const
1183
{
1,547,675,836✔
1184
  return mesh_type;
2,147,483,647✔
1185
}
2,147,483,647✔
1186

1187
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1188
{
1189
  return lower_left_[i] + ijk[i] * width_[i];
709,198,267✔
1190
}
1191

1,483,036,185✔
1192
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1193
{
1194
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,398,770,932✔
1195
}
1,398,770,932✔
1196

1197
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
1198
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1,398,770,932✔
1199
  double l) const
1,398,770,932✔
1200
{
1201
  MeshDistance d;
1202
  d.next_index = ijk[i];
1203
  if (std::abs(u[i]) < FP_PRECISION)
1,398,770,932✔
1204
    return d;
1,398,770,932✔
1205

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

703,205,403✔
1215
  return d;
703,205,403✔
1216
}
1217

1218
std::pair<vector<double>, vector<double>> RegularMesh::plot(
703,205,403✔
1219
  Position plot_ll, Position plot_ur) const
1220
{
1221
  // Figure out which axes lie in the plane of the plot.
1222
  array<int, 2> axes {-1, -1};
703,205,403✔
1223
  if (plot_ur.z == plot_ll.z) {
689,053,409✔
1224
    axes[0] = 0;
1225
    if (n_dimension_ > 1)
1226
      axes[1] = 1;
1227
  } else if (plot_ur.y == plot_ll.y) {
1228
    axes[0] = 0;
1229
    if (n_dimension_ > 2)
1230
      axes[1] = 2;
84,265,253✔
1231
  } else if (plot_ur.x == plot_ll.x) {
335,236,781✔
1232
    if (n_dimension_ > 1)
343,037,713✔
1233
      axes[0] = 1;
92,066,185✔
1234
    if (n_dimension_ > 2)
87,177,843✔
1235
      axes[1] = 2;
87,177,843✔
1236
  } else {
1237
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1238
  }
1239

84,265,253✔
UNCOV
1240
  // Get the coordinates of the mesh lines along both of the axes.
×
1241
  array<vector<double>, 2> axis_lines;
1242
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1243
    int axis = axes[i_ax];
1244
    if (axis == -1)
84,265,253✔
1245
      continue;
78,272,389✔
1246
    auto& lines {axis_lines[i_ax]};
1247

1248
    double coord = lower_left_[axis];
1249
    for (int i = 0; i < shape_[axis] + 1; ++i) {
5,992,864✔
1250
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
23,865,889✔
1251
        lines.push_back(coord);
17,873,025✔
1252
      coord += width_[axis];
17,873,025✔
1253
    }
1254
  }
1255

1256
  return {axis_lines[0], axis_lines[1]};
5,992,864✔
1257
}
5,802,828✔
1258

1259
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1260
{
1261
  write_dataset(mesh_group, "dimension", get_x_shape());
1262
  write_dataset(mesh_group, "lower_left", lower_left_);
783,204,637✔
1263
  write_dataset(mesh_group, "upper_right", upper_right_);
1264
  write_dataset(mesh_group, "width", width_);
1265
}
1266

1267
xt::xtensor<double, 1> RegularMesh::count_sites(
1268
  const SourceSite* bank, int64_t length, bool* outside) const
1269
{
1270
  // Determine shape of array for counts
783,204,637✔
1271
  std::size_t m = this->n_bins();
1272
  vector<std::size_t> shape = {m};
783,204,637✔
1273

783,204,637✔
1274
  // Create array of zeros
1,398,061,640✔
1275
  xt::xarray<double> cnt {shape, 0.0};
1,399,417,204✔
1276
  bool outside_ = false;
1277

1,399,417,204✔
1278
  for (int64_t i = 0; i < length; i++) {
1,399,417,204✔
1279
    const auto& site = bank[i];
1,399,417,204✔
1280

1281
    // determine scoring bin for entropy mesh
1282
    int mesh_bin = get_bin(site.r);
1283

1284
    // if outside mesh, skip particle
1285
    if (mesh_bin < 0) {
1286
      outside_ = true;
1287
      continue;
783,204,637✔
1288
    }
783,204,637✔
1289

1290
    // Add to appropriate bin
131,513,308✔
1291
    cnt(mesh_bin) += site.wgt;
1292
  }
1293

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

131,513,308✔
1300
#ifdef OPENMC_MPI
131,513,308✔
1301
  // collect values from all processors
66,969,105✔
1302
  MPI_Reduce(
1303
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1304

66,969,105✔
1305
  // Check if there were sites outside the mesh for any processor
66,969,105✔
1306
  if (outside) {
33,448,809✔
1307
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
66,969,105✔
1308
  }
32,907,341✔
1309
#else
66,969,105✔
1310
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
66,969,105✔
1311
  if (outside)
163,756,601✔
1312
    *outside = outside_;
1313
#endif
1314

1315
  // Adapt reduced values in array back into an xarray
1316
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1317
  xt::xarray<double> counts = arr;
1318

131,513,308✔
1319
  return counts;
131,513,308✔
1320
}
1321

1322
double RegularMesh::volume(const MeshIndex& ijk) const
1323
{
1324
  return element_volume_;
1325
}
1,862✔
1326

1327
//==============================================================================
1328
// RectilinearMesh implementation
1,862✔
1329
//==============================================================================
×
1330

1331
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1332
{
1,862✔
1333
  n_dimension_ = 3;
1,862✔
1334

1,862✔
UNCOV
1335
  grid_[0] = get_node_array<double>(node, "x_grid");
×
1336
  grid_[1] = get_node_array<double>(node, "y_grid");
1337
  grid_[2] = get_node_array<double>(node, "z_grid");
1,862✔
1338

1339
  if (int err = set_grid()) {
1340
    fatal_error(openmc_err_msg);
1,862✔
UNCOV
1341
  }
×
1342
}
1343

1344
const std::string RectilinearMesh::mesh_type = "rectilinear";
1345

1346
std::string RectilinearMesh::get_mesh_type() const
1,862✔
1347
{
1348
  return mesh_type;
1,862✔
1349
}
1350

×
1351
double RectilinearMesh::positive_grid_boundary(
1352
  const MeshIndex& ijk, int i) const
1353
{
1354
  return grid_[i][ijk[i]];
1,862✔
UNCOV
1355
}
×
1356

1357
double RectilinearMesh::negative_grid_boundary(
1358
  const MeshIndex& ijk, int i) const
1359
{
1,862✔
1360
  return grid_[i][ijk[i] - 1];
1361
}
48✔
UNCOV
1362

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

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

1,814✔
1383
int RectilinearMesh::set_grid()
1,814✔
1384
{
1385
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1386
    static_cast<int>(grid_[1].size()) - 1,
1,814✔
1387
    static_cast<int>(grid_[2].size()) - 1};
1,814✔
UNCOV
1388

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

1403
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
1404
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
1405

1,862✔
1406
  return 0;
1407
}
1,862✔
1408

7,085✔
1409
int RectilinearMesh::get_index_in_direction(double r, int i) const
5,223✔
1410
{
1411
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1,862✔
1412
}
1413

2,147,483,647✔
1414
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1415
  Position plot_ll, Position plot_ur) const
2,147,483,647✔
1416
{
1417
  // Figure out which axes lie in the plane of the plot.
1418
  array<int, 2> axes {-1, -1};
1419
  if (plot_ur.z == plot_ll.z) {
1420
    axes = {0, 1};
3,100✔
1421
  } else if (plot_ur.y == plot_ll.y) {
1422
    axes = {0, 2};
3,100✔
1423
  } else if (plot_ur.x == plot_ll.x) {
1424
    axes = {1, 2};
1425
  } else {
1,466,018,150✔
1426
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1427
  }
1,466,018,150✔
1428

1429
  // Get the coordinates of the mesh lines along both of the axes.
1430
  array<vector<double>, 2> axis_lines;
1,403,146,633✔
1431
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1432
    int axis = axes[i_ax];
1,403,146,633✔
1433
    vector<double>& lines {axis_lines[i_ax]};
1434

1435
    for (auto coord : grid_[axis]) {
2,147,483,647✔
1436
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1437
        lines.push_back(coord);
1438
    }
1439
  }
2,147,483,647✔
1440

2,147,483,647✔
1441
  return {axis_lines[0], axis_lines[1]};
2,147,483,647✔
1442
}
1,458,182✔
1443

1444
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
2,147,483,647✔
1445
{
2,147,483,647✔
1446
  write_dataset(mesh_group, "x_grid", grid_[0]);
1,461,776,297✔
1447
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,461,776,297✔
1448
  write_dataset(mesh_group, "z_grid", grid_[2]);
1,421,601,953✔
1449
}
1,398,904,780✔
1450

1,398,904,780✔
1451
double RectilinearMesh::volume(const MeshIndex& ijk) const
1452
{
1453
  double vol {1.0};
2,147,483,647✔
1454

1455
  for (int i = 0; i < n_dimension_; i++) {
1456
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
22✔
1457
  }
1458
  return vol;
1459
}
1460

22✔
1461
//==============================================================================
22✔
1462
// CylindricalMesh implementation
22✔
1463
//==============================================================================
22✔
1464

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

×
1474
  if (int err = set_grid()) {
UNCOV
1475
    fatal_error(openmc_err_msg);
×
1476
  }
1477
}
1478

1479
const std::string CylindricalMesh::mesh_type = "cylindrical";
22✔
1480

66✔
1481
std::string CylindricalMesh::get_mesh_type() const
44✔
1482
{
44✔
UNCOV
1483
  return mesh_type;
×
1484
}
44✔
1485

1486
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
44✔
1487
  Position r, bool& in_mesh) const
286✔
1488
{
242✔
1489
  r = local_coords(r);
242✔
1490

242✔
1491
  Position mapped_r;
1492
  mapped_r[0] = std::hypot(r.x, r.y);
1493
  mapped_r[2] = r[2];
1494

44✔
1495
  if (mapped_r[0] < FP_PRECISION) {
22✔
1496
    mapped_r[1] = 0.0;
1497
  } else {
1,914✔
1498
    mapped_r[1] = std::atan2(r.y, r.x);
1499
    if (mapped_r[1] < 0)
1,914✔
1500
      mapped_r[1] += 2 * M_PI;
1,914✔
1501
  }
1,914✔
1502

1,914✔
1503
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
1,914✔
1504

1505
  idx[1] = sanitize_phi(idx[1]);
8,409✔
1506

1507
  return idx;
1508
}
1509

8,409✔
1510
Position CylindricalMesh::sample_element(
8,409✔
1511
  const MeshIndex& ijk, uint64_t* seed) const
1512
{
1513
  double r_min = this->r(ijk[0] - 1);
8,409✔
1514
  double r_max = this->r(ijk[0]);
8,409✔
1515

1516
  double phi_min = this->phi(ijk[1] - 1);
8,247,479✔
1517
  double phi_max = this->phi(ijk[1]);
8,239,070✔
1518

1519
  double z_min = this->z(ijk[2] - 1);
1520
  double z_max = this->z(ijk[2]);
8,239,070✔
1521

1522
  double r_min_sq = r_min * r_min;
1523
  double r_max_sq = r_max * r_max;
8,239,070✔
UNCOV
1524
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
×
UNCOV
1525
  double phi = uniform_distribution(phi_min, phi_max, seed);
×
1526
  double z = uniform_distribution(z_min, z_max, seed);
1527

1528
  double x = r * std::cos(phi);
1529
  double y = r * std::sin(phi);
8,239,070✔
1530

1531
  return origin_ + Position(x, y, z);
1532
}
1533

1534
double CylindricalMesh::find_r_crossing(
1535
  const Position& r, const Direction& u, double l, int shell) const
8,409✔
1536
{
8,409✔
1537

1538
  if ((shell < 0) || (shell > shape_[0]))
1539
    return INFTY;
1540

4,185✔
1541
  // solve r.x^2 + r.y^2 == r0^2
4,185✔
1542
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1543
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1544

4,185✔
1545
  const double r0 = grid_[0][shell];
4,185✔
1546
  if (r0 == 0.0)
1547
    return INFTY;
1548

4,224✔
1549
  const double denominator = u.x * u.x + u.y * u.y;
4,224✔
1550

4,224✔
1551
  // Direction of flight is in z-direction. Will never intersect r.
1552
  if (std::abs(denominator) < FP_PRECISION)
1553
    return INFTY;
1554

8,409✔
1555
  // inverse of dominator to help the compiler to speed things up
8,409✔
1556
  const double inv_denominator = 1.0 / denominator;
1557

16,818✔
1558
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
8,409✔
1559
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1560
  double D = p * p - c * inv_denominator;
7,452,165✔
1561

1562
  if (D < 0.0)
7,452,165✔
1563
    return INFTY;
1564

1565
  D = std::sqrt(D);
1566

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

1571
  if (-p - D > l)
103✔
1572
    return -p - D;
1573
  if (-p + D > l)
103✔
1574
    return -p + D;
103✔
1575

103✔
1576
  return INFTY;
1577
}
103✔
UNCOV
1578

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

1586
  shell = sanitize_phi(shell);
261✔
1587

1588
  const double p0 = grid_[1][shell];
1589

30,573,125✔
1590
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1591
  // => x(s) * cos(p0) = y(s) * sin(p0)
1592
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
30,573,125✔
1593
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1594

1595
  const double c0 = std::cos(p0);
29,822,735✔
1596
  const double s0 = std::sin(p0);
1597

1598
  const double denominator = (u.x * s0 - u.y * c0);
29,822,735✔
1599

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

571,824✔
1609
  return INFTY;
1610
}
61,208,506✔
1611

61,208,506✔
1612
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
30,573,125✔
1613
  const Position& r, const Direction& u, double l, int shell) const
30,573,125✔
1614
{
30,635,381✔
1615
  MeshDistance d;
29,822,735✔
1616
  d.next_index = shell;
29,822,735✔
1617

1618
  // Direction of flight is within xy-plane. Will never intersect z.
61,208,506✔
1619
  if (std::abs(u.z) < FP_PRECISION)
1620
    return d;
1621

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

1633
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
453✔
1634
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
906✔
UNCOV
1635
  double l) const
×
1636
{
UNCOV
1637
  if (i == 0) {
×
1638

1639
    return std::min(
1640
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
1641
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
151✔
1642

151✔
1643
  } else if (i == 1) {
1644

151✔
1645
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1646
                      find_phi_crossing(r0, u, l, ijk[i])),
1647
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
86,309,547✔
1648
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
1649

86,309,547✔
1650
  } else {
1651
    return find_z_crossing(r0, u, l, ijk[i]);
1652
  }
11✔
1653
}
1654

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

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

92✔
1688
    return OPENMC_E_INVALID_ARGUMENT;
1689
  }
132✔
1690

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

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

1698
  return 0;
1699
}
1700

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

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

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

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

1724
double CylindricalMesh::volume(const MeshIndex& ijk) const
47,366,451✔
1725
{
1726
  double r_i = grid_[0][ijk[0] - 1];
1727
  double r_o = grid_[0][ijk[0]];
47,366,451✔
1728

1729
  double phi_i = grid_[1][ijk[1] - 1];
47,366,451✔
1730
  double phi_o = grid_[1][ijk[1]];
47,366,451✔
1731

47,366,451✔
1732
  double z_i = grid_[2][ijk[2] - 1];
1733
  double z_o = grid_[2][ijk[2]];
47,366,451✔
UNCOV
1734

×
1735
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
1736
}
47,366,451✔
1737

47,366,451✔
1738
//==============================================================================
23,697,839✔
1739
// SphericalMesh implementation
1740
//==============================================================================
1741

47,366,451✔
1742
SphericalMesh::SphericalMesh(pugi::xml_node node)
1743
  : PeriodicStructuredMesh {node}
47,366,451✔
1744
{
1745
  n_dimension_ = 3;
47,366,451✔
1746

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

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

1757
const std::string SphericalMesh::mesh_type = "spherical";
88,110✔
1758

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

88,110✔
1764
StructuredMesh::MeshIndex SphericalMesh::get_indices(
88,110✔
1765
  Position r, bool& in_mesh) const
1766
{
88,110✔
1767
  r = local_coords(r);
88,110✔
1768

1769
  Position mapped_r;
88,110✔
1770
  mapped_r[0] = r.norm();
1771

1772
  if (mapped_r[0] < FP_PRECISION) {
141,838,246✔
1773
    mapped_r[1] = 0.0;
1774
    mapped_r[2] = 0.0;
1775
  } else {
1776
    mapped_r[1] = std::acos(r.z / mapped_r.x);
141,838,246✔
1777
    mapped_r[2] = std::atan2(r.y, r.x);
17,677,528✔
1778
    if (mapped_r[2] < 0)
1779
      mapped_r[2] += 2 * M_PI;
1780
  }
1781

1782
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
1783

124,160,718✔
1784
  idx[1] = sanitize_theta(idx[1]);
124,160,718✔
1785
  idx[2] = sanitize_phi(idx[2]);
7,015,096✔
1786

1787
  return idx;
117,145,622✔
1788
}
1789

1790
Position SphericalMesh::sample_element(
117,145,622✔
1791
  const MeshIndex& ijk, uint64_t* seed) const
58,960✔
1792
{
1793
  double r_min = this->r(ijk[0] - 1);
1794
  double r_max = this->r(ijk[0]);
117,086,662✔
1795

1796
  double theta_min = this->theta(ijk[1] - 1);
117,086,662✔
1797
  double theta_max = this->theta(ijk[1]);
117,086,662✔
1798

117,086,662✔
1799
  double phi_min = this->phi(ijk[2] - 1);
1800
  double phi_max = this->phi(ijk[2]);
117,086,662✔
1801

9,633,712✔
1802
  double cos_theta =
1803
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
107,452,950✔
1804
  double sin_theta = std::sin(std::acos(cos_theta));
1805
  double phi = uniform_distribution(phi_min, phi_max, seed);
1806
  double r_min_cub = std::pow(r_min, 3);
107,452,950✔
1807
  double r_max_cub = std::pow(r_max, 3);
6,469,408✔
1808
  // might be faster to do rejection here?
1809
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
100,983,542✔
1810

20,133,047✔
1811
  double x = r * std::cos(phi) * sin_theta;
80,850,495✔
1812
  double y = r * std::sin(phi) * sin_theta;
50,032,807✔
1813
  double z = r * cos_theta;
1814

30,817,688✔
1815
  return origin_ + Position(x, y, z);
1816
}
1817

73,733,814✔
1818
double SphericalMesh::find_r_crossing(
1819
  const Position& r, const Direction& u, double l, int shell) const
1820
{
1821
  if ((shell < 0) || (shell > shape_[0]))
73,733,814✔
1822
    return INFTY;
29,760,896✔
1823

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

1833
  if (std::abs(c) <= RADIAL_MESH_TOL)
43,972,918✔
1834
    return INFTY;
43,972,918✔
1835

1836
  if (D >= 0.0) {
43,972,918✔
1837
    D = std::sqrt(D);
1838
    // the solution -p - D is always smaller as -p + D : Check this one first
1839
    if (-p - D > l)
43,972,918✔
1840
      return -p - D;
43,712,174✔
1841
    if (-p + D > l)
1842
      return -p + D;
1843
  }
43,712,174✔
1844

20,218,363✔
1845
  return INFTY;
1846
}
1847

23,754,555✔
1848
double SphericalMesh::find_theta_crossing(
1849
  const Position& r, const Direction& u, double l, int shell) const
1850
{
36,340,216✔
1851
  // Theta grid is [0, π], thus there is no real surface to cross
1852
  if (full_theta_ && (shape_[1] == 1))
1853
    return INFTY;
36,340,216✔
1854

36,340,216✔
1855
  shell = sanitize_theta(shell);
1856

1857
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
36,340,216✔
1858
  // yields
1,118,216✔
1859
  // a*s^2 + 2*b*s + c == 0 with
1860
  // a = cos(theta)^2 - u.z * u.z
35,222,000✔
1861
  // b = r*u * cos(theta)^2 - u.z * r.z
35,222,000✔
1862
  // c = r*r * cos(theta)^2 - r.z^2
16,558,091✔
1863

16,558,091✔
1864
  const double cos_t = std::cos(grid_[1][shell]);
18,663,909✔
1865
  const bool sgn = std::signbit(cos_t);
16,810,321✔
1866
  const double cos_t_2 = cos_t * cos_t;
16,810,321✔
1867

1868
  const double a = cos_t_2 - u.z * u.z;
35,222,000✔
1869
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1870
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
1871

144,126,246✔
1872
  // if factor of s^2 is zero, direction of flight is parallel to theta
1873
  // surface
1874
  if (std::abs(a) < FP_PRECISION) {
1875
    // if b vanishes, direction of flight is within theta surface and crossing
144,126,246✔
1876
    // is not possible
1877
    if (std::abs(b) < FP_PRECISION)
70,919,123✔
1878
      return INFTY;
70,919,123✔
1879

141,838,246✔
1880
    const double s = -0.5 * c / b;
1881
    // Check if solution is in positive direction of flight and has correct
73,207,123✔
1882
    // sign
1883
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
36,866,907✔
1884
      return s;
36,866,907✔
1885

36,866,907✔
1886
    // no crossing is possible
73,733,814✔
1887
    return INFTY;
1888
  }
1889

36,340,216✔
1890
  const double p = b / a;
1891
  double D = p * p - c / a;
1892

1893
  if (D < 0.0)
412✔
1894
    return INFTY;
1895

412✔
1896
  D = std::sqrt(D);
412✔
1897

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

×
1904
  s = -p + D;
1905
  // Check if solution is in positive direction of flight and has correct sign
1,236✔
1906
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
2,472✔
1907
    return s;
×
1908

UNCOV
1909
  return INFTY;
×
1910
}
1911

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

×
1919
  shell = sanitize_phi(shell);
UNCOV
1920

×
1921
  const double p0 = grid_[2][shell];
1922

412✔
UNCOV
1923
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
×
1924
  // => x(s) * cos(p0) = y(s) * sin(p0)
1925
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
UNCOV
1926
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
×
1927

1928
  const double c0 = std::cos(p0);
1929
  const double s0 = std::sin(p0);
412✔
1930

1931
  const double denominator = (u.x * s0 - u.y * c0);
824✔
1932

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

142,099,353✔
1942
  return INFTY;
1943
}
UNCOV
1944

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

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

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

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

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

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

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

2005
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
67,739,463✔
2006
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
2007

67,739,463✔
2008
  double r = grid_[0].back();
67,739,463✔
2009
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
2010
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
67,739,463✔
UNCOV
2011

×
UNCOV
2012
  return 0;
×
2013
}
2014

67,739,463✔
2015
int SphericalMesh::get_index_in_direction(double r, int i) const
67,739,463✔
2016
{
67,739,463✔
2017
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
33,878,218✔
2018
}
2019

2020
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
67,739,463✔
2021
  Position plot_ll, Position plot_ur) const
2022
{
67,739,463✔
2023
  fatal_error("Plot of spherical Mesh not implemented");
67,739,463✔
2024

2025
  // Figure out which axes lie in the plane of the plot.
67,739,463✔
2026
  array<vector<double>, 2> axis_lines;
2027
  return {axis_lines[0], axis_lines[1]};
2028
}
110✔
2029

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

110✔
2038
double SphericalMesh::volume(const MeshIndex& ijk) const
110✔
2039
{
2040
  double r_i = grid_[0][ijk[0] - 1];
2041
  double r_o = grid_[0][ijk[0]];
110✔
2042

110✔
2043
  double theta_i = grid_[1][ijk[1] - 1];
110✔
2044
  double theta_o = grid_[1][ijk[1]];
110✔
2045

110✔
2046
  double phi_i = grid_[2][ijk[2] - 1];
2047
  double phi_o = grid_[2][ijk[2]];
110✔
2048

2049
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
110✔
2050
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
110✔
2051
}
110✔
2052

2053
//==============================================================================
110✔
2054
// Helper functions for the C API
2055
//==============================================================================
2056

441,926,232✔
2057
int check_mesh(int32_t index)
2058
{
2059
  if (index < 0 || index >= model::meshes.size()) {
441,926,232✔
2060
    set_errmsg("Index in meshes array is out of bounds.");
39,388,107✔
2061
    return OPENMC_E_OUT_OF_BOUNDS;
2062
  }
2063
  return 0;
2064
}
402,538,125✔
2065

402,538,125✔
2066
template<class T>
6,974,066✔
2067
int check_mesh_type(int32_t index)
395,564,059✔
2068
{
395,564,059✔
2069
  if (int err = check_mesh(index))
395,564,059✔
2070
    return err;
2071

395,564,059✔
2072
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
10,586,180✔
2073
  if (!mesh) {
2074
    set_errmsg("This function is not valid for input mesh.");
384,977,879✔
2075
    return OPENMC_E_INVALID_TYPE;
357,140,443✔
2076
  }
2077
  return 0;
357,140,443✔
2078
}
64,176,541✔
2079

292,963,902✔
2080
template<class T>
176,563,519✔
2081
bool is_mesh_type(int32_t index)
2082
{
2083
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
144,237,819✔
2084
  return mesh;
2085
}
2086

108,467,832✔
2087
//==============================================================================
2088
// C API functions
2089
//==============================================================================
2090

108,467,832✔
2091
// Return the type of mesh as a C string
70,123,416✔
2092
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2093
{
38,344,416✔
2094
  if (int err = check_mesh(index))
2095
    return err;
2096

2097
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
2098

2099
  return 0;
2100
}
2101

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

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

×
2126
  return 0;
2127
}
2128

37,861,868✔
2129
//! Adds a new unstructured mesh to OpenMC
37,861,868✔
2130
extern "C" int openmc_add_unstructured_mesh(
2131
  const char filename[], const char library[], int* id)
37,861,868✔
2132
{
10,945,825✔
2133
  std::string lib_name(library);
2134
  std::string mesh_file(filename);
26,916,043✔
2135
  bool valid_lib = false;
2136

2137
#ifdef DAGMC
26,916,043✔
2138
  if (lib_name == MOABMesh::mesh_lib_type) {
2139
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
26,916,043✔
2140
    valid_lib = true;
5,283,102✔
2141
  }
2142
#endif
21,632,941✔
2143

2144
#ifdef LIBMESH
21,632,941✔
2145
  if (lib_name == LibMesh::mesh_lib_type) {
10,154,661✔
2146
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2147
    valid_lib = true;
11,478,280✔
2148
  }
2149
#endif
2150

110,058,036✔
2151
  if (!valid_lib) {
2152
    set_errmsg(fmt::format("Mesh library {} is not supported "
2153
                           "by this build of OpenMC",
2154
      lib_name));
110,058,036✔
2155
    return OPENMC_E_INVALID_ARGUMENT;
70,123,416✔
2156
  }
2157

39,934,620✔
2158
  // auto-assign new ID
2159
  model::meshes.back()->set_id(-1);
39,934,620✔
2160
  *id = model::meshes.back()->id_;
2161

2162
  return 0;
2163
}
2164

2165
//! Return the index in the meshes array of a mesh with a given ID
2166
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
39,934,620✔
2167
{
39,934,620✔
2168
  auto pair = model::mesh_map.find(id);
2169
  if (pair == model::mesh_map.end()) {
39,934,620✔
2170
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
2171
    return OPENMC_E_INVALID_ID;
2172
  }
39,934,620✔
2173
  *index = pair->second;
39,700,628✔
2174
  return 0;
2175
}
2176

39,700,628✔
2177
//! Return the ID of a mesh
17,576,130✔
2178
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2179
{
2180
  if (int err = check_mesh(index))
22,358,490✔
2181
    return err;
2182
  *id = model::meshes[index]->id_;
2183
  return 0;
330,226,050✔
2184
}
2185

2186
//! Set the ID of a mesh
2187
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
2188
{
330,226,050✔
2189
  if (int err = check_mesh(index))
220,963,116✔
2190
    return err;
220,963,116✔
2191
  model::meshes[index]->id_ = id;
441,926,232✔
2192
  model::mesh_map[id] = index;
2193
  return 0;
109,262,934✔
2194
}
54,233,916✔
2195

54,233,916✔
2196
//! Get the number of elements in a mesh
54,233,916✔
2197
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
108,467,832✔
2198
{
2199
  if (int err = check_mesh(index))
2200
    return err;
55,029,018✔
2201
  *n = model::meshes[index]->n_bins();
55,029,018✔
2202
  return 0;
55,029,018✔
2203
}
110,058,036✔
2204

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

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

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

×
2224
  // set lower left corner values
2225
  ll[0] = bbox.xmin;
1,005✔
UNCOV
2226
  ll[1] = bbox.ymin;
×
2227
  ll[2] = bbox.zmin;
UNCOV
2228

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

×
2236
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
2237
  int nz, int table_size, int32_t* materials, double* volumes)
335✔
UNCOV
2238
{
×
2239
  if (int err = check_mesh(index))
UNCOV
2240
    return err;
×
2241

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

203,218,389✔
2254
  return 0;
2255
}
203,218,389✔
2256

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

2264
  int pixel_width = pixels[0];
2265
  int pixel_height = pixels[1];
2266

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

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

2293
  // set initial position
2294
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2295
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
6,631✔
2296

2297
#pragma omp parallel
6,631✔
UNCOV
2298
  {
×
2299
    Position r = xyz;
×
2300

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

1,186✔
2311
  return 0;
1,186✔
2312
}
×
UNCOV
2313

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

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

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

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

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

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

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

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

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

2391
  // Set material volumes
2392

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

48✔
2401
  return 0;
44✔
2402
}
22✔
2403

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

283✔
2413
  C* m = dynamic_cast<C*>(model::meshes[index].get());
283✔
2414

2415
  m->n_dimension_ = 3;
UNCOV
2416

×
2417
  m->grid_[0].reserve(nx);
2418
  m->grid_[1].reserve(ny);
UNCOV
2419
  m->grid_[2].reserve(nz);
×
UNCOV
2420

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

2431
  int err = m->set_grid();
2432
  return err;
2433
}
2434

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

2444
  if (m->lower_left_.dimension() == 0) {
UNCOV
2445
    set_errmsg("Mesh parameters have not been set.");
×
UNCOV
2446
    return OPENMC_E_ALLOCATE;
×
2447
  }
UNCOV
2448

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

459✔
UNCOV
2456
  return 0;
×
UNCOV
2457
}
×
2458

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

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

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

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

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

880✔
2498
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2499
    index, grid_x, nx, grid_y, ny, grid_z, nz);
88✔
2500
  ;
2501
}
2502

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

143✔
2512
#ifdef DAGMC
143✔
2513

143✔
2514
const std::string MOABMesh::mesh_lib_type = "moab";
2515

2516
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
143✔
2517
{
143✔
2518
  initialize();
143✔
2519
}
143✔
2520

2521
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2522
{
154✔
2523
  filename_ = filename;
2524
  set_length_multiplier(length_multiplier);
2525
  initialize();
154✔
UNCOV
2526
}
×
2527

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

11✔
2535
void MOABMesh::initialize()
UNCOV
2536
{
×
2537

2538
  // Create the MOAB interface and load data from file
11✔
2539
  this->create_interface();
2540

143✔
2541
  // Initialise MOAB error code
2542
  moab::ErrorCode rval = moab::MB_SUCCESS;
2543

44✔
2544
  // Set the dimension
2545
  n_dimension_ = 3;
2546

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

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

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

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

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

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

×
2606
  // Determine bounds of mesh
11✔
2607
  this->determine_bounds();
11✔
2608
}
11✔
2609

11✔
2610
void MOABMesh::prepare_for_point_location()
2611
{
2612
  // if the KDTree has already been constructed, do nothing
2613
  if (kdtree_)
213✔
2614
    return;
2615

2616
  // build acceleration data structures
213✔
UNCOV
2617
  compute_barycentric_data(ehs_);
×
2618
  build_kdtree(ehs_);
213✔
2619
}
2620

2621
void MOABMesh::create_interface()
213✔
2622
{
213✔
2623
  // Do not create a MOAB instance if one is already in memory
213✔
2624
  if (mbi_)
2625
    return;
2626

2627
  // create MOAB instance
235✔
2628
  mbi_ = std::make_shared<moab::Core>();
2629

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

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

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

2654
  // combine into one range
246✔
UNCOV
2655
  moab::Range all_tets_and_tris;
×
UNCOV
2656
  all_tets_and_tris.merge(all_tets);
×
2657
  all_tets_and_tris.merge(all_tris);
2658

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

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

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

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

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

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

92✔
2702
  // sorts by first component of std::pair by default
2703
  std::sort(hits.begin(), hits.end());
92✔
2704
}
92✔
2705

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

331✔
2714
  double track_len = (end - start).length();
239✔
2715
  if (track_len == 0.0)
2716
    return;
2717

92✔
2718
  start -= TINY_BIT * dir;
92✔
2719
  end += TINY_BIT * dir;
2720

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

2724
  bins.clear();
22✔
UNCOV
2725
  lengths.clear();
×
2726

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

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

22✔
UNCOV
2753
    // determine the start point for this segment
×
2754
    current = r0 + u * hit;
2755

22✔
2756
    if (bin == -1) {
2757
      continue;
22✔
2758
    }
2759

22✔
2760
    bins.push_back(bin);
22✔
2761
    lengths.push_back(segment_length / track_len);
22✔
2762
  }
2763

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

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

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

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

2804
  // if no tet is found, return an invalid handle
2805
  return 0;
2806
}
2807

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

389✔
2813
std::string MOABMesh::library() const
2814
{
389✔
UNCOV
2815
  return mesh_lib_type;
×
UNCOV
2816
}
×
2817

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

389✔
2822
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
389✔
2823

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

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

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

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

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

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

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

147✔
UNCOV
2878
  baryc_data_.clear();
×
UNCOV
2879
  baryc_data_.resize(tets.size());
×
2880

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

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

2896
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
147✔
2897

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

2904
bool MOABMesh::point_in_tet(
2905
  const moab::CartVect& r, moab::EntityHandle tet) const
48✔
2906
{
48✔
2907

2908
  moab::ErrorCode rval;
2909

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

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

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

121✔
2933
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2934

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

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

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

2955
int MOABMesh::get_index_from_bin(int bin) const
2956
{
2957
  return bin;
2958
}
2959

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

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

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

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

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

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

24✔
3012
Position MOABMesh::centroid(int bin) const
3013
{
3014
  moab::ErrorCode rval;
3015

3016
  auto tet = this->get_ent_handle_from_bin(bin);
3017

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

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

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

24✔
3041
  return {centroid[0], centroid[1], centroid[2]};
24✔
3042
}
3043

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

3049
Position MOABMesh::vertex(int id) const
3050
{
20✔
3051

20✔
3052
  moab::ErrorCode rval;
3053

3054
  moab::EntityHandle vert = verts_[id];
24✔
3055

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

23✔
3062
  return {coords[0], coords[1], coords[2]};
3063
}
3064

23✔
3065
std::vector<int> MOABMesh::connectivity(int bin) const
23✔
3066
{
3067
  moab::ErrorCode rval;
3068

3069
  auto tet = get_ent_handle_from_bin(bin);
3070

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

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

3084
  return verts;
3085
}
3086

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

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

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

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

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

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

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

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

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

242,659✔
3169
void MOABMesh::set_score_data(const std::string& score,
3170
  const vector<double>& values, const vector<double>& std_dev)
720,627✔
3171
{
3172
  auto score_tags = this->get_score_tags(score);
3173

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

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

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

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

3212
#endif
7,307,001✔
3213

3214
#ifdef LIBMESH
7,307,001✔
3215

3216
const std::string LibMesh::mesh_lib_type = "libmesh";
7,307,001✔
3217

7,307,001✔
3218
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
7,307,001✔
3219
{
1,010,077✔
3220
  // filename_ and length_multiplier_ will already be set by the
3221
  // UnstructuredMesh constructor
3222
  set_mesh_pointer_from_filename(filename_);
3223
  set_length_multiplier(length_multiplier_);
6,296,924✔
3224
  initialize();
6,296,924✔
3225
}
6,296,924✔
3226

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

3236
  m_ = &input_mesh;
3237
  set_length_multiplier(length_multiplier);
3238
  initialize();
2,846✔
3239
}
7,307,001✔
3240

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

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

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

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

3276
  // assuming that unstructured meshes used in OpenMC are 3D
3277
  n_dimension_ = 3;
400,820✔
3278

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

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

335,712✔
3295
  for (int i = 0; i < num_threads(); i++) {
167,856✔
3296
    pl_.emplace_back(m_->sub_point_locator());
3297
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
7,307,001✔
3298
    pl_.back()->enable_out_of_mesh_mode();
3299
  }
7,307,001✔
3300

7,307,001✔
3301
  // store first element in the mesh to use as an offset for bin indices
1,012,923✔
3302
  auto first_elem = *m_->elements_begin();
3303
  first_element_id_ = first_elem->id();
6,294,078✔
3304

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

3315
      bin_to_elem_map_.push_back(elem->id());
3316
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
239,732✔
3317
    }
239,712✔
3318
  }
239,712✔
3319

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

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

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

3349
int LibMesh::n_vertices() const
3350
{
3351
  return m_->n_nodes();
3352
}
3353

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

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

442,748,125✔
3370
std::string LibMesh::library() const
281,671,630✔
3371
{
260,007,978✔
3372
  return mesh_lib_type;
3373
}
3374

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

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

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

3405
    return;
3406
  }
3407

815,424✔
3408
  if (!equation_systems_) {
3409
    build_eqn_sys();
3410
  }
266,541,768✔
3411

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

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

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

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

3448
    return;
3449
  }
3450

3451
  if (!equation_systems_) {
3452
    build_eqn_sys();
3453
  }
3454

3455
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3456

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

3461
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3462

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

3470
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3471
       it++) {
3472
    if (!(*it)->active()) {
3473
      continue;
3474
    }
3475

3476
    auto bin = get_bin_from_element(*it);
3477

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

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

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

203,856✔
3499
    return;
3500
  }
3501

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

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

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

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

3527
  const auto& point_locator = pl_.at(thread_num());
3528

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

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

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

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

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

3559
#endif // LIBMESH
3560

3561
//==============================================================================
3562
// Non-member functions
3563
//==============================================================================
3564

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

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

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

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

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

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

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

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

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

3647
  close_group(meshes_group);
3648
}
3649

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

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

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

© 2025 Coveralls, Inc