Settings help

## 1Purpose

d02bj is the AD Library version of the primal routine d02bjf. Based (in the C++ interface) on overload resolution, d02bj can be used for primal, tangent and adjoint evaluation. It supports tangents and adjoints of first and second order.

## 2Specification

Fortran Interface
 Subroutine d02bj_AD_f ( x, xend, n, y, fcn, tol, relabs, output, g, w, iuser, ruser, ifail)
 Integer, Intent (In) :: n Integer, Intent (Inout) :: iuser(*), ifail External :: g, fcn, output ADTYPE, Intent (In) :: xend, tol ADTYPE, Intent (Inout) :: x, y(n), ruser(*) ADTYPE, Intent (Out) :: w(20*n) Character (1), Intent (In) :: relabs Type (c_ptr), Intent (Inout) :: ad_handle
Corresponding to the overloaded C++ function, the Fortran interface provides five routines with names reflecting the type used for active real arguments. The actual subroutine and type names are formed by replacing AD and ADTYPE in the above as follows:
C++ Interface
#include <dco.hpp>
namespace nag {
template <typename FCN_T, typename OUTPUT_T, typename G_T>
}
}
The function is overloaded on ADTYPE which represents the type of active arguments. ADTYPE may be any of the following types:
double,
dco::ga1s<double>::type,
dco::gt1s<double>::type,
dco::gt1s<dco::gt1s<double>::type>::type,
dco::ga1s<dco::gt1s<double>::type>::type,

## 3Description

d02bj is the AD Library version of the primal routine d02bjf.
d02bjf integrates a system of first-order ordinary differential equations over an interval with suitable initial conditions, using a fixed order Runge–Kutta method, until a user-specified function, if supplied, of the solution is zero, and returns the solution at points specified by you, if desired. For further information see Section 3 in the documentation for d02bjf.

## 4References

Shampine L F (1994) Numerical solution of ordinary differential equations Chapman and Hall

## 5Arguments

In addition to the arguments present in the interface of the primal routine, d02bj includes some arguments specific to AD.
A brief summary of the AD specific arguments is given below. For the remainder, links are provided to the corresponding argument from the primal routine. A tooltip popup for all arguments can be found by hovering over the argument name in Section 2 and in this section.
On entry: a configuration object that holds information on the differentiation strategy. Details on setting the AD strategy are described in AD handle object in the NAG AD Library Introduction.
2: Input/Output
3: Input
4: n – Integer Input
5: y(n) – ADTYPE array Input/Output
6: fcn – Callable Input
fcn needs to be callable with the specification listed below. This can be a C++ lambda, a functor or a (static member) function pointer. If using a lambda, parameters can be captured safely by reference. No copies of the callable are made internally.
The specification of fcn is:
Fortran Interface
 Subroutine fcn ( x, y, f, iuser, ruser)
 Integer, Intent (Inout) :: iuser(*) ADTYPE, Intent (In) :: x, y(*) ADTYPE, Intent (Inout) :: f(*), ruser(*) Type (c_ptr), Intent (Inout) :: ad_handle
C++ Interface
On entry: a handle to the AD handle object.
2: Input
*: iuser($*$) – Integer array User Workspace
*: ruser($*$)ADTYPE array User Workspace
7: Input
8: relabs – character Input
9: output – Callable Input
output needs to be callable with the specification listed below. This can be a C++ lambda, a functor or a (static member) function pointer. If using a lambda, parameters can be captured safely by reference. No copies of the callable are made internally.
If a null pointer is used as the argument, then a NAG supplied routine will be used as the argument for this parameter (C++ only).
For the Fortran interface, the NAG supplied routine d02bj_AD_x may be used as the actual argument for this parameter.
The specification of output is:
Fortran Interface
 Subroutine output ( xsol, y, iuser, ruser)
 Integer, Intent (Inout) :: iuser(*) ADTYPE, Intent (In) :: y(*) ADTYPE, Intent (Inout) :: xsol, ruser(*) Type (c_ptr), Intent (Inout) :: ad_handle
C++ Interface
On entry: a handle to the AD handle object.
2: Input/Output
*: iuser($*$) – Integer array User Workspace
*: ruser($*$)ADTYPE array User Workspace
10: g – Callable Input
g needs to be callable with the specification listed below. This can be a C++ lambda, a functor or a (static member) function pointer. If using a lambda, parameters can be captured safely by reference. No copies of the callable are made internally.
If a null pointer is used as the argument, then a NAG supplied routine will be used as the argument for this parameter (C++ only).
For the Fortran interface, the NAG supplied routine d02bj_AD_w may be used as the actual argument for this parameter.
Note that g is a subroutine in this interface, returning the function value via the additional output parameter retval.
The specification of g is:
Fortran Interface
 Subroutine g ( x, y, retval, iuser, ruser)
 Integer, Intent (Inout) :: iuser(*) ADTYPE, Intent (In) :: x, y(*) ADTYPE, Intent (Inout) :: ruser(*) ADTYPE, Intent (Out) :: retval Type (c_ptr), Intent (Inout) :: ad_handle
C++ Interface
On entry: a handle to the AD handle object.
2: Input
On exit: the value of $g\left(x,y\right)$ at the specified point.
*: iuser($*$) – Integer array User Workspace
*: ruser($*$)ADTYPE array User Workspace
11: w($20×{\mathbf{n}}$) – ADTYPE array Workspace
*: iuser($*$) – Integer array User Workspace
User workspace.
*: ruser($*$) – ADTYPE array User Workspace
User workspace.
12: ifail – Integer Input/Output

## 6Error Indicators and Warnings

d02bj preserves all error codes from d02bjf and in addition can return:
${\mathbf{ifail}}=-89$
See Error Handling in the NAG AD Library Introduction for further information.
${\mathbf{ifail}}=-199$
The routine was called using a strategy that has not yet been implemented.
See AD Strategies in the NAG AD Library Introduction for further information.
${\mathbf{ifail}}=-444$
A C++ exception was thrown.
The error message will show the details of the C++ exception text.
${\mathbf{ifail}}=-899$
Dynamic memory allocation failed for AD.
See Error Handling in the NAG AD Library Introduction for further information.

Not applicable.

## 8Parallelism and Performance

d02bj is not threaded in any implementation.

None.

## 10Example

The following examples are variants of the example for d02bjf, modified to demonstrate calling the NAG AD Library.
Description of the primal example.
This example illustrates the solution of four different problems. In each case the differential system (for a projectile) is
 $y′=tan⁡ϕ v′= -0.032tan⁡ϕv- 0.02v cos⁡ϕ ϕ′= -0.032v2$
over an interval ${\mathbf{x}}=0.0$ to ${\mathbf{xend}}=10.0$ starting with values $y=0.5$, $v=0.5$ and $\varphi =\pi /5$. We solve each of the following problems with local error tolerances $\text{1.0E−4}$ and $\text{1.0E−5}$.
1. (i)To integrate to $x=10.0$ producing intermediate output at intervals of $2.0$ until a root is encountered where $y=0.0$.
2. (ii)As (i) but with no intermediate output.
3. (iii)As (i) but with no termination on a root-finding condition.
4. (iv)As (i) but with no intermediate output and no root-finding termination condition.

Language Source File Data Results
Fortran d02bj_a1t1w_fe.f90 d02bj_a1t1w_fe.d d02bj_a1t1w_fe.r
Fortran d02bj_a1w_fe.f90 d02bj_a1w_fe.d d02bj_a1w_fe.r
C++ d02bj_a1_algo_dcoe.cpp None d02bj_a1_algo_dcoe.r
C++ d02bj_a1t1_algo_dcoe.cpp None d02bj_a1t1_algo_dcoe.r

### 10.2Tangent modes

Language Source File Data Results
Fortran d02bj_t1w_fe.f90 d02bj_t1w_fe.d d02bj_t1w_fe.r
Fortran d02bj_t2w_fe.f90 d02bj_t2w_fe.d d02bj_t2w_fe.r
C++ d02bj_t1_algo_dcoe.cpp None d02bj_t1_algo_dcoe.r
C++ d02bj_t2_algo_dcoe.cpp None d02bj_t2_algo_dcoe.r

### 10.3Passive mode

Language Source File Data Results
Fortran d02bj_p0w_fe.f90 d02bj_p0w_fe.d d02bj_p0w_fe.r
C++ d02bj_passive_dcoe.cpp None d02bj_passive_dcoe.r