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