/[MITgcm]/MITgcm/pkg/grdchk/grdchk_main.F
ViewVC logotype

Contents of /MITgcm/pkg/grdchk/grdchk_main.F

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download)
Fri Jul 27 22:18:57 2007 UTC (16 years, 10 months ago) by heimbach
Branch: MAIN
Changes since 1.21: +2 -1 lines
Comment all relevant #ifndef ALLOW_AUTODIFF_TAMC
that used to hide exch2 or cubed-sphere specific code
(commented via 'cph-exch2')

1 C
2 C $Header: /u/gcmpack/MITgcm/pkg/grdchk/grdchk_main.F,v 1.21 2007/04/20 19:26:43 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 #include "ctrl.h"
85 #ifdef ALLOW_TANGENTLINEAR_RUN
86 #include "g_cost.h"
87 #endif
88
89 C !INPUT/OUTPUT PARAMETERS:
90 c == routine arguments ==
91 integer mythid
92
93 #ifdef ALLOW_GRDCHK
94 C !LOCAL VARIABLES:
95 c == local variables ==
96 integer myiter
97 _RL mytime
98 integer bi, itlo, ithi
99 integer bj, jtlo, jthi
100 integer i, imin, imax
101 integer j, jmin, jmax
102 integer k
103
104 integer icomp
105 integer ichknum
106 integer icvrec
107 integer jtile
108 integer itile
109 integer layer
110 integer obcspos
111 integer itilepos
112 integer jtilepos
113 integer icglo
114 integer itest
115 integer ierr
116 integer ierr_grdchk
117 _RL gfd
118 _RL fcref
119 _RL fcpertplus, fcpertminus
120 _RL ratio_ad
121 _RL ratio_ftl
122 _RL xxmemo_ref
123 _RL xxmemo_pert
124 _RL adxxmemo
125 _RL ftlxxmemo
126 _RL localEps
127 _RL grdchk_epsfac
128
129 _RL tmpplot1(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
130 _RL tmpplot2(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
131 _RL tmpplot3(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
132
133 CHARACTER*(MAX_LEN_MBUF) msgBuf
134
135 c == end of interface ==
136 CEOP
137
138 c-- Set the loop ranges.
139 jtlo = mybylo(mythid)
140 jthi = mybyhi(mythid)
141 itlo = mybxlo(mythid)
142 ithi = mybxhi(mythid)
143 jmin = 1
144 jmax = sny
145 imin = 1
146 imax = snx
147
148 print *, 'ph-check entering grdchk_main '
149
150 c-- initialise variables
151 call grdchk_init( mythid )
152
153 c-- Compute the adjoint models' gradients.
154 c-- Compute the unperturbed cost function.
155 cph Gradient via adjoint has already been computed,
156 cph and so has unperturbed cost function,
157 cph assuming all xx_ fields are initialised to zero.
158
159 ierr_grdchk = 0
160 #ifdef ALLOW_ADMTLM
161 fcref = objf_state_final(idep,jdep,1,1,1)
162 #else
163 fcref = fc
164 #endif
165
166 print *, 'ph-check fcref = ', fcref
167
168 do bj = jtlo, jthi
169 do bi = itlo, ithi
170 do k = 1, nr
171 do j = jmin, jmax
172 do i = imin, imax
173 tmpplot1(i,j,k,bi,bj) = 0. _d 0
174 tmpplot2(i,j,k,bi,bj) = 0. _d 0
175 tmpplot3(i,j,k,bi,bj) = 0. _d 0
176 end do
177 end do
178 end do
179 end do
180 end do
181
182 if ( useCentralDiff ) then
183 grdchk_epsfac = 2. _d 0
184 else
185 grdchk_epsfac = 1. _d 0
186 end if
187
188 print *, 'grad-res -------------------------------'
189 print '(2a)',
190 & ' grad-res proc # i j k bi bj iobc',
191 & ' fc ref fc + eps fc - eps'
192 #ifdef ALLOW_TANGENTLINEAR_RUN
193 print '(2a)',
194 & ' grad-res proc # i j k bi bj iobc',
195 & ' tlm grad fd grad 1 - fd/tlm'
196 #else
197 print '(2a)',
198 & ' grad-res proc # i j k bi bj iobc',
199 & ' adj grad fd grad 1 - fd/adj'
200 #endif
201
202 c-- Compute the finite difference approximations.
203 c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep
204 c-- gradient checks.
205
206 if ( nbeg .EQ. 0 )
207 & call grdchk_get_position( mythid )
208
209 do icomp = nbeg, nend, nstep
210
211 ichknum = (icomp - nbeg)/nstep + 1
212
213 cph(
214 cph-print print *, 'ph-grd _main: nbeg, icomp, ichknum ',
215 cph-print & nbeg, icomp, ichknum
216 cph)
217 if (ichknum .le. maxgrdchecks ) then
218
219 c-- Determine the location of icomp on the grid.
220 if ( myProcId .EQ. grdchkwhichproc ) then
221 call grdchk_loc( icomp, ichknum,
222 & icvrec, itile, jtile, layer, obcspos,
223 & itilepos, jtilepos, icglo, itest, ierr,
224 & mythid )
225 cph(
226 cph-print print *, 'ph-grd ----- back from loc -----',
227 cph-print & icvrec, itilepos, jtilepos, layer, obcspos
228 cph)
229 endif
230 _BARRIER
231
232 c******************************************************
233 c-- (A): get gradient component calculated via adjoint
234 c******************************************************
235
236 c-- get gradient component calculated via adjoint
237 if ( myProcId .EQ. grdchkwhichproc .AND.
238 & ierr .EQ. 0 ) then
239 call grdchk_getadxx( icvrec,
240 & itile, jtile, layer,
241 & itilepos, jtilepos,
242 & adxxmemo, mythid )
243 endif
244 _BARRIER
245
246 #ifdef ALLOW_TANGENTLINEAR_RUN
247 c******************************************************
248 c-- (B): Get gradient component g_fc from tangent linear run:
249 c******************************************************
250 c--
251 c-- 1. perturb control vector component: xx(i)=1.
252 ftlxxmemo = 0.
253
254 if ( myProcId .EQ. grdchkwhichproc .AND.
255 & ierr .EQ. 0 ) then
256 localEps = 1. _d 0
257 call grdchk_getxx( icvrec, TANGENT_SIMULATION,
258 & itile, jtile, layer,
259 & itilepos, jtilepos,
260 & xxmemo_ref, xxmemo_pert, localEps,
261 & mythid )
262 endif
263 _BARRIER
264
265 c--
266 c-- 2. perform tangent linear run
267 mytime = starttime
268 myiter = niter0
269 #ifdef ALLOW_ADMTLM
270 do k=1,4*Nr+1
271 do j=1,sny
272 do i=1,snx
273 g_objf_state_final(i,j,1,1,k) = 0.
274 enddo
275 enddo
276 enddo
277 #else
278 g_fc = 0.
279 #endif
280
281 call g_the_main_loop( mytime, myiter, mythid )
282 _BARRIER
283 #ifdef ALLOW_ADMTLM
284 ftlxxmemo = g_objf_state_final(idep,jdep,1,1,1)
285 #else
286 ftlxxmemo = g_fc
287 #endif
288
289 c--
290 c-- 3. reset control vector
291 if ( myProcId .EQ. grdchkwhichproc .AND.
292 & ierr .EQ. 0 ) then
293 call grdchk_setxx( icvrec, TANGENT_SIMULATION,
294 & itile, jtile, layer,
295 & itilepos, jtilepos,
296 & xxmemo_ref, mythid )
297 endif
298 _BARRIER
299
300 #endif /* ALLOW_TANGENTLINEAR_RUN */
301
302
303 c******************************************************
304 c-- (C): Get gradient via finite difference perturbation
305 c******************************************************
306
307 c-- get control vector component from file
308 c-- perturb it and write back to file
309 c-- positive perturbation
310 localEps = abs(grdchk_eps)
311 if ( myProcId .EQ. grdchkwhichproc .AND.
312 & ierr .EQ. 0 ) then
313 call grdchk_getxx( icvrec, FORWARD_SIMULATION,
314 & itile, jtile, layer,
315 & itilepos, jtilepos,
316 & xxmemo_ref, xxmemo_pert, localEps,
317 & mythid )
318 endif
319 _BARRIER
320
321 c-- forward run with perturbed control vector
322 mytime = starttime
323 myiter = niter0
324 call the_main_loop( mytime, myiter, mythid )
325 #ifdef ALLOW_ADMTLM
326 fcpertplus = objf_state_final(idep,jdep,1,1,1)
327 #else
328 fcpertplus = fc
329 #endif
330 print *, 'ph-check fcpertplus = ', fcpertplus
331 _BARRIER
332
333 c-- Reset control vector.
334 if ( myProcId .EQ. grdchkwhichproc .AND.
335 & ierr .EQ. 0 ) then
336 call grdchk_setxx( icvrec, FORWARD_SIMULATION,
337 & itile, jtile, layer,
338 & itilepos, jtilepos,
339 & xxmemo_ref, mythid )
340 endif
341 _BARRIER
342
343 fcpertminus = fcref
344 print *, 'ph-check fcpertminus = ', fcpertminus
345
346 if ( useCentralDiff ) then
347
348 c-- get control vector component from file
349 c-- perturb it and write back to file
350 c-- repeat the proceedure for a negative perturbation
351 if ( myProcId .EQ. grdchkwhichproc .AND.
352 & ierr .EQ. 0 ) then
353 localEps = - abs(grdchk_eps)
354 call grdchk_getxx( icvrec, FORWARD_SIMULATION,
355 & itile, jtile, layer,
356 & itilepos, jtilepos,
357 & xxmemo_ref, xxmemo_pert, localEps,
358 & mythid )
359 endif
360 _BARRIER
361
362 c-- forward run with perturbed control vector
363 mytime = starttime
364 myiter = niter0
365 call the_main_loop( mytime, myiter, mythid )
366 _BARRIER
367 #ifdef ALLOW_ADMTLM
368 fcpertminus = objf_state_final(idep,jdep,1,1,1)
369 #else
370 fcpertminus = fc
371 #endif
372
373 c-- Reset control vector.
374 if ( myProcId .EQ. grdchkwhichproc .AND.
375 & ierr .EQ. 0 ) then
376 call grdchk_setxx( icvrec, FORWARD_SIMULATION,
377 & itile, jtile, layer,
378 & itilepos, jtilepos,
379 & xxmemo_ref, mythid )
380 endif
381 _BARRIER
382
383 c-- end of if useCentralDiff ...
384 end if
385
386 c******************************************************
387 c-- (D): calculate relative differences between gradients
388 c******************************************************
389
390 if ( myProcId .EQ. grdchkwhichproc .AND.
391 & ierr .EQ. 0 ) then
392
393 if ( grdchk_eps .eq. 0. ) then
394 gfd = (fcpertplus-fcpertminus)
395 else
396 gfd = (fcpertplus-fcpertminus)
397 & /(grdchk_epsfac*grdchk_eps)
398 endif
399
400 if ( adxxmemo .eq. 0. ) then
401 ratio_ad = abs( adxxmemo - gfd )
402 else
403 ratio_ad = 1. - gfd/adxxmemo
404 endif
405
406 if ( ftlxxmemo .eq. 0. ) then
407 ratio_ftl = abs( ftlxxmemo - gfd )
408 else
409 ratio_ftl = 1. - gfd/ftlxxmemo
410 endif
411
412 tmpplot1(itilepos,jtilepos,layer,itile,jtile)
413 & = gfd
414 tmpplot2(itilepos,jtilepos,layer,itile,jtile)
415 & = ratio_ad
416 tmpplot3(itilepos,jtilepos,layer,itile,jtile)
417 & = ratio_ftl
418
419 fcrmem ( ichknum ) = fcref
420 fcppmem ( ichknum ) = fcpertplus
421 fcpmmem ( ichknum ) = fcpertminus
422 xxmemref ( ichknum ) = xxmemo_ref
423 xxmempert ( ichknum ) = xxmemo_pert
424 gfdmem ( ichknum ) = gfd
425 adxxmem ( ichknum ) = adxxmemo
426 ftlxxmem ( ichknum ) = ftlxxmemo
427 ratioadmem ( ichknum ) = ratio_ad
428 ratioftlmem ( ichknum ) = ratio_ftl
429
430 irecmem ( ichknum ) = icvrec
431 bimem ( ichknum ) = itile
432 bjmem ( ichknum ) = jtile
433 ilocmem ( ichknum ) = itilepos
434 jlocmem ( ichknum ) = jtilepos
435 klocmem ( ichknum ) = layer
436 iobcsmem ( ichknum ) = obcspos
437 icompmem ( ichknum ) = icomp
438 ichkmem ( ichknum ) = ichknum
439 itestmem ( ichknum ) = itest
440 ierrmem ( ichknum ) = ierr
441 icglomem ( ichknum ) = icglo
442
443 print *, 'grad-res -------------------------------'
444 print '(a,8I5,2x,3(1x,E18.12))', ' grad-res ',
445 & myprocid,ichknum,itilepos,jtilepos,
446 & layer,itile,jtile,obcspos,
447 & fcref, fcpertplus, fcpertminus
448 #ifdef ALLOW_TANGENTLINEAR_RUN
449 print '(a,8I5,2x,3(1x,E18.12))', ' grad-res ',
450 & myprocid,ichknum,ichkmem(ichknum),
451 & icompmem(ichknum),itestmem(ichknum),
452 & bimem(ichknum),bjmem(ichknum),iobcsmem(ichknum),
453 & ftlxxmemo, gfd, ratio_ftl
454 WRITE(msgBuf,'(A34,2(1PE24.14,X))')
455 & 'precision_grdchk_result TLM ', fcref, ftlxxmemo
456 CALL PRINT_MESSAGE
457 & (msgBuf,standardMessageUnit,SQUEEZE_RIGHT,1)
458 #else
459 print '(a,8I5,2x,3(1x,E18.12))', ' grad-res ',
460 & myprocid,ichknum,ichkmem(ichknum),
461 & icompmem(ichknum),itestmem(ichknum),
462 & bimem(ichknum),bjmem(ichknum),obcspos,
463 & adxxmemo, gfd, ratio_ad
464 WRITE(msgBuf,'(A34,2(1PE24.14,X))')
465 & 'precision_grdchk_result ADM ', fcref, adxxmemo
466 CALL PRINT_MESSAGE
467 & (msgBuf,standardMessageUnit,SQUEEZE_RIGHT,1)
468 #endif
469
470 endif
471
472 print *, 'ph-grd ierr ---------------------------'
473 print *, 'ph-grd ierr = ', ierr, ', icomp = ', icomp,
474 & ', ichknum = ', ichknum
475
476 _BARRIER
477
478 c-- else of if ( ichknum ...
479 else
480 ierr_grdchk = -1
481
482 c-- end of if ( ichknum ...
483 endif
484
485 c-- end of do icomp = ...
486 enddo
487
488 if ( myProcId .EQ. grdchkwhichproc ) then
489 CALL WRITE_REC_XYZ_RL(
490 & 'grd_findiff' , tmpplot1, 1, 0, myThid)
491 CALL WRITE_REC_XYZ_RL(
492 & 'grd_ratio_ad' , tmpplot2, 1, 0, myThid)
493 CALL WRITE_REC_XYZ_RL(
494 & 'grd_ratio_ftl' , tmpplot3, 1, 0, myThid)
495 endif
496
497 c-- Everyone has to wait for the component to be reset.
498 _BARRIER
499
500 c-- Print the results of the gradient check.
501 call grdchk_print( ichknum, ierr_grdchk, mythid )
502
503 #endif /* ALLOW_GRDCHK */
504
505 end

  ViewVC Help
Powered by ViewVC 1.1.22