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

openmc-dev / openmc / 16190841638

10 Jul 2025 09:03AM UTC coverage: 84.887% (-0.4%) from 85.251%
16190841638

Pull #3279

github

web-flow
Merge 5f15cb8bb into d700d395d
Pull Request #3279: Hexagonal mesh model

564 of 801 new or added lines in 6 files covered. (70.41%)

116 existing lines in 16 files now uncovered.

53118 of 62575 relevant lines covered (84.89%)

37709984.48 hits per line

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

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

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

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

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

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

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

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

61
namespace openmc {
62

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

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

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

77
namespace model {
78

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

82
} // namespace model
83

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

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

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

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

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

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

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

143
namespace detail {
144

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

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

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

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

169
    // Found the desired material; accumulate volume
170
    if (current_val == index_material) {
2,642,410✔
171
#pragma omp atomic
1,318,014✔
172
      this->volumes(index_elem, slot) += volume;
2,635,253✔
173
      return;
2,635,253✔
174
    }
175

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

183
      // If we claimed the slot or another thread claimed it but the same
184
      // material was inserted, proceed to accumulate
185
      if (claimed_slot || (expected_val == index_material)) {
1,471✔
186
#pragma omp atomic
742✔
187
        this->volumes(index_elem, slot) += volume;
1,471✔
188
        return;
1,471✔
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
Mesh::Mesh(pugi::xml_node node)
2,967✔
235
{
236
  // Read mesh id
237
  id_ = std::stoi(get_node_value(node, "id"));
2,967✔
238
  if (check_for_node(node, "name"))
2,967✔
239
    name_ = get_node_value(node, "name");
17✔
240
}
2,967✔
241

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

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

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

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

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

272
vector<double> Mesh::volumes() const
270✔
273
{
274
  vector<double> volumes(n_bins());
270✔
275
  for (int i = 0; i < n_bins(); i++) {
1,220,934✔
276
    volumes[i] = this->volume(i);
1,220,664✔
277
  }
278
  return volumes;
270✔
279
}
×
280

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

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

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

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

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

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

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

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

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

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

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

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

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

362
          p.from_source(&site);
554,730✔
363

364
          // Determine particle's location
365
          if (!exhaustive_find_cell(p)) {
554,730✔
366
            out_of_model = true;
47,916✔
367
            continue;
47,916✔
368
          }
369

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

374
          // Initialize last cells from current cell
375
          for (int j = 0; j < p.n_coord(); ++j) {
1,013,628✔
376
            p.cell_last(j) = p.coord(j).cell;
506,814✔
377
          }
378
          p.n_coord_last() = p.n_coord();
506,814✔
379

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

385
            // Find the distance to the nearest boundary
386
            BoundaryInfo boundary = distance_to_boundary(p);
1,043,142✔
387

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

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

397
            // Add volumes to any mesh elements that were crossed
398
            int i_material = p.material();
1,043,142✔
399
            if (i_material != C_NONE) {
1,043,142✔
400
              i_material = model::materials[i_material]->id();
1,009,026✔
401
            }
402
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
2,361,504✔
403
              int mesh_index = bins[i_bin];
1,318,362✔
404
              double length = distance * length_fractions[i_bin];
1,318,362✔
405

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

410
            if (distance == max_distance)
1,043,142✔
411
              break;
506,814✔
412

413
            // cross next geometric surface
414
            for (int j = 0; j < p.n_coord(); ++j) {
1,072,656✔
415
              p.cell_last(j) = p.coord(j).cell;
536,328✔
416
            }
417
            p.n_coord_last() = p.n_coord();
536,328✔
418

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

555
  if (n_dimension_ > 2) {
5,584,596✔
556
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
11,137,008✔
557
  } else if (n_dimension_ > 1) {
16,092✔
558
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,584✔
559
  } else {
560
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
561
  }
562
}
563

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

718
  int num_elem_skipped = 0;
31✔
719

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

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

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

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

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

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

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

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

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

787
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
788
      in_mesh = false;
108,831,184✔
789
  }
790
  return ijk;
1,251,274,505✔
791
}
792

793
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,831,154,997✔
794
{
795
  switch (n_dimension_) {
1,831,154,997✔
796
  case 1:
956,736✔
797
    return ijk[0] - 1;
956,736✔
798
  case 2:
76,627,284✔
799
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
76,627,284✔
800
  case 3:
1,753,570,977✔
801
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,753,570,977✔
802
  default:
×
803
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
804
  }
805
}
806

807
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,479,182✔
808
{
809
  MeshIndex ijk;
810
  if (n_dimension_ == 1) {
8,479,182✔
811
    ijk[0] = bin + 1;
300✔
812
  } else if (n_dimension_ == 2) {
8,478,882✔
813
    ijk[0] = bin % shape_[0] + 1;
15,792✔
814
    ijk[1] = bin / shape_[0] + 1;
15,792✔
815
  } else if (n_dimension_ == 3) {
8,463,090✔
816
    ijk[0] = bin % shape_[0] + 1;
8,463,090✔
817
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,463,090✔
818
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,463,090✔
819
  }
820
  return ijk;
8,479,182✔
821
}
822

823
int StructuredMesh::get_bin(Position r) const
277,785,536✔
824
{
825
  // Determine indices
826
  bool in_mesh;
827
  MeshIndex ijk = get_indices(r, in_mesh);
277,785,536✔
828
  if (!in_mesh)
277,785,536✔
829
    return -1;
22,174,314✔
830

831
  // Convert indices to bin
832
  return get_bin_from_indices(ijk);
255,611,222✔
833
}
834

835
int StructuredMesh::n_bins() const
1,236,921✔
836
{
837
  return std::accumulate(
1,236,921✔
838
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,473,842✔
839
}
840

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

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

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

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

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

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

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

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

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

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

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

898
  return counts;
×
899
}
900

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

914
  // Compute the length of the entire track.
915
  double total_distance = (r1 - r0).norm();
979,265,181✔
916
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
979,265,181✔
917
    return;
12,554,184✔
918

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

924
  const int n = n_dimension_;
966,710,997✔
925

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

929
  // Position is r = r0 + u * traveled_distance, start at r0
930
  double traveled_distance {0.0};
966,710,997✔
931

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

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

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

951
  // Loop until r = r1 is eventually reached
952
  while (true) {
805,950,674✔
953

954
    if (in_mesh) {
1,772,288,411✔
955

956
      // find surface with minimal distance to current position
957
      const auto k = std::min_element(distances.begin(), distances.end()) -
1,679,365,753✔
958
                     distances.begin();
1,679,365,753✔
959

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

965
      // update position and leave, if we have reached end position
966
      traveled_distance = distances[k].distance;
1,679,365,753✔
967
      if (traveled_distance >= total_distance)
1,679,365,753✔
968
        return;
880,193,051✔
969

970
      // If we have not reached r1, we have hit a surface. Tally outward
971
      // current
972
      tally.surface(ijk, k, distances[k].max_surface, false);
799,172,702✔
973

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

980
      // Check if we have left the interior of the mesh
981
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
799,172,702✔
982

983
      // If we are still inside the mesh, tally inward current for the next
984
      // cell
985
      if (in_mesh)
799,172,702✔
986
        tally.surface(ijk, k, !distances[k].max_surface, true);
783,439,950✔
987

988
    } else { // not inside mesh
989

990
      // For all directions outside the mesh, find the distance that we need
991
      // to travel to reach the next surface. Use the largest distance, as
992
      // only this will cross all outer surfaces.
993
      int k_max {-1};
92,922,658✔
994
      for (int k = 0; k < n; ++k) {
370,076,416✔
995
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
378,734,742✔
996
            (distances[k].distance > traveled_distance)) {
101,580,984✔
997
          traveled_distance = distances[k].distance;
96,189,264✔
998
          k_max = k;
96,189,264✔
999
        }
1000
      }
1001
      // Assure some distance is traveled
1002
      if (k_max == -1) {
92,922,658✔
1003
        traveled_distance += TINY_BIT;
120✔
1004
      }
1005

1006
      // If r1 is not inside the mesh, exit here
1007
      if (traveled_distance >= total_distance)
92,922,658✔
1008
        return;
86,144,686✔
1009

1010
      // Calculate the new cell index and update all distances to next
1011
      // surfaces.
1012
      ijk = get_indices(global_r + (traveled_distance + TINY_BIT) * u, in_mesh);
6,777,972✔
1013
      for (int k = 0; k < n; ++k) {
26,882,628✔
1014
        distances[k] =
20,104,656✔
1015
          distance_to_grid_boundary(ijk, k, local_r, u, traveled_distance);
20,104,656✔
1016
      }
1017

1018
      // If inside the mesh, Tally inward current
1019
      if (in_mesh && k_max >= 0)
6,777,972✔
1020
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
6,547,212✔
1021
    }
1022
  }
1023
}
1024

141,666,700✔
1025
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
1026
  vector<int>& bins, vector<double>& lengths) const
1027
{
1028

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

141,666,700✔
1044
    const StructuredMesh* mesh;
1045
    vector<int>& bins;
1046
    vector<double>& lengths;
1047
  };
1048

1049
  // Perform the mesh raytrace with the helper class.
141,666,700✔
1050
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1051
}
1052

1053
void StructuredMesh::surface_bins_crossed(
141,666,700✔
1054
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
1055
{
1056

1057
  // Helper tally class.
141,666,700✔
1058
  // stores a pointer to the mesh class and a reference to the bins parameter.
×
1059
  // Performs the actual tally through the surface method.
×
1060
  struct SurfaceAggregator {
1061
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
×
1062
      : mesh(_mesh), bins(_bins)
1063
    {}
1064
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
1065
    {
283,333,400✔
1066
      int i_bin =
564,830,656✔
1067
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
423,163,956✔
1068
      if (max)
1069
        i_bin += 2;
1070
      if (inward)
1071
        i_bin += 1;
36,990,766✔
1072
      bins.push_back(i_bin);
1073
    }
178,657,466✔
1074
    void track(const MeshIndex& idx, double l) const {}
1075

1076
    const StructuredMesh* mesh;
176,443,756✔
1077
    vector<int>& bins;
176,443,756✔
1078
  };
1079

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

1084
//==============================================================================
1085
// RegularMesh implementation
176,443,756✔
1086
//==============================================================================
176,443,756✔
1087

139,696,698✔
1088
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
1089
{
1090
  // Determine number of dimensions for mesh
1091
  if (!check_for_node(node, "dimension")) {
36,747,058✔
1092
    fatal_error("Must specify <dimension> on a regular mesh.");
1093
  }
1094

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

1102
  // Check that dimensions are all greater than zero
1103
  if (xt::any(shape <= 0)) {
1104
    fatal_error("All entries on the <dimension> element for a tally "
36,747,058✔
1105
                "mesh must be positive.");
35,282,880✔
1106
  }
1107

1108
  // Check for lower-left coordinates
1109
  if (check_for_node(node, "lower_left")) {
1110
    // Read mesh lower-left corner location
1111
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1112
  } else {
2,213,710✔
1113
    fatal_error("Must specify <lower_left> on a mesh.");
8,503,540✔
1114
  }
8,660,092✔
1115

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

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

1,970,002✔
1128
    width_ = get_node_xarray<double>(node, "width");
1129

1130
    // Check to ensure width has same dimensions
1131
    auto n = width_.size();
243,708✔
1132
    if (n != lower_left_.size()) {
860,736✔
1133
      fatal_error("Number of entries on <width> must be the same as "
617,028✔
1134
                  "the number of entries on <lower_left>.");
617,028✔
1135
    }
1136

1137
    // Check for negative widths
1138
    if (xt::any(width_ < 0.0)) {
243,708✔
1139
      fatal_error("Cannot have a negative <width> on a tally mesh.");
218,592✔
1140
    }
1141

1142
    // Set width and upper right coordinate
1143
    upper_right_ = xt::eval(lower_left_ + shape * width_);
837,598,481✔
1144

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

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

837,598,481✔
1155
    // Check that upper-right is above lower-left
12,554,184✔
1156
    if (xt::any(upper_right_ < lower_left_)) {
1157
      fatal_error("The <upper_right> coordinates must be greater than "
1158
                  "the <lower_left> coordinates on a tally mesh.");
1159
    }
825,044,297✔
1160

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

1167
  // Set material volumes
1168
  volume_frac_ = 1.0 / xt::prod(shape)();
825,044,297✔
1169

1170
  element_volume_ = 1.0;
1171
  for (int i = 0; i < n_dimension_; i++) {
1172
    element_volume_ *= width_[i];
825,044,297✔
1173
  }
1174
}
1175

1176
int RegularMesh::get_index_in_direction(double r, int i) const
825,044,297✔
1177
{
373,260✔
1178
  return std::ceil((r - lower_left_[i]) / width_[i]);
373,248✔
1179
}
1180

373,260✔
1181
const std::string RegularMesh::mesh_type = "regular";
1182

1183
std::string RegularMesh::get_mesh_type() const
1184
{
1,649,342,074✔
1185
  return mesh_type;
2,147,483,647✔
1186
}
2,147,483,647✔
1187

1188
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
1189
{
1190
  return lower_left_[i] + ijk[i] * width_[i];
768,959,908✔
1191
}
1192

1,593,630,945✔
1193
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
1194
{
1195
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1,502,921,997✔
1196
}
1,502,921,997✔
1197

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

740,496,353✔
1207
  d.max_surface = (u[i] > 0);
1208
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1209
    d.next_index++;
1210
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
762,425,644✔
1211
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1212
    d.next_index--;
1213
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1214
  }
762,425,644✔
1215

762,425,644✔
1216
  return d;
762,425,644✔
1217
}
1218

1219
std::pair<vector<double>, vector<double>> RegularMesh::plot(
762,425,644✔
1220
  Position plot_ll, Position plot_ur) const
1221
{
1222
  // Figure out which axes lie in the plane of the plot.
1223
  array<int, 2> axes {-1, -1};
762,425,644✔
1224
  if (plot_ur.z == plot_ll.z) {
748,157,070✔
1225
    axes[0] = 0;
1226
    if (n_dimension_ > 1)
1227
      axes[1] = 1;
1228
  } else if (plot_ur.y == plot_ll.y) {
1229
    axes[0] = 0;
1230
    if (n_dimension_ > 2)
1231
      axes[1] = 2;
90,708,948✔
1232
  } else if (plot_ur.x == plot_ll.x) {
361,572,876✔
1233
    if (n_dimension_ > 1)
370,074,650✔
1234
      axes[0] = 1;
99,210,722✔
1235
    if (n_dimension_ > 2)
93,883,586✔
1236
      axes[1] = 2;
93,883,586✔
1237
  } else {
1238
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1239
  }
1240

90,708,948✔
1241
  // Get the coordinates of the mesh lines along both of the axes.
×
1242
  array<vector<double>, 2> axis_lines;
1243
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1244
    int axis = axes[i_ax];
1245
    if (axis == -1)
90,708,948✔
1246
      continue;
84,174,684✔
1247
    auto& lines {axis_lines[i_ax]};
1248

1249
    double coord = lower_left_[axis];
1250
    for (int i = 0; i < shape_[axis] + 1; ++i) {
6,534,264✔
1251
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
26,021,892✔
1252
        lines.push_back(coord);
19,487,628✔
1253
      coord += width_[axis];
19,487,628✔
1254
    }
1255
  }
1256

1257
  return {axis_lines[0], axis_lines[1]};
6,534,264✔
1258
}
6,328,620✔
1259

1260
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1261
{
1262
  write_dataset(mesh_group, "dimension", get_x_shape());
1263
  write_dataset(mesh_group, "lower_left", lower_left_);
837,598,481✔
1264
  write_dataset(mesh_group, "upper_right", upper_right_);
1265
  write_dataset(mesh_group, "width", width_);
1266
}
1267

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

837,598,481✔
1275
  // Create array of zeros
1,516,911,334✔
1276
  xt::xarray<double> cnt {shape, 0.0};
1,503,295,245✔
1277
  bool outside_ = false;
1278

1,503,295,245✔
1279
  for (int64_t i = 0; i < length; i++) {
1,503,295,245✔
1280
    const auto& site = bank[i];
1,503,295,245✔
1281

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

1285
    // if outside mesh, skip particle
1286
    if (mesh_bin < 0) {
1287
      outside_ = true;
1288
      continue;
837,598,481✔
1289
    }
837,598,481✔
1290

1291
    // Add to appropriate bin
141,666,700✔
1292
    cnt(mesh_bin) += site.wgt;
1293
  }
1294

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

141,666,700✔
1301
#ifdef OPENMC_MPI
141,666,700✔
1302
  // collect values from all processors
72,248,530✔
1303
  MPI_Reduce(
1304
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1305

72,248,530✔
1306
  // Check if there were sites outside the mesh for any processor
72,248,530✔
1307
  if (outside) {
36,085,368✔
1308
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
72,248,530✔
1309
  }
35,501,472✔
1310
#else
72,248,530✔
1311
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
72,248,530✔
1312
  if (outside)
176,443,756✔
1313
    *outside = outside_;
1314
#endif
1315

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

141,666,700✔
1320
  return counts;
141,666,700✔
1321
}
1322

1323
double RegularMesh::volume(const MeshIndex& ijk) const
1324
{
1325
  return element_volume_;
1326
}
2,015✔
1327

1328
//==============================================================================
1329
// RectilinearMesh implementation
2,015✔
1330
//==============================================================================
×
1331

1332
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1333
{
2,015✔
1334
  n_dimension_ = 3;
2,015✔
1335

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

1340
  if (int err = set_grid()) {
1341
    fatal_error(openmc_err_msg);
2,015✔
1342
  }
×
1343
}
1344

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

1347
std::string RectilinearMesh::get_mesh_type() const
2,015✔
1348
{
1349
  return mesh_type;
2,015✔
1350
}
1351

×
1352
double RectilinearMesh::positive_grid_boundary(
1353
  const MeshIndex& ijk, int i) const
1354
{
1355
  return grid_[i][ijk[i]];
2,015✔
1356
}
×
1357

1358
double RectilinearMesh::negative_grid_boundary(
1359
  const MeshIndex& ijk, int i) const
1360
{
2,015✔
1361
  return grid_[i][ijk[i] - 1];
1362
}
51✔
1363

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

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

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

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

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

2,015✔
1407
  return 0;
1408
}
2,015✔
1409

7,662✔
1410
int RectilinearMesh::get_index_in_direction(double r, int i) const
5,647✔
1411
{
1412
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
2,015✔
1413
}
1414

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

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

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

2,147,483,647✔
1442
  return {axis_lines[0], axis_lines[1]};
2,147,483,647✔
1443
}
1,590,744✔
1444

1445
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
2,147,483,647✔
1446
{
2,147,483,647✔
1447
  write_dataset(mesh_group, "x_grid", grid_[0]);
1,563,443,445✔
1448
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,563,443,445✔
1449
  write_dataset(mesh_group, "z_grid", grid_[2]);
1,520,060,840✔
1450
}
1,496,512,296✔
1451

1,496,512,296✔
1452
double RectilinearMesh::volume(const MeshIndex& ijk) const
1453
{
1454
  double vol {1.0};
2,147,483,647✔
1455

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

24✔
1462
//==============================================================================
24✔
1463
// CylindricalMesh implementation
24✔
1464
//==============================================================================
24✔
1465

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

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

1480
const std::string CylindricalMesh::mesh_type = "cylindrical";
24✔
1481

72✔
1482
std::string CylindricalMesh::get_mesh_type() const
48✔
1483
{
48✔
1484
  return mesh_type;
×
1485
}
48✔
1486

1487
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
48✔
1488
  Position r, bool& in_mesh) const
312✔
1489
{
264✔
1490
  r = local_coords(r);
264✔
1491

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

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

2,090✔
1504
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
2,090✔
1505

1506
  idx[1] = sanitize_phi(idx[1]);
9,113✔
1507

1508
  return idx;
1509
}
1510

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

1517
  double phi_min = this->phi(ijk[1] - 1);
8,946,451✔
1518
  double phi_max = this->phi(ijk[1]);
8,937,338✔
1519

1520
  double z_min = this->z(ijk[2] - 1);
1521
  double z_max = this->z(ijk[2]);
8,937,338✔
1522

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

1529
  double x = r * std::cos(phi);
1530
  double y = r * std::sin(phi);
8,937,338✔
1531

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

1535
double CylindricalMesh::find_r_crossing(
1536
  const Position& r, const Direction& u, double l, int shell) const
9,113✔
1537
{
9,113✔
1538

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

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

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

4,928✔
1550
  const double denominator = u.x * u.x + u.y * u.y;
4,928✔
1551

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

9,113✔
1556
  // inverse of dominator to help the compiler to speed things up
9,113✔
1557
  const double inv_denominator = 1.0 / denominator;
1558

18,226✔
1559
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
9,113✔
1560
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1561
  double D = p * p - c * inv_denominator;
1,221,984✔
1562

1563
  if (D < 0.0)
1,221,984✔
1564
    return INFTY;
1565

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

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

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

123✔
1577
  return INFTY;
1578
}
123✔
1579

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

1587
  shell = sanitize_phi(shell);
420,488✔
1588

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

32,973,128✔
1591
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1592
  // => x(s) * cos(p0) = y(s) * sin(p0)
1593
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
32,973,128✔
1594
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1595

1596
  const double c0 = std::cos(p0);
32,154,994✔
1597
  const double s0 = std::sin(p0);
1598

1599
  const double denominator = (u.x * s0 - u.y * c0);
32,154,994✔
1600

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

623,808✔
1610
  return INFTY;
1611
}
66,012,384✔
1612

66,012,384✔
1613
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
32,973,128✔
1614
  const Position& r, const Direction& u, double l, int shell) const
32,973,128✔
1615
{
33,039,256✔
1616
  MeshDistance d;
32,154,994✔
1617
  d.next_index = shell;
32,154,994✔
1618

1619
  // Direction of flight is within xy-plane. Will never intersect z.
66,012,384✔
1620
  if (std::abs(u.z) < FP_PRECISION)
1621
    return d;
1622

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

1634
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
525✔
1635
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1,050✔
1636
  double l) const
×
1637
{
1638
  if (i == 0) {
×
1639

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

175✔
1644
  } else if (i == 1) {
1645

175✔
1646
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1647
                      find_phi_crossing(r0, u, l, ijk[i])),
1648
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
93,027,000✔
1649
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
1650

93,027,000✔
1651
  } else {
1652
    return find_z_crossing(r0, u, l, ijk[i]);
1653
  }
12✔
1654
}
1655

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

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

112✔
1689
    return OPENMC_E_INVALID_ARGUMENT;
1690
  }
144✔
1691

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

1694
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
576✔
1695
    origin_[2] + grid_[2].front()};
432✔
1696
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
1697
    origin_[2] + grid_[2].back()};
144✔
1698

1699
  return 0;
1700
}
1701

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

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

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

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

1725
double CylindricalMesh::volume(const MeshIndex& ijk) const
51,672,492✔
1726
{
1727
  double r_i = grid_[0][ijk[0] - 1];
1728
  double r_o = grid_[0][ijk[0]];
51,672,492✔
1729

1730
  double phi_i = grid_[1][ijk[1] - 1];
51,672,492✔
1731
  double phi_o = grid_[1][ijk[1]];
51,672,492✔
1732

51,672,492✔
1733
  double z_i = grid_[2][ijk[2] - 1];
1734
  double z_o = grid_[2][ijk[2]];
51,672,492✔
1735

×
1736
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
1737
}
51,672,492✔
1738

51,672,492✔
1739
//==============================================================================
25,852,188✔
1740
// SphericalMesh implementation
1741
//==============================================================================
1742

51,672,492✔
1743
SphericalMesh::SphericalMesh(pugi::xml_node node)
1744
  : PeriodicStructuredMesh {node}
51,672,492✔
1745
{
1746
  n_dimension_ = 3;
51,672,492✔
1747

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

96,120✔
1753
  if (int err = set_grid()) {
96,120✔
1754
    fatal_error(openmc_err_msg);
1755
  }
96,120✔
1756
}
96,120✔
1757

1758
const std::string SphericalMesh::mesh_type = "spherical";
96,120✔
1759

96,120✔
1760
std::string SphericalMesh::get_mesh_type() const
1761
{
96,120✔
1762
  return mesh_type;
96,120✔
1763
}
96,120✔
1764

96,120✔
1765
StructuredMesh::MeshIndex SphericalMesh::get_indices(
96,120✔
1766
  Position r, bool& in_mesh) const
1767
{
96,120✔
1768
  r = local_coords(r);
96,120✔
1769

1770
  Position mapped_r;
96,120✔
1771
  mapped_r[0] = r.norm();
1772

1773
  if (mapped_r[0] < FP_PRECISION) {
154,732,632✔
1774
    mapped_r[1] = 0.0;
1775
    mapped_r[2] = 0.0;
1776
  } else {
1777
    mapped_r[1] = std::acos(r.z / mapped_r.x);
154,732,632✔
1778
    mapped_r[2] = std::atan2(r.y, r.x);
19,284,576✔
1779
    if (mapped_r[2] < 0)
1780
      mapped_r[2] += 2 * M_PI;
1781
  }
1782

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

135,448,056✔
1785
  idx[1] = sanitize_theta(idx[1]);
135,448,056✔
1786
  idx[2] = sanitize_phi(idx[2]);
7,652,832✔
1787

1788
  return idx;
127,795,224✔
1789
}
1790

1791
Position SphericalMesh::sample_element(
127,795,224✔
1792
  const MeshIndex& ijk, uint64_t* seed) const
64,320✔
1793
{
1794
  double r_min = this->r(ijk[0] - 1);
1795
  double r_max = this->r(ijk[0]);
127,730,904✔
1796

1797
  double theta_min = this->theta(ijk[1] - 1);
127,730,904✔
1798
  double theta_max = this->theta(ijk[1]);
127,730,904✔
1799

127,730,904✔
1800
  double phi_min = this->phi(ijk[2] - 1);
1801
  double phi_max = this->phi(ijk[2]);
127,730,904✔
1802

10,509,504✔
1803
  double cos_theta =
1804
    uniform_distribution(std::cos(theta_min), std::cos(theta_max), seed);
117,221,400✔
1805
  double sin_theta = std::sin(std::acos(cos_theta));
1806
  double phi = uniform_distribution(phi_min, phi_max, seed);
1807
  double r_min_cub = std::pow(r_min, 3);
117,221,400✔
1808
  double r_max_cub = std::pow(r_max, 3);
7,057,536✔
1809
  // might be faster to do rejection here?
1810
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
110,163,864✔
1811

21,963,324✔
1812
  double x = r * std::cos(phi) * sin_theta;
88,200,540✔
1813
  double y = r * std::sin(phi) * sin_theta;
54,581,244✔
1814
  double z = r * cos_theta;
1815

33,619,296✔
1816
  return origin_ + Position(x, y, z);
1817
}
1818

80,436,888✔
1819
double SphericalMesh::find_r_crossing(
1820
  const Position& r, const Direction& u, double l, int shell) const
1821
{
1822
  if ((shell < 0) || (shell > shape_[0]))
80,436,888✔
1823
    return INFTY;
32,466,432✔
1824

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

1834
  if (std::abs(c) <= RADIAL_MESH_TOL)
47,970,456✔
1835
    return INFTY;
47,970,456✔
1836

1837
  if (D >= 0.0) {
47,970,456✔
1838
    D = std::sqrt(D);
1839
    // the solution -p - D is always smaller as -p + D : Check this one first
1840
    if (-p - D > l)
47,970,456✔
1841
      return -p - D;
47,686,008✔
1842
    if (-p + D > l)
1843
      return -p + D;
1844
  }
47,686,008✔
1845

22,056,396✔
1846
  return INFTY;
1847
}
1848

25,914,060✔
1849
double SphericalMesh::find_theta_crossing(
1850
  const Position& r, const Direction& u, double l, int shell) const
1851
{
39,643,872✔
1852
  // Theta grid is [0, π], thus there is no real surface to cross
1853
  if (full_theta_ && (shape_[1] == 1))
1854
    return INFTY;
39,643,872✔
1855

39,643,872✔
1856
  shell = sanitize_theta(shell);
1857

1858
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
39,643,872✔
1859
  // yields
1,219,872✔
1860
  // a*s^2 + 2*b*s + c == 0 with
1861
  // a = cos(theta)^2 - u.z * u.z
38,424,000✔
1862
  // b = r*u * cos(theta)^2 - u.z * r.z
38,424,000✔
1863
  // c = r*r * cos(theta)^2 - r.z^2
18,063,372✔
1864

18,063,372✔
1865
  const double cos_t = std::cos(grid_[1][shell]);
20,360,628✔
1866
  const bool sgn = std::signbit(cos_t);
18,338,532✔
1867
  const double cos_t_2 = cos_t * cos_t;
18,338,532✔
1868

1869
  const double a = cos_t_2 - u.z * u.z;
38,424,000✔
1870
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1871
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
1872

157,228,632✔
1873
  // if factor of s^2 is zero, direction of flight is parallel to theta
1874
  // surface
1875
  if (std::abs(a) < FP_PRECISION) {
1876
    // if b vanishes, direction of flight is within theta surface and crossing
157,228,632✔
1877
    // is not possible
1878
    if (std::abs(b) < FP_PRECISION)
77,366,316✔
1879
      return INFTY;
77,366,316✔
1880

154,732,632✔
1881
    const double s = -0.5 * c / b;
1882
    // Check if solution is in positive direction of flight and has correct
79,862,316✔
1883
    // sign
1884
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
40,218,444✔
1885
      return s;
40,218,444✔
1886

40,218,444✔
1887
    // no crossing is possible
80,436,888✔
1888
    return INFTY;
1889
  }
1890

39,643,872✔
1891
  const double p = b / a;
1892
  double D = p * p - c / a;
1893

1894
  if (D < 0.0)
449✔
1895
    return INFTY;
1896

449✔
1897
  D = std::sqrt(D);
449✔
1898

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

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

1910
  return INFTY;
×
1911
}
1912

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

×
1920
  shell = sanitize_phi(shell);
1921

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

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

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

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

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

155,017,476✔
1943
  return INFTY;
1944
}
1945

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

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

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

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

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

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

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

2006
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
73,897,596✔
2007
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
2008

73,897,596✔
2009
  double r = grid_[0].back();
73,897,596✔
2010
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
2011
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
73,897,596✔
2012

×
2013
  return 0;
×
2014
}
2015

73,897,596✔
2016
int SphericalMesh::get_index_in_direction(double r, int i) const
73,897,596✔
2017
{
73,897,596✔
2018
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
36,958,056✔
2019
}
2020

2021
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
73,897,596✔
2022
  Position plot_ll, Position plot_ur) const
2023
{
73,897,596✔
2024
  fatal_error("Plot of spherical Mesh not implemented");
73,897,596✔
2025

2026
  // Figure out which axes lie in the plane of the plot.
73,897,596✔
2027
  array<vector<double>, 2> axis_lines;
2028
  return {axis_lines[0], axis_lines[1]};
2029
}
120✔
2030

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

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

120✔
2044
  double theta_i = grid_[1][ijk[1] - 1];
120✔
2045
  double theta_o = grid_[1][ijk[1]];
120✔
2046

120✔
2047
  double phi_i = grid_[2][ijk[2] - 1];
2048
  double phi_o = grid_[2][ijk[2]];
120✔
2049

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

2054
//==============================================================================
120✔
2055
// Helper functions for the C API
2056
//==============================================================================
2057

482,101,344✔
2058
int check_mesh(int32_t index)
2059
{
2060
  if (index < 0 || index >= model::meshes.size()) {
482,101,344✔
2061
    set_errmsg("Index in meshes array is out of bounds.");
42,968,844✔
2062
    return OPENMC_E_OUT_OF_BOUNDS;
2063
  }
2064
  return 0;
2065
}
439,132,500✔
2066

439,132,500✔
2067
template<class T>
7,608,072✔
2068
int check_mesh_type(int32_t index)
431,524,428✔
2069
{
431,524,428✔
2070
  if (int err = check_mesh(index))
431,524,428✔
2071
    return err;
2072

431,524,428✔
2073
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
11,548,560✔
2074
  if (!mesh) {
2075
    set_errmsg("This function is not valid for input mesh.");
419,975,868✔
2076
    return OPENMC_E_INVALID_TYPE;
389,607,756✔
2077
  }
2078
  return 0;
389,607,756✔
2079
}
70,010,772✔
2080

319,596,984✔
2081
template<class T>
192,614,748✔
2082
bool is_mesh_type(int32_t index)
2083
{
2084
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
157,350,348✔
2085
  return mesh;
2086
}
2087

118,328,544✔
2088
//==============================================================================
2089
// C API functions
2090
//==============================================================================
2091

118,328,544✔
2092
// Return the type of mesh as a C string
76,498,272✔
2093
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2094
{
41,830,272✔
2095
  if (int err = check_mesh(index))
2096
    return err;
2097

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

2100
  return 0;
2101
}
2102

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

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

2129
  return 0;
41,303,856✔
2130
}
41,303,856✔
2131

2132
//! Adds a new unstructured mesh to OpenMC
41,303,856✔
2133
extern "C" int openmc_add_unstructured_mesh(
11,940,900✔
2134
  const char filename[], const char library[], int* id)
2135
{
29,362,956✔
2136
  std::string lib_name(library);
2137
  std::string mesh_file(filename);
2138
  bool valid_lib = false;
29,362,956✔
2139

2140
#ifdef DAGMC
29,362,956✔
2141
  if (lib_name == MOABMesh::mesh_lib_type) {
5,763,384✔
2142
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
2143
    valid_lib = true;
23,599,572✔
2144
  }
2145
#endif
23,599,572✔
2146

11,077,812✔
2147
#ifdef LIBMESH
2148
  if (lib_name == LibMesh::mesh_lib_type) {
12,521,760✔
2149
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2150
    valid_lib = true;
2151
  }
120,063,312✔
2152
#endif
2153

2154
  if (!valid_lib) {
2155
    set_errmsg(fmt::format("Mesh library {} is not supported "
120,063,312✔
2156
                           "by this build of OpenMC",
76,498,272✔
2157
      lib_name));
2158
    return OPENMC_E_INVALID_ARGUMENT;
43,565,040✔
2159
  }
2160

43,565,040✔
2161
  // auto-assign new ID
2162
  model::meshes.back()->set_id(-1);
2163
  *id = model::meshes.back()->id_;
2164

2165
  return 0;
2166
}
2167

43,565,040✔
2168
//! Return the index in the meshes array of a mesh with a given ID
43,565,040✔
2169
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
2170
{
43,565,040✔
2171
  auto pair = model::mesh_map.find(id);
2172
  if (pair == model::mesh_map.end()) {
2173
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
43,565,040✔
2174
    return OPENMC_E_INVALID_ID;
43,309,776✔
2175
  }
2176
  *index = pair->second;
2177
  return 0;
43,309,776✔
2178
}
19,173,960✔
2179

2180
//! Return the ID of a mesh
2181
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
24,391,080✔
2182
{
2183
  if (int err = check_mesh(index))
2184
    return err;
360,246,600✔
2185
  *id = model::meshes[index]->id_;
2186
  return 0;
2187
}
2188

2189
//! Set the ID of a mesh
360,246,600✔
2190
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
241,050,672✔
2191
{
241,050,672✔
2192
  if (int err = check_mesh(index))
482,101,344✔
2193
    return err;
2194
  model::meshes[index]->id_ = id;
119,195,928✔
2195
  model::mesh_map[id] = index;
59,164,272✔
2196
  return 0;
59,164,272✔
2197
}
59,164,272✔
2198

118,328,544✔
2199
//! Get the number of elements in a mesh
2200
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
2201
{
60,031,656✔
2202
  if (int err = check_mesh(index))
60,031,656✔
2203
    return err;
60,031,656✔
2204
  *n = model::meshes[index]->n_bins();
120,063,312✔
2205
  return 0;
2206
}
2207

2208
//! Get the volume of each element in the mesh
365✔
2209
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
2210
{
365✔
2211
  if (int err = check_mesh(index))
365✔
2212
    return err;
365✔
2213
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
2214
    volumes[i] = model::meshes[index]->volume(i);
1,460✔
2215
  }
1,095✔
UNCOV
2216
  return 0;
×
2217
}
2218

×
2219
//! Get the bounding box of a mesh
2220
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
1,095✔
2221
{
2,190✔
UNCOV
2222
  if (int err = check_mesh(index))
×
2223
    return err;
2224

×
2225
  BoundingBox bbox = model::meshes[index]->bounding_box();
2226

1,095✔
UNCOV
2227
  // set lower left corner values
×
2228
  ll[0] = bbox.xmin;
2229
  ll[1] = bbox.ymin;
×
2230
  ll[2] = bbox.zmin;
2231

2232
  // set upper right corner values
365✔
UNCOV
2233
  ur[0] = bbox.xmax;
×
2234
  ur[1] = bbox.ymax;
2235
  ur[2] = bbox.zmax;
UNCOV
2236
  return 0;
×
2237
}
2238

365✔
UNCOV
2239
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
×
2240
  int nz, int table_size, int32_t* materials, double* volumes)
2241
{
×
2242
  if (int err = check_mesh(index))
2243
    return err;
2244

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

221,692,788✔
2257
  return 0;
2258
}
UNCOV
2259

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

2267
  int pixel_width = pixels[0];
2268
  int pixel_height = pixels[1];
2269

324✔
2270
  // get pixel size
2271
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
324✔
2272
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
324✔
2273

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

2296
  // set initial position
7,196✔
2297
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2298
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
7,196✔
UNCOV
2299

×
UNCOV
2300
#pragma omp parallel
×
2301
  {
2302
    Position r = xyz;
7,196✔
2303

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

×
UNCOV
2314
  return 0;
×
2315
}
2316

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

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

156✔
UNCOV
2337
  // Copy dimension
×
UNCOV
2338
  mesh->n_dimension_ = n;
×
2339
  std::copy(dims, dims + n, mesh->shape_.begin());
2340
  return 0;
156✔
2341
}
2342

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

×
2351
  if (m->lower_left_.dimension() == 0) {
2352
    set_errmsg("Mesh parameters have not been set.");
212✔
2353
    return OPENMC_E_ALLOCATE;
2354
  }
762✔
2355

2356
  *ll = m->lower_left_.data();
762✔
UNCOV
2357
  *ur = m->upper_right_.data();
×
2358
  *width = m->width_.data();
2359
  *n = m->n_dimension_;
762✔
2360
  return 0;
762✔
UNCOV
2361
}
×
UNCOV
2362

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

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

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

306✔
2394
  // Set material volumes
306✔
2395

306✔
2396
  // TODO: incorporate this into method in RegularMesh that can be called from
2397
  // here and from constructor
612✔
2398
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
306✔
2399
  m->element_volume_ = 1.0;
206✔
2400
  for (int i = 0; i < m->n_dimension_; i++) {
100✔
2401
    m->element_volume_ *= m->width_[i];
52✔
2402
  }
48✔
2403

24✔
2404
  return 0;
24✔
2405
}
24✔
UNCOV
2406

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

306✔
2416
  C* m = dynamic_cast<C*>(model::meshes[index].get());
306✔
2417

2418
  m->n_dimension_ = 3;
2419

×
2420
  m->grid_[0].reserve(nx);
2421
  m->grid_[1].reserve(ny);
2422
  m->grid_[2].reserve(nz);
×
2423

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

2434
  int err = m->set_grid();
2435
  return err;
2436
}
2437

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

2447
  if (m->lower_left_.dimension() == 0) {
2448
    set_errmsg("Mesh parameters have not been set.");
×
2449
    return OPENMC_E_ALLOCATE;
×
2450
  }
2451

×
2452
  *grid_x = m->grid_[0].data();
2453
  *nx = m->grid_[0].size();
2454
  *grid_y = m->grid_[1].data();
2455
  *ny = m->grid_[1].size();
498✔
2456
  *grid_z = m->grid_[2].data();
2457
  *nz = m->grid_[2].size();
498✔
2458

498✔
2459
  return 0;
×
2460
}
×
2461

2462
//! Get the rectilinear mesh grid
498✔
2463
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
498✔
2464
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2465
{
2466
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
2467
    index, grid_x, nx, grid_y, ny, grid_z, nz);
3,222✔
2468
}
2469

3,222✔
2470
//! Set the rectilienar mesh parameters
×
2471
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
3,222✔
2472
  const double* grid_x, const int nx, const double* grid_y, const int ny,
3,222✔
2473
  const double* grid_z, const int nz)
2474
{
2475
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
2476
    index, grid_x, nx, grid_y, ny, grid_z, nz);
306✔
2477
}
2478

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

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

96✔
2496
//! Get the spherical mesh grid
2497
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
96✔
2498
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2499
{
1,056✔
2500

960✔
2501
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2502
    index, grid_x, nx, grid_y, ny, grid_z, nz);
96✔
2503
  ;
2504
}
2505

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

156✔
2515
#ifdef DAGMC
156✔
2516

156✔
2517
const std::string MOABMesh::mesh_lib_type = "moab";
2518

2519
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
156✔
2520
{
156✔
2521
  initialize();
156✔
2522
}
156✔
2523

2524
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2525
{
168✔
2526
  filename_ = filename;
2527
  set_length_multiplier(length_multiplier);
2528
  initialize();
168✔
2529
}
×
2530

2531
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
2532
{
168✔
2533
  mbi_ = external_mbi;
2534
  filename_ = "unknown (external file)";
12✔
2535
  this->initialize();
12✔
2536
}
12✔
2537

12✔
2538
void MOABMesh::initialize()
2539
{
×
2540

2541
  // Create the MOAB interface and load data from file
12✔
2542
  this->create_interface();
2543

156✔
2544
  // Initialise MOAB error code
2545
  moab::ErrorCode rval = moab::MB_SUCCESS;
2546

48✔
2547
  // Set the dimension
2548
  n_dimension_ = 3;
2549

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

2556
  if (!ehs_.all_of_type(moab::MBTET)) {
2557
    warning("Non-tetrahedral elements found in unstructured "
48✔
2558
            "mesh file: " +
48✔
2559
            filename_);
2560
  }
2561

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

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

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

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

×
2609
  // Determine bounds of mesh
12✔
2610
  this->determine_bounds();
12✔
2611
}
12✔
2612

12✔
2613
void MOABMesh::prepare_for_point_location()
2614
{
2615
  // if the KDTree has already been constructed, do nothing
2616
  if (kdtree_)
230✔
2617
    return;
2618

2619
  // build acceleration data structures
230✔
2620
  compute_barycentric_data(ehs_);
×
2621
  build_kdtree(ehs_);
230✔
2622
}
2623

2624
void MOABMesh::create_interface()
230✔
2625
{
230✔
2626
  // Do not create a MOAB instance if one is already in memory
230✔
2627
  if (mbi_)
2628
    return;
2629

2630
  // create MOAB instance
254✔
2631
  mbi_ = std::make_shared<moab::Core>();
2632

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

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

266✔
2651
  if (!all_tris.all_of_type(moab::MBTRI)) {
2652
    warning("Non-triangle elements found in tet adjacencies in "
2653
            "unstructured mesh file: " +
266✔
2654
            filename_);
×
2655
  }
266✔
2656

2657
  // combine into one range
266✔
2658
  moab::Range all_tets_and_tris;
×
2659
  all_tets_and_tris.merge(all_tets);
×
2660
  all_tets_and_tris.merge(all_tris);
2661

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

242✔
2667
  // Determine what options to use
24✔
2668
  std::ostringstream options_stream;
12✔
2669
  if (options_.empty()) {
12✔
2670
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
12✔
2671
  } else {
12✔
2672
    options_stream << options_;
12✔
2673
  }
12✔
2674
  moab::FileOptions file_opts(options_stream.str().c_str());
12✔
2675

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

266✔
2685
void MOABMesh::intersect_track(const moab::CartVect& start,
266✔
2686
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
1,064✔
2687
{
798✔
2688
  hits.clear();
2689

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

2702
  // remove duplicate intersection distances
100✔
2703
  std::unique(hits.begin(), hits.end());
2704

100✔
2705
  // sorts by first component of std::pair by default
2706
  std::sort(hits.begin(), hits.end());
100✔
2707
}
100✔
2708

100✔
2709
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2710
  vector<int>& bins, vector<double>& lengths) const
680✔
2711
{
580✔
2712
  moab::CartVect start(r0.x, r0.y, r0.z);
2713
  moab::CartVect end(r1.x, r1.y, r1.z);
384✔
2714
  moab::CartVect dir(u.x, u.y, u.z);
284✔
2715
  dir.normalize();
2716

360✔
2717
  double track_len = (end - start).length();
260✔
2718
  if (track_len == 0.0)
2719
    return;
2720

100✔
2721
  start -= TINY_BIT * dir;
100✔
2722
  end += TINY_BIT * dir;
2723

24✔
2724
  vector<double> hits;
2725
  intersect_track(start, dir, track_len, hits);
2726

2727
  bins.clear();
24✔
2728
  lengths.clear();
×
2729

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

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

24✔
2756
    // determine the start point for this segment
×
2757
    current = r0 + u * hit;
2758

24✔
2759
    if (bin == -1) {
2760
      continue;
24✔
2761
    }
2762

24✔
2763
    bins.push_back(bin);
24✔
2764
    lengths.push_back(segment_length / track_len);
24✔
2765
  }
2766

96✔
2767
  // tally remaining portion of track after last hit if
72✔
2768
  // the last segment of the track is in the mesh but doesn't
2769
  // reach the other side of the tet
108✔
2770
  if (hits.back() < track_len) {
84✔
2771
    Position segment_start = r0 + u * hits.back();
2772
    double segment_length = track_len - hits.back();
84✔
2773
    Position midpoint = segment_start + u * (segment_length * 0.5);
60✔
2774
    int bin = this->get_bin(midpoint);
2775
    if (bin != -1) {
2776
      bins.push_back(bin);
24✔
2777
      lengths.push_back(segment_length / track_len);
24✔
2778
    }
2779
  }
52✔
2780
};
2781

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

52✔
2792
  // retrieve the tet elements of this leaf
52✔
2793
  moab::EntityHandle leaf = kdtree_iter.handle();
2794
  moab::Range tets;
488✔
2795
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
436✔
2796
  if (rval != moab::MB_SUCCESS) {
2797
    warning("MOAB error finding tets.");
180✔
2798
  }
128✔
2799

2800
  // loop over the tets in this leaf, returning the containing tet if found
168✔
2801
  for (const auto& tet : tets) {
116✔
2802
    if (point_in_tet(pos, tet)) {
2803
      return tet;
2804
    }
52✔
2805
  }
52✔
2806

2807
  // if no tet is found, return an invalid handle
2808
  return 0;
2809
}
2810

424✔
2811
double MOABMesh::volume(int bin) const
2812
{
2813
  return tet_volume(get_ent_handle_from_bin(bin));
424✔
2814
}
×
2815

424✔
2816
std::string MOABMesh::library() const
2817
{
424✔
2818
  return mesh_lib_type;
×
2819
}
×
2820

2821
// Sample position within a tet for MOAB type tets
2822
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
424✔
2823
{
424✔
2824

424✔
2825
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
424✔
2826

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

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

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

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

2864
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
132✔
2865
}
132✔
2866

132✔
2867
int MOABMesh::get_bin(Position r) const
132✔
2868
{
132✔
2869
  moab::EntityHandle tet = get_tet(r);
132✔
2870
  if (tet == 0) {
2871
    return -1;
132✔
2872
  } else {
2873
    return get_bin_from_ent_handle(tet);
160✔
2874
  }
2875
}
2876

160✔
2877
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
×
2878
{
160✔
2879
  moab::ErrorCode rval;
2880

160✔
2881
  baryc_data_.clear();
×
2882
  baryc_data_.resize(tets.size());
×
2883

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

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

2899
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
160✔
2900

160✔
2901
    // invert now to avoid this cost later
2902
    a = a.transpose().inverse();
2903
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
2904
  }
52✔
2905
}
2906

2907
bool MOABMesh::point_in_tet(
2908
  const moab::CartVect& r, moab::EntityHandle tet) const
52✔
2909
{
52✔
2910

2911
  moab::ErrorCode rval;
2912

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

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

2932
  // look up barycentric data
2933
  int idx = get_bin_from_ent_handle(tet);
2934
  const moab::Matrix3& a_inv = baryc_data_[idx];
132✔
2935

132✔
2936
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2937

2938
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
2939
          bary_coords[2] >= 0.0 &&
2940
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
24✔
2941
}
2942

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

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

2958
int MOABMesh::get_index_from_bin(int bin) const
2959
{
2960
  return bin;
2961
}
2962

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

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

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

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

24✔
2997
int MOABMesh::n_bins() const
24✔
2998
{
24✔
2999
  return ehs_.size();
3000
}
3001

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

24✔
3015
Position MOABMesh::centroid(int bin) const
3016
{
3017
  moab::ErrorCode rval;
3018

3019
  auto tet = this->get_ent_handle_from_bin(bin);
3020

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

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

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

24✔
3044
  return {centroid[0], centroid[1], centroid[2]};
24✔
3045
}
3046

20✔
3047
int MOABMesh::n_vertices() const
3048
{
3049
  return verts_.size();
20✔
3050
}
3051

3052
Position MOABMesh::vertex(int id) const
3053
{
20✔
3054

20✔
3055
  moab::ErrorCode rval;
3056

3057
  moab::EntityHandle vert = verts_[id];
24✔
3058

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

23✔
3065
  return {coords[0], coords[1], coords[2]};
3066
}
3067

23✔
3068
std::vector<int> MOABMesh::connectivity(int bin) const
23✔
3069
{
3070
  moab::ErrorCode rval;
3071

3072
  auto tet = get_ent_handle_from_bin(bin);
3073

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

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

3087
  return verts;
3088
}
3089

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

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

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

1,542,122✔
3125
  // return the populated tag handles
3126
  return {value_tag, error_tag};
3127
}
3128

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

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

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

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

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

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

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

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

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

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

3215
#endif
7,313,035✔
3216

3217
#ifdef LIBMESH
7,313,035✔
3218

3219
const std::string LibMesh::mesh_lib_type = "libmesh";
7,313,035✔
3220

7,313,035✔
3221
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
7,313,035✔
3222
{
1,010,077✔
3223
  // filename_ and length_multiplier_ will already be set by the
3224
  // UnstructuredMesh constructor
3225
  set_mesh_pointer_from_filename(filename_);
3226
  set_length_multiplier(length_multiplier_);
6,302,958✔
3227
  initialize();
6,302,958✔
3228
}
6,302,958✔
3229

6,302,958✔
3230
// create the mesh from a pointer to a libMesh Mesh
3231
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
3232
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
3233
{
3234
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
260,145,730✔
3235
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
260,142,884✔
3236
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
6,300,112✔
3237
  }
3238

3239
  m_ = &input_mesh;
3240
  set_length_multiplier(length_multiplier);
3241
  initialize();
2,846✔
3242
}
7,313,035✔
3243

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

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

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

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

3279
  // assuming that unstructured meshes used in OpenMC are 3D
3280
  n_dimension_ = 3;
400,820✔
3281

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

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

335,712✔
3298
  for (int i = 0; i < num_threads(); i++) {
167,856✔
3299
    pl_.emplace_back(m_->sub_point_locator());
3300
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
7,313,035✔
3301
    pl_.back()->enable_out_of_mesh_mode();
3302
  }
7,313,035✔
3303

7,313,035✔
3304
  // store first element in the mesh to use as an offset for bin indices
1,012,923✔
3305
  auto first_elem = *m_->elements_begin();
3306
  first_element_id_ = first_elem->id();
6,300,112✔
3307

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

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

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

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

3345
Position LibMesh::centroid(int bin) const
3346
{
3347
  const auto& elem = this->get_element_from_bin(bin);
260,142,884✔
3348
  auto centroid = elem.vertex_average();
260,142,884✔
3349
  return {centroid(0), centroid(1), centroid(2)};
260,142,884✔
3350
}
3351

3352
int LibMesh::n_vertices() const
3353
{
3354
  return m_->n_nodes();
3355
}
3356

260,142,884✔
3357
Position LibMesh::vertex(int vertex_id) const
260,142,884✔
3358
{
260,142,884✔
3359
  const auto node_ref = m_->node_ref(vertex_id);
3360
  return {node_ref(0), node_ref(1), node_ref(2)};
3361
}
3362

3363
std::vector<int> LibMesh::connectivity(int elem_id) const
3364
{
3365
  std::vector<int> conn;
3366
  const auto* elem_ptr = m_->elem_ptr(elem_id);
260,142,884✔
3367
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
260,142,884✔
3368
    conn.push_back(elem_ptr->node_id(i));
3369
  }
260,142,884✔
3370
  return conn;
3371
}
421,311,194✔
3372

442,992,623✔
3373
std::string LibMesh::library() const
281,824,313✔
3374
{
260,142,884✔
3375
  return mesh_lib_type;
3376
}
3377

3378
int LibMesh::n_bins() const
3379
{
3380
  return m_->n_active_elem();
3381
}
3382

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

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

3408
    return;
3409
  }
3410

815,424✔
3411
  if (!equation_systems_) {
3412
    build_eqn_sys();
3413
  }
266,682,708✔
3414

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

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

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

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

3451
    return;
3452
  }
3453

3454
  if (!equation_systems_) {
3455
    build_eqn_sys();
3456
  }
3457

3458
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3459

3460
  if (!eqn_sys.is_initialized()) {
3461
    equation_systems_->init();
3462
  }
3463

3464
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3465

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

3473
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3474
       it++) {
3475
    if (!(*it)->active()) {
3476
      continue;
3477
    }
3478

3479
    auto bin = get_bin_from_element(*it);
3480

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

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

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

203,856✔
3502
    return;
3503
  }
3504

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

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

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

3525
  // quick rejection check
3526
  if (!bbox_.contains_point(p)) {
3527
    return -1;
3528
  }
3529

3530
  const auto& point_locator = pl_.at(thread_num());
3531

3532
  const auto elem_ptr = (*point_locator)(p);
3533
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3534
}
3535

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

3546
std::pair<vector<double>, vector<double>> LibMesh::plot(
3547
  Position plot_ll, Position plot_ur) const
3548
{
3549
  return {};
3550
}
3551

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

3557
double LibMesh::volume(int bin) const
3558
{
3559
  return this->get_element_from_bin(bin).volume();
3560
}
3561

3562
#endif // LIBMESH
3563

3564
//==============================================================================
3565
// Non-member functions
3566
//==============================================================================
3567

3568
void read_meshes(pugi::xml_node root)
3569
{
3570
  std::unordered_set<int> mesh_ids;
3571

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

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

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

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

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

3630
    // Map ID to position in vector
3631
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3632
  }
3633
}
3634

3635
void meshes_to_hdf5(hid_t group)
3636
{
3637
  // Write number of meshes
3638
  hid_t meshes_group = create_group(group, "meshes");
3639
  int32_t n_meshes = model::meshes.size();
3640
  write_attribute(meshes_group, "n_meshes", n_meshes);
3641

3642
  if (n_meshes > 0) {
3643
    // Write IDs of meshes
3644
    vector<int> ids;
3645
    for (const auto& m : model::meshes) {
3646
      m->to_hdf5(meshes_group);
3647
      ids.push_back(m->id_);
3648
    }
3649
    write_attribute(meshes_group, "ids", ids);
3650
  }
3651

3652
  close_group(meshes_group);
3653
}
3654

23✔
3655
void free_memory_mesh()
3656
{
3657
  model::meshes.clear();
3658
  model::mesh_map.clear();
23✔
3659
}
23✔
3660

23✔
3661
extern "C" int n_meshes()
23✔
3662
{
3663
  return model::meshes.size();
3664
}
3665

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