ece-lsm.sh.tmpl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #!/usr/bin/env bash
  2. set -ue
  3. # autosubmit stuff - this is ignored if using the classic runtime
  4. if [[ "%%_%%" == "%_%" ]]
  5. then
  6. use_autosubmit=true
  7. set -xuve
  8. # go to autosubmit root directory
  9. cd %CURRENT_ROOTDIR%
  10. # Simulation member. Use any (reasonable) syntax you want.
  11. member="%MEMBER%"
  12. else
  13. use_autosubmit=false
  14. # Simulation member. Use any (reasonable) syntax you want.
  15. member="fc0"
  16. fi
  17. # librunscript defines some helper functions
  18. source ./librunscript.sh
  19. # =============================================================================
  20. # *** BEGIN User configuration
  21. # =============================================================================
  22. # -----------------------------------------------------------------------------
  23. # *** General configuration
  24. # -----------------------------------------------------------------------------
  25. # Component configuration (for syntax of the $config variable, see librunscript.sh)
  26. #
  27. if $use_autosubmit
  28. then
  29. [[ "%LPJG%" = TRUE ]] && config="%LPJG_CONFIG% lpjg_forcing:IFS oasis" || config=""
  30. else
  31. config="lpjg lpjg_forcing:IFS oasis"
  32. fi
  33. # minimum sanity
  34. has_config lpjg_forcing ifs && error "Cannot have both ifs and lpjg_forcing in config!!"
  35. # Experiment name (exactly 4 letters!)
  36. export exp_name=[[[MOD:GENERAL:EXP_NAME]]]
  37. # Simulation start and end date. Use any (reasonable) syntax you want.
  38. run_start_date="[[[MOD:GENERAL:RUN_START_DATE]]]"
  39. run_end_date="[[[MOD:GENERAL:RUN_END_DATE]]]"
  40. # Set $force_run_from_scratch to 'true' if you want to force this run to start
  41. # from scratch, possibly ignoring any restart files present in the run
  42. # directory. Leave set to 'false' otherwise.
  43. # NOTE: If set to 'true' the run directory $run_dir is cleaned!
  44. force_run_from_scratch=[[[MOD:GENERAL:FORCE_RUN_FROM_SCRATCH]]]
  45. # Resolution (only T255L91 supported for now)
  46. ifs_grid=T255L91 #[[[MOD:IFS:GRID]]]
  47. #atm_grid=A128
  48. # Restart frequency. Use any (reasonable) number and time unit you want.
  49. # For runs without restart, leave this variable empty
  50. rst_freq="[[[MOD:GENERAL:RST_FREQ]]]"
  51. # Number of restart legs to be run in one go
  52. run_num_legs=[[[MOD:GENERAL:RUN_NUM_LEGS]]]
  53. # Coupling frequencies
  54. has_config lpjg && cpl_freq_atm_lpjg_hrs=[[[MOD:OASIS:CPL_FREQ_ATM_LPJG_HRS]]] #24
  55. # Directories
  56. start_dir=${PWD}
  57. ctrl_file_dir=${start_dir}/ctrl
  58. # Architecture
  59. build_arch=[[[PLT:ACTIVE:BUILD_ARCH]]]
  60. # This file is used to store information about restarts
  61. ece_info_file="ece.info"
  62. # -----------------------------------------------------------------------------
  63. # *** Read platform dependent configuration
  64. # -----------------------------------------------------------------------------
  65. source ./ecconf.cfg
  66. configure
  67. # -----------------------------------------------------------------------------
  68. # *** IFS configuration - these variables are needed by LPJG
  69. # -----------------------------------------------------------------------------
  70. export ifs_res_hor=$(echo ${ifs_grid} | sed 's:T\([0-9]\+\)L\([0-9]\+\):\1:')
  71. ifs_res_ver=$(echo ${ifs_grid} | sed 's:T\([0-9]\+\)L\([0-9]\+\):\2:')
  72. ifs_cmip_fixyear=[[[MOD:IFS:CMIP_FIXYEAR]]]
  73. ifs_A4xCO2=[[[MOD:IFS:CMIP6_A4xCO2]]]
  74. ifs_1PCTCO2=[[[MOD:IFS:CMIP6_1PCTCO2]]]
  75. # -----------------------------------------------------------------------------
  76. # *** LPJ-GUESS offline forcing configuration
  77. # -----------------------------------------------------------------------------
  78. lpjg_forcing_exe_file=${ecearth_src_dir}/lpjg/offline/lpjg_forcing
  79. has_config lpjg_forcing:IFS && lpjg_forcing_exe_file=${ecearth_src_dir}/lpjg/offline/lpjg_forcing_ifs
  80. lpjg_forcing_numproc=1
  81. # TODO add this to config-run.xml ?
  82. lpjg_forcing_files_path="/gpfs/projects/bsc32/bsc32051/lpjg/forcing/a0pq/out"
  83. # -----------------------------------------------------------------------------
  84. # *** LPJ-GUESS configuration
  85. # -----------------------------------------------------------------------------
  86. lpjg_time_step_sec=[[[MOD:LPJG:TIME_STEP_SEC]]]
  87. lpjg_numproc=[[[MOD:LPJG:NUMPROC]]]
  88. has_config lpjg && lpjg_on=1
  89. has_config lpjg:fdbck && lpjg_fdbck=1
  90. has_config tm5 && lpjg_fdbck_tm5=1 || lpjg_fdbck_tm5=0
  91. export lpjg_fixNdepafter=[[[MOD:LPJG:LPJG_FIXNDEPAFTER]]]
  92. export lpjg_fixLUafter=[[[MOD:LPJG:LPJG_FIXLUAFTER]]]
  93. lpjg_exe_file=${ecearth_src_dir}/lpjg/build/guess
  94. # -----------------------------------------------------------------------------
  95. # =============================================================================
  96. # *** END of User configuration
  97. # =============================================================================
  98. # =============================================================================
  99. # *** This is where the code begins ...
  100. # =============================================================================
  101. # -----------------------------------------------------------------------------
  102. # *** Create the run dir if necessary and go there
  103. # Everything is done from here.
  104. # -----------------------------------------------------------------------------
  105. if [ ! -d ${run_dir} ]
  106. then
  107. mkdir -p ${run_dir}
  108. fi
  109. cd ${run_dir}
  110. # -----------------------------------------------------------------------------
  111. # *** Determine the time span of this run and whether it's a restart leg
  112. # -----------------------------------------------------------------------------
  113. # Regularise the format of the start and end date of the simulation
  114. run_start_date=$(date -uR -d "${run_start_date}")
  115. run_end_date=$(date -uR -d "${run_end_date}")
  116. # Loop over the number of legs
  117. for (( ; run_num_legs>0 ; run_num_legs-- ))
  118. do
  119. # Check for restart information file and set the current leg start date
  120. # Ignore restart information file if force_run_from_scratch is true
  121. if ${force_run_from_scratch} || ! [ -r ${ece_info_file} ]
  122. then
  123. leg_is_restart=false
  124. leg_start_date=${run_start_date}
  125. leg_number=1
  126. else
  127. leg_is_restart=true
  128. . ./${ece_info_file}
  129. leg_start_date=${leg_end_date}
  130. leg_number=$((leg_number+1))
  131. fi
  132. # Compute the end date of the current leg
  133. if [ -n "${rst_freq}" ]
  134. then
  135. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq}")
  136. else
  137. leg_end_date=${run_end_date}
  138. fi
  139. if [ $(date -d "${leg_end_date}" +%s) -gt $(date -d "${run_end_date}" +%s) ]
  140. then
  141. leg_end_date=${run_end_date}
  142. fi
  143. # Some time variables needed later
  144. leg_length_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${leg_start_date}" +%s) ))
  145. leg_start_sec=$(( $(date -d "${leg_start_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  146. leg_end_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  147. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  148. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  149. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  150. leg_length_years=$(( ${leg_end_date_yyyy} - ${leg_start_date_yyyy} ))
  151. # Check whether there's actually time left to simulate - exit otherwise
  152. if [ ${leg_length_sec} -le 0 ]
  153. then
  154. info "Leg start date equal to or after end of simulation."
  155. info "Nothing left to do. Exiting."
  156. exit 0
  157. fi
  158. # -------------------------------------------------------------------------
  159. # *** Prepare the run directory for a run from scratch
  160. # -------------------------------------------------------------------------
  161. if ! $leg_is_restart
  162. then
  163. # ---------------------------------------------------------------------
  164. # *** Check if run dir is empty. If not, and if we are allowed to do so
  165. # by ${force_run_from_scratch}, remove everything
  166. # ---------------------------------------------------------------------
  167. if $(ls * >& /dev/null)
  168. then
  169. if ${force_run_from_scratch}
  170. then
  171. rm -fr ${run_dir}/*
  172. else
  173. error "Run directory ${run_dir} not empty and \$force_run_from_scratch not set."
  174. fi
  175. fi
  176. # ---------------------------------------------------------------------
  177. # *** Copy executables of model components
  178. # *** Additionally, create symlinks to the original place for reference
  179. # ---------------------------------------------------------------------
  180. if $(has_config lpjg)
  181. then
  182. cp ${lpjg_exe_file} .
  183. ln -s ${lpjg_exe_file} $(basename ${lpjg_exe_file}).lnk
  184. fi
  185. if $(has_config lpjg_forcing)
  186. then
  187. cp ${lpjg_forcing_exe_file} .
  188. fi
  189. # ---------------------------------------------------------------------
  190. # *** Files needed for LPJ-GUESS
  191. # ---------------------------------------------------------------------
  192. if $(has_config lpjg)
  193. then
  194. # Initial data - saved state for LPJ-GUESS (.bin format)
  195. lpjgstartdir=$(printf "lpjg_state_%04d" $leg_start_date_yyyy)
  196. ln -sf ${ini_data_dir}/lpjg/${lpjgstartdir}/ ${run_dir}/${lpjgstartdir}
  197. # Control files (i.e. .ins, landuse, N deposition, soil type files etc.)
  198. #FIXME: the ins files should not come from two sources and even if, the source dir should be the first that is copied
  199. #and then insfiles out of the ini or some experiment data dir could overwrite some of the defaults.
  200. #cp ${ini_data_dir}/lpjg/insfiles/* ${run_dir}
  201. cp -f ${ecearth_src_dir}/lpjg/data/ins/*.ins .
  202. mkdir -p ${run_dir}/landuse
  203. cp ${ini_data_dir}/lpjg/landuse/* ${run_dir}/landuse
  204. mkdir -p ${run_dir}/ndep
  205. cp ${ini_data_dir}/lpjg/ndep/* ${run_dir}/ndep
  206. cp ${ini_data_dir}/lpjg/ndep/* ${run_dir}
  207. cp ${ini_data_dir}/ifs/cmip6-data/CMIP6_histo_mole_fraction_of_carbon_dioxide_in_air_input4MIPs_gr1-GMNHSH.nc ${run_dir}
  208. fi
  209. #TODO ET ask Anthoni for this file
  210. # if $(has_config GSWP3)
  211. # then
  212. # cp -f ${ecearth_src_dir}/lpjg/offline/guess_GSWP3.ins ${run_dir}/guess.ins
  213. # fi
  214. else # i.e. $leg_is_restart == true
  215. # ---------------------------------------------------------------------
  216. # *** Remove all leftover output files from previous legs
  217. # ---------------------------------------------------------------------
  218. # OSM lpjg files ????
  219. echo nothing to do
  220. fi # ! $leg_is_restart
  221. # -------------------------------------------------------------------------
  222. # *** Create some control files
  223. # -------------------------------------------------------------------------
  224. # namelist and namcouple files
  225. has_config lpjg_forcing && . ${ctrl_file_dir}/namelist.lpjg_forcing.sh > namelist.lpjg_forcing
  226. . ${ctrl_file_dir}/namcouple.sh > namcouple
  227. # -------------------------------------------------------------------------
  228. # *** LPJ-GUESS initial data
  229. # -------------------------------------------------------------------------
  230. if $(has_config lpjg)
  231. then
  232. # LPJG runtime rcfile - update with leg dates
  233. . ${ctrl_file_dir}/namelist.lpjg.sh > lpjg_steps.rc
  234. # Populate or update LPJG run directories
  235. for (( n=1; n<=${lpjg_numproc}; n++ ))
  236. do
  237. if ! $leg_is_restart
  238. then
  239. # Make output directories
  240. # add the ioopt config option if running on one node to speed up output
  241. if $(has_config lpjg:ioopt)
  242. then
  243. mkdir -p $TMPDIR/lpjg/run${n}/output
  244. mkdir -p ${run_dir}/run${n}
  245. ln -sf $TMPDIR/lpjg/run${n}/output ${run_dir}/run${n}/output
  246. else
  247. mkdir -p ${run_dir}/run${n}/output
  248. fi
  249. # Copy *.ins, lpjg_steps.rc and OASIS-MCT grid and restart files
  250. cp ${run_dir}/*.ins ${run_dir}/run${n}
  251. cp ${ini_data_dir}/lpjg/oasismct/*.nc ${run_dir}
  252. cp ${ini_data_dir}/lpjg/oasismct/*.txt ${run_dir}/run${n}
  253. # Link OASIS-MCT grid definition files
  254. # would normally come from oasis/T255-... dir
  255. for f in areas grids masks
  256. do
  257. ln -sf ${f}lpjg.nc ${f}.nc
  258. done
  259. # If num_proc has increased provide additional rundirs.
  260. elif [ ! -d ${run_dir}/run${n} ]
  261. then
  262. mkdir -p ${run_dir}/run${n}/output
  263. cp ${run_dir}/*.ins ${run_dir}/run${n}
  264. cp ${ini_data_dir}/lpjg/oasismct/*.txt ${run_dir}/run${n}
  265. fi
  266. # Refresh dirs after they hav been removed after end of leg
  267. mkdir -p ${run_dir}/run${n}/output/CMIP6
  268. done
  269. if $leg_is_restart
  270. then
  271. lpjg_restart_dir="restart/lpjg/$(printf %03d $((leg_number-1)))"
  272. lpjg_rst_state="${lpjg_restart_dir}/lpjg_state_${leg_start_date_yyyy}"
  273. if [ -d "$lpjg_rst_state" ]
  274. then
  275. ln -sf $lpjg_rst_state
  276. else
  277. echo "lpjg restart dir $lpjg_rst_state not available"
  278. exit -1
  279. fi
  280. # get oasis restart files
  281. oasis_restart_dir="restart/oasis/$(printf %03d $((leg_number)))"
  282. cp -f ${oasis_restart_dir}/*.nc ${run_dir}/
  283. fi
  284. fi
  285. # -------------------------------------------------------------------------
  286. # *** Remove some OASIS files of the previous leg
  287. # -------------------------------------------------------------------------
  288. # TODO check oasis restart files
  289. #if $leg_is_restart
  290. #then
  291. # rm -f anaisout_*
  292. #fi
  293. # -------------------------------------------------------------------------
  294. # *** Start the run
  295. # -------------------------------------------------------------------------
  296. #needed for osm???
  297. # export DR_HOOK_IGNORE_SIGNALS='-1'
  298. # export CPLNG='active'
  299. has_config lpjg && cmd="${lpjg_numproc} ${lpjg_exe_file} guess.ins -parallel" || error "no lpjg in config"
  300. has_config lpjg_forcing && cmd=${cmd}" -- ${lpjg_forcing_numproc} ${lpjg_forcing_exe_file}"
  301. t1=$(date +%s)
  302. launch $cmd
  303. t2=$(date +%s)
  304. tr=$(date -d "0 -$t1 sec + $t2 sec" +%T)
  305. # -------------------------------------------------------------------------
  306. # *** Check for signs of success
  307. # Note the tests provide no guarantee that things went fine! They are
  308. # just based on the IFS, NEMO and TM5 log files. More tests (e.g. checking
  309. # restart files) could be implemented.
  310. # -------------------------------------------------------------------------
  311. # Check for LPJG success ???
  312. # -------------------------------------------------------------------------
  313. # *** Move LPJ-GUESS output files to archive directory
  314. # -------------------------------------------------------------------------
  315. if $(has_config lpjg)
  316. then
  317. outdir="output/lpjg/$(printf %03d $((leg_number)))"
  318. if [ -d ${outdir} ]
  319. then
  320. rm -rf ${outdir}
  321. fi
  322. mkdir -p ${outdir}
  323. # LPJG run directories
  324. # concatenate *.out files from each run* into output dir
  325. # DMG: adding escaped % for autosubmit, undo when issue #42 is resolved
  326. if $use_autosubmit
  327. then
  328. flist=$(find ${run_dir}/run1/output/ -type f | sed "s%${run_dir}/run1/output\/%%%%")
  329. else
  330. flist=$(find ${run_dir}/run1/output/ -type f | sed "s%${run_dir}/run1/output\/%%")
  331. fi
  332. mkdir ${outdir}/CMIP6
  333. for (( n=1; n<=${lpjg_numproc}; n++ ))
  334. do
  335. for ofile in $flist
  336. do
  337. if (( n == 1 ))
  338. then
  339. cat ${run_dir}/run${n}/output/${ofile} > ${outdir}/$ofile
  340. else
  341. awk '(FNR!=1){print $0}' ${run_dir}/run${n}/output/${ofile} >> ${outdir}/$ofile
  342. fi
  343. done
  344. rm -rf ${run_dir}/run${n}/output
  345. done
  346. # move monthly file if available
  347. if [ -f ${run_dir}/LPJ-GUESS_monthlyoutput.txt ]
  348. then
  349. mv ${run_dir}/LPJ-GUESS_monthlyoutput.txt ${outdir}
  350. fi
  351. fi
  352. #the output might be large, lets gzip those text files
  353. # this is not in the esm runscript anymore
  354. find ${outdir} -type f | xargs -I %% gzip %%
  355. # -------------------------------------------------------------------------
  356. # *** Move LPJ-GUESS restart files to archive directory
  357. # -------------------------------------------------------------------------
  358. if $(has_config lpjg)
  359. then
  360. outdir="restart/lpjg/$(printf %03d $((leg_number)))"
  361. if [ -d ${outdir} ]
  362. then
  363. rm -rf ${outdir}
  364. fi
  365. mkdir -p ${outdir}
  366. state_dir="./lpjg_state_$(printf %04d $((leg_end_date_yyyy)))"
  367. mv ${state_dir} ${outdir}
  368. # LPJG writes into run1 dir, so mv to main rundir
  369. mv -f run1/lpjgv.nc .
  370. # remove restart link
  371. if $leg_is_restart
  372. then
  373. old_state_dir="./lpjg_state_$(printf %04d $((leg_start_date_yyyy)))"
  374. if [ -L $old_state_dir ]
  375. then
  376. rm -f "$old_state_dir"
  377. fi
  378. fi
  379. fi
  380. # -------------------------------------------------------------------------
  381. # *** Move log files to archive directory
  382. # -------------------------------------------------------------------------
  383. outdir="log/$(printf %03d $((leg_number)))"
  384. mkdir -p ${outdir}
  385. for f in \
  386. guess.log \
  387. nout.000000 debug.??.?????? A*_??.nc O*_??.nc
  388. do
  389. test -f ${f} && mv ${f} ${outdir}
  390. done
  391. # lpjg run logs
  392. # this is not in the esm runscript anymore
  393. # other (lpjg_forcing osm) logs ???
  394. if $(has_config lpjg)
  395. then
  396. mkdir -p ${outdir}/lpjg
  397. for (( n=1; n<=${lpjg_numproc}; n++ ))
  398. do
  399. #concat the lpjg run logs
  400. if (( n == 1 ))
  401. then
  402. echo "--------- lpjg run${n} log --------" > ${outdir}/lpjg/guess_runs.log
  403. else
  404. echo "--------- lpjg run${n} log --------" >> ${outdir}/lpjg/guess_runs.log
  405. fi
  406. cat ${run_dir}/run${n}/guess$((n-1)).log >> ${outdir}/lpjg/guess_runs.log
  407. done
  408. fi
  409. # -------------------------------------------------------------------------
  410. # *** Copy OASIS restart files to archive directory
  411. # NOTE: These files are copied and not moved as they are used in the
  412. # next leg!
  413. # Note also that the OASIS restart files present at the end of
  414. # the leg correspond to the start of the next leg!
  415. # -------------------------------------------------------------------------
  416. outdir="restart/oasis/$(printf %03d $((leg_number+1)))"
  417. mkdir -p ${outdir}
  418. for f in vegin.nc lpjgv.nc
  419. do
  420. test -f ${f} && cp ${f} ${outdir}
  421. done
  422. # -------------------------------------------------------------------------
  423. # *** Write the restart control file
  424. # -------------------------------------------------------------------------
  425. # Compute CPMIP performance
  426. sypd="$(cpmip_sypd $leg_length_sec $(($t2 - $t1)))"
  427. ncores=0
  428. # has_config osm && (( ncores+=${osm_numproc} )) || :
  429. has_config lpjg && (( ncores+=${lpjg_numproc} )) || :
  430. has_config lpjg_forcing && (( ncores+=${lpjg_forcing_numproc} )) || :
  431. chpsy="$(cpmip_chpsy $leg_length_sec $(($t2 - $t1 +1)) $ncores)" #t_run+1 prevent div zero, just in case
  432. echo "#" | tee -a ${ece_info_file}
  433. echo "# Finished leg at `date '+%F %T'` after ${tr} (hh:mm:ss)" \
  434. | tee -a ${ece_info_file}
  435. echo "# CPMIP performance: $sypd SYPD $chpsy CHPSY"| tee -a ${ece_info_file}
  436. echo "leg_number=${leg_number}" | tee -a ${ece_info_file}
  437. echo "leg_start_date=\"${leg_start_date}\"" | tee -a ${ece_info_file}
  438. echo "leg_end_date=\"${leg_end_date}\"" | tee -a ${ece_info_file}
  439. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  440. force_run_from_scratch=false
  441. done # loop over legs
  442. # -----------------------------------------------------------------------------
  443. # *** Platform dependent finalising of the run
  444. # -----------------------------------------------------------------------------
  445. finalise
  446. $use_autosubmit || exit 0