From 6436cab30206e41bed29e44e88a5743a68a3fadc Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Thu, 8 Nov 2018 13:41:23 -0600 Subject: [PATCH] More namespacing for global variables --- include/openmc/capi.h | 2 -- include/openmc/dagmc.h | 14 ++++++++++- include/openmc/geometry.h | 6 ++++- include/openmc/lattice.h | 6 ++++- src/cell.cpp | 4 +-- src/dagmc.cpp | 40 +++++++++++++++--------------- src/finalize.cpp | 2 +- src/geometry.cpp | 27 ++++++++++++++------ src/geometry_aux.cpp | 32 ++++++++++++------------ src/geometry_header.F90 | 2 +- src/lattice.cpp | 21 +++++++++------- src/output.cpp | 10 ++++---- src/plot.cpp | 4 +-- src/summary.cpp | 4 +-- src/tallies/filter_distribcell.cpp | 2 +- 15 files changed, 105 insertions(+), 71 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 91d038d46..a04f1a622 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -135,14 +135,12 @@ extern "C" { // Global variables extern char openmc_err_msg[256]; - extern int32_t n_lattices; extern int32_t n_materials; extern int n_nuclides; extern int32_t n_plots; extern int32_t n_realizations; extern int32_t n_sab_tables; extern int32_t n_sources; - extern int32_t n_surfaces; extern int32_t n_tallies; extern int32_t n_universes; diff --git a/include/openmc/dagmc.h b/include/openmc/dagmc.h index afbd9cbc6..51d97d737 100644 --- a/include/openmc/dagmc.h +++ b/include/openmc/dagmc.h @@ -10,12 +10,24 @@ namespace openmc { +//============================================================================== +// Global variables +//============================================================================== + +namespace model { + extern moab::DagMC* DAG; +} // namespace model + +//============================================================================== +// Non-member functions +//============================================================================== + extern "C" void load_dagmc_geometry(); extern "C" void free_memory_dagmc(); -} +} // namespace openmc #endif // DAGMC diff --git a/include/openmc/geometry.h b/include/openmc/geometry.h index f045209ee..2f0853e0c 100644 --- a/include/openmc/geometry.h +++ b/include/openmc/geometry.h @@ -13,10 +13,14 @@ namespace openmc { // Global variables //============================================================================== -extern "C" int openmc_root_universe; +namespace model { + +extern "C" int root_universe; extern std::vector overlap_check_count; +} // namespace model + //============================================================================== //! Check for overlapping cells at a particle's position. //============================================================================== diff --git a/include/openmc/lattice.h b/include/openmc/lattice.h index cacb87abe..5a5eff0b4 100644 --- a/include/openmc/lattice.h +++ b/include/openmc/lattice.h @@ -31,10 +31,14 @@ enum class LatticeType { //============================================================================== class Lattice; -extern std::vector lattices; +namespace model { + +extern std::vector lattices; extern std::unordered_map lattice_map; +} // namespace model + //============================================================================== //! \class Lattice //! \brief Abstract type for ordered array of universes. diff --git a/src/cell.cpp b/src/cell.cpp index 2e4c0144a..8c5aaf29c 100644 --- a/src/cell.cpp +++ b/src/cell.cpp @@ -509,7 +509,7 @@ CSGCell::to_hdf5(hid_t cell_group) const } else if (type_ == FILL_LATTICE) { write_dataset(group, "fill_type", "lattice"); - write_dataset(group, "lattice", lattices[fill_]->id_); + write_dataset(group, "lattice", model::lattices[fill_]->id_); } close_group(group); @@ -682,7 +682,7 @@ read_cells(pugi::xml_node* node) model::universes.shrink_to_fit(); // Allocate the cell overlap count if necessary. - if (settings::check_overlaps) overlap_check_count.resize(model::n_cells, 0); + if (settings::check_overlaps) model::overlap_check_count.resize(model::n_cells, 0); } //============================================================================== diff --git a/src/dagmc.cpp b/src/dagmc.cpp index f11fbe203..e6f086605 100644 --- a/src/dagmc.cpp +++ b/src/dagmc.cpp @@ -18,16 +18,16 @@ moab::DagMC* DAG; void load_dagmc_geometry() { - if (!DAG) { - DAG = new moab::DagMC(); + if (!model::DAG) { + model::DAG = new moab::DagMC(); } int32_t dagmc_univ_id = 0; // universe is always 0 for DAGMC - moab::ErrorCode rval = DAG->load_file("dagmc.h5m"); + moab::ErrorCode rval = model::DAG->load_file("dagmc.h5m"); MB_CHK_ERR_CONT(rval); - rval = DAG->init_OBBTree(); + rval = model::DAG->init_OBBTree(); MB_CHK_ERR_CONT(rval); std::vector prop_keywords; @@ -35,22 +35,22 @@ void load_dagmc_geometry() prop_keywords.push_back("boundary"); std::map ph; - DAG->parse_properties(prop_keywords, ph, ":"); + model::DAG->parse_properties(prop_keywords, ph, ":"); MB_CHK_ERR_CONT(rval); // initialize cell objects - model::n_cells = DAG->num_entities(3); + model::n_cells = model::DAG->num_entities(3); // Allocate the cell overlap count if necessary. - if (settings::check_overlaps) overlap_check_count.resize(model::n_cells, 0); + if (settings::check_overlaps) model::overlap_check_count.resize(model::n_cells, 0); for (int i = 0; i < model::n_cells; i++) { - moab::EntityHandle vol_handle = DAG->entity_by_index(3, i+1); + moab::EntityHandle vol_handle = model::DAG->entity_by_index(3, i+1); // set cell ids using global IDs DAGCell* c = new DAGCell(); - c->id_ = DAG->id_by_index(3, i+1); - c->dagmc_ptr_ = DAG; + c->id_ = model::DAG->id_by_index(3, i+1); + c->dagmc_ptr_ = model::DAG; c->universe_ = dagmc_univ_id; // set to zero for now c->fill_ = C_NONE; // no fill, single universe @@ -68,15 +68,15 @@ void load_dagmc_geometry() model::universes[it->second]->cells_.push_back(i); } - if (DAG->is_implicit_complement(vol_handle)) { + if (model::DAG->is_implicit_complement(vol_handle)) { // assuming implicit complement is void for now c->material_.push_back(MATERIAL_VOID); continue; } - if (DAG->has_prop(vol_handle, "mat")){ + if (model::DAG->has_prop(vol_handle, "mat")){ std::string mat_value; - rval = DAG->prop_value(vol_handle, "mat", mat_value); + rval = model::DAG->prop_value(vol_handle, "mat", mat_value); MB_CHK_ERR_CONT(rval); to_lower(mat_value); @@ -93,20 +93,20 @@ void load_dagmc_geometry() } // initialize surface objects - n_surfaces = DAG->num_entities(2); + n_surfaces = model::DAG->num_entities(2); surfaces.resize(n_surfaces); for (int i = 0; i < n_surfaces; i++) { - moab::EntityHandle surf_handle = DAG->entity_by_index(2, i+1); + moab::EntityHandle surf_handle = model::DAG->entity_by_index(2, i+1); // set cell ids using global IDs DAGSurface* s = new DAGSurface(); - s->id_ = DAG->id_by_index(2, i+1); - s->dagmc_ptr_ = DAG; + s->id_ = model::DAG->id_by_index(2, i+1); + s->dagmc_ptr_ = model::DAG; - if (DAG->has_prop(surf_handle, "boundary")) { + if (model::DAG->has_prop(surf_handle, "boundary")) { std::string bc_value; - rval = DAG->prop_value(surf_handle, "boundary", bc_value); + rval = model::DAG->prop_value(surf_handle, "boundary", bc_value); MB_CHK_ERR_CONT(rval); to_lower(bc_value); @@ -138,7 +138,7 @@ void load_dagmc_geometry() void free_memory_dagmc() { - delete DAG; + delete model::DAG; } } diff --git a/src/finalize.cpp b/src/finalize.cpp index b9d17eac3..7439e7bdb 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -78,7 +78,7 @@ int openmc_finalize() energy_max = {INFTY, INFTY}; energy_min = {0.0, 0.0}; n_tallies = 0; - openmc_root_universe = -1; + model::root_universe = -1; openmc_set_seed(DEFAULT_SEED); // Deallocate arrays diff --git a/src/geometry.cpp b/src/geometry.cpp index e4591cf9e..87c61ebfa 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -14,8 +14,21 @@ namespace openmc { +//============================================================================== +// Global variables +//============================================================================== + + +namespace model { + +int root_universe {-1}; + std::vector overlap_check_count; +} // namespace model + +//============================================================================== +// Non-member functions //============================================================================== extern "C" bool @@ -38,7 +51,7 @@ check_cell_overlap(Particle* p) { << univ.id_; fatal_error(err_msg); } - ++overlap_check_count[index_cell]; + ++model::overlap_check_count[index_cell]; } } } @@ -57,8 +70,8 @@ find_cell(Particle* p, int search_surf) { // Determine universe (if not yet set, use root universe) int i_universe = p->coord[p->n_coord-1].universe; if (i_universe == C_NONE) { - p->coord[p->n_coord-1].universe = openmc_root_universe; - i_universe = openmc_root_universe; + p->coord[p->n_coord-1].universe = model::root_universe; + i_universe = model::root_universe; } // If a surface was indicated, only search cells from the neighbor list of @@ -113,7 +126,7 @@ find_cell(Particle* p, int search_surf) { if (c_i.type_ == FILL_UNIVERSE) { offset += c_i.offset_[c.distribcell_index_]; } else if (c_i.type_ == FILL_LATTICE) { - Lattice& lat {*lattices[p->coord[i+1].lattice-1]}; + Lattice& lat {*model::lattices[p->coord[i+1].lattice-1]}; int i_xyz[3] {p->coord[i+1].lattice_x, p->coord[i+1].lattice_y, p->coord[i+1].lattice_z}; @@ -198,7 +211,7 @@ find_cell(Particle* p, int search_surf) { //======================================================================== //! Found a lower lattice, update this coord level then search the next. - Lattice& lat {*lattices[c.fill_]}; + Lattice& lat {*model::lattices[c.fill_]}; // Determine lattice indices. Position r {p->coord[p->n_coord-1].xyz}; @@ -251,7 +264,7 @@ find_cell(Particle* p, int search_surf) { extern "C" void cross_lattice(Particle* p, int lattice_translation[3]) { - Lattice& lat {*lattices[p->coord[p->n_coord-1].lattice-1]}; + Lattice& lat {*model::lattices[p->coord[p->n_coord-1].lattice-1]}; if (settings::verbosity >= 10 || simulation::trace) { std::stringstream msg; @@ -335,7 +348,7 @@ distance_to_boundary(Particle* p, double* dist, int* surface_crossed, // Find the distance to the next lattice tile crossing. if (p->coord[i].lattice != F90_NONE) { - Lattice& lat {*lattices[p->coord[i].lattice-1]}; + Lattice& lat {*model::lattices[p->coord[i].lattice-1]}; std::array i_xyz {p->coord[i].lattice_x, p->coord[i].lattice_y, p->coord[i].lattice_z}; //TODO: refactor so both lattice use the same position argument (which diff --git a/src/geometry_aux.cpp b/src/geometry_aux.cpp index 5662bd8de..2a3cf1db9 100644 --- a/src/geometry_aux.cpp +++ b/src/geometry_aux.cpp @@ -28,11 +28,11 @@ adjust_indices() if (c->fill_ != C_NONE) { int32_t id = c->fill_; auto search_univ = model::universe_map.find(id); - auto search_lat = lattice_map.find(id); + auto search_lat = model::lattice_map.find(id); if (search_univ != model::universe_map.end()) { c->type_ = FILL_UNIVERSE; c->fill_ = search_univ->second; - } else if (search_lat != lattice_map.end()) { + } else if (search_lat != model::lattice_map.end()) { c->type_ = FILL_LATTICE; c->fill_ = search_lat->second; } else { @@ -74,7 +74,7 @@ adjust_indices() } // Change all lattice universe values from IDs to indices. - for (Lattice* l : lattices) { + for (Lattice* l : model::lattices) { l->adjust_indices(); } } @@ -122,7 +122,7 @@ find_root_universe() } // Find all the universes contained in a lattice. - for (Lattice* lat : lattices) { + for (Lattice* lat : model::lattices) { for (auto it = lat->begin(); it != lat->end(); ++it) { fill_univ_ids.insert(*it); } @@ -245,7 +245,7 @@ prepare_distribcell() c->offset_.resize(n_maps, C_NONE); } } - for (Lattice* lat : lattices) { + for (Lattice* lat : model::lattices) { lat->allocate_offset_table(n_maps); } @@ -263,7 +263,7 @@ prepare_distribcell() offset += count_universe_instances(search_univ, target_univ_id); } else if (c.type_ == FILL_LATTICE) { - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; offset = lat.fill_offset_table(offset, target_univ_id, map); } } @@ -286,7 +286,7 @@ count_cell_instances(int32_t univ_indx) } else if (c.type_ == FILL_LATTICE) { // This cell contains a lattice. Recurse into the lattice universes. - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { count_cell_instances(*it); } @@ -313,7 +313,7 @@ count_universe_instances(int32_t search_univ, int32_t target_univ_id) count += count_universe_instances(next_univ, target_univ_id); } else if (c.type_ == FILL_LATTICE) { - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { int32_t next_univ = *it; count += count_universe_instances(next_univ, target_univ_id); @@ -358,7 +358,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, if (c.type_ == FILL_UNIVERSE) { temp_offset = offset + c.offset_[map]; } else { - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; int32_t indx = lat.universes_.size()*map + lat.begin().indx_; temp_offset = offset + lat.offsets_[indx]; } @@ -381,7 +381,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, return path.str(); } else { // Recurse into the lattice cell. - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; path << "l" << lat.id_; for (ReverseLatticeIter it = lat.rbegin(); it != lat.rend(); ++it) { int32_t indx = lat.universes_.size()*map + it.indx_; @@ -401,7 +401,7 @@ distribcell_path_inner(int32_t target_cell, int32_t map, int32_t target_offset, std::string distribcell_path(int32_t target_cell, int32_t map, int32_t target_offset) { - auto& root_univ = *model::universes[openmc_root_universe]; + auto& root_univ = *model::universes[model::root_universe]; return distribcell_path_inner(target_cell, map, target_offset, root_univ, 0); } @@ -418,7 +418,7 @@ maximum_levels(int32_t univ) int32_t next_univ = c.fill_; levels_below = std::max(levels_below, maximum_levels(next_univ)); } else if (c.type_ == FILL_LATTICE) { - Lattice& lat = *lattices[c.fill_]; + Lattice& lat = *model::lattices[c.fill_]; for (auto it = lat.begin(); it != lat.end(); ++it) { int32_t next_univ = *it; levels_below = std::max(levels_below, maximum_levels(next_univ)); @@ -444,11 +444,11 @@ free_memory_geometry_c() model::universes.clear(); model::universe_map.clear(); - for (Lattice* lat : lattices) {delete lat;} - lattices.clear(); - lattice_map.clear(); + for (Lattice* lat : model::lattices) {delete lat;} + model::lattices.clear(); + model::lattice_map.clear(); - overlap_check_count.clear(); + model::overlap_check_count.clear(); } } // namespace openmc diff --git a/src/geometry_header.F90 b/src/geometry_header.F90 index 9af1fcbaa..df2819382 100644 --- a/src/geometry_header.F90 +++ b/src/geometry_header.F90 @@ -143,7 +143,7 @@ module geometry_header end type Cell ! array index of the root universe - integer(C_INT), bind(C, name='openmc_root_universe') :: root_universe = -1 + integer(C_INT), bind(C) :: root_universe integer(C_INT32_T), bind(C) :: n_cells ! # of cells integer(C_INT32_T), bind(C) :: n_universes ! # of universes diff --git a/src/lattice.cpp b/src/lattice.cpp index 1df21774f..a8966f898 100644 --- a/src/lattice.cpp +++ b/src/lattice.cpp @@ -18,10 +18,13 @@ namespace openmc { // Global variables //============================================================================== -std::vector lattices; +namespace model { +std::vector lattices; std::unordered_map lattice_map; +} + //============================================================================== // Lattice implementation //============================================================================== @@ -865,18 +868,18 @@ extern "C" void read_lattices(pugi::xml_node *node) { for (pugi::xml_node lat_node : node->children("lattice")) { - lattices.push_back(new RectLattice(lat_node)); + model::lattices.push_back(new RectLattice(lat_node)); } for (pugi::xml_node lat_node : node->children("hex_lattice")) { - lattices.push_back(new HexLattice(lat_node)); + model::lattices.push_back(new HexLattice(lat_node)); } // Fill the lattice map. - for (int i_lat = 0; i_lat < lattices.size(); i_lat++) { - int id = lattices[i_lat]->id_; - auto in_map = lattice_map.find(id); - if (in_map == lattice_map.end()) { - lattice_map[id] = i_lat; + for (int i_lat = 0; i_lat < model::lattices.size(); i_lat++) { + int id = model::lattices[i_lat]->id_; + auto in_map = model::lattice_map.find(id); + if (in_map == model::lattice_map.end()) { + model::lattice_map[id] = i_lat; } else { std::stringstream err_msg; err_msg << "Two or more lattices use the same unique ID: " << id; @@ -890,7 +893,7 @@ read_lattices(pugi::xml_node *node) //============================================================================== extern "C" { - Lattice* lattice_pointer(int lat_ind) {return lattices[lat_ind];} + Lattice* lattice_pointer(int lat_ind) {return model::lattices[lat_ind];} int32_t lattice_id(Lattice *lat) {return lat->id_;} } diff --git a/src/output.cpp b/src/output.cpp index 79c3f7f0b..6e07e2456 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -129,9 +129,9 @@ void print_plot() { void print_overlap_check() { #ifdef OPENMC_MPI - std::vector temp(overlap_check_count); - int err = MPI_Reduce(temp.data(), overlap_check_count.data(), - overlap_check_count.size(), MPI_INT64_T, MPI_SUM, 0, + std::vector temp(model::overlap_check_count); + int err = MPI_Reduce(temp.data(), model::overlap_check_count.data(), + model::overlap_check_count.size(), MPI_INT64_T, MPI_SUM, 0, mpi::intracomm); #endif @@ -142,8 +142,8 @@ print_overlap_check() { std::vector sparse_cell_ids; for (int i = 0; i < model::n_cells; i++) { std::cout << " " << std::setw(8) << model::cells[i]->id_ << std::setw(17) - << overlap_check_count[i] << "\n"; - if (overlap_check_count[i] < 10) { + << model::overlap_check_count[i] << "\n"; + if (model::overlap_check_count[i] < 10) { sparse_cell_ids.push_back(model::cells[i]->id_); } } diff --git a/src/plot.cpp b/src/plot.cpp index 403b27884..d325919e4 100644 --- a/src/plot.cpp +++ b/src/plot.cpp @@ -123,7 +123,7 @@ void create_ppm(Plot pl) p.initialize(); std::copy(xyz, xyz+3, p.coord[0].xyz); std::copy(dir, dir+3, p.coord[0].uvw); - p.coord[0].universe = openmc_root_universe; + p.coord[0].universe = model::root_universe; #pragma omp for for (int y = 0; y < height; y++) { @@ -852,7 +852,7 @@ void create_voxel(Plot pl) p.initialize(); std::copy(ll.begin(), ll.begin()+ll.size(), p.coord[0].xyz); std::copy(dir, dir+3, p.coord[0].uvw); - p.coord[0].universe = openmc_root_universe; + p.coord[0].universe = model::root_universe; // Open binary plot file for writing std::ofstream of; diff --git a/src/summary.cpp b/src/summary.cpp index 8431e2a5f..185ad9cce 100644 --- a/src/summary.cpp +++ b/src/summary.cpp @@ -21,7 +21,7 @@ write_geometry(hid_t file_id) { write_attribute(geom_group, "n_cells", model::cells.size()); write_attribute(geom_group, "n_surfaces", surfaces.size()); write_attribute(geom_group, "n_universes", model::universes.size()); - write_attribute(geom_group, "n_lattices", lattices.size()); + write_attribute(geom_group, "n_lattices", model::lattices.size()); auto cells_group = create_group(geom_group, "cells"); for (Cell* c : model::cells) c->to_hdf5(cells_group); @@ -36,7 +36,7 @@ write_geometry(hid_t file_id) { close_group(universes_group); auto lattices_group = create_group(geom_group, "lattices"); - for (Lattice* lat : lattices) lat->to_hdf5(lattices_group); + for (Lattice* lat : model::lattices) lat->to_hdf5(lattices_group); close_group(lattices_group); close_group(geom_group); diff --git a/src/tallies/filter_distribcell.cpp b/src/tallies/filter_distribcell.cpp index 8af01b906..ee4f08dd4 100644 --- a/src/tallies/filter_distribcell.cpp +++ b/src/tallies/filter_distribcell.cpp @@ -45,7 +45,7 @@ DistribcellFilter::get_all_bins(const Particle* p, int estimator, if (c.type_ == FILL_UNIVERSE) { offset += c.offset_[distribcell_index]; } else if (c.type_ == FILL_LATTICE) { - auto& lat {*lattices[p->coord[i+1].lattice-1]}; + auto& lat {*model::lattices[p->coord[i+1].lattice-1]}; int i_xyz[3] {p->coord[i+1].lattice_x, p->coord[i+1].lattice_y, p->coord[i+1].lattice_z};