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

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

PEM:
Addition in the launching script of the possibility to submit a job with PBS/TORQUE + Modification to make the time limit detection in "pem.F90" work with PBS/TORQUE + Update of the headers of .job files.
JBC

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