Skip to content

Commit

Permalink
Merge pull request payu-org#423 from jo-basevi/422-date-based-restart…
Browse files Browse the repository at this point in the history
…-history-bug

Add bug fix for restart history with empty archive directory
  • Loading branch information
aidanheerdegen authored Mar 8, 2024
2 parents b6db50b + 022fe9d commit 421431b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
31 changes: 17 additions & 14 deletions payu/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ def run(self, *user_flags):
else:
mpi_flags = []


if not isinstance(mpi_flags, list):
mpi_flags = [mpi_flags]

Expand Down Expand Up @@ -968,9 +967,12 @@ def get_restarts_to_prune(self,
if not os.path.exists(self.archive_path):
return []

# List all restart directories in archive
# Sorted list of restart directories in archive
restarts = list_archive_dirs(archive_path=self.archive_path,
dir_type='restart')
restart_indices = {}
for restart in restarts:
restart_indices[restart] = int(restart.lstrip('restart'))

# TODO: Previous logic was to prune all restarts if self.repeat_run
# Still need to figure out what should happen in this case
Expand All @@ -984,9 +986,8 @@ def get_restarts_to_prune(self,
restart_freq = self.config.get('restart_freq', default_restart_freq)
if isinstance(restart_freq, int):
# Using integer frequency to prune restarts
for restart in restarts:
restart_idx = int(restart.lstrip('restart'))
if not restart_idx % restart_freq == 0:
for restart, restart_index in restart_indices.items():
if not restart_index % restart_freq == 0:
intermediate_restarts.append(restart)
else:
# Add any intermediate restarts to restarts to prune
Expand Down Expand Up @@ -1054,16 +1055,18 @@ def get_restarts_to_prune(self,
raise ValueError("payu: error: restart_history is not an "
f"integer value: {restart_history}")

# Keep restart_history latest restarts, in addition to the
# permanently saved restarts defined by restart_freq
restarts_to_prune.extend(intermediate_restarts)
max_index = self.max_output_index(output_type="restart")
index_bound = max_index - restart_history
restarts_to_prune = [res for res in restarts_to_prune
if int(res.lstrip('restart')) <= index_bound]
if len(restarts) > 0:
max_index = restart_indices[restarts[-1]]
index_bound = max_index - restart_history

# Keep restart_history latest restarts, in addition to the
# permanently saved restarts defined by restart_freq
restarts_to_prune.extend(intermediate_restarts)
restarts_to_prune = [res for res in restarts_to_prune
if restart_indices[res] <= index_bound]

# Only expect at most 1 restart to be pruned with restart_history
is_unexpected = len(restarts_to_prune) > 1
# Expect at most 1 restart to be pruned with restart_history
is_unexpected = len(restarts_to_prune) > 1

# Log out warning if more restarts than expected will be deleted
if not force and is_unexpected:
Expand Down
24 changes: 21 additions & 3 deletions test/test_prune_restarts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import payu

from test.common import cd
from test.common import tmpdir, ctrldir, labdir
from test.common import tmpdir, ctrldir, labdir, expt_archive_dir
from test.common import config as config_orig
from test.common import write_config
from test.common import make_all_files
Expand Down Expand Up @@ -63,6 +63,9 @@ def teardown():
# Remove any created restart files
remove_expt_archive_dirs(type='restart')

# Remove experiment archive
expt_archive_dir.rmdir()


def create_test_2Y_1_month_frequency_restarts():
"""Create 2 years + 1 month worth of mom restarts directories
Expand All @@ -88,14 +91,15 @@ def create_test_2Y_1_month_frequency_restarts():
additional_path='ocean')


def write_test_config(restart_freq, restart_history=None):
def write_test_config(restart_freq=None, restart_history=None):
test_config = copy.deepcopy(config)
test_config['model'] = 'access-om2'
test_config['submodels'] = [
{'name': 'atmosphere', 'model': 'yatm'},
{'name': 'ocean', 'model': 'mom'}
]
test_config['restart_freq'] = restart_freq
if restart_freq:
test_config['restart_freq'] = restart_freq
if restart_history:
test_config['restart_history'] = restart_history

Expand Down Expand Up @@ -257,3 +261,17 @@ def test_prune_restarts_ignores_empty_restart_dirs():
]

assert restarts_to_prune_indices == [5]


def test_restart_history_with_empty_archive():
write_test_config(restart_history=3)
expt_archive_dir.mkdir()

with cd(ctrldir):
lab = payu.laboratory.Laboratory(lab_path=str(labdir))
expt = payu.experiment.Experiment(lab, reproduce=False)

# Function to test
restarts_to_prune = expt.get_restarts_to_prune()

assert restarts_to_prune == []

0 comments on commit 421431b

Please sign in to comment.