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

openmc-dev / openmc / 12605079372

03 Jan 2025 11:05PM UTC coverage: 84.823% (-0.003%) from 84.826%
12605079372

Pull #3129

github

web-flow
Merge d4ae59bfd into 775c41512
Pull Request #3129: Compute material volumes in mesh elements based on raytracing

196 of 222 new or added lines in 4 files covered. (88.29%)

1044 existing lines in 27 files now uncovered.

49961 of 58900 relevant lines covered (84.82%)

34048479.7 hits per line

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

87.27
/src/mesh.cpp
1
#include "openmc/mesh.h"
2
#include <algorithm> // for copy, equal, min, min_element
3
#include <cmath>     // for ceil
4
#include <cstddef>   // for size_t
5
#include <gsl/gsl-lite.hpp>
6
#include <string>
7

8
#ifdef OPENMC_MPI
9
#include "mpi.h"
10
#endif
11

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

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

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

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

55
namespace openmc {
56

57
//==============================================================================
58
// Global variables
59
//==============================================================================
60

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

67
namespace model {
68

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

72
} // namespace model
73

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

81
//==============================================================================
82
// Helper functions
83
//==============================================================================
84

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

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

108
//==============================================================================
109
// MaterialVolumes implementation
110
//==============================================================================
111

112
namespace detail {
113

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

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

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

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

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

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

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

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

171
} // namespace detail
172

173
//==============================================================================
174
// Mesh implementation
175
//==============================================================================
176

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

364
              cross_lattice(p, boundary);
365
            } else {
366
              // Particle crosses surface
367
              // TODO: off-by-one
368
              const auto& surf {
369
                model::surfaces[std::abs(p.surface()) - 1].get()};
527,616✔
370
              p.cross_surface(*surf);
527,616✔
371
            }
372
          }
527,616✔
373
        }
374
      }
375
    }
376
  }
78✔
377

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

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

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

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

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

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

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

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

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

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

464
  // Write mesh type
465
  write_attribute(mesh_group, "type", this->get_mesh_type());
3,036✔
466

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

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

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

476
  // Close group
477
  close_group(mesh_group);
3,036✔
478
}
3,036✔
479

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

484
std::string StructuredMesh::bin_label(int bin) const
5,437,344✔
485
{
486
  MeshIndex ijk = get_indices_from_bin(bin);
5,437,344✔
487

488
  if (n_dimension_ > 2) {
5,437,344✔
489
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,842,336✔
490
  } else if (n_dimension_ > 1) {
16,176✔
491
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,752✔
492
  } else {
493
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
494
  }
495
}
496

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

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

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

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

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

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

525
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
45✔
526
{
527

528
  // check the mesh type
529
  if (check_for_node(node, "type")) {
45✔
530
    auto temp = get_node_value(node, "type", true, true);
45✔
531
    if (temp != mesh_type) {
45✔
UNCOV
532
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
533
    }
534
  }
45✔
535

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

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

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

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

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

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

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

615
const std::string UnstructuredMesh::mesh_type = "unstructured";
616

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

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

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

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

641
  if (length_multiplier_ > 0.0)
30✔
UNCOV
642
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
643

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

652
  int num_elem_skipped = 0;
30✔
653

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

661
    volumes.emplace_back(this->volume(i));
337,712✔
662

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

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

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

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

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

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

713
StructuredMesh::MeshIndex StructuredMesh::get_indices(
967,362,727✔
714
  Position r, bool& in_mesh) const
715
{
716
  MeshIndex ijk;
717
  in_mesh = true;
967,362,727✔
718
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
719
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
720

721
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
722
      in_mesh = false;
91,752,208✔
723
  }
724
  return ijk;
967,362,727✔
725
}
726

727
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,017,135,023✔
728
{
729
  switch (n_dimension_) {
1,017,135,023✔
730
  case 1:
956,736✔
731
    return ijk[0] - 1;
956,736✔
732
  case 2:
21,602,040✔
733
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,602,040✔
734
  case 3:
994,576,247✔
735
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
994,576,247✔
UNCOV
736
  default:
×
UNCOV
737
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
738
  }
739
}
740

741
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
8,059,872✔
742
{
743
  MeshIndex ijk;
744
  if (n_dimension_ == 1) {
8,059,872✔
745
    ijk[0] = bin + 1;
300✔
746
  } else if (n_dimension_ == 2) {
8,059,572✔
747
    ijk[0] = bin % shape_[0] + 1;
15,876✔
748
    ijk[1] = bin / shape_[0] + 1;
15,876✔
749
  } else if (n_dimension_ == 3) {
8,043,696✔
750
    ijk[0] = bin % shape_[0] + 1;
8,043,696✔
751
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
8,043,696✔
752
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
8,043,696✔
753
  }
754
  return ijk;
8,059,872✔
755
}
756

757
int StructuredMesh::get_bin(Position r) const
324,602,319✔
758
{
759
  // Determine indices
760
  bool in_mesh;
761
  MeshIndex ijk = get_indices(r, in_mesh);
324,602,319✔
762
  if (!in_mesh)
324,602,319✔
763
    return -1;
22,164,978✔
764

765
  // Convert indices to bin
766
  return get_bin_from_indices(ijk);
302,437,341✔
767
}
768

769
int StructuredMesh::n_bins() const
1,001,276✔
770
{
771
  return std::accumulate(
1,001,276✔
772
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,002,552✔
773
}
774

775
int StructuredMesh::n_surface_bins() const
593✔
776
{
777
  return 4 * n_dimension_ * n_bins();
593✔
778
}
779

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

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

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

794
    // determine scoring bin for entropy mesh
UNCOV
795
    int mesh_bin = get_bin(site.r);
×
796

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

803
    // Add to appropriate bin
UNCOV
804
    cnt(mesh_bin) += site.wgt;
×
805
  }
806

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

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

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

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

UNCOV
832
  return counts;
×
833
}
834

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

848
  // Compute the length of the entire track.
849
  double total_distance = (r1 - r0).norm();
642,840,220✔
850
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
642,840,220✔
851
    return;
5,711,856✔
852

853
  const int n = n_dimension_;
637,128,364✔
854

855
  // Flag if position is inside the mesh
856
  bool in_mesh;
857

858
  // Position is r = r0 + u * traveled_distance, start at r0
859
  double traveled_distance {0.0};
637,128,364✔
860

861
  // Calculate index of current cell. Offset the position a tiny bit in
862
  // direction of flight
863
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
637,128,364✔
864

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

874
  // translate start and end positions,
875
  // this needs to come after the get_indices call because it does its own
876
  // translation
877
  local_coords(r0);
637,128,352✔
878
  local_coords(r1);
637,128,352✔
879

880
  // Calculate initial distances to next surfaces in all three dimensions
881
  std::array<MeshDistance, 3> distances;
1,274,256,704✔
882
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
883
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
1,893,353,016✔
884
  }
885

886
  // Loop until r = r1 is eventually reached
887
  while (true) {
357,898,280✔
888

889
    if (in_mesh) {
995,026,632✔
890

891
      // find surface with minimal distance to current position
892
      const auto k = std::min_element(distances.begin(), distances.end()) -
903,056,471✔
893
                     distances.begin();
903,056,471✔
894

895
      // Tally track length delta since last step
896
      tally.track(ijk,
903,056,471✔
897
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
903,056,471✔
898
          total_distance);
899

900
      // update position and leave, if we have reached end position
901
      traveled_distance = distances[k].distance;
903,056,471✔
902
      if (traveled_distance >= total_distance)
903,056,471✔
903
        return;
550,790,235✔
904

905
      // If we have not reached r1, we have hit a surface. Tally outward
906
      // current
907
      tally.surface(ijk, k, distances[k].max_surface, false);
352,266,236✔
908

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

915
      // Check if we have left the interior of the mesh
916
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
352,266,236✔
917

918
      // If we are still inside the mesh, tally inward current for the next
919
      // cell
920
      if (in_mesh)
352,266,236✔
921
        tally.surface(ijk, k, !distances[k].max_surface, true);
327,732,141✔
922

923
    } else { // not inside mesh
924

925
      // For all directions outside the mesh, find the distance that we need
926
      // to travel to reach the next surface. Use the largest distance, as
927
      // only this will cross all outer surfaces.
928
      int k_max {0};
91,970,161✔
929
      for (int k = 0; k < n; ++k) {
365,539,171✔
930
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
366,881,745✔
931
            (distances[k].distance > traveled_distance)) {
93,312,735✔
932
          traveled_distance = distances[k].distance;
92,526,975✔
933
          k_max = k;
92,526,975✔
934
        }
935
      }
936

937
      // If r1 is not inside the mesh, exit here
938
      if (traveled_distance >= total_distance)
91,970,161✔
939
        return;
86,338,117✔
940

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

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

250,709,558✔
956
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
957
  vector<int>& bins, vector<double>& lengths) const
958
{
959

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

975
    const StructuredMesh* mesh;
250,709,558✔
976
    vector<int>& bins;
977
    vector<double>& lengths;
978
  };
979

250,709,558✔
980
  // Perform the mesh raytrace with the helper class.
981
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
982
}
983

250,709,558✔
UNCOV
984
void StructuredMesh::surface_bins_crossed(
×
UNCOV
985
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
986
{
UNCOV
987

×
988
  // Helper tally class.
989
  // stores a pointer to the mesh class and a reference to the bins parameter.
990
  // Performs the actual tally through the surface method.
991
  struct SurfaceAggregator {
992
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
993
      : mesh(_mesh), bins(_bins)
250,709,558✔
994
    {}
250,709,558✔
995
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
996
    {
997
      int i_bin =
501,419,116✔
998
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,001,002,088✔
999
      if (max)
750,292,530✔
1000
        i_bin += 2;
1001
      if (inward)
1002
        i_bin += 1;
1003
      bins.push_back(i_bin);
61,870,061✔
1004
    }
1005
    void track(const MeshIndex& idx, double l) const {}
312,579,619✔
1006

1007
    const StructuredMesh* mesh;
1008
    vector<int>& bins;
309,532,184✔
1009
  };
309,532,184✔
1010

1011
  // Perform the mesh raytrace with the helper class.
1012
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
309,532,184✔
1013
}
309,532,184✔
1014

1015
//==============================================================================
1016
// RegularMesh implementation
1017
//==============================================================================
309,532,184✔
1018

309,532,184✔
1019
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
247,905,711✔
1020
{
1021
  // Determine number of dimensions for mesh
1022
  if (!check_for_node(node, "dimension")) {
1023
    fatal_error("Must specify <dimension> on a regular mesh.");
61,626,473✔
1024
  }
1025

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

61,626,473✔
1033
  // Check that dimensions are all greater than zero
1034
  if (xt::any(shape <= 0)) {
1035
    fatal_error("All entries on the <dimension> element for a tally "
1036
                "mesh must be positive.");
61,626,473✔
1037
  }
59,328,330✔
1038

1039
  // Check for lower-left coordinates
1040
  if (check_for_node(node, "lower_left")) {
1041
    // Read mesh lower-left corner location
1042
    lower_left_ = get_node_xarray<double>(node, "lower_left");
1043
  } else {
1044
    fatal_error("Must specify <lower_left> on a mesh.");
3,047,435✔
1045
  }
11,838,440✔
1046

11,994,992✔
1047
  // Make sure lower_left and dimension match
3,203,987✔
1048
  if (shape.size() != lower_left_.size()) {
3,139,523✔
1049
    fatal_error("Number of entries on <lower_left> must be the same "
3,139,523✔
1050
                "as the number of entries on <dimension>.");
1051
  }
1052

1053
  if (check_for_node(node, "width")) {
1054
    // Make sure one of upper-right or width were specified
3,047,435✔
1055
    if (check_for_node(node, "upper_right")) {
2,803,847✔
1056
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
1057
    }
1058

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

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

218,592✔
1068
    // Check for negative widths
1069
    if (xt::any(width_ < 0.0)) {
1070
      fatal_error("Cannot have a negative <width> on a tally mesh.");
1071
    }
392,130,662✔
1072

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

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

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

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

386,418,806✔
1092
    // Set width
1093
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
1094
  } else {
1095
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
386,418,806✔
1096
  }
1097

1098
  // Set material volumes
1099
  volume_frac_ = 1.0 / xt::prod(shape)();
386,418,806✔
1100

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

1107
int RegularMesh::get_index_in_direction(double r, int i) const
1108
{
1109
  return std::ceil((r - lower_left_[i]) / width_[i]);
386,418,794✔
1110
}
386,418,794✔
1111

1112
const std::string RegularMesh::mesh_type = "regular";
1113

772,837,588✔
1114
std::string RegularMesh::get_mesh_type() const
1,529,479,280✔
1115
{
1,143,060,486✔
1116
  return mesh_type;
1117
}
1118

1119
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
296,028,219✔
1120
{
1121
  return lower_left_[i] + ijk[i] * width_[i];
682,447,013✔
1122
}
1123

1124
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
593,524,287✔
1125
{
593,524,287✔
1126
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
1127
}
1128

593,524,287✔
1129
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
593,524,287✔
1130
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1131
  double l) const
1132
{
1133
  MeshDistance d;
593,524,287✔
1134
  d.next_index = ijk[i];
593,524,287✔
1135
  if (std::abs(u[i]) < FP_PRECISION)
302,884,524✔
1136
    return d;
1137

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

290,639,763✔
1149
std::pair<vector<double>, vector<double>> RegularMesh::plot(
1150
  Position plot_ll, Position plot_ur) const
1151
{
1152
  // Figure out which axes lie in the plane of the plot.
290,639,763✔
1153
  array<int, 2> axes {-1, -1};
268,403,811✔
1154
  if (plot_ur.z == plot_ll.z) {
1155
    axes[0] = 0;
1156
    if (n_dimension_ > 1)
1157
      axes[1] = 1;
1158
  } else if (plot_ur.y == plot_ll.y) {
1159
    axes[0] = 0;
1160
    if (n_dimension_ > 2)
88,922,726✔
1161
      axes[1] = 2;
353,700,731✔
1162
  } else if (plot_ur.x == plot_ll.x) {
354,886,753✔
1163
    if (n_dimension_ > 1)
90,108,748✔
1164
      axes[0] = 1;
89,387,452✔
1165
    if (n_dimension_ > 2)
89,387,452✔
1166
      axes[1] = 2;
1167
  } else {
1168
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1169
  }
1170

88,922,726✔
1171
  // Get the coordinates of the mesh lines along both of the axes.
83,534,270✔
1172
  array<vector<double>, 2> axis_lines;
1173
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1174
    int axis = axes[i_ax];
1175
    if (axis == -1)
5,388,456✔
1176
      continue;
21,438,660✔
1177
    auto& lines {axis_lines[i_ax]};
16,050,204✔
1178

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

1187
  return {axis_lines[0], axis_lines[1]};
1188
}
392,130,662✔
1189

1190
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
1191
{
1192
  write_dataset(mesh_group, "type", "regular");
1193
  write_dataset(mesh_group, "dimension", get_x_shape());
1194
  write_dataset(mesh_group, "lower_left", lower_left_);
1195
  write_dataset(mesh_group, "upper_right", upper_right_);
1196
  write_dataset(mesh_group, "width", width_);
392,130,662✔
1197
}
1198

392,130,662✔
1199
xt::xtensor<double, 1> RegularMesh::count_sites(
392,130,662✔
1200
  const SourceSite* bank, int64_t length, bool* outside) const
563,079,966✔
1201
{
593,524,287✔
1202
  // Determine shape of array for counts
1203
  std::size_t m = this->n_bins();
593,524,287✔
1204
  vector<std::size_t> shape = {m};
593,524,287✔
1205

593,524,287✔
1206
  // Create array of zeros
1207
  xt::xarray<double> cnt {shape, 0.0};
1208
  bool outside_ = false;
1209

1210
  for (int64_t i = 0; i < length; i++) {
1211
    const auto& site = bank[i];
1212

1213
    // determine scoring bin for entropy mesh
392,130,662✔
1214
    int mesh_bin = get_bin(site.r);
392,130,662✔
1215

1216
    // if outside mesh, skip particle
250,709,558✔
1217
    if (mesh_bin < 0) {
1218
      outside_ = true;
1219
      continue;
1220
    }
1221

1222
    // Add to appropriate bin
1223
    cnt(mesh_bin) += site.wgt;
1224
  }
250,709,558✔
1225

250,709,558✔
1226
  // Create copy of count data. Since ownership will be acquired by xtensor,
250,709,558✔
1227
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
121,173,395✔
1228
  // warnings.
1229
  int total = cnt.size();
1230
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
121,173,395✔
1231

121,173,395✔
1232
#ifdef OPENMC_MPI
60,541,998✔
1233
  // collect values from all processors
121,173,395✔
1234
  MPI_Reduce(
59,546,922✔
1235
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
121,173,395✔
1236

121,173,395✔
1237
  // Check if there were sites outside the mesh for any processor
309,532,184✔
1238
  if (outside) {
1239
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1240
  }
1241
#else
1242
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1243
  if (outside)
1244
    *outside = outside_;
250,709,558✔
1245
#endif
250,709,558✔
1246

1247
  // Adapt reduced values in array back into an xarray
1248
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1249
  xt::xarray<double> counts = arr;
1250

1251
  return counts;
1,936✔
1252
}
1253

1254
double RegularMesh::volume(const MeshIndex& ijk) const
1,936✔
UNCOV
1255
{
×
1256
  return element_volume_;
1257
}
1258

1,936✔
1259
//==============================================================================
1,936✔
1260
// RectilinearMesh implementation
1,936✔
UNCOV
1261
//==============================================================================
×
1262

1263
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1,936✔
1264
{
1265
  n_dimension_ = 3;
1266

1,936✔
UNCOV
1267
  grid_[0] = get_node_array<double>(node, "x_grid");
×
1268
  grid_[1] = get_node_array<double>(node, "y_grid");
1269
  grid_[2] = get_node_array<double>(node, "z_grid");
1270

1271
  if (int err = set_grid()) {
1272
    fatal_error(openmc_err_msg);
1,936✔
1273
  }
1274
}
1,936✔
1275

UNCOV
1276
const std::string RectilinearMesh::mesh_type = "rectilinear";
×
1277

1278
std::string RectilinearMesh::get_mesh_type() const
1279
{
1280
  return mesh_type;
1,936✔
UNCOV
1281
}
×
1282

1283
double RectilinearMesh::positive_grid_boundary(
1284
  const MeshIndex& ijk, int i) const
1285
{
1,936✔
1286
  return grid_[i][ijk[i]];
1287
}
51✔
UNCOV
1288

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

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

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

×
1315
int RectilinearMesh::set_grid()
1316
{
1317
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1318
    static_cast<int>(grid_[1].size()) - 1,
1319
    static_cast<int>(grid_[2].size()) - 1};
1,885✔
UNCOV
1320

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

7,477✔
1335
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
5,541✔
1336
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
1337

1,936✔
1338
  return 0;
1339
}
2,147,483,647✔
1340

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

1346
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
3,923✔
1347
  Position plot_ll, Position plot_ur) const
1348
{
3,923✔
1349
  // Figure out which axes lie in the plane of the plot.
1350
  array<int, 2> axes {-1, -1};
1351
  if (plot_ur.z == plot_ll.z) {
812,148,831✔
1352
    axes = {0, 1};
1353
  } else if (plot_ur.y == plot_ll.y) {
812,148,831✔
1354
    axes = {0, 2};
1355
  } else if (plot_ur.x == plot_ll.x) {
1356
    axes = {1, 2};
810,909,471✔
1357
  } else {
1358
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
810,909,471✔
1359
  }
1360

1361
  // Get the coordinates of the mesh lines along both of the axes.
1,638,225,452✔
1362
  array<vector<double>, 2> axis_lines;
1363
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1364
    int axis = axes[i_ax];
1365
    vector<double>& lines {axis_lines[i_ax]};
1,638,225,452✔
1366

1,638,225,452✔
1367
    for (auto coord : grid_[axis]) {
1,638,225,452✔
1368
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,314,072✔
1369
        lines.push_back(coord);
1370
    }
1,636,911,380✔
1371
  }
1,636,911,380✔
1372

807,521,715✔
1373
  return {axis_lines[0], axis_lines[1]};
807,521,715✔
1374
}
829,389,665✔
1375

806,282,355✔
1376
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
806,282,355✔
1377
{
1378
  write_dataset(mesh_group, "type", "rectilinear");
1,636,911,380✔
1379
  write_dataset(mesh_group, "x_grid", grid_[0]);
1380
  write_dataset(mesh_group, "y_grid", grid_[1]);
1381
  write_dataset(mesh_group, "z_grid", grid_[2]);
24✔
1382
}
1383

1384
double RectilinearMesh::volume(const MeshIndex& ijk) const
1385
{
24✔
1386
  double vol {1.0};
24✔
1387

24✔
1388
  for (int i = 0; i < n_dimension_; i++) {
24✔
1389
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
24✔
UNCOV
1390
  }
×
UNCOV
1391
  return vol;
×
UNCOV
1392
}
×
UNCOV
1393

×
UNCOV
1394
//==============================================================================
×
UNCOV
1395
// CylindricalMesh implementation
×
UNCOV
1396
//==============================================================================
×
UNCOV
1397

×
UNCOV
1398
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
×
1399
  : PeriodicStructuredMesh {node}
UNCOV
1400
{
×
1401
  n_dimension_ = 3;
1402
  grid_[0] = get_node_array<double>(node, "r_grid");
1403
  grid_[1] = get_node_array<double>(node, "phi_grid");
1404
  grid_[2] = get_node_array<double>(node, "z_grid");
24✔
1405
  origin_ = get_node_position(node, "origin");
72✔
1406

48✔
1407
  if (int err = set_grid()) {
48✔
UNCOV
1408
    fatal_error(openmc_err_msg);
×
1409
  }
48✔
1410
}
1411

48✔
1412
const std::string CylindricalMesh::mesh_type = "cylindrical";
312✔
1413

264✔
1414
std::string CylindricalMesh::get_mesh_type() const
264✔
1415
{
264✔
1416
  return mesh_type;
1417
}
1418

1419
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
48✔
1420
  Position r, bool& in_mesh) const
24✔
1421
{
1422
  local_coords(r);
2,176✔
1423

1424
  Position mapped_r;
2,176✔
1425
  mapped_r[0] = std::hypot(r.x, r.y);
2,176✔
1426
  mapped_r[2] = r[2];
2,176✔
1427

2,176✔
1428
  if (mapped_r[0] < FP_PRECISION) {
2,176✔
1429
    mapped_r[1] = 0.0;
2,176✔
1430
  } else {
1431
    mapped_r[1] = std::atan2(r.y, r.x);
12,248✔
1432
    if (mapped_r[1] < 0)
1433
      mapped_r[1] += 2 * M_PI;
1434
  }
1435

12,248✔
1436
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,248✔
1437

1438
  idx[1] = sanitize_phi(idx[1]);
1439

12,248✔
1440
  return idx;
12,248✔
1441
}
1442

12,019,257✔
1443
Position CylindricalMesh::sample_element(
12,007,009✔
1444
  const MeshIndex& ijk, uint64_t* seed) const
1445
{
1446
  double r_min = this->r(ijk[0] - 1);
12,007,009✔
1447
  double r_max = this->r(ijk[0]);
1448

1449
  double phi_min = this->phi(ijk[1] - 1);
12,007,009✔
UNCOV
1450
  double phi_max = this->phi(ijk[1]);
×
UNCOV
1451

×
1452
  double z_min = this->z(ijk[2] - 1);
1453
  double z_max = this->z(ijk[2]);
1454

1455
  double r_min_sq = r_min * r_min;
12,007,009✔
1456
  double r_max_sq = r_max * r_max;
1457
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1458
  double phi = uniform_distribution(phi_min, phi_max, seed);
1459
  double z = uniform_distribution(z_min, z_max, seed);
1460

1461
  double x = r * std::cos(phi);
12,248✔
1462
  double y = r * std::sin(phi);
12,248✔
1463

1464
  return origin_ + Position(x, y, z);
1465
}
1466

7,320✔
1467
double CylindricalMesh::find_r_crossing(
7,320✔
1468
  const Position& r, const Direction& u, double l, int shell) const
1469
{
1470

7,320✔
1471
  if ((shell < 0) || (shell > shape_[0]))
7,320✔
1472
    return INFTY;
1473

1474
  // solve r.x^2 + r.y^2 == r0^2
4,928✔
1475
  // 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✔
1476
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
4,928✔
1477

1478
  const double r0 = grid_[0][shell];
1479
  if (r0 == 0.0)
1480
    return INFTY;
12,248✔
1481

12,248✔
1482
  const double denominator = u.x * u.x + u.y * u.y;
1483

24,496✔
1484
  // Direction of flight is in z-direction. Will never intersect r.
12,248✔
1485
  if (std::abs(denominator) < FP_PRECISION)
1486
    return INFTY;
983,100✔
1487

1488
  // inverse of dominator to help the compiler to speed things up
983,100✔
1489
  const double inv_denominator = 1.0 / denominator;
1490

1491
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
1492
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1493
  double D = p * p - c * inv_denominator;
1494

1495
  if (D < 0.0)
111✔
1496
    return INFTY;
1497

111✔
1498
  D = std::sqrt(D);
1499

111✔
1500
  // the solution -p - D is always smaller as -p + D : Check this one first
111✔
1501
  if (std::abs(c) <= RADIAL_MESH_TOL)
111✔
1502
    return INFTY;
1503

111✔
UNCOV
1504
  if (-p - D > l)
×
1505
    return -p - D;
1506
  if (-p + D > l)
111✔
1507
    return -p + D;
1508

1509
  return INFTY;
1510
}
328✔
1511

1512
double CylindricalMesh::find_phi_crossing(
328✔
1513
  const Position& r, const Direction& u, double l, int shell) const
1514
{
1515
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
55,925,134✔
1516
  if (full_phi_ && (shape_[1] == 1))
1517
    return INFTY;
1518

55,925,134✔
1519
  shell = sanitize_phi(shell);
1520

1521
  const double p0 = grid_[1][shell];
55,078,367✔
1522

1523
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1524
  // => x(s) * cos(p0) = y(s) * sin(p0)
55,078,367✔
1525
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1526
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1527

112,648,356✔
1528
  const double c0 = std::cos(p0);
1529
  const double s0 = std::sin(p0);
1530

1531
  const double denominator = (u.x * s0 - u.y * c0);
112,648,356✔
1532

112,648,356✔
1533
  // Check if direction of flight is not parallel to phi surface
112,648,356✔
1534
  if (std::abs(denominator) > FP_PRECISION) {
623,808✔
1535
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1536
    // Check if solution is in positive direction of flight and crosses the
112,024,548✔
1537
    // correct phi surface (not -phi)
112,024,548✔
1538
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
55,925,134✔
1539
      return s;
55,925,134✔
1540
  }
56,099,414✔
1541

55,078,367✔
1542
  return INFTY;
55,078,367✔
1543
}
1544

112,024,548✔
1545
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
1546
  const Position& r, const Direction& u, double l, int shell) const
1547
{
185✔
1548
  MeshDistance d;
1549
  d.next_index = shell;
185✔
1550

185✔
1551
  // Direction of flight is within xy-plane. Will never intersect z.
185✔
1552
  if (std::abs(u.z) < FP_PRECISION)
1553
    return d;
740✔
1554

555✔
UNCOV
1555
  d.max_surface = (u.z > 0.0);
×
1556
  if (d.max_surface && (shell <= shape_[2])) {
UNCOV
1557
    d.next_index += 1;
×
1558
    d.distance = (grid_[2][shell] - r.z) / u.z;
1559
  } else if (!d.max_surface && (shell > 0)) {
555✔
1560
    d.next_index -= 1;
1,110✔
UNCOV
1561
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
×
1562
  }
1563
  return d;
×
1564
}
1565

1566
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1567
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
185✔
1568
  double l) const
185✔
1569
{
1570
  Position r = r0 - origin_;
185✔
1571

1572
  if (i == 0) {
1573

161,323,602✔
1574
    return std::min(
1575
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
161,323,602✔
1576
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
1577

1578
  } else if (i == 1) {
12✔
1579

1580
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
1581
                      find_phi_crossing(r, u, l, ijk[i])),
1582
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
12✔
1583
        find_phi_crossing(r, u, l, ijk[i] - 1)));
12✔
UNCOV
1584

×
1585
  } else {
12✔
1586
    return find_z_crossing(r, u, l, ijk[i]);
12✔
UNCOV
1587
  }
×
UNCOV
1588
}
×
1589

UNCOV
1590
int CylindricalMesh::set_grid()
×
1591
{
1592
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1593
    static_cast<int>(grid_[1].size()) - 1,
1594
    static_cast<int>(grid_[2].size()) - 1};
12✔
1595

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

1623
    return OPENMC_E_INVALID_ARGUMENT;
144✔
1624
  }
1625

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

1628
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
1629
    origin_[2] + grid_[2].front()};
1630
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
413✔
1631
    origin_[2] + grid_[2].back()};
413✔
1632

1633
  return 0;
413✔
1634
}
413✔
1635

413✔
1636
int CylindricalMesh::get_index_in_direction(double r, int i) const
413✔
1637
{
413✔
1638
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1639
}
413✔
UNCOV
1640

×
1641
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1642
  Position plot_ll, Position plot_ur) const
413✔
1643
{
1644
  fatal_error("Plot of cylindrical Mesh not implemented");
1645

1646
  // Figure out which axes lie in the plane of the plot.
516✔
1647
  array<vector<double>, 2> axis_lines;
1648
  return {axis_lines[0], axis_lines[1]};
516✔
1649
}
1650

1651
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
50,568,432✔
1652
{
1653
  write_dataset(mesh_group, "type", "cylindrical");
1654
  write_dataset(mesh_group, "r_grid", grid_[0]);
50,568,432✔
1655
  write_dataset(mesh_group, "phi_grid", grid_[1]);
1656
  write_dataset(mesh_group, "z_grid", grid_[2]);
50,568,432✔
1657
  write_dataset(mesh_group, "origin", origin_);
50,568,432✔
1658
}
50,568,432✔
1659

1660
double CylindricalMesh::volume(const MeshIndex& ijk) const
50,568,432✔
UNCOV
1661
{
×
1662
  double r_i = grid_[0][ijk[0] - 1];
1663
  double r_o = grid_[0][ijk[0]];
50,568,432✔
1664

50,568,432✔
1665
  double phi_i = grid_[1][ijk[1] - 1];
25,300,356✔
1666
  double phi_o = grid_[1][ijk[1]];
1667

1668
  double z_i = grid_[2][ijk[2] - 1];
50,568,432✔
1669
  double z_o = grid_[2][ijk[2]];
1670

50,568,432✔
1671
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
1672
}
50,568,432✔
1673

1674
//==============================================================================
1675
// SphericalMesh implementation
96,000✔
1676
//==============================================================================
1677

1678
SphericalMesh::SphericalMesh(pugi::xml_node node)
96,000✔
1679
  : PeriodicStructuredMesh {node}
96,000✔
1680
{
1681
  n_dimension_ = 3;
96,000✔
1682

96,000✔
1683
  grid_[0] = get_node_array<double>(node, "r_grid");
1684
  grid_[1] = get_node_array<double>(node, "theta_grid");
96,000✔
1685
  grid_[2] = get_node_array<double>(node, "phi_grid");
96,000✔
1686
  origin_ = get_node_position(node, "origin");
1687

96,000✔
1688
  if (int err = set_grid()) {
96,000✔
1689
    fatal_error(openmc_err_msg);
96,000✔
1690
  }
96,000✔
1691
}
96,000✔
1692

1693
const std::string SphericalMesh::mesh_type = "spherical";
96,000✔
1694

96,000✔
1695
std::string SphericalMesh::get_mesh_type() const
1696
{
96,000✔
1697
  return mesh_type;
1698
}
1699

148,757,592✔
1700
StructuredMesh::MeshIndex SphericalMesh::get_indices(
1701
  Position r, bool& in_mesh) const
1702
{
1703
  local_coords(r);
148,757,592✔
1704

18,260,184✔
1705
  Position mapped_r;
1706
  mapped_r[0] = r.norm();
1707

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

123,223,788✔
1718
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
64,320✔
1719

1720
  idx[1] = sanitize_theta(idx[1]);
1721
  idx[2] = sanitize_phi(idx[2]);
123,159,468✔
1722

1723
  return idx;
123,159,468✔
1724
}
123,159,468✔
1725

123,159,468✔
1726
Position SphericalMesh::sample_element(
1727
  const MeshIndex& ijk, uint64_t* seed) const
123,159,468✔
1728
{
17,009,892✔
1729
  double r_min = this->r(ijk[0] - 1);
1730
  double r_max = this->r(ijk[0]);
106,149,576✔
1731

1732
  double theta_min = this->theta(ijk[1] - 1);
1733
  double theta_max = this->theta(ijk[1]);
106,149,576✔
1734

7,057,536✔
1735
  double phi_min = this->phi(ijk[2] - 1);
1736
  double phi_max = this->phi(ijk[2]);
99,092,040✔
1737

20,246,952✔
1738
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
78,845,088✔
1739
  double sin_theta = std::sin(std::acos(cos_theta));
47,862,120✔
1740
  double phi = uniform_distribution(phi_min, phi_max, seed);
1741
  double r_min_cub = std::pow(r_min, 3);
30,982,968✔
1742
  double r_max_cub = std::pow(r_max, 3);
1743
  // might be faster to do rejection here?
1744
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
74,283,288✔
1745

1746
  double x = r * std::cos(phi) * sin_theta;
1747
  double y = r * std::sin(phi) * sin_theta;
1748
  double z = r * cos_theta;
74,283,288✔
1749

32,466,432✔
1750
  return origin_ + Position(x, y, z);
1751
}
41,816,856✔
1752

1753
double SphericalMesh::find_r_crossing(
41,816,856✔
1754
  const Position& r, const Direction& u, double l, int shell) const
1755
{
1756
  if ((shell < 0) || (shell > shape_[0]))
1757
    return INFTY;
1758

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

41,532,408✔
1768
  if (std::abs(c) <= RADIAL_MESH_TOL)
1769
    return INFTY;
1770

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

1780
  return INFTY;
39,992,448✔
1781
}
39,992,448✔
1782

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

17,769,456✔
1790
  shell = sanitize_theta(shell);
17,769,456✔
1791

21,003,120✔
1792
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
18,037,548✔
1793
  // yields
18,037,548✔
1794
  // a*s^2 + 2*b*s + c == 0 with
1795
  // a = cos(theta)^2 - u.z * u.z
38,772,576✔
1796
  // b = r*u * cos(theta)^2 - u.z * r.z
1797
  // c = r*r * cos(theta)^2 - r.z^2
1798

151,512,888✔
1799
  const double cos_t = std::cos(grid_[1][shell]);
1800
  const bool sgn = std::signbit(cos_t);
1801
  const double cos_t_2 = cos_t * cos_t;
1802

151,512,888✔
1803
  const double a = cos_t_2 - u.z * u.z;
1804
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
151,512,888✔
1805
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
1806

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

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

1821
    // no crossing is possible
1822
    return INFTY;
437✔
1823
  }
1824

437✔
1825
  const double p = b / a;
437✔
1826
  double D = p * p - c / a;
437✔
1827

1828
  if (D < 0.0)
1,748✔
1829
    return INFTY;
1,311✔
UNCOV
1830

×
1831
  D = std::sqrt(D);
1832

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

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

UNCOV
1844
  return INFTY;
×
1845
}
1846

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

1854
  shell = sanitize_phi(shell);
UNCOV
1855

×
1856
  const double p0 = grid_[2][shell];
1857

1858
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
437✔
1859
  // => x(s) * cos(p0) = y(s) * sin(p0)
1860
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
874✔
1861
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
874✔
1862

874✔
1863
  const double c0 = std::cos(p0);
874✔
1864
  const double s0 = std::sin(p0);
1865

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

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

×
1877
  return INFTY;
1878
}
1879

1880
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1881
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1882
  double l) const
1883
{
396✔
1884

1885
  if (i == 0) {
396✔
1886
    return std::min(
396✔
1887
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
396✔
1888
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
396✔
1889

396✔
1890
  } else if (i == 1) {
396✔
1891
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
1892
                      find_theta_crossing(r0, u, l, ijk[i])),
384✔
1893
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
1894
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
384✔
1895

384✔
1896
  } else {
1897
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
384✔
1898
                      find_phi_crossing(r0, u, l, ijk[i])),
384✔
1899
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1900
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
384✔
1901
  }
384✔
1902
}
1903

384✔
1904
int SphericalMesh::set_grid()
1905
{
1906
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1907
    static_cast<int>(grid_[1].size()) - 1,
1908
    static_cast<int>(grid_[2].size()) - 1};
1909

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

1932
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1933
  }
1934
  if (grid_[2].back() > 2 * PI) {
1935
    set_errmsg("phi-grids for "
73,752,732✔
1936
               "spherical meshes must end with phi <= 2*pi.");
1937
    return OPENMC_E_INVALID_ARGUMENT;
73,752,732✔
1938
  }
73,752,732✔
1939

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

×
1943
  double r = grid_[0].back();
1944
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
73,752,732✔
1945
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
73,752,732✔
1946

73,752,732✔
1947
  return 0;
36,882,336✔
1948
}
1949

1950
int SphericalMesh::get_index_in_direction(double r, int i) const
73,752,732✔
1951
{
1952
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
73,752,732✔
1953
}
73,752,732✔
1954

1955
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
73,752,732✔
1956
  Position plot_ll, Position plot_ur) const
1957
{
UNCOV
1958
  fatal_error("Plot of spherical Mesh not implemented");
×
1959

1960
  // Figure out which axes lie in the plane of the plot.
UNCOV
1961
  array<vector<double>, 2> axis_lines;
×
UNCOV
1962
  return {axis_lines[0], axis_lines[1]};
×
1963
}
UNCOV
1964

×
UNCOV
1965
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
×
1966
{
UNCOV
1967
  write_dataset(mesh_group, "type", SphericalMesh::mesh_type);
×
UNCOV
1968
  write_dataset(mesh_group, "r_grid", grid_[0]);
×
1969
  write_dataset(mesh_group, "theta_grid", grid_[1]);
UNCOV
1970
  write_dataset(mesh_group, "phi_grid", grid_[2]);
×
UNCOV
1971
  write_dataset(mesh_group, "origin", origin_);
×
UNCOV
1972
}
×
UNCOV
1973

×
UNCOV
1974
double SphericalMesh::volume(const MeshIndex& ijk) const
×
1975
{
UNCOV
1976
  double r_i = grid_[0][ijk[0] - 1];
×
1977
  double r_o = grid_[0][ijk[0]];
UNCOV
1978

×
UNCOV
1979
  double theta_i = grid_[1][ijk[1] - 1];
×
UNCOV
1980
  double theta_o = grid_[1][ijk[1]];
×
1981

UNCOV
1982
  double phi_i = grid_[2][ijk[2] - 1];
×
1983
  double phi_o = grid_[2][ijk[2]];
1984

1985
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
481,615,104✔
1986
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
1987
}
1988

481,615,104✔
1989
//==============================================================================
44,030,820✔
1990
// Helper functions for the C API
1991
//==============================================================================
1992

1993
int check_mesh(int32_t index)
437,584,284✔
1994
{
437,584,284✔
1995
  if (index < 0 || index >= model::meshes.size()) {
7,601,184✔
1996
    set_errmsg("Index in meshes array is out of bounds.");
429,983,100✔
1997
    return OPENMC_E_OUT_OF_BOUNDS;
429,983,100✔
1998
  }
429,983,100✔
1999
  return 0;
2000
}
429,983,100✔
2001

11,548,560✔
2002
template<class T>
2003
int check_mesh_type(int32_t index)
418,434,540✔
2004
{
388,836,240✔
2005
  if (int err = check_mesh(index))
2006
    return err;
388,836,240✔
2007

69,512,340✔
2008
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
319,323,900✔
2009
  if (!mesh) {
192,371,628✔
2010
    set_errmsg("This function is not valid for input mesh.");
2011
    return OPENMC_E_INVALID_TYPE;
2012
  }
156,550,572✔
2013
  return 0;
2014
}
2015

118,217,520✔
2016
template<class T>
2017
bool is_mesh_type(int32_t index)
2018
{
2019
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
118,217,520✔
2020
  return mesh;
76,498,272✔
2021
}
2022

41,719,248✔
2023
//==============================================================================
2024
// C API functions
2025
//==============================================================================
2026

2027
// Return the type of mesh as a C string
2028
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
2029
{
2030
  if (int err = check_mesh(index))
2031
    return err;
41,719,248✔
2032

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

2035
  return 0;
41,719,248✔
2036
}
41,719,248✔
2037

41,719,248✔
2038
//! Extend the meshes array by n elements
2039
extern "C" int openmc_extend_meshes(
2040
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
2041
{
41,719,248✔
2042
  if (index_start)
2043
    *index_start = model::meshes.size();
2044
  std::string mesh_type;
526,416✔
2045

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

11,955,456✔
2062
  return 0;
2063
}
29,237,376✔
2064

2065
//! Adds a new unstructured mesh to OpenMC
2066
extern "C" int openmc_add_unstructured_mesh(
29,237,376✔
2067
  const char filename[], const char library[], int* id)
2068
{
29,237,376✔
2069
  std::string lib_name(library);
5,599,656✔
2070
  std::string mesh_file(filename);
2071
  bool valid_lib = false;
23,637,720✔
2072

2073
#ifdef DAGMC
23,637,720✔
2074
  if (lib_name == MOABMesh::mesh_lib_type) {
11,064,504✔
2075
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
2076
    valid_lib = true;
12,573,216✔
2077
  }
2078
#endif
2079

119,966,232✔
2080
#ifdef LIBMESH
2081
  if (lib_name == LibMesh::mesh_lib_type) {
2082
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
2083
    valid_lib = true;
119,966,232✔
2084
  }
76,498,272✔
2085
#endif
2086

43,467,960✔
2087
  if (!valid_lib) {
2088
    set_errmsg(fmt::format("Mesh library {} is not supported "
43,467,960✔
2089
                           "by this build of OpenMC",
2090
      lib_name));
2091
    return OPENMC_E_INVALID_ARGUMENT;
2092
  }
2093

2094
  // auto-assign new ID
2095
  model::meshes.back()->set_id(-1);
43,467,960✔
2096
  *id = model::meshes.back()->id_;
43,467,960✔
2097

2098
  return 0;
43,467,960✔
2099
}
2100

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

359,899,428✔
2113
//! Return the ID of a mesh
2114
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
2115
{
2116
  if (int err = check_mesh(index))
2117
    return err;
359,899,428✔
2118
  *id = model::meshes[index]->id_;
240,807,552✔
2119
  return 0;
240,807,552✔
2120
}
481,615,104✔
2121

2122
//! Set the ID of a mesh
119,091,876✔
2123
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
59,108,760✔
2124
{
59,108,760✔
2125
  if (int err = check_mesh(index))
59,108,760✔
2126
    return err;
118,217,520✔
2127
  model::meshes[index]->id_ = id;
2128
  model::mesh_map[id] = index;
2129
  return 0;
59,983,116✔
2130
}
59,983,116✔
2131

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

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

UNCOV
2152
//! Get the bounding box of a mesh
×
2153
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
2154
{
1,023✔
UNCOV
2155
  if (int err = check_mesh(index))
×
2156
    return err;
UNCOV
2157

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

2160
  // set lower left corner values
341✔
UNCOV
2161
  ll[0] = bbox.xmin;
×
2162
  ll[1] = bbox.ymin;
2163
  ll[2] = bbox.zmin;
UNCOV
2164

×
2165
  // set upper right corner values
2166
  ur[0] = bbox.xmax;
341✔
UNCOV
2167
  ur[1] = bbox.ymax;
×
2168
  ur[2] = bbox.zmax;
NEW
2169
  return 0;
×
2170
}
2171

2172
extern "C" int openmc_mesh_material_volumes(int32_t index, int nx, int ny,
341✔
2173
  int nz, int max_mats, int32_t* materials, double* volumes)
341✔
2174
{
2175
  if (int err = check_mesh(index))
341✔
2176
    return err;
341✔
2177

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

UNCOV
2190
  return 0;
×
2191
}
2192

2193
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
2194
  Position width, int basis, int* pixels, int32_t* data)
2195
{
2196
  if (int err = check_mesh(index))
2197
    return err;
312✔
2198
  const auto& mesh = model::meshes[index].get();
2199

312✔
2200
  int pixel_width = pixels[0];
312✔
2201
  int pixel_height = pixels[1];
312✔
2202

312✔
2203
  // get pixel size
312✔
2204
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
312✔
2205
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
2206

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

×
UNCOV
2229
  // set initial position
×
2230
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2231
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
9,424✔
2232

2233
#pragma omp parallel
2234
  {
2235
    Position r = xyz;
1,759✔
2236

2237
#pragma omp for
1,759✔
UNCOV
2238
    for (int y = 0; y < pixel_height; y++) {
×
2239
      r[out_i] = xyz[out_i] - out_pixel * y;
2240
      for (int x = 0; x < pixel_width; x++) {
1,759✔
2241
        r[in_i] = xyz[in_i] + in_pixel * x;
1,759✔
UNCOV
2242
        data[pixel_width * y + x] = mesh->get_bin(r);
×
UNCOV
2243
      }
×
2244
    }
2245
  }
1,759✔
2246

2247
  return 0;
156✔
2248
}
2249

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

156✔
UNCOV
2262
//! Set the dimension of a regular mesh
×
2263
extern "C" int openmc_regular_mesh_set_dimension(
2264
  int32_t index, int n, const int* dims)
156✔
2265
{
156✔
UNCOV
2266
  if (int err = check_mesh_type<RegularMesh>(index))
×
UNCOV
2267
    return err;
×
2268
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2269

156✔
2270
  // Copy dimension
2271
  mesh->n_dimension_ = n;
256✔
2272
  std::copy(dims, dims + n, mesh->shape_.begin());
2273
  return 0;
256✔
UNCOV
2274
}
×
2275

2276
//! Get the regular mesh parameters
256✔
2277
extern "C" int openmc_regular_mesh_get_params(
256✔
UNCOV
2278
  int32_t index, double** ll, double** ur, double** width, int* n)
×
UNCOV
2279
{
×
2280
  if (int err = check_mesh_type<RegularMesh>(index))
2281
    return err;
256✔
2282
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2283

1,191✔
2284
  if (m->lower_left_.dimension() == 0) {
2285
    set_errmsg("Mesh parameters have not been set.");
1,191✔
UNCOV
2286
    return OPENMC_E_ALLOCATE;
×
2287
  }
2288

1,191✔
2289
  *ll = m->lower_left_.data();
1,191✔
UNCOV
2290
  *ur = m->upper_right_.data();
×
UNCOV
2291
  *width = m->width_.data();
×
2292
  *n = m->n_dimension_;
2293
  return 0;
1,191✔
2294
}
2295

2296
//! Set the regular mesh parameters
2297
extern "C" int openmc_regular_mesh_set_params(
2298
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2299
{
2300
  if (int err = check_mesh_type<RegularMesh>(index))
2301
    return err;
2302
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2303

2304
  if (m->n_dimension_ == -1) {
2305
    set_errmsg("Need to set mesh dimension before setting parameters.");
2306
    return OPENMC_E_UNASSIGNED;
2307
  }
2308

2,133✔
2309
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2310
  if (ll && ur) {
2,133✔
UNCOV
2311
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
×
2312
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2313
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
2,133✔
2314
  } else if (ll && width) {
2315
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
2,133✔
2316
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2317
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2318
  } else if (ur && width) {
2319
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
471✔
2320
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
2321
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
2322
  } else {
471✔
2323
    set_errmsg("At least two parameters must be specified.");
471✔
2324
    return OPENMC_E_INVALID_ARGUMENT;
471✔
2325
  }
2326

942✔
2327
  // Set material volumes
471✔
2328

349✔
2329
  // TODO: incorporate this into method in RegularMesh that can be called from
122✔
2330
  // here and from constructor
74✔
2331
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
48✔
2332
  m->element_volume_ = 1.0;
24✔
2333
  for (int i = 0; i < m->n_dimension_; i++) {
24✔
2334
    m->element_volume_ *= m->width_[i];
24✔
2335
  }
UNCOV
2336

×
2337
  return 0;
2338
}
2339

471✔
2340
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
×
2341
template<class C>
2342
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
471✔
2343
  const int nx, const double* grid_y, const int ny, const double* grid_z,
471✔
2344
  const int nz)
2345
{
UNCOV
2346
  if (int err = check_mesh_type<C>(index))
×
2347
    return err;
2348

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

×
UNCOV
2351
  m->n_dimension_ = 3;
×
2352

2353
  m->grid_[0].reserve(nx);
2354
  m->grid_[1].reserve(ny);
2355
  m->grid_[2].reserve(nz);
2356

2357
  for (int i = 0; i < nx; i++) {
2358
    m->grid_[0].push_back(grid_x[i]);
2359
  }
2360
  for (int i = 0; i < ny; i++) {
2361
    m->grid_[1].push_back(grid_y[i]);
2362
  }
2363
  for (int i = 0; i < nz; i++) {
2364
    m->grid_[2].push_back(grid_z[i]);
2365
  }
2366

UNCOV
2367
  int err = m->set_grid();
×
UNCOV
2368
  return err;
×
2369
}
2370

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

2380
  if (m->lower_left_.dimension() == 0) {
2381
    set_errmsg("Mesh parameters have not been set.");
2382
    return OPENMC_E_ALLOCATE;
663✔
2383
  }
2384

663✔
2385
  *grid_x = m->grid_[0].data();
663✔
UNCOV
2386
  *nx = m->grid_[0].size();
×
UNCOV
2387
  *grid_y = m->grid_[1].data();
×
2388
  *ny = m->grid_[1].size();
2389
  *grid_z = m->grid_[2].data();
663✔
2390
  *nz = m->grid_[2].size();
663✔
2391

2392
  return 0;
2393
}
2394

4,365✔
2395
//! Get the rectilinear mesh grid
2396
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
4,365✔
UNCOV
2397
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
×
2398
{
4,365✔
2399
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
4,365✔
2400
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2401
}
2402

2403
//! Set the rectilienar mesh parameters
471✔
2404
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
2405
  const double* grid_x, const int nx, const double* grid_y, const int ny,
471✔
UNCOV
2406
  const double* grid_z, const int nz)
×
2407
{
471✔
2408
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
471✔
2409
    index, grid_x, nx, grid_y, ny, grid_z, nz);
471✔
2410
}
2411

2412
//! Get the cylindrical mesh grid
2413
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
252✔
2414
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2415
{
252✔
UNCOV
2416
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
×
2417
    index, grid_x, nx, grid_y, ny, grid_z, nz);
252✔
2418
}
252✔
2419

2420
//! Set the cylindrical mesh parameters
2421
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
2422
  const double* grid_x, const int nx, const double* grid_y, const int ny,
96✔
2423
  const double* grid_z, const int nz)
2424
{
96✔
UNCOV
2425
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
×
2426
    index, grid_x, nx, grid_y, ny, grid_z, nz);
1,056✔
2427
}
960✔
2428

2429
//! Get the spherical mesh grid
96✔
2430
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
2431
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2432
{
2433

144✔
2434
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2435
    index, grid_x, nx, grid_y, ny, grid_z, nz);
144✔
UNCOV
2436
  ;
×
2437
}
2438

144✔
2439
//! Set the spherical mesh parameters
2440
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
2441
  const double* grid_x, const int nx, const double* grid_y, const int ny,
144✔
2442
  const double* grid_z, const int nz)
144✔
2443
{
144✔
2444
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
2445
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2446
}
144✔
2447

144✔
2448
#ifdef DAGMC
144✔
2449

144✔
2450
const std::string MOABMesh::mesh_lib_type = "moab";
2451

2452
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
156✔
2453
{
2454
  initialize();
2455
}
156✔
2456

×
2457
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2458
{
2459
  filename_ = filename;
156✔
2460
  set_length_multiplier(length_multiplier);
2461
  initialize();
12✔
2462
}
12✔
2463

12✔
2464
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
12✔
2465
{
UNCOV
2466
  mbi_ = external_mbi;
×
2467
  filename_ = "unknown (external file)";
2468
  this->initialize();
12✔
2469
}
2470

144✔
2471
void MOABMesh::initialize()
2472
{
2473

48✔
2474
  // Create the MOAB interface and load data from file
2475
  this->create_interface();
2476

48✔
UNCOV
2477
  // Initialise MOAB error code
×
2478
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2479

2480
  // Set the dimension
48✔
2481
  n_dimension_ = 3;
48✔
2482

2483
  // set member range of tetrahedral entities
2484
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
48✔
2485
  if (rval != moab::MB_SUCCESS) {
48✔
2486
    fatal_error("Failed to get all tetrahedral elements");
2487
  }
2488

2489
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2490
    warning("Non-tetrahedral elements found in unstructured "
2491
            "mesh file: " +
48✔
2492
            filename_);
48✔
2493
  }
48✔
2494

48✔
2495
  // set member range of vertices
48✔
2496
  int vertex_dim = 0;
48✔
2497
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
UNCOV
2498
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2499
    fatal_error("Failed to get all vertex handles");
×
UNCOV
2500
  }
×
UNCOV
2501

×
UNCOV
2502
  // make an entity set for all tetrahedra
×
UNCOV
2503
  // this is used for convenience later in output
×
UNCOV
2504
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
×
UNCOV
2505
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2506
    fatal_error("Failed to create an entity set for the tetrahedral elements");
×
2507
  }
2508

2509
  rval = mbi_->add_entities(tetset_, ehs_);
2510
  if (rval != moab::MB_SUCCESS) {
48✔
2511
    fatal_error("Failed to add tetrahedra to an entity set.");
48✔
2512
  }
2513

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

2542
  // Determine bounds of mesh
2543
  this->determine_bounds();
373✔
2544
}
2545

2546
void MOABMesh::prepare_for_point_location()
373✔
UNCOV
2547
{
×
2548
  // if the KDTree has already been constructed, do nothing
373✔
2549
  if (kdtree_)
2550
    return;
2551

373✔
2552
  // build acceleration data structures
373✔
2553
  compute_barycentric_data(ehs_);
373✔
2554
  build_kdtree(ehs_);
2555
}
2556

2557
void MOABMesh::create_interface()
397✔
2558
{
2559
  // Do not create a MOAB instance if one is already in memory
2560
  if (mbi_)
397✔
UNCOV
2561
    return;
×
2562

397✔
2563
  // create MOAB instance
2564
  mbi_ = std::make_shared<moab::Core>();
397✔
UNCOV
2565

×
UNCOV
2566
  // load unstructured mesh file
×
2567
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
2568
  if (rval != moab::MB_SUCCESS) {
2569
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
397✔
2570
  }
397✔
2571
}
397✔
2572

397✔
2573
void MOABMesh::build_kdtree(const moab::Range& all_tets)
397✔
2574
{
2575
  moab::Range all_tris;
2576
  int adj_dim = 2;
2577
  write_message("Getting tet adjacencies...", 7);
409✔
2578
  moab::ErrorCode rval = mbi_->get_adjacencies(
2579
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
2580
  if (rval != moab::MB_SUCCESS) {
409✔
UNCOV
2581
    fatal_error("Failed to get adjacent triangles for tets");
×
2582
  }
409✔
2583

2584
  if (!all_tris.all_of_type(moab::MBTRI)) {
409✔
UNCOV
2585
    warning("Non-triangle elements found in tet adjacencies in "
×
UNCOV
2586
            "unstructured mesh file: " +
×
2587
            filename_);
2588
  }
2589

409✔
2590
  // combine into one range
409✔
2591
  moab::Range all_tets_and_tris;
385✔
2592
  all_tets_and_tris.merge(all_tets);
385✔
2593
  all_tets_and_tris.merge(all_tris);
385✔
2594

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

12✔
2600
  // Determine what options to use
12✔
2601
  std::ostringstream options_stream;
12✔
2602
  if (options_.empty()) {
UNCOV
2603
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
×
UNCOV
2604
  } else {
×
2605
    options_stream << options_;
2606
  }
2607
  moab::FileOptions file_opts(options_stream.str().c_str());
2608

2609
  // Build the k-d tree
2610
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2611
  if (rval != moab::MB_SUCCESS) {
409✔
2612
    fatal_error("Failed to construct KDTree for the "
409✔
2613
                "unstructured mesh file: " +
1,636✔
2614
                filename_);
1,227✔
2615
  }
2616
}
2617

409✔
2618
void MOABMesh::intersect_track(const moab::CartVect& start,
409✔
2619
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2620
{
2621
  hits.clear();
2622

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

122✔
2635
  // remove duplicate intersection distances
122✔
2636
  std::unique(hits.begin(), hits.end());
2637

988✔
2638
  // sorts by first component of std::pair by default
866✔
2639
  std::sort(hits.begin(), hits.end());
2640
}
450✔
2641

328✔
2642
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2643
  vector<int>& bins, vector<double>& lengths) const
426✔
2644
{
304✔
2645
  moab::CartVect start(r0.x, r0.y, r0.z);
2646
  moab::CartVect end(r1.x, r1.y, r1.z);
2647
  moab::CartVect dir(u.x, u.y, u.z);
122✔
2648
  dir.normalize();
122✔
2649

2650
  double track_len = (end - start).length();
24✔
2651
  if (track_len == 0.0)
2652
    return;
2653

2654
  start -= TINY_BIT * dir;
24✔
UNCOV
2655
  end += TINY_BIT * dir;
×
2656

2657
  vector<double> hits;
24✔
2658
  intersect_track(start, dir, track_len, hits);
2659

24✔
2660
  bins.clear();
2661
  lengths.clear();
24✔
2662

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

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

2689
    // determine the start point for this segment
24✔
2690
    current = r0 + u * hit;
24✔
2691

24✔
2692
    if (bin == -1) {
2693
      continue;
96✔
2694
    }
72✔
2695

2696
    bins.push_back(bin);
108✔
2697
    lengths.push_back(segment_length / track_len);
84✔
2698
  }
2699

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

2715
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
74✔
2716
{
2717
  moab::CartVect pos(r.x, r.y, r.z);
74✔
2718
  // find the leaf of the kd-tree for this position
74✔
2719
  moab::AdaptiveKDTreeIter kdtree_iter;
74✔
2720
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
2721
  if (rval != moab::MB_SUCCESS) {
796✔
2722
    return 0;
722✔
2723
  }
2724

246✔
2725
  // retrieve the tet elements of this leaf
172✔
2726
  moab::EntityHandle leaf = kdtree_iter.handle();
2727
  moab::Range tets;
234✔
2728
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
160✔
2729
  if (rval != moab::MB_SUCCESS) {
2730
    warning("MOAB error finding tets.");
2731
  }
74✔
2732

74✔
2733
  // loop over the tets in this leaf, returning the containing tet if found
2734
  for (const auto& tet : tets) {
2735
    if (point_in_tet(pos, tet)) {
2736
      return tet;
2737
    }
446✔
2738
  }
2739

2740
  // if no tet is found, return an invalid handle
446✔
UNCOV
2741
  return 0;
×
2742
}
446✔
2743

2744
double MOABMesh::volume(int bin) const
446✔
UNCOV
2745
{
×
UNCOV
2746
  return tet_volume(get_ent_handle_from_bin(bin));
×
2747
}
2748

2749
std::string MOABMesh::library() const
446✔
2750
{
446✔
2751
  return mesh_lib_type;
446✔
2752
}
446✔
2753

446✔
2754
// Sample position within a tet for MOAB type tets
446✔
2755
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2756
{
446✔
2757

2758
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2759

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

132✔
2775
  std::array<Position, 4> tet_verts;
132✔
2776
  for (int i = 0; i < 4; i++) {
2777
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
132✔
2778
  }
2779
  // Samples position within tet using Barycentric stuff
132✔
2780
  return this->sample_tet(tet_verts, seed);
2781
}
2782

132✔
UNCOV
2783
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2784
{
132✔
2785
  vector<moab::EntityHandle> conn;
2786
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
132✔
UNCOV
2787
  if (rval != moab::MB_SUCCESS) {
×
UNCOV
2788
    fatal_error("Failed to get tet connectivity");
×
2789
  }
2790

2791
  moab::CartVect p[4];
132✔
2792
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
132✔
2793
  if (rval != moab::MB_SUCCESS) {
132✔
2794
    fatal_error("Failed to get tet coords");
132✔
2795
  }
132✔
2796

132✔
2797
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2798
}
132✔
2799

2800
int MOABMesh::get_bin(Position r) const
182✔
2801
{
2802
  moab::EntityHandle tet = get_tet(r);
2803
  if (tet == 0) {
182✔
UNCOV
2804
    return -1;
×
2805
  } else {
182✔
2806
    return get_bin_from_ent_handle(tet);
2807
  }
182✔
UNCOV
2808
}
×
UNCOV
2809

×
2810
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
2811
{
2812
  moab::ErrorCode rval;
182✔
2813

182✔
2814
  baryc_data_.clear();
182✔
2815
  baryc_data_.resize(tets.size());
182✔
2816

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

2826
    moab::CartVect p[4];
182✔
2827
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
182✔
2828
    if (rval != moab::MB_SUCCESS) {
2829
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2830
    }
2831

74✔
2832
    moab::Matrix3 a(p[1] - p[0], p[2] - p[0], p[3] - p[0], true);
2833

2834
    // invert now to avoid this cost later
2835
    a = a.transpose().inverse();
74✔
2836
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
74✔
2837
  }
2838
}
2839

2840
bool MOABMesh::point_in_tet(
132✔
2841
  const moab::CartVect& r, moab::EntityHandle tet) const
2842
{
2843

132✔
2844
  moab::ErrorCode rval;
132✔
2845

2846
  // get tet vertices
2847
  vector<moab::EntityHandle> verts;
2848
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2849
  if (rval != moab::MB_SUCCESS) {
2850
    warning("Failed to get vertices of tet in umesh: " + filename_);
2851
    return false;
2852
  }
24✔
2853

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

2865
  // look up barycentric data
2866
  int idx = get_bin_from_ent_handle(tet);
2867
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2868

2869
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2870

2871
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
24✔
2872
          bary_coords[2] >= 0.0 &&
24✔
2873
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2874
}
2875

2876
int MOABMesh::get_bin_from_index(int idx) const
2877
{
2878
  if (idx >= n_bins()) {
2879
    fatal_error(fmt::format("Invalid bin index: {}", idx));
22✔
2880
  }
2881
  return ehs_[idx] - ehs_[0];
22✔
2882
}
22✔
2883

2884
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2885
{
2886
  int bin = get_bin(r);
2887
  *in_mesh = bin != -1;
2888
  return bin;
2889
}
2890

2891
int MOABMesh::get_index_from_bin(int bin) const
1✔
2892
{
2893
  return bin;
1✔
2894
}
1✔
2895

1✔
2896
std::pair<vector<double>, vector<double>> MOABMesh::plot(
1✔
2897
  Position plot_ll, Position plot_ur) const
2898
{
23✔
2899
  // TODO: Implement mesh lines
2900
  return {};
2901
}
2902

23✔
2903
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2904
{
2905
  int idx = vert - verts_[0];
23✔
2906
  if (idx >= n_vertices()) {
2907
    fatal_error(
2908
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
23✔
2909
  }
2910
  return idx;
2911
}
23✔
2912

23✔
2913
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2914
{
2915
  int bin = eh - ehs_[0];
2916
  if (bin >= n_bins()) {
23✔
2917
    fatal_error(fmt::format("Invalid bin: {}", bin));
2918
  }
2919
  return bin;
2920
}
2921

2922
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2923
{
23✔
2924
  if (bin >= n_bins()) {
23✔
2925
    fatal_error(fmt::format("Invalid bin index: ", bin));
23✔
2926
  }
2927
  return ehs_[0] + bin;
2928
}
2929

2930
int MOABMesh::n_bins() const
2931
{
23✔
2932
  return ehs_.size();
23✔
2933
}
2934

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

2948
Position MOABMesh::centroid(int bin) const
2949
{
2950
  moab::ErrorCode rval;
2951

2952
  auto tet = this->get_ent_handle_from_bin(bin);
2953

2954
  // look up the tet connectivity
2955
  vector<moab::EntityHandle> conn;
2956
  rval = mbi_->get_connectivity(&tet, 1, conn);
2957
  if (rval != moab::MB_SUCCESS) {
2958
    warning("Failed to get connectivity of a mesh element.");
2959
    return {};
2960
  }
2961

2962
  // get the coordinates
2963
  vector<moab::CartVect> coords(conn.size());
2964
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
2965
  if (rval != moab::MB_SUCCESS) {
2966
    warning("Failed to get the coordinates of a mesh element.");
2967
    return {};
2968
  }
2969

2970
  // compute the centroid of the element vertices
23✔
2971
  moab::CartVect centroid(0.0, 0.0, 0.0);
23✔
2972
  for (const auto& coord : coords) {
2973
    centroid += coord;
19✔
2974
  }
2975
  centroid /= double(coords.size());
2976

19✔
2977
  return {centroid[0], centroid[1], centroid[2]};
2978
}
2979

2980
int MOABMesh::n_vertices() const
19✔
2981
{
19✔
2982
  return verts_.size();
2983
}
2984

23✔
2985
Position MOABMesh::vertex(int id) const
2986
{
2987

23✔
2988
  moab::ErrorCode rval;
1✔
2989

2990
  moab::EntityHandle vert = verts_[id];
2991

22✔
2992
  moab::CartVect coords;
2993
  rval = mbi_->get_coords(&vert, 1, coords.array());
2994
  if (rval != moab::MB_SUCCESS) {
22✔
2995
    fatal_error("Failed to get the coordinates of a vertex.");
22✔
2996
  }
2997

2998
  return {coords[0], coords[1], coords[2]};
2999
}
3000

19✔
3001
std::vector<int> MOABMesh::connectivity(int bin) const
3002
{
19✔
3003
  moab::ErrorCode rval;
19✔
3004

19✔
3005
  auto tet = get_ent_handle_from_bin(bin);
19✔
3006

3007
  // look up the tet connectivity
19✔
3008
  vector<moab::EntityHandle> conn;
3009
  rval = mbi_->get_connectivity(&tet, 1, conn);
3010
  if (rval != moab::MB_SUCCESS) {
3011
    fatal_error("Failed to get connectivity of a mesh element.");
19✔
3012
    return {};
3013
  }
3014

3015
  std::vector<int> verts(4);
3016
  for (int i = 0; i < verts.size(); i++) {
3017
    verts[i] = get_vert_idx_from_handle(conn[i]);
3018
  }
19✔
3019

19✔
3020
  return verts;
19✔
3021
}
3022

3023
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
19✔
3024
  std::string score) const
19✔
3025
{
19✔
3026
  moab::ErrorCode rval;
3027
  // add a tag to the mesh
3028
  // all scores are treated as a single value
19✔
3029
  // with an uncertainty
19✔
3030
  moab::Tag value_tag;
3✔
3031

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

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

1,519,602✔
3058
  // return the populated tag handles
3059
  return {value_tag, error_tag};
3060
}
3061

3062
void MOABMesh::add_score(const std::string& score)
3063
{
1,519,602✔
3064
  auto score_tags = get_score_tags(score);
3065
  tag_names_.push_back(score);
3066
}
1,519,602✔
3067

1,519,602✔
3068
void MOABMesh::remove_scores()
3069
{
1,519,602✔
3070
  for (const auto& name : tag_names_) {
3071
    auto value_name = name + "_mean";
3072
    moab::Tag tag;
1,519,602✔
3073
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
1,519,602✔
3074
    if (rval != moab::MB_SUCCESS)
1,519,602✔
3075
      return;
1,519,602✔
3076

3077
    rval = mbi_->tag_delete(tag);
1,519,602✔
3078
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
3079
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
701,483✔
3080
                             " on unstructured mesh {}",
3081
        name, id_);
1,519,602✔
3082
      fatal_error(msg);
1,519,602✔
3083
    }
3084

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

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

3105
void MOABMesh::set_score_data(const std::string& score,
818,119✔
3106
  const vector<double>& values, const vector<double>& std_dev)
818,119✔
3107
{
5,506,248✔
3108
  auto score_tags = this->get_score_tags(score);
3109

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

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

3130
void MOABMesh::write(const std::string& base_filename) const
818,119✔
3131
{
818,119✔
3132
  // add extension to the base name
818,119✔
3133
  auto filename = base_filename + ".vtk";
818,119✔
3134
  write_message(5, "Writing unstructured mesh {}...", filename);
818,119✔
3135
  filename = settings::path_output + filename;
818,119✔
3136

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

7,279,728✔
3148
#endif
7,279,728✔
3149

1,010,077✔
3150
#ifdef LIBMESH
3151

3152
const std::string LibMesh::mesh_lib_type = "libmesh";
3153

6,269,651✔
3154
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
6,269,651✔
3155
{
6,269,651✔
3156
  // filename_ and length_multiplier_ will already be set by the
6,269,651✔
3157
  // UnstructuredMesh constructor
3158
  set_mesh_pointer_from_filename(filename_);
3159
  set_length_multiplier(length_multiplier_);
3160
  initialize();
3161
}
259,367,091✔
3162

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

155,856✔
3172
  m_ = &input_mesh;
3173
  set_length_multiplier(length_multiplier);
155,856✔
3174
  initialize();
3175
}
3176

30✔
3177
// create the mesh from an input file
3178
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
30✔
3179
  : adaptive_(false)
3180
{
3181
  set_mesh_pointer_from_filename(filename);
3182
  set_length_multiplier(length_multiplier);
200,410✔
3183
  initialize();
3184
}
3185

200,410✔
3186
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
3187
{
3188
  filename_ = filename;
3189
  unique_m_ =
3190
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
200,410✔
3191
  m_ = unique_m_.get();
200,410✔
3192
  m_->read(filename_);
3193
}
3194

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

1,002,050✔
3204
// intialize from mesh file
801,640✔
3205
void LibMesh::initialize()
3206
{
3207
  if (!settings::libmesh_comm) {
400,820✔
3208
    fatal_error("Attempting to use an unstructured mesh without a libMesh "
3209
                "communicator.");
3210
  }
155,856✔
3211

3212
  // assuming that unstructured meshes used in OpenMC are 3D
155,856✔
3213
  n_dimension_ = 3;
155,856✔
3214

155,856✔
3215
  if (length_multiplier_ > 0.0) {
3216
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
3217
  }
3218
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
779,280✔
3219
  // Otherwise assume that it is prepared by its owning application
155,856✔
3220
  if (unique_m_) {
155,856✔
3221
    m_->prepare_for_use();
3222
  }
3223

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

7,279,728✔
3231
  for (int i = 0; i < num_threads(); i++) {
1,012,923✔
3232
    pl_.emplace_back(m_->sub_point_locator());
3233
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
6,266,805✔
3234
    pl_.back()->enable_out_of_mesh_mode();
3235
  }
3236

3237
  // store first element in the mesh to use as an offset for bin indices
19✔
3238
  auto first_elem = *m_->elements_begin();
3239
  first_element_id_ = first_elem->id();
3240

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

3251
      bin_to_elem_map_.push_back(elem->id());
3252
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3253
    }
1,138,560✔
3254
  }
227,712✔
3255

227,712✔
3256
  // bounding box for the mesh for quick rejection checks
3257
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3258
  libMesh::Point ll = bbox_.min();
3259
  libMesh::Point ur = bbox_.max();
227,712✔
3260
  lower_left_ = {ll(0), ll(1), ll(2)};
3261
  upper_right_ = {ur(0), ur(1), ur(2)};
3262
}
227,712✔
3263

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

3278
Position LibMesh::centroid(int bin) const
3279
{
3280
  const auto& elem = this->get_element_from_bin(bin);
3281
  auto centroid = elem.vertex_average();
3282
  return {centroid(0), centroid(1), centroid(2)};
3283
}
259,364,245✔
3284

259,364,245✔
3285
int LibMesh::n_vertices() const
259,364,245✔
3286
{
3287
  return m_->n_nodes();
3288
}
3289

3290
Position LibMesh::vertex(int vertex_id) const
3291
{
3292
  const auto node_ref = m_->node_ref(vertex_id);
3293
  return {node_ref(0), node_ref(1), node_ref(2)};
259,364,245✔
3294
}
259,364,245✔
3295

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

3306
std::string LibMesh::library() const
3307
{
3308
  return mesh_lib_type;
3309
}
3310

3311
int LibMesh::n_bins() const
3312
{
3313
  return m_->n_active_elem();
3314
}
3315

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

767,424✔
3334
void LibMesh::add_score(const std::string& var_name)
3335
{
3336
  if (adaptive_) {
3337
    warning(fmt::format(
767,424✔
3338
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3339
      this->id_));
3340

265,858,762✔
3341
    return;
3342
  }
265,858,762✔
3343

265,858,762✔
3344
  if (!equation_systems_) {
3345
    build_eqn_sys();
3346
  }
265,858,762✔
3347

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

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

3367
void LibMesh::remove_scores()
3368
{
3369
  if (equation_systems_) {
3370
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3371
    eqn_sys.clear();
3372
    variable_map_.clear();
3373
  }
3374
}
3375

3376
void LibMesh::set_score_data(const std::string& var_name,
3377
  const vector<double>& values, const vector<double>& std_dev)
3378
{
3379
  if (adaptive_) {
3380
    warning(fmt::format(
3381
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3382
      this->id_));
3383

3384
    return;
3385
  }
3386

3387
  if (!equation_systems_) {
3388
    build_eqn_sys();
3389
  }
3390

3391
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3392

3393
  if (!eqn_sys.is_initialized()) {
3394
    equation_systems_->init();
3395
  }
3396

3397
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3398

3399
  // look up the value variable
3400
  std::string value_name = var_name + "_mean";
3401
  unsigned int value_num = variable_map_.at(value_name);
3402
  // look up the std dev variable
3403
  std::string std_dev_name = var_name + "_std_dev";
3404
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3405

3406
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3407
       it++) {
795,427✔
3408
    if (!(*it)->active()) {
3409
      continue;
795,427✔
3410
    }
3411

3412
    auto bin = get_bin_from_element(*it);
81,537✔
3413

3414
    // set value
3415
    vector<libMesh::dof_id_type> value_dof_indices;
3416
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3417
    Ensures(value_dof_indices.size() == 1);
81,537✔
3418
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3419

81,537✔
3420
    // set std dev
81,537✔
3421
    vector<libMesh::dof_id_type> std_dev_dof_indices;
81,537✔
3422
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3423
    Ensures(std_dev_dof_indices.size() == 1);
3424
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3425
  }
163,074✔
3426
}
3427

3428
void LibMesh::write(const std::string& filename) const
191,856✔
3429
{
3430
  if (adaptive_) {
3431
    warning(fmt::format(
3432
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
191,856✔
3433
      this->id_));
3434

3435
    return;
191,856✔
3436
  }
191,856✔
3437

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

3446
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3447
  vector<int>& bins, vector<double>& lengths) const
191,856✔
3448
{
191,856✔
3449
  // TODO: Implement triangle crossings here
3450
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3451
}
3452

3453
int LibMesh::get_bin(Position r) const
3454
{
3455
  // look-up a tet using the point locator
3456
  libMesh::Point p(r.x, r.y, r.z);
3457

3458
  // quick rejection check
3459
  if (!bbox_.contains_point(p)) {
3460
    return -1;
3461
  }
3462

3463
  const auto& point_locator = pl_.at(thread_num());
3464

3465
  const auto elem_ptr = (*point_locator)(p);
3466
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3467
}
3468

3469
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3470
{
3471
  int bin =
3472
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3473
  if (bin >= n_bins() || bin < 0) {
3474
    fatal_error(fmt::format("Invalid bin: {}", bin));
3475
  }
3476
  return bin;
3477
}
3478

3479
std::pair<vector<double>, vector<double>> LibMesh::plot(
3480
  Position plot_ll, Position plot_ur) const
3481
{
3482
  return {};
3483
}
3484

3485
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3486
{
3487
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3488
}
3489

3490
double LibMesh::volume(int bin) const
3491
{
3492
  return this->get_element_from_bin(bin).volume();
3493
}
3494

3495
#endif // LIBMESH
3496

3497
//==============================================================================
3498
// Non-member functions
3499
//==============================================================================
3500

3501
void read_meshes(pugi::xml_node root)
3502
{
3503
  std::unordered_set<int> mesh_ids;
3504

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

3515
    // If we've already read a mesh with the same ID in a *different* file,
3516
    // assume it is the same here
3517
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3518
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3519
      continue;
3520
    }
3521

3522
    std::string mesh_type;
3523
    if (check_for_node(node, "type")) {
3524
      mesh_type = get_node_value(node, "type", true, true);
3525
    } else {
3526
      mesh_type = "regular";
3527
    }
3528

3529
    // determine the mesh library to use
3530
    std::string mesh_lib;
3531
    if (check_for_node(node, "library")) {
3532
      mesh_lib = get_node_value(node, "library", true, true);
3533
    }
3534

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

3561
    // Map ID to position in vector
3562
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3563
  }
3564
}
3565

3566
void meshes_to_hdf5(hid_t group)
3567
{
3568
  // Write number of meshes
3569
  hid_t meshes_group = create_group(group, "meshes");
3570
  int32_t n_meshes = model::meshes.size();
3571
  write_attribute(meshes_group, "n_meshes", n_meshes);
3572

3573
  if (n_meshes > 0) {
3574
    // Write IDs of meshes
3575
    vector<int> ids;
3576
    for (const auto& m : model::meshes) {
3577
      m->to_hdf5(meshes_group);
3578
      ids.push_back(m->id_);
3579
    }
3580
    write_attribute(meshes_group, "ids", ids);
3581
  }
23✔
3582

3583
  close_group(meshes_group);
3584
}
3585

23✔
3586
void free_memory_mesh()
23✔
3587
{
23✔
3588
  model::meshes.clear();
23✔
3589
  model::mesh_map.clear();
3590
}
3591

3592
extern "C" int n_meshes()
3593
{
3594
  return model::meshes.size();
3595
}
3596

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