! E04VKF Example Program Text ! Mark 23 Release. NAG Copyright 2011. MODULE e04vkfe_mod ! E04VKF Example Program Module: ! Parameters and User-defined Routines ! .. Use Statements .. USE nag_library, ONLY : nag_wp ! .. Implicit None Statement .. IMPLICIT NONE ! .. Parameters .. INTEGER, PARAMETER :: lencw = 600, leniw = 600, & lenrw = 600, nin = 5, & ninopt = 7, nout = 6 CONTAINS SUBROUTINE usrfun(status,n,x,needf,nf,f,needg,leng,g,cuser,iuser,ruser) ! .. Implicit None Statement .. IMPLICIT NONE ! .. Scalar Arguments .. INTEGER, INTENT (IN) :: leng, n, needf, needg, nf INTEGER, INTENT (INOUT) :: status ! .. Array Arguments .. REAL (KIND=nag_wp), INTENT (INOUT) :: f(nf), g(leng), ruser(*) REAL (KIND=nag_wp), INTENT (IN) :: x(n) INTEGER, INTENT (INOUT) :: iuser(*) CHARACTER (8), INTENT (INOUT) :: cuser(*) ! .. Intrinsic Functions .. INTRINSIC cos, sin ! .. Executable Statements .. IF (needf>0) THEN ! The nonlinear components of f_i(x) need to be assigned, ! for i = 1 to NF f(1) = 1000.0E+0_nag_wp*sin(-x(1)-0.25E+0_nag_wp) + & 1000.0E+0_nag_wp*sin(-x(2)-0.25E+0_nag_wp) f(2) = 1000.0E+0_nag_wp*sin(x(1)-0.25E+0_nag_wp) + & 1000.0E+0_nag_wp*sin(x(1)-x(2)-0.25E+0_nag_wp) f(3) = 1000.0E+0_nag_wp*sin(x(2)-x(1)-0.25E+0_nag_wp) + & 1000.0E+0_nag_wp*sin(x(2)-0.25E+0_nag_wp) ! N.B. in this example there is no need to assign for the wholly ! linear components f_4(x) and f_5(x). f(6) = 1.0E-6_nag_wp*x(3)**3 + 2.0E-6_nag_wp*x(4)**3/ & 3.0E+0_nag_wp END IF IF (needg>0) THEN ! The derivatives of the function f_i(x) need to be assigned. ! G(k) should be set to partial derivative df_i(x)/dx_j where ! i = IGFUN(k) and j = IGVAR(k), for k = 1 to LENG. g(1) = -1000.0E+0_nag_wp*cos(-x(1)-0.25E+0_nag_wp) g(2) = -1000.0E+0_nag_wp*cos(-x(2)-0.25E+0_nag_wp) g(3) = 1000.0E+0_nag_wp*cos(x(1)-0.25E+0_nag_wp) + & 1000.0E+0_nag_wp*cos(x(1)-x(2)-0.25E+0_nag_wp) g(4) = -1000.0E+0_nag_wp*cos(x(1)-x(2)-0.25E+0_nag_wp) g(5) = -1000.0E+0_nag_wp*cos(x(2)-x(1)-0.25E+0_nag_wp) g(6) = 1000.0E+0_nag_wp*cos(x(2)-x(1)-0.25E+0_nag_wp) + & 1000.0E+0_nag_wp*cos(x(2)-0.25E+0_nag_wp) g(7) = 3.0E-6_nag_wp*x(3)**2 g(8) = 2.0E-6_nag_wp*x(4)**2 END IF RETURN END SUBROUTINE usrfun END MODULE e04vkfe_mod PROGRAM e04vkfe ! E04VKF Example Main Program ! .. Use Statements .. USE nag_library, ONLY : e04vgf, e04vhf, e04vkf, e04vlf, e04vmf, e04vnf, & e04vrf, e04vsf, nag_wp, x04acf, x04baf USE e04vkfe_mod, ONLY : lencw, leniw, lenrw, nin, ninopt, nout, usrfun ! .. Implicit None Statement .. IMPLICIT NONE ! .. Parameters .. CHARACTER (*), PARAMETER :: fname = 'e04vkfe.opt' ! .. Local Scalars .. REAL (KIND=nag_wp) :: bndinf, featol, objadd, sinf INTEGER :: elmode, i, ifail, lena, leng, & mode, n, nea, neg, nf, nfname, & ninf, ns, nxname, objrow, start CHARACTER (8) :: prob CHARACTER (80) :: rec ! .. Local Arrays .. REAL (KIND=nag_wp), ALLOCATABLE :: a(:), f(:), flow(:), fmul(:), & fupp(:), x(:), xlow(:), xmul(:), & xupp(:) REAL (KIND=nag_wp) :: ruser(1), rw(lenrw) INTEGER, ALLOCATABLE :: fstate(:), iafun(:), igfun(:), & javar(:), jgvar(:), xstate(:) INTEGER :: iuser(1), iw(leniw) CHARACTER (8) :: cuser(1), cw(lencw) CHARACTER (8), ALLOCATABLE :: fnames(:), xnames(:) ! .. Intrinsic Functions .. INTRINSIC max ! .. Executable Statements .. WRITE (rec,99995) 'E04VKF Example Program Results' CALL x04baf(nout,rec) ! This program demonstrates the use of routines to set and ! get values of optional parameters associated with E04VHF. ! Skip heading in data file READ (nin,*) READ (nin,*) n, nf READ (nin,*) nea, neg, objrow, start lena = max(1,nea) leng = max(1,neg) nxname = n nfname = nf ALLOCATE (iafun(lena),javar(lena),igfun(leng),jgvar(leng),xstate(n), & fstate(nf),a(lena),xlow(n),xupp(n),flow(nf),fupp(nf),x(n),xmul(n), & f(nf),fmul(nf),xnames(nxname),fnames(nfname)) ! Read the variable names READ (nin,*) xnames(1:nxname) ! Read the function names READ (nin,*) fnames(1:nfname) ! Read the sparse matrix A, the linear part of F DO i = 1, nea ! For each element read row, column, A(row,column) READ (nin,*) iafun(i), javar(i), a(i) END DO ! Read the structure of sparse matrix G, the nonlinear part of F DO i = 1, neg ! For each element read row, column READ (nin,*) igfun(i), jgvar(i) END DO ! Read the lower and upper bounds on the variables DO i = 1, n READ (nin,*) xlow(i), xupp(i) END DO ! Read the lower and upper bounds on the functions DO i = 1, nf READ (nin,*) flow(i), fupp(i) END DO ! Initialise X, XSTATE, XMUL, F, FSTATE, FMUL READ (nin,*) x(1:n) READ (nin,*) xstate(1:n) READ (nin,*) xmul(1:n) READ (nin,*) f(1:nf) READ (nin,*) fstate(1:nf) READ (nin,*) fmul(1:nf) objadd = 0.0E0_nag_wp prob = ' ' ! Call E04VGF to initialise E04VHF. ifail = 0 CALL e04vgf(cw,lencw,iw,leniw,rw,lenrw,ifail) ! By default E04VHF does not print monitoring ! information. Set the print file unit or the summary ! file unit to get information. ifail = 0 CALL e04vmf('Print file',nout,cw,iw,rw,ifail) ! Open the options file for reading mode = 0 ifail = 0 CALL x04acf(ninopt,fname,mode,ifail) ! Use E04VKF to read some options from the options file ifail = 0 CALL e04vkf(ninopt,cw,iw,rw,ifail) WRITE (rec,'()') CALL x04baf(nout,rec) ! Use E04VRF to find the value of integer-valued option ! 'Elastic mode'. ifail = 0 CALL e04vrf('Elastic mode',elmode,cw,iw,rw,ifail) WRITE (rec,99999) elmode CALL x04baf(nout,rec) ! Use E04VNF to set the value of real-valued option ! 'Infinite bound size'. bndinf = 1.0E10_nag_wp ifail = 0 CALL e04vnf('Infinite bound size',bndinf,cw,iw,rw,ifail) ! Use E04VSF to find the value of real-valued option ! 'Feasibility tolerance'. ifail = 0 CALL e04vsf('Feasibility tolerance',featol,cw,iw,rw,ifail) WRITE (rec,99998) featol CALL x04baf(nout,rec) ! Use E04VLF to set the option 'Major iterations limit'. ifail = 0 CALL e04vlf('Major iterations limit 50',cw,iw,rw,ifail) ! Solve the problem. ifail = 0 CALL e04vhf(start,nf,n,nxname,nfname,objadd,objrow,prob,usrfun,iafun, & javar,a,lena,nea,igfun,jgvar,leng,neg,xlow,xupp,xnames,flow,fupp, & fnames,x,xstate,xmul,f,fstate,fmul,ns,ninf,sinf,cw,lencw,iw,leniw, & rw,lenrw,cuser,iuser,ruser,ifail) WRITE (rec,'()') CALL x04baf(nout,rec) WRITE (rec,99997) f(objrow) CALL x04baf(nout,rec) WRITE (rec,99996) x(1:n) CALL x04baf(nout,rec) 99999 FORMAT (1X,'Option ''Elastic mode'' has the value ',I3,'.') 99998 FORMAT (1X,'Option ''Feasibility tolerance'' has the value ',1P,E13.5, & '.') 99997 FORMAT (1X,'Final objective value = ',F11.1) 99996 FORMAT (1X,'Optimal X = ',7F9.2) 99995 FORMAT (1X,A) END PROGRAM e04vkfe