Example description
/* C05AY_A1W_F C++ Header Example Program.
 *
 * Copyright 2019 Numerical Algorithms Group.
 * Mark 27, 2019.
 */

#include <nag.h>
#include <dco.hpp>
#include <nagad.h>
#include <stdio.h>
#include <math.h>
#include <nag_stdlib.h>
#include <nagx07.h>
#include <iostream>
#include <string>
using namespace std;

extern "C"
{
  static void (NAG_CALL f)(void* &ad_handle,
                           const nagad_a1w_w_rtype &x,
                           nagad_a1w_w_rtype &z,
                           Integer iuser[],
                           nagad_a1w_w_rtype ruser[]);
  static void (NAG_CALL cb_sym)(int callmode, void* cb_handle);

}

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

  cout << "C05AY_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, epsr, etar, ruserr;
  cin >> ar;
  cin >> br;
  cin >> epsr;
  cin >> etar;
  cin >> ruserr;

  // Create AD tape
  nagad_a1w_ir_create();

  // Create AD configuration data object
  Integer ifail = 0;
  void    *ad_handle = 0;
  x10aa_a1w_f_(ad_handle,ifail);

  // Read AD computational mode
  Integer mode;
  cin >> mode;

  // Set this mode.
  ifail = 0;
  x10ac_a1w_f_(ad_handle,mode,ifail);
  
  // Register variables to differentiate w.r.t.
  nagad_a1w_w_rtype a, b, eps, eta, ruser[1];
  a = ar;
  b = br;
  eps = epsr;
  eta = etar;
  ruser[0] = ruserr;
  nagad_a1w_ir_register_variable(&a);
  nagad_a1w_ir_register_variable(&b);
  nagad_a1w_ir_register_variable(&ruser[0]);

  // Call AD routine
  nagad_a1w_w_rtype x;
  Integer           iuser[1];
  iuser[0] = 0;
  // if mode is symbolic then set iuser[0] = 1 use symbolic adjoint callback

  if (mode==nagad_symbolic) {
    iuser[0] = 1;
  }
    
  ifail = -1;
  c05ay_a1w_f_(ad_handle,a,b,eps,eta,f,x,iuser,ruser,ifail);

  // Setup evaluation of derivatives via adjoints
  double inc = 1.0;
  nagad_a1w_set_derivative(&x,inc);
  
  ifail = 0;
  nagad_a1w_ir_interpret_adjoint(ifail);

  cout << "\n Derivatives calculated: First order adjoints\n";
  if (mode==nagad_algorithmic) {
    cout << " Computational mode    : algorithmic\n";
  } else {
    cout << " Computational mode    : symbolic\n";
  }

  // Get derivatives
  double da = nagad_a1w_get_derivative(a);
  double db = nagad_a1w_get_derivative(b);
  double druser = nagad_a1w_get_derivative(ruser[0]);

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

  cout << " Solution, x = " << nagad_a1w_get_value(x) << endl;
  cout << "\n Derivatives:\n";
  cout << "     d/da(x)     = " << da << endl;
  cout << "     d/db(x)     = " << db << endl;
  cout << "     d/druser(x) = " << druser << endl;

  // Remove computational data object and tape
  x10ab_a1w_f_(ad_handle,ifail);
  nagad_a1w_ir_remove();

  return exit_status;
}

static void (NAG_CALL f)(void* &ad_handle,
                         const nagad_a1w_w_rtype &x,
                         nagad_a1w_w_rtype &z,
                         Integer iuser[],
                         nagad_a1w_w_rtype ruser[])
{
  Integer ifail = 0, mode;
  // Get computational mode
  x10ad_a1w_f_(ad_handle,mode,ifail);
  if (mode==nagad_algorithmic) {
    // Evaluate nonlinear function (this needs dco/c++)
    z = exp(-x) - x*ruser[0];
  } else {
    Integer cb_mode;
    ifail = 0;
    x10bd_a1w_f_(ad_handle,cb_mode,ifail);
    // Evaluate primal only
    double xr = nagad_a1w_get_value(x);
    double rr = nagad_a1w_get_value(ruser[0]);
    double zr = exp(-xr) - xr*rr;
    if (cb_mode==nagad_primal || cb_mode==0) {
      z = zr;
    } else {
      // Perform differentials symbolically using companion callback
      
      // Create callback data object
      void    *cb_handle;
      ifail = 0;
      x10ba_a1w_f_(cb_handle,ifail);
      // Write cb_mode to object
      x10be_a1w_f_(cb_handle,cb_mode,ifail);
      // Write inputs to object
      x10bj_a1w_f_(cb_handle,x,ifail);
      x10bj_a1w_f_(cb_handle,ruser[0],ifail);

      z = zr;
      // Register z
      nagad_a1w_ir_register_variable(&z);
      // Write output to object
      ifail = 0;
      x10bj_a1w_f_(cb_handle,z,ifail);

      // Insert callback
      void *cb_loc = (void *) cb_sym;
      x10bb_a1w_f_(cb_handle,cb_loc,ifail);
    }
  }
  return;
}

static void (NAG_CALL cb_sym)(int callmode, void* cb_handle)
{
  // Extract data from callback data object
  
  if (callmode == FREE_CHECKPOINT) return;
  Integer ifail, cb_mode;
  ifail = 0;
  x10ce_a1w_f_(cb_handle,cb_mode,ifail);
  nagad_a1w_w_rtype  x, ruser, z;
  x10cj_a1w_f_(cb_handle,x,ifail);
  x10cj_a1w_f_(cb_handle,ruser,ifail);
  x10cj_a1w_f_(cb_handle,z,ifail);
  
  double za = nagad_a1w_get_derivative(z);

  if (cb_mode==nagad_dstate || cb_mode==nagad_dall) {
    // d/dx
    double xr = nagad_a1w_get_value(x);
    double rr = nagad_a1w_get_value(ruser);
    double xa = (-exp(-xr) - rr)*za;
    nagad_a1w_inc_derivative(&x,xa);
  }
  if (cb_mode==nagad_dparam || cb_mode==nagad_dall) {
    // d/druser
    double xr = nagad_a1w_get_value(x);
    double ra = -xr*za;
    nagad_a1w_inc_derivative(&ruser,ra);
  }
  return;
}