!!----------------------------------------------------------------------------
!! $Id: compute_energy_path.f90,v 1.1 1997/12/19 19:37:59 scott Exp $ 
!! subroutine compute_energy_path
!!    This subroutine will compute the energy of the entire system with a
!!    non-broken path for the potential energy
!!    Author: Scott A. Klasky
!!    Date  : 10/20/97
!!----------------------------------------------------------------------------
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

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


interface
   real*8 function compute_potential_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

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

end interface

   potential_energy =compute_potential_energy_path(max_params,params,payoff, &
                                                    eta,io)
   kinetic_energy  =compute_kinetic_energy(max_params,params,payoff,eta)

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

!!----------------------------------------------------------------------------
!! function compute_potential_energy_path
!!    This subroutine will compute the local potential energy (unbroken path) 
!!    Author: Scott A. Klasky
!!    Date  : 10/20/97
!!----------------------------------------------------------------------------
real*8 function compute_potential_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

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

   compute_potential_energy_path = 0.0d0
   do stock = 1,int(params(P_NPATH))
      do k = 1,int(params(P_NTIME))
         if(eta(k,io(k,stock))%spin == 1) then
            compute_potential_energy_path = compute_potential_energy_path + payoff(k,io(k,stock))
            exit !k
         end if
      end do
   end do

return
end function compute_potential_energy_path




