| 1 | #!/bin/bash |
|---|
| 2 | ######################################################################## |
|---|
| 3 | ######## Library of bash functions for the PEM workflow script ######### |
|---|
| 4 | ######################################################################## |
|---|
| 5 | |
|---|
| 6 | # To end the workflow script |
|---|
| 7 | end_workflow() { |
|---|
| 8 | # Restore the previous value of LC_NUMERIC |
|---|
| 9 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 10 | date |
|---|
| 11 | echo "Success: the PEM workflow script completed normally!" |
|---|
| 12 | exit 0 |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | # To abort the workflow script with error |
|---|
| 16 | abort_workflow() { |
|---|
| 17 | # Restore the previous value of LC_NUMERIC |
|---|
| 18 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 19 | |
|---|
| 20 | date |
|---|
| 21 | echo "Error: an issue occured in the PEM workflow script!" |
|---|
| 22 | exit 1 |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | # To check what is the job scheduler |
|---|
| 26 | function detect_scheduler() { |
|---|
| 27 | if command -v squeue &> /dev/null; then |
|---|
| 28 | echo "SLURM is installed on $machine." |
|---|
| 29 | job_scheduler="SLURM" |
|---|
| 30 | job_name="#SBATCH --job-name=" |
|---|
| 31 | kill_job="scancel" |
|---|
| 32 | elif command -v qstat &> /dev/null; then |
|---|
| 33 | echo "PBS/TORQUE is installed on $machine." |
|---|
| 34 | job_scheduler="PBS" |
|---|
| 35 | job_name="#PBS -N " |
|---|
| 36 | kill_job="qdel" |
|---|
| 37 | else |
|---|
| 38 | echo "Error: neither SLURM nor TORQUE/PBS is installed on $machine!" |
|---|
| 39 | echo "You need to adapt the script to your job job_scheduler or set 'mode' to 0." |
|---|
| 40 | abort_workflow |
|---|
| 41 | fi |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | # To get the number of slopes for the simulation |
|---|
| 45 | get_nslope() { |
|---|
| 46 | ns=1 |
|---|
| 47 | if [ -f "startfi.nc" ]; then |
|---|
| 48 | ns=$(ncdump -h startfi.nc | sed -n 's/.*nslope = \([0-9]*\) ;.*/\1/p') |
|---|
| 49 | else |
|---|
| 50 | for f in run_pcm.def callphys.def; do |
|---|
| 51 | if [[ -f "$f" ]]; then |
|---|
| 52 | while IFS= read -r line; do |
|---|
| 53 | # Remove leading whitespace |
|---|
| 54 | trimmed=$(echo "$line" | sed 's/^[[:space:]]*//') |
|---|
| 55 | # Skip lines that are commented out |
|---|
| 56 | if [[ "$trimmed" == \#* ]]; then |
|---|
| 57 | continue |
|---|
| 58 | fi |
|---|
| 59 | # Check if line contains 'nslope = N' |
|---|
| 60 | if [[ "$trimmed" =~ ^nslope[[:space:]]*=[[:space:]]*([0-9]+) ]]; then |
|---|
| 61 | ns="${BASH_REMATCH[1]}" |
|---|
| 62 | break |
|---|
| 63 | fi |
|---|
| 64 | done < "$f" |
|---|
| 65 | [[ -n "$ns" ]] && break |
|---|
| 66 | fi |
|---|
| 67 | done |
|---|
| 68 | fi |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | # To modify the xml file according nslope |
|---|
| 72 | config_xios_outputs() { |
|---|
| 73 | tmp="tmp_file_def.xml" |
|---|
| 74 | in_outdaily4pem=false |
|---|
| 75 | in_outyearly4pem=false |
|---|
| 76 | in_outdaily4pem_s=false |
|---|
| 77 | in_outyearly4pem_s=false |
|---|
| 78 | |
|---|
| 79 | sed -i 's/enabled="\.true\.\">/enabled=".false.">/g' file_def_physics_mars.xml |
|---|
| 80 | |
|---|
| 81 | while IFS= read -r line; do |
|---|
| 82 | # Detect file blocks |
|---|
| 83 | case "$line" in |
|---|
| 84 | *'<file id="outdaily4pem"'*) |
|---|
| 85 | in_outdaily4pem=true |
|---|
| 86 | ;; |
|---|
| 87 | *'<file id="outyearly4pem"'*) |
|---|
| 88 | in_outyearly4pem=true |
|---|
| 89 | ;; |
|---|
| 90 | *'<file id="outdaily4pem_s"'*) |
|---|
| 91 | in_outdaily4pem_s=true |
|---|
| 92 | ;; |
|---|
| 93 | *'<file id="outyearly4pem_s"'*) |
|---|
| 94 | in_outyearly4pem_s=true |
|---|
| 95 | ;; |
|---|
| 96 | esac |
|---|
| 97 | |
|---|
| 98 | # Handle enabled attribute |
|---|
| 99 | if [[ $line == *'enabled="'* ]]; then |
|---|
| 100 | if $in_outdaily4pem || $in_outyearly4pem; then |
|---|
| 101 | if [[ $ns -eq 1 ]]; then |
|---|
| 102 | line=' enabled=".true.">' |
|---|
| 103 | else |
|---|
| 104 | line=' enabled=".false.">' |
|---|
| 105 | fi |
|---|
| 106 | elif $in_outdaily4pem_s || $in_outyearly4pem_s; then |
|---|
| 107 | if [[ $ns -eq 1 ]]; then |
|---|
| 108 | line=' enabled=".false.">' |
|---|
| 109 | else |
|---|
| 110 | line=' enabled=".true.">' |
|---|
| 111 | fi |
|---|
| 112 | fi |
|---|
| 113 | fi |
|---|
| 114 | |
|---|
| 115 | # Handle slope variables |
|---|
| 116 | if ( $in_outdaily4pem_s || $in_outyearly4pem_s ) && [[ $line =~ slope([0-9]+) ]]; then |
|---|
| 117 | slope_id="${BASH_REMATCH[1]}" |
|---|
| 118 | if (( 10#$slope_id > ns )); then |
|---|
| 119 | # Ensure the line is commented |
|---|
| 120 | if [[ $line != "<!--"* ]]; then |
|---|
| 121 | line="<!-- $line -->" |
|---|
| 122 | fi |
|---|
| 123 | else |
|---|
| 124 | # Ensure the line is uncommented |
|---|
| 125 | if [[ $line == "<!--"* ]]; then |
|---|
| 126 | line="${line#<!-- }" # remove leading <!-- |
|---|
| 127 | line="${line% -->}" # remove trailing --> |
|---|
| 128 | fi |
|---|
| 129 | fi |
|---|
| 130 | fi |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | # Leaving the file block |
|---|
| 134 | case "$line" in |
|---|
| 135 | *'</file>'*) |
|---|
| 136 | in_outdaily4pem=false |
|---|
| 137 | in_outyearly4pem=false |
|---|
| 138 | in_outdaily4pem_s=false |
|---|
| 139 | in_outyearly4pem_s=false |
|---|
| 140 | ;; |
|---|
| 141 | esac |
|---|
| 142 | |
|---|
| 143 | echo "$line" >> "$tmp" |
|---|
| 144 | done < file_def_physics_mars.xml |
|---|
| 145 | |
|---|
| 146 | mv "$tmp" file_def_physics_mars.xml |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | # To check if a PCM run is one year |
|---|
| 150 | check_run_yearly() { |
|---|
| 151 | if [ -f "startfi.nc" ]; then |
|---|
| 152 | year_sol=$(ncdump -v controle startfi.nc 2>/dev/null | \ |
|---|
| 153 | sed -n '/controle =/,/;/p' | tr -d '[:space:]' | \ |
|---|
| 154 | sed 's/.*=//; s/;//' | tr ',' '\n' | sed -n '14p') |
|---|
| 155 | else |
|---|
| 156 | echo "Warning: no \"startfi.nc\" found! So default year_sol=669 (Mars year) is taken..." |
|---|
| 157 | year_sol=669 # Length of Martian year (sols) |
|---|
| 158 | fi |
|---|
| 159 | sol_in_file=$(awk -F'=' '/^[[:space:]]*(nday|ndt)[[:space:]]*=/ { |
|---|
| 160 | val=$2 |
|---|
| 161 | gsub(/^[[:space:]]+|[[:space:]]+$/,"",val) |
|---|
| 162 | print val |
|---|
| 163 | exit |
|---|
| 164 | }' run_pcm.def) |
|---|
| 165 | |
|---|
| 166 | if [ -z "$sol_in_file" ]; then |
|---|
| 167 | echo "Error: no length of year found in \"run_pcm.def\"!" |
|---|
| 168 | abort_workflow |
|---|
| 169 | elif [ "$sol_in_file" -eq "$year_sol" ]; then |
|---|
| 170 | # Good: we do nothing |
|---|
| 171 | : |
|---|
| 172 | else |
|---|
| 173 | echo "Error: length of year mismatch between \"run_pcm.def\" ($sol_in_file) and \"startfi.nc\" ($year_sol)!" |
|---|
| 174 | abort_workflow |
|---|
| 175 | fi |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | # To check if everything necessary for the workflow script is ok |
|---|
| 179 | check_workflow() { |
|---|
| 180 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 181 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 182 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 183 | |
|---|
| 184 | if [ -v n_planetary_years ] && [ ! -z "$n_planetary_years" ]; then |
|---|
| 185 | if [ $(echo "$n_planetary_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 186 | echo "Error: 'n_planetary_years' must be > 0!" |
|---|
| 187 | abort_workflow |
|---|
| 188 | fi |
|---|
| 189 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 190 | if [ $(echo "$n_earth_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 191 | echo "Error: 'n_earth_years' must be > 0!" |
|---|
| 192 | abort_workflow |
|---|
| 193 | fi |
|---|
| 194 | else |
|---|
| 195 | echo "Error: the number of years to be simulated is not set!" |
|---|
| 196 | abort_workflow |
|---|
| 197 | fi |
|---|
| 198 | if [ $n_pcm_runs_ini -lt 2 ] || [ -z "$n_pcm_runs_ini" ]; then |
|---|
| 199 | echo "Error: 'n_pcm_runs_ini' must be >= 2!" |
|---|
| 200 | abort_workflow |
|---|
| 201 | fi |
|---|
| 202 | if [ $n_pcm_runs -lt 2 ] || [ -z "$n_pcm_runs" ]; then |
|---|
| 203 | echo "Error: 'n_pcm_runs' must be >= 2!" |
|---|
| 204 | abort_workflow |
|---|
| 205 | fi |
|---|
| 206 | if [ ! -f "pcm_run.job" ]; then |
|---|
| 207 | echo "Error: file \"pcm_run.job\" does not exist in $dir!" |
|---|
| 208 | abort_workflow |
|---|
| 209 | fi |
|---|
| 210 | if [ ! -f "pem_run.job" ]; then |
|---|
| 211 | echo "Error: file \"pem_run.job\" does not exist in $dir!" |
|---|
| 212 | abort_workflow |
|---|
| 213 | fi |
|---|
| 214 | if [ ! -f "run_pcm.def" ]; then |
|---|
| 215 | echo "Error: file \"run_pcm.def\" does not exist in $dir!" |
|---|
| 216 | abort_workflow |
|---|
| 217 | fi |
|---|
| 218 | if [ ! -f "run_pem.def" ]; then |
|---|
| 219 | echo "Error: file \"run_pem.def\" does not exist in $dir!" |
|---|
| 220 | abort_workflow |
|---|
| 221 | fi |
|---|
| 222 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 223 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 224 | abort_workflow |
|---|
| 225 | fi |
|---|
| 226 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 227 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 228 | abort_workflow |
|---|
| 229 | fi |
|---|
| 230 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 231 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 232 | abort_workflow |
|---|
| 233 | fi |
|---|
| 234 | if [ ! -f "iodef.xml" ]; then |
|---|
| 235 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 236 | abort_workflow |
|---|
| 237 | fi |
|---|
| 238 | if [ ! -d "logs" ]; then |
|---|
| 239 | mkdir logs |
|---|
| 240 | fi |
|---|
| 241 | if [ ! -d "starts" ]; then |
|---|
| 242 | mkdir starts |
|---|
| 243 | fi |
|---|
| 244 | if [ ! -d "diags" ]; then |
|---|
| 245 | mkdir diags |
|---|
| 246 | fi |
|---|
| 247 | if [ $exec_mode -ne 0 ]; then |
|---|
| 248 | detect_scheduler |
|---|
| 249 | fi |
|---|
| 250 | # Set automatically the XIOS output file for the PEM according to the number of slopes |
|---|
| 251 | get_nslope |
|---|
| 252 | config_xios_outputs |
|---|
| 253 | # Check if a PCM run is one year |
|---|
| 254 | check_run_yearly |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | # To convert Earth years into Mars years |
|---|
| 258 | convert_earth2plnt_years() { |
|---|
| 259 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 260 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 261 | r_plnt2earth_yr=$(echo "$myear/$eyear" | bc -l) |
|---|
| 262 | r_plnt2earth_yr=$(printf "%.4f" $r_plnt2earth_yr) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 263 | if [ -v n_planetary_years ]; then |
|---|
| 264 | ntot_yr_sim=$n_planetary_years |
|---|
| 265 | echo "Number of years to be simulated: $ntot_yr_sim Martian years." |
|---|
| 266 | elif [ -v n_earth_years ]; then |
|---|
| 267 | ntot_yr_sim=$(echo "$n_earth_years/$r_plnt2earth_yr" | bc -l) |
|---|
| 268 | echo "Number of years to be simulated: $n_earth_years Earth years = $ntot_yr_sim Martian years." |
|---|
| 269 | fi |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | # To initialize the workflow script |
|---|
| 273 | ini_workflow() { |
|---|
| 274 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user." |
|---|
| 275 | convert_earth2plnt_years |
|---|
| 276 | n_yr_sim=0. |
|---|
| 277 | i_pem_run=1 |
|---|
| 278 | i_pcm_run=1 |
|---|
| 279 | if [ -f "startfi.nc" ]; then |
|---|
| 280 | cp startfi.nc starts/ |
|---|
| 281 | fi |
|---|
| 282 | if [ -f "start.nc" ]; then |
|---|
| 283 | cp start.nc starts/ |
|---|
| 284 | elif [ -f "start1D.txt" ]; then |
|---|
| 285 | cp start1D.txt starts/ |
|---|
| 286 | fi |
|---|
| 287 | if [ -f "startpem.nc" ]; then |
|---|
| 288 | cp startpem.nc starts/ |
|---|
| 289 | fi |
|---|
| 290 | |
|---|
| 291 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 292 | echo $n_yr_sim $ntot_yr_sim $r_plnt2earth_yr $i_pcm_run $i_pem_run $n_pcm_runs $n_pcm_runs_ini > pem_workflow.sts |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | # To submit the PCM runs |
|---|
| 296 | # arg1: execution mode |
|---|
| 297 | # arg2: number of PCM runs to submit |
|---|
| 298 | # arg3: local number of the PCM run from which to start (optional) |
|---|
| 299 | submit_pcm_phase() { |
|---|
| 300 | find . -type f -name "PCMrun*.job" ! -name "pcm_run.job" -delete |
|---|
| 301 | ii=1 |
|---|
| 302 | if [ ! -z $3 ]; then |
|---|
| 303 | ii=$3 |
|---|
| 304 | fi |
|---|
| 305 | if [ $(echo "$n_yr_sim < $ntot_yr_sim" | bc -l) -eq 1 ]; then |
|---|
| 306 | echo "Run \"PCM $i_pcm_run\" ($ii/$2)" |
|---|
| 307 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 308 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $2 + 2" | bc)/" pcm_run.job |
|---|
| 309 | ./pcm_run.job |
|---|
| 310 | if [ $? -ne 0 ]; then |
|---|
| 311 | abort_workflow |
|---|
| 312 | fi |
|---|
| 313 | else # Mode: submitting jobs |
|---|
| 314 | cp pcm_run.job PCMrun${i_pcm_run}.job |
|---|
| 315 | sed -i -E "/^$job_name/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${i_pcm_run}\3/" PCMrun${i_pcm_run}.job |
|---|
| 316 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $2 + 2" | bc)/" PCMrun${i_pcm_run}.job |
|---|
| 317 | if [[ "$job_scheduler" == "SLURM" ]]; then |
|---|
| 318 | jobID=$(sbatch --parsable PCMrun${i_pcm_run}.job) |
|---|
| 319 | elif [[ "$job_scheduler" == "PBS" ]]; then |
|---|
| 320 | jobID=$(qsub PCMrun${i_pcm_run}.job | cut -d. -f1) |
|---|
| 321 | fi |
|---|
| 322 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 323 | echo "#!/bin/bash" > kill_pem_workflow.sh |
|---|
| 324 | chmod +x kill_pem_workflow.sh |
|---|
| 325 | echo $kill_job $jobID >> kill_pem_workflow.sh |
|---|
| 326 | fi |
|---|
| 327 | ((i_pcm_run++)) |
|---|
| 328 | ((ii++)) |
|---|
| 329 | else |
|---|
| 330 | end_workflow |
|---|
| 331 | fi |
|---|
| 332 | for ((i = $ii; i <= $2; i++)); do |
|---|
| 333 | if [ $(echo "$n_yr_sim < $ntot_yr_sim" | bc -l) -eq 1 ]; then |
|---|
| 334 | echo "Run \"PCM $i_pcm_run\" ($i/$2)" |
|---|
| 335 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 336 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $2 + 2" | bc)/" pcm_run.job |
|---|
| 337 | ./pcm_run.job |
|---|
| 338 | if [ $? -ne 0 ]; then |
|---|
| 339 | abort_workflow |
|---|
| 340 | fi |
|---|
| 341 | else # Mode: submitting jobs |
|---|
| 342 | cp pcm_run.job PCMrun${i_pcm_run}.job |
|---|
| 343 | sed -i -E "/^$job_name/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${i_pcm_run}\3/" PCMrun${i_pcm_run}.job |
|---|
| 344 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $2 + 2" | bc)/" PCMrun${i_pcm_run}.job |
|---|
| 345 | if [[ "$job_scheduler" == "SLURM" ]]; then |
|---|
| 346 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} PCMrun${i_pcm_run}.job) |
|---|
| 347 | elif [[ "$job_scheduler" == "PBS" ]]; then |
|---|
| 348 | jobID=$(qsub -W depend=afterok:${jobID} PCMrun${i_pcm_run}.job | cut -d. -f1) |
|---|
| 349 | fi |
|---|
| 350 | echo $kill_job $jobID >> kill_pem_workflow.sh |
|---|
| 351 | fi |
|---|
| 352 | ((i_pcm_run++)) |
|---|
| 353 | else |
|---|
| 354 | end_workflow |
|---|
| 355 | fi |
|---|
| 356 | done |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | # To submit the PEM run |
|---|
| 360 | # arg1: execution mode |
|---|
| 361 | submit_pem_phase() { |
|---|
| 362 | if [ $(echo "$n_yr_sim < $ntot_yr_sim" | bc -l) -eq 1 ]; then |
|---|
| 363 | echo "Run \"PEM $i_pem_run\"" |
|---|
| 364 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 365 | ./pem_run.job |
|---|
| 366 | if [ $? -ne 0 ]; then |
|---|
| 367 | abort_workflow |
|---|
| 368 | fi |
|---|
| 369 | else # Mode: submitting jobs |
|---|
| 370 | sed -i -E "/^$job_name/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${i_pem_run}\3/" pem_run.job |
|---|
| 371 | if [[ "$job_scheduler" == "SLURM" ]]; then |
|---|
| 372 | jobID=$(sbatch --parsable pem_run.job) |
|---|
| 373 | elif [[ "$job_scheduler" == "PBS" ]]; then |
|---|
| 374 | jobID=$(qsub pem_run.job | cut -d. -f1) |
|---|
| 375 | fi |
|---|
| 376 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 377 | echo "#!/bin/bash" > kill_pem_workflow.sh |
|---|
| 378 | chmod +x kill_pem_workflow.sh |
|---|
| 379 | echo $kill_job $jobID >> kill_pem_workflow.sh |
|---|
| 380 | fi |
|---|
| 381 | else |
|---|
| 382 | end_workflow |
|---|
| 383 | fi |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | # To make one cycle of PCM and PEM runs |
|---|
| 387 | # arg1: execution mode |
|---|
| 388 | # arg2: number of PCM runs to submit |
|---|
| 389 | # arg3: local number of the PCM run from which to start (optional) |
|---|
| 390 | submit_cycle() { |
|---|
| 391 | # PCM runs |
|---|
| 392 | submit_pcm_phase $1 $2 $3 |
|---|
| 393 | |
|---|
| 394 | # PEM run |
|---|
| 395 | if [ $(echo "$n_yr_sim < $ntot_yr_sim" | bc -l) -eq 1 ]; then |
|---|
| 396 | echo "Run \"PEM $i_pem_run\"" |
|---|
| 397 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| 398 | ./pem_run.job |
|---|
| 399 | if [ $? -ne 0 ]; then |
|---|
| 400 | abort_workflow |
|---|
| 401 | fi |
|---|
| 402 | else # Mode: submitting jobs |
|---|
| 403 | sed -i -E "/^$job_name/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${i_pem_run}\3/" pem_run.job |
|---|
| 404 | if [[ "$job_scheduler" == "SLURM" ]]; then |
|---|
| 405 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} pem_run.job) |
|---|
| 406 | elif [[ "$job_scheduler" == "PBS" ]]; then |
|---|
| 407 | jobID=$(qsub -W depend=afterok:${jobID} pem_run.job | cut -d. -f1) |
|---|
| 408 | fi |
|---|
| 409 | echo $kill_job $jobID >> kill_pem_workflow.sh |
|---|
| 410 | fi |
|---|
| 411 | else |
|---|
| 412 | end_workflow |
|---|
| 413 | fi |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | # To clean-up files after resuming |
|---|
| 417 | # arg1: file name prefix to clean |
|---|
| 418 | # arg2: file name extension to clean |
|---|
| 419 | # arg3: file number from which to clean |
|---|
| 420 | cleanup() { |
|---|
| 421 | prefix=$1 |
|---|
| 422 | extension=$2 |
|---|
| 423 | if [ -z "$extension" ]; then |
|---|
| 424 | for file in ${prefix}*; do |
|---|
| 425 | num=${file#$prefix} |
|---|
| 426 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 427 | rm $file |
|---|
| 428 | fi |
|---|
| 429 | done |
|---|
| 430 | else |
|---|
| 431 | for file in ${prefix}*${extension}; do |
|---|
| 432 | num=${file#$prefix} |
|---|
| 433 | num=${num%$extension} |
|---|
| 434 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 435 | rm $file |
|---|
| 436 | fi |
|---|
| 437 | done |
|---|
| 438 | fi |
|---|
| 439 | } |
|---|
| 440 | |
|---|
| 441 | # To resume workflow from PCM run |
|---|
| 442 | # arg1: execution mode |
|---|
| 443 | resume_from_pcm_run() { |
|---|
| 444 | i_pcm_run=$(($i_resume + 1)) |
|---|
| 445 | cleanup diags/diagfi .nc $i_resume |
|---|
| 446 | cleanup diags/diagsoil .nc $i_resume |
|---|
| 447 | cleanup diags/Xoutdaily4pem .nc $i_resume |
|---|
| 448 | cleanup diags/Xoutyearly4pem .nc $i_resume |
|---|
| 449 | cleanup logs/runPCM .log $i_resume |
|---|
| 450 | cleanup starts/restart1D .txt $i_resume |
|---|
| 451 | cleanup starts/restart .nc $i_resume |
|---|
| 452 | cleanup starts/restartfi .nc $i_resume |
|---|
| 453 | cp starts/restartfi${i_resume}.nc startfi.nc |
|---|
| 454 | if [ -f "starts/restart${i_resume}.nc" ]; then |
|---|
| 455 | cp starts/restart${i_resume}.nc start.nc |
|---|
| 456 | elif [ -f "starts/restart1D${i_resume}.txt" ]; then |
|---|
| 457 | cp starts/restart1D${i_resume}.txt start1D.txt |
|---|
| 458 | fi |
|---|
| 459 | if [ $i_resume -le $n_pcm_runs_ini ]; then |
|---|
| 460 | # PCM resumption during the initialization cycle |
|---|
| 461 | i_pem_run=1 |
|---|
| 462 | n_yr_sim=0 |
|---|
| 463 | sed -i "1s/.*/$n_yr_sim $ntot_yr_sim $r_plnt2earth_yr $i_pcm_run $i_pem_run $n_pcm_runs $n_pcm_runs_ini/" pem_workflow.sts |
|---|
| 464 | cleanup diags/diagevol .nc $(($i_pem_run - 1)) |
|---|
| 465 | cleanup diags/diagevol_soil .nc $(($i_pem_run - 1)) |
|---|
| 466 | cleanup logs/runPEM .log $(($i_pem_run - 1)) |
|---|
| 467 | cleanup starts/restart1D_postPEM .txt $(($i_pem_run - 1)) |
|---|
| 468 | cleanup starts/restart_postPEM .nc $(($i_pem_run - 1)) |
|---|
| 469 | cleanup starts/restartfi_postPEM .nc $(($i_pem_run - 1)) |
|---|
| 470 | cleanup starts/restartpem .nc $(($i_pem_run - 1)) |
|---|
| 471 | rm -f startpem.nc |
|---|
| 472 | if [ -f "starts/startpem.nc" ]; then |
|---|
| 473 | cp starts/startpem.nc . |
|---|
| 474 | fi |
|---|
| 475 | if [ $i_resume -eq $(($n_pcm_runs_ini - 1)) ]; then |
|---|
| 476 | cp diags/Xoutdaily4pem${i_resume}.nc Xoutdaily4pem_Y1.nc |
|---|
| 477 | cp diags/Xoutyearly4pem${i_resume}.nc Xoutyearly4pem_Y1.nc |
|---|
| 478 | submit_cycle $1 $n_pcm_runs_ini $i_pcm_run |
|---|
| 479 | elif [ $i_resume -eq $n_pcm_runs_ini ]; then |
|---|
| 480 | cp diags/Xoutdaily4pem$(($i_resume - 1)).nc Xoutdaily4pem_Y1.nc |
|---|
| 481 | cp diags/Xoutyearly4pem$(($i_resume - 1)).nc Xoutyearly4pem_Y1.nc |
|---|
| 482 | cp diags/Xoutdaily4pem${i_resume}.nc Xoutdaily4pem_Y2.nc |
|---|
| 483 | cp diags/Xoutyearly4pem${i_resume}.nc Xoutyearly4pem_Y2.nc |
|---|
| 484 | submit_pem_phase $1 # The next job is a PEM run |
|---|
| 485 | else |
|---|
| 486 | submit_cycle $1 $n_pcm_runs_ini $i_pcm_run |
|---|
| 487 | fi |
|---|
| 488 | else |
|---|
| 489 | # PCM resumption during a cycle |
|---|
| 490 | i_pem_run=$(echo "($i_pcm_run - $n_pcm_runs_ini)/$n_pcm_runs + 1" | bc) |
|---|
| 491 | il=$(echo "($i_resume - $n_pcm_runs_ini + 1)%$n_pcm_runs + 1" | bc) |
|---|
| 492 | n_yr_sim=$(awk "NR==$i_pem_run {printf \"%s\n\", \$3}" "pem_workflow.sts") |
|---|
| 493 | sed -i "1s/.*/$n_yr_sim $ntot_yr_sim $r_plnt2earth_yr $i_pcm_run $i_pem_run $n_pcm_runs $n_pcm_runs_ini/" pem_workflow.sts |
|---|
| 494 | cleanup diags/diagevol .nc $(($i_pem_run - 1)) |
|---|
| 495 | cleanup diags/diagevol_soil .nc $(($i_pem_run - 1)) |
|---|
| 496 | cleanup logs/runPEM .log $(($i_pem_run - 1)) |
|---|
| 497 | cleanup starts/restart1D_postPEM .txt $(($i_pem_run - 1)) |
|---|
| 498 | cleanup starts/restart_postPEM .nc $(($i_pem_run - 1)) |
|---|
| 499 | cleanup starts/restartfi_postPEM .nc $(($i_pem_run - 1)) |
|---|
| 500 | cleanup starts/restartpem .nc $(($i_pem_run - 1)) |
|---|
| 501 | cp starts/restartpem$(($i_pem_run - 1)).nc startpem.nc |
|---|
| 502 | if [ $il -eq $(($n_pcm_runs - 1)) ]; then # Second to last PCM run |
|---|
| 503 | cp diags/Xoutdaily4pem${i_resume}.nc Xoutdaily4pem_Y1.nc |
|---|
| 504 | cp diags/Xoutyearly4pem${i_resume}.nc Xoutyearly4pem_Y1.nc |
|---|
| 505 | submit_cycle $1 $n_pcm_runs $(($il + 1)) |
|---|
| 506 | elif [ $il -eq $n_pcm_runs ]; then # Last PCM run so the next job is a PEM run |
|---|
| 507 | cp diags/Xoutdaily4pem$(($i_resume - 1)).nc Xoutdaily4pem_Y1.nc |
|---|
| 508 | cp diags/Xoutyearly4pem$(($i_resume - 1)).nc Xoutyearly4pem_Y1.nc |
|---|
| 509 | cp diags/Xoutdaily4pem${i_resume}.nc Xoutdaily4pem_Y2.nc |
|---|
| 510 | cp diags/Xoutyearly4pem${i_resume}.nc Xoutyearly4pem_Y2.nc |
|---|
| 511 | submit_pem_phase $1 |
|---|
| 512 | else |
|---|
| 513 | submit_cycle $1 $n_pcm_runs $(($il + 1)) |
|---|
| 514 | fi |
|---|
| 515 | fi |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | # To resume workflow from PEM run |
|---|
| 519 | # arg1: execution mode |
|---|
| 520 | resume_from_pem_run() { |
|---|
| 521 | i_pem_run=$(echo "$i_resume + 1" | bc) |
|---|
| 522 | i_pcm_run=$(echo "$n_pcm_runs_ini + $n_pcm_runs*($i_resume - 1) + 1" | bc) |
|---|
| 523 | n_yr_sim=$(awk "NR==$i_pem_run {printf \"%s\n\", \$3}" "pem_workflow.sts") |
|---|
| 524 | sed -i "1s/.*/$n_yr_sim $ntot_yr_sim $r_plnt2earth_yr $i_pcm_run $i_pem_run $n_pcm_runs $n_pcm_runs_ini/" pem_workflow.sts |
|---|
| 525 | cleanup diags/diagfi .nc $(($i_pcm_run - 1)) |
|---|
| 526 | cleanup diags/diagsoil .nc $(($i_pcm_run - 1)) |
|---|
| 527 | cleanup logs/runPCM .log $(($i_pcm_run - 1)) |
|---|
| 528 | cleanup starts/restart1D .txt $(($i_pcm_run - 1)) |
|---|
| 529 | cleanup starts/restart .nc $(($i_pcm_run - 1)) |
|---|
| 530 | cleanup starts/restartfi .nc $(($i_pcm_run - 1)) |
|---|
| 531 | cleanup diags/Xoutdaily4pem .nc $(($i_pcm_run - 1)) |
|---|
| 532 | cleanup diags/Xoutyearly4pem .nc $(($i_pcm_run - 1)) |
|---|
| 533 | cleanup diags/diagevol .nc $i_resume |
|---|
| 534 | cleanup diags/diagevol_soil .nc $i_resume |
|---|
| 535 | cleanup logs/runPEM .log $i_resume |
|---|
| 536 | cleanup starts/restart1D_postPEM .txt $i_resume |
|---|
| 537 | cleanup starts/restart_postPEM .nc $i_resume |
|---|
| 538 | cleanup starts/restartfi_postPEM .nc $i_resume |
|---|
| 539 | cleanup starts/restartpem .nc $i_resume |
|---|
| 540 | cp starts/restartpem${i_resume}.nc startpem.nc |
|---|
| 541 | cp starts/restartfi_postPEM${i_resume}.nc startfi.nc |
|---|
| 542 | if [ -f "starts/restart_postPEM${i_resume}.nc" ]; then |
|---|
| 543 | cp starts/restart_postPEM${i_resume}.nc start.nc |
|---|
| 544 | elif [ -f "starts/restart1D_postPEM${i_resume}.txt" ]; then |
|---|
| 545 | cp starts/restart1D_postPEM${i_resume}.txt start1D.txt |
|---|
| 546 | fi |
|---|
| 547 | submit_cycle $1 $n_pcm_runs |
|---|
| 548 | } |
|---|