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

NAG AD Library Introduction
Example description
/* D01PA_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;

extern "C"
{
  static void NAG_CALL functn(void *&                 ad_handle,
                              const Integer &         ndim,
                              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;
  Integer mxord = 5, ndim = 3, sdvert = 8, ldvert = 4;

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

  // Allocate memory
  nagad_a1w_w_rtype *finvls = 0, *vert = 0, *vert_in = 0;
  if (!(finvls = NAG_ALLOC(mxord, nagad_a1w_w_rtype)) ||
      !(vert = NAG_ALLOC(ldvert * sdvert, nagad_a1w_w_rtype)) ||
      !(vert_in = NAG_ALLOC(ldvert * ndim, nagad_a1w_w_rtype)))
    {
      cout << "Allocation failure\n";
      exit_status = -1;
    }
  else
    {

      for (int i = 0; i < ndim * ldvert; i++)
        {
          vert_in[i] = 0.0;
        }
      for (int i = 1; i < ndim * ldvert; i = i + ldvert + 1)
        {
          vert_in[i] = 1.0;
        }

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

      /* Register variables to differentiate w.r.t. */
      for (int i = 0; i < ndim * ldvert; i++)
        {
          dco::ga1s<double>::global_tape->register_variable(vert_in[i]);
        }

      for (int i = 0; i < ndim * ldvert; i++)
        {
          vert[i] = vert_in[i];
        }

      cout << "\n Maxord   Estimated      Estimated         Integrand\n";
      cout << "           value         accuracy        evaluations\n";
      cout.setf(ios::scientific, ios::floatfield);
      cout.precision(4);
      Integer minord = 0, nevals = 1;
      for (Integer maxord = 1; maxord <= mxord; maxord++)
        {

          // Call the AD routine
          nagad_a1w_w_rtype esterr, ruser[1];
          Integer           iuser[1];
          ifail = 0;
          nag::ad::d01pa(ad_handle, ndim, vert, ldvert, sdvert, functn, minord,
                         maxord, finvls, esterr, -1, iuser, -1, ruser, ifail);
          double finv_r = dco::value(finvls[maxord - 1]);
          double estr_r = dco::value(esterr);
          cout.width(5);
          cout << maxord;
          cout.width(15);
          cout << finv_r;
          cout.width(15);
          cout << estr_r;
          cout.width(12);
          cout << nevals << endl;

          nevals = (nevals * (maxord + ndim + 1)) / maxord;
        }

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

      // Get derivatives
      double            inc = 1.0;
      nagad_a1w_w_rtype sol = finvls[mxord - 1];

      dco::derivative(sol) += inc;
      dco::ga1s<double>::global_tape->sparse_interpret() = true;
      dco::ga1s<double>::global_tape->interpret_adjoint();

      cout.setf(ios::right);
      cout.precision(4);
      cout << "\n Solution, I = ";
      double ans_value = dco::value(finvls[mxord - 1]);
      cout.width(12);
      cout << ans_value << endl;
      cout << "\n Derivatives w.r.t vertices:\n";
      cout << "   i   j    d/dv(i,j)\n";

      cout.setf(ios::scientific, ios::floatfield);

      int k = -1;
      for (int i = 1; i <= ndim + 1; i++)
        {
          for (int j = 1; j <= ndim; j++)
            {
              k           = k + 1;
              double dfdv = dco::derivative(vert_in[k]);

              cout.width(4);
              cout << i;
              cout.width(4);
              cout << j;
              cout.width(14);
              cout << dfdv << endl;
            }
        }

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

  NAG_FREE(vert);
  NAG_FREE(vert_in);
  NAG_FREE(finvls);

  return exit_status;
}
static void NAG_CALL functn(void *&                 ad_handle,
                            const Integer &         ndim,
                            const nagad_a1w_w_rtype x[],
                            nagad_a1w_w_rtype &     ret,
                            Integer                 iuser[],
                            nagad_a1w_w_rtype       ruser[])
{
  nagad_a1w_w_rtype tmp1;
  // dco/c++ used here to perform AD of the following
  tmp1 = x[0] + x[1] + x[2];
  ret  = exp(tmp1) * cos(tmp1);
  return;
}