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

openmc-dev / openmc / 10586562087

27 Aug 2024 10:05PM UTC coverage: 84.707% (-0.2%) from 84.9%
10586562087

Pull #3112

github

web-flow
Merge f7f32bf18 into 5bc04b5d7
Pull Request #3112: Revamp CI with dependency and Python caching for efficient installs

49553 of 58499 relevant lines covered (84.71%)

34324762.08 hits per line

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

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

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

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

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

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

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

51
namespace openmc {
52

53
//==============================================================================
54
// Global variables
55
//==============================================================================
56

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

63
namespace model {
64

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

68
} // namespace model
69

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

77
//==============================================================================
78
// Helper functions
79
//==============================================================================
80

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

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

104
//==============================================================================
105
// Mesh implementation
106
//==============================================================================
107

108
Mesh::Mesh(pugi::xml_node node)
2,742✔
109
{
110
  // Read mesh id
111
  id_ = std::stoi(get_node_value(node, "id"));
2,742✔
112
}
2,742✔
113

114
void Mesh::set_id(int32_t id)
1✔
115
{
116
  Expects(id >= 0 || id == C_NONE);
1✔
117

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

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

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

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

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

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

159
#pragma omp parallel
192✔
160
  {
161
    vector<int32_t> local_materials;
192✔
162
    vector<int64_t> local_hits;
192✔
163
    GeometryState geom;
192✔
164

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

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

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

179
      int i_material = geom.material();
25,802,964✔
180

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

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

196
  // Advance RNG seed
197
  advance_prn_seed(3 * n_sample, seed);
384✔
198

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

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

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

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

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

232
    // Otherwise, increase capacity of the vector
233
    result.reserve(2 * result.capacity());
×
234
  }
235

236
  return result;
×
237
}
×
238

239
//==============================================================================
240
// Structured Mesh implementation
241
//==============================================================================
242

243
std::string StructuredMesh::bin_label(int bin) const
5,437,248✔
244
{
245
  MeshIndex ijk = get_indices_from_bin(bin);
5,437,248✔
246

247
  if (n_dimension_ > 2) {
5,437,248✔
248
    return fmt::format("Mesh Index ({}, {}, {})", ijk[0], ijk[1], ijk[2]);
10,842,336✔
249
  } else if (n_dimension_ > 1) {
16,080✔
250
    return fmt::format("Mesh Index ({}, {})", ijk[0], ijk[1]);
31,560✔
251
  } else {
252
    return fmt::format("Mesh Index ({})", ijk[0]);
600✔
253
  }
254
}
255

256
xt::xtensor<int, 1> StructuredMesh::get_x_shape() const
2,970✔
257
{
258
  // because method is const, shape_ is const as well and can't be adapted
259
  auto tmp_shape = shape_;
2,970✔
260
  return xt::adapt(tmp_shape, {n_dimension_});
5,940✔
261
}
262

263
Position StructuredMesh::sample_element(
51,709,608✔
264
  const MeshIndex& ijk, uint64_t* seed) const
265
{
266
  // lookup the lower/upper bounds for the mesh element
267
  double x_min = negative_grid_boundary(ijk, 0);
51,709,608✔
268
  double x_max = positive_grid_boundary(ijk, 0);
51,709,608✔
269

270
  double y_min = (n_dimension_ >= 2) ? negative_grid_boundary(ijk, 1) : 0.0;
51,709,608✔
271
  double y_max = (n_dimension_ >= 2) ? positive_grid_boundary(ijk, 1) : 0.0;
51,709,608✔
272

273
  double z_min = (n_dimension_ == 3) ? negative_grid_boundary(ijk, 2) : 0.0;
51,709,608✔
274
  double z_max = (n_dimension_ == 3) ? positive_grid_boundary(ijk, 2) : 0.0;
51,709,608✔
275

276
  return {x_min + (x_max - x_min) * prn(seed),
51,709,608✔
277
    y_min + (y_max - y_min) * prn(seed), z_min + (z_max - z_min) * prn(seed)};
51,709,608✔
278
}
279

280
//==============================================================================
281
// Unstructured Mesh implementation
282
//==============================================================================
283

284
UnstructuredMesh::UnstructuredMesh(pugi::xml_node node) : Mesh(node)
42✔
285
{
286

287
  // check the mesh type
288
  if (check_for_node(node, "type")) {
42✔
289
    auto temp = get_node_value(node, "type", true, true);
42✔
290
    if (temp != mesh_type) {
42✔
291
      fatal_error(fmt::format("Invalid mesh type: {}", temp));
×
292
    }
293
  }
42✔
294

295
  // check if a length unit multiplier was specified
296
  if (check_for_node(node, "length_multiplier")) {
42✔
297
    length_multiplier_ = std::stod(get_node_value(node, "length_multiplier"));
×
298
  }
299

300
  // get the filename of the unstructured mesh to load
301
  if (check_for_node(node, "filename")) {
42✔
302
    filename_ = get_node_value(node, "filename");
42✔
303
    if (!file_exists(filename_)) {
42✔
304
      fatal_error("Mesh file '" + filename_ + "' does not exist!");
×
305
    }
306
  } else {
307
    fatal_error(fmt::format(
×
308
      "No filename supplied for unstructured mesh with ID: {}", id_));
×
309
  }
310

311
  if (check_for_node(node, "options")) {
42✔
312
    options_ = get_node_value(node, "options");
16✔
313
  }
314

315
  // check if mesh tally data should be written with
316
  // statepoint files
317
  if (check_for_node(node, "output")) {
42✔
318
    output_ = get_node_value_bool(node, "output");
×
319
  }
320
}
42✔
321

322
void UnstructuredMesh::determine_bounds()
22✔
323
{
324
  double xmin = INFTY;
22✔
325
  double ymin = INFTY;
22✔
326
  double zmin = INFTY;
22✔
327
  double xmax = -INFTY;
22✔
328
  double ymax = -INFTY;
22✔
329
  double zmax = -INFTY;
22✔
330
  int n = this->n_vertices();
22✔
331
  for (int i = 0; i < n; ++i) {
51,272✔
332
    auto v = this->vertex(i);
51,250✔
333
    xmin = std::min(v.x, xmin);
51,250✔
334
    ymin = std::min(v.y, ymin);
51,250✔
335
    zmin = std::min(v.z, zmin);
51,250✔
336
    xmax = std::max(v.x, xmax);
51,250✔
337
    ymax = std::max(v.y, ymax);
51,250✔
338
    zmax = std::max(v.z, zmax);
51,250✔
339
  }
340
  lower_left_ = {xmin, ymin, zmin};
22✔
341
  upper_right_ = {xmax, ymax, zmax};
22✔
342
}
22✔
343

344
Position UnstructuredMesh::sample_tet(
601,230✔
345
  std::array<Position, 4> coords, uint64_t* seed) const
346
{
347
  // Uniform distribution
348
  double s = prn(seed);
601,230✔
349
  double t = prn(seed);
601,230✔
350
  double u = prn(seed);
601,230✔
351

352
  // From PyNE implementation of moab tet sampling C. Rocchini & P. Cignoni
353
  // (2000) Generating Random Points in a Tetrahedron, Journal of Graphics
354
  // Tools, 5:4, 9-12, DOI: 10.1080/10867651.2000.10487528
355
  if (s + t > 1) {
601,230✔
356
    s = 1.0 - s;
300,861✔
357
    t = 1.0 - t;
300,861✔
358
  }
359
  if (s + t + u > 1) {
601,230✔
360
    if (t + u > 1) {
399,954✔
361
      double old_t = t;
200,208✔
362
      t = 1.0 - u;
200,208✔
363
      u = 1.0 - s - old_t;
200,208✔
364
    } else if (t + u <= 1) {
199,746✔
365
      double old_s = s;
199,746✔
366
      s = 1.0 - t - u;
199,746✔
367
      u = old_s + t + u - 1;
199,746✔
368
    }
369
  }
370
  return s * (coords[1] - coords[0]) + t * (coords[2] - coords[0]) +
1,202,460✔
371
         u * (coords[3] - coords[0]) + coords[0];
1,803,690✔
372
}
373

374
const std::string UnstructuredMesh::mesh_type = "unstructured";
375

376
std::string UnstructuredMesh::get_mesh_type() const
×
377
{
378
  return mesh_type;
×
379
}
380

381
void UnstructuredMesh::surface_bins_crossed(
×
382
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
383
{
384
  fatal_error("Unstructured mesh surface tallies are not implemented.");
×
385
}
386

387
std::string UnstructuredMesh::bin_label(int bin) const
193,712✔
388
{
389
  return fmt::format("Mesh Index ({})", bin);
387,424✔
390
};
391

392
void UnstructuredMesh::to_hdf5(hid_t group) const
27✔
393
{
394
  hid_t mesh_group = create_group(group, fmt::format("mesh {}", id_));
54✔
395

396
  write_dataset(mesh_group, "type", mesh_type);
27✔
397
  write_dataset(mesh_group, "filename", filename_);
27✔
398
  write_dataset(mesh_group, "library", this->library());
27✔
399
  if (!options_.empty()) {
27✔
400
    write_attribute(mesh_group, "options", options_);
8✔
401
  }
402

403
  if (length_multiplier_ > 0.0)
27✔
404
    write_dataset(mesh_group, "length_multiplier", length_multiplier_);
×
405

406
  // write vertex coordinates
407
  xt::xtensor<double, 2> vertices({static_cast<size_t>(this->n_vertices()), 3});
27✔
408
  for (int i = 0; i < this->n_vertices(); i++) {
60,932✔
409
    auto v = this->vertex(i);
60,905✔
410
    xt::view(vertices, i, xt::all()) = xt::xarray<double>({v.x, v.y, v.z});
60,905✔
411
  }
412
  write_dataset(mesh_group, "vertices", vertices);
27✔
413

414
  int num_elem_skipped = 0;
27✔
415

416
  // write element types and connectivity
417
  vector<double> volumes;
27✔
418
  xt::xtensor<int, 2> connectivity({static_cast<size_t>(this->n_bins()), 8});
27✔
419
  xt::xtensor<int, 2> elem_types({static_cast<size_t>(this->n_bins()), 1});
27✔
420
  for (int i = 0; i < this->n_bins(); i++) {
301,739✔
421
    auto conn = this->connectivity(i);
301,712✔
422

423
    volumes.emplace_back(this->volume(i));
301,712✔
424

425
    // write linear tet element
426
    if (conn.size() == 4) {
301,712✔
427
      xt::view(elem_types, i, xt::all()) =
599,424✔
428
        static_cast<int>(ElementType::LINEAR_TET);
599,424✔
429
      xt::view(connectivity, i, xt::all()) =
599,424✔
430
        xt::xarray<int>({conn[0], conn[1], conn[2], conn[3], -1, -1, -1, -1});
899,136✔
431
      // write linear hex element
432
    } else if (conn.size() == 8) {
2,000✔
433
      xt::view(elem_types, i, xt::all()) =
4,000✔
434
        static_cast<int>(ElementType::LINEAR_HEX);
4,000✔
435
      xt::view(connectivity, i, xt::all()) = xt::xarray<int>({conn[0], conn[1],
8,000✔
436
        conn[2], conn[3], conn[4], conn[5], conn[6], conn[7]});
6,000✔
437
    } else {
438
      num_elem_skipped++;
×
439
      xt::view(elem_types, i, xt::all()) =
×
440
        static_cast<int>(ElementType::UNSUPPORTED);
441
      xt::view(connectivity, i, xt::all()) = -1;
×
442
    }
443
  }
301,712✔
444

445
  // warn users that some elements were skipped
446
  if (num_elem_skipped > 0) {
27✔
447
    warning(fmt::format("The connectivity of {} elements "
×
448
                        "on mesh {} were not written "
449
                        "because they are not of type linear tet/hex.",
450
      num_elem_skipped, this->id_));
×
451
  }
452

453
  write_dataset(mesh_group, "volumes", volumes);
27✔
454
  write_dataset(mesh_group, "connectivity", connectivity);
27✔
455
  write_dataset(mesh_group, "element_types", elem_types);
27✔
456

457
  close_group(mesh_group);
27✔
458
}
27✔
459

460
void UnstructuredMesh::set_length_multiplier(double length_multiplier)
21✔
461
{
462
  length_multiplier_ = length_multiplier;
21✔
463
}
21✔
464

465
ElementType UnstructuredMesh::element_type(int bin) const
120,000✔
466
{
467
  auto conn = connectivity(bin);
120,000✔
468

469
  if (conn.size() == 4)
120,000✔
470
    return ElementType::LINEAR_TET;
120,000✔
471
  else if (conn.size() == 8)
×
472
    return ElementType::LINEAR_HEX;
×
473
  else
474
    return ElementType::UNSUPPORTED;
×
475
}
120,000✔
476

477
StructuredMesh::MeshIndex StructuredMesh::get_indices(
965,093,491✔
478
  Position r, bool& in_mesh) const
479
{
480
  MeshIndex ijk;
481
  in_mesh = true;
965,093,491✔
482
  for (int i = 0; i < n_dimension_; ++i) {
2,147,483,647✔
483
    ijk[i] = get_index_in_direction(r[i], i);
2,147,483,647✔
484

485
    if (ijk[i] < 1 || ijk[i] > shape_[i])
2,147,483,647✔
486
      in_mesh = false;
91,285,024✔
487
  }
488
  return ijk;
965,093,491✔
489
}
490

491
int StructuredMesh::get_bin_from_indices(const MeshIndex& ijk) const
1,014,634,340✔
492
{
493
  switch (n_dimension_) {
1,014,634,340✔
494
  case 1:
956,736✔
495
    return ijk[0] - 1;
956,736✔
496
  case 2:
21,561,048✔
497
    return (ijk[1] - 1) * shape_[0] + ijk[0] - 1;
21,561,048✔
498
  case 3:
992,116,556✔
499
    return ((ijk[2] - 1) * shape_[1] + (ijk[1] - 1)) * shape_[0] + ijk[0] - 1;
992,116,556✔
500
  default:
×
501
    throw std::runtime_error {"Invalid number of mesh dimensions"};
×
502
  }
503
}
504

505
StructuredMesh::MeshIndex StructuredMesh::get_indices_from_bin(int bin) const
60,386,904✔
506
{
507
  MeshIndex ijk;
508
  if (n_dimension_ == 1) {
60,386,904✔
509
    ijk[0] = bin + 1;
300✔
510
  } else if (n_dimension_ == 2) {
60,386,604✔
511
    ijk[0] = bin % shape_[0] + 1;
15,780✔
512
    ijk[1] = bin / shape_[0] + 1;
15,780✔
513
  } else if (n_dimension_ == 3) {
60,370,824✔
514
    ijk[0] = bin % shape_[0] + 1;
60,370,824✔
515
    ijk[1] = (bin % (shape_[0] * shape_[1])) / shape_[0] + 1;
60,370,824✔
516
    ijk[2] = bin / (shape_[0] * shape_[1]) + 1;
60,370,824✔
517
  }
518
  return ijk;
60,386,904✔
519
}
520

521
int StructuredMesh::get_bin(Position r) const
324,569,178✔
522
{
523
  // Determine indices
524
  bool in_mesh;
525
  MeshIndex ijk = get_indices(r, in_mesh);
324,569,178✔
526
  if (!in_mesh)
324,569,178✔
527
    return -1;
22,164,978✔
528

529
  // Convert indices to bin
530
  return get_bin_from_indices(ijk);
302,404,200✔
531
}
532

533
int StructuredMesh::n_bins() const
1,000,049✔
534
{
535
  return std::accumulate(
1,000,049✔
536
    shape_.begin(), shape_.begin() + n_dimension_, 1, std::multiplies<>());
2,000,098✔
537
}
538

539
int StructuredMesh::n_surface_bins() const
593✔
540
{
541
  return 4 * n_dimension_ * n_bins();
593✔
542
}
543

544
xt::xtensor<double, 1> StructuredMesh::count_sites(
×
545
  const SourceSite* bank, int64_t length, bool* outside) const
546
{
547
  // Determine shape of array for counts
548
  std::size_t m = this->n_bins();
×
549
  vector<std::size_t> shape = {m};
×
550

551
  // Create array of zeros
552
  xt::xarray<double> cnt {shape, 0.0};
×
553
  bool outside_ = false;
×
554

555
  for (int64_t i = 0; i < length; i++) {
×
556
    const auto& site = bank[i];
×
557

558
    // determine scoring bin for entropy mesh
559
    int mesh_bin = get_bin(site.r);
×
560

561
    // if outside mesh, skip particle
562
    if (mesh_bin < 0) {
×
563
      outside_ = true;
×
564
      continue;
×
565
    }
566

567
    // Add to appropriate bin
568
    cnt(mesh_bin) += site.wgt;
×
569
  }
570

571
  // Create copy of count data. Since ownership will be acquired by xtensor,
572
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
573
  // warnings.
574
  int total = cnt.size();
×
575
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
×
576

577
#ifdef OPENMC_MPI
578
  // collect values from all processors
579
  MPI_Reduce(
580
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
581

582
  // Check if there were sites outside the mesh for any processor
583
  if (outside) {
584
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
585
  }
586
#else
587
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
588
  if (outside)
589
    *outside = outside_;
590
#endif
591

592
  // Adapt reduced values in array back into an xarray
593
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
×
594
  xt::xarray<double> counts = arr;
×
595

596
  return counts;
×
597
}
598

599
// raytrace through the mesh. The template class T will do the tallying.
600
// A modern optimizing compiler can recognize the noop method of T and eleminate
601
// that call entirely.
602
template<class T>
603
void StructuredMesh::raytrace_mesh(
640,900,045✔
604
  Position r0, Position r1, const Direction& u, T tally) const
605
{
606
  // TODO: when c++-17 is available, use "if constexpr ()" to compile-time
607
  // enable/disable tally calls for now, T template type needs to provide both
608
  // surface and track methods, which might be empty. modern optimizing
609
  // compilers will (hopefully) eliminate the complete code (including
610
  // calculation of parameters) but for the future: be explicit
611

612
  // Compute the length of the entire track.
613
  double total_distance = (r1 - r0).norm();
640,900,045✔
614
  if (total_distance == 0.0 && settings::solver_type != SolverType::RANDOM_RAY)
640,900,045✔
615
    return;
5,711,856✔
616

617
  const int n = n_dimension_;
635,188,189✔
618

619
  // Flag if position is inside the mesh
620
  bool in_mesh;
621

622
  // Position is r = r0 + u * traveled_distance, start at r0
623
  double traveled_distance {0.0};
635,188,189✔
624

625
  // Calculate index of current cell. Offset the position a tiny bit in
626
  // direction of flight
627
  MeshIndex ijk = get_indices(r0 + TINY_BIT * u, in_mesh);
635,188,189✔
628

629
  // if track is very short, assume that it is completely inside one cell.
630
  // Only the current cell will score and no surfaces
631
  if (total_distance < 2 * TINY_BIT) {
635,188,189✔
632
    if (in_mesh) {
12✔
633
      tally.track(ijk, 1.0);
×
634
    }
635
    return;
12✔
636
  }
637

638
  // translate start and end positions,
639
  // this needs to come after the get_indices call because it does its own
640
  // translation
641
  local_coords(r0);
635,188,177✔
642
  local_coords(r1);
635,188,177✔
643

644
  // Calculate initial distances to next surfaces in all three dimensions
645
  std::array<MeshDistance, 3> distances;
1,270,376,354✔
646
  for (int k = 0; k < n; ++k) {
2,147,483,647✔
647
    distances[k] = distance_to_grid_boundary(ijk, k, r0, u, 0.0);
1,887,532,491✔
648
  }
649

650
  // Loop until r = r1 is eventually reached
651
  while (true) {
356,607,809✔
652

653
    if (in_mesh) {
991,795,986✔
654

655
      // find surface with minimal distance to current position
656
      const auto k = std::min_element(distances.begin(), distances.end()) -
900,588,929✔
657
                     distances.begin();
900,588,929✔
658

659
      // Tally track length delta since last step
660
      tally.track(ijk,
900,588,929✔
661
        (std::min(distances[k].distance, total_distance) - traveled_distance) /
900,588,929✔
662
          total_distance);
663

664
      // update position and leave, if we have reached end position
665
      traveled_distance = distances[k].distance;
900,588,929✔
666
      if (traveled_distance >= total_distance)
900,588,929✔
667
        return;
549,317,244✔
668

669
      // If we have not reached r1, we have hit a surface. Tally outward current
670
      tally.surface(ijk, k, distances[k].max_surface, false);
351,271,685✔
671

672
      // Update cell and calculate distance to next surface in k-direction.
673
      // The two other directions are still valid!
674
      ijk[k] = distances[k].next_index;
351,271,685✔
675
      distances[k] =
351,271,685✔
676
        distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
351,271,685✔
677

678
      // Check if we have left the interior of the mesh
679
      in_mesh = ((ijk[k] >= 1) && (ijk[k] <= shape_[k]));
351,271,685✔
680

681
      // If we are still inside the mesh, tally inward current for the next cell
682
      if (in_mesh)
351,271,685✔
683
        tally.surface(ijk, k, !distances[k].max_surface, true);
327,033,510✔
684

685
    } else { // not inside mesh
686

687
      // For all directions outside the mesh, find the distance that we need to
688
      // travel to reach the next surface. Use the largest distance, as only
689
      // this will cross all outer surfaces.
690
      int k_max {0};
91,207,057✔
691
      for (int k = 0; k < n; ++k) {
362,486,755✔
692
        if ((ijk[k] < 1 || ijk[k] > shape_[k]) &&
363,829,329✔
693
            (distances[k].distance > traveled_distance)) {
92,549,631✔
694
          traveled_distance = distances[k].distance;
91,763,871✔
695
          k_max = k;
91,763,871✔
696
        }
697
      }
698

699
      // If r1 is not inside the mesh, exit here
700
      if (traveled_distance >= total_distance)
91,207,057✔
701
        return;
85,870,933✔
702

703
      // Calculate the new cell index and update all distances to next surfaces.
704
      ijk = get_indices(r0 + (traveled_distance + TINY_BIT) * u, in_mesh);
5,336,124✔
705
      for (int k = 0; k < n; ++k) {
21,115,236✔
706
        distances[k] =
15,779,112✔
707
          distance_to_grid_boundary(ijk, k, r0, u, traveled_distance);
15,779,112✔
708
      }
709

710
      // If inside the mesh, Tally inward current
711
      if (in_mesh)
5,336,124✔
712
        tally.surface(ijk, k_max, !distances[k_max].max_surface, true);
3,959,064✔
713
    }
714
  }
715
}
716

250,709,558✔
717
void StructuredMesh::bins_crossed(Position r0, Position r1, const Direction& u,
718
  vector<int>& bins, vector<double>& lengths) const
719
{
720

721
  // Helper tally class.
722
  // stores a pointer to the mesh class and references to bins and lengths
723
  // parameters. Performs the actual tally through the track method.
724
  struct TrackAggregator {
725
    TrackAggregator(
726
      const StructuredMesh* _mesh, vector<int>& _bins, vector<double>& _lengths)
250,709,558✔
727
      : mesh(_mesh), bins(_bins), lengths(_lengths)
250,709,558✔
728
    {}
×
729
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const {}
730
    void track(const MeshIndex& ijk, double l) const
250,709,558✔
731
    {
732
      bins.push_back(mesh->get_bin_from_indices(ijk));
733
      lengths.push_back(l);
734
    }
735

736
    const StructuredMesh* mesh;
250,709,558✔
737
    vector<int>& bins;
738
    vector<double>& lengths;
739
  };
740

250,709,558✔
741
  // Perform the mesh raytrace with the helper class.
742
  raytrace_mesh(r0, r1, u, TrackAggregator(this, bins, lengths));
743
}
744

250,709,558✔
745
void StructuredMesh::surface_bins_crossed(
×
746
  Position r0, Position r1, const Direction& u, vector<int>& bins) const
×
747
{
748

×
749
  // Helper tally class.
750
  // stores a pointer to the mesh class and a reference to the bins parameter.
751
  // Performs the actual tally through the surface method.
752
  struct SurfaceAggregator {
753
    SurfaceAggregator(const StructuredMesh* _mesh, vector<int>& _bins)
754
      : mesh(_mesh), bins(_bins)
250,709,558✔
755
    {}
250,709,558✔
756
    void surface(const MeshIndex& ijk, int k, bool max, bool inward) const
757
    {
758
      int i_bin =
501,419,116✔
759
        4 * mesh->n_dimension_ * mesh->get_bin_from_indices(ijk) + 4 * k;
1,001,002,088✔
760
      if (max)
750,292,530✔
761
        i_bin += 2;
762
      if (inward)
763
        i_bin += 1;
764
      bins.push_back(i_bin);
61,870,061✔
765
    }
766
    void track(const MeshIndex& idx, double l) const {}
312,579,619✔
767

768
    const StructuredMesh* mesh;
769
    vector<int>& bins;
309,532,184✔
770
  };
309,532,184✔
771

772
  // Perform the mesh raytrace with the helper class.
773
  raytrace_mesh(r0, r1, u, SurfaceAggregator(this, bins));
309,532,184✔
774
}
309,532,184✔
775

776
//==============================================================================
777
// RegularMesh implementation
778
//==============================================================================
309,532,184✔
779

309,532,184✔
780
RegularMesh::RegularMesh(pugi::xml_node node) : StructuredMesh {node}
247,905,711✔
781
{
782
  // Determine number of dimensions for mesh
783
  if (!check_for_node(node, "dimension")) {
61,626,473✔
784
    fatal_error("Must specify <dimension> on a regular mesh.");
785
  }
786

787
  xt::xtensor<int, 1> shape = get_node_xarray<int>(node, "dimension");
61,626,473✔
788
  int n = n_dimension_ = shape.size();
61,626,473✔
789
  if (n != 1 && n != 2 && n != 3) {
61,626,473✔
790
    fatal_error("Mesh must be one, two, or three dimensions.");
791
  }
792
  std::copy(shape.begin(), shape.end(), shape_.begin());
61,626,473✔
793

794
  // Check that dimensions are all greater than zero
795
  if (xt::any(shape <= 0)) {
61,626,473✔
796
    fatal_error("All entries on the <dimension> element for a tally "
59,328,330✔
797
                "mesh must be positive.");
798
  }
799

800
  // Check for lower-left coordinates
801
  if (check_for_node(node, "lower_left")) {
802
    // Read mesh lower-left corner location
803
    lower_left_ = get_node_xarray<double>(node, "lower_left");
3,047,435✔
804
  } else {
11,838,440✔
805
    fatal_error("Must specify <lower_left> on a mesh.");
11,994,992✔
806
  }
3,203,987✔
807

3,139,523✔
808
  // Make sure lower_left and dimension match
3,139,523✔
809
  if (shape.size() != lower_left_.size()) {
810
    fatal_error("Number of entries on <lower_left> must be the same "
811
                "as the number of entries on <dimension>.");
812
  }
813

3,047,435✔
814
  if (check_for_node(node, "width")) {
2,803,847✔
815
    // Make sure one of upper-right or width were specified
816
    if (check_for_node(node, "upper_right")) {
817
      fatal_error("Cannot specify both <upper_right> and <width> on a mesh.");
243,588✔
818
    }
860,256✔
819

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

822
    // Check to ensure width has same dimensions
823
    auto n = width_.size();
824
    if (n != lower_left_.size()) {
243,588✔
825
      fatal_error("Number of entries on <width> must be the same as "
218,592✔
826
                  "the number of entries on <lower_left>.");
827
    }
828

829
    // Check for negative widths
390,190,487✔
830
    if (xt::any(width_ < 0.0)) {
831
      fatal_error("Cannot have a negative <width> on a tally mesh.");
832
    }
833

834
    // Set width and upper right coordinate
835
    upper_right_ = xt::eval(lower_left_ + shape * width_);
836

837
  } else if (check_for_node(node, "upper_right")) {
838
    upper_right_ = get_node_xarray<double>(node, "upper_right");
839

390,190,487✔
840
    // Check to ensure width has same dimensions
390,190,487✔
841
    auto n = upper_right_.size();
5,711,856✔
842
    if (n != lower_left_.size()) {
843
      fatal_error("Number of entries on <upper_right> must be the "
384,478,631✔
844
                  "same as the number of entries on <lower_left>.");
845
    }
846

847
    // Check that upper-right is above lower-left
848
    if (xt::any(upper_right_ < lower_left_)) {
849
      fatal_error("The <upper_right> coordinates must be greater than "
384,478,631✔
850
                  "the <lower_left> coordinates on a tally mesh.");
851
    }
852

853
    // Set width
384,478,631✔
854
    width_ = xt::eval((upper_right_ - lower_left_) / shape);
855
  } else {
856
    fatal_error("Must specify either <upper_right> or <width> on a mesh.");
857
  }
384,478,631✔
858

12✔
859
  // Set material volumes
×
860
  volume_frac_ = 1.0 / xt::prod(shape)();
861

12✔
862
  element_volume_ = 1.0;
863
  for (int i = 0; i < n_dimension_; i++) {
864
    element_volume_ *= width_[i];
865
  }
866
}
867

384,478,619✔
868
int RegularMesh::get_index_in_direction(double r, int i) const
384,478,619✔
869
{
870
  return std::ceil((r - lower_left_[i]) / width_[i]);
871
}
768,957,238✔
872

1,521,718,580✔
873
const std::string RegularMesh::mesh_type = "regular";
1,137,239,961✔
874

875
std::string RegularMesh::get_mesh_type() const
876
{
877
  return mesh_type;
294,737,748✔
878
}
879

679,216,367✔
880
double RegularMesh::positive_grid_boundary(const MeshIndex& ijk, int i) const
881
{
882
  return lower_left_[i] + ijk[i] * width_[i];
591,056,745✔
883
}
591,056,745✔
884

885
double RegularMesh::negative_grid_boundary(const MeshIndex& ijk, int i) const
886
{
591,056,745✔
887
  return lower_left_[i] + (ijk[i] - 1) * width_[i];
591,056,745✔
888
}
889

890
StructuredMesh::MeshDistance RegularMesh::distance_to_grid_boundary(
891
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
591,056,745✔
892
  double l) const
591,056,745✔
893
{
301,411,533✔
894
  MeshDistance d;
895
  d.next_index = ijk[i];
896
  if (std::abs(u[i]) < FP_PRECISION)
289,645,212✔
897
    return d;
898

899
  d.max_surface = (u[i] > 0);
900
  if (d.max_surface && (ijk[i] <= shape_[i])) {
289,645,212✔
901
    d.next_index++;
289,645,212✔
902
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
289,645,212✔
903
  } else if (!d.max_surface && (ijk[i] >= 1)) {
904
    d.next_index--;
905
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
289,645,212✔
906
  }
907
  return d;
908
}
289,645,212✔
909

267,705,180✔
910
std::pair<vector<double>, vector<double>> RegularMesh::plot(
911
  Position plot_ll, Position plot_ur) const
912
{
913
  // Figure out which axes lie in the plane of the plot.
914
  array<int, 2> axes {-1, -1};
915
  if (plot_ur.z == plot_ll.z) {
916
    axes[0] = 0;
88,159,622✔
917
    if (n_dimension_ > 1)
350,648,315✔
918
      axes[1] = 1;
351,834,337✔
919
  } else if (plot_ur.y == plot_ll.y) {
89,345,644✔
920
    axes[0] = 0;
88,624,348✔
921
    if (n_dimension_ > 2)
88,624,348✔
922
      axes[1] = 2;
923
  } else if (plot_ur.x == plot_ll.x) {
924
    if (n_dimension_ > 1)
925
      axes[0] = 1;
926
    if (n_dimension_ > 2)
88,159,622✔
927
      axes[1] = 2;
83,067,086✔
928
  } else {
929
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
930
  }
5,092,536✔
931

20,254,980✔
932
  // Get the coordinates of the mesh lines along both of the axes.
15,162,444✔
933
  array<vector<double>, 2> axis_lines;
15,162,444✔
934
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
935
    int axis = axes[i_ax];
936
    if (axis == -1)
937
      continue;
5,092,536✔
938
    auto& lines {axis_lines[i_ax]};
3,740,472✔
939

940
    double coord = lower_left_[axis];
941
    for (int i = 0; i < shape_[axis] + 1; ++i) {
942
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
943
        lines.push_back(coord);
390,190,487✔
944
      coord += width_[axis];
945
    }
946
  }
947

948
  return {axis_lines[0], axis_lines[1]};
949
}
950

951
void RegularMesh::to_hdf5(hid_t group) const
390,190,487✔
952
{
953
  hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_));
390,190,487✔
954

390,190,487✔
955
  write_dataset(mesh_group, "type", "regular");
561,090,864✔
956
  write_dataset(mesh_group, "dimension", get_x_shape());
591,056,745✔
957
  write_dataset(mesh_group, "lower_left", lower_left_);
958
  write_dataset(mesh_group, "upper_right", upper_right_);
591,056,745✔
959
  write_dataset(mesh_group, "width", width_);
591,056,745✔
960

591,056,745✔
961
  close_group(mesh_group);
962
}
963

964
xt::xtensor<double, 1> RegularMesh::count_sites(
965
  const SourceSite* bank, int64_t length, bool* outside) const
966
{
967
  // Determine shape of array for counts
968
  std::size_t m = this->n_bins();
390,190,487✔
969
  vector<std::size_t> shape = {m};
390,190,487✔
970

971
  // Create array of zeros
250,709,558✔
972
  xt::xarray<double> cnt {shape, 0.0};
973
  bool outside_ = false;
974

975
  for (int64_t i = 0; i < length; i++) {
976
    const auto& site = bank[i];
977

978
    // determine scoring bin for entropy mesh
979
    int mesh_bin = get_bin(site.r);
250,709,558✔
980

250,709,558✔
981
    // if outside mesh, skip particle
250,709,558✔
982
    if (mesh_bin < 0) {
121,173,395✔
983
      outside_ = true;
984
      continue;
985
    }
121,173,395✔
986

121,173,395✔
987
    // Add to appropriate bin
60,541,998✔
988
    cnt(mesh_bin) += site.wgt;
121,173,395✔
989
  }
59,546,922✔
990

121,173,395✔
991
  // Create copy of count data. Since ownership will be acquired by xtensor,
121,173,395✔
992
  // std::allocator must be used to avoid Valgrind mismatched free() / delete
309,532,184✔
993
  // warnings.
994
  int total = cnt.size();
995
  double* cnt_reduced = std::allocator<double> {}.allocate(total);
996

997
#ifdef OPENMC_MPI
998
  // collect values from all processors
999
  MPI_Reduce(
250,709,558✔
1000
    cnt.data(), cnt_reduced, total, MPI_DOUBLE, MPI_SUM, 0, mpi::intracomm);
250,709,558✔
1001

1002
  // Check if there were sites outside the mesh for any processor
1003
  if (outside) {
1004
    MPI_Reduce(&outside_, outside, 1, MPI_C_BOOL, MPI_LOR, 0, mpi::intracomm);
1005
  }
1006
#else
1,871✔
1007
  std::copy(cnt.data(), cnt.data() + total, cnt_reduced);
1008
  if (outside)
1009
    *outside = outside_;
1,871✔
1010
#endif
×
1011

1012
  // Adapt reduced values in array back into an xarray
1013
  auto arr = xt::adapt(cnt_reduced, total, xt::acquire_ownership(), shape);
1,871✔
1014
  xt::xarray<double> counts = arr;
1,871✔
1015

1,871✔
1016
  return counts;
×
1017
}
1018

1,871✔
1019
double RegularMesh::volume(const MeshIndex& ijk) const
1020
{
1021
  return element_volume_;
1,871✔
1022
}
×
1023

1024
//==============================================================================
1025
// RectilinearMesh implementation
1026
//==============================================================================
1027

1,871✔
1028
RectilinearMesh::RectilinearMesh(pugi::xml_node node) : StructuredMesh {node}
1029
{
1,871✔
1030
  n_dimension_ = 3;
1031

×
1032
  grid_[0] = get_node_array<double>(node, "x_grid");
1033
  grid_[1] = get_node_array<double>(node, "y_grid");
1034
  grid_[2] = get_node_array<double>(node, "z_grid");
1035

1,871✔
1036
  if (int err = set_grid()) {
×
1037
    fatal_error(openmc_err_msg);
1038
  }
1039
}
1040

1,871✔
1041
const std::string RectilinearMesh::mesh_type = "rectilinear";
1042

51✔
1043
std::string RectilinearMesh::get_mesh_type() const
×
1044
{
1045
  return mesh_type;
1046
}
51✔
1047

1048
double RectilinearMesh::positive_grid_boundary(
1049
  const MeshIndex& ijk, int i) const
51✔
1050
{
51✔
1051
  return grid_[i][ijk[i]];
×
1052
}
1053

1054
double RectilinearMesh::negative_grid_boundary(
1055
  const MeshIndex& ijk, int i) const
1056
{
51✔
1057
  return grid_[i][ijk[i] - 1];
×
1058
}
1059

1060
StructuredMesh::MeshDistance RectilinearMesh::distance_to_grid_boundary(
1061
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
51✔
1062
  double l) const
1063
{
1,820✔
1064
  MeshDistance d;
1,820✔
1065
  d.next_index = ijk[i];
1066
  if (std::abs(u[i]) < FP_PRECISION)
1067
    return d;
1,820✔
1068

1,820✔
1069
  d.max_surface = (u[i] > 0);
×
1070
  if (d.max_surface && (ijk[i] <= shape_[i])) {
1071
    d.next_index++;
1072
    d.distance = (positive_grid_boundary(ijk, i) - r0[i]) / u[i];
1073
  } else if (!d.max_surface && (ijk[i] > 0)) {
1074
    d.next_index--;
1,820✔
1075
    d.distance = (negative_grid_boundary(ijk, i) - r0[i]) / u[i];
×
1076
  }
1077
  return d;
1078
}
1079

1080
int RectilinearMesh::set_grid()
1,820✔
1081
{
1082
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
×
1083
    static_cast<int>(grid_[1].size()) - 1,
1084
    static_cast<int>(grid_[2].size()) - 1};
1085

1086
  for (const auto& g : grid_) {
1,871✔
1087
    if (g.size() < 2) {
1088
      set_errmsg("x-, y-, and z- grids for rectilinear meshes "
1,871✔
1089
                 "must each have at least 2 points");
7,234✔
1090
      return OPENMC_E_INVALID_ARGUMENT;
5,363✔
1091
    }
1092
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
1,871✔
1093
        g.end()) {
1094
      set_errmsg("Values in for x-, y-, and z- grids for "
2,147,483,647✔
1095
                 "rectilinear meshes must be sorted and unique.");
1096
      return OPENMC_E_INVALID_ARGUMENT;
2,147,483,647✔
1097
    }
1098
  }
1099

1100
  lower_left_ = {grid_[0].front(), grid_[1].front(), grid_[2].front()};
1101
  upper_right_ = {grid_[0].back(), grid_[1].back(), grid_[2].back()};
1,699✔
1102

1103
  return 0;
1,699✔
1104
}
1105

1106
int RectilinearMesh::get_index_in_direction(double r, int i) const
960,402,640✔
1107
{
1108
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
960,402,640✔
1109
}
1110

1111
std::pair<vector<double>, vector<double>> RectilinearMesh::plot(
959,983,922✔
1112
  Position plot_ll, Position plot_ur) const
1113
{
959,983,922✔
1114
  // Figure out which axes lie in the plane of the plot.
1115
  array<int, 2> axes {-1, -1};
1116
  if (plot_ur.z == plot_ll.z) {
1,636,116,224✔
1117
    axes = {0, 1};
1118
  } else if (plot_ur.y == plot_ll.y) {
1119
    axes = {0, 2};
1120
  } else if (plot_ur.x == plot_ll.x) {
1,636,116,224✔
1121
    axes = {1, 2};
1,636,116,224✔
1122
  } else {
1,636,116,224✔
1123
    fatal_error("Can only plot mesh lines on an axis-aligned plot");
×
1124
  }
1125

1,636,116,224✔
1126
  // Get the coordinates of the mesh lines along both of the axes.
1,636,116,224✔
1127
  array<vector<double>, 2> axis_lines;
806,713,816✔
1128
  for (int i_ax = 0; i_ax < 2; ++i_ax) {
806,713,816✔
1129
    int axis = axes[i_ax];
829,402,408✔
1130
    vector<double>& lines {axis_lines[i_ax]};
806,295,098✔
1131

806,295,098✔
1132
    for (auto coord : grid_[axis]) {
1133
      if (coord >= plot_ll[axis] && coord <= plot_ur[axis])
1,636,116,224✔
1134
        lines.push_back(coord);
1135
    }
1136
  }
24✔
1137

1138
  return {axis_lines[0], axis_lines[1]};
1139
}
1140

24✔
1141
void RectilinearMesh::to_hdf5(hid_t group) const
24✔
1142
{
24✔
1143
  hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_));
24✔
1144

24✔
1145
  write_dataset(mesh_group, "type", "rectilinear");
×
1146
  write_dataset(mesh_group, "x_grid", grid_[0]);
×
1147
  write_dataset(mesh_group, "y_grid", grid_[1]);
×
1148
  write_dataset(mesh_group, "z_grid", grid_[2]);
×
1149

×
1150
  close_group(mesh_group);
×
1151
}
×
1152

×
1153
double RectilinearMesh::volume(const MeshIndex& ijk) const
×
1154
{
1155
  double vol {1.0};
×
1156

1157
  for (int i = 0; i < n_dimension_; i++) {
1158
    vol *= grid_[i][ijk[i]] - grid_[i][ijk[i] - 1];
1159
  }
24✔
1160
  return vol;
72✔
1161
}
48✔
1162

48✔
1163
//==============================================================================
×
1164
// CylindricalMesh implementation
48✔
1165
//==============================================================================
1166

48✔
1167
CylindricalMesh::CylindricalMesh(pugi::xml_node node)
312✔
1168
  : PeriodicStructuredMesh {node}
264✔
1169
{
264✔
1170
  n_dimension_ = 3;
264✔
1171
  grid_[0] = get_node_array<double>(node, "r_grid");
1172
  grid_[1] = get_node_array<double>(node, "phi_grid");
1173
  grid_[2] = get_node_array<double>(node, "z_grid");
1174
  origin_ = get_node_position(node, "origin");
48✔
1175

24✔
1176
  if (int err = set_grid()) {
1177
    fatal_error(openmc_err_msg);
2,152✔
1178
  }
1179
}
2,152✔
1180

1181
const std::string CylindricalMesh::mesh_type = "cylindrical";
2,152✔
1182

2,152✔
1183
std::string CylindricalMesh::get_mesh_type() const
2,152✔
1184
{
2,152✔
1185
  return mesh_type;
2,152✔
1186
}
1187

2,152✔
1188
StructuredMesh::MeshIndex CylindricalMesh::get_indices(
2,152✔
1189
  Position r, bool& in_mesh) const
1190
{
12,248✔
1191
  local_coords(r);
1192

1193
  Position mapped_r;
1194
  mapped_r[0] = std::hypot(r.x, r.y);
12,248✔
1195
  mapped_r[2] = r[2];
12,248✔
1196

1197
  if (mapped_r[0] < FP_PRECISION) {
1198
    mapped_r[1] = 0.0;
12,248✔
1199
  } else {
12,248✔
1200
    mapped_r[1] = std::atan2(r.y, r.x);
1201
    if (mapped_r[1] < 0)
12,019,257✔
1202
      mapped_r[1] += 2 * M_PI;
12,007,009✔
1203
  }
1204

1205
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
12,007,009✔
1206

1207
  idx[1] = sanitize_phi(idx[1]);
1208

12,007,009✔
1209
  return idx;
×
1210
}
×
1211

1212
Position CylindricalMesh::sample_element(
1213
  const MeshIndex& ijk, uint64_t* seed) const
1214
{
12,007,009✔
1215
  double r_min = this->r(ijk[0] - 1);
1216
  double r_max = this->r(ijk[0]);
1217

1218
  double phi_min = this->phi(ijk[1] - 1);
1219
  double phi_max = this->phi(ijk[1]);
1220

12,248✔
1221
  double z_min = this->z(ijk[2] - 1);
12,248✔
1222
  double z_max = this->z(ijk[2]);
1223

1224
  double r_min_sq = r_min * r_min;
1225
  double r_max_sq = r_max * r_max;
7,320✔
1226
  double r = std::sqrt(uniform_distribution(r_min_sq, r_max_sq, seed));
7,320✔
1227
  double phi = uniform_distribution(phi_min, phi_max, seed);
1228
  double z = uniform_distribution(z_min, z_max, seed);
1229

7,320✔
1230
  double x = r * std::cos(phi);
7,320✔
1231
  double y = r * std::sin(phi);
1232

1233
  return origin_ + Position(x, y, z);
4,928✔
1234
}
4,928✔
1235

4,928✔
1236
double CylindricalMesh::find_r_crossing(
1237
  const Position& r, const Direction& u, double l, int shell) const
1238
{
1239

12,248✔
1240
  if ((shell < 0) || (shell > shape_[0]))
12,248✔
1241
    return INFTY;
1242

24,496✔
1243
  // solve r.x^2 + r.y^2 == r0^2
12,248✔
1244
  // x^2 + 2*s*u*x + s^2*u^2 + s^2*v^2+2*s*v*y + y^2 -r0^2 = 0
1245
  // s^2 * (u^2 + v^2) + 2*s*(u*x+v*y) + x^2+y^2-r0^2 = 0
982,884✔
1246

1247
  const double r0 = grid_[0][shell];
982,884✔
1248
  if (r0 == 0.0)
1249
    return INFTY;
1250

1251
  const double denominator = u.x * u.x + u.y * u.y;
1252

1253
  // Direction of flight is in z-direction. Will never intersect r.
1254
  if (std::abs(denominator) < FP_PRECISION)
111✔
1255
    return INFTY;
1256

111✔
1257
  // inverse of dominator to help the compiler to speed things up
1258
  const double inv_denominator = 1.0 / denominator;
111✔
1259

111✔
1260
  const double p = (u.x * r.x + u.y * r.y) * inv_denominator;
111✔
1261
  double c = r.x * r.x + r.y * r.y - r0 * r0;
1262
  double D = p * p - c * inv_denominator;
111✔
1263

×
1264
  if (D < 0.0)
1265
    return INFTY;
111✔
1266

1267
  D = std::sqrt(D);
1268

1269
  // the solution -p - D is always smaller as -p + D : Check this one first
206✔
1270
  if (std::abs(c) <= RADIAL_MESH_TOL)
1271
    return INFTY;
206✔
1272

1273
  if (-p - D > l)
1274
    return -p - D;
56,980,318✔
1275
  if (-p + D > l)
1276
    return -p + D;
1277

56,980,318✔
1278
  return INFTY;
1279
}
1280

56,518,367✔
1281
double CylindricalMesh::find_phi_crossing(
1282
  const Position& r, const Direction& u, double l, int shell) const
1283
{
56,518,367✔
1284
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
1285
  if (full_phi_ && (shape_[1] == 1))
1286
    return INFTY;
111,639,732✔
1287

1288
  shell = sanitize_phi(shell);
1289

1290
  const double p0 = grid_[1][shell];
111,639,732✔
1291

111,639,732✔
1292
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
111,639,732✔
1293
  // => x(s) * cos(p0) = y(s) * sin(p0)
×
1294
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
1295
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
111,639,732✔
1296

111,639,732✔
1297
  const double c0 = std::cos(p0);
55,540,318✔
1298
  const double s0 = std::sin(p0);
55,540,318✔
1299

56,099,414✔
1300
  const double denominator = (u.x * s0 - u.y * c0);
55,078,367✔
1301

55,078,367✔
1302
  // Check if direction of flight is not parallel to phi surface
1303
  if (std::abs(denominator) > FP_PRECISION) {
111,639,732✔
1304
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1305
    // Check if solution is in positive direction of flight and crosses the
1306
    // correct phi surface (not -phi)
185✔
1307
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
1308
      return s;
185✔
1309
  }
185✔
1310

185✔
1311
  return INFTY;
1312
}
740✔
1313

555✔
1314
StructuredMesh::MeshDistance CylindricalMesh::find_z_crossing(
×
1315
  const Position& r, const Direction& u, double l, int shell) const
1316
{
×
1317
  MeshDistance d;
1318
  d.next_index = shell;
555✔
1319

1,110✔
1320
  // Direction of flight is within xy-plane. Will never intersect z.
×
1321
  if (std::abs(u.z) < FP_PRECISION)
1322
    return d;
×
1323

1324
  d.max_surface = (u.z > 0.0);
1325
  if (d.max_surface && (shell <= shape_[2])) {
1326
    d.next_index += 1;
185✔
1327
    d.distance = (grid_[2][shell] - r.z) / u.z;
185✔
1328
  } else if (!d.max_surface && (shell > 0)) {
1329
    d.next_index -= 1;
185✔
1330
    d.distance = (grid_[2][shell - 1] - r.z) / u.z;
1331
  }
1332
  return d;
160,387,890✔
1333
}
1334

160,387,890✔
1335
StructuredMesh::MeshDistance CylindricalMesh::distance_to_grid_boundary(
1336
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
1337
  double l) const
12✔
1338
{
1339
  Position r = r0 - origin_;
1340

1341
  if (i == 0) {
12✔
1342

12✔
1343
    return std::min(
×
1344
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r, u, l, ijk[i])),
12✔
1345
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r, u, l, ijk[i] - 1)));
12✔
1346

×
1347
  } else if (i == 1) {
×
1348

1349
    return std::min(MeshDistance(sanitize_phi(ijk[i] + 1), true,
×
1350
                      find_phi_crossing(r, u, l, ijk[i])),
1351
      MeshDistance(sanitize_phi(ijk[i] - 1), false,
1352
        find_phi_crossing(r, u, l, ijk[i] - 1)));
1353

12✔
1354
  } else {
36✔
1355
    return find_z_crossing(r, u, l, ijk[i]);
24✔
1356
  }
24✔
1357
}
1358

120✔
1359
int CylindricalMesh::set_grid()
96✔
1360
{
96✔
1361
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
1362
    static_cast<int>(grid_[1].size()) - 1,
1363
    static_cast<int>(grid_[2].size()) - 1};
1364

24✔
1365
  for (const auto& g : grid_) {
12✔
1366
    if (g.size() < 2) {
1367
      set_errmsg("r-, phi-, and z- grids for cylindrical meshes "
122✔
1368
                 "must each have at least 2 points");
1369
      return OPENMC_E_INVALID_ARGUMENT;
122✔
1370
    }
1371
    if (std::adjacent_find(g.begin(), g.end(), std::greater_equal<>()) !=
122✔
1372
        g.end()) {
122✔
1373
      set_errmsg("Values in for r-, phi-, and z- grids for "
122✔
1374
                 "cylindrical meshes must be sorted and unique.");
122✔
1375
      return OPENMC_E_INVALID_ARGUMENT;
1376
    }
122✔
1377
  }
122✔
1378
  if (grid_[0].front() < 0.0) {
1379
    set_errmsg("r-grid for "
228✔
1380
               "cylindrical meshes must start at r >= 0.");
1381
    return OPENMC_E_INVALID_ARGUMENT;
228✔
1382
  }
1383
  if (grid_[1].front() < 0.0) {
912✔
1384
    set_errmsg("phi-grid for "
684✔
1385
               "cylindrical meshes must start at phi >= 0.");
1386
    return OPENMC_E_INVALID_ARGUMENT;
228✔
1387
  }
1388
  if (grid_[1].back() > 2.0 * PI) {
1389
    set_errmsg("phi-grids for "
1390
               "cylindrical meshes must end with theta <= 2*pi.");
1391

1392
    return OPENMC_E_INVALID_ARGUMENT;
1393
  }
401✔
1394

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

401✔
1397
  lower_left_ = {origin_[0] - grid_[0].back(), origin_[1] - grid_[0].back(),
401✔
1398
    origin_[2] + grid_[2].front()};
401✔
1399
  upper_right_ = {origin_[0] + grid_[0].back(), origin_[1] + grid_[0].back(),
401✔
1400
    origin_[2] + grid_[2].back()};
401✔
1401

1402
  return 0;
401✔
1403
}
×
1404

1405
int CylindricalMesh::get_index_in_direction(double r, int i) const
401✔
1406
{
1407
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1408
}
1409

108✔
1410
std::pair<vector<double>, vector<double>> CylindricalMesh::plot(
1411
  Position plot_ll, Position plot_ur) const
108✔
1412
{
1413
  fatal_error("Plot of cylindrical Mesh not implemented");
1414

49,788,192✔
1415
  // Figure out which axes lie in the plane of the plot.
1416
  array<vector<double>, 2> axis_lines;
1417
  return {axis_lines[0], axis_lines[1]};
49,788,192✔
1418
}
1419

49,788,192✔
1420
void CylindricalMesh::to_hdf5(hid_t group) const
49,788,192✔
1421
{
49,788,192✔
1422
  hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_));
1423

49,788,192✔
1424
  write_dataset(mesh_group, "type", "cylindrical");
×
1425
  write_dataset(mesh_group, "r_grid", grid_[0]);
1426
  write_dataset(mesh_group, "phi_grid", grid_[1]);
49,788,192✔
1427
  write_dataset(mesh_group, "z_grid", grid_[2]);
49,788,192✔
1428
  write_dataset(mesh_group, "origin", origin_);
24,869,868✔
1429

1430
  close_group(mesh_group);
1431
}
49,788,192✔
1432

1433
double CylindricalMesh::volume(const MeshIndex& ijk) const
49,788,192✔
1434
{
1435
  double r_i = grid_[0][ijk[0] - 1];
49,788,192✔
1436
  double r_o = grid_[0][ijk[0]];
1437

1438
  double phi_i = grid_[1][ijk[1] - 1];
816,000✔
1439
  double phi_o = grid_[1][ijk[1]];
1440

1441
  double z_i = grid_[2][ijk[2] - 1];
816,000✔
1442
  double z_o = grid_[2][ijk[2]];
816,000✔
1443

1444
  return 0.5 * (r_o * r_o - r_i * r_i) * (phi_o - phi_i) * (z_o - z_i);
816,000✔
1445
}
816,000✔
1446

1447
//==============================================================================
816,000✔
1448
// SphericalMesh implementation
816,000✔
1449
//==============================================================================
1450

816,000✔
1451
SphericalMesh::SphericalMesh(pugi::xml_node node)
816,000✔
1452
  : PeriodicStructuredMesh {node}
816,000✔
1453
{
816,000✔
1454
  n_dimension_ = 3;
816,000✔
1455

1456
  grid_[0] = get_node_array<double>(node, "r_grid");
816,000✔
1457
  grid_[1] = get_node_array<double>(node, "theta_grid");
816,000✔
1458
  grid_[2] = get_node_array<double>(node, "phi_grid");
1459
  origin_ = get_node_position(node, "origin");
816,000✔
1460

1461
  if (int err = set_grid()) {
1462
    fatal_error(openmc_err_msg);
146,628,600✔
1463
  }
1464
}
1465

1466
const std::string SphericalMesh::mesh_type = "spherical";
146,628,600✔
1467

17,850,072✔
1468
std::string SphericalMesh::get_mesh_type() const
1469
{
1470
  return mesh_type;
1471
}
1472

1473
StructuredMesh::MeshIndex SphericalMesh::get_indices(
128,778,528✔
1474
  Position r, bool& in_mesh) const
128,778,528✔
1475
{
7,044,084✔
1476
  local_coords(r);
1477

121,734,444✔
1478
  Position mapped_r;
1479
  mapped_r[0] = r.norm();
1480

121,734,444✔
1481
  if (mapped_r[0] < FP_PRECISION) {
×
1482
    mapped_r[1] = 0.0;
1483
    mapped_r[2] = 0.0;
1484
  } else {
121,734,444✔
1485
    mapped_r[1] = std::acos(r.z / mapped_r.x);
1486
    mapped_r[2] = std::atan2(r.y, r.x);
121,734,444✔
1487
    if (mapped_r[2] < 0)
121,734,444✔
1488
      mapped_r[2] += 2 * M_PI;
121,734,444✔
1489
  }
1490

121,734,444✔
1491
  MeshIndex idx = StructuredMesh::get_indices(mapped_r, in_mesh);
16,859,556✔
1492

1493
  idx[1] = sanitize_theta(idx[1]);
104,874,888✔
1494
  idx[2] = sanitize_phi(idx[2]);
1495

1496
  return idx;
104,874,888✔
1497
}
7,057,536✔
1498

1499
Position SphericalMesh::sample_element(
97,817,352✔
1500
  const MeshIndex& ijk, uint64_t* seed) const
19,920,936✔
1501
{
77,896,416✔
1502
  double r_min = this->r(ijk[0] - 1);
47,239,464✔
1503
  double r_max = this->r(ijk[0]);
1504

30,656,952✔
1505
  double theta_min = this->theta(ijk[1] - 1);
1506
  double theta_max = this->theta(ijk[1]);
1507

72,236,472✔
1508
  double phi_min = this->phi(ijk[2] - 1);
1509
  double phi_max = this->phi(ijk[2]);
1510

1511
  double cos_theta = uniform_distribution(theta_min, theta_max, seed);
72,236,472✔
1512
  double sin_theta = std::sin(std::acos(cos_theta));
32,466,432✔
1513
  double phi = uniform_distribution(phi_min, phi_max, seed);
1514
  double r_min_cub = std::pow(r_min, 3);
39,770,040✔
1515
  double r_max_cub = std::pow(r_max, 3);
1516
  // might be faster to do rejection here?
39,770,040✔
1517
  double r = std::cbrt(uniform_distribution(r_min_cub, r_max_cub, seed));
1518

1519
  double x = r * std::cos(phi) * sin_theta;
1520
  double y = r * std::sin(phi) * sin_theta;
1521
  double z = r * cos_theta;
1522

1523
  return origin_ + Position(x, y, z);
39,770,040✔
1524
}
39,770,040✔
1525

1526
double SphericalMesh::find_r_crossing(
39,770,040✔
1527
  const Position& r, const Direction& u, double l, int shell) const
1528
{
1529
  if ((shell < 0) || (shell > shape_[0]))
39,770,040✔
1530
    return INFTY;
39,770,040✔
1531

1532
  // solve |r+s*u| = r0
1533
  // |r+s*u| = |r| + 2*s*r*u + s^2 (|u|==1 !)
39,770,040✔
1534
  const double r0 = grid_[0][shell];
18,739,656✔
1535
  if (r0 == 0.0)
1536
    return INFTY;
1537
  const double p = r.dot(u);
21,030,384✔
1538
  double c = r.dot(r) - r0 * r0;
1539
  double D = p * p - c;
1540

39,212,208✔
1541
  if (std::abs(c) <= RADIAL_MESH_TOL)
1542
    return INFTY;
1543

39,212,208✔
1544
  if (D >= 0.0) {
39,212,208✔
1545
    D = std::sqrt(D);
1546
    // the solution -p - D is always smaller as -p + D : Check this one first
1547
    if (-p - D > l)
39,212,208✔
1548
      return -p - D;
480,000✔
1549
    if (-p + D > l)
1550
      return -p + D;
38,732,208✔
1551
  }
38,732,208✔
1552

17,729,088✔
1553
  return INFTY;
17,729,088✔
1554
}
21,003,120✔
1555

18,037,548✔
1556
double SphericalMesh::find_theta_crossing(
18,037,548✔
1557
  const Position& r, const Direction& u, double l, int shell) const
1558
{
38,732,208✔
1559
  // Theta grid is [0, π], thus there is no real surface to cross
1560
  if (full_theta_ && (shape_[1] == 1))
1561
    return INFTY;
148,644,744✔
1562

1563
  shell = sanitize_theta(shell);
1564

1565
  // solving z(s) = cos/theta) * r(s) with r(s) = r+s*u
148,644,744✔
1566
  // yields
1567
  // a*s^2 + 2*b*s + c == 0 with
148,644,744✔
1568
  // a = cos(theta)^2 - u.z * u.z
1569
  // b = r*u * cos(theta)^2 - u.z * r.z
73,314,300✔
1570
  // c = r*r * cos(theta)^2 - r.z^2
73,314,300✔
1571

146,628,600✔
1572
  const double cos_t = std::cos(grid_[1][shell]);
1573
  const bool sgn = std::signbit(cos_t);
75,330,444✔
1574
  const double cos_t_2 = cos_t * cos_t;
1575

36,118,236✔
1576
  const double a = cos_t_2 - u.z * u.z;
36,118,236✔
1577
  const double b = r.dot(u) * cos_t_2 - r.z * u.z;
36,118,236✔
1578
  const double c = r.dot(r) * cos_t_2 - r.z * r.z;
72,236,472✔
1579

1580
  // if factor of s^2 is zero, direction of flight is parallel to theta surface
1581
  if (std::abs(a) < FP_PRECISION) {
39,212,208✔
1582
    // if b vanishes, direction of flight is within theta surface and crossing
1583
    // is not possible
1584
    if (std::abs(b) < FP_PRECISION)
1585
      return INFTY;
425✔
1586

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

1,700✔
1592
    // no crossing is possible
1,275✔
1593
    return INFTY;
×
1594
  }
1595

×
1596
  const double p = b / a;
1597
  double D = p * p - c / a;
1,275✔
1598

2,550✔
1599
  if (D < 0.0)
×
1600
    return INFTY;
1601

×
1602
  D = std::sqrt(D);
1603

1604
  // the solution -p-D is always smaller as -p+D : Check this one first
425✔
1605
  double s = -p - D;
×
1606
  // Check if solution is in positive direction of flight and has correct sign
1607
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
1608
    return s;
1609

425✔
1610
  s = -p + D;
×
1611
  // Check if solution is in positive direction of flight and has correct sign
1612
  if ((s > l) && (std::signbit(r.z + s * u.z) == sgn))
×
1613
    return s;
1614

425✔
1615
  return INFTY;
×
1616
}
1617

1618
double SphericalMesh::find_phi_crossing(
×
1619
  const Position& r, const Direction& u, double l, int shell) const
1620
{
1621
  // Phi grid is [0, 2Ï€], thus there is no real surface to cross
425✔
1622
  if (full_phi_ && (shape_[2] == 1))
1623
    return INFTY;
850✔
1624

850✔
1625
  shell = sanitize_phi(shell);
850✔
1626

850✔
1627
  const double p0 = grid_[2][shell];
1628

425✔
1629
  // solve y(s)/x(s) = tan(p0) = sin(p0)/cos(p0)
1630
  // => x(s) * cos(p0) = y(s) * sin(p0)
1631
  // => (y + s * v) * cos(p0) = (x + s * u) * sin(p0)
149,364,576✔
1632
  // = s * (v * cos(p0) - u * sin(p0)) = - (y * cos(p0) - x * sin(p0))
1633

149,364,576✔
1634
  const double c0 = std::cos(p0);
1635
  const double s0 = std::sin(p0);
1636

×
1637
  const double denominator = (u.x * s0 - u.y * c0);
1638

1639
  // Check if direction of flight is not parallel to phi surface
×
1640
  if (std::abs(denominator) > FP_PRECISION) {
1641
    const double s = -(r.x * s0 - r.y * c0) / denominator;
1642
    // Check if solution is in positive direction of flight and crosses the
1643
    // correct phi surface (not -phi)
1644
    if ((s > l) && ((c0 * (r.x + s * u.x) + s0 * (r.y + s * u.y)) > 0.0))
1645
      return s;
1646
  }
396✔
1647

1648
  return INFTY;
396✔
1649
}
1650

396✔
1651
StructuredMesh::MeshDistance SphericalMesh::distance_to_grid_boundary(
396✔
1652
  const MeshIndex& ijk, int i, const Position& r0, const Direction& u,
396✔
1653
  double l) const
396✔
1654
{
396✔
1655

1656
  if (i == 0) {
396✔
1657
    return std::min(
396✔
1658
      MeshDistance(ijk[i] + 1, true, find_r_crossing(r0, u, l, ijk[i])),
1659
      MeshDistance(ijk[i] - 1, false, find_r_crossing(r0, u, l, ijk[i] - 1)));
336✔
1660

1661
  } else if (i == 1) {
336✔
1662
    return std::min(MeshDistance(sanitize_theta(ijk[i] + 1), true,
336✔
1663
                      find_theta_crossing(r0, u, l, ijk[i])),
1664
      MeshDistance(sanitize_theta(ijk[i] - 1), false,
336✔
1665
        find_theta_crossing(r0, u, l, ijk[i] - 1)));
336✔
1666

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

1675
int SphericalMesh::set_grid()
1676
{
1677
  shape_ = {static_cast<int>(grid_[0].size()) - 1,
317✔
1678
    static_cast<int>(grid_[1].size()) - 1,
317✔
1679
    static_cast<int>(grid_[2].size()) - 1};
1680

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

73,258,044✔
1703
    return OPENMC_E_INVALID_ARGUMENT;
1704
  }
73,258,044✔
1705
  if (grid_[2].back() > 2 * PI) {
73,258,044✔
1706
    set_errmsg("phi-grids for "
1707
               "spherical meshes must end with phi <= 2*pi.");
73,258,044✔
1708
    return OPENMC_E_INVALID_ARGUMENT;
×
1709
  }
×
1710

1711
  full_theta_ = (grid_[1].front() == 0.0) && (grid_[1].back() == PI);
73,258,044✔
1712
  full_phi_ = (grid_[2].front() == 0.0) && (grid_[2].back() == 2 * PI);
73,258,044✔
1713

73,258,044✔
1714
  double r = grid_[0].back();
36,598,944✔
1715
  lower_left_ = {origin_[0] - r, origin_[1] - r, origin_[2] - r};
1716
  upper_right_ = {origin_[0] + r, origin_[1] + r, origin_[2] + r};
1717

73,258,044✔
1718
  return 0;
1719
}
73,258,044✔
1720

73,258,044✔
1721
int SphericalMesh::get_index_in_direction(double r, int i) const
1722
{
73,258,044✔
1723
  return lower_bound_index(grid_[i].begin(), grid_[i].end(), r) + 1;
1724
}
1725

1,440,000✔
1726
std::pair<vector<double>, vector<double>> SphericalMesh::plot(
1727
  Position plot_ll, Position plot_ur) const
1728
{
1,440,000✔
1729
  fatal_error("Plot of spherical Mesh not implemented");
1,440,000✔
1730

1731
  // Figure out which axes lie in the plane of the plot.
1,440,000✔
1732
  array<vector<double>, 2> axis_lines;
1,440,000✔
1733
  return {axis_lines[0], axis_lines[1]};
1734
}
1,440,000✔
1735

1,440,000✔
1736
void SphericalMesh::to_hdf5(hid_t group) const
1737
{
1,440,000✔
1738
  hid_t mesh_group = create_group(group, "mesh " + std::to_string(id_));
1,440,000✔
1739

1,440,000✔
1740
  write_dataset(mesh_group, "type", SphericalMesh::mesh_type);
1,440,000✔
1741
  write_dataset(mesh_group, "r_grid", grid_[0]);
1,440,000✔
1742
  write_dataset(mesh_group, "theta_grid", grid_[1]);
1743
  write_dataset(mesh_group, "phi_grid", grid_[2]);
1,440,000✔
1744
  write_dataset(mesh_group, "origin", origin_);
1745

1,440,000✔
1746
  close_group(mesh_group);
1,440,000✔
1747
}
1,440,000✔
1748

1749
double SphericalMesh::volume(const MeshIndex& ijk) const
1,440,000✔
1750
{
1751
  double r_i = grid_[0][ijk[0] - 1];
1752
  double r_o = grid_[0][ijk[0]];
480,342,048✔
1753

1754
  double theta_i = grid_[1][ijk[1] - 1];
1755
  double theta_o = grid_[1][ijk[1]];
480,342,048✔
1756

43,677,828✔
1757
  double phi_i = grid_[2][ijk[2] - 1];
1758
  double phi_o = grid_[2][ijk[2]];
1759

1760
  return (1.0 / 3.0) * (r_o * r_o * r_o - r_i * r_i * r_i) *
436,664,220✔
1761
         (std::cos(theta_i) - std::cos(theta_o)) * (phi_o - phi_i);
436,664,220✔
1762
}
7,577,856✔
1763

429,086,364✔
1764
//==============================================================================
429,086,364✔
1765
// Helper functions for the C API
429,086,364✔
1766
//==============================================================================
1767

429,086,364✔
1768
int check_mesh(int32_t index)
11,548,560✔
1769
{
1770
  if (index < 0 || index >= model::meshes.size()) {
417,537,804✔
1771
    set_errmsg("Index in meshes array is out of bounds.");
388,160,352✔
1772
    return OPENMC_E_OUT_OF_BOUNDS;
1773
  }
388,160,352✔
1774
  return 0;
69,316,164✔
1775
}
318,844,188✔
1776

192,088,092✔
1777
template<class T>
1778
int check_mesh_type(int32_t index)
1779
{
156,133,548✔
1780
  if (int err = check_mesh(index))
1781
    return err;
1782

117,164,688✔
1783
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1784
  if (!mesh) {
1785
    set_errmsg("This function is not valid for input mesh.");
1786
    return OPENMC_E_INVALID_TYPE;
117,164,688✔
1787
  }
76,498,272✔
1788
  return 0;
1789
}
40,666,416✔
1790

1791
template<class T>
1792
bool is_mesh_type(int32_t index)
1793
{
1794
  T* mesh = dynamic_cast<T*>(model::meshes[index].get());
1795
  return mesh;
1796
}
1797

1798
//==============================================================================
40,666,416✔
1799
// C API functions
40,666,416✔
1800
//==============================================================================
40,666,416✔
1801

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

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

1810
  return 0;
×
1811
}
×
1812

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

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

23,605,992✔
1837
  return 0;
1838
}
23,605,992✔
1839

11,064,504✔
1840
//! Adds a new unstructured mesh to OpenMC
1841
extern "C" int openmc_add_unstructured_mesh(
12,541,488✔
1842
  const char filename[], const char library[], int* id)
1843
{
1844
  std::string lib_name(library);
118,858,440✔
1845
  std::string mesh_file(filename);
1846
  bool valid_lib = false;
1847

1848
#ifdef DAGMC
118,858,440✔
1849
  if (lib_name == MOABMesh::mesh_lib_type) {
76,498,272✔
1850
    model::meshes.push_back(std::move(make_unique<MOABMesh>(mesh_file)));
1851
    valid_lib = true;
42,360,168✔
1852
  }
1853
#endif
42,360,168✔
1854

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

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

1869
  // auto-assign new ID
1870
  model::meshes.back()->set_id(-1);
42,360,168✔
1871
  *id = model::meshes.back()->id_;
18,707,712✔
1872

1873
  return 0;
1874
}
23,652,456✔
1875

1876
//! Return the index in the meshes array of a mesh with a given ID
1877
extern "C" int openmc_get_mesh_index(int32_t id, int32_t* index)
358,182,588✔
1878
{
1879
  auto pair = model::mesh_map.find(id);
1880
  if (pair == model::mesh_map.end()) {
1881
    set_errmsg("No mesh exists with ID=" + std::to_string(id) + ".");
1882
    return OPENMC_E_INVALID_ID;
358,182,588✔
1883
  }
240,171,024✔
1884
  *index = pair->second;
240,171,024✔
1885
  return 0;
480,342,048✔
1886
}
1887

118,011,564✔
1888
//! Return the ID of a mesh
58,582,344✔
1889
extern "C" int openmc_mesh_get_id(int32_t index, int32_t* id)
58,582,344✔
1890
{
58,582,344✔
1891
  if (int err = check_mesh(index))
117,164,688✔
1892
    return err;
1893
  *id = model::meshes[index]->id_;
1894
  return 0;
59,429,220✔
1895
}
59,429,220✔
1896

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

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

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

×
1927
//! Get the bounding box of a mesh
1928
extern "C" int openmc_mesh_bounding_box(int32_t index, double* ll, double* ur)
1929
{
×
1930
  if (int err = check_mesh(index))
1931
    return err;
341✔
1932

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

×
1935
  // set lower left corner values
1936
  ll[0] = bbox.xmin;
1937
  ll[1] = bbox.ymin;
341✔
1938
  ll[2] = bbox.zmin;
341✔
1939

1940
  // set upper right corner values
341✔
1941
  ur[0] = bbox.xmax;
341✔
1942
  ur[1] = bbox.ymax;
341✔
1943
  ur[2] = bbox.zmax;
1944
  return 0;
341✔
1945
}
1946

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

×
1956
  if (int err = check_mesh(index))
1957
    return err;
1958

1959
  int n = model::meshes[index]->material_volumes(
1960
    n_sample, bin, {result_, result_ + result_size}, seed);
1961
  *hits = n;
1962
  return (n == -1) ? OPENMC_E_ALLOCATE : 0;
312✔
1963
}
1964

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

1972
  int pixel_width = pixels[0];
312✔
1973
  int pixel_height = pixels[1];
312✔
1974

1975
  // get pixel size
600✔
1976
  double in_pixel = (width[0]) / static_cast<double>(pixel_width);
1977
  double out_pixel = (width[1]) / static_cast<double>(pixel_height);
600✔
1978

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

9,376✔
2001
  // set initial position
2002
  xyz[in_i] = origin[in_i] - width[0] / 2. + in_pixel / 2.;
2003
  xyz[out_i] = origin[out_i] + width[1] / 2. - out_pixel / 2.;
2004

1,759✔
2005
#pragma omp parallel
2006
  {
1,759✔
2007
    Position r = xyz;
×
2008

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

156✔
2019
  return 0;
×
2020
}
2021

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

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

2042
  // Copy dimension
256✔
2043
  mesh->n_dimension_ = n;
×
2044
  std::copy(dims, dims + n, mesh->shape_.begin());
2045
  return 0;
256✔
2046
}
256✔
2047

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

×
2056
  if (m->lower_left_.dimension() == 0) {
2057
    set_errmsg("Mesh parameters have not been set.");
1,191✔
2058
    return OPENMC_E_ALLOCATE;
1,191✔
2059
  }
×
2060

×
2061
  *ll = m->lower_left_.data();
2062
  *ur = m->upper_right_.data();
1,191✔
2063
  *width = m->width_.data();
2064
  *n = m->n_dimension_;
2065
  return 0;
2066
}
2067

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

2076
  if (m->n_dimension_ == -1) {
2077
    set_errmsg("Need to set mesh dimension before setting parameters.");
2,073✔
2078
    return OPENMC_E_UNASSIGNED;
2079
  }
2,073✔
2080

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

122✔
2099
  // Set material volumes
74✔
2100

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

471✔
2109
  return 0;
×
2110
}
2111

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

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

2123
  m->n_dimension_ = 3;
2124

2125
  m->grid_[0].reserve(nx);
2126
  m->grid_[1].reserve(ny);
2127
  m->grid_[2].reserve(nz);
2128

2129
  for (int i = 0; i < nx; i++) {
2130
    m->grid_[0].push_back(grid_x[i]);
2131
  }
2132
  for (int i = 0; i < ny; i++) {
2133
    m->grid_[1].push_back(grid_y[i]);
2134
  }
2135
  for (int i = 0; i < nz; i++) {
2136
    m->grid_[2].push_back(grid_z[i]);
×
2137
  }
×
2138

2139
  int err = m->set_grid();
2140
  return err;
×
2141
}
2142

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

663✔
2152
  if (m->lower_left_.dimension() == 0) {
2153
    set_errmsg("Mesh parameters have not been set.");
663✔
2154
    return OPENMC_E_ALLOCATE;
663✔
2155
  }
×
2156

×
2157
  *grid_x = m->grid_[0].data();
2158
  *nx = m->grid_[0].size();
663✔
2159
  *grid_y = m->grid_[1].data();
663✔
2160
  *ny = m->grid_[1].size();
2161
  *grid_z = m->grid_[2].data();
2162
  *nz = m->grid_[2].size();
2163

4,305✔
2164
  return 0;
2165
}
4,305✔
2166

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

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

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

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

2201
//! Get the spherical mesh grid
2202
extern "C" int openmc_spherical_mesh_get_grid(int32_t index, double** grid_x,
48✔
2203
  int* nx, double** grid_y, int* ny, double** grid_z, int* nz)
2204
{
48✔
2205

×
2206
  return openmc_structured_mesh_get_grid_impl<SphericalMesh>(
2207
    index, grid_x, nx, grid_y, ny, grid_z, nz);
48✔
2208
  ;
2209
}
2210

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

2220
#ifdef DAGMC
2221

384✔
2222
const std::string MOABMesh::mesh_lib_type = "moab";
2223

2224
MOABMesh::MOABMesh(pugi::xml_node node) : UnstructuredMesh(node)
384✔
2225
{
384✔
2226
  initialize();
×
2227
}
×
2228

2229
MOABMesh::MOABMesh(const std::string& filename, double length_multiplier)
2230
{
384✔
2231
  filename_ = filename;
×
2232
  set_length_multiplier(length_multiplier);
2233
  initialize();
768✔
2234
}
384✔
2235

384✔
2236
MOABMesh::MOABMesh(std::shared_ptr<moab::Interface> external_mbi)
384✔
2237
{
2238
  mbi_ = external_mbi;
2239
  filename_ = "unknown (external file)";
48✔
2240
  this->initialize();
2241
}
2242

48✔
2243
void MOABMesh::initialize()
×
2244
{
48✔
2245

2246
  // Create the MOAB interface and load data from file
48✔
2247
  this->create_interface();
48✔
2248

2249
  // Initialise MOAB error code
2250
  moab::ErrorCode rval = moab::MB_SUCCESS;
48✔
2251

48✔
2252
  // Set the dimension
2253
  n_dimension_ = 3;
2254

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

48✔
2261
  if (!ehs_.all_of_type(moab::MBTET)) {
48✔
2262
    warning("Non-tetrahedral elements found in unstructured "
48✔
2263
            "mesh file: " +
×
2264
            filename_);
×
2265
  }
×
2266

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

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

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

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

×
2314
  // Determine bounds of mesh
373✔
2315
  this->determine_bounds();
2316
}
2317

373✔
2318
void MOABMesh::prepare_for_tallies()
373✔
2319
{
373✔
2320
  // if the KDTree has already been constructed, do nothing
2321
  if (kdtree_)
2322
    return;
2323

397✔
2324
  // build acceleration data structures
2325
  compute_barycentric_data(ehs_);
2326
  build_kdtree(ehs_);
397✔
2327
}
×
2328

397✔
2329
void MOABMesh::create_interface()
2330
{
397✔
2331
  // Do not create a MOAB instance if one is already in memory
×
2332
  if (mbi_)
×
2333
    return;
2334

2335
  // create MOAB instance
397✔
2336
  mbi_ = std::make_shared<moab::Core>();
397✔
2337

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

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

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

12✔
2362
  // combine into one range
12✔
2363
  moab::Range all_tets_and_tris;
12✔
2364
  all_tets_and_tris.merge(all_tets);
12✔
2365
  all_tets_and_tris.merge(all_tris);
12✔
2366

12✔
2367
  // create a kd-tree instance
12✔
2368
  write_message("Building adaptive k-d tree for tet mesh...", 7);
2369
  kdtree_ = make_unique<moab::AdaptiveKDTree>(mbi_.get());
×
2370

×
2371
  // Determine what options to use
2372
  std::ostringstream options_stream;
2373
  if (options_.empty()) {
2374
    options_stream << "MAX_DEPTH=20;PLANE_SET=2;";
2375
  } else {
2376
    options_stream << options_;
2377
  }
409✔
2378
  moab::FileOptions file_opts(options_stream.str().c_str());
409✔
2379

1,636✔
2380
  // Build the k-d tree
1,227✔
2381
  rval = kdtree_->build_tree(all_tets_and_tris, &kdtree_root_, &file_opts);
2382
  if (rval != moab::MB_SUCCESS) {
2383
    fatal_error("Failed to construct KDTree for the "
409✔
2384
                "unstructured mesh file: " +
409✔
2385
                filename_);
2386
  }
2387
}
2388

122✔
2389
void MOABMesh::intersect_track(const moab::CartVect& start,
2390
  const moab::CartVect& dir, double track_len, vector<double>& hits) const
2391
{
2392
  hits.clear();
122✔
2393

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

866✔
2405
  // remove duplicate intersection distances
2406
  std::unique(hits.begin(), hits.end());
450✔
2407

328✔
2408
  // sorts by first component of std::pair by default
2409
  std::sort(hits.begin(), hits.end());
426✔
2410
}
304✔
2411

2412
void MOABMesh::bins_crossed(Position r0, Position r1, const Direction& u,
2413
  vector<int>& bins, vector<double>& lengths) const
122✔
2414
{
122✔
2415
  moab::CartVect start(r0.x, r0.y, r0.z);
2416
  moab::CartVect end(r1.x, r1.y, r1.z);
24✔
2417
  moab::CartVect dir(u.x, u.y, u.z);
2418
  dir.normalize();
2419

2420
  double track_len = (end - start).length();
24✔
2421
  if (track_len == 0.0)
×
2422
    return;
2423

24✔
2424
  start -= TINY_BIT * dir;
2425
  end += TINY_BIT * dir;
24✔
2426

2427
  vector<double> hits;
24✔
2428
  intersect_track(start, dir, track_len, hits);
24✔
2429

24✔
2430
  bins.clear();
2431
  lengths.clear();
96✔
2432

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

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

2459
    // determine the start point for this segment
96✔
2460
    current = r0 + u * hit;
72✔
2461

2462
    if (bin == -1) {
108✔
2463
      continue;
84✔
2464
    }
2465

84✔
2466
    bins.push_back(bin);
60✔
2467
    lengths.push_back(segment_length / track_len);
2468
  }
2469

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

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

160✔
2495
  // retrieve the tet elements of this leaf
2496
  moab::EntityHandle leaf = kdtree_iter.handle();
2497
  moab::Range tets;
74✔
2498
  rval = mbi_->get_entities_by_dimension(leaf, 3, tets, false);
74✔
2499
  if (rval != moab::MB_SUCCESS) {
2500
    warning("MOAB error finding tets.");
2501
  }
2502

2503
  // loop over the tets in this leaf, returning the containing tet if found
446✔
2504
  for (const auto& tet : tets) {
2505
    if (point_in_tet(pos, tet)) {
2506
      return tet;
446✔
2507
    }
×
2508
  }
446✔
2509

2510
  // if no tet is found, return an invalid handle
446✔
2511
  return 0;
×
2512
}
×
2513

2514
double MOABMesh::volume(int bin) const
2515
{
446✔
2516
  return tet_volume(get_ent_handle_from_bin(bin));
446✔
2517
}
446✔
2518

446✔
2519
std::string MOABMesh::library() const
446✔
2520
{
446✔
2521
  return mesh_lib_type;
2522
}
446✔
2523

2524
// Sample position within a tet for MOAB type tets
132✔
2525
Position MOABMesh::sample_element(int32_t bin, uint64_t* seed) const
2526
{
2527

132✔
2528
  moab::EntityHandle tet_ent = get_ent_handle_from_bin(bin);
×
2529

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

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

132✔
2553
double MOABMesh::tet_volume(moab::EntityHandle tet) const
×
2554
{
×
2555
  vector<moab::EntityHandle> conn;
2556
  moab::ErrorCode rval = mbi_->get_connectivity(&tet, 1, conn);
2557
  if (rval != moab::MB_SUCCESS) {
132✔
2558
    fatal_error("Failed to get tet connectivity");
132✔
2559
  }
132✔
2560

132✔
2561
  moab::CartVect p[4];
132✔
2562
  rval = mbi_->get_coords(conn.data(), conn.size(), p[0].array());
132✔
2563
  if (rval != moab::MB_SUCCESS) {
2564
    fatal_error("Failed to get tet coords");
132✔
2565
  }
2566

182✔
2567
  return 1.0 / 6.0 * (((p[1] - p[0]) * (p[2] - p[0])) % (p[3] - p[0]));
2568
}
2569

182✔
2570
int MOABMesh::get_bin(Position r) const
×
2571
{
182✔
2572
  moab::EntityHandle tet = get_tet(r);
2573
  if (tet == 0) {
182✔
2574
    return -1;
×
2575
  } else {
×
2576
    return get_bin_from_ent_handle(tet);
2577
  }
2578
}
182✔
2579

182✔
2580
void MOABMesh::compute_barycentric_data(const moab::Range& tets)
182✔
2581
{
182✔
2582
  moab::ErrorCode rval;
182✔
2583

182✔
2584
  baryc_data_.clear();
2585
  baryc_data_.resize(tets.size());
182✔
2586

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

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

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

2604
    // invert now to avoid this cost later
2605
    a = a.transpose().inverse();
2606
    baryc_data_.at(get_bin_from_ent_handle(tet)) = a;
132✔
2607
  }
2608
}
2609

132✔
2610
bool MOABMesh::point_in_tet(
132✔
2611
  const moab::CartVect& r, moab::EntityHandle tet) const
2612
{
2613

2614
  moab::ErrorCode rval;
24✔
2615

2616
  // get tet vertices
2617
  vector<moab::EntityHandle> verts;
2618
  rval = mbi_->get_connectivity(&tet, 1, verts);
24✔
2619
  if (rval != moab::MB_SUCCESS) {
24✔
2620
    warning("Failed to get vertices of tet in umesh: " + filename_);
2621
    return false;
2622
  }
2623

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

2635
  // look up barycentric data
2636
  int idx = get_bin_from_ent_handle(tet);
2637
  const moab::Matrix3& a_inv = baryc_data_[idx];
24✔
2638

24✔
2639
  moab::CartVect bary_coords = a_inv * (r - p_zero);
2640

2641
  return (bary_coords[0] >= 0.0 && bary_coords[1] >= 0.0 &&
2642
          bary_coords[2] >= 0.0 &&
2643
          bary_coords[0] + bary_coords[1] + bary_coords[2] <= 1.0);
2644
}
2645

21✔
2646
int MOABMesh::get_bin_from_index(int idx) const
2647
{
21✔
2648
  if (idx >= n_bins()) {
21✔
2649
    fatal_error(fmt::format("Invalid bin index: {}", idx));
2650
  }
2651
  return ehs_[idx] - ehs_[0];
2652
}
2653

2654
int MOABMesh::get_index(const Position& r, bool* in_mesh) const
2655
{
2656
  int bin = get_bin(r);
2657
  *in_mesh = bin != -1;
1✔
2658
  return bin;
2659
}
1✔
2660

1✔
2661
int MOABMesh::get_index_from_bin(int bin) const
1✔
2662
{
1✔
2663
  return bin;
2664
}
22✔
2665

2666
std::pair<vector<double>, vector<double>> MOABMesh::plot(
2667
  Position plot_ll, Position plot_ur) const
2668
{
22✔
2669
  // TODO: Implement mesh lines
2670
  return {};
2671
}
22✔
2672

2673
int MOABMesh::get_vert_idx_from_handle(moab::EntityHandle vert) const
2674
{
22✔
2675
  int idx = vert - verts_[0];
2676
  if (idx >= n_vertices()) {
2677
    fatal_error(
22✔
2678
      fmt::format("Invalid vertex idx {} (# vertices {})", idx, n_vertices()));
22✔
2679
  }
2680
  return idx;
2681
}
2682

22✔
2683
int MOABMesh::get_bin_from_ent_handle(moab::EntityHandle eh) const
2684
{
2685
  int bin = eh - ehs_[0];
2686
  if (bin >= n_bins()) {
2687
    fatal_error(fmt::format("Invalid bin: {}", bin));
2688
  }
2689
  return bin;
22✔
2690
}
22✔
2691

22✔
2692
moab::EntityHandle MOABMesh::get_ent_handle_from_bin(int bin) const
2693
{
2694
  if (bin >= n_bins()) {
2695
    fatal_error(fmt::format("Invalid bin index: ", bin));
2696
  }
2697
  return ehs_[0] + bin;
22✔
2698
}
22✔
2699

2700
int MOABMesh::n_bins() const
2701
{
2702
  return ehs_.size();
22✔
2703
}
22✔
2704

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

2718
Position MOABMesh::centroid(int bin) const
2719
{
2720
  moab::ErrorCode rval;
2721

2722
  auto tet = this->get_ent_handle_from_bin(bin);
2723

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

2732
  // get the coordinates
2733
  vector<moab::CartVect> coords(conn.size());
2734
  rval = mbi_->get_coords(conn.data(), conn.size(), coords[0].array());
2735
  if (rval != moab::MB_SUCCESS) {
2736
    warning("Failed to get the coordinates of a mesh element.");
22✔
2737
    return {};
22✔
2738
  }
2739

18✔
2740
  // compute the centroid of the element vertices
2741
  moab::CartVect centroid(0.0, 0.0, 0.0);
2742
  for (const auto& coord : coords) {
18✔
2743
    centroid += coord;
2744
  }
2745
  centroid /= double(coords.size());
2746

18✔
2747
  return {centroid[0], centroid[1], centroid[2]};
18✔
2748
}
2749

2750
int MOABMesh::n_vertices() const
22✔
2751
{
2752
  return verts_.size();
2753
}
22✔
2754

1✔
2755
Position MOABMesh::vertex(int id) const
2756
{
2757

21✔
2758
  moab::ErrorCode rval;
2759

2760
  moab::EntityHandle vert = verts_[id];
21✔
2761

21✔
2762
  moab::CartVect coords;
2763
  rval = mbi_->get_coords(&vert, 1, coords.array());
2764
  if (rval != moab::MB_SUCCESS) {
2765
    fatal_error("Failed to get the coordinates of a vertex.");
2766
  }
18✔
2767

2768
  return {coords[0], coords[1], coords[2]};
18✔
2769
}
18✔
2770

18✔
2771
std::vector<int> MOABMesh::connectivity(int bin) const
18✔
2772
{
2773
  moab::ErrorCode rval;
18✔
2774

2775
  auto tet = get_ent_handle_from_bin(bin);
2776

2777
  // look up the tet connectivity
18✔
2778
  vector<moab::EntityHandle> conn;
2779
  rval = mbi_->get_connectivity(&tet, 1, conn);
2780
  if (rval != moab::MB_SUCCESS) {
2781
    fatal_error("Failed to get connectivity of a mesh element.");
2782
    return {};
2783
  }
2784

18✔
2785
  std::vector<int> verts(4);
18✔
2786
  for (int i = 0; i < verts.size(); i++) {
18✔
2787
    verts[i] = get_vert_idx_from_handle(conn[i]);
2788
  }
2789

18✔
2790
  return verts;
18✔
2791
}
2792

2793
std::pair<moab::Tag, moab::Tag> MOABMesh::get_score_tags(
18✔
2794
  std::string score) const
18✔
2795
{
2✔
2796
  moab::ErrorCode rval;
2797
  // add a tag to the mesh
16✔
2798
  // all scores are treated as a single value
2799
  // with an uncertainty
18✔
2800
  moab::Tag value_tag;
2801

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

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

1,519,602✔
2828
  // return the populated tag handles
2829
  return {value_tag, error_tag};
2830
}
1,519,602✔
2831

1,519,602✔
2832
void MOABMesh::add_score(const std::string& score)
2833
{
1,519,602✔
2834
  auto score_tags = get_score_tags(score);
2835
  tag_names_.push_back(score);
2836
}
1,519,602✔
2837

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

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

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

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

4,688,129✔
2875
void MOABMesh::set_score_data(const std::string& score,
2876
  const vector<double>& values, const vector<double>& std_dev)
4,688,129✔
2877
{
2878
  auto score_tags = this->get_score_tags(score);
4,688,129✔
2879

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

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

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

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

6,266,482✔
2918
#endif
6,266,482✔
2919

6,266,482✔
2920
#ifdef LIBMESH
6,266,482✔
2921

2922
const std::string LibMesh::mesh_lib_type = "libmesh";
2923

2924
LibMesh::LibMesh(pugi::xml_node node) : UnstructuredMesh(node)
2925
{
259,295,916✔
2926
  // filename_ and length_multiplier_ will already be set by the
259,293,070✔
2927
  // UnstructuredMesh constructor
6,263,636✔
2928
  set_mesh_pointer_from_filename(filename_);
2929
  set_length_multiplier(length_multiplier_);
2930
  initialize();
2931
}
2932

2,846✔
2933
// create the mesh from a pointer to a libMesh Mesh
7,276,559✔
2934
LibMesh::LibMesh(libMesh::MeshBase& input_mesh, double length_multiplier)
2935
{
143,856✔
2936
  m_ = &input_mesh;
2937
  set_length_multiplier(length_multiplier);
143,856✔
2938
  initialize();
2939
}
2940

29✔
2941
// create the mesh from an input file
2942
LibMesh::LibMesh(const std::string& filename, double length_multiplier)
29✔
2943
{
2944
  set_mesh_pointer_from_filename(filename);
2945
  set_length_multiplier(length_multiplier);
2946
  initialize();
200,410✔
2947
}
2948

2949
void LibMesh::set_mesh_pointer_from_filename(const std::string& filename)
200,410✔
2950
{
2951
  filename_ = filename;
2952
  unique_m_ = make_unique<libMesh::Mesh>(*settings::libmesh_comm, n_dimension_);
2953
  m_ = unique_m_.get();
2954
  m_->read(filename_);
200,410✔
2955
}
200,410✔
2956

2957
// intialize from mesh file
2958
void LibMesh::initialize()
2959
{
2960
  if (!settings::libmesh_comm) {
1,002,050✔
2961
    fatal_error(
200,410✔
2962
      "Attempting to use an unstructured mesh without a libMesh communicator.");
200,410✔
2963
  }
2964

2965
  // assuming that unstructured meshes used in OpenMC are 3D
2966
  n_dimension_ = 3;
200,410✔
2967

1,002,050✔
2968
  if (length_multiplier_ > 0.0) {
801,640✔
2969
    libMesh::MeshTools::Modification::scale(*m_, length_multiplier_);
2970
  }
2971
  // if OpenMC is managing the libMesh::MeshBase instance, prepare the mesh.
400,820✔
2972
  // Otherwise assume that it is prepared by its owning application
2973
  if (unique_m_) {
2974
    m_->prepare_for_use();
143,856✔
2975
  }
2976

143,856✔
2977
  // ensure that the loaded mesh is 3 dimensional
143,856✔
2978
  if (m_->mesh_dimension() != n_dimension_) {
143,856✔
2979
    fatal_error(fmt::format("Mesh file {} specified for use in an unstructured "
2980
                            "mesh is not a 3D mesh.",
2981
      filename_));
2982
  }
719,280✔
2983

143,856✔
2984
  // create an equation system for storing values
143,856✔
2985
  eq_system_name_ = fmt::format("mesh_{}_system", id_);
2986

2987
  equation_systems_ = make_unique<libMesh::EquationSystems>(*m_);
2988
  libMesh::ExplicitSystem& eq_sys =
287,712✔
2989
    equation_systems_->add_system<libMesh::ExplicitSystem>(eq_system_name_);
143,856✔
2990

2991
  for (int i = 0; i < num_threads(); i++) {
7,276,559✔
2992
    pl_.emplace_back(m_->sub_point_locator());
2993
    pl_.back()->set_contains_point_tol(FP_COINCIDENT);
7,276,559✔
2994
    pl_.back()->enable_out_of_mesh_mode();
7,276,559✔
2995
  }
1,012,923✔
2996

2997
  // store first element in the mesh to use as an offset for bin indices
6,263,636✔
2998
  auto first_elem = *m_->elements_begin();
2999
  first_element_id_ = first_elem->id();
3000

3001
  // bounding box for the mesh for quick rejection checks
18✔
3002
  bbox_ = libMesh::MeshTools::create_bounding_box(*m_);
3003
  libMesh::Point ll = bbox_.min();
3004
  libMesh::Point ur = bbox_.max();
3005
  lower_left_ = {ll(0), ll(1), ll(2)};
18✔
3006
  upper_right_ = {ur(0), ur(1), ur(2)};
18✔
3007
}
3008

3009
// Sample position within a tet for LibMesh type tets
3010
Position LibMesh::sample_element(int32_t bin, uint64_t* seed) const
215,730✔
3011
{
215,712✔
3012
  const auto& elem = get_element_from_bin(bin);
215,712✔
3013
  // Get tet vertex coordinates from LibMesh
215,712✔
3014
  std::array<Position, 4> tet_verts;
3015
  for (int i = 0; i < elem.n_nodes(); i++) {
3016
    auto node_ref = elem.node_ref(i);
3017
    tet_verts[i] = {node_ref(0), node_ref(1), node_ref(2)};
1,078,560✔
3018
  }
215,712✔
3019
  // Samples position within tet using Barycentric coordinates
215,712✔
3020
  return this->sample_tet(tet_verts, seed);
3021
}
3022

3023
Position LibMesh::centroid(int bin) const
215,712✔
3024
{
3025
  const auto& elem = this->get_element_from_bin(bin);
3026
  auto centroid = elem.vertex_average();
215,712✔
3027
  return {centroid(0), centroid(1), centroid(2)};
215,712✔
3028
}
215,712✔
3029

18✔
3030
int LibMesh::n_vertices() const
3031
{
259,293,070✔
3032
  return m_->n_nodes();
3033
}
3034

3035
Position LibMesh::vertex(int vertex_id) const
3036
{
3037
  const auto node_ref = m_->node_ref(vertex_id);
3038
  return {node_ref(0), node_ref(1), node_ref(2)};
259,293,070✔
3039
}
259,293,070✔
3040

259,293,070✔
3041
std::vector<int> LibMesh::connectivity(int elem_id) const
3042
{
3043
  std::vector<int> conn;
3044
  const auto* elem_ptr = m_->elem_ptr(elem_id);
3045
  for (int i = 0; i < elem_ptr->n_nodes(); i++) {
3046
    conn.push_back(elem_ptr->node_id(i));
3047
  }
259,293,070✔
3048
  return conn;
259,293,070✔
3049
}
259,293,070✔
3050

3051
std::string LibMesh::library() const
3052
{
3053
  return mesh_lib_type;
3054
}
3055

3056
int LibMesh::n_bins() const
3057
{
259,293,070✔
3058
  return m_->n_elem();
259,293,070✔
3059
}
3060

259,293,070✔
3061
int LibMesh::n_surface_bins() const
3062
{
419,926,894✔
3063
  int n_bins = 0;
441,495,093✔
3064
  for (int i = 0; i < this->n_bins(); i++) {
280,861,269✔
3065
    const libMesh::Elem& e = get_element_from_bin(i);
259,293,070✔
3066
    n_bins += e.n_faces();
3067
    // if this is a boundary element, it will only be visited once,
3068
    // the number of surface bins is incremented to
3069
    for (auto neighbor_ptr : e.neighbor_ptr_range()) {
3070
      // null neighbor pointer indicates a boundary face
3071
      if (!neighbor_ptr) {
3072
        n_bins++;
3073
      }
3074
    }
3075
  }
3076
  return n_bins;
3077
}
3078

3079
void LibMesh::add_score(const std::string& var_name)
3080
{
3081
  // check if this is a new variable
3082
  std::string value_name = var_name + "_mean";
3083
  if (!variable_map_.count(value_name)) {
3084
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3085
    auto var_num =
3086
      eqn_sys.add_variable(value_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3087
    variable_map_[value_name] = var_num;
3088
  }
3089

3090
  std::string std_dev_name = var_name + "_std_dev";
3091
  // check if this is a new variable
3092
  if (!variable_map_.count(std_dev_name)) {
3093
    auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3094
    auto var_num =
719,424✔
3095
      eqn_sys.add_variable(std_dev_name, libMesh::CONSTANT, libMesh::MONOMIAL);
3096
    variable_map_[std_dev_name] = var_num;
719,424✔
3097
  }
719,424✔
3098
}
3099

3100
void LibMesh::remove_scores()
3101
{
719,424✔
3102
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
3103
  eqn_sys.clear();
3104
  variable_map_.clear();
265,772,418✔
3105
}
3106

265,772,418✔
3107
void LibMesh::set_score_data(const std::string& var_name,
265,772,418✔
3108
  const vector<double>& values, const vector<double>& std_dev)
3109
{
3110
  auto& eqn_sys = equation_systems_->get_system(eq_system_name_);
265,772,418✔
3111

3112
  if (!eqn_sys.is_initialized()) {
3113
    equation_systems_->init();
524,122✔
3114
  }
3115

524,122✔
3116
  const libMesh::DofMap& dof_map = eqn_sys.get_dof_map();
3117

3118
  // look up the value variable
524,122✔
3119
  std::string value_name = var_name + "_mean";
3120
  unsigned int value_num = variable_map_.at(value_name);
3121
  // look up the std dev variable
266,476,455✔
3122
  std::string std_dev_name = var_name + "_std_dev";
3123
  unsigned int std_dev_num = variable_map_.at(std_dev_name);
266,476,455✔
3124

3125
  for (auto it = m_->local_elements_begin(); it != m_->local_elements_end();
3126
       it++) {
3127
    auto bin = get_bin_from_element(*it);
3128

3129
    // set value
3130
    vector<libMesh::dof_id_type> value_dof_indices;
3131
    dof_map.dof_indices(*it, value_dof_indices, value_num);
3132
    Ensures(value_dof_indices.size() == 1);
3133
    eqn_sys.solution->set(value_dof_indices[0], values.at(bin));
3134

3135
    // set std dev
3136
    vector<libMesh::dof_id_type> std_dev_dof_indices;
3137
    dof_map.dof_indices(*it, std_dev_dof_indices, std_dev_num);
3138
    Ensures(std_dev_dof_indices.size() == 1);
3139
    eqn_sys.solution->set(std_dev_dof_indices[0], std_dev.at(bin));
3140
  }
3141
}
3142

3143
void LibMesh::write(const std::string& filename) const
3144
{
3145
  write_message(fmt::format(
3146
    "Writing file: {}.e for unstructured mesh {}", filename, this->id_));
3147
  libMesh::ExodusII_IO exo(*m_);
3148
  std::set<std::string> systems_out = {eq_system_name_};
3149
  exo.write_discontinuous_exodusII(
3150
    filename + ".e", *equation_systems_, &systems_out);
3151
}
3152

3153
void LibMesh::bins_crossed(Position r0, Position r1, const Direction& u,
3154
  vector<int>& bins, vector<double>& lengths) const
3155
{
3156
  // TODO: Implement triangle crossings here
3157
  fatal_error("Tracklength tallies on libMesh instances are not implemented.");
3158
}
3159

3160
int LibMesh::get_bin(Position r) const
3161
{
3162
  // look-up a tet using the point locator
3163
  libMesh::Point p(r.x, r.y, r.z);
3164

3165
  // quick rejection check
3166
  if (!bbox_.contains_point(p)) {
3167
    return -1;
3168
  }
3169

3170
  const auto& point_locator = pl_.at(thread_num());
3171

745,093✔
3172
  const auto elem_ptr = (*point_locator)(p);
3173
  return elem_ptr ? get_bin_from_element(elem_ptr) : -1;
745,093✔
3174
}
3175

3176
int LibMesh::get_bin_from_element(const libMesh::Elem* elem) const
76,875✔
3177
{
3178
  int bin = elem->id() - first_element_id_;
3179
  if (bin >= n_bins() || bin < 0) {
3180
    fatal_error(fmt::format("Invalid bin: {}", bin));
3181
  }
76,875✔
3182
  return bin;
3183
}
76,875✔
3184

76,875✔
3185
std::pair<vector<double>, vector<double>> LibMesh::plot(
76,875✔
3186
  Position plot_ll, Position plot_ur) const
3187
{
3188
  return {};
3189
}
153,750✔
3190

3191
const libMesh::Elem& LibMesh::get_element_from_bin(int bin) const
3192
{
179,856✔
3193
  return m_->elem_ref(bin);
3194
}
3195

3196
double LibMesh::volume(int bin) const
179,856✔
3197
{
3198
  return this->get_element_from_bin(bin).volume();
3199
}
179,856✔
3200

179,856✔
3201
#endif // LIBMESH
179,856✔
3202

3203
//==============================================================================
3204
// Non-member functions
3205
//==============================================================================
3206

179,856✔
3207
void read_meshes(pugi::xml_node root)
899,280✔
3208
{
719,424✔
3209
  std::unordered_set<int> mesh_ids;
3210

3211
  for (auto node : root.children("mesh")) {
179,856✔
3212
    // Check to make sure multiple meshes in the same file don't share IDs
179,856✔
3213
    int id = std::stoi(get_node_value(node, "id"));
3214
    if (contains(mesh_ids, id)) {
3215
      fatal_error(fmt::format(
3216
        "Two or more meshes use the same unique ID '{}' in the same input file",
3217
        id));
3218
    }
3219
    mesh_ids.insert(id);
3220

3221
    // If we've already read a mesh with the same ID in a *different* file,
3222
    // assume it is the same here
3223
    if (model::mesh_map.find(id) != model::mesh_map.end()) {
3224
      warning(fmt::format("Mesh with ID={} appears in multiple files.", id));
3225
      continue;
3226
    }
3227

3228
    std::string mesh_type;
3229
    if (check_for_node(node, "type")) {
3230
      mesh_type = get_node_value(node, "type", true, true);
3231
    } else {
3232
      mesh_type = "regular";
3233
    }
3234

3235
    // determine the mesh library to use
3236
    std::string mesh_lib;
3237
    if (check_for_node(node, "library")) {
3238
      mesh_lib = get_node_value(node, "library", true, true);
3239
    }
3240

3241
    // Read mesh and add to vector
3242
    if (mesh_type == RegularMesh::mesh_type) {
3243
      model::meshes.push_back(make_unique<RegularMesh>(node));
3244
    } else if (mesh_type == RectilinearMesh::mesh_type) {
3245
      model::meshes.push_back(make_unique<RectilinearMesh>(node));
3246
    } else if (mesh_type == CylindricalMesh::mesh_type) {
3247
      model::meshes.push_back(make_unique<CylindricalMesh>(node));
3248
    } else if (mesh_type == SphericalMesh::mesh_type) {
3249
      model::meshes.push_back(make_unique<SphericalMesh>(node));
3250
#ifdef DAGMC
3251
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3252
               mesh_lib == MOABMesh::mesh_lib_type) {
3253
      model::meshes.push_back(make_unique<MOABMesh>(node));
3254
#endif
3255
#ifdef LIBMESH
3256
    } else if (mesh_type == UnstructuredMesh::mesh_type &&
3257
               mesh_lib == LibMesh::mesh_lib_type) {
3258
      model::meshes.push_back(make_unique<LibMesh>(node));
3259
#endif
3260
    } else if (mesh_type == UnstructuredMesh::mesh_type) {
3261
      fatal_error("Unstructured mesh support is not enabled or the mesh "
3262
                  "library is invalid.");
3263
    } else {
3264
      fatal_error("Invalid mesh type: " + mesh_type);
3265
    }
3266

3267
    // Map ID to position in vector
3268
    model::mesh_map[model::meshes.back()->id_] = model::meshes.size() - 1;
3269
  }
3270
}
3271

3272
void meshes_to_hdf5(hid_t group)
3273
{
3274
  // Write number of meshes
3275
  hid_t meshes_group = create_group(group, "meshes");
3276
  int32_t n_meshes = model::meshes.size();
3277
  write_attribute(meshes_group, "n_meshes", n_meshes);
3278

3279
  if (n_meshes > 0) {
3280
    // Write IDs of meshes
3281
    vector<int> ids;
3282
    for (const auto& m : model::meshes) {
3283
      m->to_hdf5(meshes_group);
3284
      ids.push_back(m->id_);
3285
    }
3286
    write_attribute(meshes_group, "ids", ids);
3287
  }
3288

3289
  close_group(meshes_group);
3290
}
3291

3292
void free_memory_mesh()
3293
{
3294
  model::meshes.clear();
3295
  model::mesh_map.clear();
3296
}
3297

3298
extern "C" int n_meshes()
3299
{
3300
  return model::meshes.size();
3301
}
3302

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