From 8c7b9969f94541447dd8332f2959e85dc597b343 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 30 Oct 2018 21:29:35 -0500 Subject: [PATCH] Updating xml calls for reading plots.xml. --- include/openmc/xml_interface.h | 5 - src/plot.cpp | 191 ++++++++++++++------------------- src/xml_interface.cpp | 28 ----- 3 files changed, 81 insertions(+), 143 deletions(-) diff --git a/include/openmc/xml_interface.h b/include/openmc/xml_interface.h index 29de8839e0..6ab8c148cd 100644 --- a/include/openmc/xml_interface.h +++ b/include/openmc/xml_interface.h @@ -49,10 +49,5 @@ xt::xarray get_node_xarray(pugi::xml_node node, const char* name, return xt::adapt(v, shape); } -std::vector -get_child_nodes(pugi::xml_node node, const char* name = NULL); - -int node_word_count(pugi::xml_node node, const char* name); - } // namespace openmc #endif // OPENMC_XML_INTERFACE_H diff --git a/src/plot.cpp b/src/plot.cpp index f278324866..ec3934fdd8 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -63,16 +63,11 @@ int openmc_plot_geometry() void read_plots(pugi::xml_node* plots_node) { - - std::vector plot_nodes; - plot_nodes = get_child_nodes(*plots_node, "plot"); - - n_plots = plot_nodes.size(); - - for (int i = 0; i < plot_nodes.size(); i++) { - Plot pl(plot_nodes[i]); + n_plots = 0; + for (auto node : plots_node->children("plot")) { + Plot pl(node); plots.push_back(pl); - plot_map[pl.id_] = i; + plot_map[pl.id_] = n_plots++; } } @@ -87,8 +82,8 @@ void create_ppm(Plot pl) size_t width = pl.pixels_[0]; size_t height = pl.pixels_[1]; - double in_pixel = (pl.width_[0])/double(width); - double out_pixel = (pl.width_[1])/double(height); + double in_pixel = (pl.width_[0])/static_cast(width); + double out_pixel = (pl.width_[1])/static_cast(height); ImageData data; data.resize({width, height}); @@ -218,10 +213,9 @@ Plot::set_output_path(pugi::xml_node plot_node) path_plot_ = filename.str(); // Copy plot pixel size - std::vector pxls; + std::vector pxls = get_node_array(plot_node, "pixels"); if (PlotType::slice == type_) { - if (node_word_count(plot_node, "pixels") == 2) { - pxls = get_node_array(plot_node, "pixels"); + if (pxls.size() == 2) { pixels_[0] = pxls[0]; pixels_[1] = pxls[1]; } else { @@ -231,8 +225,7 @@ Plot::set_output_path(pugi::xml_node plot_node) fatal_error(err_msg.str()); } } else if (PlotType::voxel == type_) { - if (node_word_count(plot_node, "pixels") == 3) { - pxls = get_node_array(plot_node, "pixels"); + if (pxls.size() == 3) { pixels_[0] = pxls[0]; pixels_[1] = pxls[1]; pixels_[2] = pxls[2]; @@ -249,8 +242,8 @@ void Plot::set_bg_color(pugi::xml_node plot_node) { // Copy plot background color - std::vector bg_rgb; if (check_for_node(plot_node, "background")) { + std::vector bg_rgb = get_node_array(plot_node, "background"); if (PlotType::voxel == type_) { if (openmc_master) { std::stringstream err_msg; @@ -259,8 +252,7 @@ Plot::set_bg_color(pugi::xml_node plot_node) warning(err_msg.str()); } } - if (node_word_count(plot_node, "background") == 3) { - bg_rgb = get_node_array(plot_node, "background"); + if (bg_rgb.size() == 3) { not_found_[RED] = bg_rgb[RED]; not_found_[GREEN] = bg_rgb[GREEN]; not_found_[BLUE] = bg_rgb[BLUE]; @@ -306,9 +298,8 @@ void Plot::set_origin(pugi::xml_node plot_node) { // Copy plotting origin - std::vector pl_origin; - if (node_word_count(plot_node, "origin") == 3) { - pl_origin = get_node_array(plot_node, "origin"); + std::vector pl_origin = get_node_array(plot_node, "origin"); + if (pl_origin.size() == 3) { origin_[0] = pl_origin[0]; origin_[1] = pl_origin[1]; origin_[2] = pl_origin[2]; @@ -324,10 +315,9 @@ void Plot::set_width(pugi::xml_node plot_node) { // Copy plotting width - std::vector pl_width; + std::vector pl_width = get_node_array(plot_node, "width"); if (PlotType::slice == type_) { - if (node_word_count(plot_node, "width") == 2) { - pl_width = get_node_array(plot_node, "width"); + if (pl_width.size() == 2) { width_[0] = pl_width[0]; width_[1] = pl_width[1]; } else { @@ -337,7 +327,7 @@ Plot::set_width(pugi::xml_node plot_node) fatal_error(err_msg); } } else if (PlotType::voxel == type_) { - if (node_word_count(plot_node, "width") == 3) { + if (pl_width.size() == 3) { pl_width = get_node_array(plot_node, "width"); width_[0] = pl_width[0]; width_[1] = pl_width[1]; @@ -403,92 +393,78 @@ Plot::set_default_colors(pugi::xml_node plot_node) void Plot::set_user_colors(pugi::xml_node plot_node) { - // Get the number of nodes and get a list of them - std::vector color_nodes; - color_nodes = get_child_nodes(plot_node, "color"); - - // Copy user-specified colors - if (color_nodes.size() != 0) { - - if (PlotType::voxel == type_) { - if (openmc_master) { - std::stringstream err_msg; - err_msg << "Color specifications ignored in voxel plot " - << id_; - warning(err_msg); - } + if (!plot_node.select_nodes("color").empty() && PlotType::voxel == type_) { + if (openmc_master) { + std::stringstream err_msg; + err_msg << "Color specifications ignored in voxel plot " + << id_; + warning(err_msg); } - - for (auto cn : color_nodes) { - // Check and make sure 3 values are specified for RGB - if (node_word_count(cn, "rgb") != 3) { - std::stringstream err_msg; - err_msg << "Bad RGB in plot " << id_; - fatal_error(err_msg); - } - // Ensure that there is an id for this color specification - int col_id; - if (check_for_node(cn, "id")) { - col_id = std::stoi(get_node_value(cn, "id")); + } + + for (auto cn : plot_node.children("color")) { + // Make sure 3 values are specified for RGB + std::vector user_rgb = get_node_array(cn, "rgb"); + if (user_rgb.size() != 3) { + std::stringstream err_msg; + err_msg << "Bad RGB in plot " << id_; + fatal_error(err_msg); + } + // Ensure that there is an id for this color specification + int col_id; + if (check_for_node(cn, "id")) { + col_id = std::stoi(get_node_value(cn, "id")); + } else { + std::stringstream err_msg; + err_msg << "Must specify id for color specification in plot " + << id_; + fatal_error(err_msg); + } + // Add RGB + if (PlotColorBy::cells == color_by_) { + if (cell_map.find(col_id) != cell_map.end()) { + col_id = cell_map[col_id]; + colors_[col_id][RED] = user_rgb[RED]; + colors_[col_id][GREEN] = user_rgb[GREEN]; + colors_[col_id][BLUE] = user_rgb[BLUE]; } else { std::stringstream err_msg; - err_msg << "Must specify id for color specification in plot " - << id_; + err_msg << "Could not find cell " << col_id + << " specified in plot " << id_; fatal_error(err_msg); } - // Add RGB - if (PlotColorBy::cells == color_by_) { - std::vector cell_rgb; - if (cell_map.find(col_id) != cell_map.end()) { - col_id = cell_map[col_id]; - cell_rgb = get_node_array(cn, "rgb"); - colors_[col_id][RED] = cell_rgb[RED]; - colors_[col_id][GREEN] = cell_rgb[GREEN]; - colors_[col_id][BLUE] = cell_rgb[BLUE]; - } else { - std::stringstream err_msg; - err_msg << "Could not find cell " << col_id - << " specified in plot " << id_; - fatal_error(err_msg); - } - } else if (PlotColorBy::mats == color_by_) { - std::vector mat_rgb; - if (material_map.find(col_id) != material_map.end()) { - col_id = material_map[col_id]; - mat_rgb = get_node_array(cn, "rgb"); - colors_[col_id][RED] = mat_rgb[RED]; - colors_[col_id][GREEN] = mat_rgb[GREEN]; - colors_[col_id][BLUE] = mat_rgb[BLUE]; - } else { - std::stringstream err_msg; - err_msg << "Could not find material " << col_id - << " specified in plot " << id_; - fatal_error(err_msg); - } + } else if (PlotColorBy::mats == color_by_) { + if (material_map.find(col_id) != material_map.end()) { + col_id = material_map[col_id]; + colors_[col_id][RED] = user_rgb[RED]; + colors_[col_id][GREEN] = user_rgb[GREEN]; + colors_[col_id][BLUE] = user_rgb[BLUE]; + } else { + std::stringstream err_msg; + err_msg << "Could not find material " << col_id + << " specified in plot " << id_; + fatal_error(err_msg); } - } // color node loop - } + } + } // color node loop } void Plot::set_meshlines(pugi::xml_node plot_node) { // Deal with meshlines - std::vector mesh_line_nodes; - mesh_line_nodes = get_child_nodes(plot_node, "meshlines"); + pugi::xpath_node_set mesh_line_nodes = plot_node.select_nodes("meshlines"); - int n_meshlines = mesh_line_nodes.size(); - - if (n_meshlines != 0) { + if (!mesh_line_nodes.empty()) { if (PlotType::voxel == type_) { std::stringstream msg; msg << "Meshlines ignored in voxel plot " << id_; warning(msg); } - if (1 == n_meshlines) { + if (mesh_line_nodes.size() == 1) { // Get first meshline node - pugi::xml_node meshlines_node = mesh_line_nodes[0]; + pugi::xml_node meshlines_node = mesh_line_nodes[0].node(); // Check mesh type std::string meshtype; @@ -512,15 +488,14 @@ Plot::set_meshlines(pugi::xml_node plot_node) } // Check for color - std::vector ml_rgb; if (check_for_node(meshlines_node, "color")) { // Check and make sure 3 values are specified for RGB - if (node_word_count(meshlines_node, "color") != 3) { + std::vector ml_rgb = get_node_array(meshlines_node, "color"); + if (ml_rgb.size() != 3) { std::stringstream err_msg; err_msg << "Bad RGB for meshlines color in plot " << id_; fatal_error(err_msg); } - ml_rgb = get_node_array(meshlines_node, "color"); meshlines_color_[0] = ml_rgb[0]; meshlines_color_[1] = ml_rgb[1]; meshlines_color_[2] = ml_rgb[2]; @@ -570,10 +545,10 @@ Plot::set_meshlines(pugi::xml_node plot_node) int idx; int err = openmc_get_mesh_index(tally_mesh_id, &idx); if (err != 0) { - std::stringstream err_msg; - err_msg << "Could not find mesh " << tally_mesh_id - << " specified in meshlines for plot " << id_; - fatal_error(err_msg); + std::stringstream err_msg; + err_msg << "Could not find mesh " << tally_mesh_id + << " specified in meshlines for plot " << id_; + fatal_error(err_msg); } index_meshlines_mesh_ = idx; } else { @@ -593,11 +568,9 @@ void Plot::set_mask(pugi::xml_node plot_node) { // Deal with masks - std::vector mask_nodes; - mask_nodes = get_child_nodes(plot_node, "mask"); - int n_masks = mask_nodes.size(); - - if (n_masks > 0) { + pugi::xpath_node_set mask_nodes = plot_node.select_nodes("mask"); + + if (!mask_nodes.empty()) { if (PlotType::voxel == type_) { if (openmc_master) { std::stringstream wrn_msg; @@ -606,19 +579,17 @@ Plot::set_mask(pugi::xml_node plot_node) } } - if (1 == n_masks) { + if (mask_nodes.size() == 1) { // Get pointer to mask - pugi::xml_node mask_node = mask_nodes[0]; + pugi::xml_node mask_node = mask_nodes[0].node(); // Determine how many components there are and allocate - int n_comp; - n_comp = node_word_count(mask_node, "components"); - if (0 == n_comp) { + std::vector iarray = get_node_array(mask_node, "components"); + if (iarray.size() == 0) { std::stringstream err_msg; err_msg << "Missing in mask of plot " << id_; fatal_error(err_msg); } - std::vector iarray = get_node_array(mask_node, "components"); // First we need to change the user-specified identifiers to indices // in the cell and material arrays diff --git a/src/xml_interface.cpp b/src/xml_interface.cpp index 93b65521c0..60ae7ffcab 100644 --- a/src/xml_interface.cpp +++ b/src/xml_interface.cpp @@ -3,7 +3,6 @@ #include // for transform #include -#include "openmc/string_functions.h" #include "openmc/error.h" @@ -57,31 +56,4 @@ get_node_value_bool(pugi::xml_node node, const char* name) return false; } - - -std::vector -get_child_nodes(pugi::xml_node node, const char* name) -{ - pugi::xml_node current; - std::vector node_list; - - // add all child nodes with name to vector and return - if (name) { - for (pugi::xml_node current : node.children(name)) { - node_list.push_back(current); - } - } else { - for (pugi::xml_node current : node.children()) { - node_list.push_back(current); - } - } - return node_list; -} - -int node_word_count(pugi::xml_node node, const char* name) -{ - std::string s = get_node_value(node, name); - return word_count(s); -} - } // namespace openmc