From 6f80abfe2bdc430914a7933d0b364255e3378a3f Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Tue, 9 Oct 2018 22:34:08 -0500 Subject: [PATCH] Move variables in simulation.h into simulation namespace --- include/openmc/capi.h | 3 --- include/openmc/simulation.h | 25 +++++++++++++++++-------- openmc/capi/core.py | 2 +- src/cmfd_execute.cpp | 3 ++- src/eigenvalue.cpp | 6 +++--- src/geometry.cpp | 4 ++-- src/initialize.cpp | 7 ++++--- src/particle.cpp | 16 ++++++++-------- src/settings.cpp | 17 +++++++++-------- src/simulation.cpp | 12 ++++++++---- src/simulation_header.F90 | 19 +++++++++---------- src/source.cpp | 16 ++++++++-------- src/state_point.cpp | 9 +++++---- 13 files changed, 76 insertions(+), 63 deletions(-) diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 57d817d23..3bf1d83ce 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -148,7 +148,6 @@ extern "C" { extern int32_t n_surfaces; extern int32_t n_tallies; extern int32_t n_universes; - extern bool openmc_simulation_initialized; extern double global_tally_absorption; #pragma omp threadprivate(global_tally_absorption) @@ -156,9 +155,7 @@ extern "C" { // later) extern bool openmc_master; extern int openmc_n_procs; - extern int openmc_n_threads; extern int openmc_rank; - extern int64_t openmc_work; #ifdef __cplusplus } diff --git a/include/openmc/simulation.h b/include/openmc/simulation.h index 9c0779072..939b26c14 100644 --- a/include/openmc/simulation.h +++ b/include/openmc/simulation.h @@ -10,19 +10,28 @@ namespace openmc { //============================================================================== -// Global variables +// Global variable declarations //============================================================================== -extern "C" int openmc_current_batch; -extern "C" int openmc_current_gen; -extern "C" int64_t openmc_current_work; -extern "C" int openmc_n_lost_particles; -extern "C" int openmc_total_gen; -extern "C" bool openmc_trace; +namespace simulation { + +extern "C" int current_batch; +extern "C" int current_gen; +extern "C" int64_t current_work; +extern "C" int n_lost_particles; +extern "C" int total_gen; +extern "C" bool trace; +extern "C" int64_t work; + +#ifdef _OPENMP +extern "C" int n_threads; +#endif extern std::vector work_index; -#pragma omp threadprivate(openmc_current_work, openmc_trace) +#pragma omp threadprivate(current_work, trace) + +} // namespace simulation //============================================================================== // Functions diff --git a/openmc/capi/core.py b/openmc/capi/core.py index fa6517488..205123ff8 100644 --- a/openmc/capi/core.py +++ b/openmc/capi/core.py @@ -71,7 +71,7 @@ def current_batch(): Current batch of the simulation """ - return c_int.in_dll(_dll, 'openmc_current_batch').value + return c_int.in_dll(_dll, 'current_batch').value def finalize(): diff --git a/src/cmfd_execute.cpp b/src/cmfd_execute.cpp index 740c1f51f..00d162069 100644 --- a/src/cmfd_execute.cpp +++ b/src/cmfd_execute.cpp @@ -7,6 +7,7 @@ #include "openmc/capi.h" #include "openmc/mesh.h" +#include "openmc/simulation.h" namespace openmc { @@ -23,7 +24,7 @@ cmfd_populate_sourcecounts(int n_energy, const double* energies, // Get source counts in each mesh bin / energy bin auto& m = meshes.at(index_cmfd_mesh); - xt::xarray counts = m->count_sites(openmc_work, source_bank, n_energy, energies, outside); + xt::xarray counts = m->count_sites(simulation::work, source_bank, n_energy, energies, outside); // Copy data from the xarray into the source counts array std::copy(counts.begin(), counts.end(), source_counts); diff --git a/src/eigenvalue.cpp b/src/eigenvalue.cpp index 454fd57e6..f03820492 100644 --- a/src/eigenvalue.cpp +++ b/src/eigenvalue.cpp @@ -66,7 +66,7 @@ void ufs_count_sites() { auto &m = meshes[settings::index_ufs_mesh]; - if (openmc_current_batch == 1 && openmc_current_gen == 1) { + if (simulation::current_batch == 1 && simulation::current_gen == 1) { // On the first generation, just assume that the source is already evenly // distributed so that effectively the production of fission sites is not // biased @@ -82,7 +82,7 @@ void ufs_count_sites() // count number of source sites in each ufs mesh cell bool sites_outside; - source_frac = m->count_sites(openmc_work, source_bank, 0, nullptr, + source_frac = m->count_sites(simulation::work, source_bank, 0, nullptr, &sites_outside); // Check for sites outside of the mesh @@ -102,7 +102,7 @@ void ufs_count_sites() // Since the total starting weight is not equal to n_particles, we need to // renormalize the weight of the source sites - for (int i = 0; i < openmc_work; ++i) { + for (int i = 0; i < simulation::work; ++i) { source_bank[i].wgt *= settings::n_particles / total; } } diff --git a/src/geometry.cpp b/src/geometry.cpp index 857dbd32e..28885a5db 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -91,7 +91,7 @@ find_cell(Particle* p, int search_surf) { if (cells[i_cell]->contains(r, u, surf)) { p->coord[p->n_coord-1].cell = i_cell; - if (settings::verbosity >= 10 || openmc_trace) { + if (settings::verbosity >= 10 || simulation::trace) { std::stringstream msg; msg << " Entering cell " << cells[i_cell]->id_; write_message(msg, 1); @@ -255,7 +255,7 @@ cross_lattice(Particle* p, int lattice_translation[3]) { Lattice& lat {*lattices[p->coord[p->n_coord-1].lattice-1]}; - if (settings::verbosity >= 10 || openmc_trace) { + if (settings::verbosity >= 10 || simulation::trace) { std::stringstream msg; msg << " Crossing lattice " << lat.id_ << ". Current position (" << p->coord[p->n_coord-1].lattice_x << "," diff --git a/src/initialize.cpp b/src/initialize.cpp index 0c26135dc..ac7a5ef15 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -15,6 +15,7 @@ #include "openmc/hdf5_interface.h" #include "openmc/message_passing.h" #include "openmc/settings.h" +#include "openmc/simulation.h" #include "openmc/string_utils.h" // data/functions from Fortran side @@ -172,13 +173,13 @@ parse_command_line(int argc, char* argv[]) #ifdef _OPENMP // Read and set number of OpenMP threads - openmc_n_threads = std::stoi(argv[i]); - if (openmc_n_threads < 1) { + simulation::n_threads = std::stoi(argv[i]); + if (simulation::n_threads < 1) { std::string msg {"Number of threads must be positive."}; strcpy(openmc_err_msg, msg.c_str()); return OPENMC_E_INVALID_ARGUMENT; } - omp_set_num_threads(openmc_n_threads); + omp_set_num_threads(simulation::n_threads); #else if (openmc_master) warning("Ignoring number of threads specified on command line."); diff --git a/src/particle.cpp b/src/particle.cpp index ca57c2faa..d6dfa969c 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -127,15 +127,15 @@ Particle::mark_as_lost(const char* message) // Increment number of lost particles alive = false; #pragma omp atomic - openmc_n_lost_particles += 1; + simulation::n_lost_particles += 1; // Count the total number of simulated particles (on this processor) - auto n = openmc_current_batch * settings::gen_per_batch * openmc_work; + auto n = simulation::current_batch * settings::gen_per_batch * simulation::work; // Abort the simulation if the maximum number of lost particles has been // reached - if (openmc_n_lost_particles >= MAX_LOST_PARTICLES && - openmc_n_lost_particles >= REL_MAX_LOST_PARTICLES*n) { + if (simulation::n_lost_particles >= MAX_LOST_PARTICLES && + simulation::n_lost_particles >= REL_MAX_LOST_PARTICLES*n) { fatal_error("Maximum number of lost particles has been reached."); } } @@ -148,7 +148,7 @@ Particle::write_restart() const // Set up file name std::stringstream filename; - filename << settings::path_output << "particle_" << openmc_current_batch + filename << settings::path_output << "particle_" << simulation::current_batch << '_' << id << ".h5"; #pragma omp critical (WriteParticleRestart) @@ -165,9 +165,9 @@ Particle::write_restart() const #endif // Write data to file - write_dataset(file_id, "current_batch", openmc_current_batch); + write_dataset(file_id, "current_batch", simulation::current_batch); write_dataset(file_id, "generations_per_batch", settings::gen_per_batch); - write_dataset(file_id, "current_generation", openmc_current_gen); + write_dataset(file_id, "current_generation", simulation::current_gen); write_dataset(file_id, "n_particles", settings::n_particles); switch (settings::run_mode) { case RUN_MODE_FIXEDSOURCE: @@ -188,7 +188,7 @@ Particle::write_restart() const int64_t n; openmc_source_bank(&src, &n); - int64_t i = openmc_current_work; + int64_t i = simulation::current_work; write_dataset(file_id, "weight", src[i-1].wgt); write_dataset(file_id, "energy", src[i-1].E); hsize_t dims[] {3}; diff --git a/src/settings.cpp b/src/settings.cpp index b5f2bf5c6..bf97e44b8 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -17,6 +17,7 @@ #include "openmc/mesh.h" #include "openmc/output.h" #include "openmc/random_lcg.h" +#include "openmc/simulation.h" #include "openmc/source.h" #include "openmc/string_utils.h" #include "openmc/xml_interface.h" @@ -57,7 +58,7 @@ bool urr_ptables_on {true}; bool write_all_tracks {false}; bool write_initial_source {false}; bool dagmc {false}; - + std::string path_cross_sections; std::string path_input; std::string path_multipole; @@ -218,7 +219,7 @@ void read_settings_xml() fatal_error("DAGMC mode unsupported for this build of OpenMC"); } #endif - + // To this point, we haven't displayed any output since we didn't know what // the verbosity is. Now that we checked for it, show the title if necessary if (openmc_master) { @@ -393,14 +394,14 @@ void read_settings_xml() // Number of OpenMP threads if (check_for_node(root, "threads")) { #ifdef _OPENMP - if (openmc_n_threads == 0) { - openmc_n_threads = std::stoi(get_node_value(root, "threads")); - if (openmc_n_threads < 1) { + if (simulation::n_threads == 0) { + simulation::n_threads = std::stoi(get_node_value(root, "threads")); + if (simulation::n_threads < 1) { std::stringstream msg; - msg << "Invalid number of threads: " << openmc_n_threads; + msg << "Invalid number of threads: " << simulation::n_threads; fatal_error(msg); } - omp_set_num_threads(openmc_n_threads); + omp_set_num_threads(simulation::n_threads); } #else if (openmc_master) warning("OpenMC was not compiled with OpenMP support; " @@ -414,7 +415,7 @@ void read_settings_xml() omp_set_num_threads(1); } #endif - + // ========================================================================== // EXTERNAL SOURCE diff --git a/src/simulation.cpp b/src/simulation.cpp index 74a3c14fc..5c2a13678 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -27,8 +27,12 @@ namespace openmc { // Global variables //============================================================================== +namespace simulation { + std::vector work_index; +} // namespace simulation + //============================================================================== // Functions //============================================================================== @@ -48,18 +52,18 @@ void calculate_work() int64_t remainder = settings::n_particles % mpi::n_procs; int64_t i_bank = 0; - work_index.reserve(mpi::n_procs); - work_index.push_back(0); + simulation::work_index.reserve(mpi::n_procs); + simulation::work_index.push_back(0); for (int i = 0; i < mpi::n_procs; ++i) { // Number of particles for rank i int64_t work_i = i < remainder ? min_work + 1 : min_work; // Set number of particles - if (mpi::rank == i) openmc_work = work_i; + if (mpi::rank == i) simulation::work = work_i; // Set index into source bank for rank i i_bank += work_i; - work_index.push_back(i_bank); + simulation::work_index.push_back(i_bank); } } diff --git a/src/simulation_header.F90 b/src/simulation_header.F90 index ad5513c61..b96caf257 100644 --- a/src/simulation_header.F90 +++ b/src/simulation_header.F90 @@ -13,18 +13,17 @@ module simulation_header ! GEOMETRY-RELATED VARIABLES ! Number of lost particles - integer(C_INT), bind(C, name='openmc_n_lost_particles') :: n_lost_particles = 0 + integer(C_INT), bind(C) :: n_lost_particles = 0 real(8) :: log_spacing ! spacing on logarithmic grid ! ============================================================================ ! SIMULATION VARIABLES - integer(C_INT), bind(C, name='openmc_current_batch') :: current_batch ! current batch - integer(C_INT), bind(C, name='openmc_current_gen') :: current_gen ! current generation within a batch - integer(C_INT), bind(C, name='openmc_total_gen') :: total_gen = 0 ! total number of generations simulated - logical(C_BOOL), bind(C, name='openmc_simulation_initialized') :: & - simulation_initialized = .false. + integer(C_INT), bind(C) :: current_batch ! current batch + integer(C_INT), bind(C) :: current_gen ! current generation within a batch + integer(C_INT), bind(C) :: total_gen = 0 ! total number of generations simulated + logical(C_BOOL), bind(C) :: simulation_initialized = .false. logical :: need_depletion_rx ! need to calculate depletion reaction rx? ! ============================================================================ @@ -32,9 +31,9 @@ module simulation_header logical :: satisfy_triggers = .false. ! whether triggers are satisfied - integer(C_INT64_T), bind(C, name='openmc_work') :: work ! number of particles per processor + integer(C_INT64_T), bind(C) :: work ! number of particles per processor integer(C_INT64_T), allocatable :: work_index(:) ! starting index in source bank for each process - integer(C_INT64_T), bind(C, name='openmc_current_work') :: current_work ! index in source bank of current history simulated + integer(C_INT64_T), bind(C) :: current_work ! index in source bank of current history simulated ! ============================================================================ ! K-EIGENVALUE SIMULATION VARIABLES @@ -51,7 +50,7 @@ module simulation_header ! PARALLEL PROCESSING VARIABLES #ifdef _OPENMP - integer(C_INT), bind(C, name='openmc_n_threads') :: n_threads = NONE ! number of OpenMP threads + integer(C_INT), bind(C) :: n_threads = NONE ! number of OpenMP threads integer :: thread_id ! ID of a given thread #endif @@ -60,7 +59,7 @@ module simulation_header integer :: restart_batch - logical(C_BOOL), bind(C, name='openmc_trace') :: trace + logical(C_BOOL), bind(C) :: trace !$omp threadprivate(trace, thread_id, current_work) diff --git a/src/source.cpp b/src/source.cpp index 666bbb783..1290c5f0d 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -264,17 +264,17 @@ void initialize_source() } // Read in the source bank - read_source_bank(file_id, work_index.data(), source_bank); + read_source_bank(file_id, simulation::work_index.data(), source_bank); // Close file file_close(file_id); } else { // Generation source sites from specified distribution in user input - for (int64_t i = 0; i < openmc_work; ++i) { + for (int64_t i = 0; i < simulation::work; ++i) { // initialize random number seed - int64_t id = openmc_total_gen*settings::n_particles + - work_index[openmc::mpi::rank] + i + 1; + int64_t id = simulation::total_gen*settings::n_particles + + simulation::work_index[mpi::rank] + i + 1; set_particle_seed(id); // sample external source distribution @@ -287,7 +287,7 @@ void initialize_source() write_message("Writing out initial source...", 5); std::string filename = settings::path_output + "initial_source.h5"; hid_t file_id = file_open(filename, 'w', true); - write_source_bank(file_id, work_index.data(), source_bank); + write_source_bank(file_id, simulation::work_index.data(), source_bank); file_close(file_id); } } @@ -364,10 +364,10 @@ extern "C" void fill_source_bank_fixedsource() int64_t n; openmc_source_bank(&source_bank, &n); - for (int64_t i = 0; i < openmc_work; ++i) { + for (int64_t i = 0; i < simulation::work; ++i) { // initialize random number seed - int64_t id = (openmc_total_gen + overall_generation()) * - settings::n_particles + work_index[openmc::mpi::rank] + i + 1; + int64_t id = (simulation::total_gen + overall_generation()) * + settings::n_particles + simulation::work_index[mpi::rank] + i + 1; set_particle_seed(id); // sample external source distribution diff --git a/src/state_point.cpp b/src/state_point.cpp index dba0fe1b8..e24993b6c 100644 --- a/src/state_point.cpp +++ b/src/state_point.cpp @@ -11,6 +11,7 @@ #include "openmc/error.h" #include "openmc/message_passing.h" #include "openmc/settings.h" +#include "openmc/simulation.h" namespace openmc { @@ -46,7 +47,7 @@ write_source_bank(hid_t group_id, int64_t* work_index, Bank* source_bank) H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); // Create another data space but for each proc individually - hsize_t count[] {static_cast(openmc_work)}; + hsize_t count[] {static_cast(simulation::work)}; hid_t memspace = H5Screate_simple(1, count, nullptr); // Select hyperslab for this dataspace @@ -77,7 +78,7 @@ write_source_bank(hid_t group_id, int64_t* work_index, Bank* source_bank) // Save source bank sites since the souce_bank array is overwritten below #ifdef OPENMC_MPI - std::vector temp_source {source_bank, source_bank + openmc_work}; + std::vector temp_source {source_bank, source_bank + simulation::work}; #endif for (int i = 0; i < openmc::mpi::n_procs; ++i) { @@ -113,7 +114,7 @@ write_source_bank(hid_t group_id, int64_t* work_index, Bank* source_bank) #endif } else { #ifdef OPENMC_MPI - MPI_Send(source_bank, openmc_work, openmc::mpi::bank, 0, openmc::mpi::rank, + MPI_Send(source_bank, simulation::work, openmc::mpi::bank, 0, openmc::mpi::rank, openmc::mpi::intracomm); #endif } @@ -131,7 +132,7 @@ void read_source_bank(hid_t group_id, int64_t* work_index, Bank* source_bank) hid_t dset = H5Dopen(group_id, "source_bank", H5P_DEFAULT); // Create another data space but for each proc individually - hsize_t dims[] {static_cast(openmc_work)}; + hsize_t dims[] {static_cast(simulation::work)}; hid_t memspace = H5Screate_simple(1, dims, nullptr); // Make sure source bank is big enough