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

Last change on this file since 3363 was 3355, checked in by jbclement, 20 months ago

PEM:
The launching script can now operate a relaunch from any already computed PCM/PEM run. The starting point is asked to the user interactively on the terminal.
Some features might not work very well yet since not every stituation has not been tested.
JBC

  • Property svn:executable set to *
File size: 9.2 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 [ ! -x $(command -v squeue) ]; 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    is_slurm
42    if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then
43        if [ $n_mars_years -lt 1 ]; then
44            echo "Error: the value of 'n_mars_years' must be >0!"
45            errlaunch
46        fi
47    elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then
48        if [ $n_earth_years -lt 1 ]; then
49            echo "Error: the value of 'n_earth_years' must be >0!"
50            errlaunch
51        fi
52    else
53        echo "Error: no number of years to be simulated has been set!"
54        errlaunch
55    fi
56    if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then
57        echo "Error: the value of 'nPCM_ini' must be >1!"
58        errlaunch
59    fi
60    if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then
61        echo "Error: the value of 'nPCM' must be >1!"
62        errlaunch
63    fi
64    if [ ! -f "jobPCM.slurm" ]; then
65        echo "Error: file \"jobPCM.slurm\" does not exist in $dir!"
66        errlaunch
67    fi
68    if [ ! -f "jobPEM.slurm" ]; then
69        echo "Error: file \"$jobPEM.slurm\" does not exist in $dir!"
70        errlaunch
71    fi
72    if [ ! -f "run_PCM.def" ]; then
73        echo "Error: file \"run_PCM.def\" does not exist in $dir!"
74        errlaunch
75    fi
76    if [ ! -f "run_PEM.def" ]; then
77        echo "Error: file \"run_PEM.def\" does not exist in $dir!"
78        errlaunch
79    fi
80    if [ ! -f "context_lmdz_physics.xml" ]; then
81        echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!"
82        errlaunch
83    fi
84    if [ ! -f "field_def_physics_mars.xml" ]; then
85        echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!"
86        errlaunch
87    fi
88    if [ ! -f "file_def_physics_mars.xml" ]; then
89        echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!"
90        errlaunch
91    fi
92    if [ ! -f "iodef.xml" ]; then
93        echo "Error: file \"iodef.xml\" does not exist in $dir!"
94        errlaunch
95    fi
96    if [ ! -d "out_PCM" ]; then
97        mkdir out_PCM
98    fi
99    if [ ! -d "out_PEM" ]; then
100        mkdir out_PEM
101    fi
102    if [ ! -d "starts" ]; then
103        mkdir starts
104    fi
105    if [ ! -d "diags" ]; then
106        mkdir diags
107    fi
108}
109
110# To initialize the launching script
111initlaunch() {
112    echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user."
113    myear=686.9725      # Number of Earth days in Martian year
114    eyear=365.256363004 # Number of days in Earth year
115    convert_years=$(echo "$myear/$eyear" | bc -l)
116    convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year
117    if [ -v n_mars_years ]; then
118        n_myear=$n_mars_years
119        echo "Number of years to be simulated: $n_myear Martian years."
120    elif [ -v n_earth_years ]; then
121        n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years
122        echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years."
123    fi
124    i_myear=0
125    iPEM=1
126    iPCM=1
127    cp startfi.nc starts/
128    if [ -f "start.nc" ]; then
129        cp start.nc starts/
130    elif [ -f "star1D.nc" ]; then
131        cp star1D.txt starts/
132    fi
133
134    # Create a file to manage years of the chained simulation and store some info from the PEM runs
135    echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > info_PEM.txt
136}
137
138# To submit the PCM runs
139submitPCM() {
140    find . -type f -name "jobPCM*.slurm" ! -name "jobPCM.slurm" -delete
141    ii=1
142    if [ ! -z $2 ]; then
143        ii=$2
144    fi
145    if [ $i_myear -lt $n_myear ]; then
146        echo "Run PCM $iPCM: call $ii/$1..."
147        cp jobPCM.slurm jobPCM${iPCM}.slurm
148        sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm
149        sed -i "s/^k=[0-9]\+$/k=$(echo "3 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm
150        jobID=$(sbatch --parsable jobPCM${iPCM}.slurm)
151        # Create a file to cancel the dependent jobs of the cycle
152        echo "#!/bin/bash" > kill_launchPEM.sh
153        chmod +x kill_launchPEM.sh
154        echo "scancel" $jobID >> kill_launchPEM.sh
155        ((iPCM++))
156        ((i_myear++))
157        ((ii++))
158    else
159        endlaunch
160    fi
161    for ((i = $ii; i <= $1; i++)); do
162        if [ $i_myear -lt $n_myear ]; then
163            echo "Run PCM $iPCM: call $i/$1..."
164            cp jobPCM.slurm jobPCM${iPCM}.slurm
165            sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm
166            sed -i "s/^k=[0-9]\+$/k=$(echo "$i + 2 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm
167            jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPCM${iPCM}.slurm)
168            echo "scancel" $jobID >> kill_launchPEM.sh
169            ((iPCM++))
170            ((i_myear++))
171        else
172            endlaunch
173        fi
174    done
175}
176
177# To submit the PEM run
178submitPEM() {
179    if [ $i_myear -lt $n_myear ]; then
180        echo "Run PEM $iPEM"
181        sed -i "s/#SBATCH --job-name=jobPEM.*/#SBATCH --job-name=jobPEM${iPEM}/" jobPEM.slurm
182        jobID=$(sbatch --parsable jobPEM.slurm)
183        # Create a file to cancel the dependent jobs of the cycle
184        echo "#!/bin/bash" > kill_launchPEM.sh
185        chmod +x kill_launchPEM.sh
186        echo "scancel" $jobID >> kill_launchPEM.sh
187    else
188        endlaunch
189    fi
190}
191
192# To make one cycle of PCM and PEM runs
193cyclelaunch() {
194    # PCM runs
195    submitPCM $1 $2
196
197    # PEM run
198    if [ $i_myear -lt $n_myear ]; then
199        echo "Run PEM $iPEM"
200        sed -i "s/#SBATCH --job-name=jobPEM.*/#SBATCH --job-name=jobPEM${iPEM}/" jobPEM.slurm
201        jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPEM.slurm)
202        echo "scancel" $jobID >> kill_launchPEM.sh
203    else
204        endlaunch
205    fi
206}
207
208# To relaunch from PCM run
209relaunchPCM() {
210    iPCM=$(($irelaunch + 1))
211    cp starts/restartfi${irelaunch}.nc startfi.nc
212    if [ -f "starts/restart${irelaunch}.nc" ]; then
213        cp starts/restart${irelaunch}.nc start.nc
214    elif [ -f "starts/restart1D${irelaunch}.txt" ]; then
215        cp starts/restart1D${irelaunch}.txt start1D.txt
216    fi
217    if [ $irelaunch -le $nPCM_ini ]; then
218        # PCM relaunch during the initialization cycle
219        iPEM=1
220        i_myear=$irelaunch
221        sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
222        if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then
223            cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
224            cyclelaunch $nPCM_ini $irelaunch
225        elif [ $irelaunch -eq $nPCM_ini ]; then
226            cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
227            cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
228            submitPEM # The next job is a PEM run
229        else
230            cyclelaunch $nPCM_ini $iPCM
231        fi
232    else
233        # PCM relaunch during a cycle
234        iPEM=$(($irelaunch/$nPCM + 1))
235        il=$(echo "($irelaunch - $nPCM_ini)%$nPCM" | bc -l)
236        i_myear=$(($(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt") + $il))
237        sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
238        cp starts/restartpem${iPEM}.nc startpem.nc
239        if [ $il -eq $(($nPCM - 1)) ]; then
240            cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
241            cyclelaunch $nPCM $il
242        elif [ $il -eq $nPCM ]; then
243            cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
244            cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
245            submitPEM # The next job is a PEM run
246        else
247            cyclelaunch $nPCM $il
248        fi
249    fi
250}
251
252# To relaunch from PEM run
253relaunchPEM() {
254    iPEM=$irelaunch
255    iPCM=$(($nPCM_ini + ($nPCM - 1)*$irelaunch + 1))
256    i_myear=$(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt")
257    sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
258    cp starts/restartpem${irelaunch}.nc startpem.nc
259    cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc
260    if [ -f "starts/restart_postPEM${irelaunch}.nc" ]; then
261        cp starts/restart_postPEM${irelaunch}.nc start.nc
262    elif [ -f "starts/restart1D_postPEM${irelaunch}.txt" ]; then
263        cp starts/restart1D_postPEM${irelaunch}.txt start1D.txt
264    fi
265    cyclelaunch $nPCM
266}
Note: See TracBrowser for help on using the repository browser.