run.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #!/bin/bash
  2. # Submission script for Lemaitre2
  3. #SBATCH --job-name=NEMO2r0
  4. #SBATCH --time=10:00:00
  5. #
  6. #SBATCH --ntasks=65
  7. #SBATCH --mem-per-cpu=3072
  8. #SBATCH --partition=Def
  9. #
  10. #SBATCH --mail-user=pierre-yves.barriat@uclouvain.be
  11. #SBATCH --mail-type=ALL
  12. set -ue
  13. # =============================================================================
  14. # *** BEGIN User configuration
  15. # =============================================================================
  16. script_name="run.sh"
  17. # -----------------------------------------------------------------------------
  18. # *** General configuration
  19. # -----------------------------------------------------------------------------
  20. # Component configuration
  21. # (for syntax of the $config variable, see librunscript.sh)
  22. config="nemo lim3 xios:detached"
  23. # Experiment name (exactly 4 letters!)
  24. exp_name=EXP9
  25. # Simulation start and end date. Use any (reasonable) syntax you want.
  26. run_start_date="1990-01-01"
  27. run_end_date="${run_start_date} + 4 years"
  28. # Set $force_run_from_scratch to 'true' if you want to force this run to start
  29. # from scratch, possibly ignoring any restart files present in the run
  30. # directory. Leave set to 'false' otherwise.
  31. # NOTE: If set to 'true' the run directory $run_dir is cleaned!
  32. force_run_from_scratch=false
  33. # Resolution
  34. #nem_grid=ORCA1L75
  35. nem_grid=ORCA2L31
  36. # Restart frequency. Use any (reasonable) number and time unit you want.
  37. # For runs without restart, leave this variable empty
  38. rst_freq="1 year"
  39. # Number of restart legs to be run in one go
  40. run_num_legs=2
  41. # Directories
  42. start_dir=${SLURM_SUBMIT_DIR}
  43. #start_dir=${PWD}
  44. ctrl_file_dir=${start_dir}/ctrl
  45. # This file is used to store information about restarts
  46. ece_info_file="ece.info"
  47. # -----------------------------------------------------------------------------
  48. # *** Read platform dependent configuration
  49. # -----------------------------------------------------------------------------
  50. # librunscript defines some helper functions
  51. source ${start_dir}/librunscript.sh
  52. source ${start_dir}/nemo.cfg
  53. configure
  54. # -----------------------------------------------------------------------------
  55. # *** NEMO/LIM configuration
  56. # -----------------------------------------------------------------------------
  57. nem_time_step_sec=2700
  58. lim_time_step_sec=2700
  59. nem_restart_offset=0
  60. nem_res_hor=$(echo ${nem_grid} | sed 's:ORCA\([0-9]\+\)L[0-9]\+:\1:')
  61. nem_forcing_set=fill
  62. nem_config_name=ORCA2_LIM3
  63. nem_exe_file=${nemo_src_dir}/CONFIG/${nem_config_name}/BLD/bin/nemo.exe
  64. nem_numproc=64
  65. # -----------------------------------------------------------------------------
  66. # *** XIOS configuration
  67. # -----------------------------------------------------------------------------
  68. xio_exe_file=${nemo_src_dir}/EXTERNAL/xios-1.0/bin/xios_server.exe
  69. xio_numproc=1
  70. # =============================================================================
  71. # *** END of User configuration
  72. # =============================================================================
  73. # =============================================================================
  74. # *** This is where the code begins ...
  75. # =============================================================================
  76. # -----------------------------------------------------------------------------
  77. # *** Make sure to clean up on exit
  78. # -----------------------------------------------------------------------------
  79. trap 'cleanup' EXIT SIGHUP SIGINT SIGTERM
  80. # -----------------------------------------------------------------------------
  81. # *** Create the run dir if necessary and go there
  82. # Everything is done from here.
  83. # -----------------------------------------------------------------------------
  84. if [ ! -d ${run_dir} ]
  85. then
  86. mkdir -p ${run_dir}
  87. force_run_from_scratch=true
  88. else
  89. force_run_from_scratch=false
  90. if ! [ -r ${run_dir}/${ece_info_file} ]
  91. then
  92. info "Problem: ${ece_info_file} doesn't exist. Exiting."
  93. exit 0
  94. fi
  95. fi
  96. cd ${run_dir}
  97. # -----------------------------------------------------------------------------
  98. # *** Determine the time span of this run and whether it's a restart leg
  99. # -----------------------------------------------------------------------------
  100. # Regularise the format of the start and end date of the simulation
  101. run_start_date=$(date -uR -d "${run_start_date}")
  102. run_end_date=$(date -uR -d "${run_end_date}")
  103. # Loop over the number of legs
  104. for (( ; run_num_legs>0 ; run_num_legs-- ))
  105. do
  106. # Check for restart information file and set the current leg start date
  107. # Ignore restart information file if force_run_from_scratch is true
  108. if ${force_run_from_scratch} || ! [ -r ${ece_info_file} ]
  109. then
  110. leg_is_restart=false
  111. leg_start_date=${run_start_date}
  112. leg_number=1
  113. else
  114. leg_is_restart=true
  115. . ./${ece_info_file}
  116. leg_start_date=${leg_end_date}
  117. leg_number=$((leg_number+1))
  118. fi
  119. # Compute the end date of the current leg
  120. if [ -n "${rst_freq}" ]
  121. then
  122. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq}")
  123. else
  124. leg_end_date=${run_end_date}
  125. fi
  126. if [ $(date -d "${leg_end_date}" +%s) -gt $(date -d "${run_end_date}" +%s) ]
  127. then
  128. leg_end_date=${run_end_date}
  129. fi
  130. # Some time variables needed later
  131. leg_length_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${leg_start_date}" +%s) ))
  132. leg_start_sec=$(( $(date -d "${leg_start_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  133. leg_end_sec=$(( $(date -d "${leg_end_date}" +%s) - $(date -d "${run_start_date}" +%s) ))
  134. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  135. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  136. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  137. # Correct for leap days because NEMO standalone uses no-leap calendar
  138. leg_length_sec=$(( leg_length_sec - $(leap_days "${leg_start_date}" "${leg_end_date}")*24*3600 ))
  139. leg_start_sec=$(( leg_start_sec - $(leap_days "${run_start_date}" "${leg_start_date}")*24*3600 ))
  140. leg_end_sec=$(( leg_end_sec - $(leap_days "${run_start_date}" "${leg_end_date}")*24*3600 ))
  141. # Check whether there's actually time left to simulate - exit otherwise
  142. if [ ${leg_length_sec} -le 0 ]
  143. then
  144. info "Leg start date equal to or after end of simulation."
  145. info "Nothing left to do. Exiting."
  146. exit 0
  147. fi
  148. # -------------------------------------------------------------------------
  149. # *** Prepare the run directory for a run from scratch
  150. # -------------------------------------------------------------------------
  151. if ! $leg_is_restart
  152. then
  153. # ---------------------------------------------------------------------
  154. # *** Check if run dir is empty. If not, and if we are allowed to do so
  155. # by ${force_run_from_scratch}, remove everything
  156. # ---------------------------------------------------------------------
  157. if $(ls * >& /dev/null)
  158. then
  159. if ${force_run_from_scratch}
  160. then
  161. rm -fr ${run_dir}/*
  162. else
  163. error "Run directory not empty and \$force_run_from_scratch not set."
  164. fi
  165. fi
  166. # ---------------------------------------------------------------------
  167. # *** Copy executables of model components
  168. # *** Additionally, create symlinks to the original place for reference
  169. # ---------------------------------------------------------------------
  170. cp ${nem_exe_file} .
  171. ln -s ${nem_exe_file} $(basename ${nem_exe_file}).lnk
  172. cp ${xio_exe_file} .
  173. ln -s ${xio_exe_file} $(basename ${xio_exe_file}).lnk
  174. # ---------------------------------------------------------------------
  175. # *** Files needed for NEMO (linked)
  176. # ---------------------------------------------------------------------
  177. # Various initialisation and forcing files
  178. configure_opa
  179. # Write fake file for previous fresh water budget adjustment (nn_fwb==2 in namelist)
  180. echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  181. # XIOS files
  182. configure_xios
  183. else # i.e. $leg_is_restart == true
  184. # ---------------------------------------------------------------------
  185. # *** Remove all leftover output files from previous legs
  186. # ---------------------------------------------------------------------
  187. # NEMO output files
  188. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,SBC_scalar,scalar}.nc
  189. rm -f ${exp_name}_??_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,SBC_scalar,scalar}.nc
  190. fi # ! $leg_is_restart
  191. # -------------------------------------------------------------------------
  192. # *** Create some control files
  193. # -------------------------------------------------------------------------
  194. # NEMO and LIM namelists
  195. cp ${ctrl_file_dir}/namelist_ref .
  196. cp ${ctrl_file_dir}/namelist_ice_lim3_ref ./namelist_ice_ref
  197. cp ${ctrl_file_dir}/namelist_ice_lim3_cfg ./namelist_ice_cfg
  198. . ${ctrl_file_dir}/namelist_${nem_grid}_cfg.sh > namelist_cfg
  199. # -------------------------------------------------------------------------
  200. # *** Link the appropriate NEMO restart files of the previous leg
  201. # -------------------------------------------------------------------------
  202. if $leg_is_restart
  203. then
  204. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec - nem_restart_offset )))
  205. for (( n=0 ; n<nem_numproc ; n++ ))
  206. do
  207. np=$(printf %04d ${n})
  208. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  209. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  210. done
  211. fi
  212. # -------------------------------------------------------------------------
  213. # *** Start the run
  214. # -------------------------------------------------------------------------
  215. # Use the launch function from the platform configuration file
  216. t1=$(date +%s)
  217. launch \
  218. ${xio_numproc} ${xio_exe_file} -- \
  219. ${nem_numproc} ${nem_exe_file}
  220. t2=$(date +%s)
  221. tr=$(date -d "0 -$t1 sec + $t2 sec" +%T)
  222. # -------------------------------------------------------------------------
  223. # *** Check for signs of success
  224. # Note the tests provide no guarantee that things went fine! They are
  225. # just based on the IFS and NEMO log files. More tests (e.g. checking
  226. # restart files) could be implemented.
  227. # -------------------------------------------------------------------------
  228. # Check for NEMO success
  229. if [ -f ocean.output ]
  230. then
  231. if [ "$(awk '/New day/{d=$10}END{print d}' ocean.output)" == "$(date -d "${leg_end_date} - 1 day" +%Y/%m/%d)" ]
  232. then
  233. info "Leg successfully completed according to NEMO log file 'ocean.output'."
  234. else
  235. error "Leg not completed according to NEMO log file 'ocean.output'."
  236. fi
  237. else
  238. error "NEMO log file 'ocean.output' not found after run."
  239. fi
  240. # -------------------------------------------------------------------------
  241. # *** Move NEMO output files to archive directory
  242. # -------------------------------------------------------------------------
  243. outdir="output/nemo/$(printf %03d $((leg_number)))"
  244. mkdir -p ${outdir}
  245. for v in grid_U grid_V grid_W grid_T icemod SBC SBC_scalar scalar
  246. do
  247. for f in ${exp_name}_??_????????_????????_${v}.nc
  248. do
  249. test -f $f && mv $f $outdir/
  250. done
  251. for f in ${exp_name}_??_${v}.nc
  252. do
  253. test -f $f && mv $f $outdir/
  254. done
  255. done
  256. # -------------------------------------------------------------------------
  257. # *** Move NEMO restart files to archive directory
  258. # -------------------------------------------------------------------------
  259. if $leg_is_restart
  260. then
  261. outdir="restart/nemo/$(printf %03d $((leg_number)))"
  262. mkdir -p ${outdir}
  263. ls .
  264. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec - nem_restart_offset )))
  265. for f in oce ice
  266. do
  267. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  268. done
  269. echo "PEDRO"
  270. ls .
  271. fi
  272. # -------------------------------------------------------------------------
  273. # *** Move log files to archive directory
  274. # -------------------------------------------------------------------------
  275. outdir="log/$(printf %03d $((leg_number)))"
  276. mkdir -p ${outdir}
  277. for f in \
  278. ocean.output time.step solver.stat
  279. do
  280. test -f ${f} && mv ${f} ${outdir}
  281. done
  282. # -------------------------------------------------------------------------
  283. # *** Write the restart control file
  284. # -------------------------------------------------------------------------
  285. echo "#" | tee -a ${ece_info_file}
  286. echo "# Finished leg at `date '+%F %T'` after ${tr} (hh:mm:ss)" \
  287. | tee -a ${ece_info_file}
  288. echo "leg_number=${leg_number}" | tee -a ${ece_info_file}
  289. echo "leg_start_date=\"${leg_start_date}\"" | tee -a ${ece_info_file}
  290. echo "leg_end_date=\"${leg_end_date}\"" | tee -a ${ece_info_file}
  291. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  292. force_run_from_scratch=false
  293. done # loop over legs
  294. # -----------------------------------------------------------------------------
  295. # *** Platform dependent finalising of the run
  296. # -----------------------------------------------------------------------------
  297. finalise
  298. exit 0