!!----------------------------------------------------------------------------
!! Modules for the american code 
!! Written by: Scott A. Klasky
!! $Id: modules.f90,v 1.23 1997/11/12 20:24:11 scott Exp $
!!----------------------------------------------------------------------------

module PATHS
  type PATH
     real*8                       :: spin 
     real*8,dimension(:),pointer  :: nns
     real*8,dimension(:),pointer  :: nng
     integer,dimension(:),pointer :: nnt
     integer,dimension(:),pointer :: nni
     integer,dimension(:),pointer :: nnj
     integer                      :: nnn
     ! nearest neighbors in stock, nearest neighbors in time, nearest neighbors in r
  end type PATH
end module PATHS

module MOD_INT
  interface

     real*8 function compute_lpotential_energy(t,p,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                                        :: t,p
     end function compute_lpotential_energy

     real*8 function compute_lkinetic_energy(t,p,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                                        :: t,p
     end function compute_lkinetic_energy

     real*8 function compute_lpotential_energy_path(t,p,max_params,params, &
          payoff,eta,io)
       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                                        :: t,p
       integer,dimension(:,:),intent(IN)              :: io
     end function compute_lpotential_energy_path

     real*8 function average_payoff(max_params,params,payoff,eta,sortorder)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                      ::max_params
       real*8,dimension(max_params),intent(IN) ::params
       real*8,dimension(:,:),intent(IN)        ::payoff
       type (PATH),dimension(:,:),intent(IN)   :: eta  
       integer,dimension(:,:),intent(IN)        :: sortorder
     end function average_payoff

     real*8 function sorted_average_payoff(max_params,params,payoff,eta, &
          sortorder)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                      ::max_params
       real*8,dimension(max_params),intent(IN) ::params
       real*8,dimension(:,:),intent(IN)        ::payoff
       type (PATH),dimension(:,:),intent(IN)   :: eta  
       integer,dimension(:,:),intent(IN)        :: sortorder
     end function sorted_average_payoff


     subroutine read_data(fname,max_params,params,stockprice)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                       :: max_params
       real*8,dimension(max_params),intent(OUT) :: params
       real*8,dimension(:,:),intent(OUT)        :: stockprice
       character*(*),intent(IN)                 :: fname
     end subroutine read_data

     subroutine read_params(max_params,params)
       use PATHS
       use input_params
       implicit none
       integer                                  :: max_params
       real*8,dimension(max_params)             :: params
     end subroutine read_params

     subroutine calculate_payoff(max_params,params,stockprice,payoff)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                       :: max_params
       real*8,dimension(max_params),intent(IN)  :: params
       real*8,dimension(:,:),intent(IN)         :: stockprice
       real*8,dimension(:,:),intent(OUT)        :: payoff
     end subroutine calculate_payoff

     subroutine sort_stock(max_params,params,payoff,sort_order,sortpayoff)
       use PATHS
       use input_params
       implicit none
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)       :: params
       real*8,dimension(:,:),intent(IN)               :: payoff
       real*8,dimension(:,:),intent(OUT)              :: sortpayoff 
       integer,dimension(:,:),intent(OUT)             :: sort_order
     end subroutine sort_stock

     subroutine find_nearest_neighbor_list(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
       real*8,dimension(:,:),intent(IN)               :: payoff
       type (PATH),dimension(:,:),intent(INOUT)       :: eta
     end subroutine find_nearest_neighbor_list

     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
     end subroutine initialize_spins

     real*8 function compute_energy(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 function compute_energy

     real*8 function compute_local_energy(t,p,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                                        :: t,p
     end function compute_local_energy

     real*8 function compute_energy_path(max_params,params,payoff,eta,io)
       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,dimension(:,:),intent(IN)               :: io
     end function compute_energy_path

     real*8 function compute_local_energy_path(t,p,max_params,params,       &
          payoff,eta,io)
       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,dimension(:,:),intent(IN)               :: io
       integer                                        :: t,p
     end function compute_local_energy_path


     subroutine dump_data(name,time,max_params,params,gf)
       use PATHS
       use input_params
       implicit none
       character*(*),intent(IN)                       :: name
       real*8,intent(IN)                              :: time
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)        :: params
       type (PATH),dimension(:,:),intent(IN)          :: gf
     end subroutine dump_data

     subroutine dump_dat1d(name,it,time,max_params,params,x2d,gf)
       use PATHS
       use input_params
       implicit none
       character*(*),intent(IN)                       :: name
       real*8,intent(IN)                              :: time
       integer,intent(IN)                             :: max_params
       integer                                        :: it
       real*8,dimension(max_params),intent(IN)        :: params
       real*8,dimension(:,:),intent(IN)               :: x2d
       type (PATH),dimension(:,:),intent(IN)          :: gf
     end subroutine dump_dat1d

     subroutine dump_datar(name,time,max_params,params,gf)
       use PATHS
       use input_params
       implicit none
       character*(*),intent(IN)                       :: name
       real*8,intent(IN)                              :: time
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)        :: params
       real*8,dimension(:,:),intent(IN)          :: gf
     end subroutine dump_datar

     subroutine dump_data1d(name,time,max_params,params,gf)
       use input_params
       implicit none
       character*(*),intent(IN)                       :: name
       real*8,intent(IN)                              :: time
       integer,intent(IN)                             :: max_params
       real*8,dimension(max_params),intent(IN)        :: params
       real*8,dimension(:,:),intent(IN)               :: gf
     end subroutine dump_data1d

  end interface
end module MOD_INT
