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

NAG AD Library Introduction
Example description
!   D02PR_T1W_F Example Program Text
!   Mark 27.2 Release. NAG Copyright 2021.

    Module d02pr_t1w_fe_mod

!     D02PR_T1W_F Example Program Module:
!            Parameters and User-defined Routines

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: nagad_t1w_w_rtype, sqrt, Operator (/),          &
                               Operator (+), Operator (*), Operator (**),      &
                               Operator (-)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: liwsav = 130, n = 4, nin = 5,        &
                                          nout = 6, npts = 6
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
    Contains

      Subroutine f(ad_handle,t,n,y,yp,iuser,ruser)
!       .. Implicit None Statement ..
        Implicit None
!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t1w_w_rtype), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Type (nagad_t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t1w_w_rtype), Intent (In) :: y(n)
        Type (nagad_t1w_w_rtype), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Type (nagad_t1w_w_rtype)       :: r
!       .. Executable Statements ..
        r = 1.0_nag_wp/sqrt(y(1)*y(1)+y(2)*y(2))
        r = r**3
        yp(1) = y(3)
        yp(2) = y(4)
        yp(3) = -y(1)*r
        yp(4) = -y(2)*r
        Return
      End Subroutine f
    End Module d02pr_t1w_fe_mod

    Program d02pr_t1w_fe

!     D02PR_T1W_F Example Main Program

!     .. Use Statements ..
      Use d02pr_t1w_fe_mod, Only: f, liwsav, lrwsav, n, nin, nout, npts
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d02pf_t1w_f, d02pq_t1w_f, d02pr_t1w_f,          &
                               d02pt_t1w_f, nagad_t1w_set_derivative,          &
                               nagad_t1w_w_rtype, sqrt, x10aa_t1w_f,           &
                               x10ab_t1w_f, Assignment (=), Operator (-),      &
                               Operator (/), Operator (+), Operator (==),      &
                               Operator (>=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_t1w_w_rtype)         :: eps, hnext, hs, te, tinc, tnow, tol, &
                                          ts, waste
      Real (Kind=nag_wp)               :: epsr, hstart, t, tend, tolr, tstart
      Integer                          :: fevals, ifail, method, stepcost,     &
                                          stepsok
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype)         :: ruser(1), th(n), y(n), ynow(n),      &
                                          ypnow(n)
      Type (nagad_t1w_w_rtype), Allocatable :: rwsav(:)
      Real (Kind=nag_wp)               :: thresh(n), yr(n)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'D02PR_T1W_F Example Program Results'

      Allocate (iwsav(liwsav),rwsav(lrwsav))

!     Set initial conditions and input

!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) method
      Read (nin,*) tstart, tend
      Read (nin,*) epsr
      Read (nin,*) hstart
      Read (nin,*) thresh(1:n)

!     Set control for output

      tolr = 1.0E-5_nag_wp
      eps = epsr
      tol = tolr
      ts = tstart
      te = tend
      th(1:n) = thresh(1:n)
      hs = hstart

      tinc = (te-ts)/real(npts,kind=nag_wp)
      te = ts + tinc

!     Create AD configuration data object
      ifail = 0
      Call x10aa_t1w_f(ad_handle,ifail)

      Call nagad_t1w_set_derivative(eps,1.0E0_nag_wp)

      y(1) = 1.0_nag_wp - eps
      y(2) = 0.0_nag_wp
      y(3) = 0.0_nag_wp
      y(4) = sqrt((1.0_nag_wp+eps)/(1.0_nag_wp-eps))

      Write (nout,99999) tolr
      Write (nout,99998) epsr
      Write (nout,99997) tstart, y(1:n)%value
99999 Format (/,' Calculation with tol = ',1P,E8.1)
99998 Format (/,'                  eps = ',1P,E8.1)
99997 Format (1X,F6.3,4(3X,F8.4))

!     ifail: behaviour on error exit
!            =0 for hard exit, =1 for quiet-soft, =-1 for noisy-soft
      ifail = 0
      Call d02pq_t1w_f(ad_handle,n,ts,te,y,tol,th,method,hs,iwsav,rwsav,ifail)

loop: Do
        ifail = 0
        Call d02pf_t1w_f(ad_handle,f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,      &
          rwsav,ifail)

        If (tnow==te) Then
          t = tnow%value
          yr(1:n) = ynow(1:n)%value
          Write (nout,99997) t, yr(1:n)
          If (tnow>=tend) Then
            Exit loop
          End If
          te = te + tinc
          Call d02pr_t1w_f(ad_handle,te,iwsav,rwsav,ifail)
        End If
      End Do loop

      ifail = 0
      Call d02pt_t1w_f(ad_handle,fevals,stepcost,waste,stepsok,hnext,iwsav,    &
        rwsav,ifail)
      Write (nout,99996) fevals
99996 Format (/,' Cost of the integration in evaluations of F is',I6)

      epsr = ynow(1)%tangent

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'
      Write (nout,*)
      Write (nout,*) ' Derivatives:'
      Write (nout,99995) ' dy(t)/deps  =', epsr
99995 Format (1X,A,1X,E12.5)

!     Remove computational data object
      ifail = 0
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program d02pr_t1w_fe