!!----------------------------------------------------------------------------
!! $Id: initialize_spins.f90,v 1.1 1997/12/19 19:37:59 scott Exp $ 
!! subroutine initialize_spins 
!!    This subroutine will initialize the spins 
!!    Author: Scott A. Klasky
!!    Date  : 10/20/97
!!----------------------------------------------------------------------------
subroutine initialize_spins(max_params,params,payoff,eta)
  use PATHS
  use input_params
  implicit none
  integer,intent(IN)                             :: max_params
  real*8,dimension(max_params),intent(IN)        :: params
  type (PATH),dimension(:,:),intent(OUT)         :: eta
  real*8,dimension(:,:),intent(IN)               :: payoff

  integer                                        :: init_toggle

  interface
     subroutine standard_init(max_params,params,payoff,eta)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)        :: params
       type (PATH),dimension(:,:),intent(OUT)         :: eta
       real*8,dimension(:,:),intent(IN)               :: payoff
     end subroutine standard_init

     subroutine standard_init2(max_params,params,payoff,eta)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)        :: params
       type (PATH),dimension(:,:),intent(OUT)         :: eta
       real*8,dimension(:,:),intent(IN)               :: payoff

     end subroutine standard_init2

  end interface

  init_toggle = int(params(P_INIT))


  select case(init_toggle)

  case(1)
     call standard_init(max_params,params,payoff,eta)
  case(2)
     call standard_init2(max_params,params,payoff,eta)

  case default 
     write(*,*)'Invalid value for initialization type'
     stop
  end select

  return
end subroutine initialize_spins
!!----------------------------------------------------------------------------
!! subroutine standard_init
!!    This subroutine will initialize the spins using method 1
!!    Author: Scott A. Klasky
!!    Date  : 10/20/97
!!----------------------------------------------------------------------------
subroutine standard_init(max_params,params,payoff,eta)
  use PATHS
  use input_params
  implicit none
  integer,intent(IN)                             :: max_params
  real*8,dimension(max_params),intent(IN)        :: params
  type (PATH),dimension(:,:),intent(OUT)         :: eta
  real*8,dimension(:,:),intent(IN)               :: payoff

  real*8                                         :: strike
  integer                                        :: i,j
  real*8,dimension(:,:),allocatable :: r

  allocate (r(int(params(P_NTIME)),int(params(P_NPATH)) ) )
	call random_number(r)
  eta(:,:)%spin = int(r+0.5)
  strike = params(P_STRIKE)
  do j = 1, int(params(P_NPATH))
!     eta(:,j)%spin = 0.0d0
     if (payoff(int(params(P_NTIME)),j) > (0.0d0) )  then
        eta(int(params(P_NTIME)),j)%spin = 1.0d0
     else
        eta(int(params(P_NTIME)),j)%spin = 0.0d0
     end if
  end do !j
  eta(int(params(P_NTIME))-1,1)%spin = 1.0d0
  eta(int(params(P_NTIME))-1,int(params(P_NPATH)))%spin = 0.0d0

  return
end subroutine standard_init

!!----------------------------------------------------------------------------
!! subroutine standard_init2
!!    This subroutine will initialize the spins using method 1
!!    Author: Scott A. Klasky
!!    Date  : 10/20/97
!!----------------------------------------------------------------------------
subroutine standard_init2(max_params,params,payoff,eta)
  use PATHS
  use input_params
  implicit none
  integer,intent(IN)                             :: max_params
  real*8,dimension(max_params),intent(IN)        :: params
  type (PATH),dimension(:,:),intent(OUT)         :: eta
  real*8,dimension(:,:),intent(IN)               :: payoff

  real*8                                         :: strike
  integer                                        :: i,j

  strike = params(P_STRIKE)

  eta(:,:)%spin = 0.0d0
  do j = 1 , int(params(P_NPATH))
     do i = int(params(P_NTIME)),1,-1
        if (payoff(i,j) > (0.0d0) )  then
           eta(i:1,j)%spin = 1.0d0
           exit !out of the time look, i.e. we found the "boundary" value
        end if
     end do !i
  end do !j

  return
end subroutine standard_init2
