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

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

    Module d02pe_t2w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: nagad_t2w_w_rtype, Operator (-)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: liwsav = 130, n = 2, nin = 5,        &
                                          nout = 6, npts = 8
      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_t2w_w_rtype), Intent (In) :: t
        Integer, Intent (In)           :: n
!       .. Array Arguments ..
        Type (nagad_t2w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_t2w_w_rtype), Intent (In) :: y(n)
        Type (nagad_t2w_w_rtype), Intent (Out) :: yp(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        yp(1) = y(2)
        yp(2) = -y(1)
        Return
      End Subroutine f
    End Module d02pe_t2w_fe_mod

    Program d02pe_t2w_fe

!     D02PE_T2W_F Example Main Program

!     .. Use Statements ..
      Use d02pe_t2w_fe_mod, Only: f, liwsav, lrwsav, n, nin, nout, npts
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d02pe_t2w_f, d02pq_t2w_f, d02pt_t2w_f,          &
                               nagad_t2w_w_rtype, x10aa_t2w_f, x10ab_t2w_f,    &
                               Assignment (=), Operator (+)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_t2w_w_rtype)         :: hnext, hs, te, tgot, tinc, tol, ts,  &
                                          twant, waste
      Real (Kind=nag_wp)               :: hstart, t, tend, tolr, tstart
      Integer                          :: fevals, i, ifail, j, method,         &
                                          stepcost, stepsok
!     .. Local Arrays ..
      Type (nagad_t2w_w_rtype)         :: ruser(1), th(n), y(n), ygot(n),      &
                                          yin(n), ymax(n), ypgot(n)
      Type (nagad_t2w_w_rtype), Allocatable :: rwsav(:)
      Real (Kind=nag_wp)               :: dr(2), thresh(n), yinit(n), yr(n)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'D02PE_T2W_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,*) yinit(1:n)
      Read (nin,*) hstart
      Read (nin,*) thresh(1:n)

!     Set control for output

      tinc = (tend-tstart)/real(npts,kind=nag_wp)

      tolr = 1.0E-4_nag_wp

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

      Write (nout,99999) tolr
      Write (nout,99998)
      Write (nout,99997) tstart, yinit(1:n)
99999 Format (/,' Calculation with TOL = ',1P,E8.1)
99998 Format (/,'    t         y1        y2',/)
99997 Format (1X,F6.3,2(3X,F7.3))

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

      Do i = 1, n
        y(1:n) = yin(1:n)
        y(i)%value%tangent = 1.0_nag_wp
        y(i)%tangent%value = 1.0_nag_wp

        ifail = 0
        Call d02pq_t2w_f(ad_handle,n,ts,te,y,tol,th,method,hs,iwsav,rwsav,     &
          ifail)

        twant = tstart
        Do j = 1, npts
          twant = twant + tinc

          ifail = 0
          Call d02pe_t2w_f(ad_handle,f,n,twant,tgot,ygot,ypgot,ymax,iuser,     &
            ruser,iwsav,rwsav,ifail)
          If (i==1) Then
            t = tgot%value%value
            yr(1:n) = ygot(1:n)%value%value
            Write (nout,99997) t, yr(1:n)
          End If
        End Do

        y(i)%value%tangent = 0.0_nag_wp
        y(i)%tangent%value = 0.0_nag_wp

        dr(i) = ygot(1)%tangent%tangent
      End Do

      ifail = 0
      Call d02pt_t2w_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)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: Second order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'
      Write (nout,*)
      Write (nout,*) ' Derivatives:'
      Write (nout,99995) ' d^2y(t)/dy0^2  =', dr(1)
      Write (nout,99995) ' d^2y(t)/dy0''^2 =', dr(2)
99995 Format (1X,A,1X,E12.5)

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

    End Program d02pe_t2w_fe