Skip to content

Commit 6de3bb5

Browse files
authored
Merge pull request #483 from ACCESS-NRI/esm1.5-patch-cice-history-nml
Enable seperate ice_history.nml & cice_in.nml settings
2 parents 39b9ba4 + 64e356c commit 6de3bb5

File tree

3 files changed

+157
-3
lines changed

3 files changed

+157
-3
lines changed

payu/models/cice.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def __init__(self, expt, name, config):
4545

4646
self.ice_nml_fname = 'cice_in.nml'
4747

48+
self.history_nml_fname = 'ice_history.nml' # only used by payu
49+
4850
self.set_timestep = self.set_local_timestep
4951

5052
self.copy_inputs = False
@@ -156,6 +158,14 @@ def get_access_ptr_restart_dir(self):
156158
def setup(self):
157159
super(Cice, self).setup()
158160

161+
# If there is a seperate ice_history.nml,
162+
# update the cice namelist with its contents
163+
history_nml_fpath = os.path.join(self.control_path,
164+
self.history_nml_fname)
165+
if os.path.isfile(history_nml_fpath):
166+
history_nml = f90nml.read(history_nml_fpath)
167+
self.ice_in.patch(history_nml)
168+
159169
setup_nml = self.ice_in['setup_nml']
160170
init_date = datetime.date(year=setup_nml['year_init'], month=1, day=1)
161171

@@ -244,9 +254,6 @@ def setup(self):
244254
assert(total_runtime % setup_nml['dt'] == 0)
245255
setup_nml['istep0'] = int(total_runtime / setup_nml['dt'])
246256

247-
# Force creation of a dump (restart) file at end of run
248-
setup_nml['dump_last'] = True
249-
250257
nml_path = os.path.join(self.work_path, self.ice_nml_fname)
251258
self.ice_in.write(nml_path, force=True)
252259

payu/models/cice5.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def set_local_timestep(self, t_step):
5353
self.ice_in.write(ice_in_path, force=True)
5454

5555
def setup(self):
56+
# Force creation of a dump (restart) file at end of run
57+
self.ice_in['setup_nml']['dump_last'] = True
58+
5659
super(Cice5, self).setup()
5760

5861
# Make log dir

test/models/test_cice.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import os
2+
import shutil
3+
4+
import pytest
5+
import f90nml
6+
7+
import payu
8+
9+
from test.common import cd
10+
from test.common import tmpdir, ctrldir, labdir, expt_workdir, ctrldir_basename
11+
from test.common import write_config, write_metadata
12+
from test.common import make_inputs, make_exe
13+
14+
verbose = True
15+
16+
DEFAULT_CICE_NML = {
17+
"setup_nml": {
18+
"history_dir": "./HISTORY/",
19+
"restart_dir": "./RESTART/",
20+
"year_init": 9999,
21+
"days_per_year": 360,
22+
"ice_ic": "default",
23+
"restart": ".false.",
24+
"pointer_file": "./RESTART/ice.restart_file",
25+
"runtype": "initial",
26+
"npt": 99999,
27+
"dt": 1,
28+
},
29+
"grid_nml": {"grid_file": "./INPUT/grid.nc", "kmt_file": "./INPUT/kmt.nc"},
30+
"icefields_nml": {"f_icy": "x"},
31+
}
32+
CICE_NML_NAME = "cice_in.nml"
33+
HIST_NML_NAME = "ice_history.nml"
34+
35+
36+
def setup_module(module):
37+
"""
38+
Put any test-wide setup code in here, e.g. creating test files
39+
"""
40+
if verbose:
41+
print("setup_module module:%s" % module.__name__)
42+
43+
# Should be taken care of by teardown, in case remnants lying around
44+
try:
45+
shutil.rmtree(tmpdir)
46+
except FileNotFoundError:
47+
pass
48+
49+
try:
50+
tmpdir.mkdir()
51+
labdir.mkdir()
52+
ctrldir.mkdir()
53+
expt_workdir.mkdir(parents=True)
54+
make_inputs()
55+
make_exe()
56+
write_metadata()
57+
except Exception as e:
58+
print(e)
59+
60+
config = {
61+
"laboratory": "lab",
62+
"jobname": "testrun",
63+
"model": "cice",
64+
"exe": "test.exe",
65+
"experiment": ctrldir_basename,
66+
"metadata": {"enable": False},
67+
}
68+
write_config(config)
69+
70+
71+
def teardown_module(module):
72+
"""
73+
Put any test-wide teardown code in here, e.g. removing test outputs
74+
"""
75+
if verbose:
76+
print("teardown_module module:%s" % module.__name__)
77+
78+
try:
79+
shutil.rmtree(tmpdir)
80+
print("removing tmp")
81+
except Exception as e:
82+
print(e)
83+
84+
85+
# Confirm that 1: payu overwrites cice_in with ice_history
86+
# 2: payu works without ice_history.nml
87+
# 3: payu overwrites cice_in and allows additional fields
88+
# In all cases confirm dump_last is not added to model_type='cice'
89+
@pytest.mark.parametrize(
90+
"ice_history",
91+
[
92+
{"icefields_nml": { "f_icy": "m" }},
93+
False,
94+
{"icefields_nml": {"f_icy": "m", "f_new": "y"}},
95+
],
96+
)
97+
def test_setup(ice_history):
98+
cice_nml = DEFAULT_CICE_NML
99+
100+
# create the files parsed by setup:
101+
# 1. a restart pointer file
102+
with cd(expt_workdir):
103+
os.mkdir(cice_nml["setup_nml"]["restart_dir"])
104+
with open(cice_nml["setup_nml"]["pointer_file"], "w") as f:
105+
f.write("./RESTART/ice.r")
106+
f.close()
107+
108+
with cd(ctrldir):
109+
# 2. Create config.nml
110+
f90nml.write(cice_nml, CICE_NML_NAME)
111+
if ice_history:
112+
f90nml.write(ice_history, HIST_NML_NAME)
113+
114+
lab = payu.laboratory.Laboratory(lab_path=str(labdir))
115+
expt = payu.experiment.Experiment(lab, reproduce=False)
116+
model = expt.models[0]
117+
118+
# Function to test
119+
model.setup()
120+
121+
# Check config files are moved to model's work path
122+
work_path_files = os.listdir(model.work_path)
123+
assert CICE_NML_NAME in work_path_files
124+
125+
# Check cice_in was patched with ice_history
126+
work_input_fpath = os.path.join(model.work_path, CICE_NML_NAME)
127+
input_nml = f90nml.read(work_input_fpath)
128+
if ice_history:
129+
assert input_nml["icefields_nml"] == ice_history["icefields_nml"]
130+
else:
131+
assert input_nml["icefields_nml"] == DEFAULT_CICE_NML["icefields_nml"]
132+
133+
# Check dump_last doesn't exist
134+
with pytest.raises(KeyError, match="dump_last"):
135+
input_nml["setup_nml"]["dump_last"]
136+
137+
# cleanup
138+
with cd(expt_workdir):
139+
os.remove(cice_nml["setup_nml"]["pointer_file"])
140+
os.rmdir(cice_nml["setup_nml"]["restart_dir"])
141+
with cd(ctrldir):
142+
os.remove(CICE_NML_NAME)
143+
if ice_history:
144+
os.remove(HIST_NML_NAME)

0 commit comments

Comments
 (0)