| 1 | #!/bin/bash |
|---|
| 2 | ##################################################################### |
|---|
| 3 | ### Launching script for a chained simulation of PEM and PCM runs ### |
|---|
| 4 | ##################################################################### |
|---|
| 5 | |
|---|
| 6 | echo "The launching script is starting!" |
|---|
| 7 | echo "The output file is \"loglaunch.txt\"." |
|---|
| 8 | if [ "$1" = "bg" ]; then |
|---|
| 9 | date |
|---|
| 10 | else |
|---|
| 11 | nohup "$0" bg > loglaunch.txt 2>&1 & |
|---|
| 12 | exit 1 |
|---|
| 13 | fi |
|---|
| 14 | |
|---|
| 15 | # A few parameters that might need be changed depending on your setup: |
|---|
| 16 | # Path to the arch.env to source: |
|---|
| 17 | source ../trunk/LMDZ.COMMON/arch.env |
|---|
| 18 | |
|---|
| 19 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 20 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 21 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | ##################################################################### |
|---|
| 25 | #---------- Modify here the number of years to be simulated ------------ |
|---|
| 26 | ## set the number of years, either Martian or Earth years: |
|---|
| 27 | n_mars_years=1000 |
|---|
| 28 | #n_earth_years=300 |
|---|
| 29 | |
|---|
| 30 | #------------- Modify here the number of initial PCM calls ------------- |
|---|
| 31 | ## set the number of initial PCM calls between each PEM call: |
|---|
| 32 | nPCM_ini=4 |
|---|
| 33 | |
|---|
| 34 | #---------------- Modify here the number of PCM calls ------------------ |
|---|
| 35 | ## set the number of PCM calls between each PEM call: |
|---|
| 36 | nPCM=2 |
|---|
| 37 | |
|---|
| 38 | #------------------ Modify here the name of PEM exe -------------------- |
|---|
| 39 | ## fill in the name of executable for PEM: |
|---|
| 40 | exePEM="pem_29_phymars_seq.e" |
|---|
| 41 | |
|---|
| 42 | #-------------- Modify here the name of reshape XIOS exe --------------- |
|---|
| 43 | ## fill in the name of executable for reshape XIOS: |
|---|
| 44 | exeReshape="reshape_XIOS_output_64x48x29_phymars_seq.e" |
|---|
| 45 | ##################################################################### |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | #------ Check if files/directories necessary for the script exist ------ |
|---|
| 49 | dir=`pwd` |
|---|
| 50 | machine=`hostname` |
|---|
| 51 | address=`whoami` |
|---|
| 52 | if [ ! -f "$exePEM" ]; then |
|---|
| 53 | echo "Error: file \"$exePEM\" does not exist in $dir!" |
|---|
| 54 | exit 1 |
|---|
| 55 | fi |
|---|
| 56 | if [ ! -f "$exeReshape" ]; then |
|---|
| 57 | echo "Error: file \"$exeReshape\" does not exist in $dir!" |
|---|
| 58 | exit 1 |
|---|
| 59 | fi |
|---|
| 60 | if [ ! -f "exePCM.sh" ]; then |
|---|
| 61 | echo "Error: file \"exePCM.sh\" does not exist in $dir!" |
|---|
| 62 | exit 1 |
|---|
| 63 | fi |
|---|
| 64 | if [ ! -f "run_PEM.def" ]; then |
|---|
| 65 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
|---|
| 66 | exit 1 |
|---|
| 67 | fi |
|---|
| 68 | if [ ! -f "run_PCM.def" ]; then |
|---|
| 69 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
|---|
| 70 | exit 1 |
|---|
| 71 | fi |
|---|
| 72 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 73 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 74 | exit 1 |
|---|
| 75 | fi |
|---|
| 76 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 77 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 78 | exit 1 |
|---|
| 79 | fi |
|---|
| 80 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 81 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 82 | exit 1 |
|---|
| 83 | fi |
|---|
| 84 | if [ ! -f "iodef.xml" ]; then |
|---|
| 85 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 86 | exit 1 |
|---|
| 87 | fi |
|---|
| 88 | if [ ! -d "out_PCM" ]; then |
|---|
| 89 | mkdir out_PCM |
|---|
| 90 | fi |
|---|
| 91 | if [ ! -d "out_PEM" ]; then |
|---|
| 92 | mkdir out_PEM |
|---|
| 93 | fi |
|---|
| 94 | if [ ! -d "starts" ]; then |
|---|
| 95 | mkdir starts |
|---|
| 96 | fi |
|---|
| 97 | if [ ! -d "diags" ]; then |
|---|
| 98 | mkdir diags |
|---|
| 99 | fi |
|---|
| 100 | |
|---|
| 101 | #---------------------------- Initialization --------------------------- |
|---|
| 102 | dir=`pwd` |
|---|
| 103 | machine=`hostname` |
|---|
| 104 | address=`whoami` |
|---|
| 105 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine." |
|---|
| 106 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 107 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 108 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 109 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 110 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| 111 | n_myear=$n_mars_years |
|---|
| 112 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 113 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 114 | n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years |
|---|
| 115 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 116 | else |
|---|
| 117 | echo "No number of years to be simulated has been set!" |
|---|
| 118 | exit 1 |
|---|
| 119 | fi |
|---|
| 120 | i_myear=0 |
|---|
| 121 | iPEM=1 |
|---|
| 122 | ((iPCM = ($iPEM - 1)*$nPCM + 1)) |
|---|
| 123 | cp startfi.nc starts/ |
|---|
| 124 | if [ -f "start.nc" ]; then |
|---|
| 125 | cp start.nc starts/ |
|---|
| 126 | elif [ -f "star1D.nc" ]; then |
|---|
| 127 | cp star1D.txt starts/ |
|---|
| 128 | fi |
|---|
| 129 | |
|---|
| 130 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 131 | if [ -f "info_PEM.txt" ]; then |
|---|
| 132 | rm info_PEM.txt |
|---|
| 133 | fi |
|---|
| 134 | echo $i_myear $n_myear $convert_years > info_PEM.txt |
|---|
| 135 | |
|---|
| 136 | #-------------------------- Initial PCM runs --------------------------- |
|---|
| 137 | cp run_PCM.def run.def |
|---|
| 138 | for ((i = 1; i <= $nPCM_ini; i++)); do |
|---|
| 139 | echo "Run PCM $iPCM (initial): call $i/$nPCM_ini..." |
|---|
| 140 | sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh |
|---|
| 141 | sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh |
|---|
| 142 | sbatch -W exePCM.sh |
|---|
| 143 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
|---|
| 144 | echo "Error: the run PCM $iPCM crashed!" |
|---|
| 145 | exit 1 |
|---|
| 146 | fi |
|---|
| 147 | # Copy data files and prepare the next run |
|---|
| 148 | mv out_runPCM${iPCM} out_PCM/run${iPCM} |
|---|
| 149 | mv diagfi.nc diags/diagfi${iPCM}.nc |
|---|
| 150 | if [ -f "diagsoil.nc" ]; then |
|---|
| 151 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
|---|
| 152 | fi |
|---|
| 153 | if [ -f "stats.nc" ]; then |
|---|
| 154 | mv stats.nc diags/stats${iPCM}.nc |
|---|
| 155 | fi |
|---|
| 156 | k=$(echo "$i + 2 - $nPCM_ini" | bc -l) |
|---|
| 157 | if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM |
|---|
| 158 | mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
|---|
| 159 | else |
|---|
| 160 | cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
|---|
| 161 | mv Xdiurnalave.nc data2reshape_Y${k}.nc |
|---|
| 162 | fi |
|---|
| 163 | cp restartfi.nc starts/startfi${iPCM}.nc |
|---|
| 164 | mv restartfi.nc startfi.nc |
|---|
| 165 | if [ -f "restart.nc" ]; then |
|---|
| 166 | cp restart.nc starts/restart${iPCM}.nc |
|---|
| 167 | mv restart.nc start.nc |
|---|
| 168 | elif [ -f "restart1D.txt" ]; then |
|---|
| 169 | cp restart1D.txt starts/restart1D${iPCM}.txt |
|---|
| 170 | mv restart1D.txt start1D.txt |
|---|
| 171 | fi |
|---|
| 172 | ((iPCM++)) |
|---|
| 173 | ((i_myear++)) |
|---|
| 174 | echo "Done!" |
|---|
| 175 | done |
|---|
| 176 | sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt |
|---|
| 177 | |
|---|
| 178 | #---------------------- Main loop to call PEM/PCM ---------------------- |
|---|
| 179 | while [ $i_myear -lt $n_myear ]; do |
|---|
| 180 | #--- Reshaping PCM data with XIOS |
|---|
| 181 | echo "Reshaping PCM data with XIOS..." |
|---|
| 182 | ./$exeReshape |
|---|
| 183 | echo "Done!" |
|---|
| 184 | |
|---|
| 185 | #--- Running PEM |
|---|
| 186 | echo "Run PEM $iPEM..." |
|---|
| 187 | cp run_PEM.def run.def |
|---|
| 188 | mv startfi.nc startfi_evol.nc |
|---|
| 189 | if [ -f "start.nc" ]; then |
|---|
| 190 | mv start.nc start_evol.nc |
|---|
| 191 | elif [ -f "start1D.txt" ]; then |
|---|
| 192 | mv start1D.txt start1D_evol.txt |
|---|
| 193 | fi |
|---|
| 194 | ./$exePEM > out_runPEM${iPEM} 2>&1 |
|---|
| 195 | if [ ! -f "restartfi_evol.nc" ]; then # Check if run ended abnormally |
|---|
| 196 | echo "Error: the run PEM $iPEM crashed!" |
|---|
| 197 | exit 1 |
|---|
| 198 | fi |
|---|
| 199 | # Copy data files and prepare the next run |
|---|
| 200 | mv out_runPEM${iPEM} out_PEM/run${iPEM} |
|---|
| 201 | mv diagpem.nc diags/diagpem${iPEM}.nc |
|---|
| 202 | if [ -f "diagsoilpem.nc" ]; then |
|---|
| 203 | mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc |
|---|
| 204 | fi |
|---|
| 205 | cp restartpem.nc starts/startpem${iPEM}.nc |
|---|
| 206 | mv restartpem.nc startpem.nc |
|---|
| 207 | cp restartfi_evol.nc starts/startfi_postPEM${iPEM}.nc |
|---|
| 208 | mv restartfi_evol.nc startfi.nc |
|---|
| 209 | if [ -f "restart_evol.nc" ]; then |
|---|
| 210 | cp restart_evol.nc starts/restart_postPEM${iPEM}.nc |
|---|
| 211 | mv restart_evol.nc start.nc |
|---|
| 212 | elif [ -f "restart1D_evol.txt" ]; then |
|---|
| 213 | cp restart1D_evol.txt starts/restart1D_postPEM${iPEM}.txt |
|---|
| 214 | mv restart1D_evol.txt start1D.txt |
|---|
| 215 | fi |
|---|
| 216 | ((iPEM++)) |
|---|
| 217 | read i_myear n_myear convert_years < info_PEM.txt |
|---|
| 218 | echo "Done!" |
|---|
| 219 | |
|---|
| 220 | #--- Loop to run PCM year by year |
|---|
| 221 | cp run_PCM.def run.def |
|---|
| 222 | for ((i = 1; i <= $nPCM; i++)); do |
|---|
| 223 | echo "Run PCM $iPCM: call $i/$nPCM..." |
|---|
| 224 | sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh |
|---|
| 225 | sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh |
|---|
| 226 | sbatch -W exePCM.sh |
|---|
| 227 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
|---|
| 228 | echo "Error: the run PCM $iPCM crashed!" |
|---|
| 229 | exit 1 |
|---|
| 230 | fi |
|---|
| 231 | # Copy data files and prepare the next run |
|---|
| 232 | mv out_runPCM${iPCM} out_PCM/run${iPCM} |
|---|
| 233 | mv diagfi.nc diags/diagfi${iPCM}.nc |
|---|
| 234 | if [ -f "diagsoil.nc" ]; then |
|---|
| 235 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
|---|
| 236 | fi |
|---|
| 237 | if [ -f "stats.nc" ]; then |
|---|
| 238 | mv stats.nc diags/stats${iPCM}.nc |
|---|
| 239 | fi |
|---|
| 240 | k=$(echo "$i + 2 - $nPCM" | bc -l) |
|---|
| 241 | if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM |
|---|
| 242 | mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
|---|
| 243 | else |
|---|
| 244 | cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
|---|
| 245 | mv Xdiurnalave.nc data2reshape_Y${k}.nc |
|---|
| 246 | fi |
|---|
| 247 | cp restartfi.nc starts/startfi${iPCM}.nc |
|---|
| 248 | mv restartfi.nc startfi.nc |
|---|
| 249 | if [ -f "restart.nc" ]; then |
|---|
| 250 | cp restart.nc starts/restart${iPCM}.nc |
|---|
| 251 | mv restart.nc start.nc |
|---|
| 252 | elif [ -f "restart1D.txt" ]; then |
|---|
| 253 | cp restart1D.txt starts/restart1D${iPCM}.txt |
|---|
| 254 | mv restart1D.txt start1D.txt |
|---|
| 255 | fi |
|---|
| 256 | ((iPCM++)) |
|---|
| 257 | ((i_myear++)) |
|---|
| 258 | echo "Done!" |
|---|
| 259 | done |
|---|
| 260 | sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt |
|---|
| 261 | done |
|---|
| 262 | |
|---|
| 263 | # Restore the previous value of LC_NUMERIC |
|---|
| 264 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 265 | |
|---|
| 266 | date |
|---|
| 267 | echo "The launching script ended." |
|---|