! C05QS_A1W_F Example Program Text
! Mark 30.0 Release. NAG Copyright 2024.
Module c05qs_a1w_fe_mod
! C05QS_A1W_F Example Program Module:
! Parameters and User-defined Routines
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: nagad_a1w_w_rtype, Assignment (=), &
Operator (-), Operator (+), Operator (*)
! .. Implicit None Statement ..
Implicit None
! .. Accessibility Statements ..
Private
Public :: fcn
! .. Parameters ..
Integer, Parameter, Public :: n = 7, nout = 6
Contains
Subroutine fcn(ad_handle,n,lindf,indf,x,fvec,iuser,ruser,iflag)
! .. Scalar Arguments ..
Type (c_ptr), Intent (Inout) :: ad_handle
Integer, Intent (Inout) :: iflag
Integer, Intent (In) :: lindf, n
! .. Array Arguments ..
Type (nagad_a1w_w_rtype), Intent (Out) :: fvec(n)
Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
Type (nagad_a1w_w_rtype), Intent (In) :: x(n)
Integer, Intent (In) :: indf(lindf)
Integer, Intent (Inout) :: iuser(*)
! .. Local Scalars ..
Integer :: i, ind
! .. Executable Statements ..
iflag = 0
Do ind = 1, lindf
i = indf(ind)
fvec(i) = (ruser(2)+ruser(3)*x(i))*x(i) - ruser(5)
If (i>1) Then
fvec(i) = fvec(i) + ruser(1)*x(i-1)
End If
If (i<n) Then
fvec(i) = fvec(i) + ruser(4)*x(i+1)
End If
End Do
End Subroutine fcn
End Module c05qs_a1w_fe_mod
Program c05qs_a1w_fe
! C05QS_A1W_F Example Main Program
! .. Use Statements ..
Use c05qs_a1w_fe_mod, Only: fcn, n, nout
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: c05qs_a1w_f, nagad_a1w_get_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_set_derivative, nagad_a1w_w_rtype, &
x10aa_a1w_f, x10ab_a1w_f, Assignment (=)
Use nag_library, Only: nag_wp, x02ajf, x04caf
! .. Implicit None Statement ..
Implicit None
! .. Local Scalars ..
Type (c_ptr) :: ad_handle
Type (nagad_a1w_w_rtype) :: xtol
Integer :: i, ifail, licomm, lrcomm
! .. Local Arrays ..
Type (nagad_a1w_w_rtype), Allocatable :: fvec(:), rcomm(:), x(:)
Type (nagad_a1w_w_rtype) :: ruser(5)
Real (Kind=nag_wp), Allocatable :: dr(:,:)
Integer, Allocatable :: icomm(:)
Integer :: iuser(1)
! .. Intrinsic Procedures ..
Intrinsic :: sqrt
! .. Executable Statements ..
Write (nout,*) 'C05QS_A1W_F Example Program Results'
lrcomm = 12 + 3*n
licomm = 8*n + 19 + 3*n
Allocate (fvec(n),x(n),rcomm(lrcomm),icomm(licomm),dr(n,5))
! The following starting values provide a rough solution.
x(1:n) = -1.0_nag_wp
x(1:n)%tapeindex = 0.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)
ruser(1) = -1.0_nag_wp
ruser(2) = 3.0_nag_wp
ruser(3) = -2.0_nag_wp
ruser(4) = -2.0_nag_wp
ruser(5) = -1.0_nag_wp
ruser(1:5)%tapeindex = 0
! Register variables to differentiate w.r.t.
Call nagad_a1w_ir_register_variable(ruser)
xtol = sqrt(x02ajf())
ifail = 0
Call c05qs_a1w_f(ad_handle,fcn,n,x,fvec,xtol,.True.,rcomm,lrcomm,icomm, &
licomm,iuser,ruser,ifail)
Write (nout,*) 'Final approximate solution'
Write (nout,99999)(x(i)%value,i=1,n)
99999 Format (1X,3F12.4)
! Setup evaluation of derivatives via adjoints
Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order adjoints'
Write (nout,*) ' Computational mode : algorithmic'
Write (nout,*)
Write (nout,*) ' Derivatives are of solution w.r.t function params'
Write (nout,*)
Do i = 1, n
Call nagad_a1w_ir_zero_adjoints
Call nagad_a1w_set_derivative(x(i),1.0_nag_wp)
ifail = 0
Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
! Get derivatives
dr(i,1) = nagad_a1w_get_derivative(ruser(1))
dr(i,2) = nagad_a1w_get_derivative(ruser(2))
dr(i,3) = nagad_a1w_get_derivative(ruser(3))
dr(i,4) = nagad_a1w_get_derivative(ruser(4))
dr(i,5) = nagad_a1w_get_derivative(ruser(5))
End Do
Call x04caf('General',' ',n,5,dr,n,' dx/druser',ifail)
! Remove computational data object and tape
Call x10ab_a1w_f(ad_handle,ifail)
Call nagad_a1w_ir_remove
End Program c05qs_a1w_fe