1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin mul_level_adolc_ode.cpp}
{xrst_spell
adouble
cccc
}
Taylor's Ode Solver: A Multi-Level Adolc Example and Test
#########################################################
See Also
********
:ref:`taylor_ode.cpp-name` , :ref:`mul_level_ode.cpp-name`
Purpose
*******
This is a realistic example using
two levels of AD; see :ref:`mul_level-name` .
The first level uses Adolc's ``adouble`` type
to tape the solution of an ordinary differential equation.
This solution is then differentiated with respect to a parameter vector.
The second level uses CppAD's type ``AD<adouble>``
to take derivatives during the solution of the differential equation.
These derivatives are used in the application
of Taylor's method to the solution of the ODE.
ODE
***
For this example the function
:math:`y : \B{R} \times \B{R}^n \rightarrow \B{R}^n` is defined by
:math:`y(0, x) = 0` and
:math:`\partial_t y(t, x) = g(y, x)` where
:math:`g : \B{R}^n \times \B{R}^n \rightarrow \B{R}^n` is defined by
.. math::
g(y, x) =
\left( \begin{array}{c}
x_0 \\
x_1 y_0 \\
\vdots \\
x_{n-1} y_{n-2}
\end{array} \right)
ODE Solution
************
The solution for this example can be calculated by
starting with the first row and then using the solution
for the first row to solve the second and so on.
Doing this we obtain
.. math::
y(t, x ) =
\left( \begin{array}{c}
x_0 t \\
x_1 x_0 t^2 / 2 \\
\vdots \\
x_{n-1} x_{n-2} \ldots x_0 t^n / n !
\end{array} \right)
Derivative of ODE Solution
**************************
Differentiating the solution above,
with respect to the parameter vector :math:`x`,
we notice that
.. math::
\partial_x y(t, x ) =
\left( \begin{array}{cccc}
y_0 (t,x) / x_0 & 0 & \cdots & 0 \\
y_1 (t,x) / x_0 & y_1 (t,x) / x_1 & 0 & \vdots \\
\vdots & \vdots & \ddots & 0 \\
y_{n-1} (t,x) / x_0 & y_{n-1} (t,x) / x_1 & \cdots & y_{n-1} (t,x) / x_{n-1}
\end{array} \right)
Taylor's Method Using AD
************************
We define the function :math:`z(t, x)` by the equation
.. math::
z ( t , x ) = g[ y ( t , x ) ] = h [ x , y( t , x ) ]
see :ref:`taylor_ode-name` for the method used to compute the
Taylor coefficients w.r.t :math:`t` of :math:`y(t, x)`.
base_adolc.hpp
**************
The file :ref:`base_adolc.hpp-name` is implements the
:ref:`Base type requirements<base_require-name>` where *Base*
is ``adolc`` .
Memory Management
*****************
Adolc uses raw memory arrays that depend on the number of
dependent and independent variables.
The :ref:`thread_alloc-name` memory management utilities
:ref:`create_array<ta_create_array-name>` and
:ref:`delete_array<ta_delete_array-name>`
are used to manage this memory allocation.
Configuration Requirement
*************************
This example will be compiled and tested provided
:ref:`cmake@include_ipopt` is on the cmake command line.
Source
******
{xrst_literal
// BEGIN C++
// END C++
}
{xrst_end mul_level_adolc_ode.cpp}
--------------------------------------------------------------------------
*/
// BEGIN C++
// suppress conversion warnings before other includes
# include <cppad/wno_conversion.hpp>
//
# include <adolc/adouble.h>
# include <adolc/taping.h>
# include <adolc/drivers/drivers.h>
// definitions not in Adolc distribution and required to use CppAD::AD<adouble>
# include <cppad/example/base_adolc.hpp>
# include <cppad/cppad.hpp>
// ==========================================================================
namespace { // BEGIN empty namespace
// define types for each level
typedef adouble a1type;
typedef CppAD::AD<a1type> a2type;
// -------------------------------------------------------------------------
// class definition for C++ function object that defines ODE
class Ode {
private:
// copy of a that is set by constructor and used by g(y)
CPPAD_TESTVECTOR(a1type) a1x_;
public:
// constructor
Ode(const CPPAD_TESTVECTOR(a1type)& a1x) : a1x_(a1x)
{ }
// the function g(y) is evaluated with two levels of taping
CPPAD_TESTVECTOR(a2type) operator()
( const CPPAD_TESTVECTOR(a2type)& a2y) const
{ size_t n = a2y.size();
CPPAD_TESTVECTOR(a2type) a2g(n);
size_t i;
a2g[0] = a1x_[0];
for(i = 1; i < n; i++)
a2g[i] = a1x_[i] * a2y[i-1];
return a2g;
}
};
// -------------------------------------------------------------------------
// Routine that uses Taylor's method to solve ordinary differential equaitons
// and allows for algorithmic differentiation of the solution.
CPPAD_TESTVECTOR(a1type) taylor_ode_adolc(
Ode G , // function that defines the ODE
size_t order , // order of Taylor's method used
size_t nstep , // number of steps to take
const a1type &a1dt , // Delta t for each step
const CPPAD_TESTVECTOR(a1type) &a1y_ini) // y(t) at the initial time
{
// some temporary indices
size_t i, k, ell;
// number of variables in the ODE
size_t n = a1y_ini.size();
// copies of x and g(y) with two levels of taping
CPPAD_TESTVECTOR(a2type) a2y(n), Z(n);
// y, y^{(k)} , z^{(k)}, and y^{(k+1)}
CPPAD_TESTVECTOR(a1type) a1y(n), a1y_k(n), a1z_k(n), a1y_kp(n);
// initialize x
for(i = 0; i < n; i++)
a1y[i] = a1y_ini[i];
// loop with respect to each step of Taylors method
for(ell = 0; ell < nstep; ell++)
{ // prepare to compute derivatives using a1type
for(i = 0; i < n; i++)
a2y[i] = a1y[i];
CppAD::Independent(a2y);
// evaluate ODE using a2type
Z = G(a2y);
// define differentiable version of g: X -> Y
// that computes its derivatives using a1type
CppAD::ADFun<a1type> a1g(a2y, Z);
// Use Taylor's method to take a step
a1y_k = a1y; // initialize y^{(k)}
a1type dt_kp = a1dt; // initialize dt^(k+1)
for(k = 0; k <= order; k++)
{ // evaluate k-th order Taylor coefficient of y
a1z_k = a1g.Forward(k, a1y_k);
for(i = 0; i < n; i++)
{ // convert to (k+1)-Taylor coefficient for x
a1y_kp[i] = a1z_k[i] / a1type(k + 1);
// add term for to this Taylor coefficient
// to solution for y(t, x)
a1y[i] += a1y_kp[i] * dt_kp;
}
// next power of t
dt_kp *= a1dt;
// next Taylor coefficient
a1y_k = a1y_kp;
}
}
return a1y;
}
} // END empty namespace
// ==========================================================================
// Routine that tests algorithmic differentiation of solutions computed
// by the routine taylor_ode.
bool mul_level_adolc_ode(void)
{ bool ok = true;
double eps = 100. * std::numeric_limits<double>::epsilon();
// number of components in differential equation
size_t n = 4;
// some temporary indices
size_t i, j;
// set up for thread_alloc memory allocator
using CppAD::thread_alloc; // the allocator
size_t capacity; // capacity of an allocation
// the vector x with length n (or greater) in double
double* x = thread_alloc::create_array<double>(n, capacity);
// the vector x with length n in a1type
CPPAD_TESTVECTOR(a1type) a1x(n);
for(i = 0; i < n; i++)
a1x[i] = x[i] = double(i + 1);
// declare the parameters as the independent variable
short tag = 0; // Adolc setup
int keep = 1;
trace_on(tag, keep);
for(i = 0; i < n; i++)
a1x[i] <<= double(i + 1); // a1x is independent for adouble type
// arguments to taylor_ode_adolc
Ode G(a1x); // function that defines the ODE
size_t order = n; // order of Taylor's method used
size_t nstep = 2; // number of steps to take
a1type a1dt = 1.; // Delta t for each step
// value of y(t, x) at the initial time
CPPAD_TESTVECTOR(a1type) a1y_ini(n);
for(i = 0; i < n; i++)
a1y_ini[i] = 0.;
// integrate the differential equation
CPPAD_TESTVECTOR(a1type) a1y_final(n);
a1y_final = taylor_ode_adolc(G, order, nstep, a1dt, a1y_ini);
// declare the differentiable function f : x -> y_final
// (corresponding to the tape of adouble operations)
double* y_final = thread_alloc::create_array<double>(n, capacity);
for(i = 0; i < n; i++)
a1y_final[i] >>= y_final[i];
trace_off();
// check function values
double check = 1.;
double t = nstep * a1dt.value();
for(i = 0; i < n; i++)
{ check *= x[i] * t / double(i + 1);
ok &= CppAD::NearEqual(y_final[i], check, eps, eps);
}
// memory where Jacobian will be returned
double* jac_ = thread_alloc::create_array<double>(n * n, capacity);
double** jac = thread_alloc::create_array<double*>(n, capacity);
for(i = 0; i < n; i++)
jac[i] = jac_ + i * n;
// evaluate Jacobian of h at a
size_t m = n; // # dependent variables
jacobian(tag, int(m), int(n), x, jac);
// check Jacobian
for(i = 0; i < n; i++)
{ for(j = 0; j < n; j++)
{ if( i < j )
check = 0.;
else
check = y_final[i] / x[j];
ok &= CppAD::NearEqual(jac[i][j], check, eps, eps);
}
}
// make memory available for other use by this thread
thread_alloc::delete_array(x);
thread_alloc::delete_array(y_final);
thread_alloc::delete_array(jac_);
thread_alloc::delete_array(jac);
return ok;
}
// END C++
|