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

NAG AD Library Introduction
Example description
!   D02PS_A1T1W_F Example Program Text
!   Mark 29.2 Release. NAG Copyright 2023.

    Module d02ps_a1t1w_fe_mod

!     D02PS_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, Operator (*),              &
                               Assignment (=), Operator (-)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f
!     .. Parameters ..
      Integer, Parameter, Public       :: liwsav = 130, n = 2, nin = 5,        &
                                          nout = 6, npts = 16, nwant = 1
      Integer, Parameter, Public       :: lrwsav = 350 + 32*n
      Integer, Parameter, Public       :: lwcomm = n + 5*nwant
    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(*)
!       .. Executable Statements ..
        yp(1) = ruser(1)*y(2)
        yp(2) = -ruser(2)*y(1)
        Return
      End Subroutine f
    End Module d02ps_a1t1w_fe_mod

    Program d02ps_a1t1w_fe

!     D02PS_A1T1W_F Example Main Program

!     .. Use Statements ..
      Use d02ps_a1t1w_fe_mod, Only: f, liwsav, lrwsav, lwcomm, n, nin, nout,   &
                                    npts, nwant
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d02pf_a1t1w_f, d02pq_a1t1w_f, d02ps_a1t1w_f,    &
                               d02pt_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, x10aa_a1t1w_f,               &
                               x10ab_a1t1w_f, x10za_a1t1w_f, Operator (/),     &
                               Operator (-), 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)       :: hnext, hs, te, tinc, tnow, tol, ts,  &
                                          twant, waste
      Type (nagad_t1w_w_rtype)         :: t_t
      Real (Kind=nag_wp)               :: dr, hstart, t, tend, tolr, tstart
      Integer                          :: fevals, i, ideriv, ifail, method,    &
                                          stepcost, stepsok
!     .. Local Arrays ..
      Type (nagad_a1t1w_w_rtype)       :: ruser(2), th(n), y(n), ynow(n),      &
                                          ypnow(n), ypwant(nwant),             &
                                          ywant(nwant)
      Type (nagad_a1t1w_w_rtype), Allocatable :: rwsav(:), wcomm(:)
      Real (Kind=nag_wp)               :: thresh(n), yinit(n), yr(n)
      Integer                          :: iuser(1)
      Integer, Allocatable             :: iwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: real
!     .. Executable Statements ..
      Write (nout,*) 'D02PS_A1T1W_F Example Program Results'

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

!     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

      tolr = 1.0E-4_nag_wp

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

      tinc = (te-ts)/real(npts,kind=nag_wp)
!     Create AD tape
      Call x10za_a1t1w_f

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

      ruser(1) = 1.0_nag_wp
      ruser(2) = 1.0_nag_wp

!     Register variables to differentiate w.r.t.
      ruser(1:2)%value%tangent = 1.0_nag_wp
      Call nagad_a1t1w_ir_register_variable(ruser)

!     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, yinit(1:n)

      twant = ts + tinc
      tnow = ts
loop: Do While (tnow<te)

!       Integrate one step to tnow
        ifail = 0
        Call d02pf_a1t1w_f(ad_handle,f,n,tnow,ynow,ypnow,iuser,ruser,iwsav,    &
          rwsav,ifail)

!       Interpolate at any required points in previous step
interp: Do While (twant<=tnow)

          ideriv = 2
          ifail = 0
          Call d02ps_a1t1w_f(ad_handle,n,twant,ideriv,nwant,ywant,ypwant,f,    &
            wcomm,lwcomm,iuser,ruser,iwsav,rwsav,ifail)

          t = twant%value%value
          yr(1) = ywant(1)%value%value
          yr(2) = ypwant(1)%value%value
          Write (nout,99997) t, yr(1:n)

!         Set next required solution point
          twant = twant + tinc
        End Do interp
      End Do loop

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

99999 Format (/,' Calculation with TOL = ',1P,E8.1)
99998 Format (/,'    t         y1        y1''',/)
99997 Format (1X,F6.3,2(3X,F7.3))
99996 Format (/,' Cost of the integration in evaluations of F is',I6)

      t_t = 1.0_nag_wp
      Do i = 1, nwant
        Call nagad_a1t1w_inc_derivative(ywant(i),t_t)
      End Do
      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(ruser(1))
      dr = t_t%tangent
      t_t = nagad_a1t1w_get_derivative(ruser(2))
      dr = dr + t_t%tangent
      Write (nout,*)                                                           &
        ' Sum of Hessian terms for solution w.r.t. system parameters:'
      Write (nout,*)
      Write (nout,99995) ' Sum_{i,j,k} d^2y_k/druser(i)druser(j)  =', dr
99995 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 d02ps_a1t1w_fe