/[MITgcm]/manual/s_autodiff/doc_ad_examples.tex
ViewVC logotype

Annotation of /manual/s_autodiff/doc_ad_examples.tex

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


Revision 1.1 - (hide annotations) (download) (as text)
Thu Feb 28 19:32:20 2002 UTC (23 years, 4 months ago) by cnh
Branch: MAIN
File MIME type: application/x-tex
Updates for special on-line version with
hyperlinked and animated figures

Separating tutorials and reference material

1 cnh 1.1 % $Header: /u/u0/gcmpack/manual/part5/doc_ad_2.tex,v 1.13 2002/01/18 22:56:45 heimbach Exp $
2     % $Name: $
3    
4     %**********************************************************************
5     \section{Sensitivity of Air-Sea Exchange to Tracer Injection Site }
6     \label{sect:eg-simple-tracer-adjoint}
7     \label{sec_ad_setup_ex}
8     %**********************************************************************
9    
10     The MITGCM has been adapted to enable AD using TAMC or TAF.
11     The present description, therefore, is specific to the
12     use of TAMC or TAF as AD tool.
13     The following sections describe the steps which are necessary to
14     generate a tangent linear or adjoint model of the MITGCM.
15     We take as an example the sensitivity of carbon sequestration
16     in the ocean.
17     The AD-relevant hooks in the code are sketched in
18     \ref{fig:adthemodel}, \ref{fig:adthemain}.
19    
20     \subsection{Overview of the experiment}
21    
22     We describe an adjoint sensitivity analysis of out-gassing from
23     the ocean into the atmosphere of a carbon-like tracer injected
24     into the ocean interior (see \cite{hil-eta:01}).
25    
26     \subsubsection{Passive tracer equation}
27    
28     For this work the MITGCM was augmented with a thermodynamically
29     inactive tracer, $C$. Tracer residing in the ocean
30     model surface layer is out-gassed according to a relaxation time scale,
31     $\mu$. Within the ocean interior, the tracer is passively advected
32     by the ocean model currents. The full equation for the time evolution
33     %
34     \begin{equation}
35     \label{carbon_ddt}
36     \frac{\partial C}{\partial t} \, = \,
37     -U\cdot \nabla C \, - \, \mu C \, + \, \Gamma(C) \,+ \, S
38     \end{equation}
39     %
40     also includes a source term $S$. This term
41     represents interior sources of $C$ such as would arise due to
42     direct injection.
43     The velocity term, $U$, is the sum of the
44     model Eulerian circulation and an eddy-induced velocity, the latter
45     parameterized according to Gent/McWilliams
46     (\cite{gen-mcw:90, gen-eta:95}).
47     The convection function, $\Gamma$, mixes $C$ vertically wherever the
48     fluid is locally statically unstable.
49    
50     The out-gassing time scale, $\mu$, in eqn. (\ref{carbon_ddt})
51     is set so that \( 1/\mu \sim 1 \ \mathrm{year} \) for the surface
52     ocean and $\mu=0$ elsewhere. With this value, eqn. (\ref{carbon_ddt})
53     is valid as a prognostic equation for small perturbations in oceanic
54     carbon concentrations. This configuration provides a
55     powerful tool for examining the impact of large-scale ocean circulation
56     on $ CO_2 $ out-gassing due to interior injections.
57     As source we choose a constant in time injection of
58     $ S = 1 \,\, {\rm mol / s}$.
59    
60     \subsubsection{Model configuration}
61    
62     The model configuration employed has a constant
63     $4^\circ \times 4^\circ$ resolution horizontal grid and realistic
64     geography and bathymetry. Twenty vertical layers are used with
65     vertical spacing ranging
66     from 50 m near the surface to 815 m at depth.
67     Driven to steady-state by climatological wind-stress, heat and
68     fresh-water forcing the model reproduces well known large-scale
69     features of the ocean general circulation.
70    
71     \subsubsection{Out-gassing cost function}
72    
73     To quantify and understand out-gassing due to injections of $C$
74     in eqn. (\ref{carbon_ddt}),
75     we define a cost function $ {\cal J} $ that measures the total amount of
76     tracer out-gassed at each timestep:
77     %
78     \begin{equation}
79     \label{cost_tracer}
80     {\cal J}(t=T)=\int_{t=0}^{t=T}\int_{A} \mu C \, dA \, dt
81     \end{equation}
82     %
83     Equation(\ref{cost_tracer}) integrates the out-gassing term, $\mu C$,
84     from (\ref{carbon_ddt})
85     over the entire ocean surface area, $A$, and accumulates it
86     up to time $T$.
87     Physically, ${\cal J}$ can be thought of as representing the amount of
88     $CO_2$ that our model predicts would be out-gassed following an
89     injection at rate $S$.
90     The sensitivity of ${\cal J}$ to the spatial location of $S$,
91     $\frac{\partial {\cal J}}{\partial S}$,
92     can be used to identify regions from which circulation
93     would cause $CO_2$ to rapidly out-gas following injection
94     and regions in which $CO_2$ injections would remain effectively
95     sequestered within the ocean.
96    
97     \subsection{Code configuration}
98    
99     The model configuration for this experiment resides under the
100     directory {\it verification/carbon/}.
101     The code customization routines are in {\it verification/carbon/code/}:
102     %
103     \begin{itemize}
104     %
105     \item {\it .genmakerc}
106     %
107     \item {\it COST\_CPPOPTIONS.h}
108     %
109     \item {\it CPP\_EEOPTIONS.h}
110     %
111     \item {\it CPP\_OPTIONS.h}
112     %
113     \item {\it CTRL\_OPTIONS.h}
114     %
115     \item {\it ECCO\_OPTIONS.h}
116     %
117     \item {\it SIZE.h}
118     %
119     \item {\it adcommon.h}
120     %
121     \item {\it tamc.h}
122     %
123     \end{itemize}
124     %
125     The runtime flag and parameters settings are contained in
126     {\it verification/carbon/input/},
127     together with the forcing fields and and restart files:
128     %
129     \begin{itemize}
130     %
131     \item {\it data}
132     %
133     \item {\it data.cost}
134     %
135     \item {\it data.ctrl}
136     %
137     \item {\it data.gmredi}
138     %
139     \item {\it data.grdchk}
140     %
141     \item {\it data.optim}
142     %
143     \item {\it data.pkg}
144     %
145     \item {\it eedata}
146     %
147     \item {\it topog.bin}
148     %
149     \item {\it windx.bin, windy.bin}
150     %
151     \item {\it salt.bin, theta.bin}
152     %
153     \item {\it SSS.bin, SST.bin}
154     %
155     \item {\it pickup*}
156     %
157     \end{itemize}
158     %
159     Finally, the file to generate the adjoint code resides in
160     $ adjoint/ $:
161     %
162     \begin{itemize}
163     %
164     \item {\it makefile}
165     %
166     \end{itemize}
167     %
168    
169     Below we describe the customizations of this files which are
170     specific to this experiment.
171    
172     \subsubsection{File {\it .genmakerc}}
173     This file overwrites default settings of {\it genmake}.
174     In the present example it is used to switch on the following
175     packages which are related to automatic differentiation
176     and are disabled by default: \\
177     \hspace*{4ex} {\tt set ENABLE=( autodiff cost ctrl ecco gmredi grdchk kpp )} \\
178     Other packages which are not needed are switched off: \\
179     \hspace*{4ex} {\tt set DISABLE=( aim obcs zonal\_filt shap\_filt cal exf )}
180    
181     \subsubsection{File {\it COST\_CPPOPTIONS.h, CTRL\_OPTIONS.h}}
182    
183     These files used to contain package-specific CPP-options
184     (see Section \ref{???}).
185     For technical reasons those options have been grouped together
186     in the file {\it ECCO\_OPTIONS.h}.
187     To retain the modularity, the files have been kept and contain
188     the standard include of the {\it CPP\_OPTIONS.h} file.
189    
190     \subsubsection{File {\it CPP\_EEOPTIONS.h}}
191    
192     This file contains 'wrapper'-specific CPP options.
193     It only needs to be changed if the code is to be run
194     in a parallel environment (see Section \ref{???}).
195    
196     \subsubsection{File {\it CPP\_OPTIONS.h}}
197    
198     This file contains model-specific CPP options
199     (see Section \ref{???}).
200     Most options are related to the forward model setup.
201     They are identical to the global steady circulation setup of
202     {\it verification/exp2/}.
203     The three options specific to this experiment are \\
204     \hspace*{4ex} {\tt \#define ALLOW\_PASSIVE\_TRACER} \\
205     This flag enables the code to carry through the
206     advection/diffusion of a passive tracer along the
207     model integration. \\
208     \hspace*{4ex} {\tt \#define ALLOW\_MIT\_ADJOINT\_RUN} \\
209     This flag enables the inclusion of some AD-related fields
210     concerning initialization, link between control variables
211     and forward model variables, and the call to the top-level
212     forward/adjoint subroutine {\it adthe\_main\_loop}
213     instead of {\it the\_main\_loop}. \\
214     \hspace*{4ex} {\tt \#define ALLOW\_GRADIENT\_CHECK} \\
215     This flag enables the gradient check package.
216     After computing the unperturbed cost function and its gradient,
217     a series of computations are performed for which \\
218     $\bullet$ an element of the control vector is perturbed \\
219     $\bullet$ the cost function w.r.t. the perturbed element is
220     computed \\
221     $\bullet$ the difference between the perturbed and unperturbed
222     cost function is computed to compute the finite difference gradient \\
223     $\bullet$ the finite difference gradient is compared with the
224     adjoint-generated gradient.
225     The gradient check package is further described in Section ???.
226    
227     \subsubsection{File {\it ECCO\_OPTIONS.h}}
228    
229     The CPP options of several AD-related packages are grouped
230     in this file:
231     %
232     \begin{itemize}
233     %
234     \item
235     Adjoint support package: {\it pkg/autodiff/} \\
236     This package contains hand-written adjoint code such as
237     active file handling, flow directives for files which must not
238     be differentiated, and TAMC-specific header files. \\
239     \hspace*{4ex} {\tt \#define ALLOW\_AUTODIFF\_TAMC} \\
240     defines TAMC-related features in the code. \\
241     \hspace*{4ex} {\tt \#define ALLOW\_TAMC\_CHECKPOINTING} \\
242     enables the checkpointing feature of TAMC
243     (see Section \ref{???}).
244     In the present example a 3-level checkpointing is implemented.
245     The code contains the relevant store directives, common block
246     and tape initializations, storing key computation,
247     and loop index handling.
248     The checkpointing length at each level is defined in
249     file {\it tamc.h}, cf. below.
250     %
251     \item Cost function package: {\it pkg/cost/} \\
252     This package contains all relevant routines for
253     initializing, accumulating and finalizing the cost function
254     (see Section \ref{???}). \\
255     \hspace*{4ex} {\tt \#define ALLOW\_COST} \\
256     enables all general aspects of the cost function handling,
257     in particular the hooks in the forward code for
258     initializing, accumulating and finalizing the cost function. \\
259     \hspace*{4ex} {\tt \#define ALLOW\_COST\_TRACER} \\
260     includes the call to the cost function for this
261     particular experiment, eqn. (\ref{cost_tracer}).
262     %
263     \item Control variable package: {\it pkg/ctrl/} \\
264     This package contains all relevant routines for
265     the handling of the control vector.
266     Each control variable can be enabled/disabled with its own flag: \\
267     \begin{tabular}{ll}
268     \hspace*{2ex} {\tt \#define ALLOW\_THETA0\_CONTROL} &
269     initial temperature \\
270     \hspace*{2ex} {\tt \#define ALLOW\_SALT0\_CONTROL} &
271     initial salinity \\
272     \hspace*{2ex} {\tt \#define ALLOW\_TR0\_CONTROL} &
273     initial passive tracer concentration \\
274     \hspace*{2ex} {\tt \#define ALLOW\_TAUU0\_CONTROL} &
275     zonal wind stress \\
276     \hspace*{2ex} {\tt \#define ALLOW\_TAUV0\_CONTROL} &
277     meridional wind stress \\
278     \hspace*{2ex} {\tt \#define ALLOW\_SFLUX0\_CONTROL} &
279     freshwater flux \\
280     \hspace*{2ex} {\tt \#define ALLOW\_HFLUX0\_CONTROL} &
281     heat flux \\
282     \hspace*{2ex} {\tt \#define ALLOW\_DIFFKR\_CONTROL} &
283     diapycnal diffusivity \\
284     \hspace*{2ex} {\tt \#undef ALLOW\_KAPPAGM\_CONTROL} &
285     isopycnal diffusivity \\
286     \end{tabular}
287     %
288     \end{itemize}
289    
290     \subsubsection{File {\it SIZE.h}}
291    
292     The file contains the grid point dimensions of the forward
293     model. It is identical to the {\it verification/exp2/}: \\
294     \hspace*{4ex} {\tt sNx = 90} \\
295     \hspace*{4ex} {\tt sNy = 40} \\
296     \hspace*{4ex} {\tt Nr = 20} \\
297     It corresponds to a single-tile/single-processor setup:
298     {\tt nSx = nSy = 1, nPx = nPy = 1},
299     with standard overlap dimensioning
300     {\tt OLx = OLy = 3}.
301    
302     \subsubsection{File {\it adcommon.h}}
303    
304     This file contains common blocks of some adjoint variables
305     that are generated by TAMC.
306     The common blocks are used by the adjoint support routine
307     {\it addummy\_in\_stepping} which needs to access those variables:
308    
309     \begin{tabular}{ll}
310     \hspace*{4ex} {\tt common /addynvars\_r/} &
311     \hspace*{4ex} is related to {\it DYNVARS.h} \\
312     \hspace*{4ex} {\tt common /addynvars\_cd/} &
313     \hspace*{4ex} is related to {\it DYNVARS.h} \\
314     \hspace*{4ex} {\tt common /addynvars\_diffkr/} &
315     \hspace*{4ex} is related to {\it DYNVARS.h} \\
316     \hspace*{4ex} {\tt common /addynvars\_kapgm/} &
317     \hspace*{4ex} is related to {\it DYNVARS.h} \\
318     \hspace*{4ex} {\tt common /adtr1\_r/} &
319     \hspace*{4ex} is related to {\it TR1.h} \\
320     \hspace*{4ex} {\tt common /adffields/} &
321     \hspace*{4ex} is related to {\it FFIELDS.h}\\
322     \end{tabular}
323    
324     Note that if the structure of the common block changes in the
325     above header files of the forward code, the structure
326     of the adjoint common blocks will change accordingly.
327     Thus, it has to be made sure that the structure of the
328     adjoint common block in the hand-written file {\it adcommon.h}
329     complies with the automatically generated adjoint common blocks
330     in {\it adjoint\_model.F}.
331    
332     \subsubsection{File {\it tamc.h}}
333    
334     This routine contains the dimensions for TAMC checkpointing.
335     %
336     \begin{itemize}
337     %
338     \item {\tt \#ifdef ALLOW\_TAMC\_CHECKPOINTING} \\
339     3-level checkpointing is enabled, i.e. the timestepping
340     is divided into three different levels (see Section \ref{???}).
341     The model state of the outermost ({\tt nchklev\_3}) and the
342     intermediate ({\tt nchklev\_2}) timestepping loop are stored to file
343     (handled in {\it the\_main\_loop}).
344     The innermost loop ({\tt nchklev\_1})
345     avoids I/O by storing all required variables
346     to common blocks. This storing may also be necessary if
347     no checkpointing is chosen
348     (nonlinear functions, if-statements, iterative loops, ...).
349     In the present example the dimensions are chosen as follows: \\
350     \hspace*{4ex} {\tt nchklev\_1 = 36 } \\
351     \hspace*{4ex} {\tt nchklev\_2 = 30 } \\
352     \hspace*{4ex} {\tt nchklev\_3 = 60 } \\
353     To guarantee that the checkpointing intervals span the entire
354     integration period the following relation must be satisfied: \\
355     \hspace*{4ex} {\tt nchklev\_1*nchklev\_2*nchklev\_3 $ \ge $ nTimeSteps} \\
356     where {\tt nTimeSteps} is either specified in {\it data}
357     or computed via \\
358     \hspace*{4ex} {\tt nTimeSteps = (endTime-startTime)/deltaTClock }.
359     %
360     \item {\tt \#undef ALLOW\_TAMC\_CHECKPOINTING} \\
361     No checkpointing is enabled.
362     In this case the relevant counter is {\tt nchklev\_0}.
363     Similar to above, the following relation has to be satisfied \\
364     \hspace*{4ex} {\tt nchklev\_0 $ \ge $ nTimeSteps}.
365     %
366     \end{itemize}
367    
368     The following parameters may be worth describing: \\
369     %
370     \hspace*{4ex} {\tt isbyte} \\
371     \hspace*{4ex} {\tt maxpass} \\
372     ~
373    
374     \subsubsection{File {\it makefile}}
375    
376     This file contains all relevant parameter flags and
377     lists to run TAMC or TAF.
378     It is assumed that TAMC is available to you, either locally,
379     being installed on your network, or remotely through the 'TAMC Utility'.
380     TAMC is called with the command {\tt tamc} followed by a
381     number of options. They are described in detail in the
382     TAMC manual \cite{gie:99}.
383     Here we briefly discuss the main flags used in the {\it makefile}
384     %
385     \begin{itemize}
386     \item [{\tt tamc}] {\tt
387     -input <variable names>
388     -output <variable name> -r4 ... \\
389     -toplevel <S/R name> -reverse <file names>
390     }
391     \end{itemize}
392     %
393     \begin{itemize}
394     %
395     \item {\tt -toplevel <S/R name>} \\
396     Name of the toplevel routine, with respect to which the
397     control flow analysis is performed.
398     %
399     \item {\tt -input <variable names>} \\
400     List of independent variables $ u $ with respect to which the
401     dependent variable $ J $ is differentiated.
402     %
403     \item {\tt -output <variable name>} \\
404     Dependent variable $ J $ which is to be differentiated.
405     %
406     \item {\tt -reverse <file names>} \\
407     Adjoint code is generated to compute the sensitivity of an
408     independent variable w.r.t. many dependent variables.
409     In the discussion of Section ???
410     the generated adjoint top-level routine computes the product
411     of the transposed Jacobian matrix $ M^T $ times
412     the gradient vector $ \nabla_v J $.
413     \\
414     {\tt <file names>} refers to the list of files {\it .f} which are to be
415     analyzed by TAMC. This list is generally smaller than the full list
416     of code to be compiled. The files not contained are either
417     above the top-level routine (some initializations), or are
418     deliberately hidden from TAMC, either because hand-written
419     adjoint routines exist, or the routines must not (or don't have to)
420     be differentiated. For each routine which is part of the flow tree
421     of the top-level routine, but deliberately hidden from TAMC
422     (or for each package which contains such routines),
423     a corresponding file {\it .flow} exists containing flow directives
424     for TAMC.
425     %
426     \item {\tt -r4} \\
427     ~
428     %
429     \end{itemize}
430    
431    
432     \subsubsection{The input parameter files}
433    
434     \paragraph{File {\it data}}
435    
436     \paragraph{File {\it data.cost}}
437    
438     \paragraph{File {\it data.ctrl}}
439    
440     \paragraph{File {\it data.gmredi}}
441    
442     \paragraph{File {\it data.grdchk}}
443    
444     \paragraph{File {\it data.optim}}
445    
446     \paragraph{File {\it data.pkg}}
447    
448     \paragraph{File {\it eedata}}
449    
450     \paragraph{File {\it topog.bin}}
451    
452     \paragraph{File {\it windx.bin, windy.bin}}
453    
454     \paragraph{File {\it salt.bin, theta.bin}}
455    
456     \paragraph{File {\it SSS.bin, SST.bin}}
457    
458     \paragraph{File {\it pickup*}}
459    
460     \subsection{Compiling the model and its adjoint}
461    
462     The built process of the adjoint model is slightly more
463     complex than that of compiling the forward code.
464     The main reason is that the adjoint code generation requires
465     a specific list of routines that are to be differentiated
466     (as opposed to the automatic generation of a list of
467     files to be compiled by genmake).
468     This list excludes routines that don't have to be or must not be
469     differentiated. For some of the latter routines flow directives
470     may be necessary, a list of which has to be given as well.
471     For this reason, a separate {\it makefile} is currently
472     maintained in the directory {\tt adjoint/}. This
473     makefile is responsible for the adjoint code generation.
474    
475     In the following we describe the build process step by step,
476     assuming you are in the directory {\tt bin/}.
477     A summary of steps to follow is given at the end.
478    
479     \paragraph{Adjoint code generation and compilation -- step by step}
480    
481     \begin{enumerate}
482     %
483     \item
484     {\tt ln -s ../verification/???/code/.genmakerc .} \\
485     {\tt ln -s ../verification/???/code/*.[Fh] .} \\
486     Link your customized genmake options, header files,
487     and modified code to the compile directory.
488     %
489     \item
490     {\tt ../tools/genmake -makefile} \\
491     Generate your Makefile (cf. Section ???).
492     %
493     \item
494     {\tt make depend} \\
495     Dependency analysis for the CPP pre-compiler (cf. Section ???).
496     %
497     \item
498     {\tt make small\_f} \\
499     This is the first difference between forward code compilation
500     and adjoint code generation and compilation.
501     Instead of going through the entire compilation process
502     (CPP precompiling -- {\tt .f}, object code generation -- {\tt .o},
503     linking of object files and libraries to generate executable),
504     only the CPP compiler is invoked at this stage to generate
505     the {\tt .f} files.
506     %
507     \item
508     {\tt cd ../adjoint} \\
509     {\tt make adtaf} or {\tt make adtamc} \\
510     Depending on whether you have TAF or TAMC at your disposal,
511     you'll choose {\tt adtaf} or {\tt adtamc} as your
512     make target for the {\it makefile} in the directory {\tt adjoint/}.
513     Several things happen at this stage.
514     %
515     \begin{enumerate}
516     %
517     \item
518     The initial template file {\it adjoint\_model.F} which is part
519     of the compiling list created by {\it genmake} is restored.
520     %
521     \item
522     All Fortran routines {\tt *.f} in {\tt bin/} are
523     concatenated into a single file (it's current name is
524     {\it tamc\_code.f}).
525     %
526     \item
527     Adjoint code is generated by TAMC or TAF.
528     The adjoint code is written to the file {\it tamc\_code\_ad.f}.
529     It contains all adjoint routines of the forward routines
530     concatenated in {\it tamc\_code.f}.
531     For a given forward routines {\tt subroutine routinename}
532     the adjoint routine is named {\tt adsubroutine routinename}
533     by default (that default can be changed via the flag
534     {\tt -admark <markname>}).
535     Furthermore, it may contain modified code which
536     incorporates the translation of adjoint store directives
537     into specific Fortran code.
538     For a given forward routines {\tt subroutine routinename}
539     the modified routine is named {\tt mdsubroutine routinename}.
540     TAMC or TAF info is written to file
541     {\it tamc\_code.prot} or {\it taf.log}, respectively.
542     %
543     \end{enumerate}
544     %
545     \item
546     {\tt make adchange} \\
547     The multi-threading capability of the MITGCM requires a slight
548     change in the parameter list of some routines that are related to
549     to active file handling.
550     This post-processing invokes the sed script {\it adjoint\_ecco\_sed.com}
551     to insert the threading counter {\bf myThId} into the parameter list
552     of those subroutines.
553     The resulting code is written to file {\it tamc\_code\_sed\_ad.f}
554     and appended to the file {\it adjoint\_model.F}.
555     This concludes the adjoint code generation.
556     %
557     \item
558     {\tt cd ../bin} \\
559     {\tt make} \\
560     The file {\it adjoint\_model.F} now contains the full adjoint code.
561     All routines are now compiled.
562     %
563     \end{enumerate}
564    
565     \paragraph{Adjoint code generation and compilation -- summary}
566     ~ \\
567    
568     \[
569     \boxed{
570     \begin{split}
571     ~ & \mbox{\tt cd bin} \\
572     ~ & \mbox{\tt ln -s ../verification/my\_experiment/code/.genmakerc .} \\
573     ~ & \mbox{\tt ln -s ../verification/my\_experiment/code/*.[Fh] .} \\
574     ~ & \mbox{\tt ../tools/genmake -makefile} \\
575     ~ & \mbox{\tt make depend} \\
576     ~ & \mbox{\tt make small\_f} \\
577     ~ & \mbox{\tt cd ../adjoint} \\
578     ~ & \mbox{\tt make adtaf <OR: make adtamc>} \\
579     ~ & \mbox{\tt make adchange} \\
580     ~ & \mbox{\tt cd ../bin} \\
581     ~ & \mbox{\tt make} \\
582     \end{split}
583     }
584     \]
585    

  ViewVC Help
Powered by ViewVC 1.1.22