C ******************************************************************************
C       P R O G R A M    E X D R I V E R   -   J A N U A R Y   1,  1 9 8 9
C ******************************************************************************
C  PURPOSE: To run subroutine DsAprx and its associated routines to solve (OPT).
C           This is an example driver.  The user may rewrite the driver to
C           fit the specific example
C  
C  RESTRICTIONS: 
C           Arguments to DsAprx must be defined and initialized and the 
C           initial guess at F(i) must be given.  Dimensions must be observed.
C *****************************************************************************

      PROGRAM  ExDriver

C  Local variables an Lower-level arguments
C  Note:  MxSp and MxDim must equal MxSp and MxDim in DsAprx.
C  As of January 1989, MxSp=1024 and MxDim=3.
      INTEGER           nData, Info, NSp, NDim, ItsUsed,
     &                  MaxIts, nIn, nOut, Io, Jo, Ko, MxSp, MxDim
      PARAMETER        (MxSp=1024, MxDim=3)
      DOUBLE COMPLEX    F(mxsp,mxdim), E(mxsp),a(10)
      DOUBLE PRECISION  GDiff, Tol, Value, Term, Eps ,Wno2


      CHARACTER*25      File, File1, File2
      EXTERNAL          MakeGk, MakeAk, MakeHk

      Common / param / a
      Common / e / E



C  Assign Unit numbers and file names
C  ==================================
C  (Note:  this could be done interactively if different file names are wanted) 
      nIn   = 5
      nOut  = 6
      Io    = 9
      Jo    = 10
      Ko    = 11
      File  = 'Primary.dat'
      File1 = 'Summary1.dat'
      File2 = 'Summary2.dat'
      nparam = 12

C  Required information for DsApprx subroutine arguments
C  =====================================================
      WRITE(nout,*)   'Enter the maximum number of iterations (integer)'
      READ(nin,*)      MaxIts 
      WRITE(nout,*)   'Enter level of diagnostics (integer 1 to 4)'
      READ(nin,*)      Info
      include 'defs.h'
      WRITE(nout,*)   'Enter error Tolerance ',
     &                '(double precision)'
      READ(nin,*)      Tol
      WRITE(nout,*)   'Enter initial error tolerance for subproblem',
     &                '(double precision)'
      READ(nin,*)      Eps

C  Initial Guess at F(i)
C  =====================
C  Use file created by Makedata program to read initial guess in proper format
      WRITE(nout,2500)  MxSp
2500  FORMAT (' Enter unit number containing initial guess at F(i)'/
     &        ' (note that the number of sample points can not ', 
     &        'exceed ',i6,')') 
      READ(nin,*)  nData
      IF ((nData.LT.0)   .OR. (nData.GT.99) .OR. (nData.EQ.nIn) .OR.
     &   (nData.EQ.nOut) .OR. (nData.EQ.Io) .OR. (nData.EQ.Jo)  .OR.
     &   (nData.EQ.Ko))  THEN 
         WRITE(nout,2600)  nIn, nOut, Io, Jo, Ko
2600     FORMAT('Unit number must be between 0 and 99 and must not'
     &          ' include the following numbers'/' ',5i4) 
         STOP
      ENDIF

      OPEN (Unit=nData)
      READ(ndata,*)   NSp
      DO 10  j = 1,NDim
         DO 15  i = 1,NSp
            READ(ndata,*)  F(i,j)
15       CONTINUE
10    CONTINUE
      CLOSE (Unit=Ndata)

cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      call getdata(nsp,nin,nout,nerror)
      if(nerror.eq.1)then
         write(nout,*)'error detected when reading file uncerdata'
         go to 999
      endif
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

C  Set up variables specific to case (will be used in MakeGk, MakeAk, MakeHk)
C  =================================
      Term = (8.0 * DATAN(1.0d0)) / DBLE(NSp)
         DO 35  i = 1,NSp
            E(i) = CDEXP (DCMPLX (0.0d0, Term*DBLE(i-1)) )
35       CONTINUE

      open(unit=nparam, file = 'param.dat',status='old')
      do 101 i=1,10
        read(nparam,*)a(i)
101   continue 
      close(unit = nparam)
C  Compute the solution
C  ====================
      CALL  DsAprx (NSp, NDim, MxSp, MxDim, F, MaxIts, Tol, Info,
     &              nIn, nOut, Io, Jo, Ko, File, File1, File2,
     &              MakeGk, MakeAk, MakeHk,
     &              ItsUsed, Value, GDiff,Wno2, Eps,nData)



C  Display some of the computed variables
C  ======================================
      If((NDim.gt.1).and.(Wno2.ge.0.0d0))then
         WRITE(nout,1000)  ItsUsed, Value, GDiff,Wno2
      else
         WRITE(nout,1001)  ItsUsed, Value, GDiff
      endif
1000  FORMAT (1h0,'Number of iterations:',T31,i3/
     &        'Opt. value sup{ gamma(f)}:',T31,g14.6/
     &        'Diagnostic (Sup-Inf) ',T31,g14.6/ 
     &        'Diagnostic (WnoV) ',T31,g14.6//) 
1001  FORMAT (1h0,'Number of Iterations :',T31,i3/
     &        'Opt. value sup{ gamma(f)}:',T31,g14.6/
     &        'Diagnostic (Sup-Inf) ',T31,g14.6//) 




999   continue
      STOP
      END

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      subroutine getdata(nsp,nin,nout,error)
c
c  Orlando M. Dec. 89.
c
c  reads from unit = nin the exponent n (n=large makes the
c  function gamma look like supremum), and the
c  uncertainty parameter delta in (0,1).
c
c  also reads the file uncerdata.
c  the file uncerdata must contain an integer nsp2 (gridsize),
c  positive functions w1 and w2 with complex format,
c  and the complex function po (nominal plant)
c
c
      double precision w(1024),w1(1024),delta,n
      double complex p0(1024),zeta
      integer error,nin,nout
c
      common/data1/n,delta,w,w1,p0
c
      error = 0
      write(nout,*)'input exponent n: '
      read(nin,*)n
      write(nout,*)'input uncertainty parameter delta: '
      read(nin,*)delta
      open(unit=40,file='w',status='old')
      read(40,*)nsp2
      write(nout,*)'nsp2 =',nsp2
      if(nsp2.ne.nsp)then
          write(nout,*)'no match in gridsize of w'
          error = 1
          return
      endif
      do 10 i=1,nsp
         read(40,*)zeta
         w1(i) = dreal(zeta)
         if( (dreal(zeta).le.0.) .or.
     &     (dabs(dimag(zeta)).gt.(1.d-12)))error = 1
10    continue
      if(error.eq.1)then
        write(nout,*)'weight w is not positive '
        return
      endif
      do 20 i=1,nsp
         read(40,*)zeta
         w(i) = w1(i)/(w1(i)+dreal(zeta))
         if( (dreal(zeta).le.0.) .or.
     &     (dabs(dimag(zeta)).gt.(1.d-12)))error = 1
20    continue
      if(error.eq.1)then
        write(nout,*)'weight w is not positive '
        return
      endif
      do 30 i=1,nsp
         read(40,*)p0(i)
30    continue
      close(unit=40)
      return
      end
