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

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

#include <dco.hpp>
#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 nagad_t1w_w_rtype &t,
                         const Integer &          n,
                         const nagad_t1w_w_rtype  y[],
                         nagad_t1w_w_rtype        yp[],
                         Integer                  iuser[],
                         nagad_t1w_w_rtype        ruser[]);
#ifdef __cplusplus
}
#endif

int main(void)
{
  const Integer n = 4, npts = 6;
  const Integer liwsav = 130;
  const Integer lrwsav = 350 + 32 * n;

  Integer exit_status = 0;

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

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

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

  // Set initial conditions for ODE and parameters for the integrator.
  Integer           method = -3;
  nagad_t1w_w_rtype eps, tol, hstart, tend, tstart;
  eps    = 0.7;
  tstart = 0.0;
  tol    = 1.0e-4;
  tend   = 6.0 * nag_math_pi;
  hstart = 0.0;
  for (int k = 0; k < n; k++)
    {
      thresh[k] = 1.0e-10;
    }

  {
    double tolr = dco::value(tol);
    cout << "\n  Calculation with tol = " << tolr << endl;
  }
  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  {
    double t = dco::value(tstart);
    cout << "\n    t         y1        y2" << endl;
    cout.width(6);
    cout << t;
  }

  // Create AD configuration data object
  Integer ifail     = 0;
  void *  ad_handle = 0;
  nag::ad::x10aa(ad_handle, ifail);

  double inc           = 1.0;
  dco::derivative(eps) = inc;

  y[0] = 1.0 - eps;
  y[1] = 0.0;
  y[2] = 0.0;
  y[3] = sqrt((1.0 + eps) / (1.0 - eps));
  for (int k = 0; k < n; k++)
    {
      double yr = dco::value(y[k]);
      cout.width(10);
      cout << yr;
    }
  cout << endl;

  nagad_t1w_w_rtype tnow, twant, tinc;
  tinc  = (tend - tstart) / ((double)npts);
  twant = tstart + tinc;

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

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

  nagad_t1w_w_rtype 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;

  cout << "\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";

  // Get derivatives
  cout << "\n Derivatives: (solution w.r.t. eps)\n";

  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(5);
  double deps;
  deps = dco::derivative(ynow[0]);
  cout << " dy(t)/deps = ";
  cout.width(12);
  cout << deps << endl;

  ifail = 0;
  nag::ad::x10ab(ad_handle, ifail);

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

static void NAG_CALL f(void *&                  ad_handle,
                       const nagad_t1w_w_rtype &t,
                       const Integer &          n,
                       const nagad_t1w_w_rtype  y[],
                       nagad_t1w_w_rtype        yp[],
                       Integer                  iuser[],
                       nagad_t1w_w_rtype        ruser[])
{
  nagad_t1w_w_rtype r;
  r     = 1.0 / sqrt(y[0] * y[0] + y[1] * y[1]);
  r     = r * r * r;
  yp[0] = y[2];
  yp[1] = y[3];
  yp[2] = -y[0] * r;
  yp[3] = -y[1] * r;
}