1 | #!/bin/bash |
---|
2 | ##################################################################### |
---|
3 | ### Launching script for a chained simulation of PEM and GCM 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 Earth years to be simulated --------- |
---|
24 | ## set the number of years: |
---|
25 | n_earth_years=1000000 |
---|
26 | |
---|
27 | #---------------- Modify here the number of GCM calls ------------------ |
---|
28 | ## set the number of GCM calls between each PEM call: |
---|
29 | nGCM=2 |
---|
30 | |
---|
31 | #------------------ Modify here the name of PEM exe -------------------- |
---|
32 | ## fill in the name of executable for PEM: |
---|
33 | exePEM=pem_29_phymars_seq.e |
---|
34 | |
---|
35 | #-------------- Modify here the name of reshape XIOS exe --------------- |
---|
36 | ## fill in the name of executable for reshape XIOS: |
---|
37 | exeReshape=reshape_XIOS_output_64x48x29_phymars_seq.e |
---|
38 | |
---|
39 | #------------------------------ Parameters ----------------------------- |
---|
40 | myear=686.9725 # Number of Earth days in Martian year |
---|
41 | eyear=365.256363004 # Number of days in Earth year |
---|
42 | |
---|
43 | #------ Check if files/directories necessary for the script exist ------ |
---|
44 | dir=`pwd` |
---|
45 | machine=`hostname` |
---|
46 | address=`whoami` |
---|
47 | if [ ! -f "exeGCM.sh" ]; then |
---|
48 | echo "Error: file \"exeGCM.sh\" does not exist in $dir!" |
---|
49 | exit 1 |
---|
50 | fi |
---|
51 | if [ ! -f "run_PEM.def" ]; then |
---|
52 | echo "Error: file \"run_PEM.def\" does not exist in $dir!" |
---|
53 | exit 1 |
---|
54 | fi |
---|
55 | if [ ! -f "run_GCM.def" ]; then |
---|
56 | echo "Error: file \"run_GCM.def\" does not exist in $dir!" |
---|
57 | exit 1 |
---|
58 | fi |
---|
59 | if [ ! -f "context_lmdz_physics.xml" ]; then |
---|
60 | echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!" |
---|
61 | exit 1 |
---|
62 | fi |
---|
63 | if [ ! -f "field_def_physics_mars.xml" ]; then |
---|
64 | echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!" |
---|
65 | exit 1 |
---|
66 | fi |
---|
67 | if [ ! -f "file_def_physics_mars.xml" ]; then |
---|
68 | echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!" |
---|
69 | exit 1 |
---|
70 | fi |
---|
71 | if [ ! -f "iodef.xml" ]; then |
---|
72 | echo "Error: file \"iodef.xml\" does not exist in $dir!" |
---|
73 | exit 1 |
---|
74 | fi |
---|
75 | if [ ! -d "out_GCM" ]; then |
---|
76 | mkdir out_GCM |
---|
77 | fi |
---|
78 | if [ ! -d "out_PEM" ]; then |
---|
79 | mkdir out_PEM |
---|
80 | fi |
---|
81 | if [ ! -d "starts" ]; then |
---|
82 | mkdir starts |
---|
83 | fi |
---|
84 | if [ ! -d "diags" ]; then |
---|
85 | mkdir diags |
---|
86 | fi |
---|
87 | |
---|
88 | #---------------------------- Initialization --------------------------- |
---|
89 | dir=`pwd` |
---|
90 | machine=`hostname` |
---|
91 | address=`whoami` |
---|
92 | echo "This is a chained simulation for PEM and GCM runs in $dir on $machine." |
---|
93 | convert_years=$(echo "$myear/$eyear" | bc -l) |
---|
94 | convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year |
---|
95 | n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years |
---|
96 | echo "Number of years to simulate: $n_earth_years Earth years = $n_myear Martian years." |
---|
97 | i_myear=0 |
---|
98 | iPEM=1 |
---|
99 | ((iGCM = ($iPEM - 1)*$nGCM + 1)) |
---|
100 | cp startfi.nc starts/ |
---|
101 | if [ -f "start.nc" ]; then |
---|
102 | cp start.nc starts/ |
---|
103 | fi |
---|
104 | |
---|
105 | # Create a temporary file to manage years of the chained simulation |
---|
106 | echo $i_myear $n_myear $convert_years > tmp_PEMyears.txt |
---|
107 | |
---|
108 | #---------------------- Main loop to call PEM/GCM ---------------------- |
---|
109 | while [ $i_myear -lt $n_myear ]; do |
---|
110 | #--- Loop to run GCM year by year |
---|
111 | cp run_GCM.def run.def |
---|
112 | if [ -f "diagfi.def" ]; then |
---|
113 | rm diagfi.def |
---|
114 | fi |
---|
115 | if [ -f "diagfi_GCM.def" ]; then |
---|
116 | cp diagfi_GCM.def diagfi.def |
---|
117 | fi |
---|
118 | for ((i = 1; i <= $nGCM; i++)); do |
---|
119 | echo "Run GCM $iGCM: call $i/$nGCM..." |
---|
120 | sed -i "s/#SBATCH --job-name=runGCM.*/#SBATCH --job-name=runGCM${iGCM}/" exeGCM.sh |
---|
121 | sed -i "s/out_runGCM[0-9]\+/out_runGCM${iGCM}/" exeGCM.sh |
---|
122 | sbatch -W exeGCM.sh |
---|
123 | if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally |
---|
124 | echo "Error: the run GCM $iGCM has crashed!" |
---|
125 | exit 1 |
---|
126 | fi |
---|
127 | # Copy data files and prepare the next run |
---|
128 | mv out_runGCM${iGCM} out_GCM/run${iGCM} |
---|
129 | mv diagfi.nc diags/diagfi${iGCM}.nc |
---|
130 | if [ -f "diagsoil.nc" ]; then |
---|
131 | mv diagsoil.nc diags/diagsoil${iGCM}.nc |
---|
132 | fi |
---|
133 | if [ -f "stats.nc" ]; then |
---|
134 | mv stats.nc diags/stats${iGCM}.nc |
---|
135 | fi |
---|
136 | cp Xdiurnalave.nc diags/data2reshape${iGCM}.nc |
---|
137 | mv Xdiurnalave.nc data2reshape${i}.nc |
---|
138 | cp restartfi.nc starts/startfi${iGCM}.nc |
---|
139 | mv restartfi.nc startfi.nc |
---|
140 | if [ -f "restart.nc" ]; then |
---|
141 | cp restart.nc starts/restart${iGCM}.nc |
---|
142 | mv restart.nc start.nc |
---|
143 | elif [ -f "restart1D.txt" ]; then |
---|
144 | cp restart1D.txt starts/restart1D${iGCM}.txt |
---|
145 | mv restart1D.txt start1D.txt |
---|
146 | fi |
---|
147 | ((iGCM++)) |
---|
148 | ((i_myear++)) |
---|
149 | echo "Done!" |
---|
150 | done |
---|
151 | echo $i_myear $n_myear $convert_years > tmp_PEMyears.txt |
---|
152 | #--- Reshaping GCM data with XIOS |
---|
153 | echo "Reshaping GCM data with XIOS..." |
---|
154 | ./$exeReshape |
---|
155 | for file in datareshaped*; do |
---|
156 | mv $file ${file/reshaped/_GCM_Y} |
---|
157 | done |
---|
158 | echo "Done!" |
---|
159 | #--- Running PEM |
---|
160 | echo "Run PEM $iPEM..." |
---|
161 | cp run_PEM.def run.def |
---|
162 | if [ -f "diagfi.def" ]; then |
---|
163 | rm diagfi.def |
---|
164 | fi |
---|
165 | if [ -f "diagfi_PEM.def" ]; then |
---|
166 | cp diagfi_PEM.def diagfi.def |
---|
167 | fi |
---|
168 | mv startfi.nc startfi_evol.nc |
---|
169 | if [ -f "start.nc" ]; then |
---|
170 | mv start.nc start_evol.nc |
---|
171 | elif [ -f "start1D.txt" ]; then |
---|
172 | mv start1D.txt start1D_evol.txt |
---|
173 | fi |
---|
174 | ./$exePEM > out_runPEM${iPEM} 2>&1 |
---|
175 | if [ ! -f "restartfi_evol.nc" ]; then # Check if run ended abnormally |
---|
176 | echo "Error: the run PEM $iPEM has crashed!" |
---|
177 | exit 1 |
---|
178 | fi |
---|
179 | # Copy data files and prepare the next run |
---|
180 | mv out_runPEM${iPEM} out_PEM/run${iPEM} |
---|
181 | mv diagpem.nc diags/diagpem_${iPEM}.nc |
---|
182 | cp restartpem.nc starts/startpem_${iPEM}.nc |
---|
183 | mv restartpem.nc startpem.nc |
---|
184 | cp restartfi_evol.nc starts/startfi_postPEM${iPEM}.nc |
---|
185 | mv restartfi_evol.nc startfi.nc |
---|
186 | if [ -f "restart_evol.nc" ]; then |
---|
187 | cp restart_evol.nc starts/restart_postPEM${iPEM}.nc |
---|
188 | mv restart_evol.nc start.nc |
---|
189 | elif [ -f "restart1D_evol.txt" ]; then |
---|
190 | cp restart1D_evol.txt starts/restart1D_postPEM${iPEM}.txt |
---|
191 | mv restart1D_evol.txt start1D.txt |
---|
192 | fi |
---|
193 | ((iPEM++)) |
---|
194 | read i_myear n_myear convert_years < tmp_PEMyears.txt |
---|
195 | echo "Done!" |
---|
196 | done |
---|
197 | |
---|
198 | # Delete the temporary file to manage years of the chained simulation |
---|
199 | rm tmp_PEMyears.txt |
---|
200 | |
---|
201 | # Restore the previous value of LC_NUMERIC |
---|
202 | LC_NUMERIC=$OLD_LC_NUMERIC |
---|
203 | |
---|
204 | #----------------- Preparation for relaunch if needed ------------------ |
---|
205 | #echo "Reinitializing starting files..." |
---|
206 | #cp starts/startfi.nc . |
---|
207 | #for file in profiles/*_0; do |
---|
208 | # cp $file ${file/_0/} |
---|
209 | #done |
---|
210 | #echo "Done!" |
---|
211 | |
---|
212 | date |
---|
213 | echo "The launching script has terminated." |
---|