Example description
/* D02PU_P0W_F C++ Header Example Program.
 *
 * Copyright 2020 Numerical Algorithms Group.
 *
 * Mark 27.1, 2020.
 */

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

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

  Integer           exit_status = 0;

  double            *rwsav = 0, *thresh = 0, *ygot = 0, *ymax = 0;
  double            *ypgot = 0, *y = 0, ruser[1];
  Integer           *iwsav = 0, iuser[1];
  double            *rmserr = 0;

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

  thresh = new double [n];
  ygot   = new double [n];
  y      = new double [n];
  ypgot  = new double [n];
  ymax   = new double [n];
  iwsav  = new Integer [liwsav];
  rwsav  = new double [lrwsav];
  rmserr = new double [n];

  // Set initial conditions for ODE and parameters for the integrator.
  Integer           method = 3;
  double            tol, hstart, tend, tstart, eps;
  eps    = 0.7;
  tstart = 0.0;
  tol    = 1.0e-6;
  tend   = 3.0*nag_math_pi;
  hstart = 0.0;
  for (int i = 0; i < n; ++i) {
    thresh[i] = 1.0e-10;
  }
  
  cout << "\n  Calculation with tol = " << tol << endl;

  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  cout << "\n    t         y1         y2         y3         y4" << endl;
  cout.width(6); cout << tstart;

  
  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++) {
    cout.width(11); cout << y[k];
  }
  cout << endl;

  // Initialize Runge-Kutta method for integrating ODE
  Integer ifail = 0;
  void    *ad_handle = 0;
  d02pq_p0w_f_(ad_handle,n,tstart,tend,y,tol,thresh,method,hstart,
               iwsav,rwsav,ifail);

  double tgot, twant;
  twant = tend;
  
  ifail = 2;
  while (ifail>1 && ifail<5) {
    ifail = -1;
    d02pe_p0w_f_(ad_handle,f,n,twant,tgot,ygot,ypgot,ymax,iuser,ruser,
                 iwsav,rwsav,ifail);
  }

  if (ifail==0) {
    cout.width(6); cout << tgot;
    for (int k = 0; k < n; ++k) {
      cout.width(11); cout << ygot[k];
    }
    cout << endl;

    // Get Error estimates
    double errmax, terrmx;
    ifail = 0;
    d02pu_p0w_f_(ad_handle,n,rmserr,errmax,terrmx,iwsav,rwsav,ifail);

    cout.setf(ios::scientific,ios::floatfield);
    cout.precision(2);
    cout << "\n Componentwise error assessment\n        ";
    for (int k = 0; k < n; ++k) {
      cout.width(11); cout << rmserr[k];
    }
    cout << endl;
    cout.precision(3);
    cout << "\n Worst global error observed was ";
    cout.width(9); cout << errmax << endl;
    cout << "              it occurred at T = ";
    cout.width(8); cout << terrmx << endl;
    
    double            hnext, waste;
    Integer           fevals, stepcost, stepsok;
    ifail = 0;
    d02pt_p0w_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;
  }    
  delete [] thresh;
  delete [] ygot;
  delete [] y;
  delete [] ypgot;
  delete [] ymax;
  delete [] iwsav;
  delete [] rwsav;
  delete [] rmserr;
  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[])
{
  double 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;
}