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

openmc-dev / openmc / 12971699935

26 Jan 2025 05:13AM UTC coverage: 84.856% (-0.009%) from 84.865%
12971699935

Pull #3129

github

web-flow
Merge f8a88a3c9 into 2bea7f338
Pull Request #3129: Compute material volumes in mesh elements based on raytracing

204 of 230 new or added lines in 3 files covered. (88.7%)

772 existing lines in 29 files now uncovered.

50223 of 59186 relevant lines covered (84.86%)

35109960.84 hits per line

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

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

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

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

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

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

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

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

60
namespace openmc {
61

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

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

72
namespace model {
73

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

77
} // namespace model
78

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

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

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

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

113
//==============================================================================
114
// MaterialVolumes implementation
115
//==============================================================================
116

117
namespace detail {
118

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

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

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

144
void MaterialVolumes::add_volume(
2,475,156✔
145
  int index_elem, int index_material, double volume)
146
{
147
  // Loop for linear probing
148
  for (int attempt = 0; attempt < table_size_; ++attempt) {
2,543,388✔
149
    // Determine slot to check
150
    int slot = (index_material + attempt) % table_size_;
2,543,388✔
151

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

155
    // Found the desired material; accumulate volume
156
    if (current_val == index_material) {
2,543,388✔
157
#pragma omp atomic
1,237,144✔
158
      this->volumes(index_elem, slot) += volume;
2,473,786✔
159
      return;
2,473,786✔
160
    }
161

162
    // Slot appears to be empty; attempt to claim
163
    if (current_val == EMPTY) {
69,602✔
164
      // Attempt compare-and-swap from EMPTY to index_material
165
      bool claimed_slot = atomic_cas_int32(&current_val, EMPTY, index_material);
1,370✔
166

167
      // If we claimed the slot or another thread claimed it but they inserted
168
      // the same material, proceed to accumulate
169
      if (claimed_slot || (current_val == index_material)) {
1,370✔
170
#pragma omp atomic
687✔
171
        this->volumes(index_elem, slot) += volume;
1,370✔
172
        return;
1,370✔
173
      }
174
    }
175
  }
176

177
  // If table is full, set a flag that can be checked later
NEW
178
  too_many_mats_ = true;
×
179
}
180

NEW
181
void MaterialVolumes::add_volume_unsafe(
×
182
  int index_elem, int index_material, double volume)
183
{
184
  // Linear probe
NEW
185
  for (int attempt = 0; attempt < table_size_; ++attempt) {
×
NEW
186
    int slot = (index_material + attempt) % table_size_;
×
187

188
    // Read current material
NEW
189
    int32_t current_val = this->materials(index_elem, slot);
×
190

191
    // Found the desired material; accumulate volume
NEW
192
    if (current_val == index_material) {
×
NEW
193
      this->volumes(index_elem, slot) += volume;
×
NEW
194
      return;
×
195
    }
196

197
    // Claim empty slot
NEW
198
    if (current_val == EMPTY) {
×
NEW
199
      this->materials(index_elem, slot) = index_material;
×
NEW
200
      this->volumes(index_elem, slot) += volume;
×
NEW
201
      return;
×
202
    }
203
  }
204

205
  // If table is full, set a flag that can be checked later
NEW
206
  too_many_mats_ = true;
×
207
}
208

209
} // namespace detail
210

211
//==============================================================================
212
// Mesh implementation
213
//==============================================================================
214

215
Mesh::Mesh(pugi::xml_node node)
2,900✔
216
{
217
  // Read mesh id
218
  id_ = std::stoi(get_node_value(node, "id"));
2,900✔
219
  if (check_for_node(node, "name"))
2,900✔
220
    name_ = get_node_value(node, "name");
17✔
221
}
2,900✔
222

223
void Mesh::set_id(int32_t id)
1✔
224
{
225
  Expects(id >= 0 || id == C_NONE);
1✔
226

227
  // Clear entry in mesh map in case one was already assigned
228
  if (id_ != C_NONE) {
1✔
229
    model::mesh_map.erase(id_);
×
230
    id_ = C_NONE;
×
231
  }
232

233
  // Ensure no other mesh has the same ID
234
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
1✔
235
    throw std::runtime_error {
×
236
      fmt::format("Two meshes have the same ID: {}", id)};
×
237
  }
238

239
  // If no ID is specified, auto-assign the next ID in the sequence
240
  if (id == C_NONE) {
1✔
241
    id = 0;
1✔
242
    for (const auto& m : model::meshes) {
3✔
243
      id = std::max(id, m->id_);
2✔
244
    }
245
    ++id;
1✔
246
  }
247

248
  // Update ID and entry in the mesh map
249
  id_ = id;
1✔
250
  model::mesh_map[id] = model::meshes.size() - 1;
1✔
251
}
1✔
252

253
vector<double> Mesh::volumes() const
168✔
254
{
255
  vector<double> volumes(n_bins());
168✔
256
  for (int i = 0; i < n_bins(); i++) {
983,988✔
257
    volumes[i] = this->volume(i);
983,820✔
258
  }
259
  return volumes;
168✔
260
}
×
261

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

276
  Timer timer;
156✔
277
  timer.start();
156✔
278

279
  // Create object for keeping track of materials/volumes
280
  detail::MaterialVolumes result(materials, volumes, table_size);
156✔
281

282
  // Determine bounding box
283
  auto bbox = this->bounding_box();
156✔
284

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

287
  // Determine effective width of rays
288
  Position width((bbox.xmax - bbox.xmin) / nx, (bbox.ymax - bbox.ymin) / ny,
156✔
289
    (bbox.zmax - bbox.zmin) / nz);
156✔
290

291
  // Set flag for mesh being contained within model
292
  bool out_of_model = false;
156✔
293

294
#pragma omp parallel
78✔
295
  {
296
    // Preallocate vector for mesh indices and lenght fractions and p
297
    std::vector<int> bins;
78✔
298
    std::vector<double> length_fractions;
78✔
299
    Particle p;
78✔
300

301
    SourceSite site;
78✔
302
    site.E = 1.0;
78✔
303
    site.particle = ParticleType::neutron;
78✔
304

305
    for (int axis = 0; axis < 3; ++axis) {
312✔
306
      // Set starting position and direction
307
      site.r = {0.0, 0.0, 0.0};
234✔
308
      site.r[axis] = bbox.min()[axis];
234✔
309
      site.u = {0.0, 0.0, 0.0};
234✔
310
      site.u[axis] = 1.0;
234✔
311

312
      // Determine width of rays and number of rays in other directions
313
      int ax1 = (axis + 1) % 3;
234✔
314
      int ax2 = (axis + 2) % 3;
234✔
315
      double min1 = bbox.min()[ax1];
234✔
316
      double min2 = bbox.min()[ax2];
234✔
317
      double d1 = width[ax1];
234✔
318
      double d2 = width[ax2];
234✔
319
      int n1 = n_rays[ax1];
234✔
320
      int n2 = n_rays[ax2];
234✔
321

322
      // Divide rays in first direction over MPI processes by computing starting
323
      // and ending indices
324
      int min_work = n1 / mpi::n_procs;
234✔
325
      int remainder = n1 % mpi::n_procs;
234✔
326
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
234✔
327
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
234✔
328
      int i1_end = i1_start + n1_local;
234✔
329

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

337
          p.from_source(&site);
494,274✔
338

339
          // Determine particle's location
340
          if (!exhaustive_find_cell(p)) {
494,274✔
341
            out_of_model = true;
47,916✔
342
            continue;
47,916✔
343
          }
344

345
          // Set birth cell attribute
346
          if (p.cell_born() == C_NONE)
446,358✔
347
            p.cell_born() = p.lowest_coord().cell;
446,358✔
348

349
          // Initialize last cells from current cell
350
          for (int j = 0; j < p.n_coord(); ++j) {
892,716✔
351
            p.cell_last(j) = p.coord(j).cell;
446,358✔
352
          }
353
          p.n_coord_last() = p.n_coord();
446,358✔
354

355
          while (true) {
356
            // Ray trace from r_start to r_end
357
            Position r0 = p.r();
973,974✔
358
            double max_distance = bbox.max()[axis] - r0[axis];
973,974✔
359

360
            // Find the distance to the nearest boundary
361
            BoundaryInfo boundary = distance_to_boundary(p);
973,974✔
362

363
            // Advance particle forward
364
            double distance = std::min(boundary.distance, max_distance);
973,974✔
365
            p.move_distance(distance);
973,974✔
366

367
            // Determine what mesh elements were crossed by particle
368
            bins.clear();
973,974✔
369
            length_fractions.clear();
973,974✔
370
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
973,974✔
371

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

381
              // Add volume to result
382
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,237,578✔
383
            }
384

385
            if (distance == max_distance)
973,974✔
386
              break;
446,358✔
387

388
            for (int j = 0; j < p.n_coord(); ++j) {
1,055,232✔
389
              p.cell_last(j) = p.coord(j).cell;
527,616✔
390
            }
391
            p.n_coord_last() = p.n_coord();
527,616✔
392

393
            // Set surface that particle is on and adjust coordinate levels
394
            p.surface() = boundary.surface;
527,616✔
395
            p.n_coord() = boundary.coord_level;
527,616✔
396

397
            if (boundary.lattice_translation[0] != 0 ||
527,616✔
398
                boundary.lattice_translation[1] != 0 ||
1,055,232✔
399
                boundary.lattice_translation[2] != 0) {
527,616✔
400
              // Particle crosses lattice boundary
401

402
              cross_lattice(p, boundary);
403
            } else {
404
              // Particle crosses surface
405
              const auto& surf {model::surfaces[p.surface_index()].get()};
527,616✔
406
              p.cross_surface(*surf);
527,616✔
407
            }
408
          }
527,616✔
409
        }
410
      }
411
    }
412
  }
78✔
413

414
  // Check for errors
415
  if (out_of_model) {
156✔
416
    throw std::runtime_error("Mesh not fully contained in geometry.");
12✔
417
  } else if (result.too_many_mats()) {
144✔
NEW
418
    throw std::runtime_error("Maximum number of materials for mesh material "
×
NEW
419
                             "volume calculation insufficient.");
×
420
  }
421

422
  // Compute time for raytracing
423
  double t_raytrace = timer.elapsed();
144✔
424

425
#ifdef OPENMC_MPI
426
  // Combine results from multiple MPI processes
427
  if (mpi::n_procs > 1) {
60✔
428
    int total = this->n_bins() * table_size;
429
    if (mpi::master) {
430
      // Allocate temporary buffer for receiving data
431
      std::vector<int32_t> mats(total);
432
      std::vector<double> vols(total);
433

434
      for (int i = 1; i < mpi::n_procs; ++i) {
435
        // Receive material indices and volumes from process i
436
        MPI_Recv(
437
          mats.data(), total, MPI_INT, i, i, mpi::intracomm, MPI_STATUS_IGNORE);
438
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
439
          MPI_STATUS_IGNORE);
440

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

458
  // Report time for MPI communication
459
  double t_mpi = timer.elapsed() - t_raytrace;
60✔
460
#else
461
  double t_mpi = 0.0;
84✔
462
#endif
463

464
  // Normalize based on known volumes of elements
465
  for (int i = 0; i < this->n_bins(); ++i) {
1,020✔
466
    // Estimated total volume in element i
467
    double volume = 0.0;
876✔
468
    for (int j = 0; j < table_size; ++j) {
7,884✔
469
      volume += result.volumes(i, j);
7,008✔
470
    }
471

472
    // Renormalize volumes based on known volume of element i
473
    double norm = this->volume(i) / volume;
876✔
474
    for (int j = 0; j < table_size; ++j) {
7,884✔
475
      result.volumes(i, j) *= norm;
7,008✔
476
    }
477
  }
478

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

494
void Mesh::to_hdf5(hid_t group) const
3,144✔
495
{
496
  // Create group for mesh
497
  std::string group_name = fmt::format("mesh {}", id_);
5,712✔
498
  hid_t mesh_group = create_group(group, group_name.c_str());
3,144✔
499

500
  // Write mesh type
501
  write_dataset(mesh_group, "type", this->get_mesh_type());
3,144✔
502

503
  // Write mesh ID
504
  write_attribute(mesh_group, "id", id_);
3,144✔
505

506
  // Write mesh name
507
  write_dataset(mesh_group, "name", name_);
3,144✔
508

509
  // Write mesh data
510
  this->to_hdf5_inner(mesh_group);
3,144✔
511

512
  // Close group
513
  close_group(mesh_group);
3,144✔
514
}
3,144✔
515

516
//==============================================================================
517
// Structured Mesh implementation
518
//==============================================================================
519

520
std::string StructuredMesh::bin_label(int bin) const
5,443,548✔
521
{
522
  MeshIndex ijk = get_indices_from_bin(bin);
5,443,548✔
523

524
  if (n_dimension_ > 2) {
5,443,548✔
525
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,854,744✔
526
  } else if (n_dimension_ > 1) {
16,176✔
527
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,752✔
528
  } else {
529
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
530
  }
531
}
532

533
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
3,126✔
534
{
535
  // because method is const, shape_ is const as well and can't be adapted
536
  auto tmp_shape = shape_;
3,126✔
537
  return xt::adapt(tmp_shape, {n_dimension_});
6,252✔
538
}
539

540
Position StructuredMesh::sample_element(
1,542,372✔
541
  const MeshIndex& ijk, uint64_t* seed) const
542
{
543
  // lookup the lower/upper bounds for the mesh element
544
  double x_min = negative_grid_boundary(ijk, 0);
1,542,372✔
545
  double x_max = positive_grid_boundary(ijk, 0);
1,542,372✔
546

547
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
548
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
549

550
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
551
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
552

553
  return {x_min + (x_max - x_min) * prn(seed),
1,542,372✔
554
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,542,372✔
555
}
556

557
//==============================================================================
558
// Unstructured Mesh implementation
559
//==============================================================================
560

561
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
45✔
562
{
563
  // check the mesh type
564
  if (check_for_node(node, "type")) {
45✔
565
    auto temp = get_node_value(node, "type", true, true);
45✔
566
    if (temp != mesh_type) {
45✔
567
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
568
    }
569
  }
45✔
570

571
  // check if a length unit multiplier was specified
572
  if (check_for_node(node, "length_multiplier")) {
45✔
573
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
574
  }
575

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

587
  if (check_for_node(node, "options")) {
45✔
588
    options_ = get_node_value(node, "options");
16✔
589
  }
590

591
  // check if mesh tally data should be written with
592
  // statepoint files
593
  if (check_for_node(node, "output")) {
45✔
594
    output_ = get_node_value_bool(node, "output");
×
595
  }
596
}
45✔
597

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

620
Position UnstructuredMesh::sample_tet(
601,230✔
621
  std::array<Position, 4> coords, uint64_t* seed) const
622
{
623
  // Uniform distribution
624
  double s = prn(seed);
601,230✔
625
  double t = prn(seed);
601,230✔
626
  double u = prn(seed);
601,230✔
627

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

650
const std::string UnstructuredMesh::mesh_type = "unstructured";
651

652
std::string UnstructuredMesh::get_mesh_type() const
30✔
653
{
654
  return mesh_type;
30✔
655
}
656

657
void UnstructuredMesh::surface_bins_crossed(
×
658
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
659
{
660
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
661
}
662

663
std::string UnstructuredMesh::bin_label(int bin) const
193,712✔
664
{
665
  return fmt::format("Mesh Index ({})", bin);
193,712✔
666
};
667

668
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
30✔
669
{
670
  write_dataset(mesh_group, "filename", filename_);
30✔
671
  write_dataset(mesh_group, "library", this->library());
30✔
672
  if (!options_.empty()) {
30✔
673
    write_attribute(mesh_group, "options", options_);
8✔
674
  }
675

676
  if (length_multiplier_ > 0.0)
30✔
677
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
678

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

687
  int num_elem_skipped = 0;
30✔
688

689
  // write element types and connectivity
690
  vector<double> volumes;
30✔
691
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
30✔
692
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
30✔
693
  for (int i = 0; i < this->n_bins(); i++) {
337,742✔
694
    auto conn = this->connectivity(i);
337,712✔
695

696
    volumes.emplace_back(this->volume(i));
337,712✔
697

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

718
  // warn users that some elements were skipped
719
  if (num_elem_skipped > 0) {
30✔
720
    warning(fmt::format("The connectivity of {} elements "
×
721
                        "on mesh {} were not written "
722
                        "because they are not of type linear tet/hex.",
723
      num_elem_skipped, this->id_));
×
724
  }
725

726
  write_dataset(mesh_group, "volumes", volumes);
30✔
727
  write_dataset(mesh_group, "connectivity", connectivity);
30✔
728
  write_dataset(mesh_group, "element_types", elem_types);
30✔
729
}
30✔
730

731
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
732
{
733
  length_multiplier_ = length_multiplier;
23✔
734
}
23✔
735

736
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
737
{
738
  auto conn = connectivity(bin);
120,000✔
739

740
  if (conn.size() == 4)
120,000✔
741
    return ElementType::LINEAR_TET;
120,000✔
742
  else if (conn.size() == 8)
×
743
    return ElementType::LINEAR_HEX;
×
744
  else
745
    return ElementType::UNSUPPORTED;
×
746
}
120,000✔
747

748
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,059,655,314✔
749
  Position r, bool& in_mesh) const
750
{
751
  MeshIndex ijk;
752
  in_mesh = true;
1,059,655,314✔
753
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
754
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
755

756
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
757
      in_mesh = false;
91,752,208✔
758
  }
759
  return ijk;
1,059,655,314✔
760
}
761

762
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,108,787,222✔
763
{
764
  switch (n_dimension_) {
1,108,787,222✔
765
  case 1:
956,736✔
766
    return ijk[0] - 1;
956,736✔
767
  case 2:
21,602,040✔
768
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,602,040✔
769
  case 3:
1,086,228,446✔
770
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,086,228,446✔
771
  default:
×
772
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
773
  }
774
}
775

776
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,067,576✔
777
{
778
  MeshIndex ijk;
779
  if (n_dimension_ == 1) {
8,067,576✔
780
    ijk[0] = bin + 1;
300✔
781
  } else if (n_dimension_ == 2) {
8,067,276✔
782
    ijk[0] = bin % shape_[0] + 1;
15,876✔
783
    ijk[1] = bin / shape_[0] + 1;
15,876✔
784
  } else if (n_dimension_ == 3) {
8,051,400✔
785
    ijk[0] = bin % shape_[0] + 1;
8,051,400✔
786
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,051,400✔
787
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,051,400✔
788
  }
789
  return ijk;
8,067,576✔
790
}
791

792
int StructuredMesh::get_bin(Position r) const
339,226,150✔
793
{
794
  // Determine indices
795
  bool in_mesh;
796
  MeshIndex ijk = get_indices(r, in_mesh);
339,226,150✔
797
  if (!in_mesh)
339,226,150✔
798
    return -1;
22,164,978✔
799

800
  // Convert indices to bin
801
  return get_bin_from_indices(ijk);
317,061,172✔
802
}
803

804
int StructuredMesh::n_bins() const
1,003,238✔
805
{
806
  return std::accumulate(
1,003,238✔
807
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,006,476✔
808
}
809

810
int StructuredMesh::n_surface_bins() const
608✔
811
{
812
  return 4 * n_dimension_ * n_bins();
608✔
813
}
814

815
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
816
  const SourceSite* bank, int64_t length, bool* outside) const
817
{
818
  // Determine shape of array for counts
819
  std::size_t m = this->n_bins();
×
820
  vector<std::size_t> shape = {m};
×
821

822
  // Create array of zeros
823
  xt::xarray<double> cnt {shape, 0.0};
×
824
  bool outside_ = false;
×
825

826
  for (int64_t i = 0; i < length; i++) {
×
827
    const auto& site = bank[i];
×
828

829
    // determine scoring bin for entropy mesh
830
    int mesh_bin = get_bin(site.r);
×
831

832
    // if outside mesh, skip particle
833
    if (mesh_bin < 0) {
×
834
      outside_ = true;
×
835
      continue;
×
836
    }
837

838
    // Add to appropriate bin
839
    cnt(mesh_bin) += site.wgt;
×
840
  }
841

842
  // Create copy of count data. Since ownership will be acquired by xtensor,
843
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
844
  // warnings.
845
  int total = cnt.size();
×
846
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
847

848
#ifdef OPENMC_MPI
849
  // collect values from all processors
850
  MPI_Reduce(
851
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
852

853
  // Check if there were sites outside the mesh for any processor
854
  if (outside) {
855
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
856
  }
857
#else
858
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
859
  if (outside)
860
    *outside = outside_;
861
#endif
862

863
  // Adapt reduced values in array back into an xarray
864
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
865
  xt::xarray<double> counts = arr;
×
866

867
  return counts;
×
868
}
869

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

883
  // Compute the length of the entire track.
884
  double total_distance = (r1 - r0).norm();
724,689,934✔
885
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
724,689,934✔
886
    return;
9,892,814✔
887

888
  const int n = n_dimension_;
714,797,120✔
889

890
  // Flag if position is inside the mesh
891
  bool in_mesh;
892

893
  // Position is r = r0 + u * traveled_distance, start at r0
894
  double traveled_distance {0.0};
714,797,120✔
895

896
  // Calculate index of current cell. Offset the position a tiny bit in
897
  // direction of flight
898
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
714,797,120✔
899

900
  // if track is very short, assume that it is completely inside one cell.
901
  // Only the current cell will score and no surfaces
902
  if (total_distance < 2 * TINY_BIT) {
714,797,120✔
903
    if (in_mesh) {
12✔
904
      tally.track(ijk, 1.0);
×
905
    }
906
    return;
12✔
907
  }
908

909
  // translate start and end positions,
910
  // this needs to come after the get_indices call because it does its own
911
  // translation
912
  local_coords(r0);
714,797,108✔
913
  local_coords(r1);
714,797,108✔
914

915
  // Calculate initial distances to next surfaces in all three dimensions
916
  std::array<MeshDistance, 3> distances;
1,429,594,216✔
917
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
918
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
2,126,359,284✔
919
  }
920

921
  // Loop until r = r1 is eventually reached
922
  while (true) {
366,706,332✔
923

924
    if (in_mesh) {
1,081,503,440✔
925

926
      // find surface with minimal distance to current position
927
      const auto k = std::min_element(distances.begin(), distances.end()) -
987,736,072✔
928
                     distances.begin();
987,736,072✔
929

930
      // Tally track length delta since last step
931
      tally.track(ijk,
987,736,072✔
932
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
987,736,072✔
933
          total_distance);
934

935
      // update position and leave, if we have reached end position
936
      traveled_distance = distances[k].distance;
987,736,072✔
937
      if (traveled_distance >= total_distance)
987,736,072✔
938
        return;
626,661,784✔
939

940
      // If we have not reached r1, we have hit a surface. Tally outward
941
      // current
942
      tally.surface(ijk, k, distances[k].max_surface, false);
361,074,288✔
943

944
      // Update cell and calculate distance to next surface in k-direction.
945
      // The two other directions are still valid!
946
      ijk[k] = distances[k].next_index;
361,074,288✔
947
      distances[k] =
361,074,288✔
948
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
361,074,288✔
949

950
      // Check if we have left the interior of the mesh
951
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
361,074,288✔
952

953
      // If we are still inside the mesh, tally inward current for the next
954
      // cell
955
      if (in_mesh)
361,074,288✔
956
        tally.surface(ijk, k, !distances[k].max_surface, true);
334,742,986✔
957

958
    } else { // not inside mesh
959

960
      // For all directions outside the mesh, find the distance that we need
961
      // to travel to reach the next surface. Use the largest distance, as
962
      // only this will cross all outer surfaces.
963
      int k_max {0};
93,767,368✔
964
      for (int k = 0; k < n; ++k) {
372,728,086✔
965
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
374,070,660✔
966
            (distances[k].distance > traveled_distance)) {
95,109,942✔
967
          traveled_distance = distances[k].distance;
94,324,182✔
968
          k_max = k;
94,324,182✔
969
        }
970
      }
971

972
      // If r1 is not inside the mesh, exit here
973
      if (traveled_distance >= total_distance)
93,767,368✔
974
        return;
88,135,324✔
975

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

984
      // If inside the mesh, Tally inward current
985
      if (in_mesh)
5,632,044✔
986
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
4,254,984✔
987
    }
988
  }
989
}
990

260,622,556✔
991
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
992
  vector<int>& bins, vector<double>& lengths) const
993
{
994

995
  // Helper tally class.
996
  // stores a pointer to the mesh class and references to bins and lengths
997
  // parameters. Performs the actual tally through the track method.
998
  struct TrackAggregator {
999
    TrackAggregator(
1000
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
260,622,556✔
1001
      : mesh(_mesh), bins(_bins), lengths(_lengths)
260,622,556✔
1002
    {}
×
1003
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
1004
    void track(const MeshIndex& ijk, double l) const
260,622,556✔
1005
    {
1006
      bins.push_back(mesh->get_bin_from_indices(ijk));
1007
      lengths.push_back(l);
1008
    }
1009

1010
    const StructuredMesh* mesh;
260,622,556✔
1011
    vector<int>& bins;
1012
    vector<double>& lengths;
1013
  };
1014

260,622,556✔
1015
  // Perform the mesh raytrace with the helper class.
1016
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
1017
}
1018

260,622,556✔
1019
void StructuredMesh::surface_bins_crossed(
×
1020
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
1021
{
1022

×
1023
  // Helper tally class.
1024
  // stores a pointer to the mesh class and a reference to the bins parameter.
1025
  // Performs the actual tally through the surface method.
1026
  struct SurfaceAggregator {
1027
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
1028
      : mesh(_mesh), bins(_bins)
260,622,556✔
1029
    {}
260,622,556✔
1030
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
1031
    {
1032
      int i_bin =
521,245,112✔
1033
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,040,654,080✔
1034
      if (max)
780,031,524✔
1035
        i_bin += 2;
1036
      if (inward)
1037
        i_bin += 1;
1038
      bins.push_back(i_bin);
64,131,826✔
1039
    }
1040
    void track(const MeshIndex& idx, double l) const {}
324,754,382✔
1041

1042
    const StructuredMesh* mesh;
1043
    vector<int>& bins;
321,631,132✔
1044
  };
321,631,132✔
1045

1046
  // Perform the mesh raytrace with the helper class.
1047
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
321,631,132✔
1048
}
321,631,132✔
1049

1050
//==============================================================================
1051
// RegularMesh implementation
1052
//==============================================================================
321,631,132✔
1053

321,631,132✔
1054
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
257,742,894✔
1055
{
1056
  // Determine number of dimensions for mesh
1057
  if (!check_for_node(node, "dimension")) {
1058
    fatal_error("Must specify <dimension> on a regular mesh.");
63,888,238✔
1059
  }
1060

1061
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
1062
  int n = n_dimension_ = shape.size();
63,888,238✔
1063
  if (n != 1 && n != 2 && n != 3) {
63,888,238✔
1064
    fatal_error("Mesh must be one, two, or three dimensions.");
63,888,238✔
1065
  }
1066
  std::copy(shape.begin(), shape.end(), shape_.begin());
1067

63,888,238✔
1068
  // Check that dimensions are all greater than zero
1069
  if (xt::any(shape <= 0)) {
1070
    fatal_error("All entries on the <dimension> element for a tally "
1071
                "mesh must be positive.");
63,888,238✔
1072
  }
61,514,280✔
1073

1074
  // Check for lower-left coordinates
1075
  if (check_for_node(node, "lower_left")) {
1076
    // Read mesh lower-left corner location
1077
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1078
  } else {
1079
    fatal_error("Must specify <lower_left> on a mesh.");
3,123,250✔
1080
  }
12,141,700✔
1081

12,298,252✔
1082
  // Make sure lower_left and dimension match
3,279,802✔
1083
  if (shape.size() != lower_left_.size()) {
3,215,338✔
1084
    fatal_error("Number of entries on <lower_left> must be the same "
3,215,338✔
1085
                "as the number of entries on <dimension>.");
1086
  }
1087

1088
  if (check_for_node(node, "width")) {
1089
    // Make sure one of upper-right or width were specified
3,123,250✔
1090
    if (check_for_node(node, "upper_right")) {
2,879,662✔
1091
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1092
    }
1093

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

860,256✔
1096
    // Check to ensure width has same dimensions
616,668✔
1097
    auto n = width_.size();
616,668✔
1098
    if (n != lower_left_.size()) {
1099
      fatal_error("Number of entries on <width> must be the same as "
1100
                  "the number of entries on <lower_left>.");
1101
    }
243,588✔
1102

218,592✔
1103
    // Check for negative widths
1104
    if (xt::any(width_ < 0.0)) {
1105
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1106
    }
464,067,378✔
1107

1108
    // Set width and upper right coordinate
1109
    upper_right_ = xt::eval(lower_left_ + shape * width_);
1110

1111
  } else if (check_for_node(node, "upper_right")) {
1112
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1113

1114
    // Check to ensure width has same dimensions
1115
    auto n = upper_right_.size();
1116
    if (n != lower_left_.size()) {
464,067,378✔
1117
      fatal_error("Number of entries on <upper_right> must be the "
464,067,378✔
1118
                  "same as the number of entries on <lower_left>.");
9,892,814✔
1119
    }
1120

454,174,564✔
1121
    // Check that upper-right is above lower-left
1122
    if (xt::any(upper_right_ < lower_left_)) {
1123
      fatal_error("The <upper_right> coordinates must be greater than "
1124
                  "the <lower_left> coordinates on a tally mesh.");
1125
    }
1126

454,174,564✔
1127
    // Set width
1128
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1129
  } else {
1130
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
454,174,564✔
1131
  }
1132

1133
  // Set material volumes
1134
  volume_frac_ = 1.0 / xt::prod(shape)();
454,174,564✔
1135

12✔
UNCOV
1136
  element_volume_ = 1.0;
×
1137
  for (int i = 0; i < n_dimension_; i++) {
1138
    element_volume_ *= width_[i];
12✔
1139
  }
1140
}
1141

1142
int RegularMesh::get_index_in_direction(double r, int i) const
1143
{
1144
  return std::ceil((r - lower_left_[i]) / width_[i]);
454,174,552✔
1145
}
454,174,552✔
1146

1147
const std::string RegularMesh::mesh_type = "regular";
1148

908,349,104✔
1149
std::string RegularMesh::get_mesh_type() const
1,800,502,312✔
1150
{
1,346,327,760✔
1151
  return mesh_type;
1152
}
1153

1154
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
302,574,506✔
1155
{
1156
  return lower_left_[i] + ijk[i] * width_[i];
756,749,058✔
1157
}
1158

1159
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
666,104,940✔
1160
{
666,104,940✔
1161
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1162
}
1163

666,104,940✔
1164
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
666,104,940✔
1165
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1166
  double l) const
1167
{
1168
  MeshDistance d;
666,104,940✔
1169
  d.next_index = ijk[i];
666,104,940✔
1170
  if (std::abs(u[i]) < FP_PRECISION)
368,918,890✔
1171
    return d;
1172

1173
  d.max_surface = (u[i] > 0);
1174
  if (d.max_surface && (ijk[i] <= shape_[i])) {
297,186,050✔
1175
    d.next_index++;
1176
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1177
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1178
    d.next_index--;
297,186,050✔
1179
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
297,186,050✔
1180
  }
297,186,050✔
1181
  return d;
1182
}
1183

297,186,050✔
1184
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1185
  Position plot_ll, Position plot_ur) const
1186
{
1187
  // Figure out which axes lie in the plane of the plot.
297,186,050✔
1188
  array<int, 2> axes {-1, -1};
273,228,706✔
1189
  if (plot_ur.z == plot_ll.z) {
1190
    axes[0] = 0;
1191
    if (n_dimension_ > 1)
1192
      axes[1] = 1;
1193
  } else if (plot_ur.y == plot_ll.y) {
1194
    axes[0] = 0;
1195
    if (n_dimension_ > 2)
90,644,118✔
1196
      axes[1] = 2;
360,586,386✔
1197
  } else if (plot_ur.x == plot_ll.x) {
361,772,408✔
1198
    if (n_dimension_ > 1)
91,830,140✔
1199
      axes[0] = 1;
91,108,844✔
1200
    if (n_dimension_ > 2)
91,108,844✔
1201
      axes[1] = 2;
1202
  } else {
1203
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1204
  }
1205

90,644,118✔
1206
  // Get the coordinates of the mesh lines along both of the axes.
85,255,662✔
1207
  array<vector<double>, 2> axis_lines;
1208
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1209
    int axis = axes[i_ax];
1210
    if (axis == -1)
5,388,456✔
1211
      continue;
21,438,660✔
1212
    auto& lines {axis_lines[i_ax]};
16,050,204✔
1213

16,050,204✔
1214
    double coord = lower_left_[axis];
1215
    for (int i = 0; i < shape_[axis] + 1; ++i) {
1216
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1217
        lines.push_back(coord);
5,388,456✔
1218
      coord += width_[axis];
4,036,392✔
1219
    }
1220
  }
1221

1222
  return {axis_lines[0], axis_lines[1]};
1223
}
464,067,378✔
1224

1225
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1226
{
1227
  write_dataset(mesh_group, "dimension", get_x_shape());
1228
  write_dataset(mesh_group, "lower_left", lower_left_);
1229
  write_dataset(mesh_group, "upper_right", upper_right_);
1230
  write_dataset(mesh_group, "width", width_);
1231
}
464,067,378✔
1232

1233
xt::xtensor<double, 1> RegularMesh::count_sites(
464,067,378✔
1234
  const SourceSite* bank, int64_t length, bool* outside) const
464,067,378✔
1235
{
574,451,148✔
1236
  // Determine shape of array for counts
666,104,940✔
1237
  std::size_t m = this->n_bins();
1238
  vector<std::size_t> shape = {m};
666,104,940✔
1239

666,104,940✔
1240
  // Create array of zeros
666,104,940✔
1241
  xt::xarray<double> cnt {shape, 0.0};
1242
  bool outside_ = false;
1243

1244
  for (int64_t i = 0; i < length; i++) {
1245
    const auto& site = bank[i];
1246

1247
    // determine scoring bin for entropy mesh
1248
    int mesh_bin = get_bin(site.r);
464,067,378✔
1249

464,067,378✔
1250
    // if outside mesh, skip particle
1251
    if (mesh_bin < 0) {
260,622,556✔
1252
      outside_ = true;
1253
      continue;
1254
    }
1255

1256
    // Add to appropriate bin
1257
    cnt(mesh_bin) += site.wgt;
1258
  }
1259

260,622,556✔
1260
  // Create copy of count data. Since ownership will be acquired by xtensor,
260,622,556✔
1261
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
260,622,556✔
1262
  // warnings.
125,621,110✔
1263
  int total = cnt.size();
1264
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
1265

125,621,110✔
1266
#ifdef OPENMC_MPI
125,621,110✔
1267
  // collect values from all processors
62,765,328✔
1268
  MPI_Reduce(
125,621,110✔
1269
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
61,732,872✔
1270

125,621,110✔
1271
  // Check if there were sites outside the mesh for any processor
125,621,110✔
1272
  if (outside) {
321,631,132✔
1273
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1274
  }
1275
#else
1276
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1277
  if (outside)
1278
    *outside = outside_;
1279
#endif
260,622,556✔
1280

260,622,556✔
1281
  // Adapt reduced values in array back into an xarray
1282
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1283
  xt::xarray<double> counts = arr;
1284

1285
  return counts;
1286
}
2,014✔
1287

1288
double RegularMesh::volume(const MeshIndex& ijk) const
1289
{
2,014✔
1290
  return element_volume_;
×
1291
}
1292

1293
//==============================================================================
2,014✔
1294
// RectilinearMesh implementation
2,014✔
1295
//==============================================================================
2,014✔
1296

×
1297
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1298
{
2,014✔
1299
  n_dimension_ = 3;
1300

1301
  grid_[0] = get_node_array<double>(node, "x_grid");
2,014✔
UNCOV
1302
  grid_[1] = get_node_array<double>(node, "y_grid");
×
1303
  grid_[2] = get_node_array<double>(node, "z_grid");
1304

1305
  if (int err = set_grid()) {
1306
    fatal_error(openmc_err_msg);
1307
  }
2,014✔
1308
}
1309

2,014✔
1310
const std::string RectilinearMesh::mesh_type = "rectilinear";
UNCOV
1311

×
1312
std::string RectilinearMesh::get_mesh_type() const
1313
{
1314
  return mesh_type;
1315
}
2,014✔
UNCOV
1316

×
1317
double RectilinearMesh::positive_grid_boundary(
1318
  const MeshIndex& ijk, int i) const
1319
{
1320
  return grid_[i][ijk[i]];
2,014✔
1321
}
1322

51✔
UNCOV
1323
double RectilinearMesh::negative_grid_boundary(
×
1324
  const MeshIndex& ijk, int i) const
1325
{
1326
  return grid_[i][ijk[i] - 1];
51✔
1327
}
1328

1329
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
51✔
1330
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
51✔
1331
  double l) const
×
1332
{
1333
  MeshDistance d;
1334
  d.next_index = ijk[i];
1335
  if (std::abs(u[i]) < FP_PRECISION)
1336
    return d;
51✔
UNCOV
1337

×
1338
  d.max_surface = (u[i] > 0);
1339
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1340
    d.next_index++;
1341
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
51✔
1342
  } else if (!d.max_surface && (ijk[i] > 0)) {
1343
    d.next_index--;
1,963✔
1344
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,963✔
1345
  }
1346
  return d;
1347
}
1,963✔
1348

1,963✔
1349
int RectilinearMesh::set_grid()
×
1350
{
1351
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1352
    static_cast<int>(grid_[1].size()) - 1,
1353
    static_cast<int>(grid_[2].size()) - 1};
1354

1,963✔
UNCOV
1355
  for (const auto& g : grid_) {
×
1356
    if (g.size() < 2) {
1357
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1358
                 "must each have at least 2 points");
1359
      return OPENMC_E_INVALID_ARGUMENT;
1360
    }
1,963✔
1361
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
UNCOV
1362
        g.end()) {
×
1363
      set_errmsg("Values in for x-, y-, and z- grids for "
1364
                 "rectilinear meshes must be sorted and unique.");
1365
      return OPENMC_E_INVALID_ARGUMENT;
1366
    }
2,014✔
1367
  }
1368

2,014✔
1369
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
7,789✔
1370
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
5,775✔
1371

1372
  return 0;
2,014✔
1373
}
1374

2,147,483,647✔
1375
int RectilinearMesh::get_index_in_direction(double r, int i) const
1376
{
2,147,483,647✔
1377
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1378
}
1379

1380
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1381
  Position plot_ll, Position plot_ur) const
4,072✔
1382
{
1383
  // Figure out which axes lie in the plane of the plot.
4,072✔
1384
  array<int, 2> axes {-1, -1};
1385
  if (plot_ur.z == plot_ll.z) {
1386
    axes = {0, 1};
963,729,135✔
1387
  } else if (plot_ur.y == plot_ll.y) {
1388
    axes = {0, 2};
963,729,135✔
1389
  } else if (plot_ur.x == plot_ll.x) {
1390
    axes = {1, 2};
1391
  } else {
895,175,791✔
1392
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1393
  }
895,175,791✔
1394

1395
  // Get the coordinates of the mesh lines along both of the axes.
1396
  array<vector<double>, 2> axis_lines;
1,875,856,848✔
1397
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1398
    int axis = axes[i_ax];
1399
    vector<double>& lines {axis_lines[i_ax]};
1400

1,875,856,848✔
1401
    for (auto coord : grid_[axis]) {
1,875,856,848✔
1402
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,875,856,848✔
1403
        lines.push_back(coord);
1,314,072✔
1404
    }
1405
  }
1,874,542,776✔
1406

1,874,542,776✔
1407
  return {axis_lines[0], axis_lines[1]};
959,102,019✔
1408
}
959,102,019✔
1409

915,440,757✔
1410
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
890,548,675✔
1411
{
890,548,675✔
1412
  write_dataset(mesh_group, "x_grid", grid_[0]);
1413
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,874,542,776✔
1414
  write_dataset(mesh_group, "z_grid", grid_[2]);
1415
}
1416

24✔
1417
double RectilinearMesh::volume(const MeshIndex& ijk) const
1418
{
1419
  double vol {1.0};
1420

24✔
1421
  for (int i = 0; i < n_dimension_; i++) {
24✔
1422
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
24✔
1423
  }
24✔
1424
  return vol;
24✔
1425
}
×
1426

×
1427
//==============================================================================
×
UNCOV
1428
// CylindricalMesh implementation
×
1429
//==============================================================================
×
UNCOV
1430

×
UNCOV
1431
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
×
UNCOV
1432
  : PeriodicStructuredMesh {node}
×
UNCOV
1433
{
×
1434
  n_dimension_ = 3;
UNCOV
1435
  grid_[0] = get_node_array<double>(node, "r_grid");
×
1436
  grid_[1] = get_node_array<double>(node, "phi_grid");
1437
  grid_[2] = get_node_array<double>(node, "z_grid");
1438
  origin_ = get_node_position(node, "origin");
1439

24✔
1440
  if (int err = set_grid()) {
72✔
1441
    fatal_error(openmc_err_msg);
48✔
1442
  }
48✔
UNCOV
1443
}
×
1444

48✔
1445
const std::string CylindricalMesh::mesh_type = "cylindrical";
1446

48✔
1447
std::string CylindricalMesh::get_mesh_type() const
312✔
1448
{
264✔
1449
  return mesh_type;
264✔
1450
}
264✔
1451

1452
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
1453
  Position r, bool& in_mesh) const
1454
{
48✔
1455
  local_coords(r);
24✔
1456

1457
  Position mapped_r;
2,282✔
1458
  mapped_r[0] = std::hypot(r.x, r.y);
1459
  mapped_r[2] = r[2];
2,282✔
1460

2,282✔
1461
  if (mapped_r[0] < FP_PRECISION) {
2,282✔
1462
    mapped_r[1] = 0.0;
2,282✔
1463
  } else {
2,282✔
1464
    mapped_r[1] = std::atan2(r.y, r.x);
1465
    if (mapped_r[1] < 0)
12,533✔
1466
      mapped_r[1] += 2 * M_PI;
1467
  }
1468

1469
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,533✔
1470

12,533✔
1471
  idx[1] = sanitize_phi(idx[1]);
1472

1473
  return idx;
12,533✔
1474
}
12,533✔
1475

1476
Position CylindricalMesh::sample_element(
12,298,603✔
1477
  const MeshIndex& ijk, uint64_t* seed) const
12,286,070✔
1478
{
1479
  double r_min = this->r(ijk[0] - 1);
1480
  double r_max = this->r(ijk[0]);
12,286,070✔
1481

1482
  double phi_min = this->phi(ijk[1] - 1);
1483
  double phi_max = this->phi(ijk[1]);
12,286,070✔
UNCOV
1484

×
UNCOV
1485
  double z_min = this->z(ijk[2] - 1);
×
1486
  double z_max = this->z(ijk[2]);
1487

1488
  double r_min_sq = r_min * r_min;
1489
  double r_max_sq = r_max * r_max;
12,286,070✔
1490
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1491
  double phi = uniform_distribution(phi_min, phi_max, seed);
1492
  double z = uniform_distribution(z_min, z_max, seed);
1493

1494
  double x = r * std::cos(phi);
1495
  double y = r * std::sin(phi);
12,533✔
1496

12,533✔
1497
  return origin_ + Position(x, y, z);
1498
}
1499

1500
double CylindricalMesh::find_r_crossing(
7,605✔
1501
  const Position& r, const Direction& u, double l, int shell) const
7,605✔
1502
{
1503

1504
  if ((shell < 0) || (shell > shape_[0]))
7,605✔
1505
    return INFTY;
7,605✔
1506

1507
  // solve r.x^2 + r.y^2 == r0^2
1508
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
4,928✔
1509
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,928✔
1510

4,928✔
1511
  const double r0 = grid_[0][shell];
1512
  if (r0 == 0.0)
1513
    return INFTY;
1514

12,533✔
1515
  const double denominator = u.x * u.x + u.y * u.y;
12,533✔
1516

1517
  // Direction of flight is in z-direction. Will never intersect r.
25,066✔
1518
  if (std::abs(denominator) < FP_PRECISION)
12,533✔
1519
    return INFTY;
1520

984,600✔
1521
  // inverse of dominator to help the compiler to speed things up
1522
  const double inv_denominator = 1.0 / denominator;
984,600✔
1523

1524
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
1525
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1526
  double D = p * p - c * inv_denominator;
1527

1528
  if (D < 0.0)
1529
    return INFTY;
111✔
1530

1531
  D = std::sqrt(D);
111✔
1532

1533
  // the solution -p - D is always smaller as -p + D : Check this one first
111✔
1534
  if (std::abs(c) <= RADIAL_MESH_TOL)
111✔
1535
    return INFTY;
111✔
1536

1537
  if (-p - D > l)
111✔
UNCOV
1538
    return -p - D;
×
1539
  if (-p + D > l)
1540
    return -p + D;
111✔
1541

1542
  return INFTY;
1543
}
1544

332✔
1545
double CylindricalMesh::find_phi_crossing(
1546
  const Position& r, const Direction& u, double l, int shell) const
332✔
1547
{
1548
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1549
  if (full_phi_ && (shape_[1] == 1))
58,011,680✔
1550
    return INFTY;
1551

1552
  shell = sanitize_phi(shell);
58,011,680✔
1553

1554
  const double p0 = grid_[1][shell];
1555

57,162,310✔
1556
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1557
  // => x(s) * cos(p0) = y(s) * sin(p0)
1558
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
57,162,310✔
1559
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1560

1561
  const double c0 = std::cos(p0);
116,831,280✔
1562
  const double s0 = std::sin(p0);
1563

1564
  const double denominator = (u.x * s0 - u.y * c0);
1565

116,831,280✔
1566
  // Check if direction of flight is not parallel to phi surface
116,831,280✔
1567
  if (std::abs(denominator) > FP_PRECISION) {
116,831,280✔
1568
    const double s = -(r.x * s0 - r.y * c0) / denominator;
623,808✔
1569
    // Check if solution is in positive direction of flight and crosses the
1570
    // correct phi surface (not -phi)
116,207,472✔
1571
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
116,207,472✔
1572
      return s;
58,011,680✔
1573
  }
58,011,680✔
1574

58,195,792✔
1575
  return INFTY;
57,162,310✔
1576
}
57,162,310✔
1577

1578
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
116,207,472✔
1579
  const Position& r, const Direction& u, double l, int shell) const
1580
{
1581
  MeshDistance d;
187✔
1582
  d.next_index = shell;
1583

187✔
1584
  // Direction of flight is within xy-plane. Will never intersect z.
187✔
1585
  if (std::abs(u.z) < FP_PRECISION)
187✔
1586
    return d;
1587

748✔
1588
  d.max_surface = (u.z > 0.0);
561✔
1589
  if (d.max_surface && (shell <= shape_[2])) {
×
1590
    d.next_index += 1;
1591
    d.distance = (grid_[2][shell] - r.z) / u.z;
×
1592
  } else if (!d.max_surface && (shell > 0)) {
1593
    d.next_index -= 1;
561✔
1594
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
1,122✔
UNCOV
1595
  }
×
1596
  return d;
UNCOV
1597
}
×
1598

1599
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1600
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1601
  double l) const
187✔
1602
{
187✔
1603
  Position r = r0 - origin_;
1604

187✔
1605
  if (i == 0) {
1606

1607
    return std::min(
167,532,384✔
1608
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1609
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
167,532,384✔
1610

1611
  } else if (i == 1) {
1612

12✔
1613
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1614
                      find_phi_crossing(r, u, l, ijk[i])),
1615
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1616
        find_phi_crossing(r, u, l, ijk[i] - 1)));
12✔
1617

12✔
1618
  } else {
×
1619
    return find_z_crossing(r, u, l, ijk[i]);
12✔
1620
  }
12✔
UNCOV
1621
}
×
UNCOV
1622

×
1623
int CylindricalMesh::set_grid()
UNCOV
1624
{
×
1625
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1626
    static_cast<int>(grid_[1].size()) - 1,
1627
    static_cast<int>(grid_[2].size()) - 1};
1628

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

1656
    return OPENMC_E_INVALID_ARGUMENT;
144✔
1657
  }
1658

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

1661
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
1662
    origin_[2] + grid_[2].front()};
1663
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
413✔
1664
    origin_[2] + grid_[2].back()};
413✔
1665

1666
  return 0;
413✔
1667
}
413✔
1668

413✔
1669
int CylindricalMesh::get_index_in_direction(double r, int i) const
413✔
1670
{
413✔
1671
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1672
}
413✔
UNCOV
1673

×
1674
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1675
  Position plot_ll, Position plot_ur) const
413✔
1676
{
1677
  fatal_error("Plot of cylindrical Mesh not implemented");
1678

1679
  // Figure out which axes lie in the plane of the plot.
516✔
1680
  array<vector<double>, 2> axis_lines;
1681
  return {axis_lines[0], axis_lines[1]};
516✔
1682
}
1683

1684
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
50,568,432✔
1685
{
1686
  write_dataset(mesh_group, "r_grid", grid_[0]);
1687
  write_dataset(mesh_group, "phi_grid", grid_[1]);
50,568,432✔
1688
  write_dataset(mesh_group, "z_grid", grid_[2]);
1689
  write_dataset(mesh_group, "origin", origin_);
50,568,432✔
1690
}
50,568,432✔
1691

50,568,432✔
1692
double CylindricalMesh::volume(const MeshIndex& ijk) const
1693
{
50,568,432✔
UNCOV
1694
  double r_i = grid_[0][ijk[0] - 1];
×
1695
  double r_o = grid_[0][ijk[0]];
1696

50,568,432✔
1697
  double phi_i = grid_[1][ijk[1] - 1];
50,568,432✔
1698
  double phi_o = grid_[1][ijk[1]];
25,300,356✔
1699

1700
  double z_i = grid_[2][ijk[2] - 1];
1701
  double z_o = grid_[2][ijk[2]];
50,568,432✔
1702

1703
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
50,568,432✔
1704
}
1705

50,568,432✔
1706
//==============================================================================
1707
// SphericalMesh implementation
1708
//==============================================================================
96,000✔
1709

1710
SphericalMesh::SphericalMesh(pugi::xml_node node)
1711
  : PeriodicStructuredMesh {node}
96,000✔
1712
{
96,000✔
1713
  n_dimension_ = 3;
1714

96,000✔
1715
  grid_[0] = get_node_array<double>(node, "r_grid");
96,000✔
1716
  grid_[1] = get_node_array<double>(node, "theta_grid");
1717
  grid_[2] = get_node_array<double>(node, "phi_grid");
96,000✔
1718
  origin_ = get_node_position(node, "origin");
96,000✔
1719

1720
  if (int err = set_grid()) {
96,000✔
1721
    fatal_error(openmc_err_msg);
96,000✔
1722
  }
96,000✔
1723
}
96,000✔
1724

96,000✔
1725
const std::string SphericalMesh::mesh_type = "spherical";
1726

96,000✔
1727
std::string SphericalMesh::get_mesh_type() const
96,000✔
1728
{
1729
  return mesh_type;
96,000✔
1730
}
1731

1732
StructuredMesh::MeshIndex SphericalMesh::get_indices(
148,757,592✔
1733
  Position r, bool& in_mesh) const
1734
{
1735
  local_coords(r);
1736

148,757,592✔
1737
  Position mapped_r;
18,260,184✔
1738
  mapped_r[0] = r.norm();
1739

1740
  if (mapped_r[0] < FP_PRECISION) {
1741
    mapped_r[1] = 0.0;
1742
    mapped_r[2] = 0.0;
1743
  } else {
130,497,408✔
1744
    mapped_r[1] = std::acos(r.z / mapped_r.x);
130,497,408✔
1745
    mapped_r[2] = std::atan2(r.y, r.x);
7,273,620✔
1746
    if (mapped_r[2] < 0)
1747
      mapped_r[2] += 2 * M_PI;
123,223,788✔
1748
  }
1749

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

64,320✔
1752
  idx[1] = sanitize_theta(idx[1]);
1753
  idx[2] = sanitize_phi(idx[2]);
1754

123,159,468✔
1755
  return idx;
1756
}
123,159,468✔
1757

123,159,468✔
1758
Position SphericalMesh::sample_element(
123,159,468✔
1759
  const MeshIndex& ijk, uint64_t* seed) const
1760
{
123,159,468✔
1761
  double r_min = this->r(ijk[0] - 1);
17,009,892✔
1762
  double r_max = this->r(ijk[0]);
1763

106,149,576✔
1764
  double theta_min = this->theta(ijk[1] - 1);
1765
  double theta_max = this->theta(ijk[1]);
1766

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

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

74,283,288✔
1778
  double x = r * std::cos(phi) * sin_theta;
1779
  double y = r * std::sin(phi) * sin_theta;
1780
  double z = r * cos_theta;
1781

74,283,288✔
1782
  return origin_ + Position(x, y, z);
32,466,432✔
1783
}
1784

41,816,856✔
1785
double SphericalMesh::find_r_crossing(
1786
  const Position& r, const Direction& u, double l, int shell) const
41,816,856✔
1787
{
1788
  if ((shell < 0) || (shell > shape_[0]))
1789
    return INFTY;
1790

1791
  // solve |r+s*u| = r0
1792
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
1793
  const double r0 = grid_[0][shell];
41,816,856✔
1794
  if (r0 == 0.0)
41,816,856✔
1795
    return INFTY;
1796
  const double p = r.dot(u);
41,816,856✔
1797
  double c = r.dot(r) - r0 * r0;
1798
  double D = p * p - c;
1799

41,816,856✔
1800
  if (std::abs(c) <= RADIAL_MESH_TOL)
41,532,408✔
1801
    return INFTY;
1802

1803
  if (D >= 0.0) {
41,532,408✔
1804
    D = std::sqrt(D);
19,437,552✔
1805
    // the solution -p - D is always smaller as -p + D : Check this one first
1806
    if (-p - D > l)
1807
      return -p - D;
22,379,304✔
1808
    if (-p + D > l)
1809
      return -p + D;
1810
  }
39,992,448✔
1811

1812
  return INFTY;
1813
}
39,992,448✔
1814

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

38,772,576✔
1822
  shell = sanitize_theta(shell);
17,769,456✔
1823

17,769,456✔
1824
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
21,003,120✔
1825
  // yields
18,037,548✔
1826
  // a*s^2 + 2*b*s + c == 0 with
18,037,548✔
1827
  // a = cos(theta)^2 - u.z * u.z
1828
  // b = r*u * cos(theta)^2 - u.z * r.z
38,772,576✔
1829
  // c = r*r * cos(theta)^2 - r.z^2
1830

1831
  const double cos_t = std::cos(grid_[1][shell]);
151,512,888✔
1832
  const bool sgn = std::signbit(cos_t);
1833
  const double cos_t_2 = cos_t * cos_t;
1834

1835
  const double a = cos_t_2 - u.z * u.z;
151,512,888✔
1836
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1837
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
151,512,888✔
1838

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

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

1853
    // no crossing is possible
1854
    return INFTY;
1855
  }
437✔
1856

1857
  const double p = b / a;
437✔
1858
  double D = p * p - c / a;
437✔
1859

437✔
1860
  if (D < 0.0)
1861
    return INFTY;
1,748✔
1862

1,311✔
UNCOV
1863
  D = std::sqrt(D);
×
1864

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

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

×
1876
  return INFTY;
UNCOV
1877
}
×
1878

1879
double SphericalMesh::find_phi_crossing(
437✔
UNCOV
1880
  const Position& r, const Direction& u, double l, int shell) const
×
1881
{
UNCOV
1882
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
×
1883
  if (full_phi_ && (shape_[2] == 1))
1884
    return INFTY;
437✔
UNCOV
1885

×
1886
  shell = sanitize_phi(shell);
1887

UNCOV
1888
  const double p0 = grid_[2][shell];
×
1889

1890
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1891
  // => x(s) * cos(p0) = y(s) * sin(p0)
437✔
1892
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1893
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
874✔
1894

874✔
1895
  const double c0 = std::cos(p0);
874✔
1896
  const double s0 = std::sin(p0);
874✔
1897

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

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

UNCOV
1909
  return INFTY;
×
1910
}
1911

1912
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1913
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1914
  double l) const
1915
{
1916

396✔
1917
  if (i == 0) {
1918
    return std::min(
396✔
1919
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
396✔
1920
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
396✔
1921

396✔
1922
  } else if (i == 1) {
396✔
1923
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
1924
                      find_theta_crossing(r0, u, l, ijk[i])),
384✔
1925
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
1926
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
384✔
1927

384✔
1928
  } else {
1929
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
384✔
1930
                      find_phi_crossing(r0, u, l, ijk[i])),
384✔
1931
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1932
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
384✔
1933
  }
384✔
1934
}
1935

384✔
1936
int SphericalMesh::set_grid()
1937
{
1938
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1939
    static_cast<int>(grid_[1].size()) - 1,
1940
    static_cast<int>(grid_[2].size()) - 1};
1941

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

1964
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1965
  }
1966
  if (grid_[2].back() > 2 * PI) {
1967
    set_errmsg("phi-grids for "
73,752,732✔
1968
               "spherical meshes must end with phi <= 2*pi.");
1969
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1970
  }
73,752,732✔
1971

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

×
1975
  double r = grid_[0].back();
1976
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
73,752,732✔
1977
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
73,752,732✔
1978

73,752,732✔
1979
  return 0;
36,882,336✔
1980
}
1981

1982
int SphericalMesh::get_index_in_direction(double r, int i) const
73,752,732✔
1983
{
1984
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
73,752,732✔
1985
}
73,752,732✔
1986

1987
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
73,752,732✔
1988
  Position plot_ll, Position plot_ur) const
1989
{
UNCOV
1990
  fatal_error("Plot of spherical Mesh not implemented");
×
1991

1992
  // Figure out which axes lie in the plane of the plot.
UNCOV
1993
  array<vector<double>, 2> axis_lines;
×
UNCOV
1994
  return {axis_lines[0], axis_lines[1]};
×
1995
}
UNCOV
1996

×
UNCOV
1997
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
×
1998
{
UNCOV
1999
  write_dataset(mesh_group, "r_grid", grid_[0]);
×
UNCOV
2000
  write_dataset(mesh_group, "theta_grid", grid_[1]);
×
2001
  write_dataset(mesh_group, "phi_grid", grid_[2]);
UNCOV
2002
  write_dataset(mesh_group, "origin", origin_);
×
UNCOV
2003
}
×
UNCOV
2004

×
UNCOV
2005
double SphericalMesh::volume(const MeshIndex& ijk) const
×
UNCOV
2006
{
×
2007
  double r_i = grid_[0][ijk[0] - 1];
UNCOV
2008
  double r_o = grid_[0][ijk[0]];
×
2009

UNCOV
2010
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
2011
  double theta_o = grid_[1][ijk[1]];
×
UNCOV
2012

×
2013
  double phi_i = grid_[2][ijk[2] - 1];
UNCOV
2014
  double phi_o = grid_[2][ijk[2]];
×
2015

2016
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
2017
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
481,615,104✔
2018
}
2019

2020
//==============================================================================
481,615,104✔
2021
// Helper functions for the C API
44,030,820✔
2022
//==============================================================================
2023

2024
int check_mesh(int32_t index)
2025
{
437,584,284✔
2026
  if (index < 0 || index >= model::meshes.size()) {
437,584,284✔
2027
    set_errmsg("Index in meshes array is out of bounds.");
7,601,184✔
2028
    return OPENMC_E_OUT_OF_BOUNDS;
429,983,100✔
2029
  }
429,983,100✔
2030
  return 0;
429,983,100✔
2031
}
2032

429,983,100✔
2033
template<class T>
11,548,560✔
2034
int check_mesh_type(int32_t index)
2035
{
418,434,540✔
2036
  if (int err = check_mesh(index))
388,836,240✔
2037
    return err;
2038

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

2047
template<class T>
118,217,520✔
2048
bool is_mesh_type(int32_t index)
2049
{
2050
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2051
  return mesh;
118,217,520✔
2052
}
76,498,272✔
2053

2054
//==============================================================================
41,719,248✔
2055
// C API functions
2056
//==============================================================================
2057

2058
// Return the type of mesh as a C string
2059
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2060
{
2061
  if (int err = check_mesh(index))
2062
    return err;
2063

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

41,719,248✔
2066
  return 0;
2067
}
41,719,248✔
2068

41,719,248✔
2069
//! Extend the meshes array by n elements
41,719,248✔
2070
extern "C" int openmc_extend_meshes(
2071
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2072
{
2073
  if (index_start)
41,719,248✔
2074
    *index_start = model::meshes.size();
2075
  std::string mesh_type;
2076

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

41,192,832✔
2093
  return 0;
11,955,456✔
2094
}
2095

29,237,376✔
2096
//! Adds a new unstructured mesh to OpenMC
2097
extern "C" int openmc_add_unstructured_mesh(
2098
  const char filename[], const char library[], int* id)
29,237,376✔
2099
{
2100
  std::string lib_name(library);
29,237,376✔
2101
  std::string mesh_file(filename);
5,599,656✔
2102
  bool valid_lib = false;
2103

23,637,720✔
2104
#ifdef DAGMC
2105
  if (lib_name == MOABMesh::mesh_lib_type) {
23,637,720✔
2106
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
11,064,504✔
2107
    valid_lib = true;
2108
  }
12,573,216✔
2109
#endif
2110

2111
#ifdef LIBMESH
119,966,232✔
2112
  if (lib_name == LibMesh::mesh_lib_type) {
2113
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2114
    valid_lib = true;
2115
  }
119,966,232✔
2116
#endif
76,498,272✔
2117

2118
  if (!valid_lib) {
43,467,960✔
2119
    set_errmsg(fmt::format("Mesh library {} is not supported "
2120
                           "by this build of OpenMC",
43,467,960✔
2121
      lib_name));
2122
    return OPENMC_E_INVALID_ARGUMENT;
2123
  }
2124

2125
  // auto-assign new ID
2126
  model::meshes.back()->set_id(-1);
2127
  *id = model::meshes.back()->id_;
43,467,960✔
2128

43,467,960✔
2129
  return 0;
2130
}
43,467,960✔
2131

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

2144
//! Return the ID of a mesh
359,899,428✔
2145
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2146
{
2147
  if (int err = check_mesh(index))
2148
    return err;
2149
  *id = model::meshes[index]->id_;
359,899,428✔
2150
  return 0;
240,807,552✔
2151
}
240,807,552✔
2152

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

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

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

×
2183
//! Get the bounding box of a mesh
UNCOV
2184
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
×
2185
{
2186
  if (int err = check_mesh(index))
1,023✔
2187
    return err;
×
2188

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

2191
  // set lower left corner values
2192
  ll[0] = bbox.xmin;
341✔
2193
  ll[1] = bbox.ymin;
×
2194
  ll[2] = bbox.zmin;
2195

UNCOV
2196
  // set upper right corner values
×
2197
  ur[0] = bbox.xmax;
2198
  ur[1] = bbox.ymax;
341✔
UNCOV
2199
  ur[2] = bbox.zmax;
×
2200
  return 0;
UNCOV
2201
}
×
2202

2203
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
2204
  int nz, int table_size, int32_t* materials, double* volumes)
341✔
2205
{
341✔
2206
  if (int err = check_mesh(index))
2207
    return err;
341✔
2208

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

2221
  return 0;
UNCOV
2222
}
×
2223

2224
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
2225
  Position width, int basis, int* pixels, int32_t* data)
2226
{
2227
  if (int err = check_mesh(index))
2228
    return err;
2229
  const auto& mesh = model::meshes[index].get();
312✔
2230

2231
  int pixel_width = pixels[0];
312✔
2232
  int pixel_height = pixels[1];
312✔
2233

312✔
2234
  // get pixel size
312✔
2235
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
312✔
2236
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
2237

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

×
UNCOV
2260
  // set initial position
×
2261
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2262
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
9,632✔
2263

2264
#pragma omp parallel
2265
  {
2266
    Position r = xyz;
1,802✔
2267

2268
#pragma omp for
1,802✔
UNCOV
2269
    for (int y = 0; y < pixel_height; y++) {
×
2270
      r[out_i] = xyz[out_i] - out_pixel * y;
2271
      for (int x = 0; x < pixel_width; x++) {
1,802✔
2272
        r[in_i] = xyz[in_i] + in_pixel * x;
1,802✔
UNCOV
2273
        data[pixel_width * y + x] = mesh->get_bin(r);
×
UNCOV
2274
      }
×
2275
    }
2276
  }
1,802✔
2277

2278
  return 0;
156✔
2279
}
2280

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

156✔
UNCOV
2293
//! Set the dimension of a regular mesh
×
2294
extern "C" int openmc_regular_mesh_set_dimension(
2295
  int32_t index, int n, const int* dims)
156✔
2296
{
156✔
UNCOV
2297
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2298
    return err;
×
2299
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2300

156✔
2301
  // Copy dimension
2302
  mesh->n_dimension_ = n;
260✔
2303
  std::copy(dims, dims + n, mesh->shape_.begin());
2304
  return 0;
260✔
UNCOV
2305
}
×
2306

2307
//! Get the regular mesh parameters
260✔
2308
extern "C" int openmc_regular_mesh_get_params(
260✔
UNCOV
2309
  int32_t index, double** ll, double** ur, double** width, int* n)
×
UNCOV
2310
{
×
2311
  if (int err = check_mesh_type<RegularMesh>(index))
2312
    return err;
260✔
2313
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2314

1,230✔
2315
  if (m->lower_left_.dimension() == 0) {
2316
    set_errmsg("Mesh parameters have not been set.");
1,230✔
UNCOV
2317
    return OPENMC_E_ALLOCATE;
×
2318
  }
2319

1,230✔
2320
  *ll = m->lower_left_.data();
1,230✔
UNCOV
2321
  *ur = m->upper_right_.data();
×
UNCOV
2322
  *width = m->width_.data();
×
2323
  *n = m->n_dimension_;
2324
  return 0;
1,230✔
2325
}
2326

2327
//! Set the regular mesh parameters
2328
extern "C" int openmc_regular_mesh_set_params(
2329
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2330
{
2331
  if (int err = check_mesh_type<RegularMesh>(index))
2332
    return err;
2333
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2334

2335
  if (m->n_dimension_ == -1) {
2336
    set_errmsg("Need to set mesh dimension before setting parameters.");
2337
    return OPENMC_E_UNASSIGNED;
2338
  }
2339

2,178✔
2340
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2341
  if (ll && ur) {
2,178✔
UNCOV
2342
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
×
2343
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2344
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
2,178✔
2345
  } else if (ll && width) {
2346
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
2,178✔
2347
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2348
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2349
  } else if (ur && width) {
2350
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
486✔
2351
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2352
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
2353
  } else {
486✔
2354
    set_errmsg("At least two parameters must be specified.");
486✔
2355
    return OPENMC_E_INVALID_ARGUMENT;
486✔
2356
  }
2357

972✔
2358
  // Set material volumes
486✔
2359

362✔
2360
  // TODO: incorporate this into method in RegularMesh that can be called from
124✔
2361
  // here and from constructor
76✔
2362
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
48✔
2363
  m->element_volume_ = 1.0;
24✔
2364
  for (int i = 0; i < m->n_dimension_; i++) {
24✔
2365
    m->element_volume_ *= m->width_[i];
24✔
2366
  }
UNCOV
2367

×
2368
  return 0;
2369
}
2370

486✔
UNCOV
2371
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
×
2372
template<class C>
2373
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
486✔
2374
  const int nx, const double* grid_y, const int ny, const double* grid_z,
486✔
2375
  const int nz)
2376
{
2377
  if (int err = check_mesh_type<C>(index))
×
2378
    return err;
2379

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

×
UNCOV
2382
  m->n_dimension_ = 3;
×
2383

2384
  m->grid_[0].reserve(nx);
2385
  m->grid_[1].reserve(ny);
2386
  m->grid_[2].reserve(nz);
2387

2388
  for (int i = 0; i < nx; i++) {
2389
    m->grid_[0].push_back(grid_x[i]);
2390
  }
2391
  for (int i = 0; i < ny; i++) {
2392
    m->grid_[1].push_back(grid_y[i]);
2393
  }
2394
  for (int i = 0; i < nz; i++) {
2395
    m->grid_[2].push_back(grid_z[i]);
2396
  }
2397

UNCOV
2398
  int err = m->set_grid();
×
2399
  return err;
×
2400
}
2401

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

2411
  if (m->lower_left_.dimension() == 0) {
2412
    set_errmsg("Mesh parameters have not been set.");
2413
    return OPENMC_E_ALLOCATE;
678✔
2414
  }
2415

678✔
2416
  *grid_x = m->grid_[0].data();
678✔
UNCOV
2417
  *nx = m->grid_[0].size();
×
UNCOV
2418
  *grid_y = m->grid_[1].data();
×
2419
  *ny = m->grid_[1].size();
2420
  *grid_z = m->grid_[2].data();
678✔
2421
  *nz = m->grid_[2].size();
678✔
2422

2423
  return 0;
2424
}
2425

4,470✔
2426
//! Get the rectilinear mesh grid
2427
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
4,470✔
UNCOV
2428
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2429
{
4,470✔
2430
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
4,470✔
2431
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2432
}
2433

2434
//! Set the rectilienar mesh parameters
486✔
2435
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
2436
  const double* grid_x, const int nx, const double* grid_y, const int ny,
486✔
UNCOV
2437
  const double* grid_z, const int nz)
×
2438
{
486✔
2439
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
486✔
2440
    index, grid_x, nx, grid_y, ny, grid_z, nz);
486✔
2441
}
2442

2443
//! Get the cylindrical mesh grid
2444
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
252✔
2445
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2446
{
252✔
UNCOV
2447
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
×
2448
    index, grid_x, nx, grid_y, ny, grid_z, nz);
252✔
2449
}
252✔
2450

2451
//! Set the cylindrical mesh parameters
2452
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
2453
  const double* grid_x, const int nx, const double* grid_y, const int ny,
96✔
2454
  const double* grid_z, const int nz)
2455
{
96✔
UNCOV
2456
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
×
2457
    index, grid_x, nx, grid_y, ny, grid_z, nz);
1,056✔
2458
}
960✔
2459

2460
//! Get the spherical mesh grid
96✔
2461
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
2462
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2463
{
2464

144✔
2465
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2466
    index, grid_x, nx, grid_y, ny, grid_z, nz);
144✔
UNCOV
2467
  ;
×
2468
}
2469

144✔
2470
//! Set the spherical mesh parameters
2471
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
2472
  const double* grid_x, const int nx, const double* grid_y, const int ny,
144✔
2473
  const double* grid_z, const int nz)
144✔
2474
{
144✔
2475
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
2476
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2477
}
144✔
2478

144✔
2479
#ifdef DAGMC
144✔
2480

144✔
2481
const std::string MOABMesh::mesh_lib_type = "moab";
2482

2483
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
156✔
2484
{
2485
  initialize();
2486
}
156✔
UNCOV
2487

×
2488
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2489
{
2490
  filename_ = filename;
156✔
2491
  set_length_multiplier(length_multiplier);
2492
  initialize();
12✔
2493
}
12✔
2494

12✔
2495
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
12✔
2496
{
UNCOV
2497
  mbi_ = external_mbi;
×
2498
  filename_ = "unknown (external file)";
2499
  this->initialize();
12✔
2500
}
2501

144✔
2502
void MOABMesh::initialize()
2503
{
2504

48✔
2505
  // Create the MOAB interface and load data from file
2506
  this->create_interface();
2507

48✔
UNCOV
2508
  // Initialise MOAB error code
×
2509
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2510

2511
  // Set the dimension
48✔
2512
  n_dimension_ = 3;
48✔
2513

2514
  // set member range of tetrahedral entities
2515
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
48✔
2516
  if (rval != moab::MB_SUCCESS) {
48✔
2517
    fatal_error("Failed to get all tetrahedral elements");
2518
  }
2519

2520
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2521
    warning("Non-tetrahedral elements found in unstructured "
2522
            "mesh file: " +
48✔
2523
            filename_);
48✔
2524
  }
48✔
2525

48✔
2526
  // set member range of vertices
48✔
2527
  int vertex_dim = 0;
48✔
2528
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
2529
  if (rval != moab::MB_SUCCESS) {
×
2530
    fatal_error("Failed to get all vertex handles");
×
2531
  }
×
UNCOV
2532

×
UNCOV
2533
  // make an entity set for all tetrahedra
×
UNCOV
2534
  // this is used for convenience later in output
×
UNCOV
2535
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
×
UNCOV
2536
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2537
    fatal_error("Failed to create an entity set for the tetrahedral elements");
×
2538
  }
2539

2540
  rval = mbi_->add_entities(tetset_, ehs_);
2541
  if (rval != moab::MB_SUCCESS) {
48✔
2542
    fatal_error("Failed to add tetrahedra to an entity set.");
48✔
2543
  }
2544

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

2573
  // Determine bounds of mesh
2574
  this->determine_bounds();
386✔
2575
}
2576

2577
void MOABMesh::prepare_for_point_location()
386✔
UNCOV
2578
{
×
2579
  // if the KDTree has already been constructed, do nothing
386✔
2580
  if (kdtree_)
2581
    return;
2582

386✔
2583
  // build acceleration data structures
386✔
2584
  compute_barycentric_data(ehs_);
386✔
2585
  build_kdtree(ehs_);
2586
}
2587

2588
void MOABMesh::create_interface()
410✔
2589
{
2590
  // Do not create a MOAB instance if one is already in memory
2591
  if (mbi_)
410✔
UNCOV
2592
    return;
×
2593

410✔
2594
  // create MOAB instance
2595
  mbi_ = std::make_shared<moab::Core>();
410✔
UNCOV
2596

×
UNCOV
2597
  // load unstructured mesh file
×
2598
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2599
  if (rval != moab::MB_SUCCESS) {
2600
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
410✔
2601
  }
410✔
2602
}
410✔
2603

410✔
2604
void MOABMesh::build_kdtree(const moab::Range& all_tets)
410✔
2605
{
2606
  moab::Range all_tris;
2607
  int adj_dim = 2;
2608
  write_message("Getting tet adjacencies...", 7);
422✔
2609
  moab::ErrorCode rval = mbi_->get_adjacencies(
2610
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2611
  if (rval != moab::MB_SUCCESS) {
422✔
UNCOV
2612
    fatal_error("Failed to get adjacent triangles for tets");
×
2613
  }
422✔
2614

2615
  if (!all_tris.all_of_type(moab::MBTRI)) {
422✔
UNCOV
2616
    warning("Non-triangle elements found in tet adjacencies in "
×
UNCOV
2617
            "unstructured mesh file: " +
×
2618
            filename_);
2619
  }
2620

422✔
2621
  // combine into one range
422✔
2622
  moab::Range all_tets_and_tris;
398✔
2623
  all_tets_and_tris.merge(all_tets);
398✔
2624
  all_tets_and_tris.merge(all_tris);
398✔
2625

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

12✔
2631
  // Determine what options to use
12✔
2632
  std::ostringstream options_stream;
12✔
2633
  if (options_.empty()) {
UNCOV
2634
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
×
UNCOV
2635
  } else {
×
2636
    options_stream << options_;
2637
  }
2638
  moab::FileOptions file_opts(options_stream.str().c_str());
2639

2640
  // Build the k-d tree
2641
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2642
  if (rval != moab::MB_SUCCESS) {
422✔
2643
    fatal_error("Failed to construct KDTree for the "
422✔
2644
                "unstructured mesh file: " +
1,688✔
2645
                filename_);
1,266✔
2646
  }
2647
}
2648

422✔
2649
void MOABMesh::intersect_track(const moab::CartVect& start,
422✔
2650
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2651
{
2652
  hits.clear();
2653

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

124✔
2666
  // remove duplicate intersection distances
124✔
2667
  std::unique(hits.begin(), hits.end());
2668

1,016✔
2669
  // sorts by first component of std::pair by default
892✔
2670
  std::sort(hits.begin(), hits.end());
2671
}
456✔
2672

332✔
2673
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2674
  vector<int>& bins, vector<double>& lengths) const
432✔
2675
{
308✔
2676
  moab::CartVect start(r0.x, r0.y, r0.z);
2677
  moab::CartVect end(r1.x, r1.y, r1.z);
2678
  moab::CartVect dir(u.x, u.y, u.z);
124✔
2679
  dir.normalize();
124✔
2680

2681
  double track_len = (end - start).length();
24✔
2682
  if (track_len == 0.0)
2683
    return;
2684

2685
  start -= TINY_BIT * dir;
24✔
UNCOV
2686
  end += TINY_BIT * dir;
×
2687

2688
  vector<double> hits;
24✔
2689
  intersect_track(start, dir, track_len, hits);
2690

24✔
2691
  bins.clear();
2692
  lengths.clear();
24✔
2693

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

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

2720
    // determine the start point for this segment
24✔
2721
    current = r0 + u * hit;
24✔
2722

24✔
2723
    if (bin == -1) {
2724
      continue;
96✔
2725
    }
72✔
2726

2727
    bins.push_back(bin);
108✔
2728
    lengths.push_back(segment_length / track_len);
84✔
2729
  }
2730

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

2746
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
76✔
2747
{
2748
  moab::CartVect pos(r.x, r.y, r.z);
76✔
2749
  // find the leaf of the kd-tree for this position
76✔
2750
  moab::AdaptiveKDTreeIter kdtree_iter;
76✔
2751
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
2752
  if (rval != moab::MB_SUCCESS) {
824✔
2753
    return 0;
748✔
2754
  }
2755

252✔
2756
  // retrieve the tet elements of this leaf
176✔
2757
  moab::EntityHandle leaf = kdtree_iter.handle();
2758
  moab::Range tets;
240✔
2759
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
164✔
2760
  if (rval != moab::MB_SUCCESS) {
2761
    warning("MOAB error finding tets.");
2762
  }
76✔
2763

76✔
2764
  // loop over the tets in this leaf, returning the containing tet if found
2765
  for (const auto& tet : tets) {
2766
    if (point_in_tet(pos, tet)) {
2767
      return tet;
2768
    }
448✔
2769
  }
2770

2771
  // if no tet is found, return an invalid handle
448✔
2772
  return 0;
×
2773
}
448✔
2774

2775
double MOABMesh::volume(int bin) const
448✔
UNCOV
2776
{
×
UNCOV
2777
  return tet_volume(get_ent_handle_from_bin(bin));
×
2778
}
2779

2780
std::string MOABMesh::library() const
448✔
2781
{
448✔
2782
  return mesh_lib_type;
448✔
2783
}
448✔
2784

448✔
2785
// Sample position within a tet for MOAB type tets
448✔
2786
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2787
{
448✔
2788

2789
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2790

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

132✔
2806
  std::array<Position, 4> tet_verts;
132✔
2807
  for (int i = 0; i < 4; i++) {
2808
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
132✔
2809
  }
2810
  // Samples position within tet using Barycentric stuff
132✔
2811
  return this->sample_tet(tet_verts, seed);
2812
}
2813

132✔
2814
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2815
{
132✔
2816
  vector<moab::EntityHandle> conn;
2817
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
132✔
UNCOV
2818
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2819
    fatal_error("Failed to get tet connectivity");
×
2820
  }
2821

2822
  moab::CartVect p[4];
132✔
2823
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
132✔
2824
  if (rval != moab::MB_SUCCESS) {
132✔
2825
    fatal_error("Failed to get tet coords");
132✔
2826
  }
132✔
2827

132✔
2828
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2829
}
132✔
2830

2831
int MOABMesh::get_bin(Position r) const
184✔
2832
{
2833
  moab::EntityHandle tet = get_tet(r);
2834
  if (tet == 0) {
184✔
2835
    return -1;
×
2836
  } else {
184✔
2837
    return get_bin_from_ent_handle(tet);
2838
  }
184✔
UNCOV
2839
}
×
UNCOV
2840

×
2841
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2842
{
2843
  moab::ErrorCode rval;
184✔
2844

184✔
2845
  baryc_data_.clear();
184✔
2846
  baryc_data_.resize(tets.size());
184✔
2847

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

2857
    moab::CartVect p[4];
184✔
2858
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
184✔
2859
    if (rval != moab::MB_SUCCESS) {
2860
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2861
    }
2862

76✔
2863
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2864

2865
    // invert now to avoid this cost later
2866
    a = a.transpose().inverse();
76✔
2867
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
76✔
2868
  }
2869
}
2870

2871
bool MOABMesh::point_in_tet(
132✔
2872
  const moab::CartVect& r, moab::EntityHandle tet) const
2873
{
2874

132✔
2875
  moab::ErrorCode rval;
132✔
2876

2877
  // get tet vertices
2878
  vector<moab::EntityHandle> verts;
2879
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2880
  if (rval != moab::MB_SUCCESS) {
2881
    warning("Failed to get vertices of tet in umesh: " + filename_);
2882
    return false;
2883
  }
24✔
2884

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

2896
  // look up barycentric data
2897
  int idx = get_bin_from_ent_handle(tet);
2898
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2899

2900
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2901

2902
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
24✔
2903
          bary_coords[2] >= 0.0 &&
24✔
2904
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2905
}
2906

2907
int MOABMesh::get_bin_from_index(int idx) const
2908
{
2909
  if (idx >= n_bins()) {
2910
    fatal_error(fmt::format("Invalid bin index: {}", idx));
22✔
2911
  }
2912
  return ehs_[idx] - ehs_[0];
22✔
2913
}
22✔
2914

2915
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2916
{
2917
  int bin = get_bin(r);
2918
  *in_mesh = bin != -1;
2919
  return bin;
2920
}
2921

2922
int MOABMesh::get_index_from_bin(int bin) const
1✔
2923
{
2924
  return bin;
1✔
2925
}
1✔
2926

1✔
2927
std::pair<vector<double>, vector<double>> MOABMesh::plot(
1✔
2928
  Position plot_ll, Position plot_ur) const
2929
{
23✔
2930
  // TODO: Implement mesh lines
2931
  return {};
2932
}
2933

23✔
2934
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2935
{
2936
  int idx = vert - verts_[0];
23✔
2937
  if (idx >= n_vertices()) {
2938
    fatal_error(
2939
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
23✔
2940
  }
2941
  return idx;
2942
}
23✔
2943

23✔
2944
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2945
{
2946
  int bin = eh - ehs_[0];
2947
  if (bin >= n_bins()) {
23✔
2948
    fatal_error(fmt::format("Invalid bin: {}", bin));
2949
  }
2950
  return bin;
2951
}
2952

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

2961
int MOABMesh::n_bins() const
2962
{
23✔
2963
  return ehs_.size();
23✔
2964
}
2965

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

2979
Position MOABMesh::centroid(int bin) const
2980
{
2981
  moab::ErrorCode rval;
2982

2983
  auto tet = this->get_ent_handle_from_bin(bin);
2984

2985
  // look up the tet connectivity
2986
  vector<moab::EntityHandle> conn;
2987
  rval = mbi_->get_connectivity(&tet, 1, conn);
2988
  if (rval != moab::MB_SUCCESS) {
2989
    warning("Failed to get connectivity of a mesh element.");
2990
    return {};
2991
  }
2992

2993
  // get the coordinates
2994
  vector<moab::CartVect> coords(conn.size());
2995
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
2996
  if (rval != moab::MB_SUCCESS) {
2997
    warning("Failed to get the coordinates of a mesh element.");
2998
    return {};
2999
  }
3000

3001
  // compute the centroid of the element vertices
23✔
3002
  moab::CartVect centroid(0.0, 0.0, 0.0);
23✔
3003
  for (const auto& coord : coords) {
3004
    centroid += coord;
19✔
3005
  }
3006
  centroid /= double(coords.size());
3007

19✔
3008
  return {centroid[0], centroid[1], centroid[2]};
3009
}
3010

3011
int MOABMesh::n_vertices() const
19✔
3012
{
19✔
3013
  return verts_.size();
3014
}
3015

23✔
3016
Position MOABMesh::vertex(int id) const
3017
{
3018

23✔
3019
  moab::ErrorCode rval;
1✔
3020

3021
  moab::EntityHandle vert = verts_[id];
3022

22✔
3023
  moab::CartVect coords;
3024
  rval = mbi_->get_coords(&vert, 1, coords.array());
3025
  if (rval != moab::MB_SUCCESS) {
22✔
3026
    fatal_error("Failed to get the coordinates of a vertex.");
22✔
3027
  }
3028

3029
  return {coords[0], coords[1], coords[2]};
3030
}
3031

19✔
3032
std::vector<int> MOABMesh::connectivity(int bin) const
3033
{
19✔
3034
  moab::ErrorCode rval;
19✔
3035

19✔
3036
  auto tet = get_ent_handle_from_bin(bin);
19✔
3037

3038
  // look up the tet connectivity
19✔
3039
  vector<moab::EntityHandle> conn;
3040
  rval = mbi_->get_connectivity(&tet, 1, conn);
3041
  if (rval != moab::MB_SUCCESS) {
3042
    fatal_error("Failed to get connectivity of a mesh element.");
19✔
3043
    return {};
3044
  }
3045

3046
  std::vector<int> verts(4);
3047
  for (int i = 0; i < verts.size(); i++) {
3048
    verts[i] = get_vert_idx_from_handle(conn[i]);
3049
  }
19✔
3050

19✔
3051
  return verts;
19✔
3052
}
3053

3054
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
19✔
3055
  std::string score) const
19✔
3056
{
19✔
3057
  moab::ErrorCode rval;
3058
  // add a tag to the mesh
3059
  // all scores are treated as a single value
19✔
3060
  // with an uncertainty
19✔
3061
  moab::Tag value_tag;
3✔
3062

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

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

1,519,602✔
3089
  // return the populated tag handles
3090
  return {value_tag, error_tag};
3091
}
3092

3093
void MOABMesh::add_score(const std::string& score)
3094
{
1,519,602✔
3095
  auto score_tags = get_score_tags(score);
3096
  tag_names_.push_back(score);
3097
}
1,519,602✔
3098

1,519,602✔
3099
void MOABMesh::remove_scores()
3100
{
1,519,602✔
3101
  for (const auto& name : tag_names_) {
3102
    auto value_name = name + "_mean";
3103
    moab::Tag tag;
1,519,602✔
3104
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
1,519,602✔
3105
    if (rval != moab::MB_SUCCESS)
1,519,602✔
3106
      return;
1,519,602✔
3107

3108
    rval = mbi_->tag_delete(tag);
1,519,602✔
3109
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
3110
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3111
                             " on unstructured mesh {}",
3112
        name, id_);
1,519,602✔
3113
      fatal_error(msg);
1,519,602✔
3114
    }
3115

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

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

3136
void MOABMesh::set_score_data(const std::string& score,
818,119✔
3137
  const vector<double>& values, const vector<double>& std_dev)
818,119✔
3138
{
5,506,248✔
3139
  auto score_tags = this->get_score_tags(score);
3140

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

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

3161
void MOABMesh::write(const std::string& base_filename) const
818,119✔
3162
{
818,119✔
3163
  // add extension to the base name
818,119✔
3164
  auto filename = base_filename + ".vtk";
818,119✔
3165
  write_message(5, "Writing unstructured mesh {}...", filename);
818,119✔
3166
  filename = settings::path_output + filename;
818,119✔
3167

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

7,279,728✔
3179
#endif
7,279,728✔
3180

1,010,077✔
3181
#ifdef LIBMESH
3182

3183
const std::string LibMesh::mesh_lib_type = "libmesh";
3184

6,269,651✔
3185
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,269,651✔
3186
{
6,269,651✔
3187
  // filename_ and length_multiplier_ will already be set by the
6,269,651✔
3188
  // UnstructuredMesh constructor
3189
  set_mesh_pointer_from_filename(filename_);
3190
  set_length_multiplier(length_multiplier_);
3191
  initialize();
3192
}
259,367,091✔
3193

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

155,856✔
3203
  m_ = &input_mesh;
3204
  set_length_multiplier(length_multiplier);
155,856✔
3205
  initialize();
3206
}
3207

30✔
3208
// create the mesh from an input file
3209
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
30✔
3210
  : adaptive_(false)
3211
{
3212
  set_mesh_pointer_from_filename(filename);
3213
  set_length_multiplier(length_multiplier);
200,410✔
3214
  initialize();
3215
}
3216

200,410✔
3217
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
3218
{
3219
  filename_ = filename;
3220
  unique_m_ =
3221
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
200,410✔
3222
  m_ = unique_m_.get();
200,410✔
3223
  m_->read(filename_);
3224
}
3225

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

1,002,050✔
3235
// intialize from mesh file
801,640✔
3236
void LibMesh::initialize()
3237
{
3238
  if (!settings::libmesh_comm) {
400,820✔
3239
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3240
                "communicator.");
3241
  }
155,856✔
3242

3243
  // assuming that unstructured meshes used in OpenMC are 3D
155,856✔
3244
  n_dimension_ = 3;
155,856✔
3245

155,856✔
3246
  if (length_multiplier_ > 0.0) {
3247
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
3248
  }
3249
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
779,280✔
3250
  // Otherwise assume that it is prepared by its owning application
155,856✔
3251
  if (unique_m_) {
155,856✔
3252
    m_->prepare_for_use();
3253
  }
3254

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

7,279,728✔
3262
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3263
    pl_.emplace_back(m_->sub_point_locator());
3264
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,266,805✔
3265
    pl_.back()->enable_out_of_mesh_mode();
3266
  }
3267

3268
  // store first element in the mesh to use as an offset for bin indices
19✔
3269
  auto first_elem = *m_->elements_begin();
3270
  first_element_id_ = first_elem->id();
3271

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

3282
      bin_to_elem_map_.push_back(elem->id());
3283
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3284
    }
1,138,560✔
3285
  }
227,712✔
3286

227,712✔
3287
  // bounding box for the mesh for quick rejection checks
3288
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3289
  libMesh::Point ll = bbox_.min();
3290
  libMesh::Point ur = bbox_.max();
227,712✔
3291
  lower_left_ = {ll(0), ll(1), ll(2)};
3292
  upper_right_ = {ur(0), ur(1), ur(2)};
3293
}
227,712✔
3294

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

3309
Position LibMesh::centroid(int bin) const
3310
{
3311
  const auto& elem = this->get_element_from_bin(bin);
3312
  auto centroid = elem.vertex_average();
3313
  return {centroid(0), centroid(1), centroid(2)};
3314
}
259,364,245✔
3315

259,364,245✔
3316
int LibMesh::n_vertices() const
259,364,245✔
3317
{
3318
  return m_->n_nodes();
3319
}
3320

3321
Position LibMesh::vertex(int vertex_id) const
3322
{
3323
  const auto node_ref = m_->node_ref(vertex_id);
3324
  return {node_ref(0), node_ref(1), node_ref(2)};
259,364,245✔
3325
}
259,364,245✔
3326

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

3337
std::string LibMesh::library() const
3338
{
3339
  return mesh_lib_type;
3340
}
3341

3342
int LibMesh::n_bins() const
3343
{
3344
  return m_->n_active_elem();
3345
}
3346

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

767,424✔
3365
void LibMesh::add_score(const std::string& var_name)
3366
{
3367
  if (adaptive_) {
3368
    warning(fmt::format(
767,424✔
3369
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3370
      this->id_));
3371

265,858,762✔
3372
    return;
3373
  }
265,858,762✔
3374

265,858,762✔
3375
  if (!equation_systems_) {
3376
    build_eqn_sys();
3377
  }
265,858,762✔
3378

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

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

3398
void LibMesh::remove_scores()
3399
{
3400
  if (equation_systems_) {
3401
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3402
    eqn_sys.clear();
3403
    variable_map_.clear();
3404
  }
3405
}
3406

3407
void LibMesh::set_score_data(const std::string& var_name,
3408
  const vector<double>& values, const vector<double>& std_dev)
3409
{
3410
  if (adaptive_) {
3411
    warning(fmt::format(
3412
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3413
      this->id_));
3414

3415
    return;
3416
  }
3417

3418
  if (!equation_systems_) {
3419
    build_eqn_sys();
3420
  }
3421

3422
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3423

3424
  if (!eqn_sys.is_initialized()) {
3425
    equation_systems_->init();
3426
  }
3427

3428
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3429

3430
  // look up the value variable
3431
  std::string value_name = var_name + "_mean";
3432
  unsigned int value_num = variable_map_.at(value_name);
3433
  // look up the std dev variable
3434
  std::string std_dev_name = var_name + "_std_dev";
3435
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3436

3437
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3438
       it++) {
795,427✔
3439
    if (!(*it)->active()) {
3440
      continue;
795,427✔
3441
    }
3442

3443
    auto bin = get_bin_from_element(*it);
81,537✔
3444

3445
    // set value
3446
    vector<libMesh::dof_id_type> value_dof_indices;
3447
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3448
    Ensures(value_dof_indices.size() == 1);
81,537✔
3449
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3450

81,537✔
3451
    // set std dev
81,537✔
3452
    vector<libMesh::dof_id_type> std_dev_dof_indices;
81,537✔
3453
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3454
    Ensures(std_dev_dof_indices.size() == 1);
3455
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3456
  }
163,074✔
3457
}
3458

3459
void LibMesh::write(const std::string& filename) const
191,856✔
3460
{
3461
  if (adaptive_) {
3462
    warning(fmt::format(
3463
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
191,856✔
3464
      this->id_));
3465

3466
    return;
191,856✔
3467
  }
191,856✔
3468

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

3477
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3478
  vector<int>& bins, vector<double>& lengths) const
191,856✔
3479
{
191,856✔
3480
  // TODO: Implement triangle crossings here
3481
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3482
}
3483

3484
int LibMesh::get_bin(Position r) const
3485
{
3486
  // look-up a tet using the point locator
3487
  libMesh::Point p(r.x, r.y, r.z);
3488

3489
  // quick rejection check
3490
  if (!bbox_.contains_point(p)) {
3491
    return -1;
3492
  }
3493

3494
  const auto& point_locator = pl_.at(thread_num());
3495

3496
  const auto elem_ptr = (*point_locator)(p);
3497
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3498
}
3499

3500
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3501
{
3502
  int bin =
3503
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3504
  if (bin >= n_bins() || bin < 0) {
3505
    fatal_error(fmt::format("Invalid bin: {}", bin));
3506
  }
3507
  return bin;
3508
}
3509

3510
std::pair<vector<double>, vector<double>> LibMesh::plot(
3511
  Position plot_ll, Position plot_ur) const
3512
{
3513
  return {};
3514
}
3515

3516
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3517
{
3518
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3519
}
3520

3521
double LibMesh::volume(int bin) const
3522
{
3523
  return this->get_element_from_bin(bin).volume();
3524
}
3525

3526
#endif // LIBMESH
3527

3528
//==============================================================================
3529
// Non-member functions
3530
//==============================================================================
3531

3532
void read_meshes(pugi::xml_node root)
3533
{
3534
  std::unordered_set<int> mesh_ids;
3535

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

3546
    // If we've already read a mesh with the same ID in a *different* file,
3547
    // assume it is the same here
3548
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3549
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3550
      continue;
3551
    }
3552

3553
    std::string mesh_type;
3554
    if (check_for_node(node, "type")) {
3555
      mesh_type = get_node_value(node, "type", true, true);
3556
    } else {
3557
      mesh_type = "regular";
3558
    }
3559

3560
    // determine the mesh library to use
3561
    std::string mesh_lib;
3562
    if (check_for_node(node, "library")) {
3563
      mesh_lib = get_node_value(node, "library", true, true);
3564
    }
3565

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

3592
    // Map ID to position in vector
3593
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3594
  }
3595
}
3596

3597
void meshes_to_hdf5(hid_t group)
3598
{
3599
  // Write number of meshes
3600
  hid_t meshes_group = create_group(group, "meshes");
3601
  int32_t n_meshes = model::meshes.size();
3602
  write_attribute(meshes_group, "n_meshes", n_meshes);
3603

3604
  if (n_meshes > 0) {
3605
    // Write IDs of meshes
3606
    vector<int> ids;
3607
    for (const auto& m : model::meshes) {
3608
      m->to_hdf5(meshes_group);
3609
      ids.push_back(m->id_);
3610
    }
3611
    write_attribute(meshes_group, "ids", ids);
3612
  }
23✔
3613

3614
  close_group(meshes_group);
3615
}
3616

23✔
3617
void free_memory_mesh()
23✔
3618
{
23✔
3619
  model::meshes.clear();
23✔
3620
  model::mesh_map.clear();
3621
}
3622

3623
extern "C" int n_meshes()
3624
{
3625
  return model::meshes.size();
3626
}
3627

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