From 7b4db3279ab487f939734b795b7c31433b7fa84a Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Mon, 1 Jun 2020 18:06:31 -0400 Subject: [PATCH 1/9] Set n_batches and n_max_batches through C API --- docs/source/capi/index.rst | 10 +++++++++ include/openmc/capi.h | 2 ++ openmc/lib/settings.py | 23 +++++++++++++++++++- src/settings.cpp | 44 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index 0ce1c234d..600e565ba 100644 --- a/docs/source/capi/index.rst +++ b/docs/source/capi/index.rst @@ -452,6 +452,16 @@ Functions :return: Return status (negative if an error occurs) :rtype: int +.. c:function:: int openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, int32_t add_statepoint_batch) + + Set number of batches and number of max batches + + :param int32_t n_batches: Number of batches to simulate + :param int32_t n_batches: Maximum number of batches to simulate (only relevant when triggers are used) + :param bool add_statepoint_batch: Whether to add `n_batches` to `settings::statepoint_batch` + :return: Return status (negative if an error occurred) + :rtype: int + .. c:function:: int openmc_simulation_finalize() Finalize a simulation. diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 11c90ff3a..21a79d835 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -89,6 +89,8 @@ extern "C" { int openmc_reset_timers(); int openmc_run(); void openmc_set_seed(int64_t new_seed); + int openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, + bool add_statepoint_batch); int openmc_simulation_finalize(); int openmc_simulation_init(); int openmc_source_bank(void** ptr, int64_t* n); diff --git a/openmc/lib/settings.py b/openmc/lib/settings.py index b63b355e4..dbd5a084b 100644 --- a/openmc/lib/settings.py +++ b/openmc/lib/settings.py @@ -2,6 +2,7 @@ from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, c_bool from . import _dll from .core import _DLLGlobal +from .error import _error_handler _RUN_MODES = {1: 'fixed source', 2: 'eigenvalue', @@ -11,11 +12,13 @@ _RUN_MODES = {1: 'fixed source', _dll.openmc_set_seed.argtypes = [c_int64] _dll.openmc_get_seed.restype = c_int64 +_dll.openmc_set_n_batches.argtypes = [c_int32, c_int32, c_bool] +_dll.openmc_set_n_batches.restype = c_int +_dll.openmc_set_n_batches.errcheck = _error_handler class _Settings: # Attributes that are accessed through a descriptor - batches = _DLLGlobal(c_int32, 'n_batches') cmfd_run = _DLLGlobal(c_bool, 'cmfd_run') entropy_on = _DLLGlobal(c_bool, 'entropy_on') generations_per_batch = _DLLGlobal(c_int32, 'gen_per_batch') @@ -59,5 +62,23 @@ class _Settings: def seed(self, seed): _dll.openmc_set_seed(seed) + def set_n_batches(self, n_batches, n_max_batches=None, add_sp_batch=True): + """Set n_batches and n_max_batches + + Parameters + ---------- + n_batches : int + Number of batches to simulate + n_max_batches : int + Maximum number of batches. Only has an effect when triggers are used + add_sp_batch : bool + Whether to add `n_batches` as a statepoint batch + + """ + if not n_max_batches: + _dll.openmc_set_n_batches(n_batches, n_batches, add_sp_batch) + else: + _dll.openmc_set_n_batches(n_batches, n_max_batches, add_sp_batch) + settings = _Settings() diff --git a/src/settings.cpp b/src/settings.cpp index 342cb7de4..973091fa1 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -827,4 +827,48 @@ void free_memory_settings() { settings::res_scat_nuclides.clear(); } +//============================================================================== +// C API functions +//============================================================================== + +extern "C" int +openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, + bool add_statepoint_batch) +{ + if (settings::n_inactive >= n_batches || + settings::n_inactive >= n_max_batches) { + set_errmsg("Number of active batches must be greater than zero."); + return OPENMC_E_INVALID_ARGUMENT; + } + + if (simulation::current_batch >= n_batches || + simulation::current_batch >= n_max_batches) { + set_errmsg("Number of batches must be greater than current batch."); + return OPENMC_E_INVALID_ARGUMENT; + } + + if (!settings::trigger_on) { + // Set n_batches and n_max_batches to same value + settings::n_batches = n_batches; + settings::n_max_batches = n_batches; + } + else { + // Set n_batches and n_max_batches separately + settings::n_batches = n_batches; + settings::n_max_batches = n_max_batches; + } + + // Update size of k_generation and entropy + int m = settings::n_max_batches * settings::gen_per_batch; + simulation::k_generation.reserve(m); + simulation::entropy.reserve(m); + + // Add value of n_batches to statepoint_batch + if (add_statepoint_batch && + !(contains(settings::statepoint_batch, n_batches))) + settings::statepoint_batch.insert(n_batches); + + return 0; +} + } // namespace openmc From 6e7f5245615d2206db5355b041aa26d766c79fba Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Mon, 1 Jun 2020 21:19:06 -0400 Subject: [PATCH 2/9] Create getter funtion for n_batches, update test_lib.py --- docs/source/capi/index.rst | 11 +++++++++- include/openmc/capi.h | 3 ++- include/openmc/settings.h | 2 +- openmc/lib/settings.py | 42 +++++++++++++++++++++++++++--------- src/settings.cpp | 29 +++++++++++++++++-------- tests/unit_tests/test_lib.py | 4 ++-- 6 files changed, 67 insertions(+), 24 deletions(-) diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index 600e565ba..4442abbf3 100644 --- a/docs/source/capi/index.rst +++ b/docs/source/capi/index.rst @@ -266,6 +266,15 @@ Functions :return: Return status (negative if an error occurs) :rtype: int +.. c:function:: int openmc_get_n_batches(int* n_batches, int* n_max_batches) + + Get number of batches to simulate + + :param int* n_batches: Number of batches to simulate + :param int* n_max_batches: Maximum number of batches to simulate (only relevant when triggers are used) + :return: Return status (negative if an error occurred) + :rtype: int + .. c:function:: int openmc_get_nuclide_index(const char name[], int* index) Get the index in the nuclides array for a nuclide with a given name @@ -457,7 +466,7 @@ Functions Set number of batches and number of max batches :param int32_t n_batches: Number of batches to simulate - :param int32_t n_batches: Maximum number of batches to simulate (only relevant when triggers are used) + :param int32_t n_max_batches: Maximum number of batches to simulate (only relevant when triggers are used) :param bool add_statepoint_batch: Whether to add `n_batches` to `settings::statepoint_batch` :return: Return status (negative if an error occurred) :rtype: int diff --git a/include/openmc/capi.h b/include/openmc/capi.h index 21a79d835..0d2432d29 100644 --- a/include/openmc/capi.h +++ b/include/openmc/capi.h @@ -44,6 +44,7 @@ extern "C" { int openmc_get_keff(double k_combined[]); int openmc_get_material_index(int32_t id, int32_t* index); int openmc_get_mesh_index(int32_t id, int32_t* index); + int openmc_get_n_batches(int* n_batches, bool get_max_batches); int openmc_get_nuclide_index(const char name[], int* index); int64_t openmc_get_seed(); int openmc_get_tally_index(int32_t id, int32_t* index); @@ -89,7 +90,7 @@ extern "C" { int openmc_reset_timers(); int openmc_run(); void openmc_set_seed(int64_t new_seed); - int openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, + int openmc_set_n_batches(int32_t n_batches, bool set_max_batches, bool add_statepoint_batch); int openmc_simulation_finalize(); int openmc_simulation_init(); diff --git a/include/openmc/settings.h b/include/openmc/settings.h index 494cfd1a2..85ef1c71e 100644 --- a/include/openmc/settings.h +++ b/include/openmc/settings.h @@ -64,7 +64,6 @@ extern std::string path_source_library; //!< path to the source shared object extern std::string path_sourcepoint; //!< path to a source file extern "C" std::string path_statepoint; //!< path to a statepoint file -extern "C" int32_t n_batches; //!< number of (inactive+active) batches extern "C" int32_t n_inactive; //!< number of inactive batches extern "C" int32_t max_lost_particles; //!< maximum number of lost particles extern double rel_max_lost_particles; //!< maximum number of lost particles, relative to the total number of particles @@ -79,6 +78,7 @@ extern std::array energy_cutoff; //!< Energy cutoff in [eV] for each extern int legendre_to_tabular_points; //!< number of points to convert Legendres extern int max_order; //!< Maximum Legendre order for multigroup data extern int n_log_bins; //!< number of bins for logarithmic energy grid +extern int n_batches; //!< number of (inactive+active) batches extern int n_max_batches; //!< Maximum number of batches extern ResScatMethod res_scat_method; //!< resonance upscattering method extern double res_scat_energy_min; //!< Min energy in [eV] for res. upscattering diff --git a/openmc/lib/settings.py b/openmc/lib/settings.py index dbd5a084b..935eb2706 100644 --- a/openmc/lib/settings.py +++ b/openmc/lib/settings.py @@ -1,4 +1,4 @@ -from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, c_bool +from ctypes import c_int, c_int32, c_int64, c_double, c_char_p, c_bool, POINTER from . import _dll from .core import _DLLGlobal @@ -12,7 +12,10 @@ _RUN_MODES = {1: 'fixed source', _dll.openmc_set_seed.argtypes = [c_int64] _dll.openmc_get_seed.restype = c_int64 -_dll.openmc_set_n_batches.argtypes = [c_int32, c_int32, c_bool] +_dll.openmc_get_n_batches.argtypes = [POINTER(c_int), c_bool] +_dll.openmc_get_n_batches.restype = c_int +_dll.openmc_get_n_batches.errcheck = _error_handler +_dll.openmc_set_n_batches.argtypes = [c_int32, c_bool, c_bool] _dll.openmc_set_n_batches.restype = c_int _dll.openmc_set_n_batches.errcheck = _error_handler @@ -62,23 +65,42 @@ class _Settings: def seed(self, seed): _dll.openmc_set_seed(seed) - def set_n_batches(self, n_batches, n_max_batches=None, add_sp_batch=True): - """Set n_batches and n_max_batches + def set_batches(self, n_batches, set_max_batches=True, add_sp_batch=True): + """Set n_batches Parameters ---------- n_batches : int Number of batches to simulate - n_max_batches : int - Maximum number of batches. Only has an effect when triggers are used + set_max_batches : int + Whether to set maximum number of batches. If true, the value of + `n_max_batches` is overridden, otherwise the value of `n_batches` + is overridden. Only has an effect when triggers are used add_sp_batch : bool Whether to add `n_batches` as a statepoint batch """ - if not n_max_batches: - _dll.openmc_set_n_batches(n_batches, n_batches, add_sp_batch) - else: - _dll.openmc_set_n_batches(n_batches, n_max_batches, add_sp_batch) + _dll.openmc_set_n_batches(n_batches, set_max_batches, add_sp_batch) + + def get_batches(self, get_max_batches=True): + """Set n_batches and n_max_batches + + Parameters + ---------- + get_max_batches : bool + Return `n_max_batches` if true, else return `n_batches`. Difference + arises only if triggers are used. + + Returns + ------- + int + Number of batches to simulate + + """ + n_batches = c_int() + _dll.openmc_get_n_batches(n_batches, get_max_batches) + + return n_batches.value settings = _Settings() diff --git a/src/settings.cpp b/src/settings.cpp index 973091fa1..4c8a5c6b1 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -78,7 +78,6 @@ std::string path_source_library; std::string path_sourcepoint; std::string path_statepoint; -int32_t n_batches; int32_t n_inactive {0}; int32_t max_lost_particles {10}; double rel_max_lost_particles {1.0e-6}; @@ -92,6 +91,7 @@ std::array energy_cutoff {0.0, 1000.0, 0.0, 0.0}; int legendre_to_tabular_points {C_NONE}; int max_order {0}; int n_log_bins {8000}; +int n_batches; int n_max_batches; ResScatMethod res_scat_method {ResScatMethod::rvs}; double res_scat_energy_min {0.01}; @@ -832,17 +832,15 @@ void free_memory_settings() { //============================================================================== extern "C" int -openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, +openmc_set_n_batches(int32_t n_batches, bool set_max_batches, bool add_statepoint_batch) { - if (settings::n_inactive >= n_batches || - settings::n_inactive >= n_max_batches) { + if (settings::n_inactive >= n_batches) { set_errmsg("Number of active batches must be greater than zero."); return OPENMC_E_INVALID_ARGUMENT; } - if (simulation::current_batch >= n_batches || - simulation::current_batch >= n_max_batches) { + if (simulation::current_batch >= n_batches) { set_errmsg("Number of batches must be greater than current batch."); return OPENMC_E_INVALID_ARGUMENT; } @@ -853,9 +851,11 @@ openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, settings::n_max_batches = n_batches; } else { - // Set n_batches and n_max_batches separately - settings::n_batches = n_batches; - settings::n_max_batches = n_max_batches; + // Set n_batches and n_max_batches based on value of set_max_batches + if (set_max_batches) + settings::n_max_batches = n_batches; + else + settings::n_batches = n_batches; } // Update size of k_generation and entropy @@ -871,4 +871,15 @@ openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, return 0; } +extern "C" int +openmc_get_n_batches(int* n_batches, bool get_max_batches) +{ + if (get_max_batches) + *n_batches = settings::n_max_batches; + else + *n_batches = settings::n_batches; + + return 0; +} + } // namespace openmc diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 264fcafdd..c783a0d20 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -162,8 +162,8 @@ def test_nuclide_mapping(lib_init): def test_settings(lib_init): settings = openmc.lib.settings - assert settings.batches == 10 - settings.batches = 10 + assert settings.get_batches() == 10 + settings.set_batches(10) assert settings.inactive == 5 assert settings.generations_per_batch == 1 assert settings.particles == 100 From a9d0b127b7d14e1d94c194f3dbe9fc98ededd890 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Mon, 1 Jun 2020 21:25:34 -0400 Subject: [PATCH 3/9] Update docs --- docs/source/capi/index.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index 4442abbf3..fe6d2f504 100644 --- a/docs/source/capi/index.rst +++ b/docs/source/capi/index.rst @@ -266,12 +266,12 @@ Functions :return: Return status (negative if an error occurs) :rtype: int -.. c:function:: int openmc_get_n_batches(int* n_batches, int* n_max_batches) +.. c:function:: int openmc_get_n_batches(int* n_batches, bool get_max_batches) Get number of batches to simulate :param int* n_batches: Number of batches to simulate - :param int* n_max_batches: Maximum number of batches to simulate (only relevant when triggers are used) + :param int* get_max_batches: Whether to return `n_batches` or `n_max_batches` (only relevant when triggers are used) :return: Return status (negative if an error occurred) :rtype: int @@ -461,12 +461,12 @@ Functions :return: Return status (negative if an error occurs) :rtype: int -.. c:function:: int openmc_set_n_batches(int32_t n_batches, int32_t n_max_batches, int32_t add_statepoint_batch) +.. c:function:: int openmc_set_n_batches(int32_t n_batches, bool set_max_batches, bool add_statepoint_batch) Set number of batches and number of max batches :param int32_t n_batches: Number of batches to simulate - :param int32_t n_max_batches: Maximum number of batches to simulate (only relevant when triggers are used) + :param bool set_max_batches: Whether to set `settings::n_max_batches` or `settings::n_batches` (only relevant when triggers are used) :param bool add_statepoint_batch: Whether to add `n_batches` to `settings::statepoint_batch` :return: Return status (negative if an error occurred) :rtype: int From 2a8973ec424b059c02584d872227583e3a4f4723 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Mon, 1 Jun 2020 21:33:23 -0400 Subject: [PATCH 4/9] Update batches accessor call in cmfd.py --- openmc/cmfd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openmc/cmfd.py b/openmc/cmfd.py index 09e1486d0..78901b03b 100644 --- a/openmc/cmfd.py +++ b/openmc/cmfd.py @@ -830,7 +830,7 @@ class CMFDRun: """ if filename is None: - batch_str_len = len(str(openmc.lib.settings.batches)) + batch_str_len = len(str(openmc.lib.settings.get_batches())) batch_str = str(openmc.lib.current_batch()).zfill(batch_str_len) filename = 'statepoint.{}.h5'.format(batch_str) @@ -1164,8 +1164,8 @@ class CMFDRun: self._k_cmfd.append(self._keff) # Check to perform adjoint on last batch - if (openmc.lib.current_batch() == openmc.lib.settings.batches - and self._run_adjoint): + batches = openmc.lib.settings.get_batches() + if openmc.lib.current_batch() == batches and self._run_adjoint: self._cmfd_solver_execute(adjoint=True) # Calculate fission source From 1f12ca286f79ee0d964d044e4484d26123d21f0d Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Tue, 2 Jun 2020 04:09:18 -0400 Subject: [PATCH 5/9] Add unit tests for setting n_batches through C API --- tests/unit_tests/test_lib.py | 67 ++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index c783a0d20..e23934a56 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -162,8 +162,6 @@ def test_nuclide_mapping(lib_init): def test_settings(lib_init): settings = openmc.lib.settings - assert settings.get_batches() == 10 - settings.set_batches(10) assert settings.inactive == 5 assert settings.generations_per_batch == 1 assert settings.particles == 100 @@ -318,6 +316,40 @@ def test_by_batch(lib_run): openmc.lib.simulation_finalize() +def test_set_n_batches(lib_run, mpi_intracomm): + # Run simulation_init so that current_batch reset to 0 + openmc.lib.hard_reset() + openmc.lib.finalize() + openmc.lib.init(intracomm=mpi_intracomm) + openmc.lib.simulation_init() + + settings = openmc.lib.settings + assert settings.get_batches() == 10 + + # Setting n_batches less than n_inactive should raise error + with pytest.raises(exc.InvalidArgumentError): + settings.set_batches(3) + # n_batches should stay the same + assert settings.get_batches() == 10 + + for i in range(7): + openmc.lib.next_batch() + # Setting n_batches less than current_batch should raise error + with pytest.raises(exc.InvalidArgumentError): + settings.set_batches(6) + # n_batches should stay the same + assert settings.get_batches() == 10 + + # Change n_batches from 10 to 20 + settings.set_batches(20) + for _ in openmc.lib.iter_batches(): + pass + openmc.lib.simulation_finalize() + + # n_active should have been overwritten from 5 to 15 + assert openmc.lib.num_realizations() == 15 + + def test_reset(lib_run): # Init and run 10 batches. openmc.lib.hard_reset() @@ -515,3 +547,34 @@ def test_global_bounding_box(lib_init): assert tuple(llc) == expected_llc assert tuple(urc) == expected_urc + + +def test_trigger_set_n_batches(lib_run, mpi_intracomm): + openmc.reset_auto_ids() + pincell = openmc.examples.pwr_pin_cell() + pincell.settings.verbosity = 1 + pincell.settings.keff_trigger = {'type': 'std_dev', 'threshold': 0.01} + pincell.settings.trigger_active = True + pincell.settings.trigger_max_batches = 10 + pincell.settings.trigger_batch_interval = 1 + + pincell.export_to_xml() + openmc.lib.hard_reset() + openmc.lib.finalize() + openmc.lib.init(intracomm=mpi_intracomm) + openmc.lib.simulation_init() + + settings = openmc.lib.settings + # Change n_batches to 12 and n_max_batches to 20 + settings.set_batches(12, set_max_batches=False) + settings.set_batches(20, set_max_batches=True) + + assert settings.get_batches(get_max_batches=False) == 12 + assert settings.get_batches(get_max_batches=True) == 20 + + for _ in openmc.lib.iter_batches(): + pass + openmc.lib.simulation_finalize() + + # n_active should have been overwritten from 5 to 15 + assert openmc.lib.num_realizations() == 15 From 961efe5614e6be6a2f1e82835a7ace6d173eccf2 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Tue, 2 Jun 2020 11:00:03 -0400 Subject: [PATCH 6/9] Fix issue with statepoints not being created in test_lib.py --- src/finalize.cpp | 2 ++ tests/unit_tests/test_lib.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/finalize.cpp b/src/finalize.cpp index b5d5eb038..bf9b437e8 100644 --- a/src/finalize.cpp +++ b/src/finalize.cpp @@ -162,6 +162,8 @@ int openmc_reset() simulation::k_abs_tra = 0.0; simulation::k_sum = {0.0, 0.0}; + settings::cmfd_run = false; + return 0; } diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index e23934a56..2f02d7ea9 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -316,11 +316,9 @@ def test_by_batch(lib_run): openmc.lib.simulation_finalize() -def test_set_n_batches(lib_run, mpi_intracomm): +def test_set_n_batches(lib_run): # Run simulation_init so that current_batch reset to 0 openmc.lib.hard_reset() - openmc.lib.finalize() - openmc.lib.init(intracomm=mpi_intracomm) openmc.lib.simulation_init() settings = openmc.lib.settings @@ -348,6 +346,7 @@ def test_set_n_batches(lib_run, mpi_intracomm): # n_active should have been overwritten from 5 to 15 assert openmc.lib.num_realizations() == 15 + assert os.path.exists('statepoint.20.h5') def test_reset(lib_run): @@ -566,8 +565,8 @@ def test_trigger_set_n_batches(lib_run, mpi_intracomm): settings = openmc.lib.settings # Change n_batches to 12 and n_max_batches to 20 - settings.set_batches(12, set_max_batches=False) - settings.set_batches(20, set_max_batches=True) + settings.set_batches(12, set_max_batches=False, add_sp_batch=False) + settings.set_batches(20, set_max_batches=True, add_sp_batch=True) assert settings.get_batches(get_max_batches=False) == 12 assert settings.get_batches(get_max_batches=True) == 20 @@ -578,3 +577,7 @@ def test_trigger_set_n_batches(lib_run, mpi_intracomm): # n_active should have been overwritten from 5 to 15 assert openmc.lib.num_realizations() == 15 + + # Ensure statepoint was created only at batch 20 when calling set_batches + assert not os.path.exists('statepoint.12.h5') + assert os.path.exists('statepoint.20.h5') From f404ffb20b2dcdd12eef46c26234953e4dec9384 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Tue, 2 Jun 2020 12:29:11 -0400 Subject: [PATCH 7/9] Add minor comment --- tests/unit_tests/test_lib.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 2f02d7ea9..53fd217bd 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -346,6 +346,8 @@ def test_set_n_batches(lib_run): # n_active should have been overwritten from 5 to 15 assert openmc.lib.num_realizations() == 15 + + # Ensure statepoint created at new value of n_batches assert os.path.exists('statepoint.20.h5') From 62d1a650e3bb6d8424d76d2fd9f27c364c045fd9 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Wed, 10 Jun 2020 20:33:11 -0400 Subject: [PATCH 8/9] Address @promano comments --- docs/source/capi/index.rst | 2 +- openmc/lib/settings.py | 6 ++--- src/settings.cpp | 13 +++++------ tests/unit_tests/test_lib.py | 43 +++++++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index fe6d2f504..63e7028aa 100644 --- a/docs/source/capi/index.rst +++ b/docs/source/capi/index.rst @@ -271,7 +271,7 @@ Functions Get number of batches to simulate :param int* n_batches: Number of batches to simulate - :param int* get_max_batches: Whether to return `n_batches` or `n_max_batches` (only relevant when triggers are used) + :param bool get_max_batches: Whether to return `n_batches` or `n_max_batches` (only relevant when triggers are used) :return: Return status (negative if an error occurred) :rtype: int diff --git a/openmc/lib/settings.py b/openmc/lib/settings.py index 935eb2706..4ea17233e 100644 --- a/openmc/lib/settings.py +++ b/openmc/lib/settings.py @@ -66,14 +66,14 @@ class _Settings: _dll.openmc_set_seed(seed) def set_batches(self, n_batches, set_max_batches=True, add_sp_batch=True): - """Set n_batches + """Set number of batches or maximum number of batches Parameters ---------- n_batches : int Number of batches to simulate - set_max_batches : int - Whether to set maximum number of batches. If true, the value of + set_max_batches : bool + Whether to set maximum number of batches. If True, the value of `n_max_batches` is overridden, otherwise the value of `n_batches` is overridden. Only has an effect when triggers are used add_sp_batch : bool diff --git a/src/settings.cpp b/src/settings.cpp index 4c8a5c6b1..c50f251e1 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -849,13 +849,13 @@ openmc_set_n_batches(int32_t n_batches, bool set_max_batches, // Set n_batches and n_max_batches to same value settings::n_batches = n_batches; settings::n_max_batches = n_batches; - } - else { + } else { // Set n_batches and n_max_batches based on value of set_max_batches - if (set_max_batches) + if (set_max_batches) { settings::n_max_batches = n_batches; - else + } else { settings::n_batches = n_batches; + } } // Update size of k_generation and entropy @@ -874,10 +874,7 @@ openmc_set_n_batches(int32_t n_batches, bool set_max_batches, extern "C" int openmc_get_n_batches(int* n_batches, bool get_max_batches) { - if (get_max_batches) - *n_batches = settings::n_max_batches; - else - *n_batches = settings::n_batches; + *n_batches = get_max_batches ? settings::n_max_batches : settings::n_batches; return 0; } diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py index 53fd217bd..5553f91e9 100644 --- a/tests/unit_tests/test_lib.py +++ b/tests/unit_tests/test_lib.py @@ -49,6 +49,37 @@ def pincell_model(): yield +@pytest.fixture(scope='module') +def uo2_trigger_model(): + """Set up a simple UO2 model with k-eff trigger""" + model = openmc.model.Model() + m = openmc.Material(name='UO2') + m.add_nuclide('U235', 1.0) + m.add_nuclide('O16', 2.0) + m.set_density('g/cm3', 10.0) + model.materials.append(m) + + cyl = openmc.ZCylinder(r=1.0, boundary_type='vacuum') + c = openmc.Cell(fill=m, region=-cyl) + model.geometry.root_universe = openmc.Universe(cells=[c]) + + model.settings.batches = 10 + model.settings.inactive = 5 + model.settings.particles = 100 + model.settings.source = openmc.Source(space=openmc.stats.Box( + [-0.5, -0.5, -1], [0.5, 0.5, 1], only_fissionable=True)) + model.settings.verbosity = 1 + model.settings.keff_trigger = {'type': 'std_dev', 'threshold': 0.001} + model.settings.trigger_active = True + model.settings.trigger_max_batches = 10 + model.settings.trigger_batch_interval = 1 + + # Write XML files in tmpdir + with cdtemp(): + model.export_to_xml() + yield + + @pytest.fixture(scope='module') def lib_init(pincell_model, mpi_intracomm): openmc.lib.init(intracomm=mpi_intracomm) @@ -550,17 +581,7 @@ def test_global_bounding_box(lib_init): assert tuple(urc) == expected_urc -def test_trigger_set_n_batches(lib_run, mpi_intracomm): - openmc.reset_auto_ids() - pincell = openmc.examples.pwr_pin_cell() - pincell.settings.verbosity = 1 - pincell.settings.keff_trigger = {'type': 'std_dev', 'threshold': 0.01} - pincell.settings.trigger_active = True - pincell.settings.trigger_max_batches = 10 - pincell.settings.trigger_batch_interval = 1 - - pincell.export_to_xml() - openmc.lib.hard_reset() +def test_trigger_set_n_batches(uo2_trigger_model, mpi_intracomm): openmc.lib.finalize() openmc.lib.init(intracomm=mpi_intracomm) openmc.lib.simulation_init() From ec0b3234d9bf443e1711c63025b90457edade411 Mon Sep 17 00:00:00 2001 From: Shikhar Kumar Date: Thu, 11 Jun 2020 11:04:20 -0400 Subject: [PATCH 9/9] Update getter comment --- openmc/lib/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openmc/lib/settings.py b/openmc/lib/settings.py index 4ea17233e..573303857 100644 --- a/openmc/lib/settings.py +++ b/openmc/lib/settings.py @@ -83,7 +83,7 @@ class _Settings: _dll.openmc_set_n_batches(n_batches, set_max_batches, add_sp_batch) def get_batches(self, get_max_batches=True): - """Set n_batches and n_max_batches + """Get number of batches or maximum number of batches Parameters ----------