!!----------------------------------------------------------------------------
!! Modules for the american code 
!! Written by: Scott A. Klasky
!! $Id: modules.f90,v 1.3 1997/11/11 13:15:25 scott Exp scott $
!!----------------------------------------------------------------------------

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

     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,sort_orderi, &
                           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
       integer,dimension(:,:),intent(OUT)             :: sort_orderi
     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

     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

		 integer function grid_solve(npath,otl,params,sortpayoff,eta, &
                                 sort_order,sort_orderi)
        use PATHS
        use input_params
        implicit none
      
        integer,dimension(:,:),intent(OUT)             :: sort_order
        integer,dimension(:,:),intent(OUT)             :: sort_orderi
        integer,intent(IN)                                :: npath, otl
        real*8,dimension(:),intent(IN)                    :: params
        real*8,dimension(:,:),intent(IN)                  :: sortpayoff
        type (PATH),dimension(:,:),intent(INOUT)          :: eta
     end function grid_solve 


  end interface
end module MOD_INT
