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

Last change on this file since 3618 was 3611, checked in by jbclement, 10 months ago

PEM:
Few bug corrections:

  • global surface pressure was badlu updated (r3610);
  • XIOS slope variables were not consistent;
  • missing initialization for slope number (r3609);
  • taking into account that there may be no "startfi.nc" at the beginning of the PEM simulation.

JBC

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