PROGRAM simann

USE simulated_anneal_sp
USE sep
use OW_type
use OW_sltpar

IMPLICIT NONE

INTEGER, PARAMETER      :: neps = 4
REAL ,ALLOCATABLE       :: lb(:), ub(:), x(:), xopt(:), c(:), vm(:)
real, allocatable	    :: data(:,:),mask(:,:),dd(:,:)
REAL                    :: t, rt, fopt
REAL                    :: t1, eps1, rt1,sla1,svm1,lb1,ub1
INTEGER                 :: ns, nt, nfcnev, ier, iseed1, iseed2, i, maxevl, iprint, nacc, nobds
LOGICAL                 :: max

 call sep_init()
 call sep_begin_prog()

 call owsltpar()
 call parameters()

 allocate(lb(ntot),ub(ntot),x(ntot),xopt(ntot),c(ntot),vm(ntot))

!  Set input parameters.
!  Set input values of the input/output parameters.
 call from_param("max",max,.false.)
 call from_param("eps",eps1,1.0e-6)
 call from_param("rt",rt1,.1)
 call from_param("sla",sla1,2.)
 call from_param("svm",svm1,2.)
 call from_param("lb",lb1,-80.)
 call from_param("ub",ub1, 80.)
 call from_param("iseed1",iseed1,1)
 call from_param("iseed2",iseed2,2)
 call from_param("ns",ns,1) !20
 call from_param("nt",nt,5)
 call from_param("maxevl",maxevl,1000000)
 call from_param("iprint",iprint,1)
 call from_param("t",t1,95.0) !5.0)
 
 lb = lb1 ! -1.01
 ub = ub1 ! 1.01
!  Set input values of the input/output parameters.
 vm = (svm1)
 c  = (sla1)
 t=(t1)
 rt=(rt1)

! Initial model
 write(0,*) ntot
 write(0,*) "Initiate"
  call sreed("dip",x,4*sog%z%n*sog%xm%n)
  allocate(data(sog%z%n,sog%xm%n))
  call sreed("in",data,4*sog%z%n*sog%xm%n)

  allocate(mask(sog%z%n,sog%xm%n))
  if (exist_file("mask")) then
     call sreed("mask",mask,4*sog%z%n*sog%xm%n)
     where(mask >= maskval)
        mask=1
     elsewhere
        mask=0.
     end where
  else
     mask=1.
  end if
  call objfcn_init(data,mask)
  deallocate(mask,data)
  call sa(ntot, x, max, rt, eps1, ns, nt, neps, maxevl, lb, ub, c, iprint, iseed1,  &
        iseed2, t, vm, xopt, fopt, nacc, nfcnev, nobds, ier)

  write(*, '(/, "  ****   RESULTS AFTER SA   ****   ")')
  write(*,1001) fopt, nfcnev, nacc, nobds, t, ier

  1001 FORMAT(/,' OPTIMAL FUNCTION VALUE: ',g20.13  &
            /,' NUMBER OF FUNCTION EVALUATIONS:     ',i10,  &
            /,' NUMBER OF ACCEPTED EVALUATIONS:     ',i10,  &
            /,' NUMBER OF OUT OF BOUND EVALUATIONS: ',i10,  &
            /,' FINAL TEMP: ', g20.13,'  IER: ', i3)

  call io(xopt)

  if (exist_file("dd")) then
     allocate(dd(sog%z%n,sog%xm%n))
     call modela(xopt,dd)
     call to_history("n1",sog%z%n,"dd");call to_history("d1",sog%z%d);call to_history("o1",sog%z%o,"dd")
     call to_history("n2",sog%xm%n,"dd");call to_history("d2",sog%xm%d,"dd");call to_history("o2",sog%xm%o,"dd")
     call to_history("n3",1,"dd");call to_history("d3",1.,"dd");call to_history("o3",1.,"dd")
     call to_history("n4",1,"dd");call to_history("d4",1.,"dd");call to_history("o4",1.,"dd")
     call to_history("label1","z","dd");call to_history("label2","x","dd")
     call to_history("label3"," ","dd");call to_history("label4"," ","dd")
     call srite("dd",dd,4*sog%z%n*sog%xm%n)
  end if

 call sep_end_prog()
 stop
 end program simann
