1 | #!/bin/bash |
---|
2 | ##################################################################### |
---|
3 | ### Launching script for a chained simulation of PEM and PCM runs ### |
---|
4 | ##################################################################### |
---|
5 | |
---|
6 | echo "The launching script is starting!" |
---|
7 | echo "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 |
---|
10 | if [ "$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 |
---|
15 | else |
---|
16 | nohup "$0" bg > loglaunch.txt 2>&1 & |
---|
17 | exit 1 |
---|
18 | fi |
---|
19 | |
---|
20 | |
---|
21 | ##################################################################### |
---|
22 | # A few parameters that might need be changed depending on your setup: |
---|
23 | # Path to the arch.env to source: |
---|
24 | source ../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: |
---|
28 | n_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: |
---|
33 | nPCM_ini=4 |
---|
34 | |
---|
35 | #---------------- Modify here the number of PCM calls ------------------ |
---|
36 | ## set the number of PCM calls between each PEM call: |
---|
37 | nPCM=2 |
---|
38 | |
---|
39 | #------------------ Modify here the name of PEM exe -------------------- |
---|
40 | ## fill in the name of executable for PEM: |
---|
41 | exePEM="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: |
---|
45 | exeReshape="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 |
---|
50 | OLD_LC_NUMERIC=$LC_NUMERIC |
---|
51 | LC_NUMERIC=en_US.UTF-8 |
---|
52 | |
---|
53 | #------ Check if files/directories necessary for the script exist ------ |
---|
54 | dir=`pwd` |
---|
55 | machine=`hostname` |
---|
56 | address=`whoami` |
---|
57 | if [ ! -f "$exePEM" ]; then |
---|
58 | echo "Error: file \"$exePEM\" does not exist in $dir!" |
---|
59 | exit 1 |
---|
60 | fi |
---|
61 | if [ ! -f "$exeReshape" ]; then |
---|
62 | echo "Error: file \"$exeReshape\" does not exist in $dir!" |
---|
63 | exit 1 |
---|
64 | fi |
---|
65 | if [ ! -f "exePCM.sh" ]; then |
---|
66 | echo "Error: file \"exePCM.sh\" does not exist in $dir!" |
---|
67 | exit 1 |
---|
68 | fi |
---|
69 | if [ ! -f "run_PEM.def" ]; then |
---|
70 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
---|
71 | exit 1 |
---|
72 | fi |
---|
73 | if [ ! -f "run_PCM.def" ]; then |
---|
74 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
---|
75 | exit 1 |
---|
76 | fi |
---|
77 | if [ ! -f "context_lmdz_physics.xml" ]; then |
---|
78 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
---|
79 | exit 1 |
---|
80 | fi |
---|
81 | if [ ! -f "field_def_physics_mars.xml" ]; then |
---|
82 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
---|
83 | exit 1 |
---|
84 | fi |
---|
85 | if [ ! -f "file_def_physics_mars.xml" ]; then |
---|
86 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
---|
87 | exit 1 |
---|
88 | fi |
---|
89 | if [ ! -f "iodef.xml" ]; then |
---|
90 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
---|
91 | exit 1 |
---|
92 | fi |
---|
93 | if [ ! -d "out_PCM" ]; then |
---|
94 | mkdir out_PCM |
---|
95 | fi |
---|
96 | if [ ! -d "out_PEM" ]; then |
---|
97 | mkdir out_PEM |
---|
98 | fi |
---|
99 | if [ ! -d "starts" ]; then |
---|
100 | mkdir starts |
---|
101 | fi |
---|
102 | if [ ! -d "diags" ]; then |
---|
103 | mkdir diags |
---|
104 | fi |
---|
105 | |
---|
106 | #---------------------------- Initialization --------------------------- |
---|
107 | dir=`pwd` |
---|
108 | machine=`hostname` |
---|
109 | address=`whoami` |
---|
110 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine." |
---|
111 | myear=686.9725 # Number of Earth days in Martian year |
---|
112 | eyear=365.256363004 # Number of days in Earth year |
---|
113 | convert_years=$(echo "$myear/$eyear" | bc -l) |
---|
114 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
---|
115 | if [ -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." |
---|
118 | elif [ -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." |
---|
121 | else |
---|
122 | echo "No number of years to be simulated has been set!" |
---|
123 | exit 1 |
---|
124 | fi |
---|
125 | i_myear=0 |
---|
126 | iPEM=1 |
---|
127 | ((iPCM = ($iPEM - 1)*$nPCM + 1)) |
---|
128 | cp startfi.nc starts/ |
---|
129 | if [ -f "start.nc" ]; then |
---|
130 | cp start.nc starts/ |
---|
131 | elif [ -f "star1D.nc" ]; then |
---|
132 | cp star1D.txt starts/ |
---|
133 | fi |
---|
134 | |
---|
135 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
---|
136 | if [ -f "info_PEM.txt" ]; then |
---|
137 | rm info_PEM.txt |
---|
138 | fi |
---|
139 | echo $i_myear $n_myear $convert_years > info_PEM.txt |
---|
140 | |
---|
141 | #---------------------------- Initial runs ----------------------------- |
---|
142 | #--- Initial PCM runs |
---|
143 | cp run_PCM.def run.def |
---|
144 | for ((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!" |
---|
181 | done |
---|
182 | sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt |
---|
183 | |
---|
184 | #--- Reshaping PCM data with XIOS |
---|
185 | echo "Reshaping PCM data with XIOS..." |
---|
186 | ./$exeReshape |
---|
187 | echo "Done!" |
---|
188 | |
---|
189 | #--- Running PEM |
---|
190 | echo "Run PEM $iPEM..." |
---|
191 | cp run_PEM.def run.def |
---|
192 | ./$exePEM > out_runPEM${iPEM} 2>&1 |
---|
193 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
---|
194 | echo "Error: the run PEM $iPEM crashed!" |
---|
195 | exit 1 |
---|
196 | fi |
---|
197 | # Copy data files and prepare the next run |
---|
198 | mv out_runPEM${iPEM} out_PEM/run${iPEM} |
---|
199 | mv diagpem.nc diags/diagpem${iPEM}.nc |
---|
200 | if [ -f "diagsoilpem.nc" ]; then |
---|
201 | mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc |
---|
202 | fi |
---|
203 | cp restartpem.nc starts/restartpem${iPEM}.nc |
---|
204 | mv restartpem.nc startpem.nc |
---|
205 | cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc |
---|
206 | mv restartfi.nc startfi.nc |
---|
207 | if [ -f "restart.nc" ]; then |
---|
208 | cp restart.nc starts/restart_postPEM${iPEM}.nc |
---|
209 | mv restart.nc start.nc |
---|
210 | elif [ -f "restart1D.txt" ]; then |
---|
211 | cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt |
---|
212 | mv restart1D.txt start1D.txt |
---|
213 | fi |
---|
214 | ((iPEM++)) |
---|
215 | read i_myear n_myear convert_years < info_PEM.txt |
---|
216 | echo "Done!" |
---|
217 | |
---|
218 | #---------------------- Main loop to call PEM/PCM ---------------------- |
---|
219 | while [ $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!" |
---|
295 | done |
---|
296 | |
---|
297 | # Restore the previous value of LC_NUMERIC |
---|
298 | LC_NUMERIC=$OLD_LC_NUMERIC |
---|
299 | |
---|
300 | date |
---|
301 | echo "The launching script ended." |
---|