source: trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh @ 3347

Last change on this file since 3347 was 3321, checked in by jbclement, 14 months ago

PEM:
Correction in the layering algorithm in case of a stratum which is disappearing + few cleanings.
JBC

  • Property svn:executable set to *
File size: 9.9 KB
Line 
1#!/bin/bash
2#####################################################################
3### Launching script for a chained simulation of PEM and PCM runs ###
4#####################################################################
5
6echo "The launching script is starting!"
7echo "The output file is \"loglaunch.txt\"."
8
9# Re-spawn the script as a background process and create a bash file to kill it in case
10if [ "$1" = "bg" ]; then 
11    date
12    echo '#!/bin/bash' > kill_launch_pem.sh
13    echo 'kill' $$ >> kill_launch_pem.sh
14    chmod +x kill_launch_pem.sh
15else
16    nohup "$0" bg > loglaunch.txt 2>&1 &
17    exit 1
18fi
19
20
21#####################################################################
22# A few parameters that might need be changed depending on your setup:
23# Path to the arch.env to source:
24source ../trunk/LMDZ.COMMON/arch.env
25
26#---------- Modify here the number of years to be simulated ------------
27## set the number of years, either Martian or Earth years:
28n_mars_years=1000
29#n_earth_years=300
30
31#------------- Modify here the number of initial PCM calls -------------
32## set the number of initial PCM calls between each PEM call:
33nPCM_ini=4
34
35#---------------- Modify here the number of PCM calls ------------------
36## set the number of PCM calls between each PEM call:
37nPCM=2
38
39#------------------ Modify here the name of PEM exe --------------------
40## fill in the name of executable for PEM:
41exePEM="pem_29_phymars_seq.e"
42
43#-------------- Modify here the name of reshape XIOS exe ---------------
44## fill in the name of executable for reshape XIOS:
45exeReshape="reshape_XIOS_output_64x48x29_phymars_seq.e"
46#####################################################################
47
48
49# Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator
50OLD_LC_NUMERIC=$LC_NUMERIC
51LC_NUMERIC=en_US.UTF-8
52
53#------ Check if files/directories necessary for the script exist ------
54dir=`pwd`
55machine=`hostname`
56address=`whoami`
57if [ ! -f "$exePEM" ]; then
58    echo "Error: file \"$exePEM\" does not exist in $dir!"
59    exit 1
60fi
61if [ ! -f "$exeReshape" ]; then
62    echo "Error: file \"$exeReshape\" does not exist in $dir!"
63    exit 1
64fi
65if [ ! -f "exePCM.sh" ]; then
66    echo "Error: file \"exePCM.sh\" does not exist in $dir!"
67    exit 1
68fi
69if [ ! -f "run_PEM.def" ]; then
70    echo "Error: file \"run_PEM.def\" does not exist in $dir!"
71    exit 1
72fi
73if [ ! -f "run_PCM.def" ]; then
74    echo "Error: file \"run_PCM.def\" does not exist in $dir!"
75    exit 1
76fi
77if [ ! -f "context_lmdz_physics.xml" ]; then
78    echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!"
79    exit 1
80fi
81if [ ! -f "field_def_physics_mars.xml" ]; then
82    echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!"
83    exit 1
84fi
85if [ ! -f "file_def_physics_mars.xml" ]; then
86    echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!"
87    exit 1
88fi
89if [ ! -f "iodef.xml" ]; then
90    echo "Error: file \"iodef.xml\" does not exist in $dir!"
91    exit 1
92fi
93if [ ! -d "out_PCM" ]; then
94    mkdir out_PCM
95fi
96if [ ! -d "out_PEM" ]; then
97    mkdir out_PEM
98fi
99if [ ! -d "starts" ]; then
100    mkdir starts
101fi
102if [ ! -d "diags" ]; then
103    mkdir diags
104fi
105
106#---------------------------- Initialization ---------------------------
107dir=`pwd`
108machine=`hostname`
109address=`whoami`
110echo "This is a chained simulation for PEM and PCM runs in $dir on $machine."
111myear=686.9725      # Number of Earth days in Martian year
112eyear=365.256363004 # Number of days in Earth year
113convert_years=$(echo "$myear/$eyear" | bc -l)
114convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year
115if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then
116    n_myear=$n_mars_years
117    echo "Number of years to be simulated: $n_myear Martian years."
118elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then
119    n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years
120    echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years."
121else
122    echo "No number of years to be simulated has been set!"
123    exit 1
124fi
125i_myear=0
126iPEM=1
127((iPCM = ($iPEM - 1)*$nPCM + 1))
128cp startfi.nc starts/
129if [ -f "start.nc" ]; then
130    cp start.nc starts/
131elif [ -f "star1D.nc" ]; then
132    cp star1D.txt starts/
133fi
134
135# Create a file to manage years of the chained simulation and store some info from the PEM runs
136if [ -f "info_PEM.txt" ]; then
137    rm info_PEM.txt
138fi
139echo $i_myear $n_myear $convert_years > info_PEM.txt
140
141#---------------------------- Initial runs -----------------------------
142#--- Initial PCM runs
143cp run_PCM.def run.def
144for ((i = 1; i <= $nPCM_ini; i++)); do
145    echo "Run PCM $iPCM (initial): call $i/$nPCM_ini..."
146    sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh
147    sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh
148    sbatch -W exePCM.sh
149    if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
150        echo "Error: the run PCM $iPCM crashed!"
151        exit 1
152    fi
153    # Copy data files and prepare the next run
154    mv out_runPCM${iPCM} out_PCM/run${iPCM}
155    mv diagfi.nc diags/diagfi${iPCM}.nc
156    if [ -f "diagsoil.nc" ]; then
157        mv diagsoil.nc diags/diagsoil${iPCM}.nc
158    fi
159    if [ -f "stats.nc" ]; then
160        mv stats.nc diags/stats${iPCM}.nc
161    fi
162    k=$(echo "$i + 2 - $nPCM_ini" | bc -l)
163    if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM
164        mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
165    else
166        cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc
167        mv Xdiurnalave.nc data2reshape_Y${k}.nc
168    fi
169    cp restartfi.nc starts/restartfi${iPCM}.nc
170    mv restartfi.nc startfi.nc
171    if [ -f "restart.nc" ]; then
172        cp restart.nc starts/restart${iPCM}.nc
173        mv restart.nc start.nc
174    elif [ -f "restart1D.txt" ]; then
175        cp restart1D.txt starts/restart1D${iPCM}.txt
176        mv restart1D.txt start1D.txt
177    fi
178    ((iPCM++))
179    ((i_myear++))
180    echo "Done!"
181done
182sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt
183
184#--- Reshaping PCM data with XIOS
185echo "Reshaping PCM data with XIOS..."
186./$exeReshape
187echo "Done!"
188
189#--- Running PEM
190echo "Run PEM $iPEM..."
191cp run_PEM.def run.def
192./$exePEM > out_runPEM${iPEM} 2>&1
193if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
194    echo "Error: the run PEM $iPEM crashed!"
195    exit 1
196fi
197# Copy data files and prepare the next run
198mv out_runPEM${iPEM} out_PEM/run${iPEM}
199mv diagpem.nc diags/diagpem${iPEM}.nc
200if [ -f "diagsoilpem.nc" ]; then
201    mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc
202fi
203cp restartpem.nc starts/restartpem${iPEM}.nc
204mv restartpem.nc startpem.nc
205cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc
206mv restartfi.nc startfi.nc
207if [ -f "restart.nc" ]; then
208    cp restart.nc starts/restart_postPEM${iPEM}.nc
209    mv restart.nc start.nc
210elif [ -f "restart1D.txt" ]; then
211    cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt
212    mv restart1D.txt start1D.txt
213fi
214((iPEM++))
215read i_myear n_myear convert_years < info_PEM.txt
216echo "Done!"
217
218#---------------------- Main loop to call PEM/PCM ----------------------
219while [ $i_myear -lt $n_myear ]; do
220    #--- Loop to run PCM year by year
221    cp run_PCM.def run.def
222    for ((i = 1; i <= $nPCM; i++)); do
223        echo "Run PCM $iPCM: call $i/$nPCM..."
224        sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh
225        sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh
226        sbatch -W exePCM.sh
227        if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
228            echo "Error: the run PCM $iPCM crashed!"
229            exit 1
230        fi
231        # Copy data files and prepare the next run
232        mv out_runPCM${iPCM} out_PCM/run${iPCM}
233        mv diagfi.nc diags/diagfi${iPCM}.nc
234        if [ -f "diagsoil.nc" ]; then
235            mv diagsoil.nc diags/diagsoil${iPCM}.nc
236        fi
237        if [ -f "stats.nc" ]; then
238            mv stats.nc diags/stats${iPCM}.nc
239        fi
240        k=$(echo "$i + 2 - $nPCM" | bc -l)
241        if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM
242            mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
243        else
244            cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc
245            mv Xdiurnalave.nc data2reshape_Y${k}.nc
246        fi
247        cp restartfi.nc starts/restartfi${iPCM}.nc
248        mv restartfi.nc startfi.nc
249        if [ -f "restart.nc" ]; then
250            cp restart.nc starts/restart${iPCM}.nc
251            mv restart.nc start.nc
252        elif [ -f "restart1D.txt" ]; then
253            cp restart1D.txt starts/restart1D${iPCM}.txt
254            mv restart1D.txt start1D.txt
255        fi
256        ((iPCM++))
257        ((i_myear++))
258        echo "Done!"
259    done
260    sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt
261
262    #--- Reshaping PCM data with XIOS
263    echo "Reshaping PCM data with XIOS..."
264    ./$exeReshape
265    echo "Done!"
266
267    #--- Running PEM
268    echo "Run PEM $iPEM..."
269    cp run_PEM.def run.def
270    ./$exePEM > out_runPEM${iPEM} 2>&1
271    if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
272        echo "Error: the run PEM $iPEM crashed!"
273        exit 1
274    fi
275    # Copy data files and prepare the next run
276    mv out_runPEM${iPEM} out_PEM/run${iPEM}
277    mv diagpem.nc diags/diagpem${iPEM}.nc
278    if [ -f "diagsoilpem.nc" ]; then
279        mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc
280    fi
281    cp restartpem.nc starts/restartpem${iPEM}.nc
282    mv restartpem.nc startpem.nc
283    cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc
284    mv restartfi.nc startfi.nc
285    if [ -f "restart.nc" ]; then
286        cp restart.nc starts/restart_postPEM${iPEM}.nc
287        mv restart.nc start.nc
288    elif [ -f "restart1D.txt" ]; then
289        cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt
290        mv restart1D.txt start1D.txt
291    fi
292    ((iPEM++))
293    read i_myear n_myear convert_years < info_PEM.txt
294    echo "Done!"
295done
296
297# Restore the previous value of LC_NUMERIC
298LC_NUMERIC=$OLD_LC_NUMERIC
299
300date
301echo "The launching script ended."
Note: See TracBrowser for help on using the repository browser.