Example description
!   E04WEF Example Program Text
!   Mark 27.1 Release. NAG Copyright 2020.

    Module e04wefe_mod

!     E04WEF 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 ..
      Integer, Parameter, Public       :: leniw = 600, lenrw = 600, nin = 5,   &
                                          ninopt = 7, nout = 6
    Contains
      Subroutine objfun(mode,n,x,objf,grad,nstate,iuser,ruser)
!       Routine to evaluate objective function and its 1st derivatives.

!       .. Scalar Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: objf
        Integer, Intent (Inout)        :: mode
        Integer, Intent (In)           :: n, nstate
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Inout) :: grad(n), ruser(*)
        Real (Kind=nag_wp), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
!       .. Executable Statements ..
        If (mode==0 .Or. mode==2) Then
          objf = x(1)*x(4)*(x(1)+x(2)+x(3)) + x(3)
        End If

        If (mode==1 .Or. mode==2) Then
          grad(1) = x(4)*(2.0E0_nag_wp*x(1)+x(2)+x(3))
          grad(2) = x(1)*x(4)
          grad(3) = x(1)*x(4) + 1.0E0_nag_wp
          grad(4) = x(1)*(x(1)+x(2)+x(3))
        End If

        Return

      End Subroutine objfun
      Subroutine confun(mode,ncnln,n,ldcj,needc,x,ccon,cjac,nstate,iuser,      &
        ruser)
!       Routine to evaluate the nonlinear constraints and their 1st
!       derivatives.

!       .. Scalar Arguments ..
        Integer, Intent (In)           :: ldcj, n, ncnln, nstate
        Integer, Intent (Inout)        :: mode
!       .. Array Arguments ..
        Real (Kind=nag_wp), Intent (Out) :: ccon(max(1,ncnln))
        Real (Kind=nag_wp), Intent (Inout) :: cjac(ldcj,n), ruser(*)
        Real (Kind=nag_wp), Intent (In) :: x(n)
        Integer, Intent (Inout)        :: iuser(*)
        Integer, Intent (In)           :: needc(ncnln)
!       .. Intrinsic Procedures ..
        Intrinsic                      :: max
!       .. 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) = 0.0E0_nag_wp
        End If

        If (needc(1)>0) Then

          If (mode==0 .Or. mode==2) Then
            ccon(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) = 2.0E0_nag_wp*x(1)
            cjac(1,2) = 2.0E0_nag_wp*x(2)
            cjac(1,3) = 2.0E0_nag_wp*x(3)
            cjac(1,4) = 2.0E0_nag_wp*x(4)
          End If

        End If

        If (needc(2)>0) Then

          If (mode==0 .Or. mode==2) Then
            ccon(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 e04wefe_mod
    Program e04wefe

!     E04WEF Example Main Program

!     .. Use Statements ..
      Use e04wefe_mod, Only: confun, leniw, lenrw, nin, ninopt, nout, objfun
      Use nag_library, Only: e04wcf, e04wdf, e04wef, e04wff, e04wgf, e04whf,   &
                             e04wkf, e04wlf, nag_wp, x04acf, x04baf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Character (*), Parameter         :: fname = 'e04wefe.opt'
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: bndinf, featol, objf
      Integer                          :: elmode, i, ifail, lda, ldcj, ldh,    &
                                          majits, mode, n, nclin, ncnln, sda,  &
                                          sdcjac
      Character (80)                   :: rec
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:,:), bl(:), bu(:), ccon(:),       &
                                          cjac(:,:), clamda(:), grad(:),       &
                                          h(:,:), x(:)
      Real (Kind=nag_wp)               :: ruser(1), rw(lenrw)
      Integer, Allocatable             :: istate(:)
      Integer                          :: iuser(1), iw(leniw)
!     .. Intrinsic Procedures ..
      Intrinsic                        :: max
!     .. Executable Statements ..
      Write (rec,99995) 'E04WEF Example Program Results'
      Call x04baf(nout,rec)

!     This program demonstrates the use of routines to set and
!     get values of optional parameters associated with E04WDF.

!     Skip heading in data file
      Read (nin,*)

      Read (nin,*) n, nclin, 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

      ldh = n
      Allocate (istate(n+nclin+ncnln),a(lda,sda),bl(n+nclin+ncnln),            &
        bu(n+nclin+ncnln),ccon(max(1,ncnln)),cjac(ldcj,sdcjac),clamda(n+nclin+ &
        ncnln),grad(n),h(ldh,n),x(n))

      If (nclin>0) Then
        Read (nin,*)(a(i,1:sda),i=1,nclin)
      End If

      Read (nin,*) bl(1:(n+nclin+ncnln))
      Read (nin,*) bu(1:(n+nclin+ncnln))
      Read (nin,*) x(1:n)

!     Call E04WCF to initialize E04WDF.

      ifail = 0
      Call e04wcf(iw,leniw,rw,lenrw,ifail)

!     By default E04WDF does not print monitoring
!     information. Set the print file unit or the summary
!     file unit to get information.

      ifail = 0
      Call e04wgf('Print file',nout,iw,rw,ifail)

!     Open the options file for reading

      mode = 0

      ifail = 0
      Call x04acf(ninopt,fname,mode,ifail)

!     Use E04WEF to read some options from the options file

      ifail = 0
      Call e04wef(ninopt,iw,rw,ifail)

      Write (rec,'()')
      Call x04baf(nout,rec)

!     Use E04WKF to find the value of integer-valued option
!     'Elastic mode'.

      ifail = 0
      Call e04wkf('Elastic mode',elmode,iw,rw,ifail)

      Write (rec,99999) elmode
      Call x04baf(nout,rec)

!     Use E04WHF to set the value of real-valued option
!     'Infinite bound size'.

      bndinf = 1.0E10_nag_wp

      ifail = 0
      Call e04whf('Infinite bound size',bndinf,iw,rw,ifail)

!     Use E04WLF to find the value of real-valued option
!     'Feasibility tolerance'.

      ifail = 0
      Call e04wlf('Feasibility tolerance',featol,iw,rw,ifail)

      Write (rec,99998) featol
      Call x04baf(nout,rec)

!     Use E04WFF to set the option 'Major iterations limit'.

      ifail = 0
      Call e04wff('Major iterations limit 50',iw,rw,ifail)

!     Solve the problem.

      ifail = 0
      Call e04wdf(n,nclin,ncnln,lda,ldcj,ldh,a,bl,bu,confun,objfun,majits,     &
        istate,ccon,cjac,clamda,objf,grad,h,x,iw,leniw,rw,lenrw,iuser,ruser,   &
        ifail)

      Write (rec,'()')
      Call x04baf(nout,rec)
      Write (rec,99997) objf
      Call x04baf(nout,rec)
      Write (rec,99996)(x(i),i=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.3)
99996 Format (1X,'Optimal X = ',7F9.2)
99995 Format (1X,A)
    End Program e04wefe