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

NAG AD Library Introduction
Example description
!   C05AU_T2W_F Example Program Text
!   Mark 30.2 Release. NAG Copyright 2024.
    Module c05au_t2w_fe_mod

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, nagad_t2w_w_rtype, Operator (-),           &
                               Operator (*)
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f_t2w
!     .. Parameters ..
      Integer, Parameter, Public       :: nout = 6
!     .. Local Scalars ..
      Type (nagad_t2w_w_rtype), Public, Save :: t
    Contains
      Subroutine f_t2w(ad_handle,x,z,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Type (nagad_t2w_w_rtype), Intent (In) :: x
        Type (nagad_t2w_w_rtype), Intent (Out) :: z
!       .. Array Arguments ..
        Type (nagad_t2w_w_rtype), Intent (Inout) :: ruser(*)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        z = x - exp(-t*x)

        Return

      End Subroutine f_t2w
    End Module c05au_t2w_fe_mod

    Program c05au_t2w_fe

!     C05AU_T2W_F Example Main Program

!     .. Use Statements ..
      Use c05au_t2w_fe_mod, Only: f_t2w, nout, t
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: c05au_t2w_f, nagad_t2w_w_rtype, x10aa_t2w_f,    &
                               x10ab_t2w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_t2w_w_rtype)         :: a, b, eps, eta, h, x
      Type (c_ptr)                     :: ad_handle
      Integer                          :: ifail
!     .. Local Arrays ..
      Type (nagad_t2w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'C05AU_T2W_F Example Program Results'

      t = 1.0E0_nag_wp
      x = 1.0E0_nag_wp
      h = 0.1E0_nag_wp
      eps = 1.0E-5_nag_wp
      eta = 0.0E0_nag_wp

      ifail = 0
      Call x10aa_t2w_f(ad_handle,ifail)

      ifail = -1
      t%value%tangent = 1.0_nag_wp
      t%tangent%value = 1.0_nag_wp
      Call c05au_t2w_f(ad_handle,x,h,eps,eta,f_t2w,a,b,iuser,ruser,ifail)

      Write (nout,*)
      Write (nout,*) 'Ifail = ', ifail

      Select Case (ifail)
      Case (0)
        Write (nout,99999) 'Root is ', x%value%value
        Write (nout,99998) 'Interval searched is [', a%value%value, ',',       &
          b%value%value, ']'
      Case (3,4)
        Write (nout,99999) 'Final value = ', x%value%value
      Case Default
        Write (nout,99996) 'The routine has failed with ifail = ', ifail
        Go To 100
      End Select

      Write (nout,99997) 'd^2x/dt^2 = ', x%tangent%tangent

100   Continue
      Call x10ab_t2w_f(ad_handle,ifail)

99999 Format (1X,A,F13.5)
99998 Format (1X,A,2(F8.5,A))
99997 Format (1X,A,F26.16)
99996 Format (1X,A,I0)

    End Program c05au_t2w_fe