! D01RG_A1W_F Example Program Text
! Mark 30.1 Release. NAG Copyright 2024.
Module d01rg_a1w_fe_mod
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: log, nagad_a1w_w_rtype, sin, Assignment (=), &
Operator (/), Operator (*), Operator (-)
Use nag_library, Only: nag_wp
! .. Implicit None Statement ..
Implicit None
! .. Accessibility Statements ..
Private
Public :: f
! .. Parameters ..
Integer, Parameter, Public :: nin = 5, nout = 6
Contains
Subroutine f(ad_handle,x,nx,fv,iflag,iuser,ruser)
! .. Scalar Arguments ..
Type (c_ptr), Intent (Inout) :: ad_handle
Integer, Intent (Inout) :: iflag
Integer, Intent (In) :: nx
! .. Array Arguments ..
Type (nagad_a1w_w_rtype), Intent (Out) :: fv(nx)
Type (nagad_a1w_w_rtype), Intent (Inout) :: ruser(*)
Type (nagad_a1w_w_rtype), Intent (In) :: x(nx)
Integer, Intent (Inout) :: iuser(*)
! .. Executable Statements ..
fv = sin(x)/x*log(10.0_nag_wp*(1.0_nag_wp-x))
Return
End Subroutine f
End Module d01rg_a1w_fe_mod
Program d01rg_a1w_fe
! D01RG_A1W_F Example Main Program
! .. Use Statements ..
Use d01rg_a1w_fe_mod, Only: f, nin, nout
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: d01rg_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, x07caf, x07cbf
! .. Implicit None Statement ..
Implicit None
! .. Local Scalars ..
Type (nagad_a1w_w_rtype) :: a, b, dinest, epsabs, epsrel, errest
Type (c_ptr) :: ad_handle
Real (Kind=nag_wp) :: ar, br, da, db, e1r, e2r
Integer :: ifail, nevals
! .. Local Arrays ..
Type (nagad_a1w_w_rtype) :: ruser(1)
Integer :: exmode(3), exmode_old(3), iuser(1)
! .. Executable Statements ..
Write (nout,*) 'D01RG_A1W_F Example Program Results'
! The example function can raise various exceptions - it contains
! a division by zero and a log singularity - although its integral
! is well behaved.
Call x07caf(exmode_old)
! Save the original halting mode
! Turn exception halting mode off for the three common exceptions
exmode = (/0,0,0/)
Call x07cbf(exmode)
! Skip first line of data file
Read (nin,*)
! Read problem parameters and initialize AD types
Read (nin,*) ar
Read (nin,*) br
Read (nin,*) e1r
Read (nin,*) e2r
! Create AD tape
Call nagad_a1w_ir_create
! Initialize AD types
a = ar
b = br
epsabs = e1r
epsrel = e2r
! Create AD configuration data object
ifail = 0
Call x10aa_a1w_f(ad_handle,ifail)
! Register variables to differentiate w.r.t.
Call nagad_a1w_ir_register_variable(a)
Call nagad_a1w_ir_register_variable(b)
! Evaluate the integral using the AD routine
ifail = -1
Call d01rg_a1w_f(ad_handle,a,b,f,epsabs,epsrel,dinest,errest,nevals, &
iuser,ruser,ifail)
If (ifail<0) Then
Write (nout,99999) 'The routine has failed with ifail = ', ifail
Go To 100
99999 Format (1X,A,I0)
End If
! Print inputs and primal outputs
Write (nout,*)
Write (nout,99998) 'a ', 'lower limit of integration', a%value
Write (nout,99998) 'b ', 'upper limit of integration', b%value
Write (nout,99997) 'epsabs', 'absolute accuracy requested', epsabs%value
Write (nout,99997) 'epsrel', 'relative accuracy requested', epsrel%value
Write (nout,*)
If (ifail>=0) Then
Write (nout,99996) 'dinest', 'approximation to the integral', &
dinest%value
Write (nout,99997) 'errest', 'estimate of the absolute error', &
errest%value
Write (nout,99995) 'nevals', 'number of function evaluations', nevals
End If
99998 Format (1X,A6,' - ',A30,' = ',F10.4)
99997 Format (1X,A6,' - ',A30,' = ',E10.2)
99996 Format (1X,A6,' - ',A30,' = ',F10.5)
99995 Format (1X,A6,' - ',A30,' = ',I10)
! Setup evaluation of derivatives via adjoints
Call nagad_a1w_inc_derivative(dinest,1.0E0_nag_wp)
Call nagad_a1w_ir_interpret_adjoint_sparse(ifail)
Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order adjoints'
Write (nout,*) ' Computational mode : algorithmic'
! Get derivatives
da = nagad_a1w_get_derivative(a)
db = nagad_a1w_get_derivative(b)
Write (nout,*) ' Derivatives:'
Write (nout,99994) 'd/da(x) =', da
Write (nout,99994) 'd/db(x) =', db
99994 Format (1X,A15,1X,F10.5)
100 Continue
! Remove computational data object and tape
Call x10ab_a1w_f(ad_handle,ifail)
Call nagad_a1w_ir_remove
! Restore the original halting mode
Call x07cbf(exmode_old)
End Program d01rg_a1w_fe