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

openmc-dev / openmc / 12658282051

07 Jan 2025 07:50PM UTC coverage: 84.855% (+0.03%) from 84.827%
12658282051

Pull #3056

github

web-flow
Merge 636160e69 into 393334829
Pull Request #3056: Differentiate materials in DAGMC universes

440 of 477 new or added lines in 13 files covered. (92.24%)

255 existing lines in 2 files now uncovered.

50034 of 58964 relevant lines covered (84.86%)

34131041.2 hits per line

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

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

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

13
#include "xtensor/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/particle_data.h"
33
#include "openmc/plot.h"
34
#include "openmc/random_dist.h"
35
#include "openmc/search.h"
36
#include "openmc/settings.h"
37
#include "openmc/tallies/filter.h"
38
#include "openmc/tallies/tally.h"
39
#include "openmc/volume_calc.h"
40
#include "openmc/xml_interface.h"
41

42
#ifdef LIBMESH
43
#include "libmesh/mesh_modification.h"
44
#include "libmesh/mesh_tools.h"
45
#include "libmesh/numeric_vector.h"
46
#endif
47

48
#ifdef DAGMC
49
#include "moab/FileOptions.hpp"
50
#endif
51

52
namespace openmc {
53

54
//==============================================================================
55
// Global variables
56
//==============================================================================
57

58
#ifdef LIBMESH
59
const bool LIBMESH_ENABLED = true;
60
#else
61
const bool LIBMESH_ENABLED = false;
62
#endif
63

64
namespace model {
65

66
std::unordered_map<int32_t, int32_t> mesh_map;
67
vector<unique_ptr<Mesh>> meshes;
68

69
} // namespace model
70

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

78
//==============================================================================
79
// Helper functions
80
//==============================================================================
81

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

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

105
//==============================================================================
106
// Mesh implementation
107
//==============================================================================
108

109
Mesh::Mesh(pugi::xml_node node)
2,762✔
110
{
111
  // Read mesh id
112
  id_ = std::stoi(get_node_value(node, "id"));
2,762✔
113
  if (check_for_node(node, "name"))
2,762✔
114
    name_ = get_node_value(node, "name");
17✔
115
}
2,762✔
116

117
void Mesh::set_id(int32_t id)
1✔
118
{
119
  Expects(id >= 0 || id == C_NONE);
1✔
120

121
  // Clear entry in mesh map in case one was already assigned
122
  if (id_ != C_NONE) {
1✔
123
    model::mesh_map.erase(id_);
×
UNCOV
124
    id_ = C_NONE;
×
125
  }
126

127
  // Ensure no other mesh has the same ID
128
  if (model::mesh_map.find(id) != model::mesh_map.end()) {
1✔
129
    throw std::runtime_error {
×
UNCOV
130
      fmt::format("Two meshes have the same ID: {}", id)};
×
131
  }
132

133
  // If no ID is specified, auto-assign the next ID in the sequence
134
  if (id == C_NONE) {
1✔
135
    id = 0;
1✔
136
    for (const auto& m : model::meshes) {
3✔
137
      id = std::max(id, m->id_);
2✔
138
    }
139
    ++id;
1✔
140
  }
141

142
  // Update ID and entry in the mesh map
143
  id_ = id;
1✔
144
  model::mesh_map[id] = model::meshes.size() - 1;
1✔
145
}
1✔
146

147
vector<double> Mesh::volumes() const
156✔
148
{
149
  vector<double> volumes(n_bins());
156✔
150
  for (int i = 0; i < n_bins(); i++) {
982,476✔
151
    volumes[i] = this->volume(i);
982,320✔
152
  }
153
  return volumes;
156✔
UNCOV
154
}
×
155

156
int Mesh::material_volumes(
384✔
157
  int n_sample, int bin, gsl::span<MaterialVolume> result, uint64_t* seed) const
158
{
159
  vector<int32_t> materials;
384✔
160
  vector<int64_t> hits;
384✔
161

162
#pragma omp parallel
192✔
163
  {
164
    vector<int32_t> local_materials;
192✔
165
    vector<int64_t> local_hits;
192✔
166
    GeometryState geom;
192✔
167

168
#pragma omp for
169
    for (int i = 0; i < n_sample; ++i) {
26,166,192✔
170
      // Get seed for i-th sample
171
      uint64_t seed_i = future_seed(3 * i, *seed);
26,166,000✔
172

173
      // Sample position and set geometry state
174
      geom.r() = this->sample_element(bin, &seed_i);
26,166,000✔
175
      geom.u() = {1., 0., 0.};
26,166,000✔
176
      geom.n_coord() = 1;
26,166,000✔
177

178
      // If this location is not in the geometry at all, move on to next block
179
      if (!exhaustive_find_cell(geom))
26,166,000✔
180
        continue;
363,036✔
181

182
      int i_material = geom.material();
25,802,964✔
183

184
      // Check if this material was previously hit and if so, increment count
185
      auto it =
186
        std::find(local_materials.begin(), local_materials.end(), i_material);
25,802,964✔
187
      if (it == local_materials.end()) {
25,802,964✔
188
        local_materials.push_back(i_material);
384✔
189
        local_hits.push_back(1);
384✔
190
      } else {
191
        local_hits[it - local_materials.begin()]++;
25,802,580✔
192
      }
193
    } // omp for
194

195
    // Reduce index/hits lists from each thread into a single copy
196
    reduce_indices_hits(local_materials, local_hits, materials, hits);
192✔
197
  } // omp parallel
192✔
198

199
  // Advance RNG seed
200
  advance_prn_seed(3 * n_sample, seed);
384✔
201

202
  // Make sure span passed in is large enough
203
  if (hits.size() > result.size()) {
384✔
UNCOV
204
    return -1;
×
205
  }
206

207
  // Convert hits to fractions
208
  for (int i_mat = 0; i_mat < hits.size(); ++i_mat) {
1,152✔
209
    double fraction = double(hits[i_mat]) / n_sample;
768✔
210
    result[i_mat].material = materials[i_mat];
768✔
211
    result[i_mat].volume = fraction * this->volume(bin);
768✔
212
  }
213
  return hits.size();
384✔
214
}
384✔
215

UNCOV
216
vector<Mesh::MaterialVolume> Mesh::material_volumes(
×
217
  int n_sample, int bin, uint64_t* seed) const
218
{
219
  // Create result vector with space for 8 pairs
220
  vector<Mesh::MaterialVolume> result;
×
UNCOV
221
  result.reserve(8);
×
222

UNCOV
223
  int size = -1;
×
224
  while (true) {
225
    // Get material volumes
226
    size = this->material_volumes(
×
UNCOV
227
      n_sample, bin, {result.data(), result.data() + result.capacity()}, seed);
×
228

229
    // If capacity was sufficient, resize the vector and return
230
    if (size >= 0) {
×
231
      result.resize(size);
×
UNCOV
232
      break;
×
233
    }
234

235
    // Otherwise, increase capacity of the vector
UNCOV
236
    result.reserve(2 * result.capacity());
×
237
  }
238

239
  return result;
×
UNCOV
240
}
×
241

242
void Mesh::to_hdf5(hid_t group) const
3,036✔
243
{
244
  // Create group for mesh
245
  std::string group_name = fmt::format("mesh {}", id_);
5,506✔
246
  hid_t mesh_group = create_group(group, group_name.c_str());
3,036✔
247

248
  // Write mesh type
249
  write_attribute(mesh_group, "type", this->get_mesh_type());
3,036✔
250

251
  // Write mesh ID
252
  write_attribute(mesh_group, "id", id_);
3,036✔
253

254
  // Write mesh name
255
  write_dataset(mesh_group, "name", name_);
3,036✔
256

257
  // Write mesh data
258
  this->to_hdf5_inner(mesh_group);
3,036✔
259

260
  // Close group
261
  close_group(mesh_group);
3,036✔
262
}
3,036✔
263

264
//==============================================================================
265
// Structured Mesh implementation
266
//==============================================================================
267

268
std::string StructuredMesh::bin_label(int bin) const
5,437,344✔
269
{
270
  MeshIndex ijk = get_indices_from_bin(bin);
5,437,344✔
271

272
  if (n_dimension_ > 2) {
5,437,344✔
273
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,842,336✔
274
  } else if (n_dimension_ > 1) {
16,176✔
275
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,752✔
276
  } else {
277
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
278
  }
279
}
280

281
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,994✔
282
{
283
  // because method is const, shape_ is const as well and can't be adapted
284
  auto tmp_shape = shape_;
2,994✔
285
  return xt::adapt(tmp_shape, {n_dimension_});
5,988✔
286
}
287

288
Position StructuredMesh::sample_element(
51,712,212✔
289
  const MeshIndex& ijk, uint64_t* seed) const
290
{
291
  // lookup the lower/upper bounds for the mesh element
292
  double x_min = negative_grid_boundary(ijk, 0);
51,712,212✔
293
  double x_max = positive_grid_boundary(ijk, 0);
51,712,212✔
294

295
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
51,712,212✔
296
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
51,712,212✔
297

298
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
51,712,212✔
299
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
51,712,212✔
300

301
  return {x_min + (x_max - x_min) * prn(seed),
51,712,212✔
302
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
51,712,212✔
303
}
304

305
//==============================================================================
306
// Unstructured Mesh implementation
307
//==============================================================================
308

309
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
45✔
310
{
311

312
  // check the mesh type
313
  if (check_for_node(node, "type")) {
45✔
314
    auto temp = get_node_value(node, "type", true, true);
45✔
315
    if (temp != mesh_type) {
45✔
UNCOV
316
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
317
    }
318
  }
45✔
319

320
  // check if a length unit multiplier was specified
321
  if (check_for_node(node, "length_multiplier")) {
45✔
UNCOV
322
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
323
  }
324

325
  // get the filename of the unstructured mesh to load
326
  if (check_for_node(node, "filename")) {
45✔
327
    filename_ = get_node_value(node, "filename");
45✔
328
    if (!file_exists(filename_)) {
45✔
UNCOV
329
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
330
    }
331
  } else {
332
    fatal_error(fmt::format(
×
UNCOV
333
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
334
  }
335

336
  if (check_for_node(node, "options")) {
45✔
337
    options_ = get_node_value(node, "options");
16✔
338
  }
339

340
  // check if mesh tally data should be written with
341
  // statepoint files
342
  if (check_for_node(node, "output")) {
45✔
UNCOV
343
    output_ = get_node_value_bool(node, "output");
×
344
  }
345
}
45✔
346

347
void UnstructuredMesh::determine_bounds()
23✔
348
{
349
  double xmin = INFTY;
23✔
350
  double ymin = INFTY;
23✔
351
  double zmin = INFTY;
23✔
352
  double xmax = -INFTY;
23✔
353
  double ymax = -INFTY;
23✔
354
  double zmax = -INFTY;
23✔
355
  int n = this->n_vertices();
23✔
356
  for (int i = 0; i < n; ++i) {
53,604✔
357
    auto v = this->vertex(i);
53,581✔
358
    xmin = std::min(v.x, xmin);
53,581✔
359
    ymin = std::min(v.y, ymin);
53,581✔
360
    zmin = std::min(v.z, zmin);
53,581✔
361
    xmax = std::max(v.x, xmax);
53,581✔
362
    ymax = std::max(v.y, ymax);
53,581✔
363
    zmax = std::max(v.z, zmax);
53,581✔
364
  }
365
  lower_left_ = {xmin, ymin, zmin};
23✔
366
  upper_right_ = {xmax, ymax, zmax};
23✔
367
}
23✔
368

369
Position UnstructuredMesh::sample_tet(
601,230✔
370
  std::array<Position, 4> coords, uint64_t* seed) const
371
{
372
  // Uniform distribution
373
  double s = prn(seed);
601,230✔
374
  double t = prn(seed);
601,230✔
375
  double u = prn(seed);
601,230✔
376

377
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
378
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
379
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
380
  if (s + t > 1) {
601,230✔
381
    s = 1.0 - s;
301,419✔
382
    t = 1.0 - t;
301,419✔
383
  }
384
  if (s + t + u > 1) {
601,230✔
385
    if (t + u > 1) {
400,902✔
386
      double old_t = t;
200,169✔
387
      t = 1.0 - u;
200,169✔
388
      u = 1.0 - s - old_t;
200,169✔
389
    } else if (t + u <= 1) {
200,733✔
390
      double old_s = s;
200,733✔
391
      s = 1.0 - t - u;
200,733✔
392
      u = old_s + t + u - 1;
200,733✔
393
    }
394
  }
395
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
396
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
397
}
398

399
const std::string UnstructuredMesh::mesh_type = "unstructured";
400

401
std::string UnstructuredMesh::get_mesh_type() const
30✔
402
{
403
  return mesh_type;
30✔
404
}
405

UNCOV
406
void UnstructuredMesh::surface_bins_crossed(
×
407
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
408
{
UNCOV
409
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
410
}
411

412
std::string UnstructuredMesh::bin_label(int bin) const
193,712✔
413
{
414
  return fmt::format("Mesh Index ({})", bin);
193,712✔
415
};
416

417
void UnstructuredMesh::to_hdf5_inner(hid_t mesh_group) const
30✔
418
{
419
  write_dataset(mesh_group, "filename", filename_);
30✔
420
  write_dataset(mesh_group, "library", this->library());
30✔
421
  if (!options_.empty()) {
30✔
422
    write_attribute(mesh_group, "options", options_);
8✔
423
  }
424

425
  if (length_multiplier_ > 0.0)
30✔
UNCOV
426
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
427

428
  // write vertex coordinates
429
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
30✔
430
  for (int i = 0; i < this->n_vertices(); i++) {
67,928✔
431
    auto v = this->vertex(i);
67,898✔
432
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
67,898✔
433
  }
434
  write_dataset(mesh_group, "vertices", vertices);
30✔
435

436
  int num_elem_skipped = 0;
30✔
437

438
  // write element types and connectivity
439
  vector<double> volumes;
30✔
440
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
30✔
441
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
30✔
442
  for (int i = 0; i < this->n_bins(); i++) {
337,742✔
443
    auto conn = this->connectivity(i);
337,712✔
444

445
    volumes.emplace_back(this->volume(i));
337,712✔
446

447
    // write linear tet element
448
    if (conn.size() == 4) {
337,712✔
449
      xt::view(elem_types, i, xt::all()) =
671,424✔
450
        static_cast<int>(ElementType::LINEAR_TET);
671,424✔
451
      xt::view(connectivity, i, xt::all()) =
671,424✔
452
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
1,007,136✔
453
      // write linear hex element
454
    } else if (conn.size() == 8) {
2,000✔
455
      xt::view(elem_types, i, xt::all()) =
4,000✔
456
        static_cast<int>(ElementType::LINEAR_HEX);
4,000✔
457
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000✔
458
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000✔
459
    } else {
460
      num_elem_skipped++;
×
UNCOV
461
      xt::view(elem_types, i, xt::all()) =
×
462
        static_cast<int>(ElementType::UNSUPPORTED);
UNCOV
463
      xt::view(connectivity, i, xt::all()) = -1;
×
464
    }
465
  }
337,712✔
466

467
  // warn users that some elements were skipped
468
  if (num_elem_skipped > 0) {
30✔
UNCOV
469
    warning(fmt::format("The connectivity of {} elements "
×
470
                        "on mesh {} were not written "
471
                        "because they are not of type linear tet/hex.",
UNCOV
472
      num_elem_skipped, this->id_));
×
473
  }
474

475
  write_dataset(mesh_group, "volumes", volumes);
30✔
476
  write_dataset(mesh_group, "connectivity", connectivity);
30✔
477
  write_dataset(mesh_group, "element_types", elem_types);
30✔
478
}
30✔
479

480
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
23✔
481
{
482
  length_multiplier_ = length_multiplier;
23✔
483
}
23✔
484

485
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
486
{
487
  auto conn = connectivity(bin);
120,000✔
488

489
  if (conn.size() == 4)
120,000✔
490
    return ElementType::LINEAR_TET;
120,000✔
491
  else if (conn.size() == 8)
×
UNCOV
492
    return ElementType::LINEAR_HEX;
×
493
  else
UNCOV
494
    return ElementType::UNSUPPORTED;
×
495
}
120,000✔
496

497
StructuredMesh::MeshIndex StructuredMesh::get_indices(
965,044,551✔
498
  Position r, bool& in_mesh) const
499
{
500
  MeshIndex ijk;
501
  in_mesh = true;
965,044,551✔
502
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
503
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
504

505
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
506
      in_mesh = false;
91,285,024✔
507
  }
508
  return ijk;
965,044,551✔
509
}
510

511
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,014,582,304✔
512
{
513
  switch (n_dimension_) {
1,014,582,304✔
514
  case 1:
956,736✔
515
    return ijk[0] - 1;
956,736✔
516
  case 2:
21,602,040✔
517
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,602,040✔
518
  case 3:
992,023,528✔
519
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
992,023,528✔
520
  default:
×
UNCOV
521
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
522
  }
523
}
524

525
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
60,389,604✔
526
{
527
  MeshIndex ijk;
528
  if (n_dimension_ == 1) {
60,389,604✔
529
    ijk[0] = bin + 1;
300✔
530
  } else if (n_dimension_ == 2) {
60,389,304✔
531
    ijk[0] = bin % shape_[0] + 1;
15,876✔
532
    ijk[1] = bin / shape_[0] + 1;
15,876✔
533
  } else if (n_dimension_ == 3) {
60,373,428✔
534
    ijk[0] = bin % shape_[0] + 1;
60,373,428✔
535
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
60,373,428✔
536
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
60,373,428✔
537
  }
538
  return ijk;
60,389,604✔
539
}
540

541
int StructuredMesh::get_bin(Position r) const
324,565,408✔
542
{
543
  // Determine indices
544
  bool in_mesh;
545
  MeshIndex ijk = get_indices(r, in_mesh);
324,565,408✔
546
  if (!in_mesh)
324,565,408✔
547
    return -1;
22,164,978✔
548

549
  // Convert indices to bin
550
  return get_bin_from_indices(ijk);
302,400,430✔
551
}
552

553
int StructuredMesh::n_bins() const
1,000,136✔
554
{
555
  return std::accumulate(
1,000,136✔
556
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,000,272✔
557
}
558

559
int StructuredMesh::n_surface_bins() const
593✔
560
{
561
  return 4 * n_dimension_ * n_bins();
593✔
562
}
563

UNCOV
564
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
565
  const SourceSite* bank, int64_t length, bool* outside) const
566
{
567
  // Determine shape of array for counts
568
  std::size_t m = this->n_bins();
×
UNCOV
569
  vector<std::size_t> shape = {m};
×
570

571
  // Create array of zeros
572
  xt::xarray<double> cnt {shape, 0.0};
×
UNCOV
573
  bool outside_ = false;
×
574

575
  for (int64_t i = 0; i < length; i++) {
×
UNCOV
576
    const auto& site = bank[i];
×
577

578
    // determine scoring bin for entropy mesh
UNCOV
579
    int mesh_bin = get_bin(site.r);
×
580

581
    // if outside mesh, skip particle
582
    if (mesh_bin < 0) {
×
583
      outside_ = true;
×
UNCOV
584
      continue;
×
585
    }
586

587
    // Add to appropriate bin
UNCOV
588
    cnt(mesh_bin) += site.wgt;
×
589
  }
590

591
  // Create copy of count data. Since ownership will be acquired by xtensor,
592
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
593
  // warnings.
594
  int total = cnt.size();
×
UNCOV
595
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
596

597
#ifdef OPENMC_MPI
598
  // collect values from all processors
599
  MPI_Reduce(
600
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
601

602
  // Check if there were sites outside the mesh for any processor
603
  if (outside) {
604
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
605
  }
606
#else
607
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
608
  if (outside)
609
    *outside = outside_;
610
#endif
611

612
  // Adapt reduced values in array back into an xarray
613
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
UNCOV
614
  xt::xarray<double> counts = arr;
×
615

UNCOV
616
  return counts;
×
617
}
618

619
// raytrace through the mesh. The template class T will do the tallying.
620
// A modern optimizing compiler can recognize the noop method of T and eleminate
621
// that call entirely.
622
template<class T>
623
void StructuredMesh::raytrace_mesh(
640,854,875✔
624
  Position r0, Position r1, const Direction& u, T tally) const
625
{
626
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
627
  // enable/disable tally calls for now, T template type needs to provide both
628
  // surface and track methods, which might be empty. modern optimizing
629
  // compilers will (hopefully) eliminate the complete code (including
630
  // calculation of parameters) but for the future: be explicit
631

632
  // Compute the length of the entire track.
633
  double total_distance = (r1 - r0).norm();
640,854,875✔
634
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
640,854,875✔
635
    return;
5,711,856✔
636

637
  const int n = n_dimension_;
635,143,019✔
638

639
  // Flag if position is inside the mesh
640
  bool in_mesh;
641

642
  // Position is r = r0 + u * traveled_distance, start at r0
643
  double traveled_distance {0.0};
635,143,019✔
644

645
  // Calculate index of current cell. Offset the position a tiny bit in
646
  // direction of flight
647
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
635,143,019✔
648

649
  // if track is very short, assume that it is completely inside one cell.
650
  // Only the current cell will score and no surfaces
651
  if (total_distance < 2 * TINY_BIT) {
635,143,019✔
652
    if (in_mesh) {
12✔
UNCOV
653
      tally.track(ijk, 1.0);
×
654
    }
655
    return;
12✔
656
  }
657

658
  // translate start and end positions,
659
  // this needs to come after the get_indices call because it does its own
660
  // translation
661
  local_coords(r0);
635,143,007✔
662
  local_coords(r1);
635,143,007✔
663

664
  // Calculate initial distances to next surfaces in all three dimensions
665
  std::array<MeshDistance, 3> distances;
1,270,286,014✔
666
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
667
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
1,887,396,981✔
668
  }
669

670
  // Loop until r = r1 is eventually reached
671
  while (true) {
356,604,713✔
672

673
    if (in_mesh) {
991,747,720✔
674

675
      // find surface with minimal distance to current position
676
      const auto k = std::min_element(distances.begin(), distances.end()) -
900,540,663✔
677
                     distances.begin();
900,540,663✔
678

679
      // Tally track length delta since last step
680
      tally.track(ijk,
900,540,663✔
681
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
900,540,663✔
682
          total_distance);
683

684
      // update position and leave, if we have reached end position
685
      traveled_distance = distances[k].distance;
900,540,663✔
686
      if (traveled_distance >= total_distance)
900,540,663✔
687
        return;
549,272,074✔
688

689
      // If we have not reached r1, we have hit a surface. Tally outward current
690
      tally.surface(ijk, k, distances[k].max_surface, false);
351,268,589✔
691

692
      // Update cell and calculate distance to next surface in k-direction.
693
      // The two other directions are still valid!
694
      ijk[k] = distances[k].next_index;
351,268,589✔
695
      distances[k] =
351,268,589✔
696
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
351,268,589✔
697

698
      // Check if we have left the interior of the mesh
699
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
351,268,589✔
700

701
      // If we are still inside the mesh, tally inward current for the next cell
702
      if (in_mesh)
351,268,589✔
703
        tally.surface(ijk, k, !distances[k].max_surface, true);
327,030,414✔
704

705
    } else { // not inside mesh
706

707
      // For all directions outside the mesh, find the distance that we need to
708
      // travel to reach the next surface. Use the largest distance, as only
709
      // this will cross all outer surfaces.
710
      int k_max {0};
91,207,057✔
711
      for (int k = 0; k < n; ++k) {
362,486,755✔
712
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
363,829,329✔
713
            (distances[k].distance > traveled_distance)) {
92,549,631✔
714
          traveled_distance = distances[k].distance;
91,763,871✔
715
          k_max = k;
91,763,871✔
716
        }
717
      }
718

719
      // If r1 is not inside the mesh, exit here
720
      if (traveled_distance >= total_distance)
91,207,057✔
721
        return;
85,870,933✔
722

723
      // Calculate the new cell index and update all distances to next surfaces.
724
      ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
5,336,124✔
725
      for (int k = 0; k < n; ++k) {
21,115,236✔
726
        distances[k] =
15,779,112✔
727
          distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
15,779,112✔
728
      }
729

730
      // If inside the mesh, Tally inward current
731
      if (in_mesh)
5,336,124✔
732
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
3,959,064✔
733
    }
734
  }
735
}
736

250,709,558✔
737
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
738
  vector<int>& bins, vector<double>& lengths) const
739
{
740

741
  // Helper tally class.
742
  // stores a pointer to the mesh class and references to bins and lengths
743
  // parameters. Performs the actual tally through the track method.
744
  struct TrackAggregator {
745
    TrackAggregator(
746
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
250,709,558✔
747
      : mesh(_mesh), bins(_bins), lengths(_lengths)
250,709,558✔
UNCOV
748
    {}
×
749
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
750
    void track(const MeshIndex& ijk, double l) const
250,709,558✔
751
    {
752
      bins.push_back(mesh->get_bin_from_indices(ijk));
753
      lengths.push_back(l);
754
    }
755

756
    const StructuredMesh* mesh;
250,709,558✔
757
    vector<int>& bins;
758
    vector<double>& lengths;
759
  };
760

250,709,558✔
761
  // Perform the mesh raytrace with the helper class.
762
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
763
}
764

250,709,558✔
765
void StructuredMesh::surface_bins_crossed(
×
UNCOV
766
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
767
{
UNCOV
768

×
769
  // Helper tally class.
770
  // stores a pointer to the mesh class and a reference to the bins parameter.
771
  // Performs the actual tally through the surface method.
772
  struct SurfaceAggregator {
773
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
774
      : mesh(_mesh), bins(_bins)
250,709,558✔
775
    {}
250,709,558✔
776
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
777
    {
778
      int i_bin =
501,419,116✔
779
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,001,002,088✔
780
      if (max)
750,292,530✔
781
        i_bin += 2;
782
      if (inward)
783
        i_bin += 1;
784
      bins.push_back(i_bin);
61,870,061✔
785
    }
786
    void track(const MeshIndex& idx, double l) const {}
312,579,619✔
787

788
    const StructuredMesh* mesh;
789
    vector<int>& bins;
309,532,184✔
790
  };
309,532,184✔
791

792
  // Perform the mesh raytrace with the helper class.
793
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
309,532,184✔
794
}
309,532,184✔
795

796
//==============================================================================
797
// RegularMesh implementation
798
//==============================================================================
309,532,184✔
799

309,532,184✔
800
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
247,905,711✔
801
{
802
  // Determine number of dimensions for mesh
803
  if (!check_for_node(node, "dimension")) {
61,626,473✔
804
    fatal_error("Must specify <dimension> on a regular mesh.");
805
  }
806

807
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
61,626,473✔
808
  int n = n_dimension_ = shape.size();
61,626,473✔
809
  if (n != 1 && n != 2 && n != 3) {
61,626,473✔
810
    fatal_error("Mesh must be one, two, or three dimensions.");
811
  }
812
  std::copy(shape.begin(), shape.end(), shape_.begin());
61,626,473✔
813

814
  // Check that dimensions are all greater than zero
815
  if (xt::any(shape <= 0)) {
61,626,473✔
816
    fatal_error("All entries on the <dimension> element for a tally "
59,328,330✔
817
                "mesh must be positive.");
818
  }
819

820
  // Check for lower-left coordinates
821
  if (check_for_node(node, "lower_left")) {
822
    // Read mesh lower-left corner location
823
    lower_left_ = get_node_xarray<double>(node, "lower_left");
3,047,435✔
824
  } else {
11,838,440✔
825
    fatal_error("Must specify <lower_left> on a mesh.");
11,994,992✔
826
  }
3,203,987✔
827

3,139,523✔
828
  // Make sure lower_left and dimension match
3,139,523✔
829
  if (shape.size() != lower_left_.size()) {
830
    fatal_error("Number of entries on <lower_left> must be the same "
831
                "as the number of entries on <dimension>.");
832
  }
833

3,047,435✔
834
  if (check_for_node(node, "width")) {
2,803,847✔
835
    // Make sure one of upper-right or width were specified
836
    if (check_for_node(node, "upper_right")) {
837
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
243,588✔
838
    }
860,256✔
839

616,668✔
840
    width_ = get_node_xarray<double>(node, "width");
616,668✔
841

842
    // Check to ensure width has same dimensions
843
    auto n = width_.size();
844
    if (n != lower_left_.size()) {
243,588✔
845
      fatal_error("Number of entries on <width> must be the same as "
218,592✔
846
                  "the number of entries on <lower_left>.");
847
    }
848

849
    // Check for negative widths
390,145,317✔
850
    if (xt::any(width_ < 0.0)) {
851
      fatal_error("Cannot have a negative <width> on a tally mesh.");
852
    }
853

854
    // Set width and upper right coordinate
855
    upper_right_ = xt::eval(lower_left_ + shape * width_);
856

857
  } else if (check_for_node(node, "upper_right")) {
858
    upper_right_ = get_node_xarray<double>(node, "upper_right");
859

390,145,317✔
860
    // Check to ensure width has same dimensions
390,145,317✔
861
    auto n = upper_right_.size();
5,711,856✔
862
    if (n != lower_left_.size()) {
863
      fatal_error("Number of entries on <upper_right> must be the "
384,433,461✔
864
                  "same as the number of entries on <lower_left>.");
865
    }
866

867
    // Check that upper-right is above lower-left
868
    if (xt::any(upper_right_ < lower_left_)) {
869
      fatal_error("The <upper_right> coordinates must be greater than "
384,433,461✔
870
                  "the <lower_left> coordinates on a tally mesh.");
871
    }
872

873
    // Set width
384,433,461✔
874
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
875
  } else {
876
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
877
  }
384,433,461✔
878

12✔
UNCOV
879
  // Set material volumes
×
880
  volume_frac_ = 1.0 / xt::prod(shape)();
881

12✔
882
  element_volume_ = 1.0;
883
  for (int i = 0; i < n_dimension_; i++) {
884
    element_volume_ *= width_[i];
885
  }
886
}
887

384,433,449✔
888
int RegularMesh::get_index_in_direction(double r, int i) const
384,433,449✔
889
{
890
  return std::ceil((r - lower_left_[i]) / width_[i]);
891
}
768,866,898✔
892

1,521,537,900✔
893
const std::string RegularMesh::mesh_type = "regular";
1,137,104,451✔
894

895
std::string RegularMesh::get_mesh_type() const
896
{
897
  return mesh_type;
294,734,652✔
898
}
899

679,168,101✔
900
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
901
{
902
  return lower_left_[i] + ijk[i] * width_[i];
591,008,479✔
903
}
591,008,479✔
904

905
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
906
{
591,008,479✔
907
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
591,008,479✔
908
}
909

910
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
911
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
591,008,479✔
912
  double l) const
591,008,479✔
913
{
301,366,363✔
914
  MeshDistance d;
915
  d.next_index = ijk[i];
916
  if (std::abs(u[i]) < FP_PRECISION)
289,642,116✔
917
    return d;
918

919
  d.max_surface = (u[i] > 0);
920
  if (d.max_surface && (ijk[i] <= shape_[i])) {
289,642,116✔
921
    d.next_index++;
289,642,116✔
922
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
289,642,116✔
923
  } else if (!d.max_surface && (ijk[i] >= 1)) {
924
    d.next_index--;
925
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
289,642,116✔
926
  }
927
  return d;
928
}
289,642,116✔
929

267,702,084✔
930
std::pair<vector<double>, vector<double>> RegularMesh::plot(
931
  Position plot_ll, Position plot_ur) const
932
{
933
  // Figure out which axes lie in the plane of the plot.
934
  array<int, 2> axes {-1, -1};
935
  if (plot_ur.z == plot_ll.z) {
936
    axes[0] = 0;
88,159,622✔
937
    if (n_dimension_ > 1)
350,648,315✔
938
      axes[1] = 1;
351,834,337✔
939
  } else if (plot_ur.y == plot_ll.y) {
89,345,644✔
940
    axes[0] = 0;
88,624,348✔
941
    if (n_dimension_ > 2)
88,624,348✔
942
      axes[1] = 2;
943
  } else if (plot_ur.x == plot_ll.x) {
944
    if (n_dimension_ > 1)
945
      axes[0] = 1;
946
    if (n_dimension_ > 2)
88,159,622✔
947
      axes[1] = 2;
83,067,086✔
948
  } else {
949
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
950
  }
5,092,536✔
951

20,254,980✔
952
  // Get the coordinates of the mesh lines along both of the axes.
15,162,444✔
953
  array<vector<double>, 2> axis_lines;
15,162,444✔
954
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
955
    int axis = axes[i_ax];
956
    if (axis == -1)
957
      continue;
5,092,536✔
958
    auto& lines {axis_lines[i_ax]};
3,740,472✔
959

960
    double coord = lower_left_[axis];
961
    for (int i = 0; i < shape_[axis] + 1; ++i) {
962
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
963
        lines.push_back(coord);
390,145,317✔
964
      coord += width_[axis];
965
    }
966
  }
967

968
  return {axis_lines[0], axis_lines[1]};
969
}
970

971
void RegularMesh::to_hdf5_inner(hid_t mesh_group) const
390,145,317✔
972
{
973
  write_dataset(mesh_group, "type", "regular");
390,145,317✔
974
  write_dataset(mesh_group, "dimension", get_x_shape());
390,145,317✔
975
  write_dataset(mesh_group, "lower_left", lower_left_);
561,084,672✔
976
  write_dataset(mesh_group, "upper_right", upper_right_);
591,008,479✔
977
  write_dataset(mesh_group, "width", width_);
978
}
591,008,479✔
979

591,008,479✔
980
xt::xtensor<double, 1> RegularMesh::count_sites(
591,008,479✔
981
  const SourceSite* bank, int64_t length, bool* outside) const
982
{
983
  // Determine shape of array for counts
984
  std::size_t m = this->n_bins();
985
  vector<std::size_t> shape = {m};
986

987
  // Create array of zeros
988
  xt::xarray<double> cnt {shape, 0.0};
390,145,317✔
989
  bool outside_ = false;
390,145,317✔
990

991
  for (int64_t i = 0; i < length; i++) {
250,709,558✔
992
    const auto& site = bank[i];
993

994
    // determine scoring bin for entropy mesh
995
    int mesh_bin = get_bin(site.r);
996

997
    // if outside mesh, skip particle
998
    if (mesh_bin < 0) {
999
      outside_ = true;
250,709,558✔
1000
      continue;
250,709,558✔
1001
    }
250,709,558✔
1002

121,173,395✔
1003
    // Add to appropriate bin
1004
    cnt(mesh_bin) += site.wgt;
1005
  }
121,173,395✔
1006

121,173,395✔
1007
  // Create copy of count data. Since ownership will be acquired by xtensor,
60,541,998✔
1008
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
121,173,395✔
1009
  // warnings.
59,546,922✔
1010
  int total = cnt.size();
121,173,395✔
1011
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
121,173,395✔
1012

309,532,184✔
1013
#ifdef OPENMC_MPI
1014
  // collect values from all processors
1015
  MPI_Reduce(
1016
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
1017

1018
  // Check if there were sites outside the mesh for any processor
1019
  if (outside) {
250,709,558✔
1020
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
250,709,558✔
1021
  }
1022
#else
1023
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1024
  if (outside)
1025
    *outside = outside_;
1026
#endif
1,888✔
1027

1028
  // Adapt reduced values in array back into an xarray
1029
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1,888✔
UNCOV
1030
  xt::xarray<double> counts = arr;
×
1031

1032
  return counts;
1033
}
1,888✔
1034

1,888✔
1035
double RegularMesh::volume(const MeshIndex& ijk) const
1,888✔
UNCOV
1036
{
×
1037
  return element_volume_;
1038
}
1,888✔
1039

1040
//==============================================================================
1041
// RectilinearMesh implementation
1,888✔
UNCOV
1042
//==============================================================================
×
1043

1044
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1045
{
1046
  n_dimension_ = 3;
1047

1,888✔
1048
  grid_[0] = get_node_array<double>(node, "x_grid");
1049
  grid_[1] = get_node_array<double>(node, "y_grid");
1,888✔
1050
  grid_[2] = get_node_array<double>(node, "z_grid");
UNCOV
1051

×
1052
  if (int err = set_grid()) {
1053
    fatal_error(openmc_err_msg);
1054
  }
1055
}
1,888✔
UNCOV
1056

×
1057
const std::string RectilinearMesh::mesh_type = "rectilinear";
1058

1059
std::string RectilinearMesh::get_mesh_type() const
1060
{
1,888✔
1061
  return mesh_type;
1062
}
51✔
UNCOV
1063

×
1064
double RectilinearMesh::positive_grid_boundary(
1065
  const MeshIndex& ijk, int i) const
1066
{
51✔
1067
  return grid_[i][ijk[i]];
1068
}
1069

51✔
1070
double RectilinearMesh::negative_grid_boundary(
51✔
UNCOV
1071
  const MeshIndex& ijk, int i) const
×
1072
{
1073
  return grid_[i][ijk[i] - 1];
1074
}
1075

1076
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
51✔
UNCOV
1077
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
×
1078
  double l) const
1079
{
1080
  MeshDistance d;
1081
  d.next_index = ijk[i];
51✔
1082
  if (std::abs(u[i]) < FP_PRECISION)
1083
    return d;
1,837✔
1084

1,837✔
1085
  d.max_surface = (u[i] > 0);
1086
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1087
    d.next_index++;
1,837✔
1088
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1,837✔
UNCOV
1089
  } else if (!d.max_surface && (ijk[i] > 0)) {
×
1090
    d.next_index--;
1091
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
1092
  }
1093
  return d;
1094
}
1,837✔
UNCOV
1095

×
1096
int RectilinearMesh::set_grid()
1097
{
1098
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1099
    static_cast<int>(grid_[1].size()) - 1,
1100
    static_cast<int>(grid_[2].size()) - 1};
1,837✔
1101

UNCOV
1102
  for (const auto& g : grid_) {
×
1103
    if (g.size() < 2) {
1104
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1105
                 "must each have at least 2 points");
1106
      return OPENMC_E_INVALID_ARGUMENT;
1,888✔
1107
    }
1108
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,888✔
1109
        g.end()) {
7,285✔
1110
      set_errmsg("Values in for x-, y-, and z- grids for "
5,397✔
1111
                 "rectilinear meshes must be sorted and unique.");
1112
      return OPENMC_E_INVALID_ARGUMENT;
1,888✔
1113
    }
1114
  }
2,147,483,647✔
1115

1116
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
2,147,483,647✔
1117
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
1118

1119
  return 0;
1120
}
1121

3,875✔
1122
int RectilinearMesh::get_index_in_direction(double r, int i) const
1123
{
3,875✔
1124
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1125
}
1126

960,343,631✔
1127
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
1128
  Position plot_ll, Position plot_ur) const
960,343,631✔
1129
{
1130
  // Figure out which axes lie in the plane of the plot.
1131
  array<int, 2> axes {-1, -1};
959,919,949✔
1132
  if (plot_ur.z == plot_ll.z) {
1133
    axes = {0, 1};
959,919,949✔
1134
  } else if (plot_ur.y == plot_ll.y) {
1135
    axes = {0, 2};
1136
  } else if (plot_ur.x == plot_ll.x) {
1,635,977,618✔
1137
    axes = {1, 2};
1138
  } else {
1139
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
1140
  }
1,635,977,618✔
1141

1,635,977,618✔
1142
  // Get the coordinates of the mesh lines along both of the axes.
1,635,977,618✔
UNCOV
1143
  array<vector<double>, 2> axis_lines;
×
1144
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
1145
    int axis = axes[i_ax];
1,635,977,618✔
1146
    vector<double>& lines {axis_lines[i_ax]};
1,635,977,618✔
1147

806,646,995✔
1148
    for (auto coord : grid_[axis]) {
806,646,995✔
1149
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
829,330,623✔
1150
        lines.push_back(coord);
806,223,313✔
1151
    }
806,223,313✔
1152
  }
1153

1,635,977,618✔
1154
  return {axis_lines[0], axis_lines[1]};
1155
}
1156

24✔
1157
void RectilinearMesh::to_hdf5_inner(hid_t mesh_group) const
1158
{
1159
  write_dataset(mesh_group, "type", "rectilinear");
1160
  write_dataset(mesh_group, "x_grid", grid_[0]);
24✔
1161
  write_dataset(mesh_group, "y_grid", grid_[1]);
24✔
1162
  write_dataset(mesh_group, "z_grid", grid_[2]);
24✔
1163
}
24✔
1164

24✔
1165
double RectilinearMesh::volume(const MeshIndex& ijk) const
×
1166
{
×
1167
  double vol {1.0};
×
1168

×
1169
  for (int i = 0; i < n_dimension_; i++) {
×
1170
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
×
1171
  }
×
1172
  return vol;
×
UNCOV
1173
}
×
1174

UNCOV
1175
//==============================================================================
×
1176
// CylindricalMesh implementation
1177
//==============================================================================
1178

1179
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
24✔
1180
  : PeriodicStructuredMesh {node}
72✔
1181
{
48✔
1182
  n_dimension_ = 3;
48✔
UNCOV
1183
  grid_[0] = get_node_array<double>(node, "r_grid");
×
1184
  grid_[1] = get_node_array<double>(node, "phi_grid");
48✔
1185
  grid_[2] = get_node_array<double>(node, "z_grid");
1186
  origin_ = get_node_position(node, "origin");
48✔
1187

312✔
1188
  if (int err = set_grid()) {
264✔
1189
    fatal_error(openmc_err_msg);
264✔
1190
  }
264✔
1191
}
1192

1193
const std::string CylindricalMesh::mesh_type = "cylindrical";
1194

48✔
1195
std::string CylindricalMesh::get_mesh_type() const
24✔
1196
{
1197
  return mesh_type;
2,176✔
1198
}
1199

2,176✔
1200
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
2,176✔
1201
  Position r, bool& in_mesh) const
2,176✔
1202
{
2,176✔
1203
  local_coords(r);
2,176✔
1204

2,176✔
1205
  Position mapped_r;
1206
  mapped_r[0] = std::hypot(r.x, r.y);
12,248✔
1207
  mapped_r[2] = r[2];
1208

1209
  if (mapped_r[0] < FP_PRECISION) {
1210
    mapped_r[1] = 0.0;
12,248✔
1211
  } else {
12,248✔
1212
    mapped_r[1] = std::atan2(r.y, r.x);
1213
    if (mapped_r[1] < 0)
1214
      mapped_r[1] += 2 * M_PI;
12,248✔
1215
  }
12,248✔
1216

1217
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,019,257✔
1218

12,007,009✔
1219
  idx[1] = sanitize_phi(idx[1]);
1220

1221
  return idx;
12,007,009✔
1222
}
1223

1224
Position CylindricalMesh::sample_element(
12,007,009✔
1225
  const MeshIndex& ijk, uint64_t* seed) const
×
UNCOV
1226
{
×
1227
  double r_min = this->r(ijk[0] - 1);
1228
  double r_max = this->r(ijk[0]);
1229

1230
  double phi_min = this->phi(ijk[1] - 1);
12,007,009✔
1231
  double phi_max = this->phi(ijk[1]);
1232

1233
  double z_min = this->z(ijk[2] - 1);
1234
  double z_max = this->z(ijk[2]);
1235

1236
  double r_min_sq = r_min * r_min;
12,248✔
1237
  double r_max_sq = r_max * r_max;
12,248✔
1238
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
1239
  double phi = uniform_distribution(phi_min, phi_max, seed);
1240
  double z = uniform_distribution(z_min, z_max, seed);
1241

7,320✔
1242
  double x = r * std::cos(phi);
7,320✔
1243
  double y = r * std::sin(phi);
1244

1245
  return origin_ + Position(x, y, z);
7,320✔
1246
}
7,320✔
1247

1248
double CylindricalMesh::find_r_crossing(
1249
  const Position& r, const Direction& u, double l, int shell) const
4,928✔
1250
{
4,928✔
1251

4,928✔
1252
  if ((shell < 0) || (shell > shape_[0]))
1253
    return INFTY;
1254

1255
  // solve r.x^2 + r.y^2 == r0^2
12,248✔
1256
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
12,248✔
1257
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
1258

24,496✔
1259
  const double r0 = grid_[0][shell];
12,248✔
1260
  if (r0 == 0.0)
1261
    return INFTY;
982,884✔
1262

1263
  const double denominator = u.x * u.x + u.y * u.y;
982,884✔
1264

1265
  // Direction of flight is in z-direction. Will never intersect r.
1266
  if (std::abs(denominator) < FP_PRECISION)
1267
    return INFTY;
1268

1269
  // inverse of dominator to help the compiler to speed things up
1270
  const double inv_denominator = 1.0 / denominator;
111✔
1271

1272
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
111✔
1273
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1274
  double D = p * p - c * inv_denominator;
111✔
1275

111✔
1276
  if (D < 0.0)
111✔
1277
    return INFTY;
1278

111✔
UNCOV
1279
  D = std::sqrt(D);
×
1280

1281
  // the solution -p - D is always smaller as -p + D : Check this one first
111✔
1282
  if (std::abs(c) <= RADIAL_MESH_TOL)
1283
    return INFTY;
1284

1285
  if (-p - D > l)
328✔
1286
    return -p - D;
1287
  if (-p + D > l)
328✔
1288
    return -p + D;
1289

1290
  return INFTY;
56,980,318✔
1291
}
1292

1293
double CylindricalMesh::find_phi_crossing(
56,980,318✔
1294
  const Position& r, const Direction& u, double l, int shell) const
1295
{
1296
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
56,518,367✔
1297
  if (full_phi_ && (shape_[1] == 1))
1298
    return INFTY;
1299

56,518,367✔
1300
  shell = sanitize_phi(shell);
1301

1302
  const double p0 = grid_[1][shell];
111,639,732✔
1303

1304
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1305
  // => x(s) * cos(p0) = y(s) * sin(p0)
1306
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
111,639,732✔
1307
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
111,639,732✔
1308

111,639,732✔
UNCOV
1309
  const double c0 = std::cos(p0);
×
1310
  const double s0 = std::sin(p0);
1311

111,639,732✔
1312
  const double denominator = (u.x * s0 - u.y * c0);
111,639,732✔
1313

55,540,318✔
1314
  // Check if direction of flight is not parallel to phi surface
55,540,318✔
1315
  if (std::abs(denominator) > FP_PRECISION) {
56,099,414✔
1316
    const double s = -(r.x * s0 - r.y * c0) / denominator;
55,078,367✔
1317
    // Check if solution is in positive direction of flight and crosses the
55,078,367✔
1318
    // correct phi surface (not -phi)
1319
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
111,639,732✔
1320
      return s;
1321
  }
1322

185✔
1323
  return INFTY;
1324
}
185✔
1325

185✔
1326
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
185✔
1327
  const Position& r, const Direction& u, double l, int shell) const
1328
{
740✔
1329
  MeshDistance d;
555✔
UNCOV
1330
  d.next_index = shell;
×
1331

UNCOV
1332
  // Direction of flight is within xy-plane. Will never intersect z.
×
1333
  if (std::abs(u.z) < FP_PRECISION)
1334
    return d;
555✔
1335

1,110✔
UNCOV
1336
  d.max_surface = (u.z > 0.0);
×
1337
  if (d.max_surface && (shell <= shape_[2])) {
UNCOV
1338
    d.next_index += 1;
×
1339
    d.distance = (grid_[2][shell] - r.z) / u.z;
1340
  } else if (!d.max_surface && (shell > 0)) {
1341
    d.next_index -= 1;
1342
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
185✔
1343
  }
185✔
1344
  return d;
1345
}
185✔
1346

1347
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1348
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
160,387,890✔
1349
  double l) const
1350
{
160,387,890✔
1351
  Position r = r0 - origin_;
1352

1353
  if (i == 0) {
12✔
1354

1355
    return std::min(
1356
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
1357
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
12✔
1358

12✔
UNCOV
1359
  } else if (i == 1) {
×
1360

12✔
1361
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
12✔
1362
                      find_phi_crossing(r, u, l, ijk[i])),
×
UNCOV
1363
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
×
1364
        find_phi_crossing(r, u, l, ijk[i] - 1)));
UNCOV
1365

×
1366
  } else {
1367
    return find_z_crossing(r, u, l, ijk[i]);
1368
  }
1369
}
12✔
1370

36✔
1371
int CylindricalMesh::set_grid()
24✔
1372
{
24✔
1373
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1374
    static_cast<int>(grid_[1].size()) - 1,
120✔
1375
    static_cast<int>(grid_[2].size()) - 1};
96✔
1376

96✔
1377
  for (const auto& g : grid_) {
1378
    if (g.size() < 2) {
1379
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
1380
                 "must each have at least 2 points");
24✔
1381
      return OPENMC_E_INVALID_ARGUMENT;
12✔
1382
    }
1383
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
122✔
1384
        g.end()) {
1385
      set_errmsg("Values in for r-, phi-, and z- grids for "
122✔
1386
                 "cylindrical meshes must be sorted and unique.");
122✔
1387
      return OPENMC_E_INVALID_ARGUMENT;
122✔
1388
    }
122✔
1389
  }
122✔
1390
  if (grid_[0].front() < 0.0) {
1391
    set_errmsg("r-grid for "
228✔
1392
               "cylindrical meshes must start at r >= 0.");
1393
    return OPENMC_E_INVALID_ARGUMENT;
228✔
1394
  }
1395
  if (grid_[1].front() < 0.0) {
912✔
1396
    set_errmsg("phi-grid for "
684✔
1397
               "cylindrical meshes must start at phi >= 0.");
1398
    return OPENMC_E_INVALID_ARGUMENT;
228✔
1399
  }
1400
  if (grid_[1].back() > 2.0 * PI) {
1401
    set_errmsg("phi-grids for "
1402
               "cylindrical meshes must end with theta <= 2*pi.");
1403

1404
    return OPENMC_E_INVALID_ARGUMENT;
1405
  }
401✔
1406

401✔
1407
  full_phi_ = (grid_[1].front() == 0.0) && (grid_[1].back() == 2.0 * PI);
1408

401✔
1409
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
401✔
1410
    origin_[2] + grid_[2].front()};
401✔
1411
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
401✔
1412
    origin_[2] + grid_[2].back()};
401✔
1413

1414
  return 0;
401✔
UNCOV
1415
}
×
1416

1417
int CylindricalMesh::get_index_in_direction(double r, int i) const
401✔
1418
{
1419
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1420
}
1421

504✔
1422
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1423
  Position plot_ll, Position plot_ur) const
504✔
1424
{
1425
  fatal_error("Plot of cylindrical Mesh not implemented");
1426

49,788,192✔
1427
  // Figure out which axes lie in the plane of the plot.
1428
  array<vector<double>, 2> axis_lines;
1429
  return {axis_lines[0], axis_lines[1]};
49,788,192✔
1430
}
1431

49,788,192✔
1432
void CylindricalMesh::to_hdf5_inner(hid_t mesh_group) const
49,788,192✔
1433
{
49,788,192✔
1434
  write_dataset(mesh_group, "type", "cylindrical");
1435
  write_dataset(mesh_group, "r_grid", grid_[0]);
49,788,192✔
UNCOV
1436
  write_dataset(mesh_group, "phi_grid", grid_[1]);
×
1437
  write_dataset(mesh_group, "z_grid", grid_[2]);
1438
  write_dataset(mesh_group, "origin", origin_);
49,788,192✔
1439
}
49,788,192✔
1440

24,869,868✔
1441
double CylindricalMesh::volume(const MeshIndex& ijk) const
1442
{
1443
  double r_i = grid_[0][ijk[0] - 1];
49,788,192✔
1444
  double r_o = grid_[0][ijk[0]];
1445

49,788,192✔
1446
  double phi_i = grid_[1][ijk[1] - 1];
1447
  double phi_o = grid_[1][ijk[1]];
49,788,192✔
1448

1449
  double z_i = grid_[2][ijk[2] - 1];
1450
  double z_o = grid_[2][ijk[2]];
816,000✔
1451

1452
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
1453
}
816,000✔
1454

816,000✔
1455
//==============================================================================
1456
// SphericalMesh implementation
816,000✔
1457
//==============================================================================
816,000✔
1458

1459
SphericalMesh::SphericalMesh(pugi::xml_node node)
816,000✔
1460
  : PeriodicStructuredMesh {node}
816,000✔
1461
{
1462
  n_dimension_ = 3;
816,000✔
1463

816,000✔
1464
  grid_[0] = get_node_array<double>(node, "r_grid");
816,000✔
1465
  grid_[1] = get_node_array<double>(node, "theta_grid");
816,000✔
1466
  grid_[2] = get_node_array<double>(node, "phi_grid");
816,000✔
1467
  origin_ = get_node_position(node, "origin");
1468

816,000✔
1469
  if (int err = set_grid()) {
816,000✔
1470
    fatal_error(openmc_err_msg);
1471
  }
816,000✔
1472
}
1473

1474
const std::string SphericalMesh::mesh_type = "spherical";
146,628,600✔
1475

1476
std::string SphericalMesh::get_mesh_type() const
1477
{
1478
  return mesh_type;
146,628,600✔
1479
}
17,850,072✔
1480

1481
StructuredMesh::MeshIndex SphericalMesh::get_indices(
1482
  Position r, bool& in_mesh) const
1483
{
1484
  local_coords(r);
1485

128,778,528✔
1486
  Position mapped_r;
128,778,528✔
1487
  mapped_r[0] = r.norm();
7,044,084✔
1488

1489
  if (mapped_r[0] < FP_PRECISION) {
121,734,444✔
1490
    mapped_r[1] = 0.0;
1491
    mapped_r[2] = 0.0;
1492
  } else {
121,734,444✔
UNCOV
1493
    mapped_r[1] = std::acos(r.z / mapped_r.x);
×
1494
    mapped_r[2] = std::atan2(r.y, r.x);
1495
    if (mapped_r[2] < 0)
1496
      mapped_r[2] += 2 * M_PI;
121,734,444✔
1497
  }
1498

121,734,444✔
1499
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
121,734,444✔
1500

121,734,444✔
1501
  idx[1] = sanitize_theta(idx[1]);
1502
  idx[2] = sanitize_phi(idx[2]);
121,734,444✔
1503

16,859,556✔
1504
  return idx;
1505
}
104,874,888✔
1506

1507
Position SphericalMesh::sample_element(
1508
  const MeshIndex& ijk, uint64_t* seed) const
104,874,888✔
1509
{
7,057,536✔
1510
  double r_min = this->r(ijk[0] - 1);
1511
  double r_max = this->r(ijk[0]);
97,817,352✔
1512

19,920,936✔
1513
  double theta_min = this->theta(ijk[1] - 1);
77,896,416✔
1514
  double theta_max = this->theta(ijk[1]);
47,239,464✔
1515

1516
  double phi_min = this->phi(ijk[2] - 1);
30,656,952✔
1517
  double phi_max = this->phi(ijk[2]);
1518

1519
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
72,236,472✔
1520
  double sin_theta = std::sin(std::acos(cos_theta));
1521
  double phi = uniform_distribution(phi_min, phi_max, seed);
1522
  double r_min_cub = std::pow(r_min, 3);
1523
  double r_max_cub = std::pow(r_max, 3);
72,236,472✔
1524
  // might be faster to do rejection here?
32,466,432✔
1525
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
1526

39,770,040✔
1527
  double x = r * std::cos(phi) * sin_theta;
1528
  double y = r * std::sin(phi) * sin_theta;
39,770,040✔
1529
  double z = r * cos_theta;
1530

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

1534
double SphericalMesh::find_r_crossing(
1535
  const Position& r, const Direction& u, double l, int shell) const
39,770,040✔
1536
{
39,770,040✔
1537
  if ((shell < 0) || (shell > shape_[0]))
1538
    return INFTY;
39,770,040✔
1539

1540
  // solve |r+s*u| = r0
1541
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
39,770,040✔
1542
  const double r0 = grid_[0][shell];
39,770,040✔
1543
  if (r0 == 0.0)
1544
    return INFTY;
1545
  const double p = r.dot(u);
39,770,040✔
1546
  double c = r.dot(r) - r0 * r0;
18,739,656✔
1547
  double D = p * p - c;
1548

1549
  if (std::abs(c) <= RADIAL_MESH_TOL)
21,030,384✔
1550
    return INFTY;
1551

1552
  if (D >= 0.0) {
39,212,208✔
1553
    D = std::sqrt(D);
1554
    // the solution -p - D is always smaller as -p + D : Check this one first
1555
    if (-p - D > l)
39,212,208✔
1556
      return -p - D;
39,212,208✔
1557
    if (-p + D > l)
1558
      return -p + D;
1559
  }
39,212,208✔
1560

480,000✔
1561
  return INFTY;
1562
}
38,732,208✔
1563

38,732,208✔
1564
double SphericalMesh::find_theta_crossing(
17,729,088✔
1565
  const Position& r, const Direction& u, double l, int shell) const
17,729,088✔
1566
{
21,003,120✔
1567
  // Theta grid is [0, π], thus there is no real surface to cross
18,037,548✔
1568
  if (full_theta_ && (shape_[1] == 1))
18,037,548✔
1569
    return INFTY;
1570

38,732,208✔
1571
  shell = sanitize_theta(shell);
1572

1573
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
148,644,744✔
1574
  // yields
1575
  // a*s^2 + 2*b*s + c == 0 with
1576
  // a = cos(theta)^2 - u.z * u.z
1577
  // b = r*u * cos(theta)^2 - u.z * r.z
148,644,744✔
1578
  // c = r*r * cos(theta)^2 - r.z^2
1579

148,644,744✔
1580
  const double cos_t = std::cos(grid_[1][shell]);
1581
  const bool sgn = std::signbit(cos_t);
73,314,300✔
1582
  const double cos_t_2 = cos_t * cos_t;
73,314,300✔
1583

146,628,600✔
1584
  const double a = cos_t_2 - u.z * u.z;
1585
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
75,330,444✔
1586
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
1587

36,118,236✔
1588
  // if factor of s^2 is zero, direction of flight is parallel to theta surface
36,118,236✔
1589
  if (std::abs(a) < FP_PRECISION) {
36,118,236✔
1590
    // if b vanishes, direction of flight is within theta surface and crossing
72,236,472✔
1591
    // is not possible
1592
    if (std::abs(b) < FP_PRECISION)
1593
      return INFTY;
39,212,208✔
1594

1595
    const double s = -0.5 * c / b;
1596
    // Check if solution is in positive direction of flight and has correct sign
1597
    if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
425✔
1598
      return s;
1599

425✔
1600
    // no crossing is possible
425✔
1601
    return INFTY;
425✔
1602
  }
1603

1,700✔
1604
  const double p = b / a;
1,275✔
UNCOV
1605
  double D = p * p - c / a;
×
1606

UNCOV
1607
  if (D < 0.0)
×
1608
    return INFTY;
1609

1,275✔
1610
  D = std::sqrt(D);
2,550✔
UNCOV
1611

×
1612
  // the solution -p-D is always smaller as -p+D : Check this one first
UNCOV
1613
  double s = -p - D;
×
1614
  // Check if solution is in positive direction of flight and has correct sign
1615
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1616
    return s;
425✔
UNCOV
1617

×
1618
  s = -p + D;
UNCOV
1619
  // Check if solution is in positive direction of flight and has correct sign
×
1620
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
1621
    return s;
425✔
UNCOV
1622

×
1623
  return INFTY;
UNCOV
1624
}
×
1625

1626
double SphericalMesh::find_phi_crossing(
425✔
UNCOV
1627
  const Position& r, const Direction& u, double l, int shell) const
×
1628
{
1629
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
UNCOV
1630
  if (full_phi_ && (shape_[2] == 1))
×
1631
    return INFTY;
1632

1633
  shell = sanitize_phi(shell);
425✔
1634

1635
  const double p0 = grid_[2][shell];
850✔
1636

850✔
1637
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
850✔
1638
  // => x(s) * cos(p0) = y(s) * sin(p0)
850✔
1639
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1640
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
425✔
1641

1642
  const double c0 = std::cos(p0);
1643
  const double s0 = std::sin(p0);
149,364,576✔
1644

1645
  const double denominator = (u.x * s0 - u.y * c0);
149,364,576✔
1646

1647
  // Check if direction of flight is not parallel to phi surface
UNCOV
1648
  if (std::abs(denominator) > FP_PRECISION) {
×
1649
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1650
    // Check if solution is in positive direction of flight and crosses the
UNCOV
1651
    // correct phi surface (not -phi)
×
1652
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
1653
      return s;
1654
  }
1655

1656
  return INFTY;
1657
}
1658

396✔
1659
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
1660
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
396✔
1661
  double l) const
396✔
1662
{
396✔
1663

396✔
1664
  if (i == 0) {
396✔
1665
    return std::min(
396✔
1666
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
1667
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
336✔
1668

1669
  } else if (i == 1) {
336✔
1670
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
336✔
1671
                      find_theta_crossing(r0, u, l, ijk[i])),
1672
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
336✔
1673
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
336✔
1674

1675
  } else {
336✔
1676
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
336✔
1677
                      find_phi_crossing(r0, u, l, ijk[i])),
1678
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
336✔
1679
        find_phi_crossing(r0, u, l, ijk[i] - 1)));
1680
  }
1681
}
1682

1683
int SphericalMesh::set_grid()
1684
{
1685
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
317✔
1686
    static_cast<int>(grid_[1].size()) - 1,
317✔
1687
    static_cast<int>(grid_[2].size()) - 1};
1688

317✔
1689
  for (const auto& g : grid_) {
1690
    if (g.size() < 2) {
317✔
1691
      set_errmsg("x-, y-, and z- grids for spherical meshes "
317✔
1692
                 "must each have at least 2 points");
317✔
1693
      return OPENMC_E_INVALID_ARGUMENT;
317✔
1694
    }
1695
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
317✔
UNCOV
1696
        g.end()) {
×
1697
      set_errmsg("Values in for r-, theta-, and phi- grids for "
1698
                 "spherical meshes must be sorted and unique.");
317✔
1699
      return OPENMC_E_INVALID_ARGUMENT;
1700
    }
1701
    if (g.front() < 0.0) {
1702
      set_errmsg("r-, theta-, and phi- grids for "
372✔
1703
                 "spherical meshes must start at v >= 0.");
1704
      return OPENMC_E_INVALID_ARGUMENT;
372✔
1705
    }
1706
  }
1707
  if (grid_[1].back() > PI) {
73,258,044✔
1708
    set_errmsg("theta-grids for "
1709
               "spherical meshes must end with theta <= pi.");
1710

73,258,044✔
1711
    return OPENMC_E_INVALID_ARGUMENT;
1712
  }
73,258,044✔
1713
  if (grid_[2].back() > 2 * PI) {
73,258,044✔
1714
    set_errmsg("phi-grids for "
1715
               "spherical meshes must end with phi <= 2*pi.");
73,258,044✔
1716
    return OPENMC_E_INVALID_ARGUMENT;
×
UNCOV
1717
  }
×
1718

1719
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
73,258,044✔
1720
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
73,258,044✔
1721

73,258,044✔
1722
  double r = grid_[0].back();
36,598,944✔
1723
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
1724
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
1725

73,258,044✔
1726
  return 0;
1727
}
73,258,044✔
1728

73,258,044✔
1729
int SphericalMesh::get_index_in_direction(double r, int i) const
1730
{
73,258,044✔
1731
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1732
}
1733

1,440,000✔
1734
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
1735
  Position plot_ll, Position plot_ur) const
1736
{
1,440,000✔
1737
  fatal_error("Plot of spherical Mesh not implemented");
1,440,000✔
1738

1739
  // Figure out which axes lie in the plane of the plot.
1,440,000✔
1740
  array<vector<double>, 2> axis_lines;
1,440,000✔
1741
  return {axis_lines[0], axis_lines[1]};
1742
}
1,440,000✔
1743

1,440,000✔
1744
void SphericalMesh::to_hdf5_inner(hid_t mesh_group) const
1745
{
1,440,000✔
1746
  write_dataset(mesh_group, "type", SphericalMesh::mesh_type);
1,440,000✔
1747
  write_dataset(mesh_group, "r_grid", grid_[0]);
1,440,000✔
1748
  write_dataset(mesh_group, "theta_grid", grid_[1]);
1,440,000✔
1749
  write_dataset(mesh_group, "phi_grid", grid_[2]);
1,440,000✔
1750
  write_dataset(mesh_group, "origin", origin_);
1751
}
1,440,000✔
1752

1753
double SphericalMesh::volume(const MeshIndex& ijk) const
1,440,000✔
1754
{
1,440,000✔
1755
  double r_i = grid_[0][ijk[0] - 1];
1,440,000✔
1756
  double r_o = grid_[0][ijk[0]];
1757

1,440,000✔
1758
  double theta_i = grid_[1][ijk[1] - 1];
1759
  double theta_o = grid_[1][ijk[1]];
1760

480,342,048✔
1761
  double phi_i = grid_[2][ijk[2] - 1];
1762
  double phi_o = grid_[2][ijk[2]];
1763

480,342,048✔
1764
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
43,677,828✔
1765
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
1766
}
1767

1768
//==============================================================================
436,664,220✔
1769
// Helper functions for the C API
436,664,220✔
1770
//==============================================================================
7,577,856✔
1771

429,086,364✔
1772
int check_mesh(int32_t index)
429,086,364✔
1773
{
429,086,364✔
1774
  if (index < 0 || index >= model::meshes.size()) {
1775
    set_errmsg("Index in meshes array is out of bounds.");
429,086,364✔
1776
    return OPENMC_E_OUT_OF_BOUNDS;
11,548,560✔
1777
  }
1778
  return 0;
417,537,804✔
1779
}
388,160,352✔
1780

1781
template<class T>
388,160,352✔
1782
int check_mesh_type(int32_t index)
69,316,164✔
1783
{
318,844,188✔
1784
  if (int err = check_mesh(index))
192,088,092✔
1785
    return err;
1786

1787
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
156,133,548✔
1788
  if (!mesh) {
1789
    set_errmsg("This function is not valid for input mesh.");
1790
    return OPENMC_E_INVALID_TYPE;
117,164,688✔
1791
  }
1792
  return 0;
1793
}
1794

117,164,688✔
1795
template<class T>
76,498,272✔
1796
bool is_mesh_type(int32_t index)
1797
{
40,666,416✔
1798
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1799
  return mesh;
1800
}
1801

1802
//==============================================================================
1803
// C API functions
1804
//==============================================================================
1805

1806
// Return the type of mesh as a C string
40,666,416✔
1807
extern "C" int openmc_mesh_get_type(int32_t index, char* type)
40,666,416✔
1808
{
40,666,416✔
1809
  if (int err = check_mesh(index))
1810
    return err;
40,666,416✔
1811

40,666,416✔
1812
  std::strcpy(type, model::meshes[index].get()->get_mesh_type().c_str());
40,666,416✔
1813

1814
  return 0;
1815
}
40,666,416✔
1816

1817
//! Extend the meshes array by n elements
1818
extern "C" int openmc_extend_meshes(
×
UNCOV
1819
  int32_t n, const char* type, int32_t* index_start, int32_t* index_end)
×
1820
{
UNCOV
1821
  if (index_start)
×
1822
    *index_start = model::meshes.size();
1823
  std::string mesh_type;
×
UNCOV
1824

×
1825
  for (int i = 0; i < n; ++i) {
1826
    if (RegularMesh::mesh_type == type) {
UNCOV
1827
      model::meshes.push_back(make_unique<RegularMesh>());
×
1828
    } else if (RectilinearMesh::mesh_type == type) {
1829
      model::meshes.push_back(make_unique<RectilinearMesh>());
1830
    } else if (CylindricalMesh::mesh_type == type) {
40,666,416✔
1831
      model::meshes.push_back(make_unique<CylindricalMesh>());
40,666,416✔
1832
    } else if (SphericalMesh::mesh_type == type) {
1833
      model::meshes.push_back(make_unique<SphericalMesh>());
40,666,416✔
1834
    } else {
11,532,864✔
1835
      throw std::runtime_error {"Unknown mesh type: " + std::string(type)};
1836
    }
29,133,552✔
1837
  }
1838
  if (index_end)
1839
    *index_end = model::meshes.size() - 1;
29,133,552✔
1840

1841
  return 0;
29,133,552✔
1842
}
5,527,560✔
1843

1844
//! Adds a new unstructured mesh to OpenMC
23,605,992✔
1845
extern "C" int openmc_add_unstructured_mesh(
1846
  const char filename[], const char library[], int* id)
23,605,992✔
1847
{
11,064,504✔
1848
  std::string lib_name(library);
1849
  std::string mesh_file(filename);
12,541,488✔
1850
  bool valid_lib = false;
1851

1852
#ifdef DAGMC
118,858,440✔
1853
  if (lib_name == MOABMesh::mesh_lib_type) {
1854
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
1855
    valid_lib = true;
1856
  }
118,858,440✔
1857
#endif
76,498,272✔
1858

1859
#ifdef LIBMESH
42,360,168✔
1860
  if (lib_name == LibMesh::mesh_lib_type) {
1861
    model::meshes.push_back(std::move(make_unique<LibMesh>(mesh_file)));
42,360,168✔
1862
    valid_lib = true;
1863
  }
1864
#endif
1865

1866
  if (!valid_lib) {
1867
    set_errmsg(fmt::format("Mesh library {} is not supported "
1868
                           "by this build of OpenMC",
42,360,168✔
1869
      lib_name));
42,360,168✔
1870
    return OPENMC_E_INVALID_ARGUMENT;
1871
  }
42,360,168✔
1872

1873
  // auto-assign new ID
1874
  model::meshes.back()->set_id(-1);
42,360,168✔
1875
  *id = model::meshes.back()->id_;
42,360,168✔
1876

1877
  return 0;
1878
}
42,360,168✔
1879

18,707,712✔
1880
//! Return the index in the meshes array of a mesh with a given ID
1881
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
1882
{
23,652,456✔
1883
  auto pair = model::mesh_map.find(id);
1884
  if (pair == model::mesh_map.end()) {
1885
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
358,182,588✔
1886
    return OPENMC_E_INVALID_ID;
1887
  }
1888
  *index = pair->second;
1889
  return 0;
1890
}
358,182,588✔
1891

240,171,024✔
1892
//! Return the ID of a mesh
240,171,024✔
1893
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
480,342,048✔
1894
{
1895
  if (int err = check_mesh(index))
118,011,564✔
1896
    return err;
58,582,344✔
1897
  *id = model::meshes[index]->id_;
58,582,344✔
1898
  return 0;
58,582,344✔
1899
}
117,164,688✔
1900

1901
//! Set the ID of a mesh
1902
extern "C" int openmc_mesh_set_id(int32_t index, int32_t id)
59,429,220✔
1903
{
59,429,220✔
1904
  if (int err = check_mesh(index))
59,429,220✔
1905
    return err;
118,858,440✔
1906
  model::meshes[index]->id_ = id;
1907
  model::mesh_map[id] = index;
1908
  return 0;
1909
}
341✔
1910

1911
//! Get the number of elements in a mesh
341✔
1912
extern "C" int openmc_mesh_get_n_elements(int32_t index, size_t* n)
341✔
1913
{
341✔
1914
  if (int err = check_mesh(index))
1915
    return err;
1,364✔
1916
  *n = model::meshes[index]->n_bins();
1,023✔
UNCOV
1917
  return 0;
×
1918
}
UNCOV
1919

×
1920
//! Get the volume of each element in the mesh
1921
extern "C" int openmc_mesh_get_volumes(int32_t index, double* volumes)
1,023✔
1922
{
2,046✔
UNCOV
1923
  if (int err = check_mesh(index))
×
1924
    return err;
UNCOV
1925
  for (int i = 0; i < model::meshes[index]->n_bins(); ++i) {
×
1926
    volumes[i] = model::meshes[index]->volume(i);
1927
  }
1,023✔
UNCOV
1928
  return 0;
×
1929
}
UNCOV
1930

×
1931
//! Get the bounding box of a mesh
1932
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
1933
{
341✔
UNCOV
1934
  if (int err = check_mesh(index))
×
1935
    return err;
1936

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

1939
  // set lower left corner values
341✔
UNCOV
1940
  ll[0] = bbox.xmin;
×
1941
  ll[1] = bbox.ymin;
UNCOV
1942
  ll[2] = bbox.zmin;
×
1943

1944
  // set upper right corner values
1945
  ur[0] = bbox.xmax;
341✔
1946
  ur[1] = bbox.ymax;
341✔
1947
  ur[2] = bbox.zmax;
1948
  return 0;
341✔
1949
}
341✔
1950

341✔
1951
extern "C" int openmc_mesh_material_volumes(int32_t index, int n_sample,
1952
  int bin, int result_size, void* result, int* hits, uint64_t* seed)
341✔
1953
{
1954
  auto result_ = reinterpret_cast<Mesh::MaterialVolume*>(result);
1955
  if (!result_) {
219,774,132✔
1956
    set_errmsg("Invalid result pointer passed to openmc_mesh_material_volumes");
1957
    return OPENMC_E_INVALID_ARGUMENT;
219,774,132✔
1958
  }
1959

UNCOV
1960
  if (int err = check_mesh(index))
×
1961
    return err;
1962

UNCOV
1963
  int n = model::meshes[index]->material_volumes(
×
1964
    n_sample, bin, {result_, result_ + result_size}, seed);
1965
  *hits = n;
1966
  return (n == -1) ? OPENMC_E_ALLOCATE : 0;
1967
}
1968

1969
extern "C" int openmc_mesh_get_plot_bins(int32_t index, Position origin,
1970
  Position width, int basis, int* pixels, int32_t* data)
312✔
1971
{
1972
  if (int err = check_mesh(index))
312✔
1973
    return err;
312✔
1974
  const auto& mesh = model::meshes[index].get();
312✔
1975

312✔
1976
  int pixel_width = pixels[0];
312✔
1977
  int pixel_height = pixels[1];
312✔
1978

1979
  // get pixel size
600✔
1980
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
1981
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
600✔
1982

600✔
1983
  // setup basis indices and initial position centered on pixel
1984
  int in_i, out_i;
600✔
1985
  Position xyz = origin;
600✔
1986
  enum class PlotBasis { xy = 1, xz = 2, yz = 3 };
1987
  PlotBasis basis_enum = static_cast<PlotBasis>(basis);
600✔
1988
  switch (basis_enum) {
600✔
1989
  case PlotBasis::xy:
1990
    in_i = 0;
600✔
1991
    out_i = 1;
600✔
1992
    break;
1993
  case PlotBasis::xz:
1994
    in_i = 0;
1995
    out_i = 2;
1996
    break;
1997
  case PlotBasis::yz:
1998
    in_i = 1;
9,376✔
1999
    out_i = 2;
2000
    break;
9,376✔
2001
  default:
×
UNCOV
2002
    UNREACHABLE();
×
2003
  }
2004

9,376✔
2005
  // set initial position
2006
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2007
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
2008

1,759✔
2009
#pragma omp parallel
2010
  {
1,759✔
UNCOV
2011
    Position r = xyz;
×
2012

2013
#pragma omp for
1,759✔
2014
    for (int y = 0; y < pixel_height; y++) {
1,759✔
2015
      r[out_i] = xyz[out_i] - out_pixel * y;
×
UNCOV
2016
      for (int x = 0; x < pixel_width; x++) {
×
2017
        r[in_i] = xyz[in_i] + in_pixel * x;
2018
        data[pixel_width * y + x] = mesh->get_bin(r);
1,759✔
2019
      }
2020
    }
156✔
2021
  }
2022

156✔
UNCOV
2023
  return 0;
×
2024
}
2025

156✔
2026
//! Get the dimension of a regular mesh
156✔
2027
extern "C" int openmc_regular_mesh_get_dimension(
×
UNCOV
2028
  int32_t index, int** dims, int* n)
×
2029
{
2030
  if (int err = check_mesh_type<RegularMesh>(index))
156✔
2031
    return err;
2032
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
156✔
2033
  *dims = mesh->shape_.data();
2034
  *n = mesh->n_dimension_;
156✔
UNCOV
2035
  return 0;
×
2036
}
2037

156✔
2038
//! Set the dimension of a regular mesh
156✔
2039
extern "C" int openmc_regular_mesh_set_dimension(
×
UNCOV
2040
  int32_t index, int n, const int* dims)
×
2041
{
2042
  if (int err = check_mesh_type<RegularMesh>(index))
156✔
2043
    return err;
2044
  RegularMesh* mesh = dynamic_cast<RegularMesh*>(model::meshes[index].get());
256✔
2045

2046
  // Copy dimension
256✔
UNCOV
2047
  mesh->n_dimension_ = n;
×
2048
  std::copy(dims, dims + n, mesh->shape_.begin());
2049
  return 0;
256✔
2050
}
256✔
2051

×
UNCOV
2052
//! Get the regular mesh parameters
×
2053
extern "C" int openmc_regular_mesh_get_params(
2054
  int32_t index, double** ll, double** ur, double** width, int* n)
256✔
2055
{
2056
  if (int err = check_mesh_type<RegularMesh>(index))
1,191✔
2057
    return err;
2058
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
1,191✔
UNCOV
2059

×
2060
  if (m->lower_left_.dimension() == 0) {
2061
    set_errmsg("Mesh parameters have not been set.");
1,191✔
2062
    return OPENMC_E_ALLOCATE;
1,191✔
2063
  }
×
UNCOV
2064

×
2065
  *ll = m->lower_left_.data();
2066
  *ur = m->upper_right_.data();
1,191✔
2067
  *width = m->width_.data();
2068
  *n = m->n_dimension_;
2069
  return 0;
2070
}
2071

2072
//! Set the regular mesh parameters
2073
extern "C" int openmc_regular_mesh_set_params(
2074
  int32_t index, int n, const double* ll, const double* ur, const double* width)
2075
{
2076
  if (int err = check_mesh_type<RegularMesh>(index))
2077
    return err;
2078
  RegularMesh* m = dynamic_cast<RegularMesh*>(model::meshes[index].get());
2079

2080
  if (m->n_dimension_ == -1) {
2081
    set_errmsg("Need to set mesh dimension before setting parameters.");
2,073✔
2082
    return OPENMC_E_UNASSIGNED;
2083
  }
2,073✔
UNCOV
2084

×
2085
  vector<std::size_t> shape = {static_cast<std::size_t>(n)};
2086
  if (ll && ur) {
2,073✔
2087
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
2088
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
2,073✔
2089
    m->width_ = (m->upper_right_ - m->lower_left_) / m->get_x_shape();
2090
  } else if (ll && width) {
2091
    m->lower_left_ = xt::adapt(ll, n, xt::no_ownership(), shape);
2092
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
471✔
2093
    m->upper_right_ = m->lower_left_ + m->get_x_shape() * m->width_;
2094
  } else if (ur && width) {
2095
    m->upper_right_ = xt::adapt(ur, n, xt::no_ownership(), shape);
471✔
2096
    m->width_ = xt::adapt(width, n, xt::no_ownership(), shape);
471✔
2097
    m->lower_left_ = m->upper_right_ - m->get_x_shape() * m->width_;
471✔
2098
  } else {
2099
    set_errmsg("At least two parameters must be specified.");
942✔
2100
    return OPENMC_E_INVALID_ARGUMENT;
471✔
2101
  }
349✔
2102

122✔
2103
  // Set material volumes
74✔
2104

48✔
2105
  // TODO: incorporate this into method in RegularMesh that can be called from
24✔
2106
  // here and from constructor
24✔
2107
  m->volume_frac_ = 1.0 / xt::prod(m->get_x_shape())();
24✔
2108
  m->element_volume_ = 1.0;
UNCOV
2109
  for (int i = 0; i < m->n_dimension_; i++) {
×
2110
    m->element_volume_ *= m->width_[i];
2111
  }
2112

471✔
UNCOV
2113
  return 0;
×
2114
}
2115

471✔
2116
//! Set the mesh parameters for rectilinear, cylindrical and spharical meshes
471✔
2117
template<class C>
2118
int openmc_structured_mesh_set_grid_impl(int32_t index, const double* grid_x,
UNCOV
2119
  const int nx, const double* grid_y, const int ny, const double* grid_z,
×
2120
  const int nz)
2121
{
2122
  if (int err = check_mesh_type<C>(index))
×
2123
    return err;
×
UNCOV
2124

×
2125
  C* m = dynamic_cast<C*>(model::meshes[index].get());
2126

2127
  m->n_dimension_ = 3;
2128

2129
  m->grid_[0].reserve(nx);
2130
  m->grid_[1].reserve(ny);
2131
  m->grid_[2].reserve(nz);
2132

2133
  for (int i = 0; i < nx; i++) {
2134
    m->grid_[0].push_back(grid_x[i]);
2135
  }
2136
  for (int i = 0; i < ny; i++) {
2137
    m->grid_[1].push_back(grid_y[i]);
2138
  }
2139
  for (int i = 0; i < nz; i++) {
2140
    m->grid_[2].push_back(grid_z[i]);
×
UNCOV
2141
  }
×
2142

2143
  int err = m->set_grid();
UNCOV
2144
  return err;
×
2145
}
2146

2147
//! Get the mesh parameters for rectilinear, cylindrical and spherical meshes
2148
template<class C>
×
UNCOV
2149
int openmc_structured_mesh_get_grid_impl(int32_t index, double** grid_x,
×
2150
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
UNCOV
2151
{
×
2152
  if (int err = check_mesh_type<C>(index))
2153
    return err;
2154
  C* m = dynamic_cast<C*>(model::meshes[index].get());
2155

663✔
2156
  if (m->lower_left_.dimension() == 0) {
2157
    set_errmsg("Mesh parameters have not been set.");
663✔
2158
    return OPENMC_E_ALLOCATE;
663✔
2159
  }
×
UNCOV
2160

×
2161
  *grid_x = m->grid_[0].data();
2162
  *nx = m->grid_[0].size();
663✔
2163
  *grid_y = m->grid_[1].data();
663✔
2164
  *ny = m->grid_[1].size();
2165
  *grid_z = m->grid_[2].data();
2166
  *nz = m->grid_[2].size();
2167

4,305✔
2168
  return 0;
2169
}
4,305✔
UNCOV
2170

×
2171
//! Get the rectilinear mesh grid
4,305✔
2172
extern "C" int openmc_rectilinear_mesh_get_grid(int32_t index, double** grid_x,
4,305✔
2173
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2174
{
2175
  return openmc_structured_mesh_get_grid_impl<RectilinearMesh>(
2176
    index, grid_x, nx, grid_y, ny, grid_z, nz);
471✔
2177
}
2178

471✔
UNCOV
2179
//! Set the rectilienar mesh parameters
×
2180
extern "C" int openmc_rectilinear_mesh_set_grid(int32_t index,
471✔
2181
  const double* grid_x, const int nx, const double* grid_y, const int ny,
471✔
2182
  const double* grid_z, const int nz)
471✔
2183
{
2184
  return openmc_structured_mesh_set_grid_impl<RectilinearMesh>(
2185
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2186
}
192✔
2187

2188
//! Get the cylindrical mesh grid
192✔
UNCOV
2189
extern "C" int openmc_cylindrical_mesh_get_grid(int32_t index, double** grid_x,
×
2190
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
192✔
2191
{
192✔
2192
  return openmc_structured_mesh_get_grid_impl<CylindricalMesh>(
2193
    index, grid_x, nx, grid_y, ny, grid_z, nz);
2194
}
2195

96✔
2196
//! Set the cylindrical mesh parameters
2197
extern "C" int openmc_cylindrical_mesh_set_grid(int32_t index,
96✔
UNCOV
2198
  const double* grid_x, const int nx, const double* grid_y, const int ny,
×
2199
  const double* grid_z, const int nz)
1,056✔
2200
{
960✔
2201
  return openmc_structured_mesh_set_grid_impl<CylindricalMesh>(
2202
    index, grid_x, nx, grid_y, ny, grid_z, nz);
96✔
2203
}
2204

2205
//! Get the spherical mesh grid
2206
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
48✔
2207
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2208
{
48✔
UNCOV
2209

×
2210
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2211
    index, grid_x, nx, grid_y, ny, grid_z, nz);
48✔
2212
  ;
2213
}
2214

48✔
2215
//! Set the spherical mesh parameters
48✔
2216
extern "C" int openmc_spherical_mesh_set_grid(int32_t index,
48✔
2217
  const double* grid_x, const int nx, const double* grid_y, const int ny,
2218
  const double* grid_z, const int nz)
2219
{
48✔
2220
  return openmc_structured_mesh_set_grid_impl<SphericalMesh>(
48✔
2221
    index, grid_x, nx, grid_y, ny, grid_z, nz);
48✔
2222
}
48✔
2223

2224
#ifdef DAGMC
2225

384✔
2226
const std::string MOABMesh::mesh_lib_type = "moab";
2227

2228
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
384✔
2229
{
384✔
2230
  initialize();
×
UNCOV
2231
}
×
2232

2233
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2234
{
384✔
UNCOV
2235
  filename_ = filename;
×
2236
  set_length_multiplier(length_multiplier);
2237
  initialize();
768✔
2238
}
384✔
2239

384✔
2240
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
384✔
2241
{
2242
  mbi_ = external_mbi;
2243
  filename_ = "unknown (external file)";
48✔
2244
  this->initialize();
2245
}
2246

48✔
UNCOV
2247
void MOABMesh::initialize()
×
2248
{
48✔
2249

2250
  // Create the MOAB interface and load data from file
48✔
2251
  this->create_interface();
48✔
2252

2253
  // Initialise MOAB error code
2254
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2255

48✔
2256
  // Set the dimension
2257
  n_dimension_ = 3;
2258

2259
  // set member range of tetrahedral entities
48✔
2260
  rval = mbi_->get_entities_by_dimension(0, n_dimension_, ehs_);
2261
  if (rval != moab::MB_SUCCESS) {
48✔
2262
    fatal_error("Failed to get all tetrahedral elements");
48✔
2263
  }
48✔
2264

48✔
2265
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2266
    warning("Non-tetrahedral elements found in unstructured "
48✔
2267
            "mesh file: " +
×
2268
            filename_);
×
2269
  }
×
2270

×
2271
  // set member range of vertices
×
2272
  int vertex_dim = 0;
×
2273
  rval = mbi_->get_entities_by_dimension(0, vertex_dim, verts_);
×
2274
  if (rval != moab::MB_SUCCESS) {
×
2275
    fatal_error("Failed to get all vertex handles");
×
UNCOV
2276
  }
×
2277

2278
  // make an entity set for all tetrahedra
2279
  // this is used for convenience later in output
2280
  rval = mbi_->create_meshset(moab::MESHSET_SET, tetset_);
48✔
2281
  if (rval != moab::MB_SUCCESS) {
48✔
2282
    fatal_error("Failed to create an entity set for the tetrahedral elements");
2283
  }
24✔
2284

2285
  rval = mbi_->add_entities(tetset_, ehs_);
24✔
2286
  if (rval != moab::MB_SUCCESS) {
2287
    fatal_error("Failed to add tetrahedra to an entity set.");
2288
  }
504✔
2289

480✔
2290
  if (length_multiplier_ > 0.0) {
10,080✔
2291
    // get the connectivity of all tets
9,600✔
2292
    moab::Range adj;
9,600✔
2293
    rval = mbi_->get_adjacencies(ehs_, 0, true, adj, moab::Interface::UNION);
2294
    if (rval != moab::MB_SUCCESS) {
2295
      fatal_error("Failed to get adjacent vertices of tetrahedra.");
2296
    }
2297
    // scale all vertex coords by multiplier (done individually so not all
48✔
2298
    // coordinates are in memory twice at once)
2299
    for (auto vert : adj) {
2300
      // retrieve coords
2301
      std::array<double, 3> coord;
12✔
2302
      rval = mbi_->get_coords(&vert, 1, coord.data());
2303
      if (rval != moab::MB_SUCCESS) {
2304
        fatal_error("Could not get coordinates of vertex.");
12✔
UNCOV
2305
      }
×
2306
      // scale coords
12✔
2307
      for (auto& c : coord) {
12✔
2308
        c *= length_multiplier_;
12✔
2309
      }
12✔
2310
      // set new coords
2311
      rval = mbi_->set_coords(&vert, 1, coord.data());
2312
      if (rval != moab::MB_SUCCESS) {
2313
        fatal_error("Failed to set new vertex coordinates");
373✔
2314
      }
2315
    }
2316
  }
373✔
UNCOV
2317

×
2318
  // Determine bounds of mesh
373✔
2319
  this->determine_bounds();
2320
}
2321

373✔
2322
void MOABMesh::prepare_for_point_location()
373✔
2323
{
373✔
2324
  // if the KDTree has already been constructed, do nothing
2325
  if (kdtree_)
2326
    return;
2327

397✔
2328
  // build acceleration data structures
2329
  compute_barycentric_data(ehs_);
2330
  build_kdtree(ehs_);
397✔
UNCOV
2331
}
×
2332

397✔
2333
void MOABMesh::create_interface()
2334
{
397✔
2335
  // Do not create a MOAB instance if one is already in memory
×
UNCOV
2336
  if (mbi_)
×
2337
    return;
2338

2339
  // create MOAB instance
397✔
2340
  mbi_ = std::make_shared<moab::Core>();
397✔
2341

397✔
2342
  // load unstructured mesh file
397✔
2343
  moab::ErrorCode rval = mbi_->load_file(filename_.c_str());
397✔
2344
  if (rval != moab::MB_SUCCESS) {
2345
    fatal_error("Failed to load the unstructured mesh file: " + filename_);
2346
  }
2347
}
409✔
2348

2349
void MOABMesh::build_kdtree(const moab::Range& all_tets)
2350
{
409✔
UNCOV
2351
  moab::Range all_tris;
×
2352
  int adj_dim = 2;
409✔
2353
  write_message("Getting tet adjacencies...", 7);
2354
  moab::ErrorCode rval = mbi_->get_adjacencies(
409✔
2355
    all_tets, adj_dim, true, all_tris, moab::Interface::UNION);
×
UNCOV
2356
  if (rval != moab::MB_SUCCESS) {
×
2357
    fatal_error("Failed to get adjacent triangles for tets");
2358
  }
2359

409✔
2360
  if (!all_tris.all_of_type(moab::MBTRI)) {
409✔
2361
    warning("Non-triangle elements found in tet adjacencies in "
385✔
2362
            "unstructured mesh file: " +
385✔
2363
            filename_);
385✔
2364
  }
24✔
2365

12✔
2366
  // combine into one range
12✔
2367
  moab::Range all_tets_and_tris;
12✔
2368
  all_tets_and_tris.merge(all_tets);
12✔
2369
  all_tets_and_tris.merge(all_tris);
12✔
2370

12✔
2371
  // create a kd-tree instance
12✔
2372
  write_message(
2373
    7, "Building adaptive k-d tree for tet mesh with ID {}...", id_);
×
UNCOV
2374
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
×
2375

2376
  // Determine what options to use
2377
  std::ostringstream options_stream;
2378
  if (options_.empty()) {
2379
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
2380
  } else {
2381
    options_stream << options_;
409✔
2382
  }
409✔
2383
  moab::FileOptions file_opts(options_stream.str().c_str());
1,636✔
2384

1,227✔
2385
  // Build the k-d tree
2386
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2387
  if (rval != moab::MB_SUCCESS) {
409✔
2388
    fatal_error("Failed to construct KDTree for the "
409✔
2389
                "unstructured mesh file: " +
2390
                filename_);
2391
  }
2392
}
122✔
2393

2394
void MOABMesh::intersect_track(const moab::CartVect& start,
2395
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2396
{
122✔
UNCOV
2397
  hits.clear();
×
2398

2399
  moab::ErrorCode rval;
122✔
2400
  vector<moab::EntityHandle> tris;
2401
  // get all intersections with triangles in the tet mesh
122✔
2402
  // (distances are relative to the start point, not the previous intersection)
2403
  rval = kdtree_->ray_intersect_triangles(kdtree_root_, FP_COINCIDENT,
122✔
2404
    dir.array(), start.array(), tris, hits, 0, track_len);
122✔
2405
  if (rval != moab::MB_SUCCESS) {
122✔
2406
    fatal_error(
2407
      "Failed to compute intersections on unstructured mesh: " + filename_);
988✔
2408
  }
866✔
2409

2410
  // remove duplicate intersection distances
450✔
2411
  std::unique(hits.begin(), hits.end());
328✔
2412

2413
  // sorts by first component of std::pair by default
426✔
2414
  std::sort(hits.begin(), hits.end());
304✔
2415
}
2416

2417
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
122✔
2418
  vector<int>& bins, vector<double>& lengths) const
122✔
2419
{
2420
  moab::CartVect start(r0.x, r0.y, r0.z);
24✔
2421
  moab::CartVect end(r1.x, r1.y, r1.z);
2422
  moab::CartVect dir(u.x, u.y, u.z);
2423
  dir.normalize();
2424

24✔
UNCOV
2425
  double track_len = (end - start).length();
×
2426
  if (track_len == 0.0)
2427
    return;
24✔
2428

2429
  start -= TINY_BIT * dir;
24✔
2430
  end += TINY_BIT * dir;
2431

24✔
2432
  vector<double> hits;
24✔
2433
  intersect_track(start, dir, track_len, hits);
24✔
2434

2435
  bins.clear();
96✔
2436
  lengths.clear();
72✔
2437

2438
  // if there are no intersections the track may lie entirely
96✔
2439
  // within a single tet. If this is the case, apply entire
72✔
2440
  // score to that tet and return.
2441
  if (hits.size() == 0) {
108✔
2442
    Position midpoint = r0 + u * (track_len * 0.5);
84✔
2443
    int bin = this->get_bin(midpoint);
2444
    if (bin != -1) {
2445
      bins.push_back(bin);
24✔
2446
      lengths.push_back(1.0);
24✔
2447
    }
2448
    return;
24✔
2449
  }
2450

2451
  // for each segment in the set of tracks, try to look up a tet
2452
  // at the midpoint of the segment
24✔
UNCOV
2453
  Position current = r0;
×
2454
  double last_dist = 0.0;
2455
  for (const auto& hit : hits) {
24✔
2456
    // get the segment length
2457
    double segment_length = hit - last_dist;
24✔
2458
    last_dist = hit;
2459
    // find the midpoint of this segment
24✔
2460
    Position midpoint = current + u * (segment_length * 0.5);
24✔
2461
    // try to find a tet for this position
24✔
2462
    int bin = this->get_bin(midpoint);
2463

96✔
2464
    // determine the start point for this segment
72✔
2465
    current = r0 + u * hit;
2466

108✔
2467
    if (bin == -1) {
84✔
2468
      continue;
2469
    }
84✔
2470

60✔
2471
    bins.push_back(bin);
2472
    lengths.push_back(segment_length / track_len);
2473
  }
24✔
2474

24✔
2475
  // tally remaining portion of track after last hit if
2476
  // the last segment of the track is in the mesh but doesn't
74✔
2477
  // reach the other side of the tet
2478
  if (hits.back() < track_len) {
2479
    Position segment_start = r0 + u * hits.back();
2480
    double segment_length = track_len - hits.back();
74✔
UNCOV
2481
    Position midpoint = segment_start + u * (segment_length * 0.5);
×
2482
    int bin = this->get_bin(midpoint);
2483
    if (bin != -1) {
74✔
2484
      bins.push_back(bin);
2485
      lengths.push_back(segment_length / track_len);
74✔
2486
    }
2487
  }
74✔
2488
};
74✔
2489

74✔
2490
moab::EntityHandle MOABMesh::get_tet(const Position& r) const
2491
{
796✔
2492
  moab::CartVect pos(r.x, r.y, r.z);
722✔
2493
  // find the leaf of the kd-tree for this position
2494
  moab::AdaptiveKDTreeIter kdtree_iter;
246✔
2495
  moab::ErrorCode rval = kdtree_->point_search(pos.array(), kdtree_iter);
172✔
2496
  if (rval != moab::MB_SUCCESS) {
2497
    return 0;
234✔
2498
  }
160✔
2499

2500
  // retrieve the tet elements of this leaf
2501
  moab::EntityHandle leaf = kdtree_iter.handle();
74✔
2502
  moab::Range tets;
74✔
2503
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
2504
  if (rval != moab::MB_SUCCESS) {
2505
    warning("MOAB error finding tets.");
2506
  }
2507

446✔
2508
  // loop over the tets in this leaf, returning the containing tet if found
2509
  for (const auto& tet : tets) {
2510
    if (point_in_tet(pos, tet)) {
446✔
UNCOV
2511
      return tet;
×
2512
    }
446✔
2513
  }
2514

446✔
2515
  // if no tet is found, return an invalid handle
×
UNCOV
2516
  return 0;
×
2517
}
2518

2519
double MOABMesh::volume(int bin) const
446✔
2520
{
446✔
2521
  return tet_volume(get_ent_handle_from_bin(bin));
446✔
2522
}
446✔
2523

446✔
2524
std::string MOABMesh::library() const
446✔
2525
{
2526
  return mesh_lib_type;
446✔
2527
}
2528

132✔
2529
// Sample position within a tet for MOAB type tets
2530
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2531
{
132✔
UNCOV
2532

×
2533
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
132✔
2534

2535
  // Get vertex coordinates for MOAB tet
132✔
2536
  const moab::EntityHandle* conn1;
×
UNCOV
2537
  int conn1_size;
×
2538
  moab::ErrorCode rval = mbi_->get_connectivity(tet_ent, conn1, conn1_size);
2539
  if (rval != moab::MB_SUCCESS || conn1_size != 4) {
2540
    fatal_error(fmt::format(
132✔
2541
      "Failed to get tet connectivity or connectivity size ({}) is invalid.",
132✔
2542
      conn1_size));
132✔
2543
  }
132✔
2544
  moab::CartVect p[4];
132✔
2545
  rval = mbi_->get_coords(conn1, conn1_size, p[0].array());
132✔
2546
  if (rval != moab::MB_SUCCESS) {
2547
    fatal_error("Failed to get tet coords");
132✔
2548
  }
2549

132✔
2550
  std::array<Position, 4> tet_verts;
2551
  for (int i = 0; i < 4; i++) {
2552
    tet_verts[i] = {p[i][0], p[i][1], p[i][2]};
132✔
UNCOV
2553
  }
×
2554
  // Samples position within tet using Barycentric stuff
132✔
2555
  return this->sample_tet(tet_verts, seed);
2556
}
132✔
2557

×
UNCOV
2558
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2559
{
2560
  vector<moab::EntityHandle> conn;
2561
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
132✔
2562
  if (rval != moab::MB_SUCCESS) {
132✔
2563
    fatal_error("Failed to get tet connectivity");
132✔
2564
  }
132✔
2565

132✔
2566
  moab::CartVect p[4];
132✔
2567
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
2568
  if (rval != moab::MB_SUCCESS) {
132✔
2569
    fatal_error("Failed to get tet coords");
2570
  }
182✔
2571

2572
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2573
}
182✔
UNCOV
2574

×
2575
int MOABMesh::get_bin(Position r) const
182✔
2576
{
2577
  moab::EntityHandle tet = get_tet(r);
182✔
2578
  if (tet == 0) {
×
UNCOV
2579
    return -1;
×
2580
  } else {
2581
    return get_bin_from_ent_handle(tet);
2582
  }
182✔
2583
}
182✔
2584

182✔
2585
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
182✔
2586
{
182✔
2587
  moab::ErrorCode rval;
182✔
2588

2589
  baryc_data_.clear();
182✔
2590
  baryc_data_.resize(tets.size());
2591

2592
  // compute the barycentric data for each tet element
2593
  // and store it as a 3x3 matrix
182✔
2594
  for (auto& tet : tets) {
2595
    vector<moab::EntityHandle> verts;
2596
    rval = mbi_->get_connectivity(&tet, 1, verts);
182✔
2597
    if (rval != moab::MB_SUCCESS) {
182✔
2598
      fatal_error("Failed to get connectivity of tet on umesh: " + filename_);
2599
    }
2600

2601
    moab::CartVect p[4];
74✔
2602
    rval = mbi_->get_coords(verts.data(), verts.size(), p[0].array());
2603
    if (rval != moab::MB_SUCCESS) {
2604
      fatal_error("Failed to get coordinates of a tet in umesh: " + filename_);
2605
    }
74✔
2606

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

2609
    // invert now to avoid this cost later
2610
    a = a.transpose().inverse();
132✔
2611
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
2612
  }
2613
}
132✔
2614

132✔
2615
bool MOABMesh::point_in_tet(
2616
  const moab::CartVect& r, moab::EntityHandle tet) const
2617
{
2618

24✔
2619
  moab::ErrorCode rval;
2620

2621
  // get tet vertices
2622
  vector<moab::EntityHandle> verts;
24✔
2623
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2624
  if (rval != moab::MB_SUCCESS) {
2625
    warning("Failed to get vertices of tet in umesh: " + filename_);
2626
    return false;
2627
  }
132✔
2628

2629
  // first vertex is used as a reference point for the barycentric data -
2630
  // retrieve its coordinates
2631
  moab::CartVect p_zero;
132✔
2632
  rval = mbi_->get_coords(verts.data(), 1, p_zero.array());
132✔
2633
  if (rval != moab::MB_SUCCESS) {
2634
    warning("Failed to get coordinates of a vertex in "
2635
            "unstructured mesh: " +
2636
            filename_);
2637
    return false;
24✔
2638
  }
2639

2640
  // look up barycentric data
2641
  int idx = get_bin_from_ent_handle(tet);
24✔
2642
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2643

2644
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2645

2646
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
2647
          bary_coords[2] >= 0.0 &&
2648
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2649
}
22✔
2650

2651
int MOABMesh::get_bin_from_index(int idx) const
22✔
2652
{
22✔
2653
  if (idx >= n_bins()) {
2654
    fatal_error(fmt::format("Invalid bin index: {}", idx));
2655
  }
2656
  return ehs_[idx] - ehs_[0];
2657
}
2658

2659
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2660
{
2661
  int bin = get_bin(r);
1✔
2662
  *in_mesh = bin != -1;
2663
  return bin;
1✔
2664
}
1✔
2665

1✔
2666
int MOABMesh::get_index_from_bin(int bin) const
1✔
2667
{
2668
  return bin;
23✔
2669
}
2670

2671
std::pair<vector<double>, vector<double>> MOABMesh::plot(
2672
  Position plot_ll, Position plot_ur) const
23✔
2673
{
2674
  // TODO: Implement mesh lines
2675
  return {};
23✔
2676
}
2677

2678
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
23✔
2679
{
2680
  int idx = vert - verts_[0];
2681
  if (idx >= n_vertices()) {
23✔
2682
    fatal_error(
23✔
2683
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
2684
  }
2685
  return idx;
2686
}
23✔
2687

2688
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2689
{
2690
  int bin = eh - ehs_[0];
2691
  if (bin >= n_bins()) {
2692
    fatal_error(fmt::format("Invalid bin: {}", bin));
2693
  }
23✔
2694
  return bin;
23✔
2695
}
23✔
2696

2697
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2698
{
2699
  if (bin >= n_bins()) {
2700
    fatal_error(fmt::format("Invalid bin index: ", bin));
2701
  }
23✔
2702
  return ehs_[0] + bin;
23✔
2703
}
2704

2705
int MOABMesh::n_bins() const
2706
{
23✔
2707
  return ehs_.size();
23✔
2708
}
2709

2710
int MOABMesh::n_surface_bins() const
2711
{
23✔
2712
  // collect all triangles in the set of tets for this mesh
2713
  moab::Range tris;
2714
  moab::ErrorCode rval;
2715
  rval = mbi_->get_entities_by_type(0, moab::MBTRI, tris);
2716
  if (rval != moab::MB_SUCCESS) {
2717
    warning("Failed to get all triangles in the mesh instance");
2718
    return -1;
2719
  }
2720
  return 2 * tris.size();
2721
}
2722

2723
Position MOABMesh::centroid(int bin) const
2724
{
2725
  moab::ErrorCode rval;
2726

2727
  auto tet = this->get_ent_handle_from_bin(bin);
2728

2729
  // look up the tet connectivity
2730
  vector<moab::EntityHandle> conn;
2731
  rval = mbi_->get_connectivity(&tet, 1, conn);
2732
  if (rval != moab::MB_SUCCESS) {
2733
    warning("Failed to get connectivity of a mesh element.");
2734
    return {};
2735
  }
2736

2737
  // get the coordinates
2738
  vector<moab::CartVect> coords(conn.size());
2739
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
2740
  if (rval != moab::MB_SUCCESS) {
23✔
2741
    warning("Failed to get the coordinates of a mesh element.");
23✔
2742
    return {};
2743
  }
19✔
2744

2745
  // compute the centroid of the element vertices
2746
  moab::CartVect centroid(0.0, 0.0, 0.0);
19✔
2747
  for (const auto& coord : coords) {
2748
    centroid += coord;
2749
  }
2750
  centroid /= double(coords.size());
19✔
2751

19✔
2752
  return {centroid[0], centroid[1], centroid[2]};
2753
}
2754

23✔
2755
int MOABMesh::n_vertices() const
2756
{
2757
  return verts_.size();
23✔
2758
}
1✔
2759

2760
Position MOABMesh::vertex(int id) const
2761
{
22✔
2762

2763
  moab::ErrorCode rval;
2764

22✔
2765
  moab::EntityHandle vert = verts_[id];
22✔
2766

2767
  moab::CartVect coords;
2768
  rval = mbi_->get_coords(&vert, 1, coords.array());
2769
  if (rval != moab::MB_SUCCESS) {
2770
    fatal_error("Failed to get the coordinates of a vertex.");
19✔
2771
  }
2772

19✔
2773
  return {coords[0], coords[1], coords[2]};
19✔
2774
}
19✔
2775

19✔
2776
std::vector<int> MOABMesh::connectivity(int bin) const
2777
{
19✔
2778
  moab::ErrorCode rval;
2779

2780
  auto tet = get_ent_handle_from_bin(bin);
2781

19✔
2782
  // look up the tet connectivity
2783
  vector<moab::EntityHandle> conn;
2784
  rval = mbi_->get_connectivity(&tet, 1, conn);
2785
  if (rval != moab::MB_SUCCESS) {
2786
    fatal_error("Failed to get connectivity of a mesh element.");
2787
    return {};
2788
  }
19✔
2789

19✔
2790
  std::vector<int> verts(4);
19✔
2791
  for (int i = 0; i < verts.size(); i++) {
2792
    verts[i] = get_vert_idx_from_handle(conn[i]);
2793
  }
19✔
2794

19✔
2795
  return verts;
19✔
2796
}
2797

2798
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
19✔
2799
  std::string score) const
19✔
2800
{
3✔
2801
  moab::ErrorCode rval;
2802
  // add a tag to the mesh
16✔
2803
  // all scores are treated as a single value
2804
  // with an uncertainty
19✔
2805
  moab::Tag value_tag;
2806

2807
  // create the value tag if not present and get handle
19✔
2808
  double default_val = 0.0;
19✔
2809
  auto val_string = score + "_mean";
2810
  rval = mbi_->tag_get_handle(val_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
2811
    value_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
2812
  if (rval != moab::MB_SUCCESS) {
2813
    auto msg =
19✔
2814
      fmt::format("Could not create or retrieve the value tag for the score {}"
2815
                  " on unstructured mesh {}",
1,519,602✔
2816
        score, id_);
2817
    fatal_error(msg);
2818
  }
1,519,602✔
2819

2820
  // create the std dev tag if not present and get handle
2821
  moab::Tag error_tag;
1,519,602✔
2822
  std::string err_string = score + "_std_dev";
2823
  rval = mbi_->tag_get_handle(err_string.c_str(), 1, moab::MB_TYPE_DOUBLE,
2824
    error_tag, moab::MB_TAG_DENSE | moab::MB_TAG_CREAT, &default_val);
1,519,602✔
2825
  if (rval != moab::MB_SUCCESS) {
2826
    auto msg =
1,519,602✔
2827
      fmt::format("Could not create or retrieve the error tag for the score {}"
2828
                  " on unstructured mesh {}",
2829
        score, id_);
2830
    fatal_error(msg);
2831
  }
2832

1,519,602✔
2833
  // return the populated tag handles
2834
  return {value_tag, error_tag};
2835
}
1,519,602✔
2836

1,519,602✔
2837
void MOABMesh::add_score(const std::string& score)
2838
{
1,519,602✔
2839
  auto score_tags = get_score_tags(score);
2840
  tag_names_.push_back(score);
2841
}
1,519,602✔
2842

1,519,602✔
2843
void MOABMesh::remove_scores()
1,519,602✔
2844
{
1,519,602✔
2845
  for (const auto& name : tag_names_) {
2846
    auto value_name = name + "_mean";
1,519,602✔
2847
    moab::Tag tag;
1,519,602✔
2848
    moab::ErrorCode rval = mbi_->tag_get_handle(value_name.c_str(), tag);
701,483✔
2849
    if (rval != moab::MB_SUCCESS)
2850
      return;
1,519,602✔
2851

1,519,602✔
2852
    rval = mbi_->tag_delete(tag);
2853
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
2854
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
1,519,602✔
2855
                             " on unstructured mesh {}",
2856
        name, id_);
1,519,602✔
2857
      fatal_error(msg);
1,519,602✔
2858
    }
2859

2860
    auto std_dev_name = name + "_std_dev";
2861
    rval = mbi_->tag_get_handle(std_dev_name.c_str(), tag);
2862
    if (rval != moab::MB_SUCCESS) {
1,519,602✔
2863
      auto msg =
701,483✔
2864
        fmt::format("Std. Dev. mesh tag does not exist for the score {}"
701,483✔
2865
                    " on unstructured mesh {}",
701,483✔
2866
          name, id_);
223,515✔
2867
    }
223,515✔
2868

2869
    rval = mbi_->tag_delete(tag);
701,483✔
2870
    if (rval != moab::MB_SUCCESS) {
2871
      auto msg = fmt::format("Failed to delete mesh tag for the score {}"
2872
                             " on unstructured mesh {}",
2873
        name, id_);
2874
      fatal_error(msg);
818,119✔
2875
    }
818,119✔
2876
  }
5,506,248✔
2877
  tag_names_.clear();
2878
}
4,688,129✔
2879

4,688,129✔
2880
void MOABMesh::set_score_data(const std::string& score,
2881
  const vector<double>& values, const vector<double>& std_dev)
4,688,129✔
2882
{
2883
  auto score_tags = this->get_score_tags(score);
4,688,129✔
2884

2885
  moab::ErrorCode rval;
2886
  // set the score value
4,688,129✔
2887
  rval = mbi_->tag_set_data(score_tags.first, ehs_, values.data());
2888
  if (rval != moab::MB_SUCCESS) {
4,688,129✔
2889
    auto msg = fmt::format("Failed to set the tally value for score '{}' "
20,480✔
2890
                           "on unstructured mesh {}",
2891
      score, id_);
2892
    warning(msg);
4,667,649✔
2893
  }
4,667,649✔
2894

2895
  // set the error value
2896
  rval = mbi_->tag_set_data(score_tags.second, ehs_, std_dev.data());
2897
  if (rval != moab::MB_SUCCESS) {
2898
    auto msg = fmt::format("Failed to set the tally error for score '{}' "
2899
                           "on unstructured mesh {}",
818,119✔
2900
      score, id_);
818,119✔
2901
    warning(msg);
818,119✔
2902
  }
818,119✔
2903
}
818,119✔
2904

818,119✔
2905
void MOABMesh::write(const std::string& base_filename) const
762,819✔
2906
{
762,819✔
2907
  // add extension to the base name
2908
  auto filename = base_filename + ".vtk";
2909
  write_message(5, "Writing unstructured mesh {}...", filename);
1,519,602✔
2910
  filename = settings::path_output + filename;
2911

7,279,728✔
2912
  // write the tetrahedral elements of the mesh only
2913
  // to avoid clutter from zero-value data on other
7,279,728✔
2914
  // elements during visualization
2915
  moab::ErrorCode rval;
7,279,728✔
2916
  rval = mbi_->write_mesh(filename.c_str(), &tetset_, 1);
7,279,728✔
2917
  if (rval != moab::MB_SUCCESS) {
7,279,728✔
2918
    auto msg = fmt::format("Failed to write unstructured mesh {}", id_);
1,010,077✔
2919
    warning(msg);
2920
  }
2921
}
2922

6,269,651✔
2923
#endif
6,269,651✔
2924

6,269,651✔
2925
#ifdef LIBMESH
6,269,651✔
2926

2927
const std::string LibMesh::mesh_lib_type = "libmesh";
2928

2929
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node), adaptive_(false)
2930
{
259,367,091✔
2931
  // filename_ and length_multiplier_ will already be set by the
259,364,245✔
2932
  // UnstructuredMesh constructor
6,266,805✔
2933
  set_mesh_pointer_from_filename(filename_);
2934
  set_length_multiplier(length_multiplier_);
2935
  initialize();
2936
}
2937

2,846✔
2938
// create the mesh from a pointer to a libMesh Mesh
7,279,728✔
2939
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
2940
  : adaptive_(input_mesh.n_active_elem() != input_mesh.n_elem())
155,856✔
2941
{
2942
  if (!dynamic_cast<libMesh::ReplicatedMesh*>(&input_mesh)) {
155,856✔
2943
    fatal_error("At present LibMesh tallies require a replicated mesh. Please "
2944
                "ensure 'input_mesh' is a libMesh::ReplicatedMesh.");
2945
  }
30✔
2946

2947
  m_ = &input_mesh;
30✔
2948
  set_length_multiplier(length_multiplier);
2949
  initialize();
2950
}
2951

200,410✔
2952
// create the mesh from an input file
2953
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
2954
  : adaptive_(false)
200,410✔
2955
{
2956
  set_mesh_pointer_from_filename(filename);
2957
  set_length_multiplier(length_multiplier);
2958
  initialize();
2959
}
200,410✔
2960

200,410✔
2961
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
2962
{
2963
  filename_ = filename;
2964
  unique_m_ =
2965
    make_unique<libMesh::ReplicatedMesh>(*settings::libmesh_comm, n_dimension_);
1,002,050✔
2966
  m_ = unique_m_.get();
200,410✔
2967
  m_->read(filename_);
200,410✔
2968
}
2969

2970
// build a libMesh equation system for storing values
2971
void LibMesh::build_eqn_sys()
200,410✔
2972
{
1,002,050✔
2973
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
801,640✔
2974
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
2975
  libMesh::ExplicitSystem& eq_sys =
2976
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
400,820✔
2977
}
2978

2979
// intialize from mesh file
155,856✔
2980
void LibMesh::initialize()
2981
{
155,856✔
2982
  if (!settings::libmesh_comm) {
155,856✔
2983
    fatal_error(
155,856✔
2984
      "Attempting to use an unstructured mesh without a libMesh communicator.");
2985
  }
2986

2987
  // assuming that unstructured meshes used in OpenMC are 3D
779,280✔
2988
  n_dimension_ = 3;
155,856✔
2989

155,856✔
2990
  if (length_multiplier_ > 0.0) {
2991
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
2992
  }
2993
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
311,712✔
2994
  // Otherwise assume that it is prepared by its owning application
155,856✔
2995
  if (unique_m_) {
2996
    m_->prepare_for_use();
7,279,728✔
2997
  }
2998

7,279,728✔
2999
  // ensure that the loaded mesh is 3 dimensional
7,279,728✔
3000
  if (m_->mesh_dimension() != n_dimension_) {
1,012,923✔
3001
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
3002
                            "mesh is not a 3D mesh.",
6,266,805✔
3003
      filename_));
3004
  }
3005

3006
  for (int i = 0; i < num_threads(); i++) {
19✔
3007
    pl_.emplace_back(m_->sub_point_locator());
3008
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
3009
    pl_.back()->enable_out_of_mesh_mode();
3010
  }
19✔
3011

19✔
3012
  // store first element in the mesh to use as an offset for bin indices
3013
  auto first_elem = *m_->elements_begin();
3014
  first_element_id_ = first_elem->id();
3015

227,731✔
3016
  // if the mesh is adaptive elements aren't guaranteed by libMesh to be
227,712✔
3017
  // contiguous in ID space, so we need to map from bin indices (defined over
227,712✔
3018
  // active elements) to global dof ids
227,712✔
3019
  if (adaptive_) {
3020
    bin_to_elem_map_.reserve(m_->n_active_elem());
3021
    elem_to_bin_map_.resize(m_->n_elem(), -1);
3022
    for (auto it = m_->active_elements_begin(); it != m_->active_elements_end();
1,138,560✔
3023
         it++) {
227,712✔
3024
      auto elem = *it;
227,712✔
3025

3026
      bin_to_elem_map_.push_back(elem->id());
3027
      elem_to_bin_map_[elem->id()] = bin_to_elem_map_.size() - 1;
3028
    }
227,712✔
3029
  }
3030

3031
  // bounding box for the mesh for quick rejection checks
227,712✔
3032
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
227,712✔
3033
  libMesh::Point ll = bbox_.min();
227,712✔
3034
  libMesh::Point ur = bbox_.max();
19✔
3035
  lower_left_ = {ll(0), ll(1), ll(2)};
3036
  upper_right_ = {ur(0), ur(1), ur(2)};
259,364,245✔
3037
}
3038

3039
// Sample position within a tet for LibMesh type tets
3040
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
3041
{
3042
  const auto& elem = get_element_from_bin(bin);
3043
  // Get tet vertex coordinates from LibMesh
259,364,245✔
3044
  std::array<Position, 4> tet_verts;
259,364,245✔
3045
  for (int i = 0; i < elem.n_nodes(); i++) {
259,364,245✔
3046
    auto node_ref = elem.node_ref(i);
3047
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
3048
  }
3049
  // Samples position within tet using Barycentric coordinates
3050
  return this->sample_tet(tet_verts, seed);
3051
}
3052

259,364,245✔
3053
Position LibMesh::centroid(int bin) const
259,364,245✔
3054
{
259,364,245✔
3055
  const auto& elem = this->get_element_from_bin(bin);
3056
  auto centroid = elem.vertex_average();
3057
  return {centroid(0), centroid(1), centroid(2)};
3058
}
3059

3060
int LibMesh::n_vertices() const
3061
{
3062
  return m_->n_nodes();
259,364,245✔
3063
}
259,364,245✔
3064

3065
Position LibMesh::vertex(int vertex_id) const
259,364,245✔
3066
{
3067
  const auto node_ref = m_->node_ref(vertex_id);
420,046,390✔
3068
  return {node_ref(0), node_ref(1), node_ref(2)};
441,624,143✔
3069
}
280,941,998✔
3070

259,364,245✔
3071
std::vector<int> LibMesh::connectivity(int elem_id) const
3072
{
3073
  std::vector<int> conn;
3074
  const auto* elem_ptr = m_->elem_ptr(elem_id);
3075
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
3076
    conn.push_back(elem_ptr->node_id(i));
3077
  }
3078
  return conn;
3079
}
3080

3081
std::string LibMesh::library() const
3082
{
3083
  return mesh_lib_type;
3084
}
3085

3086
int LibMesh::n_bins() const
3087
{
3088
  return m_->n_active_elem();
3089
}
3090

3091
int LibMesh::n_surface_bins() const
3092
{
3093
  int n_bins = 0;
3094
  for (int i = 0; i < this->n_bins(); i++) {
3095
    const libMesh::Elem& e = get_element_from_bin(i);
3096
    n_bins += e.n_faces();
3097
    // if this is a boundary element, it will only be visited once,
3098
    // the number of surface bins is incremented to
3099
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
767,424✔
3100
      // null neighbor pointer indicates a boundary face
3101
      if (!neighbor_ptr) {
767,424✔
3102
        n_bins++;
767,424✔
3103
      }
3104
    }
3105
  }
3106
  return n_bins;
767,424✔
3107
}
3108

3109
void LibMesh::add_score(const std::string& var_name)
265,858,762✔
3110
{
3111
  if (adaptive_) {
265,858,762✔
3112
    warning(fmt::format(
265,858,762✔
3113
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3114
      this->id_));
3115

265,858,762✔
3116
    return;
3117
  }
3118

548,122✔
3119
  if (!equation_systems_) {
3120
    build_eqn_sys();
548,122✔
3121
  }
3122

3123
  // check if this is a new variable
548,122✔
3124
  std::string value_name = var_name + "_mean";
3125
  if (!variable_map_.count(value_name)) {
3126
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
266,598,805✔
3127
    auto var_num =
3128
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
266,598,805✔
3129
    variable_map_[value_name] = var_num;
3130
  }
3131

3132
  std::string std_dev_name = var_name + "_std_dev";
3133
  // check if this is a new variable
3134
  if (!variable_map_.count(std_dev_name)) {
3135
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3136
    auto var_num =
3137
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3138
    variable_map_[std_dev_name] = var_num;
3139
  }
3140
}
3141

3142
void LibMesh::remove_scores()
3143
{
3144
  if (equation_systems_) {
3145
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3146
    eqn_sys.clear();
3147
    variable_map_.clear();
3148
  }
3149
}
3150

3151
void LibMesh::set_score_data(const std::string& var_name,
3152
  const vector<double>& values, const vector<double>& std_dev)
3153
{
3154
  if (adaptive_) {
3155
    warning(fmt::format(
3156
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3157
      this->id_));
3158

3159
    return;
3160
  }
3161

3162
  if (!equation_systems_) {
3163
    build_eqn_sys();
3164
  }
3165

3166
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3167

3168
  if (!eqn_sys.is_initialized()) {
3169
    equation_systems_->init();
3170
  }
3171

3172
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3173

3174
  // look up the value variable
3175
  std::string value_name = var_name + "_mean";
3176
  unsigned int value_num = variable_map_.at(value_name);
795,427✔
3177
  // look up the std dev variable
3178
  std::string std_dev_name = var_name + "_std_dev";
795,427✔
3179
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
3180

3181
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
81,537✔
3182
       it++) {
3183
    if (!(*it)->active()) {
3184
      continue;
3185
    }
3186

81,537✔
3187
    auto bin = get_bin_from_element(*it);
3188

81,537✔
3189
    // set value
81,537✔
3190
    vector<libMesh::dof_id_type> value_dof_indices;
81,537✔
3191
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3192
    Ensures(value_dof_indices.size() == 1);
3193
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3194

163,074✔
3195
    // set std dev
3196
    vector<libMesh::dof_id_type> std_dev_dof_indices;
3197
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
191,856✔
3198
    Ensures(std_dev_dof_indices.size() == 1);
3199
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3200
  }
3201
}
191,856✔
3202

3203
void LibMesh::write(const std::string& filename) const
3204
{
191,856✔
3205
  if (adaptive_) {
191,856✔
3206
    warning(fmt::format(
191,856✔
3207
      "Exodus output cannot be provided as unstructured mesh {} is adaptive.",
3208
      this->id_));
3209

3210
    return;
3211
  }
191,856✔
3212

959,280✔
3213
  write_message(fmt::format(
767,424✔
3214
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
3215
  libMesh::ExodusII_IO exo(*m_);
3216
  std::set<std::string> systems_out = {eq_system_name_};
191,856✔
3217
  exo.write_discontinuous_exodusII(
191,856✔
3218
    filename + ".e", *equation_systems_, &systems_out);
3219
}
3220

3221
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3222
  vector<int>& bins, vector<double>& lengths) const
3223
{
3224
  // TODO: Implement triangle crossings here
3225
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3226
}
3227

3228
int LibMesh::get_bin(Position r) const
3229
{
3230
  // look-up a tet using the point locator
3231
  libMesh::Point p(r.x, r.y, r.z);
3232

3233
  // quick rejection check
3234
  if (!bbox_.contains_point(p)) {
3235
    return -1;
3236
  }
3237

3238
  const auto& point_locator = pl_.at(thread_num());
3239

3240
  const auto elem_ptr = (*point_locator)(p);
3241
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
3242
}
3243

3244
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
3245
{
3246
  int bin =
3247
    adaptive_ ? elem_to_bin_map_[elem->id()] : elem->id() - first_element_id_;
3248
  if (bin >= n_bins() || bin < 0) {
3249
    fatal_error(fmt::format("Invalid bin: {}", bin));
3250
  }
3251
  return bin;
3252
}
3253

3254
std::pair<vector<double>, vector<double>> LibMesh::plot(
3255
  Position plot_ll, Position plot_ur) const
3256
{
3257
  return {};
3258
}
3259

3260
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3261
{
3262
  return adaptive_ ? m_->elem_ref(bin_to_elem_map_.at(bin)) : m_->elem_ref(bin);
3263
}
3264

3265
double LibMesh::volume(int bin) const
3266
{
3267
  return this->get_element_from_bin(bin).volume();
3268
}
3269

3270
#endif // LIBMESH
3271

3272
//==============================================================================
3273
// Non-member functions
3274
//==============================================================================
3275

3276
void read_meshes(pugi::xml_node root)
3277
{
3278
  std::unordered_set<int> mesh_ids;
3279

3280
  for (auto node : root.children("mesh")) {
3281
    // Check to make sure multiple meshes in the same file don't share IDs
3282
    int id = std::stoi(get_node_value(node, "id"));
3283
    if (contains(mesh_ids, id)) {
3284
      fatal_error(fmt::format(
3285
        "Two or more meshes use the same unique ID '{}' in the same input file",
3286
        id));
3287
    }
3288
    mesh_ids.insert(id);
3289

3290
    // If we've already read a mesh with the same ID in a *different* file,
3291
    // assume it is the same here
3292
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3293
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3294
      continue;
3295
    }
3296

3297
    std::string mesh_type;
3298
    if (check_for_node(node, "type")) {
3299
      mesh_type = get_node_value(node, "type", true, true);
3300
    } else {
3301
      mesh_type = "regular";
3302
    }
3303

3304
    // determine the mesh library to use
3305
    std::string mesh_lib;
3306
    if (check_for_node(node, "library")) {
3307
      mesh_lib = get_node_value(node, "library", true, true);
3308
    }
3309

3310
    // Read mesh and add to vector
3311
    if (mesh_type == RegularMesh::mesh_type) {
3312
      model::meshes.push_back(make_unique<RegularMesh>(node));
3313
    } else if (mesh_type == RectilinearMesh::mesh_type) {
3314
      model::meshes.push_back(make_unique<RectilinearMesh>(node));
3315
    } else if (mesh_type == CylindricalMesh::mesh_type) {
3316
      model::meshes.push_back(make_unique<CylindricalMesh>(node));
3317
    } else if (mesh_type == SphericalMesh::mesh_type) {
3318
      model::meshes.push_back(make_unique<SphericalMesh>(node));
3319
#ifdef DAGMC
3320
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3321
               mesh_lib == MOABMesh::mesh_lib_type) {
3322
      model::meshes.push_back(make_unique<MOABMesh>(node));
3323
#endif
3324
#ifdef LIBMESH
3325
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3326
               mesh_lib == LibMesh::mesh_lib_type) {
3327
      model::meshes.push_back(make_unique<LibMesh>(node));
3328
#endif
3329
    } else if (mesh_type == UnstructuredMesh::mesh_type) {
3330
      fatal_error("Unstructured mesh support is not enabled or the mesh "
3331
                  "library is invalid.");
3332
    } else {
3333
      fatal_error("Invalid mesh type: " + mesh_type);
3334
    }
3335

3336
    // Map ID to position in vector
3337
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3338
  }
3339
}
3340

3341
void meshes_to_hdf5(hid_t group)
3342
{
3343
  // Write number of meshes
3344
  hid_t meshes_group = create_group(group, "meshes");
3345
  int32_t n_meshes = model::meshes.size();
3346
  write_attribute(meshes_group, "n_meshes", n_meshes);
3347

3348
  if (n_meshes > 0) {
3349
    // Write IDs of meshes
3350
    vector<int> ids;
23✔
3351
    for (const auto& m : model::meshes) {
3352
      m->to_hdf5(meshes_group);
3353
      ids.push_back(m->id_);
3354
    }
23✔
3355
    write_attribute(meshes_group, "ids", ids);
23✔
3356
  }
23✔
3357

23✔
3358
  close_group(meshes_group);
3359
}
3360

3361
void free_memory_mesh()
3362
{
3363
  model::meshes.clear();
3364
  model::mesh_map.clear();
3365
}
3366

3367
extern "C" int n_meshes()
3368
{
3369
  return model::meshes.size();
3370
}
3371

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

© 2025 Coveralls, Inc