|
| 1 | +""" |
| 2 | +Parent class for CTSM-specific tests that first run the subset_data tool and then ensure |
| 3 | +that CTSM does not fail using the just-generated input files |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import logging |
| 9 | +from CIME.SystemTests.system_tests_common import SystemTestsCommon |
| 10 | +from CIME.user_mod_support import apply_user_mods |
| 11 | +from CIME.XML.standard_module_setup import * |
| 12 | + |
| 13 | +# In case we need to import subset_data later |
| 14 | +_CTSM_PYTHON = os.path.join( |
| 15 | + os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, "python" |
| 16 | +) |
| 17 | +sys.path.insert(1, _CTSM_PYTHON) |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class SUBSETDATASHARED(SystemTestsCommon): |
| 23 | + def __init__(self, case, subset_data_cmd): |
| 24 | + """ |
| 25 | + initialize an object interface to the SMS system test |
| 26 | + """ |
| 27 | + SystemTestsCommon.__init__(self, case) |
| 28 | + |
| 29 | + # Check the test setup |
| 30 | + if not self._case.get_value("LND_GRID") == "CLM_USRDAT": |
| 31 | + raise RuntimeError("SUBSETDATA tests require resolution CLM_USRDAT") |
| 32 | + if "serial" not in self._case.get_value("MPILIB"): |
| 33 | + raise RuntimeError("SUBSETDATA tests require a serial MPILIB") |
| 34 | + if "BGC-CROP" not in self._case.get_value("COMPSET"): |
| 35 | + raise RuntimeError("SUBSETDATA tests require a BGC-CROP compset") |
| 36 | + |
| 37 | + # Add standard subset_data arguments |
| 38 | + out_dir = os.path.join(self._get_caseroot(), "subset_data_output") |
| 39 | + self.usermods_dir = os.path.join(out_dir, "user_mods") |
| 40 | + self.subset_data_cmd = subset_data_cmd + [ |
| 41 | + "--create-user-mods", |
| 42 | + "--outdir", |
| 43 | + out_dir, |
| 44 | + "--user-mods-dir", |
| 45 | + self.usermods_dir, |
| 46 | + "--overwrite", |
| 47 | + ] |
| 48 | + |
| 49 | + # Run subset_data, if needed. |
| 50 | + # It's needed during SETUP and/or NLCOMP phases if comparing/generating a baseline because |
| 51 | + # the namelist comparison will require generating a namelist, and that will fail if we |
| 52 | + # haven't specified our custom fsurdat and other stuff. By calling self._run_subset_data() |
| 53 | + # only if the usermods directory doesn't yet exist, we avoid it being called every time the |
| 54 | + # test class is initialized (which happens, e.g., in RUN phase). |
| 55 | + # if not os.path.exists(self.usermods_dir): |
| 56 | + # self._run_subset_data() |
| 57 | + |
| 58 | + def build_phase(self, sharedlib_only=False, model_only=False): |
| 59 | + """ |
| 60 | + Run subset_data and then build the model |
| 61 | + """ |
| 62 | + |
| 63 | + # Run subset_data. |
| 64 | + # build_phase gets called twice: |
| 65 | + # - once with sharedlib_only = True and |
| 66 | + # - once with model_only = True |
| 67 | + # Because we only need subset_data run once, we only do it for the sharedlib_only call. |
| 68 | + # We could also check for the existence of the subset_data outputs, but that might lead to |
| 69 | + # a situation where the user expects subset_data to be called but it's not. Better to run |
| 70 | + # unnecessarily (e.g., if you fixed some FORTRAN code and just need to rebuild). |
| 71 | + # In that same vein, yes we did run subset_data during the first time this test case was |
| 72 | + # initialized (see __init__() above), but we're doing it again here just to be safe. |
| 73 | + if sharedlib_only: |
| 74 | + self._run_subset_data() |
| 75 | + |
| 76 | + # Do the build |
| 77 | + self.build_indv(sharedlib_only=sharedlib_only, model_only=model_only) |
| 78 | + |
| 79 | + def _run_subset_data(self): |
| 80 | + """ |
| 81 | + Run subset_data |
| 82 | + """ |
| 83 | + # Import subset_data. Do it here rather than at top because otherwise the import will |
| 84 | + # be attempted even during RUN phase. |
| 85 | + # pylint: disable=wrong-import-position,import-outside-toplevel |
| 86 | + from ctsm.subset_data import main as subset_data |
| 87 | + |
| 88 | + # Run subset_data |
| 89 | + sys.argv = self.subset_data_cmd |
| 90 | + subset_data() |
| 91 | + |
| 92 | + # Required so that CTSM doesn't fail |
| 93 | + user_nl_clm_path = os.path.join(self.usermods_dir, "user_nl_clm") |
| 94 | + with open(user_nl_clm_path, "a", encoding="utf-8") as user_nl_clm: |
| 95 | + user_nl_clm.write("\ncheck_dynpft_consistency = .false.\n") |
| 96 | + |
| 97 | + # Apply the user mods |
| 98 | + self._case.flush(flushall=True) |
| 99 | + apply_user_mods(self._get_caseroot(), self.usermods_dir) |
| 100 | + self._case.read_xml() |
0 commit comments