diff --git a/include/openmc/capi.h b/include/openmc/capi.h index a9c1a88f9a..fdea48e9e1 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -138,9 +138,7 @@ extern "C" { extern int n_nuclides; extern int32_t n_realizations; extern int32_t n_sab_tables; - extern int32_t n_sources; extern int32_t n_tallies; - extern int32_t n_universes; // Variables that are shared by necessity (can be removed from public header // later) diff --git a/include/openmc/source.h b/include/openmc/source.h index efc6f12a61..45f4896b46 100644 --- a/include/openmc/source.h +++ b/include/openmc/source.h @@ -16,6 +16,18 @@ namespace openmc { +//============================================================================== +// Global variables +//============================================================================== + +class SourceDistribution; + +namespace model { + +extern std::vector external_sources; + +} // namespace model + //============================================================================== //! External source distribution //============================================================================== @@ -40,12 +52,6 @@ private: UPtrDist energy_; //!< Energy distribution }; -//============================================================================== -// Global variables -//============================================================================== - -extern std::vector external_sources; - //============================================================================== // Functions //============================================================================== diff --git a/include/openmc/surface.h b/include/openmc/surface.h index 23c3b61ec2..d0ada79ea1 100644 --- a/include/openmc/surface.h +++ b/include/openmc/surface.h @@ -32,13 +32,15 @@ extern "C" const int BC_PERIODIC; // Global variables //============================================================================== -extern "C" int32_t n_surfaces; - class Surface; -extern std::vector surfaces; +namespace model { + +extern std::vector surfaces; extern std::map surface_map; +} // namespace model + //============================================================================== //! Coordinates for an axis-aligned cube that bounds a geometric object. //============================================================================== diff --git a/src/cell.cpp b/src/cell.cpp index 5c341eb15c..f30758b382 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -318,7 +318,7 @@ CSGCell::CSGCell(pugi::xml_node cell_node) // Convert user IDs to surface indices. for (auto& r : region_) { if (r < OP_UNION) { - r = copysign(surface_map[abs(r)] + 1, r); + r = copysign(model::surface_map[abs(r)] + 1, r); } } @@ -422,7 +422,7 @@ CSGCell::distance(Position r, Direction u, int32_t on_surface) const // Calculate the distance to this surface. // Note the off-by-one indexing bool coincident {token == on_surface}; - double d {surfaces[abs(token)-1]->distance(r, u, coincident)}; + double d {model::surfaces[abs(token)-1]->distance(r, u, coincident)}; // Check if this distance is the new minimum. if (d < min_dist) { @@ -468,7 +468,7 @@ CSGCell::to_hdf5(hid_t cell_group) const } else { // Note the off-by-one indexing region_spec << " " - << copysign(surfaces[abs(token)-1]->id_, token); + << copysign(model::surfaces[abs(token)-1]->id_, token); } } write_string(group, "region", region_spec.str(), false); @@ -531,7 +531,7 @@ CSGCell::contains_simple(Position r, Direction u, int32_t on_surface) const return false; } else { // Note the off-by-one indexing - bool sense = surfaces[abs(token)-1]->sense(r, u); + bool sense = model::surfaces[abs(token)-1]->sense(r, u); if (sense != (token > 0)) {return false;} } } @@ -573,7 +573,7 @@ CSGCell::contains_complex(Position r, Direction u, int32_t on_surface) const stack[i_stack] = false; } else { // Note the off-by-one indexing - bool sense = surfaces[abs(token)-1]->sense(r, u); + bool sense = model::surfaces[abs(token)-1]->sense(r, u); stack[i_stack] = (sense == (token > 0)); } } diff --git a/src/dagmc.cpp b/src/dagmc.cpp index e6f086605c..11b1694897 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -93,7 +93,7 @@ void load_dagmc_geometry() } // initialize surface objects - n_surfaces = model::DAG->num_entities(2); + int n_surfaces = model::DAG->num_entities(2); surfaces.resize(n_surfaces); for (int i = 0; i < n_surfaces; i++) { @@ -129,8 +129,8 @@ void load_dagmc_geometry() } // add to global array and map - surfaces[i] = s; - surface_map[s->id_] = s->id_; + model::surfaces[i] = s; + model::surface_map[s->id_] = s->id_; } return; diff --git a/src/geometry.cpp b/src/geometry.cpp index 87c61ebfa3..fe3d52c68a 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -79,9 +79,9 @@ find_cell(Particle* p, int search_surf) { // the positive or negative side of the surface should be searched. const std::vector* search_cells; if (search_surf > 0) { - search_cells = &surfaces[search_surf-1]->neighbor_pos_; + search_cells = &model::surfaces[search_surf-1]->neighbor_pos_; } else if (search_surf < 0) { - search_cells = &surfaces[-search_surf-1]->neighbor_neg_; + search_cells = &model::surfaces[-search_surf-1]->neighbor_neg_; } else { // No surface was indicated, search all cells in the universe. search_cells = &model::universes[i_universe]->cells_; @@ -390,7 +390,7 @@ distance_to_boundary(Particle* p, double* dist, int* surface_crossed, *surface_crossed = level_surf_cross; } else { Position r_hit = r + d_surf * u; - Surface& surf {*surfaces[std::abs(level_surf_cross)-1]}; + Surface& surf {*model::surfaces[std::abs(level_surf_cross)-1]}; Direction norm = surf.normal(r_hit); if (u.dot(norm) > 0) { *surface_crossed = std::abs(level_surf_cross); diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 52d0ae92ad..8ebb70eb73 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -167,14 +167,14 @@ neighbor_lists() // This token is a surface index. Add the cell to the surface's list. if (token > 0) { - surfaces[std::abs(token)-1]->neighbor_pos_.push_back(i); + model::surfaces[std::abs(token)-1]->neighbor_pos_.push_back(i); } else { - surfaces[std::abs(token)-1]->neighbor_neg_.push_back(i); + model::surfaces[std::abs(token)-1]->neighbor_neg_.push_back(i); } } } - for (Surface* surf : surfaces) { + for (Surface* surf : model::surfaces) { surf->neighbor_pos_.shrink_to_fit(); surf->neighbor_neg_.shrink_to_fit(); } diff --git a/src/settings.cpp b/src/settings.cpp index b5bb4fe80c..36657c6c62 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -416,17 +416,17 @@ void read_settings_xml() // Get point to list of elements and make sure there is at least one for (pugi::xml_node node : root.children("source")) { - external_sources.emplace_back(node); + model::external_sources.emplace_back(node); } // If no source specified, default to isotropic point source at origin with Watt spectrum - if (external_sources.empty()) { + if (model::external_sources.empty()) { SourceDistribution source { UPtrSpace{new SpatialPoint({0.0, 0.0, 0.0})}, UPtrAngle{new Isotropic()}, UPtrDist{new Watt(0.988, 2.249e-6)} }; - external_sources.push_back(std::move(source)); + model::external_sources.push_back(std::move(source)); } // Check if we want to write out source diff --git a/src/source.cpp b/src/source.cpp index 1cb844ec9a..6d071c0c7a 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -27,8 +27,12 @@ namespace openmc { // Global variables //============================================================================== +namespace model { + std::vector external_sources; +} + //============================================================================== // SourceDistribution implementation //============================================================================== @@ -299,22 +303,22 @@ Bank sample_external_source() // Determine total source strength double total_strength = 0.0; - for (auto& s : external_sources) + for (auto& s : model::external_sources) total_strength += s.strength(); // Sample from among multiple source distributions int i = 0; - if (external_sources.size() > 1) { + if (model::external_sources.size() > 1) { double xi = prn()*total_strength; double c = 0.0; - for (; i < external_sources.size(); ++i) { - c += external_sources[i].strength(); + for (; i < model::external_sources.size(); ++i) { + c += model::external_sources[i].strength(); if (xi < c) break; } } // Sample source site from i-th source distribution - Bank site {external_sources[i].sample()}; + Bank site {model::external_sources[i].sample()}; // If running in MG, convert site % E to group if (!settings::run_CE) { @@ -335,13 +339,13 @@ Bank sample_external_source() extern "C" void free_memory_source() { - external_sources.clear(); + model::external_sources.clear(); } extern "C" double total_source_strength() { double strength = 0.0; - for (const auto& s : external_sources) { + for (const auto& s : model::external_sources) { strength += s.strength(); } return strength; diff --git a/src/summary.cpp b/src/summary.cpp index 185ad9cce0..398f304fbe 100644 --- a/src/summary.cpp +++ b/src/summary.cpp @@ -19,7 +19,7 @@ write_geometry(hid_t file_id) { #endif write_attribute(geom_group, "n_cells", model::cells.size()); - write_attribute(geom_group, "n_surfaces", surfaces.size()); + write_attribute(geom_group, "n_surfaces", model::surfaces.size()); write_attribute(geom_group, "n_universes", model::universes.size()); write_attribute(geom_group, "n_lattices", model::lattices.size()); @@ -28,7 +28,7 @@ write_geometry(hid_t file_id) { close_group(cells_group); auto surfaces_group = create_group(geom_group, "surfaces"); - for (Surface* surf : surfaces) surf->to_hdf5(surfaces_group); + for (Surface* surf : model::surfaces) surf->to_hdf5(surfaces_group); close_group(surfaces_group); auto universes_group = create_group(geom_group, "universes"); diff --git a/src/surface.cpp b/src/surface.cpp index 66217a58b6..806a2b92df 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -25,12 +25,13 @@ extern "C" const int BC_PERIODIC {3}; // Global variables //============================================================================== -int32_t n_surfaces; +namespace model { std::vector surfaces; - std::map surface_map; +} // namespace model + //============================================================================== // Helper functions for reading the "coeffs" node of an XML surface element //============================================================================== @@ -1063,13 +1064,14 @@ extern "C" void read_surfaces(pugi::xml_node* node) { // Count the number of surfaces. - for (pugi::xml_node surf_node: node->children("surface")) {n_surfaces++;} + int n_surfaces = 0; + for (pugi::xml_node surf_node : node->children("surface")) {n_surfaces++;} if (n_surfaces == 0) { fatal_error("No surfaces found in geometry.xml!"); } // Loop over XML surface elements and populate the array. - surfaces.reserve(n_surfaces); + model::surfaces.reserve(n_surfaces); { pugi::xml_node surf_node; int i_surf; @@ -1078,40 +1080,40 @@ read_surfaces(pugi::xml_node* node) std::string surf_type = get_node_value(surf_node, "type", true, true); if (surf_type == "x-plane") { - surfaces.push_back(new SurfaceXPlane(surf_node)); + model::surfaces.push_back(new SurfaceXPlane(surf_node)); } else if (surf_type == "y-plane") { - surfaces.push_back(new SurfaceYPlane(surf_node)); + model::surfaces.push_back(new SurfaceYPlane(surf_node)); } else if (surf_type == "z-plane") { - surfaces.push_back(new SurfaceZPlane(surf_node)); + model::surfaces.push_back(new SurfaceZPlane(surf_node)); } else if (surf_type == "plane") { - surfaces.push_back(new SurfacePlane(surf_node)); + model::surfaces.push_back(new SurfacePlane(surf_node)); } else if (surf_type == "x-cylinder") { - surfaces.push_back(new SurfaceXCylinder(surf_node)); + model::surfaces.push_back(new SurfaceXCylinder(surf_node)); } else if (surf_type == "y-cylinder") { - surfaces.push_back(new SurfaceYCylinder(surf_node)); + model::surfaces.push_back(new SurfaceYCylinder(surf_node)); } else if (surf_type == "z-cylinder") { - surfaces.push_back(new SurfaceZCylinder(surf_node)); + model::surfaces.push_back(new SurfaceZCylinder(surf_node)); } else if (surf_type == "sphere") { - surfaces.push_back(new SurfaceSphere(surf_node)); + model::surfaces.push_back(new SurfaceSphere(surf_node)); } else if (surf_type == "x-cone") { - surfaces.push_back(new SurfaceXCone(surf_node)); + model::surfaces.push_back(new SurfaceXCone(surf_node)); } else if (surf_type == "y-cone") { - surfaces.push_back(new SurfaceYCone(surf_node)); + model::surfaces.push_back(new SurfaceYCone(surf_node)); } else if (surf_type == "z-cone") { - surfaces.push_back(new SurfaceZCone(surf_node)); + model::surfaces.push_back(new SurfaceZCone(surf_node)); } else if (surf_type == "quadric") { - surfaces.push_back(new SurfaceQuadric(surf_node)); + model::surfaces.push_back(new SurfaceQuadric(surf_node)); } else { std::stringstream err_msg; @@ -1122,11 +1124,11 @@ read_surfaces(pugi::xml_node* node) } // Fill the surface map. - for (int i_surf = 0; i_surf < n_surfaces; i_surf++) { - int id = surfaces[i_surf]->id_; - auto in_map = surface_map.find(id); - if (in_map == surface_map.end()) { - surface_map[id] = i_surf; + for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) { + int id = model::surfaces[i_surf]->id_; + auto in_map = model::surface_map.find(id); + if (in_map == model::surface_map.end()) { + model::surface_map[id] = i_surf; } else { std::stringstream err_msg; err_msg << "Two or more surfaces use the same unique ID: " << id; @@ -1138,10 +1140,10 @@ read_surfaces(pugi::xml_node* node) double xmin {INFTY}, xmax {-INFTY}, ymin {INFTY}, ymax {-INFTY}, zmin {INFTY}, zmax {-INFTY}; int i_xmin, i_xmax, i_ymin, i_ymax, i_zmin, i_zmax; - for (int i_surf = 0; i_surf < n_surfaces; i_surf++) { - if (surfaces[i_surf]->bc_ == BC_PERIODIC) { + for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) { + if (model::surfaces[i_surf]->bc_ == BC_PERIODIC) { // Downcast to the PeriodicSurface type. - Surface* surf_base = surfaces[i_surf]; + Surface* surf_base = model::surfaces[i_surf]; PeriodicSurface* surf = dynamic_cast(surf_base); // Make sure this surface inherits from PeriodicSurface. @@ -1183,10 +1185,10 @@ read_surfaces(pugi::xml_node* node) } // Set i_periodic for periodic BC surfaces. - for (int i_surf = 0; i_surf < n_surfaces; i_surf++) { - if (surfaces[i_surf]->bc_ == BC_PERIODIC) { + for (int i_surf = 0; i_surf < model::surfaces.size(); i_surf++) { + if (model::surfaces[i_surf]->bc_ == BC_PERIODIC) { // Downcast to the PeriodicSurface type. - Surface* surf_base = surfaces[i_surf]; + Surface* surf_base = model::surfaces[i_surf]; PeriodicSurface* surf = dynamic_cast(surf_base); // Also try downcasting to the SurfacePlane type (which must be handled @@ -1216,7 +1218,7 @@ read_surfaces(pugi::xml_node* node) } } else { // Convert the surface id to an index. - surf->i_periodic_ = surface_map[surf->i_periodic_]; + surf->i_periodic_ = model::surface_map[surf->i_periodic_]; } } else { // This is a SurfacePlane. We won't try to find it's partner if the @@ -1228,12 +1230,12 @@ read_surfaces(pugi::xml_node* node) fatal_error(err_msg); } else { // Convert the surface id to an index. - surf->i_periodic_ = surface_map[surf->i_periodic_]; + surf->i_periodic_ = model::surface_map[surf->i_periodic_]; } } // Make sure the opposite surface is also periodic. - if (surfaces[surf->i_periodic_]->bc_ != BC_PERIODIC) { + if (model::surfaces[surf->i_periodic_]->bc_ != BC_PERIODIC) { std::stringstream err_msg; err_msg << "Could not find matching surface for periodic boundary " "condition on surface " << surf->id_; @@ -1248,7 +1250,7 @@ read_surfaces(pugi::xml_node* node) //============================================================================== extern "C" { - Surface* surface_pointer(int surf_ind) {return surfaces[surf_ind];} + Surface* surface_pointer(int surf_ind) {return model::surfaces[surf_ind];} int surface_id(Surface* surf) {return surf->id_;} @@ -1288,10 +1290,9 @@ extern "C" { void free_memory_surfaces_c() { - for (Surface* surf : surfaces) {delete surf;} - surfaces.clear(); - n_surfaces = 0; - surface_map.clear(); + for (Surface* surf : model::surfaces) {delete surf;} + model::surfaces.clear(); + model::surface_map.clear(); } } diff --git a/src/tallies/filter_surface.cpp b/src/tallies/filter_surface.cpp index 5458d20519..5b175ca708 100644 --- a/src/tallies/filter_surface.cpp +++ b/src/tallies/filter_surface.cpp @@ -20,8 +20,8 @@ SurfaceFilter::initialize() { // Convert surface IDs to indices of the global array. for (auto& s : surfaces_) { - auto search = surface_map.find(s); - if (search != surface_map.end()) { + auto search = model::surface_map.find(s); + if (search != model::surface_map.end()) { s = search->second; } else { std::stringstream err_msg; @@ -58,7 +58,7 @@ SurfaceFilter::to_statepoint(hid_t filter_group) const { Filter::to_statepoint(filter_group); std::vector surface_ids; - for (auto c : surfaces_) surface_ids.push_back(surfaces[c]->id_); + for (auto c : surfaces_) surface_ids.push_back(model::surfaces[c]->id_); write_dataset(filter_group, "bins", surface_ids); } @@ -66,7 +66,7 @@ std::string SurfaceFilter::text_label(int bin) const { //TODO: off-by-one - return "Surface " + std::to_string(surfaces[surfaces_[bin-1]]->id_); + return "Surface " + std::to_string(model::surfaces[surfaces_[bin-1]]->id_); } } // namespace openmc