1 |
C $Header: /u/gcmpack/MITgcm/model/src/the_main_loop.F,v 1.44 2003/11/04 18:40:58 edhill Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "PACKAGES_CONFIG.h" |
5 |
#include "CPP_OPTIONS.h" |
6 |
|
7 |
CBOP |
8 |
C !ROUTINE: THE_MAIN_LOOP |
9 |
C !INTERFACE: |
10 |
SUBROUTINE THE_MAIN_LOOP( mytime, myiter, mythid ) |
11 |
|
12 |
C !DESCRIPTION: \bv |
13 |
C *================================================================* |
14 |
C | SUBROUTINE the_main_loop |
15 |
C | o Run the ocean model and evaluate the specified cost function. |
16 |
C *================================================================* |
17 |
C | |
18 |
C | THE_MAIN_LOOP is the toplevel routine for the Tangent Linear and |
19 |
C | Adjoint Model Compiler (TAMC). For this purpose the initialization |
20 |
C | of the model was split into two parts. Those parameters that do |
21 |
C | not depend on a specific model run are set in INITIALISE_FIXED, |
22 |
C | whereas those that do depend on the specific realization are |
23 |
C | initialized in INITIALISE_VARIA. |
24 |
C | This routine is to be used in conjuction with the MITgcmuv |
25 |
C | checkpoint 37. |
26 |
C *================================================================* |
27 |
C \ev |
28 |
|
29 |
C !USES: |
30 |
IMPLICIT NONE |
31 |
C == Global variables == |
32 |
#include "SIZE.h" |
33 |
#include "EEPARAMS.h" |
34 |
#include "PARAMS.h" |
35 |
|
36 |
c************************************** |
37 |
#ifdef ALLOW_AUTODIFF_TAMC |
38 |
|
39 |
c These includes are needed for |
40 |
c AD-checkpointing. |
41 |
c They provide the fields to be stored. |
42 |
|
43 |
# include "GRID.h" |
44 |
# include "DYNVARS.h" |
45 |
# include "FFIELDS.h" |
46 |
# include "EOS.h" |
47 |
# include "GAD.h" |
48 |
# ifdef ALLOW_CD_CODE |
49 |
# include "CD_CODE_VARS.h" |
50 |
# endif |
51 |
# ifdef ALLOW_PASSIVE_TRACER |
52 |
# include "TR1.h" |
53 |
# endif |
54 |
# ifdef ALLOW_PTRACERS |
55 |
# include "PTRACERS.h" |
56 |
# endif |
57 |
# ifdef ALLOW_NONHYDROSTATIC |
58 |
# include "CG3D.h" |
59 |
# endif |
60 |
# ifdef EXACT_CONSERV |
61 |
# include "SURFACE.h" |
62 |
# endif |
63 |
# ifdef ALLOW_OBCS |
64 |
# include "OBCS.h" |
65 |
# endif |
66 |
# ifdef ALLOW_EXF |
67 |
# include "exf_fields.h" |
68 |
# include "exf_clim_fields.h" |
69 |
# ifdef ALLOW_BULKFORMULAE |
70 |
# include "exf_constants.h" |
71 |
# endif |
72 |
# endif /* ALLOW_EXF */ |
73 |
# ifdef ALLOW_SEAICE |
74 |
# include "SEAICE.h" |
75 |
# endif |
76 |
# ifdef ALLOW_DIVIDED_ADJOINT_MPI |
77 |
# include "mpif.h" |
78 |
# endif |
79 |
|
80 |
# include "tamc.h" |
81 |
# include "ctrl.h" |
82 |
# include "ctrl_dummy.h" |
83 |
# include "cost.h" |
84 |
|
85 |
#endif /* ALLOW_AUTODIFF_TAMC */ |
86 |
c************************************** |
87 |
|
88 |
C !INPUT/OUTPUT PARAMETERS: |
89 |
C == Routine arguments == |
90 |
C note: under the multi-threaded model myiter and |
91 |
C mytime are local variables passed around as routine |
92 |
C arguments. Although this is fiddly it saves the need to |
93 |
C impose additional synchronisation points when they are |
94 |
C updated. |
95 |
C myIter - iteration counter for this thread |
96 |
C myTime - time counter for this thread |
97 |
C myThid - thread number for this instance of the routine. |
98 |
INTEGER myThid |
99 |
INTEGER myIter |
100 |
_RL myTime |
101 |
|
102 |
C !LOCAL VARIABLES: |
103 |
C == Local variables == |
104 |
integer iloop |
105 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
106 |
integer ilev_1 |
107 |
integer ilev_2 |
108 |
integer ilev_3 |
109 |
integer max_lev2 |
110 |
integer max_lev3 |
111 |
#endif |
112 |
CEOP |
113 |
|
114 |
#ifdef ALLOW_DEBUG |
115 |
IF (debugMode) CALL DEBUG_ENTER('THE_MAIN_LOOP',myThid) |
116 |
#endif |
117 |
|
118 |
#ifdef ALLOW_AUTODIFF_TAMC |
119 |
c-- Initialize storage for the cost function evaluation. |
120 |
CADJ INIT dummytape = common, 1 |
121 |
c-- Initialize storage for the outermost loop. |
122 |
CADJ INIT tapelev3 = USER |
123 |
CADJ INIT tapelev_ini_bibj_k = USER |
124 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
125 |
nIter0 = INT( startTime/deltaTClock ) |
126 |
ikey_dynamics = 1 |
127 |
#endif |
128 |
CALL TIMER_START('ADJOINT SPIN-UP', mythid) |
129 |
#endif |
130 |
|
131 |
#ifdef ALLOW_DEBUG |
132 |
IF (debugMode) CALL DEBUG_CALL('INITIALISE_VARIA',myThid) |
133 |
#endif |
134 |
|
135 |
C-- Set initial conditions (variable arrays) |
136 |
CALL TIMER_START('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
137 |
CALL INITIALISE_VARIA( mythid ) |
138 |
CALL TIMER_STOP ('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
139 |
|
140 |
c-- Dump for start state. |
141 |
#ifdef ALLOW_DEBUG |
142 |
IF (debugMode) CALL DEBUG_CALL('WRITE_STATE',myThid) |
143 |
#endif |
144 |
CALL TIMER_START('WRITE_STATE [THE_MAIN_LOOP]', mythid) |
145 |
CALL WRITE_STATE( mytime, myiter, mythid ) |
146 |
CALL TIMER_STOP ('WRITE_STATE [THE_MAIN_LOOP]', mythid) |
147 |
|
148 |
#ifdef ALLOW_MONITOR |
149 |
#ifdef ALLOW_DEBUG |
150 |
IF (debugMode) CALL DEBUG_CALL('MONITOR',myThid) |
151 |
#endif |
152 |
C-- Check status of solution (statistics, cfl, etc...) |
153 |
CALL TIMER_START('MONITOR [THE_MAIN_LOOP]', mythid) |
154 |
CALL MONITOR( myIter, myTime, myThid ) |
155 |
CALL TIMER_STOP ('MONITOR [THE_MAIN_LOOP]', mythid) |
156 |
#endif /* ALLOW_MONITOR */ |
157 |
|
158 |
#ifdef ALLOW_AUTODIFF_TAMC |
159 |
CALL TIMER_STOP ('ADJOINT SPIN-UP', mythid) |
160 |
_BARRIER |
161 |
#endif |
162 |
|
163 |
c-- Do the model integration. |
164 |
CALL TIMER_START('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
165 |
|
166 |
c >>>>>>>>>>>>>>>>>>>>>>>>>>> LOOP <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
167 |
c >>>>>>>>>>>>>>>>>>>>>>>>>>> STARTS <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
168 |
|
169 |
#ifdef ALLOW_AUTODIFF_TAMC |
170 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
171 |
c-- Implement a three level checkpointing. For a two level |
172 |
c-- checkpointing delete the middle loop; for n levels (n > 3) |
173 |
c-- insert more loops. |
174 |
|
175 |
c-- Check the choice of the checkpointing parameters in relation |
176 |
c-- to nTimeSteps: (nchklev_1*nchklev_2*nchklev_3 .ge. nTimeSteps) |
177 |
if (nchklev_1*nchklev_2*nchklev_3 .lt. nTimeSteps) then |
178 |
print* |
179 |
print*, ' the_main_loop: TAMC checkpointing parameters' |
180 |
print*, ' nchklev_1*nchklev_2*nchklev_3 = ', |
181 |
& nchklev_1*nchklev_2*nchklev_3 |
182 |
print*, ' are not consistent with nTimeSteps = ', |
183 |
& nTimeSteps |
184 |
stop ' ... stopped in the_main_loop.' |
185 |
endif |
186 |
max_lev3=nTimeSteps/(nchklev_1*nchklev_2)+1 |
187 |
max_lev2=nTimeSteps/nchklev_1+1 |
188 |
|
189 |
c************************************** |
190 |
#ifdef ALLOW_DIVIDED_ADJOINT |
191 |
CADJ loop = divided |
192 |
#endif |
193 |
c************************************** |
194 |
|
195 |
do ilev_3 = 1,nchklev_3 |
196 |
if(ilev_3.le.max_lev3) then |
197 |
c************************************** |
198 |
#include "checkpoint_lev3_directives.h" |
199 |
c************************************** |
200 |
|
201 |
c-- Initialise storage for the middle loop. |
202 |
CADJ INIT tapelev2 = USER |
203 |
|
204 |
do ilev_2 = 1,nchklev_2 |
205 |
if(ilev_2.le.max_lev2) then |
206 |
c************************************** |
207 |
#include "checkpoint_lev2_directives.h" |
208 |
c************************************** |
209 |
|
210 |
c************************************** |
211 |
#ifdef ALLOW_AUTODIFF_TAMC |
212 |
c-- Initialize storage for the innermost loop. |
213 |
c-- Always check common block sizes for the checkpointing! |
214 |
c-- |
215 |
CADJ INIT comlev1 = COMMON,nchklev_1 |
216 |
CADJ INIT comlev1_bibj = COMMON,nchklev_1*nsx*nsy*nthreads_chkpt |
217 |
CADJ INIT comlev1_bibj_k = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt |
218 |
c-- |
219 |
#ifdef ALLOW_KPP |
220 |
CADJ INIT comlev1_kpp = COMMON,nchklev_1*nsx*nsy |
221 |
#endif /* ALLOW_KPP */ |
222 |
c-- |
223 |
#ifdef ALLOW_GMREDI |
224 |
CADJ INIT comlev1_gmredi_k_gad |
225 |
CADJ & = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass |
226 |
#endif /* ALLOW_GMREDI */ |
227 |
c-- |
228 |
#ifdef ALLOW_PTRACERS |
229 |
CADJ INIT comlev1_bibj_ptracers = COMMON, |
230 |
CADJ & nchklev_1*nsx*nsy*nthreads_chkpt*NUMBER_OF_PTRACERS |
231 |
#endif /* ALLOW_PTRACERS */ |
232 |
c-- |
233 |
#ifndef DISABLE_MULTIDIM_ADVECTION |
234 |
CADJ INIT comlev1_bibj_k_gad |
235 |
CADJ & = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass |
236 |
CADJ INIT comlev1_bibj_k_gad_pass |
237 |
CADJ & = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass*maxcube |
238 |
#endif /* DISABLE_MULTIDIM_ADVECTION */ |
239 |
c-- |
240 |
#if (defined (ALLOW_EXF) && defined (ALLOW_BULKFORMULAE)) |
241 |
CADJ INIT comlev1_exf_1 |
242 |
CADJ & = COMMON,nchklev_1*snx*nsx*sny*nsy*nthreads_chkpt |
243 |
CADJ INIT comlev1_exf_2 |
244 |
CADJ & = COMMON,niter_bulk*nchklev_1*snx*nsx*sny*nsy*nthreads_chkpt |
245 |
#endif /* ALLOW_BULKFORMULAE */ |
246 |
c-- |
247 |
#ifdef ALLOW_SEAICE |
248 |
# ifdef SEAICE_ALLOW_DYNAMICS |
249 |
CADJ INIT comlev1_lsr = COMMON,nchklev_1*2 |
250 |
# endif |
251 |
#endif /* ALLOW_SEAICE */ |
252 |
c-- |
253 |
#endif /* ALLOW_AUTODIFF_TAMC */ |
254 |
c************************************** |
255 |
|
256 |
do ilev_1 = 1,nchklev_1 |
257 |
|
258 |
c-- The if-statement below introduces a some flexibility in the |
259 |
c-- choice of the 3-tupel ( nchklev_1, nchklev_2, nchklev_3 ). |
260 |
c-- |
261 |
c-- Requirement: nchklev_1*nchklev_2*nchklev_3 .ge. nTimeSteps . |
262 |
|
263 |
iloop = (ilev_3 - 1)*nchklev_2*nchklev_1 + |
264 |
& (ilev_2 - 1)*nchklev_1 + ilev_1 |
265 |
|
266 |
if ( iloop .le. nTimeSteps ) then |
267 |
|
268 |
#else /* ALLOW_TAMC_CHECKPOINTING undefined */ |
269 |
c-- Initialise storage for reference trajectory without TAMC check- |
270 |
c-- pointing. |
271 |
CADJ INIT history = USER |
272 |
CADJ INIT comlev1_bibj = COMMON,nchklev_0*nsx*nsy*nthreads_chkpt |
273 |
CADJ INIT comlev1_bibj_k = COMMON,nchklev_0*nsx*nsy*nr*nthreads_chkpt |
274 |
CADJ INIT comlev1_kpp = COMMON,nchklev_0*nsx*nsy |
275 |
|
276 |
c-- Check the choice of the checkpointing parameters in relation |
277 |
c-- to nTimeSteps: (nchklev_0 .ge. nTimeSteps) |
278 |
if (nchklev_0 .lt. nTimeSteps) then |
279 |
print* |
280 |
print*, ' the_main_loop: TAMC checkpointing parameter ', |
281 |
& 'nchklev_0 = ', nchklev_0 |
282 |
print*, ' not consistent with nTimeSteps = ', |
283 |
& nTimeSteps |
284 |
stop ' ... stopped in the_main_loop.' |
285 |
endif |
286 |
|
287 |
DO iloop = 1, nTimeSteps |
288 |
|
289 |
#endif /* ALLOW_TAMC_CHECKPOINTING */ |
290 |
|
291 |
#else /* ALLOW_AUTODIFF_TAMC undefined */ |
292 |
|
293 |
c-- Start the main loop of adjoint_Objfunc. Automatic differentiation |
294 |
c-- NOT enabled. |
295 |
DO iloop = 1, nTimeSteps |
296 |
|
297 |
#endif /* ALLOW_AUTODIFF_TAMC */ |
298 |
|
299 |
c-- >>> Loop body start <<< |
300 |
|
301 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
302 |
nIter0 = INT( startTime/deltaTClock ) |
303 |
ikey_dynamics = ilev_1 |
304 |
#endif |
305 |
|
306 |
#ifdef ALLOW_DEBUG |
307 |
IF (debugMode) CALL DEBUG_CALL('FORWARD_STEP',myThid) |
308 |
#endif |
309 |
|
310 |
CALL TIMER_START('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
311 |
CALL FORWARD_STEP( iloop, mytime, myiter, mythid ) |
312 |
CALL TIMER_STOP ('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
313 |
|
314 |
#ifdef ALLOW_COST |
315 |
C-- compare model with data and compute cost function |
316 |
C-- this is done after exchanges to allow interpolation |
317 |
ceh3 perhaps needs an IF ( useCOST ) THEN |
318 |
CALL TIMER_START('COST_TILE [THE_MAIN_LOOP]',myThid) |
319 |
CALL COST_TILE ( myThid ) |
320 |
CALL TIMER_STOP ('COST_TILE [THE_MAIN_LOOP]',myThid) |
321 |
#endif |
322 |
|
323 |
c-- >>> Loop body end <<< |
324 |
|
325 |
#ifdef ALLOW_AUTODIFF_TAMC |
326 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
327 |
endif |
328 |
enddo |
329 |
endif |
330 |
enddo |
331 |
endif |
332 |
enddo |
333 |
#else |
334 |
enddo |
335 |
#endif |
336 |
|
337 |
#else |
338 |
enddo |
339 |
#endif |
340 |
|
341 |
#ifdef ALLOW_COST |
342 |
c-- Sum all cost function contributions. |
343 |
call TIMER_START('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
344 |
call COST_FINAL ( mythid ) |
345 |
call TIMER_STOP ('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
346 |
#endif |
347 |
|
348 |
_BARRIER |
349 |
CALL TIMER_STOP ('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
350 |
|
351 |
#ifdef ALLOW_DEBUG |
352 |
IF (debugMode) CALL DEBUG_LEAVE('THE_MAIN_LOOP',myThid) |
353 |
#endif |
354 |
|
355 |
END |