/* D02PE_A1W_F C++ Header Example Program.
*
* Copyright 2020 Numerical Algorithms Group.
*
* Mark 27.1, 2020.
*/
#include <nag.h>
#include <dco.hpp>
#include <nagad.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C"
{
#endif
static void NAG_CALL f(void * &ad_handle, const nagad_a1w_w_rtype &t,
const Integer &n, const nagad_a1w_w_rtype y[],
nagad_a1w_w_rtype yp[],
Integer iuser[], nagad_a1w_w_rtype ruser[]);
#ifdef __cplusplus
}
#endif
int main(void)
{
const Integer n = 2, npts = 8;
const Integer liwsav = 130;
const Integer lrwsav = 350 + 32 * n;
Integer exit_status = 0;
nagad_a1w_w_rtype *rwsav = 0, *thresh = 0, *ygot = 0, *yinit = 0, *ymax = 0;
nagad_a1w_w_rtype *ypgot = 0, *y = 0, ruser[1];
Integer *iwsav = 0, iuser[1];
cout << "D02PE_A1W_F C++ Header Example Program Results\n\n";
thresh = new nagad_a1w_w_rtype [n];
ygot = new nagad_a1w_w_rtype [n];
y = new nagad_a1w_w_rtype [n];
yinit = new nagad_a1w_w_rtype [n];
ypgot = new nagad_a1w_w_rtype [n];
ymax = new nagad_a1w_w_rtype [n];
iwsav = new Integer [liwsav];
rwsav = new nagad_a1w_w_rtype [lrwsav];
// Set initial conditions for ODE and parameters for the integrator.
Integer method = 1;
nagad_a1w_w_rtype tol, hstart, tend, tstart;
tstart = 0.0;
tol = 1.0e-4;
tend = 2.0*nag_math_pi;
yinit[0] = 0.0;
yinit[1] = 1.0;
hstart = 0.0;
thresh[0] = 1.0e-8;
thresh[1] = 1.0e-8;
{
double tolr = nagad_a1w_get_value(tol);
cout << "\n Calculation with tol = " << tolr << endl;
}
cout.setf(ios::fixed);
cout.setf(ios::right);
cout.precision(3);
{
double t = nagad_a1w_get_value(tstart);
cout << "\n t y1 y2" << endl;
cout.width(6); cout << t;
}
for (int k = 0; k < n; k++) {
double yr = nagad_a1w_get_value(yinit[k]);
cout.width(10); cout << yr;
}
cout << endl;
// Set control for output
double tinc = 2.0*nag_math_pi / (double) (npts);
// Create AD tape
nagad_a1w_ir_create();
// Create AD configuration data object
Integer ifail = 0;
void *ad_handle = 0;
x10aa_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_register_variable(&yinit[0]);
nagad_a1w_ir_register_variable(&yinit[1]);
y[0] = yinit[0];
y[1] = yinit[1];
// Initialize Runge-Kutta method for integrating ODE
ifail = 0;
d02pq_a1w_f_(ad_handle,n,tstart,tend,y,tol,thresh,method,hstart,
iwsav,rwsav,ifail);
nagad_a1w_w_rtype tgot, twant;
twant = tstart;
for (int j = 0; j < npts; ++j) {
twant = twant + tinc;
ifail = 0;
d02pe_a1w_f_(ad_handle,f,n,twant,tgot,ygot,ypgot,ymax,iuser,ruser,
iwsav,rwsav,ifail);
cout.width(6); cout << nagad_a1w_get_value(tgot);
for (int k = 0; k < n; ++k) {
cout.width(10); cout << nagad_a1w_get_value(ygot[k]);
}
cout << endl;
}
nagad_a1w_w_rtype hnext, waste;
Integer fevals, stepcost, stepsok;
ifail = 0;
d02pt_a1w_f_(ad_handle,fevals,stepcost,waste,stepsok,hnext,iwsav,
rwsav,ifail);
cout << "\n Cost of the integration in evaluations of f is " << fevals;
cout << endl;
// Setup evaluation of derivatives via adjoints.
double inc = 1.0;
nagad_a1w_inc_derivative(&ygot[0],inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
// Get derivatives
cout << "\n Derivatives: (solution w.r.t. initial values)\n";
cout.setf(ios::scientific,ios::floatfield);
cout.precision(5);
double dr;
dr = nagad_a1w_get_derivative(yinit[0]);
cout << " dy(t)/dy0 = ";
cout.width(12); cout << dr << endl;
dr = nagad_a1w_get_derivative(yinit[1]);
cout << " dy(t)/dy0' = ";
cout.width(12); cout << dr << endl;
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
delete [] thresh;
delete [] ygot;
delete [] y;
delete [] yinit;
delete [] ypgot;
delete [] ymax;
delete [] iwsav;
delete [] rwsav;
return exit_status;
}
static void NAG_CALL f(void * &ad_handle, const nagad_a1w_w_rtype &t,
const Integer &n, const nagad_a1w_w_rtype y[],
nagad_a1w_w_rtype yp[],
Integer iuser[], nagad_a1w_w_rtype ruser[])
{
yp[0] = y[1];
yp[1] = -y[0];
}