diff --git a/src/cell.cpp b/src/cell.cpp index 8cdc7b6f64..e4388e7012 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -33,11 +33,11 @@ extern "C" double FP_PRECISION; int32_t n_cells {0}; -std::vector cells_c; -std::unordered_map cell_dict; +std::vector global_cells; +std::unordered_map cell_map; -std::vector universes_c; -std::unordered_map universe_dict; +std::vector global_universes; +std::unordered_map universe_map; //============================================================================== //! Convert region specification string to integer tokens. @@ -246,7 +246,7 @@ Cell::Cell(pugi::xml_node cell_node) } // Read the region specification. - std::string region_spec {""}; + std::string region_spec; if (check_for_node(cell_node, "region")) { region_spec = get_node_value(cell_node, "region"); } @@ -256,10 +256,9 @@ Cell::Cell(pugi::xml_node cell_node) region.shrink_to_fit(); // Convert user IDs to surface indices. - // Note that the index has 1 added to it in order to preserve the sign. - for (auto it = region.begin(); it != region.end(); it++) { - if (*it < OP_UNION) { - *it = copysign(surface_dict[abs(*it)]+1, *it); + for (auto &r : region) { + if (r < OP_UNION) { + r = copysign(surface_map[abs(r)] + 1, r); } } @@ -330,8 +329,8 @@ Cell::to_hdf5(hid_t cell_group) const } //TODO: Fix the off-by-one indexing. - write_int(cell_group, 0, nullptr, "universe", &universes_c[universe-1]->id, - false); + write_int(cell_group, 0, nullptr, "universe", + &global_universes[universe-1]->id, false); // Write the region specification. if (!region.empty()) { @@ -446,24 +445,24 @@ read_cells(pugi::xml_node *node) } // Allocate the vector of Cells. - cells_c.reserve(n_cells); + global_cells.reserve(n_cells); // Loop over XML cell elements and populate the array. for (pugi::xml_node cell_node: node->children("cell")) { - cells_c.push_back(new Cell(cell_node)); + global_cells.push_back(new Cell(cell_node)); } - // Populate the Universe vector and dictionary. - for (int i = 0; i < cells_c.size(); i++) { - int32_t uid = cells_c[i]->universe; - auto it = universe_dict.find(uid); - if (it == universe_dict.end()) { - universes_c.push_back(new Universe()); - universes_c.back()->id = uid; - universes_c.back()->cells.push_back(i); - universe_dict[uid] = universes_c.size() - 1; + // Populate the Universe vector and map. + for (int i = 0; i < global_cells.size(); i++) { + int32_t uid = global_cells[i]->universe; + auto it = universe_map.find(uid); + if (it == universe_map.end()) { + global_universes.push_back(new Universe()); + global_universes.back()->id = uid; + global_universes.back()->cells.push_back(i); + universe_map[uid] = global_universes.size() - 1; } else { - universes_c[it->second]->cells.push_back(i); + global_universes[it->second]->cells.push_back(i); } } } @@ -473,7 +472,7 @@ read_cells(pugi::xml_node *node) //============================================================================== extern "C" { - Cell* cell_pointer(int32_t cell_ind) {return cells_c[cell_ind];} + Cell* cell_pointer(int32_t cell_ind) {return global_cells[cell_ind];} int32_t cell_id(Cell *c) {return c->id;} @@ -499,11 +498,11 @@ extern "C" { {return c->contains(xyz, uvw, on_surface);} void cell_distance(Cell *c, double xyz[3], double uvw[3], int32_t on_surface, - double &min_dist, int32_t &i_surf) + double *min_dist, int32_t *i_surf) { std::pair out = c->distance(xyz, uvw, on_surface); - min_dist = out.first; - i_surf = out.second; + *min_dist = out.first; + *i_surf = out.second; } int32_t cell_offset(Cell *c, int map) {return c->offset[map];} @@ -512,11 +511,11 @@ extern "C" { void extend_cells_c(int32_t n) { - cells_c.reserve(cells_c.size() + n); + global_cells.reserve(global_cells.size() + n); for (int32_t i = 0; i < n; i++) { - cells_c.push_back(new Cell()); + global_cells.push_back(new Cell()); } - n_cells = cells_c.size(); + n_cells = global_cells.size(); } } diff --git a/src/cell.h b/src/cell.h index f55c8e6fd4..a2f72c8337 100644 --- a/src/cell.h +++ b/src/cell.h @@ -27,12 +27,12 @@ extern "C" int FILL_LATTICE; extern "C" int32_t n_cells; class Cell; -extern std::vector cells_c; -extern std::unordered_map cell_dict; +extern std::vector global_cells; +extern std::unordered_map cell_map; class Universe; -extern std::vector universes_c; -extern std::unordered_map universe_dict; +extern std::vector global_universes; +extern std::unordered_map universe_map; //============================================================================== //! A geometry primitive that fills all space and contains cells. @@ -54,7 +54,7 @@ class Cell { public: int32_t id; //!< Unique ID - std::string name{""}; //!< User-defined name + std::string name; //!< User-defined name int type; //!< Material, universe, or lattice int32_t universe; //!< Universe # this cell is in int32_t fill; //!< Universe # filling this cell diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index dc31edf7e4..3f36cf299a 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -9,8 +9,6 @@ #include "error.h" #include "lattice.h" -#include //TODO: remove this - namespace openmc { @@ -20,15 +18,15 @@ void adjust_indices_c() { // Adjust material/fill idices. - for (Cell *c : cells_c) { + for (Cell *c : global_cells) { if (c->material[0] == C_NONE) { int32_t id = c->fill; - auto search_univ = universe_dict.find(id); - auto search_lat = lattice_dict.find(id); - if (search_univ != universe_dict.end()) { + auto search_univ = universe_map.find(id); + auto search_lat = lattice_map.find(id); + if (search_univ != universe_map.end()) { c->type = FILL_UNIVERSE; c->fill = search_univ->second; - } else if (search_lat != lattice_dict.end()) { + } else if (search_lat != lattice_map.end()) { c->type = FILL_LATTICE; c->fill = search_lat->second; } else { @@ -44,9 +42,9 @@ adjust_indices_c() } // Change cell.universe values from IDs to indices. - for (Cell *c : cells_c) { - auto search = universe_dict.find(c->universe); - if (search != universe_dict.end()) { + for (Cell *c : global_cells) { + auto search = universe_map.find(c->universe); + if (search != universe_map.end()) { //TODO: Remove this off-by-one indexing. c->universe = search->second + 1; } else { @@ -70,7 +68,7 @@ find_root_universe() { // Find all the universes listed as a cell fill. std::unordered_set fill_univ_ids; - for (Cell *c : cells_c) { + for (Cell *c : global_cells) { fill_univ_ids.insert(c->fill); } @@ -87,8 +85,8 @@ find_root_universe() // Figure out which universe is not in the set. This is the root universe. bool root_found {false}; int32_t root_univ; - for (int32_t i = 0; i < universes_c.size(); i++) { - auto search = fill_univ_ids.find(universes_c[i]->id); + for (int32_t i = 0; i < global_universes.size(); i++) { + auto search = fill_univ_ids.find(global_universes[i]->id); if (search == fill_univ_ids.end()) { if (root_found) { fatal_error("Two or more universes are not used as fill universes, so " @@ -111,7 +109,7 @@ find_root_universe() void allocate_offset_tables(int n_maps) { - for (Cell *c : cells_c) { + for (Cell *c : global_cells) { if (c->type != FILL_MATERIAL) { c->offset.resize(n_maps, C_NONE); } @@ -127,8 +125,8 @@ allocate_offset_tables(int n_maps) void count_cell_instances(int32_t univ_indx) { - for (int32_t cell_indx : universes_c[univ_indx]->cells) { - Cell &c = *cells_c[cell_indx]; + for (int32_t cell_indx : global_universes[univ_indx]->cells) { + Cell &c = *global_cells[cell_indx]; ++c.n_instances; if (c.type == FILL_UNIVERSE) { @@ -151,13 +149,13 @@ int count_universe_instances(int32_t search_univ, int32_t target_univ_id) { // If this is the target, it can't contain itself. - if (universes_c[search_univ]->id == target_univ_id) { + if (global_universes[search_univ]->id == target_univ_id) { return 1; } int count {0}; - for (int32_t cell_indx : universes_c[search_univ]->cells) { - Cell &c = *cells_c[cell_indx]; + for (int32_t cell_indx : global_universes[search_univ]->cells) { + Cell &c = *global_cells[cell_indx]; if (c.type == FILL_UNIVERSE) { int32_t next_univ = c.fill; @@ -180,10 +178,10 @@ count_universe_instances(int32_t search_univ, int32_t target_univ_id) void fill_offset_tables(int32_t target_univ_id, int map) { - for (Universe *univ : universes_c) { + for (Universe *univ : global_universes) { int32_t offset {0}; // TODO: is this a bug? It matches F90 implementation. for (int32_t cell_indx : univ->cells) { - Cell &c = *cells_c[cell_indx]; + Cell &c = *global_cells[cell_indx]; if (c.type == FILL_UNIVERSE) { c.offset[map] = offset; @@ -212,7 +210,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, // write to the path and return. for (int32_t cell_indx : search_univ.cells) { if ((cell_indx == target_cell) && (offset == target_offset)) { - Cell &c = *cells_c[cell_indx]; + Cell &c = *global_cells[cell_indx]; path << "c" << c.id; return path.str(); } @@ -224,7 +222,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, std::vector::const_reverse_iterator cell_it {search_univ.cells.crbegin()}; for (; cell_it != search_univ.cells.crend(); ++cell_it) { - Cell &c = *cells_c[*cell_it]; + Cell &c = *global_cells[*cell_it]; // Material cells don't contain other cells so ignore them. if (c.type != FILL_MATERIAL) { @@ -244,14 +242,14 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, } // Add the cell to the path string. - Cell &c = *cells_c[*cell_it]; + Cell &c = *global_cells[*cell_it]; path << "c" << c.id << "->"; if (c.type == FILL_UNIVERSE) { // Recurse into the fill cell. offset += c.offset[map]; path << distribcell_path_inner(target_cell, map, target_offset, - *universes_c[c.fill], offset); + *global_universes[c.fill], offset); return path.str(); } else { // Recurse into the lattice cell. @@ -264,7 +262,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, offset = temp_offset; path << "(" << lat.index_to_string(it.indx) << ")->"; path << distribcell_path_inner(target_cell, map, target_offset, - *universes_c[*it], offset); + *global_universes[*it], offset); return path.str(); } } @@ -277,7 +275,7 @@ int distribcell_path_len(int32_t target_cell, int32_t map, int32_t target_offset, int32_t root_univ) { - Universe &root = *universes_c[root_univ]; + Universe &root = *global_universes[root_univ]; std::string path_ {distribcell_path_inner(target_cell, map, target_offset, root, 0)}; return path_.size() + 1; @@ -289,7 +287,7 @@ void distribcell_path(int32_t target_cell, int32_t map, int32_t target_offset, int32_t root_univ, char *path) { - Universe &root = *universes_c[root_univ]; + Universe &root = *global_universes[root_univ]; std::string path_ {distribcell_path_inner(target_cell, map, target_offset, root, 0)}; path_.copy(path, path_.size()); @@ -303,8 +301,8 @@ maximum_levels(int32_t univ) { int levels_below {0}; - for (int32_t cell_indx : universes_c[univ]->cells) { - Cell &c = *cells_c[cell_indx]; + for (int32_t cell_indx : global_universes[univ]->cells) { + Cell &c = *global_cells[cell_indx]; if (c.type == FILL_UNIVERSE) { int32_t next_univ = c.fill; levels_below = std::max(levels_below, maximum_levels(next_univ)); @@ -326,18 +324,18 @@ maximum_levels(int32_t univ) void free_memory_geometry_c() { - for (Cell *c : cells_c) {delete c;} - cells_c.clear(); - cell_dict.clear(); + for (Cell *c : global_cells) {delete c;} + global_cells.clear(); + cell_map.clear(); n_cells = 0; - for (Universe *u : universes_c) {delete u;} - universes_c.clear(); - universe_dict.clear(); + for (Universe *u : global_universes) {delete u;} + global_universes.clear(); + universe_map.clear(); for (Lattice *lat : lattices_c) {delete lat;} lattices_c.clear(); - lattice_dict.clear(); + lattice_map.clear(); } } // namespace openmc diff --git a/src/lattice.cpp b/src/lattice.cpp index 2d9bf02c58..959ab932e4 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -8,6 +8,7 @@ #include "error.h" #include "geometry_aux.h" #include "hdf5_interface.h" +#include "string_utils.h" #include "xml_interface.h" @@ -19,7 +20,7 @@ namespace openmc { std::vector lattices_c; -std::unordered_map lattice_dict; +std::unordered_map lattice_map; //============================================================================== // Lattice implementation @@ -64,8 +65,8 @@ Lattice::adjust_indices() // Adjust the indices for the universes array. for (LatticeIter it = begin(); it != end(); ++it) { int uid = *it; - auto search = universe_dict.find(uid); - if (search != universe_dict.end()) { + auto search = universe_map.find(uid); + if (search != universe_map.end()) { *it = search->second; } else { std::stringstream err_msg; @@ -77,8 +78,8 @@ Lattice::adjust_indices() // Adjust the index for the outer universe. if (outer != NO_OUTER_UNIVERSE) { - auto search = universe_dict.find(outer); - if (search != universe_dict.end()) { + auto search = universe_map.find(outer); + if (search != universe_map.end()) { outer = search->second; } else { std::stringstream err_msg; @@ -117,7 +118,7 @@ Lattice::to_hdf5(hid_t lattices_group) const } if (outer != NO_OUTER_UNIVERSE) { - int32_t outer_id = universes_c[outer]->id; + int32_t outer_id = global_universes[outer]->id; write_int(lat_group, 0, nullptr, "outer", &outer_id, false); } else { write_int(lat_group, 0, nullptr, "outer", &outer, false); @@ -372,7 +373,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const for (int j = 0; j < nx; j++) { int indx1 = nx*ny*m + nx*k + j; int indx2 = nx*ny*m + nx*(ny-k-1) + j; - out[indx2] = universes_c[universes[indx1]]->id; + out[indx2] = global_universes[universes[indx1]]->id; } } } @@ -389,7 +390,7 @@ RectLattice::to_hdf5_inner(hid_t lat_group) const for (int j = 0; j < nx; j++) { int indx1 = nx*k + j; int indx2 = nx*(ny-k-1) + j; - out[indx2] = universes_c[universes[indx1]]->id; + out[indx2] = global_universes[universes[indx1]]->id; } } @@ -854,7 +855,7 @@ HexLattice::to_hdf5_inner(hid_t lat_group) const // This array position is never used; put a -1 to indicate this. out[indx] = -1; } else { - out[indx] = universes_c[universes[indx]]->id; + out[indx] = global_universes[universes[indx]]->id; } } } @@ -871,19 +872,19 @@ HexLattice::to_hdf5_inner(hid_t lat_group) const extern "C" void read_lattices(pugi::xml_node *node) { - for (pugi::xml_node lat_node: node->children("lattice")) { + for (pugi::xml_node lat_node : node->children("lattice")) { lattices_c.push_back(new RectLattice(lat_node)); } - for (pugi::xml_node lat_node: node->children("hex_lattice")) { + for (pugi::xml_node lat_node : node->children("hex_lattice")) { lattices_c.push_back(new HexLattice(lat_node)); } - // Fill the lattice dictionary. + // Fill the lattice map. for (int i_lat = 0; i_lat < lattices_c.size(); i_lat++) { int id = lattices_c[i_lat]->id; - auto in_dict = lattice_dict.find(id); - if (in_dict == lattice_dict.end()) { - lattice_dict[id] = i_lat; + auto in_map = lattice_map.find(id); + if (in_map == lattice_map.end()) { + lattice_map[id] = i_lat; } else { std::stringstream err_msg; err_msg << "Two or more lattices use the same unique ID: " << id; diff --git a/src/lattice.h b/src/lattice.h index 909029aa90..19749ce4f0 100644 --- a/src/lattice.h +++ b/src/lattice.h @@ -27,7 +27,7 @@ constexpr int32_t NO_OUTER_UNIVERSE{-1}; class Lattice; extern std::vector lattices_c; -extern std::unordered_map lattice_dict; +extern std::unordered_map lattice_map; //============================================================================== //! \class Lattice @@ -58,7 +58,7 @@ public: virtual ReverseLatticeIter rbegin(); ReverseLatticeIter rend(); - //! Convert internal universe values from IDs to indices using universe_dict. + //! Convert internal universe values from IDs to indices using universe_map. void adjust_indices(); //! Allocate offset table for distribcell. diff --git a/src/string_utils.h b/src/string_utils.h new file mode 100644 index 0000000000..e2cb773ab1 --- /dev/null +++ b/src/string_utils.h @@ -0,0 +1,35 @@ +#ifndef STRING_UTILS_H +#define STRING_UTILS_H + +#include +#include + + +namespace openmc { + +std::vector +split(const std::string &in) +{ + std::vector out; + + for (int i = 0; i < in.size(); ) { + // Increment i until we find a non-whitespace character. + if (std::isspace(in[i])) { + i++; + + } else { + // Find the next whitespace character at j. + int j = i + 1; + while (j < in.size() && std::isspace(in[j]) == 0) {j++;} + + // Push-back everything between i and j. + out.push_back(in.substr(i, j-i)); + i = j + 1; // j is whitespace so leapfrog to j+1 + } + } + + return out; +} + +} // namespace openmc +#endif // STRING_UTILS_H diff --git a/src/surface.cpp b/src/surface.cpp index 1155559fd3..56e57a854f 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -28,7 +28,7 @@ int32_t n_surfaces; Surface **surfaces_c; -std::map surface_dict; +std::map surface_map; //============================================================================== // Helper functions for reading the "coeffs" node of an XML surface element @@ -1129,12 +1129,12 @@ read_surfaces(pugi::xml_node *node) } } - // Fill the surface dictionary. + // Fill the surface map. for (int i_surf = 0; i_surf < n_surfaces; i_surf++) { int id = surfaces_c[i_surf]->id; - auto in_dict = surface_dict.find(id); - if (in_dict == surface_dict.end()) { - surface_dict[id] = i_surf; + auto in_map = surface_map.find(id); + if (in_map == surface_map.end()) { + surface_map[id] = i_surf; } else { std::stringstream err_msg; err_msg << "Two or more surfaces use the same unique ID: " << id; @@ -1224,7 +1224,7 @@ read_surfaces(pugi::xml_node *node) } } else { // Convert the surface id to an index. - surf->i_periodic = surface_dict[surf->i_periodic]; + surf->i_periodic = surface_map[surf->i_periodic]; } } else { // This is a SurfacePlane. We won't try to find it's partner if the @@ -1236,7 +1236,7 @@ read_surfaces(pugi::xml_node *node) fatal_error(err_msg); } else { // Convert the surface id to an index. - surf->i_periodic = surface_dict[surf->i_periodic]; + surf->i_periodic = surface_map[surf->i_periodic]; } } @@ -1283,7 +1283,7 @@ extern "C" { delete surfaces_c; surfaces_c = nullptr; n_surfaces = 0; - surface_dict.clear(); + surface_map.clear(); } } diff --git a/src/surface.h b/src/surface.h index 99c51d7473..1d8cc6ac7b 100644 --- a/src/surface.h +++ b/src/surface.h @@ -31,7 +31,7 @@ extern "C" int32_t n_surfaces; class Surface; extern Surface **surfaces_c; -extern std::map surface_dict; +extern std::map surface_map; //============================================================================== //! Coordinates for an axis-aligned cube that bounds a geometric object. @@ -58,7 +58,7 @@ public: //int neighbor_pos[], //!< List of cells on positive side // neighbor_neg[]; //!< List of cells on negative side int bc; //!< Boundary condition - std::string name{""}; //!< User-defined name + std::string name; //!< User-defined name explicit Surface(pugi::xml_node surf_node); diff --git a/src/xml_interface.cpp b/src/xml_interface.cpp index 77859ede8c..b4fcc524c6 100644 --- a/src/xml_interface.cpp +++ b/src/xml_interface.cpp @@ -35,28 +35,4 @@ get_node_value(pugi::xml_node node, const char *name) return value; } -std::vector -split(const std::string in) -{ - std::vector out; - - for (int i = 0; i < in.size(); ) { - // Increment i until we find a non-whitespace character. - if (std::isspace(in[i])) { - i++; - - } else { - // Find the next whitespace character at j. - int j = i + 1; - while (j < in.size() && std::isspace(in[j]) == 0) {j++;} - - // Push-back everything between i and j. - out.push_back(in.substr(i, j-i)); - i = j + 1; // j is whitespace so leapfrog to j+1 - } - } - - return out; -} - } // namespace openmc diff --git a/src/xml_interface.h b/src/xml_interface.h index 41252ed611..b6f9e3246d 100644 --- a/src/xml_interface.h +++ b/src/xml_interface.h @@ -17,7 +17,5 @@ check_for_node(pugi::xml_node node, const char *name) std::string get_node_value(pugi::xml_node node, const char *name); -std::vector split(const std::string in); - } // namespace openmc #endif // XML_INTERFACE_H