[3145] | 1 | #!/bin/bash |
---|
| 2 | ################################################################ |
---|
| 3 | ### Launching script for a chained simulation of 1D PCM runs ### |
---|
[3209] | 4 | ### following orbital changes ### |
---|
[3145] | 5 | ################################################################ |
---|
| 6 | |
---|
| 7 | echo "The launching script is starting!" |
---|
| 8 | echo "The output file is \"loglaunch.txt\"." |
---|
| 9 | if [ "$1" = "bg" ]; then |
---|
| 10 | date |
---|
| 11 | else |
---|
| 12 | nohup "$0" bg > loglaunch.txt 2>&1 & |
---|
| 13 | exit 1 |
---|
| 14 | fi |
---|
| 15 | |
---|
| 16 | # A few parameters that might need be changed depending on your setup: |
---|
| 17 | # Path to the arch.env to source: |
---|
| 18 | source ../trunk/LMDZ.COMMON/arch.env |
---|
| 19 | |
---|
| 20 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
---|
| 21 | OLD_LC_NUMERIC=$LC_NUMERIC |
---|
| 22 | LC_NUMERIC=en_US.UTF-8 |
---|
| 23 | |
---|
[3209] | 24 | |
---|
| 25 | ################################################################ |
---|
[3145] | 26 | #--------- Modify here the number of years for initialization ---------- |
---|
| 27 | ## set the number of Earth years before present to start: |
---|
| 28 | eyears_bp_ini=-1000 |
---|
| 29 | |
---|
| 30 | #---------- Modify here the number of years to be simulated ------------ |
---|
| 31 | ## set the number of Martian years: |
---|
| 32 | n_myears=500 |
---|
| 33 | |
---|
| 34 | #------------------ Modify here the name of PCM exe -------------------- |
---|
| 35 | ## fill in the name of executable for PCM: |
---|
[3181] | 36 | exePCM="testphys1d_29_phymars_para.e" |
---|
[3145] | 37 | |
---|
| 38 | #----------- Modify here the name of the orbital data file ------------- |
---|
| 39 | ## fill in the name of executable for PCM: |
---|
[3181] | 40 | orb_data="obl_ecc_lsp.asc" |
---|
[3209] | 41 | ################################################################ |
---|
[3145] | 42 | |
---|
[3209] | 43 | |
---|
[3145] | 44 | #------ Check if files/directories necessary for the script exist ------ |
---|
| 45 | dir=`pwd` |
---|
| 46 | machine=`hostname` |
---|
| 47 | address=`whoami` |
---|
| 48 | if [ ! -f "$exePCM" ]; then |
---|
| 49 | echo "Error: file \"$exePCM\" does not exist in $dir!" |
---|
| 50 | exit 1 |
---|
| 51 | fi |
---|
| 52 | if [ ! -f "$orb_data" ]; then |
---|
| 53 | echo "Error: file \"$orb_data\" does not exist in $dir!" |
---|
| 54 | exit 1 |
---|
| 55 | fi |
---|
| 56 | if [ ! -f "startfi.nc" ]; then |
---|
| 57 | echo "Error: file \"startfi.nc\" does not exist in $dir!" |
---|
| 58 | exit 1 |
---|
| 59 | fi |
---|
| 60 | if [ ! -d "out_PCM" ]; then |
---|
| 61 | mkdir out_PCM |
---|
| 62 | fi |
---|
| 63 | if [ ! -d "starts" ]; then |
---|
| 64 | mkdir starts |
---|
| 65 | fi |
---|
| 66 | if [ ! -d "diags" ]; then |
---|
| 67 | mkdir diags |
---|
| 68 | fi |
---|
| 69 | |
---|
| 70 | #---------------------------- Initialization --------------------------- |
---|
| 71 | dir=`pwd` |
---|
| 72 | machine=`hostname` |
---|
| 73 | address=`whoami` |
---|
| 74 | echo "This is a chained simulation for PCM runs in $dir on $machine." |
---|
| 75 | echo "Number of years to be simulated: $n_myears Martian years." |
---|
| 76 | myear=686.9725 # Number of Earth days in Martian year |
---|
| 77 | eyear=365.256363004 # Number of days in Earth year |
---|
| 78 | convert_years=$(echo "$myear/$eyear" | bc -l) |
---|
| 79 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
---|
| 80 | i_myear=0 |
---|
| 81 | iPCM=1 |
---|
| 82 | cp startfi.nc starts/ |
---|
| 83 | if [ -f "star1D.nc" ]; then |
---|
| 84 | cp star1D.txt starts/ |
---|
| 85 | fi |
---|
| 86 | |
---|
| 87 | #---------------- Main loop to to run PCM year by year ----------------- |
---|
| 88 | while [ $i_myear -lt $n_myears ]; do |
---|
| 89 | # Get the new values for the orbital parameters |
---|
[3350] | 90 | yearlask=$(echo "($eyears_bp_ini + $i_myear*$convert_years)/1000." | bc -l) |
---|
[3145] | 91 | found=false |
---|
[3173] | 92 | read -r y1 obl1 ecc1 lsp1 < "$orb_data" |
---|
| 93 | while read -r y2 obl2 ecc2 lsp2; do |
---|
| 94 | if [ "$first_line" = true ]; then |
---|
| 95 | first_line=false |
---|
| 96 | continue # Skip the first line as it's already read outside the loop |
---|
| 97 | fi |
---|
[3145] | 98 | if [ "$(echo "$y1 >= $yearlask && $yearlask > $y2" | bc)" -eq 1 ]; then |
---|
| 99 | found=true |
---|
| 100 | # Calculate linear interpolations for each orbital parameter |
---|
| 101 | new_obl=$(echo "($obl2 - $obl1)/($y2 - $y1)*($yearlask - $y1) + $obl1" | bc -l) |
---|
| 102 | new_ecc=$(echo "($ecc2 - $ecc1)/($y2 - $y1)*($yearlask - $y1) + $ecc1" | bc -l) |
---|
| 103 | if [ "$(echo "$lsp2 - $lsp1 > 300. || $lsp2 - $lsp1 < -300." | bc)" -eq 1 ]; then # If modulo is "crossed" through the interval |
---|
| 104 | if [ "$(echo "$lsp1 < $lsp2" | bc -l)" -eq 1 ]; then # Lsp should be decreasing |
---|
| 105 | lsp1=$(echo "$lsp1 + 360" | bc -l) |
---|
| 106 | else # Lsp should be increasing |
---|
| 107 | lsp2=$(echo "$lsp2 + 360" | bc -l) |
---|
| 108 | fi |
---|
| 109 | fi |
---|
| 110 | new_Lsp=$(echo "($lsp2 - $lsp1)/($y2 - $y1)*($yearlask - $y1) + $lsp1" | bc -l) |
---|
| 111 | echo "New obliquity = $new_obl" |
---|
| 112 | echo "New eccentricity = $new_ecc" |
---|
| 113 | echo "New Lsp = $new_Lsp" |
---|
| 114 | echo "Done!" |
---|
| 115 | break |
---|
| 116 | fi |
---|
[3173] | 117 | y1=$y2 |
---|
| 118 | obl1=$obl2 |
---|
| 119 | ecc1=$ecc2 |
---|
| 120 | lsp1=$lsp2 |
---|
[3145] | 121 | done < "$orb_data" |
---|
| 122 | if [ "$found" = false ]; then |
---|
| 123 | echo "Error: the current year could not be found in the file \"$orb_data\"!" |
---|
| 124 | exit 1 |
---|
| 125 | fi |
---|
| 126 | # Compute the modified values in the "starfi.nc" |
---|
| 127 | year_day=669. |
---|
| 128 | halfaxe=227.94 |
---|
| 129 | pi=$(echo "4.*a(1.)" | bc -l) |
---|
| 130 | degrad=$(echo "180./$pi" | bc -l) |
---|
| 131 | periheli=$(echo "$halfaxe*(1. - $new_ecc)" | bc -l) |
---|
| 132 | aphelie=$(echo "$halfaxe*(1. + $new_ecc)" | bc -l) |
---|
| 133 | tan=$(echo "s(0.5*$new_Lsp/$degrad)/c(0.5*$new_Lsp/$degrad)" | bc -l) |
---|
| 134 | zx0=$(echo "-2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l) |
---|
| 135 | if [ $(echo "$zx0 <= 0." | bc -l) -eq 1 ]; then |
---|
| 136 | zx0=$(echo "$zx0 + 2.*$pi" | bc -l) |
---|
| 137 | fi |
---|
| 138 | peri_day=$(echo "$year_day*(1. - ($zx0 - $new_ecc*s($zx0))/(2.*$pi))" | bc -l) |
---|
| 139 | ztheta=$(echo "($new_iniLs - $new_Lsp)/$degrad" | bc -l) |
---|
| 140 | tan=$(echo "s(0.5*$ztheta)/c(0.5*$ztheta)" | bc -l) |
---|
| 141 | zx0=$(echo "2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l) |
---|
| 142 | xref=$(echo "$zx0 - $new_ecc*s($zx0)" | bc -l) |
---|
| 143 | # Modify the orbital parameters |
---|
| 144 | # controle(15) = periheli ! min. Sun-Mars distance (Mkm) ̃206.66 |
---|
[3209] | 145 | # controle(16) = aphelie ! max. Sun-Mars distance (Mkm) ̃249.22 |
---|
[3145] | 146 | # controle(17) = peri_day ! date of perihelion (sols since N. spring) |
---|
| 147 | # controle(18) = obliquit ! Obliquity of the planet (deg) ̃23.98 |
---|
| 148 | ncap2 -O -s "controle(17)=$new_obl" \ |
---|
| 149 | -s "controle(14)=$periheli" \ |
---|
| 150 | -s "controle(15)=$aphelie" \ |
---|
| 151 | -s "controle(16)=$peri_day" \ |
---|
| 152 | "startfi.nc" "startfi.nc" |
---|
| 153 | echo "New periheli = $periheli" |
---|
| 154 | echo "New aphelie = $aphelie" |
---|
| 155 | echo "New peri_day = $peri_day" |
---|
| 156 | echo "Done!" |
---|
| 157 | # Run the PCM |
---|
| 158 | echo "Run PCM $iPCM: year $iPCM/$n_myears..." |
---|
| 159 | ./$exePCM > out_runPCM${iPCM} 2>&1 |
---|
| 160 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
---|
[3213] | 161 | echo "Error: the run PCM $iPCM crashed!" |
---|
[3145] | 162 | exit 1 |
---|
| 163 | fi |
---|
| 164 | # Copy data files and prepare the next run |
---|
| 165 | mv out_runPCM${iPCM} out_PCM/run${iPCM} |
---|
| 166 | mv diagfi.nc diags/diagfi${iPCM}.nc |
---|
| 167 | if [ -f "diagsoil.nc" ]; then |
---|
| 168 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
---|
| 169 | fi |
---|
| 170 | if [ -f "stats.nc" ]; then |
---|
| 171 | mv stats.nc diags/stats${iPCM}.nc |
---|
| 172 | fi |
---|
| 173 | if [ -f "Xdiurnalave.nc" ]; then |
---|
| 174 | mv Xdiurnalave.nc diags/Xdiurnalave${iPCM}.nc |
---|
| 175 | fi |
---|
| 176 | cp restartfi.nc starts/startfi${iPCM}.nc |
---|
| 177 | mv restartfi.nc startfi.nc |
---|
| 178 | if [ -f "restart1D.txt" ]; then |
---|
| 179 | cp restart1D.txt starts/restart1D${iPCM}.txt |
---|
| 180 | mv restart1D.txt start1D.txt |
---|
| 181 | fi |
---|
| 182 | ((iPCM++)) |
---|
| 183 | ((i_myear++)) |
---|
| 184 | echo "Done!" |
---|
| 185 | done |
---|
| 186 | |
---|
| 187 | # Restore the previous value of LC_NUMERIC |
---|
| 188 | LC_NUMERIC=$OLD_LC_NUMERIC |
---|
| 189 | |
---|
| 190 | date |
---|
[3213] | 191 | echo "The launching script ended." |
---|