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

NAG AD Library Introduction
Example description
    Program s18de_a1w_fe

!     S18DE_A1W_F Example Program Text
!     Mark 30.0 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: 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_ctype, nagad_a1w_w_rtype,         &
                               s18de_a1w_f, x10aa_a1w_f, x10ab_a1w_f,          &
                               Assignment (=)
      Use nag_library, Only: nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: n = 1, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: fnu
      Type (nagad_a1w_w_ctype)         :: z
      Real (Kind=nag_wp)               :: dyidnu, dyrdnu
      Integer                          :: ifail, nz
      Character (1)                    :: scal
!     .. Local Arrays ..
      Type (nagad_a1w_w_ctype)         :: cy(n)
!     .. Executable Statements ..
      Write (nout,*) 'S18DE_A1W_F Example Program Results'

      Call nagad_a1w_ir_create

      fnu = 5.5_nag_wp
      scal = 'S'
      z%vreal = -6.1_nag_wp
      z%vimag = 9.8_nag_wp

      Write (nout,99998) 'FNU', 'Z', 'SCAL', 'CY(1)', 'dCY/dFNU'

!     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(fnu)

      ifail = 0
      Call s18de_a1w_f(ad_handle,fnu,z,n,scal,cy,nz,ifail)

!     Get derivatives
      Call nagad_a1w_inc_derivative(cy(1)%vreal,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      dyrdnu = nagad_a1w_get_derivative(fnu)

      Call nagad_a1w_ir_zero_adjoints
      Call nagad_a1w_inc_derivative(cy(1)%vimag,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      dyidnu = nagad_a1w_get_derivative(fnu)

      Write (nout,99999) fnu%value, z%vreal%value, z%vimag%value, scal,        &
        cy(1)%vreal%value, cy(1)%vimag%value, dyrdnu, dyidnu

      ifail = 0
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove

99999 Format (1X,F7.4,'  (',F7.3,',',F7.3,')   ',A,2('  (',F7.3,',',F7.3,')'))
99998 Format (/,A7,A12,A12,A12,A19,/)
    End Program s18de_a1w_fe