source: trunk/LMDZ.MARS/deftank/launch_orb_1Dchained.sh @ 3580

Last change on this file since 3580 was 3580, checked in by jbclement, 8 hours ago

Mars PCM:
Improvement of Bash scripts for the 1D in the deftank with an automatic error detection which ends the script with a message.
JBC

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