From 8540838950355e0cc21b65fea19d5e55b6474d3f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Fri, 26 Oct 2018 11:04:38 -0500 Subject: [PATCH] Updating style. Using macros. Adding to get_child_nodes function. --- include/openmc/constants.h | 3 +- include/openmc/output.h | 4 +- include/openmc/plot.h | 66 +++++------ include/openmc/string_functions.h | 2 +- include/openmc/xml_interface.h | 6 +- src/plot.cpp | 178 ++++++++++++++++-------------- src/string_functions.cpp | 8 +- src/xml_interface.cpp | 10 +- 8 files changed, 144 insertions(+), 133 deletions(-) diff --git a/include/openmc/constants.h b/include/openmc/constants.h index 60a2e0691..bfd4960f7 100644 --- a/include/openmc/constants.h +++ b/include/openmc/constants.h @@ -85,8 +85,7 @@ constexpr double INFTY {std::numeric_limits::max()}; constexpr double ONE {1.0}; constexpr double TWO {2.0}; constexpr double HALF {0.5}; - - + // Physical constants constexpr double MASS_NEUTRON {1.00866491588}; // mass of a neutron in amu constexpr double MASS_NEUTRON_EV {939.5654133e6}; // mass of a neutron in eV/c^2 diff --git a/include/openmc/output.h b/include/openmc/output.h index 4ba1303ea..8f76e43ae 100644 --- a/include/openmc/output.h +++ b/include/openmc/output.h @@ -22,9 +22,9 @@ void header(const char* msg, int level); //! Retrieve a time stamp with the format "yyyy-mm-dd hh:mm:ss" //! //============================================================================== - + std::string time_stamp(); - + //============================================================================== //! Display information regarding cell overlap checking. //============================================================================== diff --git a/include/openmc/plot.h b/include/openmc/plot.h index a02db40d3..004ca30d1 100644 --- a/include/openmc/plot.h +++ b/include/openmc/plot.h @@ -52,42 +52,42 @@ namespace openmc { // ObjectPlot holds plot information //=============================================================================== - struct ObjectPlot { +class ObjectPlot +{ - ObjectPlot(pugi::xml_node plot); +public: + ObjectPlot(pugi::xml_node plot); - int id; - int type; - int color_by; - Position origin; - Position width; - int basis; - int pixels[3]; - int meshlines_width; - int level; - int index_meshlines_mesh; - ObjectColor meshlines_color; - ObjectColor not_found; - std::vector colors; - std::string path_plot; - pugi::xml_node plot_node; + int id; + int type; + int color_by; + Position origin; + Position width; + int basis; + int pixels[3]; + int meshlines_width; + int level; + int index_meshlines_mesh; + ObjectColor meshlines_color; + ObjectColor not_found; + std::vector colors; + std::string path_plot; + pugi::xml_node plot_node; - private: - void set_id(); - void set_type(); - void set_output_path(); - void set_bg_color(); - void set_basis(); - void set_origin(); - void set_width(); - void set_universe(); - void set_default_colors(); - void set_user_colors(); - void set_meshlines(); - void set_mask(); - }; - - //extern "C" int openmc_plot_geometry(); + private: + void set_id(); + void set_type(); + void set_output_path(); + void set_bg_color(); + void set_basis(); + void set_origin(); + void set_width(); + void set_universe(); + void set_default_colors(); + void set_user_colors(); + void set_meshlines(); + void set_mask(); +}; extern "C" void read_plots(pugi::xml_node* plot_node); diff --git a/include/openmc/string_functions.h b/include/openmc/string_functions.h index acbbe4e1d..a653d1e77 100644 --- a/include/openmc/string_functions.h +++ b/include/openmc/string_functions.h @@ -15,6 +15,6 @@ char* strtrim(char* c_str); void to_lower(std::string& str); int word_count(std::string const& str); - + } // namespace openmc #endif // STRING_FUNCTIONS_H diff --git a/include/openmc/xml_interface.h b/include/openmc/xml_interface.h index e8248ca86..104e6ec27 100644 --- a/include/openmc/xml_interface.h +++ b/include/openmc/xml_interface.h @@ -50,12 +50,8 @@ xt::xarray get_node_xarray(pugi::xml_node node, const char* name, return xt::adapt(v, shape); } -//=============================================================================== -// GET_NODE_LIST is used to get a pointer to a list of sub-element nodes -//=============================================================================== - std::vector -get_child_nodes(pugi::xml_node node, const char* name); +get_child_nodes(pugi::xml_node node, const char* name = NULL); int node_word_count(pugi::xml_node node, const char* name); diff --git a/src/plot.cpp b/src/plot.cpp index 4bab60f24..0cbdd8d2b 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -17,13 +17,13 @@ namespace openmc { - int PLOT_LEVEL_LOWEST = -1; +int PLOT_LEVEL_LOWEST = -1; - std::map plot_dict; +std::map plot_dict; - int n_plots; +int n_plots; - std::vector plots; +std::vector plots; const int RED = 0; const int GREEN = 1; @@ -37,7 +37,8 @@ const int NULLRGB[3] = {0, 0, 0}; //=============================================================================== extern "C" -int openmc_plot_geometry() { +int openmc_plot_geometry() +{ int err; for(int i = 0; i < n_plots; i++) { @@ -57,13 +58,14 @@ int openmc_plot_geometry() { create_voxel(pl); continue; } - } return 0; } -void read_plots(pugi::xml_node* plots_node) { +void +read_plots(pugi::xml_node* plots_node) +{ std::vector plot_nodes; plot_nodes = get_child_nodes(*plots_node, "plot"); @@ -82,7 +84,8 @@ void read_plots(pugi::xml_node* plots_node) { // specification in the portable pixmap format (PPM) //=============================================================================== -void create_ppm(ObjectPlot* pl) { +void create_ppm(ObjectPlot* pl) +{ int width = pl->pixels[0]; int height = pl->pixels[1]; @@ -146,14 +149,16 @@ void create_ppm(ObjectPlot* pl) { } } + // draw mesh lines if present if (pl->index_meshlines_mesh >= 0) { draw_mesh_lines(pl, data); } + // write ppm data to file output_ppm(pl, data); - } void -ObjectPlot::set_id() { +ObjectPlot::set_id() +{ // Copy data into plots if (check_for_node(plot_node, "id")) { id = std::stoi(get_node_value(plot_node, "id")); @@ -168,34 +173,39 @@ ObjectPlot::set_id() { err_msg << id; fatal_error(err_msg.str()); } - } void -ObjectPlot::set_type() { +ObjectPlot::set_type() +{ // Copy plot type // Default is slice std::string type_str = "slice"; type = PLOT_TYPE::SLICE; + // check type specified on plot node if (check_for_node(plot_node, "type")) { type_str = get_node_value(plot_node, "type"); to_lower(type_str); + // set type using node value if (type_str == "slice") { type = PLOT_TYPE::SLICE; + return; } else if (type_str == "voxel") { type = PLOT_TYPE::VOXEL; - } else { - std::stringstream err_msg; - err_msg << "Unsupported plot type '" << type_str - << "' in plot " << id; - fatal_error(err_msg.str()); + return; } - } + // if we're here, something is wrong + std::stringstream err_msg; + err_msg << "Unsupported plot type '" << type_str + << "' in plot " << id; + fatal_error(err_msg.str()); + } } void -ObjectPlot::set_output_path() { +ObjectPlot::set_output_path() +{ // Set output file path std::stringstream filename; filename << "plot_" << id; @@ -243,7 +253,8 @@ ObjectPlot::set_output_path() { } void -ObjectPlot::set_bg_color(){ +ObjectPlot::set_bg_color() +{ // Copy plot background color std::vector bg_rgb; if (check_for_node(plot_node, "background")) { @@ -257,9 +268,9 @@ ObjectPlot::set_bg_color(){ } if (node_word_count(plot_node, "background") == 3) { bg_rgb = get_node_array(plot_node, "background"); - not_found.rgb[0] = bg_rgb[0]; - not_found.rgb[1] = bg_rgb[1]; - not_found.rgb[2] = bg_rgb[2]; + not_found.rgb[RED] = bg_rgb[RED]; + not_found.rgb[GREEN] = bg_rgb[GREEN]; + not_found.rgb[BLUE] = bg_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Bad background RGB in plot " @@ -268,14 +279,15 @@ ObjectPlot::set_bg_color(){ } } else { // default to a white background - not_found.rgb[0] = 255; - not_found.rgb[1] = 255; - not_found.rgb[2] = 255; + not_found.rgb[RED] = 255; + not_found.rgb[GREEN] = 255; + not_found.rgb[BLUE] = 255; } } void -ObjectPlot::set_basis() { +ObjectPlot::set_basis() +{ // Copy plot basis if (PLOT_TYPE::SLICE == type) { std::string pl_basis = "xy"; @@ -296,10 +308,11 @@ ObjectPlot::set_basis() { fatal_error(err_msg); } } -} +} void -ObjectPlot::set_origin() { +ObjectPlot::set_origin() +{ // Copy plotting origin std::vector pl_origin; if (node_word_count(plot_node, "origin") == 3) { @@ -316,8 +329,9 @@ ObjectPlot::set_origin() { } void -ObjectPlot::set_width() { - // Copy plotting width +ObjectPlot::set_width() +{ + // Copy plotting width std::vector pl_width; if (PLOT_TYPE::SLICE == type) { if (node_word_count(plot_node, "width") == 2) { @@ -346,7 +360,8 @@ ObjectPlot::set_width() { } void -ObjectPlot::set_universe() { +ObjectPlot::set_universe() +{ // Copy plot universe level if (check_for_node(plot_node, "level")) { level = std::stoi(get_node_value(plot_node, "level")); @@ -358,10 +373,11 @@ ObjectPlot::set_universe() { } else { level = PLOT_LEVEL_LOWEST; } -} +} void -ObjectPlot::set_default_colors() { +ObjectPlot::set_default_colors() +{ // Copy plot color type and initialize all colors randomly std::string pl_color_by = "cell"; if (check_for_node(plot_node, "color_by")) { @@ -370,23 +386,20 @@ ObjectPlot::set_default_colors() { } if ("cell" == pl_color_by) { color_by = PLOT_COLOR_BY::CELLS; - for(int i = 0; i < n_cells; i++) { colors.push_back(new ObjectColor()); - colors[i]->rgb[0] = int(prn()*255); - colors[i]->rgb[1] = int(prn()*255); - colors[i]->rgb[2] = int(prn()*255); + colors[i]->rgb[RED] = int(prn()*255); + colors[i]->rgb[GREEN] = int(prn()*255); + colors[i]->rgb[BLUE] = int(prn()*255); } } else if("material" == pl_color_by) { - color_by = PLOT_COLOR_BY::MATS; - for(int i = 0; i < materials.size(); i++) { colors.push_back(new ObjectColor()); - colors[i]->rgb[0] = int(prn()*255); - colors[i]->rgb[1] = int(prn()*255); - colors[i]->rgb[2] = int(prn()*255); + colors[i]->rgb[RED] = int(prn()*255); + colors[i]->rgb[GREEN] = int(prn()*255); + colors[i]->rgb[BLUE] = int(prn()*255); } } else { std::stringstream err_msg; @@ -397,8 +410,9 @@ ObjectPlot::set_default_colors() { } void -ObjectPlot::set_user_colors() { - // Get the number of nodes and get a list of them +ObjectPlot::set_user_colors() +{ + // Get the number of nodes and get a list of them std::vector color_nodes; color_nodes = get_child_nodes(plot_node, "color"); @@ -415,14 +429,12 @@ ObjectPlot::set_user_colors() { } 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")) { @@ -433,16 +445,15 @@ ObjectPlot::set_user_colors() { << id; fatal_error(err_msg); } - // Add RGB if (PLOT_COLOR_BY::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 - 1]->rgb[0] = cell_rgb[0]; - colors[col_id - 1]->rgb[1] = cell_rgb[1]; - colors[col_id - 1]->rgb[2] = cell_rgb[2]; + colors[col_id - 1]->rgb[RED] = cell_rgb[RED]; + colors[col_id - 1]->rgb[GREEN] = cell_rgb[GREEN]; + colors[col_id - 1]->rgb[BLUE] = cell_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Could not find cell " << col_id @@ -454,9 +465,9 @@ ObjectPlot::set_user_colors() { 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 - 1]->rgb[0] = mat_rgb[0]; - colors[col_id - 1]->rgb[1] = mat_rgb[1]; - colors[col_id - 1]->rgb[2] = mat_rgb[2]; + colors[col_id - 1]->rgb[RED] = mat_rgb[RED]; + colors[col_id - 1]->rgb[GREEN] = mat_rgb[GREEN]; + colors[col_id - 1]->rgb[BLUE] = mat_rgb[BLUE]; } else { std::stringstream err_msg; err_msg << "Could not find material " << col_id @@ -469,15 +480,15 @@ ObjectPlot::set_user_colors() { } void -ObjectPlot::set_meshlines() { - // Deal with meshlines +ObjectPlot::set_meshlines() +{ + // Deal with meshlines std::vector mesh_line_nodes; mesh_line_nodes = get_child_nodes(plot_node, "meshlines"); int n_meshlines = mesh_line_nodes.size(); if (n_meshlines != 0) { - if (PLOT_TYPE::VOXEL == type) { std::stringstream msg; msg << "Meshlines ignored in voxel plot " << id; @@ -598,14 +609,14 @@ ObjectPlot::set_meshlines() { } void -ObjectPlot::set_mask() { - // Deal with masks +ObjectPlot::set_mask() +{ + // 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) { - if (PLOT_TYPE::VOXEL == type) { if (openmc_master) { std::stringstream wrn_msg; @@ -663,13 +674,13 @@ ObjectPlot::set_mask() { if (std::find(iarray.begin(), iarray.end(), j) != iarray.end()) { if (check_for_node(mask_node, "background")) { std::vector bg_rgb = get_node_array(mask_node, "background"); - colors[j]->rgb[0] = bg_rgb[0]; - colors[j]->rgb[1] = bg_rgb[1]; - colors[j]->rgb[2] = bg_rgb[2]; + colors[j]->rgb[RED] = bg_rgb[RED]; + colors[j]->rgb[GREEN] = bg_rgb[GREEN]; + colors[j]->rgb[BLUE] = bg_rgb[BLUE]; } else { - colors[j]->rgb[0] = 255; - colors[j]->rgb[1] = 255; - colors[j]->rgb[2] = 255; + colors[j]->rgb[RED] = 255; + colors[j]->rgb[GREEN] = 255; + colors[j]->rgb[BLUE] = 255; } } } @@ -681,12 +692,13 @@ ObjectPlot::set_mask() { } } } - -ObjectPlot::ObjectPlot(pugi::xml_node plot_node) - : index_meshlines_mesh(-1), plot_node(plot_node) { + +ObjectPlot::ObjectPlot(pugi::xml_node plot_node): +index_meshlines_mesh(-1), plot_node(plot_node) +{ set_id(); - + set_type(); set_output_path(); @@ -696,21 +708,21 @@ ObjectPlot::ObjectPlot(pugi::xml_node plot_node) set_basis(); set_origin(); - + set_width(); - set_universe(); + set_universe(); set_default_colors(); - + set_user_colors(); set_meshlines(); - + set_mask(); plot_node = pugi::xml_node(); // set to null node after construction - + } // End ObjectPlot constructor //=============================================================================== @@ -718,7 +730,8 @@ ObjectPlot::ObjectPlot(pugi::xml_node plot_node) // current particle's position //=============================================================================== -void position_rgb(Particle* p, ObjectPlot* pl, int rgb[3], int &id) { +void position_rgb(Particle* p, ObjectPlot* pl, int rgb[3], int &id) +{ bool found_cell; p->n_coord = 1; @@ -805,7 +818,7 @@ void output_ppm(ObjectPlot* pl, const ImageData &data) } } // Close file - // THIS IS HERE TO MATCH FORTRAN VERSION, NOT NECESSARY + // THIS IS HERE TO MATCH FORTRAN VERSION, NOT TECHNICALLY NECESSARY of << std::endl; of.close(); } @@ -909,8 +922,7 @@ void draw_mesh_lines(ObjectPlot *pl, ImageData &data) } // end if(in mesh) } } // end outer loops - -} +} // end draw_mesh_lines //=============================================================================== // CREATE_VOXEL outputs a binary file that can be input into silomesh for 3D @@ -924,7 +936,8 @@ void draw_mesh_lines(ObjectPlot *pl, ImageData &data) // id. For 1 million voxels this produces a file of approximately 15MB. //=============================================================================== -void create_voxel(ObjectPlot *pl) { +void create_voxel(ObjectPlot *pl) +{ // compute voxel widths in each direction double vox[3]; @@ -1013,10 +1026,9 @@ void create_voxel(ObjectPlot *pl) { } - void -voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace, hid_t* dset, - hid_t* memspace) +voxel_init(hid_t file_id, const hsize_t* dims, + hid_t* dspace, hid_t* dset, hid_t* memspace) { // Create dataspace/dataset for voxel data *dspace = H5Screate_simple(3, dims, nullptr); diff --git a/src/string_functions.cpp b/src/string_functions.cpp index 6c077c8c0..2539586c3 100644 --- a/src/string_functions.cpp +++ b/src/string_functions.cpp @@ -28,15 +28,13 @@ void to_lower(std::string& str) for (int i = 0; i < str.size(); i++) str[i] = std::tolower(str[i]); } -int word_count(std::string const& str) { +int word_count(std::string const& str) +{ std::stringstream stream(str); std::string dum; int count = 0; while(stream >> dum) { count++; } return count; } - + } // namespace openmc - - - diff --git a/src/xml_interface.cpp b/src/xml_interface.cpp index 82c59a27b..fa50eb332 100644 --- a/src/xml_interface.cpp +++ b/src/xml_interface.cpp @@ -64,10 +64,16 @@ get_child_nodes(pugi::xml_node node, const char* name) pugi::xml_node current; std::vector node_list; - for (pugi::xml_node current : node.children(name)) { + // 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; }