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

NAG AD Library Introduction
Example description
!   D02PU_A1T1W_F Example Program Text
!   Mark 28.3 Release. NAG Copyright 2022.

    Module d02pu_a1t1w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: nagad_a1t1w_w_rtype, sqrt, Assignment (=),      &
                               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
      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_a1t1w_w_rtype), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Type (nagad_a1t1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_a1t1w_w_rtype), Intent (In) :: y(n)
        Type (nagad_a1t1w_w_rtype), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Local Scalars ..
        Type (nagad_a1t1w_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 d02pu_a1t1w_fe_mod

    Program d02pu_a1t1w_fe

!     D02PU_A1T1W_F Example Main Program

!     .. Use Statements ..
      Use d02pu_a1t1w_fe_mod, Only: f, liwsav, lrwsav, n, nin, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d02pe_a1t1w_f, d02pq_a1t1w_f, d02pt_a1t1w_f,    &
                               d02pu_a1t1w_f, nagad_a1t1w_get_derivative,      &
                               nagad_a1t1w_inc_derivative,                     &
                               nagad_a1t1w_ir_interpret_adjoint,               &
                               nagad_a1t1w_ir_register_variable,               &
                               nagad_a1t1w_ir_remove, nagad_a1t1w_w_rtype,     &
                               nagad_t1w_w_rtype, sqrt, x10aa_a1t1w_f,         &
                               x10ab_a1t1w_f, x10za_a1t1w_f, Operator (-),     &
                               Operator (/), Operator (+), Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1t1w_w_rtype)       :: eps, errmax, hnext, hs, te, terrmx,  &
                                          tgot, tol, ts, twant, waste
      Type (nagad_t1w_w_rtype)         :: t_t
      Real (Kind=nag_wp)               :: epsr, hstart, t, tend, tolr, tstart
      Integer                          :: fevals, ifail, method, stepcost,     &
                                          stepsok
!     .. Local Arrays ..
      Type (nagad_a1t1w_w_rtype)       :: rmserr(n), ruser(1), th(n), y(n),    &
                                          ygot(n), ymax(n), ypgot(n)
      Type (nagad_a1t1w_w_rtype), Allocatable :: rwsav(:)
      Real (Kind=nag_wp)               :: thresh(n), yr(n)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Executable Statements ..
      Write (nout,*) 'D02PU_A1T1W_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, tolr
      Read (nin,*) thresh(1:n)

!     Set control for output

      ts = tstart
      te = tend
      eps = epsr
      tol = tolr
      th(1:n) = thresh(1:n)
      hs = hstart

!     Create AD tape
      Call x10za_a1t1w_f

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

!     Register variables to differentiate w.r.t.
      eps%value%tangent = 1.0_nag_wp
      Call nagad_a1t1w_ir_register_variable(eps)

      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))

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

      Write (nout,99999) tolr
      Write (nout,99998)
      Write (nout,99997) tstart, y(1:n)%value%value

      twant = te

integ: Do

        ifail = -1
        Call d02pe_a1t1w_f(ad_handle,f,n,twant,tgot,ygot,ypgot,ymax,iuser,     &
          ruser,iwsav,rwsav,ifail)

        If (ifail<2 .Or. ifail>4) Then
          Exit integ
        End If
      End Do integ

      If (ifail==0) Then
        t = tgot%value%value
        yr(1:n) = ygot(1:n)%value%value
        Write (nout,99997) t, yr(1:n)

!       Get error estimates
        ifail = 0
        Call d02pu_a1t1w_f(ad_handle,n,rmserr,errmax,terrmx,iwsav,rwsav,ifail)

        Write (nout,99996) rmserr(1:n)%value%value
        Write (nout,99995) errmax%value%value, terrmx%value%value

        ifail = 0
        Call d02pt_a1t1w_f(ad_handle,fevals,stepcost,waste,stepsok,hnext,      &
          iwsav,rwsav,ifail)
        Write (nout,99994) fevals
      End If

99999 Format (/,' Calculation with TOL = ',1P,E8.1)
99998 Format (/,'    t         y1         y2         y3         y4',/)
99997 Format (1X,F6.3,4(3X,F8.4))
99996 Format (/,' Componentwise error assessment',/,9X,4(2X,E9.2))
99995 Format (/,' Worst global error observed was ',E9.2,                      &
        ' - it occurred at T = ',F6.3)
99994 Format (/,' Cost of the integration in evaluations of F is',I6)

      t_t = 1.0_nag_wp
      Call nagad_a1t1w_inc_derivative(errmax,t_t)
      Call nagad_a1t1w_inc_derivative(terrmx,t_t)
      Call nagad_a1t1w_ir_interpret_adjoint(ifail)

      Write (nout,*)
      Write (nout,*)                                                           &
        ' Derivatives calculated: Second order, adjoints of tangents'
      Write (nout,*) ' Computational mode    : algorithmic'

!     Get derivatives
      t_t = nagad_a1t1w_get_derivative(eps)
      epsr = t_t%tangent

      Write (nout,*) ' Sum of Hessian terms for error estimates w.r.t. eps'
      Write (nout,*)
      Write (nout,99993) ' d^2errmax/deps^2 + d^2terrmx/deps^2 =', epsr
99993 Format (1X,A,1X,E12.5)

!     Remove computational data object and tape
      ifail = 0
      Call x10ab_a1t1w_f(ad_handle,ifail)
      Call nagad_a1t1w_ir_remove

    End Program d02pu_a1t1w_fe