| 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 |
|---|
| 12 | echo "Successful end of the launching script for the PEM simulation." |
|---|
| 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 |
|---|
| 22 | echo "End with error of the launching script for the PEM." |
|---|
| 23 | exit 1 |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | # To check if SLURM is the job scheduler |
|---|
| 27 | function is_slurm() { |
|---|
| 28 | if [ ! -x $(command -v squeue) ]; then |
|---|
| 29 | echo "Error: the job scheduler is not SLURM on $machine!" |
|---|
| 30 | echo "You need to adapt the script to your case." |
|---|
| 31 | errlaunch |
|---|
| 32 | fi |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | # To check if everything necessary for the launching script is ok |
|---|
| 36 | checklaunch() { |
|---|
| 37 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 38 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 39 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 40 | |
|---|
| 41 | is_slurm |
|---|
| 42 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| 43 | if [ $n_mars_years -lt 1 ]; then |
|---|
| 44 | echo "Error: the value of 'n_mars_years' must be >0!" |
|---|
| 45 | errlaunch |
|---|
| 46 | fi |
|---|
| 47 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 48 | if [ $n_earth_years -lt 1 ]; then |
|---|
| 49 | echo "Error: the value of 'n_earth_years' must be >0!" |
|---|
| 50 | errlaunch |
|---|
| 51 | fi |
|---|
| 52 | else |
|---|
| 53 | echo "Error: no number of years to be simulated has been set!" |
|---|
| 54 | errlaunch |
|---|
| 55 | fi |
|---|
| 56 | if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then |
|---|
| 57 | echo "Error: the value of 'nPCM_ini' must be >1!" |
|---|
| 58 | errlaunch |
|---|
| 59 | fi |
|---|
| 60 | if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then |
|---|
| 61 | echo "Error: the value of 'nPCM' must be >1!" |
|---|
| 62 | errlaunch |
|---|
| 63 | fi |
|---|
| 64 | if [ ! -f "jobPCM.slurm" ]; then |
|---|
| 65 | echo "Error: file \"jobPCM.slurm\" does not exist in $dir!" |
|---|
| 66 | errlaunch |
|---|
| 67 | fi |
|---|
| 68 | if [ ! -f "jobPEM.slurm" ]; then |
|---|
| 69 | echo "Error: file \"$jobPEM.slurm\" does not exist in $dir!" |
|---|
| 70 | errlaunch |
|---|
| 71 | fi |
|---|
| 72 | if [ ! -f "run_PCM.def" ]; then |
|---|
| 73 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
|---|
| 74 | errlaunch |
|---|
| 75 | fi |
|---|
| 76 | if [ ! -f "run_PEM.def" ]; then |
|---|
| 77 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
|---|
| 78 | errlaunch |
|---|
| 79 | fi |
|---|
| 80 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 81 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 82 | errlaunch |
|---|
| 83 | fi |
|---|
| 84 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 85 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 86 | errlaunch |
|---|
| 87 | fi |
|---|
| 88 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 89 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 90 | errlaunch |
|---|
| 91 | fi |
|---|
| 92 | if [ ! -f "iodef.xml" ]; then |
|---|
| 93 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 94 | errlaunch |
|---|
| 95 | fi |
|---|
| 96 | if [ ! -d "out_PCM" ]; then |
|---|
| 97 | mkdir out_PCM |
|---|
| 98 | fi |
|---|
| 99 | if [ ! -d "out_PEM" ]; then |
|---|
| 100 | mkdir out_PEM |
|---|
| 101 | fi |
|---|
| 102 | if [ ! -d "starts" ]; then |
|---|
| 103 | mkdir starts |
|---|
| 104 | fi |
|---|
| 105 | if [ ! -d "diags" ]; then |
|---|
| 106 | mkdir diags |
|---|
| 107 | fi |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | # To initialize the launching script |
|---|
| 111 | initlaunch() { |
|---|
| 112 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user." |
|---|
| 113 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 114 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 115 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 116 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 117 | if [ -v n_mars_years ]; then |
|---|
| 118 | n_myear=$n_mars_years |
|---|
| 119 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 120 | elif [ -v n_earth_years ]; then |
|---|
| 121 | n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years |
|---|
| 122 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 123 | fi |
|---|
| 124 | i_myear=0 |
|---|
| 125 | iPEM=1 |
|---|
| 126 | iPCM=1 |
|---|
| 127 | cp startfi.nc starts/ |
|---|
| 128 | if [ -f "start.nc" ]; then |
|---|
| 129 | cp start.nc starts/ |
|---|
| 130 | elif [ -f "star1D.nc" ]; then |
|---|
| 131 | cp star1D.txt starts/ |
|---|
| 132 | fi |
|---|
| 133 | |
|---|
| 134 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 135 | echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > info_PEM.txt |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | # To submit the PCM runs |
|---|
| 139 | submitPCM() { |
|---|
| 140 | find . -type f -name "jobPCM*.slurm" ! -name "jobPCM.slurm" -delete |
|---|
| 141 | ii=1 |
|---|
| 142 | if [ ! -z $2 ]; then |
|---|
| 143 | ii=$2 |
|---|
| 144 | fi |
|---|
| 145 | if [ $i_myear -lt $n_myear ]; then |
|---|
| 146 | echo "Run PCM $iPCM: call $ii/$1..." |
|---|
| 147 | cp jobPCM.slurm jobPCM${iPCM}.slurm |
|---|
| 148 | sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm |
|---|
| 149 | sed -i "s/^k=[0-9]\+$/k=$(echo "3 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm |
|---|
| 150 | jobID=$(sbatch --parsable jobPCM${iPCM}.slurm) |
|---|
| 151 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 152 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 153 | chmod +x kill_launchPEM.sh |
|---|
| 154 | echo "scancel" $jobID >> kill_launchPEM.sh |
|---|
| 155 | ((iPCM++)) |
|---|
| 156 | ((i_myear++)) |
|---|
| 157 | ((ii++)) |
|---|
| 158 | else |
|---|
| 159 | endlaunch |
|---|
| 160 | fi |
|---|
| 161 | for ((i = $ii; i <= $1; i++)); do |
|---|
| 162 | if [ $i_myear -lt $n_myear ]; then |
|---|
| 163 | echo "Run PCM $iPCM: call $i/$1..." |
|---|
| 164 | cp jobPCM.slurm jobPCM${iPCM}.slurm |
|---|
| 165 | sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm |
|---|
| 166 | sed -i "s/^k=[0-9]\+$/k=$(echo "$i + 2 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm |
|---|
| 167 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPCM${iPCM}.slurm) |
|---|
| 168 | echo "scancel" $jobID >> kill_launchPEM.sh |
|---|
| 169 | ((iPCM++)) |
|---|
| 170 | ((i_myear++)) |
|---|
| 171 | else |
|---|
| 172 | endlaunch |
|---|
| 173 | fi |
|---|
| 174 | done |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | # To submit the PEM run |
|---|
| 178 | submitPEM() { |
|---|
| 179 | if [ $i_myear -lt $n_myear ]; then |
|---|
| 180 | echo "Run PEM $iPEM" |
|---|
| 181 | sed -i "s/#SBATCH --job-name=jobPEM.*/#SBATCH --job-name=jobPEM${iPEM}/" jobPEM.slurm |
|---|
| 182 | jobID=$(sbatch --parsable jobPEM.slurm) |
|---|
| 183 | # Create a file to cancel the dependent jobs of the cycle |
|---|
| 184 | echo "#!/bin/bash" > kill_launchPEM.sh |
|---|
| 185 | chmod +x kill_launchPEM.sh |
|---|
| 186 | echo "scancel" $jobID >> kill_launchPEM.sh |
|---|
| 187 | else |
|---|
| 188 | endlaunch |
|---|
| 189 | fi |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | # To make one cycle of PCM and PEM runs |
|---|
| 193 | cyclelaunch() { |
|---|
| 194 | # PCM runs |
|---|
| 195 | submitPCM $1 $2 |
|---|
| 196 | |
|---|
| 197 | # PEM run |
|---|
| 198 | if [ $i_myear -lt $n_myear ]; then |
|---|
| 199 | echo "Run PEM $iPEM" |
|---|
| 200 | sed -i "s/#SBATCH --job-name=jobPEM.*/#SBATCH --job-name=jobPEM${iPEM}/" jobPEM.slurm |
|---|
| 201 | jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPEM.slurm) |
|---|
| 202 | echo "scancel" $jobID >> kill_launchPEM.sh |
|---|
| 203 | else |
|---|
| 204 | endlaunch |
|---|
| 205 | fi |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | # To clean files after the starting run of the relaunch |
|---|
| 209 | cleanfiles() { |
|---|
| 210 | prefix=$1 |
|---|
| 211 | extension=$2 |
|---|
| 212 | if [ -z "$extension" ]; then |
|---|
| 213 | for file in ${prefix}*; do |
|---|
| 214 | num=${file#$prefix} |
|---|
| 215 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 216 | rm $file |
|---|
| 217 | fi |
|---|
| 218 | done |
|---|
| 219 | else |
|---|
| 220 | for file in ${prefix}*${extension}; do |
|---|
| 221 | num=${file#$prefix} |
|---|
| 222 | num=${num%$extension} |
|---|
| 223 | if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then |
|---|
| 224 | rm $file |
|---|
| 225 | fi |
|---|
| 226 | done |
|---|
| 227 | fi |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | # To relaunch from PCM run |
|---|
| 231 | relaunchPCM() { |
|---|
| 232 | iPCM=$(($irelaunch + 1)) |
|---|
| 233 | cleanfiles diags/diagfi .nc $irelaunch |
|---|
| 234 | cleanfiles "out_PCM/run" "" $irelaunch |
|---|
| 235 | cleanfiles starts/restart1D .txt $irelaunch |
|---|
| 236 | cleanfiles starts/restart .nc $irelaunch |
|---|
| 237 | cleanfiles starts/restartfi .nc $irelaunch |
|---|
| 238 | cp starts/restartfi${irelaunch}.nc startfi.nc |
|---|
| 239 | if [ -f "starts/restart${irelaunch}.nc" ]; then |
|---|
| 240 | cp starts/restart${irelaunch}.nc start.nc |
|---|
| 241 | elif [ -f "starts/restart1D${irelaunch}.txt" ]; then |
|---|
| 242 | cp starts/restart1D${irelaunch}.txt start1D.txt |
|---|
| 243 | fi |
|---|
| 244 | if [ $irelaunch -le $nPCM_ini ]; then |
|---|
| 245 | # PCM relaunch during the initialization cycle |
|---|
| 246 | iPEM=1 |
|---|
| 247 | cleanfiles diags/diagpem .nc $iPEM |
|---|
| 248 | cleanfiles "out_PEM/run" "" $iPEM |
|---|
| 249 | cleanfiles starts/restart1D_postPEM .txt $iPEM |
|---|
| 250 | cleanfiles starts/restart_postPEM .nc $iPEM |
|---|
| 251 | cleanfiles starts/restartfi_postPEM .nc $iPEM |
|---|
| 252 | cleanfiles starts/restartpem .nc $iPEM |
|---|
| 253 | i_myear=$irelaunch |
|---|
| 254 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| 255 | if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then |
|---|
| 256 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc |
|---|
| 257 | cleanfiles diags/data2reshape .nc $irelaunch |
|---|
| 258 | cyclelaunch $nPCM_ini $irelaunch |
|---|
| 259 | elif [ $irelaunch -eq 0 ]; then |
|---|
| 260 | cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc |
|---|
| 261 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc |
|---|
| 262 | cleanfiles diags/data2reshape .nc $(($irelaunch - 1)) |
|---|
| 263 | submitPEM # The next job is a PEM run |
|---|
| 264 | else |
|---|
| 265 | cleanfiles diags/data2reshape .nc 0 |
|---|
| 266 | cyclelaunch $nPCM_ini $iPCM |
|---|
| 267 | fi |
|---|
| 268 | else |
|---|
| 269 | # PCM relaunch during a cycle |
|---|
| 270 | iPEM=$((($irelaunch - $nPCM_ini)/$nPCM + 1)) |
|---|
| 271 | il=$(echo "($irelaunch - $nPCM_ini)%$nPCM" | bc -l) |
|---|
| 272 | cleanfiles diags/diagpem .nc $iPEM |
|---|
| 273 | cleanfiles "out_PEM/run" "" $iPEM |
|---|
| 274 | cleanfiles starts/restart1D_postPEM .txt $iPEM |
|---|
| 275 | cleanfiles starts/restart_postPEM .nc $iPEM |
|---|
| 276 | cleanfiles starts/restartfi_postPEM .nc $iPEM |
|---|
| 277 | cleanfiles starts/restartpem .nc $iPEM |
|---|
| 278 | cp starts/restartpem${iPEM}.nc startpem.nc |
|---|
| 279 | if [ $il -eq $(($nPCM - 1)) ]; then |
|---|
| 280 | i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il)) |
|---|
| 281 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| 282 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc |
|---|
| 283 | cleanfiles diags/data2reshape .nc $irelaunch |
|---|
| 284 | cyclelaunch $nPCM $il |
|---|
| 285 | elif [ $il -eq 0 ]; then |
|---|
| 286 | i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $nPCM)) |
|---|
| 287 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| 288 | cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc |
|---|
| 289 | cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc |
|---|
| 290 | cleanfiles diags/data2reshape .nc $(($irelaunch - 1)) |
|---|
| 291 | submitPEM # The next job is a PEM run |
|---|
| 292 | else |
|---|
| 293 | i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il)) |
|---|
| 294 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| 295 | cleanfiles diags/data2reshape .nc $irelaunch |
|---|
| 296 | cyclelaunch $nPCM $il |
|---|
| 297 | fi |
|---|
| 298 | fi |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | # To relaunch from PEM run |
|---|
| 302 | relaunchPEM() { |
|---|
| 303 | iPEM=$irelaunch |
|---|
| 304 | iPCM=$(($nPCM_ini + ($nPCM - 1)*$irelaunch + 1)) |
|---|
| 305 | i_myear=$(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt") |
|---|
| 306 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt |
|---|
| 307 | cleanfiles diags/diagfi .nc $(($iPCM - 1)) |
|---|
| 308 | cleanfiles "out_PCM/run" "" $(($iPCM - 1)) |
|---|
| 309 | cleanfiles starts/restart1D .txt $(($iPCM - 1)) |
|---|
| 310 | cleanfiles starts/restart .nc $(($iPCM - 1)) |
|---|
| 311 | cleanfiles starts/restartfi .nc $(($iPCM - 1)) |
|---|
| 312 | cleanfiles diags/data2reshape .nc $(($iPCM - 1)) |
|---|
| 313 | cleanfiles diags/diagpem .nc $irelaunch |
|---|
| 314 | cleanfiles "out_PEM/run" "" $irelaunch |
|---|
| 315 | cleanfiles starts/restart1D_postPEM .txt $irelaunch |
|---|
| 316 | cleanfiles starts/restart_postPEM .nc $irelaunch |
|---|
| 317 | cleanfiles starts/restartfi_postPEM .nc $irelaunch |
|---|
| 318 | cleanfiles starts/restartpem .nc $irelaunch |
|---|
| 319 | cp starts/restartpem${irelaunch}.nc startpem.nc |
|---|
| 320 | cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc |
|---|
| 321 | if [ -f "starts/restart_postPEM${irelaunch}.nc" ]; then |
|---|
| 322 | cp starts/restart_postPEM${irelaunch}.nc start.nc |
|---|
| 323 | elif [ -f "starts/restart1D_postPEM${irelaunch}.txt" ]; then |
|---|
| 324 | cp starts/restart1D_postPEM${irelaunch}.txt start1D.txt |
|---|
| 325 | fi |
|---|
| 326 | cyclelaunch $nPCM |
|---|
| 327 | } |
|---|