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

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

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

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

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

        Return

      End Subroutine f_a1w
    End Module c05au_a1w_fe_mod

    Program c05au_a1w_fe

!     C05AU_A1W_F Example Main Program

!     .. Use Statements ..
      Use c05au_a1w_fe_mod, Only: f_a1w, nout, t
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: c05au_a1w_f, nagad_a1w_get_derivative,          &
                               nagad_a1w_inc_derivative,                       &
                               nagad_a1w_ir_create => x10za_a1w_f,             &
                               nagad_a1w_ir_interpret_adjoint_sparse,          &
                               nagad_a1w_ir_register_variable,                 &
                               nagad_a1w_ir_remove, nagad_a1w_w_rtype,         &
                               x10aa_a1w_f, x10ab_a1w_f, Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Local Scalars ..
      Type (nagad_a1w_w_rtype)         :: a, b, eps, eta, h, x
      Type (c_ptr)                     :: ad_handle
      Integer                          :: adifail, ifail
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..
      Write (nout,*) 'C05AU_A1W_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

      Call nagad_a1w_ir_create

      ifail = 0
      Call x10aa_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_register_variable(t)

      ifail = -1
      Call c05au_a1w_f(ad_handle,x,h,eps,eta,f_a1w,a,b,iuser,ruser,ifail)

      Call nagad_a1w_inc_derivative(x,1.0_nag_wp)
      Call nagad_a1w_ir_interpret_adjoint_sparse(adifail)
      Write (nout,*)

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

      Write (nout,99997) 'dx/dt = ', nagad_a1w_get_derivative(t)
100   Continue
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove


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_a1w_fe