NAG Library Manual, Mark 27.2
Interfaces:  FL   CL   CPP   AD 

NAG AD Library Introduction
Example description
/* D02PF_P0W_F C++ Header Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.2, 2021.
 */

#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
using namespace std;

#ifdef __cplusplus
extern "C"
{
#endif
  static void NAG_CALL f(void *&        ad_handle,
                         const double & t,
                         const Integer &n,
                         const double   y[],
                         double         yp[],
                         Integer        iuser[],
                         double         ruser[]);
#ifdef __cplusplus
}
#endif

int main(void)
{
  const Integer n      = 2;
  const Integer liwsav = 130;
  const Integer lrwsav = 350 + 32 * n;

  Integer exit_status = 0;

  double * rwsav = 0, *thresh = 0, *ynow = 0;
  double * ypnow = 0, *y = 0, ruser[2];
  Integer *iwsav = 0, iuser[1];

  cout << "D02PF_P0W_F C++ Header Example Program Results\n\n";

  thresh = new double[n];
  ynow   = new double[n];
  y      = new double[n];
  ypnow  = new double[n];
  iwsav  = new Integer[liwsav];
  rwsav  = new double[lrwsav];

  // Set initial conditions for ODE and parameters for the integrator.
  Integer method = 2;
  double  tol, hstart, tend, tstart;
  tstart    = 0.0;
  tol       = 1.0e-4;
  tend      = 2.0 * nag_math_pi;
  y[0]      = 0.0;
  y[1]      = 1.0;
  hstart    = 0.0;
  thresh[0] = 1.0e-8;
  thresh[1] = 1.0e-8;
  ruser[0]  = 1.0;
  ruser[1]  = 1.0;

  cout << "\n  Calculation with tol = " << tol << endl;
  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  cout << "\n    t         y1        y2" << endl;
  cout.width(6);
  cout << tstart;
  for (int k = 0; k < n; k++)
    {
      cout.width(10);
      cout << y[k];
    }
  cout << endl;

  Integer ifail     = 0;
  void *  ad_handle = 0;

  // Initialize Runge-Kutta method for integrating ODE
  ifail = 0;
  nag::ad::d02pq(ad_handle, n, tstart, tend, y, tol, thresh, method, hstart,
                 iwsav, rwsav, ifail);

  double tnow;
  tnow = tstart;
  do
    {
      ifail = 0;
      nag::ad::d02pf(ad_handle, f, n, tnow, ynow, ypnow, -1, iuser, -1, ruser,
                     iwsav, rwsav, ifail);
      cout.width(6);
      cout << tnow;
      for (int k = 0; k < n; ++k)
        {
          cout.width(10);
          cout << ynow[k];
        }
      cout << endl;
    }
  while (tnow < tend);

  double  hnext, waste;
  Integer fevals, stepcost, stepsok;
  ifail = 0;
  nag::ad::d02pt(ad_handle, fevals, stepcost, waste, stepsok, hnext, iwsav,
                 rwsav, ifail);
  cout << "\n Cost of the integration in evaluations of f is " << fevals;
  cout << endl;

  delete[] thresh;
  delete[] ynow;
  delete[] y;
  delete[] ypnow;
  delete[] iwsav;
  delete[] rwsav;
  return exit_status;
}

static void NAG_CALL f(void *&        ad_handle,
                       const double & t,
                       const Integer &n,
                       const double   y[],
                       double         yp[],
                       Integer        iuser[],
                       double         ruser[])
{
  yp[0] = ruser[0] * y[1];
  yp[1] = -ruser[1] * y[0];
}