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