diff --git a/docs/source/capi/index.rst b/docs/source/capi/index.rst index fe6d2f504e..63e7028aa2 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 935eb2706f..4ea17233e0 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 4c8a5c6b15..c50f251e16 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 53fd217bdc..5553f91e9f 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()