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

openmc-dev / openmc / 13591325608

28 Feb 2025 03:31PM UTC coverage: 85.04% (-0.003%) from 85.043%
13591325608

Pull #3330

github

web-flow
Merge deaf66279 into c26fde666
Pull Request #3330: Fix reading of horizontal field of view for ray-traced plots

1 of 4 new or added lines in 1 file covered. (25.0%)

60 existing lines in 3 files now uncovered.

51017 of 59992 relevant lines covered (85.04%)

35290015.83 hits per line

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

83.32
/src/plot.cpp
1
#include "openmc/plot.h"
2

3
#include <algorithm>
4
#define _USE_MATH_DEFINES // to make M_PI declared in Intel and MSVC compilers
5
#include <cmath>
6
#include <cstdio>
7
#include <fstream>
8
#include <sstream>
9

10
#include "xtensor/xmanipulation.hpp"
11
#include "xtensor/xview.hpp"
12
#include <fmt/core.h>
13
#include <fmt/ostream.h>
14
#ifdef USE_LIBPNG
15
#include <png.h>
16
#endif
17

18
#include "openmc/constants.h"
19
#include "openmc/container_util.h"
20
#include "openmc/dagmc.h"
21
#include "openmc/error.h"
22
#include "openmc/file_utils.h"
23
#include "openmc/geometry.h"
24
#include "openmc/hdf5_interface.h"
25
#include "openmc/material.h"
26
#include "openmc/mesh.h"
27
#include "openmc/message_passing.h"
28
#include "openmc/openmp_interface.h"
29
#include "openmc/output.h"
30
#include "openmc/particle.h"
31
#include "openmc/progress_bar.h"
32
#include "openmc/random_lcg.h"
33
#include "openmc/settings.h"
34
#include "openmc/simulation.h"
35
#include "openmc/string_utils.h"
36

37
namespace openmc {
38

39
//==============================================================================
40
// Constants
41
//==============================================================================
42

43
constexpr int PLOT_LEVEL_LOWEST {-1}; //!< lower bound on plot universe level
44
constexpr int32_t NOT_FOUND {-2};
45
constexpr int32_t OVERLAP {-3};
46

47
IdData::IdData(size_t h_res, size_t v_res) : data_({v_res, h_res, 3}, NOT_FOUND)
6,552✔
48
{}
6,552✔
49

50
void IdData::set_value(size_t y, size_t x, const GeometryState& p, int level)
52,542,238✔
51
{
52
  // set cell data
53
  if (p.n_coord() <= level) {
52,542,238✔
54
    data_(y, x, 0) = NOT_FOUND;
×
55
    data_(y, x, 1) = NOT_FOUND;
×
56
  } else {
57
    data_(y, x, 0) = model::cells.at(p.coord(level).cell)->id_;
52,542,238✔
58
    data_(y, x, 1) = level == p.n_coord() - 1
105,084,476✔
59
                       ? p.cell_instance()
52,542,238✔
60
                       : cell_instance_at_level(p, level);
×
61
  }
62

63
  // set material data
64
  Cell* c = model::cells.at(p.lowest_coord().cell).get();
52,542,238✔
65
  if (p.material() == MATERIAL_VOID) {
52,542,238✔
66
    data_(y, x, 2) = MATERIAL_VOID;
42,224,728✔
67
    return;
42,224,728✔
68
  } else if (c->type_ == Fill::MATERIAL) {
10,317,510✔
69
    Material* m = model::materials.at(p.material()).get();
10,317,510✔
70
    data_(y, x, 2) = m->id_;
10,317,510✔
71
  }
72
}
73

74
void IdData::set_overlap(size_t y, size_t x)
475,944✔
75
{
76
  xt::view(data_, y, x, xt::all()) = OVERLAP;
475,944✔
77
}
475,944✔
78

79
PropertyData::PropertyData(size_t h_res, size_t v_res)
14✔
80
  : data_({v_res, h_res, 2}, NOT_FOUND)
14✔
81
{}
14✔
82

83
void PropertyData::set_value(
126✔
84
  size_t y, size_t x, const GeometryState& p, int level)
85
{
86
  Cell* c = model::cells.at(p.lowest_coord().cell).get();
126✔
87
  data_(y, x, 0) = (p.sqrtkT() * p.sqrtkT()) / K_BOLTZMANN;
126✔
88
  if (c->type_ != Fill::UNIVERSE && p.material() != MATERIAL_VOID) {
126✔
89
    Material* m = model::materials.at(p.material()).get();
126✔
90
    data_(y, x, 1) = m->density_gpcc_;
126✔
91
  }
92
}
126✔
93

94
void PropertyData::set_overlap(size_t y, size_t x)
×
95
{
96
  data_(y, x) = OVERLAP;
×
97
}
98

99
//==============================================================================
100
// Global variables
101
//==============================================================================
102

103
namespace model {
104

105
std::unordered_map<int, int> plot_map;
106
vector<std::unique_ptr<PlottableInterface>> plots;
107
uint64_t plotter_seed = 1;
108

109
} // namespace model
110

111
//==============================================================================
112
// RUN_PLOT controls the logic for making one or many plots
113
//==============================================================================
114

115
extern "C" int openmc_plot_geometry()
350✔
116
{
117

118
  for (auto& pl : model::plots) {
1,050✔
119
    write_message(5, "Processing plot {}: {}...", pl->id(), pl->path_plot());
700✔
120
    pl->create_output();
700✔
121
  }
122

123
  return 0;
350✔
124
}
125

126
void Plot::create_output() const
588✔
127
{
128
  if (PlotType::slice == type_) {
588✔
129
    // create 2D image
130
    create_image();
518✔
131
  } else if (PlotType::voxel == type_) {
70✔
132
    // create voxel file for 3D viewing
133
    create_voxel();
70✔
134
  }
135
}
588✔
136

137
void Plot::print_info() const
560✔
138
{
139
  // Plot type
140
  if (PlotType::slice == type_) {
560✔
141
    fmt::print("Plot Type: Slice\n");
490✔
142
  } else if (PlotType::voxel == type_) {
70✔
143
    fmt::print("Plot Type: Voxel\n");
70✔
144
  }
145

146
  // Plot parameters
147
  fmt::print("Origin: {} {} {}\n", origin_[0], origin_[1], origin_[2]);
1,040✔
148

149
  if (PlotType::slice == type_) {
560✔
150
    fmt::print("Width: {:4} {:4}\n", width_[0], width_[1]);
980✔
151
  } else if (PlotType::voxel == type_) {
70✔
152
    fmt::print("Width: {:4} {:4} {:4}\n", width_[0], width_[1], width_[2]);
140✔
153
  }
154

155
  if (PlotColorBy::cells == color_by_) {
560✔
156
    fmt::print("Coloring: Cells\n");
434✔
157
  } else if (PlotColorBy::mats == color_by_) {
126✔
158
    fmt::print("Coloring: Materials\n");
126✔
159
  }
160

161
  if (PlotType::slice == type_) {
560✔
162
    switch (basis_) {
490✔
163
    case PlotBasis::xy:
280✔
164
      fmt::print("Basis: XY\n");
240✔
165
      break;
280✔
166
    case PlotBasis::xz:
112✔
167
      fmt::print("Basis: XZ\n");
96✔
168
      break;
112✔
169
    case PlotBasis::yz:
98✔
170
      fmt::print("Basis: YZ\n");
84✔
171
      break;
98✔
172
    }
173
    fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
980✔
174
  } else if (PlotType::voxel == type_) {
70✔
175
    fmt::print("Voxels: {} {} {}\n", pixels_[0], pixels_[1], pixels_[2]);
140✔
176
  }
177
}
560✔
178

179
void read_plots_xml()
1,651✔
180
{
181
  // Check if plots.xml exists; this is only necessary when the plot runmode is
182
  // initiated. Otherwise, we want to read plots.xml because it may be called
183
  // later via the API. In that case, its ok for a plots.xml to not exist
184
  std::string filename = settings::path_input + "plots.xml";
1,651✔
185
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
1,651✔
186
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
187
  }
188

189
  write_message("Reading plot XML file...", 5);
1,651✔
190

191
  // Parse plots.xml file
192
  pugi::xml_document doc;
1,651✔
193
  doc.load_file(filename.c_str());
1,651✔
194

195
  pugi::xml_node root = doc.document_element();
1,651✔
196

197
  read_plots_xml(root);
1,651✔
198
}
1,651✔
199

200
void read_plots_xml(pugi::xml_node root)
2,243✔
201
{
202
  for (auto node : root.children("plot")) {
3,665✔
203
    std::string id_string = get_node_value(node, "id", true);
1,434✔
204
    int id = std::stoi(id_string);
1,434✔
205
    if (check_for_node(node, "type")) {
1,434✔
206
      std::string type_str = get_node_value(node, "type", true);
1,434✔
207
      if (type_str == "slice") {
1,434✔
208
        model::plots.emplace_back(
1,240✔
209
          std::make_unique<Plot>(node, Plot::PlotType::slice));
2,492✔
210
      } else if (type_str == "voxel") {
182✔
211
        model::plots.emplace_back(
70✔
212
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
140✔
213
      } else if (type_str == "wireframe_raytrace") {
112✔
214
        model::plots.emplace_back(
70✔
215
          std::make_unique<WireframeRayTracePlot>(node));
140✔
216
      } else if (type_str == "solid_raytrace") {
42✔
217
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
42✔
218
      } else {
219
        fatal_error(
×
220
          fmt::format("Unsupported plot type '{}' in plot {}", type_str, id));
×
221
      }
222
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
1,422✔
223
    } else {
1,422✔
224
      fatal_error(fmt::format("Must specify plot type in plot {}", id));
×
225
    }
226
  }
1,422✔
227
}
2,231✔
228

229
void free_memory_plot()
7,999✔
230
{
231
  model::plots.clear();
7,999✔
232
  model::plot_map.clear();
7,999✔
233
}
7,999✔
234

235
// creates an image based on user input from a plots.xml <plot>
236
// specification in the PNG/PPM format
237
void Plot::create_image() const
518✔
238
{
239

240
  size_t width = pixels_[0];
518✔
241
  size_t height = pixels_[1];
518✔
242

243
  ImageData data({width, height}, not_found_);
518✔
244

245
  // generate ids for the plot
246
  auto ids = get_map<IdData>();
518✔
247

248
  // assign colors
249
  for (size_t y = 0; y < height; y++) {
88,326✔
250
    for (size_t x = 0; x < width; x++) {
19,277,076✔
251
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
19,189,268✔
252
      auto id = ids.data_(y, x, idx);
19,189,268✔
253
      // no setting needed if not found
254
      if (id == NOT_FOUND) {
19,189,268✔
255
        continue;
3,860,724✔
256
      }
257
      if (id == OVERLAP) {
15,804,488✔
258
        data(x, y) = overlap_color_;
475,944✔
259
        continue;
475,944✔
260
      }
261
      if (PlotColorBy::cells == color_by_) {
15,328,544✔
262
        data(x, y) = colors_[model::cell_map[id]];
10,874,584✔
263
      } else if (PlotColorBy::mats == color_by_) {
4,453,960✔
264
        if (id == MATERIAL_VOID) {
4,453,960✔
265
          data(x, y) = WHITE;
×
266
          continue;
×
267
        }
268
        data(x, y) = colors_[model::material_map[id]];
4,453,960✔
269
      } // color_by if-else
270
    }
271
  }
272

273
  // draw mesh lines if present
274
  if (index_meshlines_mesh_ >= 0) {
518✔
275
    draw_mesh_lines(data);
42✔
276
  }
277

278
// create image file
279
#ifdef USE_LIBPNG
280
  output_png(path_plot(), data);
518✔
281
#else
282
  output_ppm(path_plot(), data);
283
#endif
284
}
518✔
285

286
void PlottableInterface::set_id(pugi::xml_node plot_node)
1,434✔
287
{
288
  // Copy data into plots
289
  if (check_for_node(plot_node, "id")) {
1,434✔
290
    id_ = std::stoi(get_node_value(plot_node, "id"));
1,434✔
291
  } else {
292
    fatal_error("Must specify plot id in plots XML file.");
×
293
  }
294

295
  // Check to make sure 'id' hasn't been used
296
  if (model::plot_map.find(id_) != model::plot_map.end()) {
1,434✔
297
    fatal_error(
×
298
      fmt::format("Two or more plots use the same unique ID: {}", id_));
×
299
  }
300
}
1,434✔
301

302
// Checks if png or ppm is already present
303
bool file_extension_present(
1,422✔
304
  const std::string& filename, const std::string& extension)
305
{
306
  std::string file_extension_if_present =
307
    filename.substr(filename.find_last_of(".") + 1);
1,422✔
308
  if (file_extension_if_present == extension)
1,422✔
309
    return true;
70✔
310
  return false;
1,352✔
311
}
1,422✔
312

313
void Plot::set_output_path(pugi::xml_node plot_node)
1,322✔
314
{
315
  // Set output file path
316
  std::string filename;
1,322✔
317

318
  if (check_for_node(plot_node, "filename")) {
1,322✔
319
    filename = get_node_value(plot_node, "filename");
314✔
320
  } else {
321
    filename = fmt::format("plot_{}", id());
2,016✔
322
  }
323
  const std::string dir_if_present =
324
    filename.substr(0, filename.find_last_of("/") + 1);
1,322✔
325
  if (dir_if_present.size() > 0 && !dir_exists(dir_if_present)) {
1,322✔
326
    fatal_error(fmt::format("Directory '{}' does not exist!", dir_if_present));
12✔
327
  }
328
  // add appropriate file extension to name
329
  switch (type_) {
1,310✔
330
  case PlotType::slice:
1,240✔
331
#ifdef USE_LIBPNG
332
    if (!file_extension_present(filename, "png"))
1,240✔
333
      filename.append(".png");
1,240✔
334
#else
335
    if (!file_extension_present(filename, "ppm"))
336
      filename.append(".ppm");
337
#endif
338
    break;
1,240✔
339
  case PlotType::voxel:
70✔
340
    if (!file_extension_present(filename, "h5"))
70✔
341
      filename.append(".h5");
70✔
342
    break;
70✔
343
  }
344

345
  path_plot_ = filename;
1,310✔
346

347
  // Copy plot pixel size
348
  vector<int> pxls = get_node_array<int>(plot_node, "pixels");
2,620✔
349
  if (PlotType::slice == type_) {
1,310✔
350
    if (pxls.size() == 2) {
1,240✔
351
      pixels_[0] = pxls[0];
1,240✔
352
      pixels_[1] = pxls[1];
1,240✔
353
    } else {
354
      fatal_error(
×
355
        fmt::format("<pixels> must be length 2 in slice plot {}", id()));
×
356
    }
357
  } else if (PlotType::voxel == type_) {
70✔
358
    if (pxls.size() == 3) {
70✔
359
      pixels_[0] = pxls[0];
70✔
360
      pixels_[1] = pxls[1];
70✔
361
      pixels_[2] = pxls[2];
70✔
362
    } else {
363
      fatal_error(
×
364
        fmt::format("<pixels> must be length 3 in voxel plot {}", id()));
×
365
    }
366
  }
367
}
1,310✔
368

369
void PlottableInterface::set_bg_color(pugi::xml_node plot_node)
1,434✔
370
{
371
  // Copy plot background color
372
  if (check_for_node(plot_node, "background")) {
1,434✔
373
    vector<int> bg_rgb = get_node_array<int>(plot_node, "background");
56✔
374
    if (bg_rgb.size() == 3) {
56✔
375
      not_found_ = bg_rgb;
56✔
376
    } else {
377
      fatal_error(fmt::format("Bad background RGB in plot {}", id()));
×
378
    }
379
  }
56✔
380
}
1,434✔
381

382
void Plot::set_basis(pugi::xml_node plot_node)
1,310✔
383
{
384
  // Copy plot basis
385
  if (PlotType::slice == type_) {
1,310✔
386
    std::string pl_basis = "xy";
1,240✔
387
    if (check_for_node(plot_node, "basis")) {
1,240✔
388
      pl_basis = get_node_value(plot_node, "basis", true);
1,240✔
389
    }
390
    if ("xy" == pl_basis) {
1,240✔
391
      basis_ = PlotBasis::xy;
920✔
392
    } else if ("xz" == pl_basis) {
320✔
393
      basis_ = PlotBasis::xz;
154✔
394
    } else if ("yz" == pl_basis) {
166✔
395
      basis_ = PlotBasis::yz;
166✔
396
    } else {
397
      fatal_error(
×
398
        fmt::format("Unsupported plot basis '{}' in plot {}", pl_basis, id()));
×
399
    }
400
  }
1,240✔
401
}
1,310✔
402

403
void Plot::set_origin(pugi::xml_node plot_node)
1,310✔
404
{
405
  // Copy plotting origin
406
  auto pl_origin = get_node_array<double>(plot_node, "origin");
1,310✔
407
  if (pl_origin.size() == 3) {
1,310✔
408
    origin_ = pl_origin;
1,310✔
409
  } else {
410
    fatal_error(fmt::format("Origin must be length 3 in plot {}", id()));
×
411
  }
412
}
1,310✔
413

414
void Plot::set_width(pugi::xml_node plot_node)
1,310✔
415
{
416
  // Copy plotting width
417
  vector<double> pl_width = get_node_array<double>(plot_node, "width");
1,310✔
418
  if (PlotType::slice == type_) {
1,310✔
419
    if (pl_width.size() == 2) {
1,240✔
420
      width_.x = pl_width[0];
1,240✔
421
      width_.y = pl_width[1];
1,240✔
422
    } else {
423
      fatal_error(
×
424
        fmt::format("<width> must be length 2 in slice plot {}", id()));
×
425
    }
426
  } else if (PlotType::voxel == type_) {
70✔
427
    if (pl_width.size() == 3) {
70✔
428
      pl_width = get_node_array<double>(plot_node, "width");
70✔
429
      width_ = pl_width;
70✔
430
    } else {
431
      fatal_error(
×
432
        fmt::format("<width> must be length 3 in voxel plot {}", id()));
×
433
    }
434
  }
435
}
1,310✔
436

437
void PlottableInterface::set_universe(pugi::xml_node plot_node)
1,434✔
438
{
439
  // Copy plot universe level
440
  if (check_for_node(plot_node, "level")) {
1,434✔
441
    level_ = std::stoi(get_node_value(plot_node, "level"));
×
442
    if (level_ < 0) {
×
443
      fatal_error(fmt::format("Bad universe level in plot {}", id()));
×
444
    }
445
  } else {
446
    level_ = PLOT_LEVEL_LOWEST;
1,434✔
447
  }
448
}
1,434✔
449

450
void PlottableInterface::set_default_colors(pugi::xml_node plot_node)
1,434✔
451
{
452
  // Copy plot color type and initialize all colors randomly
453
  std::string pl_color_by = "cell";
1,434✔
454
  if (check_for_node(plot_node, "color_by")) {
1,434✔
455
    pl_color_by = get_node_value(plot_node, "color_by", true);
1,392✔
456
  }
457
  if ("cell" == pl_color_by) {
1,434✔
458
    color_by_ = PlotColorBy::cells;
802✔
459
    colors_.resize(model::cells.size());
802✔
460
  } else if ("material" == pl_color_by) {
632✔
461
    color_by_ = PlotColorBy::mats;
632✔
462
    colors_.resize(model::materials.size());
632✔
463
  } else {
464
    fatal_error(fmt::format(
×
465
      "Unsupported plot color type '{}' in plot {}", pl_color_by, id()));
×
466
  }
467

468
  for (auto& c : colors_) {
5,562✔
469
    c = random_color();
4,128✔
470
    // make sure we don't interfere with some default colors
471
    while (c == RED || c == WHITE) {
4,128✔
472
      c = random_color();
×
473
    }
474
  }
475
}
1,434✔
476

477
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
1,434✔
478
{
479
  for (auto cn : plot_node.children("color")) {
1,840✔
480
    // Make sure 3 values are specified for RGB
481
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
406✔
482
    if (user_rgb.size() != 3) {
406✔
483
      fatal_error(fmt::format("Bad RGB in plot {}", id()));
×
484
    }
485
    // Ensure that there is an id for this color specification
486
    int col_id;
487
    if (check_for_node(cn, "id")) {
406✔
488
      col_id = std::stoi(get_node_value(cn, "id"));
406✔
489
    } else {
490
      fatal_error(fmt::format(
×
491
        "Must specify id for color specification in plot {}", id()));
×
492
    }
493
    // Add RGB
494
    if (PlotColorBy::cells == color_by_) {
406✔
495
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
154✔
496
        col_id = model::cell_map[col_id];
112✔
497
        colors_[col_id] = user_rgb;
112✔
498
      } else {
499
        warning(fmt::format(
42✔
500
          "Could not find cell {} specified in plot {}", col_id, id()));
84✔
501
      }
502
    } else if (PlotColorBy::mats == color_by_) {
252✔
503
      if (model::material_map.find(col_id) != model::material_map.end()) {
252✔
504
        col_id = model::material_map[col_id];
252✔
505
        colors_[col_id] = user_rgb;
252✔
506
      } else {
507
        warning(fmt::format(
×
508
          "Could not find material {} specified in plot {}", col_id, id()));
×
509
      }
510
    }
511
  } // color node loop
406✔
512
}
1,434✔
513

514
void Plot::set_meshlines(pugi::xml_node plot_node)
1,310✔
515
{
516
  // Deal with meshlines
517
  pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines");
1,310✔
518

519
  if (!mesh_line_nodes.empty()) {
1,310✔
520
    if (PlotType::voxel == type_) {
42✔
521
      warning(fmt::format("Meshlines ignored in voxel plot {}", id()));
×
522
    }
523

524
    if (mesh_line_nodes.size() == 1) {
42✔
525
      // Get first meshline node
526
      pugi::xml_node meshlines_node = mesh_line_nodes[0].node();
42✔
527

528
      // Check mesh type
529
      std::string meshtype;
42✔
530
      if (check_for_node(meshlines_node, "meshtype")) {
42✔
531
        meshtype = get_node_value(meshlines_node, "meshtype");
42✔
532
      } else {
533
        fatal_error(fmt::format(
×
534
          "Must specify a meshtype for meshlines specification in plot {}",
535
          id()));
×
536
      }
537

538
      // Ensure that there is a linewidth for this meshlines specification
539
      std::string meshline_width;
42✔
540
      if (check_for_node(meshlines_node, "linewidth")) {
42✔
541
        meshline_width = get_node_value(meshlines_node, "linewidth");
42✔
542
        meshlines_width_ = std::stoi(meshline_width);
42✔
543
      } else {
544
        fatal_error(fmt::format(
×
545
          "Must specify a linewidth for meshlines specification in plot {}",
546
          id()));
×
547
      }
548

549
      // Check for color
550
      if (check_for_node(meshlines_node, "color")) {
42✔
551
        // Check and make sure 3 values are specified for RGB
552
        vector<int> ml_rgb = get_node_array<int>(meshlines_node, "color");
×
553
        if (ml_rgb.size() != 3) {
×
554
          fatal_error(
×
555
            fmt::format("Bad RGB for meshlines color in plot {}", id()));
×
556
        }
557
        meshlines_color_ = ml_rgb;
×
558
      }
559

560
      // Set mesh based on type
561
      if ("ufs" == meshtype) {
42✔
562
        if (!simulation::ufs_mesh) {
×
563
          fatal_error(
×
564
            fmt::format("No UFS mesh for meshlines on plot {}", id()));
×
565
        } else {
566
          for (int i = 0; i < model::meshes.size(); ++i) {
×
567
            if (const auto* m =
×
568
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
×
569
              if (m == simulation::ufs_mesh) {
×
570
                index_meshlines_mesh_ = i;
×
571
              }
572
            }
573
          }
574
          if (index_meshlines_mesh_ == -1)
×
575
            fatal_error("Could not find the UFS mesh for meshlines plot");
×
576
        }
577
      } else if ("entropy" == meshtype) {
42✔
578
        if (!simulation::entropy_mesh) {
28✔
579
          fatal_error(
×
580
            fmt::format("No entropy mesh for meshlines on plot {}", id()));
×
581
        } else {
582
          for (int i = 0; i < model::meshes.size(); ++i) {
70✔
583
            if (const auto* m =
42✔
584
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
42✔
585
              if (m == simulation::entropy_mesh) {
28✔
586
                index_meshlines_mesh_ = i;
28✔
587
              }
588
            }
589
          }
590
          if (index_meshlines_mesh_ == -1)
28✔
591
            fatal_error("Could not find the entropy mesh for meshlines plot");
×
592
        }
593
      } else if ("tally" == meshtype) {
14✔
594
        // Ensure that there is a mesh id if the type is tally
595
        int tally_mesh_id;
596
        if (check_for_node(meshlines_node, "id")) {
14✔
597
          tally_mesh_id = std::stoi(get_node_value(meshlines_node, "id"));
14✔
598
        } else {
599
          std::stringstream err_msg;
×
600
          fatal_error(fmt::format("Must specify a mesh id for meshlines tally "
×
601
                                  "mesh specification in plot {}",
602
            id()));
×
603
        }
×
604
        // find the tally index
605
        int idx;
606
        int err = openmc_get_mesh_index(tally_mesh_id, &idx);
14✔
607
        if (err != 0) {
14✔
608
          fatal_error(fmt::format("Could not find mesh {} specified in "
×
609
                                  "meshlines for plot {}",
610
            tally_mesh_id, id()));
×
611
        }
612
        index_meshlines_mesh_ = idx;
14✔
613
      } else {
614
        fatal_error(fmt::format("Invalid type for meshlines on plot {}", id()));
×
615
      }
616
    } else {
42✔
617
      fatal_error(fmt::format("Mutliple meshlines specified in plot {}", id()));
×
618
    }
619
  }
620
}
1,310✔
621

622
void PlottableInterface::set_mask(pugi::xml_node plot_node)
1,434✔
623
{
624
  // Deal with masks
625
  pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask");
1,434✔
626

627
  if (!mask_nodes.empty()) {
1,434✔
628
    if (mask_nodes.size() == 1) {
42✔
629
      // Get pointer to mask
630
      pugi::xml_node mask_node = mask_nodes[0].node();
42✔
631

632
      // Determine how many components there are and allocate
633
      vector<int> iarray = get_node_array<int>(mask_node, "components");
42✔
634
      if (iarray.size() == 0) {
42✔
635
        fatal_error(
×
636
          fmt::format("Missing <components> in mask of plot {}", id()));
×
637
      }
638

639
      // First we need to change the user-specified identifiers to indices
640
      // in the cell and material arrays
641
      for (auto& col_id : iarray) {
126✔
642
        if (PlotColorBy::cells == color_by_) {
84✔
643
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
84✔
644
            col_id = model::cell_map[col_id];
84✔
645
          } else {
646
            fatal_error(fmt::format("Could not find cell {} specified in the "
×
647
                                    "mask in plot {}",
648
              col_id, id()));
×
649
          }
650
        } else if (PlotColorBy::mats == color_by_) {
×
651
          if (model::material_map.find(col_id) != model::material_map.end()) {
×
652
            col_id = model::material_map[col_id];
×
653
          } else {
654
            fatal_error(fmt::format("Could not find material {} specified in "
×
655
                                    "the mask in plot {}",
656
              col_id, id()));
×
657
          }
658
        }
659
      }
660

661
      // Alter colors based on mask information
662
      for (int j = 0; j < colors_.size(); j++) {
168✔
663
        if (contains(iarray, j)) {
126✔
664
          if (check_for_node(mask_node, "background")) {
84✔
665
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
84✔
666
            colors_[j] = bg_rgb;
84✔
667
          } else {
84✔
668
            colors_[j] = WHITE;
×
669
          }
670
        }
671
      }
672

673
    } else {
42✔
674
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
675
    }
676
  }
677
}
1,434✔
678

679
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
1,434✔
680
{
681
  color_overlaps_ = false;
1,434✔
682
  if (check_for_node(plot_node, "show_overlaps")) {
1,434✔
683
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
42✔
684
    // check for custom overlap color
685
    if (check_for_node(plot_node, "overlap_color")) {
42✔
686
      if (!color_overlaps_) {
14✔
687
        warning(fmt::format(
×
688
          "Overlap color specified in plot {} but overlaps won't be shown.",
689
          id()));
×
690
      }
691
      vector<int> olap_clr = get_node_array<int>(plot_node, "overlap_color");
14✔
692
      if (olap_clr.size() == 3) {
14✔
693
        overlap_color_ = olap_clr;
14✔
694
      } else {
695
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
696
      }
697
    }
14✔
698
  }
699

700
  // make sure we allocate the vector for counting overlap checks if
701
  // they're going to be plotted
702
  if (color_overlaps_ && settings::run_mode == RunMode::PLOTTING) {
1,434✔
703
    settings::check_overlaps = true;
42✔
704
    model::overlap_check_count.resize(model::cells.size(), 0);
42✔
705
  }
706
}
1,434✔
707

708
PlottableInterface::PlottableInterface(pugi::xml_node plot_node)
1,434✔
709
{
710
  set_id(plot_node);
1,434✔
711
  set_bg_color(plot_node);
1,434✔
712
  set_universe(plot_node);
1,434✔
713
  set_default_colors(plot_node);
1,434✔
714
  set_user_colors(plot_node);
1,434✔
715
  set_mask(plot_node);
1,434✔
716
  set_overlap_color(plot_node);
1,434✔
717
}
1,434✔
718

719
Plot::Plot(pugi::xml_node plot_node, PlotType type)
1,322✔
720
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
1,322✔
721
{
722
  set_output_path(plot_node);
1,322✔
723
  set_basis(plot_node);
1,310✔
724
  set_origin(plot_node);
1,310✔
725
  set_width(plot_node);
1,310✔
726
  set_meshlines(plot_node);
1,310✔
727
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
1,310✔
728
  slice_color_overlaps_ = color_overlaps_;
1,310✔
729
}
1,310✔
730

731
//==============================================================================
732
// OUTPUT_PPM writes out a previously generated image to a PPM file
733
//==============================================================================
734

735
void output_ppm(const std::string& filename, const ImageData& data)
×
736
{
737
  // Open PPM file for writing
738
  std::string fname = filename;
×
739
  fname = strtrim(fname);
×
740
  std::ofstream of;
×
741

742
  of.open(fname);
×
743

744
  // Write header
745
  of << "P6\n";
×
746
  of << data.shape()[0] << " " << data.shape()[1] << "\n";
×
747
  of << "255\n";
×
748
  of.close();
×
749

750
  of.open(fname, std::ios::binary | std::ios::app);
×
751
  // Write color for each pixel
752
  for (int y = 0; y < data.shape()[1]; y++) {
×
753
    for (int x = 0; x < data.shape()[0]; x++) {
×
754
      RGBColor rgb = data(x, y);
×
755
      of << rgb.red << rgb.green << rgb.blue;
×
756
    }
757
  }
758
  of << "\n";
×
759
}
760

761
//==============================================================================
762
// OUTPUT_PNG writes out a previously generated image to a PNG file
763
//==============================================================================
764

765
#ifdef USE_LIBPNG
766
void output_png(const std::string& filename, const ImageData& data)
630✔
767
{
768
  // Open PNG file for writing
769
  std::string fname = filename;
630✔
770
  fname = strtrim(fname);
630✔
771
  auto fp = std::fopen(fname.c_str(), "wb");
630✔
772

773
  // Initialize write and info structures
774
  auto png_ptr =
775
    png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
630✔
776
  auto info_ptr = png_create_info_struct(png_ptr);
630✔
777

778
  // Setup exception handling
779
  if (setjmp(png_jmpbuf(png_ptr)))
630✔
780
    fatal_error("Error during png creation");
×
781

782
  png_init_io(png_ptr, fp);
630✔
783

784
  // Write header (8 bit colour depth)
785
  int width = data.shape()[0];
630✔
786
  int height = data.shape()[1];
630✔
787
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
630✔
788
    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
789
  png_write_info(png_ptr, info_ptr);
630✔
790

791
  // Allocate memory for one row (3 bytes per pixel - RGB)
792
  std::vector<png_byte> row(3 * width);
630✔
793

794
  // Write color for each pixel
795
  for (int y = 0; y < height; y++) {
110,838✔
796
    for (int x = 0; x < width; x++) {
23,779,476✔
797
      RGBColor rgb = data(x, y);
23,669,268✔
798
      row[3 * x] = rgb.red;
23,669,268✔
799
      row[3 * x + 1] = rgb.green;
23,669,268✔
800
      row[3 * x + 2] = rgb.blue;
23,669,268✔
801
    }
802
    png_write_row(png_ptr, row.data());
110,208✔
803
  }
804

805
  // End write
806
  png_write_end(png_ptr, nullptr);
630✔
807

808
  // Clean up data structures
809
  std::fclose(fp);
630✔
810
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
630✔
811
  png_destroy_write_struct(&png_ptr, &info_ptr);
630✔
812
}
630✔
813
#endif
814

815
//==============================================================================
816
// DRAW_MESH_LINES draws mesh line boundaries on an image
817
//==============================================================================
818

819
void Plot::draw_mesh_lines(ImageData& data) const
42✔
820
{
821
  RGBColor rgb;
42✔
822
  rgb = meshlines_color_;
42✔
823

824
  int ax1, ax2;
825
  switch (basis_) {
42✔
826
  case PlotBasis::xy:
28✔
827
    ax1 = 0;
28✔
828
    ax2 = 1;
28✔
829
    break;
28✔
830
  case PlotBasis::xz:
14✔
831
    ax1 = 0;
14✔
832
    ax2 = 2;
14✔
833
    break;
14✔
834
  case PlotBasis::yz:
×
835
    ax1 = 1;
×
836
    ax2 = 2;
×
837
    break;
×
838
  default:
×
839
    UNREACHABLE();
×
840
  }
841

842
  Position ll_plot {origin_};
42✔
843
  Position ur_plot {origin_};
42✔
844

845
  ll_plot[ax1] -= width_[0] / 2.;
42✔
846
  ll_plot[ax2] -= width_[1] / 2.;
42✔
847
  ur_plot[ax1] += width_[0] / 2.;
42✔
848
  ur_plot[ax2] += width_[1] / 2.;
42✔
849

850
  Position width = ur_plot - ll_plot;
42✔
851

852
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
853
  auto axis_lines =
854
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
42✔
855

856
  // Find the bounds along the second axis (accounting for low-D meshes).
857
  int ax2_min, ax2_max;
858
  if (axis_lines.second.size() > 0) {
42✔
859
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
42✔
860
    ax2_min = (1.0 - frac) * pixels_[1];
42✔
861
    if (ax2_min < 0)
42✔
862
      ax2_min = 0;
×
863
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
42✔
864
    ax2_max = (1.0 - frac) * pixels_[1];
42✔
865
    if (ax2_max > pixels_[1])
42✔
866
      ax2_max = pixels_[1];
×
867
  } else {
868
    ax2_min = 0;
×
869
    ax2_max = pixels_[1];
×
870
  }
871

872
  // Iterate across the first axis and draw lines.
873
  for (auto ax1_val : axis_lines.first) {
238✔
874
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
196✔
875
    int ax1_ind = frac * pixels_[0];
196✔
876
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
31,752✔
877
      for (int plus = 0; plus <= meshlines_width_; plus++) {
63,112✔
878
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels_[0])
31,556✔
879
          data(ax1_ind + plus, ax2_ind) = rgb;
31,556✔
880
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels_[0])
31,556✔
881
          data(ax1_ind - plus, ax2_ind) = rgb;
31,556✔
882
      }
883
    }
884
  }
885

886
  // Find the bounds along the first axis.
887
  int ax1_min, ax1_max;
888
  if (axis_lines.first.size() > 0) {
42✔
889
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
42✔
890
    ax1_min = frac * pixels_[0];
42✔
891
    if (ax1_min < 0)
42✔
892
      ax1_min = 0;
×
893
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
42✔
894
    ax1_max = frac * pixels_[0];
42✔
895
    if (ax1_max > pixels_[0])
42✔
896
      ax1_max = pixels_[0];
×
897
  } else {
898
    ax1_min = 0;
×
899
    ax1_max = pixels_[0];
×
900
  }
901

902
  // Iterate across the second axis and draw lines.
903
  for (auto ax2_val : axis_lines.second) {
266✔
904
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
224✔
905
    int ax2_ind = (1.0 - frac) * pixels_[1];
224✔
906
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
36,064✔
907
      for (int plus = 0; plus <= meshlines_width_; plus++) {
71,680✔
908
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels_[1])
35,840✔
909
          data(ax1_ind, ax2_ind + plus) = rgb;
35,840✔
910
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels_[1])
35,840✔
911
          data(ax1_ind, ax2_ind - plus) = rgb;
35,840✔
912
      }
913
    }
914
  }
915
}
42✔
916

917
/* outputs a binary file that can be input into silomesh for 3D geometry
918
 * visualization.  It works the same way as create_image by dragging a particle
919
 * across the geometry for the specified number of voxels. The first 3 int's in
920
 * the binary are the number of x, y, and z voxels.  The next 3 double's are
921
 * the widths of the voxels in the x, y, and z directions. The next 3 double's
922
 * are the x, y, and z coordinates of the lower left point. Finally the binary
923
 * is filled with entries of four int's each. Each 'row' in the binary contains
924
 * four int's: 3 for x,y,z position and 1 for cell or material id.  For 1
925
 * million voxels this produces a file of approximately 15MB.
926
 */
927
void Plot::create_voxel() const
70✔
928
{
929
  // compute voxel widths in each direction
930
  array<double, 3> vox;
931
  vox[0] = width_[0] / static_cast<double>(pixels_[0]);
70✔
932
  vox[1] = width_[1] / static_cast<double>(pixels_[1]);
70✔
933
  vox[2] = width_[2] / static_cast<double>(pixels_[2]);
70✔
934

935
  // initial particle position
936
  Position ll = origin_ - width_ / 2.;
70✔
937

938
  // Open binary plot file for writing
939
  std::ofstream of;
70✔
940
  std::string fname = std::string(path_plot_);
70✔
941
  fname = strtrim(fname);
70✔
942
  hid_t file_id = file_open(fname, 'w');
70✔
943

944
  // write header info
945
  write_attribute(file_id, "filetype", "voxel");
70✔
946
  write_attribute(file_id, "version", VERSION_VOXEL);
70✔
947
  write_attribute(file_id, "openmc_version", VERSION);
70✔
948

949
#ifdef GIT_SHA1
950
  write_attribute(file_id, "git_sha1", GIT_SHA1);
951
#endif
952

953
  // Write current date and time
954
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
70✔
955
  array<int, 3> pixels;
956
  std::copy(pixels_.begin(), pixels_.end(), pixels.begin());
70✔
957
  write_attribute(file_id, "num_voxels", pixels);
70✔
958
  write_attribute(file_id, "voxel_width", vox);
70✔
959
  write_attribute(file_id, "lower_left", ll);
70✔
960

961
  // Create dataset for voxel data -- note that the dimensions are reversed
962
  // since we want the order in the file to be z, y, x
963
  hsize_t dims[3];
964
  dims[0] = pixels_[2];
70✔
965
  dims[1] = pixels_[1];
70✔
966
  dims[2] = pixels_[0];
70✔
967
  hid_t dspace, dset, memspace;
968
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
70✔
969

970
  SlicePlotBase pltbase;
70✔
971
  pltbase.width_ = width_;
70✔
972
  pltbase.origin_ = origin_;
70✔
973
  pltbase.basis_ = PlotBasis::xy;
70✔
974
  pltbase.pixels_ = pixels_;
70✔
975
  pltbase.slice_color_overlaps_ = color_overlaps_;
70✔
976

977
  ProgressBar pb;
70✔
978
  for (int z = 0; z < pixels_[2]; z++) {
6,090✔
979
    // update z coordinate
980
    pltbase.origin_.z = ll.z + z * vox[2];
6,020✔
981

982
    // generate ids using plotbase
983
    IdData ids = pltbase.get_map<IdData>();
6,020✔
984

985
    // select only cell/material ID data and flip the y-axis
986
    int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
6,020✔
987
    xt::xtensor<int32_t, 2> data_slice =
988
      xt::view(ids.data_, xt::all(), xt::all(), idx);
6,020✔
989
    xt::xtensor<int32_t, 2> data_flipped = xt::flip(data_slice, 0);
6,020✔
990

991
    // Write to HDF5 dataset
992
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
6,020✔
993

994
    // update progress bar
995
    pb.set_value(
6,020✔
996
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels_[2])));
6,020✔
997
  }
6,020✔
998

999
  voxel_finalize(dspace, dset, memspace);
70✔
1000
  file_close(file_id);
70✔
1001
}
70✔
1002

1003
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
70✔
1004
  hid_t* memspace)
1005
{
1006
  // Create dataspace/dataset for voxel data
1007
  *dspace = H5Screate_simple(3, dims, nullptr);
70✔
1008
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
70✔
1009
    H5P_DEFAULT, H5P_DEFAULT);
1010

1011
  // Create dataspace for a slice of the voxel
1012
  hsize_t dims_slice[2] {dims[1], dims[2]};
70✔
1013
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
70✔
1014

1015
  // Select hyperslab in dataspace
1016
  hsize_t start[3] {0, 0, 0};
70✔
1017
  hsize_t count[3] {1, dims[1], dims[2]};
70✔
1018
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
70✔
1019
}
70✔
1020

1021
void voxel_write_slice(
6,020✔
1022
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1023
{
1024
  hssize_t offset[3] {x, 0, 0};
6,020✔
1025
  H5Soffset_simple(dspace, offset);
6,020✔
1026
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
6,020✔
1027
}
6,020✔
1028

1029
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
70✔
1030
{
1031
  H5Dclose(dset);
70✔
1032
  H5Sclose(dspace);
70✔
1033
  H5Sclose(memspace);
70✔
1034
}
70✔
1035

1036
RGBColor random_color(void)
4,128✔
1037
{
1038
  return {int(prn(&model::plotter_seed) * 255),
4,128✔
1039
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
4,128✔
1040
}
1041

1042
RayTracePlot::RayTracePlot(pugi::xml_node node) : PlottableInterface(node)
112✔
1043
{
1044
  set_look_at(node);
112✔
1045
  set_camera_position(node);
112✔
1046
  set_field_of_view(node);
112✔
1047
  set_pixels(node);
112✔
1048
  set_orthographic_width(node);
112✔
1049
  set_output_path(node);
112✔
1050

1051
  if (check_for_node(node, "orthographic_width") &&
126✔
1052
      check_for_node(node, "field_of_view"))
14✔
1053
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1054
                "parameters.");
1055

1056
  // Get centerline vector for camera-to-model. We create vectors around this
1057
  // that form a pixel array, and then trace rays along that.
1058
  auto up = up_ / up_.norm();
112✔
1059
  Direction looking_direction = look_at_ - camera_position_;
112✔
1060
  looking_direction /= looking_direction.norm();
112✔
1061
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
112✔
1062
    fatal_error("Up vector cannot align with vector between camera position "
×
1063
                "and look_at!");
1064
  Direction cam_yaxis = looking_direction.cross(up);
112✔
1065
  cam_yaxis /= cam_yaxis.norm();
112✔
1066
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
112✔
1067
  cam_zaxis /= cam_zaxis.norm();
112✔
1068

1069
  // Cache the camera-to-model matrix
1070
  camera_to_model_ = {looking_direction.x, cam_yaxis.x, cam_zaxis.x,
112✔
1071
    looking_direction.y, cam_yaxis.y, cam_zaxis.y, looking_direction.z,
112✔
1072
    cam_yaxis.z, cam_zaxis.z};
112✔
1073
}
112✔
1074

1075
WireframeRayTracePlot::WireframeRayTracePlot(pugi::xml_node node)
70✔
1076
  : RayTracePlot(node)
70✔
1077
{
1078
  set_opacities(node);
70✔
1079
  set_wireframe_thickness(node);
70✔
1080
  set_wireframe_ids(node);
70✔
1081
  set_wireframe_color(node);
70✔
1082
}
70✔
1083

1084
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
70✔
1085
{
1086
  // Copy plot wireframe color
1087
  if (check_for_node(plot_node, "wireframe_color")) {
70✔
1088
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
1089
    if (w_rgb.size() == 3) {
×
1090
      wireframe_color_ = w_rgb;
×
1091
    } else {
1092
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1093
    }
1094
  }
1095
}
70✔
1096

1097
void RayTracePlot::set_output_path(pugi::xml_node node)
112✔
1098
{
1099
  // Set output file path
1100
  std::string filename;
112✔
1101

1102
  if (check_for_node(node, "filename")) {
112✔
1103
    filename = get_node_value(node, "filename");
98✔
1104
  } else {
1105
    filename = fmt::format("plot_{}", id());
28✔
1106
  }
1107

1108
#ifdef USE_LIBPNG
1109
  if (!file_extension_present(filename, "png"))
112✔
1110
    filename.append(".png");
42✔
1111
#else
1112
  if (!file_extension_present(filename, "ppm"))
1113
    filename.append(".ppm");
1114
#endif
1115
  path_plot_ = filename;
112✔
1116
}
112✔
1117

1118
bool WireframeRayTracePlot::trackstack_equivalent(
3,870,566✔
1119
  const std::vector<TrackSegment>& track1,
1120
  const std::vector<TrackSegment>& track2) const
1121
{
1122
  if (wireframe_ids_.empty()) {
3,870,566✔
1123
    // Draw wireframe for all surfaces/cells/materials
1124
    if (track1.size() != track2.size())
3,239,180✔
1125
      return false;
68,698✔
1126
    for (int i = 0; i < track1.size(); ++i) {
8,537,396✔
1127
      if (track1[i].id != track2[i].id ||
10,784,340✔
1128
          track1[i].surface_index != track2[i].surface_index) {
5,392,086✔
1129
        return false;
25,340✔
1130
      }
1131
    }
1132
    return true;
3,145,142✔
1133
  } else {
1134
    // This runs in O(nm) where n is the intersection stack size
1135
    // and m is the number of IDs we are wireframing. A simpler
1136
    // algorithm can likely be found.
1137
    for (const int id : wireframe_ids_) {
1,255,156✔
1138
      int t1_i = 0;
631,386✔
1139
      int t2_i = 0;
631,386✔
1140

1141
      // Advance to first instance of the ID
1142
      while (t1_i < track1.size() && t2_i < track2.size()) {
715,820✔
1143
        while (t1_i < track1.size() && track1[t1_i].id != id)
499,968✔
1144
          t1_i++;
291,522✔
1145
        while (t2_i < track2.size() && track2[t2_i].id != id)
501,032✔
1146
          t2_i++;
292,586✔
1147

1148
        // This one is really important!
1149
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
504,658✔
1150
            (t1_i != track1.size() && t2_i == track2.size()))
296,212✔
1151
          return false;
7,616✔
1152
        if (t1_i == track1.size() && t2_i == track2.size())
203,714✔
1153
          break;
116,396✔
1154
        // Check if surface different
1155
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
87,318✔
1156
          return false;
1,890✔
1157

1158
        // Pretty sure this should not be used:
1159
        // if (t2_i != track2.size() - 1 &&
1160
        //     t1_i != track1.size() - 1 &&
1161
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1162
        if (t2_i != 0 && t1_i != 0 &&
154,084✔
1163
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
68,656✔
1164
          return false;
994✔
1165

1166
        // Check if neighboring cells are different
1167
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1168
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1169
        // ].id !=
1170
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1171
        //    false;
1172
        t1_i++, t2_i++;
84,434✔
1173
      }
1174
    }
1175
    return true;
623,770✔
1176
  }
1177
}
1178

1179
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
4,480,000✔
1180
  int horiz, int vert) const
1181
{
1182
  // Compute field of view in radians
1183
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
4,480,000✔
1184
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
4,480,000✔
1185
  double p0 = static_cast<double>(pixels_[0]);
4,480,000✔
1186
  double p1 = static_cast<double>(pixels_[1]);
4,480,000✔
1187
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
4,480,000✔
1188

1189
  // focal_plane_dist can be changed to alter the perspective distortion
1190
  // effect. This is in units of cm. This seems to look good most of the
1191
  // time. TODO let this variable be set through XML.
1192
  constexpr double focal_plane_dist = 10.0;
4,480,000✔
1193
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
4,480,000✔
1194
  const double dy = p1 / p0 * dx;
4,480,000✔
1195

1196
  std::pair<Position, Direction> result;
4,480,000✔
1197

1198
  // Generate the starting position/direction of the ray
1199
  if (orthographic_width_ == C_NONE) { // perspective projection
4,480,000✔
1200
    Direction camera_local_vec;
3,920,000✔
1201
    camera_local_vec.x = focal_plane_dist;
3,920,000✔
1202
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
3,920,000✔
1203
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
3,920,000✔
1204
    camera_local_vec /= camera_local_vec.norm();
3,920,000✔
1205

1206
    result.first = camera_position_;
3,920,000✔
1207
    result.second = camera_local_vec.rotate(camera_to_model_);
3,920,000✔
1208
  } else { // orthographic projection
1209

1210
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
560,000✔
1211
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
560,000✔
1212

1213
    result.first = camera_position_ +
1214
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
560,000✔
1215
                   camera_z_axis() * y_pix_coord * orthographic_width_;
560,000✔
1216
    result.second = camera_x_axis();
560,000✔
1217
  }
1218

1219
  return result;
4,480,000✔
1220
}
1221

1222
void WireframeRayTracePlot::create_output() const
70✔
1223
{
1224
  size_t width = pixels_[0];
70✔
1225
  size_t height = pixels_[1];
70✔
1226
  ImageData data({width, height}, not_found_);
70✔
1227

1228
  // This array marks where the initial wireframe was drawn. We convolve it with
1229
  // a filter that gets adjusted with the wireframe thickness in order to
1230
  // thicken the lines.
1231
  xt::xtensor<int, 2> wireframe_initial({width, height}, 0);
70✔
1232

1233
  /* Holds all of the track segments for the current rendered line of pixels.
1234
   * old_segments holds a copy of this_line_segments from the previous line.
1235
   * By holding both we can check if the cell/material intersection stack
1236
   * differs from the left or upper neighbor. This allows a robustly drawn
1237
   * wireframe. If only checking the left pixel (which requires substantially
1238
   * less memory), the wireframe tends to be spotty and be disconnected for
1239
   * surface edges oriented horizontally in the rendering.
1240
   *
1241
   * Note that a vector of vectors is required rather than a 2-tensor,
1242
   * since the stack size varies within each column.
1243
   */
1244
  const int n_threads = num_threads();
70✔
1245
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1246
    n_threads);
70✔
1247
  for (int t = 0; t < n_threads; ++t) {
170✔
1248
    this_line_segments[t].resize(pixels_[0]);
100✔
1249
  }
1250

1251
  // The last thread writes to this, and the first thread reads from it.
1252
  std::vector<std::vector<TrackSegment>> old_segments(pixels_[0]);
70✔
1253

1254
#pragma omp parallel
30✔
1255
  {
1256
    const int n_threads = num_threads();
40✔
1257
    const int tid = thread_num();
40✔
1258

1259
    int vert = tid;
40✔
1260
    for (int iter = 0; iter <= pixels_[1] / n_threads; iter++) {
8,080✔
1261

1262
      // Save bottom line of current work chunk to compare against later. This
1263
      // used to be inside the below if block, but it causes a spurious line to
1264
      // be drawn at the bottom of the image. Not sure why, but moving it here
1265
      // fixes things.
1266
      if (tid == n_threads - 1)
8,040✔
1267
        old_segments = this_line_segments[n_threads - 1];
8,040✔
1268

1269
      if (vert < pixels_[1]) {
8,040✔
1270

1271
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,608,000✔
1272

1273
          // RayTracePlot implements camera ray generation
1274
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
1,600,000✔
1275

1276
          this_line_segments[tid][horiz].clear();
1,600,000✔
1277
          ProjectionRay ray(
1278
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
1,600,000✔
1279

1280
          ray.trace();
1,600,000✔
1281

1282
          // Now color the pixel based on what we have intersected...
1283
          // Loops backwards over intersections.
1284
          Position current_color(
1285
            not_found_.red, not_found_.green, not_found_.blue);
1,600,000✔
1286
          const auto& segments = this_line_segments[tid][horiz];
1,600,000✔
1287

1288
          // There must be at least two cell intersections to color, front and
1289
          // back of the cell. Maybe an infinitely thick cell could be present
1290
          // with no back, but why would you want to color that? It's easier to
1291
          // just skip that edge case and not even color it.
1292
          if (segments.size() <= 1)
1,600,000✔
1293
            continue;
986,648✔
1294

1295
          for (int i = segments.size() - 2; i >= 0; --i) {
1,715,736✔
1296
            int colormap_idx = segments[i].id;
1,102,384✔
1297
            RGBColor seg_color = colors_[colormap_idx];
1,102,384✔
1298
            Position seg_color_vec(
1299
              seg_color.red, seg_color.green, seg_color.blue);
1,102,384✔
1300
            double mixing =
1301
              std::exp(-xs_[colormap_idx] *
1,102,384✔
1302
                       (segments[i + 1].length - segments[i].length));
1,102,384✔
1303
            current_color =
1304
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
1,102,384✔
1305
          }
1306

1307
          // save result converting from double-precision color coordinates to
1308
          // byte-sized
1309
          RGBColor result;
613,352✔
1310
          result.red = static_cast<uint8_t>(current_color.x);
613,352✔
1311
          result.green = static_cast<uint8_t>(current_color.y);
613,352✔
1312
          result.blue = static_cast<uint8_t>(current_color.z);
613,352✔
1313
          data(horiz, vert) = result;
613,352✔
1314

1315
          // Check to draw wireframe in horizontal direction. No inter-thread
1316
          // comm.
1317
          if (horiz > 0) {
613,352✔
1318
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
611,752✔
1319
                  this_line_segments[tid][horiz - 1])) {
611,752✔
1320
              wireframe_initial(horiz, vert) = 1;
25,136✔
1321
            }
1322
          }
1323
        }
1,600,000✔
1324
      } // end "if" vert in correct range
1325

1326
      // We require a barrier before comparing vertical neighbors' intersection
1327
      // stacks. i.e. all threads must be done with their line.
1328
#pragma omp barrier
1329

1330
      // Now that the horizontal line has finished rendering, we can fill in
1331
      // wireframe entries that require comparison among all the threads. Hence
1332
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1333
      if (vert < pixels_[1]) {
8,040✔
1334
        // Loop over horizontal pixels, checking intersection stack of upper
1335
        // neighbor
1336

1337
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
8,000✔
1338
        if (tid == 0)
8,000✔
1339
          top_cmp = &old_segments;
8,000✔
1340
        else
1341
          top_cmp = &this_line_segments[tid - 1];
1342

1343
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,608,000✔
1344
          if (!trackstack_equivalent(
1,600,000✔
1345
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
1,600,000✔
1346
            wireframe_initial(horiz, vert) = 1;
32,952✔
1347
          }
1348
        }
1349
      }
1350

1351
      // We need another barrier to ensure threads don't proceed to modify their
1352
      // intersection stacks on that horizontal line while others are
1353
      // potentially still working on the above.
1354
#pragma omp barrier
1355
      vert += n_threads;
8,040✔
1356
    }
1357
  } // end omp parallel
1358

1359
  // Now thicken the wireframe lines and apply them to our image
1360
  for (int vert = 0; vert < pixels_[1]; ++vert) {
14,070✔
1361
    for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
2,814,000✔
1362
      if (wireframe_initial(horiz, vert)) {
2,800,000✔
1363
        if (wireframe_thickness_ == 1)
90,342✔
1364
          data(horiz, vert) = wireframe_color_;
38,430✔
1365
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
249,102✔
1366
             ++i)
1367
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
696,024✔
1368
               ++j)
1369
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
537,264✔
1370

1371
              // Check if wireframe pixel is out of bounds
1372
              int w_i = std::max(std::min(horiz + i, pixels_[0] - 1), 0);
537,264✔
1373
              int w_j = std::max(std::min(vert + j, pixels_[1] - 1), 0);
537,264✔
1374
              data(w_i, w_j) = wireframe_color_;
537,264✔
1375
            }
1376
      }
1377
    }
1378
  }
1379

1380
#ifdef USE_LIBPNG
1381
  output_png(path_plot(), data);
70✔
1382
#else
1383
  output_ppm(path_plot(), data);
1384
#endif
1385
}
70✔
1386

1387
void RayTracePlot::print_info() const
112✔
1388
{
1389
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
96✔
1390
    camera_position_.y, camera_position_.z);
112✔
1391
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
208✔
1392
  fmt::print(
96✔
1393
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
112✔
1394
  fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
208✔
1395
}
112✔
1396

1397
void WireframeRayTracePlot::print_info() const
70✔
1398
{
1399
  fmt::print("Plot Type: Wireframe ray-traced\n");
70✔
1400
  RayTracePlot::print_info();
70✔
1401
}
70✔
1402

1403
void WireframeRayTracePlot::set_opacities(pugi::xml_node node)
70✔
1404
{
1405
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
70✔
1406

1407
  for (auto cn : node.children("color")) {
154✔
1408
    // Make sure 3 values are specified for RGB
1409
    double user_xs = std::stod(get_node_value(cn, "xs"));
84✔
1410
    int col_id = std::stoi(get_node_value(cn, "id"));
84✔
1411

1412
    // Add RGB
1413
    if (PlotColorBy::cells == color_by_) {
84✔
1414
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
84✔
1415
        col_id = model::cell_map[col_id];
84✔
1416
        xs_[col_id] = user_xs;
84✔
1417
      } else {
1418
        warning(fmt::format(
×
1419
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1420
      }
1421
    } else if (PlotColorBy::mats == color_by_) {
×
1422
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
1423
        col_id = model::material_map[col_id];
×
1424
        xs_[col_id] = user_xs;
×
1425
      } else {
1426
        warning(fmt::format(
×
1427
          "Could not find material {} specified in plot {}", col_id, id()));
×
1428
      }
1429
    }
1430
  }
1431
}
70✔
1432

1433
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
112✔
1434
{
1435
  if (check_for_node(node, "orthographic_width")) {
112✔
1436
    double orthographic_width =
1437
      std::stod(get_node_value(node, "orthographic_width", true));
14✔
1438
    if (orthographic_width < 0.0)
14✔
1439
      fatal_error("Requires positive orthographic_width");
×
1440
    orthographic_width_ = orthographic_width;
14✔
1441
  }
1442
}
112✔
1443

1444
void WireframeRayTracePlot::set_wireframe_thickness(pugi::xml_node node)
70✔
1445
{
1446
  if (check_for_node(node, "wireframe_thickness")) {
70✔
1447
    int wireframe_thickness =
1448
      std::stoi(get_node_value(node, "wireframe_thickness", true));
28✔
1449
    if (wireframe_thickness < 0)
28✔
1450
      fatal_error("Requires non-negative wireframe thickness");
×
1451
    wireframe_thickness_ = wireframe_thickness;
28✔
1452
  }
1453
}
70✔
1454

1455
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
70✔
1456
{
1457
  if (check_for_node(node, "wireframe_ids")) {
70✔
1458
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
14✔
1459
    // It is read in as actual ID values, but we have to convert to indices in
1460
    // mat/cell array
1461
    for (auto& x : wireframe_ids_)
28✔
1462
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
14✔
1463
                                         : model::cell_map[x];
×
1464
  }
1465
  // We make sure the list is sorted in order to later use
1466
  // std::binary_search.
1467
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
70✔
1468
}
70✔
1469

1470
void RayTracePlot::set_pixels(pugi::xml_node node)
112✔
1471
{
1472
  vector<int> pxls = get_node_array<int>(node, "pixels");
112✔
1473
  if (pxls.size() != 2)
112✔
1474
    fatal_error(
×
1475
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1476
  pixels_[0] = pxls[0];
112✔
1477
  pixels_[1] = pxls[1];
112✔
1478
}
112✔
1479

1480
void RayTracePlot::set_camera_position(pugi::xml_node node)
112✔
1481
{
1482
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
112✔
1483
  if (camera_pos.size() != 3) {
112✔
1484
    fatal_error(fmt::format(
×
1485
      "camera_position element must have three floating point values"));
1486
  }
1487
  camera_position_.x = camera_pos[0];
112✔
1488
  camera_position_.y = camera_pos[1];
112✔
1489
  camera_position_.z = camera_pos[2];
112✔
1490
}
112✔
1491

1492
void RayTracePlot::set_look_at(pugi::xml_node node)
112✔
1493
{
1494
  vector<double> look_at = get_node_array<double>(node, "look_at");
112✔
1495
  if (look_at.size() != 3) {
112✔
1496
    fatal_error("look_at element must have three floating point values");
×
1497
  }
1498
  look_at_.x = look_at[0];
112✔
1499
  look_at_.y = look_at[1];
112✔
1500
  look_at_.z = look_at[2];
112✔
1501
}
112✔
1502

1503
void RayTracePlot::set_field_of_view(pugi::xml_node node)
112✔
1504
{
1505
  // Defaults to 70 degree horizontal field of view (see .h file)
1506
  if (check_for_node(node, "horizontal_field_of_view")) {
112✔
1507
    double fov =
NEW
1508
      std::stod(get_node_value(node, "horizontal_field_of_view", true));
×
UNCOV
1509
    if (fov < 180.0 && fov > 0.0) {
×
UNCOV
1510
      horizontal_field_of_view_ = fov;
×
1511
    } else {
NEW
1512
      fatal_error(fmt::format(
×
NEW
1513
        "Horizontal field of view for plot {} out-of-range. Must be in (0, 180) degrees.", id()));
×
1514
    }
1515
  }
1516
}
112✔
1517

1518
SolidRayTracePlot::SolidRayTracePlot(pugi::xml_node node) : RayTracePlot(node)
42✔
1519
{
1520
  set_opaque_ids(node);
42✔
1521
  set_diffuse_fraction(node);
42✔
1522
  set_light_position(node);
42✔
1523
}
42✔
1524

1525
void SolidRayTracePlot::print_info() const
42✔
1526
{
1527
  fmt::print("Plot Type: Solid ray-traced\n");
42✔
1528
  RayTracePlot::print_info();
42✔
1529
}
42✔
1530

1531
void SolidRayTracePlot::create_output() const
42✔
1532
{
1533
  size_t width = pixels_[0];
42✔
1534
  size_t height = pixels_[1];
42✔
1535
  ImageData data({width, height}, not_found_);
42✔
1536

1537
#pragma omp parallel for schedule(dynamic) collapse(2)
18✔
1538
  for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
4,824✔
1539
    for (int vert = 0; vert < pixels_[1]; ++vert) {
964,800✔
1540
      // RayTracePlot implements camera ray generation
1541
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
960,000✔
1542
      PhongRay ray(ru.first, ru.second, *this);
960,000✔
1543
      ray.trace();
960,000✔
1544
      data(horiz, vert) = ray.result_color();
960,000✔
1545
    }
960,000✔
1546
  }
1547

1548
#ifdef USE_LIBPNG
1549
  output_png(path_plot(), data);
42✔
1550
#else
1551
  output_ppm(path_plot(), data);
1552
#endif
1553
}
42✔
1554

1555
void SolidRayTracePlot::set_opaque_ids(pugi::xml_node node)
42✔
1556
{
1557
  if (check_for_node(node, "opaque_ids")) {
42✔
1558
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
42✔
1559

1560
    // It is read in as actual ID values, but we have to convert to indices in
1561
    // mat/cell array
1562
    for (auto& x : opaque_ids_tmp)
126✔
1563
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
84✔
UNCOV
1564
                                         : model::cell_map[x];
×
1565

1566
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
42✔
1567
  }
42✔
1568
}
42✔
1569

1570
void SolidRayTracePlot::set_light_position(pugi::xml_node node)
42✔
1571
{
1572
  if (check_for_node(node, "light_position")) {
42✔
1573
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
14✔
1574

1575
    if (light_pos_tmp.size() != 3)
14✔
UNCOV
1576
      fatal_error("Light position must be given as 3D coordinates");
×
1577

1578
    light_location_.x = light_pos_tmp[0];
14✔
1579
    light_location_.y = light_pos_tmp[1];
14✔
1580
    light_location_.z = light_pos_tmp[2];
14✔
1581
  } else {
14✔
1582
    light_location_ = camera_position();
28✔
1583
  }
1584
}
42✔
1585

1586
void SolidRayTracePlot::set_diffuse_fraction(pugi::xml_node node)
42✔
1587
{
1588
  if (check_for_node(node, "diffuse_fraction")) {
42✔
1589
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
14✔
1590
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
14✔
UNCOV
1591
      fatal_error("Must have 0 <= diffuse fraction <= 1");
×
1592
    }
1593
  }
1594
}
42✔
1595

1596
void Ray::compute_distance()
3,833,844✔
1597
{
1598
  boundary() = distance_to_boundary(*this);
3,833,844✔
1599
}
3,833,844✔
1600

1601
void Ray::trace()
4,480,000✔
1602
{
1603
  // To trace the ray from its origin all the way through the model, we have
1604
  // to proceed in two phases. In the first, the ray may or may not be found
1605
  // inside the model. If the ray is already in the model, phase one can be
1606
  // skipped. Otherwise, the ray has to be advanced to the boundary of the
1607
  // model where all the cells are defined. Importantly, this is assuming that
1608
  // the model is convex, which is a very reasonable assumption for any
1609
  // radiation transport model.
1610
  //
1611
  // After phase one is done, we can starting tracing from cell to cell within
1612
  // the model. This step can use neighbor lists to accelerate the ray tracing.
1613

1614
  // Attempt to initialize the particle. We may have to enter a loop to move
1615
  // it up to the edge of the model.
1616
  bool inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
4,480,000✔
1617

1618
  // Advance to the boundary of the model
1619
  while (!inside_cell) {
19,875,660✔
1620
    advance_to_boundary_from_void();
19,875,660✔
1621
    inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
19,875,660✔
1622

1623
    // If true this means no surface was intersected. See cell.cpp and search
1624
    // for numeric_limits to see where we return it.
1625
    if (surface() == std::numeric_limits<int>::max()) {
19,875,660✔
UNCOV
1626
      warning(fmt::format("Lost a ray, r = {}, u = {}", r(), u()));
×
1627
      return;
×
1628
    }
1629

1630
    // Exit this loop and enter into cell-to-cell ray tracing (which uses
1631
    // neighbor lists)
1632
    if (inside_cell)
19,875,660✔
1633
      break;
1,971,508✔
1634

1635
    // if there is no intersection with the model, we're done
1636
    if (boundary().surface == SURFACE_NONE)
17,904,152✔
1637
      return;
2,508,492✔
1638

1639
    event_counter_++;
15,395,660✔
1640
    if (event_counter_ > MAX_INTERSECTIONS) {
15,395,660✔
UNCOV
1641
      warning("Likely infinite loop in ray traced plot");
×
1642
      return;
×
1643
    }
1644
  }
1645

1646
  // Call the specialized logic for this type of ray. This is for the
1647
  // intersection for the first intersection if we had one.
1648
  if (boundary().surface != SURFACE_NONE) {
1,971,508✔
1649
    // set the geometry state's surface attribute to be used for
1650
    // surface normal computation
1651
    surface() = boundary().surface;
1,971,508✔
1652
    on_intersection();
1,971,508✔
1653
    if (stop_)
1,971,508✔
UNCOV
1654
      return;
×
1655
  }
1656

1657
  // reset surface attribute to zero after the first intersection so that it
1658
  // doesn't perturb surface crossing logic from here on out
1659
  surface() = 0;
1,971,508✔
1660

1661
  // This is the ray tracing loop within the model. It exits after exiting
1662
  // the model, which is equivalent to assuming that the model is convex.
1663
  // It would be nice to factor out the on_intersection at the end of this
1664
  // loop and then do "while (inside_cell)", but we can't guarantee it's
1665
  // on a surface in that case. There might be some other way to set it
1666
  // up that is perhaps a little more elegant, but this is what works just
1667
  // fine.
1668
  while (true) {
1669

1670
    compute_distance();
2,935,716✔
1671

1672
    // There are no more intersections to process
1673
    // if we hit the edge of the model, so stop
1674
    // the particle in that case. Also, just exit
1675
    // if a negative distance was somehow computed.
1676
    if (boundary().distance == INFTY || boundary().distance == INFINITY ||
5,871,432✔
1677
        boundary().distance < 0) {
2,935,716✔
UNCOV
1678
      return;
×
1679
    }
1680

1681
    // See below comment where call_on_intersection is checked in an
1682
    // if statement for an explanation of this.
1683
    bool call_on_intersection {true};
2,935,716✔
1684
    if (boundary().distance < 10 * TINY_BIT) {
2,935,716✔
1685
      call_on_intersection = false;
755,090✔
1686
    }
1687

1688
    // DAGMC surfaces expect us to go a little bit further than the advance
1689
    // distance to properly check cell inclusion.
1690
    boundary().distance += TINY_BIT;
2,935,716✔
1691

1692
    // Advance particle, prepare for next intersection
1693
    for (int lev = 0; lev < n_coord(); ++lev) {
5,871,432✔
1694
      coord(lev).r += boundary().distance * coord(lev).u;
2,935,716✔
1695
    }
1696
    surface() = boundary().surface;
2,935,716✔
1697
    n_coord_last() = n_coord();
2,935,716✔
1698
    n_coord() = boundary().coord_level;
2,935,716✔
1699
    if (boundary().lattice_translation[0] != 0 ||
2,935,716✔
1700
        boundary().lattice_translation[1] != 0 ||
5,871,432✔
1701
        boundary().lattice_translation[2] != 0) {
2,935,716✔
UNCOV
1702
      cross_lattice(*this, boundary(), settings::verbosity >= 10);
×
1703
    }
1704

1705
    // Record how far the ray has traveled
1706
    traversal_distance_ += boundary().distance;
2,935,716✔
1707
    inside_cell = neighbor_list_find_cell(*this, settings::verbosity >= 10);
2,935,716✔
1708

1709
    // Call the specialized logic for this type of ray. Note that we do not
1710
    // call this if the advance distance is very small. Unfortunately, it seems
1711
    // darn near impossible to get the particle advanced to the model boundary
1712
    // and through it without sometimes accidentally calling on_intersection
1713
    // twice. This incorrectly shades the region as occluded when it might not
1714
    // actually be. By screening out intersection distances smaller than a
1715
    // threshold 10x larger than the scoot distance used to advance up to the
1716
    // model boundary, we can avoid that situation.
1717
    if (call_on_intersection) {
2,935,716✔
1718
      on_intersection();
2,180,626✔
1719
      if (stop_)
2,180,626✔
1720
        return;
45,206✔
1721
    }
1722

1723
    if (!inside_cell)
2,890,510✔
1724
      return;
1,926,302✔
1725

1726
    event_counter_++;
964,208✔
1727
    if (event_counter_ > MAX_INTERSECTIONS) {
964,208✔
UNCOV
1728
      warning("Likely infinite loop in ray traced plot");
×
1729
      return;
×
1730
    }
1731
  }
964,208✔
1732
}
1733

1734
void ProjectionRay::on_intersection()
3,002,552✔
1735
{
1736
  // This records a tuple with the following info
1737
  //
1738
  // 1) ID (material or cell depending on color_by_)
1739
  // 2) Distance traveled by the ray through that ID
1740
  // 3) Index of the intersected surface (starting from 1)
1741

1742
  line_segments_.emplace_back(
3,002,552✔
1743
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
3,002,552✔
1744
      ? material()
694,806✔
1745
      : lowest_coord().cell,
2,307,746✔
1746
    traversal_distance_, boundary().surface_index());
3,002,552✔
1747
}
3,002,552✔
1748

1749
void PhongRay::on_intersection()
1,149,582✔
1750
{
1751
  // Check if we hit an opaque material or cell
1752
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
1,149,582✔
1753
                 ? material()
1,149,582✔
UNCOV
1754
                 : lowest_coord().cell;
×
1755

1756
  // If we are reflected and have advanced beyond the camera,
1757
  // the ray is done. This is checked here because we should
1758
  // kill the ray even if the material is not opaque.
1759
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
1,149,582✔
UNCOV
1760
    stop();
×
1761
    return;
206,248✔
1762
  }
1763

1764
  // Anything that's not opaque has zero impact on the plot.
1765
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
1,149,582✔
1766
    return;
206,248✔
1767

1768
  if (!reflected_) {
943,334✔
1769
    // reflect the particle and set the color to be colored by
1770
    // the normal or the diffuse lighting contribution
1771
    reflected_ = true;
898,128✔
1772
    result_color_ = plot_.colors_[hit_id];
898,128✔
1773
    Direction to_light = plot_.light_location_ - r();
898,128✔
1774
    to_light /= to_light.norm();
898,128✔
1775

1776
    // TODO
1777
    // Not sure what can cause a surface token to be invalid here, although it
1778
    // sometimes happens for a few pixels. It's very very rare, so proceed by
1779
    // coloring the pixel with the overlap color. It seems to happen only for a
1780
    // few pixels on the outer boundary of a hex lattice.
1781
    //
1782
    // We cannot detect it in the outer loop, and it only matters here, so
1783
    // that's why the error handling is a little different than for a lost
1784
    // ray.
1785
    if (surface() == 0) {
898,128✔
UNCOV
1786
      result_color_ = plot_.overlap_color_;
×
1787
      stop();
×
1788
      return;
×
1789
    }
1790

1791
    // Get surface pointer
1792
    const auto& surf = model::surfaces.at(surface_index());
898,128✔
1793

1794
    Direction normal = surf->normal(r_local());
898,128✔
1795
    normal /= normal.norm();
898,128✔
1796

1797
    // Need to apply translations to find the normal vector in
1798
    // the base level universe's coordinate system.
1799
    for (int lev = n_coord() - 2; lev >= 0; --lev) {
898,128✔
UNCOV
1800
      if (coord(lev + 1).rotated) {
×
1801
        const Cell& c {*model::cells[coord(lev).cell]};
×
1802
        normal = normal.inverse_rotate(c.rotation_);
×
1803
      }
1804
    }
1805

1806
    // use the normal opposed to the ray direction
1807
    if (normal.dot(u()) > 0.0) {
898,128✔
1808
      normal *= -1.0;
81,186✔
1809
    }
1810

1811
    // Facing away from the light means no lighting
1812
    double dotprod = normal.dot(to_light);
898,128✔
1813
    dotprod = std::max(0.0, dotprod);
898,128✔
1814

1815
    double modulation =
898,128✔
1816
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
898,128✔
1817
    result_color_ *= modulation;
898,128✔
1818

1819
    // Now point the particle to the camera. We now begin
1820
    // checking to see if it's occluded by another surface
1821
    u() = to_light;
898,128✔
1822

1823
    orig_hit_id_ = hit_id;
898,128✔
1824

1825
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1826
    // in how they interpret particles that are sitting on a surface.
1827
    // I don't know exactly why, but this makes everything work beautifully.
1828
    if (surf->geom_type() == GeometryType::DAG) {
898,128✔
UNCOV
1829
      surface() = 0;
×
1830
    } else {
1831
      surface() = -surface(); // go to other side
898,128✔
1832
    }
1833

1834
    // Must fully restart coordinate search. Why? Not sure.
1835
    clear();
898,128✔
1836

1837
    // Note this could likely be faster if we cached the previous
1838
    // cell we were in before the reflection. This is the easiest
1839
    // way to fully initialize all the sub-universe coordinates and
1840
    // directions though.
1841
    bool found = exhaustive_find_cell(*this);
898,128✔
1842
    if (!found) {
898,128✔
UNCOV
1843
      fatal_error("Lost particle after reflection.");
×
1844
    }
1845

1846
    // Must recalculate distance to boundary due to the
1847
    // direction change
1848
    compute_distance();
898,128✔
1849

1850
  } else {
1851
    // If it's not facing the light, we color with the diffuse contribution, so
1852
    // next we check if we're going to occlude the last reflected surface. if
1853
    // so, color by the diffuse contribution instead
1854

1855
    if (orig_hit_id_ == -1)
45,206✔
UNCOV
1856
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1857

1858
    result_color_ = plot_.colors_[orig_hit_id_];
45,206✔
1859
    result_color_ *= plot_.diffuse_fraction_;
45,206✔
1860
    stop();
45,206✔
1861
  }
1862
}
1863

1864
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
14✔
1865
{
1866

1867
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
14✔
1868
  if (!plt) {
14✔
UNCOV
1869
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
1870
    return OPENMC_E_INVALID_ARGUMENT;
×
1871
  }
1872

1873
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
14✔
UNCOV
1874
    model::overlap_check_count.resize(model::cells.size());
×
1875
  }
1876

1877
  auto ids = plt->get_map<IdData>();
14✔
1878

1879
  // write id data to array
1880
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
14✔
1881

1882
  return 0;
14✔
1883
}
14✔
1884

1885
extern "C" int openmc_property_map(const void* plot, double* data_out)
14✔
1886
{
1887

1888
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
14✔
1889
  if (!plt) {
14✔
UNCOV
1890
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
1891
    return OPENMC_E_INVALID_ARGUMENT;
×
1892
  }
1893

1894
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
14✔
UNCOV
1895
    model::overlap_check_count.resize(model::cells.size());
×
1896
  }
1897

1898
  auto props = plt->get_map<PropertyData>();
14✔
1899

1900
  // write id data to array
1901
  std::copy(props.data_.begin(), props.data_.end(), data_out);
14✔
1902

1903
  return 0;
14✔
1904
}
14✔
1905

1906
} // namespace openmc
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc