Skip to content

Commit

Permalink
Remove archive stage check of iced restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
blimlim committed Dec 12, 2024
1 parent 9135f89 commit cd7d68c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 144 deletions.
50 changes: 23 additions & 27 deletions payu/models/access.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,29 @@ def archive(self):
mom = None

for model in self.expt.models:
if model.model_type == 'cice':

# Copy supplemental restart files to RESTART path
for f_name in model.access_restarts:
f_src = os.path.join(model.work_path, f_name)
f_dst = os.path.join(model.restart_path, f_name)

if os.path.exists(f_src):
shutil.move(f_src, f_dst)

# Copy "cice_in.nml" from work path to restart.
work_ice_nml_path = os.path.join(
model.work_path,
model.ice_nml_fname
)
restart_ice_nml_path = os.path.join(
model.restart_path,
model.ice_nml_fname
)

if os.path.exists(work_ice_nml_path):
shutil.copy2(work_ice_nml_path, restart_ice_nml_path)

if model.model_type in ('cice', 'matm'):
# Write the simulation end date to the restart date
# namelist.
Expand Down Expand Up @@ -284,33 +307,6 @@ def archive(self):
model.start_date_nml_name)
f90nml.write(end_date_dict, end_date_path, force=True)

if model.model_type == 'cice':

# Copy supplemental restart files to RESTART path
for f_name in model.access_restarts:
f_src = os.path.join(model.work_path, f_name)
f_dst = os.path.join(model.restart_path, f_name)

if os.path.exists(f_src):
shutil.move(f_src, f_dst)

# Copy "cice_in.nml" from work path to restart.
work_ice_nml_path = os.path.join(
model.work_path,
model.ice_nml_fname
)
restart_ice_nml_path = os.path.join(
model.restart_path,
model.ice_nml_fname
)

if os.path.exists(work_ice_nml_path):
shutil.copy2(work_ice_nml_path, restart_ice_nml_path)

# Check that run produced iced.YYYYMMDD restart file matching
# run end date.
model.find_matching_iced(model.restart_path, run_end_date)

if model.model_type == 'cice5':
cice5 = model
elif model.model_type == 'mom':
Expand Down
128 changes: 11 additions & 117 deletions test/models/test_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from test.common import list_expt_archive_dirs
from test.common import make_expt_archive_dir, remove_expt_archive_dirs
from test.common import config_path
from test.models.test_cice import write_iced_header
from payu.calendar import GREGORIAN, NOLEAP


Expand Down Expand Up @@ -256,13 +255,7 @@ def test_access_cice_calendar_cycling_500(
):
access_model.setup()

# Skip check of archived iced restart file. This is tested in
# in the other test_access.py tests.
with patch(
'payu.models.cice.Cice.find_matching_iced',
return_value=None
):
access_model.archive()
access_model.archive()

end_date_fpath = os.path.join(
cice_model.restart_path,
Expand All @@ -280,15 +273,15 @@ def test_access_cice_calendar_cycling_500(


@pytest.mark.parametrize(
"start_date_int, caltype, expected_runtime, expected_end_date",
[(1010101, GREGORIAN, 365*SEC_PER_DAY, 1020101),
(1010101, NOLEAP, 365*SEC_PER_DAY, 1020101),
(1040101, GREGORIAN, 366*SEC_PER_DAY, 1050101),
(1040101, NOLEAP, 365*SEC_PER_DAY, 1050101),
(3000101, GREGORIAN, 365*SEC_PER_DAY, 3010101),
(3000101, NOLEAP, 365*SEC_PER_DAY, 3010101),
(4000101, GREGORIAN, 366*SEC_PER_DAY, 4010101),
(4000101, NOLEAP, 365*SEC_PER_DAY, 4010101)]
"start_date_int, caltype, expected_runtime",
[(1010101, GREGORIAN, 365*SEC_PER_DAY),
(1010101, NOLEAP, 365*SEC_PER_DAY),
(1040101, GREGORIAN, 366*SEC_PER_DAY),
(1040101, NOLEAP, 365*SEC_PER_DAY),
(3000101, GREGORIAN, 365*SEC_PER_DAY),
(3000101, NOLEAP, 365*SEC_PER_DAY),
(4000101, GREGORIAN, 366*SEC_PER_DAY),
(4000101, NOLEAP, 365*SEC_PER_DAY)]
)
def test_access_cice_1year_runtimes(
access_1year_config,
Expand All @@ -298,8 +291,7 @@ def test_access_cice_1year_runtimes(
initial_start_date_file,
start_date_int,
caltype,
expected_runtime,
expected_end_date
expected_runtime
):
"""
The large setup/archive cycling test won't pick up situations
Expand Down Expand Up @@ -378,101 +370,3 @@ def test_access_cice_1year_runtimes(
work_input_ice = f90nml.read(cice_model.work_path/INPUT_ICE_FNAME)
written_runtime = work_input_ice["coupling"]["runtime"]
assert written_runtime == expected_runtime

# Write an iced to the restart directory matching the run end date.
iced_name = f"iced.{expected_end_date:08d}"
iced_path = cice_model.restart_path / iced_name
# Values in header unused in this test
write_iced_header(iced_path,
bint=0,
istep0=0,
time=1,
time_forc=0)

# Run archive to check that no errors are raised when restart directory
# contains iced file with correct name.
access_model.archive()

# Check that the correct end date is written to the restart directory
# calendar file
archived_calendar_path = cice_model.restart_path / RESTART_DATE_FNAME
archived_calendar_nml = f90nml.read(archived_calendar_path)
archived_end_date = archived_calendar_nml["coupling"]["inidate"]
assert archived_end_date == expected_end_date


def test_access_cice_wrong_iced(
access_1year_config,
ice_control_directory,
fake_cice_in,
restart_dir
):
"""
Test that archive stage fails when iced.YYYYMMDD restart file
matching the run end date is missing from the restart directory.
"""

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

# For the purposes of the test, use one year runtime
expt.runtime["years"] = 1
expt.runtime["months"] = 0
expt.runtime["days"] = 0
expt.runtime["seconds"] = 0

# Get the models
for model in expt.models:
if model.model_type == "cice":
cice_model = model
access_model = expt.model

# Overwrite cice model paths created during experiment initialisation.
cice_model.work_path = workdir

# Path to read and write restart dates from. In a real experiment
# The read and write restart dirs would be different and increment
# each run. However we will just use one for the tests.
cice_model.prior_restart_path = restart_dir
cice_model.restart_path = restart_dir

# Write an input_ice.nml namelist to the work directory
# specifying date information. Force the archive to calculate
# the prescribed end date by setting the runtime to 0.
correct_end_date = 1010101
work_input_ice_path = cice_model.work_path / INPUT_ICE_FNAME
input_ice_nml = {
"coupling":
{
"caltype": GREGORIAN, # Use Gregorian for simplicity
"init_date": 10101,
"inidate": correct_end_date,
"runtime": 0
}
}
f90nml.write(input_ice_nml, work_input_ice_path)

# Write an iced file to the restart directory with the wrong end date.
false_end_date = 5550101

if false_end_date == correct_end_date:
msg = ("'false_end_date' and 'correct_end_date' have "
f"the same value: {correct_end_date}. The two"
"should not match.")
raise ValueError(msg)

iced_name = f"iced.{false_end_date:08d}"
iced_path = cice_model.restart_path / iced_name
# Values in header unused in this test
write_iced_header(iced_path,
bint=0,
istep0=0,
time=1,
time_forc=0)

# Check that error raised due to missing iced file
with pytest.raises(FileNotFoundError,
match="CICE restart file not found"):
access_model.archive()

0 comments on commit cd7d68c

Please sign in to comment.