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

openmc-dev / openmc / 22070080915

16 Feb 2026 04:15PM UTC coverage: 81.702% (-0.02%) from 81.72%
22070080915

Pull #3806

github

web-flow
Merge 4824efac8 into c6ef84d1d
Pull Request #3806: Introduce new C API function for slice plots

17579 of 24709 branches covered (71.14%)

Branch coverage included in aggregate %.

197 of 245 new or added lines in 4 files covered. (80.41%)

336 existing lines in 26 files now uncovered.

57190 of 66805 relevant lines covered (85.61%)

50553675.8 hits per line

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

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

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

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

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

39
namespace openmc {
40

41
//==============================================================================
42
// Constants
43
//==============================================================================
44

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

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

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

67
  // set material data
68
  Cell* c = model::cells.at(p.lowest_coord().cell()).get();
35,231,718✔
69
  if (p.material() == MATERIAL_VOID) {
35,231,718✔
70
    data_(y, x, 2) = MATERIAL_VOID;
27,102,344✔
71
  } else if (c->type_ == Fill::MATERIAL) {
8,129,374!
72
    Material* m = model::materials.at(p.material()).get();
8,129,374✔
73
    data_(y, x, 2) = m->id_;
8,129,374✔
74
  }
75
}
35,231,718✔
76

77
void IdData::set_overlap(size_t y, size_t x)
340,280✔
78
{
79
  xt::view(data_, y, x, xt::all()) = OVERLAP;
340,280✔
80
}
340,280✔
81

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

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

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

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

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

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

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

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

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

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

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

NEW
166
  property_data_(y, x, 0) = OVERLAP;
×
NEW
167
  property_data_(y, x, 1) = OVERLAP;
×
NEW
168
}
×
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()
110✔
187
{
188

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

194
  return 0;
110✔
195
}
196

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

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

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

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

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

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

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

260
void read_plots_xml()
1,234✔
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";
1,234✔
266
  if (!file_exists(filename) && settings::run_mode == RunMode::PLOTTING) {
1,234!
267
    fatal_error(fmt::format("Plots XML file '{}' does not exist!", filename));
×
268
  }
269

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

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

276
  pugi::xml_node root = doc.document_element();
1,234✔
277

278
  read_plots_xml(root);
1,234✔
279
}
1,234✔
280

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

289
    if (check_for_node(node, "type")) {
872!
290
      std::string type_str = get_node_value(node, "type", true);
872✔
291
      if (type_str == "slice") {
872✔
292
        model::plots.emplace_back(
734✔
293
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,476✔
294
      } else if (type_str == "voxel") {
130✔
295
        model::plots.emplace_back(
50✔
296
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
100✔
297
      } else if (type_str == "wireframe_raytrace") {
80✔
298
        model::plots.emplace_back(
50✔
299
          std::make_unique<WireframeRayTracePlot>(node));
100✔
300
      } else if (type_str == "solid_raytrace") {
30!
301
        model::plots.emplace_back(std::make_unique<SolidRayTracePlot>(node));
30✔
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;
864✔
307
    } else {
864✔
308
      fatal_error(fmt::format("Must specify plot type in plot {}", plot_desc));
×
309
    }
310
  }
864✔
311
}
1,648✔
312

313
void free_memory_plot()
7,615✔
314
{
315
  model::plots.clear();
7,615✔
316
  model::plot_map.clear();
7,615✔
317
}
7,615✔
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
130✔
322
{
323
  size_t width = pixels()[0];
130✔
324
  size_t height = pixels()[1];
130✔
325

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

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

331
  // assign colors
332
  for (size_t y = 0; y < height; y++) {
27,330✔
333
    for (size_t x = 0; x < width; x++) {
6,929,200✔
334
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
6,902,000✔
335
      auto id = ids.data_(y, x, idx);
6,902,000✔
336
      // no setting needed if not found
337
      if (id == NOT_FOUND) {
6,902,000✔
338
        continue;
984,120✔
339
      }
340
      if (id == OVERLAP) {
5,943,560✔
341
        data(x, y) = overlap_color_;
25,680✔
342
        continue;
25,680✔
343
      }
344
      if (PlotColorBy::cells == color_by_) {
5,917,880✔
345
        data(x, y) = colors_[model::cell_map[id]];
2,737,880✔
346
      } else if (PlotColorBy::mats == color_by_) {
3,180,000!
347
        if (id == MATERIAL_VOID) {
3,180,000!
348
          data(x, y) = WHITE;
×
349
          continue;
×
350
        }
351
        data(x, y) = colors_[model::material_map[id]];
3,180,000✔
352
      } // color_by if-else
353
    }
354
  }
355

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

361
  return data;
260✔
362
}
130✔
363

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

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

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

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

391
  if (id_ == id)
882!
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()) {
882!
396
    throw std::runtime_error {
×
397
      fmt::format("Two or more plots use the same unique ID: {}", id)};
×
398
  }
399

400
  id_ = id;
882✔
401
}
402

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

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

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

446
  path_plot_ = filename;
784✔
447

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

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

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

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

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

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

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

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

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

602
void PlottableInterface::set_user_colors(pugi::xml_node plot_node)
872✔
603
{
604
  for (auto cn : plot_node.children("color")) {
1,042✔
605
    // Make sure 3 values are specified for RGB
606
    vector<int> user_rgb = get_node_array<int>(cn, "rgb");
170✔
607
    if (user_rgb.size() != 3) {
170!
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;
612
    if (check_for_node(cn, "id")) {
170!
613
      col_id = std::stoi(get_node_value(cn, "id"));
170✔
614
    } else {
615
      fatal_error(fmt::format(
×
616
        "Must specify id for color specification in plot {}", id()));
×
617
    }
618
    // Add RGB
619
    if (PlotColorBy::cells == color_by_) {
170✔
620
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
80!
621
        col_id = model::cell_map[col_id];
80✔
622
        colors_[col_id] = user_rgb;
80✔
623
      } else {
624
        warning(fmt::format(
×
625
          "Could not find cell {} specified in plot {}", col_id, id()));
×
626
      }
627
    } else if (PlotColorBy::mats == color_by_) {
90!
628
      if (model::material_map.find(col_id) != model::material_map.end()) {
90!
629
        col_id = model::material_map[col_id];
90✔
630
        colors_[col_id] = user_rgb;
90✔
631
      } else {
632
        warning(fmt::format(
×
633
          "Could not find material {} specified in plot {}", col_id, id()));
×
634
      }
635
    }
636
  } // color node loop
170✔
637
}
872✔
638

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

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

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

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

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

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

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

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

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

757
      // Determine how many components there are and allocate
758
      vector<int> iarray = get_node_array<int>(mask_node, "components");
30✔
759
      if (iarray.size() == 0) {
30!
760
        fatal_error(
×
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) {
90✔
767
        if (PlotColorBy::cells == color_by_) {
60!
768
          if (model::cell_map.find(col_id) != model::cell_map.end()) {
60!
769
            col_id = model::cell_map[col_id];
60✔
770
          } else {
771
            fatal_error(fmt::format("Could not find cell {} specified in the "
×
772
                                    "mask in plot {}",
773
              col_id, id()));
×
774
          }
775
        } else if (PlotColorBy::mats == color_by_) {
×
776
          if (model::material_map.find(col_id) != model::material_map.end()) {
×
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 {}",
781
              col_id, id()));
×
782
          }
783
        }
784
      }
785

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

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

804
void PlottableInterface::set_overlap_color(pugi::xml_node plot_node)
872✔
805
{
806
  color_overlaps_ = false;
872✔
807
  if (check_for_node(plot_node, "show_overlaps")) {
872✔
808
    color_overlaps_ = get_node_value_bool(plot_node, "show_overlaps");
20✔
809
    // check for custom overlap color
810
    if (check_for_node(plot_node, "overlap_color")) {
20✔
811
      if (!color_overlaps_) {
10!
812
        warning(fmt::format(
×
813
          "Overlap color specified in plot {} but overlaps won't be shown.",
814
          id()));
×
815
      }
816
      vector<int> olap_clr = get_node_array<int>(plot_node, "overlap_color");
10✔
817
      if (olap_clr.size() == 3) {
10!
818
        overlap_color_ = olap_clr;
10✔
819
      } else {
820
        fatal_error(fmt::format("Bad overlap RGB in plot {}", id()));
×
821
      }
822
    }
10✔
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) {
872!
828
    settings::check_overlaps = true;
20✔
829
    model::overlap_check_count.resize(model::cells.size(), 0);
20✔
830
  }
831
}
872✔
832

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

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

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

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

868
  of.open(fname);
×
869

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

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++) {
×
880
      RGBColor rgb = data(x, y);
×
881
      of << rgb.red << rgb.green << rgb.blue;
×
882
    }
883
  }
884
  of << "\n";
×
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)
210✔
893
{
894
  // Open PNG file for writing
895
  std::string fname = filename;
210✔
896
  fname = strtrim(fname);
210✔
897
  auto fp = std::fopen(fname.c_str(), "wb");
210✔
898

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

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

908
  png_init_io(png_ptr, fp);
210✔
909

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

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

920
  // Write color for each pixel
921
  for (int y = 0; y < height; y++) {
43,410✔
922
    for (int x = 0; x < width; x++) {
10,145,200✔
923
      RGBColor rgb = data(x, y);
10,102,000✔
924
      row[3 * x] = rgb.red;
10,102,000✔
925
      row[3 * x + 1] = rgb.green;
10,102,000✔
926
      row[3 * x + 2] = rgb.blue;
10,102,000✔
927
    }
928
    png_write_row(png_ptr, row.data());
43,200✔
929
  }
930

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

934
  // Clean up data structures
935
  std::fclose(fp);
210✔
936
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
210✔
937
  png_destroy_write_struct(&png_ptr, &info_ptr);
210✔
938
}
210✔
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
30✔
946
{
947
  RGBColor rgb;
30✔
948
  rgb = meshlines_color_;
30✔
949

950
  int ax1, ax2;
951
  switch (basis_) {
30!
952
  case PlotBasis::xy:
20✔
953
    ax1 = 0;
20✔
954
    ax2 = 1;
20✔
955
    break;
20✔
956
  case PlotBasis::xz:
10✔
957
    ax1 = 0;
10✔
958
    ax2 = 2;
10✔
959
    break;
10✔
960
  case PlotBasis::yz:
×
961
    ax1 = 1;
×
962
    ax2 = 2;
×
963
    break;
×
964
  default:
×
965
    UNREACHABLE();
×
966
  }
967

968
  Position ll_plot {origin_};
30✔
969
  Position ur_plot {origin_};
30✔
970

971
  ll_plot[ax1] -= width_[0] / 2.;
30✔
972
  ll_plot[ax2] -= width_[1] / 2.;
30✔
973
  ur_plot[ax1] += width_[0] / 2.;
30✔
974
  ur_plot[ax2] += width_[1] / 2.;
30✔
975

976
  Position width = ur_plot - ll_plot;
30✔
977

978
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
979
  auto axis_lines =
980
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
30✔
981

982
  // Find the bounds along the second axis (accounting for low-D meshes).
983
  int ax2_min, ax2_max;
984
  if (axis_lines.second.size() > 0) {
30!
985
    double frac = (axis_lines.second.back() - ll_plot[ax2]) / width[ax2];
30✔
986
    ax2_min = (1.0 - frac) * pixels()[1];
30✔
987
    if (ax2_min < 0)
30!
988
      ax2_min = 0;
×
989
    frac = (axis_lines.second.front() - ll_plot[ax2]) / width[ax2];
30✔
990
    ax2_max = (1.0 - frac) * pixels()[1];
30✔
991
    if (ax2_max > pixels()[1])
30!
992
      ax2_max = pixels()[1];
×
993
  } else {
994
    ax2_min = 0;
×
995
    ax2_max = pixels()[1];
×
996
  }
997

998
  // Iterate across the first axis and draw lines.
999
  for (auto ax1_val : axis_lines.first) {
170✔
1000
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
140✔
1001
    int ax1_ind = frac * pixels()[0];
140✔
1002
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
22,680✔
1003
      for (int plus = 0; plus <= meshlines_width_; plus++) {
45,080✔
1004
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels()[0])
22,540!
1005
          data(ax1_ind + plus, ax2_ind) = rgb;
22,540✔
1006
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels()[0])
22,540!
1007
          data(ax1_ind - plus, ax2_ind) = rgb;
22,540✔
1008
      }
1009
    }
1010
  }
1011

1012
  // Find the bounds along the first axis.
1013
  int ax1_min, ax1_max;
1014
  if (axis_lines.first.size() > 0) {
30!
1015
    double frac = (axis_lines.first.front() - ll_plot[ax1]) / width[ax1];
30✔
1016
    ax1_min = frac * pixels()[0];
30✔
1017
    if (ax1_min < 0)
30!
1018
      ax1_min = 0;
×
1019
    frac = (axis_lines.first.back() - ll_plot[ax1]) / width[ax1];
30✔
1020
    ax1_max = frac * pixels()[0];
30✔
1021
    if (ax1_max > pixels()[0])
30!
1022
      ax1_max = pixels()[0];
×
1023
  } else {
1024
    ax1_min = 0;
×
1025
    ax1_max = pixels()[0];
×
1026
  }
1027

1028
  // Iterate across the second axis and draw lines.
1029
  for (auto ax2_val : axis_lines.second) {
190✔
1030
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
160✔
1031
    int ax2_ind = (1.0 - frac) * pixels()[1];
160✔
1032
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
25,760✔
1033
      for (int plus = 0; plus <= meshlines_width_; plus++) {
51,200✔
1034
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels()[1])
25,600!
1035
          data(ax1_ind, ax2_ind + plus) = rgb;
25,600✔
1036
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels()[1])
25,600!
1037
          data(ax1_ind, ax2_ind - plus) = rgb;
25,600✔
1038
      }
1039
    }
1040
  }
1041
}
30✔
1042

1043
/* outputs a binary file that can be input into silomesh for 3D geometry
1044
 * visualization.  It works the same way as create_image by dragging a particle
1045
 * across the geometry for the specified number of voxels. The first 3 int's in
1046
 * the binary are the number of x, y, and z voxels.  The next 3 double's are
1047
 * the widths of the voxels in the x, y, and z directions. The next 3 double's
1048
 * are the x, y, and z coordinates of the lower left point. Finally the binary
1049
 * is filled with entries of four int's each. Each 'row' in the binary contains
1050
 * four int's: 3 for x,y,z position and 1 for cell or material id.  For 1
1051
 * million voxels this produces a file of approximately 15MB.
1052
 */
1053
void Plot::create_voxel() const
50✔
1054
{
1055
  // compute voxel widths in each direction
1056
  array<double, 3> vox;
1057
  vox[0] = width_[0] / static_cast<double>(pixels()[0]);
50✔
1058
  vox[1] = width_[1] / static_cast<double>(pixels()[1]);
50✔
1059
  vox[2] = width_[2] / static_cast<double>(pixels()[2]);
50✔
1060

1061
  // initial particle position
1062
  Position ll = origin_ - width_ / 2.;
50✔
1063

1064
  // Open binary plot file for writing
1065
  std::ofstream of;
50✔
1066
  std::string fname = std::string(path_plot_);
50✔
1067
  fname = strtrim(fname);
50✔
1068
  hid_t file_id = file_open(fname, 'w');
50✔
1069

1070
  // write header info
1071
  write_attribute(file_id, "filetype", "voxel");
50✔
1072
  write_attribute(file_id, "version", VERSION_VOXEL);
50✔
1073
  write_attribute(file_id, "openmc_version", VERSION);
50✔
1074

1075
#ifdef GIT_SHA1
1076
  write_attribute(file_id, "git_sha1", GIT_SHA1);
1077
#endif
1078

1079
  // Write current date and time
1080
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
50✔
1081
  array<int, 3> h5_pixels;
1082
  std::copy(pixels().begin(), pixels().end(), h5_pixels.begin());
50✔
1083
  write_attribute(file_id, "num_voxels", h5_pixels);
50✔
1084
  write_attribute(file_id, "voxel_width", vox);
50✔
1085
  write_attribute(file_id, "lower_left", ll);
50✔
1086

1087
  // Create dataset for voxel data -- note that the dimensions are reversed
1088
  // since we want the order in the file to be z, y, x
1089
  hsize_t dims[3];
1090
  dims[0] = pixels()[2];
50✔
1091
  dims[1] = pixels()[1];
50✔
1092
  dims[2] = pixels()[0];
50✔
1093
  hid_t dspace, dset, memspace;
1094
  voxel_init(file_id, &(dims[0]), &dspace, &dset, &memspace);
50✔
1095

1096
  SlicePlotBase pltbase;
50✔
1097
  pltbase.width_ = width_;
50✔
1098
  pltbase.origin_ = origin_;
50✔
1099
  pltbase.basis_ = PlotBasis::xy;
50✔
1100
  pltbase.u_span_ = {width_.x, 0.0, 0.0};
50✔
1101
  pltbase.v_span_ = {0.0, width_.y, 0.0};
50✔
1102
  pltbase.pixels() = pixels();
50✔
1103
  pltbase.slice_color_overlaps_ = color_overlaps_;
50✔
1104

1105
  ProgressBar pb;
50✔
1106
  for (int z = 0; z < pixels()[2]; z++) {
4,350✔
1107
    // update z coordinate
1108
    pltbase.origin_.z = ll.z + z * vox[2];
4,300✔
1109

1110
    // generate ids using plotbase
1111
    IdData ids = pltbase.get_map<IdData>();
4,300✔
1112

1113
    // select only cell/material ID data and flip the y-axis
1114
    int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
4,300!
1115
    xt::xtensor<int32_t, 2> data_slice =
1116
      xt::view(ids.data_, xt::all(), xt::all(), idx);
4,300✔
1117
    xt::xtensor<int32_t, 2> data_flipped = xt::flip(data_slice, 0);
4,300✔
1118

1119
    // Write to HDF5 dataset
1120
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
4,300✔
1121

1122
    // update progress bar
1123
    pb.set_value(
4,300✔
1124
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels()[2])));
4,300✔
1125
  }
4,300✔
1126

1127
  voxel_finalize(dspace, dset, memspace);
50✔
1128
  file_close(file_id);
50✔
1129
}
50✔
1130

1131
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
50✔
1132
  hid_t* memspace)
1133
{
1134
  // Create dataspace/dataset for voxel data
1135
  *dspace = H5Screate_simple(3, dims, nullptr);
50✔
1136
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
50✔
1137
    H5P_DEFAULT, H5P_DEFAULT);
1138

1139
  // Create dataspace for a slice of the voxel
1140
  hsize_t dims_slice[2] {dims[1], dims[2]};
50✔
1141
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
50✔
1142

1143
  // Select hyperslab in dataspace
1144
  hsize_t start[3] {0, 0, 0};
50✔
1145
  hsize_t count[3] {1, dims[1], dims[2]};
50✔
1146
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
50✔
1147
}
50✔
1148

1149
void voxel_write_slice(
4,300✔
1150
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1151
{
1152
  hssize_t offset[3] {x, 0, 0};
4,300✔
1153
  H5Soffset_simple(dspace, offset);
4,300✔
1154
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
4,300✔
1155
}
4,300✔
1156

1157
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
50✔
1158
{
1159
  H5Dclose(dset);
50✔
1160
  H5Sclose(dspace);
50✔
1161
  H5Sclose(memspace);
50✔
1162
}
50✔
1163

1164
RGBColor random_color(void)
3,084✔
1165
{
1166
  return {int(prn(&model::plotter_seed) * 255),
3,084✔
1167
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
3,084✔
1168
}
1169

1170
RayTracePlot::RayTracePlot(pugi::xml_node node) : PlottableInterface(node)
80✔
1171
{
1172
  set_look_at(node);
80✔
1173
  set_camera_position(node);
80✔
1174
  set_field_of_view(node);
80✔
1175
  set_pixels(node);
80✔
1176
  set_orthographic_width(node);
80✔
1177
  set_output_path(node);
80✔
1178

1179
  if (check_for_node(node, "orthographic_width") &&
90!
1180
      check_for_node(node, "field_of_view"))
10!
1181
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1182
                "parameters.");
1183
}
80✔
1184

1185
void RayTracePlot::update_view()
100✔
1186
{
1187
  // Get centerline vector for camera-to-model. We create vectors around this
1188
  // that form a pixel array, and then trace rays along that.
1189
  auto up = up_ / up_.norm();
100✔
1190
  Direction looking_direction = look_at_ - camera_position_;
100✔
1191
  looking_direction /= looking_direction.norm();
100✔
1192
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
100!
1193
    fatal_error("Up vector cannot align with vector between camera position "
×
1194
                "and look_at!");
1195
  Direction cam_yaxis = looking_direction.cross(up);
100✔
1196
  cam_yaxis /= cam_yaxis.norm();
100✔
1197
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
100✔
1198
  cam_zaxis /= cam_zaxis.norm();
100✔
1199

1200
  // Cache the camera-to-model matrix
1201
  camera_to_model_ = {looking_direction.x, cam_yaxis.x, cam_zaxis.x,
100✔
1202
    looking_direction.y, cam_yaxis.y, cam_zaxis.y, looking_direction.z,
100✔
1203
    cam_yaxis.z, cam_zaxis.z};
100✔
1204
}
100✔
1205

1206
WireframeRayTracePlot::WireframeRayTracePlot(pugi::xml_node node)
50✔
1207
  : RayTracePlot(node)
50✔
1208
{
1209
  set_opacities(node);
50✔
1210
  set_wireframe_thickness(node);
50✔
1211
  set_wireframe_ids(node);
50✔
1212
  set_wireframe_color(node);
50✔
1213
  update_view();
50✔
1214
}
50✔
1215

1216
void WireframeRayTracePlot::set_wireframe_color(pugi::xml_node plot_node)
50✔
1217
{
1218
  // Copy plot wireframe color
1219
  if (check_for_node(plot_node, "wireframe_color")) {
50!
1220
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
1221
    if (w_rgb.size() == 3) {
×
1222
      wireframe_color_ = w_rgb;
×
1223
    } else {
1224
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1225
    }
1226
  }
×
1227
}
50✔
1228

1229
void RayTracePlot::set_output_path(pugi::xml_node node)
80✔
1230
{
1231
  // Set output file path
1232
  std::string filename;
80✔
1233

1234
  if (check_for_node(node, "filename")) {
80✔
1235
    filename = get_node_value(node, "filename");
70✔
1236
  } else {
1237
    filename = fmt::format("plot_{}", id());
20✔
1238
  }
1239

1240
#ifdef USE_LIBPNG
1241
  if (!file_extension_present(filename, "png"))
80✔
1242
    filename.append(".png");
30✔
1243
#else
1244
  if (!file_extension_present(filename, "ppm"))
1245
    filename.append(".ppm");
1246
#endif
1247
  path_plot_ = filename;
80✔
1248
}
80✔
1249

1250
bool WireframeRayTracePlot::trackstack_equivalent(
2,764,690✔
1251
  const std::vector<TrackSegment>& track1,
1252
  const std::vector<TrackSegment>& track2) const
1253
{
1254
  if (wireframe_ids_.empty()) {
2,764,690✔
1255
    // Draw wireframe for all surfaces/cells/materials
1256
    if (track1.size() != track2.size())
2,313,700✔
1257
      return false;
49,070✔
1258
    for (int i = 0; i < track1.size(); ++i) {
6,098,140✔
1259
      if (track1[i].id != track2[i].id ||
7,703,100✔
1260
          track1[i].surface_index != track2[i].surface_index) {
3,851,490✔
1261
        return false;
18,100✔
1262
      }
1263
    }
1264
    return true;
2,246,530✔
1265
  } else {
1266
    // This runs in O(nm) where n is the intersection stack size
1267
    // and m is the number of IDs we are wireframing. A simpler
1268
    // algorithm can likely be found.
1269
    for (const int id : wireframe_ids_) {
896,540✔
1270
      int t1_i = 0;
450,990✔
1271
      int t2_i = 0;
450,990✔
1272

1273
      // Advance to first instance of the ID
1274
      while (t1_i < track1.size() && t2_i < track2.size()) {
511,300✔
1275
        while (t1_i < track1.size() && track1[t1_i].id != id)
357,120✔
1276
          t1_i++;
208,230✔
1277
        while (t2_i < track2.size() && track2[t2_i].id != id)
357,880✔
1278
          t2_i++;
208,990✔
1279

1280
        // This one is really important!
1281
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
360,470✔
1282
            (t1_i != track1.size() && t2_i == track2.size()))
211,580✔
1283
          return false;
5,440✔
1284
        if (t1_i == track1.size() && t2_i == track2.size())
145,510!
1285
          break;
83,140✔
1286
        // Check if surface different
1287
        if (track1[t1_i].surface_index != track2[t2_i].surface_index)
62,370✔
1288
          return false;
1,350✔
1289

1290
        // Pretty sure this should not be used:
1291
        // if (t2_i != track2.size() - 1 &&
1292
        //     t1_i != track1.size() - 1 &&
1293
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1294
        if (t2_i != 0 && t1_i != 0 &&
110,060!
1295
            track1[t1_i - 1].surface_index != track2[t2_i - 1].surface_index)
49,040✔
1296
          return false;
710✔
1297

1298
        // Check if neighboring cells are different
1299
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1300
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1301
        // ].id !=
1302
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1303
        //    false;
1304
        t1_i++, t2_i++;
60,310✔
1305
      }
1306
    }
1307
    return true;
445,550✔
1308
  }
1309
}
1310

1311
std::pair<Position, Direction> RayTracePlot::get_pixel_ray(
3,200,960✔
1312
  int horiz, int vert) const
1313
{
1314
  // Compute field of view in radians
1315
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
3,200,960✔
1316
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
3,200,960✔
1317
  double p0 = static_cast<double>(pixels()[0]);
3,200,960✔
1318
  double p1 = static_cast<double>(pixels()[1]);
3,200,960✔
1319
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
3,200,960✔
1320

1321
  // focal_plane_dist can be changed to alter the perspective distortion
1322
  // effect. This is in units of cm. This seems to look good most of the
1323
  // time. TODO let this variable be set through XML.
1324
  constexpr double focal_plane_dist = 10.0;
3,200,960✔
1325
  const double dx = 2.0 * focal_plane_dist * std::tan(0.5 * horiz_fov_radians);
3,200,960✔
1326
  const double dy = p1 / p0 * dx;
3,200,960✔
1327

1328
  std::pair<Position, Direction> result;
3,200,960✔
1329

1330
  // Generate the starting position/direction of the ray
1331
  if (orthographic_width_ == C_NONE) { // perspective projection
3,200,960✔
1332
    Direction camera_local_vec;
2,800,960✔
1333
    camera_local_vec.x = focal_plane_dist;
2,800,960✔
1334
    camera_local_vec.y = -0.5 * dx + horiz * dx / p0;
2,800,960✔
1335
    camera_local_vec.z = 0.5 * dy - vert * dy / p1;
2,800,960✔
1336
    camera_local_vec /= camera_local_vec.norm();
2,800,960✔
1337

1338
    result.first = camera_position_;
2,800,960✔
1339
    result.second = camera_local_vec.rotate(camera_to_model_);
2,800,960✔
1340
  } else { // orthographic projection
1341

1342
    double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
400,000✔
1343
    double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p1;
400,000✔
1344

1345
    result.first = camera_position_ +
1346
                   camera_y_axis() * x_pix_coord * orthographic_width_ +
400,000✔
1347
                   camera_z_axis() * y_pix_coord * orthographic_width_;
400,000✔
1348
    result.second = camera_x_axis();
400,000✔
1349
  }
1350

1351
  return result;
3,200,960✔
1352
}
1353

1354
ImageData WireframeRayTracePlot::create_image() const
50✔
1355
{
1356
  size_t width = pixels()[0];
50✔
1357
  size_t height = pixels()[1];
50✔
1358
  ImageData data({width, height}, not_found_);
50✔
1359

1360
  // This array marks where the initial wireframe was drawn. We convolve it with
1361
  // a filter that gets adjusted with the wireframe thickness in order to
1362
  // thicken the lines.
1363
  xt::xtensor<int, 2> wireframe_initial({width, height}, 0);
50✔
1364

1365
  /* Holds all of the track segments for the current rendered line of pixels.
1366
   * old_segments holds a copy of this_line_segments from the previous line.
1367
   * By holding both we can check if the cell/material intersection stack
1368
   * differs from the left or upper neighbor. This allows a robustly drawn
1369
   * wireframe. If only checking the left pixel (which requires substantially
1370
   * less memory), the wireframe tends to be spotty and be disconnected for
1371
   * surface edges oriented horizontally in the rendering.
1372
   *
1373
   * Note that a vector of vectors is required rather than a 2-tensor,
1374
   * since the stack size varies within each column.
1375
   */
1376
  const int n_threads = num_threads();
50✔
1377
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1378
    n_threads);
50✔
1379
  for (int t = 0; t < n_threads; ++t) {
130✔
1380
    this_line_segments[t].resize(pixels()[0]);
80✔
1381
  }
1382

1383
  // The last thread writes to this, and the first thread reads from it.
1384
  std::vector<std::vector<TrackSegment>> old_segments(pixels()[0]);
50✔
1385

1386
#pragma omp parallel
30✔
1387
  {
1388
    const int n_threads = num_threads();
20✔
1389
    const int tid = thread_num();
20✔
1390

1391
    int vert = tid;
20✔
1392
    for (int iter = 0; iter <= pixels()[1] / n_threads; iter++) {
4,040✔
1393

1394
      // Save bottom line of current work chunk to compare against later. This
1395
      // used to be inside the below if block, but it causes a spurious line to
1396
      // be drawn at the bottom of the image. Not sure why, but moving it here
1397
      // fixes things.
1398
      if (tid == n_threads - 1)
4,020!
1399
        old_segments = this_line_segments[n_threads - 1];
4,020✔
1400

1401
      if (vert < pixels()[1]) {
4,020✔
1402

1403
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
804,000✔
1404

1405
          // RayTracePlot implements camera ray generation
1406
          std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
800,000✔
1407

1408
          this_line_segments[tid][horiz].clear();
800,000✔
1409
          ProjectionRay ray(
1410
            ru.first, ru.second, *this, this_line_segments[tid][horiz]);
800,000✔
1411

1412
          ray.trace();
800,000✔
1413

1414
          // Now color the pixel based on what we have intersected...
1415
          // Loops backwards over intersections.
1416
          Position current_color(
1417
            not_found_.red, not_found_.green, not_found_.blue);
800,000✔
1418
          const auto& segments = this_line_segments[tid][horiz];
800,000✔
1419

1420
          // There must be at least two cell intersections to color, front and
1421
          // back of the cell. Maybe an infinitely thick cell could be present
1422
          // with no back, but why would you want to color that? It's easier to
1423
          // just skip that edge case and not even color it.
1424
          if (segments.size() <= 1)
800,000✔
1425
            continue;
493,324✔
1426

1427
          for (int i = segments.size() - 2; i >= 0; --i) {
857,868✔
1428
            int colormap_idx = segments[i].id;
551,192✔
1429
            RGBColor seg_color = colors_[colormap_idx];
551,192✔
1430
            Position seg_color_vec(
1431
              seg_color.red, seg_color.green, seg_color.blue);
551,192✔
1432
            double mixing =
1433
              std::exp(-xs_[colormap_idx] *
551,192✔
1434
                       (segments[i + 1].length - segments[i].length));
551,192✔
1435
            current_color =
1436
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
551,192✔
1437
          }
1438

1439
          // save result converting from double-precision color coordinates to
1440
          // byte-sized
1441
          RGBColor result;
306,676✔
1442
          result.red = static_cast<uint8_t>(current_color.x);
306,676✔
1443
          result.green = static_cast<uint8_t>(current_color.y);
306,676✔
1444
          result.blue = static_cast<uint8_t>(current_color.z);
306,676✔
1445
          data(horiz, vert) = result;
306,676✔
1446

1447
          // Check to draw wireframe in horizontal direction. No inter-thread
1448
          // comm.
1449
          if (horiz > 0) {
306,676✔
1450
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
305,876✔
1451
                  this_line_segments[tid][horiz - 1])) {
305,876✔
1452
              wireframe_initial(horiz, vert) = 1;
12,568✔
1453
            }
1454
          }
1455
        }
800,000✔
1456
      } // end "if" vert in correct range
1457

1458
      // We require a barrier before comparing vertical neighbors' intersection
1459
      // stacks. i.e. all threads must be done with their line.
1460
#pragma omp barrier
1461

1462
      // Now that the horizontal line has finished rendering, we can fill in
1463
      // wireframe entries that require comparison among all the threads. Hence
1464
      // the omp barrier being used. It has to be OUTSIDE any if blocks!
1465
      if (vert < pixels()[1]) {
4,020✔
1466
        // Loop over horizontal pixels, checking intersection stack of upper
1467
        // neighbor
1468

1469
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
4,000✔
1470
        if (tid == 0)
4,000!
1471
          top_cmp = &old_segments;
4,000✔
1472
        else
1473
          top_cmp = &this_line_segments[tid - 1];
1474

1475
        for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
804,000✔
1476
          if (!trackstack_equivalent(
800,000✔
1477
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
800,000✔
1478
            wireframe_initial(horiz, vert) = 1;
16,476✔
1479
          }
1480
        }
1481
      }
1482

1483
      // We need another barrier to ensure threads don't proceed to modify their
1484
      // intersection stacks on that horizontal line while others are
1485
      // potentially still working on the above.
1486
#pragma omp barrier
1487
      vert += n_threads;
4,020✔
1488
    }
1489
  } // end omp parallel
1490

1491
  // Now thicken the wireframe lines and apply them to our image
1492
  for (int vert = 0; vert < pixels()[1]; ++vert) {
10,050✔
1493
    for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
2,010,000✔
1494
      if (wireframe_initial(horiz, vert)) {
2,000,000✔
1495
        if (wireframe_thickness_ == 1)
64,530✔
1496
          data(horiz, vert) = wireframe_color_;
27,450✔
1497
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
177,930✔
1498
             ++i)
1499
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
497,160✔
1500
               ++j)
1501
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
383,760!
1502

1503
              // Check if wireframe pixel is out of bounds
1504
              int w_i = std::max(std::min(horiz + i, pixels()[0] - 1), 0);
383,760✔
1505
              int w_j = std::max(std::min(vert + j, pixels()[1] - 1), 0);
383,760✔
1506
              data(w_i, w_j) = wireframe_color_;
383,760✔
1507
            }
1508
      }
1509
    }
1510
  }
1511

1512
  return data;
100✔
1513
}
50✔
1514

1515
void WireframeRayTracePlot::create_output() const
50✔
1516
{
1517
  ImageData data = create_image();
50✔
1518
  write_image(data);
50✔
1519
}
50✔
1520

1521
void RayTracePlot::print_info() const
80✔
1522
{
1523
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
64✔
1524
    camera_position_.y, camera_position_.z);
80✔
1525
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
144✔
1526
  fmt::print(
64✔
1527
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
80✔
1528
  fmt::print("Pixels: {} {}\n", pixels()[0], pixels()[1]);
144✔
1529
}
80✔
1530

1531
void WireframeRayTracePlot::print_info() const
50✔
1532
{
1533
  fmt::print("Plot Type: Wireframe ray-traced\n");
50✔
1534
  RayTracePlot::print_info();
50✔
1535
}
50✔
1536

1537
void WireframeRayTracePlot::set_opacities(pugi::xml_node node)
50✔
1538
{
1539
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
50✔
1540

1541
  for (auto cn : node.children("color")) {
110✔
1542
    // Make sure 3 values are specified for RGB
1543
    double user_xs = std::stod(get_node_value(cn, "xs"));
60✔
1544
    int col_id = std::stoi(get_node_value(cn, "id"));
60✔
1545

1546
    // Add RGB
1547
    if (PlotColorBy::cells == color_by_) {
60!
1548
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
60!
1549
        col_id = model::cell_map[col_id];
60✔
1550
        xs_[col_id] = user_xs;
60✔
1551
      } else {
1552
        warning(fmt::format(
×
1553
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1554
      }
1555
    } else if (PlotColorBy::mats == color_by_) {
×
1556
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
1557
        col_id = model::material_map[col_id];
×
1558
        xs_[col_id] = user_xs;
×
1559
      } else {
1560
        warning(fmt::format(
×
1561
          "Could not find material {} specified in plot {}", col_id, id()));
×
1562
      }
1563
    }
1564
  }
1565
}
50✔
1566

1567
void RayTracePlot::set_orthographic_width(pugi::xml_node node)
80✔
1568
{
1569
  if (check_for_node(node, "orthographic_width")) {
80✔
1570
    double orthographic_width =
1571
      std::stod(get_node_value(node, "orthographic_width", true));
10✔
1572
    if (orthographic_width < 0.0)
10!
1573
      fatal_error("Requires positive orthographic_width");
×
1574
    orthographic_width_ = orthographic_width;
10✔
1575
  }
1576
}
80✔
1577

1578
void WireframeRayTracePlot::set_wireframe_thickness(pugi::xml_node node)
50✔
1579
{
1580
  if (check_for_node(node, "wireframe_thickness")) {
50✔
1581
    int wireframe_thickness =
1582
      std::stoi(get_node_value(node, "wireframe_thickness", true));
20✔
1583
    if (wireframe_thickness < 0)
20!
1584
      fatal_error("Requires non-negative wireframe thickness");
×
1585
    wireframe_thickness_ = wireframe_thickness;
20✔
1586
  }
1587
}
50✔
1588

1589
void WireframeRayTracePlot::set_wireframe_ids(pugi::xml_node node)
50✔
1590
{
1591
  if (check_for_node(node, "wireframe_ids")) {
50✔
1592
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
10✔
1593
    // It is read in as actual ID values, but we have to convert to indices in
1594
    // mat/cell array
1595
    for (auto& x : wireframe_ids_)
20✔
1596
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
10!
1597
                                         : model::cell_map[x];
×
1598
  }
1599
  // We make sure the list is sorted in order to later use
1600
  // std::binary_search.
1601
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
50✔
1602
}
50✔
1603

1604
void RayTracePlot::set_pixels(pugi::xml_node node)
80✔
1605
{
1606
  vector<int> pxls = get_node_array<int>(node, "pixels");
80✔
1607
  if (pxls.size() != 2)
80!
1608
    fatal_error(
×
1609
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1610
  pixels()[0] = pxls[0];
80✔
1611
  pixels()[1] = pxls[1];
80✔
1612
}
80✔
1613

1614
void RayTracePlot::set_camera_position(pugi::xml_node node)
80✔
1615
{
1616
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
80✔
1617
  if (camera_pos.size() != 3) {
80!
1618
    fatal_error(fmt::format(
×
1619
      "camera_position element must have three floating point values"));
1620
  }
1621
  camera_position_.x = camera_pos[0];
80✔
1622
  camera_position_.y = camera_pos[1];
80✔
1623
  camera_position_.z = camera_pos[2];
80✔
1624
}
80✔
1625

1626
void RayTracePlot::set_look_at(pugi::xml_node node)
80✔
1627
{
1628
  vector<double> look_at = get_node_array<double>(node, "look_at");
80✔
1629
  if (look_at.size() != 3) {
80!
1630
    fatal_error("look_at element must have three floating point values");
×
1631
  }
1632
  look_at_.x = look_at[0];
80✔
1633
  look_at_.y = look_at[1];
80✔
1634
  look_at_.z = look_at[2];
80✔
1635
}
80✔
1636

1637
void RayTracePlot::set_field_of_view(pugi::xml_node node)
80✔
1638
{
1639
  // Defaults to 70 degree horizontal field of view (see .h file)
1640
  if (check_for_node(node, "horizontal_field_of_view")) {
80!
1641
    double fov =
1642
      std::stod(get_node_value(node, "horizontal_field_of_view", true));
×
1643
    if (fov < 180.0 && fov > 0.0) {
×
1644
      horizontal_field_of_view_ = fov;
×
1645
    } else {
1646
      fatal_error(fmt::format("Horizontal field of view for plot {} "
×
1647
                              "out-of-range. Must be in (0, 180) degrees.",
1648
        id()));
×
1649
    }
1650
  }
1651
}
80✔
1652

1653
SolidRayTracePlot::SolidRayTracePlot(pugi::xml_node node) : RayTracePlot(node)
30✔
1654
{
1655
  set_opaque_ids(node);
30✔
1656
  set_diffuse_fraction(node);
30✔
1657
  set_light_position(node);
30✔
1658
  update_view();
30✔
1659
}
30✔
1660

1661
void SolidRayTracePlot::print_info() const
30✔
1662
{
1663
  fmt::print("Plot Type: Solid ray-traced\n");
30✔
1664
  RayTracePlot::print_info();
30✔
1665
}
30✔
1666

1667
ImageData SolidRayTracePlot::create_image() const
50✔
1668
{
1669
  size_t width = pixels()[0];
50✔
1670
  size_t height = pixels()[1];
50✔
1671
  ImageData data({width, height}, not_found_);
50✔
1672

1673
#pragma omp parallel for schedule(dynamic) collapse(2)
30✔
1674
  for (int horiz = 0; horiz < pixels()[0]; ++horiz) {
2,484✔
1675
    for (int vert = 0; vert < pixels()[1]; ++vert) {
482,848✔
1676
      // RayTracePlot implements camera ray generation
1677
      std::pair<Position, Direction> ru = get_pixel_ray(horiz, vert);
480,384✔
1678
      PhongRay ray(ru.first, ru.second, *this);
480,384✔
1679
      ray.trace();
480,384✔
1680
      data(horiz, vert) = ray.result_color();
480,384✔
1681
    }
480,384✔
1682
  }
1683

1684
  return data;
50✔
1685
}
1686

1687
void SolidRayTracePlot::create_output() const
30✔
1688
{
1689
  ImageData data = create_image();
30✔
1690
  write_image(data);
30✔
1691
}
30✔
1692

1693
void SolidRayTracePlot::set_opaque_ids(pugi::xml_node node)
30✔
1694
{
1695
  if (check_for_node(node, "opaque_ids")) {
30!
1696
    auto opaque_ids_tmp = get_node_array<int>(node, "opaque_ids");
30✔
1697

1698
    // It is read in as actual ID values, but we have to convert to indices in
1699
    // mat/cell array
1700
    for (auto& x : opaque_ids_tmp)
90✔
1701
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
60!
1702
                                         : model::cell_map[x];
×
1703

1704
    opaque_ids_.insert(opaque_ids_tmp.begin(), opaque_ids_tmp.end());
30✔
1705
  }
30✔
1706
}
30✔
1707

1708
void SolidRayTracePlot::set_light_position(pugi::xml_node node)
30✔
1709
{
1710
  if (check_for_node(node, "light_position")) {
30✔
1711
    auto light_pos_tmp = get_node_array<double>(node, "light_position");
10✔
1712

1713
    if (light_pos_tmp.size() != 3)
10!
1714
      fatal_error("Light position must be given as 3D coordinates");
×
1715

1716
    light_location_.x = light_pos_tmp[0];
10✔
1717
    light_location_.y = light_pos_tmp[1];
10✔
1718
    light_location_.z = light_pos_tmp[2];
10✔
1719
  } else {
10✔
1720
    light_location_ = camera_position();
20✔
1721
  }
1722
}
30✔
1723

1724
void SolidRayTracePlot::set_diffuse_fraction(pugi::xml_node node)
30✔
1725
{
1726
  if (check_for_node(node, "diffuse_fraction")) {
30✔
1727
    diffuse_fraction_ = std::stod(get_node_value(node, "diffuse_fraction"));
10✔
1728
    if (diffuse_fraction_ < 0.0 || diffuse_fraction_ > 1.0) {
10!
1729
      fatal_error("Must have 0 <= diffuse fraction <= 1");
×
1730
    }
1731
  }
1732
}
30✔
1733

1734
void Ray::compute_distance()
2,740,580✔
1735
{
1736
  boundary() = distance_to_boundary(*this);
2,740,580✔
1737
}
2,740,580✔
1738

1739
void Ray::trace()
3,200,960✔
1740
{
1741
  // To trace the ray from its origin all the way through the model, we have
1742
  // to proceed in two phases. In the first, the ray may or may not be found
1743
  // inside the model. If the ray is already in the model, phase one can be
1744
  // skipped. Otherwise, the ray has to be advanced to the boundary of the
1745
  // model where all the cells are defined. Importantly, this is assuming that
1746
  // the model is convex, which is a very reasonable assumption for any
1747
  // radiation transport model.
1748
  //
1749
  // After phase one is done, we can starting tracing from cell to cell within
1750
  // the model. This step can use neighbor lists to accelerate the ray tracing.
1751

1752
  // Attempt to initialize the particle. We may have to enter a loop to move
1753
  // it up to the edge of the model.
1754
  bool inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
3,200,960✔
1755

1756
  // Advance to the boundary of the model
1757
  while (!inside_cell) {
14,198,580!
1758
    advance_to_boundary_from_void();
14,198,580✔
1759
    inside_cell = exhaustive_find_cell(*this, settings::verbosity >= 10);
14,198,580✔
1760

1761
    // If true this means no surface was intersected. See cell.cpp and search
1762
    // for numeric_limits to see where we return it.
1763
    if (surface() == std::numeric_limits<int>::max()) {
14,198,580!
1764
      warning(fmt::format("Lost a ray, r = {}, u = {}", r(), u()));
×
1765
      return;
×
1766
    }
1767

1768
    // Exit this loop and enter into cell-to-cell ray tracing (which uses
1769
    // neighbor lists)
1770
    if (inside_cell)
14,198,580✔
1771
      break;
1,408,940✔
1772

1773
    // if there is no intersection with the model, we're done
1774
    if (boundary().surface() == SURFACE_NONE)
12,789,640✔
1775
      return;
1,792,020✔
1776

1777
    event_counter_++;
10,997,620✔
1778
    if (event_counter_ > MAX_INTERSECTIONS) {
10,997,620!
1779
      warning("Likely infinite loop in ray traced plot");
×
1780
      return;
×
1781
    }
1782
  }
1783

1784
  // Call the specialized logic for this type of ray. This is for the
1785
  // intersection for the first intersection if we had one.
1786
  if (boundary().surface() != SURFACE_NONE) {
1,408,940!
1787
    // set the geometry state's surface attribute to be used for
1788
    // surface normal computation
1789
    surface() = boundary().surface();
1,408,940✔
1790
    on_intersection();
1,408,940✔
1791
    if (stop_)
1,408,940!
1792
      return;
×
1793
  }
1794

1795
  // reset surface attribute to zero after the first intersection so that it
1796
  // doesn't perturb surface crossing logic from here on out
1797
  surface() = 0;
1,408,940✔
1798

1799
  // This is the ray tracing loop within the model. It exits after exiting
1800
  // the model, which is equivalent to assuming that the model is convex.
1801
  // It would be nice to factor out the on_intersection at the end of this
1802
  // loop and then do "while (inside_cell)", but we can't guarantee it's
1803
  // on a surface in that case. There might be some other way to set it
1804
  // up that is perhaps a little more elegant, but this is what works just
1805
  // fine.
1806
  while (true) {
1807

1808
    compute_distance();
2,098,700✔
1809

1810
    // There are no more intersections to process
1811
    // if we hit the edge of the model, so stop
1812
    // the particle in that case. Also, just exit
1813
    // if a negative distance was somehow computed.
1814
    if (boundary().distance() == INFTY || boundary().distance() == INFINITY ||
4,197,400!
1815
        boundary().distance() < 0) {
2,098,700!
1816
      return;
×
1817
    }
1818

1819
    // See below comment where call_on_intersection is checked in an
1820
    // if statement for an explanation of this.
1821
    bool call_on_intersection {true};
2,098,700✔
1822
    if (boundary().distance() < 10 * TINY_BIT) {
2,098,700✔
1823
      call_on_intersection = false;
539,350✔
1824
    }
1825

1826
    // DAGMC surfaces expect us to go a little bit further than the advance
1827
    // distance to properly check cell inclusion.
1828
    boundary().distance() += TINY_BIT;
2,098,700✔
1829

1830
    // Advance particle, prepare for next intersection
1831
    for (int lev = 0; lev < n_coord(); ++lev) {
4,197,400✔
1832
      coord(lev).r() += boundary().distance() * coord(lev).u();
2,098,700✔
1833
    }
1834
    surface() = boundary().surface();
2,098,700✔
1835
    n_coord_last() = n_coord();
2,098,700✔
1836
    n_coord() = boundary().coord_level();
2,098,700✔
1837
    if (boundary().lattice_translation()[0] != 0 ||
2,098,700✔
1838
        boundary().lattice_translation()[1] != 0 ||
4,197,400!
1839
        boundary().lattice_translation()[2] != 0) {
2,098,700!
1840
      cross_lattice(*this, boundary(), settings::verbosity >= 10);
×
1841
    }
1842

1843
    // Record how far the ray has traveled
1844
    traversal_distance_ += boundary().distance();
2,098,700✔
1845
    inside_cell = neighbor_list_find_cell(*this, settings::verbosity >= 10);
2,098,700✔
1846

1847
    // Call the specialized logic for this type of ray. Note that we do not
1848
    // call this if the advance distance is very small. Unfortunately, it seems
1849
    // darn near impossible to get the particle advanced to the model boundary
1850
    // and through it without sometimes accidentally calling on_intersection
1851
    // twice. This incorrectly shades the region as occluded when it might not
1852
    // actually be. By screening out intersection distances smaller than a
1853
    // threshold 10x larger than the scoot distance used to advance up to the
1854
    // model boundary, we can avoid that situation.
1855
    if (call_on_intersection) {
2,098,700✔
1856
      on_intersection();
1,559,350✔
1857
      if (stop_)
1,559,350✔
1858
        return;
32,330✔
1859
    }
1860

1861
    if (!inside_cell)
2,066,370✔
1862
      return;
1,376,610✔
1863

1864
    event_counter_++;
689,760✔
1865
    if (event_counter_ > MAX_INTERSECTIONS) {
689,760!
1866
      warning("Likely infinite loop in ray traced plot");
×
1867
      return;
×
1868
    }
1869
  }
689,760✔
1870
}
1871

1872
void ProjectionRay::on_intersection()
2,144,680✔
1873
{
1874
  // This records a tuple with the following info
1875
  //
1876
  // 1) ID (material or cell depending on color_by_)
1877
  // 2) Distance traveled by the ray through that ID
1878
  // 3) Index of the intersected surface (starting from 1)
1879

1880
  line_segments_.emplace_back(
2,144,680✔
1881
    plot_.color_by_ == PlottableInterface::PlotColorBy::mats
2,144,680✔
1882
      ? material()
496,290✔
1883
      : lowest_coord().cell(),
1,648,390✔
1884
    traversal_distance_, boundary().surface_index());
2,144,680✔
1885
}
2,144,680✔
1886

1887
void PhongRay::on_intersection()
823,610✔
1888
{
1889
  // Check if we hit an opaque material or cell
1890
  int hit_id = plot_.color_by_ == PlottableInterface::PlotColorBy::mats
823,610✔
1891
                 ? material()
823,610!
1892
                 : lowest_coord().cell();
×
1893

1894
  // If we are reflected and have advanced beyond the camera,
1895
  // the ray is done. This is checked here because we should
1896
  // kill the ray even if the material is not opaque.
1897
  if (reflected_ && (r() - plot_.camera_position()).dot(u()) >= 0.0) {
823,610!
1898
    stop();
×
1899
    return;
149,400✔
1900
  }
1901

1902
  // Anything that's not opaque has zero impact on the plot.
1903
  if (plot_.opaque_ids_.find(hit_id) == plot_.opaque_ids_.end())
823,610✔
1904
    return;
149,400✔
1905

1906
  if (!reflected_) {
674,210✔
1907
    // reflect the particle and set the color to be colored by
1908
    // the normal or the diffuse lighting contribution
1909
    reflected_ = true;
641,880✔
1910
    result_color_ = plot_.colors_[hit_id];
641,880✔
1911
    Direction to_light = plot_.light_location_ - r();
641,880✔
1912
    to_light /= to_light.norm();
641,880✔
1913

1914
    // TODO
1915
    // Not sure what can cause a surface token to be invalid here, although it
1916
    // sometimes happens for a few pixels. It's very very rare, so proceed by
1917
    // coloring the pixel with the overlap color. It seems to happen only for a
1918
    // few pixels on the outer boundary of a hex lattice.
1919
    //
1920
    // We cannot detect it in the outer loop, and it only matters here, so
1921
    // that's why the error handling is a little different than for a lost
1922
    // ray.
1923
    if (surface() == 0) {
641,880!
1924
      result_color_ = plot_.overlap_color_;
×
1925
      stop();
×
1926
      return;
×
1927
    }
1928

1929
    // Get surface pointer
1930
    const auto& surf = model::surfaces.at(surface_index());
641,880✔
1931

1932
    Direction normal = surf->normal(r_local());
641,880✔
1933
    normal /= normal.norm();
641,880✔
1934

1935
    // Need to apply translations to find the normal vector in
1936
    // the base level universe's coordinate system.
1937
    for (int lev = n_coord() - 2; lev >= 0; --lev) {
641,880!
1938
      if (coord(lev + 1).rotated()) {
×
1939
        const Cell& c {*model::cells[coord(lev).cell()]};
×
1940
        normal = normal.inverse_rotate(c.rotation_);
×
1941
      }
1942
    }
1943

1944
    // use the normal opposed to the ray direction
1945
    if (normal.dot(u()) > 0.0) {
641,880✔
1946
      normal *= -1.0;
57,990✔
1947
    }
1948

1949
    // Facing away from the light means no lighting
1950
    double dotprod = normal.dot(to_light);
641,880✔
1951
    dotprod = std::max(0.0, dotprod);
641,880✔
1952

1953
    double modulation =
641,880✔
1954
      plot_.diffuse_fraction_ + (1.0 - plot_.diffuse_fraction_) * dotprod;
641,880✔
1955
    result_color_ *= modulation;
641,880✔
1956

1957
    // Now point the particle to the camera. We now begin
1958
    // checking to see if it's occluded by another surface
1959
    u() = to_light;
641,880✔
1960

1961
    orig_hit_id_ = hit_id;
641,880✔
1962

1963
    // OpenMC native CSG and DAGMC surfaces have some slight differences
1964
    // in how they interpret particles that are sitting on a surface.
1965
    // I don't know exactly why, but this makes everything work beautifully.
1966
    if (surf->geom_type() == GeometryType::DAG) {
641,880!
1967
      surface() = 0;
×
1968
    } else {
1969
      surface() = -surface(); // go to other side
641,880✔
1970
    }
1971

1972
    // Must fully restart coordinate search. Why? Not sure.
1973
    clear();
641,880✔
1974

1975
    // Note this could likely be faster if we cached the previous
1976
    // cell we were in before the reflection. This is the easiest
1977
    // way to fully initialize all the sub-universe coordinates and
1978
    // directions though.
1979
    bool found = exhaustive_find_cell(*this);
641,880✔
1980
    if (!found) {
641,880!
1981
      fatal_error("Lost particle after reflection.");
×
1982
    }
1983

1984
    // Must recalculate distance to boundary due to the
1985
    // direction change
1986
    compute_distance();
641,880✔
1987

1988
  } else {
1989
    // If it's not facing the light, we color with the diffuse contribution, so
1990
    // next we check if we're going to occlude the last reflected surface. if
1991
    // so, color by the diffuse contribution instead
1992

1993
    if (orig_hit_id_ == -1)
32,330!
1994
      fatal_error("somehow a ray got reflected but not original ID set?");
×
1995

1996
    result_color_ = plot_.colors_[orig_hit_id_];
32,330✔
1997
    result_color_ *= plot_.diffuse_fraction_;
32,330✔
1998
    stop();
32,330✔
1999
  }
2000
}
2001

2002
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
253✔
2003
{
2004

2005
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
253✔
2006
  if (!plt) {
253!
2007
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
2008
    return OPENMC_E_INVALID_ARGUMENT;
×
2009
  }
2010

2011
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
253!
2012
    model::overlap_check_count.resize(model::cells.size());
20✔
2013
  }
2014

2015
  auto ids = plt->get_map<IdData>();
253✔
2016

2017
  // write id data to array
2018
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
253✔
2019

2020
  return 0;
253✔
2021
}
253✔
2022

2023
extern "C" int openmc_property_map(const void* plot, double* data_out)
10✔
2024
{
2025

2026
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
10✔
2027
  if (!plt) {
10!
NEW
2028
    set_errmsg("Invalid slice pointer passed to openmc_property_map");
×
2029
    return OPENMC_E_INVALID_ARGUMENT;
×
2030
  }
2031

2032
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
10!
2033
    model::overlap_check_count.resize(model::cells.size());
×
2034
  }
2035

2036
  auto props = plt->get_map<PropertyData>();
10✔
2037

2038
  // write id data to array
2039
  std::copy(props.data_.begin(), props.data_.end(), data_out);
10✔
2040

2041
  return 0;
10✔
2042
}
10✔
2043

2044
extern "C" int openmc_slice_plot(const double origin[3], const double u_span[3],
100✔
2045
  const double v_span[3], const size_t pixels[2], bool color_overlaps,
2046
  int level, int32_t filter_index, int32_t* geom_data, double* property_data)
2047
{
2048
  // Validate span vectors
2049
  Position u_span_pos {u_span[0], u_span[1], u_span[2]};
100✔
2050
  Position v_span_pos {v_span[0], v_span[1], v_span[2]};
100✔
2051
  double u_norm = u_span_pos.norm();
100✔
2052
  double v_norm = v_span_pos.norm();
100✔
2053
  if (u_norm == 0.0 || v_norm == 0.0) {
100!
NEW
2054
    set_errmsg("Slice span vectors must be non-zero.");
×
NEW
2055
    return OPENMC_E_INVALID_ARGUMENT;
×
2056
  }
2057

2058
  constexpr double ORTHO_REL_TOL = 1e-10;
100✔
2059
  double dot = u_span_pos.dot(v_span_pos);
100✔
2060
  if (std::abs(dot) > ORTHO_REL_TOL * u_norm * v_norm) {
100!
NEW
2061
    set_errmsg("Slice span vectors must be orthogonal.");
×
NEW
2062
    return OPENMC_E_INVALID_ARGUMENT;
×
2063
  }
2064

2065
  // Validate filter index if provided
2066
  if (filter_index >= 0) {
100✔
2067
    if (int err = verify_filter(filter_index))
20!
NEW
2068
      return err;
×
2069
  }
2070

2071
  // Initialize overlap check vector if needed
2072
  if (color_overlaps && model::overlap_check_count.size() == 0) {
100!
2073
    model::overlap_check_count.resize(model::cells.size());
20✔
2074
  }
2075

2076
  try {
2077
    // Create a temporary SlicePlotBase object to reuse get_map logic
2078
    SlicePlotBase plot_params;
100✔
2079
    plot_params.origin_ = Position {origin[0], origin[1], origin[2]};
100✔
2080
    plot_params.u_span_ = u_span_pos;
100✔
2081
    plot_params.v_span_ = v_span_pos;
100✔
2082
    plot_params.width_ = Position {u_norm, v_norm, 0.0};
100✔
2083
    plot_params.basis_ = SlicePlotBase::PlotBasis::xy;
100✔
2084
    plot_params.pixels_[0] = pixels[0];
100✔
2085
    plot_params.pixels_[1] = pixels[1];
100✔
2086
    plot_params.slice_color_overlaps_ = color_overlaps;
100✔
2087
    plot_params.slice_level_ = level;
100✔
2088

2089
    // Use get_map<RasterData> to generate data
2090
    auto data = plot_params.get_map<RasterData>(filter_index);
100✔
2091

2092
    // Copy geometry data
2093
    std::copy(data.id_data_.begin(), data.id_data_.end(), geom_data);
100✔
2094

2095
    // Copy property data if requested
2096
    if (property_data != nullptr) {
100✔
2097
      std::copy(
50✔
2098
        data.property_data_.begin(), data.property_data_.end(), property_data);
2099
    }
2100
  } catch (const std::exception& e) {
100!
NEW
2101
    set_errmsg(e.what());
×
NEW
2102
    return OPENMC_E_UNASSIGNED;
×
NEW
2103
  }
×
2104

2105
  return 0;
100✔
2106
}
2107

2108
extern "C" int openmc_get_plot_index(int32_t id, int32_t* index)
20✔
2109
{
2110
  auto it = model::plot_map.find(id);
20✔
2111
  if (it == model::plot_map.end()) {
20!
2112
    set_errmsg("No plot exists with ID=" + std::to_string(id) + ".");
×
2113
    return OPENMC_E_INVALID_ID;
×
2114
  }
2115

2116
  *index = it->second;
20✔
2117
  return 0;
20✔
2118
}
2119

2120
extern "C" int openmc_plot_get_id(int32_t index, int32_t* id)
50✔
2121
{
2122
  if (index < 0 || index >= model::plots.size()) {
50!
2123
    set_errmsg("Index in plots array is out of bounds.");
×
2124
    return OPENMC_E_OUT_OF_BOUNDS;
×
2125
  }
2126

2127
  *id = model::plots[index]->id();
50✔
2128
  return 0;
50✔
2129
}
2130

2131
extern "C" int openmc_plot_set_id(int32_t index, int32_t id)
×
2132
{
2133
  if (index < 0 || index >= model::plots.size()) {
×
2134
    set_errmsg("Index in plots array is out of bounds.");
×
2135
    return OPENMC_E_OUT_OF_BOUNDS;
×
2136
  }
2137

2138
  if (id < 0 && id != C_NONE) {
×
2139
    set_errmsg("Invalid plot ID.");
×
2140
    return OPENMC_E_INVALID_ARGUMENT;
×
2141
  }
2142

2143
  auto* plot = model::plots[index].get();
×
2144
  int32_t old_id = plot->id();
×
2145
  if (id == old_id)
×
2146
    return 0;
×
2147

2148
  model::plot_map.erase(old_id);
×
2149
  try {
2150
    plot->set_id(id);
×
2151
  } catch (const std::runtime_error& e) {
×
2152
    model::plot_map[old_id] = index;
×
2153
    set_errmsg(e.what());
×
2154
    return OPENMC_E_INVALID_ID;
×
2155
  }
×
2156
  model::plot_map[plot->id()] = index;
×
2157
  return 0;
×
2158
}
2159

2160
extern "C" size_t openmc_plots_size()
20✔
2161
{
2162
  return model::plots.size();
20✔
2163
}
2164

2165
int map_phong_domain_id(
50✔
2166
  const SolidRayTracePlot* plot, int32_t id, int32_t* index_out)
2167
{
2168
  if (!plot || !index_out) {
50!
2169
    set_errmsg("Invalid plot pointer passed to map_phong_domain_id");
×
2170
    return OPENMC_E_INVALID_ARGUMENT;
×
2171
  }
2172

2173
  if (plot->color_by_ == PlottableInterface::PlotColorBy::mats) {
50!
2174
    auto it = model::material_map.find(id);
50✔
2175
    if (it == model::material_map.end()) {
50!
2176
      set_errmsg("Invalid material ID for SolidRayTracePlot");
×
2177
      return OPENMC_E_INVALID_ID;
×
2178
    }
2179
    *index_out = it->second;
50✔
2180
    return 0;
50✔
2181
  }
2182

2183
  if (plot->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2184
    auto it = model::cell_map.find(id);
×
2185
    if (it == model::cell_map.end()) {
×
2186
      set_errmsg("Invalid cell ID for SolidRayTracePlot");
×
2187
      return OPENMC_E_INVALID_ID;
×
2188
    }
2189
    *index_out = it->second;
×
2190
    return 0;
×
2191
  }
2192

2193
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2194
  return OPENMC_E_INVALID_TYPE;
×
2195
}
2196

2197
int get_solidraytrace_plot_by_index(int32_t index, SolidRayTracePlot** plot)
280✔
2198
{
2199
  if (!plot) {
280!
2200
    set_errmsg("Null output pointer passed to get_solidraytrace_plot_by_index");
×
2201
    return OPENMC_E_INVALID_ARGUMENT;
×
2202
  }
2203

2204
  if (index < 0 || index >= model::plots.size()) {
280!
2205
    set_errmsg("Index in plots array is out of bounds.");
×
2206
    return OPENMC_E_OUT_OF_BOUNDS;
×
2207
  }
2208

2209
  auto* plottable = model::plots[index].get();
280✔
2210
  auto* solid_plot = dynamic_cast<SolidRayTracePlot*>(plottable);
280!
2211
  if (!solid_plot) {
280!
2212
    set_errmsg("Plot at index=" + std::to_string(index) +
×
2213
               " is not a solid raytrace plot.");
2214
    return OPENMC_E_INVALID_TYPE;
×
2215
  }
2216

2217
  *plot = solid_plot;
280✔
2218
  return 0;
280✔
2219
}
2220

2221
extern "C" int openmc_solidraytrace_plot_create(int32_t* index)
10✔
2222
{
2223
  if (!index) {
10!
2224
    set_errmsg(
×
2225
      "Null output pointer passed to openmc_solidraytrace_plot_create");
2226
    return OPENMC_E_INVALID_ARGUMENT;
×
2227
  }
2228

2229
  try {
2230
    auto new_plot = std::make_unique<SolidRayTracePlot>();
10✔
2231
    new_plot->set_id();
10✔
2232
    int32_t new_plot_id = new_plot->id();
10✔
2233
#ifdef USE_LIBPNG
2234
    new_plot->path_plot() = fmt::format("plot_{}.png", new_plot_id);
10✔
2235
#else
2236
    new_plot->path_plot() = fmt::format("plot_{}.ppm", new_plot_id);
2237
#endif
2238
    int32_t new_plot_index = model::plots.size();
10✔
2239
    model::plots.emplace_back(std::move(new_plot));
10✔
2240
    model::plot_map[new_plot_id] = new_plot_index;
10✔
2241
    *index = new_plot_index;
10✔
2242
  } catch (const std::exception& e) {
10!
2243
    set_errmsg(e.what());
×
2244
    return OPENMC_E_ALLOCATE;
×
2245
  }
×
2246

2247
  return 0;
10✔
2248
}
2249

2250
extern "C" int openmc_solidraytrace_plot_get_pixels(
30✔
2251
  int32_t index, int32_t* width, int32_t* height)
2252
{
2253
  if (!width || !height) {
30!
2254
    set_errmsg(
×
2255
      "Invalid arguments passed to openmc_solidraytrace_plot_get_pixels");
2256
    return OPENMC_E_INVALID_ARGUMENT;
×
2257
  }
2258

2259
  SolidRayTracePlot* plt = nullptr;
30✔
2260
  int err = get_solidraytrace_plot_by_index(index, &plt);
30✔
2261
  if (err)
30!
2262
    return err;
×
2263

2264
  *width = plt->pixels()[0];
30✔
2265
  *height = plt->pixels()[1];
30✔
2266
  return 0;
30✔
2267
}
2268

2269
extern "C" int openmc_solidraytrace_plot_set_pixels(
10✔
2270
  int32_t index, int32_t width, int32_t height)
2271
{
2272
  if (width <= 0 || height <= 0) {
10!
2273
    set_errmsg(
×
2274
      "Invalid arguments passed to openmc_solidraytrace_plot_set_pixels");
2275
    return OPENMC_E_INVALID_ARGUMENT;
×
2276
  }
2277

2278
  SolidRayTracePlot* plt = nullptr;
10✔
2279
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2280
  if (err)
10!
2281
    return err;
×
2282

2283
  plt->pixels()[0] = width;
10✔
2284
  plt->pixels()[1] = height;
10✔
2285
  return 0;
10✔
2286
}
2287

2288
extern "C" int openmc_solidraytrace_plot_get_color_by(
10✔
2289
  int32_t index, int32_t* color_by)
2290
{
2291
  if (!color_by) {
10!
2292
    set_errmsg(
×
2293
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color_by");
2294
    return OPENMC_E_INVALID_ARGUMENT;
×
2295
  }
2296

2297
  SolidRayTracePlot* plt = nullptr;
10✔
2298
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2299
  if (err)
10!
2300
    return err;
×
2301

2302
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
10!
2303
    *color_by = 0;
10✔
2304
  } else if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2305
    *color_by = 1;
×
2306
  } else {
2307
    set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2308
    return OPENMC_E_INVALID_TYPE;
×
2309
  }
2310

2311
  return 0;
10✔
2312
}
2313

2314
extern "C" int openmc_solidraytrace_plot_set_color_by(
10✔
2315
  int32_t index, int32_t color_by)
2316
{
2317
  SolidRayTracePlot* plt = nullptr;
10✔
2318
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2319
  if (err)
10!
2320
    return err;
×
2321

2322
  if (color_by == 0) {
10!
2323
    plt->color_by_ = PlottableInterface::PlotColorBy::mats;
10✔
2324
  } else if (color_by == 1) {
×
2325
    plt->color_by_ = PlottableInterface::PlotColorBy::cells;
×
2326
  } else {
2327
    set_errmsg("Invalid color_by value for SolidRayTracePlot");
×
2328
    return OPENMC_E_INVALID_ARGUMENT;
×
2329
  }
2330

2331
  return 0;
10✔
2332
}
2333

2334
extern "C" int openmc_solidraytrace_plot_set_default_colors(int32_t index)
10✔
2335
{
2336
  SolidRayTracePlot* plt = nullptr;
10✔
2337
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2338
  if (err)
10!
2339
    return err;
×
2340

2341
  plt->set_default_colors();
10✔
2342
  return 0;
10✔
2343
}
2344

2345
extern "C" int openmc_solidraytrace_plot_set_all_opaque(int32_t index)
×
2346
{
2347
  SolidRayTracePlot* plt = nullptr;
×
2348
  int err = get_solidraytrace_plot_by_index(index, &plt);
×
2349
  if (err)
×
2350
    return err;
×
2351

2352
  plt->opaque_ids().clear();
×
2353
  if (plt->color_by_ == PlottableInterface::PlotColorBy::mats) {
×
2354
    for (int32_t i = 0; i < model::materials.size(); ++i) {
×
2355
      plt->opaque_ids().insert(i);
×
2356
    }
2357
    return 0;
×
2358
  }
2359

2360
  if (plt->color_by_ == PlottableInterface::PlotColorBy::cells) {
×
2361
    for (int32_t i = 0; i < model::cells.size(); ++i) {
×
2362
      plt->opaque_ids().insert(i);
×
2363
    }
2364
    return 0;
×
2365
  }
2366

2367
  set_errmsg("Unsupported color_by for SolidRayTracePlot");
×
2368
  return OPENMC_E_INVALID_TYPE;
×
2369
}
2370

2371
extern "C" int openmc_solidraytrace_plot_set_opaque(
20✔
2372
  int32_t index, int32_t id, bool visible)
2373
{
2374
  SolidRayTracePlot* plt = nullptr;
20✔
2375
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2376
  if (err)
20!
2377
    return err;
×
2378

2379
  int32_t domain_index = -1;
20✔
2380
  err = map_phong_domain_id(plt, id, &domain_index);
20✔
2381
  if (err)
20!
2382
    return err;
×
2383

2384
  if (visible) {
20✔
2385
    plt->opaque_ids().insert(domain_index);
10✔
2386
  } else {
2387
    plt->opaque_ids().erase(domain_index);
10✔
2388
  }
2389

2390
  return 0;
20✔
2391
}
2392

2393
extern "C" int openmc_solidraytrace_plot_set_color(
20✔
2394
  int32_t index, int32_t id, uint8_t r, uint8_t g, uint8_t b)
2395
{
2396
  SolidRayTracePlot* plt = nullptr;
20✔
2397
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2398
  if (err)
20!
2399
    return err;
×
2400

2401
  int32_t domain_index = -1;
20✔
2402
  err = map_phong_domain_id(plt, id, &domain_index);
20✔
2403
  if (err)
20!
2404
    return err;
×
2405

2406
  if (domain_index < 0 ||
40!
2407
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
20✔
2408
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
2409
    return OPENMC_E_OUT_OF_BOUNDS;
×
2410
  }
2411

2412
  plt->colors_[domain_index] = RGBColor(r, g, b);
20✔
2413
  return 0;
20✔
2414
}
2415

2416
extern "C" int openmc_solidraytrace_plot_get_camera_position(
10✔
2417
  int32_t index, double* x, double* y, double* z)
2418
{
2419
  if (!x || !y || !z) {
10!
2420
    set_errmsg("Invalid arguments passed to "
×
2421
               "openmc_solidraytrace_plot_get_camera_position");
2422
    return OPENMC_E_INVALID_ARGUMENT;
×
2423
  }
2424

2425
  SolidRayTracePlot* plt = nullptr;
10✔
2426
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2427
  if (err)
10!
2428
    return err;
×
2429

2430
  const auto& camera_position = plt->camera_position();
10✔
2431
  *x = camera_position.x;
10✔
2432
  *y = camera_position.y;
10✔
2433
  *z = camera_position.z;
10✔
2434
  return 0;
10✔
2435
}
2436

2437
extern "C" int openmc_solidraytrace_plot_set_camera_position(
10✔
2438
  int32_t index, double x, double y, double z)
2439
{
2440
  SolidRayTracePlot* plt = nullptr;
10✔
2441
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2442
  if (err)
10!
2443
    return err;
×
2444

2445
  plt->camera_position() = {x, y, z};
10✔
2446
  return 0;
10✔
2447
}
2448

2449
extern "C" int openmc_solidraytrace_plot_get_look_at(
10✔
2450
  int32_t index, double* x, double* y, double* z)
2451
{
2452
  if (!x || !y || !z) {
10!
2453
    set_errmsg(
×
2454
      "Invalid arguments passed to openmc_solidraytrace_plot_get_look_at");
2455
    return OPENMC_E_INVALID_ARGUMENT;
×
2456
  }
2457

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

2463
  const auto& look_at = plt->look_at();
10✔
2464
  *x = look_at.x;
10✔
2465
  *y = look_at.y;
10✔
2466
  *z = look_at.z;
10✔
2467
  return 0;
10✔
2468
}
2469

2470
extern "C" int openmc_solidraytrace_plot_set_look_at(
10✔
2471
  int32_t index, double x, double y, double z)
2472
{
2473
  SolidRayTracePlot* plt = nullptr;
10✔
2474
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2475
  if (err)
10!
2476
    return err;
×
2477

2478
  plt->look_at() = {x, y, z};
10✔
2479
  return 0;
10✔
2480
}
2481

2482
extern "C" int openmc_solidraytrace_plot_get_up(
10✔
2483
  int32_t index, double* x, double* y, double* z)
2484
{
2485
  if (!x || !y || !z) {
10!
2486
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_up");
×
2487
    return OPENMC_E_INVALID_ARGUMENT;
×
2488
  }
2489

2490
  SolidRayTracePlot* plt = nullptr;
10✔
2491
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2492
  if (err)
10!
2493
    return err;
×
2494

2495
  const auto& up = plt->up();
10✔
2496
  *x = up.x;
10✔
2497
  *y = up.y;
10✔
2498
  *z = up.z;
10✔
2499
  return 0;
10✔
2500
}
2501

2502
extern "C" int openmc_solidraytrace_plot_set_up(
10✔
2503
  int32_t index, double x, double y, double z)
2504
{
2505
  SolidRayTracePlot* plt = nullptr;
10✔
2506
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2507
  if (err)
10!
2508
    return err;
×
2509

2510
  plt->up() = {x, y, z};
10✔
2511
  return 0;
10✔
2512
}
2513

2514
extern "C" int openmc_solidraytrace_plot_get_light_position(
10✔
2515
  int32_t index, double* x, double* y, double* z)
2516
{
2517
  if (!x || !y || !z) {
10!
2518
    set_errmsg("Invalid arguments passed to "
×
2519
               "openmc_solidraytrace_plot_get_light_position");
2520
    return OPENMC_E_INVALID_ARGUMENT;
×
2521
  }
2522

2523
  SolidRayTracePlot* plt = nullptr;
10✔
2524
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2525
  if (err)
10!
2526
    return err;
×
2527

2528
  const auto& light_position = plt->light_location();
10✔
2529
  *x = light_position.x;
10✔
2530
  *y = light_position.y;
10✔
2531
  *z = light_position.z;
10✔
2532
  return 0;
10✔
2533
}
2534

2535
extern "C" int openmc_solidraytrace_plot_set_light_position(
10✔
2536
  int32_t index, double x, double y, double z)
2537
{
2538
  SolidRayTracePlot* plt = nullptr;
10✔
2539
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2540
  if (err)
10!
2541
    return err;
×
2542

2543
  plt->light_location() = {x, y, z};
10✔
2544
  return 0;
10✔
2545
}
2546

2547
extern "C" int openmc_solidraytrace_plot_get_fov(int32_t index, double* fov)
10✔
2548
{
2549
  if (!fov) {
10!
2550
    set_errmsg("Invalid arguments passed to openmc_solidraytrace_plot_get_fov");
×
2551
    return OPENMC_E_INVALID_ARGUMENT;
×
2552
  }
2553

2554
  SolidRayTracePlot* plt = nullptr;
10✔
2555
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2556
  if (err)
10!
2557
    return err;
×
2558

2559
  *fov = plt->horizontal_field_of_view();
10✔
2560
  return 0;
10✔
2561
}
2562

2563
extern "C" int openmc_solidraytrace_plot_set_fov(int32_t index, double fov)
10✔
2564
{
2565
  SolidRayTracePlot* plt = nullptr;
10✔
2566
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2567
  if (err)
10!
2568
    return err;
×
2569

2570
  plt->horizontal_field_of_view() = fov;
10✔
2571
  return 0;
10✔
2572
}
2573

2574
extern "C" int openmc_solidraytrace_plot_update_view(int32_t index)
20✔
2575
{
2576
  SolidRayTracePlot* plt = nullptr;
20✔
2577
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2578
  if (err)
20!
2579
    return err;
×
2580

2581
  plt->update_view();
20✔
2582
  return 0;
20✔
2583
}
2584

2585
extern "C" int openmc_solidraytrace_plot_create_image(
20✔
2586
  int32_t index, uint8_t* data_out, int32_t width, int32_t height)
2587
{
2588
  if (!data_out || width <= 0 || height <= 0) {
20!
2589
    set_errmsg(
×
2590
      "Invalid arguments passed to openmc_solidraytrace_plot_create_image");
2591
    return OPENMC_E_INVALID_ARGUMENT;
×
2592
  }
2593

2594
  SolidRayTracePlot* plt = nullptr;
20✔
2595
  int err = get_solidraytrace_plot_by_index(index, &plt);
20✔
2596
  if (err)
20!
2597
    return err;
×
2598

2599
  if (plt->pixels()[0] != width || plt->pixels()[1] != height) {
20!
2600
    set_errmsg(
×
2601
      "Requested image size does not match SolidRayTracePlot pixel settings");
2602
    return OPENMC_E_INVALID_SIZE;
×
2603
  }
2604

2605
  ImageData data = plt->create_image();
20✔
2606
  if (static_cast<int32_t>(data.shape()[0]) != width ||
40!
2607
      static_cast<int32_t>(data.shape()[1]) != height) {
20!
2608
    set_errmsg("Unexpected image size from SolidRayTracePlot create_image");
×
2609
    return OPENMC_E_INVALID_SIZE;
×
2610
  }
2611

2612
  for (int32_t y = 0; y < height; ++y) {
140✔
2613
    for (int32_t x = 0; x < width; ++x) {
1,080✔
2614
      const auto& color = data(x, y);
960✔
2615
      size_t idx = (static_cast<size_t>(y) * width + x) * 3;
960✔
2616
      data_out[idx + 0] = color.red;
960✔
2617
      data_out[idx + 1] = color.green;
960✔
2618
      data_out[idx + 2] = color.blue;
960✔
2619
    }
2620
  }
2621

2622
  return 0;
20✔
2623
}
20✔
2624

2625
extern "C" int openmc_solidraytrace_plot_get_color(
10✔
2626
  int32_t index, int32_t id, uint8_t* r, uint8_t* g, uint8_t* b)
2627
{
2628
  if (!r || !g || !b) {
10!
2629
    set_errmsg(
×
2630
      "Invalid arguments passed to openmc_solidraytrace_plot_get_color");
2631
    return OPENMC_E_INVALID_ARGUMENT;
×
2632
  }
2633

2634
  SolidRayTracePlot* plt = nullptr;
10✔
2635
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2636
  if (err)
10!
2637
    return err;
×
2638

2639
  int32_t domain_index = -1;
10✔
2640
  err = map_phong_domain_id(plt, id, &domain_index);
10✔
2641
  if (err)
10!
2642
    return err;
×
2643

2644
  if (domain_index < 0 ||
20!
2645
      static_cast<size_t>(domain_index) >= plt->colors_.size()) {
10✔
2646
    set_errmsg("Color index out of range for SolidRayTracePlot");
×
2647
    return OPENMC_E_OUT_OF_BOUNDS;
×
2648
  }
2649

2650
  const auto& color = plt->colors_[domain_index];
10✔
2651
  *r = color.red;
10✔
2652
  *g = color.green;
10✔
2653
  *b = color.blue;
10✔
2654
  return 0;
10✔
2655
}
2656

2657
extern "C" int openmc_solidraytrace_plot_get_diffuse_fraction(
10✔
2658
  int32_t index, double* diffuse_fraction)
2659
{
2660
  if (!diffuse_fraction) {
10!
2661
    set_errmsg("Invalid arguments passed to "
×
2662
               "openmc_solidraytrace_plot_get_diffuse_fraction");
2663
    return OPENMC_E_INVALID_ARGUMENT;
×
2664
  }
2665

2666
  SolidRayTracePlot* plt = nullptr;
10✔
2667
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2668
  if (err)
10!
2669
    return err;
×
2670

2671
  *diffuse_fraction = plt->diffuse_fraction();
10✔
2672
  return 0;
10✔
2673
}
2674

2675
extern "C" int openmc_solidraytrace_plot_set_diffuse_fraction(
10✔
2676
  int32_t index, double diffuse_fraction)
2677
{
2678
  SolidRayTracePlot* plt = nullptr;
10✔
2679
  int err = get_solidraytrace_plot_by_index(index, &plt);
10✔
2680
  if (err)
10!
2681
    return err;
×
2682

2683
  if (diffuse_fraction < 0.0 || diffuse_fraction > 1.0) {
10!
2684
    set_errmsg("Diffuse fraction must be between 0 and 1");
×
2685
    return OPENMC_E_INVALID_ARGUMENT;
×
2686
  }
2687

2688
  plt->diffuse_fraction() = diffuse_fraction;
10✔
2689
  return 0;
10✔
2690
}
2691

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