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

openmc-dev / openmc / 21043587909

15 Jan 2026 07:25PM UTC coverage: 81.332% (-0.7%) from 82.044%
21043587909

Pull #3734

github

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

16365 of 22657 branches covered (72.23%)

Branch coverage included in aggregate %.

157 of 180 new or added lines in 12 files covered. (87.22%)

681 existing lines in 43 files now uncovered.

54412 of 64365 relevant lines covered (84.54%)

23556062.71 hits per line

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

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

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

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

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

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

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

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

61
namespace openmc {
62

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

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

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

77
namespace model {
78

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

82
} // namespace model
83

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

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

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

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

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

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

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

143
namespace detail {
144

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

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

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

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

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

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

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

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

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

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

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

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

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

228
} // namespace detail
229

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

416
          p.from_source(&site);
450,188✔
417

418
          // Determine particle's location
419
          if (!exhaustive_find_cell(p)) {
450,188✔
420
            out_of_model = true;
31,944✔
421
            continue;
31,944✔
422
          }
423

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

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

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

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

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

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

451
            // Add volumes to any mesh elements that were crossed
452
            int i_material = p.material();
843,772✔
453
            if (i_material != C_NONE) {
843,772✔
454
              i_material = model::materials[i_material]->id();
759,220✔
455
            }
456
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
1,862,464✔
457
              int mesh_index = bins[i_bin];
1,018,692✔
458
              double length = distance * length_fractions[i_bin];
1,018,692✔
459

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

464
            if (distance == max_distance)
843,772✔
465
              break;
418,244✔
466

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

605
std::string StructuredMesh::bin_label(int bin) const
1,859,028✔
606
{
607
  MeshIndex ijk = get_indices_from_bin(bin);
1,859,028✔
608

609
  if (n_dimension_ > 2) {
1,859,028✔
610
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
3,706,496✔
611
  } else if (n_dimension_ > 1) {
5,780✔
612
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
11,360✔
613
  } else {
614
    return fmt::format("Mesh Index ({})", ijk[0]);
200✔
615
  }
616
}
617

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
786
std::string UnstructuredMesh::get_mesh_type() const
×
787
{
UNCOV
788
  return mesh_type;
×
789
}
790

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

UNCOV
797
std::string UnstructuredMesh::bin_label(int bin) const
×
798
{
UNCOV
799
  return fmt::format("Mesh Index ({})", bin);
×
800
};
801

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

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

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

UNCOV
821
  int num_elem_skipped = 0;
×
822

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

UNCOV
830
    volumes.emplace_back(this->volume(i));
×
831

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

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

UNCOV
860
  write_dataset(mesh_group, "volumes", volumes);
×
UNCOV
861
  write_dataset(mesh_group, "connectivity", connectivity);
×
UNCOV
862
  write_dataset(mesh_group, "element_types", elem_types);
×
UNCOV
863
}
×
864

UNCOV
865
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
×
866
{
UNCOV
867
  length_multiplier_ = length_multiplier;
×
UNCOV
868
}
×
869

UNCOV
870
ElementType UnstructuredMesh::element_type(int bin) const
×
871
{
UNCOV
872
  auto conn = connectivity(bin);
×
873

UNCOV
874
  if (conn.size() == 4)
×
UNCOV
875
    return ElementType::LINEAR_TET;
×
876
  else if (conn.size() == 8)
×
877
    return ElementType::LINEAR_HEX;
×
878
  else
879
    return ElementType::UNSUPPORTED;
×
UNCOV
880
}
×
881

882
StructuredMesh::MeshIndex StructuredMesh::get_indices(
414,119,870✔
883
  Position r, bool& in_mesh) const
884
{
885
  MeshIndex ijk;
886
  in_mesh = true;
414,119,870✔
887
  for (int i = 0; i < n_dimension_; ++i) {
1,632,177,880✔
888
    ijk[i] = get_index_in_direction(r[i], i);
1,218,058,010✔
889

890
    if (ijk[i] < 1 || ijk[i] > shape_[i])
1,218,058,010✔
891
      in_mesh = false;
36,490,032✔
892
  }
893
  return ijk;
414,119,870✔
894
}
895

896
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
613,508,869✔
897
{
898
  switch (n_dimension_) {
613,508,869!
899
  case 1:
320,220✔
900
    return ijk[0] - 1;
320,220✔
901
  case 2:
35,420,080✔
902
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
35,420,080✔
903
  case 3:
577,768,569✔
904
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
577,768,569✔
905
  default:
×
906
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
907
  }
908
}
909

910
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
2,856,593✔
911
{
912
  MeshIndex ijk;
913
  if (n_dimension_ == 1) {
2,856,593✔
914
    ijk[0] = bin + 1;
100✔
915
  } else if (n_dimension_ == 2) {
2,856,493✔
916
    ijk[0] = bin % shape_[0] + 1;
5,680✔
917
    ijk[1] = bin / shape_[0] + 1;
5,680✔
918
  } else if (n_dimension_ == 3) {
2,850,813!
919
    ijk[0] = bin % shape_[0] + 1;
2,850,813✔
920
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
2,850,813✔
921
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
2,850,813✔
922
  }
923
  return ijk;
2,856,593✔
924
}
925

926
int StructuredMesh::get_bin(Position r) const
89,104,646✔
927
{
928
  // Determine indices
929
  bool in_mesh;
930
  MeshIndex ijk = get_indices(r, in_mesh);
89,104,646✔
931
  if (!in_mesh)
89,104,646✔
932
    return -1;
7,131,672✔
933

934
  // Convert indices to bin
935
  return get_bin_from_indices(ijk);
81,972,974✔
936
}
937

938
int StructuredMesh::n_bins() const
416,842✔
939
{
940
  return std::accumulate(
416,842✔
941
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
833,684✔
942
}
943

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

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

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

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

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

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

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

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

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

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

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

1001
  return counts;
×
1002
}
×
1003

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

1017
  // Compute the length of the entire track.
1018
  double total_distance = (r1 - r0).norm();
326,312,268✔
1019
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
326,312,268✔
1020
    return;
4,233,240✔
1021

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

1027
  const int n = n_dimension_;
322,079,028✔
1028

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

1032
  // Position is r = r0 + u * traveled_distance, start at r0
1033
  double traveled_distance {0.0};
322,079,028✔
1034

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

1039
  // if track is very short, assume that it is completely inside one cell.
1040
  // Only the current cell will score and no surfaces
1041
  if (total_distance < 2 * TINY_BIT) {
322,079,028✔
1042
    if (in_mesh) {
120,492✔
1043
      tally.track(ijk, 1.0);
120,316✔
1044
    }
1045
    return;
120,492✔
1046
  }
1047

1048
  // Calculate initial distances to next surfaces in all three dimensions
1049
  std::array<MeshDistance, 3> distances;
643,917,072✔
1050
  for (int k = 0; k < n; ++k) {
1,264,215,908✔
1051
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
942,257,372✔
1052
  }
1053

1054
  // Loop until r = r1 is eventually reached
1055
  while (true) {
270,382,928✔
1056

1057
    if (in_mesh) {
592,341,464✔
1058

1059
      // find surface with minimal distance to current position
1060
      const auto k = std::min_element(distances.begin(), distances.end()) -
561,192,079✔
1061
                     distances.begin();
561,192,079✔
1062

1063
      // Tally track length delta since last step
1064
      tally.track(ijk,
561,192,079✔
1065
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
561,192,079✔
1066
          total_distance);
1067

1068
      // update position and leave, if we have reached end position
1069
      traveled_distance = distances[k].distance;
561,192,079✔
1070
      if (traveled_distance >= total_distance)
561,192,079✔
1071
        return;
293,249,307✔
1072

1073
      // If we have not reached r1, we have hit a surface. Tally outward
1074
      // current
1075
      tally.surface(ijk, k, distances[k].max_surface, false);
267,942,772✔
1076

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

1083
      // Check if we have left the interior of the mesh
1084
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
267,942,772✔
1085

1086
      // If we are still inside the mesh, tally inward current for the next
1087
      // cell
1088
      if (in_mesh)
267,942,772✔
1089
        tally.surface(ijk, k, !distances[k].max_surface, true);
263,111,699✔
1090

1091
    } else { // not inside mesh
1092

1093
      // For all directions outside the mesh, find the distance that we need
1094
      // to travel to reach the next surface. Use the largest distance, as
1095
      // only this will cross all outer surfaces.
1096
      int k_max {-1};
31,149,385✔
1097
      for (int k = 0; k < n; ++k) {
124,072,316✔
1098
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
126,950,864✔
1099
            (distances[k].distance > traveled_distance)) {
34,027,933✔
1100
          traveled_distance = distances[k].distance;
32,238,205✔
1101
          k_max = k;
32,238,205✔
1102
        }
1103
      }
1104
      // Assure some distance is traveled
1105
      if (k_max == -1) {
31,149,385✔
1106
        traveled_distance += TINY_BIT;
40✔
1107
      }
1108

1109
      // If r1 is not inside the mesh, exit here
1110
      if (traveled_distance >= total_distance)
31,149,385✔
1111
        return;
28,709,229✔
1112

1113
      // Calculate the new cell index and update all distances to next
1114
      // surfaces.
1115
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
2,440,156✔
1116
      for (int k = 0; k < n; ++k) {
9,684,792✔
1117
        distances[k] =
7,244,636✔
1118
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
7,244,636✔
1119
      }
1120

1121
      // If inside the mesh, Tally inward current
1122
      if (in_mesh && k_max >= 0)
2,440,156!
1123
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
2,292,260✔
1124
    }
1125
  }
1126
}
1127

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

1132
  // Helper tally class.
1133
  // stores a pointer to the mesh class and references to bins and lengths
1134
  // parameters. Performs the actual tally through the track method.
1135
  struct TrackAggregator {
1136
    TrackAggregator(
285,538,608✔
1137
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
1138
      : mesh(_mesh), bins(_bins), lengths(_lengths)
285,538,608✔
1139
    {}
285,538,608✔
1140
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
512,197,935✔
1141
    void track(const MeshIndex& ijk, double l) const
510,387,099✔
1142
    {
1143
      bins.push_back(mesh->get_bin_from_indices(ijk));
510,387,099✔
1144
      lengths.push_back(l);
510,387,099✔
1145
    }
510,387,099✔
1146

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

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

1156
void StructuredMesh::surface_bins_crossed(
40,773,660✔
1157
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1158
{
1159

1160
  // Helper tally class.
1161
  // stores a pointer to the mesh class and a reference to the bins parameter.
1162
  // Performs the actual tally through the surface method.
1163
  struct SurfaceAggregator {
1164
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
40,773,660✔
1165
      : mesh(_mesh), bins(_bins)
40,773,660✔
1166
    {}
40,773,660✔
1167
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
21,148,796✔
1168
    {
1169
      int i_bin =
1170
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
21,148,796✔
1171
      if (max)
21,148,796✔
1172
        i_bin += 2;
10,564,160✔
1173
      if (inward)
21,148,796✔
1174
        i_bin += 1;
10,393,560✔
1175
      bins.push_back(i_bin);
21,148,796✔
1176
    }
21,148,796✔
1177
    void track(const MeshIndex& idx, double l) const {}
50,925,296✔
1178

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

1183
  // Perform the mesh raytrace with the helper class.
1184
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
40,773,660✔
1185
}
40,773,660✔
1186

1187
double StructuredMesh::distance_to_next_boundary(Position r, Direction u) const
496,040✔
1188
{
1189
  Position global_r = r;
496,040✔
1190
  Position local_r = local_coords(r);
496,040✔
1191

1192
  double distance = 0.0;
496,040✔
1193
  const int n = n_dimension_;
496,040✔
1194
  bool in_mesh;
1195

1196
  StructuredMesh::MeshIndex ijk = get_indices(global_r + TINY_BIT * u, in_mesh);
496,040✔
1197

1198
  // Calculate initial distances to next surfaces in all three dimensions
1199
  std::array<StructuredMesh::MeshDistance, 3> distances;
992,080✔
1200
  for (int k = 0; k < n; ++k) {
1,984,160✔
1201
    distances[k] = distance_to_grid_boundary(ijk, k, local_r, u, 0.0);
1,488,120✔
1202
  }
1203

1204
  if (in_mesh) {
496,040✔
1205

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

1210
    distance = distances[k].distance;
496,032✔
1211

1212
  } else { // not inside mesh
1213

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

1227
  return distance;
496,040✔
1228
}
1229

1230
//==============================================================================
1231
// RegularMesh implementation
1232
//==============================================================================
1233

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

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

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

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

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

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

1269
  } else if (upper_right_.size() > 0) {
732!
1270

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

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

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

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

1293
  element_volume_ = 1.0;
750✔
1294
  for (int i = 0; i < n_dimension_; i++) {
2,824✔
1295
    element_volume_ *= width_[i];
2,074✔
1296
  }
1297
  return 0;
750✔
1298
}
750✔
1299

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

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

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

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

1328
    width_ = get_node_xarray<double>(node, "width");
18✔
1329

1330
  } else if (check_for_node(node, "upper_right")) {
728!
1331

1332
    upper_right_ = get_node_xarray<double>(node, "upper_right");
728✔
1333

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

1338
  if (int err = set_grid()) {
746!
1339
    fatal_error(openmc_err_msg);
×
1340
  }
1341
}
746✔
1342

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

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

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

1366
  if (object_exists(group, "upper_right")) {
4!
1367

1368
    read_dataset(group, "upper_right", upper_right_);
4✔
1369

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

1374
  if (int err = set_grid()) {
4!
1375
    fatal_error(openmc_err_msg);
×
1376
  }
1377
}
4✔
1378

1379
int RegularMesh::get_index_in_direction(double r, int i) const
1,064,670,866✔
1380
{
1381
  return std::ceil((r - lower_left_[i]) / width_[i]);
1,064,670,866✔
1382
}
1383

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

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

1391
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
522,044,859✔
1392
{
1393
  return lower_left_[i] + ijk[i] * width_[i];
522,044,859✔
1394
}
1395

1396
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
499,034,791✔
1397
{
1398
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
499,034,791✔
1399
}
1400

1401
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
1,026,043,620✔
1402
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1403
  double l) const
1404
{
1405
  MeshDistance d;
1,026,043,620✔
1406
  d.next_index = ijk[i];
1,026,043,620✔
1407
  if (std::abs(u[i]) < FP_PRECISION)
1,026,043,620✔
1408
    return d;
637,920✔
1409

1410
  d.max_surface = (u[i] > 0);
1,025,405,700✔
1411
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1,025,405,700✔
1412
    d.next_index++;
520,386,360✔
1413
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
520,386,360✔
1414
  } else if (!d.max_surface && (ijk[i] >= 1)) {
505,019,340✔
1415
    d.next_index--;
497,376,292✔
1416
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
497,376,292✔
1417
  }
1418

1419
  return d;
1,025,405,700✔
1420
}
1421

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

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

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

1460
  return {axis_lines[0], axis_lines[1]};
16✔
1461
}
8✔
1462

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

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

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

1482
  for (int64_t i = 0; i < length; i++) {
2,791,018✔
1483
    const auto& site = bank[i];
2,788,164✔
1484

1485
    // determine scoring bin for entropy mesh
1486
    int mesh_bin = get_bin(site.r);
2,788,164✔
1487

1488
    // if outside mesh, skip particle
1489
    if (mesh_bin < 0) {
2,788,164!
1490
      outside_ = true;
×
1491
      continue;
×
1492
    }
1493

1494
    // Add to appropriate bin
1495
    cnt(mesh_bin) += site.wgt;
2,788,164✔
1496
  }
1497

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

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

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

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

1523
  return counts;
5,708✔
1524
}
2,854✔
1525

1526
double RegularMesh::volume(const MeshIndex& ijk) const
411,976✔
1527
{
1528
  return element_volume_;
411,976✔
1529
}
1530

1531
//==============================================================================
1532
// RectilinearMesh implementation
1533
//==============================================================================
1534

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

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

1543
  if (int err = set_grid()) {
46!
1544
    fatal_error(openmc_err_msg);
×
1545
  }
1546
}
46✔
1547

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

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

1556
  if (int err = set_grid()) {
4!
1557
    fatal_error(openmc_err_msg);
×
1558
  }
1559
}
4✔
1560

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

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

1568
double RectilinearMesh::positive_grid_boundary(
9,638,532✔
1569
  const MeshIndex& ijk, int i) const
1570
{
1571
  return grid_[i][ijk[i]];
9,638,532✔
1572
}
1573

1574
double RectilinearMesh::negative_grid_boundary(
9,359,784✔
1575
  const MeshIndex& ijk, int i) const
1576
{
1577
  return grid_[i][ijk[i] - 1];
9,359,784✔
1578
}
1579

1580
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
19,491,668✔
1581
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1582
  double l) const
1583
{
1584
  MeshDistance d;
19,491,668✔
1585
  d.next_index = ijk[i];
19,491,668✔
1586
  if (std::abs(u[i]) < FP_PRECISION)
19,491,668✔
1587
    return d;
207,936✔
1588

1589
  d.max_surface = (u[i] > 0);
19,283,732✔
1590
  if (d.max_surface && (ijk[i] <= shape_[i])) {
19,283,732✔
1591
    d.next_index++;
9,638,532✔
1592
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
9,638,532✔
1593
  } else if (!d.max_surface && (ijk[i] > 0)) {
9,645,200✔
1594
    d.next_index--;
9,359,784✔
1595
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
9,359,784✔
1596
  }
1597
  return d;
19,283,732✔
1598
}
1599

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

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

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

1623
  return 0;
66✔
1624
}
1625

1626
int RectilinearMesh::get_index_in_direction(double r, int i) const
26,948,688✔
1627
{
1628
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
26,948,688✔
1629
}
1630

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

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

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

1658
  return {axis_lines[0], axis_lines[1]};
8✔
1659
}
4✔
1660

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

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

1672
  for (int i = 0; i < n_dimension_; i++) {
192✔
1673
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
144✔
1674
  }
1675
  return vol;
48✔
1676
}
1677

1678
//==============================================================================
1679
// CylindricalMesh implementation
1680
//==============================================================================
1681

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

1691
  if (int err = set_grid()) {
146!
1692
    fatal_error(openmc_err_msg);
×
1693
  }
1694
}
146✔
1695

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

1704
  if (int err = set_grid()) {
4!
1705
    fatal_error(openmc_err_msg);
×
1706
  }
1707
}
4✔
1708

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

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

1716
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
17,355,152✔
1717
  Position r, bool& in_mesh) const
1718
{
1719
  r = local_coords(r);
17,355,152✔
1720

1721
  Position mapped_r;
17,355,152✔
1722
  mapped_r[0] = std::hypot(r.x, r.y);
17,355,152✔
1723
  mapped_r[2] = r[2];
17,355,152✔
1724

1725
  if (mapped_r[0] < FP_PRECISION) {
17,355,152!
1726
    mapped_r[1] = 0.0;
×
1727
  } else {
1728
    mapped_r[1] = std::atan2(r.y, r.x);
17,355,152✔
1729
    if (mapped_r[1] < 0)
17,355,152✔
1730
      mapped_r[1] += 2 * M_PI;
8,680,884✔
1731
  }
1732

1733
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
17,355,152✔
1734

1735
  idx[1] = sanitize_phi(idx[1]);
17,355,152✔
1736

1737
  return idx;
17,355,152✔
1738
}
1739

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

1746
  double phi_min = this->phi(ijk[1] - 1);
32,040✔
1747
  double phi_max = this->phi(ijk[1]);
32,040✔
1748

1749
  double z_min = this->z(ijk[2] - 1);
32,040✔
1750
  double z_max = this->z(ijk[2]);
32,040✔
1751

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

1758
  double x = r * std::cos(phi);
32,040✔
1759
  double y = r * std::sin(phi);
32,040✔
1760

1761
  return origin_ + Position(x, y, z);
32,040✔
1762
}
1763

1764
double CylindricalMesh::find_r_crossing(
51,847,452✔
1765
  const Position& r, const Direction& u, double l, int shell) const
1766
{
1767

1768
  if ((shell < 0) || (shell > shape_[0]))
51,847,452!
1769
    return INFTY;
6,514,293✔
1770

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

1775
  const double r0 = grid_[0][shell];
45,333,159✔
1776
  if (r0 == 0.0)
45,333,159✔
1777
    return INFTY;
2,592,964✔
1778

1779
  const double denominator = u.x * u.x + u.y * u.y;
42,740,195✔
1780

1781
  // Direction of flight is in z-direction. Will never intersect r.
1782
  if (std::abs(denominator) < FP_PRECISION)
42,740,195✔
1783
    return INFTY;
21,440✔
1784

1785
  // inverse of dominator to help the compiler to speed things up
1786
  const double inv_denominator = 1.0 / denominator;
42,718,755✔
1787

1788
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
42,718,755✔
1789
  double c = r.x * r.x + r.y * r.y - r0 * r0;
42,718,755✔
1790
  double D = p * p - c * inv_denominator;
42,718,755✔
1791

1792
  if (D < 0.0)
42,718,755✔
1793
    return INFTY;
3,539,480✔
1794

1795
  D = std::sqrt(D);
39,179,275✔
1796

1797
  // the solution -p - D is always smaller as -p + D : Check this one first
1798
  if (std::abs(c) <= RADIAL_MESH_TOL)
39,179,275✔
1799
    return INFTY;
2,404,136✔
1800

1801
  if (-p - D > l)
36,775,139✔
1802
    return -p - D;
7,348,764✔
1803
  if (-p + D > l)
29,426,375✔
1804
    return -p + D;
18,216,789✔
1805

1806
  return INFTY;
11,209,586✔
1807
}
1808

1809
double CylindricalMesh::find_phi_crossing(
27,071,112✔
1810
  const Position& r, const Direction& u, double l, int shell) const
1811
{
1812
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1813
  if (full_phi_ && (shape_[1] == 1))
27,071,112✔
1814
    return INFTY;
11,081,760✔
1815

1816
  shell = sanitize_phi(shell);
15,989,352✔
1817

1818
  const double p0 = grid_[1][shell];
15,989,352✔
1819

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

1825
  const double c0 = std::cos(p0);
15,989,352✔
1826
  const double s0 = std::sin(p0);
15,989,352✔
1827

1828
  const double denominator = (u.x * s0 - u.y * c0);
15,989,352✔
1829

1830
  // Check if direction of flight is not parallel to phi surface
1831
  if (std::abs(denominator) > FP_PRECISION) {
15,989,352✔
1832
    const double s = -(r.x * s0 - r.y * c0) / denominator;
15,894,536✔
1833
    // Check if solution is in positive direction of flight and crosses the
1834
    // correct phi surface (not -phi)
1835
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
15,894,536✔
1836
      return s;
7,352,676✔
1837
  }
1838

1839
  return INFTY;
8,636,676✔
1840
}
1841

1842
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
13,341,936✔
1843
  const Position& r, const Direction& u, double l, int shell) const
1844
{
1845
  MeshDistance d;
13,341,936✔
1846
  d.next_index = shell;
13,341,936✔
1847

1848
  // Direction of flight is within xy-plane. Will never intersect z.
1849
  if (std::abs(u.z) < FP_PRECISION)
13,341,936✔
1850
    return d;
406,624✔
1851

1852
  d.max_surface = (u.z > 0.0);
12,935,312✔
1853
  if (d.max_surface && (shell <= shape_[2])) {
12,935,312✔
1854
    d.next_index += 1;
6,135,724✔
1855
    d.distance = (grid_[2][shell] - r.z) / u.z;
6,135,724✔
1856
  } else if (!d.max_surface && (shell > 0)) {
6,799,588✔
1857
    d.next_index -= 1;
6,124,892✔
1858
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
6,124,892✔
1859
  }
1860
  return d;
12,935,312✔
1861
}
1862

1863
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
52,801,218✔
1864
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1865
  double l) const
1866
{
1867
  if (i == 0) {
52,801,218✔
1868

1869
    return std::min(
25,923,726✔
1870
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
25,923,726✔
1871
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
51,847,452✔
1872

1873
  } else if (i == 1) {
26,877,492✔
1874

1875
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
13,535,556✔
1876
                      find_phi_crossing(r0, u, l, ijk[i])),
13,535,556✔
1877
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
13,535,556✔
1878
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
27,071,112✔
1879

1880
  } else {
1881
    return find_z_crossing(r0, u, l, ijk[i]);
13,341,936✔
1882
  }
1883
}
1884

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

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

1918
    return OPENMC_E_INVALID_ARGUMENT;
×
1919
  }
1920

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

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

1928
  return 0;
158✔
1929
}
1930

1931
int CylindricalMesh::get_index_in_direction(double r, int i) const
52,065,456✔
1932
{
1933
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
52,065,456✔
1934
}
1935

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

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

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

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

1959
  double phi_i = grid_[1][ijk[1] - 1];
288✔
1960
  double phi_o = grid_[1][ijk[1]];
288✔
1961

1962
  double z_i = grid_[2][ijk[2] - 1];
288✔
1963
  double z_o = grid_[2][ijk[2]];
288✔
1964

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

1968
//==============================================================================
1969
// SphericalMesh implementation
1970
//==============================================================================
1971

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

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

1982
  if (int err = set_grid()) {
126!
1983
    fatal_error(openmc_err_msg);
×
1984
  }
1985
}
126✔
1986

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

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

1996
  if (int err = set_grid()) {
4!
1997
    fatal_error(openmc_err_msg);
×
1998
  }
1999
}
4✔
2000

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

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

2008
StructuredMesh::MeshIndex SphericalMesh::get_indices(
24,791,000✔
2009
  Position r, bool& in_mesh) const
2010
{
2011
  r = local_coords(r);
24,791,000✔
2012

2013
  Position mapped_r;
24,791,000✔
2014
  mapped_r[0] = r.norm();
24,791,000✔
2015

2016
  if (mapped_r[0] < FP_PRECISION) {
24,791,000!
2017
    mapped_r[1] = 0.0;
×
2018
    mapped_r[2] = 0.0;
×
2019
  } else {
2020
    mapped_r[1] = std::acos(r.z / mapped_r.x);
24,791,000✔
2021
    mapped_r[2] = std::atan2(r.y, r.x);
24,791,000✔
2022
    if (mapped_r[2] < 0)
24,791,000✔
2023
      mapped_r[2] += 2 * M_PI;
12,386,284✔
2024
  }
2025

2026
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
24,791,000✔
2027

2028
  idx[1] = sanitize_theta(idx[1]);
24,791,000✔
2029
  idx[2] = sanitize_phi(idx[2]);
24,791,000✔
2030

2031
  return idx;
24,791,000✔
2032
}
2033

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

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

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

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

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

2059
  return origin_ + Position(x, y, z);
40✔
2060
}
2061

2062
double SphericalMesh::find_r_crossing(
161,103,820✔
2063
  const Position& r, const Direction& u, double l, int shell) const
2064
{
2065
  if ((shell < 0) || (shell > shape_[0]))
161,103,820✔
2066
    return INFTY;
14,407,388✔
2067

2068
  // solve |r+s*u| = r0
2069
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
2070
  const double r0 = grid_[0][shell];
146,696,432✔
2071
  if (r0 == 0.0)
146,696,432✔
2072
    return INFTY;
2,640,596✔
2073
  const double p = r.dot(u);
144,055,836✔
2074
  double c = r.dot(r) - r0 * r0;
144,055,836✔
2075
  double D = p * p - c;
144,055,836✔
2076

2077
  if (std::abs(c) <= RADIAL_MESH_TOL)
144,055,836✔
2078
    return INFTY;
3,854,056✔
2079

2080
  if (D >= 0.0) {
140,201,780✔
2081
    D = std::sqrt(D);
130,064,708✔
2082
    // the solution -p - D is always smaller as -p + D : Check this one first
2083
    if (-p - D > l)
130,064,708✔
2084
      return -p - D;
23,370,211✔
2085
    if (-p + D > l)
106,694,497✔
2086
      return -p + D;
64,319,894✔
2087
  }
2088

2089
  return INFTY;
52,511,675✔
2090
}
2091

2092
double SphericalMesh::find_theta_crossing(
39,755,488✔
2093
  const Position& r, const Direction& u, double l, int shell) const
2094
{
2095
  // Theta grid is [0, π], thus there is no real surface to cross
2096
  if (full_theta_ && (shape_[1] == 1))
39,755,488✔
2097
    return INFTY;
25,806,928✔
2098

2099
  shell = sanitize_theta(shell);
13,948,560✔
2100

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

2108
  const double cos_t = std::cos(grid_[1][shell]);
13,948,560✔
2109
  const bool sgn = std::signbit(cos_t);
13,948,560✔
2110
  const double cos_t_2 = cos_t * cos_t;
13,948,560✔
2111

2112
  const double a = cos_t_2 - u.z * u.z;
13,948,560✔
2113
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
13,948,560✔
2114
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
13,948,560✔
2115

2116
  // if factor of s^2 is zero, direction of flight is parallel to theta
2117
  // surface
2118
  if (std::abs(a) < FP_PRECISION) {
13,948,560✔
2119
    // if b vanishes, direction of flight is within theta surface and crossing
2120
    // is not possible
2121
    if (std::abs(b) < FP_PRECISION)
175,472!
2122
      return INFTY;
175,472✔
2123

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

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

2134
  const double p = b / a;
13,773,088✔
2135
  double D = p * p - c / a;
13,773,088✔
2136

2137
  if (D < 0.0)
13,773,088✔
2138
    return INFTY;
3,983,632✔
2139

2140
  D = std::sqrt(D);
9,789,456✔
2141

2142
  // the solution -p-D is always smaller as -p+D : Check this one first
2143
  double s = -p - D;
9,789,456✔
2144
  // Check if solution is in positive direction of flight and has correct sign
2145
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
9,789,456✔
2146
    return s;
1,920,948✔
2147

2148
  s = -p + D;
7,868,508✔
2149
  // Check if solution is in positive direction of flight and has correct sign
2150
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
7,868,508✔
2151
    return s;
3,695,744✔
2152

2153
  return INFTY;
4,172,764✔
2154
}
2155

2156
double SphericalMesh::find_phi_crossing(
40,333,480✔
2157
  const Position& r, const Direction& u, double l, int shell) const
2158
{
2159
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
2160
  if (full_phi_ && (shape_[2] == 1))
40,333,480✔
2161
    return INFTY;
25,806,928✔
2162

2163
  shell = sanitize_phi(shell);
14,526,552✔
2164

2165
  const double p0 = grid_[2][shell];
14,526,552✔
2166

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

2172
  const double c0 = std::cos(p0);
14,526,552✔
2173
  const double s0 = std::sin(p0);
14,526,552✔
2174

2175
  const double denominator = (u.x * s0 - u.y * c0);
14,526,552✔
2176

2177
  // Check if direction of flight is not parallel to phi surface
2178
  if (std::abs(denominator) > FP_PRECISION) {
14,526,552✔
2179
    const double s = -(r.x * s0 - r.y * c0) / denominator;
14,441,464✔
2180
    // Check if solution is in positive direction of flight and crosses the
2181
    // correct phi surface (not -phi)
2182
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
14,441,464✔
2183
      return s;
6,392,528✔
2184
  }
2185

2186
  return INFTY;
8,134,024✔
2187
}
2188

2189
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
120,596,394✔
2190
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
2191
  double l) const
2192
{
2193

2194
  if (i == 0) {
120,596,394✔
2195
    return std::min(
80,551,910✔
2196
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
80,551,910✔
2197
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
161,103,820✔
2198

2199
  } else if (i == 1) {
40,044,484✔
2200
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
19,877,744✔
2201
                      find_theta_crossing(r0, u, l, ijk[i])),
19,877,744✔
2202
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
19,877,744✔
2203
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
39,755,488✔
2204

2205
  } else {
2206
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
20,166,740✔
2207
                      find_phi_crossing(r0, u, l, ijk[i])),
20,166,740✔
2208
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
20,166,740✔
2209
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
40,333,480✔
2210
  }
2211
}
2212

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

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

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

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

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

2256
  return 0;
138✔
2257
}
2258

2259
int SphericalMesh::get_index_in_direction(double r, int i) const
74,373,000✔
2260
{
2261
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
74,373,000✔
2262
}
2263

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

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

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

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

2287
  double theta_i = grid_[1][ijk[1] - 1];
340✔
2288
  double theta_o = grid_[1][ijk[1]];
340✔
2289

2290
  double phi_i = grid_[2][ijk[2] - 1];
340✔
2291
  double phi_o = grid_[2][ijk[2]];
340✔
2292

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

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

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

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

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

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

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

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

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

2343
  return 0;
532✔
2344
}
2345

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

2354
  for (int i = 0; i < n; ++i) {
184✔
2355
    if (RegularMesh::mesh_type == type) {
92✔
2356
      model::meshes.push_back(make_unique<RegularMesh>());
60✔
2357
    } else if (RectilinearMesh::mesh_type == type) {
32✔
2358
      model::meshes.push_back(make_unique<RectilinearMesh>());
16✔
2359
    } else if (CylindricalMesh::mesh_type == type) {
16✔
2360
      model::meshes.push_back(make_unique<CylindricalMesh>());
8✔
2361
    } else if (SphericalMesh::mesh_type == type) {
8!
2362
      model::meshes.push_back(make_unique<SphericalMesh>());
8✔
2363
    } else {
2364
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
×
2365
    }
2366
  }
2367
  if (index_end)
92!
2368
    *index_end = model::meshes.size() - 1;
×
2369

2370
  return 0;
92✔
2371
}
92✔
2372

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

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

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

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

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

2406
  return 0;
×
2407
}
×
2408

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

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

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

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

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

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

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

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

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

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

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

2498
  return 0;
60✔
2499
}
2500

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

2508
  int pixel_width = pixels[0];
16✔
2509
  int pixel_height = pixels[1];
16✔
2510

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

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

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

2541
#pragma omp parallel
2542
  {
2543
    Position r = xyz;
16✔
2544

2545
#pragma omp for
2546
    for (int y = 0; y < pixel_height; y++) {
336✔
2547
      r[out_i] = xyz[out_i] - out_pixel * y;
320✔
2548
      for (int x = 0; x < pixel_width; x++) {
6,720✔
2549
        r[in_i] = xyz[in_i] + in_pixel * x;
6,400✔
2550
        data[pixel_width * y + x] = mesh->get_bin(r);
6,400✔
2551
      }
2552
    }
2553
  }
2554

2555
  return 0;
16✔
2556
}
2557

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

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

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

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

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

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

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

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

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

2635
  // Set material volumes
2636

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

2645
  return 0;
80✔
2646
}
80✔
2647

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

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

2659
  m->n_dimension_ = 3;
32✔
2660

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

2665
  for (int i = 0; i < nx; i++) {
208✔
2666
    m->grid_[0].push_back(grid_x[i]);
176✔
2667
  }
2668
  for (int i = 0; i < ny; i++) {
124✔
2669
    m->grid_[1].push_back(grid_y[i]);
92✔
2670
  }
2671
  for (int i = 0; i < nz; i++) {
116✔
2672
    m->grid_[2].push_back(grid_z[i]);
84✔
2673
  }
2674

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

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

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

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

2700
  return 0;
140✔
2701
}
2702

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

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

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

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

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

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

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

2756
#ifdef OPENMC_DAGMC_ENABLED
2757

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

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

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

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

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

2786
void MOABMesh::initialize()
2787
{
2788

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3159
  moab::ErrorCode rval;
3160

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3303
  moab::ErrorCode rval;
3304

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

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

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

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

3320
  auto tet = get_ent_handle_from_bin(bin);
3321

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

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

3335
  return verts;
3336
}
3337

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3463
#endif
3464

3465
#ifdef OPENMC_LIBMESH_ENABLED
3466

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3593
int LibMesh::n_vertices() const
3594
{
3595
  return m_->n_nodes();
3596
}
3597

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

3604
std::vector<int> LibMesh::connectivity(int elem_id) const
3605
{
3606
  std::vector<int> conn;
3607
  const auto* elem_ptr = m_->elem_ptr(elem_id);
3608
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
3609
    conn.push_back(elem_ptr->node_id(i));
3610
  }
3611
  return conn;
3612
}
3613

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

3619
int LibMesh::n_bins() const
3620
{
3621
  return m_->n_elem();
3622
}
3623

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

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

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

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

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

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

3683
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3684

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

3689
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3690

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

3698
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3699
       it++) {
3700
    if (!(*it)->active()) {
3701
      continue;
3702
    }
3703

3704
    auto bin = get_bin_from_element(*it);
3705

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

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

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

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

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

3742
  // quick rejection check
3743
  if (!bbox_.contains_point(p)) {
3744
    return -1;
3745
  }
3746

3747
  const auto& point_locator = pl_.at(thread_num());
3748

3749
  const auto elem_ptr = (*point_locator)(p);
3750
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3751
}
3752

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

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

3768
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3769
{
3770
  return m_->elem_ref(bin);
3771
}
3772

3773
double LibMesh::volume(int bin) const
3774
{
3775
  return this->get_element_from_bin(bin).volume();
3776
}
3777

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

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

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

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

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

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

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

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

3837
#endif // OPENMC_LIBMESH_ENABLED
3838

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

3843
void read_meshes(pugi::xml_node root)
4,454✔
3844
{
3845
  std::unordered_set<int> mesh_ids;
4,454✔
3846

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

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

3864
    std::string mesh_type;
1,044✔
3865
    if (check_for_node(node, "type")) {
1,044✔
3866
      mesh_type = get_node_value(node, "type", true, true);
332✔
3867
    } else {
3868
      mesh_type = "regular";
712✔
3869
    }
3870

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

3877
    Mesh::create(node, mesh_type, mesh_lib);
1,044✔
3878
  }
1,044✔
3879
}
4,454✔
3880

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

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

3888
  for (auto id : ids) {
20✔
3889

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

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

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

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

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

3921
    Mesh::create(mesh_group, mesh_type, mesh_lib);
×
3922
  }
×
3923
}
8✔
3924

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

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

3942
  close_group(meshes_group);
2,422✔
3943
}
2,422✔
3944

3945
void free_memory_mesh()
2,944✔
3946
{
3947
  model::meshes.clear();
2,944✔
3948
  model::mesh_map.clear();
2,944✔
3949
}
2,944✔
3950

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

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

© 2026 Coveralls, Inc