| 1 | #!/bin/bash |
|---|
| 2 | ################################################################ |
|---|
| 3 | ### Launching script for a chained simulation of 1D PCM runs ### |
|---|
| 4 | ### following orbital changes ### |
|---|
| 5 | ################################################################ |
|---|
| 6 | set -e |
|---|
| 7 | trap 'echo -e "\033[31mError: an issue occurred in the script on line $LINENO! Please review the command and try again.\033[0m"' ERR |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | ################################################################ |
|---|
| 11 | # Modify here the parameters for the simulation |
|---|
| 12 | ############################################### |
|---|
| 13 | # Path to the arch.env to source: |
|---|
| 14 | source ../trunk/LMDZ.COMMON/arch.env |
|---|
| 15 | |
|---|
| 16 | # Set the number of Earth years before present to start: |
|---|
| 17 | year_earth_bp_ini=-100 |
|---|
| 18 | |
|---|
| 19 | # Set the number of years to be simulated, either Martian or Earth years (> 0): |
|---|
| 20 | n_mars_years=100. |
|---|
| 21 | #n_earth_years=300 |
|---|
| 22 | |
|---|
| 23 | # Name of executable for the PCM: |
|---|
| 24 | exePCM="testphys1d_32_phymars_seq.e" |
|---|
| 25 | |
|---|
| 26 | # Name of the orbital data file: |
|---|
| 27 | orb_data="obl_ecc_lsp.asc" |
|---|
| 28 | ################################################################ |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | echo "The launching script is starting!" |
|---|
| 32 | echo "The output file is \"launch.log\"." |
|---|
| 33 | exec > launch.log 2>&1 |
|---|
| 34 | |
|---|
| 35 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
|---|
| 36 | OLD_LC_NUMERIC=$LC_NUMERIC |
|---|
| 37 | LC_NUMERIC=en_US.UTF-8 |
|---|
| 38 | |
|---|
| 39 | # To end the launching script with error |
|---|
| 40 | errlaunch() { |
|---|
| 41 | # Restore the previous value of LC_NUMERIC |
|---|
| 42 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 43 | |
|---|
| 44 | date |
|---|
| 45 | echo "Error: an issue occured in the launching script!" |
|---|
| 46 | exit 1 |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | # To convert Earth years into Mars years |
|---|
| 50 | convertyears() { |
|---|
| 51 | myear=686.9725 # Number of Earth days in Martian year |
|---|
| 52 | eyear=365.256363004 # Number of days in Earth year |
|---|
| 53 | convert_years=$(echo "$myear/$eyear" | bc -l) |
|---|
| 54 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
|---|
| 55 | if [ -v n_mars_years ]; then |
|---|
| 56 | n_myear=$n_mars_years |
|---|
| 57 | echo "Number of years to be simulated: $n_myear Martian years." |
|---|
| 58 | elif [ -v n_earth_years ]; then |
|---|
| 59 | n_myear=$(echo "$n_earth_years/$convert_years" | bc -l) |
|---|
| 60 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
|---|
| 61 | fi |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | # Check if the set-up is correct |
|---|
| 65 | dir=`pwd` |
|---|
| 66 | machine=`hostname` |
|---|
| 67 | address=`whoami` |
|---|
| 68 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
|---|
| 69 | if [ $(echo "$n_mars_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 70 | echo "Error: 'n_mars_years' must be > 0!" |
|---|
| 71 | errlaunch |
|---|
| 72 | fi |
|---|
| 73 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
|---|
| 74 | if [ $(echo "$n_earth_years <= 0." | bc -l) -eq 1 ]; then |
|---|
| 75 | echo "Error: 'n_earth_years' must be > 0!" |
|---|
| 76 | errlaunch |
|---|
| 77 | fi |
|---|
| 78 | else |
|---|
| 79 | echo "Error: the number of years to be simulated is not set!" |
|---|
| 80 | errlaunch |
|---|
| 81 | fi |
|---|
| 82 | if [ ! -f "$exePCM" ]; then |
|---|
| 83 | echo "Error: file \"$exePCM\" does not exist in $dir!" |
|---|
| 84 | errlaunch |
|---|
| 85 | fi |
|---|
| 86 | if [ ! -f "$orb_data" ]; then |
|---|
| 87 | echo "Error: file \"$orb_data\" does not exist in $dir!" |
|---|
| 88 | errlaunch |
|---|
| 89 | fi |
|---|
| 90 | if [ ! -f "startfi.nc" ]; then |
|---|
| 91 | echo "Error: file \"startfi.nc\" does not exist in $dir!" |
|---|
| 92 | errlaunch |
|---|
| 93 | fi |
|---|
| 94 | if [ ! -d "logs" ]; then |
|---|
| 95 | mkdir logs |
|---|
| 96 | fi |
|---|
| 97 | if [ ! -d "starts" ]; then |
|---|
| 98 | mkdir starts |
|---|
| 99 | fi |
|---|
| 100 | if [ ! -d "diags" ]; then |
|---|
| 101 | mkdir diags |
|---|
| 102 | fi |
|---|
| 103 | |
|---|
| 104 | # Initialization |
|---|
| 105 | dir=`pwd` |
|---|
| 106 | machine=`hostname` |
|---|
| 107 | address=`whoami` |
|---|
| 108 | echo "This is a chained simulation for PCM 1D runs in $dir on $machine." |
|---|
| 109 | convertyears |
|---|
| 110 | i_myear=0 |
|---|
| 111 | iPCM=1 |
|---|
| 112 | if [ -f "startfi.nc" ]; then |
|---|
| 113 | cp startfi.nc starts/ |
|---|
| 114 | fi |
|---|
| 115 | if [ -f "start1D.txt" ]; then |
|---|
| 116 | cp start1D.txt starts/ |
|---|
| 117 | fi |
|---|
| 118 | |
|---|
| 119 | # To modify orbital parameters of a file "startfi.nc" |
|---|
| 120 | modify_startfi_orb() { |
|---|
| 121 | # Get the new values for the orbital parameters |
|---|
| 122 | yearlask=$(echo "($year_earth_bp_ini + $i_myear*$convert_years)/1000." | bc -l) |
|---|
| 123 | found=false |
|---|
| 124 | read -r y1 obl1 ecc1 lsp1 < "$orb_data" |
|---|
| 125 | while read -r y2 obl2 ecc2 lsp2; do |
|---|
| 126 | if [ "$first_line" = true ]; then |
|---|
| 127 | first_line=false |
|---|
| 128 | continue # Skip the first line as it's already read outside the loop |
|---|
| 129 | fi |
|---|
| 130 | if [ "$(echo "$y1 >= $yearlask && $yearlask > $y2" | bc)" -eq 1 ]; then |
|---|
| 131 | found=true |
|---|
| 132 | # Calculate linear interpolations for each orbital parameter |
|---|
| 133 | new_obl=$(echo "($obl2 - $obl1)/($y2 - $y1)*($yearlask - $y1) + $obl1" | bc -l) |
|---|
| 134 | new_ecc=$(echo "($ecc2 - $ecc1)/($y2 - $y1)*($yearlask - $y1) + $ecc1" | bc -l) |
|---|
| 135 | if [ "$(echo "$lsp2 - $lsp1 > 300. || $lsp2 - $lsp1 < -300." | bc)" -eq 1 ]; then # If modulo is "crossed" through the interval |
|---|
| 136 | if [ "$(echo "$lsp1 < $lsp2" | bc -l)" -eq 1 ]; then # Lsp should be decreasing |
|---|
| 137 | lsp1=$(echo "$lsp1 + 360" | bc -l) |
|---|
| 138 | else # Lsp should be increasing |
|---|
| 139 | lsp2=$(echo "$lsp2 + 360" | bc -l) |
|---|
| 140 | fi |
|---|
| 141 | fi |
|---|
| 142 | new_Lsp=$(echo "($lsp2 - $lsp1)/($y2 - $y1)*($yearlask - $y1) + $lsp1" | bc -l) |
|---|
| 143 | echo "New obliquity = $new_obl" |
|---|
| 144 | echo "New eccentricity = $new_ecc" |
|---|
| 145 | echo "New Lsp = $new_Lsp" |
|---|
| 146 | echo "Done!" |
|---|
| 147 | break |
|---|
| 148 | fi |
|---|
| 149 | y1=$y2 |
|---|
| 150 | obl1=$obl2 |
|---|
| 151 | ecc1=$ecc2 |
|---|
| 152 | lsp1=$lsp2 |
|---|
| 153 | done < "$orb_data" |
|---|
| 154 | if [ "$found" = false ]; then |
|---|
| 155 | echo "Error: the current year could not be found in the file \"$orb_data\"!" |
|---|
| 156 | exit 1 |
|---|
| 157 | fi |
|---|
| 158 | # Compute the modified values in the "starfi.nc" |
|---|
| 159 | year_day=669. |
|---|
| 160 | halfaxe=227.94 |
|---|
| 161 | pi=$(echo "4.*a(1.)" | bc -l) |
|---|
| 162 | degrad=$(echo "180./$pi" | bc -l) |
|---|
| 163 | periheli=$(echo "$halfaxe*(1. - $new_ecc)" | bc -l) |
|---|
| 164 | aphelie=$(echo "$halfaxe*(1. + $new_ecc)" | bc -l) |
|---|
| 165 | tan=$(echo "s(0.5*$new_Lsp/$degrad)/c(0.5*$new_Lsp/$degrad)" | bc -l) |
|---|
| 166 | zx0=$(echo "-2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l) |
|---|
| 167 | if [ $(echo "$zx0 <= 0." | bc -l) -eq 1 ]; then |
|---|
| 168 | zx0=$(echo "$zx0 + 2.*$pi" | bc -l) |
|---|
| 169 | fi |
|---|
| 170 | peri_day=$(echo "$year_day*(1. - ($zx0 - $new_ecc*s($zx0))/(2.*$pi))" | bc -l) |
|---|
| 171 | ztheta=$(echo "($new_iniLs - $new_Lsp)/$degrad" | bc -l) |
|---|
| 172 | tan=$(echo "s(0.5*$ztheta)/c(0.5*$ztheta)" | bc -l) |
|---|
| 173 | zx0=$(echo "2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l) |
|---|
| 174 | xref=$(echo "$zx0 - $new_ecc*s($zx0)" | bc -l) |
|---|
| 175 | # Modify the orbital parameters |
|---|
| 176 | # controle(15) = periheli ! min. Sun-Mars distance (Mkm) ̃206.66 |
|---|
| 177 | # controle(16) = aphelie ! max. Sun-Mars distance (Mkm) ̃249.22 |
|---|
| 178 | # controle(17) = peri_day ! date of perihelion (sols since N. spring) |
|---|
| 179 | # controle(18) = obliquit ! Obliquity of the planet (deg) ̃23.98 |
|---|
| 180 | ncap2 -O -s "controle(17)=$new_obl" \ |
|---|
| 181 | -s "controle(14)=$periheli" \ |
|---|
| 182 | -s "controle(15)=$aphelie" \ |
|---|
| 183 | -s "controle(16)=$peri_day" \ |
|---|
| 184 | "startfi.nc" "startfi.nc" |
|---|
| 185 | echo "New periheli = $periheli" |
|---|
| 186 | echo "New aphelie = $aphelie" |
|---|
| 187 | echo "New peri_day = $peri_day" |
|---|
| 188 | echo "Done!" |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | # Main loop to to run PCM year by year |
|---|
| 193 | while [ $i_myear -lt $n_myear ]; do |
|---|
| 194 | # Run the PCM |
|---|
| 195 | echo "Run $iPCM: year $iPCM/$n_mars_years..." |
|---|
| 196 | ./$exePCM > run.log 2>&1 |
|---|
| 197 | if [ ! -f "restartfi.nc" ] || ! (tail -n 100 run.log | grep -iq "everything is cool!"); then # Check if it ended abnormally |
|---|
| 198 | echo "Error: the run $iPCM crashed!" |
|---|
| 199 | errlaunch |
|---|
| 200 | fi |
|---|
| 201 | # Copy data files and prepare the next run |
|---|
| 202 | mv run.log logs/run${iPCM}.log |
|---|
| 203 | if [ -f "diagfi.nc" ]; then |
|---|
| 204 | mv diagfi.nc diags/diagfi${iPCM}.nc |
|---|
| 205 | fi |
|---|
| 206 | if [ -f "diagsoil.nc" ]; then |
|---|
| 207 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
|---|
| 208 | fi |
|---|
| 209 | if [ -f "stats.nc" ]; then |
|---|
| 210 | mv stats.nc diags/stats${iPCM}.nc |
|---|
| 211 | fi |
|---|
| 212 | if [ -f "Xdiurnalave.nc" ]; then |
|---|
| 213 | mv Xdiurnalave.nc diags/Xdiurnalave${iPCM}.nc |
|---|
| 214 | fi |
|---|
| 215 | cp restartfi.nc starts/startfi${iPCM}.nc |
|---|
| 216 | mv restartfi.nc startfi.nc |
|---|
| 217 | if [ -f "restart1D.txt" ]; then |
|---|
| 218 | cp restart1D.txt starts/restart1D${iPCM}.txt |
|---|
| 219 | mv restart1D.txt start1D.txt |
|---|
| 220 | fi |
|---|
| 221 | ((iPCM++)) |
|---|
| 222 | ((i_myear++)) |
|---|
| 223 | echo "Done!" |
|---|
| 224 | done |
|---|
| 225 | |
|---|
| 226 | # Restore the previous value of LC_NUMERIC |
|---|
| 227 | LC_NUMERIC=$OLD_LC_NUMERIC |
|---|
| 228 | |
|---|
| 229 | date |
|---|
| 230 | echo "Success: the launching script completed normally!" |
|---|
| 231 | exit 0 |
|---|