From 7c2b695c982ff2f312789dc9557de080dced4e9e Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Feb 2020 13:06:30 +0000 Subject: [PATCH] Refactor into function as suggested by review --- include/openmc/source.h | 3 ++ src/source.cpp | 110 +++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 64 deletions(-) diff --git a/include/openmc/source.h b/include/openmc/source.h index 0a73260fd..e980f2a8c 100644 --- a/include/openmc/source.h +++ b/include/openmc/source.h @@ -72,6 +72,9 @@ Particle::Bank sample_external_source(uint64_t* seed); //! Fill source bank at end of generation for fixed source simulations void fill_source_bank_fixedsource(); +//! Fill source bank at the end of a generation for dlopen based source simulation +void fill_source_bank_dlopen_source(); + void free_memory_source(); } // namespace openmc diff --git a/src/source.cpp b/src/source.cpp index e7a476746..2cda76a5c 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -297,30 +297,7 @@ void initialize_source() // reset errors dlerror(); - // get the function from the library - sample_t sample_source = (sample_t) dlsym(source_library, "sample_source"); - const char *dlsym_error = dlerror(); - - // check for any dlsym errors - if (dlsym_error) { - std::cout << dlsym_error << std::endl; - dlclose(source_library); - fatal_error("Couldn't open the sample_source symbol"); - } - - // Generation source sites from specified distribution in the - // library source - for (int64_t i = 0; i < simulation::work_per_rank; ++i) { - // initialize random number seed - int64_t id = simulation::total_gen*settings::n_particles + - simulation::work_index[mpi::rank] + i + 1; - uint64_t seed = init_seed(id, STREAM_SOURCE); - - // sample external source distribution - simulation::source_bank[i] = sample_source(seed); - } - // release the library - dlclose(source_library); + fill_source_bank_dlopen_source(); } else { // Generation source sites from specified distribution in user input @@ -381,6 +358,50 @@ void free_memory_source() model::external_sources.clear(); } +// fill the source bank from the external source +void fill_source_bank_dlopen_source() +{ + std::stringstream msg; + + // Open the library + void* source_library = dlopen(settings::path_source_library.c_str(),RTLD_LAZY); + if(!source_library) { + std::stringstream msg("Couldn't open source library " + settings::path_source_library); + fatal_error(msg); + } + + // load the symbol + typedef Particle::Bank (*sample_t)(uint64_t seed); + + // reset errors + dlerror(); + + // get the function from the library + sample_t sample_source = (sample_t) dlsym(source_library, "sample_source"); + const char *dlsym_error = dlerror(); + + // check for any dlsym errors + if (dlsym_error) { + std::cout << dlsym_error << std::endl; + dlclose(source_library); + fatal_error("Couldn't open the sample_source symbol"); + } + + // Generation source sites from specified distribution in the + // library source + for (int64_t i = 0; i < simulation::work_per_rank; ++i) { + // initialize random number seed + int64_t id = simulation::total_gen*settings::n_particles + + simulation::work_index[mpi::rank] + i + 1; + uint64_t seed = init_seed(id, STREAM_SOURCE); + // sample external source distribution + simulation::source_bank[i] = sample_source(seed); + } + + // release the library + dlclose(source_library); +} + void fill_source_bank_fixedsource() { if (settings::path_source.empty() && settings::path_source_library.empty()) { @@ -394,46 +415,7 @@ void fill_source_bank_fixedsource() simulation::source_bank[i] = sample_external_source(&seed); } } else if (settings::path_source.empty() && !settings::path_source.empty()) { - std::stringstream msg; - - // Open the library - void* source_library = dlopen(settings::path_source_library.c_str(),RTLD_LAZY); - if(!source_library) { - std::stringstream msg("Couldn't open source library " + settings::path_source_library); - fatal_error(msg); - } - - // load the symbol - typedef Particle::Bank (*sample_t)(uint64_t seed); - - // reset errors - dlerror(); - - // get the function from the library - sample_t sample_source = (sample_t) dlsym(source_library, "sample_source"); - const char *dlsym_error = dlerror(); - - // check for any dlsym errors - if (dlsym_error) { - std::cout << dlsym_error << std::endl; - dlclose(source_library); - fatal_error("Couldn't open the sample_source symbol"); - } - - // Generation source sites from specified distribution in the - // library source - for (int64_t i = 0; i < simulation::work_per_rank; ++i) { - // initialize random number seed - int64_t id = simulation::total_gen*settings::n_particles + - simulation::work_index[mpi::rank] + i + 1; - uint64_t seed = init_seed(id, STREAM_SOURCE); - - // sample external source distribution - simulation::source_bank[i] = sample_source(seed); - } - - // release the library - dlclose(source_library); + fill_source_bank_dlopen_source(); } }