From c387bde956370a209a278160ced4c488af10d1ad Mon Sep 17 00:00:00 2001 From: Sterling Harper Date: Fri, 7 Sep 2018 17:26:57 -0400 Subject: [PATCH] Add basic C++ TallyFilterMatch class --- CMakeLists.txt | 1 + include/openmc/tallies/tally_filter.h | 34 +++++++++++++++++++++++++++ src/simulation.F90 | 19 ++++++++++++++- src/simulation.cpp | 26 +++++++++++++++++++- src/tallies/tally_filter_header.F90 | 11 +++++++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 include/openmc/tallies/tally_filter.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 23a6176a8..31df21d82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -417,6 +417,7 @@ add_library(libopenmc SHARED src/state_point.cpp src/string_functions.cpp src/surface.cpp + src/tallies/tally_filter.cpp src/thermal.cpp src/xml_interface.cpp src/xsdata.cpp) diff --git a/include/openmc/tallies/tally_filter.h b/include/openmc/tallies/tally_filter.h new file mode 100644 index 000000000..85b6f8c72 --- /dev/null +++ b/include/openmc/tallies/tally_filter.h @@ -0,0 +1,34 @@ +#ifndef OPENMC_TALLY_FILTER_H +#define OPENMC_TALLY_FILTER_H + +#include +#include + + +namespace openmc { + +//============================================================================== +// Global variables +//============================================================================== + +extern "C" int32_t n_filters; + +class TallyFilterMatch; +extern std::vector filter_matches; +#pragma omp threadprivate(filter_matches) + +//============================================================================== +//! Stores bins and weights for filtered tally events +//============================================================================== + +class TallyFilterMatch +{ +public: + int i_bin; + std::vector bins; + std::vector weights; + bool bins_present; +}; + +} // namespace openmc +#endif // OPENMC_TALLY_FILTER diff --git a/src/simulation.F90 b/src/simulation.F90 index df6bf13ba..9fa2b290d 100644 --- a/src/simulation.F90 +++ b/src/simulation.F90 @@ -36,7 +36,7 @@ module simulation use tally, only: accumulate_tallies, setup_active_tallies, & init_tally_routines use tally_header - use tally_filter_header, only: filter_matches, n_filters + use tally_filter_header, only: filter_matches, n_filters, filter_match_pointer use tally_derivative_header, only: tally_derivs use timer_header use trigger, only: check_triggers @@ -420,6 +420,11 @@ contains integer :: i + interface + subroutine openmc_simulation_init_c() bind(C) + end subroutine openmc_simulation_init_c + end interface + err = 0 ! Skip if simulation has already been initialized @@ -453,6 +458,9 @@ contains ! Allocate array for matching filter bins allocate(filter_matches(n_filters)) + do i = 1, n_filters + filter_matches(i) % ptr = filter_match_pointer(i - 1) + end do !$omp end parallel ! Reset global variables -- this is done before loading state point (as that @@ -493,6 +501,8 @@ contains end if end if + call openmc_simulation_init_c() + ! Set flag indicating initialization is done simulation_initialized = .true. @@ -520,6 +530,11 @@ contains #endif #endif + interface + subroutine openmc_simulation_finalize_c() bind(C) + end subroutine openmc_simulation_finalize_c + end interface + err = 0 ! Skip if simulation was never run @@ -590,6 +605,8 @@ contains end do end if + call openmc_simulation_finalize_c() + ! Stop timers and show timing statistics call time_finalize%stop() call time_total%stop() diff --git a/src/simulation.cpp b/src/simulation.cpp index e1dd0ac93..e9cb971cf 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -1,10 +1,12 @@ #include "openmc/capi.h" +#include "openmc/tallies/tally_filter.h" // OPENMC_RUN encompasses all the main logic where iterations are performed // over the batches, generations, and histories in a fixed source or k-eigenvalue // calculation. -int openmc_run() { +int openmc_run() +{ openmc_simulation_init(); int err = 0; @@ -16,3 +18,25 @@ int openmc_run() { openmc_simulation_finalize(); return err; } + +namespace openmc { + +extern "C" void +openmc_simulation_init_c() +{ + #pragma omp parallel + { + filter_matches.resize(n_filters); + } +} + +extern "C" void +openmc_simulation_finalize_c() +{ + #pragma omp parallel + { + filter_matches.clear(); + } +} + +} // namespace openmc diff --git a/src/tallies/tally_filter_header.F90 b/src/tallies/tally_filter_header.F90 index 57b6798bb..38de7a5ad 100644 --- a/src/tallies/tally_filter_header.F90 +++ b/src/tallies/tally_filter_header.F90 @@ -20,12 +20,23 @@ module tally_filter_header public :: openmc_filter_set_id public :: openmc_get_filter_index public :: openmc_get_filter_next_id + public :: filter_match_pointer + + interface + function filter_match_pointer(indx) bind(C) result(ptr) + import C_PTR, C_INT + integer(C_INT), intent(in), value :: indx + type(C_PTR) :: ptr + end function filter_match_pointer + end interface !=============================================================================== ! TALLYFILTERMATCH stores every valid bin and weight for a filter !=============================================================================== type, public :: TallyFilterMatch + type(C_PTR) :: ptr + ! Index of the bin and weight being used in the current filter combination integer :: i_bin type(VectorInt) :: bins_