program main

  use bmproblem, only: pdata_type,iniprob,endprob,setmann,setsigma,evallikelihood
  use iso_c_binding, only: c_ptr,c_loc

  implicit none

  ! LOCAL SCALARS
  integer :: i,imann,isigma,nmann,nsigma,noprogr,nst
  real(kind=8) :: dtsigma,mann,mannbest,mannmin,mannmax,sigma,sigmabest,sigmamin,sigmamax,vartheta,ver,verbest
  real(8) :: verant
  type(pdata_type), target :: pdata

  ! LOCAL ARRAYS
  character(len=80) :: filename

  call iniprob(c_loc(pdata))

  write(*,*) 'Enter vartheta: '
  read(*,*) vartheta

  write(*,*) 'Enter mannmin, mannmax, nmann: '
  read(*,*) mannmin,mannmax,nmann

  write(*,*) 'Enter sigmamin, sigmamax, nsigma: '
  read(*,*) sigmamin,sigmamax,nsigma
  
  write(*,*) 'Enter dtsigma: '
  read(*,*) dtsigma
  
  open(unit=10,file='dg-fundg.out')
  
  verbest = - huge( 1.0d0 )
  verant  = - huge( 1.0d0 )


  do imann = 0,nmann
     noprogr = 0
     mann = mannmin + ( mannmax - mannmin ) * imann / max(dble( nmann ),1.0)
     call setmann(mann,c_loc(pdata))
     do isigma = 0,nsigma
        sigma = sigmamin + ( sigmamax - sigmamin ) * isigma / max(dble( nsigma ),1.0)
        call setsigma(sigma,dtsigma,c_loc(pdata))
        
        call evallikelihood(vartheta,ver,c_loc(pdata))
       ! write(*,*) ver,verbest
        if ( ver .gt. verbest ) then
           verbest = ver
           mannbest = mann
           sigmabest = sigma
           write(*, 1000) imann,mann,isigma,sigma,ver
           write(10,1000) imann,mann,isigma,sigma,ver

           write(filename,"(A9,F5.3,A4)") 'bcgmrfig-',vartheta,'.dat'
        
           open(unit=20,file=trim(filename))
           if ( pdata%sigma .eq. 0.0d0 ) then
              do i = 1,pdata%ndiv+1
                 write(20,2000) pdata%x(i),pdata%reali(i,1)
              end do
           else
              do i = 1,pdata%ndiv+1
                 write(20,2000) pdata%x(i),pdata%reali(i,1:pdata%nr)
              end do
           end if
           close(20)
!
!     HIDROGRAMA
!           
           do nst=1,pdata%nshydro
!
           write(filename,"(A11,i1,A4)") 'bcgmrhyd-st',nst,'.dat'
        
           open(unit=20,file=trim(filename))
           if ( pdata%sigma .eq. 0.0d0 ) then
              do i = 1,pdata%nthydro-1
                 write(20,2000) pdata%thydro(i),pdata%hydro(i,nst,1)
              end do
           else
              do i = 1,pdata%nthydro-1
                 write(20,2000) pdata%thydro(i),pdata%hydro(i,nst,1:pdata%nr)
              end do
           end if
           close(20)

            end do ! nst
!
        else
           write(*, 1100) imann,mann,isigma,sigma,ver
           write(10,1100) imann,mann,isigma,sigma,ver
        end if
!
!
        if(verant.gt.ver) then
           noprogr = noprogr + 1
        else 
           noprogr = 0
        end if
!
        verant=ver
!
        if ( noprogr .ge. 3) then
        write(*,*) 'Saindo pois não progrediu localmente apos 3 incrementos sequenciais'
        noprogr=0
        exit
      !   write(*,*) 'sofrencia'
        end if
!
     end do
  end do
  

  write(*, *) 'SUMMARY: vartheta = ',vartheta,'mannbest = ',mannbest, &
                  'sigmabest = ',sigmabest,'verbest = ',verbest

  close(10)
  
  open(unit=30,file='tabline.txt')
  write(30,1010) pdata%ndiv,pdata%dt,pdata%dtsigma,vartheta,mannbest,sigmabest,verbest
  close(30)

  call endprob(c_loc(pdata))
  
  stop

1000 format(2(I6,1X,1P,E16.8),E16.8,1X,'BEST IMPROVED!')
!1010 format(I3,1X,F10.6,1X,F10.6,1X,F10.6,1X,2(F10.6,1X),1P,E14.8)
1010 format(I3,1X,E10.5,1X,F10.2,1X,F10.6,1X,2(e10.4,1X),1P,E14.8)
1100 format(2(I6,1X,1P,E16.8),E16.8)
2000 format(10001(1P,E10.4,1X))
  
end program main
