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

NAG AD Library Introduction
Example description
/* D01BD_A1W_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 <nagx01.h>
#include <stdio.h>
using namespace std;

extern "C"
{
  static void NAG_CALL f(void *&                  ad_handle,
                         const nagad_a1w_w_rtype &x,
                         nagad_a1w_w_rtype &      ret,
                         Integer                  iuser[],
                         nagad_a1w_w_rtype        ruser[]);
}

int main(void)
{
  // Scalars
  int exit_status = 0;

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

  // Skip first line of data file
  string mystr;
  getline(cin, mystr);

  // Read problem parameters
  double ar, br, epsabsr, epsrelr;
  cin >> ar;
  cin >> br;
  cin >> epsabsr;
  cin >> epsrelr;

  nagad_a1w_w_rtype a, b, epsabs, epsrel;
  a      = ar;
  b      = br;
  epsabs = epsabsr;
  epsrel = epsrelr;

  // Create AD tape
  dco::ga1s<double>::global_tape = dco::ga1s<double>::tape_t::create();

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

  nagad_a1w_w_rtype result, abserr, ruser[1];
  Integer           iuser[1];

  ruser[0] = 10.0;
  iuser[0] = 0;

  // Register variables to differentiate w.r.t.
  dco::ga1s<double>::global_tape->register_variable(ruser[0]);

  // Call the AD routine

  ifail = 0;
  nag::ad::d01bd(ad_handle, f, a, b, epsabs, epsrel, result, abserr, -1, iuser,
                 -1, ruser, ifail);

  // Print inputs and primal outputs.
  cout << "\n lower limit of integration (a) = " << ar << endl;
  cout << " upper limit of integration (b) = " << br << endl;
  cout << " absolute accuracy requested    = " << epsabsr << endl;
  cout << " relative accuracy requested    = " << epsrelr << endl;
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  if (ifail >= 0)
    {
      cout << "\n approximation to the integral  : " << dco::value(result)
           << endl;
      cout << " estimate of the absolute error : " << dco::value(abserr)
           << endl;
      cout << " number of function evaluations : " << iuser[0] << endl;
    }

  // Setup evaluation of derivatives via adjoints.
  double inc = 1.0;
  dco::derivative(result) += inc;
  ifail                                              = 0;
  dco::ga1s<double>::global_tape->sparse_interpret() = true;
  dco::ga1s<double>::global_tape->interpret_adjoint();

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

  // Get derivatives
  double dr = dco::derivative(ruser[0]);

  cout << "\n Derivative of solution w.r.t to ruser[0]:\n";
  cout << " d/dr(x) = " << dr << endl;

  // Remove computational data object and tape
  nag::ad::x10ab(ad_handle, ifail);
  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  return exit_status;
}

static void NAG_CALL f(void *&                  ad_handle,
                       const nagad_a1w_w_rtype &x,
                       nagad_a1w_w_rtype &      ret,
                       Integer                  iuser[],
                       nagad_a1w_w_rtype        ruser[])
{
  // dco/c++ used here to perform AD of the following
  double            pi = X01AAC;
  nagad_a1w_w_rtype prx, x2, s;
  x2  = x * x;
  prx = pi * ruser[0];
  prx = prx * x;
  s   = sin(prx);
  ret = x2 * s;
  iuser[0]++;

  return;
}