/[MITgcm]/manual/s_autodiff/text/doc_ad_2.tex
ViewVC logotype

Annotation of /manual/s_autodiff/text/doc_ad_2.tex

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


Revision 1.9 - (hide annotations) (download) (as text)
Tue Nov 13 21:06:21 2001 UTC (23 years, 8 months ago) by heimbach
Branch: MAIN
Changes since 1.8: +2 -2 lines
File MIME type: application/x-tex
Fixed gil-lem:89 reference.

1 heimbach 1.9 % $Header: /u/gcmpack/mitgcmdoc/part5/doc_ad_2.tex,v 1.8 2001/11/06 15:07:32 adcroft Exp $
2 heimbach 1.2 % $Name: $
3 adcroft 1.1
4     {\sf Automatic differentiation} (AD), also referred to as algorithmic
5     (or, more loosely, computational) differentiation, involves
6     automatically deriving code to calculate
7     partial derivatives from an existing fully non-linear prognostic code.
8     (see \cite{gri:00}).
9     A software tool is used that parses and transforms source files
10     according to a set of linguistic and mathematical rules.
11     AD tools are like source-to-source translators in that
12     they parse a program code as input and produce a new program code
13     as output.
14     However, unlike a pure source-to-source translation, the output program
15     represents a new algorithm, such as the evaluation of the
16     Jacobian, the Hessian, or higher derivative operators.
17     In principle, a variety of derived algorithms
18     can be generated automatically in this way.
19    
20     The MITGCM has been adapted for use with the
21 heimbach 1.4 Tangent linear and Adjoint Model Compiler (TAMC) and its successor TAF
22 adcroft 1.1 (Transformation of Algorithms in Fortran), developed
23     by Ralf Giering (\cite{gie-kam:98}, \cite{gie:99,gie:00}).
24 cnh 1.7 The first application of the adjoint of the MITGCM for sensitivity
25 adcroft 1.1 studies has been published by \cite{maro-eta:99}.
26     \cite{sta-eta:97,sta-eta:01} use the MITGCM and its adjoint
27     for ocean state estimation studies.
28 heimbach 1.4 In the following we shall refer to TAMC and TAF synonymously,
29     except were explicitly stated otherwise.
30 adcroft 1.1
31     TAMC exploits the chain rule for computing the first
32     derivative of a function with
33     respect to a set of input variables.
34     Treating a given forward code as a composition of operations --
35 heimbach 1.4 each line representing a compositional element, the chain rule is
36 adcroft 1.1 rigorously applied to the code, line by line. The resulting
37     tangent linear or adjoint code,
38     then, may be thought of as the composition in
39     forward or reverse order, respectively, of the
40 heimbach 1.4 Jacobian matrices of the forward code's compositional elements.
41 adcroft 1.1
42     %**********************************************************************
43     \section{Some basic algebra}
44     \label{sec_ad_algebra}
45     %**********************************************************************
46    
47     Let $ \cal{M} $ be a general nonlinear, model, i.e. a
48     mapping from the $m$-dimensional space
49     $U \subset I\!\!R^m$ of input variables
50     $\vec{u}=(u_1,\ldots,u_m)$
51     (model parameters, initial conditions, boundary conditions
52     such as forcing functions) to the $n$-dimensional space
53     $V \subset I\!\!R^n$ of
54     model output variable $\vec{v}=(v_1,\ldots,v_n)$
55 cnh 1.7 (model state, model diagnostics, objective function, ...)
56 adcroft 1.1 under consideration,
57     %
58     \begin{equation}
59     \begin{split}
60     {\cal M} \, : & \, U \,\, \longrightarrow \, V \\
61     ~ & \, \vec{u} \,\, \longmapsto \, \vec{v} \, = \,
62     {\cal M}(\vec{u})
63     \label{fulloperator}
64     \end{split}
65     \end{equation}
66     %
67     The vectors $ \vec{u} \in U $ and $ v \in V $ may be represented w.r.t.
68     some given basis vectors
69     $ {\rm span} (U) = \{ {\vec{e}_i} \}_{i = 1, \ldots , m} $ and
70     $ {\rm span} (V) = \{ {\vec{f}_j} \}_{j = 1, \ldots , n} $ as
71     \[
72     \vec{u} \, = \, \sum_{i=1}^{m} u_i \, {\vec{e}_i},
73     \qquad
74     \vec{v} \, = \, \sum_{j=1}^{n} v_j \, {\vec{f}_j}
75     \]
76    
77     Two routes may be followed to determine the sensitivity of the
78     output variable $\vec{v}$ to its input $\vec{u}$.
79    
80     \subsection{Forward or direct sensitivity}
81     %
82     Consider a perturbation to the input variables $\delta \vec{u}$
83     (typically a single component
84     $\delta \vec{u} = \delta u_{i} \, {\vec{e}_{i}}$).
85     Their effect on the output may be obtained via the linear
86     approximation of the model $ {\cal M}$ in terms of its Jacobian matrix
87     $ M $, evaluated in the point $u^{(0)}$ according to
88     %
89     \begin{equation}
90     \delta \vec{v} \, = \, M |_{\vec{u}^{(0)}} \, \delta \vec{u}
91     \label{tangent_linear}
92     \end{equation}
93     with resulting output perturbation $\delta \vec{v}$.
94     In components
95     $M_{j i} \, = \, \partial {\cal M}_{j} / \partial u_{i} $,
96     it reads
97     %
98     \begin{equation}
99     \delta v_{j} \, = \, \sum_{i}
100     \left. \frac{\partial {\cal M}_{j}}{\partial u_{i}} \right|_{u^{(0)}} \,
101     \delta u_{i}
102     \label{jacobi_matrix}
103     \end{equation}
104     %
105     Eq. (\ref{tangent_linear}) is the {\sf tangent linear model (TLM)}.
106     In contrast to the full nonlinear model $ {\cal M} $, the operator
107     $ M $ is just a matrix
108     which can readily be used to find the forward sensitivity of $\vec{v}$ to
109     perturbations in $u$,
110 heimbach 1.4 but if there are very many input variables $(\gg O(10^{6})$ for
111 adcroft 1.1 large-scale oceanographic application), it quickly becomes
112     prohibitive to proceed directly as in (\ref{tangent_linear}),
113     if the impact of each component $ {\bf e_{i}} $ is to be assessed.
114    
115     \subsection{Reverse or adjoint sensitivity}
116     %
117     Let us consider the special case of a
118     scalar objective function ${\cal J}(\vec{v})$ of the model output (e.g.
119     the total meridional heat transport,
120     the total uptake of $CO_{2}$ in the Southern
121     Ocean over a time interval,
122     or a measure of some model-to-data misfit)
123     %
124     \begin{eqnarray}
125     \begin{array}{cccccc}
126     {\cal J} \, : & U &
127     \longrightarrow & V &
128     \longrightarrow & I \!\! R \\
129     ~ & \vec{u} & \longmapsto & \vec{v}={\cal M}(\vec{u}) &
130     \longmapsto & {\cal J}(\vec{u}) = {\cal J}({\cal M}(\vec{u}))
131     \end{array}
132     \label{compo}
133     \end{eqnarray}
134     %
135 heimbach 1.4 The perturbation of $ {\cal J} $ around a fixed point $ {\cal J}_0 $,
136 adcroft 1.1 \[
137 heimbach 1.4 {\cal J} \, = \, {\cal J}_0 \, + \, \delta {\cal J}
138 adcroft 1.1 \]
139     can be expressed in both bases of $ \vec{u} $ and $ \vec{v} $
140     w.r.t. their corresponding inner product
141     $\left\langle \,\, , \,\, \right\rangle $
142     %
143     \begin{equation}
144     \begin{split}
145     {\cal J} & = \,
146     {\cal J} |_{\vec{u}^{(0)}} \, + \,
147     \left\langle \, \nabla _{u}{\cal J}^T |_{\vec{u}^{(0)}} \, , \, \delta \vec{u} \, \right\rangle
148     \, + \, O(\delta \vec{u}^2) \\
149     ~ & = \,
150     {\cal J} |_{\vec{v}^{(0)}} \, + \,
151     \left\langle \, \nabla _{v}{\cal J}^T |_{\vec{v}^{(0)}} \, , \, \delta \vec{v} \, \right\rangle
152     \, + \, O(\delta \vec{v}^2)
153     \end{split}
154     \label{deljidentity}
155     \end{equation}
156     %
157 heimbach 1.2 (note, that the gradient $ \nabla f $ is a co-vector, therefore
158 adcroft 1.1 its transpose is required in the above inner product).
159     Then, using the representation of
160     $ \delta {\cal J} =
161     \left\langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \right\rangle $,
162     the definition
163     of an adjoint operator $ A^{\ast} $ of a given operator $ A $,
164     \[
165     \left\langle \, A^{\ast} \vec{x} \, , \, \vec{y} \, \right\rangle =
166     \left\langle \, \vec{x} \, , \, A \vec{y} \, \right\rangle
167     \]
168     which for finite-dimensional vector spaces is just the
169     transpose of $ A $,
170     \[
171     A^{\ast} \, = \, A^T
172     \]
173 heimbach 1.4 and from eq. (\ref{tangent_linear}), (\ref{deljidentity}),
174     we note that
175 adcroft 1.1 (omitting $|$'s):
176     %
177     \begin{equation}
178     \delta {\cal J}
179     \, = \,
180     \left\langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \right\rangle
181     \, = \,
182     \left\langle \, \nabla _{v}{\cal J}^T \, , \, M \, \delta \vec{u} \, \right\rangle
183     \, = \,
184     \left\langle \, M^T \, \nabla _{v}{\cal J}^T \, , \,
185     \delta \vec{u} \, \right\rangle
186     \label{inner}
187     \end{equation}
188     %
189     With the identity (\ref{deljidentity}), we then find that
190     the gradient $ \nabla _{u}{\cal J} $ can be readily inferred by
191     invoking the adjoint $ M^{\ast } $ of the tangent linear model $ M $
192     %
193     \begin{equation}
194     \begin{split}
195     \nabla _{u}{\cal J}^T |_{\vec{u}} &
196     = \, M^T |_{\vec{u}} \cdot \nabla _{v}{\cal J}^T |_{\vec{v}} \\
197     ~ & = \, M^T |_{\vec{u}} \cdot \delta \vec{v}^{\ast} \\
198     ~ & = \, \delta \vec{u}^{\ast}
199     \end{split}
200     \label{adjoint}
201     \end{equation}
202     %
203     Eq. (\ref{adjoint}) is the {\sf adjoint model (ADM)},
204     in which $M^T$ is the adjoint (here, the transpose) of the
205     tangent linear operator $M$, $ \delta \vec{v}^{\ast} $
206     the adjoint variable of the model state $ \vec{v} $, and
207     $ \delta \vec{u}^{\ast} $ the adjoint variable of the control variable $ \vec{u} $.
208    
209     The {\sf reverse} nature of the adjoint calculation can be readily
210 heimbach 1.4 seen as follows.
211     Consider a model integration which consists of $ \Lambda $
212     consecutive operations
213     $ {\cal M}_{\Lambda} ( {\cal M}_{\Lambda-1} (
214     ...... ( {\cal M}_{\lambda} (
215     ......
216     ( {\cal M}_{1} ( {\cal M}_{0}(\vec{u}) )))) $,
217     where the ${\cal M}$'s could be the elementary steps, i.e. single lines
218     in the code of the model, or successive time steps of the
219     model integration,
220     starting at step 0 and moving up to step $\Lambda$, with intermediate
221     ${\cal M}_{\lambda} (\vec{u}) = \vec{v}^{(\lambda+1)}$ and final
222     ${\cal M}_{\Lambda} (\vec{u}) = \vec{v}^{(\Lambda+1)} = \vec{v}$.
223 cnh 1.7 Let ${\cal J}$ be a cost function which explicitly depends on the
224 heimbach 1.4 final state $\vec{v}$ only
225     (this restriction is for clarity reasons only).
226     %
227     ${\cal J}(u)$ may be decomposed according to:
228 adcroft 1.1 %
229     \begin{equation}
230     {\cal J}({\cal M}(\vec{u})) \, = \,
231     {\cal J} ( {\cal M}_{\Lambda} ( {\cal M}_{\Lambda-1} (
232     ...... ( {\cal M}_{\lambda} (
233     ......
234     ( {\cal M}_{1} ( {\cal M}_{0}(\vec{u}) )))))
235     \label{compos}
236     \end{equation}
237     %
238 heimbach 1.4 Then, according to the chain rule, the forward calculation reads,
239     in terms of the Jacobi matrices
240 adcroft 1.1 (we've omitted the $ | $'s which, nevertheless are important
241     to the aspect of {\it tangent} linearity;
242 heimbach 1.4 note also that by definition
243 adcroft 1.1 $ \langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \rangle
244     = \nabla_v {\cal J} \cdot \delta \vec{v} $ )
245     %
246     \begin{equation}
247     \begin{split}
248     \nabla_v {\cal J} (M(\delta \vec{u})) & = \,
249     \nabla_v {\cal J} \cdot M_{\Lambda}
250     \cdot ...... \cdot M_{\lambda} \cdot ...... \cdot
251     M_{1} \cdot M_{0} \cdot \delta \vec{u} \\
252     ~ & = \, \nabla_v {\cal J} \cdot \delta \vec{v} \\
253     \end{split}
254     \label{forward}
255     \end{equation}
256     %
257     whereas in reverse mode we have
258     %
259     \begin{equation}
260     \boxed{
261     \begin{split}
262     M^T ( \nabla_v {\cal J}^T) & = \,
263     M_{0}^T \cdot M_{1}^T
264     \cdot ...... \cdot M_{\lambda}^T \cdot ...... \cdot
265     M_{\Lambda}^T \cdot \nabla_v {\cal J}^T \\
266     ~ & = \, M_{0}^T \cdot M_{1}^T
267     \cdot ...... \cdot
268     \nabla_{v^{(\lambda)}} {\cal J}^T \\
269     ~ & = \, \nabla_u {\cal J}^T
270     \end{split}
271     }
272     \label{reverse}
273     \end{equation}
274     %
275     clearly expressing the reverse nature of the calculation.
276     Eq. (\ref{reverse}) is at the heart of automatic adjoint compilers.
277 heimbach 1.4 If the intermediate steps $\lambda$ in
278 adcroft 1.1 eqn. (\ref{compos}) -- (\ref{reverse})
279 heimbach 1.4 represent the model state (forward or adjoint) at each
280     intermediate time step as noted above, then correspondingly,
281     $ M^T (\delta \vec{v}^{(\lambda) \, \ast}) =
282     \delta \vec{v}^{(\lambda-1) \, \ast} $ for the adjoint variables.
283     It thus becomes evident that the adjoint calculation also
284     yields the adjoint of each model state component
285     $ \vec{v}^{(\lambda)} $ at each intermediate step $ \lambda $, namely
286 adcroft 1.1 %
287     \begin{equation}
288     \boxed{
289     \begin{split}
290     \nabla_{v^{(\lambda)}} {\cal J}^T |_{\vec{v}^{(\lambda)}}
291     & = \,
292     M_{\lambda}^T |_{\vec{v}^{(\lambda)}} \cdot ...... \cdot
293     M_{\Lambda}^T |_{\vec{v}^{(\lambda)}} \cdot \delta \vec{v}^{\ast} \\
294     ~ & = \, \delta \vec{v}^{(\lambda) \, \ast}
295     \end{split}
296     }
297     \end{equation}
298     %
299     in close analogy to eq. (\ref{adjoint})
300     We note in passing that that the $\delta \vec{v}^{(\lambda) \, \ast}$
301 heimbach 1.4 are the Lagrange multipliers of the model equations which determine
302     $ \vec{v}^{(\lambda)}$.
303 adcroft 1.1
304 cnh 1.7 In components, eq. (\ref{adjoint}) reads as follows.
305 adcroft 1.1 Let
306     \[
307     \begin{array}{rclcrcl}
308     \delta \vec{u} & = &
309     \left( \delta u_1,\ldots, \delta u_m \right)^T , & \qquad &
310     \delta \vec{u}^{\ast} \,\, = \,\, \nabla_u {\cal J}^T & = &
311     \left(
312     \frac{\partial {\cal J}}{\partial u_1},\ldots,
313     \frac{\partial {\cal J}}{\partial u_m}
314     \right)^T \\
315     \delta \vec{v} & = &
316     \left( \delta v_1,\ldots, \delta u_n \right)^T , & \qquad &
317     \delta \vec{v}^{\ast} \,\, = \,\, \nabla_v {\cal J}^T & = &
318     \left(
319     \frac{\partial {\cal J}}{\partial v_1},\ldots,
320     \frac{\partial {\cal J}}{\partial v_n}
321     \right)^T \\
322     \end{array}
323     \]
324     denote the perturbations in $\vec{u}$ and $\vec{v}$, respectively,
325 cnh 1.7 and their adjoint variables;
326 adcroft 1.1 further
327     \[
328     M \, = \, \left(
329     \begin{array}{ccc}
330     \frac{\partial {\cal M}_1}{\partial u_1} & \ldots &
331     \frac{\partial {\cal M}_1}{\partial u_m} \\
332     \vdots & ~ & \vdots \\
333     \frac{\partial {\cal M}_n}{\partial u_1} & \ldots &
334     \frac{\partial {\cal M}_n}{\partial u_m} \\
335     \end{array}
336     \right)
337     \]
338     is the Jacobi matrix of $ {\cal M} $
339     (an $ n \times m $ matrix)
340     such that $ \delta \vec{v} = M \cdot \delta \vec{u} $, or
341     \[
342     \delta v_{j}
343     \, = \, \sum_{i=1}^m M_{ji} \, \delta u_{i}
344     \, = \, \sum_{i=1}^m \, \frac{\partial {\cal M}_{j}}{\partial u_{i}}
345     \delta u_{i}
346     \]
347     %
348     Then eq. (\ref{adjoint}) takes the form
349     \[
350     \delta u_{i}^{\ast}
351     \, = \, \sum_{j=1}^n M_{ji} \, \delta v_{j}^{\ast}
352     \, = \, \sum_{j=1}^n \, \frac{\partial {\cal M}_{j}}{\partial u_{i}}
353     \delta v_{j}^{\ast}
354     \]
355     %
356     or
357     %
358     \[
359     \left(
360     \begin{array}{c}
361     \left. \frac{\partial}{\partial u_1} {\cal J} \right|_{\vec{u}^{(0)}} \\
362     \vdots \\
363     \left. \frac{\partial}{\partial u_m} {\cal J} \right|_{\vec{u}^{(0)}} \\
364     \end{array}
365     \right)
366     \, = \,
367     \left(
368     \begin{array}{ccc}
369     \left. \frac{\partial {\cal M}_1}{\partial u_1} \right|_{\vec{u}^{(0)}}
370     & \ldots &
371     \left. \frac{\partial {\cal M}_n}{\partial u_1} \right|_{\vec{u}^{(0)}} \\
372     \vdots & ~ & \vdots \\
373     \left. \frac{\partial {\cal M}_1}{\partial u_m} \right|_{\vec{u}^{(0)}}
374     & \ldots &
375     \left. \frac{\partial {\cal M}_n}{\partial u_m} \right|_{\vec{u}^{(0)}} \\
376     \end{array}
377     \right)
378     \cdot
379     \left(
380     \begin{array}{c}
381     \left. \frac{\partial}{\partial v_1} {\cal J} \right|_{\vec{v}} \\
382     \vdots \\
383     \left. \frac{\partial}{\partial v_n} {\cal J} \right|_{\vec{v}} \\
384     \end{array}
385     \right)
386     \]
387     %
388     Furthermore, the adjoint $ \delta v^{(\lambda) \, \ast} $
389     of any intermediate state $ v^{(\lambda)} $
390     may be obtained, using the intermediate Jacobian
391     (an $ n_{\lambda+1} \times n_{\lambda} $ matrix)
392     %
393     \[
394     M_{\lambda} \, = \,
395     \left(
396     \begin{array}{ccc}
397     \frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_1}
398     & \ldots &
399     \frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_{n_{\lambda}}} \\
400     \vdots & ~ & \vdots \\
401     \frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_1}
402     & \ldots &
403     \frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_{n_{\lambda}}} \\
404     \end{array}
405     \right)
406     \]
407     %
408     and the shorthand notation for the adjoint variables
409     $ \delta v^{(\lambda) \, \ast}_{j} = \frac{\partial}{\partial v^{(\lambda)}_{j}}
410     {\cal J}^T $, $ j = 1, \ldots , n_{\lambda} $,
411     for intermediate components, yielding
412 heimbach 1.4 \begin{equation}
413     \small
414     \begin{split}
415 adcroft 1.1 \left(
416     \begin{array}{c}
417     \delta v^{(\lambda) \, \ast}_1 \\
418     \vdots \\
419     \delta v^{(\lambda) \, \ast}_{n_{\lambda}} \\
420     \end{array}
421     \right)
422 heimbach 1.4 \, = &
423 adcroft 1.1 \left(
424     \begin{array}{ccc}
425     \frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_1}
426 heimbach 1.4 & \ldots \,\, \ldots &
427 adcroft 1.1 \frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_1} \\
428     \vdots & ~ & \vdots \\
429     \frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_{n_{\lambda}}}
430 heimbach 1.4 & \ldots \,\, \ldots &
431 adcroft 1.1 \frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_{n_{\lambda}}} \\
432     \end{array}
433     \right)
434 heimbach 1.4 \cdot
435 adcroft 1.1 %
436 heimbach 1.4 \\ ~ & ~
437     \\ ~ &
438 adcroft 1.1 %
439     \left(
440     \begin{array}{ccc}
441     \frac{\partial ({\cal M}_{\lambda+1})_1}{\partial v^{(\lambda+1)}_1}
442     & \ldots &
443     \frac{\partial ({\cal M}_{\lambda+1})_{n_{\lambda+2}}}{\partial v^{(\lambda+1)}_1} \\
444     \vdots & ~ & \vdots \\
445     \vdots & ~ & \vdots \\
446     \frac{\partial ({\cal M}_{\lambda+1})_1}{\partial v^{(\lambda+1)}_{n_{\lambda+1}}}
447     & \ldots &
448     \frac{\partial ({\cal M}_{\lambda+1})_{n_{\lambda+2}}}{\partial v^{(\lambda+1)}_{n_{\lambda+1}}} \\
449     \end{array}
450     \right)
451 heimbach 1.4 \cdot \, \ldots \, \cdot
452 adcroft 1.1 \left(
453     \begin{array}{c}
454     \delta v^{\ast}_1 \\
455     \vdots \\
456     \delta v^{\ast}_{n} \\
457     \end{array}
458     \right)
459 heimbach 1.4 \end{split}
460     \end{equation}
461 adcroft 1.1
462     Eq. (\ref{forward}) and (\ref{reverse}) are perhaps clearest in
463     showing the advantage of the reverse over the forward mode
464     if the gradient $\nabla _{u}{\cal J}$, i.e. the sensitivity of the
465     cost function $ {\cal J} $ with respect to {\it all} input
466     variables $u$
467     (or the sensitivity of the cost function with respect to
468     {\it all} intermediate states $ \vec{v}^{(\lambda)} $) are sought.
469     In order to be able to solve for each component of the gradient
470     $ \partial {\cal J} / \partial u_{i} $ in (\ref{forward})
471 cnh 1.7 a forward calculation has to be performed for each component separately,
472 adcroft 1.1 i.e. $ \delta \vec{u} = \delta u_{i} {\vec{e}_{i}} $
473     for the $i$-th forward calculation.
474     Then, (\ref{forward}) represents the
475     projection of $ \nabla_u {\cal J} $ onto the $i$-th component.
476     The full gradient is retrieved from the $ m $ forward calculations.
477     In contrast, eq. (\ref{reverse}) yields the full
478     gradient $\nabla _{u}{\cal J}$ (and all intermediate gradients
479     $\nabla _{v^{(\lambda)}}{\cal J}$) within a single reverse calculation.
480    
481 heimbach 1.4 Note, that if $ {\cal J} $ is a vector-valued function
482 adcroft 1.1 of dimension $ l > 1 $,
483     eq. (\ref{reverse}) has to be modified according to
484     \[
485     M^T \left( \nabla_v {\cal J}^T \left(\delta \vec{J}\right) \right)
486     \, = \,
487     \nabla_u {\cal J}^T \cdot \delta \vec{J}
488     \]
489 heimbach 1.4 where now $ \delta \vec{J} \in I\!\!R^l $ is a vector of
490 cnh 1.7 dimension $ l $.
491 adcroft 1.1 In this case $ l $ reverse simulations have to be performed
492     for each $ \delta J_{k}, \,\, k = 1, \ldots, l $.
493     Then, the reverse mode is more efficient as long as
494     $ l < n $, otherwise the forward mode is preferable.
495 cnh 1.7 Strictly, the reverse mode is called adjoint mode only for
496 adcroft 1.1 $ l = 1 $.
497    
498     A detailed analysis of the underlying numerical operations
499     shows that the computation of $\nabla _{u}{\cal J}$ in this way
500     requires about 2 to 5 times the computation of the cost function.
501     Alternatively, the gradient vector could be approximated
502     by finite differences, requiring $m$ computations
503     of the perturbed cost function.
504    
505     To conclude we give two examples of commonly used types
506     of cost functions:
507    
508     \paragraph{Example 1:
509     $ {\cal J} = v_{j} (T) $} ~ \\
510     The cost function consists of the $j$-th component of the model state
511     $ \vec{v} $ at time $T$.
512     Then $ \nabla_v {\cal J}^T = {\vec{f}_{j}} $ is just the $j$-th
513     unit vector. The $ \nabla_u {\cal J}^T $
514     is the projection of the adjoint
515     operator onto the $j$-th component ${\bf f_{j}}$,
516     \[
517     \nabla_u {\cal J}^T
518     \, = \, M^T \cdot \nabla_v {\cal J}^T
519     \, = \, \sum_{i} M^T_{ji} \, {\vec{e}_{i}}
520     \]
521    
522     \paragraph{Example 2:
523     $ {\cal J} = \langle \, {\cal H}(\vec{v}) - \vec{d} \, ,
524     \, {\cal H}(\vec{v}) - \vec{d} \, \rangle $} ~ \\
525 heimbach 1.4 The cost function represents the quadratic model vs. data misfit.
526 adcroft 1.1 Here, $ \vec{d} $ is the data vector and $ {\cal H} $ represents the
527     operator which maps the model state space onto the data space.
528     Then, $ \nabla_v {\cal J} $ takes the form
529     %
530     \begin{equation*}
531     \begin{split}
532     \nabla_v {\cal J}^T & = \, 2 \, \, H \cdot
533     \left( \, {\cal H}(\vec{v}) - \vec{d} \, \right) \\
534     ~ & = \, 2 \sum_{j} \left\{ \sum_k
535     \frac{\partial {\cal H}_k}{\partial v_{j}}
536     \left( {\cal H}_k (\vec{v}) - d_k \right)
537     \right\} \, {\vec{f}_{j}} \\
538     \end{split}
539     \end{equation*}
540     %
541     where $H_{kj} = \partial {\cal H}_k / \partial v_{j} $ is the
542     Jacobi matrix of the data projection operator.
543     Thus, the gradient $ \nabla_u {\cal J} $ is given by the
544     adjoint operator,
545     driven by the model vs. data misfit:
546     \[
547     \nabla_u {\cal J}^T \, = \, 2 \, M^T \cdot
548     H \cdot \left( {\cal H}(\vec{v}) - \vec{d} \, \right)
549     \]
550    
551     \subsection{Storing vs. recomputation in reverse mode}
552     \label{checkpointing}
553    
554     We note an important aspect of the forward vs. reverse
555     mode calculation.
556 heimbach 1.4 Because of the local character of the derivative
557     (a derivative is defined w.r.t. a point along the trajectory),
558 adcroft 1.1 the intermediate results of the model trajectory
559     $\vec{v}^{(\lambda+1)}={\cal M}_{\lambda}(v^{(\lambda)})$
560     are needed to evaluate the intermediate Jacobian
561     $M_{\lambda}|_{\vec{v}^{(\lambda)}} \, \delta \vec{v}^{(\lambda)} $.
562     In the forward mode, the intermediate results are required
563     in the same order as computed by the full forward model ${\cal M}$,
564 heimbach 1.4 but in the reverse mode they are required in the reverse order.
565 adcroft 1.1 Thus, in the reverse mode the trajectory of the forward model
566     integration ${\cal M}$ has to be stored to be available in the reverse
567 heimbach 1.4 calculation. Alternatively, the complete model state up to the
568     point of evaluation has to be recomputed whenever its value is required.
569 adcroft 1.1
570     A method to balance the amount of recomputations vs.
571     storage requirements is called {\sf checkpointing}
572     (e.g. \cite{res-eta:98}).
573 adcroft 1.6 It is depicted in \ref{fig:3levelcheck} for a 3-level checkpointing
574 heimbach 1.4 [as an example, we give explicit numbers for a 3-day
575 adcroft 1.1 integration with a 1-hourly timestep in square brackets].
576     \begin{itemize}
577     %
578     \item [$lev3$]
579     In a first step, the model trajectory is subdivided into
580     $ {n}^{lev3} $ subsections [$ {n}^{lev3} $=3 1-day intervals],
581     with the label $lev3$ for this outermost loop.
582 heimbach 1.4 The model is then integrated along the full trajectory,
583 adcroft 1.1 and the model state stored only at every $ k_{i}^{lev3} $-th timestep
584     [i.e. 3 times, at
585     $ i = 0,1,2 $ corresponding to $ k_{i}^{lev3} = 0, 24, 48 $].
586     %
587     \item [$lev2$]
588 heimbach 1.4 In a second step each subsection itself is divided into
589     $ {n}^{lev2} $ sub-subsections
590 adcroft 1.1 [$ {n}^{lev2} $=4 6-hour intervals per subsection].
591     The model picks up at the last outermost dumped state
592 heimbach 1.4 $ v_{k_{n}^{lev3}} $ and is integrated forward in time along
593 adcroft 1.1 the last subsection, with the label $lev2$ for this
594     intermediate loop.
595 heimbach 1.4 The model state is now stored at every $ k_{i}^{lev2} $-th
596 adcroft 1.1 timestep
597     [i.e. 4 times, at
598     $ i = 0,1,2,3 $ corresponding to $ k_{i}^{lev2} = 48, 54, 60, 66 $].
599     %
600     \item [$lev1$]
601 heimbach 1.4 Finally, the model picks up at the last intermediate dump state
602     $ v_{k_{n}^{lev2}} $ and is integrated forward in time along
603     the last sub-subsection, with the label $lev1$ for this
604 adcroft 1.1 intermediate loop.
605 heimbach 1.4 Within this sub-subsection only, the model state is stored
606 adcroft 1.1 at every timestep
607     [i.e. every hour $ i=0,...,5$ corresponding to
608     $ k_{i}^{lev1} = 66, 67, \ldots, 71 $].
609     Thus, the final state $ v_n = v_{k_{n}^{lev1}} $ is reached
610 adcroft 1.8 and the model state of all proceeding timesteps along the last
611 heimbach 1.4 sub-subsections are available, enabling integration backwards
612     in time along the last sub-subsection.
613     Thus, the adjoint can be computed along this last
614     sub-subsection $k_{n}^{lev2}$.
615 adcroft 1.1 %
616     \end{itemize}
617     %
618     This procedure is repeated consecutively for each previous
619 heimbach 1.4 sub-subsection $k_{n-1}^{lev2}, \ldots, k_{1}^{lev2} $
620 adcroft 1.1 carrying the adjoint computation to the initial time
621     of the subsection $k_{n}^{lev3}$.
622     Then, the procedure is repeated for the previous subsection
623     $k_{n-1}^{lev3}$
624     carrying the adjoint computation to the initial time
625     $k_{1}^{lev3}$.
626    
627     For the full model trajectory of
628     $ n^{lev3} \cdot n^{lev2} \cdot n^{lev1} $ timesteps
629     the required storing of the model state was significantly reduced to
630     $ n^{lev1} + n^{lev2} + n^{lev3} $
631     [i.e. for the 3-day integration with a total oof 72 timesteps
632     the model state was stored 13 times].
633     This saving in memory comes at a cost of a required
634     3 full forward integrations of the model (one for each
635     checkpointing level).
636     The balance of storage vs. recomputation certainly depends
637     on the computing resources available.
638    
639     \begin{figure}[t!]
640 adcroft 1.6 \begin{center}
641 adcroft 1.1 %\psdraft
642 adcroft 1.6 %\psfrag{v_k1^lev3}{\mathinfigure{v_{k_{1}^{lev3}}}}
643     %\psfrag{v_kn-1^lev3}{\mathinfigure{v_{k_{n-1}^{lev3}}}}
644     %\psfrag{v_kn^lev3}{\mathinfigure{v_{k_{n}^{lev3}}}}
645     %\psfrag{v_k1^lev2}{\mathinfigure{v_{k_{1}^{lev2}}}}
646     %\psfrag{v_kn-1^lev2}{\mathinfigure{v_{k_{n-1}^{lev2}}}}
647     %\psfrag{v_kn^lev2}{\mathinfigure{v_{k_{n}^{lev2}}}}
648     %\psfrag{v_k1^lev1}{\mathinfigure{v_{k_{1}^{lev1}}}}
649     %\psfrag{v_kn^lev1}{\mathinfigure{v_{k_{n}^{lev1}}}}
650     %\mbox{\epsfig{file=part5/checkpointing.eps, width=0.8\textwidth}}
651     \resizebox{5.5in}{!}{\includegraphics{part5/checkpointing.eps}}
652 adcroft 1.1 %\psfull
653 adcroft 1.6 \end{center}
654     \caption{
655     Schematic view of intermediate dump and restart for
656 adcroft 1.1 3-level checkpointing.}
657 heimbach 1.4 \label{fig:3levelcheck}
658 adcroft 1.1 \end{figure}
659    
660 heimbach 1.4 % \subsection{Optimal perturbations}
661     % \label{sec_optpert}
662 adcroft 1.1
663    
664 heimbach 1.4 % \subsection{Error covariance estimate and Hessian matrix}
665     % \label{sec_hessian}
666 adcroft 1.1
667     \newpage
668    
669     %**********************************************************************
670     \section{AD-specific setup by example: sensitivity of carbon sequestration}
671     \label{sec_ad_setup_ex}
672     %**********************************************************************
673    
674 heimbach 1.4 The MITGCM has been adapted to enable AD using TAMC or TAF.
675 adcroft 1.1 The present description, therefore, is specific to the
676 heimbach 1.4 use of TAMC or TAF as AD tool.
677 adcroft 1.1 The following sections describe the steps which are necessary to
678     generate a tangent linear or adjoint model of the MITGCM.
679     We take as an example the sensitivity of carbon sequestration
680     in the ocean.
681     The AD-relevant hooks in the code are sketched in
682 adcroft 1.6 \ref{fig:adthemodel}, \ref{fig:adthemain}.
683 adcroft 1.1
684     \subsection{Overview of the experiment}
685    
686 cnh 1.7 We describe an adjoint sensitivity analysis of out-gassing from
687 heimbach 1.4 the ocean into the atmosphere of a carbon-like tracer injected
688 adcroft 1.1 into the ocean interior (see \cite{hil-eta:01}).
689    
690     \subsubsection{Passive tracer equation}
691    
692     For this work the MITGCM was augmented with a thermodynamically
693     inactive tracer, $C$. Tracer residing in the ocean
694 cnh 1.7 model surface layer is out-gassed according to a relaxation time scale,
695 adcroft 1.1 $\mu$. Within the ocean interior, the tracer is passively advected
696     by the ocean model currents. The full equation for the time evolution
697     %
698     \begin{equation}
699     \label{carbon_ddt}
700     \frac{\partial C}{\partial t} \, = \,
701     -U\cdot \nabla C \, - \, \mu C \, + \, \Gamma(C) \,+ \, S
702     \end{equation}
703     %
704     also includes a source term $S$. This term
705     represents interior sources of $C$ such as would arise due to
706     direct injection.
707     The velocity term, $U$, is the sum of the
708     model Eulerian circulation and an eddy-induced velocity, the latter
709 heimbach 1.4 parameterized according to Gent/McWilliams
710     (\cite{gen-mcw:90, gen-eta:95}).
711 adcroft 1.1 The convection function, $\Gamma$, mixes $C$ vertically wherever the
712     fluid is locally statically unstable.
713    
714 cnh 1.7 The out-gassing time scale, $\mu$, in eqn. (\ref{carbon_ddt})
715 adcroft 1.1 is set so that \( 1/\mu \sim 1 \ \mathrm{year} \) for the surface
716     ocean and $\mu=0$ elsewhere. With this value, eqn. (\ref{carbon_ddt})
717     is valid as a prognostic equation for small perturbations in oceanic
718     carbon concentrations. This configuration provides a
719     powerful tool for examining the impact of large-scale ocean circulation
720 cnh 1.7 on $ CO_2 $ out-gassing due to interior injections.
721 adcroft 1.1 As source we choose a constant in time injection of
722     $ S = 1 \,\, {\rm mol / s}$.
723    
724     \subsubsection{Model configuration}
725    
726     The model configuration employed has a constant
727     $4^\circ \times 4^\circ$ resolution horizontal grid and realistic
728     geography and bathymetry. Twenty vertical layers are used with
729     vertical spacing ranging
730     from 50 m near the surface to 815 m at depth.
731 cnh 1.7 Driven to steady-state by climatological wind-stress, heat and
732 adcroft 1.1 fresh-water forcing the model reproduces well known large-scale
733     features of the ocean general circulation.
734    
735 cnh 1.7 \subsubsection{Out-gassing cost function}
736 adcroft 1.1
737 cnh 1.7 To quantify and understand out-gassing due to injections of $C$
738 adcroft 1.1 in eqn. (\ref{carbon_ddt}),
739     we define a cost function $ {\cal J} $ that measures the total amount of
740 cnh 1.7 tracer out-gassed at each timestep:
741 adcroft 1.1 %
742     \begin{equation}
743     \label{cost_tracer}
744     {\cal J}(t=T)=\int_{t=0}^{t=T}\int_{A} \mu C \, dA \, dt
745     \end{equation}
746     %
747 cnh 1.7 Equation(\ref{cost_tracer}) integrates the out-gassing term, $\mu C$,
748 adcroft 1.1 from (\ref{carbon_ddt})
749     over the entire ocean surface area, $A$, and accumulates it
750     up to time $T$.
751     Physically, ${\cal J}$ can be thought of as representing the amount of
752 cnh 1.7 $CO_2$ that our model predicts would be out-gassed following an
753 adcroft 1.1 injection at rate $S$.
754     The sensitivity of ${\cal J}$ to the spatial location of $S$,
755     $\frac{\partial {\cal J}}{\partial S}$,
756     can be used to identify regions from which circulation
757 cnh 1.7 would cause $CO_2$ to rapidly out-gas following injection
758 adcroft 1.1 and regions in which $CO_2$ injections would remain effectively
759 cnh 1.7 sequestered within the ocean.
760 adcroft 1.1
761     \subsection{Code configuration}
762    
763     The model configuration for this experiment resides under the
764     directory {\it verification/carbon/}.
765 cnh 1.7 The code customization routines are in {\it verification/carbon/code/}:
766 adcroft 1.1 %
767     \begin{itemize}
768     %
769     \item {\it .genmakerc}
770     %
771     \item {\it COST\_CPPOPTIONS.h}
772     %
773     \item {\it CPP\_EEOPTIONS.h}
774     %
775     \item {\it CPP\_OPTIONS.h}
776     %
777     \item {\it CTRL\_OPTIONS.h}
778     %
779     \item {\it ECCO\_OPTIONS.h}
780     %
781     \item {\it SIZE.h}
782     %
783     \item {\it adcommon.h}
784     %
785     \item {\it tamc.h}
786     %
787     \end{itemize}
788     %
789     The runtime flag and parameters settings are contained in
790     {\it verification/carbon/input/},
791     together with the forcing fields and and restart files:
792     %
793     \begin{itemize}
794     %
795     \item {\it data}
796     %
797     \item {\it data.cost}
798     %
799     \item {\it data.ctrl}
800     %
801 heimbach 1.4 \item {\it data.gmredi}
802     %
803     \item {\it data.grdchk}
804     %
805     \item {\it data.optim}
806     %
807 adcroft 1.1 \item {\it data.pkg}
808     %
809     \item {\it eedata}
810     %
811     \item {\it topog.bin}
812     %
813     \item {\it windx.bin, windy.bin}
814     %
815     \item {\it salt.bin, theta.bin}
816     %
817     \item {\it SSS.bin, SST.bin}
818     %
819     \item {\it pickup*}
820     %
821     \end{itemize}
822     %
823     Finally, the file to generate the adjoint code resides in
824     $ adjoint/ $:
825     %
826     \begin{itemize}
827     %
828     \item {\it makefile}
829     %
830     \end{itemize}
831     %
832    
833 cnh 1.7 Below we describe the customizations of this files which are
834 adcroft 1.1 specific to this experiment.
835    
836     \subsubsection{File {\it .genmakerc}}
837 heimbach 1.4 This file overwrites default settings of {\it genmake}.
838 adcroft 1.1 In the present example it is used to switch on the following
839     packages which are related to automatic differentiation
840     and are disabled by default: \\
841 heimbach 1.4 \hspace*{4ex} {\tt set ENABLE=( autodiff cost ctrl ecco gmredi grdchk kpp )} \\
842 adcroft 1.1 Other packages which are not needed are switched off: \\
843     \hspace*{4ex} {\tt set DISABLE=( aim obcs zonal\_filt shap\_filt cal exf )}
844    
845     \subsubsection{File {\it COST\_CPPOPTIONS.h, CTRL\_OPTIONS.h}}
846    
847     These files used to contain package-specific CPP-options
848     (see Section \ref{???}).
849     For technical reasons those options have been grouped together
850     in the file {\it ECCO\_OPTIONS.h}.
851     To retain the modularity, the files have been kept and contain
852     the standard include of the {\it CPP\_OPTIONS.h} file.
853    
854     \subsubsection{File {\it CPP\_EEOPTIONS.h}}
855    
856     This file contains 'wrapper'-specific CPP options.
857     It only needs to be changed if the code is to be run
858 heimbach 1.4 in a parallel environment (see Section \ref{???}).
859 adcroft 1.1
860     \subsubsection{File {\it CPP\_OPTIONS.h}}
861    
862     This file contains model-specific CPP options
863     (see Section \ref{???}).
864     Most options are related to the forward model setup.
865     They are identical to the global steady circulation setup of
866     {\it verification/exp2/}.
867 heimbach 1.4 The three options specific to this experiment are \\
868     \hspace*{4ex} {\tt \#define ALLOW\_PASSIVE\_TRACER} \\
869     This flag enables the code to carry through the
870     advection/diffusion of a passive tracer along the
871     model integration. \\
872 adcroft 1.1 \hspace*{4ex} {\tt \#define ALLOW\_MIT\_ADJOINT\_RUN} \\
873     This flag enables the inclusion of some AD-related fields
874 cnh 1.7 concerning initialization, link between control variables
875 adcroft 1.1 and forward model variables, and the call to the top-level
876     forward/adjoint subroutine {\it adthe\_main\_loop}
877 heimbach 1.4 instead of {\it the\_main\_loop}. \\
878     \hspace*{4ex} {\tt \#define ALLOW\_GRADIENT\_CHECK} \\
879     This flag enables the gradient check package.
880     After computing the unperturbed cost function and its gradient,
881     a series of computations are performed for which \\
882     $\bullet$ an element of the control vector is perturbed \\
883     $\bullet$ the cost function w.r.t. the perturbed element is
884     computed \\
885     $\bullet$ the difference between the perturbed and unperturbed
886     cost function is computed to compute the finite difference gradient \\
887     $\bullet$ the finite difference gradient is compared with the
888     adjoint-generated gradient.
889     The gradient check package is further described in Section ???.
890 adcroft 1.1
891     \subsubsection{File {\it ECCO\_OPTIONS.h}}
892    
893     The CPP options of several AD-related packages are grouped
894     in this file:
895     %
896     \begin{itemize}
897     %
898     \item
899     Adjoint support package: {\it pkg/autodiff/} \\
900     This package contains hand-written adjoint code such as
901     active file handling, flow directives for files which must not
902     be differentiated, and TAMC-specific header files. \\
903     \hspace*{4ex} {\tt \#define ALLOW\_AUTODIFF\_TAMC} \\
904     defines TAMC-related features in the code. \\
905     \hspace*{4ex} {\tt \#define ALLOW\_TAMC\_CHECKPOINTING} \\
906     enables the checkpointing feature of TAMC
907     (see Section \ref{???}).
908     In the present example a 3-level checkpointing is implemented.
909     The code contains the relevant store directives, common block
910 cnh 1.7 and tape initializations, storing key computation,
911 adcroft 1.1 and loop index handling.
912     The checkpointing length at each level is defined in
913     file {\it tamc.h}, cf. below.
914     %
915     \item Cost function package: {\it pkg/cost/} \\
916     This package contains all relevant routines for
917 cnh 1.7 initializing, accumulating and finalizing the cost function
918 adcroft 1.1 (see Section \ref{???}). \\
919     \hspace*{4ex} {\tt \#define ALLOW\_COST} \\
920     enables all general aspects of the cost function handling,
921 cnh 1.7 in particular the hooks in the forward code for
922     initializing, accumulating and finalizing the cost function. \\
923 adcroft 1.1 \hspace*{4ex} {\tt \#define ALLOW\_COST\_TRACER} \\
924 heimbach 1.4 includes the call to the cost function for this
925 adcroft 1.1 particular experiment, eqn. (\ref{cost_tracer}).
926     %
927     \item Control variable package: {\it pkg/ctrl/} \\
928     This package contains all relevant routines for
929     the handling of the control vector.
930     Each control variable can be enabled/disabled with its own flag: \\
931     \begin{tabular}{ll}
932     \hspace*{2ex} {\tt \#define ALLOW\_THETA0\_CONTROL} &
933     initial temperature \\
934     \hspace*{2ex} {\tt \#define ALLOW\_SALT0\_CONTROL} &
935     initial salinity \\
936     \hspace*{2ex} {\tt \#define ALLOW\_TR0\_CONTROL} &
937     initial passive tracer concentration \\
938     \hspace*{2ex} {\tt \#define ALLOW\_TAUU0\_CONTROL} &
939     zonal wind stress \\
940     \hspace*{2ex} {\tt \#define ALLOW\_TAUV0\_CONTROL} &
941     meridional wind stress \\
942     \hspace*{2ex} {\tt \#define ALLOW\_SFLUX0\_CONTROL} &
943     freshwater flux \\
944     \hspace*{2ex} {\tt \#define ALLOW\_HFLUX0\_CONTROL} &
945     heat flux \\
946 heimbach 1.4 \hspace*{2ex} {\tt \#define ALLOW\_DIFFKR\_CONTROL} &
947 adcroft 1.1 diapycnal diffusivity \\
948     \hspace*{2ex} {\tt \#undef ALLOW\_KAPPAGM\_CONTROL} &
949     isopycnal diffusivity \\
950     \end{tabular}
951     %
952     \end{itemize}
953    
954     \subsubsection{File {\it SIZE.h}}
955    
956     The file contains the grid point dimensions of the forward
957     model. It is identical to the {\it verification/exp2/}: \\
958     \hspace*{4ex} {\tt sNx = 90} \\
959     \hspace*{4ex} {\tt sNy = 40} \\
960     \hspace*{4ex} {\tt Nr = 20} \\
961 cnh 1.7 It corresponds to a single-tile/single-processor setup:
962 adcroft 1.1 {\tt nSx = nSy = 1, nPx = nPy = 1},
963     with standard overlap dimensioning
964     {\tt OLx = OLy = 3}.
965    
966     \subsubsection{File {\it adcommon.h}}
967    
968     This file contains common blocks of some adjoint variables
969     that are generated by TAMC.
970     The common blocks are used by the adjoint support routine
971     {\it addummy\_in\_stepping} which needs to access those variables:
972    
973     \begin{tabular}{ll}
974     \hspace*{4ex} {\tt common /addynvars\_r/} &
975     \hspace*{4ex} is related to {\it DYNVARS.h} \\
976     \hspace*{4ex} {\tt common /addynvars\_cd/} &
977     \hspace*{4ex} is related to {\it DYNVARS.h} \\
978 heimbach 1.4 \hspace*{4ex} {\tt common /addynvars\_diffkr/} &
979     \hspace*{4ex} is related to {\it DYNVARS.h} \\
980     \hspace*{4ex} {\tt common /addynvars\_kapgm/} &
981     \hspace*{4ex} is related to {\it DYNVARS.h} \\
982 adcroft 1.1 \hspace*{4ex} {\tt common /adtr1\_r/} &
983     \hspace*{4ex} is related to {\it TR1.h} \\
984     \hspace*{4ex} {\tt common /adffields/} &
985     \hspace*{4ex} is related to {\it FFIELDS.h}\\
986     \end{tabular}
987    
988     Note that if the structure of the common block changes in the
989     above header files of the forward code, the structure
990     of the adjoint common blocks will change accordingly.
991     Thus, it has to be made sure that the structure of the
992     adjoint common block in the hand-written file {\it adcommon.h}
993     complies with the automatically generated adjoint common blocks
994     in {\it adjoint\_model.F}.
995    
996     \subsubsection{File {\it tamc.h}}
997    
998     This routine contains the dimensions for TAMC checkpointing.
999     %
1000     \begin{itemize}
1001     %
1002     \item {\tt \#ifdef ALLOW\_TAMC\_CHECKPOINTING} \\
1003     3-level checkpointing is enabled, i.e. the timestepping
1004     is divided into three different levels (see Section \ref{???}).
1005     The model state of the outermost ({\tt nchklev\_3}) and the
1006 heimbach 1.4 intermediate ({\tt nchklev\_2}) timestepping loop are stored to file
1007 adcroft 1.1 (handled in {\it the\_main\_loop}).
1008     The innermost loop ({\tt nchklev\_1})
1009     avoids I/O by storing all required variables
1010     to common blocks. This storing may also be necessary if
1011     no checkpointing is chosen
1012     (nonlinear functions, if-statements, iterative loops, ...).
1013     In the present example the dimensions are chosen as follows: \\
1014     \hspace*{4ex} {\tt nchklev\_1 = 36 } \\
1015     \hspace*{4ex} {\tt nchklev\_2 = 30 } \\
1016     \hspace*{4ex} {\tt nchklev\_3 = 60 } \\
1017     To guarantee that the checkpointing intervals span the entire
1018 heimbach 1.4 integration period the following relation must be satisfied: \\
1019 adcroft 1.1 \hspace*{4ex} {\tt nchklev\_1*nchklev\_2*nchklev\_3 $ \ge $ nTimeSteps} \\
1020     where {\tt nTimeSteps} is either specified in {\it data}
1021     or computed via \\
1022     \hspace*{4ex} {\tt nTimeSteps = (endTime-startTime)/deltaTClock }.
1023     %
1024     \item {\tt \#undef ALLOW\_TAMC\_CHECKPOINTING} \\
1025     No checkpointing is enabled.
1026     In this case the relevant counter is {\tt nchklev\_0}.
1027     Similar to above, the following relation has to be satisfied \\
1028     \hspace*{4ex} {\tt nchklev\_0 $ \ge $ nTimeSteps}.
1029     %
1030     \end{itemize}
1031    
1032 heimbach 1.4 The following parameters may be worth describing: \\
1033     %
1034     \hspace*{4ex} {\tt isbyte} \\
1035     \hspace*{4ex} {\tt maxpass} \\
1036     ~
1037    
1038 adcroft 1.1 \subsubsection{File {\it makefile}}
1039    
1040 cnh 1.7 This file contains all relevant parameter flags and
1041 heimbach 1.4 lists to run TAMC or TAF.
1042 adcroft 1.1 It is assumed that TAMC is available to you, either locally,
1043     being installed on your network, or remotely through the 'TAMC Utility'.
1044     TAMC is called with the command {\tt tamc} followed by a
1045     number of options. They are described in detail in the
1046     TAMC manual \cite{gie:99}.
1047     Here we briefly discuss the main flags used in the {\it makefile}
1048     %
1049     \begin{itemize}
1050     \item [{\tt tamc}] {\tt
1051     -input <variable names>
1052 heimbach 1.4 -output <variable name> -r4 ... \\
1053 adcroft 1.1 -toplevel <S/R name> -reverse <file names>
1054     }
1055     \end{itemize}
1056     %
1057     \begin{itemize}
1058     %
1059     \item {\tt -toplevel <S/R name>} \\
1060     Name of the toplevel routine, with respect to which the
1061     control flow analysis is performed.
1062     %
1063     \item {\tt -input <variable names>} \\
1064     List of independent variables $ u $ with respect to which the
1065     dependent variable $ J $ is differentiated.
1066     %
1067     \item {\tt -output <variable name>} \\
1068     Dependent variable $ J $ which is to be differentiated.
1069     %
1070     \item {\tt -reverse <file names>} \\
1071     Adjoint code is generated to compute the sensitivity of an
1072     independent variable w.r.t. many dependent variables.
1073 heimbach 1.4 In the discussion of Section ???
1074     the generated adjoint top-level routine computes the product
1075 adcroft 1.1 of the transposed Jacobian matrix $ M^T $ times
1076     the gradient vector $ \nabla_v J $.
1077     \\
1078     {\tt <file names>} refers to the list of files {\it .f} which are to be
1079     analyzed by TAMC. This list is generally smaller than the full list
1080     of code to be compiled. The files not contained are either
1081 cnh 1.7 above the top-level routine (some initializations), or are
1082 adcroft 1.1 deliberately hidden from TAMC, either because hand-written
1083     adjoint routines exist, or the routines must not (or don't have to)
1084     be differentiated. For each routine which is part of the flow tree
1085 heimbach 1.4 of the top-level routine, but deliberately hidden from TAMC
1086     (or for each package which contains such routines),
1087 adcroft 1.1 a corresponding file {\it .flow} exists containing flow directives
1088     for TAMC.
1089     %
1090 heimbach 1.4 \item {\tt -r4} \\
1091     ~
1092     %
1093 adcroft 1.1 \end{itemize}
1094    
1095    
1096 heimbach 1.5 \subsubsection{The input parameter files}
1097 adcroft 1.1
1098 heimbach 1.5 \paragraph{File {\it data}}
1099 adcroft 1.1
1100 heimbach 1.5 \paragraph{File {\it data.cost}}
1101 adcroft 1.1
1102 heimbach 1.5 \paragraph{File {\it data.ctrl}}
1103 heimbach 1.4
1104 heimbach 1.5 \paragraph{File {\it data.gmredi}}
1105    
1106     \paragraph{File {\it data.grdchk}}
1107    
1108     \paragraph{File {\it data.optim}}
1109    
1110     \paragraph{File {\it data.pkg}}
1111    
1112     \paragraph{File {\it eedata}}
1113    
1114     \paragraph{File {\it topog.bin}}
1115    
1116     \paragraph{File {\it windx.bin, windy.bin}}
1117    
1118     \paragraph{File {\it salt.bin, theta.bin}}
1119    
1120     \paragraph{File {\it SSS.bin, SST.bin}}
1121    
1122     \paragraph{File {\it pickup*}}
1123    
1124     \subsection{Compiling the model and its adjoint}
1125    
1126     The built process of the adjoint model is slightly more
1127     complex than that of compiling the forward code.
1128     The main reason is that the adjoint code generation requires
1129     a specific list of routines that are to be differentiated
1130     (as opposed to the automatic generation of a list of
1131     files to be compiled by genmake).
1132     This list excludes routines that don't have to be or must not be
1133     differentiated. For some of the latter routines flow directives
1134     may be necessary, a list of which has to be given as well.
1135     For this reason, a separate {\it makefile} is currently
1136     maintained in the directory {\tt adjoint/}. This
1137     makefile is responsible for the adjoint code generation.
1138    
1139     In the following we describe the build process step by step,
1140     assuming you are in the directory {\tt bin/}.
1141     A summary of steps to follow is given at the end.
1142    
1143     \paragraph{Adjoint code generation and compilation -- step by step}
1144    
1145     \begin{enumerate}
1146     %
1147     \item
1148     {\tt ln -s ../verification/???/code/.genmakerc .} \\
1149     {\tt ln -s ../verification/???/code/*.[Fh] .} \\
1150     Link your customized genmake options, header files,
1151     and modified code to the compile directory.
1152     %
1153     \item
1154     {\tt ../tools/genmake -makefile} \\
1155     Generate your Makefile (cf. Section ???).
1156     %
1157     \item
1158     {\tt make depend} \\
1159     Dependency analysis for the CPP pre-compiler (cf. Section ???).
1160     %
1161     \item
1162     {\tt make small\_f} \\
1163     This is the first difference between forward code compilation
1164     and adjoint code generation and compilation.
1165     Instead of going through the entire compilation process
1166     (CPP precompiling -- {\tt .f}, object code generation -- {\tt .o},
1167     linking of object files and libraries to generate executable),
1168     only the CPP compiler is invoked at this stage to generate
1169     the {\tt .f} files.
1170     %
1171     \item
1172     {\tt cd ../adjoint} \\
1173     {\tt make adtaf} or {\tt make adtamc} \\
1174     Depending on whether you have TAF or TAMC at your disposal,
1175     you'll choose {\tt adtaf} or {\tt adtamc} as your
1176     make target for the {\it makefile} in the directory {\tt adjoint/}.
1177     Several things happen at this stage.
1178     %
1179     \begin{enumerate}
1180     %
1181     \item
1182     The initial template file {\it adjoint\_model.F} which is part
1183     of the compiling list created by {\it genmake} is restored.
1184     %
1185     \item
1186     All Fortran routines {\tt *.f} in {\tt bin/} are
1187     concatenated into a single file (it's current name is
1188     {\it tamc\_code.f}).
1189     %
1190     \item
1191     Adjoint code is generated by TAMC or TAF.
1192     The adjoint code is written to the file {\it tamc\_code\_ad.f}.
1193     It contains all adjoint routines of the forward routines
1194     concatenated in {\it tamc\_code.f}.
1195     For a given forward routines {\tt subroutine routinename}
1196     the adjoint routine is named {\tt adsubroutine routinename}
1197     by default (that default can be changed via the flag
1198     {\tt -admark <markname>}).
1199     Furthermore, it may contain modified code which
1200     incorporates the translation of adjoint store directives
1201     into specific Fortran code.
1202     For a given forward routines {\tt subroutine routinename}
1203     the modified routine is named {\tt mdsubroutine routinename}.
1204     TAMC or TAF info is written to file
1205     {\it tamc\_code.prot} or {\it taf.log}, respectively.
1206     %
1207     \end{enumerate}
1208     %
1209     \item
1210     {\tt make adchange} \\
1211     The multi-threading capability of the MITGCM requires a slight
1212     change in the parameter list of some routines that are related to
1213     to active file handling.
1214 cnh 1.7 This post-processing invokes the sed script {\it adjoint\_ecco\_sed.com}
1215 heimbach 1.5 to insert the threading counter {\bf myThId} into the parameter list
1216     of those subroutines.
1217     The resulting code is written to file {\it tamc\_code\_sed\_ad.f}
1218     and appended to the file {\it adjoint\_model.F}.
1219 cnh 1.7 This concludes the adjoint code generation.
1220 heimbach 1.5 %
1221     \item
1222     {\tt cd ../bin} \\
1223     {\tt make} \\
1224     The file {\it adjoint\_model.F} now contains the full adjoint code.
1225     All routines are now compiled.
1226     %
1227     \end{enumerate}
1228 heimbach 1.4
1229 heimbach 1.5 \paragraph{Adjoint code generation and compilation -- summary}
1230     ~ \\
1231 heimbach 1.4
1232 heimbach 1.5 \[
1233     \boxed{
1234     \begin{split}
1235     ~ & \mbox{\tt cd bin} \\
1236     ~ & \mbox{\tt ln -s ../verification/my\_experiment/code/.genmakerc .} \\
1237     ~ & \mbox{\tt ln -s ../verification/my\_experiment/code/*.[Fh] .} \\
1238     ~ & \mbox{\tt ../tools/genmake -makefile} \\
1239     ~ & \mbox{\tt make depend} \\
1240     ~ & \mbox{\tt make small\_f} \\
1241     ~ & \mbox{\tt cd ../adjoint} \\
1242     ~ & \mbox{\tt make adtaf <OR: make adtamc>} \\
1243     ~ & \mbox{\tt make adchange} \\
1244     ~ & \mbox{\tt cd ../bin} \\
1245     ~ & \mbox{\tt make} \\
1246     \end{split}
1247     }
1248     \]
1249 adcroft 1.1
1250     \newpage
1251    
1252     %**********************************************************************
1253 heimbach 1.4 \section{TLM and ADM generation in general}
1254 adcroft 1.1 \label{sec_ad_setup_gen}
1255     %**********************************************************************
1256    
1257     In this section we describe in a general fashion
1258     the parts of the code that are relevant for automatic
1259     differentiation using the software tool TAMC.
1260    
1261 heimbach 1.4 \begin{figure}[b!]
1262     \input{part5/doc_ad_the_model}
1263     \caption{~}
1264     \label{fig:adthemodel}
1265     \end{figure}
1266    
1267 adcroft 1.6 The basic flow is depicted in \ref{fig:adthemodel}.
1268 heimbach 1.4 If the option {\tt ALLOW\_AUTODIFF\_TAMC} is defined, the driver routine
1269     {\it the\_model\_main}, instead of calling {\it the\_main\_loop},
1270     invokes the adjoint of this routine, {\it adthe\_main\_loop},
1271     which is the toplevel routine in terms of reverse mode computation.
1272     The routine {\it adthe\_main\_loop} has been generated using TAMC.
1273     It contains both the forward integration of the full model,
1274     any additional storing that is required for efficient checkpointing,
1275     and the reverse integration of the adjoint model.
1276     The structure of {\it adthe\_main\_loop} has been strongly
1277     simplified for clarification; in particular, no checkpointing
1278     procedures are shown here.
1279     Prior to the call of {\it adthe\_main\_loop}, the routine
1280     {\it ctrl\_unpack} is invoked to unpack the control vector,
1281     and following that call, the routine {\it ctrl\_pack}
1282     is invoked to pack the control vector
1283     (cf. Section \ref{section_ctrl}).
1284     If gradient checks are to be performed, the option
1285     {\tt ALLOW\_GRADIENT\_CHECK} is defined. In this case
1286     the driver routine {\it grdchk\_main} is called after
1287     the gradient has been computed via the adjoint
1288     (cf. Section \ref{section_grdchk}).
1289    
1290     \subsection{The cost function (dependent variable)
1291     \label{section_cost}}
1292 adcroft 1.1
1293     The cost function $ {\cal J} $ is referred to as the {\sf dependent variable}.
1294     It is a function of the input variables $ \vec{u} $ via the composition
1295     $ {\cal J}(\vec{u}) \, = \, {\cal J}(M(\vec{u})) $.
1296     The input is referred to as the
1297     {\sf independent variables} or {\sf control variables}.
1298     All aspects relevant to the treatment of the cost function $ {\cal J} $
1299 cnh 1.7 (parameter setting, initialization, accumulation,
1300 heimbach 1.4 final evaluation), are controlled by the package {\it pkg/cost}.
1301    
1302     \begin{figure}[h!]
1303     \input{part5/doc_cost_flow}
1304     \caption{~}
1305     \label{fig:costflow}
1306     \end{figure}
1307 adcroft 1.1
1308     \subsubsection{genmake and CPP options}
1309     %
1310     \begin{itemize}
1311     %
1312     \item
1313     \fbox{
1314     \begin{minipage}{12cm}
1315     {\it genmake}, {\it CPP\_OPTIONS.h}, {\it ECCO\_CPPOPTIONS.h}
1316     \end{minipage}
1317     }
1318     \end{itemize}
1319     %
1320     The directory {\it pkg/cost} can be included to the
1321     compile list in 3 different ways (cf. Section \ref{???}):
1322     %
1323     \begin{enumerate}
1324     %
1325     \item {\it genmake}: \\
1326 heimbach 1.4 Change the default settings in the file {\it genmake} by adding
1327 adcroft 1.1 {\bf cost} to the {\bf enable} list (not recommended).
1328     %
1329     \item {\it .genmakerc}: \\
1330     Customize the settings of {\bf enable}, {\bf disable} which are
1331     appropriate for your experiment in the file {\it .genmakerc}
1332     and add the file to your compile directory.
1333     %
1334     \item genmake-options: \\
1335     Call {\it genmake} with the option
1336     {\tt genmake -enable=cost}.
1337     %
1338     \end{enumerate}
1339 heimbach 1.4 The basic CPP option to enable the cost function is {\bf ALLOW\_COST}.
1340     Each specific cost function contribution has its own option.
1341     For the present example the option is {\bf ALLOW\_COST\_TRACER}.
1342     All cost-specific options are set in {\it ECCO\_CPPOPTIONS.h}
1343 adcroft 1.1 Since the cost function is usually used in conjunction with
1344     automatic differentiation, the CPP option
1345     {\bf ALLOW\_ADJOINT\_RUN} should be defined
1346     (file {\it CPP\_OPTIONS.h}).
1347    
1348 cnh 1.7 \subsubsection{Initialization}
1349 adcroft 1.1 %
1350 cnh 1.7 The initialization of the {\it cost} package is readily enabled
1351 adcroft 1.1 as soon as the CPP option {\bf ALLOW\_ADJOINT\_RUN} is defined.
1352     %
1353     \begin{itemize}
1354     %
1355     \item
1356     \fbox{
1357     \begin{minipage}{12cm}
1358     Parameters: {\it cost\_readparms}
1359     \end{minipage}
1360     }
1361     \\
1362     This S/R
1363     reads runtime flags and parameters from file {\it data.cost}.
1364     For the present example the only relevant parameter read
1365     is {\bf mult\_tracer}. This multiplier enables different
1366     cost function contributions to be switched on
1367     ( = 1.) or off ( = 0.) at runtime.
1368     For more complex cost functions which involve model vs. data
1369     misfits, the corresponding data filenames and data
1370     specifications (start date and time, period, ...) are read
1371     in this S/R.
1372     %
1373     \item
1374     \fbox{
1375     \begin{minipage}{12cm}
1376     Variables: {\it cost\_init}
1377     \end{minipage}
1378     }
1379     \\
1380     This S/R
1381 cnh 1.7 initializes the different cost function contributions.
1382     The contribution for the present example is {\bf objf\_tracer}
1383 adcroft 1.1 which is defined on each tile (bi,bj).
1384     %
1385     \end{itemize}
1386     %
1387 heimbach 1.4 \subsubsection{Accumulation}
1388 adcroft 1.1 %
1389     \begin{itemize}
1390     %
1391     \item
1392     \fbox{
1393     \begin{minipage}{12cm}
1394     {\it cost\_tile}, {\it cost\_tracer}
1395     \end{minipage}
1396     }
1397     \end{itemize}
1398     %
1399     The 'driver' routine
1400     {\it cost\_tile} is called at the end of each time step.
1401     Within this 'driver' routine, S/R are called for each of
1402     the chosen cost function contributions.
1403     In the present example ({\bf ALLOW\_COST\_TRACER}),
1404     S/R {\it cost\_tracer} is called.
1405     It accumulates {\bf objf\_tracer} according to eqn. (\ref{???}).
1406     %
1407     \subsubsection{Finalize all contributions}
1408     %
1409     \begin{itemize}
1410     %
1411     \item
1412     \fbox{
1413     \begin{minipage}{12cm}
1414     {\it cost\_final}
1415     \end{minipage}
1416     }
1417     \end{itemize}
1418     %
1419     At the end of the forward integration S/R {\it cost\_final}
1420     is called. It accumulates the total cost function {\bf fc}
1421     from each contribution and sums over all tiles:
1422     \begin{equation}
1423     {\cal J} \, = \,
1424     {\rm fc} \, = \,
1425     {\rm mult\_tracer} \sum_{bi,\,bj}^{nSx,\,nSy}
1426     {\rm objf\_tracer}(bi,bj) \, + \, ...
1427     \end{equation}
1428     %
1429     The total cost function {\bf fc} will be the
1430     'dependent' variable in the argument list for TAMC, i.e.
1431     \begin{verbatim}
1432     tamc -output 'fc' ...
1433     \end{verbatim}
1434    
1435 cnh 1.3 %%%% \end{document}
1436 adcroft 1.1
1437     \begin{figure}
1438     \input{part5/doc_ad_the_main}
1439 heimbach 1.4 \caption{~}
1440 adcroft 1.1 \label{fig:adthemain}
1441     \end{figure}
1442    
1443 heimbach 1.4 \subsection{The control variables (independent variables)
1444     \label{section_ctrl}}
1445 adcroft 1.1
1446     The control variables are a subset of the model input
1447     (initial conditions, boundary conditions, model parameters).
1448     Here we identify them with the variable $ \vec{u} $.
1449     All intermediate variables whose derivative w.r.t. control
1450 heimbach 1.4 variables do not vanish are called {\sf active variables}.
1451 adcroft 1.1 All subroutines whose derivative w.r.t. the control variables
1452     don't vanish are called {\sf active routines}.
1453     Read and write operations from and to file can be viewed
1454     as variable assignments. Therefore, files to which
1455     active variables are written and from which active variables
1456     are read are called {\sf active files}.
1457     All aspects relevant to the treatment of the control variables
1458 cnh 1.7 (parameter setting, initialization, perturbation)
1459     are controlled by the package {\it pkg/ctrl}.
1460 adcroft 1.1
1461 heimbach 1.4 \begin{figure}[h!]
1462     \input{part5/doc_ctrl_flow}
1463     \caption{~}
1464     \label{fig:ctrlflow}
1465     \end{figure}
1466    
1467 adcroft 1.1 \subsubsection{genmake and CPP options}
1468     %
1469     \begin{itemize}
1470     %
1471     \item
1472     \fbox{
1473     \begin{minipage}{12cm}
1474     {\it genmake}, {\it CPP\_OPTIONS.h}, {\it ECCO\_CPPOPTIONS.h}
1475     \end{minipage}
1476     }
1477     \end{itemize}
1478     %
1479     To enable the directory to be included to the compile list,
1480     {\bf ctrl} has to be added to the {\bf enable} list in
1481     {\it .genmakerc} (or {\it genmake} itself).
1482     Each control variable is enabled via its own CPP option
1483     in {\it ECCO\_CPPOPTIONS.h}.
1484    
1485 cnh 1.7 \subsubsection{Initialization}
1486 adcroft 1.1 %
1487     \begin{itemize}
1488     %
1489     \item
1490     \fbox{
1491     \begin{minipage}{12cm}
1492     Parameters: {\it ctrl\_readparms}
1493     \end{minipage}
1494     }
1495     \\
1496     %
1497     This S/R
1498     reads runtime flags and parameters from file {\it data.ctrl}.
1499     For the present example the file contains the file names
1500     of each control variable that is used.
1501     In addition, the number of wet points for each control
1502     variable and the net dimension of the space of control
1503     variables (counting wet points only) {\bf nvarlength}
1504     is determined.
1505     Masks for wet points for each tile {\bf (bi,\,bj)}
1506     and vertical layer {\bf k} are generated for the three
1507     relevant categories on the C-grid:
1508     {\bf nWetCtile} for tracer fields,
1509     {\bf nWetWtile} for zonal velocity fields,
1510     {\bf nWetStile} for meridional velocity fields.
1511     %
1512     \item
1513     \fbox{
1514     \begin{minipage}{12cm}
1515     Control variables, control vector,
1516     and their gradients: {\it ctrl\_unpack}
1517     \end{minipage}
1518     }
1519     \\
1520     %
1521     Two important issues related to the handling of the control
1522     variables in the MITGCM need to be addressed.
1523     First, in order to save memory, the control variable arrays
1524     are not kept in memory, but rather read from file and added
1525 cnh 1.7 to the initial fields during the model initialization phase.
1526 adcroft 1.1 Similarly, the corresponding adjoint fields which represent
1527     the gradient of the cost function w.r.t. the control variables
1528 heimbach 1.4 are written to file at the end of the adjoint integration.
1529 adcroft 1.1 Second, in addition to the files holding the 2-dim. and 3-dim.
1530 heimbach 1.4 control variables and the corresponding cost gradients,
1531     a 1-dim. {\sf control vector}
1532 adcroft 1.1 and {\sf gradient vector} are written to file. They contain
1533     only the wet points of the control variables and the corresponding
1534     gradient.
1535     This leads to a significant data compression.
1536 heimbach 1.4 Furthermore, an option is available
1537     ({\tt ALLOW\_NONDIMENSIONAL\_CONTROL\_IO}) to
1538     non-dimensionalise the control and gradient vector,
1539     which otherwise would contain different pieces of different
1540     magnitudes and units.
1541     Finally, the control and gradient vector can be passed to a
1542 adcroft 1.1 minimization routine if an update of the control variables
1543     is sought as part of a minimization exercise.
1544    
1545     The files holding fields and vectors of the control variables
1546     and gradient are generated and initialised in S/R {\it ctrl\_unpack}.
1547     %
1548     \end{itemize}
1549    
1550     \subsubsection{Perturbation of the independent variables}
1551     %
1552 heimbach 1.4 The dependency flow for differentiation w.r.t. the controls
1553     starts with adding a perturbation onto the input variable,
1554 adcroft 1.1 thus defining the independent or control variables for TAMC.
1555 heimbach 1.4 Three types of controls may be considered:
1556 adcroft 1.1 %
1557     \begin{itemize}
1558     %
1559     \item
1560     \fbox{
1561     \begin{minipage}{12cm}
1562     {\it ctrl\_map\_ini} (initial value sensitivity):
1563     \end{minipage}
1564     }
1565     \\
1566     %
1567     Consider as an example the initial tracer distribution
1568     {\bf tr1} as control variable.
1569     After {\bf tr1} has been initialised in
1570 heimbach 1.4 {\it ini\_tr1} (dynamical variables such as
1571 adcroft 1.1 temperature and salinity are initialised in {\it ini\_fields}),
1572     a perturbation anomaly is added to the field in S/R
1573     {\it ctrl\_map\_ini}
1574     %
1575     \begin{equation}
1576     \begin{split}
1577     u & = \, u_{[0]} \, + \, \Delta u \\
1578     {\bf tr1}(...) & = \, {\bf tr1_{ini}}(...) \, + \, {\bf xx\_tr1}(...)
1579     \label{perturb}
1580     \end{split}
1581     \end{equation}
1582     %
1583 heimbach 1.4 {\bf xx\_tr1} is a 3-dim. global array
1584 adcroft 1.1 holding the perturbation. In the case of a simple
1585     sensitivity study this array is identical to zero.
1586 heimbach 1.4 However, it's specification is essential in the context
1587     of automatic differentiation since TAMC
1588 adcroft 1.1 treats the corresponding line in the code symbolically
1589     when determining the differentiation chain and its origin.
1590     Thus, the variable names are part of the argument list
1591     when calling TAMC:
1592     %
1593     \begin{verbatim}
1594     tamc -input 'xx_tr1 ...' ...
1595     \end{verbatim}
1596     %
1597     Now, as mentioned above, the MITGCM avoids maintaining
1598     an array for each control variable by reading the
1599     perturbation to a temporary array from file.
1600     To ensure the symbolic link to be recognized by TAMC, a scalar
1601     dummy variable {\bf xx\_tr1\_dummy} is introduced
1602     and an 'active read' routine of the adjoint support
1603     package {\it pkg/autodiff} is invoked.
1604     The read-procedure is tagged with the variable
1605 cnh 1.7 {\bf xx\_tr1\_dummy} enabling TAMC to recognize the
1606     initialization of the perturbation.
1607 adcroft 1.1 The modified call of TAMC thus reads
1608     %
1609     \begin{verbatim}
1610     tamc -input 'xx_tr1_dummy ...' ...
1611     \end{verbatim}
1612     %
1613     and the modified operation to (\ref{perturb})
1614     in the code takes on the form
1615     %
1616     \begin{verbatim}
1617     call active_read_xyz(
1618     & ..., tmpfld3d, ..., xx_tr1_dummy, ... )
1619    
1620     tr1(...) = tr1(...) + tmpfld3d(...)
1621     \end{verbatim}
1622     %
1623     Note, that reading an active variable corresponds
1624     to a variable assignment. Its derivative corresponds
1625     to a write statement of the adjoint variable.
1626     The 'active file' routines have been designed
1627 heimbach 1.4 to support active read and corresponding adjoint active write
1628     operations (and vice versa).
1629 adcroft 1.1 %
1630     \item
1631     \fbox{
1632     \begin{minipage}{12cm}
1633     {\it ctrl\_map\_forcing} (boundary value sensitivity):
1634     \end{minipage}
1635     }
1636     \\
1637     %
1638     The handling of boundary values as control variables
1639     proceeds exactly analogous to the initial values
1640     with the symbolic perturbation taking place in S/R
1641     {\it ctrl\_map\_forcing}.
1642     Note however an important difference:
1643     Since the boundary values are time dependent with a new
1644     forcing field applied at each time steps,
1645 heimbach 1.4 the general problem may be thought of as
1646     a new control variable at each time step
1647     (or, if the perturbation is averaged over a certain period,
1648     at each $ N $ timesteps), i.e.
1649 adcroft 1.1 \[
1650     u_{\rm forcing} \, = \,
1651     \{ \, u_{\rm forcing} ( t_n ) \, \}_{
1652     n \, = \, 1, \ldots , {\rm nTimeSteps} }
1653     \]
1654     %
1655     In the current example an equilibrium state is considered,
1656     and only an initial perturbation to
1657     surface forcing is applied with respect to the
1658     equilibrium state.
1659     A time dependent treatment of the surface forcing is
1660     implemented in the ECCO environment, involving the
1661     calendar ({\it cal}~) and external forcing ({\it exf}~) packages.
1662     %
1663     \item
1664     \fbox{
1665     \begin{minipage}{12cm}
1666     {\it ctrl\_map\_params} (parameter sensitivity):
1667     \end{minipage}
1668     }
1669     \\
1670     %
1671     This routine is not yet implemented, but would proceed
1672     proceed along the same lines as the initial value sensitivity.
1673 heimbach 1.4 The mixing parameters {\bf diffkr} and {\bf kapgm}
1674     are currently added as controls in {\it ctrl\_map\_ini.F}.
1675 adcroft 1.1 %
1676     \end{itemize}
1677     %
1678    
1679     \subsubsection{Output of adjoint variables and gradient}
1680     %
1681 heimbach 1.4 Several ways exist to generate output of adjoint fields.
1682 adcroft 1.1 %
1683     \begin{itemize}
1684     %
1685     \item
1686     \fbox{
1687     \begin{minipage}{12cm}
1688 heimbach 1.4 {\it ctrl\_map\_ini, ctrl\_map\_forcing}:
1689 adcroft 1.1 \end{minipage}
1690     }
1691     \\
1692     \begin{itemize}
1693     %
1694 heimbach 1.4 \item {\bf xx\_...}: the control variable fields \\
1695     Before the forward integration, the control
1696     variables are read from file {\bf xx\_ ...} and added to
1697     the model field.
1698 adcroft 1.1 %
1699     \item {\bf adxx\_...}: the adjoint variable fields, i.e. the gradient
1700 heimbach 1.4 $ \nabla _{u}{\cal J} $ for each control variable \\
1701     After the adjoint integration the corresponding adjoint
1702     variables are written to {\bf adxx\_ ...}.
1703 adcroft 1.1 %
1704 heimbach 1.4 \end{itemize}
1705 adcroft 1.1 %
1706 heimbach 1.4 \item
1707     \fbox{
1708     \begin{minipage}{12cm}
1709     {\it ctrl\_unpack, ctrl\_pack}:
1710     \end{minipage}
1711     }
1712     \\
1713     %
1714     \begin{itemize}
1715     %
1716     \item {\bf vector\_ctrl}: the control vector \\
1717 cnh 1.7 At the very beginning of the model initialization,
1718 heimbach 1.4 the updated compressed control vector is read (or initialised)
1719     and distributed to 2-dim. and 3-dim. control variable fields.
1720     %
1721     \item {\bf vector\_grad}: the gradient vector \\
1722     At the very end of the adjoint integration,
1723     the 2-dim. and 3-dim. adjoint variables are read,
1724     compressed to a single vector and written to file.
1725 adcroft 1.1 %
1726     \end{itemize}
1727     %
1728     \item
1729     \fbox{
1730     \begin{minipage}{12cm}
1731     {\it addummy\_in\_stepping}:
1732     \end{minipage}
1733     }
1734     \\
1735     In addition to writing the gradient at the end of the
1736 heimbach 1.4 forward/adjoint integration, many more adjoint variables
1737     of the model state
1738     at intermediate times can be written using S/R
1739 adcroft 1.1 {\it addummy\_in\_stepping}.
1740     This routine is part of the adjoint support package
1741     {\it pkg/autodiff} (cf.f. below).
1742     To be part of the adjoint code, the corresponding S/R
1743     {\it dummy\_in\_stepping} has to be called in the forward
1744     model (S/R {\it the\_main\_loop}) at the appropriate place.
1745    
1746     {\it dummy\_in\_stepping} is essentially empty,
1747     the corresponding adjoint routine is hand-written rather
1748     than generated automatically.
1749     Appropriate flow directives ({\it dummy\_in\_stepping.flow})
1750     ensure that TAMC does not automatically
1751     generate {\it addummy\_in\_stepping} by trying to differentiate
1752 heimbach 1.4 {\it dummy\_in\_stepping}, but instead refers to
1753     the hand-written routine.
1754 adcroft 1.1
1755     {\it dummy\_in\_stepping} is called in the forward code
1756     at the beginning of each
1757     timestep, before the call to {\it dynamics}, thus ensuring
1758     that {\it addummy\_in\_stepping} is called at the end of
1759     each timestep in the adjoint calculation, after the call to
1760     {\it addynamics}.
1761    
1762     {\it addummy\_in\_stepping} includes the header files
1763 heimbach 1.4 {\it adcommon.h}.
1764     This header file is also hand-written. It contains
1765     the common blocks
1766     {\bf /addynvars\_r/}, {\bf /addynvars\_cd/},
1767     {\bf /addynvars\_diffkr/}, {\bf /addynvars\_kapgm/},
1768 adcroft 1.1 {\bf /adtr1\_r/}, {\bf /adffields/},
1769     which have been extracted from the adjoint code to enable
1770     access to the adjoint variables.
1771     %
1772     \end{itemize}
1773    
1774    
1775     \subsubsection{Control variable handling for
1776     optimization applications}
1777    
1778     In optimization mode the cost function $ {\cal J}(u) $ is sought
1779     to be minimized with respect to a set of control variables
1780     $ \delta {\cal J} \, = \, 0 $, in an iterative manner.
1781     The gradient $ \nabla _{u}{\cal J} |_{u_{[k]}} $ together
1782     with the value of the cost function itself $ {\cal J}(u_{[k]}) $
1783     at iteration step $ k $ serve
1784     as input to a minimization routine (e.g. quasi-Newton method,
1785 heimbach 1.9 conjugate gradient, ... \cite{gil-lem:89})
1786 heimbach 1.4 to compute an update in the
1787 adcroft 1.1 control variable for iteration step $k+1$
1788     \[
1789     u_{[k+1]} \, = \, u_{[0]} \, + \, \Delta u_{[k+1]}
1790     \quad \mbox{satisfying} \quad
1791     {\cal J} \left( u_{[k+1]} \right) \, < \, {\cal J} \left( u_{[k]} \right)
1792     \]
1793     $ u_{[k+1]} $ then serves as input for a forward/adjoint run
1794     to determine $ {\cal J} $ and $ \nabla _{u}{\cal J} $ at iteration step
1795     $ k+1 $.
1796     Tab. \ref{???} sketches the flow between forward/adjoint model
1797     and the minimization routine.
1798    
1799     \begin{eqnarray*}
1800 heimbach 1.4 \scriptsize
1801 adcroft 1.1 \begin{array}{ccccc}
1802     u_{[0]} \,\, , \,\, \Delta u_{[k]} & ~ & ~ & ~ & ~ \\
1803     {\Big\downarrow}
1804     & ~ & ~ & ~ & ~ \\
1805     ~ & ~ & ~ & ~ & ~ \\
1806     \hline
1807     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1808     \multicolumn{1}{|c}{
1809     u_{[k]} = u_{[0]} + \Delta u_{[k]}} &
1810     \stackrel{\bf forward}{\bf \longrightarrow} &
1811     v_{[k]} = M \left( u_{[k]} \right) &
1812     \stackrel{\bf forward}{\bf \longrightarrow} &
1813     \multicolumn{1}{c|}{
1814     {\cal J}_{[k]} = {\cal J} \left( M \left( u_{[k]} \right) \right)} \\
1815     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1816     \hline
1817 heimbach 1.4 \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1818     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{{\Big\downarrow}} \\
1819     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1820 adcroft 1.1 \hline
1821     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1822     \multicolumn{1}{|c}{
1823     \nabla_u {\cal J}_{[k]} (\delta {\cal J}) =
1824 heimbach 1.4 T^{\ast} \cdot \nabla_v {\cal J} |_{v_{[k]}} (\delta {\cal J})} &
1825 adcroft 1.1 \stackrel{\bf adjoint}{\mathbf \longleftarrow} &
1826     ad \, v_{[k]} (\delta {\cal J}) =
1827     \nabla_v {\cal J} |_{v_{[k]}} (\delta {\cal J}) &
1828     \stackrel{\bf adjoint}{\mathbf \longleftarrow} &
1829     \multicolumn{1}{c|}{ ad \, {\cal J} = \delta {\cal J}} \\
1830     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1831     \hline
1832     ~ & ~ & ~ & ~ & ~ \\
1833 heimbach 1.4 \hspace*{15ex}{\Bigg\downarrow}
1834     \quad {\cal J}_{[k]}, \quad \nabla_u {\cal J}_{[k]}
1835     & ~ & ~ & ~ & ~ \\
1836 adcroft 1.1 ~ & ~ & ~ & ~ & ~ \\
1837     \hline
1838     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1839     \multicolumn{1}{|c}{
1840     {\cal J}_{[k]} \,\, , \,\, \nabla_u {\cal J}_{[k]}} &
1841     {\mathbf \longrightarrow} & \text{\bf minimisation} &
1842     {\mathbf \longrightarrow} &
1843     \multicolumn{1}{c|}{ \Delta u_{[k+1]}} \\
1844     \multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\
1845     \hline
1846     ~ & ~ & ~ & ~ & ~ \\
1847     ~ & ~ & ~ & ~ & \Big\downarrow \\
1848     ~ & ~ & ~ & ~ & \Delta u_{[k+1]} \\
1849     \end{array}
1850     \end{eqnarray*}
1851    
1852     The routines {\it ctrl\_unpack} and {\it ctrl\_pack} provide
1853     the link between the model and the minimization routine.
1854     As described in Section \ref{???}
1855     the {\it unpack} and {\it pack} routines read and write
1856     control and gradient {\it vectors} which are compressed
1857     to contain only wet points, in addition to the full
1858     2-dim. and 3-dim. fields.
1859     The corresponding I/O flow looks as follows:
1860    
1861     \vspace*{0.5cm}
1862    
1863 heimbach 1.4 {\scriptsize
1864 adcroft 1.1 \begin{tabular}{ccccc}
1865     {\bf vector\_ctrl\_$<$k$>$ } & ~ & ~ & ~ & ~ \\
1866     {\big\downarrow} & ~ & ~ & ~ & ~ \\
1867     \cline{1-1}
1868     \multicolumn{1}{|c|}{\it ctrl\_unpack} & ~ & ~ & ~ & ~ \\
1869     \cline{1-1}
1870     {\big\downarrow} & ~ & ~ & ~ & ~ \\
1871     \cline{3-3}
1872     \multicolumn{1}{l}{\bf xx\_theta0...$<$k$>$} & ~ &
1873     \multicolumn{1}{|c|}{~} & ~ & ~ \\
1874 heimbach 1.4 \multicolumn{1}{l}{\bf xx\_salt0...$<$k$>$} &
1875     $\stackrel{\mbox{read}}{\longrightarrow}$ &
1876 adcroft 1.1 \multicolumn{1}{|c|}{forward integration} & ~ & ~ \\
1877     \multicolumn{1}{l}{\bf \vdots} & ~ & \multicolumn{1}{|c|}{~}
1878     & ~ & ~ \\
1879     \cline{3-3}
1880 heimbach 1.4 ~ & ~ & $\downarrow$ & ~ & ~ \\
1881 adcroft 1.1 \cline{3-3}
1882     ~ & ~ &
1883     \multicolumn{1}{|c|}{~} & ~ &
1884     \multicolumn{1}{l}{\bf adxx\_theta0...$<$k$>$} \\
1885     ~ & ~ & \multicolumn{1}{|c|}{adjoint integration} &
1886 heimbach 1.4 $\stackrel{\mbox{write}}{\longrightarrow}$ &
1887 adcroft 1.1 \multicolumn{1}{l}{\bf adxx\_salt0...$<$k$>$} \\
1888     ~ & ~ & \multicolumn{1}{|c|}{~}
1889     & ~ & \multicolumn{1}{l}{\bf \vdots} \\
1890     \cline{3-3}
1891     ~ & ~ & ~ & ~ & {\big\downarrow} \\
1892     \cline{5-5}
1893     ~ & ~ & ~ & ~ & \multicolumn{1}{|c|}{\it ctrl\_pack} \\
1894     \cline{5-5}
1895     ~ & ~ & ~ & ~ & {\big\downarrow} \\
1896     ~ & ~ & ~ & ~ & {\bf vector\_grad\_$<$k$>$ } \\
1897     \end{tabular}
1898 heimbach 1.4 }
1899 adcroft 1.1
1900     \vspace*{0.5cm}
1901    
1902    
1903 heimbach 1.4 {\it ctrl\_unpack} reads the updated control vector
1904 adcroft 1.1 {\bf vector\_ctrl\_$<$k$>$}.
1905     It distributes the different control variables to
1906     2-dim. and 3-dim. files {\it xx\_...$<$k$>$}.
1907 heimbach 1.4 At the start of the forward integration the control variables
1908     are read from {\it xx\_...$<$k$>$} and added to the
1909     field.
1910     Correspondingly, at the end of the adjoint integration
1911     the adjoint fields are written
1912 adcroft 1.1 to {\it adxx\_...$<$k$>$}, again via the active file routines.
1913 heimbach 1.4 Finally, {\it ctrl\_pack} collects all adjoint files
1914 adcroft 1.1 and writes them to the compressed vector file
1915     {\bf vector\_grad\_$<$k$>$}.
1916    
1917     \subsection{TLM and ADM generation via TAMC}
1918    
1919    
1920    
1921 heimbach 1.4 \subsection{Flow directives and adjoint support routines \label{section_flowdir}}
1922 adcroft 1.1
1923 heimbach 1.4 \subsection{Store directives and checkpointing \label{section_checkpointing}}
1924 adcroft 1.1
1925 heimbach 1.4 \subsection{Gradient checks \label{section_grdchk}}
1926 adcroft 1.1
1927     \subsection{Second derivative generation via TAMC}
1928    
1929     \section{Example of adjoint code}

  ViewVC Help
Powered by ViewVC 1.1.22