! E04UE_T1W_F Example Program Text
! Mark 28.3 Release. NAG Copyright 2022.
Module e04ue_t1w_fe_mod
! E04UE_T1W_F Example Program Module:
! Parameters and User-defined Routines
! .. Use Statements ..
Use nag_library, Only: nag_wp
! .. Implicit None Statement ..
Implicit None
! .. Accessibility Statements ..
Private
Public :: confun, objfun
! .. Parameters ..
Real (Kind=nag_wp), Parameter, Public :: one = 1.0_nag_wp
Real (Kind=nag_wp), Parameter, Public :: zero = 0.0_nag_wp
Integer, Parameter, Public :: inc1 = 1, lcwsav = 1, liwsav = 610, &
llwsav = 120, lrwsav = 475, nin = 5, &
nout = 6
Contains
Subroutine objfun(ad_handle,mode,n,x,objf,objgrd,nstate,iuser,ruser)
! Routine to evaluate objective function and its 1st derivatives.
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: nagad_t1w_w_rtype, Operator (*), Operator (+)
! .. Scalar Arguments ..
Type (c_ptr), Intent (Inout) :: ad_handle
Type (nagad_t1w_w_rtype), Intent (Out) :: objf
Integer, Intent (Inout) :: mode
Integer, Intent (In) :: n, nstate
! .. Array Arguments ..
Type (nagad_t1w_w_rtype), Intent (Inout) :: objgrd(n), ruser(*)
Type (nagad_t1w_w_rtype), Intent (In) :: x(n)
Integer, Intent (Inout) :: iuser(*)
! .. Executable Statements ..
If (mode==0 .Or. mode==2) Then
objf = x(1)*x(4)*(ruser(1)*x(1)+ruser(2)*x(2)+ruser(3)*x(3)) + x(3)
End If
If (mode==1 .Or. mode==2) Then
objgrd(1) = x(4)*(ruser(1)*x(1)+ruser(1)*x(1)+ruser(2)*x(2)+ruser(3) &
*x(3))
objgrd(2) = x(1)*x(4)*ruser(2)
objgrd(3) = x(1)*x(4)*ruser(3) + one
objgrd(4) = x(1)*(x(1)*ruser(1)+x(2)*ruser(2)+x(3)*ruser(3))
End If
Return
End Subroutine objfun
Subroutine confun(ad_handle,mode,ncnln,n,ldcj,needc,x,c,cjac,nstate, &
iuser,ruser)
! Routine to evaluate the nonlinear constraints and their 1st
! derivatives.
! .. Use Statements ..
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: nagad_t1w_w_rtype, Assignment (=), &
Operator (+), Operator (**), Operator (*)
! .. Scalar Arguments ..
Type (c_ptr), Intent (Inout) :: ad_handle
Integer, Intent (In) :: ldcj, n, ncnln, nstate
Integer, Intent (Inout) :: mode
! .. Array Arguments ..
Type (nagad_t1w_w_rtype), Intent (Out) :: c(ncnln)
Type (nagad_t1w_w_rtype), Intent (Inout) :: cjac(ldcj,n), ruser(*)
Type (nagad_t1w_w_rtype), Intent (In) :: x(n)
Integer, Intent (Inout) :: iuser(*)
Integer, Intent (In) :: needc(ncnln)
! .. Executable Statements ..
If (nstate==1) Then
! First call to CONFUN. Set all Jacobian elements to zero.
! Note that this will only work when 'Derivative Level = 3'
! (the default; see Section 11.2).
cjac(1:ncnln,1:n) = zero
End If
If (needc(1)>0) Then
If (mode==0 .Or. mode==2) Then
c(1) = x(1)**2 + x(2)**2 + x(3)**2 + x(4)**2
End If
If (mode==1 .Or. mode==2) Then
cjac(1,1) = x(1) + x(1)
cjac(1,2) = x(2) + x(2)
cjac(1,3) = x(3) + x(3)
cjac(1,4) = x(4) + x(4)
End If
End If
If (needc(2)>0) Then
If (mode==0 .Or. mode==2) Then
c(2) = x(1)*x(2)*x(3)*x(4)
End If
If (mode==1 .Or. mode==2) Then
cjac(2,1) = x(2)*x(3)*x(4)
cjac(2,2) = x(1)*x(3)*x(4)
cjac(2,3) = x(1)*x(2)*x(4)
cjac(2,4) = x(1)*x(2)*x(3)
End If
End If
Return
End Subroutine confun
End Module e04ue_t1w_fe_mod
Program e04ue_t1w_fe
! E04UE_T1W_F Example Main Program
! .. Use Statements ..
Use e04ue_t1w_fe_mod, Only: confun, lcwsav, liwsav, llwsav, lrwsav, nin, &
nout, objfun
Use iso_c_binding, Only: c_ptr
Use nagad_library, Only: e04uc_t1w_f, e04ue_t1w_f, e04wb_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
! .. Local Scalars ..
Type (c_ptr) :: ad_handle
Type (nagad_t1w_w_rtype) :: objf
Integer :: i, ifail, inform, iter, lda, ldcj, &
ldr, liwork, lwork, n, nclin, ncnln, &
sda, sdcjac
! .. Local Arrays ..
Type (nagad_t1w_w_rtype), Allocatable :: a(:,:), bl(:), bu(:), c(:), &
cjac(:,:), clamda(:), objgrd(:), &
r(:,:), work(:), x(:)
Type (nagad_t1w_w_rtype) :: ruser(3), rwsav(lrwsav)
Real (Kind=nag_wp), Allocatable :: a_r(:,:), bl_r(:), bu_r(:), x_r(:)
Real (Kind=nag_wp) :: dr(3)
Integer, Allocatable :: istate(:), iwork(:)
Integer :: iuser(1), iwsav(liwsav)
Logical :: lwsav(llwsav)
Character (80) :: cwsav(lcwsav)
! .. Intrinsic Procedures ..
Intrinsic :: max, size
! .. Executable Statements ..
Write (nout,*) 'E04UE_T1W_F Example Program Results'
ruser(1) = 1.0_nag_wp
ruser(2) = 1.0_nag_wp
ruser(3) = 1.0_nag_wp
! Skip heading in data file
Read (nin,*)
Read (nin,*) n, nclin, ncnln
liwork = 3*n + nclin + 2*ncnln
lda = max(1,nclin)
If (nclin>0) Then
sda = n
Else
sda = 1
End If
ldcj = max(1,ncnln)
If (ncnln>0) Then
sdcjac = n
Else
sdcjac = 1
End If
ldr = n
If (ncnln==0 .And. nclin>0) Then
lwork = 2*n**2 + 20*n + 11*nclin
Else If (ncnln>0 .And. nclin>=0) Then
lwork = 2*n**2 + n*nclin + 2*n*ncnln + 20*n + 11*nclin + 21*ncnln
Else
lwork = 20*n
End If
Allocate (istate(n+nclin+ncnln),iwork(liwork),a_r(lda,sda), &
bl_r(n+nclin+ncnln),bu_r(n+nclin+ncnln),c(max(1, &
ncnln)),cjac(ldcj,sdcjac),clamda(n+nclin+ncnln),objgrd(n),r(ldr,n), &
x_r(n),work(lwork))
If (nclin>0) Then
Read (nin,*)(a_r(i,1:sda),i=1,nclin)
End If
Read (nin,*) bl_r(1:(n+nclin+ncnln))
Read (nin,*) bu_r(1:(n+nclin+ncnln))
Read (nin,*) x_r(1:n)
! Create AD configuration data object
ifail = 0
Call x10aa_t1w_f(ad_handle,ifail)
Allocate (a(size(a_r,1),size(a_r,2)),bl(size(bl_r)),bu(size(bu_r)),x( &
size(x_r)))
a(:,:) = a_r(:,:)
bl(:) = bl_r(:)
bu(:) = bu_r(:)
Do i = 1, 3
Call nagad_t1w_set_derivative(ruser(i),1.0_nag_wp)
x(:) = x_r(:)
! Initialise E04UE
ifail = 0
Call e04wb_t1w_f('E04UCA',cwsav,lcwsav,lwsav,llwsav,iwsav,liwsav, &
rwsav,lrwsav,ifail)
If (i==3) Then
! Set option via string
Call e04ue_t1w_f('Print Level = 1',lwsav,iwsav,rwsav,inform)
End If
! Solve the problem
objf = 0.0_nag_wp
ifail = -1
Call e04uc_t1w_f(ad_handle,n,nclin,ncnln,lda,ldcj,ldr,a,bl,bu,confun, &
objfun,iter,istate,c,cjac,clamda,objf,objgrd,r,x,iwork,liwork,work, &
lwork,iuser,ruser,lwsav,iwsav,rwsav,ifail)
ruser(i)%tangent = 0.0_nag_wp
dr(i) = objf%tangent
End Do
Write (nout,*)
Write (nout,*) ' Derivatives calculated: First order tangents'
Write (nout,*) ' Computational mode : algorithmic'
Write (nout,*)
Call x04caf('General',' ',3,1,dr,3,' dobjf/druser',ifail)
Write (nout,*)
ifail = 0
Call x10ab_t1w_f(ad_handle,ifail)
End Program e04ue_t1w_fe