!!----------------------------------------------------------------------------
!! $Id: compute_local_energy_path.f90,v 1.5 1997/11/12 02:03:48 svmcguir Exp $ 
!! subroutine compute_local_energy
!!    This subroutine will compute the local energy 
!!    Author: Scott A. Klasky
!!    Date  : 11/02/97
!!----------------------------------------------------------------------------
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                                        :: t,p

  real*8                                         :: potential_energy 
  real*8                                         :: kinetic_energy 
  integer,dimension(:,:),intent(IN)              :: io

  interface
     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 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

  end interface

  potential_energy=compute_lpotential_energy_path(t,p,max_params,params,  &
       payoff,eta,io)
  kinetic_energy  =compute_lkinetic_energy(t,p,max_params,params,payoff,eta)

  compute_local_energy_path = -params(P_C1)*potential_energy              &
       -params(P_C2)*kinetic_energy
  return
end function compute_local_energy_path

!!----------------------------------------------------------------------------
!! function compute_plotential_energy_path
!!    This subroutine will compute the local potential energy on a unbroken path
!!    Author: Scott A. Klasky
!!    Date  : 11/02/97
!!----------------------------------------------------------------------------
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

  integer                                        :: j,k, stock
  real*8                                         :: sum , prod

  compute_lpotential_energy_path = 0.0d0

  do j = 1 , int(params(P_NPATH))
     if (io(t,j) == p) exit !this is the j that we want
  end do !j

  do k =1,int(params(P_NTIME))
     if(eta(k,io(k,j))%spin == 1) then
        compute_lpotential_energy_path = compute_lpotential_energy_path +  &
             payoff(k,io(k,j))
        exit !k
     end if
  end do

  return
end function compute_lpotential_energy_path
