! C05RC_A1T1W_F Example Program Text
! Mark 29.2 Release. NAG Copyright 2023.
Module c05rc_a1t1w_fe_mod
! C05RC_A1T1W_F Example Program Module:
! Parameters and User-defined Routines
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: nagad_a1t1w_w_rtype, Operator (-), Operator (+), &
Assignment (=), Operator (*)
Use nag_library, Only: nag_wp
! .. Implicit None Statement ..
Implicit None
! .. Accessibility Statements ..
Private
Public :: fcn
! .. Parameters ..
Integer, Parameter, Public :: maxfev = 2000, mode = 2, n = 7, &
nout = 6, nprint = 0
Contains
Subroutine fcn(ad_handle,n,x,fvec,fjac,iuser,ruser,iflag)
! .. Scalar Arguments ..
Type (c_ptr), Intent (Inout) :: ad_handle
Integer, Intent (Inout) :: iflag
Integer, Intent (In) :: n
! .. Array Arguments ..
Type (nagad_a1t1w_w_rtype), Intent (Inout) :: fjac(n,n), fvec(n), &
ruser(*)
Type (nagad_a1t1w_w_rtype), Intent (In) :: x(n)
Integer, Intent (Inout) :: iuser(*)
! .. Local Scalars ..
Integer :: i
! .. Executable Statements ..
If (iflag==0) Then
If (nprint>0) Then
! Insert print statements here if desired.
Continue
End If
Else If (iflag/=2) Then
Do i = 1, n
fvec(i) = (ruser(2)+ruser(3)*x(i))*x(i) - ruser(5)
End Do
Do i = 2, n
fvec(i) = fvec(i) + ruser(1)*x(i-1)
End Do
Do i = 1, n - 1
fvec(i) = fvec(i) + ruser(4)*x(i+1)
End Do
Else
fjac(1:n,1:n) = 0.0_nag_wp
fjac(1,1) = ruser(2) + 2.0_nag_wp*ruser(3)*x(1)
fjac(1,2) = ruser(4)
Do i = 2, n - 1
fjac(i,i-1) = ruser(1)
fjac(i,i) = ruser(2) + 2.0_nag_wp*ruser(3)*x(i)
fjac(i,i+1) = ruser(4)
End Do
fjac(n,n-1) = ruser(1)
fjac(n,n) = ruser(2) + 2.0_nag_wp*ruser(3)*x(n)
End If
! Set iflag negative to terminate execution for any reason.
iflag = 0
Return
End Subroutine fcn
End Module c05rc_a1t1w_fe_mod
Program c05rc_a1t1w_fe
! C05RC_A1T1W_F Example Main Program
! .. Use Statements ..
Use c05rc_a1t1w_fe_mod, Only: fcn, maxfev, mode, n, nout, nprint
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: c05rc_a1t1w_f, nagad_a1t1w_get_derivative, &
nagad_a1t1w_ir_create => x10za_a1t1w_f, &
nagad_a1t1w_ir_interpret_adjoint_sparse, &
nagad_a1t1w_ir_register_variable, &
nagad_a1t1w_ir_remove, nagad_a1t1w_ir_zero_adjoints &
, nagad_a1t1w_set_derivative, nagad_a1t1w_w_rtype, &
nagad_t1w_w_rtype, x10aa_a1t1w_f, x10ab_a1t1w_f, &
Assignment (=)
Use nag_library, Only: nag_wp, x02ajf, x04caf
! .. Implicit None Statement ..
Implicit None
! .. Local Scalars ..
Type (c_ptr) :: ad_handle
Type (nagad_a1t1w_w_rtype) :: factor, xtol
Type (nagad_t1w_w_rtype) :: dtemp
Integer :: i, ifail, nfev, njev
! .. Local Arrays ..
Type (nagad_a1t1w_w_rtype), Allocatable :: diag(:), fjac(:,:), fvec(:), &
qtf(:), r(:), x(:)
Type (nagad_a1t1w_w_rtype) :: ruser(5)
Real (Kind=nag_wp) :: d2r_sum
Integer :: iuser(1)
! .. Intrinsic Procedures ..
Intrinsic :: sqrt
! .. Executable Statements ..
Write (nout,*) 'C05RC_A1T1W_F Example Program Results'
Allocate (diag(n),fjac(n,n),fvec(n),qtf(n),r(n*(n+1)/2),x(n))
! The following starting values provide a rough solution.
x(1:n) = -1.0_nag_wp
x(1:n)%tapeindex = 0
! Create AD tape
Call nagad_a1t1w_ir_create
! Create AD configuration data object
ifail = 0
Call x10aa_a1t1w_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_a1t1w_ir_register_variable(ruser)
! We're computing the sum of all 2nd derivatives; so seed all 2nd order input tangents to 1.0
ruser%value%tangent = 1.0_nag_wp
xtol = sqrt(x02ajf())
factor = 100._nag_wp
diag(1:n) = 1.0_nag_wp
ifail = 0
Call c05rc_a1t1w_f(ad_handle,fcn,n,x,fvec,fjac,xtol,maxfev,mode,diag, &
factor,nprint,nfev,njev,r,qtf,iuser,ruser,ifail)
Write (nout,*) 'Final approximate solution'
Write (nout,99999)(x(i)%value,i=1,n)
99999 Format (1X,3F12.4)
! We're computing the sum of all 2nd derivatives; so seed all output adjoints to 1.0
Do i = 1, n
dtemp = 1.0_nag_wp
Call nagad_a1t1w_set_derivative(x(i),dtemp)
End Do
ifail = 0
Call nagad_a1t1w_ir_interpret_adjoint_sparse(ifail)
! Get derivatives
d2r_sum = 0.0_nag_wp
Do i = 1, 5
dtemp = nagad_a1t1w_get_derivative(ruser(i))
d2r_sum = d2r_sum + dtemp%tangent
End Do
Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order adjoints'
Write (nout,*) ' Computational mode : algorithmic'
Write (nout,*)
Write (nout,*) ' Sum of 2nd Derivatives are of solution w.r.t function params'
Write (nout,99999) d2r_sum
Write (nout,*)
! Remove computational data object and tape
Call x10ab_a1t1w_f(ad_handle,ifail)
Call nagad_a1t1w_ir_remove
End Program c05rc_a1t1w_fe