| [3349] | 1 | #!/bin/bash |
|---|
| 2 | ######################################################################## |
|---|
| 3 | ######## Library of bash functions for the PEM launching script ######## |
|---|
| 4 | ######################################################################## |
|---|
| 5 | |
|---|
| 6 | # To end the launching script |
|---|
| 7 | endlaunch() { |
|---|
| 8 | # Restore the previous value of LC_NUMERIC |
|---|
| 9 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 10 | |
|---|
| 11 | date |
|---|
| [3579] | 12 | echo "Success: the launching script for the PEM simulation completed normally!" |
|---|
| [3349] | 13 | exit 0 |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | # To end the launching script with error |
|---|
| 17 | errlaunch() { |
|---|
| 18 | # Restore the previous value of LC_NUMERIC |
|---|
| 19 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 20 | |
|---|
| 21 | date |
|---|
| [3579] | 22 | echo "Error: an issue occured in the launching script for the PEM simulation!" |
|---|
| [3349] | 23 | exit 1 |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| [3403] | 26 | # To check what is the job scheduler |
|---|
| 27 | function job_scheduler() { |
|---|
| 28 | if command -v squeue &> /dev/null; then |
|---|
| 29 | echo "SLURM is installed on $machine." |
|---|
| [3869] | 30 | scheduler="SLURM" |
|---|
| [3403] | 31 | name_job="#SBATCH --job-name=" |
|---|
| 32 | kill_job="scancel" |
|---|
| 33 | elif command -v qstat &> /dev/null; then |
|---|
| 34 | echo "PBS/TORQUE is installed on $machine." |
|---|
| [3869] | 35 | scheduler="PBS" |
|---|
| [3403] | 36 | name_job="#PBS -N " |
|---|
| 37 | kill_job="qdel" |
|---|
| 38 | else |
|---|
| 39 | echo "Error: neither SLURM nor TORQUE/PBS is installed on $machine!" |
|---|
| [3861] | 40 | echo "You need to adapt the script to your job scheduler or set 'mode' to 0." |
|---|
| [3349] | 41 | errlaunch |
|---|
| 42 | fi |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| [3859] | 45 | # To get the number of slopes for the simulation |
|---|
| 46 | get_nslope() { |
|---|
| 47 | ns=1 |
|---|
| 48 | if [ -f "startfi.nc" ]; then |
|---|
| 49 | ns=$(ncdump -h startfi.nc | sed -n 's/.*nslope = \([0-9]*\) ;.*/\1/p') |
|---|
| 50 | else |
|---|
| 51 | for f in run_PCM.def callphys.def; do |
|---|
| 52 | if [[ -f "$f" ]]; then |
|---|
| 53 | while IFS= read -r line; do |
|---|
| 54 | # Remove leading whitespace |
|---|
| 55 | trimmed=$(echo "$line" | sed 's/^[[:space:]]*//') |
|---|
| 56 | # Skip lines that are commented out |
|---|
| 57 | if [[ "$trimmed" == \#* ]]; then |
|---|
| 58 | continue |
|---|
| 59 | fi |
|---|
| 60 | # Check if line contains 'nslope = N' |
|---|
| 61 | if [[ "$trimmed" =~ ^nslope[[:space:]]*=[[:space:]]*([0-9]+) ]]; then |
|---|
| 62 | ns="${BASH_REMATCH[1]}" |
|---|
| 63 | break |
|---|
| 64 | fi |
|---|
| 65 | done < "$f" |
|---|
| 66 | [[ -n "$ns" ]] && break |
|---|
| 67 | fi |
|---|
| 68 | done |
|---|
| 69 | fi |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | # To modify the xml file according nslope |
|---|
| 73 | modify_xml() { |
|---|
| 74 | tmp="tmp_file_def.xml" |
|---|
| 75 | in_diurnalave=false |
|---|
| 76 | in_diurnalave_s=false |
|---|
| 77 | |
|---|
| 78 | sed -i 's/enabled="\.true\.\">/enabled=".false.">/g' file_def_physics_mars.xml |
|---|
| 79 | while IFS= read -r line; do |
|---|
| 80 | case "$line" in |
|---|
| 81 | *'<file id="diurnalave"'*) |
|---|
| 82 | in_diurnalave=true |
|---|
| 83 | ;; |
|---|
| 84 | *'<file id="diurnalave_s"'*) |
|---|
| 85 | in_diurnalave_s=true |
|---|
| 86 | ;; |
|---|
| 87 | esac |
|---|
| 88 | |
|---|
| 89 | if [[ $line == *'enabled="'* ]]; then |
|---|
| 90 | if $in_diurnalave; then |
|---|
| 91 | if [[ $ns -eq 1 ]]; then |
|---|
| 92 | line=' enabled=".true.">' |
|---|
| 93 | else |
|---|
| 94 | line=' enabled=".false.">' |
|---|
| 95 | fi |
|---|
| 96 | elif $in_diurnalave_s; then |
|---|
| 97 | if [[ $ns -eq 1 ]]; then |
|---|
| 98 | line=' enabled=".false.">' |
|---|
| 99 | else |
|---|
| 100 | line=' enabled=".true.">' |
|---|
| 101 | fi |
|---|
| 102 | fi |
|---|
| 103 | fi |
|---|
| 104 | |
|---|
| 105 | case "$line" in |
|---|
| 106 | *'</file>'*) |
|---|
| 107 | in_diurnalave=false |
|---|
| 108 | in_diurnalave_s=false |
|---|
| 109 | ;; |
|---|
| 110 | esac |
|---|
| 111 | |
|---|
| 112 | echo "$line" >> "$tmp" |
|---|
| 113 | done < file_def_physics_mars.xml |
|---|
| 114 | |
|---|
| 115 | mv "$tmp" file_def_physics_mars.xml |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| [3349] | 118 | # To check if everything necessary for the launching script is ok |
|---|
| 119 | checklaunch() { |
|---|
| 120 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 121 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 122 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 123 | |
|---|
| 124 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| [3861] | 125 | if [ $(echo "$n_mars_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 126 | echo "Error: 'n_mars_years' must be > 0!" |
|---|
| [3349] | 127 | errlaunch |
|---|
| 128 | fi |
|---|
| 129 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| [3861] | 130 | if [ $(echo "$n_earth_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 131 | echo "Error: 'n_earth_years' must be > 0!" |
|---|
| [3349] | 132 | errlaunch |
|---|
| 133 | fi |
|---|
| 134 | else |
|---|
| [3861] | 135 | echo "Error: the number of years to be simulated is not set!" |
|---|
| [3349] | 136 | errlaunch |
|---|
| 137 | fi |
|---|
| 138 | if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then |
|---|
| [3861] | 139 | echo "Error: 'nPCM_ini' must be >= 2!" |
|---|
| [3349] | 140 | errlaunch |
|---|
| 141 | fi |
|---|
| 142 | if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then |
|---|
| [3861] | 143 | echo "Error: 'nPCM' must be >= 2!" |
|---|
| [3349] | 144 | errlaunch |
|---|
| 145 | fi |
|---|
| [3391] | 146 | if [ ! -f "PCMrun.job" ]; then |
|---|
| 147 | echo "Error: file \"PCMrun.job\" does not exist in $dir!" |
|---|
| [3349] | 148 | errlaunch |
|---|
| 149 | fi |
|---|
| [3391] | 150 | if [ ! -f "PEMrun.job" ]; then |
|---|
| 151 | echo "Error: file \"PEMrun.job\" does not exist in $dir!" |
|---|
| [3349] | 152 | errlaunch |
|---|
| 153 | fi |
|---|
| 154 | if [ ! -f "run_PCM.def" ]; then |
|---|
| 155 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
|---|
| 156 | errlaunch |
|---|
| 157 | fi |
|---|
| 158 | if [ ! -f "run_PEM.def" ]; then |
|---|
| 159 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
|---|
| 160 | errlaunch |
|---|
| 161 | fi |
|---|
| 162 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 163 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 164 | errlaunch |
|---|
| 165 | fi |
|---|
| 166 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 167 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 168 | errlaunch |
|---|
| 169 | fi |
|---|
| 170 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 171 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 172 | errlaunch |
|---|
| 173 | fi |
|---|
| 174 | if [ ! -f "iodef.xml" ]; then |
|---|
| 175 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 176 | errlaunch |
|---|
| 177 | fi |
|---|
| [3861] | 178 | if [ ! -d "logs" ]; then |
|---|
| 179 | mkdir logs |
|---|
| [3349] | 180 | fi |
|---|
| 181 | if [ ! -d "starts" ]; then |
|---|
| 182 | mkdir starts |
|---|
| 183 | fi |
|---|
| 184 | if [ ! -d "diags" ]; then |
|---|
| 185 | mkdir diags |
|---|
| 186 | fi |
|---|
| [3495] | 187 | if [ $mode -ne 0 ]; then |
|---|
| [3403] | 188 | job_scheduler |
|---|
| 189 | fi |
|---|
| [3859] | 190 | # Set automatically the XIOS output file for the PEM according to the number of slopes |
|---|
| 191 | get_nslope |
|---|
| 192 | modify_xml |
|---|
| [3349] | 193 | } |
|---|
| 194 | |
|---|
| [3386] | 195 | # To convert Earth years into Mars years |
|---|
| 196 | convertyears() { |
|---|
| [3349] | 197 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 198 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 199 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 200 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 201 | if [ -v n_mars_years ]; then |
|---|
| 202 | n_myear=$n_mars_years |
|---|
| 203 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 204 | elif [ -v n_earth_years ]; then |
|---|
| [3498] | 205 | n_myear=$(echo "$n_earth_years/$convert_years" | bc -l) |
|---|
| [3349] | 206 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 207 | fi |
|---|
| [3386] | 208 | } |
|---|
| 209 | |
|---|
| 210 | # To initialize the launching script |
|---|
| 211 | initlaunch() { |
|---|
| 212 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user." |
|---|
| 213 | convertyears |
|---|
| [3498] | 214 | i_myear=0. |
|---|
| [3349] | 215 | iPEM=1 |
|---|
| [3355] | 216 | iPCM=1 |
|---|
| [3611] | 217 | if [ -f "startfi.nc" ]; then |
|---|
| 218 | cp startfi.nc starts/ |
|---|
| 219 | fi |
|---|
| [3349] | 220 | if [ -f "start.nc" ]; then |
|---|
| 221 | cp start.nc starts/ |
|---|
| [3820] | 222 | elif [ -f "start1D.txt" ]; then |
|---|
| 223 | cp start1D.txt starts/ |
|---|
| [3349] | 224 | fi |
|---|
| [3428] | 225 | if [ -f "startpem.nc" ]; then |
|---|
| 226 | cp startpem.nc starts/ |
|---|
| 227 | fi |
|---|
| [3349] | 228 | |
|---|
| 229 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 230 | echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > info_PEM.txt |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | # To submit the PCM runs |
|---|
| [3495] | 234 | # arg1: launching mode |
|---|
| [3556] | 235 | # arg2: counting method |
|---|
| 236 | # arg3: number of PCM runs to launch |
|---|
| 237 | # arg4: local number of the PCM run from which to start (optional) |
|---|
| [3349] | 238 | submitPCM() { |
|---|
| [3391] | 239 | find . -type f -name "PCMrun*.job" ! -name "PCMrun.job" -delete |
|---|
| [3355] | 240 | ii=1 |
|---|
| [3556] | 241 | if [ ! -z $4 ]; then |
|---|
| 242 | ii=$4 |
|---|
| [3355] | 243 | fi |
|---|
| [3517] | 244 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| [3850] | 245 | echo "Run \"PCM $iPCM\" ($ii/$3)" |
|---|
| [3495] | 246 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| [3840] | 247 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $3 + 2" | bc)/" PCMrun.job |
|---|
| [3391] | 248 | ./PCMrun.job |
|---|
| [3430] | 249 | if [ $? -ne 0 ]; then |
|---|
| 250 | errlaunch |
|---|
| 251 | fi |
|---|
| [3498] | 252 | else # Mode: submitting jobs |
|---|
| [3391] | 253 | cp PCMrun.job PCMrun${iPCM}.job |
|---|
| [3666] | 254 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job |
|---|
| [3840] | 255 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$ii - $3 + 2" | bc)/" PCMrun${iPCM}.job |
|---|
| [3869] | 256 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 257 | jobID=$(sbatch --parsable PCMrun${iPCM}.job) |
|---|
| 258 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 259 | jobID=$(qsub PCMrun${iPCM}.job | cut -d. -f1) |
|---|
| 260 | fi |
|---|
| [3391] | 261 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 262 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 263 | chmod +x kill_launchPEM.sh |
|---|
| [3403] | 264 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| [3391] | 265 | fi |
|---|
| [3349] | 266 | ((iPCM++)) |
|---|
| [3556] | 267 | if [ $2 -ne 0 ]; then # Counting: PCM runs taken into account |
|---|
| 268 | i_myear=$(echo "$i_myear + 1." | bc -l) |
|---|
| 269 | fi |
|---|
| [3355] | 270 | ((ii++)) |
|---|
| [3349] | 271 | else |
|---|
| 272 | endlaunch |
|---|
| 273 | fi |
|---|
| [3556] | 274 | for ((i = $ii; i <= $3; i++)); do |
|---|
| [3517] | 275 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| [3850] | 276 | echo "Run \"PCM $iPCM\" ($i/$3)" |
|---|
| [3495] | 277 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| [3840] | 278 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $3 + 2" | bc)/" PCMrun.job |
|---|
| [3391] | 279 | ./PCMrun.job |
|---|
| [3430] | 280 | if [ $? -ne 0 ]; then |
|---|
| 281 | errlaunch |
|---|
| 282 | fi |
|---|
| [3498] | 283 | else # Mode: submitting jobs |
|---|
| [3391] | 284 | cp PCMrun.job PCMrun${iPCM}.job |
|---|
| [3666] | 285 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job |
|---|
| [3840] | 286 | sed -i "s/^k=-\?[0-9]\+$/k=$(echo "$i - $3 + 2" | bc)/" PCMrun${iPCM}.job |
|---|
| [3869] | 287 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 288 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} PCMrun${iPCM}.job) |
|---|
| 289 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 290 | jobID=$(qsub -W depend=afterok:${jobID} PCMrun${iPCM}.job | cut -d. -f1) |
|---|
| 291 | fi |
|---|
| [3403] | 292 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| [3391] | 293 | fi |
|---|
| [3349] | 294 | ((iPCM++)) |
|---|
| [3556] | 295 | if [ $2 -ne 0 ]; then # Counting: PCM runs taken into account |
|---|
| 296 | i_myear=$(echo "$i_myear + 1." | bc -l) |
|---|
| 297 | fi |
|---|
| [3349] | 298 | else |
|---|
| 299 | endlaunch |
|---|
| 300 | fi |
|---|
| 301 | done |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | # To submit the PEM run |
|---|
| [3495] | 305 | # arg1: launching mode |
|---|
| [3349] | 306 | submitPEM() { |
|---|
| [3517] | 307 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| [3634] | 308 | echo "Run \"PEM $iPEM\"" |
|---|
| [3495] | 309 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| [3391] | 310 | ./PEMrun.job |
|---|
| [3430] | 311 | if [ $? -ne 0 ]; then |
|---|
| 312 | errlaunch |
|---|
| 313 | fi |
|---|
| [3498] | 314 | else # Mode: submitting jobs |
|---|
| [3666] | 315 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job |
|---|
| [3869] | 316 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 317 | jobID=$(sbatch --parsable PEMrun.job) |
|---|
| 318 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 319 | jobID=$(qsub PEMrun.job | cut -d. -f1) |
|---|
| 320 | fi |
|---|
| [3391] | 321 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 322 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 323 | chmod +x kill_launchPEM.sh |
|---|
| [3403] | 324 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| [3391] | 325 | fi |
|---|
| [3349] | 326 | else |
|---|
| 327 | endlaunch |
|---|
| 328 | fi |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | # To make one cycle of PCM and PEM runs |
|---|
| [3495] | 332 | # arg1: launching mode |
|---|
| [3556] | 333 | # arg2: counting method |
|---|
| 334 | # arg3: number of PCM runs to launch |
|---|
| 335 | # arg4: local number of the PCM run from which to start (optional) |
|---|
| [3349] | 336 | cyclelaunch() { |
|---|
| 337 | # PCM runs |
|---|
| [3556] | 338 | submitPCM $1 $2 $3 $4 |
|---|
| [3349] | 339 | |
|---|
| 340 | # PEM run |
|---|
| [3517] | 341 | if [ $(echo "$i_myear < $n_myear" | bc -l) -eq 1 ]; then |
|---|
| [3634] | 342 | echo "Run \"PEM $iPEM\"" |
|---|
| [3495] | 343 | if [ $1 -eq 0 ]; then # Mode: processing scripts |
|---|
| [3391] | 344 | ./PEMrun.job |
|---|
| [3430] | 345 | if [ $? -ne 0 ]; then |
|---|
| 346 | errlaunch |
|---|
| 347 | fi |
|---|
| [3498] | 348 | else # Mode: submitting jobs |
|---|
| [3666] | 349 | sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job |
|---|
| [3869] | 350 | if [[ "$scheduler" == "SLURM" ]]; then |
|---|
| 351 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} PEMrun.job) |
|---|
| 352 | elif [[ "$scheduler" == "PBS" ]]; then |
|---|
| 353 | jobID=$(qsub -W depend=afterok:${jobID} PEMrun.job | cut -d. -f1) |
|---|
| 354 | fi |
|---|
| [3403] | 355 | echo $kill_job $jobID >> kill_launchPEM.sh |
|---|
| [3391] | 356 | fi |
|---|
| [3355] | 357 | else |
|---|
| 358 | endlaunch |
|---|
| 359 | fi |
|---|
| [3349] | 360 | } |
|---|
| [3355] | 361 | |
|---|
| [3365] | 362 | # To clean files after the starting run of the relaunch |
|---|
| [3386] | 363 | # arg1: file name prefix to clean |
|---|
| 364 | # arg2: file name extension to clean |
|---|
| 365 | # arg3: file number from which to clean |
|---|
| [3365] | 366 | cleanfiles() { |
|---|
| 367 | prefix=$1 |
|---|
| 368 | extension=$2 |
|---|
| 369 | if [ -z "$extension" ]; then |
|---|
| 370 | for file in ${prefix}*; do |
|---|
| 371 | num=${file#$prefix} |
|---|
| 372 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 373 | rm $file |
|---|
| 374 | fi |
|---|
| 375 | done |
|---|
| 376 | else |
|---|
| 377 | for file in ${prefix}*${extension}; do |
|---|
| 378 | num=${file#$prefix} |
|---|
| 379 | num=${num%$extension} |
|---|
| 380 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 381 | rm $file |
|---|
| 382 | fi |
|---|
| 383 | done |
|---|
| 384 | fi |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| [3355] | 387 | # To relaunch from PCM run |
|---|
| [3495] | 388 | # arg1: launching mode |
|---|
| [3556] | 389 | # arg2: counting method |
|---|
| [3355] | 390 | relaunchPCM() { |
|---|
| 391 | iPCM=$(($irelaunch + 1)) |
|---|
| [3365] | 392 | cleanfiles diags/diagfi .nc $irelaunch |
|---|
| [3537] | 393 | cleanfiles diags/diagsoil .nc $irelaunch |
|---|
| [3386] | 394 | cleanfiles diags/data2reshape .nc $irelaunch |
|---|
| [3861] | 395 | cleanfiles logs/runPCM .log $irelaunch |
|---|
| [3365] | 396 | cleanfiles starts/restart1D .txt $irelaunch |
|---|
| 397 | cleanfiles starts/restart .nc $irelaunch |
|---|
| 398 | cleanfiles starts/restartfi .nc $irelaunch |
|---|
| [3355] | 399 | cp starts/restartfi${irelaunch}.nc startfi.nc |
|---|
| 400 | if [ -f "starts/restart${irelaunch}.nc" ]; then |
|---|
| 401 | cp starts/restart${irelaunch}.nc start.nc |
|---|
| 402 | elif [ -f "starts/restart1D${irelaunch}.txt" ]; then |
|---|
| 403 | cp starts/restart1D${irelaunch}.txt start1D.txt |
|---|
| 404 | fi |
|---|
| 405 | if [ $irelaunch -le $nPCM_ini ]; then |
|---|
| 406 | # PCM relaunch during the initialization cycle |
|---|
| 407 | iPEM=1 |
|---|
| [3556] | 408 | if [ $2 -ne 0 ]; then # Counting: PCM runs taken into account |
|---|
| 409 | i_myear=$irelaunch |
|---|
| 410 | else # Counting: only PEM runs count |
|---|
| 411 | i_myear=0 |
|---|
| 412 | fi |
|---|
| 413 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| [3538] | 414 | cleanfiles diags/diagpem .nc $(($iPEM - 1)) |
|---|
| 415 | cleanfiles diags/diagsoilpem .nc $(($iPEM - 1)) |
|---|
| [3861] | 416 | cleanfiles logs/runPEM .log $(($iPEM - 1)) |
|---|
| [3538] | 417 | cleanfiles starts/restart1D_postPEM .txt $(($iPEM - 1)) |
|---|
| 418 | cleanfiles starts/restart_postPEM .nc $(($iPEM - 1)) |
|---|
| 419 | cleanfiles starts/restartfi_postPEM .nc $(($iPEM - 1)) |
|---|
| 420 | cleanfiles starts/restartpem .nc $(($iPEM - 1)) |
|---|
| [3367] | 421 | rm -f startpem.nc |
|---|
| [3790] | 422 | if [ -f "starts/startpem.nc" ]; then |
|---|
| 423 | cp starts/startpem.nc . |
|---|
| 424 | fi |
|---|
| [3355] | 425 | if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then |
|---|
| 426 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc |
|---|
| [3556] | 427 | cyclelaunch $1 $2 $nPCM_ini $iPCM |
|---|
| [3383] | 428 | elif [ $irelaunch -eq $nPCM_ini ]; then |
|---|
| [3355] | 429 | cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc |
|---|
| 430 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc |
|---|
| [3391] | 431 | submitPEM $1 # The next job is a PEM run |
|---|
| [3355] | 432 | else |
|---|
| [3556] | 433 | cyclelaunch $1 $2 $nPCM_ini $iPCM |
|---|
| [3355] | 434 | fi |
|---|
| 435 | else |
|---|
| 436 | # PCM relaunch during a cycle |
|---|
| [3446] | 437 | iPEM=$(echo "($iPCM - $nPCM_ini)/$nPCM + 1" | bc) |
|---|
| 438 | il=$(echo "($irelaunch - $nPCM_ini + 1)%$nPCM + 1" | bc) |
|---|
| [3556] | 439 | if [ $2 -ne 0 ]; then # Counting: PCM runs taken into account |
|---|
| [3634] | 440 | i_myear=$(echo "$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "info_PEM.txt") + $il" | bc -l) |
|---|
| [3556] | 441 | else # Counting: only PEM runs count |
|---|
| [3620] | 442 | i_myear=$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "info_PEM.txt") |
|---|
| [3556] | 443 | fi |
|---|
| 444 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| [3538] | 445 | cleanfiles diags/diagpem .nc $(($iPEM - 1)) |
|---|
| 446 | cleanfiles diags/diagsoilpem .nc $(($iPEM - 1)) |
|---|
| [3861] | 447 | cleanfiles logs/runPEM .log $(($iPEM - 1)) |
|---|
| [3538] | 448 | cleanfiles starts/restart1D_postPEM .txt $(($iPEM - 1)) |
|---|
| 449 | cleanfiles starts/restart_postPEM .nc $(($iPEM - 1)) |
|---|
| 450 | cleanfiles starts/restartfi_postPEM .nc $(($iPEM - 1)) |
|---|
| 451 | cleanfiles starts/restartpem .nc $(($iPEM - 1)) |
|---|
| 452 | cp starts/restartpem$(($iPEM - 1)).nc startpem.nc |
|---|
| [3428] | 453 | if [ $il -eq $(($nPCM - 1)) ]; then # Second to last PCM run |
|---|
| [3355] | 454 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc |
|---|
| [3850] | 455 | cyclelaunch $1 $2 $nPCM $(($il + 1)) |
|---|
| [3428] | 456 | elif [ $il -eq $nPCM ]; then # Last PCM run so the next job is a PEM run |
|---|
| [3355] | 457 | cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc |
|---|
| 458 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc |
|---|
| [3428] | 459 | submitPEM $1 |
|---|
| [3355] | 460 | else |
|---|
| [3850] | 461 | cyclelaunch $1 $2 $nPCM $(($il + 1)) |
|---|
| [3355] | 462 | fi |
|---|
| 463 | fi |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | # To relaunch from PEM run |
|---|
| [3495] | 467 | # arg1: launching mode |
|---|
| [3556] | 468 | # arg2: counting method |
|---|
| [3355] | 469 | relaunchPEM() { |
|---|
| [3432] | 470 | iPEM=$(echo "$irelaunch + 1" | bc) |
|---|
| 471 | iPCM=$(echo "$nPCM_ini + $nPCM*($irelaunch - 1) + 1" | bc) |
|---|
| [3620] | 472 | i_myear=$(awk "NR==$iPEM {printf \"%s\n\", \$3}" "info_PEM.txt") |
|---|
| [3355] | 473 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| [3365] | 474 | cleanfiles diags/diagfi .nc $(($iPCM - 1)) |
|---|
| [3537] | 475 | cleanfiles diags/diagsoil .nc $(($iPCM - 1)) |
|---|
| [3861] | 476 | cleanfiles logs/runPCM .log $(($iPCM - 1)) |
|---|
| [3365] | 477 | cleanfiles starts/restart1D .txt $(($iPCM - 1)) |
|---|
| 478 | cleanfiles starts/restart .nc $(($iPCM - 1)) |
|---|
| 479 | cleanfiles starts/restartfi .nc $(($iPCM - 1)) |
|---|
| 480 | cleanfiles diags/data2reshape .nc $(($iPCM - 1)) |
|---|
| 481 | cleanfiles diags/diagpem .nc $irelaunch |
|---|
| [3537] | 482 | cleanfiles diags/diagsoilpem .nc $irelaunch |
|---|
| [3861] | 483 | cleanfiles logs/runPEM .log $irelaunch |
|---|
| [3365] | 484 | cleanfiles starts/restart1D_postPEM .txt $irelaunch |
|---|
| 485 | cleanfiles starts/restart_postPEM .nc $irelaunch |
|---|
| 486 | cleanfiles starts/restartfi_postPEM .nc $irelaunch |
|---|
| 487 | cleanfiles starts/restartpem .nc $irelaunch |
|---|
| [3355] | 488 | cp starts/restartpem${irelaunch}.nc startpem.nc |
|---|
| 489 | cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc |
|---|
| 490 | if [ -f "starts/restart_postPEM${irelaunch}.nc" ]; then |
|---|
| 491 | cp starts/restart_postPEM${irelaunch}.nc start.nc |
|---|
| 492 | elif [ -f "starts/restart1D_postPEM${irelaunch}.txt" ]; then |
|---|
| 493 | cp starts/restart1D_postPEM${irelaunch}.txt start1D.txt |
|---|
| 494 | fi |
|---|
| [3556] | 495 | cyclelaunch $1 $2 $nPCM |
|---|
| [3355] | 496 | } |
|---|