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

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

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


Revision 1.3.6.2 - (hide annotations) (download)
Thu Feb 13 23:36:18 2003 UTC (21 years, 3 months ago) by heimbach
Branch: ecco-branch
CVS Tags: icebear4, icebear3, icebear2, ecco_c44_e26
Changes since 1.3.6.1: +13 -5 lines
Added code to perform gradient checks for bulk formulae / atmos. state
(atemp, aqh, uwind, vwind).
NOTE:
This package is out of synch with c48 package.
The latter also has the tangent linear gradient checks.
Need to be merged.

1 heimbach 1.3.6.2 C $Header: /u/gcmpack/MITgcm/pkg/grdchk/grdchk_main.F,v 1.3.6.1 2002/05/30 19:55:17 heimbach Exp $
2 heimbach 1.2
3     #include "CPP_OPTIONS.h"
4    
5 heimbach 1.3 CBOI
6     C
7     C !TITLE: GRADIENT CHECK
8     C !AUTHORS: mitgcm developers ( support@mitgcm.org )
9     C !AFFILIATION: Massachussetts Institute of Technology
10     C !DATE:
11     C !INTRODUCTION: gradient check package
12     c \bv
13     c Compare the gradients calculated by the adjoint model with
14     c finite difference approximations.
15     c
16     C !CALLING SEQUENCE:
17     c
18     c the_model_main
19     c |
20     c |-- ctrl_unpack
21     c |-- adthe_main_loop - unperturbed cost function and
22     c |-- ctrl_pack adjoint gradient are computed here
23     c |
24     c |-- grdchk_main
25     c |
26     c |-- grdchk_init
27     c |-- do icomp=... - loop over control vector elements
28     c |
29     c |-- grdchk_loc - determine location of icomp on grid
30     c |
31     c |-- grdchk_getxx - get control vector component from file
32     c | perturb it and write back to file
33     c |-- grdchk_getadxx - get gradient component calculated
34     c | via adjoint
35     c |-- the_main_loop - forward run and cost evaluation
36     c | with perturbed control vector element
37     c |-- calculate ratio of adj. vs. finite difference gradient
38     c |
39     c |-- grdchk_setxx - Reset control vector element
40     c |
41     c |-- grdchk_print - print results
42     c \ev
43     CEOI
44    
45     CBOP
46     C !ROUTINE: grdchk_main
47     C !INTERFACE:
48     subroutine grdchk_main( mythid )
49 heimbach 1.2
50 heimbach 1.3 C !DESCRIPTION: \bv
51 heimbach 1.2 c ==================================================================
52     c SUBROUTINE grdchk_main
53     c ==================================================================
54     c o Compare the gradients calculated by the adjoint model with
55     c finite difference approximations.
56     c started: Christian Eckert eckert@mit.edu 24-Feb-2000
57 heimbach 1.3.6.1 c continued&finished: heimbach@mit.edu: 13-Jun-2001
58     c changed: mlosch@ocean.mit.edu: 09-May-2002
59     c - added centered difference vs. 1-sided difference option
60     c - improved output format for readability
61     c - added control variable hFacC
62     c
63 heimbach 1.2 c ==================================================================
64     c SUBROUTINE grdchk_main
65     c ==================================================================
66 heimbach 1.3 C \ev
67 heimbach 1.2
68 heimbach 1.3 C !USES:
69 heimbach 1.2 implicit none
70    
71     c == global variables ==
72     #include "SIZE.h"
73     #include "EEPARAMS.h"
74     #include "PARAMS.h"
75     #include "grdchk.h"
76     #include "cost.h"
77    
78 heimbach 1.3 C !INPUT/OUTPUT PARAMETERS:
79 heimbach 1.2 c == routine arguments ==
80     integer mythid
81    
82     #ifdef ALLOW_GRADIENT_CHECK
83 heimbach 1.3 C !LOCAL VARIABLES:
84 heimbach 1.2 c == local variables ==
85     integer myiter
86     _RL mytime
87     integer bi, itlo, ithi
88     integer bj, jtlo, jthi
89     integer i, imin, imax
90     integer j, jmin, jmax
91     integer k
92    
93     integer jprocs
94     integer proc_grdchk
95     integer icomp
96     integer ichknum
97     integer icvrec
98     integer jtile
99     integer itile
100     integer layer
101     integer itilepos
102     integer jtilepos
103     integer itest
104     integer ierr
105     integer ierr_grdchk
106     _RL gfd
107     _RL fcref
108 heimbach 1.3.6.1 _RL fcpertplus, fcpertminus
109 heimbach 1.2 _RL ratio
110     _RL xxmemo_ref
111     _RL xxmemo_pert
112     _RL adxxmemo
113    
114     cph(
115     _RL tmpplot1(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
116     _RL tmpplot2(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
117     cph)
118    
119 heimbach 1.3.6.1 Cml(
120     _RL grdchk_epsfac
121     Cml)
122    
123 heimbach 1.2 c == end of interface ==
124 heimbach 1.3 CEOP
125 heimbach 1.2
126     c-- Set the loop ranges.
127     jtlo = mybylo(mythid)
128     jthi = mybyhi(mythid)
129     itlo = mybxlo(mythid)
130     ithi = mybxhi(mythid)
131     jmin = 1
132     jmax = sny
133     imin = 1
134     imax = snx
135    
136     c-- initialise variables
137     call grdchk_init( mythid )
138    
139     c-- Compute the adjoint models' gradients.
140     c-- Compute the unperturbed cost function.
141     cph Gradient via adjoint has already been computed,
142     cph and so has unperturbed cost function,
143     cph assuming all xx_ fields are initialised to zero.
144    
145     fcref = fc
146     ierr_grdchk = 0
147    
148     cph(
149     do bj = jtlo, jthi
150     do bi = itlo, ithi
151     do k = 1, nr
152     do j = jmin, jmax
153     do i = imin, imax
154     tmpplot1(i,j,k,bi,bj) = 0. _d 0
155     tmpplot2(i,j,k,bi,bj) = 0. _d 0
156     end do
157     end do
158     end do
159     end do
160     end do
161     cph)
162    
163 heimbach 1.3.6.1 if ( useCentralDiff ) then
164     grdchk_epsfac = 2. _d 0
165     else
166     grdchk_epsfac = 1. _d 0
167     end if
168    
169 heimbach 1.3.6.2 print *, 'ph-grd 3 -------------------------------'
170     print ('(2a)'),
171     & ' ph-grd 3 proc # i j k fc ref',
172     & ' fc + eps fc - eps'
173     print ('(2a)'),
174     & ' ph-grd 3 proc # i j k adj grad',
175     & ' fd grad 1 - fd/adj'
176    
177 heimbach 1.2 c-- Compute the finite difference approximations.
178     c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep
179     c-- gradient checks.
180     do jprocs = 1,numberOfProcs
181     proc_grdchk = jprocs - 1
182    
183     if ( myProcId .eq. proc_grdchk ) then
184    
185     do icomp = nbeg, nend, nstep
186    
187     ichknum = (icomp - nbeg)/nstep + 1
188    
189     if (ichknum .le. maxgrdchecks ) then
190    
191     c-- Determine the location of icomp on the grid.
192     call grdchk_loc( icomp, ichknum,
193     & icvrec, itile, jtile, layer,
194     & itilepos, jtilepos, itest, ierr,
195     & mythid )
196    
197     if ( ierr .eq. 0 ) then
198    
199 heimbach 1.3.6.1 c-- positive perturbation
200     grdchk_eps = abs(grdchk_eps)
201    
202     c-- get gradient component calculated via adjoint
203     call grdchk_getadxx( icvrec,
204     & itile, jtile, layer,
205     & itilepos, jtilepos,
206     & adxxmemo, mythid )
207     _BARRIER
208    
209 heimbach 1.2 c-- get control vector component from file
210     c-- perturb it and write back to file
211     call grdchk_getxx( icvrec,
212     & itile, jtile, layer,
213     & itilepos, jtilepos,
214     & xxmemo_ref, xxmemo_pert, mythid )
215     _BARRIER
216    
217 heimbach 1.3.6.1 c-- forward run with perturbed control vector
218     mytime = starttime
219     myiter = niter0
220     call the_main_loop( mytime, myiter, mythid )
221     fcpertplus = fc
222    
223     c-- Reset control vector.
224     call grdchk_setxx( icvrec,
225 heimbach 1.2 & itile, jtile, layer,
226     & itilepos, jtilepos,
227 heimbach 1.3.6.1 & xxmemo_ref, mythid )
228 heimbach 1.2 _BARRIER
229    
230 heimbach 1.3.6.1 fcpertminus = fcref
231    
232     if ( useCentralDiff ) then
233    
234     c-- repeat the proceedure for a negative perturbation
235     grdchk_eps = - abs(grdchk_eps)
236    
237     c-- get control vector component from file
238     c-- perturb it and write back to file
239     call grdchk_getxx( icvrec,
240     & itile, jtile, layer,
241     & itilepos, jtilepos,
242     & xxmemo_ref, xxmemo_pert, mythid )
243     _BARRIER
244    
245 heimbach 1.2 c-- forward run with perturbed control vector
246 heimbach 1.3.6.1 mytime = starttime
247     myiter = niter0
248     call the_main_loop( mytime, myiter, mythid )
249     fcpertminus = fc
250 heimbach 1.2
251 heimbach 1.3.6.1 c-- Reset control vector.
252     call grdchk_setxx( icvrec,
253     & itile, jtile, layer,
254     & itilepos, jtilepos,
255     & xxmemo_ref, mythid )
256     _BARRIER
257    
258     c reset grdchk_eps to a positive value
259     grdchk_eps = abs(grdchk_eps)
260    
261     end if
262     c--
263 heimbach 1.2 if ( grdchk_eps .eq. 0. ) then
264 heimbach 1.3.6.1 gfd = (fcpertplus-fcpertminus)
265 heimbach 1.2 else
266 heimbach 1.3.6.1 gfd = (fcpertplus-fcpertminus)
267     & /(grdchk_epsfac*grdchk_eps)
268 heimbach 1.2 endif
269    
270 heimbach 1.3.6.1 if ( adxxmemo .eq. 0. ) then
271 heimbach 1.2 ratio = abs( adxxmemo - gfd )
272     else
273 heimbach 1.3.6.1 ratio = 1. - gfd/adxxmemo
274 heimbach 1.2 endif
275    
276     cph(
277     tmpplot1(itilepos,jtilepos,layer,itile,jtile) =
278     & gfd
279     tmpplot2(itilepos,jtilepos,layer,itile,jtile) =
280     & ratio
281     cph)
282    
283    
284 heimbach 1.3.6.1 fcrmem ( ichknum ) = fcref
285     fcppmem ( ichknum ) = fcpertplus
286     fcpmmem ( ichknum ) = fcpertminus
287 heimbach 1.2 xxmemref ( ichknum ) = xxmemo_ref
288     xxmempert ( ichknum ) = xxmemo_pert
289     gfdmem ( ichknum ) = gfd
290     adxxmem ( ichknum ) = adxxmemo
291     ratiomem ( ichknum ) = ratio
292    
293     irecmem ( ichknum ) = icvrec
294     bimem ( ichknum ) = itile
295     bjmem ( ichknum ) = jtile
296     ilocmem ( ichknum ) = itilepos
297     jlocmem ( ichknum ) = jtilepos
298     klocmem ( ichknum ) = layer
299     icompmem ( ichknum ) = icomp
300     ichkmem ( ichknum ) = ichknum
301     itestmem ( ichknum ) = itest
302     ierrmem ( ichknum ) = ierr
303    
304     cph(
305     print *, 'ph-grd 3 -------------------------------'
306 heimbach 1.3.6.2 print '(a,5I5,2x,3(1x,E15.9))', ' ph-grd 3 ',
307     & myprocid,ichknum,itilepos,jtilepos,layer,
308 heimbach 1.3.6.1 & fcref, fcpertplus, fcpertminus
309 heimbach 1.3.6.2 print '(a,5I5,2x,3(1x,E15.9))', ' ph-grd 3 ',
310     & myprocid,ichknum,ichkmem(ichknum),
311 heimbach 1.2 & icompmem(ichknum),itestmem(ichknum),
312     & adxxmemo, gfd, ratio
313     cph)
314     else
315     c
316 heimbach 1.3.6.1 print *, 'ph-grd 3 -------------------------------'
317     print *, 'ph-grd 3 : ierr = ', ierr,
318     & ', icomp = ', icomp
319 heimbach 1.2 endif
320     else
321     ierr_grdchk = -1
322     endif
323    
324     enddo
325     endif
326    
327     c-- Everyone has to wait for the component to be reset.
328     _BARRIER
329    
330     enddo
331    
332     cph(
333     CALL WRITE_REC_XYZ_RL( 'grd_findiff' , tmpplot1, 1, 0, myThid)
334     CALL WRITE_REC_XYZ_RL( 'grd_ratio' , tmpplot2, 1, 0, myThid)
335     cph)
336    
337     c-- Print the results of the gradient check.
338     call grdchk_print( ichknum, ierr_grdchk, mythid )
339    
340     #endif /* ALLOW_GRADIENT_CHECK */
341    
342     end

  ViewVC Help
Powered by ViewVC 1.1.22