%===================================================================================
\section{Adjoint sensitivity analysis example problems}\label{s:adj_examples}
%===================================================================================

The next two subsections describe a serial example (\id{idasAkzoNob\_ASAi\_dns}) and
a parallel one (\id{idasBruss\_ASAp\_kry\_bbd\_p}).
For details on the other examples, the reader is directed to the comments in their
source files.

%------------------------------------------------------------------------
\subsection{A serial dense example: idasAkzoNob\_ASAi\_dns}\label{ss:idasAkzoNob_ASAi_dns}

The \id{idasAkzoNob\_ASAi\_dns} program solves the Akzo-Nobel chemical kinetics
problem, which consists of six nonlinear DAEs.  The system has index 1.
The problem originates from Akzo Nobel Central research in Arnhern,
The Netherlands, and describes a chemical process in which two species
are mixed, while carbon dioxide is continuously added.

The problem is of the form
\begin{equation}\label{e:DAEan}
\begin{split}
  y' &= f(y, z) \\
  0  &= g(y, z)
\end{split}
\end{equation}
with $y \in R^5$ and $z \in R$. The function $f$ is defined by
\begin{equation*}
  f(y, z) = \begin{bmatrix}
    -2r_1           &+r_2 &-r_3 &-r_4  &                &        \\
    -\frac{1}{2}r_1 &     &     &-r_4  &-\frac{1}{2}r_5 &+F_{in} \\
    r_1             &-r_2 &+r_3 &      &                &        \\
                    &-r_2 &+r_3 &-2r_4 &                &        \\
                    &r_2  &-r_3 &      &+r_5            &
  \end{bmatrix}
\end{equation*}
where the $r_i$ and $F_{in}$ are auxiliary variables, given by
\begin{equation*}
  \begin{split}
    r_1 & = k_1 y_1^4 y_2^{1/2} \\
    r_2 & = k_2 y_3 y_4 \\
    r_3 & = \frac{k_2}{K} y_1 y_5 \\
    r_4 & = k_3 y_1 y_4^{1/2} \\
    r_5 & = k_4 z^2 y_2^{1/2} \\
    F_{in} & = klA \left( \frac{p(CO_2)}{H} - y_2 \right) ~.
  \end{split}
\end{equation*}
The function $g$ in the algebraic equation is defined by
\begin{equation*}
  g(y, z) = K_s y_1 y_4 - z ~.
\end{equation*}
It is clear from the fact that the Jacobian $\partial g / \partial z$ is non-singular
that the DAE (\ref{e:DAEan}) has (differentiation) index 1.
See http://pitagora.dm.uniba.it/$\sim$testset/report/chemakzo.pdf for details.


The problem is solved with the {\sunlinsoldense} linear solver using the
default difference quotient dense Jacobian approximation.
%%
The adjoint capability of {\idas} is used to compute gradients with
respect to the initial values of $y$ of the integral
\begin{equation*}
  G = \int_0^{t_f} y_1 dt ~,
\end{equation*}
where $y_1$ is the concentration of the first species. The initial value of $z$
cannot be taken as a free parameter, since its value is determined by the value
of $y$. The sensitivity of $G$ with respect to the initial values of $y$ is
given by the first five components of the solution of the adjoint system,
evaluated at $t = 0$.

The output generated by \id{idasAkzoNob\_ASAi\_dns} is shown below.

\includeOutput{idasAkzoNob\_ASAi\_dns}{../../examples/idas/serial/idasAkzoNob_ASAi_dns.out}


%--------------------------------------------------------------------------

\subsection{A parallel example using IDABBDPRE: idasBruss\_ASAp\_kry\_bbd\_p}
\label{ss:idasBruss_ASAp_kry_bbd_p}

The \id{idasBruss\_ASAp\_kry\_bbd\_p} program solves the same problem as
\id{idasBruss\_kry\_bbd\_p} and \id{idasBruss\_FSA\_kry\_bbd\_p}, namely the
Brusselator PDE system.  (See \S\ref{ss:idasBruss_FSA_kry_bbd_p} above.)
In addition, it uses an adjoint sensitivity approach to compute the
gradients of the model output functional
\begin{equation*}
  g(t) = \int \int u(t,x,y)~dx~dy ~.
\end{equation*}
For perturbations $\delta u_0$ and $\delta v_0$ in the initial profiles
$u$ and $v$, the perturbation of $g$ at the final time is
\begin{equation*}
  \delta g(t_f) = \int \int [\lambda(0,x,y) \delta u_0 + \mu(0,x,y) \delta v_0] ~dx~dy ~,
\end{equation*}
where $\lambda(t,x,y)$ and $\mu(t,x,y)$ are the solutions of the adjoint PDEs,
\begin{equation*}
\begin{split}
  &\partial \lambda / \partial t = -\epsilon_1 (\lambda_{xx} + \lambda_{yy})
                              - (2 u v - B - 1) \lambda + (2 u v - B) \mu \\
  &\partial \mu / \partial t = -\epsilon_2 (\mu_{xx} + \mu_{yy})
                              - u^2 \lambda + u^2 \mu ~,
\end{split}
\end{equation*}
with Neumann boundary conditions, and initial (final time) conditions
\begin{equation*}
  \lambda (t_f,x,y) = 1 ~, ~~~~ \mu (t_f,x,y) =  0 ~.
\end{equation*}
The adjoint PDEs are discretized and solved in the same way as the
Brusselator PDEs.

A sample output generated by \id{idasBruss\_ASAp\_kry\_bbd\_p} is shown below.

\includeOutput{idasBruss\_ASAp\_kry\_bbd\_p}{../../examples/idas/parallel/idasBruss_ASAp_kry_bbd_p.out}
