!===============================================================================
!
! init_hmc.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2007-2008 Yoshifumi Nakamura
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
! Modified for PFHMC and the generalized integrator
!
! Editor: Taylor Haar
! Last modified: 16/03/2017
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine init_hmc(para)
  use typedef_hmc
  use module_field
  use module_input
  use module_action
  use module_integrator
  use module_switches
  implicit none
  type(hmc_para), intent(in)    :: para

  n_time_scale = 6

  if(para%m_scale5 == 0 .or. (para%m_scale5==1 .and. para%integrator(6)=="LPFSTS") .or. para%integrator(6)=="NON") n_time_scale = 5
  if(para%m_scale4 == 0 .or. (para%m_scale4==1 .and. para%integrator(5)=="LPFSTS") .or. para%integrator(5)=="NON") n_time_scale = 4
  if(para%m_scale3 == 0 .or. (para%m_scale3==1 .and. para%integrator(4)=="LPFSTS") .or. para%integrator(4)=="NON") n_time_scale = 3
  if(para%m_scale2 == 0 .or. (para%m_scale2==1 .and. para%integrator(3)=="LPFSTS") .or. para%integrator(3)=="NON") n_time_scale = 2
  if(para%m_scale  == 0 .or. (para%m_scale ==1 .and. para%integrator(2)=="LPFSTS") .or. para%integrator(2)=="NON") n_time_scale = 1

  ! PFHMC time-scale checks
  CHECK(input%hmc_dsfp_k11 <= n_time_scale)
  CHECK(input%hmc_dsfp_k12 <= n_time_scale)
  CHECK(input%hmc_dsfp_k13 <= n_time_scale)
  CHECK(input%hmc_dsfp_k21 <= n_time_scale)
  CHECK(input%hmc_dsfp_k22 <= n_time_scale)
  CHECK(input%hmc_dsfp_k23 <= n_time_scale)
  CHECK(input%hmc_dsfp_k31 <= n_time_scale)
  CHECK(input%hmc_dsfp_k32 <= n_time_scale)
  CHECK(input%hmc_dsfp_k33 <= n_time_scale)
  CHECK(input%hmc_dsfp_k41 <= n_time_scale)
  CHECK(input%hmc_dsfp_k42 <= n_time_scale)
  CHECK(input%hmc_dsfp_k43 <= n_time_scale)
  CHECK(input%hmc_dsfp_k51 <= n_time_scale)
  CHECK(input%hmc_dsfp_k52 <= n_time_scale)
  CHECK(input%hmc_dsfp_k53 <= n_time_scale)
  CHECK(input%hmc_dsfp_k61 <= n_time_scale)
  CHECK(input%hmc_dsfp_k62 <= n_time_scale)
  CHECK(input%hmc_dsfp_k63 <= n_time_scale)

  CHECK(input%hmc_dsfr_k1p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k1p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k1p3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2p3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3p3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4p3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5p3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6p1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6p2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6p3 <= n_time_scale)

  ! tRHMC time-scale checks
  CHECK(input%hmc_dsfr_k1t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k1t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k1t3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2t3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3t3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4t3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5t3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6t1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6t2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6t3 <= n_time_scale)

  CHECK(input%hmc_dsfr_k1 <= n_time_scale)
  CHECK(input%hmc_dsfr_k2 <= n_time_scale)
  CHECK(input%hmc_dsfr_k3 <= n_time_scale)
  CHECK(input%hmc_dsfr_k4 <= n_time_scale)
  CHECK(input%hmc_dsfr_k5 <= n_time_scale)
  CHECK(input%hmc_dsfr_k6 <= n_time_scale)
  CHECK(input%hmc_dsf_k11 <= n_time_scale)
  CHECK(input%hmc_dsf_k12 <= n_time_scale)
  CHECK(input%hmc_dsf_k13 <= n_time_scale)
  CHECK(input%hmc_dsf_k14 <= n_time_scale)
  CHECK(input%hmc_dsf_k21 <= n_time_scale)
  CHECK(input%hmc_dsf_k22 <= n_time_scale)
  CHECK(input%hmc_dsf_k23 <= n_time_scale)
  CHECK(input%hmc_dsf_k24 <= n_time_scale)
  CHECK(input%hmc_dsf_k31 <= n_time_scale)
  CHECK(input%hmc_dsf_k32 <= n_time_scale)
  CHECK(input%hmc_dsf_k33 <= n_time_scale)
  CHECK(input%hmc_dsf_k34 <= n_time_scale)
  CHECK(input%hmc_dsf_k41 <= n_time_scale)
  CHECK(input%hmc_dsf_k42 <= n_time_scale)
  CHECK(input%hmc_dsf_k43 <= n_time_scale)
  CHECK(input%hmc_dsf_k44 <= n_time_scale)
  CHECK(input%hmc_dsf_k51 <= n_time_scale)
  CHECK(input%hmc_dsf_k52 <= n_time_scale)
  CHECK(input%hmc_dsf_k53 <= n_time_scale)
  CHECK(input%hmc_dsf_k54 <= n_time_scale)
  CHECK(input%hmc_dsf_k61 <= n_time_scale)
  CHECK(input%hmc_dsf_k62 <= n_time_scale)
  CHECK(input%hmc_dsf_k63 <= n_time_scale)
  CHECK(input%hmc_dsf_k64 <= n_time_scale)
  CHECK(input%hmc_dsd     <= n_time_scale)
  CHECK(input%hmc_dsg     <= n_time_scale)
  CHECK(input%hmc_dsig    <= n_time_scale)

  if (para%csw /= 0 .and. input%hmc_dsd == 0) call die("when csw !=0, hmc_dsd must be > 0")
  if (gauge_action /= "WILSON" .and. input%hmc_dsig == 0) call die("when gauge_action is not WILSON, hmc_dsig must be > 0")

  if (.not. associated(m_scale))allocate(m_scale(n_time_scale ))
  if (.not. associated(scale  ))allocate(scale(  n_time_scale ))

  if (.not. switches%genint) then
     ! Normal integrators: Step counts are nested
     m_scale(1) = para%ntau
     if (n_time_scale > 1) m_scale(2) = para%m_scale
     if (n_time_scale > 2) m_scale(3) = para%m_scale2
     if (n_time_scale > 3) m_scale(4) = para%m_scale3
     if (n_time_scale > 4) m_scale(5) = para%m_scale4
     if (n_time_scale > 5) m_scale(6) = para%m_scale5

     scale(1) = para%ntau
     if (n_time_scale > 1) scale(2) = scale(1) * para%m_scale
     if (n_time_scale > 2) scale(3) = scale(2) * para%m_scale2
     if (n_time_scale > 3) scale(4) = scale(3) * para%m_scale3
     if (n_time_scale > 4) scale(5) = scale(4) * para%m_scale4
     if (n_time_scale > 5) scale(6) = scale(5) * para%m_scale5

  else
     ! Generalized integrators: Step counts are absolute
     scale(1) = para%ntau
     if (n_time_scale > 1) scale(2) = para%m_scale
     if (n_time_scale > 2) scale(3) = para%m_scale2
     if (n_time_scale > 3) scale(4) = para%m_scale3
     if (n_time_scale > 4) scale(5) = para%m_scale4
     if (n_time_scale > 5) scale(6) = para%m_scale5
  endif

  if (.not. associated(integrator))allocate(integrator(n_time_scale))

  integrator(1:n_time_scale) = para%integrator(1:n_time_scale)

end subroutine init_hmc

!-------------------------------------------------------------------------------
integer function num_time_scale(para)
  use typedef_hmc
  use module_input
  implicit none
  type(hmc_para), intent(in) :: para

  num_time_scale = 6
  if(para%m_scale5 == 0 .or. (para%m_scale5==1 .and. para%integrator(6)=="LPFSTS") .or. para%integrator(6)=="NON") num_time_scale = 5
  if(para%m_scale4 == 0 .or. (para%m_scale4==1 .and. para%integrator(5)=="LPFSTS") .or. para%integrator(5)=="NON") num_time_scale = 4
  if(para%m_scale3 == 0 .or. (para%m_scale3==1 .and. para%integrator(4)=="LPFSTS") .or. para%integrator(4)=="NON") num_time_scale = 3
  if(para%m_scale2 == 0 .or. (para%m_scale2==1 .and. para%integrator(3)=="LPFSTS") .or. para%integrator(3)=="NON") num_time_scale = 2
  if(para%m_scale  == 0 .or. (para%m_scale ==1 .and. para%integrator(2)=="LPFSTS") .or. para%integrator(2)=="NON") num_time_scale = 1

end function num_time_scale
!===============================================================================
