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 | if [ "$1" = "bg" ]; then |
---|
9 | date |
---|
10 | else |
---|
11 | nohup "$0" bg > loglaunch.txt 2>&1 & |
---|
12 | exit 1 |
---|
13 | fi |
---|
14 | |
---|
15 | # A few parameters that might need be changed depending on your setup: |
---|
16 | # Path to the arch.env to source: |
---|
17 | source ../trunk/LMDZ.COMMON/arch.env |
---|
18 | |
---|
19 | # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator |
---|
20 | OLD_LC_NUMERIC=$LC_NUMERIC |
---|
21 | LC_NUMERIC=en_US.UTF-8 |
---|
22 | |
---|
23 | #---------- Modify here the number of years to be simulated ------------ |
---|
24 | ## set the number of years, either Martian or Earth years: |
---|
25 | n_mars_years=1000000 |
---|
26 | #n_earth_years=1000000 |
---|
27 | |
---|
28 | #---------------- Modify here the number of PCM calls ------------------ |
---|
29 | ## set the number of PCM calls between each PEM call: |
---|
30 | nPCM=2 |
---|
31 | |
---|
32 | #------------------ Modify here the name of PEM exe -------------------- |
---|
33 | ## fill in the name of executable for PEM: |
---|
34 | exePEM=pem_29_phymars_seq.e |
---|
35 | |
---|
36 | #-------------- Modify here the name of reshape XIOS exe --------------- |
---|
37 | ## fill in the name of executable for reshape XIOS: |
---|
38 | exeReshape=reshape_XIOS_output_64x48x29_phymars_seq.e |
---|
39 | |
---|
40 | #------------------------------ Parameters ----------------------------- |
---|
41 | myear=686.9725 # Number of Earth days in Martian year |
---|
42 | eyear=365.256363004 # Number of days in Earth year |
---|
43 | |
---|
44 | #------ Check if files/directories necessary for the script exist ------ |
---|
45 | dir=`pwd` |
---|
46 | machine=`hostname` |
---|
47 | address=`whoami` |
---|
48 | if [ ! -f "exePCM.sh" ]; then |
---|
49 | echo "Error: file \"exePCM.sh\" does not exist in $dir!" |
---|
50 | exit 1 |
---|
51 | fi |
---|
52 | if [ ! -f "run_PEM.def" ]; then |
---|
53 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
---|
54 | exit 1 |
---|
55 | fi |
---|
56 | if [ ! -f "run_PCM.def" ]; then |
---|
57 | echo "Error: file \"run_PCM.def\" does not exist in $dir!" |
---|
58 | exit 1 |
---|
59 | fi |
---|
60 | if [ ! -f "context_lmdz_physics.xml" ]; then |
---|
61 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
---|
62 | exit 1 |
---|
63 | fi |
---|
64 | if [ ! -f "field_def_physics_mars.xml" ]; then |
---|
65 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
---|
66 | exit 1 |
---|
67 | fi |
---|
68 | if [ ! -f "file_def_physics_mars.xml" ]; then |
---|
69 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
---|
70 | exit 1 |
---|
71 | fi |
---|
72 | if [ ! -f "iodef.xml" ]; then |
---|
73 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
---|
74 | exit 1 |
---|
75 | fi |
---|
76 | if [ ! -d "out_PCM" ]; then |
---|
77 | mkdir out_PCM |
---|
78 | fi |
---|
79 | if [ ! -d "out_PEM" ]; then |
---|
80 | mkdir out_PEM |
---|
81 | fi |
---|
82 | if [ ! -d "starts" ]; then |
---|
83 | mkdir starts |
---|
84 | fi |
---|
85 | if [ ! -d "diags" ]; then |
---|
86 | mkdir diags |
---|
87 | fi |
---|
88 | |
---|
89 | #---------------------------- Initialization --------------------------- |
---|
90 | dir=`pwd` |
---|
91 | machine=`hostname` |
---|
92 | address=`whoami` |
---|
93 | echo "This is a chained simulation for PEM and PCM runs in $dir on $machine." |
---|
94 | convert_years=$(echo "$myear/$eyear" | bc -l) |
---|
95 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
---|
96 | if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then |
---|
97 | n_myear=$n_mars_years |
---|
98 | echo "Number of years to be simulated: $n_myear Martian years." |
---|
99 | elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then |
---|
100 | n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years |
---|
101 | echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years." |
---|
102 | else |
---|
103 | echo "No number of years to be simulated has been set!" |
---|
104 | exit 1 |
---|
105 | fi |
---|
106 | i_myear=0 |
---|
107 | iPEM=1 |
---|
108 | ((iPCM = ($iPEM - 1)*$nPCM + 1)) |
---|
109 | cp startfi.nc starts/ |
---|
110 | if [ -f "start.nc" ]; then |
---|
111 | cp start.nc starts/ |
---|
112 | fi |
---|
113 | |
---|
114 | # Create a file to manage years of the chained simulation and store some info from the PEM runs |
---|
115 | if [ -f "info_PEM.txt" ]; then |
---|
116 | rm info_PEM.txt |
---|
117 | fi |
---|
118 | echo $i_myear $n_myear $convert_years > info_PEM.txt |
---|
119 | |
---|
120 | #---------------------- Main loop to call PEM/PCM ---------------------- |
---|
121 | while [ $i_myear -lt $n_myear ]; do |
---|
122 | #--- Loop to run PCM year by year |
---|
123 | cp run_PCM.def run.def |
---|
124 | for ((i = 1; i <= $nPCM; i++)); do |
---|
125 | echo "Run PCM $iPCM: call $i/$nPCM..." |
---|
126 | sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh |
---|
127 | sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh |
---|
128 | sbatch -W exePCM.sh |
---|
129 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
---|
130 | echo "Error: the run PCM $iPCM has crashed!" |
---|
131 | exit 1 |
---|
132 | fi |
---|
133 | # Copy data files and prepare the next run |
---|
134 | mv out_runPCM${iPCM} out_PCM/run${iPCM} |
---|
135 | mv diagfi.nc diags/diagfi${iPCM}.nc |
---|
136 | if [ -f "diagsoil.nc" ]; then |
---|
137 | mv diagsoil.nc diags/diagsoil${iPCM}.nc |
---|
138 | fi |
---|
139 | if [ -f "stats.nc" ]; then |
---|
140 | mv stats.nc diags/stats${iPCM}.nc |
---|
141 | fi |
---|
142 | cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc |
---|
143 | mv Xdiurnalave.nc data2reshape${i}.nc |
---|
144 | cp restartfi.nc starts/startfi${iPCM}.nc |
---|
145 | mv restartfi.nc startfi.nc |
---|
146 | if [ -f "restart.nc" ]; then |
---|
147 | cp restart.nc starts/restart${iPCM}.nc |
---|
148 | mv restart.nc start.nc |
---|
149 | elif [ -f "restart1D.txt" ]; then |
---|
150 | cp restart1D.txt starts/restart1D${iPCM}.txt |
---|
151 | mv restart1D.txt start1D.txt |
---|
152 | fi |
---|
153 | ((iPCM++)) |
---|
154 | ((i_myear++)) |
---|
155 | echo "Done!" |
---|
156 | done |
---|
157 | sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt |
---|
158 | |
---|
159 | #--- Reshaping PCM data with XIOS |
---|
160 | echo "Reshaping PCM data with XIOS..." |
---|
161 | ./$exeReshape |
---|
162 | echo "Done!" |
---|
163 | |
---|
164 | #--- Running PEM |
---|
165 | echo "Run PEM $iPEM..." |
---|
166 | cp run_PEM.def run.def |
---|
167 | mv startfi.nc startfi_evol.nc |
---|
168 | if [ -f "start.nc" ]; then |
---|
169 | mv start.nc start_evol.nc |
---|
170 | elif [ -f "start1D.txt" ]; then |
---|
171 | mv start1D.txt start1D_evol.txt |
---|
172 | fi |
---|
173 | ./$exePEM > out_runPEM${iPEM} 2>&1 |
---|
174 | if [ ! -f "restartfi_evol.nc" ]; then # Check if run ended abnormally |
---|
175 | echo "Error: the run PEM $iPEM has crashed!" |
---|
176 | exit 1 |
---|
177 | fi |
---|
178 | # Copy data files and prepare the next run |
---|
179 | mv out_runPEM${iPEM} out_PEM/run${iPEM} |
---|
180 | mv diagpem.nc diags/diagpem${iPEM}.nc |
---|
181 | cp restartpem.nc starts/startpem${iPEM}.nc |
---|
182 | mv restartpem.nc startpem.nc |
---|
183 | cp restartfi_evol.nc starts/startfi_postPEM${iPEM}.nc |
---|
184 | mv restartfi_evol.nc startfi.nc |
---|
185 | if [ -f "restart_evol.nc" ]; then |
---|
186 | cp restart_evol.nc starts/restart_postPEM${iPEM}.nc |
---|
187 | mv restart_evol.nc start.nc |
---|
188 | elif [ -f "restart1D_evol.txt" ]; then |
---|
189 | cp restart1D_evol.txt starts/restart1D_postPEM${iPEM}.txt |
---|
190 | mv restart1D_evol.txt start1D.txt |
---|
191 | fi |
---|
192 | ((iPEM++)) |
---|
193 | read i_myear n_myear convert_years < info_PEM.txt |
---|
194 | echo "Done!" |
---|
195 | done |
---|
196 | |
---|
197 | # Restore the previous value of LC_NUMERIC |
---|
198 | LC_NUMERIC=$OLD_LC_NUMERIC |
---|
199 | |
---|
200 | date |
---|
201 | echo "The launching script has terminated." |
---|