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

openmc-dev / openmc / 12659257633

07 Jan 2025 09:02PM UTC coverage: 84.867% (+0.04%) from 84.823%
12659257633

Pull #2655

github

web-flow
Merge 6280602de into 393334829
Pull Request #2655: Raytrace plots

373 of 413 new or added lines in 4 files covered. (90.31%)

414 existing lines in 5 files now uncovered.

50068 of 58996 relevant lines covered (84.87%)

35067219.93 hits per line

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

83.78
/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,941✔
48
{}
5,941✔
49

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

63
  // set material data
64
  Cell* c = model::cells.at(p.lowest_coord().cell).get();
44,295,017✔
65
  if (p.material() == MATERIAL_VOID) {
44,295,017✔
66
    data_(y, x, 2) = MATERIAL_VOID;
34,714,472✔
67
    return;
34,714,472✔
68
  } else if (c->type_ == Fill::MATERIAL) {
9,580,545✔
69
    Material* m = model::materials.at(p.material()).get();
9,580,545✔
70
    data_(y, x, 2) = m->id_;
9,580,545✔
71
  }
72
}
73

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

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

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

94
void PropertyData::set_overlap(size_t y, size_t x)
×
95
{
UNCOV
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()
312✔
116
{
117

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

123
  return 0;
312✔
124
}
125

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

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

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

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

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

161
  if (PlotType::slice == type_) {
377✔
162
    switch (basis_) {
312✔
163
    case PlotBasis::xy:
182✔
164
      fmt::print("Basis: XY\n");
154✔
165
      break;
182✔
166
    case PlotBasis::xz:
65✔
167
      fmt::print("Basis: XZ\n");
55✔
168
      break;
65✔
169
    case PlotBasis::yz:
65✔
170
      fmt::print("Basis: YZ\n");
55✔
171
      break;
65✔
172
    }
173
    fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
624✔
174
  } else if (PlotType::voxel == type_) {
65✔
175
    fmt::print("Voxels: {} {} {}\n", pixels_[0], pixels_[1], pixels_[2]);
130✔
176
  }
177
}
377✔
178

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

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

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

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

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

200
void read_plots_xml(pugi::xml_node root)
2,068✔
201
{
202
  for (auto node : root.children("plot")) {
3,112✔
203
    std::string id_string = get_node_value(node, "id", true);
1,055✔
204
    int id = std::stoi(id_string);
1,055✔
205
    if (check_for_node(node, "type")) {
1,055✔
206
      std::string type_str = get_node_value(node, "type", true);
1,055✔
207
      if (type_str == "slice")
1,055✔
208
        model::plots.emplace_back(
875✔
209
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,761✔
210
      else if (type_str == "voxel")
169✔
211
        model::plots.emplace_back(
65✔
212
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
130✔
213
      else if (type_str == "projection")
104✔
214
        model::plots.emplace_back(std::make_unique<ProjectionPlot>(node));
65✔
215
      else if (type_str == "phong")
39✔
216
        model::plots.emplace_back(std::make_unique<PhongPlot>(node));
39✔
217
      else
UNCOV
218
        fatal_error(
×
UNCOV
219
          fmt::format("Unsupported plot type '{}' in plot {}", type_str, id));
×
220

221
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
1,044✔
222
    } else {
1,044✔
UNCOV
223
      fatal_error(fmt::format("Must specify plot type in plot {}", id));
×
224
    }
225
  }
1,044✔
226
}
2,057✔
227

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

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

239
  size_t width = pixels_[0];
338✔
240
  size_t height = pixels_[1];
338✔
241

242
  ImageData data({width, height}, not_found_);
338✔
243

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

247
  // assign colors
248
  for (size_t y = 0; y < height; y++) {
53,274✔
249
    for (size_t x = 0; x < width; x++) {
12,151,542✔
250
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
12,098,606✔
251
      auto id = ids.data_(y, x, idx);
12,098,606✔
252
      // no setting needed if not found
253
      if (id == NOT_FOUND) {
12,098,606✔
254
        continue;
1,950,598✔
255
      }
256
      if (id == OVERLAP) {
10,181,392✔
257
        data(x, y) = overlap_color_;
33,384✔
258
        continue;
33,384✔
259
      }
260
      if (PlotColorBy::cells == color_by_) {
10,148,008✔
261
        data(x, y) = colors_[model::cell_map[id]];
6,012,188✔
262
      } else if (PlotColorBy::mats == color_by_) {
4,135,820✔
263
        if (id == MATERIAL_VOID) {
4,135,820✔
UNCOV
264
          data(x, y) = WHITE;
×
UNCOV
265
          continue;
×
266
        }
267
        data(x, y) = colors_[model::material_map[id]];
4,135,820✔
268
      } // color_by if-else
269
    }
270
  }
271

272
  // draw mesh lines if present
273
  if (index_meshlines_mesh_ >= 0) {
338✔
274
    draw_mesh_lines(data);
39✔
275
  }
276

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

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

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

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

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

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

344
  path_plot_ = filename;
940✔
345

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

672
    } else {
39✔
UNCOV
673
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
674
    }
675
  }
676
}
1,055✔
677

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

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

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

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

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

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

UNCOV
741
  of.open(fname);
×
742

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

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

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

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

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

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

781
  png_init_io(png_ptr, fp);
442✔
782

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

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

793
  // Write color for each pixel
794
  for (int y = 0; y < height; y++) {
74,178✔
795
    for (int x = 0; x < width; x++) {
16,332,342✔
796
      RGBColor rgb = data(x, y);
16,258,606✔
797
      row[3 * x] = rgb.red;
16,258,606✔
798
      row[3 * x + 1] = rgb.green;
16,258,606✔
799
      row[3 * x + 2] = rgb.blue;
16,258,606✔
800
    }
801
    png_write_row(png_ptr, row.data());
73,736✔
802
  }
803

804
  // End write
805
  png_write_end(png_ptr, nullptr);
442✔
806

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

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

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

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

841
  Position ll_plot {origin_};
39✔
842
  Position ur_plot {origin_};
39✔
843

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

849
  Position width = ur_plot - ll_plot;
39✔
850

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

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

871
  // Iterate across the first axis and draw lines.
872
  for (auto ax1_val : axis_lines.first) {
221✔
873
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
182✔
874
    int ax1_ind = frac * pixels_[0];
182✔
875
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
29,484✔
876
      for (int plus = 0; plus <= meshlines_width_; plus++) {
58,604✔
877
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels_[0])
29,302✔
878
          data(ax1_ind + plus, ax2_ind) = rgb;
29,302✔
879
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels_[0])
29,302✔
880
          data(ax1_ind - plus, ax2_ind) = rgb;
29,302✔
881
      }
882
    }
883
  }
884

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

901
  // Iterate across the second axis and draw lines.
902
  for (auto ax2_val : axis_lines.second) {
247✔
903
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
208✔
904
    int ax2_ind = (1.0 - frac) * pixels_[1];
208✔
905
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
33,488✔
906
      for (int plus = 0; plus <= meshlines_width_; plus++) {
66,560✔
907
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels_[1])
33,280✔
908
          data(ax1_ind, ax2_ind + plus) = rgb;
33,280✔
909
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels_[1])
33,280✔
910
          data(ax1_ind, ax2_ind - plus) = rgb;
33,280✔
911
      }
912
    }
913
  }
914
}
39✔
915

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

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

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

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

948
#ifdef GIT_SHA1
949
  write_attribute(file_id, "git_sha1", GIT_SHA1);
65✔
950
#endif
951

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

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

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

976
  ProgressBar pb;
65✔
977
  for (int z = 0; z < pixels_[2]; z++) {
5,655✔
978
    // update z coordinate
979
    pltbase.origin_.z = ll.z + z * vox[2];
5,590✔
980

981
    // generate ids using plotbase
982
    IdData ids = pltbase.get_map<IdData>();
5,590✔
983

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

990
    // Write to HDF5 dataset
991
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
5,590✔
992

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

998
  voxel_finalize(dspace, dset, memspace);
65✔
999
  file_close(file_id);
65✔
1000
}
65✔
1001

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

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

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

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

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

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

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

1050
  if (check_for_node(node, "orthographic_width") &&
117✔
1051
      check_for_node(node, "field_of_view"))
13✔
NEW
1052
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1053
                "parameters.");
1054

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

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

1074
ProjectionPlot::ProjectionPlot(pugi::xml_node node) : RayTracePlot(node)
65✔
1075
{
1076
  set_opacities(node);
65✔
1077
  set_wireframe_thickness(node);
65✔
1078
  set_wireframe_ids(node);
65✔
1079
  set_wireframe_color(node);
65✔
1080
}
65✔
1081

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

1095
void RayTracePlot::set_output_path(pugi::xml_node node)
104✔
1096
{
1097
  // Set output file path
1098
  std::string filename;
104✔
1099

1100
  if (check_for_node(node, "filename")) {
104✔
1101
    filename = get_node_value(node, "filename");
91✔
1102
  } else {
1103
    filename = fmt::format("plot_{}", id());
26✔
1104
  }
1105

1106
#ifdef USE_LIBPNG
1107
  if (!file_extension_present(filename, "png"))
104✔
1108
    filename.append(".png");
39✔
1109
#else
1110
  if (!file_extension_present(filename, "ppm"))
1111
    filename.append(".ppm");
1112
#endif
1113
  path_plot_ = filename;
104✔
1114
}
104✔
1115

1116
// Advances to the next boundary from outside the geometry
1117
// Returns -1 if no intersection found, and the surface index
1118
// if an intersection was found.
1119
int RayTracePlot::advance_to_boundary_from_void(GeometryState& p)
18,856,695✔
1120
{
1121
  double min_dist {INFINITY};
18,856,695✔
1122
  auto root_coord = p.coord(0);
18,856,695✔
1123
  Universe* uni = model::universes[model::root_universe].get();
18,856,695✔
1124
  int intersected_surface = -1;
18,856,695✔
1125
  for (auto c_i : uni->cells_) {
75,426,780✔
1126
    auto dist =
1127
      model::cells.at(c_i)->distance(root_coord.r, root_coord.u, 0, &p);
56,570,085✔
1128
    if (dist.first < min_dist) {
56,570,085✔
1129
      min_dist = dist.first;
24,913,811✔
1130
      intersected_surface = dist.second;
24,913,811✔
1131
    }
1132
  }
1133
  if (min_dist > 1e300)
18,856,695✔
1134
    return -1;
2,429,934✔
1135
  else { // advance the particle
1136
    for (int j = 0; j < p.n_coord(); ++j)
32,853,522✔
1137
      p.coord(j).r += (min_dist + TINY_BIT) * p.coord(j).u;
16,426,761✔
1138
    return std::abs(intersected_surface);
16,426,761✔
1139
  }
1140
}
1141

1142
bool ProjectionPlot::trackstack_equivalent(
3,495,349✔
1143
  const std::vector<TrackSegment>& track1,
1144
  const std::vector<TrackSegment>& track2) const
1145
{
1146
  if (wireframe_ids_.empty()) {
3,495,349✔
1147
    // Draw wireframe for all surfaces/cells/materials
1148
    if (track1.size() != track2.size())
2,909,062✔
1149
      return false;
62,036✔
1150
    for (int i = 0; i < track1.size(); ++i) {
7,034,248✔
1151
      if (track1[i].id != track2[i].id ||
8,401,042✔
1152
          track1[i].surface != track2[i].surface) {
4,200,443✔
1153
        return false;
13,377✔
1154
      }
1155
    }
1156
    return true;
2,833,649✔
1157
  } else {
1158
    // This runs in O(nm) where n is the intersection stack size
1159
    // and m is the number of IDs we are wireframing. A simpler
1160
    // algorithm can likely be found.
1161
    for (const int id : wireframe_ids_) {
1,166,243✔
1162
      int t1_i = 0;
586,287✔
1163
      int t2_i = 0;
586,287✔
1164

1165
      // Advance to first instance of the ID
1166
      while (t1_i < track1.size() && t2_i < track2.size()) {
666,159✔
1167
        while (t1_i < track1.size() && track1[t1_i].id != id)
466,960✔
1168
          t1_i++;
272,142✔
1169
        while (t2_i < track2.size() && track2[t2_i].id != id)
468,260✔
1170
          t2_i++;
273,442✔
1171

1172
        // This one is really important!
1173
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
471,835✔
1174
            (t1_i != track1.size() && t2_i == track2.size()))
277,017✔
1175
          return false;
6,331✔
1176
        if (t1_i == track1.size() && t2_i == track2.size())
190,242✔
1177
          break;
108,615✔
1178
        // Check if surface different
1179
        if (track1[t1_i].surface != track2[t2_i].surface)
81,627✔
1180
          return false;
1,755✔
1181

1182
        // Pretty sure this should not be used:
1183
        // if (t2_i != track2.size() - 1 &&
1184
        //     t1_i != track1.size() - 1 &&
1185
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1186
        if (t2_i != 0 && t1_i != 0 &&
144,170✔
1187
            track1[t1_i - 1].surface != track2[t2_i - 1].surface)
64,298✔
UNCOV
1188
          return false;
×
1189

1190
        // Check if neighboring cells are different
1191
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1192
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1193
        // ].id !=
1194
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1195
        //    false;
1196
        t1_i++, t2_i++;
79,872✔
1197
      }
1198
    }
1199
    return true;
579,956✔
1200
  }
1201
}
1202

1203
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
4,160,000✔
1204
  int horiz, int vert) const
1205
{
1206
  // Compute field of view in radians
1207
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
4,160,000✔
1208
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
4,160,000✔
1209
  double p0 = static_cast<double>(pixels_[0]);
4,160,000✔
1210
  double p1 = static_cast<double>(pixels_[1]);
4,160,000✔
1211
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
4,160,000✔
1212

1213
  // focal_plane_dist can be changed to alter the perspective distortion
1214
  // effect. This is in units of cm. This seems to look good most of the
1215
  // time. TODO let this variable be set through XML.
1216
  constexpr double focal_plane_dist = 10.0;
4,160,000✔
1217
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
4,160,000✔
1218
  const double dy = p1 / p0 * dx;
4,160,000✔
1219

1220
  std::pair<Position, Direction> result;
4,160,000✔
1221

1222
  // Generate the starting position/direction of the ray
1223
  if (orthographic_width_ == 0.0) { // perspective projection
4,160,000✔
1224
    Direction camera_local_vec;
3,640,000✔
1225
    camera_local_vec.x = focal_plane_dist;
3,640,000✔
1226
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
3,640,000✔
1227
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
3,640,000✔
1228
    camera_local_vec /= camera_local_vec.norm();
3,640,000✔
1229

1230
    result.first = camera_position_;
3,640,000✔
1231
    result.second = camera_local_vec.rotate(camera_to_model_);
3,640,000✔
1232
  } else { // orthographic projection
1233

1234
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
520,000✔
1235
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
520,000✔
1236

1237
    result.first = camera_position_ +
1238
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
520,000✔
1239
                   camera_z_axis() * y_pix_coord * orthographic_width_;
520,000✔
1240
    result.second = camera_x_axis();
520,000✔
1241
  }
1242

1243
  return result;
4,160,000✔
1244
}
1245

1246
void ProjectionPlot::create_output() const
65✔
1247
{
1248
  size_t width = pixels_[0];
65✔
1249
  size_t height = pixels_[1];
65✔
1250
  ImageData data({width, height}, not_found_);
65✔
1251

1252
  // This array marks where the initial wireframe was drawn.
1253
  // We convolve it with a filter that gets adjusted with the
1254
  // wireframe thickness in order to thicken the lines.
1255
  xt::xtensor<int, 2> wireframe_initial({width, height}, 0);
65✔
1256

1257
  /* Holds all of the track segments for the current rendered line of pixels.
1258
   * old_segments holds a copy of this_line_segments from the previous line.
1259
   * By holding both we can check if the cell/material intersection stack
1260
   * differs from the left or upper neighbor. This allows a robustly drawn
1261
   * wireframe. If only checking the left pixel (which requires substantially
1262
   * less memory), the wireframe tends to be spotty and be disconnected for
1263
   * surface edges oriented horizontally in the rendering.
1264
   *
1265
   * Note that a vector of vectors is required rather than a 2-tensor,
1266
   * since the stack size varies within each column.
1267
   */
1268
  const int n_threads = num_threads();
65✔
1269
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1270
    n_threads);
65✔
1271
  for (int t = 0; t < n_threads; ++t) {
165✔
1272
    this_line_segments[t].resize(pixels_[0]);
100✔
1273
  }
1274

1275
  // The last thread writes to this, and the first thread reads from it.
1276
  std::vector<std::vector<TrackSegment>> old_segments(pixels_[0]);
65✔
1277

1278
#pragma omp parallel
35✔
1279
  {
1280
    const int n_threads = num_threads();
30✔
1281
    const int tid = thread_num();
30✔
1282

1283
    int vert = tid;
30✔
1284
    for (int iter = 0; iter <= pixels_[1] / n_threads; iter++) {
6,060✔
1285

1286
      // Save bottom line of current work chunk to compare against later
1287
      // I used to have this inside the below if block, but it causes a
1288
      // spurious line to be drawn at the bottom of the image. Not sure
1289
      // why, but moving it here fixes things.
1290
      if (tid == n_threads - 1)
6,030✔
1291
        old_segments = this_line_segments[n_threads - 1];
6,030✔
1292

1293
      if (vert < pixels_[1]) {
6,030✔
1294

1295
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,206,000✔
1296

1297
          // RayTracePlot implements camera ray generation
1298
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
1,200,000✔
1299

1300
          this_line_segments[tid][horiz].clear();
1,200,000✔
1301
          ProjectionRay ray(
1302
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
1,200,000✔
1303

1304
          ray.trace();
1,200,000✔
1305

1306
          // Now color the pixel based on what we have intersected...
1307
          // Loops backwards over intersections.
1308
          Position current_color(
1309
            not_found_.red, not_found_.green, not_found_.blue);
1,200,000✔
1310
          const auto& segments = this_line_segments[tid][horiz];
1,200,000✔
1311

1312
          // There must be at least two cell intersections to color,
1313
          // front and back of the cell. Maybe an infinitely thick
1314
          // cell could be present with no back, but why would you
1315
          // want to color that? It's easier to just skip that edge
1316
          // case and not even color it.
1317
          if (segments.size() <= 1)
1,200,000✔
1318
            continue;
786,444✔
1319

1320
          for (int i = segments.size() - 2; i >= 0; --i) {
1,092,894✔
1321
            int colormap_idx = segments[i].id;
679,338✔
1322
            RGBColor seg_color = colors_[colormap_idx];
679,338✔
1323
            Position seg_color_vec(
1324
              seg_color.red, seg_color.green, seg_color.blue);
679,338✔
1325
            double mixing =
1326
              std::exp(-xs_[colormap_idx] *
679,338✔
1327
                       (segments[i + 1].length - segments[i].length));
679,338✔
1328
            current_color =
1329
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
679,338✔
1330
          }
1331

1332
          // save result converting from double-precision color coordinates to
1333
          // byte-sized
1334
          RGBColor result;
413,556✔
1335
          result.red = static_cast<uint8_t>(current_color.x);
413,556✔
1336
          result.green = static_cast<uint8_t>(current_color.y);
413,556✔
1337
          result.blue = static_cast<uint8_t>(current_color.z);
413,556✔
1338
          data(horiz, vert) = result;
413,556✔
1339

1340
          // Check to draw wireframe in horizontal direction. No inter-thread
1341
          // comm.
1342
          if (horiz > 0) {
413,556✔
1343
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
413,238✔
1344
                  this_line_segments[tid][horiz - 1])) {
413,238✔
1345
              wireframe_initial(horiz, vert) = 1;
16,230✔
1346
            }
1347
          }
1348
        }
1,200,000✔
1349
      } // end "if" vert in correct range
1350

1351
      // We require a barrier before comparing vertical neighbors' intersection
1352
      // stacks. i.e. all threads must be done with their line.
1353
#pragma omp barrier
1354

1355
      // Now that the horizontal line has finished rendering, we can fill in
1356
      // wireframe entries that require comparison among all the threads. Hence
1357
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1358
      if (vert < pixels_[1]) {
6,030✔
1359
        // Loop over horizontal pixels, checking intersection stack of upper
1360
        // neighbor
1361

1362
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
6,000✔
1363
        if (tid == 0)
6,000✔
1364
          top_cmp = &old_segments;
6,000✔
1365
        else
1366
          top_cmp = &this_line_segments[tid - 1];
1367

1368
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,206,000✔
1369
          if (!trackstack_equivalent(
1,200,000✔
1370
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
1,200,000✔
1371
            wireframe_initial(horiz, vert) = 1;
21,498✔
1372
          }
1373
        }
1374
      }
1375

1376
      // We need another barrier to ensure threads don't proceed to modify their
1377
      // intersection stacks on that horizontal line while others are
1378
      // potentially still working on the above.
1379
#pragma omp barrier
1380
      vert += n_threads;
6,030✔
1381
    }
1382
  } // end omp parallel
1383

1384
  // Now thicken the wireframe lines and apply them to our image
1385
  for (int vert = 0; vert < pixels_[1]; ++vert) {
13,065✔
1386
    for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
2,613,000✔
1387
      if (wireframe_initial(horiz, vert)) {
2,600,000✔
1388
        if (wireframe_thickness_ == 1)
72,410✔
1389
          data(horiz, vert) = wireframe_color_;
31,863✔
1390
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
195,754✔
1391
             ++i)
1392
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
539,032✔
1393
               ++j)
1394
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
415,688✔
1395

1396
              // Check if wireframe pixel is out of bounds
1397
              int w_i = std::max(std::min(horiz + i, pixels_[0] - 1), 0);
415,688✔
1398
              int w_j = std::max(std::min(vert + j, pixels_[1] - 1), 0);
415,688✔
1399
              data(w_i, w_j) = wireframe_color_;
415,688✔
1400
            }
1401
      }
1402
    }
1403
  }
1404

1405
#ifdef USE_LIBPNG
1406
  output_png(path_plot(), data);
65✔
1407
#else
1408
  output_ppm(path_plot(), data);
1409
#endif
1410
}
65✔
1411

1412
void RayTracePlot::print_info() const
104✔
1413
{
1414
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
88✔
1415
    camera_position_.y, camera_position_.z);
104✔
1416
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
192✔
1417
  fmt::print(
88✔
1418
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
104✔
1419
  fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
192✔
1420
}
104✔
1421

1422
void ProjectionPlot::print_info() const
65✔
1423
{
1424
  fmt::print("Plot Type: Projection\n");
65✔
1425
  RayTracePlot::print_info();
65✔
1426
}
65✔
1427

1428
void ProjectionPlot::set_opacities(pugi::xml_node node)
65✔
1429
{
1430
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
65✔
1431

1432
  for (auto cn : node.children("color")) {
143✔
1433
    // Make sure 3 values are specified for RGB
1434
    double user_xs = std::stod(get_node_value(cn, "xs"));
78✔
1435
    int col_id = std::stoi(get_node_value(cn, "id"));
78✔
1436

1437
    // Add RGB
1438
    if (PlotColorBy::cells == color_by_) {
78✔
1439
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
78✔
1440
        col_id = model::cell_map[col_id];
78✔
1441
        xs_[col_id] = user_xs;
78✔
1442
      } else {
UNCOV
1443
        warning(fmt::format(
×
1444
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1445
      }
1446
    } else if (PlotColorBy::mats == color_by_) {
×
1447
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
UNCOV
1448
        col_id = model::material_map[col_id];
×
1449
        xs_[col_id] = user_xs;
×
1450
      } else {
UNCOV
1451
        warning(fmt::format(
×
UNCOV
1452
          "Could not find material {} specified in plot {}", col_id, id()));
×
1453
      }
1454
    }
1455
  }
1456
}
65✔
1457

1458
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
104✔
1459
{
1460
  if (check_for_node(node, "orthographic_width")) {
104✔
1461
    double orthographic_width =
1462
      std::stod(get_node_value(node, "orthographic_width", true));
13✔
1463
    if (orthographic_width < 0.0)
13✔
UNCOV
1464
      fatal_error("Requires positive orthographic_width");
×
1465
    orthographic_width_ = orthographic_width;
13✔
1466
  }
1467
}
104✔
1468

1469
void ProjectionPlot::set_wireframe_thickness(pugi::xml_node node)
65✔
1470
{
1471
  if (check_for_node(node, "wireframe_thickness")) {
65✔
1472
    int wireframe_thickness =
1473
      std::stoi(get_node_value(node, "wireframe_thickness", true));
26✔
1474
    if (wireframe_thickness < 0)
26✔
UNCOV
1475
      fatal_error("Requires non-negative wireframe thickness");
×
1476
    wireframe_thickness_ = wireframe_thickness;
26✔
1477
  }
1478
}
65✔
1479

1480
void ProjectionPlot::set_wireframe_ids(pugi::xml_node node)
65✔
1481
{
1482
  if (check_for_node(node, "wireframe_ids")) {
65✔
1483
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
13✔
1484
    // It is read in as actual ID values, but we have to convert to indices in
1485
    // mat/cell array
1486
    for (auto& x : wireframe_ids_)
26✔
1487
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
13✔
UNCOV
1488
                                         : model::cell_map[x];
×
1489
  }
1490
  // We make sure the list is sorted in order to later use
1491
  // std::binary_search.
1492
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
65✔
1493
}
65✔
1494

1495
void RayTracePlot::set_pixels(pugi::xml_node node)
104✔
1496
{
1497
  vector<int> pxls = get_node_array<int>(node, "pixels");
104✔
1498
  if (pxls.size() != 2)
104✔
UNCOV
1499
    fatal_error(
×
UNCOV
1500
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1501
  pixels_[0] = pxls[0];
104✔
1502
  pixels_[1] = pxls[1];
104✔
1503
}
104✔
1504

1505
void RayTracePlot::set_camera_position(pugi::xml_node node)
104✔
1506
{
1507
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
104✔
1508
  if (camera_pos.size() != 3) {
104✔
UNCOV
1509
    fatal_error(fmt::format(
×
1510
      "camera_position element must have three floating point values"));
1511
  }
1512
  camera_position_.x = camera_pos[0];
104✔
1513
  camera_position_.y = camera_pos[1];
104✔
1514
  camera_position_.z = camera_pos[2];
104✔
1515
}
104✔
1516

1517
void RayTracePlot::set_look_at(pugi::xml_node node)
104✔
1518
{
1519
  vector<double> look_at = get_node_array<double>(node, "look_at");
104✔
1520
  if (look_at.size() != 3) {
104✔
UNCOV
1521
    fatal_error("look_at element must have three floating point values");
×
1522
  }
1523
  look_at_.x = look_at[0];
104✔
1524
  look_at_.y = look_at[1];
104✔
1525
  look_at_.z = look_at[2];
104✔
1526
}
104✔
1527

1528
void RayTracePlot::set_field_of_view(pugi::xml_node node)
104✔
1529
{
1530
  // Defaults to 70 degree horizontal field of view (see .h file)
1531
  if (check_for_node(node, "field_of_view")) {
104✔
1532
    double fov = std::stod(get_node_value(node, "field_of_view", true));
26✔
1533
    if (fov < 180.0 && fov > 0.0) {
26✔
1534
      horizontal_field_of_view_ = fov;
26✔
1535
    } else {
UNCOV
1536
      fatal_error(fmt::format(
×
UNCOV
1537
        "Field of view for plot {} out-of-range. Must be in (0, 180).", id()));
×
1538
    }
1539
  }
1540
}
104✔
1541

1542
PhongPlot::PhongPlot(pugi::xml_node node) : RayTracePlot(node)
39✔
1543
{
1544
  set_opaque_ids(node);
39✔
1545
  set_diffuse_fraction(node);
39✔
1546
  set_light_position(node);
39✔
1547
}
39✔
1548

1549
void PhongPlot::print_info() const
39✔
1550
{
1551
  fmt::print("Plot Type: Phong\n");
39✔
1552
  RayTracePlot::print_info();
39✔
1553
}
39✔
1554

1555
void PhongPlot::create_output() const
39✔
1556
{
1557
  size_t width = pixels_[0];
39✔
1558
  size_t height = pixels_[1];
39✔
1559
  ImageData data({width, height}, not_found_);
39✔
1560

1561
#pragma omp parallel for schedule(dynamic) collapse(2)
21✔
1562
  for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
3,618✔
1563
    for (int vert = 0; vert < pixels_[1]; ++vert) {
723,600✔
1564
      // RayTracePlot implements camera ray generation
1565
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
720,000✔
1566
      PhongRay ray(ru.first, ru.second, *this);
720,000✔
1567
      ray.trace();
720,000✔
1568
      data(horiz, vert) = ray.result_color();
720,000✔
1569
    }
720,000✔
1570
  }
1571

1572
#ifdef USE_LIBPNG
1573
  output_png(path_plot(), data);
39✔
1574
#else
1575
  output_ppm(path_plot(), data);
1576
#endif
1577
}
39✔
1578

1579
void PhongPlot::set_opaque_ids(pugi::xml_node node)
39✔
1580
{
1581
  if (check_for_node(node, "opaque_ids")) {
39✔
1582
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
39✔
1583

1584
    // It is read in as actual ID values, but we have to convert to indices in
1585
    // mat/cell array
1586
    for (auto& x : opaque_ids_tmp)
117✔
1587
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
78✔
NEW
1588
                                         : model::cell_map[x];
×
1589

1590
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
39✔
1591
  }
39✔
1592
}
39✔
1593

1594
void PhongPlot::set_light_position(pugi::xml_node node)
39✔
1595
{
1596
  if (check_for_node(node, "light_position")) {
39✔
1597
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
13✔
1598

1599
    if (light_pos_tmp.size() != 3)
13✔
NEW
1600
      fatal_error("Light position must be given as 3D coordinates");
×
1601

1602
    light_location_.x = light_pos_tmp[0];
13✔
1603
    light_location_.y = light_pos_tmp[1];
13✔
1604
    light_location_.z = light_pos_tmp[2];
13✔
1605
  } else {
13✔
1606
    light_location_ = camera_position();
26✔
1607
  }
1608
}
39✔
1609

1610
void PhongPlot::set_diffuse_fraction(pugi::xml_node node)
39✔
1611
{
1612
  if (check_for_node(node, "diffuse_fraction")) {
39✔
1613
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
13✔
1614
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
13✔
NEW
1615
      fatal_error("Must have 0<=diffuse fraction<= 1");
×
1616
    }
1617
  }
1618
}
39✔
1619

1620
void Ray::compute_distance()
3,240,562✔
1621
{
1622
  dist_ = distance_to_boundary(*this);
3,240,562✔
1623
}
3,240,562✔
1624

1625
void Ray::trace()
4,160,000✔
1626
{
1627
  // To trace the ray from its origin all the way through the model, we have
1628
  // to proceed in two phases. In the first, the ray may or may not be found
1629
  // inside the model. If the ray is already in the model, phase one can be
1630
  // skipped. Otherwise, the ray has to be advanced to the boundary of the
1631
  // model where all the cells are defined. Importantly, this is assuming that
1632
  // the model is convex, which is a very reasonable assumption for any
1633
  // radiation transport model.
1634
  //
1635
  // After phase one is done, we can starting tracing from cell to cell within
1636
  // the model. This step can use neighbor lists to accelerate the ray tracing.
1637

1638
  int first_surface_ = -1; // surface first passed when entering the model
4,160,000✔
1639

1640
  // Attempt to initialize the particle. We may have to enter a loop to move
1641
  // it up to the edge of the model.
1642
  bool inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
4,160,000✔
1643

1644
  // Advance to the boundary of the model
1645
  while (!inside_cell) {
18,856,695✔
1646
    first_surface_ = RayTracePlot::advance_to_boundary_from_void(*this);
18,856,695✔
1647
    intersection_found_ = first_surface_ != -1; // -1 if no surface found
18,856,695✔
1648
                                                //
1649
    inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
18,856,695✔
1650

1651
    // If true this means no surface was intersected. See cell.cpp and search
1652
    // for numeric_limits to see where we return it.
1653
    if (surface() == std::numeric_limits<int>::max()) {
18,856,695✔
NEW
1654
      warning(fmt::format("Lost a ray, r = {}, u = {}", r(), u()));
×
NEW
1655
      return;
×
1656
    }
1657

1658
    // Exit this loop and enter into cell-to-cell ray tracing (which uses
1659
    // neighbor lists)
1660
    if (inside_cell)
18,856,695✔
1661
      break;
1,730,066✔
1662

1663
    if (!intersection_found_)
17,126,629✔
1664
      return;
2,429,934✔
1665

1666
    event_counter_++;
14,696,695✔
1667
    if (event_counter_ > MAX_INTERSECTIONS) {
14,696,695✔
NEW
1668
      warning("Likely infinite loop in ray traced plot");
×
NEW
1669
      return;
×
1670
    }
1671
  }
1672

1673
  // At this point the ray is inside the model
1674
  i_surface_ = first_surface_ - 1;
1,730,066✔
1675

1676
  // Call the specialized logic for this type of ray. This is for the
1677
  // intersection for the first intersection if we had one.
1678
  if (first_surface_ != -1) {
1,730,066✔
1679
    on_intersection();
1,730,066✔
1680
    if (stop_)
1,730,066✔
NEW
1681
      return;
×
1682
  }
1683

1684
  // This is the ray tracing loop within the model. It exits after exiting
1685
  // the model, which is equivalent to assuming that the model is convex.
1686
  // It would be nice to factor out the on_intersection at the end of this
1687
  // loop and then do "while (inside_cell)", but we can't guarantee it's
1688
  // on a surface in that case. There might be some other way to set it
1689
  // up that is perhaps a little more elegant, but this is what works just
1690
  // fine.
1691
  while (true) {
1692

1693
    compute_distance();
2,406,586✔
1694

1695
    // There are no more intersections to process
1696
    // if we hit the edge of the model, so stop
1697
    // the particle in that case. Also, just exit
1698
    // if a negative distance was somehow computed.
1699
    if (dist_.distance == INFTY || dist_.distance == INFINITY ||
2,406,586✔
1700
        dist_.distance < 0) {
2,406,586✔
NEW
1701
      return;
×
1702
    }
1703

1704
    // See below comment where call_on_intersection is checked in an
1705
    // if statement for an explanation of this.
1706
    bool call_on_intersection {true};
2,406,586✔
1707
    if (dist_.distance < 10 * TINY_BIT) {
2,406,586✔
1708
      call_on_intersection = false;
701,194✔
1709
    }
1710

1711
    // DAGMC surfaces expect us to go a little bit further than the advance
1712
    // distance to properly check cell inclusion.
1713
    dist_.distance += TINY_BIT;
2,406,586✔
1714

1715
    // Advance particle, prepare for next intersection
1716
    for (int lev = 0; lev < n_coord(); ++lev) {
4,813,172✔
1717
      coord(lev).r += dist_.distance * coord(lev).u;
2,406,586✔
1718
    }
1719
    surface() = dist_.surface_index;
2,406,586✔
1720
    n_coord_last() = n_coord();
2,406,586✔
1721
    n_coord() = dist_.coord_level;
2,406,586✔
1722
    if (dist_.lattice_translation[0] != 0 ||
2,406,586✔
1723
        dist_.lattice_translation[1] != 0 ||
4,813,172✔
1724
        dist_.lattice_translation[2] != 0) {
2,406,586✔
NEW
1725
      cross_lattice(*this, dist_, settings::verbosity >= 10);
×
1726
    }
1727

1728
    // Record how far the ray has traveled
1729
    traversal_distance_ += dist_.distance;
2,406,586✔
1730

1731
    inside_cell = neighbor_list_find_cell(*this, settings::verbosity >= 10);
2,406,586✔
1732
    i_surface_ = std::abs(surface()) - 1;
2,406,586✔
1733

1734
    // Call the specialized logic for this type of ray. Note that we do not
1735
    // call this if the advance distance is very small. Unfortunately, it seems
1736
    // darn near impossible to get the particle advanced to the model boundary
1737
    // and through it without sometimes accidentally calling on_intersection
1738
    // twice. This incorrectly shades the region as occluded when it might not
1739
    // actually be. By screening out intersection distances smaller than a
1740
    // threshold 10x larger than the scoot distance used to advance up to the
1741
    // model boundary, we can avoid that situation.
1742
    if (call_on_intersection) {
2,406,586✔
1743
      on_intersection();
1,705,392✔
1744
      if (stop_)
1,705,392✔
1745
        return;
41,977✔
1746
    }
1747

1748
    if (!inside_cell)
2,364,609✔
1749
      return;
1,688,089✔
1750

1751
    event_counter_++;
676,520✔
1752
    if (event_counter_ > MAX_INTERSECTIONS) {
676,520✔
NEW
1753
      warning("Likely infinite loop in ray traced plot");
×
NEW
1754
      return;
×
1755
    }
1756
  }
676,520✔
1757
}
1758

1759
void ProjectionRay::on_intersection()
2,367,989✔
1760
{
1761
  // This records a tuple with the following info
1762
  //
1763
  // 1) ID (material or cell depending on color_by_)
1764
  // 2) Distance traveled by the ray through that ID
1765
  // 3) Index of the intersected surface (starting from 1)
1766

1767
  line_segments_.emplace_back(
2,367,989✔
1768
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
2,367,989✔
1769
      ? material()
645,177✔
1770
      : lowest_coord().cell,
1,722,812✔
1771
    traversal_distance_, std::abs(dist().surface_index));
2,367,989✔
1772
}
2,367,989✔
1773

1774
void PhongRay::on_intersection()
1,067,469✔
1775
{
1776
  // Check if we hit an opaque material or cell
1777
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
1,067,469✔
1778
                 ? material()
1,067,469✔
NEW
1779
                 : lowest_coord().cell;
×
1780

1781
  // If we are reflected and have advanced beyond the camera,
1782
  // the ray is done. This is checked here because we should
1783
  // kill the ray even if the material is not opaque.
1784
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
1,067,469✔
NEW
1785
    stop();
×
1786
    return;
191,516✔
1787
  }
1788

1789
  // Anything that's not opaque has zero impact on the plot.
1790
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
1,067,469✔
1791
    return;
191,516✔
1792

1793
  if (!reflected_) {
875,953✔
1794

1795
    // reflect the particle and set the color to be colored by
1796
    // the normal or the diffuse lighting contribution
1797
    reflected_ = true;
833,976✔
1798
    result_color_ = plot_.colors_[hit_id];
833,976✔
1799
    Direction to_light = plot_.light_location_ - r();
833,976✔
1800
    to_light /= to_light.norm();
833,976✔
1801

1802
    // TODO
1803
    // Not sure what can cause i_surface()==-1, although it sometimes happens
1804
    // for a few pixels. It's very very rare, so proceed by coloring the pixel
1805
    // with the overlap color. It seems to happen only for a few pixels on the
1806
    // outer boundary of a hex lattice.
1807
    //
1808
    // We cannot detect it in the outer loop, and it only matters here, so
1809
    // that's why the error handling is a little different than for a lost
1810
    // ray.
1811
    if (i_surface() == -1) {
833,976✔
NEW
1812
      result_color_ = plot_.overlap_color_;
×
NEW
1813
      stop();
×
NEW
1814
      return;
×
1815
    }
1816

1817
    // Get surface pointer
1818
    const auto& surf = model::surfaces.at(i_surface());
833,976✔
1819

1820
    Direction normal = surf->normal(r_local());
833,976✔
1821
    normal /= normal.norm();
833,976✔
1822

1823
    // Need to apply translations to find the normal vector in
1824
    // the base level universe's coordinate system.
1825
    for (int lev = n_coord() - 2; lev >= 0; --lev) {
833,976✔
NEW
1826
      if (coord(lev + 1).rotated) {
×
NEW
1827
        const Cell& c {*model::cells[coord(lev).cell]};
×
NEW
1828
        normal = normal.inverse_rotate(c.rotation_);
×
1829
      }
1830
    }
1831

1832
    if (surf->geom_type_ != GeometryType::DAG && surface() > 0) {
833,976✔
1833
      normal *= -1.0;
75,387✔
1834
    }
1835

1836
    // Facing away from the light means no lighting
1837
    double dotprod = normal.dot(to_light);
833,976✔
1838
    dotprod = std::max(0.0, dotprod);
833,976✔
1839

1840
    double modulation =
833,976✔
1841
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
833,976✔
1842
    result_color_ *= modulation;
833,976✔
1843

1844
    // Now point the particle to the camera. We now begin
1845
    // checking to see if it's occluded by another surface
1846
    u() = to_light;
833,976✔
1847

1848
    orig_hit_id_ = hit_id;
833,976✔
1849

1850
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1851
    // in how they interpret particles that are sitting on a surface.
1852
    // I don't know exactly why, but this makes everything work beautifully.
1853
    if (surf->geom_type_ == GeometryType::DAG) {
833,976✔
NEW
1854
      surface() = 0;
×
1855
    } else {
1856
      surface() = -surface(); // go to other side
833,976✔
1857
    }
1858

1859
    // Must fully restart coordinate search. Why? Not sure.
1860
    clear();
833,976✔
1861

1862
    // Note this could likely be faster if we cached the previous
1863
    // cell we were in before the reflection. This is the easiest
1864
    // way to fully initialize all the sub-universe coordinates and
1865
    // directions though.
1866
    bool found = exhaustive_find_cell(*this);
833,976✔
1867
    if (!found) {
833,976✔
NEW
1868
      fatal_error("Lost particle after reflection.");
×
1869
    }
1870

1871
    // Must recalculate distance to boundary due to the
1872
    // direction change
1873
    compute_distance();
833,976✔
1874

1875
  } else {
1876
    // If it's not facing the light, we color with the diffuse contribution, so
1877
    // next we check if we're going to occlude the last reflected surface. if
1878
    // so, color by the diffuse contribution instead
1879

1880
    if (orig_hit_id_ == -1)
41,977✔
NEW
1881
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1882

1883
    result_color_ = plot_.colors_[orig_hit_id_];
41,977✔
1884
    result_color_ *= plot_.diffuse_fraction_;
41,977✔
1885
    stop();
41,977✔
1886
  }
1887
}
1888

1889
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
13✔
1890
{
1891

1892
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
13✔
1893
  if (!plt) {
13✔
UNCOV
1894
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
UNCOV
1895
    return OPENMC_E_INVALID_ARGUMENT;
×
1896
  }
1897

1898
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
13✔
UNCOV
1899
    model::overlap_check_count.resize(model::cells.size());
×
1900
  }
1901

1902
  auto ids = plt->get_map<IdData>();
13✔
1903

1904
  // write id data to array
1905
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
13✔
1906

1907
  return 0;
13✔
1908
}
13✔
1909

1910
extern "C" int openmc_property_map(const void* plot, double* data_out)
13✔
1911
{
1912

1913
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
13✔
1914
  if (!plt) {
13✔
UNCOV
1915
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
UNCOV
1916
    return OPENMC_E_INVALID_ARGUMENT;
×
1917
  }
1918

1919
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
13✔
UNCOV
1920
    model::overlap_check_count.resize(model::cells.size());
×
1921
  }
1922

1923
  auto props = plt->get_map<PropertyData>();
13✔
1924

1925
  // write id data to array
1926
  std::copy(props.data_.begin(), props.data_.end(), data_out);
13✔
1927

1928
  return 0;
13✔
1929
}
13✔
1930

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