Example description
/* D02PS_T1W_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_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 = 2, npts = 16, nwant = 1;
  const Integer     liwsav = 130;
  const Integer     lrwsav = 350 + 32 * n;
  const Integer     lwcomm = n + 5 * nwant;

  Integer           exit_status = 0;

  nagad_t1w_w_rtype *rwsav = 0, *thresh = 0, *ynow = 0, *wcomm = 0;
  nagad_t1w_w_rtype *ypnow = 0, *y = 0, *ywant = 0, *ypwant = 0, ruser[2];
  Integer           *iwsav = 0, iuser[1];
  double            dr[2];
  
  cout << "D02PS_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];
  ywant  = new nagad_t1w_w_rtype [n];
  ypnow  = new nagad_t1w_w_rtype [n];
  ypwant = new nagad_t1w_w_rtype [n];
  iwsav  = new Integer [liwsav];
  rwsav  = new nagad_t1w_w_rtype [lrwsav];
  wcomm  = new nagad_t1w_w_rtype [lwcomm];

  // Set initial conditions for ODE and parameters for the integrator.
  Integer           method = -1;
  nagad_t1w_w_rtype tol, hstart, tend, tstart;
  tstart    = 0.0;
  tol       = 1.0e-4;
  tend      = 2.0*nag_math_pi;
  hstart    = 0.0;
  for (int k = 0; k < n; k++) {
    thresh[k] = 1.0e-8;
  }
  ruser[0] = 1.0;
  ruser[1] = 1.0;
  y[0] = 0.0;
  y[1] = 1.0;
  
  {
    double tolr = nagad_t1w_get_value(tol);
    cout << "\n  Calculation with tol = " << tolr << endl;
  }
  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  {
    double t = nagad_t1w_get_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;
  x10aa_t1w_f_(ad_handle,ifail);
  
  for (int k = 0; k < n; k++) {
    double yr = nagad_t1w_get_value(y[k]);
    cout.width(10); cout << yr;
  }
  cout << endl;

  for (int i = 0; i < 2; ++i) {
    double inc = 1.0;
    nagad_t1w_inc_derivative(&ruser[i],inc);
    // Initialize Runge-Kutta method for integrating ODE
    ifail = 0;
    d02pq_t1w_f_(ad_handle,n,tstart,tend,y,tol,thresh,method,hstart,
                 iwsav,rwsav,ifail);

    nagad_t1w_w_rtype tnow, twant, tinc;
    tinc = (tend-tstart)/( (double) npts);
    twant = tstart + tinc;
    tnow = tstart;
    while (tnow<tend) {
      ifail = 0;
      d02pf_t1w_f_(ad_handle,f,n,tnow,ynow,ypnow,iuser,ruser,
                   iwsav,rwsav,ifail);
      while (twant <= tnow) {
        Integer ideriv = 2;
        ifail = 0;
        d02ps_t1w_f_(ad_handle,n,twant,ideriv,nwant,ywant,ypwant,f,
                     wcomm,lwcomm,iuser,ruser,iwsav,rwsav,ifail);

        if (i==0) {
          cout.width(6); cout << nagad_t1w_get_value(twant);
          cout.width(10); cout << nagad_t1w_get_value(ywant[0]);
          cout.width(10); cout << nagad_t1w_get_value(ypwant[0]);
          cout << endl;
        }
        
        twant = twant + tinc;
      }
    }
    
    dr[i] = nagad_t1w_get_derivative(ywant[0]);
    double zero = 0.0;
    nagad_t1w_set_derivative(&ruser[i],zero);
  }

  nagad_t1w_w_rtype hnext, waste;
  Integer           fevals, stepcost, stepsok;
  ifail = 0;
  d02pt_t1w_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;

  cout << "\n Derivatives calculated: First order tangents\n";
  cout << " Computational mode    : algorithmic\n";
  
  cout << "\n Derivatives: (solution w.r.t. function parameters)\n";

  cout.setf(ios::scientific,ios::floatfield);
  cout.precision(5);
  cout << " dy(t)/druser[0] = ";
  cout.width(12); cout << dr[0] << endl;
  cout << " dy(t)/druser[1] = ";
  cout.width(12); cout << dr[1] << endl;

  ifail =0;
  x10ab_t1w_f_(ad_handle,ifail);

  delete [] thresh;
  delete [] ynow;
  delete [] y;
  delete [] ywant;
  delete [] ypnow;
  delete [] ypwant;
  delete [] iwsav;
  delete [] rwsav;
  delete [] wcomm;
  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[])
{
  yp[0] = ruser[0]*y[1];
  yp[1] = -ruser[1]*y[0];
}