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