1 |
C |
2 |
C $Header: /u/gcmpack/MITgcm/pkg/grdchk/grdchk_main.F,v 1.14 2004/03/23 19:42:53 heimbach Exp $ |
3 |
C $Name: $ |
4 |
|
5 |
#include "AD_CONFIG.h" |
6 |
#include "CPP_OPTIONS.h" |
7 |
|
8 |
CBOI |
9 |
C |
10 |
C !TITLE: GRADIENT CHECK |
11 |
C !AUTHORS: mitgcm developers ( support@mitgcm.org ) |
12 |
C !AFFILIATION: Massachussetts Institute of Technology |
13 |
C !DATE: |
14 |
C !INTRODUCTION: gradient check package |
15 |
c \bv |
16 |
c Compare the gradients calculated by the adjoint model with |
17 |
c finite difference approximations. |
18 |
c |
19 |
C !CALLING SEQUENCE: |
20 |
c |
21 |
c the_model_main |
22 |
c | |
23 |
c |-- ctrl_unpack |
24 |
c |-- adthe_main_loop - unperturbed cost function and |
25 |
c |-- ctrl_pack adjoint gradient are computed here |
26 |
c | |
27 |
c |-- grdchk_main |
28 |
c | |
29 |
c |-- grdchk_init |
30 |
c |-- do icomp=... - loop over control vector elements |
31 |
c | |
32 |
c |-- grdchk_loc - determine location of icomp on grid |
33 |
c | |
34 |
c |-- grdchk_getxx - get control vector component from file |
35 |
c | perturb it and write back to file |
36 |
c |-- grdchk_getadxx - get gradient component calculated |
37 |
c | via adjoint |
38 |
c |-- the_main_loop - forward run and cost evaluation |
39 |
c | with perturbed control vector element |
40 |
c |-- calculate ratio of adj. vs. finite difference gradient |
41 |
c | |
42 |
c |-- grdchk_setxx - Reset control vector element |
43 |
c | |
44 |
c |-- grdchk_print - print results |
45 |
c \ev |
46 |
CEOI |
47 |
|
48 |
CBOP |
49 |
C !ROUTINE: grdchk_main |
50 |
C !INTERFACE: |
51 |
subroutine grdchk_main( mythid ) |
52 |
|
53 |
C !DESCRIPTION: \bv |
54 |
c ================================================================== |
55 |
c SUBROUTINE grdchk_main |
56 |
c ================================================================== |
57 |
c o Compare the gradients calculated by the adjoint model with |
58 |
c finite difference approximations. |
59 |
c started: Christian Eckert eckert@mit.edu 24-Feb-2000 |
60 |
c continued&finished: heimbach@mit.edu: 13-Jun-2001 |
61 |
c changed: mlosch@ocean.mit.edu: 09-May-2002 |
62 |
c - added centered difference vs. 1-sided difference option |
63 |
c - improved output format for readability |
64 |
c - added control variable hFacC |
65 |
c heimbach@mit.edu 24-Feb-2003 |
66 |
c - added tangent linear gradient checks |
67 |
c - fixes for multiproc. gradient checks |
68 |
c - added more control variables |
69 |
c |
70 |
c ================================================================== |
71 |
c SUBROUTINE grdchk_main |
72 |
c ================================================================== |
73 |
C \ev |
74 |
|
75 |
C !USES: |
76 |
implicit none |
77 |
|
78 |
c == global variables == |
79 |
#include "SIZE.h" |
80 |
#include "EEPARAMS.h" |
81 |
#include "PARAMS.h" |
82 |
#include "grdchk.h" |
83 |
#include "cost.h" |
84 |
#ifdef ALLOW_TANGENTLINEAR_RUN |
85 |
#include "g_cost.h" |
86 |
#endif |
87 |
|
88 |
C !INPUT/OUTPUT PARAMETERS: |
89 |
c == routine arguments == |
90 |
integer mythid |
91 |
|
92 |
#ifdef ALLOW_GRDCHK |
93 |
C !LOCAL VARIABLES: |
94 |
c == local variables == |
95 |
integer myiter |
96 |
_RL mytime |
97 |
integer bi, itlo, ithi |
98 |
integer bj, jtlo, jthi |
99 |
integer i, imin, imax |
100 |
integer j, jmin, jmax |
101 |
integer k |
102 |
|
103 |
integer icomp |
104 |
integer ichknum |
105 |
integer icvrec |
106 |
integer jtile |
107 |
integer itile |
108 |
integer layer |
109 |
integer obcspos |
110 |
integer itilepos |
111 |
integer jtilepos |
112 |
integer itest |
113 |
integer ierr |
114 |
integer ierr_grdchk |
115 |
_RL gfd |
116 |
_RL fcref |
117 |
_RL fcpertplus, fcpertminus |
118 |
_RL ratio_ad |
119 |
_RL ratio_ftl |
120 |
_RL xxmemo_ref |
121 |
_RL xxmemo_pert |
122 |
_RL adxxmemo |
123 |
_RL ftlxxmemo |
124 |
_RL localEps |
125 |
_RL grdchk_epsfac |
126 |
|
127 |
_RL tmpplot1(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) |
128 |
_RL tmpplot2(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) |
129 |
_RL tmpplot3(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) |
130 |
|
131 |
CHARACTER*(MAX_LEN_MBUF) msgBuf |
132 |
|
133 |
c == end of interface == |
134 |
CEOP |
135 |
|
136 |
c-- Set the loop ranges. |
137 |
jtlo = mybylo(mythid) |
138 |
jthi = mybyhi(mythid) |
139 |
itlo = mybxlo(mythid) |
140 |
ithi = mybxhi(mythid) |
141 |
jmin = 1 |
142 |
jmax = sny |
143 |
imin = 1 |
144 |
imax = snx |
145 |
|
146 |
c-- initialise variables |
147 |
call grdchk_init( mythid ) |
148 |
|
149 |
c-- Compute the adjoint models' gradients. |
150 |
c-- Compute the unperturbed cost function. |
151 |
cph Gradient via adjoint has already been computed, |
152 |
cph and so has unperturbed cost function, |
153 |
cph assuming all xx_ fields are initialised to zero. |
154 |
|
155 |
ierr_grdchk = 0 |
156 |
cphadmtlm( |
157 |
fcref = fc |
158 |
cphadmtlm fcref = objf_state_final(45,4,1,1,1) |
159 |
cphadmtlm) |
160 |
|
161 |
print *, 'ph-check fcref = ', fcref |
162 |
|
163 |
do bj = jtlo, jthi |
164 |
do bi = itlo, ithi |
165 |
do k = 1, nr |
166 |
do j = jmin, jmax |
167 |
do i = imin, imax |
168 |
tmpplot1(i,j,k,bi,bj) = 0. _d 0 |
169 |
tmpplot2(i,j,k,bi,bj) = 0. _d 0 |
170 |
tmpplot3(i,j,k,bi,bj) = 0. _d 0 |
171 |
end do |
172 |
end do |
173 |
end do |
174 |
end do |
175 |
end do |
176 |
|
177 |
if ( useCentralDiff ) then |
178 |
grdchk_epsfac = 2. _d 0 |
179 |
else |
180 |
grdchk_epsfac = 1. _d 0 |
181 |
end if |
182 |
|
183 |
print *, 'grad-res -------------------------------' |
184 |
print ('(2a)'), |
185 |
& ' grad-res proc # i j k fc ref', |
186 |
& ' fc + eps fc - eps' |
187 |
#ifdef ALLOW_TANGENTLINEAR_RUN |
188 |
print ('(2a)'), |
189 |
& ' grad-res proc # i j k tlm grad', |
190 |
& ' fd grad 1 - fd/tlm' |
191 |
#else |
192 |
print ('(2a)'), |
193 |
& ' grad-res proc # i j k adj grad', |
194 |
& ' fd grad 1 - fd/adj' |
195 |
#endif |
196 |
|
197 |
c-- Compute the finite difference approximations. |
198 |
c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep |
199 |
c-- gradient checks. |
200 |
|
201 |
if ( nbeg .EQ. 0 ) call grdchk_get_position( mythid ) |
202 |
|
203 |
do icomp = nbeg, nend, nstep |
204 |
|
205 |
ichknum = (icomp - nbeg)/nstep + 1 |
206 |
|
207 |
if (ichknum .le. maxgrdchecks ) then |
208 |
|
209 |
c-- Determine the location of icomp on the grid. |
210 |
if ( myProcId .EQ. grdchkwhichproc ) then |
211 |
call grdchk_loc( icomp, ichknum, |
212 |
& icvrec, itile, jtile, layer, obcspos, |
213 |
& itilepos, jtilepos, itest, ierr, |
214 |
& mythid ) |
215 |
endif |
216 |
_BARRIER |
217 |
|
218 |
c****************************************************** |
219 |
c-- (A): get gradient component calculated via adjoint |
220 |
c****************************************************** |
221 |
|
222 |
c-- get gradient component calculated via adjoint |
223 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
224 |
& ierr .EQ. 0 ) then |
225 |
call grdchk_getadxx( icvrec, |
226 |
& itile, jtile, layer, |
227 |
& itilepos, jtilepos, |
228 |
& adxxmemo, mythid ) |
229 |
endif |
230 |
_BARRIER |
231 |
|
232 |
#ifdef ALLOW_TANGENTLINEAR_RUN |
233 |
c****************************************************** |
234 |
c-- (B): Get gradient component g_fc from tangent linear run: |
235 |
c****************************************************** |
236 |
c-- |
237 |
c-- 1. perturb control vector component: xx(i)=1. |
238 |
|
239 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
240 |
& ierr .EQ. 0 ) then |
241 |
localEps = 1. _d 0 |
242 |
call grdchk_getxx( icvrec, TANGENT_SIMULATION, |
243 |
& itile, jtile, layer, |
244 |
& itilepos, jtilepos, |
245 |
& xxmemo_ref, xxmemo_pert, localEps, |
246 |
& mythid ) |
247 |
endif |
248 |
_BARRIER |
249 |
|
250 |
c-- |
251 |
c-- 2. perform tangent linear run |
252 |
mytime = starttime |
253 |
myiter = niter0 |
254 |
cphadmtlm( |
255 |
g_fc = 0. |
256 |
cphadmtlm do j=1,sny |
257 |
cphadmtlm do i=1,snx |
258 |
cphadmtlm g_objf_state_final(i,j,1,1,1) = 0. |
259 |
cphadmtlm g_objf_state_final(i,j,1,1,2) = 0. |
260 |
cphadmtlm enddo |
261 |
cphadmtlm enddo |
262 |
cphadmtlm) |
263 |
call g_the_main_loop( mytime, myiter, mythid ) |
264 |
cphadmtlm( |
265 |
ftlxxmemo = g_fc |
266 |
cphadmtlm ftlxxmemo = g_objf_state_final(45,4,1,1,1) |
267 |
cphadmtlm) |
268 |
_BARRIER |
269 |
c-- |
270 |
c-- 3. reset control vector |
271 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
272 |
& ierr .EQ. 0 ) then |
273 |
call grdchk_setxx( icvrec, TANGENT_SIMULATION, |
274 |
& itile, jtile, layer, |
275 |
& itilepos, jtilepos, |
276 |
& xxmemo_ref, mythid ) |
277 |
endif |
278 |
_BARRIER |
279 |
|
280 |
#endif /* ALLOW_TANGENTLINEAR_RUN */ |
281 |
|
282 |
|
283 |
c****************************************************** |
284 |
c-- (C): Get gradient via finite difference perturbation |
285 |
c****************************************************** |
286 |
|
287 |
c-- get control vector component from file |
288 |
c-- perturb it and write back to file |
289 |
c-- positive perturbation |
290 |
localEps = abs(grdchk_eps) |
291 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
292 |
& ierr .EQ. 0 ) then |
293 |
call grdchk_getxx( icvrec, FORWARD_SIMULATION, |
294 |
& itile, jtile, layer, |
295 |
& itilepos, jtilepos, |
296 |
& xxmemo_ref, xxmemo_pert, localEps, |
297 |
& mythid ) |
298 |
endif |
299 |
_BARRIER |
300 |
|
301 |
c-- forward run with perturbed control vector |
302 |
mytime = starttime |
303 |
myiter = niter0 |
304 |
call the_main_loop( mytime, myiter, mythid ) |
305 |
cphadmtlm( |
306 |
fcpertplus = fc |
307 |
cphadmtlm fcpertplus = objf_state_final(45,4,1,1,1) |
308 |
cphadmtlm) |
309 |
print *, 'ph-check fcpertplus = ', fcpertplus |
310 |
_BARRIER |
311 |
|
312 |
c-- Reset control vector. |
313 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
314 |
& ierr .EQ. 0 ) then |
315 |
call grdchk_setxx( icvrec, FORWARD_SIMULATION, |
316 |
& itile, jtile, layer, |
317 |
& itilepos, jtilepos, |
318 |
& xxmemo_ref, mythid ) |
319 |
endif |
320 |
_BARRIER |
321 |
|
322 |
fcpertminus = fcref |
323 |
print *, 'ph-check fcpertminus = ', fcpertminus |
324 |
|
325 |
if ( useCentralDiff ) then |
326 |
|
327 |
c-- get control vector component from file |
328 |
c-- perturb it and write back to file |
329 |
c-- repeat the proceedure for a negative perturbation |
330 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
331 |
& ierr .EQ. 0 ) then |
332 |
localEps = - abs(grdchk_eps) |
333 |
call grdchk_getxx( icvrec, FORWARD_SIMULATION, |
334 |
& itile, jtile, layer, |
335 |
& itilepos, jtilepos, |
336 |
& xxmemo_ref, xxmemo_pert, localEps, |
337 |
& mythid ) |
338 |
endif |
339 |
_BARRIER |
340 |
|
341 |
c-- forward run with perturbed control vector |
342 |
mytime = starttime |
343 |
myiter = niter0 |
344 |
call the_main_loop( mytime, myiter, mythid ) |
345 |
_BARRIER |
346 |
fcpertminus = fc |
347 |
|
348 |
c-- Reset control vector. |
349 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
350 |
& ierr .EQ. 0 ) then |
351 |
call grdchk_setxx( icvrec, FORWARD_SIMULATION, |
352 |
& itile, jtile, layer, |
353 |
& itilepos, jtilepos, |
354 |
& xxmemo_ref, mythid ) |
355 |
endif |
356 |
_BARRIER |
357 |
|
358 |
c-- end of if useCentralDiff ... |
359 |
end if |
360 |
|
361 |
c****************************************************** |
362 |
c-- (D): calculate relative differences between gradients |
363 |
c****************************************************** |
364 |
|
365 |
if ( myProcId .EQ. grdchkwhichproc .AND. |
366 |
& ierr .EQ. 0 ) then |
367 |
|
368 |
if ( grdchk_eps .eq. 0. ) then |
369 |
gfd = (fcpertplus-fcpertminus) |
370 |
else |
371 |
gfd = (fcpertplus-fcpertminus) |
372 |
& /(grdchk_epsfac*grdchk_eps) |
373 |
endif |
374 |
|
375 |
if ( adxxmemo .eq. 0. ) then |
376 |
ratio_ad = abs( adxxmemo - gfd ) |
377 |
else |
378 |
ratio_ad = 1. - gfd/adxxmemo |
379 |
endif |
380 |
|
381 |
if ( ftlxxmemo .eq. 0. ) then |
382 |
ratio_ftl = abs( ftlxxmemo - gfd ) |
383 |
else |
384 |
ratio_ftl = 1. - gfd/ftlxxmemo |
385 |
endif |
386 |
|
387 |
tmpplot1(itilepos,jtilepos,layer,itile,jtile) |
388 |
& = gfd |
389 |
tmpplot2(itilepos,jtilepos,layer,itile,jtile) |
390 |
& = ratio_ad |
391 |
tmpplot3(itilepos,jtilepos,layer,itile,jtile) |
392 |
& = ratio_ftl |
393 |
|
394 |
fcrmem ( ichknum ) = fcref |
395 |
fcppmem ( ichknum ) = fcpertplus |
396 |
fcpmmem ( ichknum ) = fcpertminus |
397 |
xxmemref ( ichknum ) = xxmemo_ref |
398 |
xxmempert ( ichknum ) = xxmemo_pert |
399 |
gfdmem ( ichknum ) = gfd |
400 |
adxxmem ( ichknum ) = adxxmemo |
401 |
ftlxxmem ( ichknum ) = ftlxxmemo |
402 |
ratioadmem ( ichknum ) = ratio_ad |
403 |
ratioftlmem ( ichknum ) = ratio_ftl |
404 |
|
405 |
irecmem ( ichknum ) = icvrec |
406 |
bimem ( ichknum ) = itile |
407 |
bjmem ( ichknum ) = jtile |
408 |
ilocmem ( ichknum ) = itilepos |
409 |
jlocmem ( ichknum ) = jtilepos |
410 |
klocmem ( ichknum ) = layer |
411 |
iobcsmem ( ichknum ) = obcspos |
412 |
icompmem ( ichknum ) = icomp |
413 |
ichkmem ( ichknum ) = ichknum |
414 |
itestmem ( ichknum ) = itest |
415 |
ierrmem ( ichknum ) = ierr |
416 |
|
417 |
print *, 'grad-res -------------------------------' |
418 |
print '(a,5I5,2x,3(1x,E15.9))', ' grad-res ', |
419 |
& myprocid,ichknum,itilepos,jtilepos,layer, |
420 |
& fcref, fcpertplus, fcpertminus |
421 |
#ifdef ALLOW_TANGENTLINEAR_RUN |
422 |
print '(a,5I5,2x,3(1x,E15.9))', ' grad-res ', |
423 |
& myprocid,ichknum,ichkmem(ichknum), |
424 |
& icompmem(ichknum),itestmem(ichknum), |
425 |
& ftlxxmemo, gfd, ratio_ftl |
426 |
WRITE(msgBuf,'(A34,2(1PE24.14,X))') |
427 |
& 'precision_grdchk_result TLM ', fcref, ftlxxmemo |
428 |
CALL PRINT_MESSAGE |
429 |
& (msgBuf,standardMessageUnit,SQUEEZE_RIGHT,1) |
430 |
#else |
431 |
print '(a,5I5,2x,3(1x,E15.9))', ' grad-res ', |
432 |
& myprocid,ichknum,ichkmem(ichknum), |
433 |
& icompmem(ichknum),itestmem(ichknum), |
434 |
& adxxmemo, gfd, ratio_ad |
435 |
WRITE(msgBuf,'(A34,2(1PE24.14,X))') |
436 |
& 'precision_grdchk_result ADM ', fcref, adxxmemo |
437 |
CALL PRINT_MESSAGE |
438 |
& (msgBuf,standardMessageUnit,SQUEEZE_RIGHT,1) |
439 |
#endif |
440 |
|
441 |
endif |
442 |
|
443 |
print *, 'ph-grd ierr ---------------------------' |
444 |
print *, 'ph-grd ierr = ', ierr, ', icomp = ', icomp, |
445 |
& ', ichknum = ', ichknum |
446 |
|
447 |
_BARRIER |
448 |
|
449 |
c-- else of if ( ichknum ... |
450 |
else |
451 |
ierr_grdchk = -1 |
452 |
|
453 |
c-- end of if ( ichknum ... |
454 |
endif |
455 |
|
456 |
c-- end of do icomp = ... |
457 |
enddo |
458 |
|
459 |
if ( myProcId .EQ. grdchkwhichproc ) then |
460 |
CALL WRITE_REC_XYZ_RL( |
461 |
& 'grd_findiff' , tmpplot1, 1, 0, myThid) |
462 |
CALL WRITE_REC_XYZ_RL( |
463 |
& 'grd_ratio_ad' , tmpplot2, 1, 0, myThid) |
464 |
CALL WRITE_REC_XYZ_RL( |
465 |
& 'grd_ratio_ftl' , tmpplot3, 1, 0, myThid) |
466 |
endif |
467 |
|
468 |
c-- Everyone has to wait for the component to be reset. |
469 |
_BARRIER |
470 |
|
471 |
c-- Print the results of the gradient check. |
472 |
call grdchk_print( ichknum, ierr_grdchk, mythid ) |
473 |
|
474 |
#endif /* ALLOW_GRDCHK */ |
475 |
|
476 |
end |