Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to batch checks for simulation restarts #2390

Merged
merged 12 commits into from
Feb 22, 2023
15 changes: 8 additions & 7 deletions openmc/executor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections.abc import Iterable
from numbers import Integral
import os
import subprocess

import openmc
Expand All @@ -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
Expand All @@ -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.

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions openmc/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ def from_xml_element(cls, elem, **kwargs):

"""
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
Expand Down
2 changes: 1 addition & 1 deletion openmc/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def from_xml(

"""

# 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))

Expand Down
6 changes: 3 additions & 3 deletions openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def from_model_xml(cls, path='model.xml'):

Parameters
----------
path : str or Pathlike
path : str or PathLike
Path to model.xml file
"""
tree = ET.parse(path)
Expand Down Expand Up @@ -473,7 +473,7 @@ def export_to_model_xml(self, path='model.xml', remove_surfs=False):

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
Expand Down Expand Up @@ -620,7 +620,7 @@ def run(self, particles=None, threads=None, geometry_debug=False,
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
Expand Down
2 changes: 1 addition & 1 deletion src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/state_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 27 additions & 4 deletions tests/regression_tests/statepoint_restart/test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -42,7 +42,7 @@ def update_results(self):

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]

Expand All @@ -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