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

openmc-dev / openmc / 12385490469

18 Dec 2024 02:58AM UTC coverage: 82.673% (-2.2%) from 84.823%
12385490469

Pull #3087

github

web-flow
Merge 3317476f5 into 775c41512
Pull Request #3087: wheel building with scikit build core

106679 of 129038 relevant lines covered (82.67%)

12084526.77 hits per line

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

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

3
#include <algorithm>
4
#include <cstdio>
5
#include <fstream>
6
#include <sstream>
7

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

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

35
namespace openmc {
36

37
//==============================================================================
38
// Constants
39
//==============================================================================
40

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

45
IdData::IdData(size_t h_res, size_t v_res) : data_({v_res, h_res, 3}, NOT_FOUND)
5,448✔
46
{}
5,448✔
47

48
void IdData::set_value(size_t y, size_t x, const GeometryState& p, int level)
38,727,600✔
49
{
50
  // set cell data
51
  if (p.n_coord() <= level) {
38,727,600✔
52
    data_(y, x, 0) = NOT_FOUND;
×
53
    data_(y, x, 1) = NOT_FOUND;
×
54
  } else {
55
    data_(y, x, 0) = model::cells.at(p.coord(level).cell)->id_;
38,727,600✔
56
    data_(y, x, 1) = level == p.n_coord() - 1
77,455,200✔
57
                       ? p.cell_instance()
38,727,600✔
58
                       : cell_instance_at_level(p, level);
×
59
  }
60

61
  // set material data
62
  Cell* c = model::cells.at(p.lowest_coord().cell).get();
38,727,600✔
63
  if (p.material() == MATERIAL_VOID) {
38,727,600✔
64
    data_(y, x, 2) = MATERIAL_VOID;
32,044,128✔
65
    return;
32,044,128✔
66
  } else if (c->type_ == Fill::MATERIAL) {
6,683,472✔
67
    Material* m = model::materials.at(p.material()).get();
6,683,472✔
68
    data_(y, x, 2) = m->id_;
6,683,472✔
69
  }
70
}
71

72
void IdData::set_overlap(size_t y, size_t x)
30,816✔
73
{
74
  xt::view(data_, y, x, xt::all()) = OVERLAP;
30,816✔
75
}
30,816✔
76

77
PropertyData::PropertyData(size_t h_res, size_t v_res)
×
78
  : data_({v_res, h_res, 2}, NOT_FOUND)
×
79
{}
×
80

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

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

97
//==============================================================================
98
// Global variables
99
//==============================================================================
100

101
namespace model {
102

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

107
} // namespace model
108

109
//==============================================================================
110
// RUN_PLOT controls the logic for making one or many plots
111
//==============================================================================
112

113
extern "C" int openmc_plot_geometry()
276✔
114
{
115

116
  for (auto& pl : model::plots) {
684✔
117
    write_message(5, "Processing plot {}: {}...", pl->id(), pl->path_plot());
408✔
118
    pl->create_output();
408✔
119
  }
120

121
  return 0;
276✔
122
}
123

124
void Plot::create_output() const
348✔
125
{
126
  if (PlotType::slice == type_) {
348✔
127
    // create 2D image
128
    create_image();
288✔
129
  } else if (PlotType::voxel == type_) {
60✔
130
    // create voxel file for 3D viewing
131
    create_voxel();
60✔
132
  }
133
}
348✔
134

135
void Plot::print_info() const
348✔
136
{
137
  // Plot type
138
  if (PlotType::slice == type_) {
348✔
139
    fmt::print("Plot Type: Slice\n");
288✔
140
  } else if (PlotType::voxel == type_) {
60✔
141
    fmt::print("Plot Type: Voxel\n");
60✔
142
  }
143

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

147
  if (PlotType::slice == type_) {
348✔
148
    fmt::print("Width: {:4} {:4}\n", width_[0], width_[1]);
576✔
149
  } else if (PlotType::voxel == type_) {
60✔
150
    fmt::print("Width: {:4} {:4} {:4}\n", width_[0], width_[1], width_[2]);
120✔
151
  }
152

153
  if (PlotColorBy::cells == color_by_) {
348✔
154
    fmt::print("Coloring: Cells\n");
240✔
155
  } else if (PlotColorBy::mats == color_by_) {
108✔
156
    fmt::print("Coloring: Materials\n");
108✔
157
  }
158

159
  if (PlotType::slice == type_) {
348✔
160
    switch (basis_) {
288✔
161
    case PlotBasis::xy:
168✔
162
      fmt::print("Basis: XY\n");
140✔
163
      break;
168✔
164
    case PlotBasis::xz:
60✔
165
      fmt::print("Basis: XZ\n");
50✔
166
      break;
60✔
167
    case PlotBasis::yz:
60✔
168
      fmt::print("Basis: YZ\n");
50✔
169
      break;
60✔
170
    }
171
    fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
576✔
172
  } else if (PlotType::voxel == type_) {
60✔
173
    fmt::print("Voxels: {} {} {}\n", pixels_[0], pixels_[1], pixels_[2]);
120✔
174
  }
175
}
348✔
176

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

187
  write_message("Reading plot XML file...", 5);
926✔
188

189
  // Parse plots.xml file
190
  pugi::xml_document doc;
926✔
191
  doc.load_file(filename.c_str());
926✔
192

193
  pugi::xml_node root = doc.document_element();
926✔
194

195
  read_plots_xml(root);
926✔
196
}
926✔
197

198
void read_plots_xml(pugi::xml_node root)
1,337✔
199
{
200
  for (auto node : root.children("plot")) {
2,027✔
201
    std::string id_string = get_node_value(node, "id", true);
700✔
202
    int id = std::stoi(id_string);
700✔
203
    if (check_for_node(node, "type")) {
700✔
204
      std::string type_str = get_node_value(node, "type", true);
700✔
205
      if (type_str == "slice")
700✔
206
        model::plots.emplace_back(
570✔
207
          std::make_unique<Plot>(node, Plot::PlotType::slice));
1,150✔
208
      else if (type_str == "voxel")
120✔
209
        model::plots.emplace_back(
60✔
210
          std::make_unique<Plot>(node, Plot::PlotType::voxel));
120✔
211
      else if (type_str == "projection")
60✔
212
        model::plots.emplace_back(std::make_unique<ProjectionPlot>(node));
60✔
213
      else
214
        fatal_error(
×
215
          fmt::format("Unsupported plot type '{}' in plot {}", type_str, id));
×
216

217
      model::plot_map[model::plots.back()->id()] = model::plots.size() - 1;
690✔
218
    } else {
690✔
219
      fatal_error(fmt::format("Must specify plot type in plot {}", id));
×
220
    }
221
  }
690✔
222
}
1,327✔
223

224
void free_memory_plot()
5,357✔
225
{
226
  model::plots.clear();
5,357✔
227
  model::plot_map.clear();
5,357✔
228
}
5,357✔
229

230
// creates an image based on user input from a plots.xml <plot>
231
// specification in the PNG/PPM format
232
void Plot::create_image() const
288✔
233
{
234

235
  size_t width = pixels_[0];
288✔
236
  size_t height = pixels_[1];
288✔
237

238
  ImageData data({width, height}, not_found_);
288✔
239

240
  // generate ids for the plot
241
  auto ids = get_map<IdData>();
288✔
242

243
  // assign colors
244
  for (size_t y = 0; y < height; y++) {
41,952✔
245
    for (size_t x = 0; x < width; x++) {
9,049,608✔
246
      int idx = color_by_ == PlotColorBy::cells ? 0 : 2;
9,007,944✔
247
      auto id = ids.data_(y, x, idx);
9,007,944✔
248
      // no setting needed if not found
249
      if (id == NOT_FOUND) {
9,007,944✔
250
        continue;
1,800,552✔
251
      }
252
      if (id == OVERLAP) {
7,238,208✔
253
        data(x, y) = overlap_color_;
30,816✔
254
        continue;
30,816✔
255
      }
256
      if (PlotColorBy::cells == color_by_) {
7,207,392✔
257
        data(x, y) = colors_[model::cell_map[id]];
4,469,712✔
258
      } else if (PlotColorBy::mats == color_by_) {
2,737,680✔
259
        if (id == MATERIAL_VOID) {
2,737,680✔
260
          data(x, y) = WHITE;
×
261
          continue;
×
262
        }
263
        data(x, y) = colors_[model::material_map[id]];
2,737,680✔
264
      } // color_by if-else
265
    }   // x for loop
266
  }     // y for loop
267

268
  // draw mesh lines if present
269
  if (index_meshlines_mesh_ >= 0) {
288✔
270
    draw_mesh_lines(data);
36✔
271
  }
272

273
// create image file
274
#ifdef USE_LIBPNG
275
  output_png(path_plot(), data);
288✔
276
#else
277
  output_ppm(path_plot(), data);
278
#endif
279
}
288✔
280

281
void PlottableInterface::set_id(pugi::xml_node plot_node)
700✔
282
{
283
  // Copy data into plots
284
  if (check_for_node(plot_node, "id")) {
700✔
285
    id_ = std::stoi(get_node_value(plot_node, "id"));
700✔
286
  } else {
287
    fatal_error("Must specify plot id in plots XML file.");
×
288
  }
289

290
  // Check to make sure 'id' hasn't been used
291
  if (model::plot_map.find(id_) != model::plot_map.end()) {
700✔
292
    fatal_error(
×
293
      fmt::format("Two or more plots use the same unique ID: {}", id_));
×
294
  }
295
}
700✔
296

297
// Checks if png or ppm is already present
298
bool file_extension_present(
690✔
299
  const std::string& filename, const std::string& extension)
300
{
301
  std::string file_extension_if_present =
302
    filename.substr(filename.find_last_of(".") + 1);
690✔
303
  if (file_extension_if_present == extension)
690✔
304
    return true;
24✔
305
  return false;
666✔
306
}
690✔
307

308
void Plot::set_output_path(pugi::xml_node plot_node)
640✔
309
{
310
  // Set output file path
311
  std::string filename;
640✔
312

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

340
  path_plot_ = filename;
630✔
341

342
  // Copy plot pixel size
343
  vector<int> pxls = get_node_array<int>(plot_node, "pixels");
1,260✔
344
  if (PlotType::slice == type_) {
630✔
345
    if (pxls.size() == 2) {
570✔
346
      pixels_[0] = pxls[0];
570✔
347
      pixels_[1] = pxls[1];
570✔
348
    } else {
349
      fatal_error(
×
350
        fmt::format("<pixels> must be length 2 in slice plot {}", id()));
×
351
    }
352
  } else if (PlotType::voxel == type_) {
60✔
353
    if (pxls.size() == 3) {
60✔
354
      pixels_[0] = pxls[0];
60✔
355
      pixels_[1] = pxls[1];
60✔
356
      pixels_[2] = pxls[2];
60✔
357
    } else {
358
      fatal_error(
×
359
        fmt::format("<pixels> must be length 3 in voxel plot {}", id()));
×
360
    }
361
  }
362
}
630✔
363

364
void PlottableInterface::set_bg_color(pugi::xml_node plot_node)
700✔
365
{
366
  // Copy plot background color
367
  if (check_for_node(plot_node, "background")) {
700✔
368
    vector<int> bg_rgb = get_node_array<int>(plot_node, "background");
48✔
369
    if (bg_rgb.size() == 3) {
48✔
370
      not_found_ = bg_rgb;
48✔
371
    } else {
372
      fatal_error(fmt::format("Bad background RGB in plot {}", id()));
×
373
    }
374
  }
48✔
375
}
700✔
376

377
void Plot::set_basis(pugi::xml_node plot_node)
630✔
378
{
379
  // Copy plot basis
380
  if (PlotType::slice == type_) {
630✔
381
    std::string pl_basis = "xy";
570✔
382
    if (check_for_node(plot_node, "basis")) {
570✔
383
      pl_basis = get_node_value(plot_node, "basis", true);
570✔
384
    }
385
    if ("xy" == pl_basis) {
570✔
386
      basis_ = PlotBasis::xy;
416✔
387
    } else if ("xz" == pl_basis) {
154✔
388
      basis_ = PlotBasis::xz;
60✔
389
    } else if ("yz" == pl_basis) {
94✔
390
      basis_ = PlotBasis::yz;
94✔
391
    } else {
392
      fatal_error(
×
393
        fmt::format("Unsupported plot basis '{}' in plot {}", pl_basis, id()));
×
394
    }
395
  }
570✔
396
}
630✔
397

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

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

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

445
void PlottableInterface::set_default_colors(pugi::xml_node plot_node)
700✔
446
{
447
  // Copy plot color type and initialize all colors randomly
448
  std::string pl_color_by = "cell";
700✔
449
  if (check_for_node(plot_node, "color_by")) {
700✔
450
    pl_color_by = get_node_value(plot_node, "color_by", true);
664✔
451
  }
452
  if ("cell" == pl_color_by) {
700✔
453
    color_by_ = PlotColorBy::cells;
351✔
454
    colors_.resize(model::cells.size());
351✔
455
  } else if ("material" == pl_color_by) {
349✔
456
    color_by_ = PlotColorBy::mats;
349✔
457
    colors_.resize(model::materials.size());
349✔
458
  } else {
459
    fatal_error(fmt::format(
×
460
      "Unsupported plot color type '{}' in plot {}", pl_color_by, id()));
×
461
  }
462

463
  for (auto& c : colors_) {
3,007✔
464
    c = random_color();
2,307✔
465
    // make sure we don't interfere with some default colors
466
    while (c == RED || c == WHITE) {
2,307✔
467
      c = random_color();
×
468
    }
469
  }
470
}
700✔
471

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

509
void Plot::set_meshlines(pugi::xml_node plot_node)
630✔
510
{
511
  // Deal with meshlines
512
  pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines");
630✔
513

514
  if (!mesh_line_nodes.empty()) {
630✔
515
    if (PlotType::voxel == type_) {
36✔
516
      warning(fmt::format("Meshlines ignored in voxel plot {}", id()));
×
517
    }
518

519
    if (mesh_line_nodes.size() == 1) {
36✔
520
      // Get first meshline node
521
      pugi::xml_node meshlines_node = mesh_line_nodes[0].node();
36✔
522

523
      // Check mesh type
524
      std::string meshtype;
36✔
525
      if (check_for_node(meshlines_node, "meshtype")) {
36✔
526
        meshtype = get_node_value(meshlines_node, "meshtype");
36✔
527
      } else {
528
        fatal_error(fmt::format(
×
529
          "Must specify a meshtype for meshlines specification in plot {}",
530
          id()));
×
531
      }
532

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

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

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

617
void PlottableInterface::set_mask(pugi::xml_node plot_node)
700✔
618
{
619
  // Deal with masks
620
  pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask");
700✔
621

622
  if (!mask_nodes.empty()) {
700✔
623
    if (mask_nodes.size() == 1) {
36✔
624
      // Get pointer to mask
625
      pugi::xml_node mask_node = mask_nodes[0].node();
36✔
626

627
      // Determine how many components there are and allocate
628
      vector<int> iarray = get_node_array<int>(mask_node, "components");
36✔
629
      if (iarray.size() == 0) {
36✔
630
        fatal_error(
×
631
          fmt::format("Missing <components> in mask of plot {}", id()));
×
632
      }
633

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

656
      // Alter colors based on mask information
657
      for (int j = 0; j < colors_.size(); j++) {
144✔
658
        if (contains(iarray, j)) {
108✔
659
          if (check_for_node(mask_node, "background")) {
72✔
660
            vector<int> bg_rgb = get_node_array<int>(mask_node, "background");
72✔
661
            colors_[j] = bg_rgb;
72✔
662
          } else {
72✔
663
            colors_[j] = WHITE;
×
664
          }
665
        }
666
      }
667

668
    } else {
36✔
669
      fatal_error(fmt::format("Mutliple masks specified in plot {}", id()));
×
670
    }
671
  }
672
}
700✔
673

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

695
  // make sure we allocate the vector for counting overlap checks if
696
  // they're going to be plotted
697
  if (color_overlaps_ && settings::run_mode == RunMode::PLOTTING) {
700✔
698
    settings::check_overlaps = true;
24✔
699
    model::overlap_check_count.resize(model::cells.size(), 0);
24✔
700
  }
701
}
700✔
702

703
PlottableInterface::PlottableInterface(pugi::xml_node plot_node)
700✔
704
{
705
  set_id(plot_node);
700✔
706
  set_bg_color(plot_node);
700✔
707
  set_universe(plot_node);
700✔
708
  set_default_colors(plot_node);
700✔
709
  set_user_colors(plot_node);
700✔
710
  set_mask(plot_node);
700✔
711
  set_overlap_color(plot_node);
700✔
712
}
700✔
713

714
Plot::Plot(pugi::xml_node plot_node, PlotType type)
640✔
715
  : PlottableInterface(plot_node), type_(type), index_meshlines_mesh_ {-1}
640✔
716
{
717
  set_output_path(plot_node);
640✔
718
  set_basis(plot_node);
630✔
719
  set_origin(plot_node);
630✔
720
  set_width(plot_node);
630✔
721
  set_meshlines(plot_node);
630✔
722
  slice_level_ = level_; // Copy level employed in SlicePlotBase::get_map
630✔
723
  slice_color_overlaps_ = color_overlaps_;
630✔
724
}
630✔
725

726
//==============================================================================
727
// OUTPUT_PPM writes out a previously generated image to a PPM file
728
//==============================================================================
729

730
void output_ppm(const std::string& filename, const ImageData& data)
×
731
{
732
  // Open PPM file for writing
733
  std::string fname = filename;
×
734
  fname = strtrim(fname);
×
735
  std::ofstream of;
×
736

737
  of.open(fname);
×
738

739
  // Write header
740
  of << "P6\n";
×
741
  of << data.shape()[0] << " " << data.shape()[1] << "\n";
×
742
  of << "255\n";
×
743
  of.close();
×
744

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

756
//==============================================================================
757
// OUTPUT_PNG writes out a previously generated image to a PNG file
758
//==============================================================================
759

760
#ifdef USE_LIBPNG
761
void output_png(const std::string& filename, const ImageData& data)
348✔
762
{
763
  // Open PNG file for writing
764
  std::string fname = filename;
348✔
765
  fname = strtrim(fname);
348✔
766
  auto fp = std::fopen(fname.c_str(), "wb");
348✔
767

768
  // Initialize write and info structures
769
  auto png_ptr =
770
    png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
348✔
771
  auto info_ptr = png_create_info_struct(png_ptr);
348✔
772

773
  // Setup exception handling
774
  if (setjmp(png_jmpbuf(png_ptr)))
348✔
775
    fatal_error("Error during png creation");
×
776

777
  png_init_io(png_ptr, fp);
348✔
778

779
  // Write header (8 bit colour depth)
780
  int width = data.shape()[0];
348✔
781
  int height = data.shape()[1];
348✔
782
  png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
348✔
783
    PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
784
  png_write_info(png_ptr, info_ptr);
348✔
785

786
  // Allocate memory for one row (3 bytes per pixel - RGB)
787
  std::vector<png_byte> row(3 * width);
348✔
788

789
  // Write color for each pixel
790
  for (int y = 0; y < height; y++) {
54,012✔
791
    for (int x = 0; x < width; x++) {
11,461,608✔
792
      RGBColor rgb = data(x, y);
11,407,944✔
793
      row[3 * x] = rgb.red;
11,407,944✔
794
      row[3 * x + 1] = rgb.green;
11,407,944✔
795
      row[3 * x + 2] = rgb.blue;
11,407,944✔
796
    }
797
    png_write_row(png_ptr, row.data());
53,664✔
798
  }
799

800
  // End write
801
  png_write_end(png_ptr, nullptr);
348✔
802

803
  // Clean up data structures
804
  std::fclose(fp);
348✔
805
  png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
348✔
806
  png_destroy_write_struct(&png_ptr, &info_ptr);
348✔
807
}
348✔
808
#endif
809

810
//==============================================================================
811
// DRAW_MESH_LINES draws mesh line boundaries on an image
812
//==============================================================================
813

814
void Plot::draw_mesh_lines(ImageData& data) const
36✔
815
{
816
  RGBColor rgb;
36✔
817
  rgb = meshlines_color_;
36✔
818

819
  int ax1, ax2;
820
  switch (basis_) {
36✔
821
  case PlotBasis::xy:
24✔
822
    ax1 = 0;
24✔
823
    ax2 = 1;
24✔
824
    break;
24✔
825
  case PlotBasis::xz:
12✔
826
    ax1 = 0;
12✔
827
    ax2 = 2;
12✔
828
    break;
12✔
829
  case PlotBasis::yz:
×
830
    ax1 = 1;
×
831
    ax2 = 2;
×
832
    break;
×
833
  default:
×
834
    UNREACHABLE();
×
835
  }
836

837
  Position ll_plot {origin_};
36✔
838
  Position ur_plot {origin_};
36✔
839

840
  ll_plot[ax1] -= width_[0] / 2.;
36✔
841
  ll_plot[ax2] -= width_[1] / 2.;
36✔
842
  ur_plot[ax1] += width_[0] / 2.;
36✔
843
  ur_plot[ax2] += width_[1] / 2.;
36✔
844

845
  Position width = ur_plot - ll_plot;
36✔
846

847
  // Find the (axis-aligned) lines of the mesh that intersect this plot.
848
  auto axis_lines =
849
    model::meshes[index_meshlines_mesh_]->plot(ll_plot, ur_plot);
36✔
850

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

867
  // Iterate across the first axis and draw lines.
868
  for (auto ax1_val : axis_lines.first) {
204✔
869
    double frac = (ax1_val - ll_plot[ax1]) / width[ax1];
168✔
870
    int ax1_ind = frac * pixels_[0];
168✔
871
    for (int ax2_ind = ax2_min; ax2_ind < ax2_max; ++ax2_ind) {
27,216✔
872
      for (int plus = 0; plus <= meshlines_width_; plus++) {
54,096✔
873
        if (ax1_ind + plus >= 0 && ax1_ind + plus < pixels_[0])
27,048✔
874
          data(ax1_ind + plus, ax2_ind) = rgb;
27,048✔
875
        if (ax1_ind - plus >= 0 && ax1_ind - plus < pixels_[0])
27,048✔
876
          data(ax1_ind - plus, ax2_ind) = rgb;
27,048✔
877
      }
878
    }
879
  }
880

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

897
  // Iterate across the second axis and draw lines.
898
  for (auto ax2_val : axis_lines.second) {
228✔
899
    double frac = (ax2_val - ll_plot[ax2]) / width[ax2];
192✔
900
    int ax2_ind = (1.0 - frac) * pixels_[1];
192✔
901
    for (int ax1_ind = ax1_min; ax1_ind < ax1_max; ++ax1_ind) {
30,912✔
902
      for (int plus = 0; plus <= meshlines_width_; plus++) {
61,440✔
903
        if (ax2_ind + plus >= 0 && ax2_ind + plus < pixels_[1])
30,720✔
904
          data(ax1_ind, ax2_ind + plus) = rgb;
30,720✔
905
        if (ax2_ind - plus >= 0 && ax2_ind - plus < pixels_[1])
30,720✔
906
          data(ax1_ind, ax2_ind - plus) = rgb;
30,720✔
907
      }
908
    }
909
  }
910
}
36✔
911

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

930
  // initial particle position
931
  Position ll = origin_ - width_ / 2.;
60✔
932

933
  // Open binary plot file for writing
934
  std::ofstream of;
60✔
935
  std::string fname = std::string(path_plot_);
60✔
936
  fname = strtrim(fname);
60✔
937
  hid_t file_id = file_open(fname, 'w');
60✔
938

939
  // write header info
940
  write_attribute(file_id, "filetype", "voxel");
60✔
941
  write_attribute(file_id, "version", VERSION_VOXEL);
60✔
942
  write_attribute(file_id, "openmc_version", VERSION);
60✔
943

944
#ifdef GIT_SHA1
945
  write_attribute(file_id, "git_sha1", GIT_SHA1);
60✔
946
#endif
947

948
  // Write current date and time
949
  write_attribute(file_id, "date_and_time", time_stamp().c_str());
60✔
950
  array<int, 3> pixels;
951
  std::copy(pixels_.begin(), pixels_.end(), pixels.begin());
60✔
952
  write_attribute(file_id, "num_voxels", pixels);
60✔
953
  write_attribute(file_id, "voxel_width", vox);
60✔
954
  write_attribute(file_id, "lower_left", ll);
60✔
955

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

965
  SlicePlotBase pltbase;
60✔
966
  pltbase.width_ = width_;
60✔
967
  pltbase.origin_ = origin_;
60✔
968
  pltbase.basis_ = PlotBasis::xy;
60✔
969
  pltbase.pixels_ = pixels_;
60✔
970
  pltbase.slice_color_overlaps_ = color_overlaps_;
60✔
971

972
  ProgressBar pb;
60✔
973
  for (int z = 0; z < pixels_[2]; z++) {
5,220✔
974
    // update z coordinate
975
    pltbase.origin_.z = ll.z + z * vox[2];
5,160✔
976

977
    // generate ids using plotbase
978
    IdData ids = pltbase.get_map<IdData>();
5,160✔
979

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

986
    // Write to HDF5 dataset
987
    voxel_write_slice(z, dspace, dset, memspace, data_flipped.data());
5,160✔
988

989
    // update progress bar
990
    pb.set_value(
5,160✔
991
      100. * static_cast<double>(z + 1) / static_cast<double>((pixels_[2])));
5,160✔
992
  }
5,160✔
993

994
  voxel_finalize(dspace, dset, memspace);
60✔
995
  file_close(file_id);
60✔
996
}
60✔
997

998
void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset,
60✔
999
  hid_t* memspace)
1000
{
1001
  // Create dataspace/dataset for voxel data
1002
  *dspace = H5Screate_simple(3, dims, nullptr);
60✔
1003
  *dset = H5Dcreate(file_id, "data", H5T_NATIVE_INT, *dspace, H5P_DEFAULT,
60✔
1004
    H5P_DEFAULT, H5P_DEFAULT);
1005

1006
  // Create dataspace for a slice of the voxel
1007
  hsize_t dims_slice[2] {dims[1], dims[2]};
60✔
1008
  *memspace = H5Screate_simple(2, dims_slice, nullptr);
60✔
1009

1010
  // Select hyperslab in dataspace
1011
  hsize_t start[3] {0, 0, 0};
60✔
1012
  hsize_t count[3] {1, dims[1], dims[2]};
60✔
1013
  H5Sselect_hyperslab(*dspace, H5S_SELECT_SET, start, nullptr, count, nullptr);
60✔
1014
}
60✔
1015

1016
void voxel_write_slice(
5,160✔
1017
  int x, hid_t dspace, hid_t dset, hid_t memspace, void* buf)
1018
{
1019
  hssize_t offset[3] {x, 0, 0};
5,160✔
1020
  H5Soffset_simple(dspace, offset);
5,160✔
1021
  H5Dwrite(dset, H5T_NATIVE_INT, memspace, dspace, H5P_DEFAULT, buf);
5,160✔
1022
}
5,160✔
1023

1024
void voxel_finalize(hid_t dspace, hid_t dset, hid_t memspace)
60✔
1025
{
1026
  H5Dclose(dset);
60✔
1027
  H5Sclose(dspace);
60✔
1028
  H5Sclose(memspace);
60✔
1029
}
60✔
1030

1031
RGBColor random_color(void)
2,307✔
1032
{
1033
  return {int(prn(&model::plotter_seed) * 255),
2,307✔
1034
    int(prn(&model::plotter_seed) * 255), int(prn(&model::plotter_seed) * 255)};
2,307✔
1035
}
1036

1037
ProjectionPlot::ProjectionPlot(pugi::xml_node node) : PlottableInterface(node)
60✔
1038
{
1039
  set_output_path(node);
60✔
1040
  set_look_at(node);
60✔
1041
  set_camera_position(node);
60✔
1042
  set_field_of_view(node);
60✔
1043
  set_pixels(node);
60✔
1044
  set_opacities(node);
60✔
1045
  set_orthographic_width(node);
60✔
1046
  set_wireframe_thickness(node);
60✔
1047
  set_wireframe_ids(node);
60✔
1048
  set_wireframe_color(node);
60✔
1049

1050
  if (check_for_node(node, "orthographic_width") &&
72✔
1051
      check_for_node(node, "field_of_view"))
12✔
1052
    fatal_error("orthographic_width and field_of_view are mutually exclusive "
×
1053
                "parameters.");
1054
}
60✔
1055

1056
void ProjectionPlot::set_wireframe_color(pugi::xml_node plot_node)
60✔
1057
{
1058
  // Copy plot background color
1059
  if (check_for_node(plot_node, "wireframe_color")) {
60✔
1060
    vector<int> w_rgb = get_node_array<int>(plot_node, "wireframe_color");
×
1061
    if (w_rgb.size() == 3) {
×
1062
      wireframe_color_ = w_rgb;
×
1063
    } else {
1064
      fatal_error(fmt::format("Bad wireframe RGB in plot {}", id()));
×
1065
    }
1066
  }
1067
}
60✔
1068

1069
void ProjectionPlot::set_output_path(pugi::xml_node node)
60✔
1070
{
1071
  // Set output file path
1072
  std::string filename;
60✔
1073

1074
  if (check_for_node(node, "filename")) {
60✔
1075
    filename = get_node_value(node, "filename");
48✔
1076
  } else {
1077
    filename = fmt::format("plot_{}", id());
24✔
1078
  }
1079

1080
#ifdef USE_LIBPNG
1081
  if (!file_extension_present(filename, "png"))
60✔
1082
    filename.append(".png");
36✔
1083
#else
1084
  if (!file_extension_present(filename, "ppm"))
1085
    filename.append(".ppm");
1086
#endif
1087
  path_plot_ = filename;
60✔
1088
}
60✔
1089

1090
// Advances to the next boundary from outside the geometry
1091
// Returns -1 if no intersection found, and the surface index
1092
// if an intersection was found.
1093
int ProjectionPlot::advance_to_boundary_from_void(GeometryState& p)
12,560,976✔
1094
{
1095
  constexpr double scoot = 1e-5;
12,560,976✔
1096
  double min_dist = {INFINITY};
12,560,976✔
1097
  auto coord = p.coord(0);
12,560,976✔
1098
  Universe* uni = model::universes[model::root_universe].get();
12,560,976✔
1099
  int intersected_surface = -1;
12,560,976✔
1100
  for (auto c_i : uni->cells_) {
50,243,904✔
1101
    auto dist = model::cells.at(c_i)->distance(coord.r, coord.u, 0, &p);
37,682,928✔
1102
    if (dist.first < min_dist) {
37,682,928✔
1103
      min_dist = dist.first;
16,415,208✔
1104
      intersected_surface = dist.second;
16,415,208✔
1105
    }
1106
  }
1107
  if (min_dist > 1e300)
12,560,976✔
1108
    return -1;
2,400,000✔
1109
  else { // advance the particle
1110
    for (int j = 0; j < p.n_coord(); ++j)
20,321,952✔
1111
      p.coord(j).r += (min_dist + scoot) * p.coord(j).u;
10,160,976✔
1112
    return std::abs(intersected_surface);
10,160,976✔
1113
  }
1114
}
1115

1116
bool ProjectionPlot::trackstack_equivalent(
4,788,000✔
1117
  const std::vector<TrackSegment>& track1,
1118
  const std::vector<TrackSegment>& track2) const
1119
{
1120
  if (wireframe_ids_.empty()) {
4,788,000✔
1121
    // Draw wireframe for all surfaces/cells/materials
1122
    if (track1.size() != track2.size())
3,830,400✔
1123
      return false;
67,368✔
1124
    for (int i = 0; i < track1.size(); ++i) {
8,036,880✔
1125
      if (track1[i].id != track2[i].id ||
8,591,496✔
1126
          track1[i].surface != track2[i].surface) {
4,295,724✔
1127
        return false;
21,924✔
1128
      }
1129
    }
1130
    return true;
3,741,108✔
1131
  } else {
1132
    // This runs in O(nm) where n is the intersection stack size
1133
    // and m is the number of IDs we are wireframing. A simpler
1134
    // algorithm can likely be found.
1135
    for (const int id : wireframe_ids_) {
1,908,588✔
1136
      int t1_i = 0;
957,600✔
1137
      int t2_i = 0;
957,600✔
1138

1139
      // Advance to first instance of the ID
1140
      while (t1_i < track1.size() && t2_i < track2.size()) {
1,052,040✔
1141
        while (t1_i < track1.size() && track1[t1_i].id != id)
542,604✔
1142
          t1_i++;
316,704✔
1143
        while (t2_i < track2.size() && track2[t2_i].id != id)
542,604✔
1144
          t2_i++;
316,704✔
1145

1146
        // This one is really important!
1147
        if ((t1_i == track1.size() && t2_i != track2.size()) ||
549,804✔
1148
            (t1_i != track1.size() && t2_i == track2.size()))
323,904✔
1149
          return false;
6,612✔
1150
        if (t1_i == track1.size() && t2_i == track2.size())
222,912✔
1151
          break;
124,848✔
1152
        // Check if surface different
1153
        if (track1[t1_i].surface != track2[t2_i].surface)
98,064✔
1154
          return false;
3,624✔
1155

1156
        // Pretty sure this should not be used:
1157
        // if (t2_i != track2.size() - 1 &&
1158
        //     t1_i != track1.size() - 1 &&
1159
        //     track1[t1_i+1].id != track2[t2_i+1].id) return false;
1160
        if (t2_i != 0 && t1_i != 0 &&
169,980✔
1161
            track1[t1_i - 1].surface != track2[t2_i - 1].surface)
75,540✔
1162
          return false;
×
1163

1164
        // Check if neighboring cells are different
1165
        // if (track1[t1_i ? t1_i - 1 : 0].id != track2[t2_i ? t2_i - 1 : 0].id)
1166
        // return false; if (track1[t1_i < track1.size() - 1 ? t1_i + 1 : t1_i
1167
        // ].id !=
1168
        //    track2[t2_i < track2.size() - 1 ? t2_i + 1 : t2_i].id) return
1169
        //    false;
1170
        t1_i++, t2_i++;
94,440✔
1171
      }
1172
    }
1173
    return true;
950,988✔
1174
  }
1175
}
1176

1177
void ProjectionPlot::create_output() const
60✔
1178
{
1179
  // Get centerline vector for camera-to-model. We create vectors around this
1180
  // that form a pixel array, and then trace rays along that.
1181
  auto up = up_ / up_.norm();
60✔
1182
  Direction looking_direction = look_at_ - camera_position_;
60✔
1183
  looking_direction /= looking_direction.norm();
60✔
1184
  if (std::abs(std::abs(looking_direction.dot(up)) - 1.0) < 1e-9)
60✔
1185
    fatal_error("Up vector cannot align with vector between camera position "
×
1186
                "and look_at!");
1187
  Direction cam_yaxis = looking_direction.cross(up);
60✔
1188
  cam_yaxis /= cam_yaxis.norm();
60✔
1189
  Direction cam_zaxis = cam_yaxis.cross(looking_direction);
60✔
1190
  cam_zaxis /= cam_zaxis.norm();
60✔
1191

1192
  // Transformation matrix for directions
1193
  std::vector<double> camera_to_model = {looking_direction.x, cam_yaxis.x,
60✔
1194
    cam_zaxis.x, looking_direction.y, cam_yaxis.y, cam_zaxis.y,
60✔
1195
    looking_direction.z, cam_yaxis.z, cam_zaxis.z};
60✔
1196

1197
  // Now we convert to the polar coordinate system with the polar angle
1198
  // measuring the angle from the vector up_. Phi is the rotation about up_. For
1199
  // now, up_ is hard-coded to be +z.
1200
  constexpr double DEGREE_TO_RADIAN = M_PI / 180.0;
60✔
1201
  double horiz_fov_radians = horizontal_field_of_view_ * DEGREE_TO_RADIAN;
60✔
1202
  double p0 = static_cast<double>(pixels_[0]);
60✔
1203
  double p1 = static_cast<double>(pixels_[1]);
60✔
1204
  double vert_fov_radians = horiz_fov_radians * p1 / p0;
60✔
1205
  double dphi = horiz_fov_radians / p0;
60✔
1206
  double dmu = vert_fov_radians / p1;
60✔
1207

1208
  size_t width = pixels_[0];
60✔
1209
  size_t height = pixels_[1];
60✔
1210
  ImageData data({width, height}, not_found_);
60✔
1211

1212
  // This array marks where the initial wireframe was drawn.
1213
  // We convolve it with a filter that gets adjusted with the
1214
  // wireframe thickness in order to thicken the lines.
1215
  xt::xtensor<int, 2> wireframe_initial({width, height}, 0);
60✔
1216

1217
  /* Holds all of the track segments for the current rendered line of pixels.
1218
   * old_segments holds a copy of this_line_segments from the previous line.
1219
   * By holding both we can check if the cell/material intersection stack
1220
   * differs from the left or upper neighbor. This allows a robustly drawn
1221
   * wireframe. If only checking the left pixel (which requires substantially
1222
   * less memory), the wireframe tends to be spotty and be disconnected for
1223
   * surface edges oriented horizontally in the rendering.
1224
   *
1225
   * Note that a vector of vectors is required rather than a 2-tensor,
1226
   * since the stack size varies within each column.
1227
   */
1228
  const int n_threads = num_threads();
60✔
1229
  std::vector<std::vector<std::vector<TrackSegment>>> this_line_segments(
1230
    n_threads);
60✔
1231
  for (int t = 0; t < n_threads; ++t) {
150✔
1232
    this_line_segments[t].resize(pixels_[0]);
90✔
1233
  }
1234

1235
  // The last thread writes to this, and the first thread reads from it.
1236
  std::vector<std::vector<TrackSegment>> old_segments(pixels_[0]);
60✔
1237

1238
#pragma omp parallel
30✔
1239
  {
1240
    const int n_threads = num_threads();
30✔
1241
    const int tid = thread_num();
30✔
1242

1243
    GeometryState p;
30✔
1244
    p.u() = {1.0, 0.0, 0.0};
30✔
1245

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

1249
      // Save bottom line of current work chunk to compare against later
1250
      // I used to have this inside the below if block, but it causes a
1251
      // spurious line to be drawn at the bottom of the image. Not sure
1252
      // why, but moving it here fixes things.
1253
      if (tid == n_threads - 1)
6,030✔
1254
        old_segments = this_line_segments[n_threads - 1];
6,030✔
1255

1256
      if (vert < pixels_[1]) {
6,030✔
1257

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

1260
          // Projection mode below decides ray starting conditions
1261
          Position init_r;
1,200,000✔
1262
          Direction init_u;
1,200,000✔
1263

1264
          // Generate the starting position/direction of the ray
1265
          if (orthographic_width_ == 0.0) { // perspective projection
1,200,000✔
1266
            double this_phi =
960,000✔
1267
              -horiz_fov_radians / 2.0 + dphi * horiz + 0.5 * dphi;
960,000✔
1268
            double this_mu =
960,000✔
1269
              -vert_fov_radians / 2.0 + dmu * vert + M_PI / 2.0 + 0.5 * dmu;
960,000✔
1270
            Direction camera_local_vec;
960,000✔
1271
            camera_local_vec.x = std::cos(this_phi) * std::sin(this_mu);
960,000✔
1272
            camera_local_vec.y = std::sin(this_phi) * std::sin(this_mu);
960,000✔
1273
            camera_local_vec.z = std::cos(this_mu);
960,000✔
1274
            init_u = camera_local_vec.rotate(camera_to_model);
960,000✔
1275
            init_r = camera_position_;
960,000✔
1276
          } else { // orthographic projection
1277
            init_u = looking_direction;
240,000✔
1278

1279
            double x_pix_coord = (static_cast<double>(horiz) - p0 / 2.0) / p0;
240,000✔
1280
            double y_pix_coord = (static_cast<double>(vert) - p1 / 2.0) / p0;
240,000✔
1281

1282
            init_r = camera_position_;
240,000✔
1283
            init_r += cam_yaxis * x_pix_coord * orthographic_width_;
240,000✔
1284
            init_r += cam_zaxis * y_pix_coord * orthographic_width_;
240,000✔
1285
          }
1286

1287
          // Resets internal geometry state of particle
1288
          p.init_from_r_u(init_r, init_u);
1,200,000✔
1289

1290
          bool hitsomething = false;
1,200,000✔
1291
          bool intersection_found = true;
1,200,000✔
1292
          int loop_counter = 0;
1,200,000✔
1293

1294
          this_line_segments[tid][horiz].clear();
1,200,000✔
1295

1296
          int first_surface =
1,200,000✔
1297
            -1; // surface first passed when entering the model
1298
          bool first_inside_model = true; // false after entering the model
1,200,000✔
1299
          while (intersection_found) {
8,260,530✔
1300
            bool inside_cell = false;
7,060,530✔
1301

1302
            int32_t i_surface = std::abs(p.surface()) - 1;
7,060,530✔
1303
            if (i_surface > 0 &&
8,384,298✔
1304
                model::surfaces[i_surface]->geom_type_ == GeometryType::DAG) {
1,323,768✔
1305
#ifdef DAGMC
1306
              int32_t i_cell = next_cell(i_surface,
1307
                p.cell_last(p.n_coord() - 1), p.lowest_coord().universe);
1308
              inside_cell = i_cell >= 0;
1309
#else
1310
              fatal_error(
1311
                "Not compiled for DAGMC, but somehow you have a DAGCell!");
1312
#endif
1313
            } else {
1314
              inside_cell = exhaustive_find_cell(p);
7,060,530✔
1315
            }
1316

1317
            if (inside_cell) {
7,060,530✔
1318

1319
              // This allows drawing wireframes with surface intersection
1320
              // edges on the model boundary for the same cell.
1321
              if (first_inside_model) {
780,042✔
1322
                this_line_segments[tid][horiz].emplace_back(
893,904✔
1323
                  color_by_ == PlotColorBy::mats ? p.material()
138,954✔
1324
                                                 : p.lowest_coord().cell,
307,998✔
1325
                  0.0, first_surface);
446,952✔
1326
                first_inside_model = false;
446,952✔
1327
              }
1328

1329
              hitsomething = true;
780,042✔
1330
              intersection_found = true;
780,042✔
1331
              auto dist = distance_to_boundary(p);
780,042✔
1332
              this_line_segments[tid][horiz].emplace_back(
1,560,084✔
1333
                color_by_ == PlotColorBy::mats ? p.material()
211,986✔
1334
                                               : p.lowest_coord().cell,
568,056✔
1335
                dist.distance, std::abs(dist.surface_index));
780,042✔
1336

1337
              // Advance particle
1338
              for (int lev = 0; lev < p.n_coord(); ++lev) {
1,560,084✔
1339
                p.coord(lev).r += dist.distance * p.coord(lev).u;
780,042✔
1340
              }
1341
              p.surface() = dist.surface_index;
780,042✔
1342
              p.n_coord_last() = p.n_coord();
780,042✔
1343
              p.n_coord() = dist.coord_level;
780,042✔
1344
              if (dist.lattice_translation[0] != 0 ||
780,042✔
1345
                  dist.lattice_translation[1] != 0 ||
1,560,084✔
1346
                  dist.lattice_translation[2] != 0) {
780,042✔
1347
                cross_lattice(p, dist);
1348
              }
1349

1350
            } else {
1351
              first_surface = advance_to_boundary_from_void(p);
6,280,488✔
1352
              intersection_found =
6,280,488✔
1353
                first_surface != -1; // -1 if no surface found
6,280,488✔
1354
            }
1355
            loop_counter++;
7,060,530✔
1356
            if (loop_counter > MAX_INTERSECTIONS)
7,060,530✔
1357
              fatal_error("Infinite loop in projection plot");
1358
          }
1359

1360
          // Now color the pixel based on what we have intersected...
1361
          // Loops backwards over intersections.
1362
          Position current_color(
1363
            not_found_.red, not_found_.green, not_found_.blue);
1,200,000✔
1364
          const auto& segments = this_line_segments[tid][horiz];
1,200,000✔
1365
          for (unsigned i = segments.size(); i-- > 0;) {
2,426,994✔
1366
            int colormap_idx = segments[i].id;
1,226,994✔
1367
            RGBColor seg_color = colors_[colormap_idx];
1,226,994✔
1368
            Position seg_color_vec(
1369
              seg_color.red, seg_color.green, seg_color.blue);
1,226,994✔
1370
            double mixing = std::exp(-xs_[colormap_idx] * segments[i].length);
1,226,994✔
1371
            current_color =
1372
              current_color * mixing + (1.0 - mixing) * seg_color_vec;
1,226,994✔
1373
            RGBColor result;
1,226,994✔
1374
            result.red = static_cast<uint8_t>(current_color.x);
1,226,994✔
1375
            result.green = static_cast<uint8_t>(current_color.y);
1,226,994✔
1376
            result.blue = static_cast<uint8_t>(current_color.z);
1,226,994✔
1377
            data(horiz, vert) = result;
1,226,994✔
1378
          }
1379

1380
          // Check to draw wireframe in horizontal direction. No inter-thread
1381
          // comm.
1382
          if (horiz > 0) {
1,200,000✔
1383
            if (!trackstack_equivalent(this_line_segments[tid][horiz],
1,194,000✔
1384
                  this_line_segments[tid][horiz - 1])) {
1,194,000✔
1385
              wireframe_initial(horiz, vert) = 1;
22,668✔
1386
            }
1387
          }
1388
        }
1389
      } // end "if" vert in correct range
1390

1391
      // We require a barrier before comparing vertical neighbors' intersection
1392
      // stacks. i.e. all threads must be done with their line.
1393
#pragma omp barrier
1394

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

1402
        const std::vector<std::vector<TrackSegment>>* top_cmp = nullptr;
6,000✔
1403
        if (tid == 0)
6,000✔
1404
          top_cmp = &old_segments;
6,000✔
1405
        else
1406
          top_cmp = &this_line_segments[tid - 1];
1407

1408
        for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
1,206,000✔
1409
          if (!trackstack_equivalent(
1,200,000✔
1410
                this_line_segments[tid][horiz], (*top_cmp)[horiz])) {
1,200,000✔
1411
            wireframe_initial(horiz, vert) = 1;
25,284✔
1412
          }
1413
        }
1414
      }
1415

1416
      // We need another barrier to ensure threads don't proceed to modify their
1417
      // intersection stacks on that horizontal line while others are
1418
      // potentially still working on the above.
1419
#pragma omp barrier
1420
      vert += n_threads;
6,030✔
1421
    }
1422
  } // end omp parallel
30✔
1423

1424
  // Now thicken the wireframe lines and apply them to our image
1425
  for (int vert = 0; vert < pixels_[1]; ++vert) {
12,060✔
1426
    for (int horiz = 0; horiz < pixels_[0]; ++horiz) {
2,412,000✔
1427
      if (wireframe_initial(horiz, vert)) {
2,400,000✔
1428
        if (wireframe_thickness_ == 1)
83,916✔
1429
          data(horiz, vert) = wireframe_color_;
37,908✔
1430
        for (int i = -wireframe_thickness_ / 2; i < wireframe_thickness_ / 2;
223,620✔
1431
             ++i)
1432
          for (int j = -wireframe_thickness_ / 2; j < wireframe_thickness_ / 2;
609,864✔
1433
               ++j)
1434
            if (i * i + j * j < wireframe_thickness_ * wireframe_thickness_) {
470,160✔
1435

1436
              // Check if wireframe pixel is out of bounds
1437
              int w_i = std::max(std::min(horiz + i, pixels_[0] - 1), 0);
470,160✔
1438
              int w_j = std::max(std::min(vert + j, pixels_[1] - 1), 0);
470,160✔
1439
              data(w_i, w_j) = wireframe_color_;
470,160✔
1440
            }
1441
      }
1442
    }
1443
  }
1444

1445
#ifdef USE_LIBPNG
1446
  output_png(path_plot(), data);
60✔
1447
#else
1448
  output_ppm(path_plot(), data);
1449
#endif
1450
}
60✔
1451

1452
void ProjectionPlot::print_info() const
60✔
1453
{
1454
  fmt::print("Plot Type: Projection\n");
50✔
1455
  fmt::print("Camera position: {} {} {}\n", camera_position_.x,
50✔
1456
    camera_position_.y, camera_position_.z);
60✔
1457
  fmt::print("Look at: {} {} {}\n", look_at_.x, look_at_.y, look_at_.z);
110✔
1458
  fmt::print(
50✔
1459
    "Horizontal field of view: {} degrees\n", horizontal_field_of_view_);
60✔
1460
  fmt::print("Pixels: {} {}\n", pixels_[0], pixels_[1]);
110✔
1461
}
60✔
1462

1463
void ProjectionPlot::set_opacities(pugi::xml_node node)
60✔
1464
{
1465
  xs_.resize(colors_.size(), 1e6); // set to large value for opaque by default
60✔
1466

1467
  for (auto cn : node.children("color")) {
132✔
1468
    // Make sure 3 values are specified for RGB
1469
    double user_xs = std::stod(get_node_value(cn, "xs"));
72✔
1470
    int col_id = std::stoi(get_node_value(cn, "id"));
72✔
1471

1472
    // Add RGB
1473
    if (PlotColorBy::cells == color_by_) {
72✔
1474
      if (model::cell_map.find(col_id) != model::cell_map.end()) {
72✔
1475
        col_id = model::cell_map[col_id];
72✔
1476
        xs_[col_id] = user_xs;
72✔
1477
      } else {
1478
        warning(fmt::format(
×
1479
          "Could not find cell {} specified in plot {}", col_id, id()));
×
1480
      }
1481
    } else if (PlotColorBy::mats == color_by_) {
×
1482
      if (model::material_map.find(col_id) != model::material_map.end()) {
×
1483
        col_id = model::material_map[col_id];
×
1484
        xs_[col_id] = user_xs;
×
1485
      } else {
1486
        warning(fmt::format(
×
1487
          "Could not find material {} specified in plot {}", col_id, id()));
×
1488
      }
1489
    }
1490
  }
1491
}
60✔
1492

1493
void ProjectionPlot::set_orthographic_width(pugi::xml_node node)
60✔
1494
{
1495
  if (check_for_node(node, "orthographic_width")) {
60✔
1496
    double orthographic_width =
1497
      std::stod(get_node_value(node, "orthographic_width", true));
12✔
1498
    if (orthographic_width < 0.0)
12✔
1499
      fatal_error("Requires positive orthographic_width");
×
1500
    orthographic_width_ = orthographic_width;
12✔
1501
  }
1502
}
60✔
1503

1504
void ProjectionPlot::set_wireframe_thickness(pugi::xml_node node)
60✔
1505
{
1506
  if (check_for_node(node, "wireframe_thickness")) {
60✔
1507
    int wireframe_thickness =
1508
      std::stoi(get_node_value(node, "wireframe_thickness", true));
24✔
1509
    if (wireframe_thickness < 0)
24✔
1510
      fatal_error("Requires non-negative wireframe thickness");
×
1511
    wireframe_thickness_ = wireframe_thickness;
24✔
1512
  }
1513
}
60✔
1514

1515
void ProjectionPlot::set_wireframe_ids(pugi::xml_node node)
60✔
1516
{
1517
  if (check_for_node(node, "wireframe_ids")) {
60✔
1518
    wireframe_ids_ = get_node_array<int>(node, "wireframe_ids");
12✔
1519
    // It is read in as actual ID values, but we have to convert to indices in
1520
    // mat/cell array
1521
    for (auto& x : wireframe_ids_)
24✔
1522
      x = color_by_ == PlotColorBy::mats ? model::material_map[x]
12✔
1523
                                         : model::cell_map[x];
×
1524
  }
1525
  // We make sure the list is sorted in order to later use
1526
  // std::binary_search.
1527
  std::sort(wireframe_ids_.begin(), wireframe_ids_.end());
60✔
1528
}
60✔
1529

1530
void ProjectionPlot::set_pixels(pugi::xml_node node)
60✔
1531
{
1532
  vector<int> pxls = get_node_array<int>(node, "pixels");
60✔
1533
  if (pxls.size() != 2)
60✔
1534
    fatal_error(
×
1535
      fmt::format("<pixels> must be length 2 in projection plot {}", id()));
×
1536
  pixels_[0] = pxls[0];
60✔
1537
  pixels_[1] = pxls[1];
60✔
1538
}
60✔
1539

1540
void ProjectionPlot::set_camera_position(pugi::xml_node node)
60✔
1541
{
1542
  vector<double> camera_pos = get_node_array<double>(node, "camera_position");
60✔
1543
  if (camera_pos.size() != 3) {
60✔
1544
    fatal_error(
×
1545
      fmt::format("look_at element must have three floating point values"));
×
1546
  }
1547
  camera_position_.x = camera_pos[0];
60✔
1548
  camera_position_.y = camera_pos[1];
60✔
1549
  camera_position_.z = camera_pos[2];
60✔
1550
}
60✔
1551

1552
void ProjectionPlot::set_look_at(pugi::xml_node node)
60✔
1553
{
1554
  vector<double> look_at = get_node_array<double>(node, "look_at");
60✔
1555
  if (look_at.size() != 3) {
60✔
1556
    fatal_error("look_at element must have three floating point values");
×
1557
  }
1558
  look_at_.x = look_at[0];
60✔
1559
  look_at_.y = look_at[1];
60✔
1560
  look_at_.z = look_at[2];
60✔
1561
}
60✔
1562

1563
void ProjectionPlot::set_field_of_view(pugi::xml_node node)
60✔
1564
{
1565
  // Defaults to 70 degree horizontal field of view (see .h file)
1566
  if (check_for_node(node, "field_of_view")) {
60✔
1567
    double fov = std::stod(get_node_value(node, "field_of_view", true));
24✔
1568
    if (fov < 180.0 && fov > 0.0) {
24✔
1569
      horizontal_field_of_view_ = fov;
24✔
1570
    } else {
1571
      fatal_error(fmt::format(
×
1572
        "Field of view for plot {} out-of-range. Must be in (0, 180).", id()));
×
1573
    }
1574
  }
1575
}
60✔
1576

1577
extern "C" int openmc_id_map(const void* plot, int32_t* data_out)
×
1578
{
1579

1580
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
1581
  if (!plt) {
×
1582
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
1583
    return OPENMC_E_INVALID_ARGUMENT;
×
1584
  }
1585

1586
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
×
1587
    model::overlap_check_count.resize(model::cells.size());
×
1588
  }
1589

1590
  auto ids = plt->get_map<IdData>();
×
1591

1592
  // write id data to array
1593
  std::copy(ids.data_.begin(), ids.data_.end(), data_out);
×
1594

1595
  return 0;
×
1596
}
1597

1598
extern "C" int openmc_property_map(const void* plot, double* data_out)
×
1599
{
1600

1601
  auto plt = reinterpret_cast<const SlicePlotBase*>(plot);
×
1602
  if (!plt) {
×
1603
    set_errmsg("Invalid slice pointer passed to openmc_id_map");
×
1604
    return OPENMC_E_INVALID_ARGUMENT;
×
1605
  }
1606

1607
  if (plt->slice_color_overlaps_ && model::overlap_check_count.size() == 0) {
×
1608
    model::overlap_check_count.resize(model::cells.size());
×
1609
  }
1610

1611
  auto props = plt->get_map<PropertyData>();
×
1612

1613
  // write id data to array
1614
  std::copy(props.data_.begin(), props.data_.end(), data_out);
×
1615

1616
  return 0;
×
1617
}
1618

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

© 2025 Coveralls, Inc