/* D02BJ_A1W_F C++ Header Example Program.
*
* Copyright 2020 Numerical Algorithms Group.
*
* Mark 27.1, 2020.
*
*/
#include <nag.h>
#include <dco.hpp>
#include <nagad.h>
#include <math.h>
#include <stdio.h>
#include <iostream>
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
nagad_a1w_ir_create();
// Create AD configuration data object
Integer ifail = 0;
void *ad_handle = 0;
x10aa_a1w_f_(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.
nagad_a1w_ir_register_variable(&ruser[0]);
nagad_a1w_ir_register_variable(&ruser[1]);
nagad_a1w_ir_register_variable(&yinit[0]);
nagad_a1w_ir_register_variable(&yinit[1]);
nagad_a1w_ir_register_variable(&yinit[2]);
y[0] = yinit[0];
y[1] = yinit[1];
y[2] = yinit[2];
ifail = 0;
d02bj_a1w_f_(ad_handle,x,xend,n,y,fcn,tol,"D", d02bj_a1w_x_, g,
w,iuser,ruser,ifail,1);
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;
nagad_a1w_inc_derivative(&x,inc);
ifail = 0;
nagad_a1w_ir_interpret_adjoint(ifail);
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 = nagad_a1w_get_derivative(ruser[0]);
cout << " dx/dg = ";
cout.width(12); cout << -dr << endl;
dr = nagad_a1w_get_derivative(ruser[1]);
cout << " dx/ddrag = ";
cout.width(12); cout << -dr << endl;
dr = nagad_a1w_get_derivative(yinit[0]);
cout << " dx/dheight = ";
cout.width(12); cout << dr << endl;
dr = nagad_a1w_get_derivative(yinit[1]);
cout << " dx/dvel = ";
cout.width(12); cout << dr << endl;
dr = nagad_a1w_get_derivative(yinit[2]);
cout << " dx/dangle = ";
cout.width(12); cout << dr << endl;
x10ab_a1w_f_(ad_handle,ifail);
nagad_a1w_ir_remove();
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];
}