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

add continue feature for depletion #3272

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
49 changes: 46 additions & 3 deletions openmc/deplete/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,18 @@ class Integrator(ABC):
:attr:`solver`.

.. versionadded:: 0.12
continue_timesteps : bool, optional
Whether or not to treat the current solve as a continuation of a
previous simulation. Defaults to `False`. If `True`, the timesteps
provided to the `Integrator` must match exactly those that exist
in the `prev_results` passed to the `Operator`. The `power`,
`power_density`, or `source_rates` must match as well. It
is the user's responsibility to make sure that the continue
solve uses the same method of specifying `power`, `power_density`,
or `source_rates`.

.. versionadded:: 0.15.1

Attributes
----------
operator : openmc.deplete.abc.TransportOperator
Expand Down Expand Up @@ -556,7 +568,8 @@ def __init__(
power_density: Optional[Union[float, Sequence[float]]] = None,
source_rates: Optional[Sequence[float]] = None,
timestep_units: str = 's',
solver: str = "cram48"
solver: str = "cram48",
continue_timesteps: bool = False,
):
# Check number of stages previously used
if operator.prev_res is not None:
Expand Down Expand Up @@ -629,6 +642,24 @@ def __init__(
else:
raise ValueError(f"Invalid timestep unit '{unit}'")

# validate existing depletion steps are consistent with those passed to operator
if continue_timesteps:
completed_timesteps = operator.prev_res.get_times()
completed_source_rates = operator.prev_res.get_source_rates()
lewisgross1296 marked this conversation as resolved.
Show resolved Hide resolved
num_previous_steps_run = len(completed_timesteps)
if (
np.array_equal(completed_timesteps, timesteps[:num_previous_steps_run]) and
np.array_equal(completed_source_rates, source_rates[:num_previous_steps_run])
):
seconds = seconds[num_previous_steps_run:]
source_rates = source_rates[num_previous_steps_run:]
else:
raise ValueError(
"You are attempting to continue a run in which the previous results "
"do not have the same initial steps as those provided to the "
"Integrator. Please make sure you are using the correct timesteps, "
"powers or power densities, and previous results file."
)
self.timesteps = np.asarray(seconds)
self.source_rates = np.asarray(source_rates)

Expand Down Expand Up @@ -919,6 +950,17 @@ class SIIntegrator(Integrator):
:attr:`solver`.

.. versionadded:: 0.12
continue_timesteps : bool, optional
Whether or not to treat the current solve as a continuation of a
previous simulation. Defaults to `False`. If `True`, the timesteps
provided to the `Integrator` must match exactly those that exist
in the `prev_results` passed to the `Opereator`. The `power`,
`power_density`, or `source_rates` must match as well. It
is the user's responsibility to make sure that the continue
solve uses the same method of specifying `power`, `power_density`,
or `source_rates`.

.. versionadded:: 0.15.1

Attributes
----------
Expand Down Expand Up @@ -960,13 +1002,14 @@ def __init__(
source_rates: Optional[Sequence[float]] = None,
timestep_units: str = 's',
n_steps: int = 10,
solver: str = "cram48"
solver: str = "cram48",
continue_timesteps: bool = False,
):
check_type("n_steps", n_steps, Integral)
check_greater_than("n_steps", n_steps, 0)
super().__init__(
operator, timesteps, power, power_density, source_rates,
timestep_units=timestep_units, solver=solver)
timestep_units=timestep_units, solver=solver, continue_timesteps=continue_timesteps)
self.n_steps = n_steps

def _get_bos_data_from_operator(self, step_index, step_power, n_bos):
Expand Down
32 changes: 27 additions & 5 deletions openmc/deplete/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

__all__ = ["Results", "ResultsList"]

_SECONDS_PER_MINUTE = 60
_SECONDS_PER_HOUR = 60*60
_SECONDS_PER_DAY = 24*60*60
_SECONDS_PER_JULIAN_YEAR = 365.25*24*60*60

def _get_time_as(seconds: float, units: str) -> float:
"""Converts the time in seconds to time in different units
Expand All @@ -31,13 +35,13 @@ def _get_time_as(seconds: float, units: str) -> float:

"""
if units == "a":
return seconds / (60 * 60 * 24 * 365.25) # 365.25 due to the leap year
return seconds / _SECONDS_PER_JULIAN_YEAR
if units == "d":
return seconds / (60 * 60 * 24)
return seconds / _SECONDS_PER_DAY
elif units == "h":
return seconds / (60 * 60)
return seconds / _SECONDS_PER_HOUR
elif units == "min":
return seconds / 60
return seconds / _SECONDS_PER_MINUTE
else:
return seconds

Expand Down Expand Up @@ -71,7 +75,6 @@ def __init__(self, filename='depletion_results.h5'):
data.append(StepResult.from_hdf5(fh, i))
super().__init__(data)


@classmethod
def from_hdf5(cls, filename: PathLike):
"""Load in depletion results from a previous file
Expand Down Expand Up @@ -460,6 +463,25 @@ def get_times(self, time_units: str = "d") -> np.ndarray:

return _get_time_as(times, time_units)

def get_source_rates(self) -> np.ndarray:
"""
.. versionadded:: 0.15.1

Returns
-------
numpy.ndarray
1-D vector of source rates at each point in the depletion simulation
with the units originally defined by the user.

"""
source_rates = np.fromiter(
(r.source_rate for r in self),
dtype=self[0].source_rate.dtype,
count=len(self)-1,
) # Results duplicates the final source rate at the final simulation time

return source_rates

def get_step_where(
self, time, time_units: str = "d", atol: float = 1e-6, rtol: float = 1e-3
) -> int:
Expand Down
90 changes: 90 additions & 0 deletions tests/unit_tests/test_deplete_continue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Unit tests for openmc.deplete continue run capability.

These tests run in two steps: first a normal run and then a continue run based on the prev_results
"""

import pytest

import openmc.deplete

from tests import dummy_operator

# test that the continue timesteps works when the second integrate call contains all previous timesteps
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a docstring for the new test

@pytest.mark.parametrize("scheme", dummy_operator.SCHEMES)
def test_continue(run_in_tmpdir, scheme):
"""Test to ensure that a properly defined continue run works"""

# set up the problem

bundle = dummy_operator.SCHEMES[scheme]

operator = dummy_operator.DummyOperator()

# take first step
bundle.solver(operator, [0.75], 1.0).integrate()

# restart
prev_res = openmc.deplete.Results(
operator.output_dir / "depletion_results.h5")
operator = dummy_operator.DummyOperator(prev_res)

# if continue run happens, test passes
bundle.solver(operator, [0.75, 0.75], [1.0, 1.0], continue_timesteps=True).integrate()

@pytest.mark.parametrize("scheme", dummy_operator.SCHEMES)
def test_mismatched_initial_times(run_in_tmpdir, scheme):
"""Test to ensure that a continue run with different initial steps is properly caught"""

# set up the problem

bundle = dummy_operator.SCHEMES[scheme]

operator = dummy_operator.DummyOperator()

# take first step
bundle.solver(operator, [0.75, 0.75], [1.0, 1.0]).integrate()

# restart
prev_res = openmc.deplete.Results(
operator.output_dir / "depletion_results.h5")
operator = dummy_operator.DummyOperator(prev_res)

with pytest.raises(
ValueError,
match = "You are attempting to continue a run in which the previous "
"results do not have the same initial steps as those provided "
"to the Integrator. Please make sure you are using the correct "
"timesteps, powers or power densities, and previous results file.",
):
bundle.solver(
operator, [0.75, 0.5, 0.75], [1.0, 1.0, 1.0], continue_timesteps=True
).integrate()

@pytest.mark.parametrize("scheme", dummy_operator.SCHEMES)
def test_mismatched_initial_source_rates(run_in_tmpdir, scheme):
"""Test to ensure that a continue run with different initial steps is properly caught"""

# set up the problem

bundle = dummy_operator.SCHEMES[scheme]

operator = dummy_operator.DummyOperator()

# take first step
bundle.solver(operator, [0.75, 0.75], [1.0, 1.0]).integrate()

# restart
prev_res = openmc.deplete.Results(
operator.output_dir / "depletion_results.h5")
operator = dummy_operator.DummyOperator(prev_res)

with pytest.raises(
ValueError,
match = "You are attempting to continue a run in which the previous "
"results do not have the same initial steps as those provided "
"to the Integrator. Please make sure you are using the correct "
"timesteps, powers or power densities, and previous results file.",
):
bundle.solver(
operator, [0.75, 0.75, 0.75], [1.0, 2.0, 1.0], continue_timesteps=True
).integrate()
Loading