| 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=1000000 |
|---|
| 28 | #n_earth_years=1000000 |
|---|
| 29 | |
|---|
| 30 | #---------------- Modify here the number of PCM calls ------------------ |
|---|
| 31 | ## set the number of PCM calls between each PEM call: |
|---|
| 32 | nPCM=2 |
|---|
| 33 | |
|---|
| 34 | #------------------ Modify here the name of PEM exe -------------------- |
|---|
| 35 | ## fill in the name of executable for PEM: |
|---|
| 36 | exePEM="pem_29_phymars_seq.e" |
|---|
| 37 | |
|---|
| 38 | #-------------- Modify here the name of reshape XIOS exe --------------- |
|---|
| 39 | ## fill in the name of executable for reshape XIOS: |
|---|
| 40 | exeReshape="reshape_XIOS_output_64x48x29_phymars_seq.e" |
|---|
| 41 | ##################################################################### |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | #------ Check if files/directories necessary for the script exist ------ |
|---|
| 45 | dir=`pwd` |
|---|
| 46 | machine=`hostname` |
|---|
| 47 | address=`whoami` |
|---|
| 48 | if [ ! -f "$exePEM" ]; then |
|---|
| 49 | echo "Error: file \"$exePEM\" does not exist in $dir!" |
|---|
| 50 | exit 1 |
|---|
| 51 | fi |
|---|
| 52 | if [ ! -f "$exeReshape" ]; then |
|---|
| 53 | echo "Error: file \"$exeReshape\" does not exist in $dir!" |
|---|
| 54 | exit 1 |
|---|
| 55 | fi |
|---|
| 56 | if [ ! -f "exePCM.sh" ]; then |
|---|
| 57 | echo "Error: file \"exePCM.sh\" does not exist in $dir!" |
|---|
| 58 | exit 1 |
|---|
| 59 | fi |
|---|
| 60 | if [ ! -f "run_PEM.def" ]; then |
|---|
| 61 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
|---|
| 62 | exit 1 |
|---|
| 63 | fi |
|---|
| 64 | if [ ! -f "run_PCM.def" ]; then |
|---|
| 65 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
|---|
| 66 | exit 1 |
|---|
| 67 | fi |
|---|
| 68 | if [ ! -f "context_lmdz_physics.xml" ]; then |
|---|
| 69 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
|---|
| 70 | exit 1 |
|---|
| 71 | fi |
|---|
| 72 | if [ ! -f "field_def_physics_mars.xml" ]; then |
|---|
| 73 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 74 | exit 1 |
|---|
| 75 | fi |
|---|
| 76 | if [ ! -f "file_def_physics_mars.xml" ]; then |
|---|
| 77 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
|---|
| 78 | exit 1 |
|---|
| 79 | fi |
|---|
| 80 | if [ ! -f "iodef.xml" ]; then |
|---|
| 81 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
|---|
| 82 | exit 1 |
|---|
| 83 | fi |
|---|
| 84 | if [ ! -d "out_PCM" ]; then |
|---|
| 85 | mkdir out_PCM |
|---|
| 86 | fi |
|---|
| 87 | if [ ! -d "out_PEM" ]; then |
|---|
| 88 | mkdir out_PEM |
|---|
| 89 | fi |
|---|
| 90 | if [ ! -d "starts" ]; then |
|---|
| 91 | mkdir starts |
|---|
| 92 | fi |
|---|
| 93 | if [ ! -d "diags" ]; then |
|---|
| 94 | mkdir diags |
|---|
| 95 | fi |
|---|
| 96 | |
|---|
| 97 | #---------------------------- Initialization --------------------------- |
|---|
| 98 | dir=`pwd` |
|---|
| 99 | machine=`hostname` |
|---|
| 100 | address=`whoami` |
|---|
| 101 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine." |
|---|
| 102 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 103 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 104 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 105 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 106 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| 107 | n_myear=$n_mars_years |
|---|
| 108 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 109 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 110 | n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years |
|---|
| 111 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 112 | else |
|---|
| 113 | echo "No number of years to be simulated has been set!" |
|---|
| 114 | exit 1 |
|---|
| 115 | fi |
|---|
| 116 | i_myear=0 |
|---|
| 117 | iPEM=1 |
|---|
| 118 | ((iPCM = ($iPEM - 1)*$nPCM + 1)) |
|---|
| 119 | cp startfi.nc starts/ |
|---|
| 120 | if [ -f "start.nc" ]; then |
|---|
| 121 | cp start.nc starts/ |
|---|
| 122 | elif [ -f "star1D.nc" ]; then |
|---|
| 123 | cp star1D.txt starts/ |
|---|
| 124 | fi |
|---|
| 125 | |
|---|
| 126 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
|---|
| 127 | if [ -f "info_PEM.txt" ]; then |
|---|
| 128 | rm info_PEM.txt |
|---|
| 129 | fi |
|---|
| 130 | echo $i_myear $n_myear $convert_years > info_PEM.txt |
|---|
| 131 | |
|---|
| 132 | #---------------------- Main loop to call PEM/PCM ---------------------- |
|---|
| 133 | while [ $i_myear -lt $n_myear ]; do |
|---|
| 134 | #--- Loop to run PCM year by year |
|---|
| 135 | cp run_PCM.def run.def |
|---|
| 136 | for ((i = 1; i <= $nPCM; i++)); do |
|---|
| 137 | echo "Run PCM $iPCM: call $i/$nPCM..." |
|---|
| 138 | sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh |
|---|
| 139 | sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh |
|---|
| 140 | sbatch -W exePCM.sh |
|---|
| 141 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
|---|
| 142 | echo "Error: the run PCM $iPCM has crashed!" |
|---|
| 143 | exit 1 |
|---|
| 144 | fi |
|---|
| 145 | # Copy data files and prepare the next run |
|---|
| 146 | mv out_runPCM${iPCM} out_PCM/run${iPCM} |
|---|
| 147 | mv diagfi.nc diags/diagfi${iPCM}.nc |
|---|
| 148 | if [ -f "diagsoil.nc" ]; then |
|---|
| 149 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
|---|
| 150 | fi |
|---|
| 151 | if [ -f "stats.nc" ]; then |
|---|
| 152 | mv stats.nc diags/stats${iPCM}.nc |
|---|
| 153 | fi |
|---|
| 154 | cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
|---|
| 155 | mv Xdiurnalave.nc data2reshape${i}.nc |
|---|
| 156 | cp restartfi.nc starts/startfi${iPCM}.nc |
|---|
| 157 | mv restartfi.nc startfi.nc |
|---|
| 158 | if [ -f "restart.nc" ]; then |
|---|
| 159 | cp restart.nc starts/restart${iPCM}.nc |
|---|
| 160 | mv restart.nc start.nc |
|---|
| 161 | elif [ -f "restart1D.txt" ]; then |
|---|
| 162 | cp restart1D.txt starts/restart1D${iPCM}.txt |
|---|
| 163 | mv restart1D.txt start1D.txt |
|---|
| 164 | fi |
|---|
| 165 | ((iPCM++)) |
|---|
| 166 | ((i_myear++)) |
|---|
| 167 | echo "Done!" |
|---|
| 168 | done |
|---|
| 169 | sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt |
|---|
| 170 | |
|---|
| 171 | #--- Reshaping PCM data with XIOS |
|---|
| 172 | echo "Reshaping PCM data with XIOS..." |
|---|
| 173 | ./$exeReshape |
|---|
| 174 | echo "Done!" |
|---|
| 175 | |
|---|
| 176 | #--- Running PEM |
|---|
| 177 | echo "Run PEM $iPEM..." |
|---|
| 178 | cp run_PEM.def run.def |
|---|
| 179 | mv startfi.nc startfi_evol.nc |
|---|
| 180 | if [ -f "start.nc" ]; then |
|---|
| 181 | mv start.nc start_evol.nc |
|---|
| 182 | elif [ -f "start1D.txt" ]; then |
|---|
| 183 | mv start1D.txt start1D_evol.txt |
|---|
| 184 | fi |
|---|
| 185 | ./$exePEM > out_runPEM${iPEM} 2>&1 |
|---|
| 186 | if [ ! -f "restartfi_evol.nc" ]; then # Check if run ended abnormally |
|---|
| 187 | echo "Error: the run PEM $iPEM has crashed!" |
|---|
| 188 | exit 1 |
|---|
| 189 | fi |
|---|
| 190 | # Copy data files and prepare the next run |
|---|
| 191 | mv out_runPEM${iPEM} out_PEM/run${iPEM} |
|---|
| 192 | mv diagpem.nc diags/diagpem${iPEM}.nc |
|---|
| 193 | mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc |
|---|
| 194 | cp restartpem.nc starts/startpem${iPEM}.nc |
|---|
| 195 | mv restartpem.nc startpem.nc |
|---|
| 196 | cp restartfi_evol.nc starts/startfi_postPEM${iPEM}.nc |
|---|
| 197 | mv restartfi_evol.nc startfi.nc |
|---|
| 198 | if [ -f "restart_evol.nc" ]; then |
|---|
| 199 | cp restart_evol.nc starts/restart_postPEM${iPEM}.nc |
|---|
| 200 | mv restart_evol.nc start.nc |
|---|
| 201 | elif [ -f "restart1D_evol.txt" ]; then |
|---|
| 202 | cp restart1D_evol.txt starts/restart1D_postPEM${iPEM}.txt |
|---|
| 203 | mv restart1D_evol.txt start1D.txt |
|---|
| 204 | fi |
|---|
| 205 | ((iPEM++)) |
|---|
| 206 | read i_myear n_myear convert_years < info_PEM.txt |
|---|
| 207 | echo "Done!" |
|---|
| 208 | done |
|---|
| 209 | |
|---|
| 210 | # Restore the previous value of LC_NUMERIC |
|---|
| 211 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 212 | |
|---|
| 213 | date |
|---|
| 214 | echo "The launching script has terminated." |
|---|