function tetrahedron_spinhall(F, E1, E2, t, hw, Ef, type, tet_cutoff, avoid_deg)
!=============================================================================
! Calculates contribution from a single tetrahedron, for the Kubo formula. !
! "integral d3k (f_{nk} - f_{mk}) * (...)"
! Ref: Minsu Ghim and Cheol-Hwan Park, PRB 106 075126 !
!=============================================================================
use w90_constants, only: dp
implicit none
real(kind=dp) :: tetrahedron_spinhall
real(kind=dp), dimension(4), intent(in) :: F, E1, E2
real(kind=dp), dimension(3, 3), intent(in) :: t
real(kind=dp), intent(in) :: hw, Ef
integer, intent(in) :: type
real(kind=dp), intent(in) :: tet_cutoff
real(kind=dp), intent(in) :: avoid_deg
!intermediate variables, s:sorted
real(kind=dp) :: t_s(3, 3), t_small(3, 3), x(3), y
real(kind=dp), dimension(4) :: D, occ1, occ2, F_s, E1_s, E2_s, &
F_small, D_small
integer :: i
logical :: flag1, flag2
!fnk-fmk = 0 then quickly returns zero
occ1 = 0.0_dp; occ2 = 0.0_dp; flag1 = .true.; flag2 = .true.
do i = 1, 4
if (E1(i) < Ef) occ1(i) = 1.0_dp
if (E2(i) < Ef) occ2(i) = 1.0_dp
if (occ1(i) /= 1.0 .or. occ2(i) /= 1.0) flag1 = flag1 .and. .false.
if (occ1(i) /= 0.0 .or. occ2(i) /= 0.0) flag2 = flag2 .and. .false.
end do
if (flag1 .or. flag2) then
tetrahedron_spinhall = 0.0_dp
else
tetrahedron_spinhall = tetrahedron_fermidirac(F, E1, E2, t, hw, Ef, type, tet_cutoff, avoid_deg) &
- tetrahedron_fermidirac(F, E2, E1, t, hw, Ef, type, tet_cutoff, avoid_deg)
end if
return
end function tetrahedron_spinhall