1 |
C $Header: /u/gcmpack/MITgcm/pkg/grdchk/grdchk_main.F,v 1.3.4.1 2002/05/30 22:12:32 heimbach Exp $ |
2 |
|
3 |
#include "CPP_OPTIONS.h" |
4 |
|
5 |
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 |
|
50 |
C !DESCRIPTION: \bv |
51 |
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 |
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 |
c ================================================================== |
64 |
c SUBROUTINE grdchk_main |
65 |
c ================================================================== |
66 |
C \ev |
67 |
|
68 |
C !USES: |
69 |
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 |
C !INPUT/OUTPUT PARAMETERS: |
79 |
c == routine arguments == |
80 |
integer mythid |
81 |
|
82 |
#ifdef ALLOW_GRADIENT_CHECK |
83 |
C !LOCAL VARIABLES: |
84 |
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 |
_RL fcpertplus, fcpertminus |
109 |
_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 |
Cml( |
120 |
_RL grdchk_epsfac |
121 |
Cml) |
122 |
|
123 |
c == end of interface == |
124 |
CEOP |
125 |
|
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 |
if ( useCentralDiff ) then |
164 |
grdchk_epsfac = 2. _d 0 |
165 |
else |
166 |
grdchk_epsfac = 1. _d 0 |
167 |
end if |
168 |
|
169 |
c-- Compute the finite difference approximations. |
170 |
c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep |
171 |
c-- gradient checks. |
172 |
do jprocs = 1,numberOfProcs |
173 |
proc_grdchk = jprocs - 1 |
174 |
|
175 |
if ( myProcId .eq. proc_grdchk ) then |
176 |
|
177 |
do icomp = nbeg, nend, nstep |
178 |
|
179 |
ichknum = (icomp - nbeg)/nstep + 1 |
180 |
|
181 |
if (ichknum .le. maxgrdchecks ) then |
182 |
|
183 |
c-- Determine the location of icomp on the grid. |
184 |
call grdchk_loc( icomp, ichknum, |
185 |
& icvrec, itile, jtile, layer, |
186 |
& itilepos, jtilepos, itest, ierr, |
187 |
& mythid ) |
188 |
|
189 |
if ( ierr .eq. 0 ) then |
190 |
|
191 |
c-- positive perturbation |
192 |
grdchk_eps = abs(grdchk_eps) |
193 |
|
194 |
c-- get gradient component calculated via adjoint |
195 |
call grdchk_getadxx( icvrec, |
196 |
& itile, jtile, layer, |
197 |
& itilepos, jtilepos, |
198 |
& adxxmemo, mythid ) |
199 |
_BARRIER |
200 |
|
201 |
c-- get control vector component from file |
202 |
c-- perturb it and write back to file |
203 |
call grdchk_getxx( icvrec, |
204 |
& itile, jtile, layer, |
205 |
& itilepos, jtilepos, |
206 |
& xxmemo_ref, xxmemo_pert, mythid ) |
207 |
_BARRIER |
208 |
|
209 |
c-- forward run with perturbed control vector |
210 |
mytime = starttime |
211 |
myiter = niter0 |
212 |
call the_main_loop( mytime, myiter, mythid ) |
213 |
fcpertplus = fc |
214 |
|
215 |
c-- Reset control vector. |
216 |
call grdchk_setxx( icvrec, |
217 |
& itile, jtile, layer, |
218 |
& itilepos, jtilepos, |
219 |
& xxmemo_ref, mythid ) |
220 |
_BARRIER |
221 |
|
222 |
fcpertminus = fcref |
223 |
|
224 |
if ( useCentralDiff ) then |
225 |
|
226 |
c-- repeat the proceedure for a negative perturbation |
227 |
grdchk_eps = - abs(grdchk_eps) |
228 |
|
229 |
c-- get control vector component from file |
230 |
c-- perturb it and write back to file |
231 |
call grdchk_getxx( icvrec, |
232 |
& itile, jtile, layer, |
233 |
& itilepos, jtilepos, |
234 |
& xxmemo_ref, xxmemo_pert, mythid ) |
235 |
_BARRIER |
236 |
|
237 |
c-- forward run with perturbed control vector |
238 |
mytime = starttime |
239 |
myiter = niter0 |
240 |
call the_main_loop( mytime, myiter, mythid ) |
241 |
fcpertminus = fc |
242 |
|
243 |
c-- Reset control vector. |
244 |
call grdchk_setxx( icvrec, |
245 |
& itile, jtile, layer, |
246 |
& itilepos, jtilepos, |
247 |
& xxmemo_ref, mythid ) |
248 |
_BARRIER |
249 |
|
250 |
c reset grdchk_eps to a positive value |
251 |
grdchk_eps = abs(grdchk_eps) |
252 |
|
253 |
end if |
254 |
c-- |
255 |
if ( grdchk_eps .eq. 0. ) then |
256 |
gfd = (fcpertplus-fcpertminus) |
257 |
else |
258 |
gfd = (fcpertplus-fcpertminus) |
259 |
& /(grdchk_epsfac*grdchk_eps) |
260 |
endif |
261 |
|
262 |
if ( adxxmemo .eq. 0. ) then |
263 |
ratio = abs( adxxmemo - gfd ) |
264 |
else |
265 |
ratio = 1. - gfd/adxxmemo |
266 |
endif |
267 |
|
268 |
cph( |
269 |
tmpplot1(itilepos,jtilepos,layer,itile,jtile) = |
270 |
& gfd |
271 |
tmpplot2(itilepos,jtilepos,layer,itile,jtile) = |
272 |
& ratio |
273 |
cph) |
274 |
|
275 |
|
276 |
fcrmem ( ichknum ) = fcref |
277 |
fcppmem ( ichknum ) = fcpertplus |
278 |
fcpmmem ( ichknum ) = fcpertminus |
279 |
xxmemref ( ichknum ) = xxmemo_ref |
280 |
xxmempert ( ichknum ) = xxmemo_pert |
281 |
gfdmem ( ichknum ) = gfd |
282 |
adxxmem ( ichknum ) = adxxmemo |
283 |
ratiomem ( ichknum ) = ratio |
284 |
|
285 |
irecmem ( ichknum ) = icvrec |
286 |
bimem ( ichknum ) = itile |
287 |
bjmem ( ichknum ) = jtile |
288 |
ilocmem ( ichknum ) = itilepos |
289 |
jlocmem ( ichknum ) = jtilepos |
290 |
klocmem ( ichknum ) = layer |
291 |
icompmem ( ichknum ) = icomp |
292 |
ichkmem ( ichknum ) = ichknum |
293 |
itestmem ( ichknum ) = itest |
294 |
ierrmem ( ichknum ) = ierr |
295 |
|
296 |
cph( |
297 |
print *, 'ph-grd 3 -------------------------------' |
298 |
print '(a,4I5,3(1x,E15.9))', 'ph-grd 3 ', |
299 |
& ichknum,itilepos,jtilepos,layer, |
300 |
& fcref, fcpertplus, fcpertminus |
301 |
print '(a,4I5,3(1x,E15.9))', 'ph-grd 3 ', |
302 |
& ichknum,ichkmem(ichknum), |
303 |
& icompmem(ichknum),itestmem(ichknum), |
304 |
& adxxmemo, gfd, ratio |
305 |
cph) |
306 |
else |
307 |
c |
308 |
print *, 'ph-grd 3 -------------------------------' |
309 |
print *, 'ph-grd 3 : ierr = ', ierr, |
310 |
& ', icomp = ', icomp |
311 |
endif |
312 |
else |
313 |
ierr_grdchk = -1 |
314 |
endif |
315 |
|
316 |
enddo |
317 |
endif |
318 |
|
319 |
c-- Everyone has to wait for the component to be reset. |
320 |
_BARRIER |
321 |
|
322 |
enddo |
323 |
|
324 |
cph( |
325 |
CALL WRITE_REC_XYZ_RL( 'grd_findiff' , tmpplot1, 1, 0, myThid) |
326 |
CALL WRITE_REC_XYZ_RL( 'grd_ratio' , tmpplot2, 1, 0, myThid) |
327 |
cph) |
328 |
|
329 |
c-- Print the results of the gradient check. |
330 |
call grdchk_print( ichknum, ierr_grdchk, mythid ) |
331 |
|
332 |
#endif /* ALLOW_GRADIENT_CHECK */ |
333 |
|
334 |
end |