1 |
C $Header: /u/gcmpack/models/MITgcmUV/pkg/grdchk/grdchk_main.F,v 1.2 2001/07/13 14:50:46 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: heimbach@mit.edu: 13-Jun-2001 |
58 |
c ================================================================== |
59 |
c SUBROUTINE grdchk_main |
60 |
c ================================================================== |
61 |
C \ev |
62 |
|
63 |
C !USES: |
64 |
implicit none |
65 |
|
66 |
c == global variables == |
67 |
#include "SIZE.h" |
68 |
#include "EEPARAMS.h" |
69 |
#include "PARAMS.h" |
70 |
#include "grdchk.h" |
71 |
#include "cost.h" |
72 |
|
73 |
C !INPUT/OUTPUT PARAMETERS: |
74 |
c == routine arguments == |
75 |
integer mythid |
76 |
|
77 |
#ifdef ALLOW_GRADIENT_CHECK |
78 |
C !LOCAL VARIABLES: |
79 |
c == local variables == |
80 |
integer myiter |
81 |
_RL mytime |
82 |
integer bi, itlo, ithi |
83 |
integer bj, jtlo, jthi |
84 |
integer i, imin, imax |
85 |
integer j, jmin, jmax |
86 |
integer k |
87 |
|
88 |
integer jprocs |
89 |
integer proc_grdchk |
90 |
integer icomp |
91 |
integer ichknum |
92 |
integer icvrec |
93 |
integer jtile |
94 |
integer itile |
95 |
integer layer |
96 |
integer itilepos |
97 |
integer jtilepos |
98 |
integer itest |
99 |
integer ierr |
100 |
integer ierr_grdchk |
101 |
_RL gfd |
102 |
_RL fcref |
103 |
_RL fcpert |
104 |
_RL ratio |
105 |
_RL xxmemo_ref |
106 |
_RL xxmemo_pert |
107 |
_RL adxxmemo |
108 |
|
109 |
cph( |
110 |
_RL tmpplot1(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) |
111 |
_RL tmpplot2(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy) |
112 |
cph) |
113 |
|
114 |
c == end of interface == |
115 |
CEOP |
116 |
|
117 |
c-- Set the loop ranges. |
118 |
jtlo = mybylo(mythid) |
119 |
jthi = mybyhi(mythid) |
120 |
itlo = mybxlo(mythid) |
121 |
ithi = mybxhi(mythid) |
122 |
jmin = 1 |
123 |
jmax = sny |
124 |
imin = 1 |
125 |
imax = snx |
126 |
|
127 |
c-- initialise variables |
128 |
call grdchk_init( mythid ) |
129 |
|
130 |
c-- Compute the adjoint models' gradients. |
131 |
c-- Compute the unperturbed cost function. |
132 |
cph Gradient via adjoint has already been computed, |
133 |
cph and so has unperturbed cost function, |
134 |
cph assuming all xx_ fields are initialised to zero. |
135 |
|
136 |
fcref = fc |
137 |
ierr_grdchk = 0 |
138 |
|
139 |
cph( |
140 |
do bj = jtlo, jthi |
141 |
do bi = itlo, ithi |
142 |
do k = 1, nr |
143 |
do j = jmin, jmax |
144 |
do i = imin, imax |
145 |
tmpplot1(i,j,k,bi,bj) = 0. _d 0 |
146 |
tmpplot2(i,j,k,bi,bj) = 0. _d 0 |
147 |
end do |
148 |
end do |
149 |
end do |
150 |
end do |
151 |
end do |
152 |
cph) |
153 |
|
154 |
c-- Compute the finite difference approximations. |
155 |
c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep |
156 |
c-- gradient checks. |
157 |
do jprocs = 1,numberOfProcs |
158 |
proc_grdchk = jprocs - 1 |
159 |
|
160 |
if ( myProcId .eq. proc_grdchk ) then |
161 |
|
162 |
do icomp = nbeg, nend, nstep |
163 |
|
164 |
ichknum = (icomp - nbeg)/nstep + 1 |
165 |
|
166 |
if (ichknum .le. maxgrdchecks ) then |
167 |
|
168 |
c-- Determine the location of icomp on the grid. |
169 |
call grdchk_loc( icomp, ichknum, |
170 |
& icvrec, itile, jtile, layer, |
171 |
& itilepos, jtilepos, itest, ierr, |
172 |
& mythid ) |
173 |
|
174 |
if ( ierr .eq. 0 ) then |
175 |
|
176 |
c-- get control vector component from file |
177 |
c-- perturb it and write back to file |
178 |
call grdchk_getxx( icvrec, |
179 |
& itile, jtile, layer, |
180 |
& itilepos, jtilepos, |
181 |
& xxmemo_ref, xxmemo_pert, mythid ) |
182 |
_BARRIER |
183 |
|
184 |
c-- get gradient component calculated via adjoint |
185 |
call grdchk_getadxx( icvrec, |
186 |
& itile, jtile, layer, |
187 |
& itilepos, jtilepos, |
188 |
& adxxmemo, mythid ) |
189 |
_BARRIER |
190 |
|
191 |
c-- forward run with perturbed control vector |
192 |
mytime = starttime |
193 |
myiter = niter0 |
194 |
call the_main_loop( mytime, myiter, mythid ) |
195 |
fcpert = fc |
196 |
|
197 |
if ( grdchk_eps .eq. 0. ) then |
198 |
gfd = (fcpert-fcref) |
199 |
else |
200 |
gfd = (fcpert-fcref)/grdchk_eps |
201 |
endif |
202 |
|
203 |
if ( gfd .eq. 0. ) then |
204 |
ratio = abs( adxxmemo - gfd ) |
205 |
else |
206 |
ratio = 1. - adxxmemo/gfd |
207 |
endif |
208 |
|
209 |
c-- Reset control vector. |
210 |
call grdchk_setxx( icvrec, |
211 |
& itile, jtile, layer, |
212 |
& itilepos, jtilepos, |
213 |
& xxmemo_ref, mythid ) |
214 |
_BARRIER |
215 |
|
216 |
cph( |
217 |
tmpplot1(itilepos,jtilepos,layer,itile,jtile) = |
218 |
& gfd |
219 |
tmpplot2(itilepos,jtilepos,layer,itile,jtile) = |
220 |
& ratio |
221 |
cph) |
222 |
|
223 |
|
224 |
fcpmem ( ichknum ) = fcpert |
225 |
xxmemref ( ichknum ) = xxmemo_ref |
226 |
xxmempert ( ichknum ) = xxmemo_pert |
227 |
gfdmem ( ichknum ) = gfd |
228 |
adxxmem ( ichknum ) = adxxmemo |
229 |
ratiomem ( ichknum ) = ratio |
230 |
|
231 |
irecmem ( ichknum ) = icvrec |
232 |
bimem ( ichknum ) = itile |
233 |
bjmem ( ichknum ) = jtile |
234 |
ilocmem ( ichknum ) = itilepos |
235 |
jlocmem ( ichknum ) = jtilepos |
236 |
klocmem ( ichknum ) = layer |
237 |
icompmem ( ichknum ) = icomp |
238 |
ichkmem ( ichknum ) = ichknum |
239 |
itestmem ( ichknum ) = itest |
240 |
ierrmem ( ichknum ) = ierr |
241 |
|
242 |
cph( |
243 |
print *, 'ph-grd 3 -------------------------------' |
244 |
print *, 'ph-grd 3 ', |
245 |
& ichknum,itilepos,jtilepos,layer, |
246 |
& fcref, fcpert |
247 |
print *, 'ph-grd 3 ', |
248 |
& ichknum,ichkmem(ichknum), |
249 |
& icompmem(ichknum),itestmem(ichknum), |
250 |
& adxxmemo, gfd, ratio |
251 |
cph) |
252 |
else |
253 |
c |
254 |
endif |
255 |
else |
256 |
ierr_grdchk = -1 |
257 |
endif |
258 |
|
259 |
enddo |
260 |
endif |
261 |
|
262 |
c-- Everyone has to wait for the component to be reset. |
263 |
_BARRIER |
264 |
|
265 |
enddo |
266 |
|
267 |
cph( |
268 |
CALL WRITE_REC_XYZ_RL( 'grd_findiff' , tmpplot1, 1, 0, myThid) |
269 |
CALL WRITE_REC_XYZ_RL( 'grd_ratio' , tmpplot2, 1, 0, myThid) |
270 |
cph) |
271 |
|
272 |
c-- Print the results of the gradient check. |
273 |
call grdchk_print( ichknum, ierr_grdchk, mythid ) |
274 |
|
275 |
#endif /* ALLOW_GRADIENT_CHECK */ |
276 |
|
277 |
end |