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

NAG AD Library Introduction
Example description
!   D01UA_A1W_F Example Program Text
!   Mark 30.0 Release. NAG Copyright 2024.

    Module d01ua_a1w_fe_mod

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: exp, log, nagad_a1w_w_rtype, Assignment (=),    &
                               Operator (/), Operator (-), Operator (*),       &
                               Operator (+)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Accessibility Statements ..
      Private
      Public                           :: f_a1w
!     .. Parameters ..
      Integer, Parameter, Public       :: nout = 6
    Contains
      Subroutine f_a1w(ad_handle,x,nx,fv,iflag,iuser,ruser)

!       .. Scalar Arguments ..
        Type (c_ptr), Intent (Inout)   :: ad_handle
        Integer, Intent (Inout)        :: iflag
        Integer, Intent (In)           :: nx
!       .. Array Arguments ..
        Type (nagad_a1w_w_rtype), Intent (Out) :: fv(nx)
        Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
        Type (nagad_a1w_w_rtype), Intent (In) :: x(nx)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        Select Case (iuser(1))
        Case (1)
          fv = 4.0E0_nag_wp/(1.0E0_nag_wp+ruser(1)*x*x)
        Case (2)
          fv = 1.0E0_nag_wp/(x*x*log(ruser(1)*x))
        Case (3)
          fv = exp(-ruser(1)*x)/x
        Case (4)
          fv = ruser(1)/x
        Case (5)
          fv = exp(-3.0E0_nag_wp*ruser(1)*x*x-4.0E0_nag_wp*x-1.0E0_nag_wp)
        Case (6)
          fv = exp(2.0E0_nag_wp*ruser(1)*x+2.0E0_nag_wp)
        Case Default
          iflag = -1
        End Select
        Return
      End Subroutine f_a1w
    End Module d01ua_a1w_fe_mod

    Program d01ua_a1w_fe
!     D01UA_A1W_F Example Main Program

!     .. Use Statements ..
      Use d01ua_a1w_fe_mod, Only: f_a1w, nout
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: d01ua_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_ir_zero_adjoints &
                               , 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, dinest
      Type (c_ptr)                     :: ad_handle
      Real (Kind=nag_wp)               :: dr
      Integer                          :: funid, ifail, key, n
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype)         :: ruser(1)
      Integer                          :: iuser(1)
!     .. Executable Statements ..

      Write (nout,*) 'D01UA_A1W_F Example Program Results'

      n = 64
      ruser(1) = 1.0_nag_wp

!     Create AD tape
      Call nagad_a1w_ir_create

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

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(ruser(1))

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order adjoints'
      Write (nout,*) ' Computational mode    : algorithmic'

cases: Do funid = 1, 6
        Write (nout,*)
        Select Case (funid)
        Case (1)
          Write (nout,*) 'Gauss-Legendre example'
          a = 0.0_nag_wp
          b = 1.0_nag_wp
          key = 0
        Case (2)
          Write (nout,*) 'Rational Gauss example'
          a = 2.0_nag_wp
          b = 0.0_nag_wp
          key = -5
        Case (3)
          Write (nout,*) 'Gauss-Laguerre example (adjusted weights)'
          a = 2.0_nag_wp
          b = 1.0_nag_wp
          key = -3
        Case (4)
          Write (nout,*) 'Gauss-Laguerre example (normal weights)'
          a = 2.0_nag_wp
          b = 1.0_nag_wp
          key = 3
        Case (5)
          Write (nout,*) 'Gauss-Hermite example (adjusted weights)'
          a = -1.0_nag_wp
          b = 3.0_nag_wp
          key = -4
        Case (6)
          Write (nout,*) 'Gauss-Hermite example (normal weights)'
          a = -1.0_nag_wp
          b = 3.0_nag_wp
          key = 4
        End Select
        iuser(1) = funid

!       Call the AD routine
        ifail = 0
        Call d01ua_a1w_f(ad_handle,key,a,b,n,f_a1w,dinest,iuser,ruser,ifail)

        Call nagad_a1w_ir_zero_adjoints
        Call nagad_a1w_inc_derivative(dinest,1.0E0_nag_wp)
        Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

!       Get derivatives
        dr = nagad_a1w_get_derivative(ruser(1))

        Write (nout,*)
        Write (nout,99999) 'dinest   =', dinest%value, 'd/druser = ', dr
99999   Format (1X,A,1X,F10.5,3X,A,1X,F10.5)

      End Do cases

!     Remove computational data object and tape
      ifail = 0
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

    End Program d01ua_a1w_fe