skeleton_isfcpl.sh 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. # Function leap days calculates the number of leap days (29th of Februrary) in
  2. # a time intervall between two dates.
  3. #
  4. # Usage leap_days START_DATE END_DATE
  5. function leap_days()
  6. {
  7. local ld=0
  8. local frstYYYY=$(date -ud "$1" +%Y)
  9. local lastYYYY=$(date -ud "$2" +%Y)
  10. set +e
  11. # Check first year for leap day between start and end date
  12. $(date -ud "${frstYYYY}-02-29" > /dev/null 2>&1) \
  13. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  14. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  15. && (( ld++ ))
  16. # Check intermediate years for leap day
  17. for (( y=(( ${frstYYYY}+1 )); y<=(( ${lastYYYY}-1 )); y++ ))
  18. do
  19. $(date -ud "$y-02-29" > /dev/null 2>&1) && (( ld++ ))
  20. done
  21. # Check last year (if different from first year) for leap day between start
  22. # and end date
  23. (( $lastYYYY > $frstYYYY )) \
  24. && $(date -ud "${lastYYYY}-02-29" > /dev/null 2>&1) \
  25. && (( $(date -ud "$1" +%s) < $(date -ud "${frstYYYY}-03-01" +%s) )) \
  26. && (( $(date -ud "$2" +%s) > $(date -ud "${lastYYYY}-02-28" +%s) )) \
  27. && (( ld++ ))
  28. set -e
  29. echo "$ld"
  30. }
  31. # Function for loading modules
  32. function load_modules_isfcpl () {
  33. if [[ ${USER} == "vsc"????? ]]; then
  34. # quick and dirty patch - only BRENIAC will work while waiting for something to distinguish between both machines (include on computing nodes)
  35. # if [[ ${HOSTNAME} == "tier2"* ]]; then
  36. # if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  37. # module purge
  38. # module load Python
  39. # PYTHONPATH="${HOME}/.local/bin:${HOME}/.local/lib/python3.7/site-packages:\${PYTHONPATH}"
  40. # module load CDO
  41. # echo ${PYTHONPATH} > ${start_dir}/ppath
  42. # echo "genius and fetish detected" > ${start_dir}/tmpout
  43. # else
  44. # echo "On Genius, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  45. # exit 2
  46. # fi
  47. # elif [[ ${HOSTNAME} == "login"? ]]; then
  48. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  49. module purge
  50. module load Python/3.7.2-intel-2018a CDO/1.9.5-intel-2018a
  51. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/netCDF4" ] && pip install --user netCDF4
  52. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/numpy" ] && pip install --target=${VSC_HOME}/.local/lib/python3.7/site-packages numpy
  53. [ ! -d "${VSC_HOME}/.local/lib/python3.7/site-packages/gsw" ] && pip install --user gsw
  54. PYTHONPATH="${HOME}/.local/bin:${HOME}/.local/lib/python3.7/site-packages:\${PYTHONPATH}"
  55. echo ${PYTHONPATH} > ${start_dir}/ppath
  56. echo "breniac and fetish detected" > ${start_dir}/tmpout
  57. else
  58. echo "On Breniac, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  59. exit 2
  60. fi
  61. # else
  62. # echo "You seem to be on a VSC machine that is neither Genius nor BRENIAC. This has not been implemented yet. Exiting"
  63. # exit 2
  64. # fi
  65. elif [[ ${HOSTNAME} == "lm3"* ]]; then
  66. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  67. echo "lemaitre3 and fetish detected" > ${start_dir}/tmpout
  68. module purge
  69. module load releases/2018a CDO/1.8.2-intel-2018a Python/3.6.4-intel-2018a
  70. else
  71. echo "On lemaitre3, ice sheet models other than fETISh not implemented yet" > ${start_dir}/tmpout
  72. exit 2
  73. fi
  74. else
  75. echo "hostname undetected. not implemented yet." > ${start_dir}/tmpout
  76. exit 2
  77. fi
  78. }
  79. [[ $@ == *verbose* ]] && set -x
  80. module load ${module_list:?}
  81. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  82. mkdir -p ${run_dir}
  83. cd ${run_dir}
  84. # Prepare iscpl
  85. if [ "${isfcpl}" == "1" ]; then
  86. if [[ ${USER} == "vsc"????? ]]; then
  87. if [[ ${VSC_INSTITUTE_CLUSTER} == "breniac" ]]; then
  88. echo "BRENIAC and iscpl detected. OK."
  89. else
  90. echo "VSC clusters other than BRENIAC not supported for isfcpl."
  91. exit 2
  92. fi
  93. elif [[ ${HOSTNAME} == "lm3"* ]]; then
  94. echo "Lemaitre3 and iscpl detected. OK."
  95. else
  96. echo "You have asked for iscpl, but I have not recognized genius nor lemaitre3. Exiting."
  97. exit 2
  98. fi
  99. if [ "${name_icesheet_model}" != "fETISh" ]; then
  100. echo "only \"fETISh\" is known as an ice sheet model. exiting."
  101. exit 2
  102. fi
  103. length_cut_pref=$(( ${#exp_name} + 4 ))
  104. rebuild_nemo_exe=${nemo_src_dir}/TOOLS/REBUILD_NEMO/rebuild_nemo
  105. if [ -f ${start_dir}/isfcpl.track ]; then
  106. source ${start_dir}/isfcpl.track
  107. fi
  108. iscpl_nm=.true.
  109. if [[ ${xio_exe_file} == *"xios-2"* ]]; then
  110. is_xios2=1
  111. else
  112. is_xios2=0
  113. fi
  114. else
  115. iscpl_nm=.false.
  116. fi
  117. run_start_epoch=$(date -u -d"${run_start_date}" +%s)
  118. run_start_date_cos="${run_start_date//-}00"
  119. run_start_date=$(date -uR -d "${run_start_date}")
  120. run_end_date="${run_start_date} + ${run_duration:?}"
  121. run_end_date=$(date -uR -d "${run_end_date}")
  122. run_end_epoch=$(date -u -d"${run_end_date}" +%s)
  123. run_end_date_cos="$(date -u -d"${run_end_date}" +%Y%m%d%H)"
  124. if [[ ${special_rs_nemo} == "true" && ${special_restart} == "true" ]]; then
  125. echo "ERROR: both special_rs_nemo and special_restart are true. only can be true (at most). exit".
  126. exit 2
  127. fi
  128. for (( ; run_num_legs>0 ; run_num_legs-- ))
  129. do
  130. # Check for restart information file and set the current leg start date
  131. # Ignore restart information file if force_run_from_scratch is true
  132. if ! [ -r ${info_file} ]
  133. then
  134. leg_is_restart=false
  135. leg_start_date=${run_start_date}
  136. leg_start_date_cos=${run_start_date_cos}
  137. leg_number=1
  138. echo "leg_is_restart=false"
  139. else
  140. leg_is_restart=true
  141. . ./${info_file}
  142. leg_start_date=${leg_end_date}
  143. leg_start_date_cos="$(date -u -d"${leg_start_date}" +%Y%m%d%H)"
  144. leg_number=$((leg_number+1))
  145. echo "leg_is_restart=true"
  146. fi
  147. # Compute the end date of the current leg
  148. if [ -n "${rst_freq}" ]
  149. then
  150. if (($(date -u -d "${leg_start_date:?} + ${rst_freq}" +%s) > $(date -u -d "${leg_start_date:?} + ${run_duration}" +%s) )); then
  151. rst_freq=$run_duration
  152. fi
  153. leg_end_date=$(date -uR -d "${leg_start_date} + ${rst_freq}")
  154. leg_end_date_cos="$(date -u -d"${leg_end_date}" +%Y%m%d%H)"
  155. else
  156. leg_end_date=${run_end_date}
  157. leg_end_date_cos=${run_end_date_cos}
  158. fi
  159. if [ $(date -u -d "${leg_end_date}" +%s) -gt $(date -u -d "${run_end_date}" +%s) ]
  160. then
  161. leg_end_date=${run_end_date}
  162. leg_end_date_cos=${run_end_date_cos}
  163. fi
  164. # Some time variables needed later
  165. leg_start_epoch=$(date -u -d "${leg_start_date}" +%s)
  166. leg_end_epoch=$(date -u -d "${leg_start_date:?} + ${rst_freq:=$run_duration}" +%s)
  167. leg_length_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${leg_start_date}" +%s) ))
  168. leg_start_sec=$(( $(date -u -d "${leg_start_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  169. leg_end_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${run_start_date}" +%s) ))
  170. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)
  171. leg_start_date_yyyy=$(date -u -d "${leg_start_date}" +%Y)
  172. leg_end_date_yyyy=$(date -u -d "${leg_end_date}" +%Y)
  173. echo "leg_number: $leg_number"
  174. echo "leg_start_date: $leg_start_date"
  175. echo "leg_end_date: $leg_end_date"
  176. # Additional time variables for iscpl
  177. if (( isfcpl == 1 )); then
  178. leg_beforestart_epoch=$(( ${leg_start_epoch} - 43200 ))
  179. leg_beforestart_date=$(date -uR -d@"${leg_beforestart_epoch}")
  180. leg_almostend_epoch=$(( ${leg_end_epoch} - 43200 ))
  181. leg_almostend_date=$(date -uR -d@"${leg_almostend_epoch}")
  182. nextleg_start_epoch=$(( ${leg_end_epoch} + 43200 ))
  183. nextleg_start_date=$(date -uR -d@"${nextleg_start_epoch}")
  184. run_start_date_yyyymmdd=$(date -u -d "${run_start_date}" +%Y%m%d) # FIXME appears unused
  185. run_end_date_yyyymmdd=$(date -u -d "${run_end_date}" +%Y%m%d) # FIXME appears unused
  186. leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d) # FIXME appears unused
  187. leg_almostend_date_yyyymmdd=$(date -u -d "${leg_almostend_date}" +%Y%m%d) # FIXME appears unused
  188. nextleg_start_date_yyyymmdd=$(date -u -d "${nextleg_start_date}" +%Y%m%d) # FIXME appears unused
  189. leg_beforestart_date_yyyymmdd=$(date -u -d "${leg_beforestart_date}" +%Y%m%d) # FIXME appears unused
  190. fi
  191. # Check whether there's actually time left to simulate - exit otherwise
  192. if [ ${leg_length_sec} -le 0 ]
  193. then
  194. info "Leg start date equal to or after end of simulation."
  195. info "Nothing left to do. Exiting."
  196. exit 0
  197. fi
  198. # -------------------------------------------------------------------------
  199. # *** Prepare the run directory for a run from scratch
  200. # -------------------------------------------------------------------------
  201. if ! $leg_is_restart
  202. then
  203. # ---------------------------------------------------------------------
  204. # *** Check if run dir is empty. If not, and if we are allowed to do so
  205. # by ${force_run_from_scratch}, remove everything
  206. # ---------------------------------------------------------------------
  207. echo "pwd: $PWD"
  208. if $(ls * >& /dev/null)
  209. then
  210. if ${force_run_from_scratch}
  211. then
  212. rm -fr ${run_dir}/*
  213. else
  214. echo "error: run directory not empty and \$force_run_from_scratch not set."
  215. exit 0
  216. fi
  217. fi
  218. # ---------------------------------------------------------------------
  219. # *** Creates some directories
  220. # ---------------------------------------------------------------------
  221. mkdir -p ${out_dir}
  222. # ---------------------------------------------------------------------
  223. # *** Copy executables and coupling-related files
  224. # ---------------------------------------------------------------------
  225. cd ${start_dir}
  226. cp -u xios_config/*xml "${run_dir}"
  227. cp -u namelists/* "${run_dir}"
  228. cd ${run_dir}
  229. cp -u "${nem_exe_file:?}" .
  230. cp -u "${xio_exe_file:?}" .
  231. if (( isfcpl == 1 )) ; then
  232. cp -u ${icesheet_exe_file} .
  233. cp -u ${isfcpl_dir}/* .
  234. ln -sf ${start_dir}/isfcpl.track ${run_dir}
  235. mkdir -p ${out_dir}/isfcpl_tmp
  236. fi
  237. # ---------------------------------------------------------------------
  238. # *** Link ic, forcing and shared files for NEMO
  239. # ---------------------------------------------------------------------
  240. [[ ! -f EMPave_old.dat ]] && echo " 0 0.0000000000000000E+00 0.0000000000000000E+00" > EMPave_old.dat
  241. for file in "${ic_files[@]}"; do
  242. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${ic_subdir}/${nem_grid}/$file")
  243. done
  244. for file in "${forcing_files[@]}"; do
  245. [[ ! -e ${file#*> } || "$file" == \** ]] && ln -sf $(sed 's/ *=> */ /' <<< "${ini_data_dir}/${forcing_subdir}/${nem_forcing_set}/$file")
  246. done
  247. for file in "${shared_files[@]}"; do
  248. [[ ! -e ${file#*> } ]] && ln -sf $(sed 's/ *=> */ /' <<< "${shared_dir}/$file")
  249. done
  250. # ---------------------------------------------------------------------
  251. # *** Prepare NEMO - ice shelf coupling
  252. # ---------------------------------------------------------------------
  253. if [ "${isfcpl}" == "1" ]; then
  254. if (( leg_number == 1 )); then
  255. mkdir -p ${archive_dir}/isfcpl
  256. mkdir -p ${archive_dir}/${name_icesheet_model}
  257. rm -f ${start_dir}/isfcpl.track
  258. isfcpl_window_days=$(echo "(${n_res_perisfcpl} * ${leg_length_sec}) / 86400" | bc)
  259. isfcpl_d30=$(echo "${isfcpl_window_days} / 30" | bc -l)
  260. n_month_isfcpl=$(echo ${isfcpl_d30} | awk '{print int($1+0.5)}')
  261. n_month_full_run=$(( $(( 12 * $(( ${run_end_date_yyyymmdd::4} - ${run_start_date_yyyymmdd::4} )) )) + $(( ${run_end_date_yyyymmdd:4:2} - ${run_start_date_yyyymmdd:4:2} )) ))
  262. if (( $(( ${n_month_full_run} % ${n_month_isfcpl} )) != 0 )); then
  263. echo "n_month_full_run=${n_month_full_run} is not a multiple of n_month_isfcpl=${n_month_isfcpl}."
  264. exit 2
  265. else
  266. n_isfcpl_window=$(echo "${n_month_full_run} / ${n_month_isfcpl}" | bc)
  267. fi
  268. tmp=`readlink -f isf_draft_meter.nc`
  269. tmp2=`readlink -f bathy_meter.nc`
  270. if [ "${tmp}" != "${tmp2}" ]; then
  271. echo "Error. At initialization, isf_draft_meter.nc and bathy_meter.nc do not point to the same file."
  272. exit 1
  273. fi
  274. init_bathy_file=`basename ${tmp}`
  275. cp -f ${tmp} ${archive_dir}/isfcpl
  276. cp -f ${tmp} ${run_dir}/init_NEMO_bathy.nc
  277. ln -sf ${icesheet_files_dir}/* ./
  278. ln -sf ${isfcpl_files_dir}/* ./
  279. date_beg_isfcpl="${run_start_date_yyyymmdd}"
  280. printf "# Information for the ice sheet model\n" > ${start_dir}/isfcpl.track
  281. printf "run_name=${exp_name}\n" >> ${start_dir}/isfcpl.track
  282. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  283. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/isfcpl.track
  284. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/isfcpl.track
  285. printf "date_beg_isfcpl=${date_beg_isfcpl}\n" >> ${start_dir}/isfcpl.track
  286. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  287. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" >> ${start_dir}/isfcpl.track
  288. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  289. printf "init_bathy=1\n" >> ${start_dir}/isfcpl.track
  290. printf "curr_bathy_file=${init_bathy_file}\n" >> ${start_dir}/isfcpl.track
  291. init_bathy=1
  292. curr_bathy_file=${init_bathy_file}
  293. fi
  294. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  295. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  296. fi # if [ "${iscpl}" == 1 ]
  297. # ---------------------------------------------------------------------
  298. # *** Copy potential special restarts
  299. # ---------------------------------------------------------------------
  300. if $special_restart
  301. then
  302. ### Getting the right output folders
  303. leg_start_date_tmp=$leg_start_date
  304. leg_end_date_tmp=$leg_end_date
  305. leg_number_tmp=$leg_number
  306. rsync -avz ${run_dir}/../../${special_restart_from}/rundir/${info_file} ${run_dir}
  307. special_date=$(date -uR -d "${special_restart_date}")
  308. sed -i "/$special_date/q" ${run_dir}/${info_file}
  309. . ${run_dir}/${info_file}
  310. special_restart_leg=$(printf %03d $((leg_number)))
  311. echo "special_date: $special_date"
  312. echo "special_restart_leg: $special_restart_leg"
  313. echo "special_restart_from: $special_restart_from"
  314. ### NEMO
  315. indir="${run_dir}/../../${special_restart_from}/archive/restart/nemo/$(printf %03d $((special_restart_leg)))"
  316. cd ${indir}
  317. for f in *.nc; do
  318. nf=`echo "${f/$special_restart_from/$exp_name}"`
  319. echo "copy $f to ${run_dir}/$nf"
  320. cp $f ${run_dir}/$nf
  321. done
  322. cd -
  323. cd ${run_dir}
  324. echo "pwd: $PWD"
  325. for f in ${exp_name}_????????_restart_???_????.nc; do
  326. nf=`echo ${f: -19}`
  327. echo "linking $f to ${run_dir}/$nf"
  328. ln -s $f $nf
  329. done
  330. cd ${run_dir}
  331. rm -rf ${info_file}
  332. leg_start_date=$leg_start_date_tmp
  333. leg_end_date=$leg_end_date_tmp
  334. leg_number=$leg_number_tmp
  335. elif ${special_rs_nemo}
  336. then
  337. dir_rs=`dirname ${special_rs_nemo_stencil}`
  338. base_rs=`basename ${special_rs_nemo_stencil}`
  339. n_oce_rs=`find ${dir_rs} -name "${base_rs}oce_????.nc" | wc -l`
  340. if(( n_oce_rs != nem_numproc )); then
  341. echo "in special_rs_nemo, not the right amount of ocean restart files."
  342. echo "${n_oce_rs} instead of ${nem_numproc}. exiting."
  343. exit 2
  344. fi
  345. n_ice_rs=`find ${dir_rs} -name "${base_rs}ice_????.nc" | wc -l`
  346. if(( n_ice_rs != nem_numproc )); then
  347. echo "in special_rs_nemo, not the right amount of ice restart files."
  348. echo "${n_ice_rs} instead of ${nem_numproc}. exiting."
  349. exit 2
  350. fi
  351. for (( n=0 ; n<nem_numproc ; n++ ))
  352. do
  353. np=$(printf %04d ${n})
  354. ln -fs ${special_rs_nemo_stencil}oce_${np}.nc restart_oce_${np}.nc
  355. ln -fs ${special_rs_nemo_stencil}ice_${np}.nc restart_ice_${np}.nc
  356. done
  357. fi # if $special_restart
  358. else # if ! $leg_is_restart
  359. # ---------------------------------------------------------------------
  360. # *** Creates some directories
  361. # ---------------------------------------------------------------------
  362. mkdir -p ${out_dir}
  363. if [ "${isfcpl}" == "1" ]; then
  364. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  365. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  366. fi
  367. # ---------------------------------------------------------------------
  368. # *** Copy restart files in run directory
  369. # ---------------------------------------------------------------------
  370. cd ${run_dir}
  371. ### NEMO
  372. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number-1)))"
  373. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  374. for f in oce ice
  375. do
  376. echo "${outdir}/${exp_name}_${ns}_restart_${f}_????.nc"
  377. ln -sf ${outdir}/${exp_name}_${ns}_restart_${f}_????.nc ${run_dir}
  378. done
  379. for (( n=0 ; n<nem_numproc ; n++ ))
  380. do
  381. np=$(printf %04d ${n})
  382. ln -fs ${exp_name}_${ns}_restart_oce_${np}.nc restart_oce_${np}.nc
  383. ln -fs ${exp_name}_${ns}_restart_ice_${np}.nc restart_ice_${np}.nc
  384. done
  385. fi # ! $leg_is_restart
  386. # -------------------------------------------------------------------------
  387. # *** Update ocean namelist
  388. # -------------------------------------------------------------------------
  389. source build_namelist_cfg.sh > namelist_cfg
  390. # -------------------------------------------------------------------------
  391. # *** Build ice sheet launch
  392. # -------------------------------------------------------------------------
  393. if (( isfcpl == 1)); then
  394. source ${icesheet_launch_template} > launch_icesheet.sh
  395. chmod +x launch_icesheet.sh
  396. fi
  397. # -------------------------------------------------------------------------
  398. # *** Launch experiment
  399. # -------------------------------------------------------------------------
  400. echo "START of mpirun"
  401. time_begin=$(date +%s)
  402. mpirun -np "${xio_numproc:?}" "${xio_exe_file:?}" : -np "${nem_numproc:?}" "${nem_exe_file:?}"
  403. time_end=$(date +%s)
  404. echo "END of: mpirun"
  405. # -------------------------------------------------------------------------
  406. # *** Ice sheet coupling
  407. # -------------------------------------------------------------------------
  408. if (( isfcpl == 1 )); then
  409. cnt_res_isfcpl=$(( $(( ${leg_number} - 1 )) % ${n_res_perisfcpl} ))
  410. fmt_res_isfcpl=$(printf "%02d" ${cnt_res_isfcpl})
  411. in_fwfisf_stencil="${exp_name}_${frequency_fwfisf}_${leg_start_date_yyyymmdd}_${leg_almostend_date_yyyymmdd}_${suffix_fwfisf}"
  412. out_fwfisf_stencil="${exp_name}_fwfisf_res"
  413. out_fwfisf_curr="${out_fwfisf_stencil}${fmt_res_isfcpl}"
  414. if (( is_xios2 == 0 )); then
  415. module purge
  416. module load ${module_list:?}
  417. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  418. /bin/bash ${rebuild_nemo_exe} ${in_fwfisf_stencil} ${xio_numproc}
  419. module purge
  420. load_modules_isfcpl
  421. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  422. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  423. rm -f ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  424. else
  425. cdo selname,${name_var_fwfisf} ${in_fwfisf_stencil}.nc tmp_fwfisf.nc
  426. cdo monmean tmp_fwfisf.nc ${out_fwfisf_curr}.nc
  427. rm -f tmp_fwfisf.nc
  428. fi
  429. mv -f ${out_fwfisf_curr}.nc ${out_dir}/isfcpl_tmp/
  430. if (( ${leg_number} % ${n_res_perisfcpl} == 0 )); then
  431. date_end_isfcpl=${leg_almostend_date_yyyymmdd}
  432. sed -i "s/.*date_end_isfcpl=.*/date_end_isfcpl=${date_end_isfcpl}/" ${start_dir}/isfcpl.track
  433. module purge
  434. module load ${module_list:?}
  435. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  436. /bin/bash ${rebuild_nemo_exe} mesh_mask ${nem_numproc}
  437. module purge
  438. load_modules_isfcpl
  439. infile_ocean2ice="${exp_name}_ocean2ice_raw_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  440. outfile_ocean2ice="${exp_name}_ocean2ice_${date_beg_isfcpl}-${date_end_isfcpl}.nc"
  441. fwfisf_records=""
  442. for((rs2=0; rs2<n_res_perisfcpl; rs2++)); do
  443. fmt_rs2=$(printf "%02d" ${rs2})
  444. curr_file="${out_dir}/isfcpl_tmp/${out_fwfisf_stencil}${fmt_rs2}.nc"
  445. if [ -f ${curr_file} ] ; then
  446. fwfisf_records="${fwfisf_records} ${curr_file}"
  447. else
  448. echo "${curr_file} is not in ${out_dir}/isfcpl_tmp. It is needed for NEMO - ice sheet coupling. Exiting"
  449. exit 2
  450. fi
  451. done
  452. cdo -O copy ${fwfisf_records} ${infile_ocean2ice}
  453. rm -f ${fwfisf_records}
  454. source build_pp_nemo_to_icesheet.sh > ./pp_nemo_to_${name_icesheet_model}.py
  455. python pp_nemo_to_${name_icesheet_model}.py
  456. mv -f ${infile_ocean2ice} ${archive_dir}/isfcpl/
  457. mv -f mesh_mask.nc ${archive_dir}/isfcpl/${exp_name}_mesh_mask_${date_beg_isfcpl}-${date_end_isfcpl}.nc
  458. module purge
  459. module load ${icesheet_modules}
  460. if [ "${icesheet_library_path}" != "" ]; then
  461. export LD_LIBRARY_PATH="${icesheet_library_path}"
  462. fi
  463. /bin/bash ./launch_icesheet.sh
  464. mv -f ${outfile_ocean2ice} ${archive_dir}/isfcpl
  465. if (( ice_atm_cpl == 1 )); then
  466. mv -f ${outfile_atm2ice} ${archive_dir}/isfcpl
  467. fi
  468. if [[ "${name_icesheet_model}" == "fETISh" ]]; then
  469. find . -type l -name "${exp_name}_${name_icesheet_model}Run_??????_toto.mat" -exec rm -f {} \;
  470. mv -f ${exp_name}_${name_icesheet_model}Run_??????_toto.mat ${archive_dir}/${name_icesheet_model}
  471. ln -sf ${archive_dir}/${name_icesheet_model}/${exp_name}_${name_icesheet_model}Run_${date_end_isfcpl::-2}_toto.mat
  472. else
  473. # GVAN EDIT HERE; DO THE SAME FOR BISICLES OUTPUT NOMENCLATURE
  474. echo "Ice-sheet models other than fETISh not implemented yet."
  475. exit 2
  476. fi
  477. curr_geom_file="${exp_name}_ice2ocean_${date_end_isfcpl}.nc"
  478. new_bathy_file="${exp_name}_bathymetry_isfdraft_${date_end_isfcpl}.nc"
  479. source build_pp_icesheet_to_nemo.sh > pp_${name_icesheet_model}_to_nemo.py
  480. if [ "${init_bathy}" == 0 ]; then
  481. mv -f ${curr_bathy_file} ${archive_dir}/isfcpl
  482. else
  483. init_bathy=0
  484. cp -f ${curr_bathy_file} ${archive_dir}/isfcpl
  485. fi
  486. module purge
  487. load_modules_isfcpl
  488. python pp_${name_icesheet_model}_to_nemo.py
  489. mv -f ${curr_geom_file} ${archive_dir}/isfcpl
  490. curr_bathy_file=${new_bathy_file}
  491. ln -sf ${curr_bathy_file} ${run_dir}/bathy_meter.nc
  492. ln -sf ${curr_bathy_file} ${run_dir}/isf_draft_meter.nc
  493. printf "# Information for the ice sheet model\n" > ${start_dir}/isfcpl.track
  494. printf "run_name=${exp_name}\n" >> ${start_dir}/isfcpl.track
  495. printf "initial_date=${run_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  496. printf "n_month_isfcpl=${n_month_isfcpl}\n" >> ${start_dir}/isfcpl.track
  497. printf "n_isfcpl_window=${n_isfcpl_window}\n" >> ${start_dir}/isfcpl.track
  498. printf "date_beg_isfcpl=${nextleg_start_date_yyyymmdd}\n" >> ${start_dir}/isfcpl.track
  499. printf "date_end_isfcpl=YYYYMMDD\n" >> ${start_dir}/isfcpl.track
  500. printf "ice_atm_cpl=${ice_atm_cpl}\n\n" >> ${start_dir}/isfcpl.track
  501. printf "# Information for NEMO to keep up\n" >> ${start_dir}/isfcpl.track
  502. printf "init_bathy=${init_bathy}\n" >> ${start_dir}/isfcpl.track
  503. printf "curr_bathy_file=${curr_bathy_file}\n" >> ${start_dir}/isfcpl.track
  504. module purge
  505. module load ${module_list:?}
  506. export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}"${extralibs_list}"
  507. fi
  508. fi
  509. # -------------------------------------------------------------------------
  510. # *** Move output files to archive directory
  511. # -------------------------------------------------------------------------
  512. ### NEMO
  513. outdir="${archive_dir}/output/nemo/$(printf %03d $((leg_number)))"
  514. mkdir -p ${outdir}
  515. for v in grid_U grid_V grid_W grid_T icemod SBC scalar SBC_scalar diad_T \
  516. grid_T_2D grid_U_2D grid_V_2D grid_W_2D grid_T_3D grid_U_3D grid_V_3D grid_W_3D \
  517. grid_1point grid_T_3D_ncatice vert_sum \
  518. grid_ptr_W_3basin_3D grid_ptr_T_3basin_2D grid_ptr_T_2D \
  519. zoom_700_sum zoom_300_sum zoom_2000_sum
  520. do
  521. for f in ${exp_name}_*_????????_????????_*${v}*.nc
  522. do
  523. test -f $f && mv $f $outdir/
  524. done
  525. done
  526. # -------------------------------------------------------------------------
  527. # *** Move restart files to archive directory
  528. # -------------------------------------------------------------------------
  529. ### NEMO
  530. outdir="${archive_dir}/restart/nemo/$(printf %03d $((leg_number)))"
  531. mkdir -p ${outdir}
  532. ns=$(printf %08d $(( leg_end_sec / nem_time_step_sec )))
  533. for f in oce ice
  534. do
  535. mv ${exp_name}_${ns}_restart_${f}_????.nc ${outdir}
  536. done
  537. # -------------------------------------------------------------------------
  538. # *** Move log files to archive directory
  539. # -------------------------------------------------------------------------
  540. outdir="${archive_dir}/log/$(printf %03d $((leg_number)))"
  541. mkdir -p ${outdir}
  542. for f in \
  543. ocean.output time.step solver.stat
  544. do
  545. test -f ${f} && mv ${f} ${outdir}
  546. done
  547. # ---------------------------------------------------------------------
  548. # *** Remove all leftover output files from previous legs
  549. # ---------------------------------------------------------------------
  550. ### NEMO files
  551. rm -f ${exp_name}_??_????????_????????_{grid_U,grid_V,grid_W,grid_T,icemod,SBC,scalar,SBC_scalar}.nc
  552. rm -f ${exp_name}_*restart_*nc
  553. ns=$(printf %08d $(( leg_start_sec / nem_time_step_sec )))
  554. for (( n=0 ; n<nem_numproc ; n++ ))
  555. do
  556. np=$(printf %04d ${n})
  557. rm -f restart_oce_${np}.nc
  558. rm -f restart_ice_${np}.nc
  559. done
  560. # -------------------------------------------------------------------------
  561. # *** Write the restart control file
  562. # -------------------------------------------------------------------------
  563. shopt -u nullglob
  564. tr=$(date -d "0 -$time_begin sec + $time_end sec" +%T)
  565. current_date=$(date +'%F %T')
  566. {
  567. echo "#"
  568. echo "# Finished leg at ${current_date} after ${tr} (hh:mm:ss)"
  569. echo "leg_number=${leg_number}"
  570. echo "leg_start_date=\"${leg_start_date}\""
  571. echo "leg_end_date=\"${leg_end_date}\""
  572. } | tee -a "${info_file}"
  573. # Need to reset force_run_from_scratch in order to avoid destroying the next leg
  574. force_run_from_scratch=false
  575. special_restart=false
  576. done # Loop over legs
  577. cd ${start_dir}
  578. cd - >/dev/null
  579. [[ $@ == *noresubmit* ]] && exit 0
  580. if (( leg_end_epoch < run_end_epoch )) ; then
  581. echo "Leg end earlier than end of simulation."
  582. echo "Submitting another job."
  583. if [[ "$@" == *"local"* ]] ; then
  584. exec "$0" "$@"
  585. elif [ "qsub" == *sbatch* ] ; then
  586. sbatch $0 $@ | awk '{print $4}' >> ${run_dir}/.coral_jobs
  587. elif [[ $USER == "vsc"* ]]; then
  588. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/credits_file ]; then
  589. credits=`cat ${PBS_O_WORKDIR-$PWD}/tmp/credits_file`
  590. else
  591. credits_tmp=`grep "account string:" ${PBS_O_WORKDIR-$PWD}/*.o* -R | sed 's/^.*: //'`
  592. credits=`echo $credits_tmp | awk '{ print $1 }'`
  593. cd ${start_dir}
  594. mkdir -p tmp
  595. echo ${credits} > tmp/credits_file
  596. fi
  597. if [ -f ${PBS_O_WORKDIR-$PWD}/tmp/script_name ]; then
  598. script_name=`cat ${PBS_O_WORKDIR-$PWD}/tmp/script_name`
  599. else
  600. script_name="run.sh"
  601. fi
  602. ssh login2 "cd $start_dir; qsub -A $credits $script_name | tee -a coral_jobs"
  603. sleep 2
  604. jobid=`cat coral_jobs`
  605. rm -f coral_jobs
  606. jobid=${jobid%%.*}
  607. echo "${jobid}" >> "${run_dir}"/.coral_jobs ;
  608. else
  609. "qsub" -v PBS_OPTIONS="$@" "$0" | tee -a coral_jobs
  610. sleep 2
  611. jobid=`cat coral_jobs`
  612. rm -f coral_jobs
  613. jobid=${jobid%.*}
  614. echo "${jobid}" >> "${run_dir}"/.coral_jobs
  615. fi
  616. else
  617. if [[ $USER == "vsc"* ]]; then
  618. rm -rf ${start_dir}/tmp
  619. fi
  620. rm -rf ${run_dir}/../cclm_out_tmp
  621. echo "Nothing left to do. Exiting."
  622. cat << "EOF"
  623. _____ _ _ _ _ _ _ _
  624. / ____(_) | | | | (_) | | | | | |
  625. | (___ _ _ __ ___ _ _| | __ _| |_ _ ___ _ __ ___ ___ _ __ ___ _ __ | | ___| |_ ___ __| |
  626. \___ \| | '_ ` _ \| | | | |/ _` | __| |/ _ \| '_ \ / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __/ _ \/ _` |
  627. ____) | | | | | | | |_| | | (_| | |_| | (_) | | | | | (_| (_) | | | | | | |_) | | __/ || __/ (_| |
  628. |_____/|_|_| |_| |_|\__,_|_|\__,_|\__|_|\___/|_| |_| \___\___/|_| |_| |_| .__/|_|\___|\__\___|\__,_|
  629. | |
  630. |_|
  631. EOF
  632. fi
  633. exit 0