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

openmc-dev / openmc / 12890256258

21 Jan 2025 03:29PM UTC coverage: 84.935% (+0.07%) from 84.865%
12890256258

Pull #3129

github

web-flow
Merge 244c7d46c into 9170bf34a
Pull Request #3129: Compute material volumes in mesh elements based on raytracing

202 of 228 new or added lines in 4 files covered. (88.6%)

136 existing lines in 1 file now uncovered.

50294 of 59215 relevant lines covered (84.93%)

34822958.36 hits per line

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

87.24
/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 OPENMC_MPI
10
#include "mpi.h"
11
#endif
12

13
#include "xtensor/xadapt.hpp"
14
#include "xtensor/xbuilder.hpp"
15
#include "xtensor/xeval.hpp"
16
#include "xtensor/xmath.hpp"
17
#include "xtensor/xsort.hpp"
18
#include "xtensor/xtensor.hpp"
19
#include "xtensor/xview.hpp"
20
#include <fmt/core.h> // for fmt
21

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

46
#ifdef LIBMESH
47
#include "libmesh/mesh_modification.h"
48
#include "libmesh/mesh_tools.h"
49
#include "libmesh/numeric_vector.h"
50
#endif
51

52
#ifdef DAGMC
53
#include "moab/FileOptions.hpp"
54
#endif
55

56
namespace openmc {
57

58
//==============================================================================
59
// Global variables
60
//==============================================================================
61

62
#ifdef LIBMESH
63
const bool LIBMESH_ENABLED = true;
64
#else
65
const bool LIBMESH_ENABLED = false;
66
#endif
67

68
namespace model {
69

70
std::unordered_map<int32_t, int32_t> mesh_map;
71
vector<unique_ptr<Mesh>> meshes;
72

73
} // namespace model
74

75
#ifdef LIBMESH
76
namespace settings {
77
unique_ptr<libMesh::LibMeshInit> libmesh_init;
78
const libMesh::Parallel::Communicator* libmesh_comm {nullptr};
79
} // namespace settings
80
#endif
81

82
//==============================================================================
83
// Helper functions
84
//==============================================================================
85

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

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

109
//==============================================================================
110
// MaterialVolumes implementation
111
//==============================================================================
112

113
namespace detail {
114

115
void MaterialVolumes::add_volume(
2,475,156✔
116
  int index_elem, int index_material, double volume)
117
{
118
  int i;
119
#pragma omp critical(MeshMatVol)
2,475,156✔
120
  for (i = 0; i < n_mats_; ++i) {
4,142,814✔
121
    // Check whether material already is present
122
    if (this->materials(index_elem, i) == index_material)
4,142,814✔
123
      break;
2,473,788✔
124

125
    // If not already present, check for unused position (-2)
126
    if (this->materials(index_elem, i) == -2) {
1,669,026✔
127
      this->materials(index_elem, i) = index_material;
1,368✔
128
      break;
1,368✔
129
    }
130
  }
131

132
  // If maximum number of materials exceeded, set a flag that can be checked
133
  // later
134
  if (i >= n_mats_) {
2,475,156✔
NEW
135
    too_many_mats_ = true;
×
NEW
136
    return;
×
137
  }
138

139
  // Accumulate volume
140
#pragma omp atomic
1,237,643✔
141
  this->volumes(index_elem, i) += volume;
2,475,156✔
142
}
143

144
// Same as add_volume above, but without the OpenMP critical/atomic section
NEW
145
void MaterialVolumes::add_volume_unsafe(
×
146
  int index_elem, int index_material, double volume)
147
{
148
  int i;
NEW
149
  for (i = 0; i < n_mats_; ++i) {
×
150
    // Check whether material already is present
NEW
151
    if (this->materials(index_elem, i) == index_material)
×
NEW
152
      break;
×
153

154
    // If not already present, check for unused position (-2)
NEW
155
    if (this->materials(index_elem, i) == -2) {
×
NEW
156
      this->materials(index_elem, i) = index_material;
×
NEW
157
      break;
×
158
    }
159
  }
160

161
  // If maximum number of materials exceeded, set a flag that can be checked
162
  // later
NEW
163
  if (i >= n_mats_) {
×
NEW
164
    too_many_mats_ = true;
×
NEW
165
    return;
×
166
  }
167

168
  // Accumulate volume
NEW
169
  this->volumes(index_elem, i) += volume;
×
170
}
171

172
} // namespace detail
173

174
//==============================================================================
175
// Mesh implementation
176
//==============================================================================
177

178
Mesh::Mesh(pugi::xml_node node)
2,882✔
179
{
180
  // Read mesh id
181
  id_ = std::stoi(get_node_value(node, "id"));
2,882✔
182
  if (check_for_node(node, "name"))
2,882✔
183
    name_ = get_node_value(node, "name");
17✔
184
}
2,882✔
185

186
void Mesh::set_id(int32_t id)
1✔
187
{
188
  Expects(id >= 0 || id == C_NONE);
1✔
189

190
  // Clear entry in mesh map in case one was already assigned
191
  if (id_ != C_NONE) {
1✔
192
    model::mesh_map.erase(id_);
×
193
    id_ = C_NONE;
×
194
  }
195

196
  // Ensure no other mesh has the same ID
197
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
1✔
198
    throw std::runtime_error {
×
199
      fmt::format("Two meshes have the same ID: {}", id)};
×
200
  }
201

202
  // If no ID is specified, auto-assign the next ID in the sequence
203
  if (id == C_NONE) {
1✔
204
    id = 0;
1✔
205
    for (const auto& m : model::meshes) {
3✔
206
      id = std::max(id, m->id_);
2✔
207
    }
208
    ++id;
1✔
209
  }
210

211
  // Update ID and entry in the mesh map
212
  id_ = id;
1✔
213
  model::mesh_map[id] = model::meshes.size() - 1;
1✔
214
}
1✔
215

216
vector<double> Mesh::volumes() const
156✔
217
{
218
  vector<double> volumes(n_bins());
156✔
219
  for (int i = 0; i < n_bins(); i++) {
982,476✔
220
    volumes[i] = this->volume(i);
982,320✔
221
  }
222
  return volumes;
156✔
223
}
×
224

225
void Mesh::material_volumes(int nx, int ny, int nz, int max_materials,
156✔
226
  int32_t* materials, double* volumes) const
227
{
228
  if (mpi::master) {
156✔
229
    header("MESH MATERIAL VOLUMES CALCULATION", 7);
156✔
230
  }
231
  write_message(7, "Number of rays (x) = {}", nx);
156✔
232
  write_message(7, "Number of rays (y) = {}", ny);
156✔
233
  write_message(7, "Number of rays (z) = {}", nz);
156✔
234
  int64_t n_total = nx * ny + ny * nz + nx * nz;
156✔
235
  write_message(7, "Total number of rays = {}", n_total);
156✔
236
  write_message(
156✔
237
    7, "Maximum number of materials per mesh element = {}", max_materials);
238

239
  Timer timer;
156✔
240
  timer.start();
156✔
241

242
  // Create object for keeping track of materials/volumes
243
  detail::MaterialVolumes result(materials, volumes, max_materials);
156✔
244

245
  // Determine bounding box
246
  auto bbox = this->bounding_box();
156✔
247

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

250
  // Determine effective width of rays
251
  Position width((bbox.xmax - bbox.xmin) / nx, (bbox.ymax - bbox.ymin) / ny,
156✔
252
    (bbox.zmax - bbox.zmin) / nz);
156✔
253

254
  // Set flag for mesh being contained within model
255
  bool out_of_model = false;
156✔
256

257
#pragma omp parallel
78✔
258
  {
259
    // Preallocate vector for mesh indices and lenght fractions and p
260
    std::vector<int> bins;
78✔
261
    std::vector<double> length_fractions;
78✔
262
    Particle p;
78✔
263

264
    SourceSite site;
78✔
265
    site.E = 1.0;
78✔
266
    site.particle = ParticleType::neutron;
78✔
267

268
    for (int axis = 0; axis < 3; ++axis) {
312✔
269
      // Set starting position and direction
270
      site.r = {0.0, 0.0, 0.0};
234✔
271
      site.r[axis] = bbox.min()[axis];
234✔
272
      site.u = {0.0, 0.0, 0.0};
234✔
273
      site.u[axis] = 1.0;
234✔
274

275
      // Determine width of rays and number of rays in other directions
276
      int ax1 = (axis + 1) % 3;
234✔
277
      int ax2 = (axis + 2) % 3;
234✔
278
      double min1 = bbox.min()[ax1];
234✔
279
      double min2 = bbox.min()[ax2];
234✔
280
      double d1 = width[ax1];
234✔
281
      double d2 = width[ax2];
234✔
282
      int n1 = n_rays[ax1];
234✔
283
      int n2 = n_rays[ax2];
234✔
284

285
      // Divide rays in first direction over MPI processes by computing starting
286
      // and ending indices
287
      int min_work = n1 / mpi::n_procs;
234✔
288
      int remainder = n1 % mpi::n_procs;
234✔
289
      int n1_local = (mpi::rank < remainder) ? min_work + 1 : min_work;
234✔
290
      int i1_start = mpi::rank * min_work + std::min(mpi::rank, remainder);
234✔
291
      int i1_end = i1_start + n1_local;
234✔
292

293
      // Loop over rays on face of bounding box
294
#pragma omp for collapse(2)
295
      for (int i1 = i1_start; i1 < i1_end; ++i1) {
9,924✔
296
        for (int i2 = 0; i2 < n2; ++i2) {
503,964✔
297
          site.r[ax1] = min1 + (i1 + 0.5) * d1;
494,274✔
298
          site.r[ax2] = min2 + (i2 + 0.5) * d2;
494,274✔
299

300
          p.from_source(&site);
494,274✔
301

302
          // Determine particle's location
303
          if (!exhaustive_find_cell(p)) {
494,274✔
304
            out_of_model = true;
47,916✔
305
            continue;
47,916✔
306
          }
307

308
          // Set birth cell attribute
309
          if (p.cell_born() == C_NONE)
446,358✔
310
            p.cell_born() = p.lowest_coord().cell;
446,358✔
311

312
          // Initialize last cells from current cell
313
          for (int j = 0; j < p.n_coord(); ++j) {
892,716✔
314
            p.cell_last(j) = p.coord(j).cell;
446,358✔
315
          }
316
          p.n_coord_last() = p.n_coord();
446,358✔
317

318
          while (true) {
319
            // Ray trace from r_start to r_end
320
            Position r0 = p.r();
973,974✔
321
            double max_distance = bbox.max()[axis] - r0[axis];
973,974✔
322

323
            // Find the distance to the nearest boundary
324
            BoundaryInfo boundary = distance_to_boundary(p);
973,974✔
325

326
            // Advance particle forward
327
            double distance = std::min(boundary.distance, max_distance);
973,974✔
328
            p.move_distance(distance);
973,974✔
329

330
            // Determine what mesh elements were crossed by particle
331
            bins.clear();
973,974✔
332
            length_fractions.clear();
973,974✔
333
            this->bins_crossed(r0, p.r(), p.u(), bins, length_fractions);
973,974✔
334

335
            // Add volumes to any mesh elements that were crossed
336
            int i_material = p.material();
973,974✔
337
            if (i_material != C_NONE) {
973,974✔
338
              i_material = model::materials[i_material]->id();
939,858✔
339
            }
340
            for (int i_bin = 0; i_bin < bins.size(); i_bin++) {
2,211,552✔
341
              int mesh_index = bins[i_bin];
1,237,578✔
342
              double length = distance * length_fractions[i_bin];
1,237,578✔
343

344
              // Add volume to result
345
              result.add_volume(mesh_index, i_material, length * d1 * d2);
1,237,578✔
346
            }
347

348
            if (distance == max_distance)
973,974✔
349
              break;
446,358✔
350

351
            for (int j = 0; j < p.n_coord(); ++j) {
1,055,232✔
352
              p.cell_last(j) = p.coord(j).cell;
527,616✔
353
            }
354
            p.n_coord_last() = p.n_coord();
527,616✔
355

356
            // Set surface that particle is on and adjust coordinate levels
357
            p.surface() = boundary.surface;
527,616✔
358
            p.n_coord() = boundary.coord_level;
527,616✔
359

360
            if (boundary.lattice_translation[0] != 0 ||
527,616✔
361
                boundary.lattice_translation[1] != 0 ||
1,055,232✔
362
                boundary.lattice_translation[2] != 0) {
527,616✔
363
              // Particle crosses lattice boundary
364

365
              cross_lattice(p, boundary);
366
            } else {
367
              // Particle crosses surface
368
              const auto& surf {model::surfaces[p.surface_index()].get()};
527,616✔
369
              p.cross_surface(*surf);
527,616✔
370
            }
371
          }
527,616✔
372
        }
373
      }
374
    }
375
  }
78✔
376

377
  // Check for errors
378
  if (out_of_model) {
156✔
379
    throw std::runtime_error("Mesh not fully contained in geometry.");
12✔
380
  } else if (result.too_many_mats()) {
144✔
NEW
381
    throw std::runtime_error("Maximum number of materials for mesh material "
×
NEW
382
                             "volume calculation insufficient.");
×
383
  }
384

385
  // Compute time for raytracing
386
  double t_raytrace = timer.elapsed();
144✔
387

388
#ifdef OPENMC_MPI
389
  // Combine results from multiple MPI processes
390
  if (mpi::n_procs > 1) {
60✔
391
    int total = this->n_bins() * max_materials;
392
    if (mpi::master) {
393
      // Allocate temporary buffer for receiving data
394
      std::vector<int32_t> mats(total);
395
      std::vector<double> vols(total);
396

397
      for (int i = 1; i < mpi::n_procs; ++i) {
398
        // Receive material indices and volumes from process i
399
        MPI_Recv(
400
          mats.data(), total, MPI_INT, i, i, mpi::intracomm, MPI_STATUS_IGNORE);
401
        MPI_Recv(vols.data(), total, MPI_DOUBLE, i, i, mpi::intracomm,
402
          MPI_STATUS_IGNORE);
403

404
        // Combine with existing results; we can call thread unsafe version of
405
        // add_volume because each thread is operating on a different element
406
#pragma omp for
407
        for (int index_elem = 0; index_elem < n_bins(); ++index_elem) {
408
          for (int k = 0; k < max_materials; ++k) {
409
            int index = index_elem * max_materials + k;
410
            result.add_volume_unsafe(index_elem, mats[index], vols[index]);
411
          }
412
        }
413
      }
414
    } else {
415
      // Send material indices and volumes to process 0
416
      MPI_Send(materials, total, MPI_INT, 0, mpi::rank, mpi::intracomm);
417
      MPI_Send(volumes, total, MPI_DOUBLE, 0, mpi::rank, mpi::intracomm);
418
    }
419
  }
420

421
  // Report time for MPI communication
422
  double t_mpi = timer.elapsed() - t_raytrace;
60✔
423
#else
424
  double t_mpi = 0.0;
84✔
425
#endif
426

427
  // Normalize based on known volumes of elements
428
  for (int i = 0; i < this->n_bins(); ++i) {
1,020✔
429
    // Estimated total volume in element i
430
    double volume = 0.0;
876✔
431
    for (int j = 0; j < max_materials; ++j) {
4,380✔
432
      volume += result.volumes(i, j);
3,504✔
433
    }
434

435
    // Renormalize volumes based on known volume of element i
436
    double norm = this->volume(i) / volume;
876✔
437
    for (int j = 0; j < max_materials; ++j) {
4,380✔
438
      result.volumes(i, j) *= norm;
3,504✔
439
    }
440
  }
441

442
  // Show elapsed time
443
  timer.stop();
144✔
444
  double t_total = timer.elapsed();
144✔
445
  double t_normalize = t_total - t_raytrace - t_mpi;
144✔
446
  if (mpi::master) {
144✔
447
    header("Timing Statistics", 7);
144✔
448
    show_time("Total time elapsed", t_total);
144✔
449
    show_time("Ray tracing", t_raytrace, 1);
144✔
450
    show_time("Ray tracing (per ray)", t_raytrace / n_total, 1);
144✔
451
    show_time("MPI communication", t_mpi, 1);
144✔
452
    show_time("Normalization", t_normalize, 1);
144✔
453
    std::fflush(stdout);
144✔
454
  }
455
}
144✔
456

457
void Mesh::to_hdf5(hid_t group) const
3,132✔
458
{
459
  // Create group for mesh
460
  std::string group_name = fmt::format("mesh {}", id_);
5,698✔
461
  hid_t mesh_group = create_group(group, group_name.c_str());
3,132✔
462

463
  // Write mesh type
464
  write_dataset(mesh_group, "type", this->get_mesh_type());
3,132✔
465

466
  // Write mesh ID
467
  write_attribute(mesh_group, "id", id_);
3,132✔
468

469
  // Write mesh name
470
  write_dataset(mesh_group, "name", name_);
3,132✔
471

472
  // Write mesh data
473
  this->to_hdf5_inner(mesh_group);
3,132✔
474

475
  // Close group
476
  close_group(mesh_group);
3,132✔
477
}
3,132✔
478

479
//==============================================================================
480
// Structured Mesh implementation
481
//==============================================================================
482

483
std::string StructuredMesh::bin_label(int bin) const
5,441,880✔
484
{
485
  MeshIndex ijk = get_indices_from_bin(bin);
5,441,880✔
486

487
  if (n_dimension_ > 2) {
5,441,880✔
488
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,851,408✔
489
  } else if (n_dimension_ > 1) {
16,176✔
490
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,752✔
491
  } else {
492
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
493
  }
494
}
495

496
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
3,138✔
497
{
498
  // because method is const, shape_ is const as well and can't be adapted
499
  auto tmp_shape = shape_;
3,138✔
500
  return xt::adapt(tmp_shape, {n_dimension_});
6,276✔
501
}
502

503
Position StructuredMesh::sample_element(
1,542,372✔
504
  const MeshIndex& ijk, uint64_t* seed) const
505
{
506
  // lookup the lower/upper bounds for the mesh element
507
  double x_min = negative_grid_boundary(ijk, 0);
1,542,372✔
508
  double x_max = positive_grid_boundary(ijk, 0);
1,542,372✔
509

510
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
511
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
1,542,372✔
512

513
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
514
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
1,542,372✔
515

516
  return {x_min + (x_max - x_min) * prn(seed),
1,542,372✔
517
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
1,542,372✔
518
}
519

520
//==============================================================================
521
// Unstructured Mesh implementation
522
//==============================================================================
523

524
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
45✔
525
{
526
  // check the mesh type
527
  if (check_for_node(node, "type")) {
45✔
528
    auto temp = get_node_value(node, "type", true, true);
45✔
529
    if (temp != mesh_type) {
45✔
530
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
531
    }
532
  }
45✔
533

534
  // check if a length unit multiplier was specified
535
  if (check_for_node(node, "length_multiplier")) {
45✔
536
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
537
  }
538

539
  // get the filename of the unstructured mesh to load
540
  if (check_for_node(node, "filename")) {
45✔
541
    filename_ = get_node_value(node, "filename");
45✔
542
    if (!file_exists(filename_)) {
45✔
543
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
544
    }
545
  } else {
546
    fatal_error(fmt::format(
×
547
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
548
  }
549

550
  if (check_for_node(node, "options")) {
45✔
551
    options_ = get_node_value(node, "options");
16✔
552
  }
553

554
  // check if mesh tally data should be written with
555
  // statepoint files
556
  if (check_for_node(node, "output")) {
45✔
557
    output_ = get_node_value_bool(node, "output");
×
558
  }
559
}
45✔
560

561
void UnstructuredMesh::determine_bounds()
23✔
562
{
563
  double xmin = INFTY;
23✔
564
  double ymin = INFTY;
23✔
565
  double zmin = INFTY;
23✔
566
  double xmax = -INFTY;
23✔
567
  double ymax = -INFTY;
23✔
568
  double zmax = -INFTY;
23✔
569
  int n = this->n_vertices();
23✔
570
  for (int i = 0; i < n; ++i) {
53,604✔
571
    auto v = this->vertex(i);
53,581✔
572
    xmin = std::min(v.x, xmin);
53,581✔
573
    ymin = std::min(v.y, ymin);
53,581✔
574
    zmin = std::min(v.z, zmin);
53,581✔
575
    xmax = std::max(v.x, xmax);
53,581✔
576
    ymax = std::max(v.y, ymax);
53,581✔
577
    zmax = std::max(v.z, zmax);
53,581✔
578
  }
579
  lower_left_ = {xmin, ymin, zmin};
23✔
580
  upper_right_ = {xmax, ymax, zmax};
23✔
581
}
23✔
582

583
Position UnstructuredMesh::sample_tet(
601,230✔
584
  std::array<Position, 4> coords, uint64_t* seed) const
585
{
586
  // Uniform distribution
587
  double s = prn(seed);
601,230✔
588
  double t = prn(seed);
601,230✔
589
  double u = prn(seed);
601,230✔
590

591
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
592
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
593
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
594
  if (s + t > 1) {
601,230✔
595
    s = 1.0 - s;
301,245✔
596
    t = 1.0 - t;
301,245✔
597
  }
598
  if (s + t + u > 1) {
601,230✔
599
    if (t + u > 1) {
400,908✔
600
      double old_t = t;
199,920✔
601
      t = 1.0 - u;
199,920✔
602
      u = 1.0 - s - old_t;
199,920✔
603
    } else if (t + u <= 1) {
200,988✔
604
      double old_s = s;
200,988✔
605
      s = 1.0 - t - u;
200,988✔
606
      u = old_s + t + u - 1;
200,988✔
607
    }
608
  }
609
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
610
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
611
}
612

613
const std::string UnstructuredMesh::mesh_type = "unstructured";
614

615
std::string UnstructuredMesh::get_mesh_type() const
30✔
616
{
617
  return mesh_type;
30✔
618
}
619

620
void UnstructuredMesh::surface_bins_crossed(
×
621
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
622
{
623
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
624
}
625

626
std::string UnstructuredMesh::bin_label(int bin) const
193,712✔
627
{
628
  return fmt::format("Mesh Index ({})", bin);
193,712✔
629
};
630

631
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
30✔
632
{
633
  write_dataset(mesh_group, "filename", filename_);
30✔
634
  write_dataset(mesh_group, "library", this->library());
30✔
635
  if (!options_.empty()) {
30✔
636
    write_attribute(mesh_group, "options", options_);
8✔
637
  }
638

639
  if (length_multiplier_ > 0.0)
30✔
640
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
641

642
  // write vertex coordinates
643
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
30✔
644
  for (int i = 0; i < this->n_vertices(); i++) {
67,928✔
645
    auto v = this->vertex(i);
67,898✔
646
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
67,898✔
647
  }
648
  write_dataset(mesh_group, "vertices", vertices);
30✔
649

650
  int num_elem_skipped = 0;
30✔
651

652
  // write element types and connectivity
653
  vector<double> volumes;
30✔
654
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
30✔
655
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
30✔
656
  for (int i = 0; i < this->n_bins(); i++) {
337,742✔
657
    auto conn = this->connectivity(i);
337,712✔
658

659
    volumes.emplace_back(this->volume(i));
337,712✔
660

661
    // write linear tet element
662
    if (conn.size() == 4) {
337,712✔
663
      xt::view(elem_types, i, xt::all()) =
671,424✔
664
        static_cast<int>(ElementType::LINEAR_TET);
671,424✔
665
      xt::view(connectivity, i, xt::all()) =
671,424✔
666
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
1,007,136✔
667
      // write linear hex element
668
    } else if (conn.size() == 8) {
2,000✔
669
      xt::view(elem_types, i, xt::all()) =
4,000✔
670
        static_cast<int>(ElementType::LINEAR_HEX);
4,000✔
671
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000✔
672
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000✔
673
    } else {
674
      num_elem_skipped++;
×
675
      xt::view(elem_types, i, xt::all()) =
×
676
        static_cast<int>(ElementType::UNSUPPORTED);
677
      xt::view(connectivity, i, xt::all()) = -1;
×
678
    }
679
  }
337,712✔
680

681
  // warn users that some elements were skipped
682
  if (num_elem_skipped > 0) {
30✔
683
    warning(fmt::format("The connectivity of {} elements "
×
684
                        "on mesh {} were not written "
685
                        "because they are not of type linear tet/hex.",
686
      num_elem_skipped, this->id_));
×
687
  }
688

689
  write_dataset(mesh_group, "volumes", volumes);
30✔
690
  write_dataset(mesh_group, "connectivity", connectivity);
30✔
691
  write_dataset(mesh_group, "element_types", elem_types);
30✔
692
}
30✔
693

694
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
695
{
696
  length_multiplier_ = length_multiplier;
23✔
697
}
23✔
698

699
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
700
{
701
  auto conn = connectivity(bin);
120,000✔
702

703
  if (conn.size() == 4)
120,000✔
704
    return ElementType::LINEAR_TET;
120,000✔
705
  else if (conn.size() == 8)
×
706
    return ElementType::LINEAR_HEX;
×
707
  else
708
    return ElementType::UNSUPPORTED;
×
709
}
120,000✔
710

711
StructuredMesh::MeshIndex StructuredMesh::get_indices(
1,013,924,485✔
712
  Position r, bool& in_mesh) const
713
{
714
  MeshIndex ijk;
715
  in_mesh = true;
1,013,924,485✔
716
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
717
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
718

719
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
720
      in_mesh = false;
91,752,208✔
721
  }
722
  return ijk;
1,013,924,485✔
723
}
724

725
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,054,620,989✔
726
{
727
  switch (n_dimension_) {
1,054,620,989✔
728
  case 1:
956,736✔
729
    return ijk[0] - 1;
956,736✔
730
  case 2:
21,602,040✔
731
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,602,040✔
732
  case 3:
1,032,062,213✔
733
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
1,032,062,213✔
734
  default:
×
735
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
736
  }
737
}
738

739
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,064,408✔
740
{
741
  MeshIndex ijk;
742
  if (n_dimension_ == 1) {
8,064,408✔
743
    ijk[0] = bin + 1;
300✔
744
  } else if (n_dimension_ == 2) {
8,064,108✔
745
    ijk[0] = bin % shape_[0] + 1;
15,876✔
746
    ijk[1] = bin / shape_[0] + 1;
15,876✔
747
  } else if (n_dimension_ == 3) {
8,048,232✔
748
    ijk[0] = bin % shape_[0] + 1;
8,048,232✔
749
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,048,232✔
750
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,048,232✔
751
  }
752
  return ijk;
8,064,408✔
753
}
754

755
int StructuredMesh::get_bin(Position r) const
336,878,024✔
756
{
757
  // Determine indices
758
  bool in_mesh;
759
  MeshIndex ijk = get_indices(r, in_mesh);
336,878,024✔
760
  if (!in_mesh)
336,878,024✔
761
    return -1;
22,164,978✔
762

763
  // Convert indices to bin
764
  return get_bin_from_indices(ijk);
314,713,046✔
765
}
766

767
int StructuredMesh::n_bins() const
1,001,936✔
768
{
769
  return std::accumulate(
1,001,936✔
770
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,003,872✔
771
}
772

773
int StructuredMesh::n_surface_bins() const
623✔
774
{
775
  return 4 * n_dimension_ * n_bins();
623✔
776
}
777

778
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
779
  const SourceSite* bank, int64_t length, bool* outside) const
780
{
781
  // Determine shape of array for counts
782
  std::size_t m = this->n_bins();
×
783
  vector<std::size_t> shape = {m};
×
784

785
  // Create array of zeros
786
  xt::xarray<double> cnt {shape, 0.0};
×
787
  bool outside_ = false;
×
788

789
  for (int64_t i = 0; i < length; i++) {
×
790
    const auto& site = bank[i];
×
791

792
    // determine scoring bin for entropy mesh
793
    int mesh_bin = get_bin(site.r);
×
794

795
    // if outside mesh, skip particle
796
    if (mesh_bin < 0) {
×
797
      outside_ = true;
×
798
      continue;
×
799
    }
800

801
    // Add to appropriate bin
802
    cnt(mesh_bin) += site.wgt;
×
803
  }
804

805
  // Create copy of count data. Since ownership will be acquired by xtensor,
806
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
807
  // warnings.
808
  int total = cnt.size();
×
809
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
810

811
#ifdef OPENMC_MPI
812
  // collect values from all processors
813
  MPI_Reduce(
814
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
815

816
  // Check if there were sites outside the mesh for any processor
817
  if (outside) {
818
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
819
  }
820
#else
821
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
822
  if (outside)
823
    *outside = outside_;
824
#endif
825

826
  // Adapt reduced values in array back into an xarray
827
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
828
  xt::xarray<double> counts = arr;
×
829

830
  return counts;
×
831
}
832

833
// raytrace through the mesh. The template class T will do the tallying.
834
// A modern optimizing compiler can recognize the noop method of T and
835
// eliminate that call entirely.
836
template<class T>
837
void StructuredMesh::raytrace_mesh(
677,126,273✔
838
  Position r0, Position r1, const Direction& u, T tally) const
839
{
840
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
841
  // enable/disable tally calls for now, T template type needs to provide both
842
  // surface and track methods, which might be empty. modern optimizing
843
  // compilers will (hopefully) eliminate the complete code (including
844
  // calculation of parameters) but for the future: be explicit
845

846
  // Compute the length of the entire track.
847
  double total_distance = (r1 - r0).norm();
677,126,273✔
848
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
677,126,273✔
849
    return;
5,711,856✔
850

851
  const int n = n_dimension_;
671,414,417✔
852

853
  // Flag if position is inside the mesh
854
  bool in_mesh;
855

856
  // Position is r = r0 + u * traveled_distance, start at r0
857
  double traveled_distance {0.0};
671,414,417✔
858

859
  // Calculate index of current cell. Offset the position a tiny bit in
860
  // direction of flight
861
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
671,414,417✔
862

863
  // if track is very short, assume that it is completely inside one cell.
864
  // Only the current cell will score and no surfaces
865
  if (total_distance < 2 * TINY_BIT) {
671,414,417✔
866
    if (in_mesh) {
12✔
867
      tally.track(ijk, 1.0);
×
868
    }
869
    return;
12✔
870
  }
871

872
  // translate start and end positions,
873
  // this needs to come after the get_indices call because it does its own
874
  // translation
875
  local_coords(r0);
671,414,405✔
876
  local_coords(r1);
671,414,405✔
877

878
  // Calculate initial distances to next surfaces in all three dimensions
879
  std::array<MeshDistance, 3> distances;
1,342,828,810✔
880
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
881
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
1,996,211,175✔
882
  }
883

884
  // Loop until r = r1 is eventually reached
885
  while (true) {
365,726,351✔
886

887
    if (in_mesh) {
1,037,140,756✔
888

889
      // find surface with minimal distance to current position
890
      const auto k = std::min_element(distances.begin(), distances.end()) -
943,569,198✔
891
                     distances.begin();
943,569,198✔
892

893
      // Tally track length delta since last step
894
      tally.track(ijk,
943,569,198✔
895
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
943,569,198✔
896
          total_distance);
897

898
      // update position and leave, if we have reached end position
899
      traveled_distance = distances[k].distance;
943,569,198✔
900
      if (traveled_distance >= total_distance)
943,569,198✔
901
        return;
583,474,891✔
902

903
      // If we have not reached r1, we have hit a surface. Tally outward
904
      // current
905
      tally.surface(ijk, k, distances[k].max_surface, false);
360,094,307✔
906

907
      // Update cell and calculate distance to next surface in k-direction.
908
      // The two other directions are still valid!
909
      ijk[k] = distances[k].next_index;
360,094,307✔
910
      distances[k] =
360,094,307✔
911
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
360,094,307✔
912

913
      // Check if we have left the interior of the mesh
914
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
360,094,307✔
915

916
      // If we are still inside the mesh, tally inward current for the next
917
      // cell
918
      if (in_mesh)
360,094,307✔
919
        tally.surface(ijk, k, !distances[k].max_surface, true);
333,958,815✔
920

921
    } else { // not inside mesh
922

923
      // For all directions outside the mesh, find the distance that we need
924
      // to travel to reach the next surface. Use the largest distance, as
925
      // only this will cross all outer surfaces.
926
      int k_max {0};
93,571,558✔
927
      for (int k = 0; k < n; ++k) {
371,944,846✔
928
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
373,287,420✔
929
            (distances[k].distance > traveled_distance)) {
94,914,132✔
930
          traveled_distance = distances[k].distance;
94,128,372✔
931
          k_max = k;
94,128,372✔
932
        }
933
      }
934

935
      // If r1 is not inside the mesh, exit here
936
      if (traveled_distance >= total_distance)
93,571,558✔
937
        return;
87,939,514✔
938

939
      // Calculate the new cell index and update all distances to next
940
      // surfaces.
941
      ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
5,632,044✔
942
      for (int k = 0; k < n; ++k) {
22,298,916✔
943
        distances[k] =
16,666,872✔
944
          distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
16,666,872✔
945
      }
946

947
      // If inside the mesh, Tally inward current
948
      if (in_mesh)
5,632,044✔
949
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
4,254,984✔
950
    }
951
  }
952
}
953

270,535,554✔
954
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
955
  vector<int>& bins, vector<double>& lengths) const
956
{
957

958
  // Helper tally class.
959
  // stores a pointer to the mesh class and references to bins and lengths
960
  // parameters. Performs the actual tally through the track method.
961
  struct TrackAggregator {
962
    TrackAggregator(
963
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
270,535,554✔
964
      : mesh(_mesh), bins(_bins), lengths(_lengths)
270,535,554✔
965
    {}
×
966
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
967
    void track(const MeshIndex& ijk, double l) const
270,535,554✔
968
    {
969
      bins.push_back(mesh->get_bin_from_indices(ijk));
970
      lengths.push_back(l);
971
    }
972

973
    const StructuredMesh* mesh;
270,535,554✔
974
    vector<int>& bins;
975
    vector<double>& lengths;
976
  };
977

270,535,554✔
978
  // Perform the mesh raytrace with the helper class.
979
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
980
}
981

270,535,554✔
982
void StructuredMesh::surface_bins_crossed(
×
983
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
984
{
985

×
986
  // Helper tally class.
987
  // stores a pointer to the mesh class and a reference to the bins parameter.
988
  // Performs the actual tally through the surface method.
989
  struct SurfaceAggregator {
990
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
991
      : mesh(_mesh), bins(_bins)
270,535,554✔
992
    {}
270,535,554✔
993
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
994
    {
995
      int i_bin =
541,071,108✔
996
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,080,306,072✔
997
      if (max)
809,770,518✔
998
        i_bin += 2;
999
      if (inward)
1000
        i_bin += 1;
1001
      bins.push_back(i_bin);
66,393,591✔
1002
    }
1003
    void track(const MeshIndex& idx, double l) const {}
336,929,145✔
1004

1005
    const StructuredMesh* mesh;
1006
    vector<int>& bins;
333,730,080✔
1007
  };
333,730,080✔
1008

1009
  // Perform the mesh raytrace with the helper class.
1010
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
333,730,080✔
1011
}
333,730,080✔
1012

1013
//==============================================================================
1014
// RegularMesh implementation
1015
//==============================================================================
333,730,080✔
1016

333,730,080✔
1017
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
267,580,077✔
1018
{
1019
  // Determine number of dimensions for mesh
1020
  if (!check_for_node(node, "dimension")) {
1021
    fatal_error("Must specify <dimension> on a regular mesh.");
66,150,003✔
1022
  }
1023

1024
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
1025
  int n = n_dimension_ = shape.size();
66,150,003✔
1026
  if (n != 1 && n != 2 && n != 3) {
66,150,003✔
1027
    fatal_error("Mesh must be one, two, or three dimensions.");
66,150,003✔
1028
  }
1029
  std::copy(shape.begin(), shape.end(), shape_.begin());
1030

66,150,003✔
1031
  // Check that dimensions are all greater than zero
1032
  if (xt::any(shape <= 0)) {
1033
    fatal_error("All entries on the <dimension> element for a tally "
1034
                "mesh must be positive.");
66,150,003✔
1035
  }
63,700,230✔
1036

1037
  // Check for lower-left coordinates
1038
  if (check_for_node(node, "lower_left")) {
1039
    // Read mesh lower-left corner location
1040
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1041
  } else {
1042
    fatal_error("Must specify <lower_left> on a mesh.");
3,199,065✔
1043
  }
12,444,960✔
1044

12,601,512✔
1045
  // Make sure lower_left and dimension match
3,355,617✔
1046
  if (shape.size() != lower_left_.size()) {
3,291,153✔
1047
    fatal_error("Number of entries on <lower_left> must be the same "
3,291,153✔
1048
                "as the number of entries on <dimension>.");
1049
  }
1050

1051
  if (check_for_node(node, "width")) {
1052
    // Make sure one of upper-right or width were specified
3,199,065✔
1053
    if (check_for_node(node, "upper_right")) {
2,955,477✔
1054
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1055
    }
1056

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

860,256✔
1059
    // Check to ensure width has same dimensions
616,668✔
1060
    auto n = width_.size();
616,668✔
1061
    if (n != lower_left_.size()) {
1062
      fatal_error("Number of entries on <width> must be the same as "
1063
                  "the number of entries on <lower_left>.");
1064
    }
243,588✔
1065

218,592✔
1066
    // Check for negative widths
1067
    if (xt::any(width_ < 0.0)) {
1068
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1069
    }
406,590,719✔
1070

1071
    // Set width and upper right coordinate
1072
    upper_right_ = xt::eval(lower_left_ + shape * width_);
1073

1074
  } else if (check_for_node(node, "upper_right")) {
1075
    upper_right_ = get_node_xarray<double>(node, "upper_right");
1076

1077
    // Check to ensure width has same dimensions
1078
    auto n = upper_right_.size();
1079
    if (n != lower_left_.size()) {
406,590,719✔
1080
      fatal_error("Number of entries on <upper_right> must be the "
406,590,719✔
1081
                  "same as the number of entries on <lower_left>.");
5,711,856✔
1082
    }
1083

400,878,863✔
1084
    // Check that upper-right is above lower-left
1085
    if (xt::any(upper_right_ < lower_left_)) {
1086
      fatal_error("The <upper_right> coordinates must be greater than "
1087
                  "the <lower_left> coordinates on a tally mesh.");
1088
    }
1089

400,878,863✔
1090
    // Set width
1091
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1092
  } else {
1093
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
400,878,863✔
1094
  }
1095

1096
  // Set material volumes
1097
  volume_frac_ = 1.0 / xt::prod(shape)();
400,878,863✔
1098

12✔
UNCOV
1099
  element_volume_ = 1.0;
×
1100
  for (int i = 0; i < n_dimension_; i++) {
1101
    element_volume_ *= width_[i];
12✔
1102
  }
1103
}
1104

1105
int RegularMesh::get_index_in_direction(double r, int i) const
1106
{
1107
  return std::ceil((r - lower_left_[i]) / width_[i]);
400,878,851✔
1108
}
400,878,851✔
1109

1110
const std::string RegularMesh::mesh_type = "regular";
1111

801,757,702✔
1112
std::string RegularMesh::get_mesh_type() const
1,587,319,508✔
1113
{
1,186,440,657✔
1114
  return mesh_type;
1115
}
1116

1117
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
299,332,760✔
1118
{
1119
  return lower_left_[i] + ijk[i] * width_[i];
700,211,611✔
1120
}
1121

1122
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
609,839,118✔
1123
{
609,839,118✔
1124
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1125
}
1126

609,839,118✔
1127
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
609,839,118✔
1128
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1129
  double l) const
1130
{
1131
  MeshDistance d;
609,839,118✔
1132
  d.next_index = ijk[i];
609,839,118✔
1133
  if (std::abs(u[i]) < FP_PRECISION)
315,894,814✔
1134
    return d;
1135

1136
  d.max_surface = (u[i] > 0);
1137
  if (d.max_surface && (ijk[i] <= shape_[i])) {
293,944,304✔
1138
    d.next_index++;
1139
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1140
  } else if (!d.max_surface && (ijk[i] >= 1)) {
1141
    d.next_index--;
293,944,304✔
1142
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
293,944,304✔
1143
  }
293,944,304✔
1144
  return d;
1145
}
1146

293,944,304✔
1147
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1148
  Position plot_ll, Position plot_ur) const
1149
{
1150
  // Figure out which axes lie in the plane of the plot.
293,944,304✔
1151
  array<int, 2> axes {-1, -1};
270,258,585✔
1152
  if (plot_ur.z == plot_ll.z) {
1153
    axes[0] = 0;
1154
    if (n_dimension_ > 1)
1155
      axes[1] = 1;
1156
  } else if (plot_ur.y == plot_ll.y) {
1157
    axes[0] = 0;
1158
    if (n_dimension_ > 2)
90,372,493✔
1159
      axes[1] = 2;
359,499,886✔
1160
  } else if (plot_ur.x == plot_ll.x) {
360,685,908✔
1161
    if (n_dimension_ > 1)
91,558,515✔
1162
      axes[0] = 1;
90,837,219✔
1163
    if (n_dimension_ > 2)
90,837,219✔
1164
      axes[1] = 2;
1165
  } else {
1166
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1167
  }
1168

90,372,493✔
1169
  // Get the coordinates of the mesh lines along both of the axes.
84,984,037✔
1170
  array<vector<double>, 2> axis_lines;
1171
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1172
    int axis = axes[i_ax];
1173
    if (axis == -1)
5,388,456✔
1174
      continue;
21,438,660✔
1175
    auto& lines {axis_lines[i_ax]};
16,050,204✔
1176

16,050,204✔
1177
    double coord = lower_left_[axis];
1178
    for (int i = 0; i < shape_[axis] + 1; ++i) {
1179
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1180
        lines.push_back(coord);
5,388,456✔
1181
      coord += width_[axis];
4,036,392✔
1182
    }
1183
  }
1184

1185
  return {axis_lines[0], axis_lines[1]};
1186
}
406,590,719✔
1187

1188
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1189
{
1190
  write_dataset(mesh_group, "dimension", get_x_shape());
1191
  write_dataset(mesh_group, "lower_left", lower_left_);
1192
  write_dataset(mesh_group, "upper_right", upper_right_);
1193
  write_dataset(mesh_group, "width", width_);
1194
}
406,590,719✔
1195

1196
xt::xtensor<double, 1> RegularMesh::count_sites(
406,590,719✔
1197
  const SourceSite* bank, int64_t length, bool* outside) const
406,590,719✔
1198
{
568,239,281✔
1199
  // Determine shape of array for counts
609,839,118✔
1200
  std::size_t m = this->n_bins();
1201
  vector<std::size_t> shape = {m};
609,839,118✔
1202

609,839,118✔
1203
  // Create array of zeros
609,839,118✔
1204
  xt::xarray<double> cnt {shape, 0.0};
1205
  bool outside_ = false;
1206

1207
  for (int64_t i = 0; i < length; i++) {
1208
    const auto& site = bank[i];
1209

1210
    // determine scoring bin for entropy mesh
1211
    int mesh_bin = get_bin(site.r);
406,590,719✔
1212

406,590,719✔
1213
    // if outside mesh, skip particle
1214
    if (mesh_bin < 0) {
270,535,554✔
1215
      outside_ = true;
1216
      continue;
1217
    }
1218

1219
    // Add to appropriate bin
1220
    cnt(mesh_bin) += site.wgt;
1221
  }
1222

270,535,554✔
1223
  // Create copy of count data. Since ownership will be acquired by xtensor,
270,535,554✔
1224
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
270,535,554✔
1225
  // warnings.
130,068,825✔
1226
  int total = cnt.size();
1227
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
1228

130,068,825✔
1229
#ifdef OPENMC_MPI
130,068,825✔
1230
  // collect values from all processors
64,988,658✔
1231
  MPI_Reduce(
130,068,825✔
1232
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
63,918,822✔
1233

130,068,825✔
1234
  // Check if there were sites outside the mesh for any processor
130,068,825✔
1235
  if (outside) {
333,730,080✔
1236
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1237
  }
1238
#else
1239
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1240
  if (outside)
1241
    *outside = outside_;
1242
#endif
270,535,554✔
1243

270,535,554✔
1244
  // Adapt reduced values in array back into an xarray
1245
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1246
  xt::xarray<double> counts = arr;
1247

1248
  return counts;
1249
}
1,996✔
1250

1251
double RegularMesh::volume(const MeshIndex& ijk) const
1252
{
1,996✔
1253
  return element_volume_;
×
1254
}
1255

1256
//==============================================================================
1,996✔
1257
// RectilinearMesh implementation
1,996✔
1258
//==============================================================================
1,996✔
1259

×
1260
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1261
{
1,996✔
1262
  n_dimension_ = 3;
1263

1264
  grid_[0] = get_node_array<double>(node, "x_grid");
1,996✔
UNCOV
1265
  grid_[1] = get_node_array<double>(node, "y_grid");
×
1266
  grid_[2] = get_node_array<double>(node, "z_grid");
1267

1268
  if (int err = set_grid()) {
1269
    fatal_error(openmc_err_msg);
1270
  }
1,996✔
1271
}
1272

1,996✔
1273
const std::string RectilinearMesh::mesh_type = "rectilinear";
UNCOV
1274

×
1275
std::string RectilinearMesh::get_mesh_type() const
1276
{
1277
  return mesh_type;
1278
}
1,996✔
UNCOV
1279

×
1280
double RectilinearMesh::positive_grid_boundary(
1281
  const MeshIndex& ijk, int i) const
1282
{
1283
  return grid_[i][ijk[i]];
1,996✔
1284
}
1285

51✔
UNCOV
1286
double RectilinearMesh::negative_grid_boundary(
×
1287
  const MeshIndex& ijk, int i) const
1288
{
1289
  return grid_[i][ijk[i] - 1];
51✔
1290
}
1291

1292
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
51✔
1293
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
51✔
1294
  double l) const
×
1295
{
1296
  MeshDistance d;
1297
  d.next_index = ijk[i];
1298
  if (std::abs(u[i]) < FP_PRECISION)
1299
    return d;
51✔
UNCOV
1300

×
1301
  d.max_surface = (u[i] > 0);
1302
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1303
    d.next_index++;
1304
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
51✔
1305
  } else if (!d.max_surface && (ijk[i] > 0)) {
1306
    d.next_index--;
1,945✔
1307
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1,945✔
1308
  }
1309
  return d;
1310
}
1,945✔
1311

1,945✔
1312
int RectilinearMesh::set_grid()
×
1313
{
1314
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1315
    static_cast<int>(grid_[1].size()) - 1,
1316
    static_cast<int>(grid_[2].size()) - 1};
1317

1,945✔
UNCOV
1318
  for (const auto& g : grid_) {
×
1319
    if (g.size() < 2) {
1320
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1321
                 "must each have at least 2 points");
1322
      return OPENMC_E_INVALID_ARGUMENT;
1323
    }
1,945✔
1324
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
UNCOV
1325
        g.end()) {
×
1326
      set_errmsg("Values in for x-, y-, and z- grids for "
1327
                 "rectilinear meshes must be sorted and unique.");
1328
      return OPENMC_E_INVALID_ARGUMENT;
1329
    }
1,996✔
1330
  }
1331

1,996✔
1332
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
7,717✔
1333
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
5,721✔
1334

1335
  return 0;
1,996✔
1336
}
1337

2,147,483,647✔
1338
int RectilinearMesh::get_index_in_direction(double r, int i) const
1339
{
2,147,483,647✔
1340
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1341
}
1342

1343
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1344
  Position plot_ll, Position plot_ur) const
4,101✔
1345
{
1346
  // Figure out which axes lie in the plane of the plot.
4,101✔
1347
  array<int, 2> axes {-1, -1};
1348
  if (plot_ur.z == plot_ll.z) {
1349
    axes = {0, 1};
862,506,704✔
1350
  } else if (plot_ur.y == plot_ll.y) {
1351
    axes = {0, 2};
862,506,704✔
1352
  } else if (plot_ur.x == plot_ll.x) {
1353
    axes = {1, 2};
1354
  } else {
861,295,453✔
1355
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1356
  }
861,295,453✔
1357

1358
  // Get the coordinates of the mesh lines along both of the axes.
1359
  array<vector<double>, 2> axis_lines;
1,740,545,834✔
1360
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1361
    int axis = axes[i_ax];
1362
    vector<double>& lines {axis_lines[i_ax]};
1363

1,740,545,834✔
1364
    for (auto coord : grid_[axis]) {
1,740,545,834✔
1365
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,740,545,834✔
1366
        lines.push_back(coord);
1,314,072✔
1367
    }
1368
  }
1,739,231,762✔
1369

1,739,231,762✔
1370
  return {axis_lines[0], axis_lines[1]};
857,879,588✔
1371
}
857,879,588✔
1372

881,352,174✔
1373
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
856,668,337✔
1374
{
856,668,337✔
1375
  write_dataset(mesh_group, "x_grid", grid_[0]);
1376
  write_dataset(mesh_group, "y_grid", grid_[1]);
1,739,231,762✔
1377
  write_dataset(mesh_group, "z_grid", grid_[2]);
1378
}
1379

24✔
1380
double RectilinearMesh::volume(const MeshIndex& ijk) const
1381
{
1382
  double vol {1.0};
1383

24✔
1384
  for (int i = 0; i < n_dimension_; i++) {
24✔
1385
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
24✔
1386
  }
24✔
1387
  return vol;
24✔
1388
}
×
1389

×
1390
//==============================================================================
×
UNCOV
1391
// CylindricalMesh implementation
×
1392
//==============================================================================
×
UNCOV
1393

×
UNCOV
1394
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
×
UNCOV
1395
  : PeriodicStructuredMesh {node}
×
UNCOV
1396
{
×
1397
  n_dimension_ = 3;
UNCOV
1398
  grid_[0] = get_node_array<double>(node, "r_grid");
×
1399
  grid_[1] = get_node_array<double>(node, "phi_grid");
1400
  grid_[2] = get_node_array<double>(node, "z_grid");
1401
  origin_ = get_node_position(node, "origin");
1402

24✔
1403
  if (int err = set_grid()) {
72✔
1404
    fatal_error(openmc_err_msg);
48✔
1405
  }
48✔
UNCOV
1406
}
×
1407

48✔
1408
const std::string CylindricalMesh::mesh_type = "cylindrical";
1409

48✔
1410
std::string CylindricalMesh::get_mesh_type() const
312✔
1411
{
264✔
1412
  return mesh_type;
264✔
1413
}
264✔
1414

1415
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
1416
  Position r, bool& in_mesh) const
1417
{
48✔
1418
  local_coords(r);
24✔
1419

1420
  Position mapped_r;
2,268✔
1421
  mapped_r[0] = std::hypot(r.x, r.y);
1422
  mapped_r[2] = r[2];
2,268✔
1423

2,268✔
1424
  if (mapped_r[0] < FP_PRECISION) {
2,268✔
1425
    mapped_r[1] = 0.0;
2,268✔
1426
  } else {
2,268✔
1427
    mapped_r[1] = std::atan2(r.y, r.x);
1428
    if (mapped_r[1] < 0)
12,818✔
1429
      mapped_r[1] += 2 * M_PI;
1430
  }
1431

1432
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,818✔
1433

12,818✔
1434
  idx[1] = sanitize_phi(idx[1]);
1435

1436
  return idx;
12,818✔
1437
}
12,818✔
1438

1439
Position CylindricalMesh::sample_element(
12,577,949✔
1440
  const MeshIndex& ijk, uint64_t* seed) const
12,565,131✔
1441
{
1442
  double r_min = this->r(ijk[0] - 1);
1443
  double r_max = this->r(ijk[0]);
12,565,131✔
1444

1445
  double phi_min = this->phi(ijk[1] - 1);
1446
  double phi_max = this->phi(ijk[1]);
12,565,131✔
UNCOV
1447

×
UNCOV
1448
  double z_min = this->z(ijk[2] - 1);
×
1449
  double z_max = this->z(ijk[2]);
1450

1451
  double r_min_sq = r_min * r_min;
1452
  double r_max_sq = r_max * r_max;
12,565,131✔
1453
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1454
  double phi = uniform_distribution(phi_min, phi_max, seed);
1455
  double z = uniform_distribution(z_min, z_max, seed);
1456

1457
  double x = r * std::cos(phi);
1458
  double y = r * std::sin(phi);
12,818✔
1459

12,818✔
1460
  return origin_ + Position(x, y, z);
1461
}
1462

1463
double CylindricalMesh::find_r_crossing(
7,890✔
1464
  const Position& r, const Direction& u, double l, int shell) const
7,890✔
1465
{
1466

1467
  if ((shell < 0) || (shell > shape_[0]))
7,890✔
1468
    return INFTY;
7,890✔
1469

1470
  // solve r.x^2 + r.y^2 == r0^2
1471
  // 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✔
1472
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,928✔
1473

4,928✔
1474
  const double r0 = grid_[0][shell];
1475
  if (r0 == 0.0)
1476
    return INFTY;
1477

12,818✔
1478
  const double denominator = u.x * u.x + u.y * u.y;
12,818✔
1479

1480
  // Direction of flight is in z-direction. Will never intersect r.
25,636✔
1481
  if (std::abs(denominator) < FP_PRECISION)
12,818✔
1482
    return INFTY;
1483

983,100✔
1484
  // inverse of dominator to help the compiler to speed things up
1485
  const double inv_denominator = 1.0 / denominator;
983,100✔
1486

1487
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
1488
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1489
  double D = p * p - c * inv_denominator;
1490

1491
  if (D < 0.0)
1492
    return INFTY;
111✔
1493

1494
  D = std::sqrt(D);
111✔
1495

1496
  // the solution -p - D is always smaller as -p + D : Check this one first
111✔
1497
  if (std::abs(c) <= RADIAL_MESH_TOL)
111✔
1498
    return INFTY;
111✔
1499

1500
  if (-p - D > l)
111✔
UNCOV
1501
    return -p - D;
×
1502
  if (-p + D > l)
1503
    return -p + D;
111✔
1504

1505
  return INFTY;
1506
}
1507

336✔
1508
double CylindricalMesh::find_phi_crossing(
1509
  const Position& r, const Direction& u, double l, int shell) const
336✔
1510
{
1511
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1512
  if (full_phi_ && (shape_[1] == 1))
60,098,226✔
1513
    return INFTY;
1514

1515
  shell = sanitize_phi(shell);
60,098,226✔
1516

1517
  const double p0 = grid_[1][shell];
1518

59,246,253✔
1519
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1520
  // => x(s) * cos(p0) = y(s) * sin(p0)
1521
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
59,246,253✔
1522
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1523

1524
  const double c0 = std::cos(p0);
121,014,204✔
1525
  const double s0 = std::sin(p0);
1526

1527
  const double denominator = (u.x * s0 - u.y * c0);
1528

121,014,204✔
1529
  // Check if direction of flight is not parallel to phi surface
121,014,204✔
1530
  if (std::abs(denominator) > FP_PRECISION) {
121,014,204✔
1531
    const double s = -(r.x * s0 - r.y * c0) / denominator;
623,808✔
1532
    // Check if solution is in positive direction of flight and crosses the
1533
    // correct phi surface (not -phi)
120,390,396✔
1534
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
120,390,396✔
1535
      return s;
60,098,226✔
1536
  }
60,098,226✔
1537

60,292,170✔
1538
  return INFTY;
59,246,253✔
1539
}
59,246,253✔
1540

1541
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
120,390,396✔
1542
  const Position& r, const Direction& u, double l, int shell) const
1543
{
1544
  MeshDistance d;
189✔
1545
  d.next_index = shell;
1546

189✔
1547
  // Direction of flight is within xy-plane. Will never intersect z.
189✔
1548
  if (std::abs(u.z) < FP_PRECISION)
189✔
1549
    return d;
1550

756✔
1551
  d.max_surface = (u.z > 0.0);
567✔
1552
  if (d.max_surface && (shell <= shape_[2])) {
×
1553
    d.next_index += 1;
1554
    d.distance = (grid_[2][shell] - r.z) / u.z;
×
1555
  } else if (!d.max_surface && (shell > 0)) {
1556
    d.next_index -= 1;
567✔
1557
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
1,134✔
UNCOV
1558
  }
×
1559
  return d;
UNCOV
1560
}
×
1561

1562
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1563
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1564
  double l) const
189✔
1565
{
189✔
1566
  Position r = r0 - origin_;
1567

189✔
1568
  if (i == 0) {
1569

1570
    return std::min(
173,741,166✔
1571
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1572
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
173,741,166✔
1573

1574
  } else if (i == 1) {
1575

12✔
1576
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1577
                      find_phi_crossing(r, u, l, ijk[i])),
1578
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1579
        find_phi_crossing(r, u, l, ijk[i] - 1)));
12✔
1580

12✔
1581
  } else {
×
1582
    return find_z_crossing(r, u, l, ijk[i]);
12✔
1583
  }
12✔
UNCOV
1584
}
×
UNCOV
1585

×
1586
int CylindricalMesh::set_grid()
UNCOV
1587
{
×
1588
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1589
    static_cast<int>(grid_[1].size()) - 1,
1590
    static_cast<int>(grid_[2].size()) - 1};
1591

12✔
1592
  for (const auto& g : grid_) {
36✔
1593
    if (g.size() < 2) {
24✔
1594
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
24✔
1595
                 "must each have at least 2 points");
1596
      return OPENMC_E_INVALID_ARGUMENT;
120✔
1597
    }
96✔
1598
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
96✔
1599
        g.end()) {
1600
      set_errmsg("Values in for r-, phi-, and z- grids for "
1601
                 "cylindrical meshes must be sorted and unique.");
1602
      return OPENMC_E_INVALID_ARGUMENT;
24✔
1603
    }
12✔
1604
  }
1605
  if (grid_[0].front() < 0.0) {
126✔
1606
    set_errmsg("r-grid for "
1607
               "cylindrical meshes must start at r >= 0.");
126✔
1608
    return OPENMC_E_INVALID_ARGUMENT;
126✔
1609
  }
126✔
1610
  if (grid_[1].front() < 0.0) {
126✔
1611
    set_errmsg("phi-grid for "
1612
               "cylindrical meshes must start at phi >= 0.");
144✔
1613
    return OPENMC_E_INVALID_ARGUMENT;
1614
  }
144✔
1615
  if (grid_[1].back() > 2.0 * PI) {
1616
    set_errmsg("phi-grids for "
576✔
1617
               "cylindrical meshes must end with theta <= 2*pi.");
432✔
1618

1619
    return OPENMC_E_INVALID_ARGUMENT;
144✔
1620
  }
1621

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

1624
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
1625
    origin_[2] + grid_[2].front()};
1626
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
413✔
1627
    origin_[2] + grid_[2].back()};
413✔
1628

1629
  return 0;
413✔
1630
}
413✔
1631

413✔
1632
int CylindricalMesh::get_index_in_direction(double r, int i) const
413✔
1633
{
413✔
1634
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1635
}
413✔
UNCOV
1636

×
1637
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1638
  Position plot_ll, Position plot_ur) const
413✔
1639
{
1640
  fatal_error("Plot of cylindrical Mesh not implemented");
1641

1642
  // Figure out which axes lie in the plane of the plot.
516✔
1643
  array<vector<double>, 2> axis_lines;
1644
  return {axis_lines[0], axis_lines[1]};
516✔
1645
}
1646

1647
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
50,568,432✔
1648
{
1649
  write_dataset(mesh_group, "r_grid", grid_[0]);
1650
  write_dataset(mesh_group, "phi_grid", grid_[1]);
50,568,432✔
1651
  write_dataset(mesh_group, "z_grid", grid_[2]);
1652
  write_dataset(mesh_group, "origin", origin_);
50,568,432✔
1653
}
50,568,432✔
1654

50,568,432✔
1655
double CylindricalMesh::volume(const MeshIndex& ijk) const
1656
{
50,568,432✔
UNCOV
1657
  double r_i = grid_[0][ijk[0] - 1];
×
1658
  double r_o = grid_[0][ijk[0]];
1659

50,568,432✔
1660
  double phi_i = grid_[1][ijk[1] - 1];
50,568,432✔
1661
  double phi_o = grid_[1][ijk[1]];
25,300,356✔
1662

1663
  double z_i = grid_[2][ijk[2] - 1];
1664
  double z_o = grid_[2][ijk[2]];
50,568,432✔
1665

1666
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
50,568,432✔
1667
}
1668

50,568,432✔
1669
//==============================================================================
1670
// SphericalMesh implementation
1671
//==============================================================================
96,000✔
1672

1673
SphericalMesh::SphericalMesh(pugi::xml_node node)
1674
  : PeriodicStructuredMesh {node}
96,000✔
1675
{
96,000✔
1676
  n_dimension_ = 3;
1677

96,000✔
1678
  grid_[0] = get_node_array<double>(node, "r_grid");
96,000✔
1679
  grid_[1] = get_node_array<double>(node, "theta_grid");
1680
  grid_[2] = get_node_array<double>(node, "phi_grid");
96,000✔
1681
  origin_ = get_node_position(node, "origin");
96,000✔
1682

1683
  if (int err = set_grid()) {
96,000✔
1684
    fatal_error(openmc_err_msg);
96,000✔
1685
  }
96,000✔
1686
}
96,000✔
1687

96,000✔
1688
const std::string SphericalMesh::mesh_type = "spherical";
1689

96,000✔
1690
std::string SphericalMesh::get_mesh_type() const
96,000✔
1691
{
1692
  return mesh_type;
96,000✔
1693
}
1694

1695
StructuredMesh::MeshIndex SphericalMesh::get_indices(
148,757,592✔
1696
  Position r, bool& in_mesh) const
1697
{
1698
  local_coords(r);
1699

148,757,592✔
1700
  Position mapped_r;
18,260,184✔
1701
  mapped_r[0] = r.norm();
1702

1703
  if (mapped_r[0] < FP_PRECISION) {
1704
    mapped_r[1] = 0.0;
1705
    mapped_r[2] = 0.0;
1706
  } else {
130,497,408✔
1707
    mapped_r[1] = std::acos(r.z / mapped_r.x);
130,497,408✔
1708
    mapped_r[2] = std::atan2(r.y, r.x);
7,273,620✔
1709
    if (mapped_r[2] < 0)
1710
      mapped_r[2] += 2 * M_PI;
123,223,788✔
1711
  }
1712

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

64,320✔
1715
  idx[1] = sanitize_theta(idx[1]);
1716
  idx[2] = sanitize_phi(idx[2]);
1717

123,159,468✔
1718
  return idx;
1719
}
123,159,468✔
1720

123,159,468✔
1721
Position SphericalMesh::sample_element(
123,159,468✔
1722
  const MeshIndex& ijk, uint64_t* seed) const
1723
{
123,159,468✔
1724
  double r_min = this->r(ijk[0] - 1);
17,009,892✔
1725
  double r_max = this->r(ijk[0]);
1726

106,149,576✔
1727
  double theta_min = this->theta(ijk[1] - 1);
1728
  double theta_max = this->theta(ijk[1]);
1729

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

99,092,040✔
1733
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
20,246,952✔
1734
  double sin_theta = std::sin(std::acos(cos_theta));
78,845,088✔
1735
  double phi = uniform_distribution(phi_min, phi_max, seed);
47,862,120✔
1736
  double r_min_cub = std::pow(r_min, 3);
1737
  double r_max_cub = std::pow(r_max, 3);
30,982,968✔
1738
  // might be faster to do rejection here?
1739
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
1740

74,283,288✔
1741
  double x = r * std::cos(phi) * sin_theta;
1742
  double y = r * std::sin(phi) * sin_theta;
1743
  double z = r * cos_theta;
1744

74,283,288✔
1745
  return origin_ + Position(x, y, z);
32,466,432✔
1746
}
1747

41,816,856✔
1748
double SphericalMesh::find_r_crossing(
1749
  const Position& r, const Direction& u, double l, int shell) const
41,816,856✔
1750
{
1751
  if ((shell < 0) || (shell > shape_[0]))
1752
    return INFTY;
1753

1754
  // solve |r+s*u| = r0
1755
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
1756
  const double r0 = grid_[0][shell];
41,816,856✔
1757
  if (r0 == 0.0)
41,816,856✔
1758
    return INFTY;
1759
  const double p = r.dot(u);
41,816,856✔
1760
  double c = r.dot(r) - r0 * r0;
1761
  double D = p * p - c;
1762

41,816,856✔
1763
  if (std::abs(c) <= RADIAL_MESH_TOL)
41,532,408✔
1764
    return INFTY;
1765

1766
  if (D >= 0.0) {
41,532,408✔
1767
    D = std::sqrt(D);
19,437,552✔
1768
    // the solution -p - D is always smaller as -p + D : Check this one first
1769
    if (-p - D > l)
1770
      return -p - D;
22,379,304✔
1771
    if (-p + D > l)
1772
      return -p + D;
1773
  }
39,992,448✔
1774

1775
  return INFTY;
1776
}
39,992,448✔
1777

39,992,448✔
1778
double SphericalMesh::find_theta_crossing(
1779
  const Position& r, const Direction& u, double l, int shell) const
1780
{
39,992,448✔
1781
  // Theta grid is [0, π], thus there is no real surface to cross
1,219,872✔
1782
  if (full_theta_ && (shape_[1] == 1))
1783
    return INFTY;
38,772,576✔
1784

38,772,576✔
1785
  shell = sanitize_theta(shell);
17,769,456✔
1786

17,769,456✔
1787
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
21,003,120✔
1788
  // yields
18,037,548✔
1789
  // a*s^2 + 2*b*s + c == 0 with
18,037,548✔
1790
  // a = cos(theta)^2 - u.z * u.z
1791
  // b = r*u * cos(theta)^2 - u.z * r.z
38,772,576✔
1792
  // c = r*r * cos(theta)^2 - r.z^2
1793

1794
  const double cos_t = std::cos(grid_[1][shell]);
151,512,888✔
1795
  const bool sgn = std::signbit(cos_t);
1796
  const double cos_t_2 = cos_t * cos_t;
1797

1798
  const double a = cos_t_2 - u.z * u.z;
151,512,888✔
1799
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
1800
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
151,512,888✔
1801

1802
  // if factor of s^2 is zero, direction of flight is parallel to theta
74,378,796✔
1803
  // surface
74,378,796✔
1804
  if (std::abs(a) < FP_PRECISION) {
148,757,592✔
1805
    // if b vanishes, direction of flight is within theta surface and crossing
1806
    // is not possible
77,134,092✔
1807
    if (std::abs(b) < FP_PRECISION)
1808
      return INFTY;
37,141,644✔
1809

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

1816
    // no crossing is possible
1817
    return INFTY;
1818
  }
437✔
1819

1820
  const double p = b / a;
437✔
1821
  double D = p * p - c / a;
437✔
1822

437✔
1823
  if (D < 0.0)
1824
    return INFTY;
1,748✔
1825

1,311✔
UNCOV
1826
  D = std::sqrt(D);
×
1827

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

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

×
1839
  return INFTY;
UNCOV
1840
}
×
1841

1842
double SphericalMesh::find_phi_crossing(
437✔
UNCOV
1843
  const Position& r, const Direction& u, double l, int shell) const
×
1844
{
UNCOV
1845
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
×
1846
  if (full_phi_ && (shape_[2] == 1))
1847
    return INFTY;
437✔
UNCOV
1848

×
1849
  shell = sanitize_phi(shell);
1850

UNCOV
1851
  const double p0 = grid_[2][shell];
×
1852

1853
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1854
  // => x(s) * cos(p0) = y(s) * sin(p0)
437✔
1855
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1856
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
874✔
1857

874✔
1858
  const double c0 = std::cos(p0);
874✔
1859
  const double s0 = std::sin(p0);
874✔
1860

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

1863
  // Check if direction of flight is not parallel to phi surface
1864
  if (std::abs(denominator) > FP_PRECISION) {
151,705,296✔
1865
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1866
    // Check if solution is in positive direction of flight and crosses the
151,705,296✔
1867
    // correct phi surface (not -phi)
1868
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
UNCOV
1869
      return s;
×
1870
  }
1871

UNCOV
1872
  return INFTY;
×
1873
}
1874

1875
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1876
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1877
  double l) const
1878
{
1879

396✔
1880
  if (i == 0) {
1881
    return std::min(
396✔
1882
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
396✔
1883
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
396✔
1884

396✔
1885
  } else if (i == 1) {
396✔
1886
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
1887
                      find_theta_crossing(r0, u, l, ijk[i])),
384✔
1888
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
1889
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
384✔
1890

384✔
1891
  } else {
1892
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
384✔
1893
                      find_phi_crossing(r0, u, l, ijk[i])),
384✔
1894
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1895
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
384✔
1896
  }
384✔
1897
}
1898

384✔
1899
int SphericalMesh::set_grid()
1900
{
1901
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1902
    static_cast<int>(grid_[1].size()) - 1,
1903
    static_cast<int>(grid_[2].size()) - 1};
1904

1905
  for (const auto& g : grid_) {
317✔
1906
    if (g.size() < 2) {
317✔
1907
      set_errmsg("x-, y-, and z- grids for spherical meshes "
1908
                 "must each have at least 2 points");
317✔
1909
      return OPENMC_E_INVALID_ARGUMENT;
1910
    }
317✔
1911
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
317✔
1912
        g.end()) {
317✔
1913
      set_errmsg("Values in for r-, theta-, and phi- grids for "
317✔
1914
                 "spherical meshes must be sorted and unique.");
1915
      return OPENMC_E_INVALID_ARGUMENT;
317✔
UNCOV
1916
    }
×
1917
    if (g.front() < 0.0) {
1918
      set_errmsg("r-, theta-, and phi- grids for "
317✔
1919
                 "spherical meshes must start at v >= 0.");
1920
      return OPENMC_E_INVALID_ARGUMENT;
1921
    }
1922
  }
372✔
1923
  if (grid_[1].back() > PI) {
1924
    set_errmsg("theta-grids for "
372✔
1925
               "spherical meshes must end with theta <= pi.");
1926

1927
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1928
  }
1929
  if (grid_[2].back() > 2 * PI) {
1930
    set_errmsg("phi-grids for "
73,752,732✔
1931
               "spherical meshes must end with phi <= 2*pi.");
1932
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1933
  }
73,752,732✔
1934

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

×
1938
  double r = grid_[0].back();
1939
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
73,752,732✔
1940
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
73,752,732✔
1941

73,752,732✔
1942
  return 0;
36,882,336✔
1943
}
1944

1945
int SphericalMesh::get_index_in_direction(double r, int i) const
73,752,732✔
1946
{
1947
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
73,752,732✔
1948
}
73,752,732✔
1949

1950
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
73,752,732✔
1951
  Position plot_ll, Position plot_ur) const
1952
{
UNCOV
1953
  fatal_error("Plot of spherical Mesh not implemented");
×
1954

1955
  // Figure out which axes lie in the plane of the plot.
UNCOV
1956
  array<vector<double>, 2> axis_lines;
×
UNCOV
1957
  return {axis_lines[0], axis_lines[1]};
×
1958
}
UNCOV
1959

×
UNCOV
1960
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
×
1961
{
UNCOV
1962
  write_dataset(mesh_group, "r_grid", grid_[0]);
×
UNCOV
1963
  write_dataset(mesh_group, "theta_grid", grid_[1]);
×
1964
  write_dataset(mesh_group, "phi_grid", grid_[2]);
UNCOV
1965
  write_dataset(mesh_group, "origin", origin_);
×
UNCOV
1966
}
×
UNCOV
1967

×
UNCOV
1968
double SphericalMesh::volume(const MeshIndex& ijk) const
×
UNCOV
1969
{
×
1970
  double r_i = grid_[0][ijk[0] - 1];
UNCOV
1971
  double r_o = grid_[0][ijk[0]];
×
1972

UNCOV
1973
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
1974
  double theta_o = grid_[1][ijk[1]];
×
UNCOV
1975

×
1976
  double phi_i = grid_[2][ijk[2] - 1];
UNCOV
1977
  double phi_o = grid_[2][ijk[2]];
×
1978

1979
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
1980
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
481,615,104✔
1981
}
1982

1983
//==============================================================================
481,615,104✔
1984
// Helper functions for the C API
44,030,820✔
1985
//==============================================================================
1986

1987
int check_mesh(int32_t index)
1988
{
437,584,284✔
1989
  if (index < 0 || index >= model::meshes.size()) {
437,584,284✔
1990
    set_errmsg("Index in meshes array is out of bounds.");
7,601,184✔
1991
    return OPENMC_E_OUT_OF_BOUNDS;
429,983,100✔
1992
  }
429,983,100✔
1993
  return 0;
429,983,100✔
1994
}
1995

429,983,100✔
1996
template<class T>
11,548,560✔
1997
int check_mesh_type(int32_t index)
1998
{
418,434,540✔
1999
  if (int err = check_mesh(index))
388,836,240✔
2000
    return err;
2001

388,836,240✔
2002
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
69,512,340✔
2003
  if (!mesh) {
319,323,900✔
2004
    set_errmsg("This function is not valid for input mesh.");
192,371,628✔
2005
    return OPENMC_E_INVALID_TYPE;
2006
  }
2007
  return 0;
156,550,572✔
2008
}
2009

2010
template<class T>
118,217,520✔
2011
bool is_mesh_type(int32_t index)
2012
{
2013
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
2014
  return mesh;
118,217,520✔
2015
}
76,498,272✔
2016

2017
//==============================================================================
41,719,248✔
2018
// C API functions
2019
//==============================================================================
2020

2021
// Return the type of mesh as a C string
2022
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2023
{
2024
  if (int err = check_mesh(index))
2025
    return err;
2026

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

41,719,248✔
2029
  return 0;
2030
}
41,719,248✔
2031

41,719,248✔
2032
//! Extend the meshes array by n elements
41,719,248✔
2033
extern "C" int openmc_extend_meshes(
2034
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2035
{
2036
  if (index_start)
41,719,248✔
2037
    *index_start = model::meshes.size();
2038
  std::string mesh_type;
2039

526,416✔
2040
  for (int i = 0; i < n; ++i) {
526,416✔
2041
    if (RegularMesh::mesh_type == type) {
UNCOV
2042
      model::meshes.push_back(make_unique<RegularMesh>());
×
2043
    } else if (RectilinearMesh::mesh_type == type) {
2044
      model::meshes.push_back(make_unique<RectilinearMesh>());
UNCOV
2045
    } else if (CylindricalMesh::mesh_type == type) {
×
UNCOV
2046
      model::meshes.push_back(make_unique<CylindricalMesh>());
×
2047
    } else if (SphericalMesh::mesh_type == type) {
2048
      model::meshes.push_back(make_unique<SphericalMesh>());
UNCOV
2049
    } else {
×
2050
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
2051
    }
2052
  }
41,192,832✔
2053
  if (index_end)
41,192,832✔
2054
    *index_end = model::meshes.size() - 1;
2055

41,192,832✔
2056
  return 0;
11,955,456✔
2057
}
2058

29,237,376✔
2059
//! Adds a new unstructured mesh to OpenMC
2060
extern "C" int openmc_add_unstructured_mesh(
2061
  const char filename[], const char library[], int* id)
29,237,376✔
2062
{
2063
  std::string lib_name(library);
29,237,376✔
2064
  std::string mesh_file(filename);
5,599,656✔
2065
  bool valid_lib = false;
2066

23,637,720✔
2067
#ifdef DAGMC
2068
  if (lib_name == MOABMesh::mesh_lib_type) {
23,637,720✔
2069
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
11,064,504✔
2070
    valid_lib = true;
2071
  }
12,573,216✔
2072
#endif
2073

2074
#ifdef LIBMESH
119,966,232✔
2075
  if (lib_name == LibMesh::mesh_lib_type) {
2076
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2077
    valid_lib = true;
2078
  }
119,966,232✔
2079
#endif
76,498,272✔
2080

2081
  if (!valid_lib) {
43,467,960✔
2082
    set_errmsg(fmt::format("Mesh library {} is not supported "
2083
                           "by this build of OpenMC",
43,467,960✔
2084
      lib_name));
2085
    return OPENMC_E_INVALID_ARGUMENT;
2086
  }
2087

2088
  // auto-assign new ID
2089
  model::meshes.back()->set_id(-1);
2090
  *id = model::meshes.back()->id_;
43,467,960✔
2091

43,467,960✔
2092
  return 0;
2093
}
43,467,960✔
2094

2095
//! Return the index in the meshes array of a mesh with a given ID
2096
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
43,467,960✔
2097
{
43,212,696✔
2098
  auto pair = model::mesh_map.find(id);
2099
  if (pair == model::mesh_map.end()) {
2100
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
43,212,696✔
2101
    return OPENMC_E_INVALID_ID;
19,003,152✔
2102
  }
2103
  *index = pair->second;
2104
  return 0;
24,464,808✔
2105
}
2106

2107
//! Return the ID of a mesh
359,899,428✔
2108
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2109
{
2110
  if (int err = check_mesh(index))
2111
    return err;
2112
  *id = model::meshes[index]->id_;
359,899,428✔
2113
  return 0;
240,807,552✔
2114
}
240,807,552✔
2115

481,615,104✔
2116
//! Set the ID of a mesh
2117
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
119,091,876✔
2118
{
59,108,760✔
2119
  if (int err = check_mesh(index))
59,108,760✔
2120
    return err;
59,108,760✔
2121
  model::meshes[index]->id_ = id;
118,217,520✔
2122
  model::mesh_map[id] = index;
2123
  return 0;
2124
}
59,983,116✔
2125

59,983,116✔
2126
//! Get the number of elements in a mesh
59,983,116✔
2127
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
119,966,232✔
2128
{
2129
  if (int err = check_mesh(index))
2130
    return err;
2131
  *n = model::meshes[index]->n_bins();
341✔
2132
  return 0;
2133
}
341✔
2134

341✔
2135
//! Get the volume of each element in the mesh
341✔
2136
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
2137
{
1,364✔
2138
  if (int err = check_mesh(index))
1,023✔
2139
    return err;
×
2140
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
2141
    volumes[i] = model::meshes[index]->volume(i);
×
2142
  }
2143
  return 0;
1,023✔
2144
}
2,046✔
UNCOV
2145

×
2146
//! Get the bounding box of a mesh
UNCOV
2147
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
×
2148
{
2149
  if (int err = check_mesh(index))
1,023✔
2150
    return err;
×
2151

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

2154
  // set lower left corner values
2155
  ll[0] = bbox.xmin;
341✔
2156
  ll[1] = bbox.ymin;
×
2157
  ll[2] = bbox.zmin;
2158

UNCOV
2159
  // set upper right corner values
×
2160
  ur[0] = bbox.xmax;
2161
  ur[1] = bbox.ymax;
341✔
UNCOV
2162
  ur[2] = bbox.zmax;
×
2163
  return 0;
UNCOV
2164
}
×
2165

2166
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
2167
  int nz, int max_mats, int32_t* materials, double* volumes)
341✔
2168
{
341✔
2169
  if (int err = check_mesh(index))
2170
    return err;
341✔
2171

341✔
2172
  try {
341✔
2173
    model::meshes[index]->material_volumes(
2174
      nx, ny, nz, max_mats, materials, volumes);
341✔
2175
  } catch (const std::exception& e) {
2176
    set_errmsg(e.what());
2177
    if (starts_with(e.what(), "Mesh")) {
221,258,196✔
2178
      return OPENMC_E_GEOMETRY;
2179
    } else {
221,258,196✔
2180
      return OPENMC_E_ALLOCATE;
2181
    }
NEW
2182
  }
×
2183

2184
  return 0;
UNCOV
2185
}
×
2186

2187
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
2188
  Position width, int basis, int* pixels, int32_t* data)
2189
{
2190
  if (int err = check_mesh(index))
2191
    return err;
2192
  const auto& mesh = model::meshes[index].get();
312✔
2193

2194
  int pixel_width = pixels[0];
312✔
2195
  int pixel_height = pixels[1];
312✔
2196

312✔
2197
  // get pixel size
312✔
2198
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
312✔
2199
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
2200

528✔
2201
  // setup basis indices and initial position centered on pixel
2202
  int in_i, out_i;
528✔
2203
  Position xyz = origin;
528✔
2204
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
2205
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
528✔
2206
  switch (basis_enum) {
528✔
2207
  case PlotBasis::xy:
2208
    in_i = 0;
528✔
2209
    out_i = 1;
528✔
2210
    break;
2211
  case PlotBasis::xz:
528✔
2212
    in_i = 0;
528✔
2213
    out_i = 2;
2214
    break;
2215
  case PlotBasis::yz:
2216
    in_i = 1;
2217
    out_i = 2;
2218
    break;
2219
  default:
9,840✔
2220
    UNREACHABLE();
2221
  }
9,840✔
UNCOV
2222

×
UNCOV
2223
  // set initial position
×
2224
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2225
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
9,840✔
2226

2227
#pragma omp parallel
2228
  {
2229
    Position r = xyz;
1,845✔
2230

2231
#pragma omp for
1,845✔
UNCOV
2232
    for (int y = 0; y < pixel_height; y++) {
×
2233
      r[out_i] = xyz[out_i] - out_pixel * y;
2234
      for (int x = 0; x < pixel_width; x++) {
1,845✔
2235
        r[in_i] = xyz[in_i] + in_pixel * x;
1,845✔
UNCOV
2236
        data[pixel_width * y + x] = mesh->get_bin(r);
×
UNCOV
2237
      }
×
2238
    }
2239
  }
1,845✔
2240

2241
  return 0;
156✔
2242
}
2243

156✔
UNCOV
2244
//! Get the dimension of a regular mesh
×
2245
extern "C" int openmc_regular_mesh_get_dimension(
2246
  int32_t index, int** dims, int* n)
156✔
2247
{
156✔
UNCOV
2248
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2249
    return err;
×
2250
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2251
  *dims = mesh->shape_.data();
156✔
2252
  *n = mesh->n_dimension_;
2253
  return 0;
156✔
2254
}
2255

156✔
UNCOV
2256
//! Set the dimension of a regular mesh
×
2257
extern "C" int openmc_regular_mesh_set_dimension(
2258
  int32_t index, int n, const int* dims)
156✔
2259
{
156✔
UNCOV
2260
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2261
    return err;
×
2262
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2263

156✔
2264
  // Copy dimension
2265
  mesh->n_dimension_ = n;
264✔
2266
  std::copy(dims, dims + n, mesh->shape_.begin());
2267
  return 0;
264✔
UNCOV
2268
}
×
2269

2270
//! Get the regular mesh parameters
264✔
2271
extern "C" int openmc_regular_mesh_get_params(
264✔
UNCOV
2272
  int32_t index, double** ll, double** ur, double** width, int* n)
×
UNCOV
2273
{
×
2274
  if (int err = check_mesh_type<RegularMesh>(index))
2275
    return err;
264✔
2276
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2277

1,269✔
2278
  if (m->lower_left_.dimension() == 0) {
2279
    set_errmsg("Mesh parameters have not been set.");
1,269✔
UNCOV
2280
    return OPENMC_E_ALLOCATE;
×
2281
  }
2282

1,269✔
2283
  *ll = m->lower_left_.data();
1,269✔
UNCOV
2284
  *ur = m->upper_right_.data();
×
UNCOV
2285
  *width = m->width_.data();
×
2286
  *n = m->n_dimension_;
2287
  return 0;
1,269✔
2288
}
2289

2290
//! Set the regular mesh parameters
2291
extern "C" int openmc_regular_mesh_set_params(
2292
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2293
{
2294
  if (int err = check_mesh_type<RegularMesh>(index))
2295
    return err;
2296
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2297

2298
  if (m->n_dimension_ == -1) {
2299
    set_errmsg("Need to set mesh dimension before setting parameters.");
2300
    return OPENMC_E_UNASSIGNED;
2301
  }
2302

2,223✔
2303
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2304
  if (ll && ur) {
2,223✔
UNCOV
2305
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
×
2306
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2307
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
2,223✔
2308
  } else if (ll && width) {
2309
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
2,223✔
2310
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2311
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2312
  } else if (ur && width) {
2313
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
501✔
2314
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2315
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
2316
  } else {
501✔
2317
    set_errmsg("At least two parameters must be specified.");
501✔
2318
    return OPENMC_E_INVALID_ARGUMENT;
501✔
2319
  }
2320

1,002✔
2321
  // Set material volumes
501✔
2322

375✔
2323
  // TODO: incorporate this into method in RegularMesh that can be called from
126✔
2324
  // here and from constructor
78✔
2325
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
48✔
2326
  m->element_volume_ = 1.0;
24✔
2327
  for (int i = 0; i < m->n_dimension_; i++) {
24✔
2328
    m->element_volume_ *= m->width_[i];
24✔
2329
  }
UNCOV
2330

×
2331
  return 0;
2332
}
2333

501✔
UNCOV
2334
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
×
2335
template<class C>
2336
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
501✔
2337
  const int nx, const double* grid_y, const int ny, const double* grid_z,
501✔
2338
  const int nz)
2339
{
2340
  if (int err = check_mesh_type<C>(index))
×
2341
    return err;
2342

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

×
UNCOV
2345
  m->n_dimension_ = 3;
×
2346

2347
  m->grid_[0].reserve(nx);
2348
  m->grid_[1].reserve(ny);
2349
  m->grid_[2].reserve(nz);
2350

2351
  for (int i = 0; i < nx; i++) {
2352
    m->grid_[0].push_back(grid_x[i]);
2353
  }
2354
  for (int i = 0; i < ny; i++) {
2355
    m->grid_[1].push_back(grid_y[i]);
2356
  }
2357
  for (int i = 0; i < nz; i++) {
2358
    m->grid_[2].push_back(grid_z[i]);
2359
  }
2360

UNCOV
2361
  int err = m->set_grid();
×
2362
  return err;
×
2363
}
2364

UNCOV
2365
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
×
2366
template<class C>
2367
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
2368
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2369
{
×
UNCOV
2370
  if (int err = check_mesh_type<C>(index))
×
2371
    return err;
UNCOV
2372
  C* m = dynamic_cast<C*>(model::meshes[index].get());
×
2373

2374
  if (m->lower_left_.dimension() == 0) {
2375
    set_errmsg("Mesh parameters have not been set.");
2376
    return OPENMC_E_ALLOCATE;
693✔
2377
  }
2378

693✔
2379
  *grid_x = m->grid_[0].data();
693✔
UNCOV
2380
  *nx = m->grid_[0].size();
×
UNCOV
2381
  *grid_y = m->grid_[1].data();
×
2382
  *ny = m->grid_[1].size();
2383
  *grid_z = m->grid_[2].data();
693✔
2384
  *nz = m->grid_[2].size();
693✔
2385

2386
  return 0;
2387
}
2388

4,575✔
2389
//! Get the rectilinear mesh grid
2390
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
4,575✔
UNCOV
2391
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2392
{
4,575✔
2393
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
4,575✔
2394
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2395
}
2396

2397
//! Set the rectilienar mesh parameters
501✔
2398
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
2399
  const double* grid_x, const int nx, const double* grid_y, const int ny,
501✔
UNCOV
2400
  const double* grid_z, const int nz)
×
2401
{
501✔
2402
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
501✔
2403
    index, grid_x, nx, grid_y, ny, grid_z, nz);
501✔
2404
}
2405

2406
//! Get the cylindrical mesh grid
2407
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
252✔
2408
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2409
{
252✔
UNCOV
2410
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
×
2411
    index, grid_x, nx, grid_y, ny, grid_z, nz);
252✔
2412
}
252✔
2413

2414
//! Set the cylindrical mesh parameters
2415
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
2416
  const double* grid_x, const int nx, const double* grid_y, const int ny,
96✔
2417
  const double* grid_z, const int nz)
2418
{
96✔
UNCOV
2419
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
×
2420
    index, grid_x, nx, grid_y, ny, grid_z, nz);
1,056✔
2421
}
960✔
2422

2423
//! Get the spherical mesh grid
96✔
2424
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
2425
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2426
{
2427

144✔
2428
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2429
    index, grid_x, nx, grid_y, ny, grid_z, nz);
144✔
UNCOV
2430
  ;
×
2431
}
2432

144✔
2433
//! Set the spherical mesh parameters
2434
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
2435
  const double* grid_x, const int nx, const double* grid_y, const int ny,
144✔
2436
  const double* grid_z, const int nz)
144✔
2437
{
144✔
2438
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
2439
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2440
}
144✔
2441

144✔
2442
#ifdef DAGMC
144✔
2443

144✔
2444
const std::string MOABMesh::mesh_lib_type = "moab";
2445

2446
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
156✔
2447
{
2448
  initialize();
2449
}
156✔
UNCOV
2450

×
2451
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2452
{
2453
  filename_ = filename;
156✔
2454
  set_length_multiplier(length_multiplier);
2455
  initialize();
12✔
2456
}
12✔
2457

12✔
2458
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
12✔
2459
{
UNCOV
2460
  mbi_ = external_mbi;
×
2461
  filename_ = "unknown (external file)";
2462
  this->initialize();
12✔
2463
}
2464

144✔
2465
void MOABMesh::initialize()
2466
{
2467

48✔
2468
  // Create the MOAB interface and load data from file
2469
  this->create_interface();
2470

48✔
UNCOV
2471
  // Initialise MOAB error code
×
2472
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2473

2474
  // Set the dimension
48✔
2475
  n_dimension_ = 3;
48✔
2476

2477
  // set member range of tetrahedral entities
2478
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
48✔
2479
  if (rval != moab::MB_SUCCESS) {
48✔
2480
    fatal_error("Failed to get all tetrahedral elements");
2481
  }
2482

2483
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2484
    warning("Non-tetrahedral elements found in unstructured "
2485
            "mesh file: " +
48✔
2486
            filename_);
48✔
2487
  }
48✔
2488

48✔
2489
  // set member range of vertices
48✔
2490
  int vertex_dim = 0;
48✔
2491
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
2492
  if (rval != moab::MB_SUCCESS) {
×
2493
    fatal_error("Failed to get all vertex handles");
×
2494
  }
×
UNCOV
2495

×
UNCOV
2496
  // make an entity set for all tetrahedra
×
UNCOV
2497
  // this is used for convenience later in output
×
UNCOV
2498
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
×
UNCOV
2499
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2500
    fatal_error("Failed to create an entity set for the tetrahedral elements");
×
2501
  }
2502

2503
  rval = mbi_->add_entities(tetset_, ehs_);
2504
  if (rval != moab::MB_SUCCESS) {
48✔
2505
    fatal_error("Failed to add tetrahedra to an entity set.");
48✔
2506
  }
2507

24✔
2508
  if (length_multiplier_ > 0.0) {
2509
    // get the connectivity of all tets
24✔
2510
    moab::Range adj;
2511
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
2512
    if (rval != moab::MB_SUCCESS) {
504✔
2513
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
480✔
2514
    }
10,080✔
2515
    // scale all vertex coords by multiplier (done individually so not all
9,600✔
2516
    // coordinates are in memory twice at once)
9,600✔
2517
    for (auto vert : adj) {
2518
      // retrieve coords
2519
      std::array<double, 3> coord;
2520
      rval = mbi_->get_coords(&vert, 1, coord.data());
2521
      if (rval != moab::MB_SUCCESS) {
48✔
2522
        fatal_error("Could not get coordinates of vertex.");
2523
      }
2524
      // scale coords
2525
      for (auto& c : coord) {
12✔
2526
        c *= length_multiplier_;
2527
      }
2528
      // set new coords
12✔
UNCOV
2529
      rval = mbi_->set_coords(&vert, 1, coord.data());
×
2530
      if (rval != moab::MB_SUCCESS) {
12✔
2531
        fatal_error("Failed to set new vertex coordinates");
12✔
2532
      }
12✔
2533
    }
12✔
2534
  }
2535

2536
  // Determine bounds of mesh
2537
  this->determine_bounds();
399✔
2538
}
2539

2540
void MOABMesh::prepare_for_point_location()
399✔
UNCOV
2541
{
×
2542
  // if the KDTree has already been constructed, do nothing
399✔
2543
  if (kdtree_)
2544
    return;
2545

399✔
2546
  // build acceleration data structures
399✔
2547
  compute_barycentric_data(ehs_);
399✔
2548
  build_kdtree(ehs_);
2549
}
2550

2551
void MOABMesh::create_interface()
423✔
2552
{
2553
  // Do not create a MOAB instance if one is already in memory
2554
  if (mbi_)
423✔
UNCOV
2555
    return;
×
2556

423✔
2557
  // create MOAB instance
2558
  mbi_ = std::make_shared<moab::Core>();
423✔
UNCOV
2559

×
UNCOV
2560
  // load unstructured mesh file
×
2561
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2562
  if (rval != moab::MB_SUCCESS) {
2563
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
423✔
2564
  }
423✔
2565
}
423✔
2566

423✔
2567
void MOABMesh::build_kdtree(const moab::Range& all_tets)
423✔
2568
{
2569
  moab::Range all_tris;
2570
  int adj_dim = 2;
2571
  write_message("Getting tet adjacencies...", 7);
435✔
2572
  moab::ErrorCode rval = mbi_->get_adjacencies(
2573
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2574
  if (rval != moab::MB_SUCCESS) {
435✔
UNCOV
2575
    fatal_error("Failed to get adjacent triangles for tets");
×
2576
  }
435✔
2577

2578
  if (!all_tris.all_of_type(moab::MBTRI)) {
435✔
UNCOV
2579
    warning("Non-triangle elements found in tet adjacencies in "
×
UNCOV
2580
            "unstructured mesh file: " +
×
2581
            filename_);
2582
  }
2583

435✔
2584
  // combine into one range
435✔
2585
  moab::Range all_tets_and_tris;
411✔
2586
  all_tets_and_tris.merge(all_tets);
411✔
2587
  all_tets_and_tris.merge(all_tris);
411✔
2588

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

12✔
2594
  // Determine what options to use
12✔
2595
  std::ostringstream options_stream;
12✔
2596
  if (options_.empty()) {
UNCOV
2597
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
×
UNCOV
2598
  } else {
×
2599
    options_stream << options_;
2600
  }
2601
  moab::FileOptions file_opts(options_stream.str().c_str());
2602

2603
  // Build the k-d tree
2604
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2605
  if (rval != moab::MB_SUCCESS) {
435✔
2606
    fatal_error("Failed to construct KDTree for the "
435✔
2607
                "unstructured mesh file: " +
1,740✔
2608
                filename_);
1,305✔
2609
  }
2610
}
2611

435✔
2612
void MOABMesh::intersect_track(const moab::CartVect& start,
435✔
2613
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2614
{
2615
  hits.clear();
2616

126✔
2617
  moab::ErrorCode rval;
2618
  vector<moab::EntityHandle> tris;
2619
  // get all intersections with triangles in the tet mesh
2620
  // (distances are relative to the start point, not the previous
126✔
NEW
2621
  // intersection)
×
2622
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
2623
    dir.array(), start.array(), tris, hits, 0, track_len);
126✔
2624
  if (rval != moab::MB_SUCCESS) {
2625
    fatal_error(
126✔
2626
      "Failed to compute intersections on unstructured mesh: " + filename_);
2627
  }
126✔
2628

126✔
2629
  // remove duplicate intersection distances
126✔
2630
  std::unique(hits.begin(), hits.end());
2631

1,044✔
2632
  // sorts by first component of std::pair by default
918✔
2633
  std::sort(hits.begin(), hits.end());
2634
}
462✔
2635

336✔
2636
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2637
  vector<int>& bins, vector<double>& lengths) const
438✔
2638
{
312✔
2639
  moab::CartVect start(r0.x, r0.y, r0.z);
2640
  moab::CartVect end(r1.x, r1.y, r1.z);
2641
  moab::CartVect dir(u.x, u.y, u.z);
126✔
2642
  dir.normalize();
126✔
2643

2644
  double track_len = (end - start).length();
24✔
2645
  if (track_len == 0.0)
2646
    return;
2647

2648
  start -= TINY_BIT * dir;
24✔
UNCOV
2649
  end += TINY_BIT * dir;
×
2650

2651
  vector<double> hits;
24✔
2652
  intersect_track(start, dir, track_len, hits);
2653

24✔
2654
  bins.clear();
2655
  lengths.clear();
24✔
2656

24✔
2657
  // if there are no intersections the track may lie entirely
24✔
2658
  // within a single tet. If this is the case, apply entire
2659
  // score to that tet and return.
96✔
2660
  if (hits.size() == 0) {
72✔
2661
    Position midpoint = r0 + u * (track_len * 0.5);
2662
    int bin = this->get_bin(midpoint);
96✔
2663
    if (bin != -1) {
72✔
2664
      bins.push_back(bin);
2665
      lengths.push_back(1.0);
108✔
2666
    }
84✔
2667
    return;
2668
  }
2669

24✔
2670
  // for each segment in the set of tracks, try to look up a tet
24✔
2671
  // at the midpoint of the segment
2672
  Position current = r0;
24✔
2673
  double last_dist = 0.0;
2674
  for (const auto& hit : hits) {
2675
    // get the segment length
2676
    double segment_length = hit - last_dist;
24✔
UNCOV
2677
    last_dist = hit;
×
2678
    // find the midpoint of this segment
2679
    Position midpoint = current + u * (segment_length * 0.5);
24✔
2680
    // try to find a tet for this position
2681
    int bin = this->get_bin(midpoint);
24✔
2682

2683
    // determine the start point for this segment
24✔
2684
    current = r0 + u * hit;
24✔
2685

24✔
2686
    if (bin == -1) {
2687
      continue;
96✔
2688
    }
72✔
2689

2690
    bins.push_back(bin);
108✔
2691
    lengths.push_back(segment_length / track_len);
84✔
2692
  }
2693

84✔
2694
  // tally remaining portion of track after last hit if
60✔
2695
  // the last segment of the track is in the mesh but doesn't
2696
  // reach the other side of the tet
2697
  if (hits.back() < track_len) {
24✔
2698
    Position segment_start = r0 + u * hits.back();
24✔
2699
    double segment_length = track_len - hits.back();
2700
    Position midpoint = segment_start + u * (segment_length * 0.5);
78✔
2701
    int bin = this->get_bin(midpoint);
2702
    if (bin != -1) {
2703
      bins.push_back(bin);
2704
      lengths.push_back(segment_length / track_len);
78✔
UNCOV
2705
    }
×
2706
  }
2707
};
78✔
2708

2709
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
78✔
2710
{
2711
  moab::CartVect pos(r.x, r.y, r.z);
78✔
2712
  // find the leaf of the kd-tree for this position
78✔
2713
  moab::AdaptiveKDTreeIter kdtree_iter;
78✔
2714
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
2715
  if (rval != moab::MB_SUCCESS) {
852✔
2716
    return 0;
774✔
2717
  }
2718

258✔
2719
  // retrieve the tet elements of this leaf
180✔
2720
  moab::EntityHandle leaf = kdtree_iter.handle();
2721
  moab::Range tets;
246✔
2722
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
168✔
2723
  if (rval != moab::MB_SUCCESS) {
2724
    warning("MOAB error finding tets.");
2725
  }
78✔
2726

78✔
2727
  // loop over the tets in this leaf, returning the containing tet if found
2728
  for (const auto& tet : tets) {
2729
    if (point_in_tet(pos, tet)) {
2730
      return tet;
2731
    }
450✔
2732
  }
2733

2734
  // if no tet is found, return an invalid handle
450✔
2735
  return 0;
×
2736
}
450✔
2737

2738
double MOABMesh::volume(int bin) const
450✔
UNCOV
2739
{
×
UNCOV
2740
  return tet_volume(get_ent_handle_from_bin(bin));
×
2741
}
2742

2743
std::string MOABMesh::library() const
450✔
2744
{
450✔
2745
  return mesh_lib_type;
450✔
2746
}
450✔
2747

450✔
2748
// Sample position within a tet for MOAB type tets
450✔
2749
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2750
{
450✔
2751

2752
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2753

2754
  // Get vertex coordinates for MOAB tet
2755
  const moab::EntityHandle* conn1;
132✔
2756
  int conn1_size;
×
2757
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
132✔
2758
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
2759
    fatal_error(fmt::format(
132✔
UNCOV
2760
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
×
UNCOV
2761
      conn1_size));
×
2762
  }
2763
  moab::CartVect p[4];
2764
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
132✔
2765
  if (rval != moab::MB_SUCCESS) {
132✔
2766
    fatal_error("Failed to get tet coords");
132✔
2767
  }
132✔
2768

132✔
2769
  std::array<Position, 4> tet_verts;
132✔
2770
  for (int i = 0; i < 4; i++) {
2771
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
132✔
2772
  }
2773
  // Samples position within tet using Barycentric stuff
132✔
2774
  return this->sample_tet(tet_verts, seed);
2775
}
2776

132✔
2777
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2778
{
132✔
2779
  vector<moab::EntityHandle> conn;
2780
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
132✔
UNCOV
2781
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2782
    fatal_error("Failed to get tet connectivity");
×
2783
  }
2784

2785
  moab::CartVect p[4];
132✔
2786
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
132✔
2787
  if (rval != moab::MB_SUCCESS) {
132✔
2788
    fatal_error("Failed to get tet coords");
132✔
2789
  }
132✔
2790

132✔
2791
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2792
}
132✔
2793

2794
int MOABMesh::get_bin(Position r) const
186✔
2795
{
2796
  moab::EntityHandle tet = get_tet(r);
2797
  if (tet == 0) {
186✔
2798
    return -1;
×
2799
  } else {
186✔
2800
    return get_bin_from_ent_handle(tet);
2801
  }
186✔
UNCOV
2802
}
×
UNCOV
2803

×
2804
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2805
{
2806
  moab::ErrorCode rval;
186✔
2807

186✔
2808
  baryc_data_.clear();
186✔
2809
  baryc_data_.resize(tets.size());
186✔
2810

186✔
2811
  // compute the barycentric data for each tet element
186✔
2812
  // and store it as a 3x3 matrix
2813
  for (auto& tet : tets) {
186✔
2814
    vector<moab::EntityHandle> verts;
2815
    rval = mbi_->get_connectivity(&tet, 1, verts);
2816
    if (rval != moab::MB_SUCCESS) {
2817
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
186✔
2818
    }
2819

2820
    moab::CartVect p[4];
186✔
2821
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
186✔
2822
    if (rval != moab::MB_SUCCESS) {
2823
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2824
    }
2825

78✔
2826
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2827

2828
    // invert now to avoid this cost later
2829
    a = a.transpose().inverse();
78✔
2830
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
78✔
2831
  }
2832
}
2833

2834
bool MOABMesh::point_in_tet(
132✔
2835
  const moab::CartVect& r, moab::EntityHandle tet) const
2836
{
2837

132✔
2838
  moab::ErrorCode rval;
132✔
2839

2840
  // get tet vertices
2841
  vector<moab::EntityHandle> verts;
2842
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2843
  if (rval != moab::MB_SUCCESS) {
2844
    warning("Failed to get vertices of tet in umesh: " + filename_);
2845
    return false;
2846
  }
24✔
2847

24✔
2848
  // first vertex is used as a reference point for the barycentric data -
2849
  // retrieve its coordinates
2850
  moab::CartVect p_zero;
2851
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
132✔
2852
  if (rval != moab::MB_SUCCESS) {
2853
    warning("Failed to get coordinates of a vertex in "
2854
            "unstructured mesh: " +
2855
            filename_);
132✔
2856
    return false;
132✔
2857
  }
2858

2859
  // look up barycentric data
2860
  int idx = get_bin_from_ent_handle(tet);
2861
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2862

2863
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2864

2865
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
24✔
2866
          bary_coords[2] >= 0.0 &&
24✔
2867
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2868
}
2869

2870
int MOABMesh::get_bin_from_index(int idx) const
2871
{
2872
  if (idx >= n_bins()) {
2873
    fatal_error(fmt::format("Invalid bin index: {}", idx));
22✔
2874
  }
2875
  return ehs_[idx] - ehs_[0];
22✔
2876
}
22✔
2877

2878
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2879
{
2880
  int bin = get_bin(r);
2881
  *in_mesh = bin != -1;
2882
  return bin;
2883
}
2884

2885
int MOABMesh::get_index_from_bin(int bin) const
1✔
2886
{
2887
  return bin;
1✔
2888
}
1✔
2889

1✔
2890
std::pair<vector<double>, vector<double>> MOABMesh::plot(
1✔
2891
  Position plot_ll, Position plot_ur) const
2892
{
23✔
2893
  // TODO: Implement mesh lines
2894
  return {};
2895
}
2896

23✔
2897
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2898
{
2899
  int idx = vert - verts_[0];
23✔
2900
  if (idx >= n_vertices()) {
2901
    fatal_error(
2902
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
23✔
2903
  }
2904
  return idx;
2905
}
23✔
2906

23✔
2907
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2908
{
2909
  int bin = eh - ehs_[0];
2910
  if (bin >= n_bins()) {
23✔
2911
    fatal_error(fmt::format("Invalid bin: {}", bin));
2912
  }
2913
  return bin;
2914
}
2915

2916
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2917
{
23✔
2918
  if (bin >= n_bins()) {
23✔
2919
    fatal_error(fmt::format("Invalid bin index: ", bin));
23✔
2920
  }
2921
  return ehs_[0] + bin;
2922
}
2923

2924
int MOABMesh::n_bins() const
2925
{
23✔
2926
  return ehs_.size();
23✔
2927
}
2928

2929
int MOABMesh::n_surface_bins() const
2930
{
23✔
2931
  // collect all triangles in the set of tets for this mesh
23✔
2932
  moab::Range tris;
2933
  moab::ErrorCode rval;
2934
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
2935
  if (rval != moab::MB_SUCCESS) {
23✔
2936
    warning("Failed to get all triangles in the mesh instance");
2937
    return -1;
2938
  }
2939
  return 2 * tris.size();
2940
}
2941

2942
Position MOABMesh::centroid(int bin) const
2943
{
2944
  moab::ErrorCode rval;
2945

2946
  auto tet = this->get_ent_handle_from_bin(bin);
2947

2948
  // look up the tet connectivity
2949
  vector<moab::EntityHandle> conn;
2950
  rval = mbi_->get_connectivity(&tet, 1, conn);
2951
  if (rval != moab::MB_SUCCESS) {
2952
    warning("Failed to get connectivity of a mesh element.");
2953
    return {};
2954
  }
2955

2956
  // get the coordinates
2957
  vector<moab::CartVect> coords(conn.size());
2958
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
2959
  if (rval != moab::MB_SUCCESS) {
2960
    warning("Failed to get the coordinates of a mesh element.");
2961
    return {};
2962
  }
2963

2964
  // compute the centroid of the element vertices
23✔
2965
  moab::CartVect centroid(0.0, 0.0, 0.0);
23✔
2966
  for (const auto& coord : coords) {
2967
    centroid += coord;
19✔
2968
  }
2969
  centroid /= double(coords.size());
2970

19✔
2971
  return {centroid[0], centroid[1], centroid[2]};
2972
}
2973

2974
int MOABMesh::n_vertices() const
19✔
2975
{
19✔
2976
  return verts_.size();
2977
}
2978

23✔
2979
Position MOABMesh::vertex(int id) const
2980
{
2981

23✔
2982
  moab::ErrorCode rval;
1✔
2983

2984
  moab::EntityHandle vert = verts_[id];
2985

22✔
2986
  moab::CartVect coords;
2987
  rval = mbi_->get_coords(&vert, 1, coords.array());
2988
  if (rval != moab::MB_SUCCESS) {
22✔
2989
    fatal_error("Failed to get the coordinates of a vertex.");
22✔
2990
  }
2991

2992
  return {coords[0], coords[1], coords[2]};
2993
}
2994

19✔
2995
std::vector<int> MOABMesh::connectivity(int bin) const
2996
{
19✔
2997
  moab::ErrorCode rval;
19✔
2998

19✔
2999
  auto tet = get_ent_handle_from_bin(bin);
19✔
3000

3001
  // look up the tet connectivity
19✔
3002
  vector<moab::EntityHandle> conn;
3003
  rval = mbi_->get_connectivity(&tet, 1, conn);
3004
  if (rval != moab::MB_SUCCESS) {
3005
    fatal_error("Failed to get connectivity of a mesh element.");
19✔
3006
    return {};
3007
  }
3008

3009
  std::vector<int> verts(4);
3010
  for (int i = 0; i < verts.size(); i++) {
3011
    verts[i] = get_vert_idx_from_handle(conn[i]);
3012
  }
19✔
3013

19✔
3014
  return verts;
19✔
3015
}
3016

3017
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
19✔
3018
  std::string score) const
19✔
3019
{
19✔
3020
  moab::ErrorCode rval;
3021
  // add a tag to the mesh
3022
  // all scores are treated as a single value
19✔
3023
  // with an uncertainty
19✔
3024
  moab::Tag value_tag;
3✔
3025

3026
  // create the value tag if not present and get handle
16✔
3027
  double default_val = 0.0;
3028
  auto val_string = score + "_mean";
19✔
3029
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
3030
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3031
  if (rval != moab::MB_SUCCESS) {
19✔
3032
    auto msg =
19✔
3033
      fmt::format("Could not create or retrieve the value tag for the score {}"
3034
                  " on unstructured mesh {}",
3035
        score, id_);
3036
    fatal_error(msg);
3037
  }
19✔
3038

3039
  // create the std dev tag if not present and get handle
1,519,602✔
3040
  moab::Tag error_tag;
3041
  std::string err_string = score + "_std_dev";
3042
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
1,519,602✔
3043
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
3044
  if (rval != moab::MB_SUCCESS) {
3045
    auto msg =
1,519,602✔
3046
      fmt::format("Could not create or retrieve the error tag for the score {}"
3047
                  " on unstructured mesh {}",
3048
        score, id_);
3049
    fatal_error(msg);
1,519,602✔
3050
  }
3051

1,519,602✔
3052
  // return the populated tag handles
3053
  return {value_tag, error_tag};
3054
}
3055

3056
void MOABMesh::add_score(const std::string& score)
3057
{
1,519,602✔
3058
  auto score_tags = get_score_tags(score);
3059
  tag_names_.push_back(score);
3060
}
1,519,602✔
3061

1,519,602✔
3062
void MOABMesh::remove_scores()
3063
{
1,519,602✔
3064
  for (const auto& name : tag_names_) {
3065
    auto value_name = name + "_mean";
3066
    moab::Tag tag;
1,519,602✔
3067
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
1,519,602✔
3068
    if (rval != moab::MB_SUCCESS)
1,519,602✔
3069
      return;
1,519,602✔
3070

3071
    rval = mbi_->tag_delete(tag);
1,519,602✔
3072
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
3073
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3074
                             " on unstructured mesh {}",
3075
        name, id_);
1,519,602✔
3076
      fatal_error(msg);
1,519,602✔
3077
    }
3078

1,519,602✔
3079
    auto std_dev_name = name + "_std_dev";
1,519,602✔
3080
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
3081
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
3082
      auto msg =
1,519,602✔
3083
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
3084
                    " on unstructured mesh {}",
3085
          name, id_);
3086
    }
3087

1,519,602✔
3088
    rval = mbi_->tag_delete(tag);
701,483✔
3089
    if (rval != moab::MB_SUCCESS) {
701,483✔
3090
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3091
                             " on unstructured mesh {}",
223,515✔
3092
        name, id_);
223,515✔
3093
      fatal_error(msg);
3094
    }
701,483✔
3095
  }
3096
  tag_names_.clear();
3097
}
3098

3099
void MOABMesh::set_score_data(const std::string& score,
818,119✔
3100
  const vector<double>& values, const vector<double>& std_dev)
818,119✔
3101
{
5,506,248✔
3102
  auto score_tags = this->get_score_tags(score);
3103

4,688,129✔
3104
  moab::ErrorCode rval;
4,688,129✔
3105
  // set the score value
3106
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
4,688,129✔
3107
  if (rval != moab::MB_SUCCESS) {
3108
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
4,688,129✔
3109
                           "on unstructured mesh {}",
3110
      score, id_);
3111
    warning(msg);
4,688,129✔
3112
  }
3113

4,688,129✔
3114
  // set the error value
20,480✔
3115
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
3116
  if (rval != moab::MB_SUCCESS) {
3117
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
4,667,649✔
3118
                           "on unstructured mesh {}",
4,667,649✔
3119
      score, id_);
3120
    warning(msg);
3121
  }
3122
}
3123

3124
void MOABMesh::write(const std::string& base_filename) const
818,119✔
3125
{
818,119✔
3126
  // add extension to the base name
818,119✔
3127
  auto filename = base_filename + ".vtk";
818,119✔
3128
  write_message(5, "Writing unstructured mesh {}...", filename);
818,119✔
3129
  filename = settings::path_output + filename;
818,119✔
3130

762,819✔
3131
  // write the tetrahedral elements of the mesh only
762,819✔
3132
  // to avoid clutter from zero-value data on other
3133
  // elements during visualization
3134
  moab::ErrorCode rval;
1,519,602✔
3135
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
3136
  if (rval != moab::MB_SUCCESS) {
7,279,728✔
3137
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
3138
    warning(msg);
7,279,728✔
3139
  }
3140
}
7,279,728✔
3141

7,279,728✔
3142
#endif
7,279,728✔
3143

1,010,077✔
3144
#ifdef LIBMESH
3145

3146
const std::string LibMesh::mesh_lib_type = "libmesh";
3147

6,269,651✔
3148
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,269,651✔
3149
{
6,269,651✔
3150
  // filename_ and length_multiplier_ will already be set by the
6,269,651✔
3151
  // UnstructuredMesh constructor
3152
  set_mesh_pointer_from_filename(filename_);
3153
  set_length_multiplier(length_multiplier_);
3154
  initialize();
3155
}
259,367,091✔
3156

259,364,245✔
3157
// create the mesh from a pointer to a libMesh Mesh
6,266,805✔
3158
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
3159
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
3160
{
3161
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
3162
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
2,846✔
3163
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
7,279,728✔
3164
  }
3165

155,856✔
3166
  m_ = &input_mesh;
3167
  set_length_multiplier(length_multiplier);
155,856✔
3168
  initialize();
3169
}
3170

30✔
3171
// create the mesh from an input file
3172
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
30✔
3173
  : adaptive_(false)
3174
{
3175
  set_mesh_pointer_from_filename(filename);
3176
  set_length_multiplier(length_multiplier);
200,410✔
3177
  initialize();
3178
}
3179

200,410✔
3180
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
3181
{
3182
  filename_ = filename;
3183
  unique_m_ =
3184
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
200,410✔
3185
  m_ = unique_m_.get();
200,410✔
3186
  m_->read(filename_);
3187
}
3188

3189
// build a libMesh equation system for storing values
3190
void LibMesh::build_eqn_sys()
1,002,050✔
3191
{
200,410✔
3192
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
200,410✔
3193
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
3194
  libMesh::ExplicitSystem& eq_sys =
3195
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
3196
}
200,410✔
3197

1,002,050✔
3198
// intialize from mesh file
801,640✔
3199
void LibMesh::initialize()
3200
{
3201
  if (!settings::libmesh_comm) {
400,820✔
3202
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3203
                "communicator.");
3204
  }
155,856✔
3205

3206
  // assuming that unstructured meshes used in OpenMC are 3D
155,856✔
3207
  n_dimension_ = 3;
155,856✔
3208

155,856✔
3209
  if (length_multiplier_ > 0.0) {
3210
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
3211
  }
3212
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
779,280✔
3213
  // Otherwise assume that it is prepared by its owning application
155,856✔
3214
  if (unique_m_) {
155,856✔
3215
    m_->prepare_for_use();
3216
  }
3217

3218
  // ensure that the loaded mesh is 3 dimensional
311,712✔
3219
  if (m_->mesh_dimension() != n_dimension_) {
155,856✔
3220
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3221
                            "mesh is not a 3D mesh.",
7,279,728✔
3222
      filename_));
3223
  }
7,279,728✔
3224

7,279,728✔
3225
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3226
    pl_.emplace_back(m_->sub_point_locator());
3227
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,266,805✔
3228
    pl_.back()->enable_out_of_mesh_mode();
3229
  }
3230

3231
  // store first element in the mesh to use as an offset for bin indices
19✔
3232
  auto first_elem = *m_->elements_begin();
3233
  first_element_id_ = first_elem->id();
3234

3235
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
19✔
3236
  // contiguous in ID space, so we need to map from bin indices (defined over
19✔
3237
  // active elements) to global dof ids
3238
  if (adaptive_) {
3239
    bin_to_elem_map_.reserve(m_->n_active_elem());
3240
    elem_to_bin_map_.resize(m_->n_elem(), -1);
227,731✔
3241
    for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
227,712✔
3242
         it++) {
227,712✔
3243
      auto elem = *it;
227,712✔
3244

3245
      bin_to_elem_map_.push_back(elem->id());
3246
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3247
    }
1,138,560✔
3248
  }
227,712✔
3249

227,712✔
3250
  // bounding box for the mesh for quick rejection checks
3251
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3252
  libMesh::Point ll = bbox_.min();
3253
  libMesh::Point ur = bbox_.max();
227,712✔
3254
  lower_left_ = {ll(0), ll(1), ll(2)};
3255
  upper_right_ = {ur(0), ur(1), ur(2)};
3256
}
227,712✔
3257

227,712✔
3258
// Sample position within a tet for LibMesh type tets
227,712✔
3259
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
19✔
3260
{
3261
  const auto& elem = get_element_from_bin(bin);
259,364,245✔
3262
  // Get tet vertex coordinates from LibMesh
3263
  std::array<Position, 4> tet_verts;
3264
  for (int i = 0; i < elem.n_nodes(); i++) {
3265
    auto node_ref = elem.node_ref(i);
3266
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
3267
  }
3268
  // Samples position within tet using Barycentric coordinates
259,364,245✔
3269
  return this->sample_tet(tet_verts, seed);
259,364,245✔
3270
}
259,364,245✔
3271

3272
Position LibMesh::centroid(int bin) const
3273
{
3274
  const auto& elem = this->get_element_from_bin(bin);
3275
  auto centroid = elem.vertex_average();
3276
  return {centroid(0), centroid(1), centroid(2)};
3277
}
259,364,245✔
3278

259,364,245✔
3279
int LibMesh::n_vertices() const
259,364,245✔
3280
{
3281
  return m_->n_nodes();
3282
}
3283

3284
Position LibMesh::vertex(int vertex_id) const
3285
{
3286
  const auto node_ref = m_->node_ref(vertex_id);
3287
  return {node_ref(0), node_ref(1), node_ref(2)};
259,364,245✔
3288
}
259,364,245✔
3289

3290
std::vector<int> LibMesh::connectivity(int elem_id) const
259,364,245✔
3291
{
3292
  std::vector<int> conn;
420,046,390✔
3293
  const auto* elem_ptr = m_->elem_ptr(elem_id);
441,624,143✔
3294
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
280,941,998✔
3295
    conn.push_back(elem_ptr->node_id(i));
259,364,245✔
3296
  }
3297
  return conn;
3298
}
3299

3300
std::string LibMesh::library() const
3301
{
3302
  return mesh_lib_type;
3303
}
3304

3305
int LibMesh::n_bins() const
3306
{
3307
  return m_->n_active_elem();
3308
}
3309

3310
int LibMesh::n_surface_bins() const
3311
{
3312
  int n_bins = 0;
3313
  for (int i = 0; i < this->n_bins(); i++) {
3314
    const libMesh::Elem& e = get_element_from_bin(i);
3315
    n_bins += e.n_faces();
3316
    // if this is a boundary element, it will only be visited once,
3317
    // the number of surface bins is incremented to
3318
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
3319
      // null neighbor pointer indicates a boundary face
3320
      if (!neighbor_ptr) {
3321
        n_bins++;
3322
      }
3323
    }
3324
  }
767,424✔
3325
  return n_bins;
3326
}
767,424✔
3327

767,424✔
3328
void LibMesh::add_score(const std::string& var_name)
3329
{
3330
  if (adaptive_) {
3331
    warning(fmt::format(
767,424✔
3332
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3333
      this->id_));
3334

265,858,762✔
3335
    return;
3336
  }
265,858,762✔
3337

265,858,762✔
3338
  if (!equation_systems_) {
3339
    build_eqn_sys();
3340
  }
265,858,762✔
3341

3342
  // check if this is a new variable
3343
  std::string value_name = var_name + "_mean";
548,122✔
3344
  if (!variable_map_.count(value_name)) {
3345
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
548,122✔
3346
    auto var_num =
3347
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3348
    variable_map_[value_name] = var_num;
548,122✔
3349
  }
3350

3351
  std::string std_dev_name = var_name + "_std_dev";
266,598,805✔
3352
  // check if this is a new variable
3353
  if (!variable_map_.count(std_dev_name)) {
266,598,805✔
3354
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3355
    auto var_num =
3356
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3357
    variable_map_[std_dev_name] = var_num;
3358
  }
3359
}
3360

3361
void LibMesh::remove_scores()
3362
{
3363
  if (equation_systems_) {
3364
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3365
    eqn_sys.clear();
3366
    variable_map_.clear();
3367
  }
3368
}
3369

3370
void LibMesh::set_score_data(const std::string& var_name,
3371
  const vector<double>& values, const vector<double>& std_dev)
3372
{
3373
  if (adaptive_) {
3374
    warning(fmt::format(
3375
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3376
      this->id_));
3377

3378
    return;
3379
  }
3380

3381
  if (!equation_systems_) {
3382
    build_eqn_sys();
3383
  }
3384

3385
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3386

3387
  if (!eqn_sys.is_initialized()) {
3388
    equation_systems_->init();
3389
  }
3390

3391
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3392

3393
  // look up the value variable
3394
  std::string value_name = var_name + "_mean";
3395
  unsigned int value_num = variable_map_.at(value_name);
3396
  // look up the std dev variable
3397
  std::string std_dev_name = var_name + "_std_dev";
3398
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3399

3400
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3401
       it++) {
795,427✔
3402
    if (!(*it)->active()) {
3403
      continue;
795,427✔
3404
    }
3405

3406
    auto bin = get_bin_from_element(*it);
81,537✔
3407

3408
    // set value
3409
    vector<libMesh::dof_id_type> value_dof_indices;
3410
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3411
    Ensures(value_dof_indices.size() == 1);
81,537✔
3412
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3413

81,537✔
3414
    // set std dev
81,537✔
3415
    vector<libMesh::dof_id_type> std_dev_dof_indices;
81,537✔
3416
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3417
    Ensures(std_dev_dof_indices.size() == 1);
3418
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3419
  }
163,074✔
3420
}
3421

3422
void LibMesh::write(const std::string& filename) const
191,856✔
3423
{
3424
  if (adaptive_) {
3425
    warning(fmt::format(
3426
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
191,856✔
3427
      this->id_));
3428

3429
    return;
191,856✔
3430
  }
191,856✔
3431

191,856✔
3432
  write_message(fmt::format(
3433
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
3434
  libMesh::ExodusII_IO exo(*m_);
3435
  std::set<std::string> systems_out = {eq_system_name_};
3436
  exo.write_discontinuous_exodusII(
191,856✔
3437
    filename + ".e", *equation_systems_, &systems_out);
959,280✔
3438
}
767,424✔
3439

3440
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3441
  vector<int>& bins, vector<double>& lengths) const
191,856✔
3442
{
191,856✔
3443
  // TODO: Implement triangle crossings here
3444
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3445
}
3446

3447
int LibMesh::get_bin(Position r) const
3448
{
3449
  // look-up a tet using the point locator
3450
  libMesh::Point p(r.x, r.y, r.z);
3451

3452
  // quick rejection check
3453
  if (!bbox_.contains_point(p)) {
3454
    return -1;
3455
  }
3456

3457
  const auto& point_locator = pl_.at(thread_num());
3458

3459
  const auto elem_ptr = (*point_locator)(p);
3460
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3461
}
3462

3463
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3464
{
3465
  int bin =
3466
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3467
  if (bin >= n_bins() || bin < 0) {
3468
    fatal_error(fmt::format("Invalid bin: {}", bin));
3469
  }
3470
  return bin;
3471
}
3472

3473
std::pair<vector<double>, vector<double>> LibMesh::plot(
3474
  Position plot_ll, Position plot_ur) const
3475
{
3476
  return {};
3477
}
3478

3479
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3480
{
3481
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3482
}
3483

3484
double LibMesh::volume(int bin) const
3485
{
3486
  return this->get_element_from_bin(bin).volume();
3487
}
3488

3489
#endif // LIBMESH
3490

3491
//==============================================================================
3492
// Non-member functions
3493
//==============================================================================
3494

3495
void read_meshes(pugi::xml_node root)
3496
{
3497
  std::unordered_set<int> mesh_ids;
3498

3499
  for (auto node : root.children("mesh")) {
3500
    // Check to make sure multiple meshes in the same file don't share IDs
3501
    int id = std::stoi(get_node_value(node, "id"));
3502
    if (contains(mesh_ids, id)) {
3503
      fatal_error(fmt::format("Two or more meshes use the same unique ID "
3504
                              "'{}' in the same input file",
3505
        id));
3506
    }
3507
    mesh_ids.insert(id);
3508

3509
    // If we've already read a mesh with the same ID in a *different* file,
3510
    // assume it is the same here
3511
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3512
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3513
      continue;
3514
    }
3515

3516
    std::string mesh_type;
3517
    if (check_for_node(node, "type")) {
3518
      mesh_type = get_node_value(node, "type", true, true);
3519
    } else {
3520
      mesh_type = "regular";
3521
    }
3522

3523
    // determine the mesh library to use
3524
    std::string mesh_lib;
3525
    if (check_for_node(node, "library")) {
3526
      mesh_lib = get_node_value(node, "library", true, true);
3527
    }
3528

3529
    // Read mesh and add to vector
3530
    if (mesh_type == RegularMesh::mesh_type) {
3531
      model::meshes.push_back(make_unique<RegularMesh>(node));
3532
    } else if (mesh_type == RectilinearMesh::mesh_type) {
3533
      model::meshes.push_back(make_unique<RectilinearMesh>(node));
3534
    } else if (mesh_type == CylindricalMesh::mesh_type) {
3535
      model::meshes.push_back(make_unique<CylindricalMesh>(node));
3536
    } else if (mesh_type == SphericalMesh::mesh_type) {
3537
      model::meshes.push_back(make_unique<SphericalMesh>(node));
3538
#ifdef DAGMC
3539
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3540
               mesh_lib == MOABMesh::mesh_lib_type) {
3541
      model::meshes.push_back(make_unique<MOABMesh>(node));
3542
#endif
3543
#ifdef LIBMESH
3544
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3545
               mesh_lib == LibMesh::mesh_lib_type) {
3546
      model::meshes.push_back(make_unique<LibMesh>(node));
3547
#endif
3548
    } else if (mesh_type == UnstructuredMesh::mesh_type) {
3549
      fatal_error("Unstructured mesh support is not enabled or the mesh "
3550
                  "library is invalid.");
3551
    } else {
3552
      fatal_error("Invalid mesh type: " + mesh_type);
3553
    }
3554

3555
    // Map ID to position in vector
3556
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3557
  }
3558
}
3559

3560
void meshes_to_hdf5(hid_t group)
3561
{
3562
  // Write number of meshes
3563
  hid_t meshes_group = create_group(group, "meshes");
3564
  int32_t n_meshes = model::meshes.size();
3565
  write_attribute(meshes_group, "n_meshes", n_meshes);
3566

3567
  if (n_meshes > 0) {
3568
    // Write IDs of meshes
3569
    vector<int> ids;
3570
    for (const auto& m : model::meshes) {
3571
      m->to_hdf5(meshes_group);
3572
      ids.push_back(m->id_);
3573
    }
3574
    write_attribute(meshes_group, "ids", ids);
3575
  }
23✔
3576

3577
  close_group(meshes_group);
3578
}
3579

23✔
3580
void free_memory_mesh()
23✔
3581
{
23✔
3582
  model::meshes.clear();
23✔
3583
  model::mesh_map.clear();
3584
}
3585

3586
extern "C" int n_meshes()
3587
{
3588
  return model::meshes.size();
3589
}
3590

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