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

openmc-dev / openmc / 20316720587

17 Dec 2025 08:44PM UTC coverage: 82.14% (-0.009%) from 82.149%
20316720587

push

github

web-flow
using id_map in model.plot for more efficient plotting (#3678)

17007 of 23578 branches covered (72.13%)

Branch coverage included in aggregate %.

75 of 86 new or added lines in 2 files covered. (87.21%)

4 existing lines in 2 files now uncovered.

55112 of 64222 relevant lines covered (85.81%)

43476980.86 hits per line

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

75.07
/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)
5,118✔
48
{}
5,118✔
49

50
void IdData::set_value(size_t y, size_t x, const GeometryState& p, int level)
38,709,295✔
51
{
52
  // set cell data
53
  if (p.n_coord() <= level) {
38,709,295!
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_;
38,709,295✔
58
    data_(y, x, 1) = level == p.n_coord() - 1
77,418,590✔
59
                       ? p.cell_instance()
38,709,295!
60
                       : cell_instance_at_level(p, level);
×
61
  }
62

63
  // set material data
64
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
38,709,295✔
65
  if (p.material() == MATERIAL_VOID) {
38,709,295✔
66
    data_(y, x, 2) = MATERIAL_VOID;
29,804,276✔
67
    return;
29,804,276✔
68
  } else if (c->type_ == Fill::MATERIAL) {
8,905,019!
69
    Material* m = model::materials.at(p.material()).get();
8,905,019✔
70
    data_(y, x, 2) = m->id_;
8,905,019✔
71
  }
72
}
73

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

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

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

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

123
  return 0;
121✔
124
}
125

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

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

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

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

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

161
  if (PlotType::slice == type_) {
154✔
162
    switch (basis_) {
121!
163
    case PlotBasis::xy:
77✔
164
      fmt::print("Basis: XY\n");
63✔
165
      break;
77✔
166
    case PlotBasis::xz:
22✔
167
      fmt::print("Basis: XZ\n");
18✔
168
      break;
22✔
169
    case PlotBasis::yz:
22✔
170
      fmt::print("Basis: YZ\n");
18✔
171
      break;
22✔
172
    }
173
    fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
242✔
174
  } else if (PlotType::voxel == type_) {
33!
175
    fmt::print("Voxels: {} {} {}\n", pixels_[0], pixels_[1], pixels_[2]);
66✔
176
  }
177
}
154✔
178

179
void read_plots_xml()
1,371✔
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,371✔
185
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
1,371!
186
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
187
  }
188

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

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

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

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

200
void read_plots_xml(pugi::xml_node root)
1,682✔
201
{
202
  for (auto node : root.children("plot")) {
2,482✔
203
    std::string id_string = get_node_value(node, "id", true);
809✔
204
    int id = std::stoi(id_string);
809✔
205
    if (check_for_node(node, "type")) {
809!
206
      std::string type_str = get_node_value(node, "type", true);
809✔
207
      if (type_str == "slice") {
809✔
208
        model::plots.emplace_back(
657✔
209
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,323✔
210
      } else if (type_str == "voxel") {
143✔
211
        model::plots.emplace_back(
55✔
212
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
110✔
213
      } else if (type_str == "wireframe_raytrace") {
88✔
214
        model::plots.emplace_back(
55✔
215
          std::make_unique<WireframeRayTracePlot>(node));
110✔
216
      } else if (type_str == "solid_raytrace") {
33!
217
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
33✔
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;
800✔
223
    } else {
800✔
224
      fatal_error(fmt::format("Must specify plot type in plot {}", id));
×
225
    }
226
  }
800✔
227
}
1,673✔
228

229
void free_memory_plot()
7,707✔
230
{
231
  model::plots.clear();
7,707✔
232
  model::plot_map.clear();
7,707✔
233
}
7,707✔
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
143✔
238
{
239

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

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

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

248
  // assign colors
249
  for (size_t y = 0; y < height; y++) {
30,063✔
250
    for (size_t x = 0; x < width; x++) {
7,622,120✔
251
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
7,592,200✔
252
      auto id = ids.data_(y, x, idx);
7,592,200✔
253
      // no setting needed if not found
254
      if (id == NOT_FOUND) {
7,592,200✔
255
        continue;
1,082,532✔
256
      }
257
      if (id == OVERLAP) {
6,537,916✔
258
        data(x, y) = overlap_color_;
28,248✔
259
        continue;
28,248✔
260
      }
261
      if (PlotColorBy::cells == color_by_) {
6,509,668✔
262
        data(x, y) = colors_[model::cell_map[id]];
3,011,668✔
263
      } else if (PlotColorBy::mats == color_by_) {
3,498,000!
264
        if (id == MATERIAL_VOID) {
3,498,000!
265
          data(x, y) = WHITE;
×
266
          continue;
×
267
        }
268
        data(x, y) = colors_[model::material_map[id]];
3,498,000✔
269
      } // color_by if-else
270
    }
271
  }
272

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

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

286
void PlottableInterface::set_id(pugi::xml_node plot_node)
809✔
287
{
288
  // Copy data into plots
289
  if (check_for_node(plot_node, "id")) {
809!
290
    id_ = std::stoi(get_node_value(plot_node, "id"));
809✔
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()) {
809!
297
    fatal_error(
×
298
      fmt::format("Two or more plots use the same unique ID: {}", id_));
×
299
  }
300
}
809✔
301

302
// Checks if png or ppm is already present
303
bool file_extension_present(
800✔
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);
800✔
308
  if (file_extension_if_present == extension)
800✔
309
    return true;
55✔
310
  return false;
745✔
311
}
800✔
312

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

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

345
  path_plot_ = filename;
712✔
346

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

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

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

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

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

437
void PlottableInterface::set_universe(pugi::xml_node plot_node)
809✔
438
{
439
  // Copy plot universe level
440
  if (check_for_node(plot_node, "level")) {
809!
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;
809✔
447
  }
448
}
809✔
449

450
void PlottableInterface::set_default_colors(pugi::xml_node plot_node)
809✔
451
{
452
  // Copy plot color type and initialize all colors randomly
453
  std::string pl_color_by = "cell";
809✔
454
  if (check_for_node(plot_node, "color_by")) {
809✔
455
    pl_color_by = get_node_value(plot_node, "color_by", true);
776✔
456
  }
457
  if ("cell" == pl_color_by) {
809✔
458
    color_by_ = PlotColorBy::cells;
290✔
459
    colors_.resize(model::cells.size());
290✔
460
  } else if ("material" == pl_color_by) {
519!
461
    color_by_ = PlotColorBy::mats;
519✔
462
    colors_.resize(model::materials.size());
519✔
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_) {
3,734✔
469
    c = random_color();
2,925✔
470
    // make sure we don't interfere with some default colors
471
    while (c == RED || c == WHITE) {
2,925!
472
      c = random_color();
×
473
    }
474
  }
475
}
809✔
476

477
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
809✔
478
{
479
  for (auto cn : plot_node.children("color")) {
996✔
480
    // Make sure 3 values are specified for RGB
481
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
187✔
482
    if (user_rgb.size() != 3) {
187!
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")) {
187!
488
      col_id = std::stoi(get_node_value(cn, "id"));
187✔
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_) {
187✔
495
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
88!
496
        col_id = model::cell_map[col_id];
88✔
497
        colors_[col_id] = user_rgb;
88✔
498
      } else {
UNCOV
499
        warning(fmt::format(
×
UNCOV
500
          "Could not find cell {} specified in plot {}", col_id, id()));
×
501
      }
502
    } else if (PlotColorBy::mats == color_by_) {
99!
503
      if (model::material_map.find(col_id) != model::material_map.end()) {
99!
504
        col_id = model::material_map[col_id];
99✔
505
        colors_[col_id] = user_rgb;
99✔
506
      } else {
507
        warning(fmt::format(
×
508
          "Could not find material {} specified in plot {}", col_id, id()));
×
509
      }
510
    }
511
  } // color node loop
187✔
512
}
809✔
513

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

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

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

528
      // Check mesh type
529
      std::string meshtype;
33✔
530
      if (check_for_node(meshlines_node, "meshtype")) {
33!
531
        meshtype = get_node_value(meshlines_node, "meshtype");
33✔
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;
33✔
540
      if (check_for_node(meshlines_node, "linewidth")) {
33!
541
        meshline_width = get_node_value(meshlines_node, "linewidth");
33✔
542
        meshlines_width_ = std::stoi(meshline_width);
33✔
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")) {
33!
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) {
33!
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) {
33✔
578
        if (!simulation::entropy_mesh) {
22!
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) {
55✔
583
            if (const auto* m =
33✔
584
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
33!
585
              if (m == simulation::entropy_mesh) {
22!
586
                index_meshlines_mesh_ = i;
22✔
587
              }
588
            }
589
          }
590
          if (index_meshlines_mesh_ == -1)
22!
591
            fatal_error("Could not find the entropy mesh for meshlines plot");
×
592
        }
593
      } else if ("tally" == meshtype) {
11!
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")) {
11!
597
          tally_mesh_id = std::stoi(get_node_value(meshlines_node, "id"));
11✔
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);
11✔
607
        if (err != 0) {
11!
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;
11✔
613
      } else {
614
        fatal_error(fmt::format("Invalid type for meshlines on plot {}", id()));
×
615
      }
616
    } else {
33✔
617
      fatal_error(fmt::format("Mutliple meshlines specified in plot {}", id()));
×
618
    }
619
  }
620
}
712✔
621

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

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

632
      // Determine how many components there are and allocate
633
      vector<int> iarray = get_node_array<int>(mask_node, "components");
33✔
634
      if (iarray.size() == 0) {
33!
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) {
99✔
642
        if (PlotColorBy::cells == color_by_) {
66!
643
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
66!
644
            col_id = model::cell_map[col_id];
66✔
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++) {
132✔
663
        if (contains(iarray, j)) {
99✔
664
          if (check_for_node(mask_node, "background")) {
66!
665
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
66✔
666
            colors_[j] = bg_rgb;
66✔
667
          } else {
66✔
668
            colors_[j] = WHITE;
×
669
          }
670
        }
671
      }
672

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

679
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
809✔
680
{
681
  color_overlaps_ = false;
809✔
682
  if (check_for_node(plot_node, "show_overlaps")) {
809✔
683
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
22✔
684
    // check for custom overlap color
685
    if (check_for_node(plot_node, "overlap_color")) {
22✔
686
      if (!color_overlaps_) {
11!
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");
11✔
692
      if (olap_clr.size() == 3) {
11!
693
        overlap_color_ = olap_clr;
11✔
694
      } else {
695
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
696
      }
697
    }
11✔
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) {
809!
703
    settings::check_overlaps = true;
22✔
704
    model::overlap_check_count.resize(model::cells.size(), 0);
22✔
705
  }
706
}
809✔
707

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

719
Plot::Plot(pugi::xml_node plot_node, PlotType type)
721✔
720
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
721✔
721
{
722
  set_output_path(plot_node);
721✔
723
  set_basis(plot_node);
712✔
724
  set_origin(plot_node);
712✔
725
  set_width(plot_node);
712✔
726
  set_meshlines(plot_node);
712✔
727
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
712✔
728
  slice_color_overlaps_ = color_overlaps_;
712✔
729
}
712✔
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)
231✔
767
{
768
  // Open PNG file for writing
769
  std::string fname = filename;
231✔
770
  fname = strtrim(fname);
231✔
771
  auto fp = std::fopen(fname.c_str(), "wb");
231✔
772

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

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

782
  png_init_io(png_ptr, fp);
231✔
783

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

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

794
  // Write color for each pixel
795
  for (int y = 0; y < height; y++) {
47,751✔
796
    for (int x = 0; x < width; x++) {
11,159,720✔
797
      RGBColor rgb = data(x, y);
11,112,200✔
798
      row[3 * x] = rgb.red;
11,112,200✔
799
      row[3 * x + 1] = rgb.green;
11,112,200✔
800
      row[3 * x + 2] = rgb.blue;
11,112,200✔
801
    }
802
    png_write_row(png_ptr, row.data());
47,520✔
803
  }
804

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

808
  // Clean up data structures
809
  std::fclose(fp);
231✔
810
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
231✔
811
  png_destroy_write_struct(&png_ptr, &info_ptr);
231✔
812
}
231✔
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
33✔
820
{
821
  RGBColor rgb;
33✔
822
  rgb = meshlines_color_;
33✔
823

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

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

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

850
  Position width = ur_plot - ll_plot;
33✔
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);
33✔
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) {
33!
859
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
33✔
860
    ax2_min = (1.0 - frac) * pixels_[1];
33✔
861
    if (ax2_min < 0)
33!
862
      ax2_min = 0;
×
863
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
33✔
864
    ax2_max = (1.0 - frac) * pixels_[1];
33✔
865
    if (ax2_max > pixels_[1])
33!
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) {
187✔
874
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
154✔
875
    int ax1_ind = frac * pixels_[0];
154✔
876
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
24,948✔
877
      for (int plus = 0; plus <= meshlines_width_; plus++) {
49,588✔
878
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels_[0])
24,794!
879
          data(ax1_ind + plus, ax2_ind) = rgb;
24,794✔
880
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels_[0])
24,794!
881
          data(ax1_ind - plus, ax2_ind) = rgb;
24,794✔
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) {
33!
889
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
33✔
890
    ax1_min = frac * pixels_[0];
33✔
891
    if (ax1_min < 0)
33!
892
      ax1_min = 0;
×
893
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
33✔
894
    ax1_max = frac * pixels_[0];
33✔
895
    if (ax1_max > pixels_[0])
33!
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) {
209✔
904
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
176✔
905
    int ax2_ind = (1.0 - frac) * pixels_[1];
176✔
906
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
28,336✔
907
      for (int plus = 0; plus <= meshlines_width_; plus++) {
56,320✔
908
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels_[1])
28,160!
909
          data(ax1_ind, ax2_ind + plus) = rgb;
28,160✔
910
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels_[1])
28,160!
911
          data(ax1_ind, ax2_ind - plus) = rgb;
28,160✔
912
      }
913
    }
914
  }
915
}
33✔
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
55✔
928
{
929
  // compute voxel widths in each direction
930
  array<double, 3> vox;
931
  vox[0] = width_[0] / static_cast<double>(pixels_[0]);
55✔
932
  vox[1] = width_[1] / static_cast<double>(pixels_[1]);
55✔
933
  vox[2] = width_[2] / static_cast<double>(pixels_[2]);
55✔
934

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

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

944
  // write header info
945
  write_attribute(file_id, "filetype", "voxel");
55✔
946
  write_attribute(file_id, "version", VERSION_VOXEL);
55✔
947
  write_attribute(file_id, "openmc_version", VERSION);
55✔
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());
55✔
955
  array<int, 3> pixels;
956
  std::copy(pixels_.begin(), pixels_.end(), pixels.begin());
55✔
957
  write_attribute(file_id, "num_voxels", pixels);
55✔
958
  write_attribute(file_id, "voxel_width", vox);
55✔
959
  write_attribute(file_id, "lower_left", ll);
55✔
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];
55✔
965
  dims[1] = pixels_[1];
55✔
966
  dims[2] = pixels_[0];
55✔
967
  hid_t dspace, dset, memspace;
968
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
55✔
969

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

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

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

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

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

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

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

1003
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
55✔
1004
  hid_t* memspace)
1005
{
1006
  // Create dataspace/dataset for voxel data
1007
  *dspace = H5Screate_simple(3, dims, nullptr);
55✔
1008
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
55✔
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]};
55✔
1013
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
55✔
1014

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

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

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

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

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

1051
  if (check_for_node(node, "orthographic_width") &&
99!
1052
      check_for_node(node, "field_of_view"))
11!
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();
88✔
1059
  Direction looking_direction = look_at_ - camera_position_;
88✔
1060
  looking_direction /= looking_direction.norm();
88✔
1061
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
88!
1062
    fatal_error("Up vector cannot align with vector between camera position "
×
1063
                "and look_at!");
1064
  Direction cam_yaxis = looking_direction.cross(up);
88✔
1065
  cam_yaxis /= cam_yaxis.norm();
88✔
1066
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
88✔
1067
  cam_zaxis /= cam_zaxis.norm();
88✔
1068

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

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

1084
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
55✔
1085
{
1086
  // Copy plot wireframe color
1087
  if (check_for_node(plot_node, "wireframe_color")) {
55!
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
}
55✔
1096

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

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

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

1118
bool WireframeRayTracePlot::trackstack_equivalent(
3,041,159✔
1119
  const std::vector<TrackSegment>& track1,
1120
  const std::vector<TrackSegment>& track2) const
1121
{
1122
  if (wireframe_ids_.empty()) {
3,041,159✔
1123
    // Draw wireframe for all surfaces/cells/materials
1124
    if (track1.size() != track2.size())
2,545,070✔
1125
      return false;
53,977✔
1126
    for (int i = 0; i < track1.size(); ++i) {
6,707,954✔
1127
      if (track1[i].id != track2[i].id ||
8,473,410✔
1128
          track1[i].surface_index != track2[i].surface_index) {
4,236,639✔
1129
        return false;
19,910✔
1130
      }
1131
    }
1132
    return true;
2,471,183✔
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_) {
986,194✔
1138
      int t1_i = 0;
496,089✔
1139
      int t2_i = 0;
496,089✔
1140

1141
      // Advance to first instance of the ID
1142
      while (t1_i < track1.size() && t2_i < track2.size()) {
562,430✔
1143
        while (t1_i < track1.size() && track1[t1_i].id != id)
392,832✔
1144
          t1_i++;
229,053✔
1145
        while (t2_i < track2.size() && track2[t2_i].id != id)
393,668✔
1146
          t2_i++;
229,889✔
1147

1148
        // This one is really important!
1149
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
396,517✔
1150
            (t1_i != track1.size() && t2_i == track2.size()))
232,738✔
1151
          return false;
5,984✔
1152
        if (t1_i == track1.size() && t2_i == track2.size())
160,061!
1153
          break;
91,454✔
1154
        // Check if surface different
1155
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
68,607✔
1156
          return false;
1,485✔
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 &&
121,066!
1163
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
53,944✔
1164
          return false;
781✔
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++;
66,341✔
1173
      }
1174
    }
1175
    return true;
490,105✔
1176
  }
1177
}
1178

1179
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
3,520,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;
3,520,000✔
1184
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
3,520,000✔
1185
  double p0 = static_cast<double>(pixels_[0]);
3,520,000✔
1186
  double p1 = static_cast<double>(pixels_[1]);
3,520,000✔
1187
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
3,520,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;
3,520,000✔
1193
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
3,520,000✔
1194
  const double dy = p1 / p0 * dx;
3,520,000✔
1195

1196
  std::pair<Position, Direction> result;
3,520,000✔
1197

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

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

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

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

1219
  return result;
3,520,000✔
1220
}
1221

1222
void WireframeRayTracePlot::create_output() const
55✔
1223
{
1224
  size_t width = pixels_[0];
55✔
1225
  size_t height = pixels_[1];
55✔
1226
  ImageData data({width, height}, not_found_);
55✔
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);
55✔
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();
55✔
1245
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1246
    n_threads);
55✔
1247
  for (int t = 0; t < n_threads; ++t) {
140✔
1248
    this_line_segments[t].resize(pixels_[0]);
85✔
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]);
55✔
1253

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

1259
    int vert = tid;
25✔
1260
    for (int iter = 0; iter <= pixels_[1] / n_threads; iter++) {
5,050✔
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)
5,025!
1267
        old_segments = this_line_segments[n_threads - 1];
5,025✔
1268

1269
      if (vert < pixels_[1]) {
5,025✔
1270

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

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

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

1280
          ray.trace();
1,000,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,000,000✔
1286
          const auto& segments = this_line_segments[tid][horiz];
1,000,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,000,000✔
1293
            continue;
616,655✔
1294

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

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

1315
          // Check to draw wireframe in horizontal direction. No inter-thread
1316
          // comm.
1317
          if (horiz > 0) {
383,345✔
1318
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
382,345✔
1319
                  this_line_segments[tid][horiz - 1])) {
382,345✔
1320
              wireframe_initial(horiz, vert) = 1;
15,710✔
1321
            }
1322
          }
1323
        }
1,000,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]) {
5,025✔
1334
        // Loop over horizontal pixels, checking intersection stack of upper
1335
        // neighbor
1336

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

1343
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,005,000✔
1344
          if (!trackstack_equivalent(
1,000,000✔
1345
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
1,000,000✔
1346
            wireframe_initial(horiz, vert) = 1;
20,595✔
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;
5,025✔
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) {
11,055✔
1361
    for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
2,211,000✔
1362
      if (wireframe_initial(horiz, vert)) {
2,200,000✔
1363
        if (wireframe_thickness_ == 1)
70,983✔
1364
          data(horiz, vert) = wireframe_color_;
30,195✔
1365
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
195,723✔
1366
             ++i)
1367
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
546,876✔
1368
               ++j)
1369
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
422,136!
1370

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

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

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

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

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

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

1412
    // Add RGB
1413
    if (PlotColorBy::cells == color_by_) {
66!
1414
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
66!
1415
        col_id = model::cell_map[col_id];
66✔
1416
        xs_[col_id] = user_xs;
66✔
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
}
55✔
1432

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

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

1455
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
55✔
1456
{
1457
  if (check_for_node(node, "wireframe_ids")) {
55✔
1458
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
11✔
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_)
22✔
1462
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
11!
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());
55✔
1468
}
55✔
1469

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

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

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

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

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

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

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

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

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

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

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

1567
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
33✔
1568
  }
33✔
1569
}
33✔
1570

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

1576
    if (light_pos_tmp.size() != 3)
11!
1577
      fatal_error("Light position must be given as 3D coordinates");
×
1578

1579
    light_location_.x = light_pos_tmp[0];
11✔
1580
    light_location_.y = light_pos_tmp[1];
11✔
1581
    light_location_.z = light_pos_tmp[2];
11✔
1582
  } else {
11✔
1583
    light_location_ = camera_position();
22✔
1584
  }
1585
}
33✔
1586

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

1597
void Ray::compute_distance()
3,012,306✔
1598
{
1599
  boundary() = distance_to_boundary(*this);
3,012,306✔
1600
}
3,012,306✔
1601

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

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

1619
  // Advance to the boundary of the model
1620
  while (!inside_cell) {
15,616,590!
1621
    advance_to_boundary_from_void();
15,616,590✔
1622
    inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
15,616,590✔
1623

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

1631
    // Exit this loop and enter into cell-to-cell ray tracing (which uses
1632
    // neighbor lists)
1633
    if (inside_cell)
15,616,590✔
1634
      break;
1,549,042✔
1635

1636
    // if there is no intersection with the model, we're done
1637
    if (boundary().surface() == SURFACE_NONE)
14,067,548✔
1638
      return;
1,970,958✔
1639

1640
    event_counter_++;
12,096,590✔
1641
    if (event_counter_ > MAX_INTERSECTIONS) {
12,096,590!
1642
      warning("Likely infinite loop in ray traced plot");
×
1643
      return;
×
1644
    }
1645
  }
1646

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

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

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

1671
    compute_distance();
2,306,634✔
1672

1673
    // There are no more intersections to process
1674
    // if we hit the edge of the model, so stop
1675
    // the particle in that case. Also, just exit
1676
    // if a negative distance was somehow computed.
1677
    if (boundary().distance() == INFTY || boundary().distance() == INFINITY ||
4,613,268!
1678
        boundary().distance() < 0) {
2,306,634!
1679
      return;
×
1680
    }
1681

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

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

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

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

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

1724
    if (!inside_cell)
2,271,115✔
1725
      return;
1,513,523✔
1726

1727
    event_counter_++;
757,592✔
1728
    if (event_counter_ > MAX_INTERSECTIONS) {
757,592!
1729
      warning("Likely infinite loop in ray traced plot");
×
1730
      return;
×
1731
    }
1732
  }
757,592✔
1733
}
1734

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

1743
  line_segments_.emplace_back(
2,359,148✔
1744
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
2,359,148✔
1745
      ? material()
545,919✔
1746
      : lowest_coord().cell(),
1,813,229✔
1747
    traversal_distance_, boundary().surface_index());
2,359,148✔
1748
}
2,359,148✔
1749

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

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

1765
  // Anything that's not opaque has zero impact on the plot.
1766
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
903,243✔
1767
    return;
162,052✔
1768

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

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

1792
    // Get surface pointer
1793
    const auto& surf = model::surfaces.at(surface_index());
705,672✔
1794

1795
    Direction normal = surf->normal(r_local());
705,672✔
1796
    normal /= normal.norm();
705,672✔
1797

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

1807
    // use the normal opposed to the ray direction
1808
    if (normal.dot(u()) > 0.0) {
705,672✔
1809
      normal *= -1.0;
63,789✔
1810
    }
1811

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

1816
    double modulation =
705,672✔
1817
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
705,672✔
1818
    result_color_ *= modulation;
705,672✔
1819

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

1824
    orig_hit_id_ = hit_id;
705,672✔
1825

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

1835
    // Must fully restart coordinate search. Why? Not sure.
1836
    clear();
705,672✔
1837

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

1847
    // Must recalculate distance to boundary due to the
1848
    // direction change
1849
    compute_distance();
705,672✔
1850

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

1856
    if (orig_hit_id_ == -1)
35,519!
1857
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1858

1859
    result_color_ = plot_.colors_[orig_hit_id_];
35,519✔
1860
    result_color_ *= plot_.diffuse_fraction_;
35,519✔
1861
    stop();
35,519✔
1862
  }
1863
}
1864

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

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

1874
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
245!
1875
    model::overlap_check_count.resize(model::cells.size());
22✔
1876
  }
1877

1878
  auto ids = plt->get_map<IdData>();
245✔
1879

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

1883
  return 0;
245✔
1884
}
245✔
1885

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

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

1895
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
11!
1896
    model::overlap_check_count.resize(model::cells.size());
×
1897
  }
1898

1899
  auto props = plt->get_map<PropertyData>();
11✔
1900

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

1904
  return 0;
11✔
1905
}
11✔
1906

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