/* D01RM_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 <iostream>
using namespace std;
extern "C"
{
static void NAG_CALL f(void * &ad_handle,
const nagad_a1w_w_rtype x[],
const Integer &nx,
nagad_a1w_w_rtype fv[],
Integer &iflag,
Integer iuser[],
nagad_a1w_w_rtype ruser[],
const void *&cpuser);
}
int main(void)
{
// Scalars
int exit_status = 0;
cout << "D01RM_A1W_F C++ Header Example Program Results\n\n";
// Create AD tape
nagad_a1w_ir_create();
nagad_a1w_w_rtype bound, epsabs, epsrel;
bound = 0.0;
epsabs = 0.0;
epsrel = 1.0e-4;
Integer inf = 1;
Integer maxsub = 20;
Integer lrinfo = 80;
Integer liinfo = 20;
nagad_a1w_w_rtype *rinfo = 0;
Integer *iinfo = 0;
rinfo = new nagad_a1w_w_rtype [lrinfo];
iinfo = new Integer [liinfo];
// Create AD configuration data object
Integer ifail = 0;
void *ad_handle = 0;
x10aa_a1w_f_(ad_handle,ifail);
double inc = 1.0;
nagad_a1w_w_rtype result, abserr, ruser[22];
Integer iuser[1];
const void *cpuser = 0;
iuser[0] = 0;
for (int i = 0; i<20; ++i) {
ruser[i] = 0.0;
}
ruser[20] = 1.0;
ruser[21] = 1.0;
// Register variables to differentiate w.r.t.
nagad_a1w_ir_register_variable(&ruser[20]);
nagad_a1w_ir_register_variable(&ruser[21]);
// Call the AD routine
ifail = -1;
d01rm_a1w_f_(ad_handle,f,bound,inf,epsabs,epsrel,maxsub,result,abserr,rinfo,iinfo,
iuser,ruser,cpuser,ifail);
if (ifail<0) {
cout << "\n ** d01rm_a1w_f_ failed error exit ifail = " << ifail << endl;
goto END;
}
// Print inputs and primal outputs.
cout << "\n lower limit of integration (bound) = " << nagad_a1w_get_value(bound) << endl;
cout << " upper limit of integration (Inf) = " << "Infinity" << endl;
cout << " absolute accuracy requested = " << nagad_a1w_get_value(epsabs) << endl;
cout << " relative accuracy requested = " << nagad_a1w_get_value(epsrel) << endl;
cout << " maximum number of subintervals = " << maxsub << endl;
cout.setf(ios::scientific,ios::floatfield);
cout.precision(4);
if (ifail >= 0) {
cout << "\n approximation to the integral : " << nagad_a1w_get_value(result) << endl;
cout << " estimate of the absolute error : " << nagad_a1w_get_value(abserr) << endl;
cout << " number of function evaluations : " << iinfo[0] << endl;
}
// Setup evaluation of derivatives via adjoints.
nagad_a1w_inc_derivative(&result,inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
cout << "\n Derivatives calculated: First order adjoints\n";
cout << " Computational mode : algorithmic\n";
// Get derivatives
double dr;
dr = nagad_a1w_get_derivative(ruser[20]);
cout << "\n Derivative of solution w.r.t function parameters:\n";
cout << " dI/druser[20] = " << dr << endl;
dr = nagad_a1w_get_derivative(ruser[21]);
cout << " dI/druser[21] = " << dr << endl;
END:
// Remove computational data object and tape
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
delete [] rinfo;
delete [] iinfo;
return exit_status;
}
static void NAG_CALL f(void * &ad_handle,
const nagad_a1w_w_rtype x[],
const Integer &nx,
nagad_a1w_w_rtype fv[],
Integer &iflag,
Integer iuser[],
nagad_a1w_w_rtype ruser[],
const void *&cpuser)
{
// dco/c++ used here to perform AD of the following
for (int i=0; i<nx; i++) {
fv[i] = 1.0/((x[i]+ruser[20])*sqrt(ruser[21]*x[i]));
}
return;
}