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

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

#ifdef __cplusplus
extern "C"
{
#endif
  static void NAG_CALL fcn(void *&                  ad_handle,
                           const nagad_a1w_w_rtype &x,
                           const nagad_a1w_w_rtype  y[],
                           nagad_a1w_w_rtype        f[],
                           Integer                  iuser[],
                           nagad_a1w_w_rtype        ruser[]);
  static void NAG_CALL g(void *&                  ad_handle,
                         const nagad_a1w_w_rtype &x,
                         const nagad_a1w_w_rtype  y[],
                         nagad_a1w_w_rtype &      retval,
                         Integer                  iuser[],
                         nagad_a1w_w_rtype        ruser[]);
#ifdef __cplusplus
}
#endif

int main(void)
{
  int           exit_status = 0;
  const Integer n           = 3;

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

  Integer            iw, iuser[1];
  double             pi;
  nagad_a1w_w_rtype  tol, x, xinit, xend, ruser[2];
  nagad_a1w_w_rtype *y = 0, *yinit = 0, *w = 0;

  iw    = 20 * n;
  w     = new nagad_a1w_w_rtype[iw];
  y     = new nagad_a1w_w_rtype[n];
  yinit = new nagad_a1w_w_rtype[n];

  xinit = 0.0;
  xend  = 10.0;
  pi    = nag_math_pi;

  cout << "\n\nCase: no intermediate output, root-finding\n";
  const double tolr = 1.0e-5;
  tol               = tolr;
  cout << "\n  Calculation with tol = " << tolr << endl;

  // 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);

  yinit[0] = 0.5;
  yinit[1] = 0.5;
  yinit[2] = 0.2 * pi;

  x                  = xinit;
  const double alpha = -0.032;
  const double beta  = -0.02;
  ruser[0]           = alpha;
  ruser[1]           = beta;

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

  y[0] = yinit[0];
  y[1] = yinit[1];
  y[2] = yinit[2];

  ifail = 0;
  nag::ad::d02bj(ad_handle, x, xend, n, y, fcn, tol, "D", nullptr, g, w,
                 -1, iuser, -1, ruser, ifail);

  cout.setf(ios::fixed);
  cout.setf(ios::right);
  cout.precision(3);
  cout << "\n  Root of Y(1) = 0.0 at ";
  cout.width(5);
  cout << x << endl;
  cout << "\n  Solution is  ";
  cout.precision(4);
  for (int i = 0; i < 3; ++i)
    {
      cout.width(10);
      cout << y[i];
    }
  cout << endl;

  // Setup evaluation of derivatives via adjoints.
  double inc = 1.0;
  dco::derivative(x) += 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
  cout << "\n Derivatives: (hit point w.r.t. parameters)\n";
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(5);
  double dr;
  dr = dco::derivative(ruser[0]);
  cout << "     dx/dg      = ";
  cout.width(12);
  cout << -dr << endl;
  dr = dco::derivative(ruser[1]);
  cout << "     dx/ddrag   = ";
  cout.width(12);
  cout << -dr << endl;
  dr = dco::derivative(yinit[0]);
  cout << "     dx/dheight = ";
  cout.width(12);
  cout << dr << endl;
  dr = dco::derivative(yinit[1]);
  cout << "     dx/dvel    = ";
  cout.width(12);
  cout << dr << endl;
  dr = dco::derivative(yinit[2]);
  cout << "     dx/dangle  = ";
  cout.width(12);
  cout << dr << endl;

  nag::ad::x10ab(ad_handle, ifail);
  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  delete[] w;
  delete[] y;
  delete[] yinit;
  return exit_status;
}

static void NAG_CALL fcn(void *&                  ad_handle,
                         const nagad_a1w_w_rtype &x,
                         const nagad_a1w_w_rtype  y[],
                         nagad_a1w_w_rtype        f[],
                         Integer                  iuser[],
                         nagad_a1w_w_rtype        ruser[])
{
  nagad_a1w_w_rtype alpha, beta;
  alpha = ruser[0];
  beta  = ruser[1];

  f[0] = tan(y[2]);
  f[1] = alpha * tan(y[2]) / y[1] + beta * y[1] / cos(y[2]);
  f[2] = alpha / (y[1] * y[1]);
}

static void NAG_CALL g(void *&                  ad_handle,
                       const nagad_a1w_w_rtype &x,
                       const nagad_a1w_w_rtype  y[],
                       nagad_a1w_w_rtype &      retval,
                       Integer                  iuser[],
                       nagad_a1w_w_rtype        ruser[])
{
  retval = y[0];
}