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

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

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: e02bc_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, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: x
      Integer                          :: i, ifail, j, l, left, m, ncap, ncap7
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: c(:), lamda(:)
      Type (nagad_a1w_w_rtype)         :: s(4)
      Real (Kind=nag_wp), Allocatable  :: dsdc(:,:,:), dsdl(:,:,:)
!     .. Executable Statements ..
      Write (nout,*) 'E02BC_A1W_F Example Program Results'

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

!     Skip heading in data file
      Read (nin,*)

      Read (nin,*) ncap, m
      ncap7 = ncap + 7
      Allocate (lamda(ncap7),c(ncap7),dsdl(ncap7,m,2),dsdc(ncap7,m,2))

      lamda = 0.0_nag_wp
      c = 0.0_nag_wp

      Read (nin,*) lamda(1:ncap7)%value
      Read (nin,*) c(1:(ncap+3))%value

      Do i = 1, m
        x = 0.0_nag_wp
        Read (nin,*) x%value

        Do left = 1, 2
!         A1W computation
          Call nagad_a1w_ir_create

          Call nagad_a1w_ir_register_variable(lamda)
          Call nagad_a1w_ir_register_variable(c)

          ifail = 0
          Call e02bc_a1w_f(ad_handle,ncap7,lamda,c,x,left,s,ifail)

          Call nagad_a1w_inc_derivative(s(1),1.0_nag_wp)
          If (left==1) Then

            If (i==1) Then
              Write (nout,*)
              Write (nout,99999) 'x', 'Spline', '1st deriv', '2nd deriv',      &
                '3rd deriv'
            End If
99999       Format (6X,A,13X,A,5X,A,3X,A,3X,A)
            Write (nout,*)
            Write (nout,99998) x%value, '  Left', (s(l)%value,l=1,4)
          Else
            Write (nout,99998) x%value, ' Right', (s(l)%value,l=1,4)
          End If

          Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)

          Do j = 1, ncap7
            dsdl(j,i,left) = nagad_a1w_get_derivative(lamda(j))
            dsdc(j,i,left) = nagad_a1w_get_derivative(c(j))
          End Do
          Call nagad_a1w_ir_remove

        End Do

      End Do
      Write (nout,*)
      Write (nout,*) ' Derivatives of spline values with respect to knots'
      Call x04caf('General',' ',ncap7,m,dsdl,ncap7,'Left:  ds/dlamda',ifail)
      Write (nout,*)
      Call x04caf('General',' ',ncap7,m,dsdl(1,1,2),ncap7,'Right: ds/dlamda',  &
        ifail)
      Write (nout,*)
      Write (nout,*) ' Derivatives with respect to spline coefficients'
      Call x04caf('General',' ',ncap7,m,dsdc,ncap7,'Left:  ds/dc',ifail)
      Write (nout,*)
      Call x04caf('General',' ',ncap7,m,dsdc(1,1,2),ncap7,'Right: ds/dc',      &
        ifail)

      Call x10ab_a1w_f(ad_handle,ifail)

99998 Format (1X,E10.2,A,4E12.4)

    End Program e02bc_a1w_fe