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

NAG AD Library Introduction
Example description
    Program f07ca_t1w_fe

!     F07CA_T1W_F Example Program Text
!     Mark 30.2 Release. NAG Copyright 2024.

!     .. Use Statements ..
      Use iso_c_binding, Only: c_ptr
      Use nagad_library, Only: f07ca_t1w_f, nagad_t1w_set_derivative,          &
                               nagad_t1w_w_rtype, x10aa_t1w_f, x10ab_t1w_f,    &
                               Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6, nrhs = 1
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Integer                          :: i, ifail, k, n
!     .. Local Arrays ..
      Type (nagad_t1w_w_rtype), Allocatable :: b(:), d(:), df(:), dl(:),       &
                                          dlf(:), du(:), duf(:), x(:)
      Real (Kind=nag_wp), Allocatable  :: dxdb(:,:), dxdd(:,:), dxdl(:,:),     &
                                          dxdu(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'F07CA_T1W_F Example Program Results'
      Write (nout,*)
!     Skip heading in data file
      Read (nin,*)
      Read (nin,*) n

      Allocate (b(n),d(n),dl(n-1),du(n-1))
      Allocate (x(n),df(n),dlf(n-1),duf(n-1))
      Allocate (dxdb(n,n),dxdd(n,n),dxdl(n,n-1),dxdu(n,n-1))

!     Read the tridiagonal matrix A and the right hand side B from
!     data file and initialize AD arrays

      Read (nin,*) dxdu(1:n-1,1)
      Read (nin,*) dxdd(1:n,1)
      Read (nin,*) dxdl(1:n-1,1)
      Read (nin,*) dxdb(1:n,1)
      du(1:n-1) = dxdu(1:n-1,1)
      d(1:n) = dxdd(1:n,1)
      dl(1:n-1) = dxdl(1:n-1,1)
      b(1:n) = dxdb(1:n,1)

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

      Do i = 1, 4*n - 2
        If (i<n) Then
          k = i
          Call nagad_t1w_set_derivative(du(k),1.0_nag_wp)
        Else If (i<2*n) Then
          k = i - n + 1
          Call nagad_t1w_set_derivative(d(k),1.0_nag_wp)
        Else If (i<3*n-1) Then
          k = i - 2*n + 1
          Call nagad_t1w_set_derivative(dl(k),1.0_nag_wp)
        Else
          k = i - 3*n + 2
          Call nagad_t1w_set_derivative(b(k),1.0_nag_wp)
        End If
        dlf = dl
        df = d
        duf = du
        x = b

!       Solve the equations Ax = b for x
        ifail = 0
        Call f07ca_t1w_f(ad_handle,n,nrhs,dlf,df,duf,x,n,ifail)

        If (i<n) Then
          du(k)%tangent = 0.0_nag_wp
          dxdu(1:n,k) = x(1:n)%tangent
        Else If (i<2*n) Then
          d(k)%tangent = 0.0_nag_wp
          dxdd(1:n,k) = x(1:n)%tangent
        Else If (i<3*n-1) Then
          dl(k)%tangent = 0.0_nag_wp
          dxdl(1:n,k) = x(1:n)%tangent
        Else
          b(k)%tangent = 0.0_nag_wp
          dxdb(1:n,k) = x(1:n)%tangent
        End If
      End Do

!     Print primal solution
      Write (nout,*) 'Solution'
      Write (nout,99999) x(1:n)%value
99999 Format (1X,7F11.4)

      Write (nout,*)
      Write (nout,*) ' Derivatives calculated: First order tangents'
      Write (nout,*) ' Computational mode    : algorithmic'
      Write (nout,*)
      Write (nout,*) ' Derivatives of solution w.r.t. inputs:'
      Write (nout,*)
      Call x04caf('General',' ',n,n-1,dxdu,n,'dx/d(du)',ifail)
      Write (nout,*)
      Call x04caf('General',' ',n,n,dxdd,n,'dx/d(d)',ifail)
      Write (nout,*)
      Call x04caf('General',' ',n,n-1,dxdl,n,'dx/d(dl)',ifail)
      Write (nout,*)
      Call x04caf('General',' ',n,n,dxdb,n,'dx/d(db)',ifail)

!     Remove computational data object
      Call x10ab_t1w_f(ad_handle,ifail)

    End Program f07ca_t1w_fe