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