Merge pull request #2390 from pshriwise/restart-fixes

Updates to batch checks for simulation restarts
This commit is contained in:
Paul Romano 2023-02-22 09:16:11 -06:00 committed by GitHub
commit 635e573955
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 19 deletions

View file

@ -1,5 +1,6 @@
from collections.abc import Iterable
from numbers import Integral
import os
import subprocess
import openmc
@ -23,7 +24,7 @@ def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
Number of particles to simulate per generation.
plot : bool, optional
Run in plotting mode. Defaults to False.
restart_file : str, optional
restart_file : str or PathLike
Path to restart file to use
threads : int, optional
Number of OpenMP threads. If OpenMC is compiled with OpenMP threading
@ -42,7 +43,7 @@ def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
mpi_args : list of str, optional
MPI execute command and any additional MPI arguments to pass,
e.g., ['mpiexec', '-n', '8'].
path_input : str or Pathlike
path_input : str or PathLike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.
@ -73,8 +74,8 @@ def _process_CLI_arguments(volume=False, geometry_debug=False, particles=None,
if event_based:
args.append('-e')
if isinstance(restart_file, str):
args += ['-r', restart_file]
if isinstance(restart_file, (str, os.PathLike)):
args += ['-r', str(restart_file)]
if tracks:
args.append('-t')
@ -230,7 +231,7 @@ def calculate_volumes(threads=None, output=True, cwd='.',
cwd : str, optional
Path to working directory to run in. Defaults to the current working
directory.
path_input : str or Pathlike
path_input : str or PathLike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.
@ -270,7 +271,7 @@ def run(particles=None, threads=None, geometry_debug=False,
:envvar:`OMP_NUM_THREADS` environment variable).
geometry_debug : bool, optional
Turn on geometry debugging during simulation. Defaults to False.
restart_file : str, optional
restart_file : str or PathLike
Path to restart file to use
tracks : bool, optional
Enables the writing of particles tracks. The number of particle tracks
@ -291,7 +292,7 @@ def run(particles=None, threads=None, geometry_debug=False,
.. versionadded:: 0.12
path_input : str or Pathlike
path_input : str or PathLike
Path to a single XML file or a directory containing XML files for the
OpenMC executable to read.

View file

@ -258,6 +258,8 @@ class Filter(IDManagerMixin, metaclass=FilterMeta):
"""
filter_type = elem.get('type')
if filter_type is None:
filter_type = elem.find('type').text
# If the filter type matches this class's short_name, then
# there is no overridden from_xml_element method

View file

@ -278,7 +278,7 @@ class Geometry:
"""
# Using str and os.Pathlike here to avoid error when using just the imported PathLike
# Using str and os.PathLike here to avoid error when using just the imported PathLike
# TypeError: Subscripted generics cannot be used with class and instance checks
check_type('materials', materials, (str, os.PathLike, openmc.Materials))

View file

@ -248,7 +248,7 @@ class Model:
Parameters
----------
path : str or Pathlike
path : str or PathLike
Path to model.xml file
"""
tree = ET.parse(path)
@ -473,7 +473,7 @@ class Model:
Parameters
----------
path : str or Pathlike
path : str or PathLike
Location of the XML file to write (default is 'model.xml'). Can be a
directory or file path.
remove_surfs : bool
@ -620,7 +620,7 @@ class Model:
value set by the :envvar:`OMP_NUM_THREADS` environment variable).
geometry_debug : bool, optional
Turn on geometry debugging during simulation. Defaults to False.
restart_file : str, optional
restart_file : str or PathLike
Path to restart file to use
tracks : bool, optional
Enables the writing of particles tracks. The number of particle

View file

@ -236,7 +236,7 @@ int openmc_next_batch(int* status)
// Check simulation ending criteria
if (status) {
if (simulation::current_batch == settings::n_max_batches) {
if (simulation::current_batch >= settings::n_max_batches) {
*status = STATUS_EXIT_MAX_BATCH;
} else if (simulation::satisfy_triggers) {
*status = STATUS_EXIT_ON_TRIGGER;

View file

@ -403,9 +403,11 @@ void load_state_point()
// Read batch number to restart at
read_dataset(file_id, "current_batch", simulation::restart_batch);
if (simulation::restart_batch > settings::n_batches) {
fatal_error("The number batches specified in settings.xml is fewer "
" than the number of batches in the given statepoint file.");
if (simulation::restart_batch >= settings::n_max_batches) {
fatal_error(fmt::format(
"The number of batches specified for simulation ({}) is smaller"
" than the number of batches in the restart statepoint file ({})",
settings::n_max_batches, simulation::restart_batch));
}
// Logical flag for source present in statepoint file

View file

@ -1,11 +1,11 @@
import glob
import os
from pathlib import Path
import openmc
import pytest
from tests.testing_harness import TestHarness
from tests.regression_tests import config
from tests import cdtemp
class StatepointRestartTestHarness(TestHarness):
def __init__(self, final_sp, restart_sp):
@ -42,7 +42,7 @@ class StatepointRestartTestHarness(TestHarness):
def _run_openmc_restart(self):
# Get the name of the statepoint file.
statepoint = glob.glob(os.path.join(os.getcwd(), self._restart_sp))
statepoint = list(Path.cwd().glob(self._restart_sp))
assert len(statepoint) == 1
statepoint = statepoint[0]
@ -59,3 +59,26 @@ def test_statepoint_restart():
harness = StatepointRestartTestHarness('statepoint.10.h5',
'statepoint.07.h5')
harness.main()
def test_batch_check(request):
xmls = list(request.path.parent.glob('*.xml'))
with cdtemp(xmls):
model = openmc.Model.from_xml()
model.settings.particles = 100
# run the model
sp_file = model.run()
# run a restart with the resulting statepoint
# and the settings unchanged
with pytest.raises(RuntimeError, match='is smaller than the number of batches'):
model.run(restart_file=sp_file)
# update the number of batches and run again
model.settings.batches = 15
model.settings.statepoint = {}
sp_file = model.run(restart_file=sp_file)
sp = openmc.StatePoint(sp_file)
assert sp.n_batches == 15