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

openmc-dev / openmc / 27780945043

18 Jun 2026 06:30PM UTC coverage: 81.097% (-0.2%) from 81.34%
27780945043

Pull #3911

github

web-flow
Merge c5ade9293 into 09ee8308d
Pull Request #3911: Creating a new HDF5 nuclear data library for UQ

18119 of 26286 branches covered (68.93%)

Branch coverage included in aggregate %.

310 of 599 new or added lines in 3 files covered. (51.75%)

2247 existing lines in 53 files now uncovered.

59544 of 69480 relevant lines covered (85.7%)

40971728.6 hits per line

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

69.38
/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 "openmc/tensor.h"
11
#include <fmt/core.h>
12
#include <fmt/ostream.h>
13
#ifdef USE_LIBPNG
14
#include <png.h>
15
#endif
16

17
#include "openmc/cell.h"
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
#include "openmc/tallies/filter.h"
37

38
namespace openmc {
39

40
//==============================================================================
41
// Constants
42
//==============================================================================
43

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

48
IdData::IdData(size_t h_res, size_t v_res, bool /*include_filter*/)
3,544✔
49
  : data_({v_res, h_res, 3}, NOT_FOUND)
3,544✔
50
{}
3,544✔
51

52
void IdData::set_value(size_t y, size_t x, const Particle& p, int level,
25,747,776✔
53
  Filter* /*filter*/, FilterMatch* /*match*/)
54
{
55
  // set cell data
56
  if (p.n_coord() <= level) {
25,747,776!
UNCOV
57
    data_(y, x, 0) = NOT_FOUND;
×
UNCOV
58
    data_(y, x, 1) = NOT_FOUND;
×
59
  } else {
60
    data_(y, x, 0) = model::cells.at(p.coord(level).cell())->id_;
25,747,776!
61
    data_(y, x, 1) = level == p.n_coord() - 1
25,747,776✔
62
                       ? p.cell_instance()
25,747,776!
UNCOV
63
                       : cell_instance_at_level(p, level);
×
64
  }
65

66
  // set material data
67
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
25,747,776✔
68
  if (p.material() == MATERIAL_VOID) {
25,747,776✔
69
    data_(y, x, 2) = MATERIAL_VOID;
19,855,808✔
70
  } else if (c->type_ == Fill::MATERIAL) {
5,891,968!
71
    Material* m = model::materials.at(p.material()).get();
5,891,968✔
72
    data_(y, x, 2) = m->id_;
5,891,968✔
73
  }
74
}
25,747,776✔
75

76
void IdData::set_overlap(size_t y, size_t x)
20,544✔
77
{
78
  for (size_t k = 0; k < data_.shape(2); ++k)
164,352!
79
    data_(y, x, k) = OVERLAP;
61,632✔
80
}
20,544✔
81

UNCOV
82
PropertyData::PropertyData(size_t h_res, size_t v_res, bool /*include_filter*/)
×
UNCOV
83
  : data_({v_res, h_res, 2}, NOT_FOUND)
×
UNCOV
84
{}
×
85

UNCOV
86
void PropertyData::set_value(size_t y, size_t x, const Particle& p, int level,
×
87
  Filter* /*filter*/, FilterMatch* /*match*/)
88
{
UNCOV
89
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
×
UNCOV
90
  data_(y, x, 0) = (p.sqrtkT() * p.sqrtkT()) / K_BOLTZMANN;
×
UNCOV
91
  if (c->type_ != Fill::UNIVERSE && p.material() != MATERIAL_VOID) {
×
UNCOV
92
    Material* m = model::materials.at(p.material()).get();
×
UNCOV
93
    data_(y, x, 1) = m->density_gpcc_;
×
94
  }
95
}
×
96

97
void PropertyData::set_overlap(size_t y, size_t x)
×
98
{
UNCOV
99
  data_(y, x) = OVERLAP;
×
UNCOV
100
}
×
101

102
//==============================================================================
103
// RasterData implementation
104
//==============================================================================
105

106
RasterData::RasterData(size_t h_res, size_t v_res, bool include_filter)
291✔
107
  : id_data_({v_res, h_res, include_filter ? 4u : 3u}, NOT_FOUND),
566✔
108
    property_data_({v_res, h_res, 2}, static_cast<double>(NOT_FOUND)),
291✔
109
    include_filter_(include_filter)
291✔
110
{}
291✔
111

112
void RasterData::set_value(size_t y, size_t x, const Particle& p, int level,
2,659,956✔
113
  Filter* filter, FilterMatch* match)
114
{
115
  // set cell data
116
  if (p.n_coord() <= level) {
2,659,956!
UNCOV
117
    id_data_(y, x, 0) = NOT_FOUND;
×
UNCOV
118
    id_data_(y, x, 1) = NOT_FOUND;
×
119
  } else {
120
    id_data_(y, x, 0) = model::cells.at(p.coord(level).cell())->id_;
2,659,956!
121
    id_data_(y, x, 1) = level == p.n_coord() - 1
2,659,956✔
122
                          ? p.cell_instance()
2,659,956!
UNCOV
123
                          : cell_instance_at_level(p, level);
×
124
  }
125

126
  // set material data
127
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
2,659,956✔
128
  if (p.material() == MATERIAL_VOID) {
2,659,956✔
129
    id_data_(y, x, 2) = MATERIAL_VOID;
1,842,672✔
130
  } else if (c->type_ == Fill::MATERIAL) {
817,284!
131
    Material* m = model::materials.at(p.material()).get();
817,284✔
132
    id_data_(y, x, 2) = m->id_;
817,284✔
133
  }
134

135
  // set filter index (only if filter is being used)
136
  if (include_filter_ && filter) {
2,659,956!
137
    filter->get_all_bins(p, TallyEstimator::COLLISION, *match);
40,000✔
138
    if (match->bins_.empty()) {
40,000!
UNCOV
139
      id_data_(y, x, 3) = -1;
×
140
    } else {
141
      id_data_(y, x, 3) = match->bins_[0];
40,000✔
142
    }
143
    match->bins_.clear();
40,000!
144
    match->weights_.clear();
40,000!
145
  }
146

147
  // set temperature (in K)
148
  property_data_(y, x, 0) = (p.sqrtkT() * p.sqrtkT()) / K_BOLTZMANN;
2,659,956✔
149

150
  // set density (g/cm³)
151
  if (c->type_ != Fill::UNIVERSE && p.material() != MATERIAL_VOID) {
2,659,956!
152
    Material* m = model::materials.at(p.material()).get();
817,284✔
153
    property_data_(y, x, 1) = m->density_gpcc_;
817,284✔
154
  }
155
}
2,659,956✔
156

157
void RasterData::set_overlap(size_t y, size_t x)
251,680✔
158
{
159
  // Set cell, instance, and material to OVERLAP, but preserve filter bin
160
  id_data_(y, x, 0) = OVERLAP;
251,680✔
161
  id_data_(y, x, 1) = OVERLAP;
251,680✔
162
  id_data_(y, x, 2) = OVERLAP;
251,680✔
163
  // Note: id_data_(y, x, 3) is NOT overwritten - preserves filter bin for tally
164
  // plotting
165

166
  property_data_(y, x, 0) = OVERLAP;
251,680✔
167
  property_data_(y, x, 1) = OVERLAP;
251,680✔
168
}
251,680✔
169

170
//==============================================================================
171
// Global variables
172
//==============================================================================
173

174
namespace model {
175

176
std::unordered_map<int, int> plot_map;
177
vector<std::unique_ptr<PlottableInterface>> plots;
178
uint64_t plotter_seed = 1;
179

180
} // namespace model
181

182
//==============================================================================
183
// RUN_PLOT controls the logic for making one or many plots
184
//==============================================================================
185

186
extern "C" int openmc_plot_geometry()
88✔
187
{
188

189
  for (auto& pl : model::plots) {
296✔
190
    write_message(5, "Processing plot {}: {}...", pl->id(), pl->path_plot());
208✔
191
    pl->create_output();
208✔
192
  }
193

194
  return 0;
88✔
195
}
196

197
void PlottableInterface::write_image(const ImageData& data) const
168✔
198
{
199
#ifdef USE_LIBPNG
200
  output_png(path_plot(), data);
168✔
201
#else
202
  output_ppm(path_plot(), data);
203
#endif
204
}
168✔
205

206
void Plot::create_output() const
144✔
207
{
208
  if (PlotType::slice == type_) {
144✔
209
    // create 2D image
210
    ImageData image = create_image();
104✔
211
    write_image(image);
104✔
212
  } else if (PlotType::voxel == type_) {
144!
213
    // create voxel file for 3D viewing
214
    create_voxel();
40✔
215
  }
216
}
144✔
217

218
void Plot::print_info() const
112✔
219
{
220
  // Plot type
221
  if (PlotType::slice == type_) {
112✔
222
    fmt::print("Plot Type: Slice\n");
88✔
223
  } else if (PlotType::voxel == type_) {
24!
224
    fmt::print("Plot Type: Voxel\n");
24✔
225
  }
226

227
  // Plot parameters
228
  fmt::print("Origin: {} {} {}\n", origin_[0], origin_[1], origin_[2]);
112✔
229

230
  if (PlotType::slice == type_) {
112✔
231
    fmt::print("Width: {:4} {:4}\n", width_[0], width_[1]);
88✔
232
  } else if (PlotType::voxel == type_) {
24!
233
    fmt::print("Width: {:4} {:4} {:4}\n", width_[0], width_[1], width_[2]);
24✔
234
  }
235

236
  if (PlotColorBy::cells == color_by_) {
112✔
237
    fmt::print("Coloring: Cells\n");
64✔
238
  } else if (PlotColorBy::mats == color_by_) {
48!
239
    fmt::print("Coloring: Materials\n");
48✔
240
  }
241

242
  if (PlotType::slice == type_) {
112✔
243
    switch (basis_) {
88!
244
    case PlotBasis::xy:
56✔
245
      fmt::print("Basis: XY\n");
56✔
246
      break;
56✔
247
    case PlotBasis::xz:
16✔
248
      fmt::print("Basis: XZ\n");
16✔
249
      break;
16✔
250
    case PlotBasis::yz:
16✔
251
      fmt::print("Basis: YZ\n");
16✔
252
      break;
16✔
253
    }
254
    fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
88✔
255
  } else if (PlotType::voxel == type_) {
24!
256
    fmt::print("Voxels: {} {} {}\n", pixels()[0], pixels()[1], pixels()[2]);
24✔
257
  }
258
}
112✔
259

260
void read_plots_xml()
978✔
261
{
262
  // Check if plots.xml exists; this is only necessary when the plot runmode is
263
  // initiated. Otherwise, we want to read plots.xml because it may be called
264
  // later via the API. In that case, its ok for a plots.xml to not exist
265
  std::string filename = settings::path_input + "plots.xml";
978✔
266
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
978!
UNCOV
267
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
268
  }
269

270
  write_message("Reading plot XML file...", 5);
978✔
271

272
  // Parse plots.xml file
273
  pugi::xml_document doc;
978✔
274
  doc.load_file(filename.c_str());
978✔
275

276
  pugi::xml_node root = doc.document_element();
978✔
277

278
  read_plots_xml(root);
978✔
279
}
978✔
280

281
void read_plots_xml(pugi::xml_node root)
1,331✔
282
{
283
  for (auto node : root.children("plot")) {
2,031✔
284
    std::string plot_desc = "<auto>";
707✔
285
    if (check_for_node(node, "id")) {
707!
286
      plot_desc = get_node_value(node, "id", true);
707✔
287
    }
288

289
    if (check_for_node(node, "type")) {
707!
290
      std::string type_str = get_node_value(node, "type", true);
707✔
291
      if (type_str == "slice") {
707✔
292
        model::plots.emplace_back(
596✔
293
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,199✔
294
      } else if (type_str == "voxel") {
104✔
295
        model::plots.emplace_back(
40✔
296
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
80✔
297
      } else if (type_str == "wireframe_raytrace") {
64✔
298
        model::plots.emplace_back(
40✔
299
          std::make_unique<WireframeRayTracePlot>(node));
80✔
300
      } else if (type_str == "solid_raytrace") {
24!
301
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
24✔
302
      } else {
303
        fatal_error(fmt::format(
×
304
          "Unsupported plot type '{}' in plot {}", type_str, plot_desc));
305
      }
306
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
700✔
307
    } else {
700✔
UNCOV
308
      fatal_error(fmt::format("Must specify plot type in plot {}", plot_desc));
×
309
    }
310
  }
700✔
311
}
1,324✔
312

313
void free_memory_plot()
6,224✔
314
{
315
  model::plots.clear();
6,224✔
316
  model::plot_map.clear();
6,224✔
317
}
6,224✔
318

319
// creates an image based on user input from a plots.xml <plot>
320
// specification in the PNG/PPM format
321
ImageData Plot::create_image() const
104✔
322
{
323
  size_t width = pixels()[0];
104✔
324
  size_t height = pixels()[1];
104✔
325

326
  ImageData data({width, height}, not_found_);
104✔
327

328
  // generate ids for the plot
329
  auto ids = get_map<IdData>();
104✔
330

331
  // assign colors
332
  for (size_t y = 0; y < height; y++) {
21,864✔
333
    for (size_t x = 0; x < width; x++) {
5,543,360✔
334
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
5,521,600✔
335
      auto id = ids.data_(y, x, idx);
5,521,600✔
336
      // no setting needed if not found
337
      if (id == NOT_FOUND) {
5,521,600✔
338
        continue;
787,296✔
339
      }
340
      if (id == OVERLAP) {
4,754,848✔
341
        data(x, y) = overlap_color_;
20,544✔
342
        continue;
20,544✔
343
      }
344
      if (PlotColorBy::cells == color_by_) {
4,734,304✔
345
        data(x, y) = colors_[model::cell_map[id]];
2,190,304✔
346
      } else if (PlotColorBy::mats == color_by_) {
2,544,000!
347
        if (id == MATERIAL_VOID) {
2,544,000!
UNCOV
348
          data(x, y) = WHITE;
×
UNCOV
349
          continue;
×
350
        }
351
        data(x, y) = colors_[model::material_map[id]];
2,544,000✔
352
      } // color_by if-else
353
    }
354
  }
355

356
  // draw mesh lines if present
357
  if (index_meshlines_mesh_ >= 0) {
104✔
358
    draw_mesh_lines(data);
24✔
359
  }
360

361
  return data;
104✔
362
}
104✔
363

364
void PlottableInterface::set_id(pugi::xml_node plot_node)
707✔
365
{
366
  int id {C_NONE};
707✔
367
  if (check_for_node(plot_node, "id")) {
707!
368
    id = std::stoi(get_node_value(plot_node, "id"));
707✔
369
  }
370

371
  try {
707✔
372
    set_id(id);
707✔
UNCOV
373
  } catch (const std::runtime_error& e) {
×
UNCOV
374
    fatal_error(e.what());
×
UNCOV
375
  }
×
376
}
707✔
377

378
void PlottableInterface::set_id(int id)
715✔
379
{
380
  if (id < 0 && id != C_NONE) {
715!
UNCOV
381
    throw std::runtime_error {fmt::format("Invalid plot ID: {}", id)};
×
382
  }
383

384
  if (id == C_NONE) {
715✔
385
    id = 1;
8✔
386
    for (const auto& p : model::plots) {
16✔
387
      id = std::max(id, p->id() + 1);
16!
388
    }
389
  }
390

391
  if (id_ == id)
715!
392
    return;
393

394
  // Check to make sure this ID doesn't already exist
395
  if (model::plot_map.find(id) != model::plot_map.end()) {
715!
UNCOV
396
    throw std::runtime_error {
×
UNCOV
397
      fmt::format("Two or more plots use the same unique ID: {}", id)};
×
398
  }
399

400
  id_ = id;
715✔
401
}
402

403
// Checks if png or ppm is already present
404
bool file_extension_present(
700✔
405
  const std::string& filename, const std::string& extension)
406
{
407
  std::string file_extension_if_present =
700✔
408
    filename.substr(filename.find_last_of(".") + 1);
700✔
409
  if (file_extension_if_present == extension)
700✔
410
    return true;
40✔
411
  return false;
412
}
700✔
413

414
void Plot::set_output_path(pugi::xml_node plot_node)
643✔
415
{
416
  // Set output file path
417
  std::string filename;
643✔
418

419
  if (check_for_node(plot_node, "filename")) {
643✔
420
    filename = get_node_value(plot_node, "filename");
171✔
421
  } else {
422
    filename = fmt::format("plot_{}", id());
472✔
423
  }
424
  const std::string dir_if_present =
643✔
425
    filename.substr(0, filename.find_last_of("/") + 1);
643✔
426
  if (dir_if_present.size() > 0 && !dir_exists(dir_if_present)) {
643✔
427
    fatal_error(fmt::format("Directory '{}' does not exist!", dir_if_present));
7✔
428
  }
429
  // add appropriate file extension to name
430
  switch (type_) {
636!
431
  case PlotType::slice:
596✔
432
#ifdef USE_LIBPNG
433
    if (!file_extension_present(filename, "png"))
596!
434
      filename.append(".png");
596✔
435
#else
436
    if (!file_extension_present(filename, "ppm"))
437
      filename.append(".ppm");
438
#endif
439
    break;
440
  case PlotType::voxel:
40✔
441
    if (!file_extension_present(filename, "h5"))
40!
442
      filename.append(".h5");
40✔
443
    break;
444
  }
445

446
  path_plot_ = filename;
636✔
447

448
  // Copy plot pixel size
449
  vector<int> pxls = get_node_array<int>(plot_node, "pixels");
1,272✔
450
  if (PlotType::slice == type_) {
636✔
451
    if (pxls.size() == 2) {
596!
452
      pixels()[0] = pxls[0];
596✔
453
      pixels()[1] = pxls[1];
596✔
454
    } else {
455
      fatal_error(
×
UNCOV
456
        fmt::format("<pixels> must be length 2 in slice plot {}", id()));
×
457
    }
458
  } else if (PlotType::voxel == type_) {
40!
459
    if (pxls.size() == 3) {
40!
460
      pixels()[0] = pxls[0];
40✔
461
      pixels()[1] = pxls[1];
40✔
462
      pixels()[2] = pxls[2];
40✔
463
    } else {
UNCOV
464
      fatal_error(
×
UNCOV
465
        fmt::format("<pixels> must be length 3 in voxel plot {}", id()));
×
466
    }
467
  }
468
}
636✔
469

470
void PlottableInterface::set_bg_color(pugi::xml_node plot_node)
707✔
471
{
472
  // Copy plot background color
473
  if (check_for_node(plot_node, "background")) {
707✔
474
    vector<int> bg_rgb = get_node_array<int>(plot_node, "background");
32✔
475
    if (bg_rgb.size() == 3) {
32!
476
      not_found_ = bg_rgb;
32✔
477
    } else {
UNCOV
478
      fatal_error(fmt::format("Bad background RGB in plot {}", id()));
×
479
    }
480
  }
32✔
481
}
707✔
482

483
void Plot::set_basis(pugi::xml_node plot_node)
636✔
484
{
485
  // Copy plot basis
486
  if (PlotType::slice == type_) {
636✔
487
    std::string pl_basis = "xy";
596✔
488
    if (check_for_node(plot_node, "basis")) {
596!
489
      pl_basis = get_node_value(plot_node, "basis", true);
596✔
490
    }
491
    if ("xy" == pl_basis) {
596✔
492
      basis_ = PlotBasis::xy;
544✔
493
    } else if ("xz" == pl_basis) {
52✔
494
      basis_ = PlotBasis::xz;
16✔
495
    } else if ("yz" == pl_basis) {
36!
496
      basis_ = PlotBasis::yz;
36✔
497
    } else {
UNCOV
498
      fatal_error(
×
UNCOV
499
        fmt::format("Unsupported plot basis '{}' in plot {}", pl_basis, id()));
×
500
    }
501
  }
596✔
502
}
636✔
503

504
void Plot::set_origin(pugi::xml_node plot_node)
636✔
505
{
506
  // Copy plotting origin
507
  auto pl_origin = get_node_array<double>(plot_node, "origin");
636✔
508
  if (pl_origin.size() == 3) {
636!
509
    origin_ = pl_origin;
636✔
510
  } else {
511
    fatal_error(fmt::format("Origin must be length 3 in plot {}", id()));
×
512
  }
513
}
636✔
514

515
void Plot::set_width(pugi::xml_node plot_node)
636✔
516
{
517
  // Copy plotting width
518
  vector<double> pl_width = get_node_array<double>(plot_node, "width");
636✔
519
  if (PlotType::slice == type_) {
636✔
520
    if (pl_width.size() == 2) {
596!
521
      width_.x = pl_width[0];
596✔
522
      width_.y = pl_width[1];
596✔
523
      switch (basis_) {
596!
524
      case PlotBasis::xy:
544✔
525
        u_span_ = {width_.x, 0.0, 0.0};
544✔
526
        v_span_ = {0.0, width_.y, 0.0};
544✔
527
        break;
544✔
528
      case PlotBasis::xz:
16✔
529
        u_span_ = {width_.x, 0.0, 0.0};
16✔
530
        v_span_ = {0.0, 0.0, width_.y};
16✔
531
        break;
16✔
532
      case PlotBasis::yz:
36✔
533
        u_span_ = {0.0, width_.x, 0.0};
36✔
534
        v_span_ = {0.0, 0.0, width_.y};
36✔
535
        break;
36✔
UNCOV
536
      default:
×
UNCOV
537
        UNREACHABLE();
×
538
      }
539
    } else {
UNCOV
540
      fatal_error(
×
UNCOV
541
        fmt::format("<width> must be length 2 in slice plot {}", id()));
×
542
    }
543
  } else if (PlotType::voxel == type_) {
40!
544
    if (pl_width.size() == 3) {
40!
545
      pl_width = get_node_array<double>(plot_node, "width");
80✔
546
      width_ = pl_width;
40✔
547
    } else {
UNCOV
548
      fatal_error(
×
UNCOV
549
        fmt::format("<width> must be length 3 in voxel plot {}", id()));
×
550
    }
551
  }
552
}
636✔
553

554
void PlottableInterface::set_universe(pugi::xml_node plot_node)
707✔
555
{
556
  // Copy plot universe level
557
  if (check_for_node(plot_node, "level")) {
707!
UNCOV
558
    level_ = std::stoi(get_node_value(plot_node, "level"));
×
UNCOV
559
    if (level_ < 0) {
×
560
      fatal_error(fmt::format("Bad universe level in plot {}", id()));
×
561
    }
562
  } else {
563
    level_ = PLOT_LEVEL_LOWEST;
707✔
564
  }
565
}
707✔
566

567
void PlottableInterface::set_color_by(pugi::xml_node plot_node)
707✔
568
{
569
  // Copy plot color type
570
  std::string pl_color_by = "cell";
707✔
571
  if (check_for_node(plot_node, "color_by")) {
707✔
572
    pl_color_by = get_node_value(plot_node, "color_by", true);
683✔
573
  }
574
  if ("cell" == pl_color_by) {
707✔
575
    color_by_ = PlotColorBy::cells;
206✔
576
  } else if ("material" == pl_color_by) {
501!
577
    color_by_ = PlotColorBy::mats;
501✔
578
  } else {
UNCOV
579
    fatal_error(fmt::format(
×
UNCOV
580
      "Unsupported plot color type '{}' in plot {}", pl_color_by, id()));
×
581
  }
582
}
707✔
583

584
void PlottableInterface::set_default_colors()
715✔
585
{
586
  // Copy plot color type and initialize all colors randomly
587
  if (PlotColorBy::cells == color_by_) {
715✔
588
    colors_.resize(model::cells.size());
206✔
589
  } else if (PlotColorBy::mats == color_by_) {
509!
590
    colors_.resize(model::materials.size());
509✔
591
  }
592

593
  for (auto& c : colors_) {
3,178✔
594
    c = random_color();
2,463✔
595
    // make sure we don't interfere with some default colors
596
    while (c == RED || c == WHITE) {
2,463!
597
      c = random_color();
×
598
    }
599
  }
600
}
715✔
601

602
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
707✔
603
{
604
  for (auto cn : plot_node.children("color")) {
843✔
605
    // Make sure 3 values are specified for RGB
606
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
136✔
607
    if (user_rgb.size() != 3) {
136!
608
      fatal_error(fmt::format("Bad RGB in plot {}", id()));
×
609
    }
610
    // Ensure that there is an id for this color specification
611
    int col_id;
136✔
612
    if (check_for_node(cn, "id")) {
136!
613
      col_id = std::stoi(get_node_value(cn, "id"));
272✔
614
    } else {
UNCOV
615
      fatal_error(fmt::format(
×
UNCOV
616
        "Must specify id for color specification in plot {}", id()));
×
617
    }
618
    // Add RGB
619
    if (PlotColorBy::cells == color_by_) {
136✔
620
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
64!
621
        col_id = model::cell_map[col_id];
64✔
622
        colors_[col_id] = user_rgb;
64✔
623
      } else {
UNCOV
624
        warning(fmt::format(
×
UNCOV
625
          "Could not find cell {} specified in plot {}", col_id, id()));
×
626
      }
627
    } else if (PlotColorBy::mats == color_by_) {
72!
628
      if (model::material_map.find(col_id) != model::material_map.end()) {
72!
629
        col_id = model::material_map[col_id];
72✔
630
        colors_[col_id] = user_rgb;
72✔
631
      } else {
UNCOV
632
        warning(fmt::format(
×
UNCOV
633
          "Could not find material {} specified in plot {}", col_id, id()));
×
634
      }
635
    }
636
  } // color node loop
136✔
637
}
707✔
638

639
void Plot::set_meshlines(pugi::xml_node plot_node)
636✔
640
{
641
  // Deal with meshlines
642
  pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines");
636✔
643

644
  if (!mesh_line_nodes.empty()) {
636✔
645
    if (PlotType::voxel == type_) {
24!
UNCOV
646
      warning(fmt::format("Meshlines ignored in voxel plot {}", id()));
×
647
    }
648

649
    if (mesh_line_nodes.size() == 1) {
24!
650
      // Get first meshline node
651
      pugi::xml_node meshlines_node = mesh_line_nodes[0].node();
24✔
652

653
      // Check mesh type
654
      std::string meshtype;
24✔
655
      if (check_for_node(meshlines_node, "meshtype")) {
24!
656
        meshtype = get_node_value(meshlines_node, "meshtype");
24✔
657
      } else {
UNCOV
658
        fatal_error(fmt::format(
×
659
          "Must specify a meshtype for meshlines specification in plot {}",
UNCOV
660
          id()));
×
661
      }
662

663
      // Ensure that there is a linewidth for this meshlines specification
664
      std::string meshline_width;
24✔
665
      if (check_for_node(meshlines_node, "linewidth")) {
24!
666
        meshline_width = get_node_value(meshlines_node, "linewidth");
24✔
667
        meshlines_width_ = std::stoi(meshline_width);
24✔
668
      } else {
UNCOV
669
        fatal_error(fmt::format(
×
670
          "Must specify a linewidth for meshlines specification in plot {}",
UNCOV
671
          id()));
×
672
      }
673

674
      // Check for color
675
      if (check_for_node(meshlines_node, "color")) {
24!
676
        // Check and make sure 3 values are specified for RGB
UNCOV
677
        vector<int> ml_rgb = get_node_array<int>(meshlines_node, "color");
×
UNCOV
678
        if (ml_rgb.size() != 3) {
×
UNCOV
679
          fatal_error(
×
UNCOV
680
            fmt::format("Bad RGB for meshlines color in plot {}", id()));
×
681
        }
UNCOV
682
        meshlines_color_ = ml_rgb;
×
683
      }
684

685
      // Set mesh based on type
686
      if ("ufs" == meshtype) {
24!
687
        if (!simulation::ufs_mesh) {
×
UNCOV
688
          fatal_error(
×
689
            fmt::format("No UFS mesh for meshlines on plot {}", id()));
×
690
        } else {
691
          for (int i = 0; i < model::meshes.size(); ++i) {
×
UNCOV
692
            if (const auto* m =
×
693
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
×
UNCOV
694
              if (m == simulation::ufs_mesh) {
×
695
                index_meshlines_mesh_ = i;
×
696
              }
697
            }
698
          }
UNCOV
699
          if (index_meshlines_mesh_ == -1)
×
UNCOV
700
            fatal_error("Could not find the UFS mesh for meshlines plot");
×
701
        }
702
      } else if ("entropy" == meshtype) {
24✔
703
        if (!simulation::entropy_mesh) {
16!
UNCOV
704
          fatal_error(
×
UNCOV
705
            fmt::format("No entropy mesh for meshlines on plot {}", id()));
×
706
        } else {
707
          for (int i = 0; i < model::meshes.size(); ++i) {
40✔
708
            if (const auto* m =
48✔
709
                  dynamic_cast<const RegularMesh*>(model::meshes[i].get())) {
40!
710
              if (m == simulation::entropy_mesh) {
16!
711
                index_meshlines_mesh_ = i;
16✔
712
              }
713
            }
714
          }
715
          if (index_meshlines_mesh_ == -1)
16!
UNCOV
716
            fatal_error("Could not find the entropy mesh for meshlines plot");
×
717
        }
718
      } else if ("tally" == meshtype) {
8!
719
        // Ensure that there is a mesh id if the type is tally
720
        int tally_mesh_id;
8✔
721
        if (check_for_node(meshlines_node, "id")) {
8!
722
          tally_mesh_id = std::stoi(get_node_value(meshlines_node, "id"));
16✔
723
        } else {
UNCOV
724
          std::stringstream err_msg;
×
UNCOV
725
          fatal_error(fmt::format("Must specify a mesh id for meshlines tally "
×
726
                                  "mesh specification in plot {}",
UNCOV
727
            id()));
×
728
        }
×
729
        // find the tally index
730
        int idx;
8✔
731
        int err = openmc_get_mesh_index(tally_mesh_id, &idx);
8✔
732
        if (err != 0) {
8!
UNCOV
733
          fatal_error(fmt::format("Could not find mesh {} specified in "
×
734
                                  "meshlines for plot {}",
UNCOV
735
            tally_mesh_id, id()));
×
736
        }
737
        index_meshlines_mesh_ = idx;
8✔
738
      } else {
UNCOV
739
        fatal_error(fmt::format("Invalid type for meshlines on plot {}", id()));
×
740
      }
741
    } else {
24✔
UNCOV
742
      fatal_error(fmt::format("Mutliple meshlines specified in plot {}", id()));
×
743
    }
744
  }
745
}
636✔
746

747
void PlottableInterface::set_mask(pugi::xml_node plot_node)
707✔
748
{
749
  // Deal with masks
750
  pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask");
707✔
751

752
  if (!mask_nodes.empty()) {
707✔
753
    if (mask_nodes.size() == 1) {
24!
754
      // Get pointer to mask
755
      pugi::xml_node mask_node = mask_nodes[0].node();
24✔
756

757
      // Determine how many components there are and allocate
758
      vector<int> iarray = get_node_array<int>(mask_node, "components");
24✔
759
      if (iarray.size() == 0) {
24!
UNCOV
760
        fatal_error(
×
UNCOV
761
          fmt::format("Missing <components> in mask of plot {}", id()));
×
762
      }
763

764
      // First we need to change the user-specified identifiers to indices
765
      // in the cell and material arrays
766
      for (auto& col_id : iarray) {
72✔
767
        if (PlotColorBy::cells == color_by_) {
48!
768
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
48!
769
            col_id = model::cell_map[col_id];
48✔
770
          } else {
UNCOV
771
            fatal_error(fmt::format("Could not find cell {} specified in the "
×
772
                                    "mask in plot {}",
UNCOV
773
              col_id, id()));
×
774
          }
775
        } else if (PlotColorBy::mats == color_by_) {
×
UNCOV
776
          if (model::material_map.find(col_id) != model::material_map.end()) {
×
UNCOV
777
            col_id = model::material_map[col_id];
×
778
          } else {
779
            fatal_error(fmt::format("Could not find material {} specified in "
×
780
                                    "the mask in plot {}",
UNCOV
781
              col_id, id()));
×
782
          }
783
        }
784
      }
785

786
      // Alter colors based on mask information
787
      for (int j = 0; j < colors_.size(); j++) {
96✔
788
        if (contains(iarray, j)) {
72✔
789
          if (check_for_node(mask_node, "background")) {
48!
790
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
48✔
791
            colors_[j] = bg_rgb;
48✔
792
          } else {
48✔
793
            colors_[j] = WHITE;
×
794
          }
795
        }
796
      }
797

798
    } else {
24✔
799
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
800
    }
801
  }
802
}
707✔
803

804
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
707✔
805
{
806
  color_overlaps_ = false;
707✔
807
  if (check_for_node(plot_node, "show_overlaps")) {
707✔
808
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
16✔
809
    // check for custom overlap color
810
    if (check_for_node(plot_node, "overlap_color")) {
16✔
811
      if (!color_overlaps_) {
8!
UNCOV
812
        warning(fmt::format(
×
813
          "Overlap color specified in plot {} but overlaps won't be shown.",
UNCOV
814
          id()));
×
815
      }
816
      vector<int> olap_clr = get_node_array<int>(plot_node, "overlap_color");
8✔
817
      if (olap_clr.size() == 3) {
8!
818
        overlap_color_ = olap_clr;
8✔
819
      } else {
820
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
821
      }
822
    }
8✔
823
  }
824

825
  // make sure we allocate the vector for counting overlap checks if
826
  // they're going to be plotted
827
  if (color_overlaps_ && settings::run_mode == RunMode::PLOTTING) {
707!
828
    settings::check_overlaps = true;
16✔
829
    model::overlap_check_count.resize(model::cells.size(), 0);
16✔
830
  }
831
}
707✔
832

833
PlottableInterface::PlottableInterface(pugi::xml_node plot_node)
707✔
834
{
835
  set_id(plot_node);
707✔
836
  set_bg_color(plot_node);
707✔
837
  set_universe(plot_node);
707✔
838
  set_color_by(plot_node);
707✔
839
  set_default_colors();
707✔
840
  set_user_colors(plot_node);
707✔
841
  set_mask(plot_node);
707✔
842
  set_overlap_color(plot_node);
707✔
843
}
707✔
844

845
Plot::Plot(pugi::xml_node plot_node, PlotType type)
643✔
846
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
643✔
847
{
848
  set_output_path(plot_node);
643✔
849
  set_basis(plot_node);
636✔
850
  set_origin(plot_node);
636✔
851
  set_width(plot_node);
636✔
852
  set_meshlines(plot_node);
636✔
853
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
636✔
854
  show_overlaps_ = color_overlaps_;
636✔
855
}
636✔
856

857
//==============================================================================
858
// OUTPUT_PPM writes out a previously generated image to a PPM file
859
//==============================================================================
860

UNCOV
861
void output_ppm(const std::string& filename, const ImageData& data)
×
862
{
863
  // Open PPM file for writing
UNCOV
864
  std::string fname = filename;
×
UNCOV
865
  fname = strtrim(fname);
×
UNCOV
866
  std::ofstream of;
×
867

UNCOV
868
  of.open(fname);
×
869

870
  // Write header
UNCOV
871
  of << "P6\n";
×
UNCOV
872
  of << data.shape(0) << " " << data.shape(1) << "\n";
×
UNCOV
873
  of << "255\n";
×
UNCOV
874
  of.close();
×
875

UNCOV
876
  of.open(fname, std::ios::binary | std::ios::app);
×
877
  // Write color for each pixel
878
  for (int y = 0; y < data.shape(1); y++) {
×
879
    for (int x = 0; x < data.shape(0); x++) {
×
UNCOV
880
      RGBColor rgb = data(x, y);
×
UNCOV
881
      of << rgb.red << rgb.green << rgb.blue;
×
882
    }
883
  }
UNCOV
884
  of << "\n";
×
UNCOV
885
}
×
886

887
//==============================================================================
888
// OUTPUT_PNG writes out a previously generated image to a PNG file
889
//==============================================================================
890

891
#ifdef USE_LIBPNG
892
void output_png(const std::string& filename, const ImageData& data)
168✔
893
{
894
  // Open PNG file for writing
895
  std::string fname = filename;
168✔
896
  fname = strtrim(fname);
168✔
897
  auto fp = std::fopen(fname.c_str(), "wb");
168✔
898

899
  // Initialize write and info structures
900
  auto png_ptr =
168✔
901
    png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
168✔
902
  auto info_ptr = png_create_info_struct(png_ptr);
168✔
903

904
  // Setup exception handling
905
  if (setjmp(png_jmpbuf(png_ptr)))
168!
906
    fatal_error("Error during png creation");
×
907

908
  png_init_io(png_ptr, fp);
168✔
909

910
  // Write header (8 bit colour depth)
911
  int width = data.shape(0);
168!
912
  int height = data.shape(1);
168!
913
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
168✔
914
    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
915
  png_write_info(png_ptr, info_ptr);
168✔
916

917
  // Allocate memory for one row (3 bytes per pixel - RGB)
918
  std::vector<png_byte> row(3 * width);
168✔
919

920
  // Write color for each pixel
921
  for (int y = 0; y < height; y++) {
34,728✔
922
    for (int x = 0; x < width; x++) {
8,116,160✔
923
      RGBColor rgb = data(x, y);
8,081,600✔
924
      row[3 * x] = rgb.red;
8,081,600✔
925
      row[3 * x + 1] = rgb.green;
8,081,600✔
926
      row[3 * x + 2] = rgb.blue;
8,081,600✔
927
    }
928
    png_write_row(png_ptr, row.data());
34,560✔
929
  }
930

931
  // End write
932
  png_write_end(png_ptr, nullptr);
168✔
933

934
  // Clean up data structures
935
  std::fclose(fp);
168✔
936
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
168✔
937
  png_destroy_write_struct(&png_ptr, &info_ptr);
168✔
938
}
168✔
939
#endif
940

941
//==============================================================================
942
// DRAW_MESH_LINES draws mesh line boundaries on an image
943
//==============================================================================
944

945
void Plot::draw_mesh_lines(ImageData& data) const
24✔
946
{
947
  RGBColor rgb;
24!
948
  rgb = meshlines_color_;
24✔
949

950
  int ax1, ax2;
24✔
951
  Position expected_u {};
24✔
952
  Position expected_v {};
24✔
953
  switch (basis_) {
24!
954
  case PlotBasis::xy:
16✔
955
    ax1 = 0;
16✔
956
    ax2 = 1;
16✔
957
    expected_u = {width_[0], 0.0, 0.0};
16✔
958
    expected_v = {0.0, width_[1], 0.0};
16✔
959
    break;
16✔
960
  case PlotBasis::xz:
8✔
961
    ax1 = 0;
8✔
962
    ax2 = 2;
8✔
963
    expected_u = {width_[0], 0.0, 0.0};
8✔
964
    expected_v = {0.0, 0.0, width_[1]};
8✔
965
    break;
8✔
UNCOV
966
  case PlotBasis::yz:
×
UNCOV
967
    ax1 = 1;
×
UNCOV
968
    ax2 = 2;
×
UNCOV
969
    expected_u = {0.0, width_[0], 0.0};
×
UNCOV
970
    expected_v = {0.0, 0.0, width_[1]};
×
UNCOV
971
    break;
×
UNCOV
972
  default:
×
UNCOV
973
    UNREACHABLE();
×
974
  }
975

976
  // Meshlines rely on axis-aligned indexing in global coordinates.
977
  constexpr double rel_tol {1e-12};
24✔
978
  double span_tol = rel_tol * (1.0 + u_span_.norm() + v_span_.norm());
24✔
979
  if ((u_span_ - expected_u).norm() > span_tol ||
48!
980
      (v_span_ - expected_v).norm() > span_tol) {
24✔
UNCOV
981
    fatal_error("Meshlines are only supported for axis-aligned slice plots.");
×
982
  }
983

984
  Position ll_plot {origin_};
24✔
985
  Position ur_plot {origin_};
24✔
986

987
  ll_plot[ax1] -= width_[0] / 2.;
24✔
988
  ll_plot[ax2] -= width_[1] / 2.;
24✔
989
  ur_plot[ax1] += width_[0] / 2.;
24✔
990
  ur_plot[ax2] += width_[1] / 2.;
24✔
991

992
  Position width = ur_plot - ll_plot;
24✔
993

994
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
995
  auto axis_lines =
24✔
996
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
24✔
997

998
  // Find the bounds along the second axis (accounting for low-D meshes).
999
  int ax2_min, ax2_max;
24✔
1000
  if (axis_lines.second.size() > 0) {
24!
1001
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
24✔
1002
    ax2_min = (1.0 - frac) * pixels()[1];
24✔
1003
    if (ax2_min < 0)
24✔
1004
      ax2_min = 0;
1005
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
24✔
1006
    ax2_max = (1.0 - frac) * pixels()[1];
24!
1007
    if (ax2_max > pixels()[1])
24!
UNCOV
1008
      ax2_max = pixels()[1];
×
1009
  } else {
UNCOV
1010
    ax2_min = 0;
×
UNCOV
1011
    ax2_max = pixels()[1];
×
1012
  }
1013

1014
  // Iterate across the first axis and draw lines.
1015
  for (auto ax1_val : axis_lines.first) {
136✔
1016
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
112✔
1017
    int ax1_ind = frac * pixels()[0];
112✔
1018
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
18,144✔
1019
      for (int plus = 0; plus <= meshlines_width_; plus++) {
36,064✔
1020
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels()[0])
18,032!
1021
          data(ax1_ind + plus, ax2_ind) = rgb;
18,032✔
1022
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels()[0])
18,032!
1023
          data(ax1_ind - plus, ax2_ind) = rgb;
18,032✔
1024
      }
1025
    }
1026
  }
1027

1028
  // Find the bounds along the first axis.
1029
  int ax1_min, ax1_max;
24✔
1030
  if (axis_lines.first.size() > 0) {
24!
1031
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
24✔
1032
    ax1_min = frac * pixels()[0];
24✔
1033
    if (ax1_min < 0)
24✔
1034
      ax1_min = 0;
1035
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
24✔
1036
    ax1_max = frac * pixels()[0];
24!
1037
    if (ax1_max > pixels()[0])
24!
UNCOV
1038
      ax1_max = pixels()[0];
×
1039
  } else {
UNCOV
1040
    ax1_min = 0;
×
UNCOV
1041
    ax1_max = pixels()[0];
×
1042
  }
1043

1044
  // Iterate across the second axis and draw lines.
1045
  for (auto ax2_val : axis_lines.second) {
152✔
1046
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
128✔
1047
    int ax2_ind = (1.0 - frac) * pixels()[1];
128✔
1048
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
20,608✔
1049
      for (int plus = 0; plus <= meshlines_width_; plus++) {
40,960✔
1050
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels()[1])
20,480!
1051
          data(ax1_ind, ax2_ind + plus) = rgb;
20,480✔
1052
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels()[1])
20,480!
1053
          data(ax1_ind, ax2_ind - plus) = rgb;
20,480✔
1054
      }
1055
    }
1056
  }
1057
}
24✔
1058

1059
/* outputs a binary file that can be input into silomesh for 3D geometry
1060
 * visualization.  It works the same way as create_image by dragging a particle
1061
 * across the geometry for the specified number of voxels. The first 3 int's in
1062
 * the binary are the number of x, y, and z voxels.  The next 3 double's are
1063
 * the widths of the voxels in the x, y, and z directions. The next 3 double's
1064
 * are the x, y, and z coordinates of the lower left point. Finally the binary
1065
 * is filled with entries of four int's each. Each 'row' in the binary contains
1066
 * four int's: 3 for x,y,z position and 1 for cell or material id.  For 1
1067
 * million voxels this produces a file of approximately 15MB.
1068
 */
1069
void Plot::create_voxel() const
40✔
1070
{
1071
  // compute voxel widths in each direction
1072
  array<double, 3> vox;
40✔
1073
  vox[0] = width_[0] / static_cast<double>(pixels()[0]);
40✔
1074
  vox[1] = width_[1] / static_cast<double>(pixels()[1]);
40✔
1075
  vox[2] = width_[2] / static_cast<double>(pixels()[2]);
40✔
1076

1077
  // initial particle position
1078
  Position ll = origin_ - width_ / 2.;
40✔
1079

1080
  // Open binary plot file for writing
1081
  std::ofstream of;
40✔
1082
  std::string fname = std::string(path_plot_);
40✔
1083
  fname = strtrim(fname);
40✔
1084
  hid_t file_id = file_open(fname, 'w');
40✔
1085

1086
  // write header info
1087
  write_attribute(file_id, "filetype", "voxel");
40✔
1088
  write_attribute(file_id, "version", VERSION_VOXEL);
40✔
1089
  write_attribute(file_id, "openmc_version", VERSION);
40✔
1090

1091
#ifdef GIT_SHA1
1092
  write_attribute(file_id, "git_sha1", GIT_SHA1);
1093
#endif
1094

1095
  // Write current date and time
1096
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
80✔
1097
  array<int, 3> h5_pixels;
40✔
1098
  std::copy(pixels().begin(), pixels().end(), h5_pixels.begin());
40✔
1099
  write_attribute(file_id, "num_voxels", h5_pixels);
40✔
1100
  write_attribute(file_id, "voxel_width", vox);
40✔
1101
  write_attribute(file_id, "lower_left", ll);
40✔
1102

1103
  // Create dataset for voxel data -- note that the dimensions are reversed
1104
  // since we want the order in the file to be z, y, x
1105
  hsize_t dims[3];
40✔
1106
  dims[0] = pixels()[2];
40✔
1107
  dims[1] = pixels()[1];
40✔
1108
  dims[2] = pixels()[0];
40✔
1109
  hid_t dspace, dset, memspace;
40✔
1110
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
40✔
1111

1112
  SlicePlotBase pltbase;
40✔
1113
  pltbase.origin_ = origin_;
40✔
1114
  pltbase.u_span_ = {width_.x, 0.0, 0.0};
40✔
1115
  pltbase.v_span_ = {0.0, width_.y, 0.0};
40✔
1116
  pltbase.pixels() = pixels();
40✔
1117
  pltbase.show_overlaps_ = color_overlaps_;
40✔
1118

1119
  ProgressBar pb;
40✔
1120
  for (int z = 0; z < pixels()[2]; z++) {
3,480✔
1121
    // update z coordinate
1122
    pltbase.origin_.z = ll.z + z * vox[2];
3,440✔
1123

1124
    // generate ids using plotbase
1125
    IdData ids = pltbase.get_map<IdData>();
3,440✔
1126

1127
    // select only cell/material ID data and flip the y-axis
1128
    int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
3,440!
1129
    // Extract 2D slice at index idx from 3D data
1130
    size_t rows = ids.data_.shape(0);
3,440!
1131
    size_t cols = ids.data_.shape(1);
3,440!
1132
    tensor::Tensor<int32_t> data_slice({rows, cols});
3,440✔
1133
    for (size_t r = 0; r < rows; ++r)
663,440✔
1134
      for (size_t c = 0; c < cols; ++c)
130,460,000✔
1135
        data_slice(r, c) = ids.data_(r, c, idx);
129,800,000✔
1136
    tensor::Tensor<int32_t> data_flipped = data_slice.flip(0);
3,440✔
1137

1138
    // Write to HDF5 dataset
1139
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
3,440✔
1140

1141
    // update progress bar
1142
    pb.set_value(
3,440✔
1143
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels()[2])));
3,440✔
1144
  }
10,320✔
1145

1146
  voxel_finalize(dspace, dset, memspace);
40✔
1147
  file_close(file_id);
40✔
1148
}
40✔
1149

1150
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
40✔
1151
  hid_t* memspace)
1152
{
1153
  // Create dataspace/dataset for voxel data
1154
  *dspace = H5Screate_simple(3, dims, nullptr);
40✔
1155
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
40✔
1156
    H5P_DEFAULT, H5P_DEFAULT);
1157

1158
  // Create dataspace for a slice of the voxel
1159
  hsize_t dims_slice[2] {dims[1], dims[2]};
40✔
1160
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
40✔
1161

1162
  // Select hyperslab in dataspace
1163
  hsize_t start[3] {0, 0, 0};
40✔
1164
  hsize_t count[3] {1, dims[1], dims[2]};
40✔
1165
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
40✔
1166
}
40✔
1167

1168
void voxel_write_slice(
3,440✔
1169
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1170
{
1171
  hssize_t offset[3] {x, 0, 0};
3,440✔
1172
  H5Soffset_simple(dspace, offset);
3,440✔
1173
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
3,440✔
1174
}
3,440✔
1175

1176
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
40✔
1177
{
1178
  H5Dclose(dset);
40✔
1179
  H5Sclose(dspace);
40✔
1180
  H5Sclose(memspace);
40✔
1181
}
40✔
1182

1183
RGBColor random_color(void)
2,463✔
1184
{
1185
  return {int(prn(&model::plotter_seed) * 255),
2,463✔
1186
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
2,463✔
1187
}
1188

1189
RayTracePlot::RayTracePlot(pugi::xml_node node) : PlottableInterface(node)
64✔
1190
{
1191
  set_look_at(node);
64✔
1192
  set_camera_position(node);
64✔
1193
  set_field_of_view(node);
64✔
1194
  set_pixels(node);
64✔
1195
  set_orthographic_width(node);
64✔
1196
  set_output_path(node);
64✔
1197

1198
  if (check_for_node(node, "orthographic_width") &&
72!
1199
      check_for_node(node, "field_of_view"))
8✔
UNCOV
1200
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1201
                "parameters.");
1202
}
64✔
1203

1204
void RayTracePlot::update_view()
80✔
1205
{
1206
  // Get centerline vector for camera-to-model. We create vectors around this
1207
  // that form a pixel array, and then trace rays along that.
1208
  auto up = up_ / up_.norm();
80✔
1209
  Direction looking_direction = look_at_ - camera_position_;
80✔
1210
  looking_direction /= looking_direction.norm();
80✔
1211
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
80!
UNCOV
1212
    fatal_error("Up vector cannot align with vector between camera position "
×
1213
                "and look_at!");
1214
  Direction cam_yaxis = looking_direction.cross(up);
80✔
1215
  cam_yaxis /= cam_yaxis.norm();
80✔
1216
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
80✔
1217
  cam_zaxis /= cam_zaxis.norm();
80✔
1218

1219
  // Cache the camera-to-model matrix
1220
  camera_to_model_ = {looking_direction.x, cam_yaxis.x, cam_zaxis.x,
80✔
1221
    looking_direction.y, cam_yaxis.y, cam_zaxis.y, looking_direction.z,
80✔
1222
    cam_yaxis.z, cam_zaxis.z};
80✔
1223
}
80✔
1224

1225
WireframeRayTracePlot::WireframeRayTracePlot(pugi::xml_node node)
40✔
1226
  : RayTracePlot(node)
40✔
1227
{
1228
  set_opacities(node);
40✔
1229
  set_wireframe_thickness(node);
40✔
1230
  set_wireframe_ids(node);
40✔
1231
  set_wireframe_color(node);
40✔
1232
  update_view();
40✔
1233
}
40✔
1234

1235
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
40✔
1236
{
1237
  // Copy plot wireframe color
1238
  if (check_for_node(plot_node, "wireframe_color")) {
40!
UNCOV
1239
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
UNCOV
1240
    if (w_rgb.size() == 3) {
×
UNCOV
1241
      wireframe_color_ = w_rgb;
×
1242
    } else {
UNCOV
1243
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1244
    }
UNCOV
1245
  }
×
1246
}
40✔
1247

1248
void RayTracePlot::set_output_path(pugi::xml_node node)
64✔
1249
{
1250
  // Set output file path
1251
  std::string filename;
64✔
1252

1253
  if (check_for_node(node, "filename")) {
64✔
1254
    filename = get_node_value(node, "filename");
56✔
1255
  } else {
1256
    filename = fmt::format("plot_{}", id());
8✔
1257
  }
1258

1259
#ifdef USE_LIBPNG
1260
  if (!file_extension_present(filename, "png"))
64✔
1261
    filename.append(".png");
24✔
1262
#else
1263
  if (!file_extension_present(filename, "ppm"))
1264
    filename.append(".ppm");
1265
#endif
1266
  path_plot_ = filename;
128✔
1267
}
64✔
1268

1269
bool WireframeRayTracePlot::trackstack_equivalent(
2,211,752✔
1270
  const std::vector<TrackSegment>& track1,
1271
  const std::vector<TrackSegment>& track2) const
1272
{
1273
  if (wireframe_ids_.empty()) {
2,211,752✔
1274
    // Draw wireframe for all surfaces/cells/materials
1275
    if (track1.size() != track2.size())
1,850,960✔
1276
      return false;
1277
    for (int i = 0; i < track1.size(); ++i) {
4,878,512✔
1278
      if (track1[i].id != track2[i].id ||
3,081,288✔
1279
          track1[i].surface_index != track2[i].surface_index) {
3,081,192✔
1280
        return false;
1281
      }
1282
    }
1283
    return true;
1284
  } else {
1285
    // This runs in O(nm) where n is the intersection stack size
1286
    // and m is the number of IDs we are wireframing. A simpler
1287
    // algorithm can likely be found.
1288
    for (const int id : wireframe_ids_) {
717,232✔
1289
      int t1_i = 0;
360,792✔
1290
      int t2_i = 0;
360,792✔
1291

1292
      // Advance to first instance of the ID
1293
      while (t1_i < track1.size() && t2_i < track2.size()) {
409,040✔
1294
        while (t1_i < track1.size() && track1[t1_i].id != id)
285,696✔
1295
          t1_i++;
166,584✔
1296
        while (t2_i < track2.size() && track2[t2_i].id != id)
286,304✔
1297
          t2_i++;
167,192✔
1298

1299
        // This one is really important!
1300
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
119,112✔
1301
            (t1_i != track1.size() && t2_i == track2.size()))
117,888✔
1302
          return false;
2,704✔
1303
        if (t1_i == track1.size() && t2_i == track2.size())
116,408!
1304
          break;
1305
        // Check if surface different
1306
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
49,896✔
1307
          return false;
1308

1309
        // Pretty sure this should not be used:
1310
        // if (t2_i != track2.size() - 1 &&
1311
        //     t1_i != track1.size() - 1 &&
1312
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1313
        if (t2_i != 0 && t1_i != 0 &&
48,816✔
1314
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
39,232✔
1315
          return false;
1316

1317
        // Check if neighboring cells are different
1318
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1319
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1320
        // ].id !=
1321
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1322
        //    false;
1323
        t1_i++, t2_i++;
48,248✔
1324
      }
1325
    }
1326
    return true;
1327
  }
1328
}
1329

1330
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
2,560,768✔
1331
  int horiz, int vert) const
1332
{
1333
  // Compute field of view in radians
1334
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
2,560,768✔
1335
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
2,560,768✔
1336
  double p0 = static_cast<double>(pixels()[0]);
2,560,768✔
1337
  double p1 = static_cast<double>(pixels()[1]);
2,560,768✔
1338
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
2,560,768✔
1339

1340
  // focal_plane_dist can be changed to alter the perspective distortion
1341
  // effect. This is in units of cm. This seems to look good most of the
1342
  // time. TODO let this variable be set through XML.
1343
  constexpr double focal_plane_dist = 10.0;
2,560,768✔
1344
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
2,560,768✔
1345
  const double dy = p1 / p0 * dx;
2,560,768✔
1346

1347
  std::pair<Position, Direction> result;
2,560,768✔
1348

1349
  // Generate the starting position/direction of the ray
1350
  if (orthographic_width_ == C_NONE) { // perspective projection
2,560,768✔
1351
    Direction camera_local_vec;
2,240,768✔
1352
    camera_local_vec.x = focal_plane_dist;
2,240,768✔
1353
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
2,240,768✔
1354
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
2,240,768✔
1355
    camera_local_vec /= camera_local_vec.norm();
2,240,768✔
1356

1357
    result.first = camera_position_;
2,240,768✔
1358
    result.second = camera_local_vec.rotate(camera_to_model_);
2,240,768✔
1359
  } else { // orthographic projection
1360

1361
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
320,000✔
1362
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
320,000✔
1363

1364
    result.first = camera_position_ +
320,000✔
1365
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
320,000✔
1366
                   camera_z_axis() * y_pix_coord * orthographic_width_;
320,000✔
1367
    result.second = camera_x_axis();
320,000✔
1368
  }
1369

1370
  return result;
2,560,768✔
1371
}
1372

1373
ImageData WireframeRayTracePlot::create_image() const
40✔
1374
{
1375
  size_t width = pixels()[0];
40✔
1376
  size_t height = pixels()[1];
40✔
1377
  ImageData data({width, height}, not_found_);
40✔
1378

1379
  // This array marks where the initial wireframe was drawn. We convolve it with
1380
  // a filter that gets adjusted with the wireframe thickness in order to
1381
  // thicken the lines.
1382
  tensor::Tensor<int> wireframe_initial(
40✔
1383
    {static_cast<size_t>(width), static_cast<size_t>(height)}, 0);
40✔
1384

1385
  /* Holds all of the track segments for the current rendered line of pixels.
1386
   * old_segments holds a copy of this_line_segments from the previous line.
1387
   * By holding both we can check if the cell/material intersection stack
1388
   * differs from the left or upper neighbor. This allows a robustly drawn
1389
   * wireframe. If only checking the left pixel (which requires substantially
1390
   * less memory), the wireframe tends to be spotty and be disconnected for
1391
   * surface edges oriented horizontally in the rendering.
1392
   *
1393
   * Note that a vector of vectors is required rather than a 2-tensor,
1394
   * since the stack size varies within each column.
1395
   */
1396
  const int n_threads = num_threads();
40✔
1397
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
40✔
1398
    n_threads);
40✔
1399
  for (int t = 0; t < n_threads; ++t) {
105✔
1400
    this_line_segments[t].resize(pixels()[0]);
65✔
1401
  }
1402

1403
  // The last thread writes to this, and the first thread reads from it.
1404
  std::vector<std::vector<TrackSegment>> old_segments(pixels()[0]);
40✔
1405

1406
#pragma omp parallel
25✔
1407
  {
15✔
1408
    const int n_threads = num_threads();
15✔
1409
    const int tid = thread_num();
15✔
1410

1411
    int vert = tid;
15✔
1412
    for (int iter = 0; iter <= pixels()[1] / n_threads; iter++) {
3,030✔
1413

1414
      // Save bottom line of current work chunk to compare against later. This
1415
      // used to be inside the below if block, but it causes a spurious line to
1416
      // be drawn at the bottom of the image. Not sure why, but moving it here
1417
      // fixes things.
1418
      if (tid == n_threads - 1)
3,015✔
1419
        old_segments = this_line_segments[n_threads - 1];
3,015✔
1420

1421
      if (vert < pixels()[1]) {
3,015✔
1422

1423
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
603,000✔
1424

1425
          // RayTracePlot implements camera ray generation
1426
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
600,000✔
1427

1428
          this_line_segments[tid][horiz].clear();
600,000✔
1429
          ProjectionRay ray(
600,000✔
1430
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
600,000✔
1431

1432
          ray.trace();
600,000✔
1433

1434
          // Now color the pixel based on what we have intersected...
1435
          // Loops backwards over intersections.
1436
          Position current_color(
600,000✔
1437
            not_found_.red, not_found_.green, not_found_.blue);
600,000✔
1438
          const auto& segments = this_line_segments[tid][horiz];
600,000✔
1439

1440
          // There must be at least two cell intersections to color, front and
1441
          // back of the cell. Maybe an infinitely thick cell could be present
1442
          // with no back, but why would you want to color that? It's easier to
1443
          // just skip that edge case and not even color it.
1444
          if (segments.size() <= 1)
600,000✔
1445
            continue;
369,993✔
1446

1447
          for (int i = segments.size() - 2; i >= 0; --i) {
643,401✔
1448
            int colormap_idx = segments[i].id;
413,394✔
1449
            RGBColor seg_color = colors_[colormap_idx];
413,394✔
1450
            Position seg_color_vec(
413,394✔
1451
              seg_color.red, seg_color.green, seg_color.blue);
413,394✔
1452
            double mixing =
413,394✔
1453
              std::exp(-xs_[colormap_idx] *
826,788✔
1454
                       (segments[i + 1].length - segments[i].length));
413,394✔
1455
            current_color =
413,394✔
1456
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
413,394✔
1457
          }
1458

1459
          // save result converting from double-precision color coordinates to
1460
          // byte-sized
1461
          RGBColor result;
230,007✔
1462
          result.red = static_cast<uint8_t>(current_color.x);
230,007✔
1463
          result.green = static_cast<uint8_t>(current_color.y);
230,007✔
1464
          result.blue = static_cast<uint8_t>(current_color.z);
230,007✔
1465
          data(horiz, vert) = result;
230,007✔
1466

1467
          // Check to draw wireframe in horizontal direction. No inter-thread
1468
          // comm.
1469
          if (horiz > 0) {
230,007✔
1470
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
229,407✔
1471
                  this_line_segments[tid][horiz - 1])) {
229,407✔
1472
              wireframe_initial(horiz, vert) = 1;
9,426✔
1473
            }
1474
          }
1475
        }
600,000✔
1476
      } // end "if" vert in correct range
1477

1478
      // We require a barrier before comparing vertical neighbors' intersection
1479
      // stacks. i.e. all threads must be done with their line.
1480
#pragma omp barrier
1481

1482
      // Now that the horizontal line has finished rendering, we can fill in
1483
      // wireframe entries that require comparison among all the threads. Hence
1484
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1485
      if (vert < pixels()[1]) {
3,015✔
1486
        // Loop over horizontal pixels, checking intersection stack of upper
1487
        // neighbor
1488

1489
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
1490
        if (tid == 0)
1491
          top_cmp = &old_segments;
1492
        else
1493
          top_cmp = &this_line_segments[tid - 1];
1494

1495
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
603,000✔
1496
          if (!trackstack_equivalent(
600,000✔
1497
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
600,000✔
1498
            wireframe_initial(horiz, vert) = 1;
12,357✔
1499
          }
1500
        }
1501
      }
1502

1503
      // We need another barrier to ensure threads don't proceed to modify their
1504
      // intersection stacks on that horizontal line while others are
1505
      // potentially still working on the above.
1506
#pragma omp barrier
1507
      vert += n_threads;
3,015✔
1508
    }
1509
  } // end omp parallel
1510

1511
  // Now thicken the wireframe lines and apply them to our image
1512
  for (int vert = 0; vert < pixels()[1]; ++vert) {
8,040✔
1513
    for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
1,608,000✔
1514
      if (wireframe_initial(horiz, vert)) {
1,600,000✔
1515
        if (wireframe_thickness_ == 1)
51,624✔
1516
          data(horiz, vert) = wireframe_color_;
21,960✔
1517
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
142,344✔
1518
             ++i)
1519
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
397,728✔
1520
               ++j)
1521
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
307,008!
1522

1523
              // Check if wireframe pixel is out of bounds
1524
              int w_i = std::max(std::min(horiz + i, pixels()[0] - 1), 0);
307,008!
1525
              int w_j = std::max(std::min(vert + j, pixels()[1] - 1), 0);
307,104✔
1526
              data(w_i, w_j) = wireframe_color_;
307,008✔
1527
            }
1528
      }
1529
    }
1530
  }
1531

1532
  return data;
80✔
1533
}
80✔
1534

1535
void WireframeRayTracePlot::create_output() const
40✔
1536
{
1537
  ImageData data = create_image();
40✔
1538
  write_image(data);
40✔
1539
}
40✔
1540

1541
void RayTracePlot::print_info() const
64✔
1542
{
1543
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
128✔
1544
    camera_position_.y, camera_position_.z);
64✔
1545
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
64✔
1546
  fmt::print(
128✔
1547
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
64✔
1548
  fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
64✔
1549
}
64✔
1550

1551
void WireframeRayTracePlot::print_info() const
40✔
1552
{
1553
  fmt::print("Plot Type: Wireframe ray-traced\n");
40✔
1554
  RayTracePlot::print_info();
40✔
1555
}
40✔
1556

1557
void WireframeRayTracePlot::set_opacities(pugi::xml_node node)
40✔
1558
{
1559
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
40✔
1560

1561
  for (auto cn : node.children("color")) {
88✔
1562
    // Make sure 3 values are specified for RGB
1563
    double user_xs = std::stod(get_node_value(cn, "xs"));
96✔
1564
    int col_id = std::stoi(get_node_value(cn, "id"));
96✔
1565

1566
    // Add RGB
1567
    if (PlotColorBy::cells == color_by_) {
48!
1568
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
48!
1569
        col_id = model::cell_map[col_id];
48✔
1570
        xs_[col_id] = user_xs;
48✔
1571
      } else {
UNCOV
1572
        warning(fmt::format(
×
UNCOV
1573
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1574
      }
UNCOV
1575
    } else if (PlotColorBy::mats == color_by_) {
×
UNCOV
1576
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
UNCOV
1577
        col_id = model::material_map[col_id];
×
UNCOV
1578
        xs_[col_id] = user_xs;
×
1579
      } else {
UNCOV
1580
        warning(fmt::format(
×
UNCOV
1581
          "Could not find material {} specified in plot {}", col_id, id()));
×
1582
      }
1583
    }
1584
  }
1585
}
40✔
1586

1587
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
64✔
1588
{
1589
  if (check_for_node(node, "orthographic_width")) {
64✔
1590
    double orthographic_width =
8✔
1591
      std::stod(get_node_value(node, "orthographic_width", true));
8✔
1592
    if (orthographic_width < 0.0)
8!
UNCOV
1593
      fatal_error("Requires positive orthographic_width");
×
1594
    orthographic_width_ = orthographic_width;
8✔
1595
  }
1596
}
64✔
1597

1598
void WireframeRayTracePlot::set_wireframe_thickness(pugi::xml_node node)
40✔
1599
{
1600
  if (check_for_node(node, "wireframe_thickness")) {
40✔
1601
    int wireframe_thickness =
16✔
1602
      std::stoi(get_node_value(node, "wireframe_thickness", true));
16✔
1603
    if (wireframe_thickness < 0)
16!
UNCOV
1604
      fatal_error("Requires non-negative wireframe thickness");
×
1605
    wireframe_thickness_ = wireframe_thickness;
16✔
1606
  }
1607
}
40✔
1608

1609
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
40✔
1610
{
1611
  if (check_for_node(node, "wireframe_ids")) {
40✔
1612
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
8✔
1613
    // It is read in as actual ID values, but we have to convert to indices in
1614
    // mat/cell array
1615
    for (auto& x : wireframe_ids_)
16✔
1616
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
16!
UNCOV
1617
                                         : model::cell_map[x];
×
1618
  }
1619
  // We make sure the list is sorted in order to later use
1620
  // std::binary_search.
1621
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
40✔
1622
}
40✔
1623

1624
void RayTracePlot::set_pixels(pugi::xml_node node)
64✔
1625
{
1626
  vector<int> pxls = get_node_array<int>(node, "pixels");
64✔
1627
  if (pxls.size() != 2)
64!
UNCOV
1628
    fatal_error(
×
UNCOV
1629
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1630
  pixels()[0] = pxls[0];
64✔
1631
  pixels()[1] = pxls[1];
64✔
1632
}
64✔
1633

1634
void RayTracePlot::set_camera_position(pugi::xml_node node)
64✔
1635
{
1636
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
64✔
1637
  if (camera_pos.size() != 3) {
64!
UNCOV
1638
    fatal_error(fmt::format(
×
1639
      "camera_position element must have three floating point values"));
1640
  }
1641
  camera_position_.x = camera_pos[0];
64✔
1642
  camera_position_.y = camera_pos[1];
64✔
1643
  camera_position_.z = camera_pos[2];
64✔
1644
}
64✔
1645

1646
void RayTracePlot::set_look_at(pugi::xml_node node)
64✔
1647
{
1648
  vector<double> look_at = get_node_array<double>(node, "look_at");
64✔
1649
  if (look_at.size() != 3) {
64!
UNCOV
1650
    fatal_error("look_at element must have three floating point values");
×
1651
  }
1652
  look_at_.x = look_at[0];
64✔
1653
  look_at_.y = look_at[1];
64✔
1654
  look_at_.z = look_at[2];
64✔
1655
}
64✔
1656

1657
void RayTracePlot::set_field_of_view(pugi::xml_node node)
64✔
1658
{
1659
  // Defaults to 70 degree horizontal field of view (see .h file)
1660
  if (check_for_node(node, "horizontal_field_of_view")) {
64!
UNCOV
1661
    double fov =
×
UNCOV
1662
      std::stod(get_node_value(node, "horizontal_field_of_view", true));
×
UNCOV
1663
    if (fov < 180.0 && fov > 0.0) {
×
UNCOV
1664
      horizontal_field_of_view_ = fov;
×
1665
    } else {
UNCOV
1666
      fatal_error(fmt::format("Horizontal field of view for plot {} "
×
1667
                              "out-of-range. Must be in (0, 180) degrees.",
UNCOV
1668
        id()));
×
1669
    }
1670
  }
1671
}
64✔
1672

1673
SolidRayTracePlot::SolidRayTracePlot(pugi::xml_node node) : RayTracePlot(node)
24✔
1674
{
1675
  set_opaque_ids(node);
24✔
1676
  set_diffuse_fraction(node);
24✔
1677
  set_light_position(node);
24✔
1678
  update_view();
24✔
1679
}
24✔
1680

1681
void SolidRayTracePlot::print_info() const
24✔
1682
{
1683
  fmt::print("Plot Type: Solid ray-traced\n");
24✔
1684
  RayTracePlot::print_info();
24✔
1685
}
24✔
1686

1687
ImageData SolidRayTracePlot::create_image() const
40✔
1688
{
1689
  size_t width = pixels()[0];
40✔
1690
  size_t height = pixels()[1];
40✔
1691
  ImageData data({width, height}, not_found_);
40✔
1692

1693
#pragma omp parallel for schedule(dynamic) collapse(2)
25✔
1694
  for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
1,863✔
1695
    for (int vert = 0; vert < pixels()[1]; ++vert) {
362,136✔
1696
      // RayTracePlot implements camera ray generation
1697
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
360,288✔
1698
      PhongRay ray(ru.first, ru.second, *this);
360,288✔
1699
      ray.trace();
360,288✔
1700
      data(horiz, vert) = ray.result_color();
360,288✔
1701
    }
360,288✔
1702
  }
1703

1704
  return data;
40✔
1705
}
1706

1707
void SolidRayTracePlot::create_output() const
24✔
1708
{
1709
  ImageData data = create_image();
24✔
1710
  write_image(data);
24✔
1711
}
24✔
1712

1713
void SolidRayTracePlot::set_opaque_ids(pugi::xml_node node)
24✔
1714
{
1715
  if (check_for_node(node, "opaque_ids")) {
24!
1716
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
24✔
1717

1718
    // It is read in as actual ID values, but we have to convert to indices in
1719
    // mat/cell array
1720
    for (auto& x : opaque_ids_tmp)
72✔
1721
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
96!
UNCOV
1722
                                         : model::cell_map[x];
×
1723

1724
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
24✔
1725
  }
24✔
1726
}
24✔
1727

1728
void SolidRayTracePlot::set_light_position(pugi::xml_node node)
24✔
1729
{
1730
  if (check_for_node(node, "light_position")) {
24✔
1731
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
8✔
1732

1733
    if (light_pos_tmp.size() != 3)
8!
UNCOV
1734
      fatal_error("Light position must be given as 3D coordinates");
×
1735

1736
    light_location_.x = light_pos_tmp[0];
8✔
1737
    light_location_.y = light_pos_tmp[1];
8✔
1738
    light_location_.z = light_pos_tmp[2];
8✔
1739
  } else {
8✔
1740
    light_location_ = camera_position();
16✔
1741
  }
1742
}
24✔
1743

1744
void SolidRayTracePlot::set_diffuse_fraction(pugi::xml_node node)
24✔
1745
{
1746
  if (check_for_node(node, "diffuse_fraction")) {
24✔
1747
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
8✔
1748
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
8!
UNCOV
1749
      fatal_error("Must have 0 <= diffuse fraction <= 1");
×
1750
    }
1751
  }
1752
}
24✔
1753

1754
void ProjectionRay::on_intersection()
1,715,744✔
1755
{
1756
  // This records a tuple with the following info
1757
  //
1758
  // 1) ID (material or cell depending on color_by_)
1759
  // 2) Distance traveled by the ray through that ID
1760
  // 3) Index of the intersected surface (starting from 1)
1761

1762
  line_segments_.emplace_back(
1,715,744✔
1763
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
1,715,744✔
1764
      ? material()
397,032✔
1765
      : lowest_coord().cell(),
1,318,712✔
1766
    traversal_distance_, boundary().surface_index());
1,715,744✔
1767
}
1,715,744✔
1768

1769
void PhongRay::on_intersection()
658,888✔
1770
{
1771
  // Check if we hit an opaque material or cell
1772
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
658,888✔
1773
                 ? material()
658,888!
1774
                 : lowest_coord().cell();
×
1775

1776
  // If we are reflected and have advanced beyond the camera,
1777
  // the ray is done. This is checked here because we should
1778
  // kill the ray even if the material is not opaque.
1779
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
658,888!
UNCOV
1780
    stop();
×
1781
    return;
119,520✔
1782
  }
1783

1784
  // Anything that's not opaque has zero impact on the plot.
1785
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
658,888✔
1786
    return;
1787

1788
  if (!reflected_) {
539,368✔
1789
    // reflect the particle and set the color to be colored by
1790
    // the normal or the diffuse lighting contribution
1791
    reflected_ = true;
513,504✔
1792
    result_color_ = plot_.colors_[hit_id];
513,504✔
1793
    // The ray has been advanced slightly past the boundary. Use an
1794
    // approximation to the actual hit point for stable normal/lighting.
1795
    Position r_hit = r() - TINY_BIT * u();
513,504✔
1796
    Direction to_light = plot_.light_location_ - r_hit;
513,504✔
1797
    to_light /= to_light.norm();
513,504✔
1798

1799
    // TODO
1800
    // Not sure what can cause a surface token to be invalid here, although it
1801
    // sometimes happens for a few pixels. It's very very rare, so proceed by
1802
    // coloring the pixel with the overlap color. It seems to happen only for a
1803
    // few pixels on the outer boundary of a hex lattice.
1804
    //
1805
    // We cannot detect it in the outer loop, and it only matters here, so
1806
    // that's why the error handling is a little different than for a lost
1807
    // ray.
1808
    if (surface() == 0) {
513,504!
UNCOV
1809
      result_color_ = plot_.overlap_color_;
×
UNCOV
1810
      stop();
×
UNCOV
1811
      return;
×
1812
    }
1813

1814
    // Get surface pointer
1815
    const auto& surf = model::surfaces.at(surface_index());
513,504✔
1816

1817
    // The crossed surface may be on a higher coordinate level than the
1818
    // innermost local coordinates, so we check the surface's coordinate level
1819
    // to find the appropriate coordinate level to use for the normal
1820
    // calculation
1821
    int surf_level = boundary().coord_level() - 1;
513,504!
1822
    // ensure surface level is within bounds of current coordinate stack
1823
    surf_level = std::max(0, std::min(surf_level, n_coord() - 1));
513,504!
1824

1825
    Position r_hit_level =
513,504✔
1826
      coord(surf_level).r() - TINY_BIT * coord(surf_level).u();
513,504✔
1827
    Direction normal = surf->normal(r_hit_level);
513,504✔
1828
    normal /= normal.norm();
513,504✔
1829

1830
    // Need to apply rotations to find the normal vector in
1831
    // the base level universe's coordinate system.
1832
    for (int lev = surf_level - 1; lev >= 0; --lev) {
513,504!
UNCOV
1833
      if (coord(lev + 1).rotated()) {
×
UNCOV
1834
        const Cell& c {*model::cells[coord(lev).cell()]};
×
UNCOV
1835
        normal = normal.inverse_rotate(c.rotation_);
×
1836
      }
1837
    }
1838

1839
    // use the normal opposed to the ray direction
1840
    if (normal.dot(u()) > 0.0) {
513,504✔
1841
      normal *= -1.0;
46,392✔
1842
    }
1843

1844
    // Facing away from the light means no lighting
1845
    double dotprod = normal.dot(to_light);
513,504✔
1846
    dotprod = std::max(0.0, dotprod);
513,504✔
1847

1848
    double modulation =
513,504✔
1849
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
513,504✔
1850
    result_color_ *= modulation;
513,504✔
1851

1852
    // Now point the particle to the camera. We now begin
1853
    // checking to see if it's occluded by another surface
1854
    u() = to_light;
513,504✔
1855

1856
    orig_hit_id_ = hit_id;
513,504✔
1857

1858
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1859
    // in how they interpret particles that are sitting on a surface.
1860
    // I don't know exactly why, but this makes everything work beautifully.
1861
    if (surf->geom_type() == GeometryType::DAG) {
513,504!
1862
      surface() = 0;
×
1863
    } else {
1864
      surface() = -surface(); // go to other side
513,504✔
1865
    }
1866

1867
    // Must fully restart coordinate search. Why? Not sure.
1868
    clear();
513,504✔
1869

1870
    // Note this could likely be faster if we cached the previous
1871
    // cell we were in before the reflection. This is the easiest
1872
    // way to fully initialize all the sub-universe coordinates and
1873
    // directions though.
1874
    bool found = exhaustive_find_cell(*this);
513,504✔
1875
    if (!found) {
513,504!
UNCOV
1876
      fatal_error("Lost particle after reflection.");
×
1877
    }
1878

1879
    // Must recalculate distance to boundary due to the
1880
    // direction change
1881
    compute_distance();
513,504✔
1882

1883
  } else {
1884
    // If it's not facing the light, we color with the diffuse contribution, so
1885
    // next we check if we're going to occlude the last reflected surface. if
1886
    // so, color by the diffuse contribution instead
1887

1888
    if (orig_hit_id_ == -1)
25,864!
UNCOV
1889
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1890

1891
    result_color_ = plot_.colors_[orig_hit_id_];
25,864✔
1892
    result_color_ *= plot_.diffuse_fraction_;
25,864✔
1893
    stop();
539,368✔
1894
  }
1895
}
1896

UNCOV
1897
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
×
1898
{
1899
  static bool warned {false};
×
UNCOV
1900
  if (!warned) {
×
UNCOV
1901
    warning("openmc_id_map is deprecated and will be removed in a future "
×
1902
            "release. Use openmc_slice_data.");
UNCOV
1903
    warned = true;
×
1904
  }
1905

1906
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
UNCOV
1907
  if (!plt) {
×
UNCOV
1908
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
UNCOV
1909
    return OPENMC_E_INVALID_ARGUMENT;
×
1910
  }
1911

1912
  if (plt->show_overlaps_ && model::overlap_check_count.size() == 0) {
×
1913
    model::overlap_check_count.resize(model::cells.size());
×
1914
  }
1915

1916
  auto ids = plt->get_map<IdData>();
×
1917

1918
  // write id data to array
1919
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
×
1920

UNCOV
1921
  return 0;
×
1922
}
×
1923

UNCOV
1924
extern "C" int openmc_property_map(const void* plot, double* data_out)
×
1925
{
UNCOV
1926
  static bool warned {false};
×
UNCOV
1927
  if (!warned) {
×
UNCOV
1928
    warning("openmc_property_map is deprecated and will be removed in a future "
×
1929
            "release. Use openmc_slice_data.");
1930
    warned = true;
×
1931
  }
1932

UNCOV
1933
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
1934
  if (!plt) {
×
1935
    set_errmsg("Invalid slice pointer passed to openmc_property_map");
×
UNCOV
1936
    return OPENMC_E_INVALID_ARGUMENT;
×
1937
  }
1938

UNCOV
1939
  if (plt->show_overlaps_ && model::overlap_check_count.size() == 0) {
×
UNCOV
1940
    model::overlap_check_count.resize(model::cells.size());
×
1941
  }
1942

1943
  auto props = plt->get_map<PropertyData>();
×
1944

1945
  // write id data to array
UNCOV
1946
  std::copy(props.data_.begin(), props.data_.end(), data_out);
×
1947

UNCOV
1948
  return 0;
×
UNCOV
1949
}
×
1950

1951
extern "C" int openmc_slice_data(const double origin[3], const double u_span[3],
291✔
1952
  const double v_span[3], const size_t pixels[2], bool color_overlaps,
1953
  int level, int32_t filter_index, int32_t* geom_data, double* property_data)
1954
{
1955
  // Validate span vectors
1956
  Direction u_span_pos {u_span[0], u_span[1], u_span[2]};
291✔
1957
  Direction v_span_pos {v_span[0], v_span[1], v_span[2]};
291✔
1958
  double u_norm = u_span_pos.norm();
291✔
1959
  double v_norm = v_span_pos.norm();
291✔
1960
  if (u_norm == 0.0 || v_norm == 0.0) {
291!
UNCOV
1961
    set_errmsg("Slice span vectors must be non-zero.");
×
UNCOV
1962
    return OPENMC_E_INVALID_ARGUMENT;
×
1963
  }
1964

1965
  constexpr double ORTHO_REL_TOL = 1e-10;
291✔
1966
  double dot = u_span_pos.dot(v_span_pos);
291!
1967
  if (std::abs(dot) > ORTHO_REL_TOL * u_norm * v_norm) {
291!
UNCOV
1968
    set_errmsg("Slice span vectors must be orthogonal.");
×
UNCOV
1969
    return OPENMC_E_INVALID_ARGUMENT;
×
1970
  }
1971

1972
  // Validate filter index if provided
1973
  if (filter_index >= 0) {
291✔
1974
    if (int err = verify_filter(filter_index))
16!
1975
      return err;
1976
  }
1977

1978
  // Initialize overlap check vector if needed
1979
  if (color_overlaps && model::overlap_check_count.size() == 0) {
291!
1980
    model::overlap_check_count.resize(model::cells.size());
32✔
1981
  }
1982

1983
  try {
291✔
1984
    // Create a temporary SlicePlotBase object to reuse get_map logic
1985
    SlicePlotBase plot_params;
291✔
1986
    plot_params.origin_ = Position {origin[0], origin[1], origin[2]};
291✔
1987
    plot_params.u_span_ = u_span_pos;
291✔
1988
    plot_params.v_span_ = v_span_pos;
291✔
1989
    plot_params.pixels_[0] = pixels[0];
291✔
1990
    plot_params.pixels_[1] = pixels[1];
291✔
1991
    plot_params.show_overlaps_ = color_overlaps;
291✔
1992
    plot_params.slice_level_ = level;
291✔
1993

1994
    // Use get_map<RasterData> to generate data
1995
    auto data = plot_params.get_map<RasterData>(filter_index);
291✔
1996

1997
    // Copy geometry data
1998
    std::copy(data.id_data_.begin(), data.id_data_.end(), geom_data);
291✔
1999

2000
    // Copy property data if requested
2001
    if (property_data != nullptr) {
291✔
2002
      std::copy(
48✔
2003
        data.property_data_.begin(), data.property_data_.end(), property_data);
2004
    }
2005
  } catch (const std::exception& e) {
291!
UNCOV
2006
    set_errmsg(e.what());
×
UNCOV
2007
    return OPENMC_E_UNASSIGNED;
×
UNCOV
2008
  }
×
2009

2010
  return 0;
291✔
2011
}
2012

2013
extern "C" int openmc_get_plot_index(int32_t id, int32_t* index)
16✔
2014
{
2015
  auto it = model::plot_map.find(id);
16!
2016
  if (it == model::plot_map.end()) {
16!
UNCOV
2017
    set_errmsg("No plot exists with ID=" + std::to_string(id) + ".");
×
UNCOV
2018
    return OPENMC_E_INVALID_ID;
×
2019
  }
2020

2021
  *index = it->second;
16✔
2022
  return 0;
16✔
2023
}
2024

2025
extern "C" int openmc_plot_get_id(int32_t index, int32_t* id)
40✔
2026
{
2027
  if (index < 0 || index >= model::plots.size()) {
40!
UNCOV
2028
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
2029
    return OPENMC_E_OUT_OF_BOUNDS;
×
2030
  }
2031

2032
  *id = model::plots[index]->id();
40✔
2033
  return 0;
40✔
2034
}
2035

2036
extern "C" int openmc_plot_set_id(int32_t index, int32_t id)
×
2037
{
UNCOV
2038
  if (index < 0 || index >= model::plots.size()) {
×
UNCOV
2039
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
2040
    return OPENMC_E_OUT_OF_BOUNDS;
×
2041
  }
2042

UNCOV
2043
  if (id < 0 && id != C_NONE) {
×
UNCOV
2044
    set_errmsg("Invalid plot ID.");
×
UNCOV
2045
    return OPENMC_E_INVALID_ARGUMENT;
×
2046
  }
2047

UNCOV
2048
  auto* plot = model::plots[index].get();
×
UNCOV
2049
  int32_t old_id = plot->id();
×
UNCOV
2050
  if (id == old_id)
×
2051
    return 0;
2052

2053
  model::plot_map.erase(old_id);
×
2054
  try {
×
UNCOV
2055
    plot->set_id(id);
×
2056
  } catch (const std::runtime_error& e) {
×
2057
    model::plot_map[old_id] = index;
×
UNCOV
2058
    set_errmsg(e.what());
×
UNCOV
2059
    return OPENMC_E_INVALID_ID;
×
UNCOV
2060
  }
×
UNCOV
2061
  model::plot_map[plot->id()] = index;
×
UNCOV
2062
  return 0;
×
2063
}
2064

2065
extern "C" size_t openmc_plots_size()
16✔
2066
{
2067
  return model::plots.size();
16✔
2068
}
2069

2070
int map_phong_domain_id(
40✔
2071
  const SolidRayTracePlot* plot, int32_t id, int32_t* index_out)
2072
{
2073
  if (!plot || !index_out) {
40!
2074
    set_errmsg("Invalid plot pointer passed to map_phong_domain_id");
×
UNCOV
2075
    return OPENMC_E_INVALID_ARGUMENT;
×
2076
  }
2077

2078
  if (plot->color_by_ == PlottableInterface::PlotColorBy::mats) {
40!
2079
    auto it = model::material_map.find(id);
40!
2080
    if (it == model::material_map.end()) {
40!
2081
      set_errmsg("Invalid material ID for SolidRayTracePlot");
×
2082
      return OPENMC_E_INVALID_ID;
×
2083
    }
2084
    *index_out = it->second;
40✔
2085
    return 0;
40✔
2086
  }
2087

UNCOV
2088
  if (plot->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2089
    auto it = model::cell_map.find(id);
×
2090
    if (it == model::cell_map.end()) {
×
2091
      set_errmsg("Invalid cell ID for SolidRayTracePlot");
×
UNCOV
2092
      return OPENMC_E_INVALID_ID;
×
2093
    }
UNCOV
2094
    *index_out = it->second;
×
UNCOV
2095
    return 0;
×
2096
  }
2097

UNCOV
2098
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2099
  return OPENMC_E_INVALID_TYPE;
×
2100
}
2101

2102
int get_solidraytrace_plot_by_index(int32_t index, SolidRayTracePlot** plot)
224✔
2103
{
2104
  if (!plot) {
224!
UNCOV
2105
    set_errmsg("Null output pointer passed to get_solidraytrace_plot_by_index");
×
UNCOV
2106
    return OPENMC_E_INVALID_ARGUMENT;
×
2107
  }
2108

2109
  if (index < 0 || index >= model::plots.size()) {
224!
UNCOV
2110
    set_errmsg("Index in plots array is out of bounds.");
×
UNCOV
2111
    return OPENMC_E_OUT_OF_BOUNDS;
×
2112
  }
2113

2114
  auto* plottable = model::plots[index].get();
224!
2115
  auto* solid_plot = dynamic_cast<SolidRayTracePlot*>(plottable);
224!
2116
  if (!solid_plot) {
224!
UNCOV
2117
    set_errmsg("Plot at index=" + std::to_string(index) +
×
2118
               " is not a solid raytrace plot.");
UNCOV
2119
    return OPENMC_E_INVALID_TYPE;
×
2120
  }
2121

2122
  *plot = solid_plot;
224✔
2123
  return 0;
224✔
2124
}
2125

2126
extern "C" int openmc_solidraytrace_plot_create(int32_t* index)
8✔
2127
{
2128
  if (!index) {
8!
UNCOV
2129
    set_errmsg(
×
2130
      "Null output pointer passed to openmc_solidraytrace_plot_create");
UNCOV
2131
    return OPENMC_E_INVALID_ARGUMENT;
×
2132
  }
2133

2134
  try {
8✔
2135
    auto new_plot = std::make_unique<SolidRayTracePlot>();
8✔
2136
    new_plot->set_id();
8✔
2137
    int32_t new_plot_id = new_plot->id();
8✔
2138
#ifdef USE_LIBPNG
2139
    new_plot->path_plot() = fmt::format("plot_{}.png", new_plot_id);
8✔
2140
#else
2141
    new_plot->path_plot() = fmt::format("plot_{}.ppm", new_plot_id);
2142
#endif
2143
    int32_t new_plot_index = model::plots.size();
8✔
2144
    model::plots.emplace_back(std::move(new_plot));
8✔
2145
    model::plot_map[new_plot_id] = new_plot_index;
8✔
2146
    *index = new_plot_index;
8✔
2147
  } catch (const std::exception& e) {
8!
UNCOV
2148
    set_errmsg(e.what());
×
2149
    return OPENMC_E_ALLOCATE;
×
UNCOV
2150
  }
×
2151

2152
  return 0;
8✔
2153
}
2154

2155
extern "C" int openmc_solidraytrace_plot_get_pixels(
24✔
2156
  int32_t index, int32_t* width, int32_t* height)
2157
{
2158
  if (!width || !height) {
24!
UNCOV
2159
    set_errmsg(
×
2160
      "Invalid arguments passed to openmc_solidraytrace_plot_get_pixels");
UNCOV
2161
    return OPENMC_E_INVALID_ARGUMENT;
×
2162
  }
2163

2164
  SolidRayTracePlot* plt = nullptr;
24✔
2165
  int err = get_solidraytrace_plot_by_index(index, &plt);
24✔
2166
  if (err)
24!
2167
    return err;
2168

2169
  *width = plt->pixels()[0];
24✔
2170
  *height = plt->pixels()[1];
24✔
2171
  return 0;
24✔
2172
}
2173

2174
extern "C" int openmc_solidraytrace_plot_set_pixels(
8✔
2175
  int32_t index, int32_t width, int32_t height)
2176
{
2177
  if (width <= 0 || height <= 0) {
8!
UNCOV
2178
    set_errmsg(
×
2179
      "Invalid arguments passed to openmc_solidraytrace_plot_set_pixels");
UNCOV
2180
    return OPENMC_E_INVALID_ARGUMENT;
×
2181
  }
2182

2183
  SolidRayTracePlot* plt = nullptr;
8✔
2184
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2185
  if (err)
8!
2186
    return err;
2187

2188
  plt->pixels()[0] = width;
8✔
2189
  plt->pixels()[1] = height;
8✔
2190
  return 0;
8✔
2191
}
2192

2193
extern "C" int openmc_solidraytrace_plot_get_color_by(
8✔
2194
  int32_t index, int32_t* color_by)
2195
{
2196
  if (!color_by) {
8!
UNCOV
2197
    set_errmsg(
×
2198
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color_by");
UNCOV
2199
    return OPENMC_E_INVALID_ARGUMENT;
×
2200
  }
2201

2202
  SolidRayTracePlot* plt = nullptr;
8✔
2203
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2204
  if (err)
8!
2205
    return err;
2206

2207
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
8!
2208
    *color_by = 0;
8✔
UNCOV
2209
  } else if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
UNCOV
2210
    *color_by = 1;
×
2211
  } else {
UNCOV
2212
    set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2213
    return OPENMC_E_INVALID_TYPE;
×
2214
  }
2215

2216
  return 0;
2217
}
2218

2219
extern "C" int openmc_solidraytrace_plot_set_color_by(
8✔
2220
  int32_t index, int32_t color_by)
2221
{
2222
  SolidRayTracePlot* plt = nullptr;
8✔
2223
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2224
  if (err)
8!
2225
    return err;
2226

2227
  if (color_by == 0) {
8!
2228
    plt->color_by_ = PlottableInterface::PlotColorBy::mats;
8✔
UNCOV
2229
  } else if (color_by == 1) {
×
UNCOV
2230
    plt->color_by_ = PlottableInterface::PlotColorBy::cells;
×
2231
  } else {
UNCOV
2232
    set_errmsg("Invalid color_by value for SolidRayTracePlot");
×
UNCOV
2233
    return OPENMC_E_INVALID_ARGUMENT;
×
2234
  }
2235

2236
  return 0;
2237
}
2238

2239
extern "C" int openmc_solidraytrace_plot_set_default_colors(int32_t index)
8✔
2240
{
2241
  SolidRayTracePlot* plt = nullptr;
8✔
2242
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2243
  if (err)
8!
2244
    return err;
2245

2246
  plt->set_default_colors();
8✔
2247
  return 0;
2248
}
2249

UNCOV
2250
extern "C" int openmc_solidraytrace_plot_set_all_opaque(int32_t index)
×
2251
{
UNCOV
2252
  SolidRayTracePlot* plt = nullptr;
×
UNCOV
2253
  int err = get_solidraytrace_plot_by_index(index, &plt);
×
UNCOV
2254
  if (err)
×
2255
    return err;
2256

UNCOV
2257
  plt->opaque_ids().clear();
×
UNCOV
2258
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
×
UNCOV
2259
    for (int32_t i = 0; i < model::materials.size(); ++i) {
×
UNCOV
2260
      plt->opaque_ids().insert(i);
×
2261
    }
UNCOV
2262
    return 0;
×
2263
  }
2264

UNCOV
2265
  if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
UNCOV
2266
    for (int32_t i = 0; i < model::cells.size(); ++i) {
×
UNCOV
2267
      plt->opaque_ids().insert(i);
×
2268
    }
UNCOV
2269
    return 0;
×
2270
  }
2271

UNCOV
2272
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
UNCOV
2273
  return OPENMC_E_INVALID_TYPE;
×
2274
}
2275

2276
extern "C" int openmc_solidraytrace_plot_set_opaque(
16✔
2277
  int32_t index, int32_t id, bool visible)
2278
{
2279
  SolidRayTracePlot* plt = nullptr;
16✔
2280
  int err = get_solidraytrace_plot_by_index(index, &plt);
16✔
2281
  if (err)
16!
2282
    return err;
2283

2284
  int32_t domain_index = -1;
16✔
2285
  err = map_phong_domain_id(plt, id, &domain_index);
16✔
2286
  if (err)
16!
2287
    return err;
2288

2289
  if (visible) {
16✔
2290
    plt->opaque_ids().insert(domain_index);
8✔
2291
  } else {
2292
    plt->opaque_ids().erase(domain_index);
8✔
2293
  }
2294

2295
  return 0;
2296
}
2297

2298
extern "C" int openmc_solidraytrace_plot_set_color(
16✔
2299
  int32_t index, int32_t id, uint8_t r, uint8_t g, uint8_t b)
2300
{
2301
  SolidRayTracePlot* plt = nullptr;
16✔
2302
  int err = get_solidraytrace_plot_by_index(index, &plt);
16✔
2303
  if (err)
16!
2304
    return err;
2305

2306
  int32_t domain_index = -1;
16✔
2307
  err = map_phong_domain_id(plt, id, &domain_index);
16✔
2308
  if (err)
16!
2309
    return err;
2310

2311
  if (domain_index < 0 ||
16!
2312
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
16!
UNCOV
2313
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
UNCOV
2314
    return OPENMC_E_OUT_OF_BOUNDS;
×
2315
  }
2316

2317
  plt->colors_[domain_index] = RGBColor(r, g, b);
16✔
2318
  return 0;
16✔
2319
}
2320

2321
extern "C" int openmc_solidraytrace_plot_get_camera_position(
8✔
2322
  int32_t index, double* x, double* y, double* z)
2323
{
2324
  if (!x || !y || !z) {
8!
UNCOV
2325
    set_errmsg("Invalid arguments passed to "
×
2326
               "openmc_solidraytrace_plot_get_camera_position");
UNCOV
2327
    return OPENMC_E_INVALID_ARGUMENT;
×
2328
  }
2329

2330
  SolidRayTracePlot* plt = nullptr;
8✔
2331
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2332
  if (err)
8!
2333
    return err;
2334

2335
  const auto& camera_position = plt->camera_position();
8✔
2336
  *x = camera_position.x;
8✔
2337
  *y = camera_position.y;
8✔
2338
  *z = camera_position.z;
8✔
2339
  return 0;
8✔
2340
}
2341

2342
extern "C" int openmc_solidraytrace_plot_set_camera_position(
8✔
2343
  int32_t index, double x, double y, double z)
2344
{
2345
  SolidRayTracePlot* plt = nullptr;
8✔
2346
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2347
  if (err)
8!
2348
    return err;
2349

2350
  plt->camera_position() = {x, y, z};
8✔
2351
  return 0;
8✔
2352
}
2353

2354
extern "C" int openmc_solidraytrace_plot_get_look_at(
8✔
2355
  int32_t index, double* x, double* y, double* z)
2356
{
2357
  if (!x || !y || !z) {
8!
2358
    set_errmsg(
×
2359
      "Invalid arguments passed to openmc_solidraytrace_plot_get_look_at");
2360
    return OPENMC_E_INVALID_ARGUMENT;
×
2361
  }
2362

2363
  SolidRayTracePlot* plt = nullptr;
8✔
2364
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2365
  if (err)
8!
2366
    return err;
2367

2368
  const auto& look_at = plt->look_at();
8✔
2369
  *x = look_at.x;
8✔
2370
  *y = look_at.y;
8✔
2371
  *z = look_at.z;
8✔
2372
  return 0;
8✔
2373
}
2374

2375
extern "C" int openmc_solidraytrace_plot_set_look_at(
8✔
2376
  int32_t index, double x, double y, double z)
2377
{
2378
  SolidRayTracePlot* plt = nullptr;
8✔
2379
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2380
  if (err)
8!
2381
    return err;
2382

2383
  plt->look_at() = {x, y, z};
8✔
2384
  return 0;
8✔
2385
}
2386

2387
extern "C" int openmc_solidraytrace_plot_get_up(
8✔
2388
  int32_t index, double* x, double* y, double* z)
2389
{
2390
  if (!x || !y || !z) {
8!
UNCOV
2391
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_up");
×
2392
    return OPENMC_E_INVALID_ARGUMENT;
×
2393
  }
2394

2395
  SolidRayTracePlot* plt = nullptr;
8✔
2396
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2397
  if (err)
8!
2398
    return err;
2399

2400
  const auto& up = plt->up();
8✔
2401
  *x = up.x;
8✔
2402
  *y = up.y;
8✔
2403
  *z = up.z;
8✔
2404
  return 0;
8✔
2405
}
2406

2407
extern "C" int openmc_solidraytrace_plot_set_up(
8✔
2408
  int32_t index, double x, double y, double z)
2409
{
2410
  SolidRayTracePlot* plt = nullptr;
8✔
2411
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2412
  if (err)
8!
2413
    return err;
2414

2415
  plt->up() = {x, y, z};
8✔
2416
  return 0;
8✔
2417
}
2418

2419
extern "C" int openmc_solidraytrace_plot_get_light_position(
8✔
2420
  int32_t index, double* x, double* y, double* z)
2421
{
2422
  if (!x || !y || !z) {
8!
UNCOV
2423
    set_errmsg("Invalid arguments passed to "
×
2424
               "openmc_solidraytrace_plot_get_light_position");
UNCOV
2425
    return OPENMC_E_INVALID_ARGUMENT;
×
2426
  }
2427

2428
  SolidRayTracePlot* plt = nullptr;
8✔
2429
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2430
  if (err)
8!
2431
    return err;
2432

2433
  const auto& light_position = plt->light_location();
8✔
2434
  *x = light_position.x;
8✔
2435
  *y = light_position.y;
8✔
2436
  *z = light_position.z;
8✔
2437
  return 0;
8✔
2438
}
2439

2440
extern "C" int openmc_solidraytrace_plot_set_light_position(
8✔
2441
  int32_t index, double x, double y, double z)
2442
{
2443
  SolidRayTracePlot* plt = nullptr;
8✔
2444
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2445
  if (err)
8!
2446
    return err;
2447

2448
  plt->light_location() = {x, y, z};
8✔
2449
  return 0;
8✔
2450
}
2451

2452
extern "C" int openmc_solidraytrace_plot_get_fov(int32_t index, double* fov)
8✔
2453
{
2454
  if (!fov) {
8!
UNCOV
2455
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_fov");
×
UNCOV
2456
    return OPENMC_E_INVALID_ARGUMENT;
×
2457
  }
2458

2459
  SolidRayTracePlot* plt = nullptr;
8✔
2460
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2461
  if (err)
8!
2462
    return err;
2463

2464
  *fov = plt->horizontal_field_of_view();
8✔
2465
  return 0;
8✔
2466
}
2467

2468
extern "C" int openmc_solidraytrace_plot_set_fov(int32_t index, double fov)
8✔
2469
{
2470
  SolidRayTracePlot* plt = nullptr;
8✔
2471
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2472
  if (err)
8!
2473
    return err;
2474

2475
  plt->horizontal_field_of_view() = fov;
8✔
2476
  return 0;
8✔
2477
}
2478

2479
extern "C" int openmc_solidraytrace_plot_update_view(int32_t index)
16✔
2480
{
2481
  SolidRayTracePlot* plt = nullptr;
16✔
2482
  int err = get_solidraytrace_plot_by_index(index, &plt);
16✔
2483
  if (err)
16!
2484
    return err;
2485

2486
  plt->update_view();
16✔
2487
  return 0;
2488
}
2489

2490
extern "C" int openmc_solidraytrace_plot_create_image(
16✔
2491
  int32_t index, uint8_t* data_out, int32_t width, int32_t height)
2492
{
2493
  if (!data_out || width <= 0 || height <= 0) {
16!
UNCOV
2494
    set_errmsg(
×
2495
      "Invalid arguments passed to openmc_solidraytrace_plot_create_image");
UNCOV
2496
    return OPENMC_E_INVALID_ARGUMENT;
×
2497
  }
2498

2499
  SolidRayTracePlot* plt = nullptr;
16✔
2500
  int err = get_solidraytrace_plot_by_index(index, &plt);
16✔
2501
  if (err)
16!
2502
    return err;
2503

2504
  if (plt->pixels()[0] != width || plt->pixels()[1] != height) {
16!
UNCOV
2505
    set_errmsg(
×
2506
      "Requested image size does not match SolidRayTracePlot pixel settings");
UNCOV
2507
    return OPENMC_E_INVALID_SIZE;
×
2508
  }
2509

2510
  ImageData data = plt->create_image();
16✔
2511
  if (static_cast<int32_t>(data.shape()[0]) != width ||
16!
2512
      static_cast<int32_t>(data.shape()[1]) != height) {
16!
UNCOV
2513
    set_errmsg("Unexpected image size from SolidRayTracePlot create_image");
×
UNCOV
2514
    return OPENMC_E_INVALID_SIZE;
×
2515
  }
2516

2517
  for (int32_t y = 0; y < height; ++y) {
112✔
2518
    for (int32_t x = 0; x < width; ++x) {
864✔
2519
      const auto& color = data(x, y);
768✔
2520
      size_t idx = (static_cast<size_t>(y) * width + x) * 3;
768✔
2521
      data_out[idx + 0] = color.red;
768✔
2522
      data_out[idx + 1] = color.green;
768✔
2523
      data_out[idx + 2] = color.blue;
768✔
2524
    }
2525
  }
2526

2527
  return 0;
2528
}
16✔
2529

2530
extern "C" int openmc_solidraytrace_plot_get_color(
8✔
2531
  int32_t index, int32_t id, uint8_t* r, uint8_t* g, uint8_t* b)
2532
{
2533
  if (!r || !g || !b) {
8!
UNCOV
2534
    set_errmsg(
×
2535
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color");
UNCOV
2536
    return OPENMC_E_INVALID_ARGUMENT;
×
2537
  }
2538

2539
  SolidRayTracePlot* plt = nullptr;
8✔
2540
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2541
  if (err)
8!
2542
    return err;
2543

2544
  int32_t domain_index = -1;
8✔
2545
  err = map_phong_domain_id(plt, id, &domain_index);
8✔
2546
  if (err)
8!
2547
    return err;
2548

2549
  if (domain_index < 0 ||
8!
2550
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
8!
UNCOV
2551
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
UNCOV
2552
    return OPENMC_E_OUT_OF_BOUNDS;
×
2553
  }
2554

2555
  const auto& color = plt->colors_[domain_index];
8✔
2556
  *r = color.red;
8✔
2557
  *g = color.green;
8✔
2558
  *b = color.blue;
8✔
2559
  return 0;
8✔
2560
}
2561

2562
extern "C" int openmc_solidraytrace_plot_get_diffuse_fraction(
8✔
2563
  int32_t index, double* diffuse_fraction)
2564
{
2565
  if (!diffuse_fraction) {
8!
UNCOV
2566
    set_errmsg("Invalid arguments passed to "
×
2567
               "openmc_solidraytrace_plot_get_diffuse_fraction");
UNCOV
2568
    return OPENMC_E_INVALID_ARGUMENT;
×
2569
  }
2570

2571
  SolidRayTracePlot* plt = nullptr;
8✔
2572
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2573
  if (err)
8!
2574
    return err;
2575

2576
  *diffuse_fraction = plt->diffuse_fraction();
8✔
2577
  return 0;
8✔
2578
}
2579

2580
extern "C" int openmc_solidraytrace_plot_set_diffuse_fraction(
8✔
2581
  int32_t index, double diffuse_fraction)
2582
{
2583
  SolidRayTracePlot* plt = nullptr;
8✔
2584
  int err = get_solidraytrace_plot_by_index(index, &plt);
8✔
2585
  if (err)
8!
2586
    return err;
2587

2588
  if (diffuse_fraction < 0.0 || diffuse_fraction > 1.0) {
8!
UNCOV
2589
    set_errmsg("Diffuse fraction must be between 0 and 1");
×
UNCOV
2590
    return OPENMC_E_INVALID_ARGUMENT;
×
2591
  }
2592

2593
  plt->diffuse_fraction() = diffuse_fraction;
8✔
2594
  return 0;
8✔
2595
}
2596

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

© 2026 Coveralls, Inc