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

NAG AD Library Introduction
Example description
    Program e04nc_a1w_fe
!     E04NC_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: e04nc_a1w_f, e04ne_a1w_f, e04wb_a1w_f,          &
                               nagad_a1w_get_derivative,                       &
                               nagad_a1w_inc_derivative,                       &
                               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, x10za_a1w_f,          &
                               Assignment (=)
      Use nag_library, Only: nag_wp, x04caf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lcwsav = 1, liwsav = 610,            &
                                          llwsav = 120, lrwsav = 475, nin = 5, &
                                          nout = 6
!     .. Local Scalars ..
      Type (c_ptr)                     :: ad_handle
      Type (nagad_a1w_w_rtype)         :: obj
      Integer                          :: i, ifail, inform, iter, lda, ldc,    &
                                          liwork, lwork, m, n, nclin, sdc
!     .. Local Arrays ..
      Type (nagad_a1w_w_rtype), Allocatable :: a(:,:), a_in(:,:), b(:), bl(:), &
                                          bu(:), b_in(:), c(:,:), clamda(:),   &
                                          cvec(:), rwsav(:), work(:), x(:)
      Real (Kind=nag_wp), Allocatable  :: a_r(:,:), bl_r(:), bu_r(:), b_r(:),  &
                                          c_r(:,:), db(:), dbl(:), dbu(:),     &
                                          x_r(:)
      Integer, Allocatable             :: istate(:), iwork(:), iwsav(:), kx(:)
      Logical, Allocatable             :: lwsav(:)
      Character (80), Allocatable      :: cwsav(:)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (nout,*) 'E04NC_A1W_F Example Program Results'

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

      Read (nin,*) m, n, nclin
      liwork = n
      ldc = max(1,nclin)
      lda = max(1,m)

      If (nclin>0) Then
        sdc = n
      Else
        sdc = 1
      End If

!     This particular example problem is of type LS1, so we allocate
!     A(LDA,N), CVEC(1), B(M) and define LWORK as below
      If (nclin>0) Then
        lwork = 2*n**2 + 9*n + 6*nclin
      Else
        lwork = 9*n
      End If

      Allocate (istate(n+nclin),kx(n),iwork(liwork),c_r(ldc,sdc),              &
        bl_r(n+nclin),bu_r(n+nclin),cvec(1),x_r(n),a_r(lda,n),b_r(m),          &
        clamda(n+nclin),work(lwork),iwsav(liwsav),lwsav(llwsav),cwsav(lcwsav), &
        rwsav(lrwsav))

      Read (nin,*)(a_r(i,1:n),i=1,m)
      Read (nin,*) b_r(1:m)
      Read (nin,*)(c_r(i,1:sdc),i=1,nclin)
      Read (nin,*) bl_r(1:(n+nclin))
      Read (nin,*) bu_r(1:(n+nclin))
      Read (nin,*) x_r(1:n)

!     Create AD tape
      Call x10za_a1w_f

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

!     Initialise E04NC
      ifail = 0
      Call e04wb_a1w_f('E04NCA',cwsav,lcwsav,lwsav,llwsav,iwsav,liwsav,rwsav,  &
        lrwsav,ifail)

!     Set option via string
      Call e04ne_a1w_f('Print Level = 1',lwsav,iwsav,rwsav,inform)

      Allocate (a(lda,n),b(m),c(ldc,sdc),bl(n+nclin),bu(n+nclin),x(n))
      Allocate (a_in(lda,n),b_in(m))
      Allocate (db(m),dbl(n+nclin),dbu(n+nclin))
      a_in(:,:) = a_r(:,:)
      b_in(:) = b_r(:)
      c(:,:) = c_r(:,:)
      bl(:) = bl_r(:)
      bu(:) = bu_r(:)
      x(:) = x_r(:)

!     Register variables to differentiate w.r.t.
      Call nagad_a1w_ir_register_variable(b_in)
      Call nagad_a1w_ir_register_variable(bl)
      Call nagad_a1w_ir_register_variable(bu)

      a = a_in
      b = b_in

!     Solve the problem
      ifail = -1

      Call e04nc_a1w_f(ad_handle,m,n,nclin,ldc,lda,c,bl,bu,cvec,istate,kx,x,a, &
        b,iter,obj,clamda,iwork,liwork,work,lwork,lwsav,iwsav,rwsav,ifail)

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

      Call nagad_a1w_inc_derivative(obj,1.0_nag_wp)
      ifail = 0
      Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
      b_r = nagad_a1w_get_derivative(b_in)
      bl_r = nagad_a1w_get_derivative(bl)
      bu_r = nagad_a1w_get_derivative(bu)
      Write (nout,*)
      Call x04caf('General',' ',m,1,b_r,m,' dobj/db',ifail)
      Write (nout,*)
      Call x04caf('General',' ',n+nclin,1,bl_r,n+nclin,' dobj/dbl',ifail)
      Write (nout,*)
      Call x04caf('General',' ',n+nclin,1,bu_r,n+nclin,' dobj/dbu',ifail)

!     Remove computational data object and tape
      Call x10ab_a1w_f(ad_handle,ifail)
      Call nagad_a1w_ir_remove
    End Program e04nc_a1w_fe