1 |
cnh |
1.18 |
C $Header: /u/gcmpack/models/MITgcmUV/model/src/the_main_loop.F,v 1.17 2001/08/21 16:21:17 heimbach Exp $ |
2 |
adcroft |
1.1 |
|
3 |
|
|
#include "CPP_OPTIONS.h" |
4 |
|
|
|
5 |
cnh |
1.18 |
CBOP |
6 |
|
|
C !ROUTINE: THE_MAIN_LOOP |
7 |
|
|
C !INTERFACE: |
8 |
|
|
SUBROUTINE THE_MAIN_LOOP( mytime, myiter, mythid ) |
9 |
|
|
|
10 |
|
|
C !DESCRIPTION: \bv |
11 |
|
|
C *================================================================* |
12 |
|
|
C | SUBROUTINE the_main_loop |
13 |
|
|
C | o Run the ocean model and evaluate the specified cost function. |
14 |
|
|
C *================================================================* |
15 |
|
|
C | |
16 |
|
|
C | THE_MAIN_LOOP is the toplevel routine for the Tangent Linear and |
17 |
|
|
C | Adjoint Model Compiler (TAMC). For this purpose the initialization |
18 |
|
|
C | of the model was split into two parts. Those parameters that do |
19 |
|
|
C | not depend on a specific model run are set in INITIALISE_FIXED, |
20 |
|
|
C | whereas those that do depend on the specific realization are |
21 |
|
|
C | initialized in INITIALISE_VARIA. |
22 |
|
|
C | This routine is to be used in conjuction with the MITgcmuv |
23 |
|
|
C | checkpoint 37. |
24 |
|
|
C *================================================================* |
25 |
|
|
C \ev |
26 |
|
|
|
27 |
|
|
C !USES: |
28 |
|
|
IMPLICIT NONE |
29 |
|
|
C == Global variables == |
30 |
adcroft |
1.1 |
#include "SIZE.h" |
31 |
|
|
#include "EEPARAMS.h" |
32 |
|
|
#include "PARAMS.h" |
33 |
heimbach |
1.6 |
#ifdef ALLOW_AUTODIFF_TAMC |
34 |
heimbach |
1.16 |
# include "tamc.h" |
35 |
|
|
# include "ctrl.h" |
36 |
|
|
# include "ctrl_dummy.h" |
37 |
|
|
# include "cost.h" |
38 |
|
|
# include "DYNVARS.h" |
39 |
|
|
# include "FFIELDS.h" |
40 |
|
|
# ifdef ALLOW_PASSIVE_TRACER |
41 |
|
|
# include "TR1.h" |
42 |
|
|
# endif |
43 |
|
|
# ifdef ALLOW_NONHYDROSTATIC |
44 |
|
|
# include "CG3D.h" |
45 |
|
|
# endif |
46 |
heimbach |
1.6 |
#endif |
47 |
|
|
|
48 |
cnh |
1.18 |
C !INPUT/OUTPUT PARAMETERS: |
49 |
|
|
C == Routine arguments == |
50 |
|
|
C note: under the multi-threaded model myiter and |
51 |
|
|
C mytime are local variables passed around as routine |
52 |
|
|
C arguments. Although this is fiddly it saves the need to |
53 |
|
|
C impose additional synchronisation points when they are |
54 |
|
|
C updated. |
55 |
|
|
C myIter - iteration counter for this thread |
56 |
|
|
C myTime - time counter for this thread |
57 |
|
|
C myThid - thread number for this instance of the routine. |
58 |
|
|
INTEGER myThid |
59 |
|
|
INTEGER myIter |
60 |
|
|
_RL myTime |
61 |
heimbach |
1.6 |
|
62 |
cnh |
1.18 |
C !LOCAL VARIABLES: |
63 |
|
|
C == Local variables == |
64 |
heimbach |
1.6 |
integer iloop |
65 |
|
|
#ifdef ALLOW_TAMC_CHECKPOINTING |
66 |
|
|
integer ilev_1 |
67 |
|
|
integer ilev_2 |
68 |
|
|
integer ilev_3 |
69 |
|
|
integer max_lev2 |
70 |
|
|
integer max_lev3 |
71 |
|
|
#endif |
72 |
cnh |
1.18 |
CEOP |
73 |
adcroft |
1.1 |
|
74 |
heimbach |
1.6 |
#ifdef ALLOW_AUTODIFF_TAMC |
75 |
|
|
c-- Initialize storage for the cost function evaluation. |
76 |
|
|
CADJ INIT dummytape = common, 1 |
77 |
|
|
c-- Initialize storage for the outermost loop. |
78 |
|
|
CADJ INIT tapelev3 = USER |
79 |
heimbach |
1.7 |
#ifdef ALLOW_TAMC_CHECKPOINTING |
80 |
heimbach |
1.13 |
nIter0 = INT( startTime/deltaTClock ) |
81 |
heimbach |
1.7 |
ikey_dynamics = 1 |
82 |
|
|
#endif |
83 |
adcroft |
1.10 |
CALL TIMER_START('ADJOINT SPIN-UP', mythid) |
84 |
heimbach |
1.6 |
#endif |
85 |
|
|
|
86 |
adcroft |
1.1 |
C-- Set initial conditions (variable arrays) |
87 |
adcroft |
1.10 |
CALL TIMER_START('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
88 |
heimbach |
1.6 |
CALL INITIALISE_VARIA( mythid ) |
89 |
adcroft |
1.10 |
CALL TIMER_STOP ('INITIALISE_VARIA [THE_MAIN_LOOP]', mythid) |
90 |
heimbach |
1.6 |
|
91 |
|
|
#ifndef ALLOW_AUTODIFF_TAMC |
92 |
|
|
c-- Dump for start state. |
93 |
adcroft |
1.10 |
CALL TIMER_START('WRITE_STATE [THE_MAIN_LOOP]', mythid) |
94 |
heimbach |
1.6 |
CALL WRITE_STATE( mytime, myiter, mythid ) |
95 |
adcroft |
1.10 |
CALL TIMER_STOP ('WRITE_STATE [THE_MAIN_LOOP]', mythid) |
96 |
heimbach |
1.6 |
#endif |
97 |
adcroft |
1.9 |
|
98 |
|
|
#ifndef EXCLUDE_MONITOR |
99 |
|
|
C-- Check status of solution (statistics, cfl, etc...) |
100 |
adcroft |
1.10 |
CALL TIMER_START('MONITOR [THE_MAIN_LOOP]', mythid) |
101 |
adcroft |
1.9 |
CALL MONITOR( myIter, myTime, myThid ) |
102 |
adcroft |
1.10 |
CALL TIMER_STOP ('MONITOR [THE_MAIN_LOOP]', mythid) |
103 |
adcroft |
1.9 |
#endif /* EXCLUDE_MONITOR */ |
104 |
heimbach |
1.6 |
|
105 |
heimbach |
1.12 |
#ifdef ALLOW_ADJOINT_RUN |
106 |
heimbach |
1.6 |
c-- Add control vector for forcing and parameter fields |
107 |
|
|
CALL CTRL_MAP_FORCING (mythid) |
108 |
|
|
#endif |
109 |
|
|
|
110 |
adcroft |
1.10 |
#ifdef ALLOW_AUTODIFF_TAMC |
111 |
heimbach |
1.6 |
CALL TIMER_STOP ('ADJOINT SPIN-UP', mythid) |
112 |
|
|
_BARRIER |
113 |
adcroft |
1.10 |
#endif |
114 |
heimbach |
1.6 |
|
115 |
|
|
c-- Do the model integration. |
116 |
adcroft |
1.10 |
CALL TIMER_START('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
117 |
heimbach |
1.6 |
|
118 |
|
|
c >>>>>>>>>>>>>>>>>>>>>>>>>>> LOOP <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
119 |
|
|
c >>>>>>>>>>>>>>>>>>>>>>>>>>> STARTS <<<<<<<<<<<<<<<<<<<<<<<<<<<< |
120 |
|
|
|
121 |
|
|
#ifdef ALLOW_AUTODIFF_TAMC |
122 |
|
|
#ifdef ALLOW_TAMC_CHECKPOINTING |
123 |
|
|
c-- Implement a three level checkpointing. For a two level |
124 |
|
|
c-- checkpointing delete the middle loop; for n levels (n > 3) |
125 |
|
|
c-- insert more loops. |
126 |
|
|
|
127 |
|
|
c-- Check the choice of the checkpointing parameters in relation |
128 |
|
|
c-- to nTimeSteps: (nchklev_1*nchklev_2*nchklev_3 .ge. nTimeSteps) |
129 |
|
|
if (nchklev_1*nchklev_2*nchklev_3 .lt. nTimeSteps) then |
130 |
|
|
print* |
131 |
|
|
print*, ' the_main_loop: TAMC checkpointing parameters' |
132 |
|
|
print*, ' nchklev_1*nchklev_2*nchklev_3 = ', |
133 |
|
|
& nchklev_1*nchklev_2*nchklev_3 |
134 |
|
|
print*, ' are not consistent with nTimeSteps = ', |
135 |
|
|
& nTimeSteps |
136 |
|
|
stop ' ... stopped in the_main_loop.' |
137 |
|
|
endif |
138 |
|
|
max_lev3=nTimeSteps/(nchklev_1*nchklev_2)+1 |
139 |
|
|
max_lev2=nTimeSteps/nchklev_1+1 |
140 |
|
|
|
141 |
|
|
do ilev_3 = 1,nchklev_3 |
142 |
|
|
if(ilev_3.le.max_lev3) then |
143 |
|
|
CADJ STORE gsnm1 = tapelev3, key = ilev_3 |
144 |
|
|
CADJ STORE gtnm1 = tapelev3, key = ilev_3 |
145 |
|
|
CADJ STORE gunm1 = tapelev3, key = ilev_3 |
146 |
|
|
CADJ STORE gvnm1 = tapelev3, key = ilev_3 |
147 |
|
|
CADJ STORE theta = tapelev3, key = ilev_3 |
148 |
|
|
CADJ STORE salt = tapelev3, key = ilev_3 |
149 |
|
|
CADJ STORE uvel = tapelev3, key = ilev_3 |
150 |
|
|
CADJ STORE vvel = tapelev3, key = ilev_3 |
151 |
|
|
CADJ STORE wvel = tapelev3, key = ilev_3 |
152 |
|
|
CADJ STORE etan = tapelev3, key = ilev_3 |
153 |
|
|
CADJ STORE etanm1 = tapelev3, key = ilev_3 |
154 |
heimbach |
1.17 |
CADJ STORE gu = tapelev3, key = ilev_3 |
155 |
|
|
CADJ STORE gv = tapelev3, key = ilev_3 |
156 |
heimbach |
1.6 |
#ifdef INCLUDE_CD_CODE |
157 |
|
|
CADJ STORE uveld = tapelev3, key = ilev_3 |
158 |
|
|
CADJ STORE vveld = tapelev3, key = ilev_3 |
159 |
|
|
CADJ STORE unm1 = tapelev3, key = ilev_3 |
160 |
|
|
CADJ STORE vnm1 = tapelev3, key = ilev_3 |
161 |
heimbach |
1.17 |
CADJ STORE gucd = tapelev3, key = ilev_3 |
162 |
|
|
CADJ STORE gvcd = tapelev3, key = ilev_3 |
163 |
heimbach |
1.6 |
#endif |
164 |
heimbach |
1.12 |
#ifdef ALLOW_COST_TRACER |
165 |
|
|
CADJ STORE tr1 = tapelev3, key = ilev_3 |
166 |
|
|
CADJ STORE gtr1nm1 = tapelev3, key = ilev_3 |
167 |
|
|
#endif |
168 |
heimbach |
1.6 |
|
169 |
|
|
c-- Initialise storage for the middle loop. |
170 |
|
|
CADJ INIT tapelev2 = USER |
171 |
|
|
|
172 |
|
|
do ilev_2 = 1,nchklev_2 |
173 |
|
|
if(ilev_2.le.max_lev2) then |
174 |
|
|
CADJ STORE gsnm1 = tapelev2, key = ilev_2 |
175 |
|
|
CADJ STORE gtnm1 = tapelev2, key = ilev_2 |
176 |
|
|
CADJ STORE gunm1 = tapelev2, key = ilev_2 |
177 |
|
|
CADJ STORE gvnm1 = tapelev2, key = ilev_2 |
178 |
|
|
CADJ STORE theta = tapelev2, key = ilev_2 |
179 |
|
|
CADJ STORE salt = tapelev2, key = ilev_2 |
180 |
|
|
CADJ STORE uvel = tapelev2, key = ilev_2 |
181 |
|
|
CADJ STORE vvel = tapelev2, key = ilev_2 |
182 |
|
|
CADJ STORE wvel = tapelev2, key = ilev_2 |
183 |
|
|
CADJ STORE etan = tapelev2, key = ilev_2 |
184 |
|
|
CADJ STORE etanm1 = tapelev2, key = ilev_2 |
185 |
heimbach |
1.17 |
CADJ STORE gu = tapelev2, key = ilev_2 |
186 |
|
|
CADJ STORE gv = tapelev2, key = ilev_2 |
187 |
heimbach |
1.6 |
#ifdef INCLUDE_CD_CODE |
188 |
|
|
CADJ STORE uveld = tapelev2, key = ilev_2 |
189 |
|
|
CADJ STORE vveld = tapelev2, key = ilev_2 |
190 |
|
|
CADJ STORE unm1 = tapelev2, key = ilev_2 |
191 |
|
|
CADJ STORE vnm1 = tapelev2, key = ilev_2 |
192 |
heimbach |
1.17 |
CADJ STORE gucd = tapelev2, key = ilev_2 |
193 |
|
|
CADJ STORE gvcd = tapelev2, key = ilev_2 |
194 |
heimbach |
1.6 |
#endif |
195 |
heimbach |
1.12 |
#ifdef ALLOW_COST_TRACER |
196 |
|
|
CADJ STORE tr1 = tapelev2, key = ilev_2 |
197 |
|
|
CADJ STORE gtr1nm1 = tapelev2, key = ilev_2 |
198 |
|
|
#endif |
199 |
heimbach |
1.6 |
|
200 |
|
|
c-- Initialize storage for the innermost loop. |
201 |
|
|
c-- Always check common block sizes for the checkpointing! |
202 |
|
|
CADJ INIT comlev1 = COMMON,nchklev_1 |
203 |
|
|
CADJ INIT comlev1_bibj = COMMON,nchklev_1*nsx*nsy*nthreads_chkpt |
204 |
|
|
CADJ INIT comlev1_bibj_k = COMMON,nchklev_1*nsx*nsy*nr*nthreads_chkpt |
205 |
|
|
CADJ INIT comlev1_kpp = COMMON,nchklev_1*nsx*nsy |
206 |
|
|
|
207 |
|
|
C-- RG replace 2 by max of num_v_smooth_Ri |
208 |
|
|
CADJ INIT comlev1_kpp_sm = COMMON,nchklev_1*nsx*nsy*2 |
209 |
|
|
|
210 |
|
|
do ilev_1 = 1,nchklev_1 |
211 |
|
|
|
212 |
|
|
c-- The if-statement below introduces a some flexibility in the |
213 |
|
|
c-- choice of the 3-tupel ( nchklev_1, nchklev_2, nchklev_3 ). |
214 |
|
|
c-- |
215 |
|
|
c-- Requirement: nchklev_1*nchklev_2*nchklev_3 .ge. nTimeSteps . |
216 |
|
|
|
217 |
|
|
iloop = (ilev_3 - 1)*nchklev_2*nchklev_1 + |
218 |
|
|
& (ilev_2 - 1)*nchklev_1 + ilev_1 |
219 |
|
|
|
220 |
|
|
if ( iloop .le. nTimeSteps ) then |
221 |
|
|
|
222 |
|
|
#else /* ALLOW_TAMC_CHECKPOINTING undefined */ |
223 |
|
|
c-- Initialise storage for reference trajectory without TAMC check- |
224 |
|
|
c-- pointing. |
225 |
|
|
CADJ INIT history = USER |
226 |
|
|
CADJ INIT comlev1_bibj = COMMON,nchklev_0*nsx*nsy*nthreads_chkpt |
227 |
|
|
CADJ INIT comlev1_bibj_k = COMMON,nchklev_0*nsx*nsy*nr*nthreads_chkpt |
228 |
|
|
CADJ INIT comlev1_kpp = COMMON,nchklev_0*nsx*nsy |
229 |
|
|
|
230 |
|
|
C-- RG replace 2 by max of num_v_smooth_Ri |
231 |
|
|
CADJ INIT comlev1_kpp_sm = COMMON,nchklev_0*nsx*nsy*2 |
232 |
|
|
|
233 |
|
|
c-- Check the choice of the checkpointing parameters in relation |
234 |
|
|
c-- to nTimeSteps: (nchklev_0 .ge. nTimeSteps) |
235 |
|
|
if (nchklev_0 .lt. nTimeSteps) then |
236 |
|
|
print* |
237 |
|
|
print*, ' the_main_loop: TAMC checkpointing parameter ', |
238 |
|
|
& nchklev_0 = ', nchklev_0 |
239 |
|
|
print*, ' not consistent with nTimeSteps = ', |
240 |
|
|
& nTimeSteps |
241 |
|
|
stop ' ... stopped in the_main_loop.' |
242 |
|
|
endif |
243 |
|
|
|
244 |
adcroft |
1.10 |
DO iloop = 1, nTimeSteps |
245 |
heimbach |
1.6 |
|
246 |
|
|
#endif /* ALLOW_TAMC_CHECKPOINTING */ |
247 |
|
|
|
248 |
|
|
#else /* ALLOW_AUTODIFF_TAMC undefined */ |
249 |
|
|
|
250 |
|
|
c-- Start the main loop of adjoint_Objfunc. Automatic differentiation |
251 |
|
|
c-- NOT enabled. |
252 |
adcroft |
1.10 |
DO iloop = 1, nTimeSteps |
253 |
heimbach |
1.6 |
|
254 |
|
|
#endif /* ALLOW_AUTODIFF_TAMC */ |
255 |
|
|
|
256 |
adcroft |
1.10 |
c-- >>> Loop body start <<< |
257 |
heimbach |
1.6 |
|
258 |
|
|
#ifdef ALLOW_TAMC_CHECKPOINTING |
259 |
heimbach |
1.14 |
nIter0 = INT( startTime/deltaTClock ) |
260 |
heimbach |
1.13 |
ikey_dynamics = ilev_1 |
261 |
heimbach |
1.6 |
#endif |
262 |
heimbach |
1.14 |
|
263 |
|
|
|
264 |
heimbach |
1.13 |
CALL TIMER_START('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
265 |
|
|
CALL FORWARD_STEP( iloop, mytime, myiter, mythid ) |
266 |
|
|
CALL TIMER_STOP ('FORWARD_STEP [THE_MAIN_LOOP]',mythid) |
267 |
adcroft |
1.8 |
|
268 |
heimbach |
1.12 |
#ifdef ALLOW_COST |
269 |
heimbach |
1.13 |
C-- compare model with data and compute cost function |
270 |
|
|
C-- this is done after exchanges to allow interpolation |
271 |
|
|
CALL TIMER_START('COST_TILE [THE_MAIN_LOOP]',myThid) |
272 |
|
|
CALL COST_TILE ( myThid ) |
273 |
|
|
CALL TIMER_STOP ('COST_TILE [THE_MAIN_LOOP]',myThid) |
274 |
heimbach |
1.12 |
#endif |
275 |
|
|
|
276 |
heimbach |
1.13 |
c-- >>> Loop body end <<< |
277 |
heimbach |
1.6 |
|
278 |
|
|
#ifdef ALLOW_AUTODIFF_TAMC |
279 |
|
|
#ifdef ALLOW_TAMC_CHECKPOINTING |
280 |
|
|
endif |
281 |
|
|
enddo |
282 |
|
|
endif |
283 |
|
|
enddo |
284 |
|
|
endif |
285 |
|
|
enddo |
286 |
|
|
#else |
287 |
|
|
enddo |
288 |
|
|
#endif |
289 |
|
|
|
290 |
|
|
#else |
291 |
|
|
enddo |
292 |
|
|
#endif |
293 |
|
|
|
294 |
heimbach |
1.12 |
#ifdef ALLOW_COST |
295 |
|
|
c-- Sum all cost function contributions. |
296 |
|
|
call TIMER_START('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
297 |
|
|
call COST_FINAL ( mythid ) |
298 |
|
|
call TIMER_STOP ('COST_FINAL [ADJOINT SPIN-DOWN]', mythid) |
299 |
|
|
#endif |
300 |
|
|
|
301 |
heimbach |
1.6 |
_BARRIER |
302 |
adcroft |
1.10 |
CALL TIMER_STOP ('MAIN LOOP [THE_MAIN_LOOP]', mythid) |
303 |
adcroft |
1.1 |
|
304 |
adcroft |
1.10 |
END |