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

NAG AD Library Introduction
Example description
/* F11JC_A1W_F C++ Header Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 * Mark 30.0, 2024.
 */
#include <dco.hpp>
#include <iostream>
#include <nag.h>
#include <nagad.h>
#include <nagx04.h>
#include <stdio.h>
using namespace std;

int main()
{
  int               exit_status = 0;
  nag::ad::handle_t ad_handle;
  Integer           ifail = 0;

  cout << "F11JC_A1W_F C++ Header Example Program Results\n\n";
  // Skip heading in data file
  string mystr;
  getline(cin, mystr);

  // Read order of matrix and number of nonzero entries
  Integer n, nnz;
  cin >> n;
  cin >> nnz;

  Integer            la     = 3 * nnz;
  Integer            liwork = 2 * la + 7 * n + 1;
  Integer            lwork  = 6 * n + 120;
  nagad_a1w_w_rtype *a = 0, *x = 0, *y = 0, *work = 0;
  double *           ar = 0, *yr = 0, *dxdy = 0;
  Integer *          icol = 0, *ipiv = 0, *irow = 0, *istr = 0, *iwork = 0;

  a     = new nagad_a1w_w_rtype[la];
  x     = new nagad_a1w_w_rtype[n];
  y     = new nagad_a1w_w_rtype[n];
  work  = new nagad_a1w_w_rtype[lwork];
  icol  = new Integer[la];
  ipiv  = new Integer[n];
  irow  = new Integer[la];
  istr  = new Integer[n + 1];
  iwork = new Integer[liwork];
  ar    = new double[la];
  yr    = new double[n];
  dxdy  = new double[n * n];

  double            dd;
  nagad_a1w_w_rtype dtol, dscale, tol;
  Integer           lfill, maxitn;
  cin >> lfill >> dd;
  dtol = dd;
  cin >> dd;
  dscale = dd;
  cin >> dd >> maxitn;
  tol = dd;

  // Read the matrix A
  for (int i = 0; i < nnz; i++)
  {
    cin >> ar[i] >> irow[i] >> icol[i];
    a[i] = ar[i];
  }

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

  // Read the vector y
  for (int i = 0; i < n; i++)
  {
    cin >> yr[i];
    y[i] = yr[i];
    dco::ga1s<double>::global_tape->register_variable(y[i]);
  }
  // Read initial vector x
  for (int i = 0; i < n; i++)
  {
    cin >> yr[i];
    x[i] = yr[i];
  }

  // Create AD configuration data object
  ifail = 0;

  // Calculate incomplete Cholesky factorization
  Integer nnzc, npivm;
  ifail = 0;
  nag::ad::f11ja(ad_handle, n, nnz, a, la, irow, icol, lfill, dtol, "N", dscale,
                 "M", ipiv, istr, nnzc, npivm, iwork, liwork, ifail);

  // Solve Ax = y
  nagad_a1w_w_rtype rnorm;
  Integer           itn;
  ifail = 0;
  nag::ad::f11jc(ad_handle, "CG", n, nnz, a, la, irow, icol, ipiv, istr, y, tol,
                 maxitn, x, rnorm, itn, work, lwork, ifail);

  // Output results
  cout << " Converged in " << itn << " iterations" << endl;
  cout << " Final residual norm = " << dco::value(rnorm) << endl;
  cout.setf(ios::scientific, ios::floatfield);
  cout.precision(4);
  cout << endl;
  cout << "  Solution vector" << endl;
  for (int i = 0; i < n; ++i)
  {
    cout.width(12);
    cout << dco::value(x[i]) << endl;
  }

  cout << "\n Derivatives calculated: First order adjoints\n";
  cout << " Computational mode    : algorithmic\n";
  cout << "\n Derivatives of solution X w.r.t RHS Y:\n";

  // Setup evaluation of derivatives via adjoints
  for (int i = 0; i < n; i++)
  {
    // Reset adjoints, initialize derivative, and evaluate adjoint
    dco::ga1s<double>::global_tape->zero_adjoints();
    double inc = 1.0;
    dco::derivative(x[i]) += inc;
    ifail                                              = 0;
    dco::ga1s<double>::global_tape->sparse_interpret() = true;
    dco::ga1s<double>::global_tape->interpret_adjoint();

    for (int j = 0; j < n; j++)
    {
      Integer k = i + j * n;
      dxdy[k]   = dco::derivative(y[j]);
    }
  }
  // Print derivatives
  cout << endl;
  NagError fail;
  INIT_FAIL(fail);
  x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, n, n, dxdy, n,
         "       dx_i/dy_j", 0, &fail);

  ifail = 0;

  dco::ga1s<double>::tape_t::remove(dco::ga1s<double>::global_tape);

  delete[] a;
  delete[] x;
  delete[] y;
  delete[] work;
  delete[] icol;
  delete[] ipiv;
  delete[] irow;
  delete[] istr;
  delete[] iwork;
  delete[] ar;
  delete[] yr;
  delete[] dxdy;

  return exit_status;
}