Skip to content

Commit

Permalink
Remove spinup variant and use the spinup variable instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mjaehn committed Sep 15, 2023
1 parent afc4738 commit 52da9b6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 60 deletions.
1 change: 0 additions & 1 deletion cases/cosmo-ghg-11km-test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

model = 'cosmo-ghg'
restart_step = 12 # hours
variant = 'spinup'
spinup = 6

if constraint == 'gpu':
Expand Down
12 changes: 7 additions & 5 deletions jobs/cosmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ def main(starttime, hstart, hstop, cfg, model_cfg):
"Start time %s must not be smaller than in laf_starttime %s." %
(str(starttime), str(startfiletime)))

# No restarts for COSMO-ART and for simulations with spinup
if 'restart' in model_cfg['models'][cfg.model]['features'] and \
cfg.variant != 'spinup':
# Create restart directory if feature is present and
# if there is no spinup
if 'restart' in model_cfg['models'][cfg.model]['features'] and not \
hasattr(cfg, 'spinup'):
tools.create_dir(cfg.cosmo_restart_out, "cosmo_restart_out")

# Copy cosmo executable
Expand Down Expand Up @@ -174,13 +175,14 @@ def main(starttime, hstart, hstop, cfg, model_cfg):

output_file = os.path.join(cfg.cosmo_work, "INPUT_" + section)
with open(output_file, "w") as outf:
if cfg.variant == 'spinup':
# no restarts
if hasattr(cfg, 'spinup'):
# no built-in restarts
to_write = to_write.format(cfg=cfg,
restart_start=12,
restart_stop=0,
restart_step=12)
else:
# built-in restarts
to_write = to_write.format(cfg=cfg,
restart_start=cfg.hstart +
cfg.restart_step,
Expand Down
2 changes: 1 addition & 1 deletion jobs/post_int2lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def main(start_time, hstart, hstop, cfg, model_cfg):
logging.info("OK")

# Meteo spinup simulation with tracer recycling
if cfg.variant == 'spinup' and \
if hasattr(cfg, 'spinup') and \
hasattr(cfg, 'post_int2lm_species_spinup') and not cfg.first_one:
var_list = cfg.post_int2lm_species_spinup
logging.info(
Expand Down
2 changes: 1 addition & 1 deletion jobs/tools/write_cosmo_input_ghg.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def main(csv_filename, namelist_filename, cfg=None):

with open(namelist_filename, 'a') as nml_file:
for group in reader:
if cfg.variant == 'spinup' and not cfg.first_one:
if hasattr(cfg, 'spinup') and not cfg.first_one:
nml_file.write(group2text(group, recycling=True))
else:
nml_file.write(group2text(group))
Expand Down
68 changes: 16 additions & 52 deletions run_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,6 @@ def load_config_file(casename, cfg):
return cfg


def check_model_set_variant(model_cfg, cfg):
"""Checks the model and sets its variant.
Check if a model was provided in the config-object. If no model is
provided, set the model to cosmo in the config-object.
Check if a variant was provided in the config-object. Variants
provide a way to customize the behaviour of the processing chain
for different types of simulations.
Raise a RuntimeError if an unsupported model or variant is given in cfg.
You can add models and variants in the config/models.yaml file.
Parameters
----------
cfg : config-object
"""
if hasattr(cfg, 'model'):
model_str = getattr(cfg, 'model')
else:
raise RuntimeError("Variable 'model' not set in config.")

models = model_cfg['models']
if cfg.model not in models:
raise ValueError("Invalid model: {}".format(model_str))

if hasattr(cfg, 'variant'):
variants = models[cfg.model]['variants']
if cfg.variant not in variants:
raise ValueError(f"Invalid variant for {cfg.model}: {cfg.variant}")
else:
setattr(cfg, 'variant', None)

return cfg


def run_chain(work_root, model_cfg, cfg, start_time, hstart, hstop, job_names,
force):
"""Run chain ignoring already finished jobs.
Expand Down Expand Up @@ -242,7 +206,7 @@ def run_chain(work_root, model_cfg, cfg, start_time, hstart, hstop, job_names,
inidate_int2lm_yyyymmddhh = (start_time +
timedelta(hours=hstart)).strftime('%Y%m%d%H')

if cfg.variant == 'spinup':
if hasattr(cfg, 'spinup'):
if cfg.first_one: # first run in spinup
chain_root_last_run = ''
else: # consecutive runs in spinup
Expand Down Expand Up @@ -308,7 +272,7 @@ def run_chain(work_root, model_cfg, cfg, start_time, hstart, hstop, job_names,
raise FileNotFoundError(f"File not found: {tracer_csvfile}")

# tracer_start namelist paramter for spinup simulation
if cfg.variant == 'spinup':
if hasattr(cfg, 'spinup'):
if cfg.first_one:
setattr(cfg, 'tracer_start', 0)
else:
Expand All @@ -331,7 +295,7 @@ def run_chain(work_root, model_cfg, cfg, start_time, hstart, hstop, job_names,
"gpu or mc")

# Spinup
if cfg.variant == 'spinup':
if hasattr(cfg, 'spinup'):
setattr(cfg, 'last_cosmo_output',
os.path.join(chain_root_last_run, 'cosmo', 'output'))
# No restart for spinup simulations (= default values for no restart)
Expand Down Expand Up @@ -645,18 +609,10 @@ def load_model_config_yaml(yamlfile):

print(f"Starting chain for case {casename} and model {cfg.model}")

# check for restart compatibility and spinup
if 'restart' in model_cfg['models'][cfg.model]['features']:
if cfg.variant is None:
restart_runs(work_root=cfg.work_root,
model_cfg=model_cfg,
cfg=cfg,
start=start_time,
hstart=args.hstart,
hstop=args.hstop,
job_names=args.job_list,
force=args.force)
elif cfg.variant == 'spinup':
print(f"Model variant is {cfg.variant}")
if hasattr(cfg, 'spinup'):
print("This is a spinup simulation.")
restart_runs_spinup(work_root=cfg.work_root,
model_cfg=model_cfg,
cfg=cfg,
Expand All @@ -666,9 +622,17 @@ def load_model_config_yaml(yamlfile):
job_names=args.job_list,
force=args.force)
else:
raise RuntimeError(f"Unknown variant: {cfg.variant}")
print("Built-in model restart is used.")
restart_runs(work_root=cfg.work_root,
model_cfg=model_cfg,
cfg=cfg,
start=start_time,
hstart=args.hstart,
hstop=args.hstop,
job_names=args.job_list,
force=args.force)
else:
# cosmoart can't do restarts
print("No restart is used.")
run_chain(work_root=cfg.work_root,
cfg=cfg,
start_time=start_time,
Expand Down

0 comments on commit 52da9b6

Please sign in to comment.