1 |
C $Header: /u/gcmpack/MITgcm/model/src/the_main_loop.F,v 1.93 2007/11/05 18:52:21 jmc Exp $ |
2 |
C $Name: $ |
3 |
|
4 |
#include "PACKAGES_CONFIG.h" |
5 |
#include "CPP_OPTIONS.h" |
6 |
#ifdef ALLOW_OBCS |
7 |
# include "OBCS_OPTIONS.h" |
8 |
#endif |
9 |
#ifdef ALLOW_SEAICE |
10 |
# include "SEAICE_OPTIONS.h" |
11 |
#endif |
12 |
#ifdef ALLOW_GMREDI |
13 |
# include "GMREDI_OPTIONS.h" |
14 |
#endif |
15 |
|
16 |
CBOP |
17 |
C !ROUTINE: THE_MAIN_LOOP |
18 |
C !INTERFACE: |
19 |
SUBROUTINE THE_MAIN_LOOP( myTime, myIter, myThid ) |
20 |
|
21 |
C !DESCRIPTION: \bv |
22 |
C *================================================================* |
23 |
C | SUBROUTINE the_main_loop |
24 |
C | o Run the ocean model and evaluate the specified cost function. |
25 |
C *================================================================* |
26 |
C | |
27 |
C | THE_MAIN_LOOP is the toplevel routine for the Tangent Linear and |
28 |
C | Adjoint Model Compiler (TAMC). |
29 |
C | For this purpose the initialization |
30 |
C | of the model was split into two parts. Those parameters that do |
31 |
C | not depend on a specific model run are set in INITIALISE_FIXED, |
32 |
C | whereas those that do depend on the specific realization are |
33 |
C | initialized in INITIALISE_VARIA. |
34 |
C | This routine is to be used in conjuction with the MITgcmuv |
35 |
C | checkpoint 37. |
36 |
C *================================================================* |
37 |
C \ev |
38 |
|
39 |
C !USES: |
40 |
IMPLICIT NONE |
41 |
C == Global variables == |
42 |
#include "SIZE.h" |
43 |
#include "EEPARAMS.h" |
44 |
#include "PARAMS.h" |
45 |
|
46 |
c************************************** |
47 |
#ifdef ALLOW_AUTODIFF_TAMC |
48 |
# ifndef ALLOW_AUTODIFF_OPENAD |
49 |
|
50 |
c These includes are needed for |
51 |
c AD-checkpointing. |
52 |
c They provide the fields to be stored. |
53 |
|
54 |
# include "GRID.h" |
55 |
# include "DYNVARS.h" |
56 |
# include "SURFACE.h" |
57 |
# include "FFIELDS.h" |
58 |
# include "EOS.h" |
59 |
# include "AUTODIFF.h" |
60 |
|
61 |
# ifdef ALLOW_GENERIC_ADVDIFF |
62 |
# include "GAD.h" |
63 |
# endif |
64 |
# ifdef ALLOW_MOM_FLUXFORM |
65 |
# include "MOM_FLUXFORM.h" |
66 |
# endif |
67 |
# ifdef ALLOW_CD_CODE |
68 |
# include "CD_CODE_VARS.h" |
69 |
# endif |
70 |
# ifdef ALLOW_PTRACERS |
71 |
# include "PTRACERS_SIZE.h" |
72 |
# include "PTRACERS_FIELDS.h" |
73 |
# endif |
74 |
# ifdef ALLOW_OBCS |
75 |
# include "OBCS.h" |
76 |
# ifdef ALLOW_PTRACERS |
77 |
# include "OBCS_PTRACERS.h" |
78 |
# endif |
79 |
# endif |
80 |
# ifdef ALLOW_EXF |
81 |
# include "EXF_FIELDS.h" |
82 |
# ifdef ALLOW_BULKFORMULAE |
83 |
# include "EXF_CONSTANTS.h" |
84 |
# endif |
85 |
# endif /* ALLOW_EXF */ |
86 |
# ifdef ALLOW_SEAICE |
87 |
# include "SEAICE.h" |
88 |
# include "SEAICE_COST.h" |
89 |
# endif |
90 |
# ifdef ALLOW_THSICE |
91 |
# include "THSICE_SIZE.h" |
92 |
# include "THSICE_PARAMS.h" |
93 |
# include "THSICE_VARS.h" |
94 |
# endif |
95 |
# ifdef ALLOW_EBM |
96 |
# include "EBM.h" |
97 |
# endif |
98 |
# ifdef ALLOW_RBCS |
99 |
# include "RBCS.h" |
100 |
# endif |
101 |
# ifdef ALLOW_DIVIDED_ADJOINT_MPI |
102 |
# include "mpif.h" |
103 |
# endif |
104 |
|
105 |
# include "tamc.h" |
106 |
# endif /* undef ALLOW_AUTODIFF_OPENAD */ |
107 |
|
108 |
# include "ctrl.h" |
109 |
# include "ctrl_dummy.h" |
110 |
# include "cost.h" |
111 |
|
112 |
#endif /* ALLOW_AUTODIFF_TAMC */ |
113 |
c************************************** |
114 |
|
115 |
C !INPUT/OUTPUT PARAMETERS: |
116 |
C == Routine arguments == |
117 |
C note: under the multi-threaded model myiter and |
118 |
C mytime are local variables passed around as routine |
119 |
C arguments. Although this is fiddly it saves the need to |
120 |
C impose additional synchronisation points when they are |
121 |
C updated. |
122 |
C myIter - iteration counter for this thread |
123 |
C myTime - time counter for this thread |
124 |
C myThid - thread number for this instance of the routine. |
125 |
INTEGER myThid |
126 |
INTEGER myIter |
127 |
_RL myTime |
128 |
|
129 |
C !FUNCTIONS: |
130 |
C == Functions == |
131 |
|
132 |
C !LOCAL VARIABLES: |
133 |
C == Local variables == |
134 |
integer iloop |
135 |
#ifdef ALLOW_AUTODIFF_TAMC |
136 |
# ifdef ALLOW_AUTODIFF_OPENAD |
137 |
integer uCheckLev1, uCheckLev2, uCheckLev3,uCheckLev4 |
138 |
integer ilev_4 |
139 |
integer theCurrentStep |
140 |
# endif |
141 |
#endif |
142 |
|
143 |
CEOP |
144 |
|
145 |
#ifdef ALLOW_DEBUG |
146 |
IF (debugMode) CALL DEBUG_ENTER('THE_MAIN_LOOP',myThid) |
147 |
#endif |
148 |
|
149 |
#ifdef ALLOW_AUTODIFF_TAMC |
150 |
c-- Initialize storage for the cost function evaluation. |
151 |
CADJ INIT dummytape = common, 1 |
152 |
c-- Initialize storage for the outermost loop. |
153 |
CADJ INIT tapelev_ini_bibj_k = USER |
154 |
CADJ INIT tapelev_init = USER |
155 |
c |
156 |
# if (defined (AUTODIFF_2_LEVEL_CHECKPOINT)) |
157 |
CADJ INIT tapelev2 = USER |
158 |
# elif (defined (AUTODIFF_4_LEVEL_CHECKPOINT)) |
159 |
CADJ INIT tapelev4 = USER |
160 |
# else |
161 |
CADJ INIT tapelev3 = USER |
162 |
# endif |
163 |
#endif |
164 |
|
165 |
#ifdef ALLOW_AUTODIFF |
166 |
nIter0 = NINT( (startTime-baseTime)/deltaTClock ) |
167 |
ikey_dynamics = 1 |
168 |
#endif |
169 |
|
170 |
#ifdef ALLOW_AUTODIFF_TAMC |
171 |
# ifdef NONLIN_FRSURF |
172 |
CADJ STORE hFacC = tapelev_init, key = 1 |
173 |
# endif |
174 |
#endif |
175 |
|
176 |
#ifdef ALLOW_AUTODIFF_OPENAD |
177 |
c$openad INDEPENDENT(xx_theta) |
178 |
c$openad INDEPENDENT(xx_salt) |
179 |
#endif |
180 |
|
181 |
#ifdef ALLOW_DEBUG |
182 |
IF (debugMode) CALL DEBUG_CALL('INITIALISE_VARIA',myThid) |
183 |
#endif |
184 |
|
185 |
C-- Set initial conditions (variable arrays) |
186 |
CALL TIMER_START('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
187 |
CALL INITIALISE_VARIA( mythid ) |
188 |
CALL TIMER_STOP ('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
189 |
|
190 |
#ifdef ALLOW_SHOWFLOPS |
191 |
CALL TIMER_START('SHOWFLOPS_INIT [THE_MAIN_LOOP]', mythid) |
192 |
CALL SHOWFLOPS_INIT( myThid ) |
193 |
CALL TIMER_STOP('SHOWFLOPS_INIT [THE_MAIN_LOOP]', mythid) |
194 |
#endif |
195 |
|
196 |
c-- Do the model integration. |
197 |
CALL TIMER_START('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
198 |
|
199 |
c >>>>>>>>>>>>>>>>>>>>>>>>>>> LOOP <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
200 |
c >>>>>>>>>>>>>>>>>>>>>>>>>>> STARTS <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
201 |
|
202 |
c++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
203 |
#ifndef ALLOW_AUTODIFF_OPENAD |
204 |
c++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
205 |
# ifdef ALLOW_AUTODIFF_TAMC |
206 |
# ifdef ALLOW_TAMC_CHECKPOINTING |
207 |
|
208 |
max_lev4=nTimeSteps/(nchklev_1*nchklev_2*nchklev_3)+1 |
209 |
max_lev3=nTimeSteps/(nchklev_1*nchklev_2)+1 |
210 |
max_lev2=nTimeSteps/nchklev_1+1 |
211 |
|
212 |
c************************************** |
213 |
# ifdef ALLOW_DIVIDED_ADJOINT |
214 |
CADJ loop = divided |
215 |
# endif |
216 |
c************************************** |
217 |
|
218 |
# ifdef AUTODIFF_4_LEVEL_CHECKPOINT |
219 |
do ilev_4 = 1,nchklev_4 |
220 |
if(ilev_4.le.max_lev4) then |
221 |
c************************************** |
222 |
CALL AUTODIFF_STORE( myThid ) |
223 |
#include "checkpoint_lev4_directives.h" |
224 |
CALL AUTODIFF_RESTORE( myThid ) |
225 |
c************************************** |
226 |
c-- Initialise storage for the middle loop. |
227 |
CADJ INIT tapelev3 = USER |
228 |
# endif /* AUTODIFF_4_LEVEL_CHECKPOINT */ |
229 |
|
230 |
# ifndef AUTODIFF_2_LEVEL_CHECKPOINT |
231 |
do ilev_3 = 1,nchklev_3 |
232 |
if(ilev_3.le.max_lev3) then |
233 |
c************************************** |
234 |
CALL AUTODIFF_STORE( myThid ) |
235 |
#include "checkpoint_lev3_directives.h" |
236 |
CALL AUTODIFF_RESTORE( myThid ) |
237 |
c************************************** |
238 |
c-- Initialise storage for the middle loop. |
239 |
CADJ INIT tapelev2 = USER |
240 |
# endif /* AUTODIFF_2_LEVEL_CHECKPOINT */ |
241 |
|
242 |
do ilev_2 = 1,nchklev_2 |
243 |
if(ilev_2.le.max_lev2) then |
244 |
c************************************** |
245 |
CALL AUTODIFF_STORE( myThid ) |
246 |
#include "checkpoint_lev2_directives.h" |
247 |
CALL AUTODIFF_RESTORE( myThid ) |
248 |
c************************************** |
249 |
|
250 |
c************************************** |
251 |
c-- |
252 |
c-- Initialize storage for the innermost loop. |
253 |
c-- Always check common block sizes for the checkpointing! |
254 |
c-- |
255 |
CADJ INIT comlev1 = COMMON,nchklev_1 |
256 |
CADJ INIT comlev1_bibj = COMMON,nchklev_1*nsx*nsy*nthreads_chkpt |
257 |
CADJ INIT comlev1_bibj_k = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt |
258 |
c-- |
259 |
# ifdef ALLOW_KPP |
260 |
CADJ INIT comlev1_kpp = COMMON,nchklev_1*nsx*nsy |
261 |
CADJ INIT comlev1_kpp_k = COMMON,nchklev_1*nsx*nsy*nr |
262 |
# endif /* ALLOW_KPP */ |
263 |
c-- |
264 |
# ifdef ALLOW_GMREDI |
265 |
CADJ INIT comlev1_gmredi_k_gad |
266 |
CADJ & = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass |
267 |
# endif /* ALLOW_GMREDI */ |
268 |
c-- |
269 |
# ifdef ALLOW_PTRACERS |
270 |
CADJ INIT comlev1_bibj_ptracers = COMMON, |
271 |
CADJ & nchklev_1*nsx*nsy*nthreads_chkpt*PTRACERS_num |
272 |
CADJ INIT comlev1_bibj_k_ptracers = COMMON, |
273 |
CADJ & nchklev_1*nsx*nsy*nthreads_chkpt*PTRACERS_num*nr |
274 |
# endif /* ALLOW_PTRACERS */ |
275 |
c-- |
276 |
# ifndef DISABLE_MULTIDIM_ADVECTION |
277 |
CADJ INIT comlev1_bibj_k_gad = COMMON, |
278 |
CADJ & nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass |
279 |
CADJ INIT comlev1_bibj_k_gad_pass = COMMON, |
280 |
CADJ & nchklev_1*nsx*nsy*nr*nthreads_chkpt*maxpass*maxpass |
281 |
# endif /* DISABLE_MULTIDIM_ADVECTION */ |
282 |
c-- |
283 |
# if (defined (ALLOW_EXF) && defined (ALLOW_BULKFORMULAE)) |
284 |
CADJ INIT comlev1_exf_1 |
285 |
CADJ & = COMMON,nchklev_1*snx*nsx*sny*nsy*nthreads_chkpt |
286 |
CADJ INIT comlev1_exf_2 |
287 |
CADJ & = COMMON,niter_bulk*nchklev_1*snx*nsx*sny*nsy*nthreads_chkpt |
288 |
# endif /* ALLOW_BULKFORMULAE */ |
289 |
c-- |
290 |
# ifdef ALLOW_SEAICE |
291 |
# ifdef SEAICE_ALLOW_DYNAMICS |
292 |
CADJ INIT comlev1_lsr = COMMON,nchklev_1*2 |
293 |
# endif |
294 |
# ifdef SEAICE_ALLOW_EVP |
295 |
CADJ INIT comlev1_evp = COMMON,nchklev_1 |
296 |
# endif |
297 |
# endif /* ALLOW_SEAICE */ |
298 |
c-- |
299 |
# ifdef ALLOW_THSICE |
300 |
CADJ INIT comlev1_thsice_1 |
301 |
CADJ & = COMMON,nchklev_1*snx*nsx*sny*nsy*nthreads_chkpt |
302 |
CADJ INIT comlev1_thsice_2 |
303 |
CADJ & = COMMON,nchklev_1*snx*nsx*sny*nsy*nlyr*nthreads_chkpt |
304 |
CADJ INIT comlev1_thsice_3 |
305 |
CADJ & = COMMON,nchklev_1*snx*nsx*sny*nsy*MaxTsf*nthreads_chkpt |
306 |
CADJ INIT comlev1_thsice_4 |
307 |
CADJ & = COMMON,nchklev_1*nsx*nsy*maxpass*nthreads_chkpt |
308 |
# endif /* ALLOW_THSICE */ |
309 |
c-- |
310 |
# ifdef ALLOW_DEPTH_CONTROL |
311 |
CADJ INIT comlev1_cg2d |
312 |
CADJ & = COMMON,nchklev_1*nthreads_chkpt |
313 |
CADJ INIT comlev1_cg2d_iter |
314 |
CADJ & = COMMON,nchklev_1*nthreads_chkpt*numItersMax |
315 |
# endif |
316 |
c-- |
317 |
c************************************** |
318 |
|
319 |
do ilev_1 = 1,nchklev_1 |
320 |
|
321 |
c-- The if-statement below introduces a some flexibility in the |
322 |
c-- choice of the 3-tupel ( nchklev_1, nchklev_2, nchklev_3 ). |
323 |
|
324 |
iloop = (ilev_2 - 1)*nchklev_1 + ilev_1 |
325 |
# ifndef AUTODIFF_2_LEVEL_CHECKPOINT |
326 |
& + (ilev_3 - 1)*nchklev_2*nchklev_1 |
327 |
# endif |
328 |
# ifdef AUTODIFF_4_LEVEL_CHECKPOINT |
329 |
& + (ilev_4 - 1)*nchklev_3*nchklev_2*nchklev_1 |
330 |
# endif |
331 |
|
332 |
if ( iloop .le. nTimeSteps ) then |
333 |
|
334 |
# else /* ALLOW_TAMC_CHECKPOINTING undefined */ |
335 |
c-- Initialise storage for reference trajectory without TAMC check- |
336 |
c-- pointing. |
337 |
CADJ INIT history = USER |
338 |
CADJ INIT comlev1_bibj = COMMON,nchklev_0*nsx*nsy*nthreads_chkpt |
339 |
CADJ INIT comlev1_bibj_k = COMMON,nchklev_0*nsx*nsy*nr*nthreads_chkpt |
340 |
CADJ INIT comlev1_kpp = COMMON,nchklev_0*nsx*nsy |
341 |
|
342 |
c-- Check the choice of the checkpointing parameters in relation |
343 |
c-- to nTimeSteps: (nchklev_0 .ge. nTimeSteps) |
344 |
if (nchklev_0 .lt. nTimeSteps) then |
345 |
print* |
346 |
print*, ' the_main_loop: TAMC checkpointing parameter ', |
347 |
& 'nchklev_0 = ', nchklev_0 |
348 |
print*, ' not consistent with nTimeSteps = ', |
349 |
& nTimeSteps |
350 |
stop ' ... stopped in the_main_loop.' |
351 |
endif |
352 |
|
353 |
DO iloop = 1, nTimeSteps |
354 |
|
355 |
# endif /* ALLOW_TAMC_CHECKPOINTING */ |
356 |
# endif /* ALLOW_AUTODIFF_TAMC */ |
357 |
|
358 |
#endif /* undef ALLOW_AUTODIFF_OPENAD */ |
359 |
|
360 |
#ifdef ALLOW_AUTODIFF_OPENAD |
361 |
call openad_checkpointinit(uCheckLev1, |
362 |
+ uCheckLev2, |
363 |
+ uCheckLev3, |
364 |
+ uCheckLev4 ) |
365 |
|
366 |
theCurrentStep=0 |
367 |
|
368 |
if (uCheckLev4 .gt. 0 ) then |
369 |
do ilev_4 = 1, uCheckLev4 |
370 |
|
371 |
#endif |
372 |
|
373 |
#ifndef ALLOW_AUTODIFF |
374 |
|
375 |
c-- Start the main loop of adjoint_Objfunc. Automatic differentiation |
376 |
c-- NOT enabled. |
377 |
DO iloop = 1, nTimeSteps |
378 |
|
379 |
#endif /* ALLOW_AUTODIFF */ |
380 |
|
381 |
c-- >>> Loop body start <<< |
382 |
|
383 |
#ifdef ALLOW_AUTODIFF_TAMC |
384 |
nIter0 = NINT( (startTime-baseTime)/deltaTClock ) |
385 |
ikey_dynamics = ilev_1 |
386 |
#endif |
387 |
|
388 |
#ifdef ALLOW_DEBUG |
389 |
IF (debugMode) CALL DEBUG_CALL('FORWARD_STEP',myThid) |
390 |
#endif |
391 |
|
392 |
#ifdef ALLOW_ATM2D |
393 |
CALL TIMER_START('FORWARD_STEP_ATM2D [THE_MAIN_LOOP]',mythid) |
394 |
CALL FORWARD_STEP_ATM2D( iloop, mytime, myiter, mythid ) |
395 |
CALL TIMER_STOP ('FORWARD_STEP_ATM2D [THE_MAIN_LOOP]',mythid) |
396 |
#else |
397 |
CALL TIMER_START('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
398 |
# ifdef ALLOW_AUTODIFF_OPENAD |
399 |
CALL THE_FOURTH_LEVEL_LOOP( ilev_4, |
400 |
+uCheckLev1, uCheckLev2, uCheckLev3,uCheckLev4, |
401 |
+theCurrentStep, |
402 |
+myTime, myIter, myThid ) |
403 |
# else |
404 |
CALL FORWARD_STEP( iloop, mytime, myiter, mythid ) |
405 |
# endif |
406 |
CALL TIMER_STOP ('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
407 |
#endif |
408 |
|
409 |
c-- >>> Loop body end <<< |
410 |
#ifdef ALLOW_AUTODIFF |
411 |
# ifndef ALLOW_AUTODIFF_OPENAD |
412 |
# ifdef ALLOW_AUTODIFF_TAMC |
413 |
|
414 |
# ifdef ALLOW_TAMC_CHECKPOINTING |
415 |
endif |
416 |
enddo |
417 |
endif |
418 |
enddo |
419 |
# ifndef AUTODIFF_2_LEVEL_CHECKPOINT |
420 |
endif |
421 |
enddo |
422 |
# endif |
423 |
# ifdef AUTODIFF_4_LEVEL_CHECKPOINT |
424 |
endif |
425 |
enddo |
426 |
# endif |
427 |
# else /* ndef ALLOW_TAMC_CHECKPOINTING */ |
428 |
enddo |
429 |
# endif /* ALLOW_TAMC_CHECKPOINTING */ |
430 |
# endif /* ALLOW_AUTODIFF_TAMC */ |
431 |
# else /* ALLOW_AUTODIFF_OPENAD */ |
432 |
end do |
433 |
else |
434 |
CALL THE_FOURTH_LEVEL_PLAIN( |
435 |
+uCheckLev1, uCheckLev2, uCheckLev3,uCheckLev4, |
436 |
+theCurrentStep, |
437 |
+myTime, myIter, myThid ) |
438 |
end if |
439 |
# endif /* ALLOW_AUTODIFF_OPENAD */ |
440 |
#else /* ALLOW_AUTODIFF */ |
441 |
enddo |
442 |
#endif /* ALLOW_AUTODIFF */ |
443 |
|
444 |
#ifdef ALLOW_PROFILES |
445 |
c-- Accumulate in-situ time averages of temperature, salinity, SSH. |
446 |
CALL TIMER_START('PROFILES_INLOOP [THE_MAIN_LOOP]', mythid) |
447 |
CALL PROFILES_INLOOP( mytime, mythid ) |
448 |
CALL TIMER_STOP ('PROFILES_INLOOP [THE_MAIN_LOOP]', mythid) |
449 |
#endif |
450 |
|
451 |
#ifdef ALLOW_COST |
452 |
c-- Sum all cost function contributions. |
453 |
CALL TIMER_START('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
454 |
CALL COST_FINAL ( mythid ) |
455 |
CALL TIMER_STOP ('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
456 |
|
457 |
# ifdef ALLOW_AUTODIFF_OPENAD |
458 |
c$openad DEPENDENT(fc) |
459 |
# endif /* ALLOW_AUTODIFF_OPENAD */ |
460 |
|
461 |
#endif /* ALLOW_COST */ |
462 |
|
463 |
_BARRIER |
464 |
CALL TIMER_STOP ('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
465 |
|
466 |
#ifdef ALLOW_DEBUG |
467 |
IF (debugMode) CALL DEBUG_LEAVE('THE_MAIN_LOOP',myThid) |
468 |
#endif |
469 |
|
470 |
END |
471 |
|