1 |
jmc |
1.23 |
% $Header: /u/gcmpack/manual/s_autodiff/text/doc_ad_2.tex,v 1.22 2010/08/27 13:09:40 jmc Exp $ |
2 |
heimbach |
1.2 |
% $Name: $ |
3 |
adcroft |
1.1 |
|
4 |
heimbach |
1.18 |
Author: Patrick Heimbach |
5 |
jmc |
1.23 |
\label{ask_the_author:doc_ad_2} |
6 |
heimbach |
1.18 |
|
7 |
adcroft |
1.1 |
{\sf Automatic differentiation} (AD), also referred to as algorithmic |
8 |
edhill |
1.20 |
(or, more loosely, computational) differentiation, involves |
9 |
|
|
automatically deriving code to calculate partial derivatives from an |
10 |
|
|
existing fully non-linear prognostic code. (see \cite{gri:00}). A |
11 |
|
|
software tool is used that parses and transforms source files |
12 |
|
|
according to a set of linguistic and mathematical rules. AD tools are |
13 |
|
|
like source-to-source translators in that they parse a program code as |
14 |
heimbach |
1.21 |
input and produce a new program code as output |
15 |
|
|
(we restrict our discussion to source-to-source tools, ignoring |
16 |
|
|
operator-overloading tools). However, unlike a |
17 |
edhill |
1.20 |
pure source-to-source translation, the output program represents a new |
18 |
|
|
algorithm, such as the evaluation of the Jacobian, the Hessian, or |
19 |
|
|
higher derivative operators. In principle, a variety of derived |
20 |
|
|
algorithms can be generated automatically in this way. |
21 |
|
|
|
22 |
|
|
MITgcm has been adapted for use with the Tangent linear and Adjoint |
23 |
|
|
Model Compiler (TAMC) and its successor TAF (Transformation of |
24 |
|
|
Algorithms in Fortran), developed by Ralf Giering (\cite{gie-kam:98}, |
25 |
|
|
\cite{gie:99,gie:00}). The first application of the adjoint of MITgcm |
26 |
|
|
for sensitivity studies has been published by \cite{maro-eta:99}. |
27 |
heimbach |
1.21 |
\cite{stam-etal:97,stam-etal:02} use MITgcm and its adjoint for ocean |
28 |
edhill |
1.20 |
state estimation studies. In the following we shall refer to TAMC and |
29 |
|
|
TAF synonymously, except were explicitly stated otherwise. |
30 |
|
|
|
31 |
heimbach |
1.21 |
As of mid-2007 we are also able to generate fairly efficient |
32 |
|
|
adjoint code of the MITgcm using a new, open-source AD tool, |
33 |
|
|
called OpenAD (see \cite{naum-etal:06,utke-etal:08}. |
34 |
|
|
This enables us for the first time to compare adjoint models |
35 |
|
|
generated from different AD tools, providing an additional |
36 |
|
|
accuracy check, complementary to finite-difference gradient checks. |
37 |
|
|
OpenAD and its application to MITgcm is described in detail |
38 |
|
|
in section \ref{sec_ad_openad}. |
39 |
|
|
|
40 |
|
|
The AD tool exploits the chain rule for computing the first derivative of a |
41 |
edhill |
1.20 |
function with respect to a set of input variables. Treating a given |
42 |
|
|
forward code as a composition of operations -- each line representing |
43 |
|
|
a compositional element, the chain rule is rigorously applied to the |
44 |
|
|
code, line by line. The resulting tangent linear or adjoint code, |
45 |
|
|
then, may be thought of as the composition in forward or reverse |
46 |
|
|
order, respectively, of the Jacobian matrices of the forward code's |
47 |
|
|
compositional elements. |
48 |
adcroft |
1.1 |
|
49 |
|
|
%********************************************************************** |
50 |
|
|
\section{Some basic algebra} |
51 |
|
|
\label{sec_ad_algebra} |
52 |
edhill |
1.17 |
\begin{rawhtml} |
53 |
|
|
<!-- CMIREDIR:sec_ad_algebra: --> |
54 |
|
|
\end{rawhtml} |
55 |
adcroft |
1.1 |
%********************************************************************** |
56 |
|
|
|
57 |
|
|
Let $ \cal{M} $ be a general nonlinear, model, i.e. a |
58 |
|
|
mapping from the $m$-dimensional space |
59 |
|
|
$U \subset I\!\!R^m$ of input variables |
60 |
|
|
$\vec{u}=(u_1,\ldots,u_m)$ |
61 |
|
|
(model parameters, initial conditions, boundary conditions |
62 |
|
|
such as forcing functions) to the $n$-dimensional space |
63 |
|
|
$V \subset I\!\!R^n$ of |
64 |
|
|
model output variable $\vec{v}=(v_1,\ldots,v_n)$ |
65 |
cnh |
1.7 |
(model state, model diagnostics, objective function, ...) |
66 |
adcroft |
1.1 |
under consideration, |
67 |
|
|
% |
68 |
|
|
\begin{equation} |
69 |
jmc |
1.23 |
\begin{aligned} |
70 |
adcroft |
1.1 |
{\cal M} \, : & \, U \,\, \longrightarrow \, V \\ |
71 |
|
|
~ & \, \vec{u} \,\, \longmapsto \, \vec{v} \, = \, |
72 |
|
|
{\cal M}(\vec{u}) |
73 |
|
|
\label{fulloperator} |
74 |
jmc |
1.23 |
\end{aligned} |
75 |
adcroft |
1.1 |
\end{equation} |
76 |
|
|
% |
77 |
|
|
The vectors $ \vec{u} \in U $ and $ v \in V $ may be represented w.r.t. |
78 |
|
|
some given basis vectors |
79 |
|
|
$ {\rm span} (U) = \{ {\vec{e}_i} \}_{i = 1, \ldots , m} $ and |
80 |
|
|
$ {\rm span} (V) = \{ {\vec{f}_j} \}_{j = 1, \ldots , n} $ as |
81 |
|
|
\[ |
82 |
|
|
\vec{u} \, = \, \sum_{i=1}^{m} u_i \, {\vec{e}_i}, |
83 |
|
|
\qquad |
84 |
|
|
\vec{v} \, = \, \sum_{j=1}^{n} v_j \, {\vec{f}_j} |
85 |
|
|
\] |
86 |
|
|
|
87 |
|
|
Two routes may be followed to determine the sensitivity of the |
88 |
|
|
output variable $\vec{v}$ to its input $\vec{u}$. |
89 |
|
|
|
90 |
|
|
\subsection{Forward or direct sensitivity} |
91 |
|
|
% |
92 |
|
|
Consider a perturbation to the input variables $\delta \vec{u}$ |
93 |
|
|
(typically a single component |
94 |
|
|
$\delta \vec{u} = \delta u_{i} \, {\vec{e}_{i}}$). |
95 |
|
|
Their effect on the output may be obtained via the linear |
96 |
|
|
approximation of the model $ {\cal M}$ in terms of its Jacobian matrix |
97 |
|
|
$ M $, evaluated in the point $u^{(0)}$ according to |
98 |
|
|
% |
99 |
|
|
\begin{equation} |
100 |
|
|
\delta \vec{v} \, = \, M |_{\vec{u}^{(0)}} \, \delta \vec{u} |
101 |
|
|
\label{tangent_linear} |
102 |
|
|
\end{equation} |
103 |
|
|
with resulting output perturbation $\delta \vec{v}$. |
104 |
|
|
In components |
105 |
|
|
$M_{j i} \, = \, \partial {\cal M}_{j} / \partial u_{i} $, |
106 |
|
|
it reads |
107 |
|
|
% |
108 |
|
|
\begin{equation} |
109 |
|
|
\delta v_{j} \, = \, \sum_{i} |
110 |
|
|
\left. \frac{\partial {\cal M}_{j}}{\partial u_{i}} \right|_{u^{(0)}} \, |
111 |
|
|
\delta u_{i} |
112 |
|
|
\label{jacobi_matrix} |
113 |
|
|
\end{equation} |
114 |
|
|
% |
115 |
|
|
Eq. (\ref{tangent_linear}) is the {\sf tangent linear model (TLM)}. |
116 |
|
|
In contrast to the full nonlinear model $ {\cal M} $, the operator |
117 |
|
|
$ M $ is just a matrix |
118 |
|
|
which can readily be used to find the forward sensitivity of $\vec{v}$ to |
119 |
|
|
perturbations in $u$, |
120 |
heimbach |
1.4 |
but if there are very many input variables $(\gg O(10^{6})$ for |
121 |
adcroft |
1.1 |
large-scale oceanographic application), it quickly becomes |
122 |
|
|
prohibitive to proceed directly as in (\ref{tangent_linear}), |
123 |
|
|
if the impact of each component $ {\bf e_{i}} $ is to be assessed. |
124 |
|
|
|
125 |
|
|
\subsection{Reverse or adjoint sensitivity} |
126 |
|
|
% |
127 |
|
|
Let us consider the special case of a |
128 |
|
|
scalar objective function ${\cal J}(\vec{v})$ of the model output (e.g. |
129 |
|
|
the total meridional heat transport, |
130 |
|
|
the total uptake of $CO_{2}$ in the Southern |
131 |
|
|
Ocean over a time interval, |
132 |
|
|
or a measure of some model-to-data misfit) |
133 |
|
|
% |
134 |
|
|
\begin{eqnarray} |
135 |
|
|
\begin{array}{cccccc} |
136 |
|
|
{\cal J} \, : & U & |
137 |
|
|
\longrightarrow & V & |
138 |
|
|
\longrightarrow & I \!\! R \\ |
139 |
|
|
~ & \vec{u} & \longmapsto & \vec{v}={\cal M}(\vec{u}) & |
140 |
|
|
\longmapsto & {\cal J}(\vec{u}) = {\cal J}({\cal M}(\vec{u})) |
141 |
|
|
\end{array} |
142 |
|
|
\label{compo} |
143 |
|
|
\end{eqnarray} |
144 |
|
|
% |
145 |
heimbach |
1.4 |
The perturbation of $ {\cal J} $ around a fixed point $ {\cal J}_0 $, |
146 |
adcroft |
1.1 |
\[ |
147 |
heimbach |
1.4 |
{\cal J} \, = \, {\cal J}_0 \, + \, \delta {\cal J} |
148 |
adcroft |
1.1 |
\] |
149 |
|
|
can be expressed in both bases of $ \vec{u} $ and $ \vec{v} $ |
150 |
|
|
w.r.t. their corresponding inner product |
151 |
|
|
$\left\langle \,\, , \,\, \right\rangle $ |
152 |
|
|
% |
153 |
|
|
\begin{equation} |
154 |
jmc |
1.23 |
\begin{aligned} |
155 |
adcroft |
1.1 |
{\cal J} & = \, |
156 |
|
|
{\cal J} |_{\vec{u}^{(0)}} \, + \, |
157 |
|
|
\left\langle \, \nabla _{u}{\cal J}^T |_{\vec{u}^{(0)}} \, , \, \delta \vec{u} \, \right\rangle |
158 |
|
|
\, + \, O(\delta \vec{u}^2) \\ |
159 |
|
|
~ & = \, |
160 |
|
|
{\cal J} |_{\vec{v}^{(0)}} \, + \, |
161 |
|
|
\left\langle \, \nabla _{v}{\cal J}^T |_{\vec{v}^{(0)}} \, , \, \delta \vec{v} \, \right\rangle |
162 |
|
|
\, + \, O(\delta \vec{v}^2) |
163 |
jmc |
1.23 |
\end{aligned} |
164 |
adcroft |
1.1 |
\label{deljidentity} |
165 |
|
|
\end{equation} |
166 |
|
|
% |
167 |
heimbach |
1.2 |
(note, that the gradient $ \nabla f $ is a co-vector, therefore |
168 |
adcroft |
1.1 |
its transpose is required in the above inner product). |
169 |
|
|
Then, using the representation of |
170 |
|
|
$ \delta {\cal J} = |
171 |
|
|
\left\langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \right\rangle $, |
172 |
|
|
the definition |
173 |
|
|
of an adjoint operator $ A^{\ast} $ of a given operator $ A $, |
174 |
|
|
\[ |
175 |
|
|
\left\langle \, A^{\ast} \vec{x} \, , \, \vec{y} \, \right\rangle = |
176 |
|
|
\left\langle \, \vec{x} \, , \, A \vec{y} \, \right\rangle |
177 |
|
|
\] |
178 |
|
|
which for finite-dimensional vector spaces is just the |
179 |
|
|
transpose of $ A $, |
180 |
|
|
\[ |
181 |
|
|
A^{\ast} \, = \, A^T |
182 |
|
|
\] |
183 |
heimbach |
1.4 |
and from eq. (\ref{tangent_linear}), (\ref{deljidentity}), |
184 |
|
|
we note that |
185 |
adcroft |
1.1 |
(omitting $|$'s): |
186 |
|
|
% |
187 |
|
|
\begin{equation} |
188 |
|
|
\delta {\cal J} |
189 |
|
|
\, = \, |
190 |
|
|
\left\langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \right\rangle |
191 |
|
|
\, = \, |
192 |
|
|
\left\langle \, \nabla _{v}{\cal J}^T \, , \, M \, \delta \vec{u} \, \right\rangle |
193 |
|
|
\, = \, |
194 |
|
|
\left\langle \, M^T \, \nabla _{v}{\cal J}^T \, , \, |
195 |
|
|
\delta \vec{u} \, \right\rangle |
196 |
|
|
\label{inner} |
197 |
|
|
\end{equation} |
198 |
|
|
% |
199 |
|
|
With the identity (\ref{deljidentity}), we then find that |
200 |
|
|
the gradient $ \nabla _{u}{\cal J} $ can be readily inferred by |
201 |
|
|
invoking the adjoint $ M^{\ast } $ of the tangent linear model $ M $ |
202 |
|
|
% |
203 |
|
|
\begin{equation} |
204 |
jmc |
1.23 |
\begin{aligned} |
205 |
adcroft |
1.1 |
\nabla _{u}{\cal J}^T |_{\vec{u}} & |
206 |
|
|
= \, M^T |_{\vec{u}} \cdot \nabla _{v}{\cal J}^T |_{\vec{v}} \\ |
207 |
|
|
~ & = \, M^T |_{\vec{u}} \cdot \delta \vec{v}^{\ast} \\ |
208 |
|
|
~ & = \, \delta \vec{u}^{\ast} |
209 |
jmc |
1.23 |
\end{aligned} |
210 |
adcroft |
1.1 |
\label{adjoint} |
211 |
|
|
\end{equation} |
212 |
|
|
% |
213 |
|
|
Eq. (\ref{adjoint}) is the {\sf adjoint model (ADM)}, |
214 |
|
|
in which $M^T$ is the adjoint (here, the transpose) of the |
215 |
|
|
tangent linear operator $M$, $ \delta \vec{v}^{\ast} $ |
216 |
|
|
the adjoint variable of the model state $ \vec{v} $, and |
217 |
|
|
$ \delta \vec{u}^{\ast} $ the adjoint variable of the control variable $ \vec{u} $. |
218 |
|
|
|
219 |
|
|
The {\sf reverse} nature of the adjoint calculation can be readily |
220 |
heimbach |
1.4 |
seen as follows. |
221 |
|
|
Consider a model integration which consists of $ \Lambda $ |
222 |
|
|
consecutive operations |
223 |
|
|
$ {\cal M}_{\Lambda} ( {\cal M}_{\Lambda-1} ( |
224 |
|
|
...... ( {\cal M}_{\lambda} ( |
225 |
|
|
...... |
226 |
|
|
( {\cal M}_{1} ( {\cal M}_{0}(\vec{u}) )))) $, |
227 |
|
|
where the ${\cal M}$'s could be the elementary steps, i.e. single lines |
228 |
|
|
in the code of the model, or successive time steps of the |
229 |
|
|
model integration, |
230 |
|
|
starting at step 0 and moving up to step $\Lambda$, with intermediate |
231 |
|
|
${\cal M}_{\lambda} (\vec{u}) = \vec{v}^{(\lambda+1)}$ and final |
232 |
|
|
${\cal M}_{\Lambda} (\vec{u}) = \vec{v}^{(\Lambda+1)} = \vec{v}$. |
233 |
cnh |
1.7 |
Let ${\cal J}$ be a cost function which explicitly depends on the |
234 |
heimbach |
1.4 |
final state $\vec{v}$ only |
235 |
|
|
(this restriction is for clarity reasons only). |
236 |
|
|
% |
237 |
|
|
${\cal J}(u)$ may be decomposed according to: |
238 |
adcroft |
1.1 |
% |
239 |
|
|
\begin{equation} |
240 |
|
|
{\cal J}({\cal M}(\vec{u})) \, = \, |
241 |
|
|
{\cal J} ( {\cal M}_{\Lambda} ( {\cal M}_{\Lambda-1} ( |
242 |
|
|
...... ( {\cal M}_{\lambda} ( |
243 |
|
|
...... |
244 |
|
|
( {\cal M}_{1} ( {\cal M}_{0}(\vec{u}) ))))) |
245 |
|
|
\label{compos} |
246 |
|
|
\end{equation} |
247 |
|
|
% |
248 |
heimbach |
1.4 |
Then, according to the chain rule, the forward calculation reads, |
249 |
|
|
in terms of the Jacobi matrices |
250 |
adcroft |
1.1 |
(we've omitted the $ | $'s which, nevertheless are important |
251 |
|
|
to the aspect of {\it tangent} linearity; |
252 |
heimbach |
1.4 |
note also that by definition |
253 |
adcroft |
1.1 |
$ \langle \, \nabla _{v}{\cal J}^T \, , \, \delta \vec{v} \, \rangle |
254 |
|
|
= \nabla_v {\cal J} \cdot \delta \vec{v} $ ) |
255 |
|
|
% |
256 |
|
|
\begin{equation} |
257 |
jmc |
1.23 |
\begin{aligned} |
258 |
adcroft |
1.1 |
\nabla_v {\cal J} (M(\delta \vec{u})) & = \, |
259 |
|
|
\nabla_v {\cal J} \cdot M_{\Lambda} |
260 |
|
|
\cdot ...... \cdot M_{\lambda} \cdot ...... \cdot |
261 |
|
|
M_{1} \cdot M_{0} \cdot \delta \vec{u} \\ |
262 |
|
|
~ & = \, \nabla_v {\cal J} \cdot \delta \vec{v} \\ |
263 |
jmc |
1.23 |
\end{aligned} |
264 |
adcroft |
1.1 |
\label{forward} |
265 |
|
|
\end{equation} |
266 |
|
|
% |
267 |
|
|
whereas in reverse mode we have |
268 |
|
|
% |
269 |
|
|
\begin{equation} |
270 |
|
|
\boxed{ |
271 |
jmc |
1.23 |
\begin{aligned} |
272 |
adcroft |
1.1 |
M^T ( \nabla_v {\cal J}^T) & = \, |
273 |
|
|
M_{0}^T \cdot M_{1}^T |
274 |
|
|
\cdot ...... \cdot M_{\lambda}^T \cdot ...... \cdot |
275 |
|
|
M_{\Lambda}^T \cdot \nabla_v {\cal J}^T \\ |
276 |
|
|
~ & = \, M_{0}^T \cdot M_{1}^T |
277 |
|
|
\cdot ...... \cdot |
278 |
|
|
\nabla_{v^{(\lambda)}} {\cal J}^T \\ |
279 |
|
|
~ & = \, \nabla_u {\cal J}^T |
280 |
jmc |
1.23 |
\end{aligned} |
281 |
adcroft |
1.1 |
} |
282 |
|
|
\label{reverse} |
283 |
|
|
\end{equation} |
284 |
|
|
% |
285 |
|
|
clearly expressing the reverse nature of the calculation. |
286 |
|
|
Eq. (\ref{reverse}) is at the heart of automatic adjoint compilers. |
287 |
heimbach |
1.4 |
If the intermediate steps $\lambda$ in |
288 |
adcroft |
1.1 |
eqn. (\ref{compos}) -- (\ref{reverse}) |
289 |
heimbach |
1.4 |
represent the model state (forward or adjoint) at each |
290 |
|
|
intermediate time step as noted above, then correspondingly, |
291 |
|
|
$ M^T (\delta \vec{v}^{(\lambda) \, \ast}) = |
292 |
|
|
\delta \vec{v}^{(\lambda-1) \, \ast} $ for the adjoint variables. |
293 |
|
|
It thus becomes evident that the adjoint calculation also |
294 |
|
|
yields the adjoint of each model state component |
295 |
|
|
$ \vec{v}^{(\lambda)} $ at each intermediate step $ \lambda $, namely |
296 |
adcroft |
1.1 |
% |
297 |
|
|
\begin{equation} |
298 |
|
|
\boxed{ |
299 |
jmc |
1.23 |
\begin{aligned} |
300 |
adcroft |
1.1 |
\nabla_{v^{(\lambda)}} {\cal J}^T |_{\vec{v}^{(\lambda)}} |
301 |
|
|
& = \, |
302 |
|
|
M_{\lambda}^T |_{\vec{v}^{(\lambda)}} \cdot ...... \cdot |
303 |
|
|
M_{\Lambda}^T |_{\vec{v}^{(\lambda)}} \cdot \delta \vec{v}^{\ast} \\ |
304 |
|
|
~ & = \, \delta \vec{v}^{(\lambda) \, \ast} |
305 |
jmc |
1.23 |
\end{aligned} |
306 |
adcroft |
1.1 |
} |
307 |
|
|
\end{equation} |
308 |
|
|
% |
309 |
|
|
in close analogy to eq. (\ref{adjoint}) |
310 |
|
|
We note in passing that that the $\delta \vec{v}^{(\lambda) \, \ast}$ |
311 |
heimbach |
1.4 |
are the Lagrange multipliers of the model equations which determine |
312 |
|
|
$ \vec{v}^{(\lambda)}$. |
313 |
adcroft |
1.1 |
|
314 |
cnh |
1.7 |
In components, eq. (\ref{adjoint}) reads as follows. |
315 |
adcroft |
1.1 |
Let |
316 |
|
|
\[ |
317 |
|
|
\begin{array}{rclcrcl} |
318 |
|
|
\delta \vec{u} & = & |
319 |
|
|
\left( \delta u_1,\ldots, \delta u_m \right)^T , & \qquad & |
320 |
|
|
\delta \vec{u}^{\ast} \,\, = \,\, \nabla_u {\cal J}^T & = & |
321 |
|
|
\left( |
322 |
|
|
\frac{\partial {\cal J}}{\partial u_1},\ldots, |
323 |
|
|
\frac{\partial {\cal J}}{\partial u_m} |
324 |
|
|
\right)^T \\ |
325 |
|
|
\delta \vec{v} & = & |
326 |
|
|
\left( \delta v_1,\ldots, \delta u_n \right)^T , & \qquad & |
327 |
|
|
\delta \vec{v}^{\ast} \,\, = \,\, \nabla_v {\cal J}^T & = & |
328 |
|
|
\left( |
329 |
|
|
\frac{\partial {\cal J}}{\partial v_1},\ldots, |
330 |
|
|
\frac{\partial {\cal J}}{\partial v_n} |
331 |
|
|
\right)^T \\ |
332 |
|
|
\end{array} |
333 |
|
|
\] |
334 |
|
|
denote the perturbations in $\vec{u}$ and $\vec{v}$, respectively, |
335 |
cnh |
1.7 |
and their adjoint variables; |
336 |
adcroft |
1.1 |
further |
337 |
|
|
\[ |
338 |
|
|
M \, = \, \left( |
339 |
|
|
\begin{array}{ccc} |
340 |
|
|
\frac{\partial {\cal M}_1}{\partial u_1} & \ldots & |
341 |
|
|
\frac{\partial {\cal M}_1}{\partial u_m} \\ |
342 |
|
|
\vdots & ~ & \vdots \\ |
343 |
|
|
\frac{\partial {\cal M}_n}{\partial u_1} & \ldots & |
344 |
|
|
\frac{\partial {\cal M}_n}{\partial u_m} \\ |
345 |
|
|
\end{array} |
346 |
|
|
\right) |
347 |
|
|
\] |
348 |
|
|
is the Jacobi matrix of $ {\cal M} $ |
349 |
|
|
(an $ n \times m $ matrix) |
350 |
|
|
such that $ \delta \vec{v} = M \cdot \delta \vec{u} $, or |
351 |
|
|
\[ |
352 |
|
|
\delta v_{j} |
353 |
|
|
\, = \, \sum_{i=1}^m M_{ji} \, \delta u_{i} |
354 |
|
|
\, = \, \sum_{i=1}^m \, \frac{\partial {\cal M}_{j}}{\partial u_{i}} |
355 |
|
|
\delta u_{i} |
356 |
|
|
\] |
357 |
|
|
% |
358 |
|
|
Then eq. (\ref{adjoint}) takes the form |
359 |
|
|
\[ |
360 |
|
|
\delta u_{i}^{\ast} |
361 |
|
|
\, = \, \sum_{j=1}^n M_{ji} \, \delta v_{j}^{\ast} |
362 |
|
|
\, = \, \sum_{j=1}^n \, \frac{\partial {\cal M}_{j}}{\partial u_{i}} |
363 |
|
|
\delta v_{j}^{\ast} |
364 |
|
|
\] |
365 |
|
|
% |
366 |
|
|
or |
367 |
|
|
% |
368 |
|
|
\[ |
369 |
|
|
\left( |
370 |
|
|
\begin{array}{c} |
371 |
|
|
\left. \frac{\partial}{\partial u_1} {\cal J} \right|_{\vec{u}^{(0)}} \\ |
372 |
|
|
\vdots \\ |
373 |
|
|
\left. \frac{\partial}{\partial u_m} {\cal J} \right|_{\vec{u}^{(0)}} \\ |
374 |
|
|
\end{array} |
375 |
|
|
\right) |
376 |
|
|
\, = \, |
377 |
|
|
\left( |
378 |
|
|
\begin{array}{ccc} |
379 |
|
|
\left. \frac{\partial {\cal M}_1}{\partial u_1} \right|_{\vec{u}^{(0)}} |
380 |
|
|
& \ldots & |
381 |
|
|
\left. \frac{\partial {\cal M}_n}{\partial u_1} \right|_{\vec{u}^{(0)}} \\ |
382 |
|
|
\vdots & ~ & \vdots \\ |
383 |
|
|
\left. \frac{\partial {\cal M}_1}{\partial u_m} \right|_{\vec{u}^{(0)}} |
384 |
|
|
& \ldots & |
385 |
|
|
\left. \frac{\partial {\cal M}_n}{\partial u_m} \right|_{\vec{u}^{(0)}} \\ |
386 |
|
|
\end{array} |
387 |
|
|
\right) |
388 |
|
|
\cdot |
389 |
|
|
\left( |
390 |
|
|
\begin{array}{c} |
391 |
|
|
\left. \frac{\partial}{\partial v_1} {\cal J} \right|_{\vec{v}} \\ |
392 |
|
|
\vdots \\ |
393 |
|
|
\left. \frac{\partial}{\partial v_n} {\cal J} \right|_{\vec{v}} \\ |
394 |
|
|
\end{array} |
395 |
|
|
\right) |
396 |
|
|
\] |
397 |
|
|
% |
398 |
|
|
Furthermore, the adjoint $ \delta v^{(\lambda) \, \ast} $ |
399 |
|
|
of any intermediate state $ v^{(\lambda)} $ |
400 |
|
|
may be obtained, using the intermediate Jacobian |
401 |
|
|
(an $ n_{\lambda+1} \times n_{\lambda} $ matrix) |
402 |
|
|
% |
403 |
|
|
\[ |
404 |
|
|
M_{\lambda} \, = \, |
405 |
|
|
\left( |
406 |
|
|
\begin{array}{ccc} |
407 |
|
|
\frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_1} |
408 |
|
|
& \ldots & |
409 |
|
|
\frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_{n_{\lambda}}} \\ |
410 |
|
|
\vdots & ~ & \vdots \\ |
411 |
|
|
\frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_1} |
412 |
|
|
& \ldots & |
413 |
|
|
\frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_{n_{\lambda}}} \\ |
414 |
|
|
\end{array} |
415 |
|
|
\right) |
416 |
|
|
\] |
417 |
|
|
% |
418 |
|
|
and the shorthand notation for the adjoint variables |
419 |
|
|
$ \delta v^{(\lambda) \, \ast}_{j} = \frac{\partial}{\partial v^{(\lambda)}_{j}} |
420 |
|
|
{\cal J}^T $, $ j = 1, \ldots , n_{\lambda} $, |
421 |
|
|
for intermediate components, yielding |
422 |
jmc |
1.23 |
{\small |
423 |
heimbach |
1.4 |
\begin{equation} |
424 |
jmc |
1.23 |
\begin{aligned} |
425 |
adcroft |
1.1 |
\left( |
426 |
|
|
\begin{array}{c} |
427 |
|
|
\delta v^{(\lambda) \, \ast}_1 \\ |
428 |
|
|
\vdots \\ |
429 |
|
|
\delta v^{(\lambda) \, \ast}_{n_{\lambda}} \\ |
430 |
|
|
\end{array} |
431 |
|
|
\right) |
432 |
heimbach |
1.4 |
\, = & |
433 |
adcroft |
1.1 |
\left( |
434 |
|
|
\begin{array}{ccc} |
435 |
|
|
\frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_1} |
436 |
heimbach |
1.4 |
& \ldots \,\, \ldots & |
437 |
adcroft |
1.1 |
\frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_1} \\ |
438 |
|
|
\vdots & ~ & \vdots \\ |
439 |
|
|
\frac{\partial ({\cal M}_{\lambda})_1}{\partial v^{(\lambda)}_{n_{\lambda}}} |
440 |
heimbach |
1.4 |
& \ldots \,\, \ldots & |
441 |
adcroft |
1.1 |
\frac{\partial ({\cal M}_{\lambda})_{n_{\lambda+1}}}{\partial v^{(\lambda)}_{n_{\lambda}}} \\ |
442 |
|
|
\end{array} |
443 |
|
|
\right) |
444 |
heimbach |
1.4 |
\cdot |
445 |
adcroft |
1.1 |
% |
446 |
heimbach |
1.4 |
\\ ~ & ~ |
447 |
|
|
\\ ~ & |
448 |
adcroft |
1.1 |
% |
449 |
|
|
\left( |
450 |
|
|
\begin{array}{ccc} |
451 |
|
|
\frac{\partial ({\cal M}_{\lambda+1})_1}{\partial v^{(\lambda+1)}_1} |
452 |
|
|
& \ldots & |
453 |
|
|
\frac{\partial ({\cal M}_{\lambda+1})_{n_{\lambda+2}}}{\partial v^{(\lambda+1)}_1} \\ |
454 |
|
|
\vdots & ~ & \vdots \\ |
455 |
|
|
\vdots & ~ & \vdots \\ |
456 |
|
|
\frac{\partial ({\cal M}_{\lambda+1})_1}{\partial v^{(\lambda+1)}_{n_{\lambda+1}}} |
457 |
|
|
& \ldots & |
458 |
|
|
\frac{\partial ({\cal M}_{\lambda+1})_{n_{\lambda+2}}}{\partial v^{(\lambda+1)}_{n_{\lambda+1}}} \\ |
459 |
|
|
\end{array} |
460 |
|
|
\right) |
461 |
heimbach |
1.4 |
\cdot \, \ldots \, \cdot |
462 |
adcroft |
1.1 |
\left( |
463 |
|
|
\begin{array}{c} |
464 |
|
|
\delta v^{\ast}_1 \\ |
465 |
|
|
\vdots \\ |
466 |
|
|
\delta v^{\ast}_{n} \\ |
467 |
|
|
\end{array} |
468 |
|
|
\right) |
469 |
jmc |
1.23 |
\end{aligned} |
470 |
heimbach |
1.4 |
\end{equation} |
471 |
jmc |
1.23 |
} |
472 |
adcroft |
1.1 |
|
473 |
|
|
Eq. (\ref{forward}) and (\ref{reverse}) are perhaps clearest in |
474 |
|
|
showing the advantage of the reverse over the forward mode |
475 |
|
|
if the gradient $\nabla _{u}{\cal J}$, i.e. the sensitivity of the |
476 |
|
|
cost function $ {\cal J} $ with respect to {\it all} input |
477 |
|
|
variables $u$ |
478 |
|
|
(or the sensitivity of the cost function with respect to |
479 |
|
|
{\it all} intermediate states $ \vec{v}^{(\lambda)} $) are sought. |
480 |
|
|
In order to be able to solve for each component of the gradient |
481 |
|
|
$ \partial {\cal J} / \partial u_{i} $ in (\ref{forward}) |
482 |
cnh |
1.7 |
a forward calculation has to be performed for each component separately, |
483 |
adcroft |
1.1 |
i.e. $ \delta \vec{u} = \delta u_{i} {\vec{e}_{i}} $ |
484 |
|
|
for the $i$-th forward calculation. |
485 |
|
|
Then, (\ref{forward}) represents the |
486 |
|
|
projection of $ \nabla_u {\cal J} $ onto the $i$-th component. |
487 |
|
|
The full gradient is retrieved from the $ m $ forward calculations. |
488 |
|
|
In contrast, eq. (\ref{reverse}) yields the full |
489 |
|
|
gradient $\nabla _{u}{\cal J}$ (and all intermediate gradients |
490 |
|
|
$\nabla _{v^{(\lambda)}}{\cal J}$) within a single reverse calculation. |
491 |
|
|
|
492 |
heimbach |
1.4 |
Note, that if $ {\cal J} $ is a vector-valued function |
493 |
adcroft |
1.1 |
of dimension $ l > 1 $, |
494 |
|
|
eq. (\ref{reverse}) has to be modified according to |
495 |
|
|
\[ |
496 |
|
|
M^T \left( \nabla_v {\cal J}^T \left(\delta \vec{J}\right) \right) |
497 |
|
|
\, = \, |
498 |
|
|
\nabla_u {\cal J}^T \cdot \delta \vec{J} |
499 |
|
|
\] |
500 |
heimbach |
1.4 |
where now $ \delta \vec{J} \in I\!\!R^l $ is a vector of |
501 |
cnh |
1.7 |
dimension $ l $. |
502 |
adcroft |
1.1 |
In this case $ l $ reverse simulations have to be performed |
503 |
|
|
for each $ \delta J_{k}, \,\, k = 1, \ldots, l $. |
504 |
|
|
Then, the reverse mode is more efficient as long as |
505 |
|
|
$ l < n $, otherwise the forward mode is preferable. |
506 |
cnh |
1.7 |
Strictly, the reverse mode is called adjoint mode only for |
507 |
adcroft |
1.1 |
$ l = 1 $. |
508 |
|
|
|
509 |
|
|
A detailed analysis of the underlying numerical operations |
510 |
|
|
shows that the computation of $\nabla _{u}{\cal J}$ in this way |
511 |
|
|
requires about 2 to 5 times the computation of the cost function. |
512 |
|
|
Alternatively, the gradient vector could be approximated |
513 |
|
|
by finite differences, requiring $m$ computations |
514 |
|
|
of the perturbed cost function. |
515 |
|
|
|
516 |
|
|
To conclude we give two examples of commonly used types |
517 |
|
|
of cost functions: |
518 |
|
|
|
519 |
|
|
\paragraph{Example 1: |
520 |
|
|
$ {\cal J} = v_{j} (T) $} ~ \\ |
521 |
|
|
The cost function consists of the $j$-th component of the model state |
522 |
|
|
$ \vec{v} $ at time $T$. |
523 |
|
|
Then $ \nabla_v {\cal J}^T = {\vec{f}_{j}} $ is just the $j$-th |
524 |
|
|
unit vector. The $ \nabla_u {\cal J}^T $ |
525 |
|
|
is the projection of the adjoint |
526 |
|
|
operator onto the $j$-th component ${\bf f_{j}}$, |
527 |
|
|
\[ |
528 |
|
|
\nabla_u {\cal J}^T |
529 |
|
|
\, = \, M^T \cdot \nabla_v {\cal J}^T |
530 |
|
|
\, = \, \sum_{i} M^T_{ji} \, {\vec{e}_{i}} |
531 |
|
|
\] |
532 |
|
|
|
533 |
|
|
\paragraph{Example 2: |
534 |
|
|
$ {\cal J} = \langle \, {\cal H}(\vec{v}) - \vec{d} \, , |
535 |
|
|
\, {\cal H}(\vec{v}) - \vec{d} \, \rangle $} ~ \\ |
536 |
heimbach |
1.4 |
The cost function represents the quadratic model vs. data misfit. |
537 |
adcroft |
1.1 |
Here, $ \vec{d} $ is the data vector and $ {\cal H} $ represents the |
538 |
|
|
operator which maps the model state space onto the data space. |
539 |
|
|
Then, $ \nabla_v {\cal J} $ takes the form |
540 |
|
|
% |
541 |
|
|
\begin{equation*} |
542 |
jmc |
1.23 |
\begin{aligned} |
543 |
adcroft |
1.1 |
\nabla_v {\cal J}^T & = \, 2 \, \, H \cdot |
544 |
|
|
\left( \, {\cal H}(\vec{v}) - \vec{d} \, \right) \\ |
545 |
|
|
~ & = \, 2 \sum_{j} \left\{ \sum_k |
546 |
|
|
\frac{\partial {\cal H}_k}{\partial v_{j}} |
547 |
|
|
\left( {\cal H}_k (\vec{v}) - d_k \right) |
548 |
|
|
\right\} \, {\vec{f}_{j}} \\ |
549 |
jmc |
1.23 |
\end{aligned} |
550 |
adcroft |
1.1 |
\end{equation*} |
551 |
|
|
% |
552 |
|
|
where $H_{kj} = \partial {\cal H}_k / \partial v_{j} $ is the |
553 |
|
|
Jacobi matrix of the data projection operator. |
554 |
|
|
Thus, the gradient $ \nabla_u {\cal J} $ is given by the |
555 |
|
|
adjoint operator, |
556 |
|
|
driven by the model vs. data misfit: |
557 |
|
|
\[ |
558 |
|
|
\nabla_u {\cal J}^T \, = \, 2 \, M^T \cdot |
559 |
|
|
H \cdot \left( {\cal H}(\vec{v}) - \vec{d} \, \right) |
560 |
|
|
\] |
561 |
|
|
|
562 |
|
|
\subsection{Storing vs. recomputation in reverse mode} |
563 |
|
|
\label{checkpointing} |
564 |
|
|
|
565 |
|
|
We note an important aspect of the forward vs. reverse |
566 |
|
|
mode calculation. |
567 |
heimbach |
1.4 |
Because of the local character of the derivative |
568 |
|
|
(a derivative is defined w.r.t. a point along the trajectory), |
569 |
adcroft |
1.1 |
the intermediate results of the model trajectory |
570 |
|
|
$\vec{v}^{(\lambda+1)}={\cal M}_{\lambda}(v^{(\lambda)})$ |
571 |
heimbach |
1.15 |
may be required to evaluate the intermediate Jacobian |
572 |
adcroft |
1.1 |
$M_{\lambda}|_{\vec{v}^{(\lambda)}} \, \delta \vec{v}^{(\lambda)} $. |
573 |
heimbach |
1.15 |
This is the case e.g. for nonlinear expressions |
574 |
|
|
(momentum advection, nonlinear equation of state), state-dependent |
575 |
|
|
conditional statements (parameterization schemes). |
576 |
adcroft |
1.1 |
In the forward mode, the intermediate results are required |
577 |
|
|
in the same order as computed by the full forward model ${\cal M}$, |
578 |
heimbach |
1.4 |
but in the reverse mode they are required in the reverse order. |
579 |
adcroft |
1.1 |
Thus, in the reverse mode the trajectory of the forward model |
580 |
|
|
integration ${\cal M}$ has to be stored to be available in the reverse |
581 |
heimbach |
1.4 |
calculation. Alternatively, the complete model state up to the |
582 |
|
|
point of evaluation has to be recomputed whenever its value is required. |
583 |
adcroft |
1.1 |
|
584 |
|
|
A method to balance the amount of recomputations vs. |
585 |
|
|
storage requirements is called {\sf checkpointing} |
586 |
heimbach |
1.15 |
(e.g. \cite{gri:92}, \cite{res-eta:98}). |
587 |
adcroft |
1.6 |
It is depicted in \ref{fig:3levelcheck} for a 3-level checkpointing |
588 |
heimbach |
1.4 |
[as an example, we give explicit numbers for a 3-day |
589 |
adcroft |
1.1 |
integration with a 1-hourly timestep in square brackets]. |
590 |
|
|
\begin{itemize} |
591 |
|
|
% |
592 |
|
|
\item [$lev3$] |
593 |
|
|
In a first step, the model trajectory is subdivided into |
594 |
|
|
$ {n}^{lev3} $ subsections [$ {n}^{lev3} $=3 1-day intervals], |
595 |
|
|
with the label $lev3$ for this outermost loop. |
596 |
heimbach |
1.4 |
The model is then integrated along the full trajectory, |
597 |
heimbach |
1.15 |
and the model state stored to disk only at every $ k_{i}^{lev3} $-th timestep |
598 |
adcroft |
1.1 |
[i.e. 3 times, at |
599 |
|
|
$ i = 0,1,2 $ corresponding to $ k_{i}^{lev3} = 0, 24, 48 $]. |
600 |
heimbach |
1.15 |
In addition, the cost function is computed, if needed. |
601 |
adcroft |
1.1 |
% |
602 |
|
|
\item [$lev2$] |
603 |
heimbach |
1.4 |
In a second step each subsection itself is divided into |
604 |
heimbach |
1.15 |
$ {n}^{lev2} $ subsections |
605 |
adcroft |
1.1 |
[$ {n}^{lev2} $=4 6-hour intervals per subsection]. |
606 |
|
|
The model picks up at the last outermost dumped state |
607 |
heimbach |
1.4 |
$ v_{k_{n}^{lev3}} $ and is integrated forward in time along |
608 |
adcroft |
1.1 |
the last subsection, with the label $lev2$ for this |
609 |
|
|
intermediate loop. |
610 |
heimbach |
1.15 |
The model state is now stored to disk at every $ k_{i}^{lev2} $-th |
611 |
adcroft |
1.1 |
timestep |
612 |
|
|
[i.e. 4 times, at |
613 |
|
|
$ i = 0,1,2,3 $ corresponding to $ k_{i}^{lev2} = 48, 54, 60, 66 $]. |
614 |
|
|
% |
615 |
|
|
\item [$lev1$] |
616 |
heimbach |
1.4 |
Finally, the model picks up at the last intermediate dump state |
617 |
|
|
$ v_{k_{n}^{lev2}} $ and is integrated forward in time along |
618 |
heimbach |
1.15 |
the last subsection, with the label $lev1$ for this |
619 |
adcroft |
1.1 |
intermediate loop. |
620 |
heimbach |
1.15 |
Within this sub-subsection only, parts of the model state is stored |
621 |
|
|
to memory at every timestep |
622 |
adcroft |
1.1 |
[i.e. every hour $ i=0,...,5$ corresponding to |
623 |
|
|
$ k_{i}^{lev1} = 66, 67, \ldots, 71 $]. |
624 |
heimbach |
1.15 |
The final state $ v_n = v_{k_{n}^{lev1}} $ is reached |
625 |
|
|
and the model state of all preceding timesteps along the last |
626 |
|
|
innermost subsection are available, enabling integration backwards |
627 |
|
|
in time along the last subsection. |
628 |
|
|
The adjoint can thus be computed along this last |
629 |
|
|
subsection $k_{n}^{lev2}$. |
630 |
adcroft |
1.1 |
% |
631 |
|
|
\end{itemize} |
632 |
|
|
% |
633 |
|
|
This procedure is repeated consecutively for each previous |
634 |
heimbach |
1.15 |
subsection $k_{n-1}^{lev2}, \ldots, k_{1}^{lev2} $ |
635 |
adcroft |
1.1 |
carrying the adjoint computation to the initial time |
636 |
|
|
of the subsection $k_{n}^{lev3}$. |
637 |
|
|
Then, the procedure is repeated for the previous subsection |
638 |
|
|
$k_{n-1}^{lev3}$ |
639 |
|
|
carrying the adjoint computation to the initial time |
640 |
|
|
$k_{1}^{lev3}$. |
641 |
|
|
|
642 |
|
|
For the full model trajectory of |
643 |
|
|
$ n^{lev3} \cdot n^{lev2} \cdot n^{lev1} $ timesteps |
644 |
|
|
the required storing of the model state was significantly reduced to |
645 |
heimbach |
1.15 |
$ n^{lev2} + n^{lev3} $ to disk and roughly $ n^{lev1} $ to memory |
646 |
adcroft |
1.1 |
[i.e. for the 3-day integration with a total oof 72 timesteps |
647 |
heimbach |
1.15 |
the model state was stored 7 times to disk and roughly 6 times |
648 |
|
|
to memory]. |
649 |
adcroft |
1.1 |
This saving in memory comes at a cost of a required |
650 |
|
|
3 full forward integrations of the model (one for each |
651 |
|
|
checkpointing level). |
652 |
heimbach |
1.15 |
The optimal balance of storage vs. recomputation certainly depends |
653 |
|
|
on the computing resources available and may be adjusted by |
654 |
|
|
adjusting the partitioning among the |
655 |
|
|
$ n^{lev3}, \,\, n^{lev2}, \,\, n^{lev1} $. |
656 |
adcroft |
1.1 |
|
657 |
|
|
\begin{figure}[t!] |
658 |
adcroft |
1.6 |
\begin{center} |
659 |
adcroft |
1.1 |
%\psdraft |
660 |
adcroft |
1.6 |
%\psfrag{v_k1^lev3}{\mathinfigure{v_{k_{1}^{lev3}}}} |
661 |
|
|
%\psfrag{v_kn-1^lev3}{\mathinfigure{v_{k_{n-1}^{lev3}}}} |
662 |
|
|
%\psfrag{v_kn^lev3}{\mathinfigure{v_{k_{n}^{lev3}}}} |
663 |
|
|
%\psfrag{v_k1^lev2}{\mathinfigure{v_{k_{1}^{lev2}}}} |
664 |
|
|
%\psfrag{v_kn-1^lev2}{\mathinfigure{v_{k_{n-1}^{lev2}}}} |
665 |
|
|
%\psfrag{v_kn^lev2}{\mathinfigure{v_{k_{n}^{lev2}}}} |
666 |
|
|
%\psfrag{v_k1^lev1}{\mathinfigure{v_{k_{1}^{lev1}}}} |
667 |
|
|
%\psfrag{v_kn^lev1}{\mathinfigure{v_{k_{n}^{lev1}}}} |
668 |
jmc |
1.22 |
%\mbox{\epsfig{file=s_autodiff/figs/checkpointing.eps, width=0.8\textwidth}} |
669 |
|
|
\resizebox{5.5in}{!}{\includegraphics{s_autodiff/figs/checkpointing.eps}} |
670 |
adcroft |
1.1 |
%\psfull |
671 |
adcroft |
1.6 |
\end{center} |
672 |
|
|
\caption{ |
673 |
|
|
Schematic view of intermediate dump and restart for |
674 |
adcroft |
1.1 |
3-level checkpointing.} |
675 |
heimbach |
1.4 |
\label{fig:3levelcheck} |
676 |
adcroft |
1.1 |
\end{figure} |
677 |
|
|
|
678 |
heimbach |
1.4 |
% \subsection{Optimal perturbations} |
679 |
|
|
% \label{sec_optpert} |
680 |
adcroft |
1.1 |
|
681 |
|
|
|
682 |
heimbach |
1.4 |
% \subsection{Error covariance estimate and Hessian matrix} |
683 |
|
|
% \label{sec_hessian} |
684 |
adcroft |
1.1 |
|
685 |
|
|
\newpage |
686 |
|
|
|
687 |
|
|
%********************************************************************** |
688 |
heimbach |
1.4 |
\section{TLM and ADM generation in general} |
689 |
adcroft |
1.1 |
\label{sec_ad_setup_gen} |
690 |
edhill |
1.17 |
\begin{rawhtml} |
691 |
|
|
<!-- CMIREDIR:sec_ad_setup_gen: --> |
692 |
|
|
\end{rawhtml} |
693 |
adcroft |
1.1 |
%********************************************************************** |
694 |
|
|
|
695 |
|
|
In this section we describe in a general fashion |
696 |
|
|
the parts of the code that are relevant for automatic |
697 |
heimbach |
1.21 |
differentiation using the software tool TAF. |
698 |
|
|
Modifications to use OpenAD are described in \ref{sec_ad_openad}. |
699 |
adcroft |
1.1 |
|
700 |
jmc |
1.22 |
\input{s_autodiff/text/doc_ad_the_model} |
701 |
heimbach |
1.4 |
|
702 |
adcroft |
1.6 |
The basic flow is depicted in \ref{fig:adthemodel}. |
703 |
heimbach |
1.19 |
If CPP option \texttt{ALLOW\_AUTODIFF\_TAMC} is defined, |
704 |
|
|
the driver routine |
705 |
heimbach |
1.4 |
{\it the\_model\_main}, instead of calling {\it the\_main\_loop}, |
706 |
heimbach |
1.19 |
invokes the adjoint of this routine, {\it adthe\_main\_loop} |
707 |
|
|
(case \texttt{\#define ALLOW\_ADJOINT\_RUN}), or |
708 |
|
|
the tangent linear of this routine {\it g\_the\_main\_loop} |
709 |
|
|
(case \texttt{\#define ALLOW\_TANGENTLINEAR\_RUN}), |
710 |
|
|
which are the toplevel routines in terms of automatic differentiation. |
711 |
|
|
The routines {\it adthe\_main\_loop} or {\it g\_the\_main\_loop} |
712 |
|
|
are generated by TAF. |
713 |
heimbach |
1.16 |
It contains both the forward integration of the full model, the |
714 |
|
|
cost function calculation, |
715 |
heimbach |
1.4 |
any additional storing that is required for efficient checkpointing, |
716 |
|
|
and the reverse integration of the adjoint model. |
717 |
heimbach |
1.16 |
|
718 |
|
|
[DESCRIBE IN A SEPARATE SECTION THE WORKING OF THE TLM] |
719 |
|
|
|
720 |
|
|
In Fig. \ref{fig:adthemodel} |
721 |
|
|
the structure of {\it adthe\_main\_loop} has been strongly |
722 |
|
|
simplified to focus on the essentials; in particular, no checkpointing |
723 |
heimbach |
1.4 |
procedures are shown here. |
724 |
|
|
Prior to the call of {\it adthe\_main\_loop}, the routine |
725 |
heimbach |
1.15 |
{\it ctrl\_unpack} is invoked to unpack the control vector |
726 |
|
|
or initialise the control variables. |
727 |
|
|
Following the call of {\it adthe\_main\_loop}, |
728 |
|
|
the routine {\it ctrl\_pack} |
729 |
heimbach |
1.4 |
is invoked to pack the control vector |
730 |
|
|
(cf. Section \ref{section_ctrl}). |
731 |
|
|
If gradient checks are to be performed, the option |
732 |
|
|
{\tt ALLOW\_GRADIENT\_CHECK} is defined. In this case |
733 |
|
|
the driver routine {\it grdchk\_main} is called after |
734 |
|
|
the gradient has been computed via the adjoint |
735 |
jmc |
1.23 |
(cf. Section \ref{sec:ad_gradient_check}). |
736 |
heimbach |
1.4 |
|
737 |
heimbach |
1.16 |
%------------------------------------------------------------------ |
738 |
|
|
|
739 |
|
|
\subsection{General setup |
740 |
|
|
\label{section_ad_setup}} |
741 |
|
|
|
742 |
|
|
In order to configure AD-related setups the following packages need |
743 |
|
|
to be enabled: |
744 |
|
|
{\it |
745 |
jmc |
1.23 |
\begin{table}[!ht] |
746 |
heimbach |
1.16 |
\begin{tabular}{l} |
747 |
|
|
autodiff \\ |
748 |
|
|
ctrl \\ |
749 |
|
|
cost \\ |
750 |
|
|
grdchk \\ |
751 |
|
|
\end{tabular} |
752 |
|
|
\end{table} |
753 |
|
|
} |
754 |
|
|
The packages are enabled by adding them to your experiment-specific |
755 |
|
|
configuration file |
756 |
|
|
{\it packages.conf} (see Section ???). |
757 |
|
|
|
758 |
|
|
The following AD-specific CPP option files need to be customized: |
759 |
|
|
% |
760 |
|
|
\begin{itemize} |
761 |
|
|
% |
762 |
|
|
\item {\it ECCO\_CPPOPTIONS.h} \\ |
763 |
|
|
This header file collects CPP options for the packages |
764 |
|
|
{\it autodiff, cost, ctrl} as well as AD-unrelated options for |
765 |
|
|
the external forcing package {\it exf}. |
766 |
|
|
\footnote{NOTE: These options are not set in their package-specific |
767 |
|
|
headers such as {\it COST\_CPPOPTIONS.h}, but are instead collected |
768 |
|
|
in the single header file {\it ECCO\_CPPOPTIONS.h}. |
769 |
|
|
The package-specific header files serve as simple |
770 |
|
|
placeholders at this point.} |
771 |
|
|
% |
772 |
|
|
\item {\it tamc.h} \\ |
773 |
|
|
This header configures the splitting of the time stepping loop |
774 |
|
|
w.r.t. the 3-level checkpointing (see section ???). |
775 |
|
|
|
776 |
|
|
% |
777 |
|
|
\end{itemize} |
778 |
|
|
|
779 |
|
|
%------------------------------------------------------------------ |
780 |
|
|
|
781 |
heimbach |
1.21 |
\subsection{Building the AD code using TAF |
782 |
heimbach |
1.16 |
\label{section_ad_build}} |
783 |
|
|
|
784 |
|
|
The build process of an AD code is very similar to building |
785 |
|
|
the forward model. However, depending on which AD code one wishes |
786 |
|
|
to generate, and on which AD tool is available (TAF or TAMC), |
787 |
|
|
the following {\tt make} targets are available: |
788 |
|
|
|
789 |
jmc |
1.23 |
\begin{table}[!ht] |
790 |
heimbach |
1.16 |
{\footnotesize |
791 |
heimbach |
1.21 |
\begin{tabular}{|ccll|} |
792 |
|
|
\hline |
793 |
heimbach |
1.16 |
~ & {\it AD-target} & {\it output} & {\it description} \\ |
794 |
|
|
\hline |
795 |
|
|
\hline |
796 |
|
|
(1) & {\tt <MODE><TOOL>only} & {\tt <MODE>\_<TOOL>\_output.f} & |
797 |
|
|
generates code for $<$MODE$>$ using $<$TOOL$>$ \\ |
798 |
|
|
~ & ~ & ~ & no {\tt make} dependencies on {\tt .F .h} \\ |
799 |
|
|
~ & ~ & ~ & useful for compiling on remote platforms \\ |
800 |
|
|
\hline |
801 |
|
|
(2) & {\tt <MODE><TOOL>} & {\tt <MODE>\_<TOOL>\_output.f} & |
802 |
|
|
generates code for $<$MODE$>$ using $<$TOOL$>$ \\ |
803 |
|
|
~ & ~ & ~ & includes {\tt make} dependencies on {\tt .F .h} \\ |
804 |
|
|
~ & ~ & ~ & i.e. input for $<$TOOL$>$ may be re-generated \\ |
805 |
|
|
\hline |
806 |
|
|
(3) & {\tt <MODE>all} & {\tt mitgcmuv\_<MODE>} & |
807 |
|
|
generates code for $<$MODE$>$ using $<$TOOL$>$ \\ |
808 |
|
|
~ & ~ & ~ & and compiles all code \\ |
809 |
|
|
~ & ~ & ~ & (use of TAF is set as default) \\ |
810 |
|
|
\hline |
811 |
|
|
\end{tabular} |
812 |
|
|
} |
813 |
|
|
\end{table} |
814 |
|
|
% |
815 |
|
|
Here, the following placeholders are used |
816 |
|
|
% |
817 |
|
|
\begin{itemize} |
818 |
|
|
% |
819 |
heimbach |
1.21 |
\item $<$TOOL$>$ |
820 |
heimbach |
1.16 |
% |
821 |
|
|
\begin{itemize} |
822 |
|
|
% |
823 |
|
|
\item {\tt TAF} |
824 |
|
|
\item {\tt TAMC} |
825 |
|
|
% |
826 |
|
|
\end{itemize} |
827 |
|
|
% |
828 |
heimbach |
1.21 |
\item $<$MODE$>$ |
829 |
heimbach |
1.16 |
% |
830 |
|
|
\begin{itemize} |
831 |
|
|
% |
832 |
|
|
\item {\tt ad} generates the adjoint model (ADM) |
833 |
|
|
\item {\tt ftl} generates the tangent linear model (TLM) |
834 |
|
|
\item {\tt svd} generates both ADM and TLM for \\ |
835 |
|
|
singular value decomposition (SVD) type calculations |
836 |
|
|
% |
837 |
|
|
\end{itemize} |
838 |
|
|
% |
839 |
|
|
\end{itemize} |
840 |
|
|
|
841 |
|
|
For example, to generate the adjoint model using TAF after routines ({\tt .F}) |
842 |
|
|
or headers ({\tt .h}) have been modified, but without compilation, |
843 |
|
|
type {\tt make adtaf}; |
844 |
|
|
or, to generate the tangent linear model using TAMC without |
845 |
|
|
re-generating the input code, type {\tt make ftltamconly}. |
846 |
|
|
|
847 |
|
|
|
848 |
|
|
A typical full build process to generate the ADM via TAF would |
849 |
|
|
look like follows: |
850 |
|
|
\begin{verbatim} |
851 |
|
|
% mkdir build |
852 |
|
|
% cd build |
853 |
|
|
% ../../../tools/genmake2 -mods=../code_ad |
854 |
|
|
% make depend |
855 |
|
|
% make adall |
856 |
|
|
\end{verbatim} |
857 |
|
|
|
858 |
|
|
%------------------------------------------------------------------ |
859 |
|
|
|
860 |
|
|
\subsection{The AD build process in detail |
861 |
|
|
\label{section_ad_build_detail}} |
862 |
|
|
|
863 |
|
|
The {\tt make <MODE>all} target consists of the following procedures: |
864 |
|
|
|
865 |
|
|
\begin{enumerate} |
866 |
|
|
% |
867 |
|
|
\item |
868 |
|
|
A header file {\tt AD\_CONFIG.h} is generated which contains a CPP option |
869 |
|
|
on which code ought to be generated. Depending on the {\tt make} target, |
870 |
heimbach |
1.21 |
the contents is one of the following: |
871 |
heimbach |
1.16 |
\begin{itemize} |
872 |
|
|
\item |
873 |
|
|
{\tt \#define ALLOW\_ADJOINT\_RUN} |
874 |
|
|
\item |
875 |
|
|
{\tt \#define ALLOW\_TANGENTLINEAR\_RUN} |
876 |
|
|
\item |
877 |
|
|
{\tt \#define ALLOW\_ECCO\_OPTIMIZATION} |
878 |
|
|
\end{itemize} |
879 |
|
|
% |
880 |
|
|
\item |
881 |
|
|
A single file {\tt <MODE>\_input\_code.f} is concatenated |
882 |
|
|
consisting of all {\tt .f} files that are part of the list {\bf AD\_FILES} |
883 |
|
|
and all {\tt .flow} files that are part of the list {\bf AD\_FLOW\_FILES}. |
884 |
|
|
% |
885 |
|
|
\item |
886 |
heimbach |
1.21 |
The AD tool is invoked with the {\tt <MODE>\_<TOOL>\_FLAGS}. |
887 |
heimbach |
1.16 |
The default AD tool flags in {\tt genmake2} can be overrwritten by |
888 |
|
|
an {\tt adjoint\_options} file (similar to the platform-specific |
889 |
|
|
{\tt build\_options}, see Section ???. |
890 |
|
|
The AD tool writes the resulting AD code into the file |
891 |
|
|
{\tt <MODE>\_input\_code\_ad.f} |
892 |
|
|
% |
893 |
|
|
\item |
894 |
|
|
A short sed script {\tt adjoint\_sed} is applied to |
895 |
|
|
{\tt <MODE>\_input\_code\_ad.f} |
896 |
|
|
to reinstate {\bf myThid} into the CALL argument list of active file I/O. |
897 |
|
|
The result is written to file {\tt <MODE>\_<TOOL>\_output.f}. |
898 |
|
|
% |
899 |
|
|
\item |
900 |
|
|
All routines are compiled and an executable is generated |
901 |
|
|
(see Table ???). |
902 |
|
|
% |
903 |
|
|
\end{enumerate} |
904 |
|
|
|
905 |
|
|
\subsubsection{The list AD\_FILES and {\tt .list} files} |
906 |
|
|
|
907 |
|
|
Not all routines are presented to the AD tool. |
908 |
|
|
Routines typically hidden are diagnostics routines which |
909 |
|
|
do not influence the cost function, but may create |
910 |
|
|
artificial flow dependencies such as I/O of active variables. |
911 |
|
|
|
912 |
|
|
{\tt genmake2} generates a list (or variable) {\bf AD\_FILES} |
913 |
|
|
which contains all routines that are shown to the AD tool. |
914 |
|
|
This list is put together from all files with suffix {\tt .list} |
915 |
|
|
that {\tt genmake2} finds in its search directories. |
916 |
|
|
The list file for the core MITgcm routines is in {\tt model/src/} |
917 |
|
|
is called {\tt model\_ad\_diff.list}. |
918 |
|
|
Note that no wrapper routine is shown to TAF. These are either |
919 |
|
|
not visible at all to the AD code, or hand-written AD code |
920 |
|
|
is available (see next section). |
921 |
|
|
|
922 |
|
|
Each package directory contains its package-specific |
923 |
|
|
list file {\tt <PKG>\_ad\_diff.list}. For example, |
924 |
|
|
{\tt pkg/ptracers/} contains the file {\tt ptracers\_ad\_diff.list}. |
925 |
|
|
Thus, enabling a package will automatically extend the |
926 |
|
|
{\bf AD\_FILES} list of {\tt genmake2} to incorporate the |
927 |
|
|
package-specific routines. |
928 |
|
|
Note that you will need to regenerate the {\tt Makefile} if |
929 |
|
|
you enable a package (e.g. by adding it to {\tt packages.conf}) |
930 |
|
|
and a {\tt Makefile} already exists. |
931 |
|
|
|
932 |
|
|
\subsubsection{The list AD\_FLOW\_FILES and {\tt .flow} files} |
933 |
|
|
|
934 |
|
|
TAMC and TAF can evaluate user-specified directives |
935 |
|
|
that start with a specific syntax ({\tt CADJ}, {\tt C\$TAF}, {\tt !\$TAF}). |
936 |
|
|
The main categories of directives are STORE directives and |
937 |
|
|
FLOW directives. Here, we are concerned with flow directives, |
938 |
|
|
store directives are treated elsewhere. |
939 |
|
|
|
940 |
|
|
Flow directives enable the AD tool to evaluate how it should treat |
941 |
|
|
routines that are 'hidden' by the user, i.e. routines which are |
942 |
|
|
not contained in the {\bf AD\_FILES} list (see previous section), |
943 |
|
|
but which are called in part of the code that the AD tool does see. |
944 |
|
|
The flow directive tell the AD tool |
945 |
|
|
% |
946 |
|
|
\begin{itemize} |
947 |
|
|
% |
948 |
|
|
\item which subroutine arguments are input/output |
949 |
|
|
\item which subroutine arguments are active |
950 |
|
|
\item which subroutine arguments are required to compute the cost |
951 |
|
|
\item which subroutine arguments are dependent |
952 |
|
|
% |
953 |
|
|
\end{itemize} |
954 |
|
|
% |
955 |
|
|
The syntax for the flow directives can be found in the |
956 |
|
|
AD tool manuals. |
957 |
|
|
|
958 |
|
|
{\tt genmake2} generates a list (or variable) {\bf AD\_FLOW\_FILES} |
959 |
|
|
which contains all files with suffix{\tt .flow} that it finds |
960 |
|
|
in its search directories. |
961 |
|
|
The flow directives for the core MITgcm routines of |
962 |
|
|
{\tt eesupp/src/} and {\tt model/src/} |
963 |
|
|
reside in {\tt pkg/autodiff/}. |
964 |
|
|
This directory also contains hand-written adjoint code |
965 |
edhill |
1.20 |
for the MITgcm WRAPPER (section \ref{chap:sarch}). |
966 |
heimbach |
1.16 |
|
967 |
|
|
Flow directives for package-specific routines are contained in |
968 |
|
|
the corresponding package directories in the file |
969 |
|
|
{\tt <PKG>\_ad.flow}, e.g. ptracers-specific directives are in |
970 |
|
|
{\tt ptracers\_ad.flow}. |
971 |
|
|
|
972 |
|
|
\subsubsection{Store directives for 3-level checkpointing} |
973 |
|
|
|
974 |
|
|
The storing that is required at each period of the |
975 |
|
|
3-level checkpointing is controled by three |
976 |
|
|
top-level headers. |
977 |
|
|
|
978 |
|
|
\begin{verbatim} |
979 |
|
|
do ilev_3 = 1, nchklev_3 |
980 |
|
|
# include ``checkpoint_lev3.h'' |
981 |
|
|
do ilev_2 = 1, nchklev_2 |
982 |
|
|
# include ``checkpoint_lev2.h'' |
983 |
|
|
do ilev_1 = 1, nchklev_1 |
984 |
|
|
# include ``checkpoint_lev1.h'' |
985 |
|
|
|
986 |
|
|
... |
987 |
|
|
|
988 |
|
|
end do |
989 |
|
|
end do |
990 |
|
|
end do |
991 |
|
|
\end{verbatim} |
992 |
|
|
|
993 |
|
|
All files {\tt checkpoint\_lev?.h} are contained in directory |
994 |
|
|
{\tt pkg/autodiff/}. |
995 |
|
|
|
996 |
|
|
|
997 |
|
|
\subsubsection{Changing the default AD tool flags: ad\_options files} |
998 |
|
|
|
999 |
|
|
|
1000 |
|
|
\subsubsection{Hand-written adjoint code} |
1001 |
|
|
|
1002 |
|
|
%------------------------------------------------------------------ |
1003 |
|
|
|
1004 |
heimbach |
1.4 |
\subsection{The cost function (dependent variable) |
1005 |
|
|
\label{section_cost}} |
1006 |
adcroft |
1.1 |
|
1007 |
|
|
The cost function $ {\cal J} $ is referred to as the {\sf dependent variable}. |
1008 |
|
|
It is a function of the input variables $ \vec{u} $ via the composition |
1009 |
|
|
$ {\cal J}(\vec{u}) \, = \, {\cal J}(M(\vec{u})) $. |
1010 |
heimbach |
1.15 |
The input are referred to as the |
1011 |
adcroft |
1.1 |
{\sf independent variables} or {\sf control variables}. |
1012 |
|
|
All aspects relevant to the treatment of the cost function $ {\cal J} $ |
1013 |
cnh |
1.7 |
(parameter setting, initialization, accumulation, |
1014 |
heimbach |
1.4 |
final evaluation), are controlled by the package {\it pkg/cost}. |
1015 |
heimbach |
1.15 |
The aspects relevant to the treatment of the independent variables |
1016 |
|
|
are controlled by the package {\it pkg/ctrl} and will be treated |
1017 |
|
|
in the next section. |
1018 |
heimbach |
1.4 |
|
1019 |
jmc |
1.22 |
\input{s_autodiff/text/doc_cost_flow} |
1020 |
adcroft |
1.1 |
|
1021 |
heimbach |
1.16 |
\subsubsection{Enabling the package} |
1022 |
|
|
|
1023 |
adcroft |
1.1 |
\fbox{ |
1024 |
|
|
\begin{minipage}{12cm} |
1025 |
heimbach |
1.16 |
{\it packages.conf}, {\it ECCO\_CPPOPTIONS.h} |
1026 |
adcroft |
1.1 |
\end{minipage} |
1027 |
|
|
} |
1028 |
heimbach |
1.16 |
\begin{itemize} |
1029 |
adcroft |
1.1 |
% |
1030 |
heimbach |
1.16 |
\item |
1031 |
|
|
The package is enabled by adding {\it cost} to your file {\it packages.conf} |
1032 |
|
|
(see Section ???) |
1033 |
adcroft |
1.1 |
% |
1034 |
heimbach |
1.16 |
\item |
1035 |
|
|
|
1036 |
|
|
|
1037 |
|
|
\end{itemize} |
1038 |
adcroft |
1.1 |
% |
1039 |
heimbach |
1.16 |
|
1040 |
heimbach |
1.15 |
N.B.: In general the following packages ought to be enabled |
1041 |
|
|
simultaneously: {\it autodiff, cost, ctrl}. |
1042 |
heimbach |
1.4 |
The basic CPP option to enable the cost function is {\bf ALLOW\_COST}. |
1043 |
|
|
Each specific cost function contribution has its own option. |
1044 |
|
|
For the present example the option is {\bf ALLOW\_COST\_TRACER}. |
1045 |
|
|
All cost-specific options are set in {\it ECCO\_CPPOPTIONS.h} |
1046 |
adcroft |
1.1 |
Since the cost function is usually used in conjunction with |
1047 |
|
|
automatic differentiation, the CPP option |
1048 |
heimbach |
1.15 |
{\bf ALLOW\_ADJOINT\_RUN} (file {\it CPP\_OPTIONS.h}) and |
1049 |
|
|
{\bf ALLOW\_AUTODIFF\_TAMC} (file {\it ECCO\_CPPOPTIONS.h}) |
1050 |
|
|
should be defined. |
1051 |
adcroft |
1.1 |
|
1052 |
cnh |
1.7 |
\subsubsection{Initialization} |
1053 |
adcroft |
1.1 |
% |
1054 |
cnh |
1.7 |
The initialization of the {\it cost} package is readily enabled |
1055 |
heimbach |
1.15 |
as soon as the CPP option {\bf ALLOW\_COST} is defined. |
1056 |
adcroft |
1.1 |
% |
1057 |
|
|
\begin{itemize} |
1058 |
|
|
% |
1059 |
|
|
\item |
1060 |
|
|
\fbox{ |
1061 |
|
|
\begin{minipage}{12cm} |
1062 |
|
|
Parameters: {\it cost\_readparms} |
1063 |
|
|
\end{minipage} |
1064 |
|
|
} |
1065 |
|
|
\\ |
1066 |
|
|
This S/R |
1067 |
|
|
reads runtime flags and parameters from file {\it data.cost}. |
1068 |
|
|
For the present example the only relevant parameter read |
1069 |
|
|
is {\bf mult\_tracer}. This multiplier enables different |
1070 |
|
|
cost function contributions to be switched on |
1071 |
|
|
( = 1.) or off ( = 0.) at runtime. |
1072 |
|
|
For more complex cost functions which involve model vs. data |
1073 |
|
|
misfits, the corresponding data filenames and data |
1074 |
|
|
specifications (start date and time, period, ...) are read |
1075 |
|
|
in this S/R. |
1076 |
|
|
% |
1077 |
|
|
\item |
1078 |
|
|
\fbox{ |
1079 |
|
|
\begin{minipage}{12cm} |
1080 |
|
|
Variables: {\it cost\_init} |
1081 |
|
|
\end{minipage} |
1082 |
|
|
} |
1083 |
|
|
\\ |
1084 |
|
|
This S/R |
1085 |
cnh |
1.7 |
initializes the different cost function contributions. |
1086 |
|
|
The contribution for the present example is {\bf objf\_tracer} |
1087 |
adcroft |
1.1 |
which is defined on each tile (bi,bj). |
1088 |
|
|
% |
1089 |
|
|
\end{itemize} |
1090 |
|
|
% |
1091 |
heimbach |
1.4 |
\subsubsection{Accumulation} |
1092 |
adcroft |
1.1 |
% |
1093 |
|
|
\begin{itemize} |
1094 |
|
|
% |
1095 |
|
|
\item |
1096 |
|
|
\fbox{ |
1097 |
|
|
\begin{minipage}{12cm} |
1098 |
|
|
{\it cost\_tile}, {\it cost\_tracer} |
1099 |
|
|
\end{minipage} |
1100 |
|
|
} |
1101 |
|
|
\end{itemize} |
1102 |
|
|
% |
1103 |
|
|
The 'driver' routine |
1104 |
|
|
{\it cost\_tile} is called at the end of each time step. |
1105 |
|
|
Within this 'driver' routine, S/R are called for each of |
1106 |
|
|
the chosen cost function contributions. |
1107 |
|
|
In the present example ({\bf ALLOW\_COST\_TRACER}), |
1108 |
|
|
S/R {\it cost\_tracer} is called. |
1109 |
jmc |
1.23 |
It accumulates {\bf objf\_tracer} according to eqn. (\ref{ask_the_author:doc_ad_2}). |
1110 |
adcroft |
1.1 |
% |
1111 |
|
|
\subsubsection{Finalize all contributions} |
1112 |
|
|
% |
1113 |
|
|
\begin{itemize} |
1114 |
|
|
% |
1115 |
|
|
\item |
1116 |
|
|
\fbox{ |
1117 |
|
|
\begin{minipage}{12cm} |
1118 |
|
|
{\it cost\_final} |
1119 |
|
|
\end{minipage} |
1120 |
|
|
} |
1121 |
|
|
\end{itemize} |
1122 |
|
|
% |
1123 |
|
|
At the end of the forward integration S/R {\it cost\_final} |
1124 |
|
|
is called. It accumulates the total cost function {\bf fc} |
1125 |
|
|
from each contribution and sums over all tiles: |
1126 |
|
|
\begin{equation} |
1127 |
|
|
{\cal J} \, = \, |
1128 |
|
|
{\rm fc} \, = \, |
1129 |
heimbach |
1.15 |
{\rm mult\_tracer} \sum_{\text{global sum}} \sum_{bi,\,bj}^{nSx,\,nSy} |
1130 |
adcroft |
1.1 |
{\rm objf\_tracer}(bi,bj) \, + \, ... |
1131 |
|
|
\end{equation} |
1132 |
|
|
% |
1133 |
|
|
The total cost function {\bf fc} will be the |
1134 |
heimbach |
1.21 |
'dependent' variable in the argument list for TAF, i.e. |
1135 |
adcroft |
1.1 |
\begin{verbatim} |
1136 |
heimbach |
1.21 |
taf -output 'fc' ... |
1137 |
adcroft |
1.1 |
\end{verbatim} |
1138 |
|
|
|
1139 |
cnh |
1.3 |
%%%% \end{document} |
1140 |
adcroft |
1.1 |
|
1141 |
jmc |
1.22 |
\input{s_autodiff/text/doc_ad_the_main} |
1142 |
adcroft |
1.1 |
|
1143 |
heimbach |
1.4 |
\subsection{The control variables (independent variables) |
1144 |
|
|
\label{section_ctrl}} |
1145 |
adcroft |
1.1 |
|
1146 |
|
|
The control variables are a subset of the model input |
1147 |
|
|
(initial conditions, boundary conditions, model parameters). |
1148 |
|
|
Here we identify them with the variable $ \vec{u} $. |
1149 |
|
|
All intermediate variables whose derivative w.r.t. control |
1150 |
heimbach |
1.4 |
variables do not vanish are called {\sf active variables}. |
1151 |
adcroft |
1.1 |
All subroutines whose derivative w.r.t. the control variables |
1152 |
|
|
don't vanish are called {\sf active routines}. |
1153 |
|
|
Read and write operations from and to file can be viewed |
1154 |
|
|
as variable assignments. Therefore, files to which |
1155 |
|
|
active variables are written and from which active variables |
1156 |
|
|
are read are called {\sf active files}. |
1157 |
|
|
All aspects relevant to the treatment of the control variables |
1158 |
cnh |
1.7 |
(parameter setting, initialization, perturbation) |
1159 |
|
|
are controlled by the package {\it pkg/ctrl}. |
1160 |
adcroft |
1.1 |
|
1161 |
jmc |
1.22 |
\input{s_autodiff/text/doc_ctrl_flow} |
1162 |
heimbach |
1.4 |
|
1163 |
adcroft |
1.1 |
\subsubsection{genmake and CPP options} |
1164 |
|
|
% |
1165 |
|
|
\begin{itemize} |
1166 |
|
|
% |
1167 |
|
|
\item |
1168 |
|
|
\fbox{ |
1169 |
|
|
\begin{minipage}{12cm} |
1170 |
|
|
{\it genmake}, {\it CPP\_OPTIONS.h}, {\it ECCO\_CPPOPTIONS.h} |
1171 |
|
|
\end{minipage} |
1172 |
|
|
} |
1173 |
|
|
\end{itemize} |
1174 |
|
|
% |
1175 |
|
|
To enable the directory to be included to the compile list, |
1176 |
|
|
{\bf ctrl} has to be added to the {\bf enable} list in |
1177 |
heimbach |
1.15 |
{\it .genmakerc} or in {\it genmake} itself (analogous to {\it cost} |
1178 |
|
|
package, cf. previous section). |
1179 |
adcroft |
1.1 |
Each control variable is enabled via its own CPP option |
1180 |
|
|
in {\it ECCO\_CPPOPTIONS.h}. |
1181 |
|
|
|
1182 |
cnh |
1.7 |
\subsubsection{Initialization} |
1183 |
adcroft |
1.1 |
% |
1184 |
|
|
\begin{itemize} |
1185 |
|
|
% |
1186 |
|
|
\item |
1187 |
|
|
\fbox{ |
1188 |
|
|
\begin{minipage}{12cm} |
1189 |
|
|
Parameters: {\it ctrl\_readparms} |
1190 |
|
|
\end{minipage} |
1191 |
|
|
} |
1192 |
|
|
\\ |
1193 |
|
|
% |
1194 |
|
|
This S/R |
1195 |
|
|
reads runtime flags and parameters from file {\it data.ctrl}. |
1196 |
|
|
For the present example the file contains the file names |
1197 |
|
|
of each control variable that is used. |
1198 |
|
|
In addition, the number of wet points for each control |
1199 |
|
|
variable and the net dimension of the space of control |
1200 |
|
|
variables (counting wet points only) {\bf nvarlength} |
1201 |
|
|
is determined. |
1202 |
|
|
Masks for wet points for each tile {\bf (bi,\,bj)} |
1203 |
|
|
and vertical layer {\bf k} are generated for the three |
1204 |
|
|
relevant categories on the C-grid: |
1205 |
|
|
{\bf nWetCtile} for tracer fields, |
1206 |
|
|
{\bf nWetWtile} for zonal velocity fields, |
1207 |
|
|
{\bf nWetStile} for meridional velocity fields. |
1208 |
|
|
% |
1209 |
|
|
\item |
1210 |
|
|
\fbox{ |
1211 |
|
|
\begin{minipage}{12cm} |
1212 |
|
|
Control variables, control vector, |
1213 |
|
|
and their gradients: {\it ctrl\_unpack} |
1214 |
|
|
\end{minipage} |
1215 |
|
|
} |
1216 |
|
|
\\ |
1217 |
|
|
% |
1218 |
|
|
Two important issues related to the handling of the control |
1219 |
edhill |
1.20 |
variables in MITgcm need to be addressed. |
1220 |
adcroft |
1.1 |
First, in order to save memory, the control variable arrays |
1221 |
|
|
are not kept in memory, but rather read from file and added |
1222 |
cnh |
1.7 |
to the initial fields during the model initialization phase. |
1223 |
adcroft |
1.1 |
Similarly, the corresponding adjoint fields which represent |
1224 |
|
|
the gradient of the cost function w.r.t. the control variables |
1225 |
heimbach |
1.4 |
are written to file at the end of the adjoint integration. |
1226 |
adcroft |
1.1 |
Second, in addition to the files holding the 2-dim. and 3-dim. |
1227 |
heimbach |
1.4 |
control variables and the corresponding cost gradients, |
1228 |
|
|
a 1-dim. {\sf control vector} |
1229 |
adcroft |
1.1 |
and {\sf gradient vector} are written to file. They contain |
1230 |
|
|
only the wet points of the control variables and the corresponding |
1231 |
|
|
gradient. |
1232 |
|
|
This leads to a significant data compression. |
1233 |
heimbach |
1.4 |
Furthermore, an option is available |
1234 |
|
|
({\tt ALLOW\_NONDIMENSIONAL\_CONTROL\_IO}) to |
1235 |
|
|
non-dimensionalise the control and gradient vector, |
1236 |
|
|
which otherwise would contain different pieces of different |
1237 |
|
|
magnitudes and units. |
1238 |
|
|
Finally, the control and gradient vector can be passed to a |
1239 |
adcroft |
1.1 |
minimization routine if an update of the control variables |
1240 |
|
|
is sought as part of a minimization exercise. |
1241 |
|
|
|
1242 |
|
|
The files holding fields and vectors of the control variables |
1243 |
|
|
and gradient are generated and initialised in S/R {\it ctrl\_unpack}. |
1244 |
|
|
% |
1245 |
|
|
\end{itemize} |
1246 |
|
|
|
1247 |
|
|
\subsubsection{Perturbation of the independent variables} |
1248 |
|
|
% |
1249 |
heimbach |
1.4 |
The dependency flow for differentiation w.r.t. the controls |
1250 |
|
|
starts with adding a perturbation onto the input variable, |
1251 |
heimbach |
1.21 |
thus defining the independent or control variables for TAF. |
1252 |
heimbach |
1.4 |
Three types of controls may be considered: |
1253 |
adcroft |
1.1 |
% |
1254 |
|
|
\begin{itemize} |
1255 |
|
|
% |
1256 |
|
|
\item |
1257 |
|
|
\fbox{ |
1258 |
|
|
\begin{minipage}{12cm} |
1259 |
|
|
{\it ctrl\_map\_ini} (initial value sensitivity): |
1260 |
|
|
\end{minipage} |
1261 |
|
|
} |
1262 |
|
|
\\ |
1263 |
|
|
% |
1264 |
|
|
Consider as an example the initial tracer distribution |
1265 |
|
|
{\bf tr1} as control variable. |
1266 |
|
|
After {\bf tr1} has been initialised in |
1267 |
heimbach |
1.4 |
{\it ini\_tr1} (dynamical variables such as |
1268 |
adcroft |
1.1 |
temperature and salinity are initialised in {\it ini\_fields}), |
1269 |
|
|
a perturbation anomaly is added to the field in S/R |
1270 |
|
|
{\it ctrl\_map\_ini} |
1271 |
|
|
% |
1272 |
jmc |
1.23 |
%\begin{eqnarray} |
1273 |
adcroft |
1.1 |
\begin{equation} |
1274 |
jmc |
1.23 |
\begin{aligned} |
1275 |
adcroft |
1.1 |
u & = \, u_{[0]} \, + \, \Delta u \\ |
1276 |
|
|
{\bf tr1}(...) & = \, {\bf tr1_{ini}}(...) \, + \, {\bf xx\_tr1}(...) |
1277 |
|
|
\label{perturb} |
1278 |
jmc |
1.23 |
\end{aligned} |
1279 |
adcroft |
1.1 |
\end{equation} |
1280 |
jmc |
1.23 |
%\end{eqnarray} |
1281 |
adcroft |
1.1 |
% |
1282 |
heimbach |
1.4 |
{\bf xx\_tr1} is a 3-dim. global array |
1283 |
adcroft |
1.1 |
holding the perturbation. In the case of a simple |
1284 |
|
|
sensitivity study this array is identical to zero. |
1285 |
heimbach |
1.4 |
However, it's specification is essential in the context |
1286 |
heimbach |
1.21 |
of automatic differentiation since TAF |
1287 |
adcroft |
1.1 |
treats the corresponding line in the code symbolically |
1288 |
|
|
when determining the differentiation chain and its origin. |
1289 |
|
|
Thus, the variable names are part of the argument list |
1290 |
heimbach |
1.21 |
when calling TAF: |
1291 |
adcroft |
1.1 |
% |
1292 |
|
|
\begin{verbatim} |
1293 |
heimbach |
1.21 |
taf -input 'xx_tr1 ...' ... |
1294 |
adcroft |
1.1 |
\end{verbatim} |
1295 |
|
|
% |
1296 |
edhill |
1.20 |
Now, as mentioned above, MITgcm avoids maintaining |
1297 |
adcroft |
1.1 |
an array for each control variable by reading the |
1298 |
|
|
perturbation to a temporary array from file. |
1299 |
heimbach |
1.21 |
To ensure the symbolic link to be recognized by TAF, a scalar |
1300 |
adcroft |
1.1 |
dummy variable {\bf xx\_tr1\_dummy} is introduced |
1301 |
|
|
and an 'active read' routine of the adjoint support |
1302 |
|
|
package {\it pkg/autodiff} is invoked. |
1303 |
|
|
The read-procedure is tagged with the variable |
1304 |
heimbach |
1.21 |
{\bf xx\_tr1\_dummy} enabling TAF to recognize the |
1305 |
cnh |
1.7 |
initialization of the perturbation. |
1306 |
heimbach |
1.21 |
The modified call of TAF thus reads |
1307 |
adcroft |
1.1 |
% |
1308 |
|
|
\begin{verbatim} |
1309 |
heimbach |
1.21 |
taf -input 'xx_tr1_dummy ...' ... |
1310 |
adcroft |
1.1 |
\end{verbatim} |
1311 |
|
|
% |
1312 |
|
|
and the modified operation to (\ref{perturb}) |
1313 |
|
|
in the code takes on the form |
1314 |
|
|
% |
1315 |
|
|
\begin{verbatim} |
1316 |
|
|
call active_read_xyz( |
1317 |
|
|
& ..., tmpfld3d, ..., xx_tr1_dummy, ... ) |
1318 |
|
|
|
1319 |
|
|
tr1(...) = tr1(...) + tmpfld3d(...) |
1320 |
|
|
\end{verbatim} |
1321 |
|
|
% |
1322 |
|
|
Note, that reading an active variable corresponds |
1323 |
|
|
to a variable assignment. Its derivative corresponds |
1324 |
heimbach |
1.15 |
to a write statement of the adjoint variable, followed by |
1325 |
|
|
a reset. |
1326 |
adcroft |
1.1 |
The 'active file' routines have been designed |
1327 |
heimbach |
1.4 |
to support active read and corresponding adjoint active write |
1328 |
|
|
operations (and vice versa). |
1329 |
adcroft |
1.1 |
% |
1330 |
|
|
\item |
1331 |
|
|
\fbox{ |
1332 |
|
|
\begin{minipage}{12cm} |
1333 |
|
|
{\it ctrl\_map\_forcing} (boundary value sensitivity): |
1334 |
|
|
\end{minipage} |
1335 |
|
|
} |
1336 |
|
|
\\ |
1337 |
|
|
% |
1338 |
|
|
The handling of boundary values as control variables |
1339 |
|
|
proceeds exactly analogous to the initial values |
1340 |
|
|
with the symbolic perturbation taking place in S/R |
1341 |
|
|
{\it ctrl\_map\_forcing}. |
1342 |
|
|
Note however an important difference: |
1343 |
|
|
Since the boundary values are time dependent with a new |
1344 |
|
|
forcing field applied at each time steps, |
1345 |
heimbach |
1.4 |
the general problem may be thought of as |
1346 |
|
|
a new control variable at each time step |
1347 |
|
|
(or, if the perturbation is averaged over a certain period, |
1348 |
|
|
at each $ N $ timesteps), i.e. |
1349 |
adcroft |
1.1 |
\[ |
1350 |
|
|
u_{\rm forcing} \, = \, |
1351 |
|
|
\{ \, u_{\rm forcing} ( t_n ) \, \}_{ |
1352 |
|
|
n \, = \, 1, \ldots , {\rm nTimeSteps} } |
1353 |
|
|
\] |
1354 |
|
|
% |
1355 |
|
|
In the current example an equilibrium state is considered, |
1356 |
|
|
and only an initial perturbation to |
1357 |
|
|
surface forcing is applied with respect to the |
1358 |
|
|
equilibrium state. |
1359 |
|
|
A time dependent treatment of the surface forcing is |
1360 |
|
|
implemented in the ECCO environment, involving the |
1361 |
|
|
calendar ({\it cal}~) and external forcing ({\it exf}~) packages. |
1362 |
|
|
% |
1363 |
|
|
\item |
1364 |
|
|
\fbox{ |
1365 |
|
|
\begin{minipage}{12cm} |
1366 |
|
|
{\it ctrl\_map\_params} (parameter sensitivity): |
1367 |
|
|
\end{minipage} |
1368 |
|
|
} |
1369 |
|
|
\\ |
1370 |
|
|
% |
1371 |
|
|
This routine is not yet implemented, but would proceed |
1372 |
|
|
proceed along the same lines as the initial value sensitivity. |
1373 |
heimbach |
1.4 |
The mixing parameters {\bf diffkr} and {\bf kapgm} |
1374 |
|
|
are currently added as controls in {\it ctrl\_map\_ini.F}. |
1375 |
adcroft |
1.1 |
% |
1376 |
|
|
\end{itemize} |
1377 |
|
|
% |
1378 |
|
|
|
1379 |
|
|
\subsubsection{Output of adjoint variables and gradient} |
1380 |
|
|
% |
1381 |
heimbach |
1.4 |
Several ways exist to generate output of adjoint fields. |
1382 |
adcroft |
1.1 |
% |
1383 |
|
|
\begin{itemize} |
1384 |
|
|
% |
1385 |
|
|
\item |
1386 |
|
|
\fbox{ |
1387 |
|
|
\begin{minipage}{12cm} |
1388 |
heimbach |
1.4 |
{\it ctrl\_map\_ini, ctrl\_map\_forcing}: |
1389 |
adcroft |
1.1 |
\end{minipage} |
1390 |
|
|
} |
1391 |
|
|
\\ |
1392 |
|
|
\begin{itemize} |
1393 |
|
|
% |
1394 |
heimbach |
1.4 |
\item {\bf xx\_...}: the control variable fields \\ |
1395 |
|
|
Before the forward integration, the control |
1396 |
|
|
variables are read from file {\bf xx\_ ...} and added to |
1397 |
|
|
the model field. |
1398 |
adcroft |
1.1 |
% |
1399 |
|
|
\item {\bf adxx\_...}: the adjoint variable fields, i.e. the gradient |
1400 |
heimbach |
1.4 |
$ \nabla _{u}{\cal J} $ for each control variable \\ |
1401 |
|
|
After the adjoint integration the corresponding adjoint |
1402 |
|
|
variables are written to {\bf adxx\_ ...}. |
1403 |
adcroft |
1.1 |
% |
1404 |
heimbach |
1.4 |
\end{itemize} |
1405 |
adcroft |
1.1 |
% |
1406 |
heimbach |
1.4 |
\item |
1407 |
|
|
\fbox{ |
1408 |
|
|
\begin{minipage}{12cm} |
1409 |
|
|
{\it ctrl\_unpack, ctrl\_pack}: |
1410 |
|
|
\end{minipage} |
1411 |
|
|
} |
1412 |
|
|
\\ |
1413 |
|
|
% |
1414 |
|
|
\begin{itemize} |
1415 |
|
|
% |
1416 |
|
|
\item {\bf vector\_ctrl}: the control vector \\ |
1417 |
cnh |
1.7 |
At the very beginning of the model initialization, |
1418 |
heimbach |
1.4 |
the updated compressed control vector is read (or initialised) |
1419 |
|
|
and distributed to 2-dim. and 3-dim. control variable fields. |
1420 |
|
|
% |
1421 |
|
|
\item {\bf vector\_grad}: the gradient vector \\ |
1422 |
|
|
At the very end of the adjoint integration, |
1423 |
|
|
the 2-dim. and 3-dim. adjoint variables are read, |
1424 |
|
|
compressed to a single vector and written to file. |
1425 |
adcroft |
1.1 |
% |
1426 |
|
|
\end{itemize} |
1427 |
|
|
% |
1428 |
|
|
\item |
1429 |
|
|
\fbox{ |
1430 |
|
|
\begin{minipage}{12cm} |
1431 |
|
|
{\it addummy\_in\_stepping}: |
1432 |
|
|
\end{minipage} |
1433 |
|
|
} |
1434 |
|
|
\\ |
1435 |
|
|
In addition to writing the gradient at the end of the |
1436 |
heimbach |
1.4 |
forward/adjoint integration, many more adjoint variables |
1437 |
|
|
of the model state |
1438 |
|
|
at intermediate times can be written using S/R |
1439 |
adcroft |
1.1 |
{\it addummy\_in\_stepping}. |
1440 |
|
|
This routine is part of the adjoint support package |
1441 |
|
|
{\it pkg/autodiff} (cf.f. below). |
1442 |
heimbach |
1.15 |
The procedure is enabled using via the CPP-option |
1443 |
|
|
{\bf ALLOW\_AUTODIFF\_MONITOR} (file {\it ECCO\_CPPOPTIONS.h}). |
1444 |
adcroft |
1.1 |
To be part of the adjoint code, the corresponding S/R |
1445 |
|
|
{\it dummy\_in\_stepping} has to be called in the forward |
1446 |
|
|
model (S/R {\it the\_main\_loop}) at the appropriate place. |
1447 |
heimbach |
1.15 |
The adjoint common blocks are extracted from the adjoint code |
1448 |
|
|
via the header file {\it adcommon.h}. |
1449 |
adcroft |
1.1 |
|
1450 |
|
|
{\it dummy\_in\_stepping} is essentially empty, |
1451 |
|
|
the corresponding adjoint routine is hand-written rather |
1452 |
|
|
than generated automatically. |
1453 |
|
|
Appropriate flow directives ({\it dummy\_in\_stepping.flow}) |
1454 |
|
|
ensure that TAMC does not automatically |
1455 |
|
|
generate {\it addummy\_in\_stepping} by trying to differentiate |
1456 |
heimbach |
1.4 |
{\it dummy\_in\_stepping}, but instead refers to |
1457 |
|
|
the hand-written routine. |
1458 |
adcroft |
1.1 |
|
1459 |
|
|
{\it dummy\_in\_stepping} is called in the forward code |
1460 |
|
|
at the beginning of each |
1461 |
|
|
timestep, before the call to {\it dynamics}, thus ensuring |
1462 |
|
|
that {\it addummy\_in\_stepping} is called at the end of |
1463 |
|
|
each timestep in the adjoint calculation, after the call to |
1464 |
|
|
{\it addynamics}. |
1465 |
|
|
|
1466 |
|
|
{\it addummy\_in\_stepping} includes the header files |
1467 |
heimbach |
1.4 |
{\it adcommon.h}. |
1468 |
|
|
This header file is also hand-written. It contains |
1469 |
|
|
the common blocks |
1470 |
|
|
{\bf /addynvars\_r/}, {\bf /addynvars\_cd/}, |
1471 |
|
|
{\bf /addynvars\_diffkr/}, {\bf /addynvars\_kapgm/}, |
1472 |
adcroft |
1.1 |
{\bf /adtr1\_r/}, {\bf /adffields/}, |
1473 |
|
|
which have been extracted from the adjoint code to enable |
1474 |
|
|
access to the adjoint variables. |
1475 |
heimbach |
1.15 |
|
1476 |
|
|
{\bf WARNING:} If the structure of the common blocks |
1477 |
|
|
{\bf /dynvars\_r/}, {\bf /dynvars\_cd/}, etc., changes |
1478 |
|
|
similar changes will occur in the adjoint common blocks. |
1479 |
|
|
Therefore, consistency between the TAMC-generated common blocks |
1480 |
|
|
and those in {\it adcommon.h} have to be checked. |
1481 |
adcroft |
1.1 |
% |
1482 |
|
|
\end{itemize} |
1483 |
|
|
|
1484 |
|
|
|
1485 |
|
|
\subsubsection{Control variable handling for |
1486 |
|
|
optimization applications} |
1487 |
|
|
|
1488 |
|
|
In optimization mode the cost function $ {\cal J}(u) $ is sought |
1489 |
|
|
to be minimized with respect to a set of control variables |
1490 |
|
|
$ \delta {\cal J} \, = \, 0 $, in an iterative manner. |
1491 |
|
|
The gradient $ \nabla _{u}{\cal J} |_{u_{[k]}} $ together |
1492 |
|
|
with the value of the cost function itself $ {\cal J}(u_{[k]}) $ |
1493 |
|
|
at iteration step $ k $ serve |
1494 |
|
|
as input to a minimization routine (e.g. quasi-Newton method, |
1495 |
heimbach |
1.9 |
conjugate gradient, ... \cite{gil-lem:89}) |
1496 |
heimbach |
1.4 |
to compute an update in the |
1497 |
adcroft |
1.1 |
control variable for iteration step $k+1$ |
1498 |
|
|
\[ |
1499 |
|
|
u_{[k+1]} \, = \, u_{[0]} \, + \, \Delta u_{[k+1]} |
1500 |
|
|
\quad \mbox{satisfying} \quad |
1501 |
|
|
{\cal J} \left( u_{[k+1]} \right) \, < \, {\cal J} \left( u_{[k]} \right) |
1502 |
|
|
\] |
1503 |
|
|
$ u_{[k+1]} $ then serves as input for a forward/adjoint run |
1504 |
|
|
to determine $ {\cal J} $ and $ \nabla _{u}{\cal J} $ at iteration step |
1505 |
|
|
$ k+1 $. |
1506 |
jmc |
1.23 |
Tab. \ref{ask_the_author:doc_ad_2} sketches the flow between forward/adjoint model |
1507 |
adcroft |
1.1 |
and the minimization routine. |
1508 |
|
|
|
1509 |
jmc |
1.23 |
{\scriptsize |
1510 |
adcroft |
1.1 |
\begin{eqnarray*} |
1511 |
|
|
\begin{array}{ccccc} |
1512 |
|
|
u_{[0]} \,\, , \,\, \Delta u_{[k]} & ~ & ~ & ~ & ~ \\ |
1513 |
|
|
{\Big\downarrow} |
1514 |
|
|
& ~ & ~ & ~ & ~ \\ |
1515 |
|
|
~ & ~ & ~ & ~ & ~ \\ |
1516 |
|
|
\hline |
1517 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1518 |
|
|
\multicolumn{1}{|c}{ |
1519 |
|
|
u_{[k]} = u_{[0]} + \Delta u_{[k]}} & |
1520 |
|
|
\stackrel{\bf forward}{\bf \longrightarrow} & |
1521 |
|
|
v_{[k]} = M \left( u_{[k]} \right) & |
1522 |
|
|
\stackrel{\bf forward}{\bf \longrightarrow} & |
1523 |
|
|
\multicolumn{1}{c|}{ |
1524 |
|
|
{\cal J}_{[k]} = {\cal J} \left( M \left( u_{[k]} \right) \right)} \\ |
1525 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1526 |
|
|
\hline |
1527 |
heimbach |
1.4 |
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1528 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{{\Big\downarrow}} \\ |
1529 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1530 |
adcroft |
1.1 |
\hline |
1531 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1532 |
|
|
\multicolumn{1}{|c}{ |
1533 |
|
|
\nabla_u {\cal J}_{[k]} (\delta {\cal J}) = |
1534 |
heimbach |
1.4 |
T^{\ast} \cdot \nabla_v {\cal J} |_{v_{[k]}} (\delta {\cal J})} & |
1535 |
adcroft |
1.1 |
\stackrel{\bf adjoint}{\mathbf \longleftarrow} & |
1536 |
|
|
ad \, v_{[k]} (\delta {\cal J}) = |
1537 |
|
|
\nabla_v {\cal J} |_{v_{[k]}} (\delta {\cal J}) & |
1538 |
|
|
\stackrel{\bf adjoint}{\mathbf \longleftarrow} & |
1539 |
|
|
\multicolumn{1}{c|}{ ad \, {\cal J} = \delta {\cal J}} \\ |
1540 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1541 |
|
|
\hline |
1542 |
|
|
~ & ~ & ~ & ~ & ~ \\ |
1543 |
heimbach |
1.4 |
\hspace*{15ex}{\Bigg\downarrow} |
1544 |
|
|
\quad {\cal J}_{[k]}, \quad \nabla_u {\cal J}_{[k]} |
1545 |
|
|
& ~ & ~ & ~ & ~ \\ |
1546 |
adcroft |
1.1 |
~ & ~ & ~ & ~ & ~ \\ |
1547 |
|
|
\hline |
1548 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1549 |
|
|
\multicolumn{1}{|c}{ |
1550 |
|
|
{\cal J}_{[k]} \,\, , \,\, \nabla_u {\cal J}_{[k]}} & |
1551 |
|
|
{\mathbf \longrightarrow} & \text{\bf minimisation} & |
1552 |
|
|
{\mathbf \longrightarrow} & |
1553 |
|
|
\multicolumn{1}{c|}{ \Delta u_{[k+1]}} \\ |
1554 |
|
|
\multicolumn{1}{|c}{~} & ~ & ~ & ~ & \multicolumn{1}{c|}{~} \\ |
1555 |
|
|
\hline |
1556 |
|
|
~ & ~ & ~ & ~ & ~ \\ |
1557 |
|
|
~ & ~ & ~ & ~ & \Big\downarrow \\ |
1558 |
|
|
~ & ~ & ~ & ~ & \Delta u_{[k+1]} \\ |
1559 |
|
|
\end{array} |
1560 |
|
|
\end{eqnarray*} |
1561 |
jmc |
1.23 |
} |
1562 |
adcroft |
1.1 |
|
1563 |
|
|
The routines {\it ctrl\_unpack} and {\it ctrl\_pack} provide |
1564 |
|
|
the link between the model and the minimization routine. |
1565 |
jmc |
1.23 |
As described in Section \ref{ask_the_author:doc_ad_2} |
1566 |
adcroft |
1.1 |
the {\it unpack} and {\it pack} routines read and write |
1567 |
|
|
control and gradient {\it vectors} which are compressed |
1568 |
|
|
to contain only wet points, in addition to the full |
1569 |
|
|
2-dim. and 3-dim. fields. |
1570 |
|
|
The corresponding I/O flow looks as follows: |
1571 |
|
|
|
1572 |
|
|
\vspace*{0.5cm} |
1573 |
|
|
|
1574 |
heimbach |
1.4 |
{\scriptsize |
1575 |
adcroft |
1.1 |
\begin{tabular}{ccccc} |
1576 |
|
|
{\bf vector\_ctrl\_$<$k$>$ } & ~ & ~ & ~ & ~ \\ |
1577 |
|
|
{\big\downarrow} & ~ & ~ & ~ & ~ \\ |
1578 |
|
|
\cline{1-1} |
1579 |
|
|
\multicolumn{1}{|c|}{\it ctrl\_unpack} & ~ & ~ & ~ & ~ \\ |
1580 |
|
|
\cline{1-1} |
1581 |
|
|
{\big\downarrow} & ~ & ~ & ~ & ~ \\ |
1582 |
|
|
\cline{3-3} |
1583 |
|
|
\multicolumn{1}{l}{\bf xx\_theta0...$<$k$>$} & ~ & |
1584 |
|
|
\multicolumn{1}{|c|}{~} & ~ & ~ \\ |
1585 |
heimbach |
1.4 |
\multicolumn{1}{l}{\bf xx\_salt0...$<$k$>$} & |
1586 |
|
|
$\stackrel{\mbox{read}}{\longrightarrow}$ & |
1587 |
adcroft |
1.1 |
\multicolumn{1}{|c|}{forward integration} & ~ & ~ \\ |
1588 |
|
|
\multicolumn{1}{l}{\bf \vdots} & ~ & \multicolumn{1}{|c|}{~} |
1589 |
|
|
& ~ & ~ \\ |
1590 |
|
|
\cline{3-3} |
1591 |
heimbach |
1.4 |
~ & ~ & $\downarrow$ & ~ & ~ \\ |
1592 |
adcroft |
1.1 |
\cline{3-3} |
1593 |
|
|
~ & ~ & |
1594 |
|
|
\multicolumn{1}{|c|}{~} & ~ & |
1595 |
|
|
\multicolumn{1}{l}{\bf adxx\_theta0...$<$k$>$} \\ |
1596 |
|
|
~ & ~ & \multicolumn{1}{|c|}{adjoint integration} & |
1597 |
heimbach |
1.4 |
$\stackrel{\mbox{write}}{\longrightarrow}$ & |
1598 |
adcroft |
1.1 |
\multicolumn{1}{l}{\bf adxx\_salt0...$<$k$>$} \\ |
1599 |
|
|
~ & ~ & \multicolumn{1}{|c|}{~} |
1600 |
|
|
& ~ & \multicolumn{1}{l}{\bf \vdots} \\ |
1601 |
|
|
\cline{3-3} |
1602 |
|
|
~ & ~ & ~ & ~ & {\big\downarrow} \\ |
1603 |
|
|
\cline{5-5} |
1604 |
|
|
~ & ~ & ~ & ~ & \multicolumn{1}{|c|}{\it ctrl\_pack} \\ |
1605 |
|
|
\cline{5-5} |
1606 |
|
|
~ & ~ & ~ & ~ & {\big\downarrow} \\ |
1607 |
|
|
~ & ~ & ~ & ~ & {\bf vector\_grad\_$<$k$>$ } \\ |
1608 |
|
|
\end{tabular} |
1609 |
heimbach |
1.4 |
} |
1610 |
adcroft |
1.1 |
|
1611 |
|
|
\vspace*{0.5cm} |
1612 |
|
|
|
1613 |
|
|
|
1614 |
heimbach |
1.4 |
{\it ctrl\_unpack} reads the updated control vector |
1615 |
adcroft |
1.1 |
{\bf vector\_ctrl\_$<$k$>$}. |
1616 |
|
|
It distributes the different control variables to |
1617 |
|
|
2-dim. and 3-dim. files {\it xx\_...$<$k$>$}. |
1618 |
heimbach |
1.4 |
At the start of the forward integration the control variables |
1619 |
|
|
are read from {\it xx\_...$<$k$>$} and added to the |
1620 |
|
|
field. |
1621 |
|
|
Correspondingly, at the end of the adjoint integration |
1622 |
|
|
the adjoint fields are written |
1623 |
adcroft |
1.1 |
to {\it adxx\_...$<$k$>$}, again via the active file routines. |
1624 |
heimbach |
1.4 |
Finally, {\it ctrl\_pack} collects all adjoint files |
1625 |
adcroft |
1.1 |
and writes them to the compressed vector file |
1626 |
|
|
{\bf vector\_grad\_$<$k$>$}. |