source: trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh @ 3402

Last change on this file since 3402 was 3394, checked in by jbclement, 18 months ago

PEM:
Updates and improvements related to the last modifications of the launching script.
JBC

  • Property svn:executable set to *
File size: 12.9 KB
Line 
1#!/bin/bash
2########################################################################
3######## Library of bash functions for the PEM launching script ########
4########################################################################
5
6# To end the launching script
7endlaunch() {
8    # Restore the previous value of LC_NUMERIC
9    LC_NUMERIC=$OLD_LC_NUMERIC
10
11    date
12    echo "Successful end of the launching script for the PEM simulation."
13    exit 0
14}
15
16# To end the launching script with error
17errlaunch() {
18    # Restore the previous value of LC_NUMERIC
19    LC_NUMERIC=$OLD_LC_NUMERIC
20
21    date
22    echo "End with error of the launching script for the PEM."
23    exit 1
24}
25
26# To check if SLURM is the job scheduler
27function is_slurm() {
28    if ! command -v squeue &> /dev/null; then
29        echo "Error: the job scheduler is not SLURM on $machine!"
30        echo "You need to adapt the script to your case."
31        errlaunch
32    fi
33}
34
35# To check if everything necessary for the launching script is ok
36checklaunch() {
37    # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator
38    OLD_LC_NUMERIC=$LC_NUMERIC
39    LC_NUMERIC=en_US.UTF-8
40
41    if [ $dim -ne 1 ]; then # 3D model works only with SLURM for now (because of chained job submission in the script + time limit management)
42        is_slurm
43    fi
44    if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then
45        if [ $n_mars_years -lt 1 ]; then
46            echo "Error: the value of 'n_mars_years' must be >0!"
47            errlaunch
48        fi
49    elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then
50        if [ $n_earth_years -lt 1 ]; then
51            echo "Error: the value of 'n_earth_years' must be >0!"
52            errlaunch
53        fi
54    else
55        echo "Error: no number of years to be simulated has been set!"
56        errlaunch
57    fi
58    if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then
59        echo "Error: the value of 'nPCM_ini' must be >1!"
60        errlaunch
61    fi
62    if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then
63        echo "Error: the value of 'nPCM' must be >1!"
64        errlaunch
65    fi
66    if [ ! -f "PCMrun.job" ]; then
67        echo "Error: file \"PCMrun.job\" does not exist in $dir!"
68        errlaunch
69    fi
70    if [ ! -f "PEMrun.job" ]; then
71        echo "Error: file \"PEMrun.job\" does not exist in $dir!"
72        errlaunch
73    fi
74    if [ ! -f "run_PCM.def" ]; then
75        echo "Error: file \"run_PCM.def\" does not exist in $dir!"
76        errlaunch
77    fi
78    if [ ! -f "run_PEM.def" ]; then
79        echo "Error: file \"run_PEM.def\" does not exist in $dir!"
80        errlaunch
81    fi
82    if [ ! -f "context_lmdz_physics.xml" ]; then
83        echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!"
84        errlaunch
85    fi
86    if [ ! -f "field_def_physics_mars.xml" ]; then
87        echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!"
88        errlaunch
89    fi
90    if [ ! -f "file_def_physics_mars.xml" ]; then
91        echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!"
92        errlaunch
93    fi
94    if [ ! -f "iodef.xml" ]; then
95        echo "Error: file \"iodef.xml\" does not exist in $dir!"
96        errlaunch
97    fi
98    if [ ! -d "out_PCM" ]; then
99        mkdir out_PCM
100    fi
101    if [ ! -d "out_PEM" ]; then
102        mkdir out_PEM
103    fi
104    if [ ! -d "starts" ]; then
105        mkdir starts
106    fi
107    if [ ! -d "diags" ]; then
108        mkdir diags
109    fi
110}
111
112# To convert Earth years into Mars years
113convertyears() {
114    myear=686.9725      # Number of Earth days in Martian year
115    eyear=365.256363004 # Number of days in Earth year
116    convert_years=$(echo "$myear/$eyear" | bc -l)
117    convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year
118    if [ -v n_mars_years ]; then
119        n_myear=$n_mars_years
120        echo "Number of years to be simulated: $n_myear Martian years."
121    elif [ -v n_earth_years ]; then
122        n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years
123        echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years."
124    fi
125}
126
127# To initialize the launching script
128initlaunch() {
129    echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user."
130    convertyears
131    i_myear=0
132    iPEM=1
133    iPCM=1
134    cp startfi.nc starts/
135    if [ -f "start.nc" ]; then
136        cp start.nc starts/
137    elif [ -f "star1D.nc" ]; then
138        cp star1D.txt starts/
139    fi
140
141    # Create a file to manage years of the chained simulation and store some info from the PEM runs
142    echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > info_PEM.txt
143}
144
145# To submit the PCM runs
146# arg1: model dimension
147# arg2: number of PCM runs to launch
148# arg3: local number of the PCM run from which to start (optional)
149submitPCM() {
150    find . -type f -name "PCMrun*.job" ! -name "PCMrun.job" -delete
151    ii=1
152    if [ ! -z $3 ]; then
153        ii=$3
154    fi
155    if [ $i_myear -lt $n_myear ]; then
156        echo "Run PCM $iPCM: call $ii/$2..."
157        if [ $1 -eq 1 ]; then # 1D model
158            sed -i "s/^k=[0-9]\+$/k=$(echo "3 - $nPCM_ini" | bc -l)/" PCMrun.job
159            ./PCMrun.job
160        else # 3D model
161            cp PCMrun.job PCMrun${iPCM}.job
162            sed -i -E "s/(#SBATCH --job-name=[^0-9]*[0-9]*[^0-9]*)[0-9]+$/\1${iPCM}/" PCMrun${iPCM}.job
163            sed -i "s/^k=[0-9]\+$/k=$(echo "3 - $nPCM_ini" | bc -l)/" PCMrun${iPCM}.job
164            jobID=$(sbatch --parsable PCMrun${iPCM}.job)
165            # Create a file to cancel the dependent jobs of the cycle
166            echo "#!/bin/bash" > kill_launchPEM.sh
167            chmod +x kill_launchPEM.sh
168            echo "scancel" $jobID >> kill_launchPEM.sh
169        fi
170        ((iPCM++))
171        ((i_myear++))
172        ((ii++))
173    else
174        endlaunch
175    fi
176    for ((i = $ii; i <= $2; i++)); do
177        if [ $i_myear -lt $n_myear ]; then
178            echo "Run PCM $iPCM: call $i/$2..."
179            if [ $1 -eq 1 ]; then # 1D model
180                sed -i "s/^k=[0-9]\+$/k=$(echo "$i + 2 - $nPCM_ini" | bc -l)/" PCMrun.job
181                ./PCMrun.job
182            else # 3D model
183                cp PCMrun.job PCMrun${iPCM}.job
184                sed -i -E "s/(#SBATCH --job-name=[^0-9]*[0-9]*[^0-9]*)[0-9]+$/\1${iPCM}/" PCMrun${iPCM}.job
185                sed -i "s/^k=[0-9]\+$/k=$(echo "$i + 2 - $nPCM_ini" | bc -l)/" PCMrun${iPCM}.job
186                jobID=$(sbatch --parsable --dependency=afterok:${jobID} PCMrun${iPCM}.job)
187                echo "scancel" $jobID >> kill_launchPEM.sh
188            fi
189            ((iPCM++))
190            ((i_myear++))
191        else
192            endlaunch
193        fi
194    done
195}
196
197# To submit the PEM run
198# arg1: model dimension
199submitPEM() {
200    if [ $i_myear -lt $n_myear ]; then
201        echo "Run PEM $iPEM"
202        if [ $1 -eq 1 ]; then # 1D model
203            ./PEMrun.job
204        else # 3D model
205            sed -i -E "s/(#SBATCH --job-name=[^0-9]*[0-9]*[^0-9]*)[0-9]+$/\1${iPEM}/" PEMrun.job
206            jobID=$(sbatch --parsable PEMrun.job)
207            # Create a file to cancel the dependent jobs of the cycle
208            echo "#!/bin/bash" > kill_launchPEM.sh
209            chmod +x kill_launchPEM.sh
210            echo "scancel" $jobID >> kill_launchPEM.sh
211        fi
212    else
213        endlaunch
214    fi
215}
216
217# To make one cycle of PCM and PEM runs
218# arg1: model dimension
219# arg2: number of PCM runs to launch
220# arg3: local number of the PCM run from which to start (optional)
221cyclelaunch() {
222    # PCM runs
223    submitPCM $1 $2 $3
224
225    # PEM run
226    if [ $i_myear -lt $n_myear ]; then
227        echo "Run PEM $iPEM"
228        if [ $1 -eq 1 ]; then # 1D model
229            ./PEMrun.job
230        else # 3D model
231            sed -i -E "s/(#SBATCH --job-name=[^0-9]*[0-9]*[^0-9]*)[0-9]+$/\1${iPEM}/" PEMrun.job
232            jobID=$(sbatch --parsable --dependency=afterok:${jobID} PEMrun.job)
233            echo "scancel" $jobID >> kill_launchPEM.sh
234        fi
235    else
236        endlaunch
237    fi
238}
239
240# To clean files after the starting run of the relaunch
241# arg1: file name prefix to clean
242# arg2: file name extension to clean
243# arg3: file number from which to clean
244cleanfiles() {
245    prefix=$1
246    extension=$2
247    if [ -z "$extension" ]; then
248        for file in ${prefix}*; do
249            num=${file#$prefix}
250            if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then
251                rm $file
252            fi
253        done
254    else
255        for file in ${prefix}*${extension}; do
256            num=${file#$prefix}
257            num=${num%$extension}
258            if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then
259                rm $file
260            fi
261        done
262    fi
263}
264
265# To relaunch from PCM run
266# arg1: model dimension
267relaunchPCM() {
268    iPCM=$(($irelaunch + 1))
269    cleanfiles diags/diagfi .nc $irelaunch
270    cleanfiles diags/data2reshape .nc $irelaunch
271    cleanfiles "out_PCM/run" "" $irelaunch
272    cleanfiles starts/restart1D .txt $irelaunch
273    cleanfiles starts/restart .nc $irelaunch
274    cleanfiles starts/restartfi .nc $irelaunch
275    cp starts/restartfi${irelaunch}.nc startfi.nc
276    if [ -f "starts/restart${irelaunch}.nc" ]; then
277        cp starts/restart${irelaunch}.nc start.nc
278    elif [ -f "starts/restart1D${irelaunch}.txt" ]; then
279        cp starts/restart1D${irelaunch}.txt start1D.txt
280    fi
281    if [ $irelaunch -le $nPCM_ini ]; then
282        # PCM relaunch during the initialization cycle
283        iPEM=1
284        cleanfiles diags/diagpem .nc $iPEM
285        cleanfiles "out_PEM/run" "" $iPEM
286        cleanfiles starts/restart1D_postPEM .txt $iPEM
287        cleanfiles starts/restart_postPEM .nc $iPEM
288        cleanfiles starts/restartfi_postPEM .nc $iPEM
289        cleanfiles starts/restartpem .nc $iPEM
290        i_myear=$irelaunch
291        sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
292        rm -f startpem.nc
293        if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then
294            cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
295            cyclelaunch $1 $nPCM_ini $iPCM
296        elif [ $irelaunch -eq $nPCM_ini ]; then
297            cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
298            cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
299            submitPEM $1 # The next job is a PEM run
300        else
301            cyclelaunch $1 $nPCM_ini $iPCM
302        fi
303    else
304        # PCM relaunch during a cycle
305        iPEM=$((($irelaunch - $nPCM_ini)/$nPCM + 1))
306        il=$(echo "($irelaunch - $nPCM_ini)%$nPCM" | bc -l)
307        cleanfiles diags/diagpem .nc $iPEM
308        cleanfiles "out_PEM/run" "" $iPEM
309        cleanfiles starts/restart1D_postPEM .txt $iPEM
310        cleanfiles starts/restart_postPEM .nc $iPEM
311        cleanfiles starts/restartfi_postPEM .nc $iPEM
312        cleanfiles starts/restartpem .nc $iPEM
313        cp starts/restartpem${iPEM}.nc startpem.nc
314        if [ $il -eq $(($nPCM - 1)) ]; then
315            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il))
316            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
317            cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
318            cyclelaunch $1 $nPCM $il
319        elif [ $il -eq 0 ]; then
320            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $nPCM))
321            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
322            cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
323            cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
324            submitPEM $1 # The next job is a PEM run
325        else
326            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il))
327            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
328            cyclelaunch $1 $nPCM $il
329        fi
330    fi
331}
332
333# To relaunch from PEM run
334# arg1: model dimension
335relaunchPEM() {
336    iPEM=$irelaunch
337    iPCM=$(($nPCM_ini + ($nPCM - 1)*$irelaunch + 1))
338    i_myear=$(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt")
339    sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
340    cleanfiles diags/diagfi .nc $(($iPCM - 1))
341    cleanfiles "out_PCM/run" "" $(($iPCM - 1))
342    cleanfiles starts/restart1D .txt $(($iPCM - 1))
343    cleanfiles starts/restart .nc $(($iPCM - 1))
344    cleanfiles starts/restartfi .nc $(($iPCM - 1))
345    cleanfiles diags/data2reshape .nc $(($iPCM - 1))
346    cleanfiles diags/diagpem .nc $irelaunch
347    cleanfiles "out_PEM/run" "" $irelaunch
348    cleanfiles starts/restart1D_postPEM .txt $irelaunch
349    cleanfiles starts/restart_postPEM .nc $irelaunch
350    cleanfiles starts/restartfi_postPEM .nc $irelaunch
351    cleanfiles starts/restartpem .nc $irelaunch
352    cp starts/restartpem${irelaunch}.nc startpem.nc
353    cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc
354    if [ -f "starts/restart_postPEM${irelaunch}.nc" ]; then
355        cp starts/restart_postPEM${irelaunch}.nc start.nc
356    elif [ -f "starts/restart1D_postPEM${irelaunch}.txt" ]; then
357        cp starts/restart1D_postPEM${irelaunch}.txt start1D.txt
358    fi
359    cyclelaunch $1 $nPCM
360}
Note: See TracBrowser for help on using the repository browser.