From 6a3911c24822e5dfa56e2c5d3ecbd484448a51ef Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 17 Oct 2018 17:04:32 -0500 Subject: [PATCH] Move openmc_reset and openmc_hard_reset to C++ --- include/openmc/timer.h | 6 ++ src/api.F90 | 150 +++-------------------------------------- src/finalize.cpp | 54 ++++++++++++++- src/timer.cpp | 6 +- src/timer_header.F90 | 10 ++- 5 files changed, 79 insertions(+), 147 deletions(-) diff --git a/include/openmc/timer.h b/include/openmc/timer.h index 518923f991..1b4d71a5c7 100644 --- a/include/openmc/timer.h +++ b/include/openmc/timer.h @@ -49,6 +49,12 @@ extern Timer time_tallies; extern Timer time_total; extern Timer time_transport; +//============================================================================== +// Non-member functions +//============================================================================== + +void reset_timers(); + } // namespace openmc #endif // OPENMC_TIMER_H diff --git a/src/api.F90 b/src/api.F90 index 67408b8163..c5ab06f592 100644 --- a/src/api.F90 +++ b/src/api.F90 @@ -2,96 +2,17 @@ module openmc_api use, intrinsic :: ISO_C_BINDING - use bank_header, only: openmc_source_bank - use constants, only: K_BOLTZMANN - use eigenvalue, only: openmc_get_keff + use constants use error use geometry, only: find_cell - use geometry_header - use hdf5_interface - use material_header - use math - use message_passing - use nuclide_header use particle_header - use plot, only: openmc_plot_geometry - use random_lcg, only: openmc_get_seed, openmc_set_seed - use settings - use simulation_header - use state_point, only: openmc_statepoint_write - use tally_header - use tally_filter_header - use tally_filter - use tally, only: openmc_tally_allocate - use simulation - use string, only: to_f_string - use timer_header - use volume_calc, only: openmc_calculate_volumes - + use string, only: to_str #ifdef DAGMC use dagmc_header, only: free_memory_dagmc #endif implicit none - private - public :: openmc_calculate_volumes - public :: openmc_cell_filter_get_bins - public :: openmc_cell_get_id - public :: openmc_cell_set_id - public :: openmc_energy_filter_get_bins - public :: openmc_energy_filter_set_bins - public :: openmc_extend_filters - public :: openmc_extend_cells - public :: openmc_extend_materials - public :: openmc_extend_tallies - public :: openmc_filter_get_id - public :: openmc_filter_get_type - public :: openmc_filter_set_id - public :: openmc_filter_set_type - public :: openmc_find_cell - public :: openmc_get_cell_index - public :: openmc_get_keff - public :: openmc_get_filter_index - public :: openmc_get_filter_next_id - public :: openmc_get_material_index - public :: openmc_get_nuclide_index - public :: openmc_get_seed - public :: openmc_get_tally_index - public :: openmc_get_tally_next_id - public :: openmc_global_tallies - public :: openmc_hard_reset - public :: openmc_load_nuclide - public :: openmc_material_add_nuclide - public :: openmc_material_get_id - public :: openmc_material_get_densities - public :: openmc_material_set_density - public :: openmc_material_set_densities - public :: openmc_material_set_id - public :: openmc_material_filter_get_bins - public :: openmc_material_filter_set_bins - public :: openmc_mesh_filter_set_mesh - public :: openmc_meshsurface_filter_set_mesh - public :: openmc_nuclide_name - public :: openmc_plot_geometry - public :: openmc_reset - public :: openmc_set_seed - public :: openmc_source_bank - public :: openmc_tally_allocate - public :: openmc_tally_get_estimator - public :: openmc_tally_get_id - public :: openmc_tally_get_filters - public :: openmc_tally_get_n_realizations - public :: openmc_tally_get_nuclides - public :: openmc_tally_get_scores - public :: openmc_tally_get_type - public :: openmc_tally_results - public :: openmc_tally_set_estimator - public :: openmc_tally_set_filters - public :: openmc_tally_set_id - public :: openmc_tally_set_nuclides - public :: openmc_tally_set_scores - public :: openmc_tally_set_type contains @@ -130,67 +51,6 @@ contains end function openmc_find_cell -!=============================================================================== -! OPENMC_HARD_RESET reset tallies and timers as well as the pseudorandom -! generator state -!=============================================================================== - - function openmc_hard_reset() result(err) bind(C) - integer(C_INT) :: err - - ! Reset all tallies and timers - err = openmc_reset() - - ! Reset total generations and keff guess - keff = ONE - total_gen = 0 - - ! Reset the random number generator state - call openmc_set_seed(DEFAULT_SEED) - end function openmc_hard_reset - -!=============================================================================== -! OPENMC_RESET resets tallies and timers -!=============================================================================== - - function openmc_reset() result(err) bind(C) - integer(C_INT) :: err - - integer :: i - interface - subroutine k_sum_reset() bind(C) - end subroutine - end interface - - if (allocated(tallies)) then - do i = 1, size(tallies) - associate (t => tallies(i) % obj) - t % n_realizations = 0 - if (allocated(t % results)) then - t % results(:, :, :) = ZERO - end if - end associate - end do - end if - - ! Reset global tallies - n_realizations = 0 - if (allocated(global_tallies)) then - global_tallies(:, :) = ZERO - end if - k_col_abs = ZERO - k_col_tra = ZERO - k_abs_tra = ZERO - call k_sum_reset() - - ! Reset timers - call time_read_xs % reset() - call time_unionize % reset() - call reset_timers() - - err = 0 - end function openmc_reset - !=============================================================================== ! FREE_MEMORY deallocates and clears all global allocatable arrays in the ! program @@ -198,13 +58,19 @@ contains subroutine free_memory() bind(C) + use bank_header use cmfd_header + use geometry_header + use material_header use photon_header use plot_header use sab_header use settings + use simulation_header use surface_header use tally_derivative_header + use tally_filter_header + use tally_header use trigger_header use volume_header diff --git a/src/finalize.cpp b/src/finalize.cpp index 7323358150..d6913fb264 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -2,20 +2,24 @@ #include "openmc/capi.h" #include "openmc/constants.h" +#include "openmc/eigenvalue.h" #include "openmc/geometry.h" #include "openmc/message_passing.h" #include "openmc/nuclide.h" #include "openmc/random_lcg.h" #include "openmc/settings.h" #include "openmc/simulation.h" +#include "openmc/timer.h" +#include "openmc/tallies/tally.h" + +using namespace openmc; // Functions defined in Fortran extern "C" void free_memory(); +extern "C" void reset_timers_f(); int openmc_finalize() { - using namespace openmc; - // Clear results openmc_reset(); @@ -87,3 +91,49 @@ int openmc_finalize() return 0; } + +int openmc_reset() +{ + for (int i = 1; i <= n_tallies; ++i) { + openmc_tally_reset(i); + } + + // Reset global tallies (can't really use global_tallies() right now because + // it doesn't have any information about whether the underlying buffer was + // allocated) + n_realizations = 0; + double* buffer = nullptr; + openmc_global_tallies(&buffer); + if (buffer) { + for (int i = 0; i < 3*N_GLOBAL_TALLIES*3; ++i) { + buffer[i] = 0.0; + } + } + // auto gt = global_tallies(); + // std::fill(gt.begin(), gt.end(), 0.0); + + simulation::k_col_abs = 0.0; + simulation::k_col_tra = 0.0; + simulation::k_abs_tra = 0.0; + k_sum = {0.0, 0.0}; + + // Reset timers + reset_timers(); + reset_timers_f(); + + return 0; +} + +int openmc_hard_reset() +{ + // Reset all tallies and timers + openmc_reset(); + + // Reset total generations and keff guess + simulation::keff = 1.0; + simulation::total_gen = 0; + + // Reset the random number generator state + openmc_set_seed(DEFAULT_SEED); + return 0; +} diff --git a/src/timer.cpp b/src/timer.cpp index ecf7d3ae93..46a84a6f1e 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -64,7 +64,11 @@ extern "C" double time_tallies_elapsed() { return time_tallies.elapsed(); } extern "C" double time_total_elapsed() { return time_total.elapsed(); } extern "C" double time_transport_elapsed() { return time_transport.elapsed(); } -extern "C" void reset_timers() +//============================================================================== +// Non-member functions +//============================================================================== + +void reset_timers() { time_active.reset(); time_bank.reset(); diff --git a/src/timer_header.F90 b/src/timer_header.F90 index d0fbc1a6da..2064f7ae66 100644 --- a/src/timer_header.F90 +++ b/src/timer_header.F90 @@ -73,8 +73,6 @@ module timer_header ! TIMING VARIABLES type(Timer) :: time_read_xs ! timer for reading cross sections - type(Timer) :: time_unionize ! timer for material xs-energy grid union - type(Timer) :: time_finalize ! timer for finalization contains @@ -139,4 +137,12 @@ contains self % elapsed = ZERO end subroutine timer_reset +!=============================================================================== +! RESET_TIMERS resets timers on the Fortran side +!=============================================================================== + + subroutine reset_timers_f() bind(C) + call time_read_xs % reset() + end subroutine + end module timer_header