/[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.6 - (show annotations) (download)
Mon Sep 16 18:11:58 2002 UTC (21 years, 8 months ago) by heimbach
Branch: MAIN
CVS Tags: checkpoint46n_post, checkpoint47e_post, checkpoint46l_post, checkpoint46g_pre, checkpoint47c_post, checkpoint46f_post, checkpoint48e_post, checkpoint46l_pre, checkpoint48b_post, checkpoint48c_pre, checkpoint47d_pre, checkpoint47a_post, checkpoint48d_pre, checkpoint47i_post, checkpoint47d_post, checkpoint48d_post, checkpoint48f_post, checkpoint46j_pre, checkpoint48h_post, checkpoint47g_post, checkpoint46j_post, checkpoint46k_post, checkpoint48a_post, checkpoint47j_post, branch-exfmods-tag, checkpoint48c_post, checkpoint47b_post, checkpoint46h_pre, checkpoint46m_post, checkpoint46g_post, checkpoint47f_post, checkpoint46i_post, checkpoint47, checkpoint48, checkpoint46h_post, checkpoint48g_post, checkpoint47h_post
Branch point for: branch-exfmods-curt
Changes since 1.5: +103 -51 lines
Enable tangent linear (forward mode) gradient checks:
o extended active file handling to g_... files
o added TANGENT_SIMULATION to theSimulationMode
o extended grdchk package accordingly

1 C $Header: /u/gcmpack/MITgcm/pkg/grdchk/grdchk_main.F,v 1.5 2002/07/13 02:55:58 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_ad
110 _RL ratio_ftl
111 _RL xxmemo_ref
112 _RL xxmemo_pert
113 _RL adxxmemo
114 _RL ftlxxmemo
115 _RL localEps
116 _RL grdchk_epsfac
117
118 #ifdef ALLOW_TANGENTLINEAR_RUN
119 _RL g_fc
120 common /g_cost_r/ g_fc
121 #endif
122
123 _RL tmpplot1(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
124 _RL tmpplot2(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
125 _RL tmpplot3(1-olx:snx+olx,1-oly:sny+oly,nr,nsx,nsy)
126
127 c == end of interface ==
128 CEOP
129
130 c-- Set the loop ranges.
131 jtlo = mybylo(mythid)
132 jthi = mybyhi(mythid)
133 itlo = mybxlo(mythid)
134 ithi = mybxhi(mythid)
135 jmin = 1
136 jmax = sny
137 imin = 1
138 imax = snx
139
140 c-- initialise variables
141 call grdchk_init( mythid )
142
143 c-- Compute the adjoint models' gradients.
144 c-- Compute the unperturbed cost function.
145 c-- Gradient via adjoint has already been computed,
146 c-- and so has unperturbed cost function,
147 c-- assuming all xx_ fields are initialised to zero.
148
149 fcref = fc
150 ierr_grdchk = 0
151
152 do bj = jtlo, jthi
153 do bi = itlo, ithi
154 do k = 1, nr
155 do j = jmin, jmax
156 do i = imin, imax
157 tmpplot1(i,j,k,bi,bj) = 0. _d 0
158 tmpplot2(i,j,k,bi,bj) = 0. _d 0
159 tmpplot3(i,j,k,bi,bj) = 0. _d 0
160 end do
161 end do
162 end do
163 end do
164 end do
165
166 if ( useCentralDiff ) then
167 grdchk_epsfac = 2. _d 0
168 else
169 grdchk_epsfac = 1. _d 0
170 end if
171
172 c-- Compute the finite difference approximations.
173 c-- Cycle through all processes doing NINT(nend-nbeg+1)/nstep
174 c-- gradient checks.
175 do jprocs = 1,numberOfProcs
176 proc_grdchk = jprocs - 1
177
178 if ( myProcId .eq. proc_grdchk ) then
179
180 do icomp = nbeg, nend, nstep
181
182 ichknum = (icomp - nbeg)/nstep + 1
183
184 if (ichknum .le. maxgrdchecks ) then
185
186 c-- Determine the location of icomp on the grid.
187 call grdchk_loc( icomp, ichknum,
188 & icvrec, itile, jtile, layer,
189 & itilepos, jtilepos, itest, ierr,
190 & mythid )
191
192 if ( ierr .eq. 0 ) then
193
194 c******************************************************
195 c-- (A): get gradient component calculated via adjoint
196 c******************************************************
197 call grdchk_getadxx( icvrec,
198 & itile, jtile, layer,
199 & itilepos, jtilepos,
200 & adxxmemo, mythid )
201 _BARRIER
202
203 #ifdef ALLOW_TANGENTLINEAR_RUN
204 c******************************************************
205 c-- (B): Get gradient component g_fc from tangent linear run:
206 c******************************************************
207 c--
208 c-- 1. perturb control vector component: xx(i)=1.
209
210 localEps = 1.
211 call grdchk_getxx( icvrec, TANGENT_SIMULATION,
212 & itile, jtile, layer,
213 & itilepos, jtilepos,
214 & xxmemo_ref, xxmemo_pert, localEps,
215 & mythid )
216 _BARRIER
217
218 c--
219 c-- 2. perform tangent linear run
220 mytime = starttime
221 myiter = niter0
222 g_fc = 0.
223 call g_the_main_loop( mytime, myiter, mythid )
224 ftlxxmemo = g_fc
225 c--
226 c-- 3. reset control vector
227 call grdchk_setxx( icvrec, TANGENT_SIMULATION,
228 & itile, jtile, layer,
229 & itilepos, jtilepos,
230 & xxmemo_ref, mythid )
231 _BARRIER
232 #endif
233
234 c******************************************************
235 c-- (C): Get gradient via finite difference perturbation
236 c******************************************************
237
238 c-- get control vector component from file
239 c-- perturb it and write back to file:
240 c-- positive perturbation
241 localEps = abs(grdchk_eps)
242 call grdchk_getxx( icvrec, FORWARD_SIMULATION,
243 & itile, jtile, layer,
244 & itilepos, jtilepos,
245 & xxmemo_ref, xxmemo_pert, localEps,
246 & mythid )
247 _BARRIER
248
249 c-- forward run with perturbed control vector
250 mytime = starttime
251 myiter = niter0
252 call the_main_loop( mytime, myiter, mythid )
253 fcpertplus = fc
254
255 c-- Reset control vector.
256 call grdchk_setxx( icvrec, FORWARD_SIMULATION,
257 & itile, jtile, layer,
258 & itilepos, jtilepos,
259 & xxmemo_ref, mythid )
260 _BARRIER
261
262 fcpertminus = fcref
263
264 if ( useCentralDiff ) then
265
266 c-- get control vector component from file
267 c-- perturb it and write back to file:
268 c-- repeat the proceedure for a negative perturbation
269 localEps = - abs(grdchk_eps)
270 call grdchk_getxx( icvrec, FORWARD_SIMULATION,
271 & itile, jtile, layer,
272 & itilepos, jtilepos,
273 & xxmemo_ref, xxmemo_pert, localEps,
274 & mythid )
275 _BARRIER
276
277 c-- forward run with perturbed control vector
278 mytime = starttime
279 myiter = niter0
280 call the_main_loop( mytime, myiter, mythid )
281 fcpertminus = fc
282
283 c-- Reset control vector.
284 call grdchk_setxx( icvrec, FORWARD_SIMULATION,
285 & itile, jtile, layer,
286 & itilepos, jtilepos,
287 & xxmemo_ref, mythid )
288 _BARRIER
289
290 end if
291 c--
292 c******************************************************
293 c-- (D): calculate relative differences between gradients
294 c******************************************************
295
296 if ( grdchk_eps .eq. 0. ) then
297 gfd = (fcpertplus-fcpertminus)
298 else
299 gfd = (fcpertplus-fcpertminus)
300 & /(grdchk_epsfac*grdchk_eps)
301 endif
302
303 if ( adxxmemo .eq. 0. ) then
304 ratio_ad = abs( adxxmemo - gfd )
305 else
306 ratio_ad = 1. - gfd/adxxmemo
307 endif
308
309 if ( ftlxxmemo .eq. 0. ) then
310 ratio_ftl = abs( ftlxxmemo - gfd )
311 else
312 ratio_ftl = 1. - gfd/ftlxxmemo
313 endif
314
315 tmpplot1(itilepos,jtilepos,layer,itile,jtile) =
316 & gfd
317 tmpplot2(itilepos,jtilepos,layer,itile,jtile) =
318 & ratio_ad
319 tmpplot3(itilepos,jtilepos,layer,itile,jtile) =
320 & ratio_ftl
321
322
323 fcrmem ( ichknum ) = fcref
324 fcppmem ( ichknum ) = fcpertplus
325 fcpmmem ( ichknum ) = fcpertminus
326 xxmemref ( ichknum ) = xxmemo_ref
327 xxmempert ( ichknum ) = xxmemo_pert
328 gfdmem ( ichknum ) = gfd
329 adxxmem ( ichknum ) = adxxmemo
330 ftlxxmem ( ichknum ) = ftlxxmemo
331 ratioadmem ( ichknum ) = ratio_ad
332 ratioftlmem ( ichknum ) = ratio_ftl
333
334 irecmem ( ichknum ) = icvrec
335 bimem ( ichknum ) = itile
336 bjmem ( ichknum ) = jtile
337 ilocmem ( ichknum ) = itilepos
338 jlocmem ( ichknum ) = jtilepos
339 klocmem ( ichknum ) = layer
340 icompmem ( ichknum ) = icomp
341 ichkmem ( ichknum ) = ichknum
342 itestmem ( ichknum ) = itest
343 ierrmem ( ichknum ) = ierr
344
345 cph(
346 print *, 'ph-grd 3 -------------------------------'
347 print '(a,4I5,3(1x,E15.9))', 'ph-grd 3 ',
348 & ichknum,itilepos,jtilepos,layer,
349 & fcref, fcpertplus, fcpertminus
350 print '(a,4I5,3(1x,E15.9))', 'ph-grd 3 ',
351 & ichknum,ichkmem(ichknum),
352 & icompmem(ichknum),itestmem(ichknum),
353 & adxxmemo, gfd, ratio_ad
354 print '(a,4I5,3(1x,E15.9))', 'ph-grd 3 ',
355 & ichknum,ichkmem(ichknum),
356 & icompmem(ichknum),itestmem(ichknum),
357 & ftlxxmemo, gfd, ratio_ftl
358 cph)
359 else
360 c
361 print *, 'ph-grd 3 -------------------------------'
362 print *, 'ph-grd 3 : ierr = ', ierr,
363 & ', icomp = ', icomp
364 endif
365 else
366 ierr_grdchk = -1
367 endif
368
369 enddo
370 endif
371
372 c-- Everyone has to wait for the component to be reset.
373 _BARRIER
374
375 enddo
376
377 CALL WRITE_REC_XYZ_RL( 'grd_findiff' , tmpplot1, 1, 0, myThid)
378 CALL WRITE_REC_XYZ_RL( 'grd_ratio_ad' , tmpplot2, 1, 0, myThid)
379 CALL WRITE_REC_XYZ_RL( 'grd_ratio_ftl' , tmpplot3, 1, 0, myThid)
380
381 c-- Print the results of the gradient check.
382 call grdchk_print( ichknum, ierr_grdchk, mythid )
383
384 #endif /* ALLOW_GRADIENT_CHECK */
385
386 end

  ViewVC Help
Powered by ViewVC 1.1.22