10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-18 12:03:57 +01:00
quantum_package/plugins/MRCC_Utils/mrcc_utils.irp.f

1360 lines
36 KiB
Fortran
Raw Normal View History

2016-06-20 10:52:54 +02:00
use bitmasks
BEGIN_PROVIDER [ integer, mrmode ]
mrmode = 0
END_PROVIDER
2016-07-06 15:43:21 +02:00
BEGIN_PROVIDER [ double precision, lambda_mrcc, (N_states, N_det_non_ref) ]
2016-04-01 23:33:58 +02:00
&BEGIN_PROVIDER [ integer, lambda_mrcc_pt2, (0:psi_det_size) ]
2016-09-17 23:33:06 +02:00
&BEGIN_PROVIDER [ integer, lambda_mrcc_kept, (0:psi_det_size) ]
implicit none
BEGIN_DOC
! cm/<Psi_0|H|D_m> or perturbative 1/Delta_E(m)
END_DOC
2016-03-31 16:48:23 +02:00
integer :: i,k
2016-03-30 21:33:38 +02:00
double precision :: ihpsi_current(N_states)
2016-04-01 23:33:58 +02:00
integer :: i_pert_count
double precision :: hii, lambda_pert
integer :: N_lambda_mrcc_pt2, N_lambda_mrcc_pt3
2016-07-06 15:43:21 +02:00
i_pert_count = 0
lambda_mrcc = 0.d0
N_lambda_mrcc_pt2 = 0
N_lambda_mrcc_pt3 = 0
lambda_mrcc_pt2(0) = 0
2016-09-17 23:33:06 +02:00
lambda_mrcc_kept(0) = 0
2016-07-06 15:43:21 +02:00
do i=1,N_det_non_ref
call i_h_psi(psi_non_ref(1,1,i), psi_ref, psi_ref_coef, N_int, N_det_ref,&
size(psi_ref_coef,1), N_states,ihpsi_current)
call i_H_j(psi_non_ref(1,1,i),psi_non_ref(1,1,i),N_int,hii)
do k=1,N_states
if (ihpsi_current(k) == 0.d0) then
ihpsi_current(k) = 1.d-32
endif
lambda_mrcc(k,i) = min(-1.d-32,psi_non_ref_coef(i,k)/ihpsi_current(k) )
lambda_pert = 1.d0 / (psi_ref_energy_diagonalized(k)-hii)
if (lambda_pert / lambda_mrcc(k,i) < 0.5d0) then
2016-09-17 23:33:06 +02:00
! Ignore lamdba
2016-07-06 15:43:21 +02:00
i_pert_count += 1
lambda_mrcc(k,i) = 0.d0
if (lambda_mrcc_pt2(N_lambda_mrcc_pt2) /= i) then
N_lambda_mrcc_pt2 += 1
lambda_mrcc_pt2(N_lambda_mrcc_pt2) = i
2016-03-31 16:48:23 +02:00
endif
2016-07-06 15:43:21 +02:00
else
2016-09-17 23:33:06 +02:00
! Keep lamdba
if (lambda_mrcc_kept(N_lambda_mrcc_pt3) /= i) then
2016-07-06 15:43:21 +02:00
N_lambda_mrcc_pt3 += 1
2016-09-17 23:33:06 +02:00
lambda_mrcc_kept(N_lambda_mrcc_pt3) = i
2016-03-31 16:48:23 +02:00
endif
2016-07-06 15:43:21 +02:00
endif
enddo
2016-07-06 15:43:21 +02:00
enddo
lambda_mrcc_pt2(0) = N_lambda_mrcc_pt2
2016-09-17 23:33:06 +02:00
lambda_mrcc_kept(0) = N_lambda_mrcc_pt3
2016-03-30 21:33:38 +02:00
print*,'N_det_non_ref = ',N_det_non_ref
print*,'psi_coef_ref_ratio = ',psi_ref_coef(2,1)/psi_ref_coef(1,1)
2016-03-31 16:48:23 +02:00
print*,'lambda max = ',maxval(dabs(lambda_mrcc))
print*,'Number of ignored determinants = ',i_pert_count
2015-09-08 15:29:05 +02:00
2015-04-01 13:23:02 +02:00
END_PROVIDER
2016-03-31 16:48:23 +02:00
2015-06-29 10:35:29 +02:00
2016-03-29 23:18:26 +02:00
BEGIN_PROVIDER [ double precision, hij_mrcc, (N_det_non_ref,N_det_ref) ]
implicit none
BEGIN_DOC
! < ref | H | Non-ref > matrix
END_DOC
integer :: i_I, k_sd
do i_I=1,N_det_ref
do k_sd=1,N_det_non_ref
call i_h_j(psi_ref(1,1,i_I),psi_non_ref(1,1,k_sd),N_int,hij_mrcc(k_sd,i_I))
enddo
enddo
END_PROVIDER
BEGIN_PROVIDER [ double precision, delta_ij, (N_states,N_det_non_ref,N_det_ref) ]
&BEGIN_PROVIDER [ double precision, delta_ii, (N_states,N_det_ref) ]
2015-04-03 14:26:14 +02:00
implicit none
BEGIN_DOC
! Dressing matrix in N_det basis
END_DOC
integer :: i,j,m
delta_ij = 0.d0
delta_ii = 0.d0
2016-03-29 23:18:26 +02:00
call H_apply_mrcc(delta_ij,delta_ii,N_states,N_det_non_ref,N_det_ref)
2016-04-26 17:27:21 +02:00
2015-04-03 14:26:14 +02:00
END_PROVIDER
2016-05-20 09:44:22 +02:00
2015-04-03 14:26:14 +02:00
BEGIN_PROVIDER [ double precision, h_matrix_dressed, (N_det,N_det,N_states) ]
2015-04-03 14:26:14 +02:00
implicit none
BEGIN_DOC
! Dressed H with Delta_ij
END_DOC
integer :: i, j,istate,ii,jj
do istate = 1,N_states
do j=1,N_det
do i=1,N_det
h_matrix_dressed(i,j,istate) = h_matrix_all_dets(i,j)
enddo
2015-04-03 14:26:14 +02:00
enddo
2015-07-13 18:00:38 +02:00
do ii = 1, N_det_ref
i =idx_ref(ii)
2016-03-29 23:18:26 +02:00
h_matrix_dressed(i,i,istate) += delta_ii(istate,ii)
2015-07-13 18:00:38 +02:00
do jj = 1, N_det_non_ref
2015-07-20 13:35:38 +02:00
j =idx_non_ref(jj)
2016-03-29 23:18:26 +02:00
h_matrix_dressed(i,j,istate) += delta_ij(istate,jj,ii)
h_matrix_dressed(j,i,istate) += delta_ij(istate,jj,ii)
enddo
enddo
2015-04-03 14:26:14 +02:00
enddo
END_PROVIDER
BEGIN_PROVIDER [ double precision, CI_electronic_energy_dressed, (N_states_diag) ]
&BEGIN_PROVIDER [ double precision, CI_eigenvectors_dressed, (N_det,N_states_diag) ]
&BEGIN_PROVIDER [ double precision, CI_eigenvectors_s2_dressed, (N_states_diag) ]
2015-07-03 14:42:55 +02:00
implicit none
BEGIN_DOC
2016-08-04 15:17:01 +02:00
! Eigenvectors/values of the dressed CI matrix
2015-07-03 14:42:55 +02:00
END_DOC
2016-08-04 15:17:01 +02:00
double precision :: ovrlp,u_dot_v
integer :: i_good_state
integer, allocatable :: index_good_state_array(:)
logical, allocatable :: good_state_array(:)
double precision, allocatable :: s2_values_tmp(:)
integer :: i_other_state
double precision, allocatable :: eigenvectors(:,:), eigenvalues(:)
integer :: i_state
2016-09-17 01:54:44 +02:00
double precision :: e_0
2016-08-04 15:17:01 +02:00
integer :: i,j,k
double precision, allocatable :: s2_eigvalues(:)
double precision, allocatable :: e_array(:)
integer, allocatable :: iorder(:)
2016-08-05 23:42:54 +02:00
integer :: mrcc_state
2015-07-03 14:42:55 +02:00
2016-08-05 23:42:54 +02:00
mrcc_state = N_states
2016-09-27 10:10:39 +02:00
do j=1,min(N_states,N_det)
2015-07-03 14:42:55 +02:00
do i=1,N_det
CI_eigenvectors_dressed(i,j) = psi_coef(i,j)
enddo
enddo
if (diag_algorithm == "Davidson") then
2016-09-30 22:29:22 +02:00
! call davidson_diag_mrcc(psi_det,CI_eigenvectors_dressed,CI_electronic_energy_dressed,&
! size(CI_eigenvectors_dressed,1),N_det,N_states,N_states_diag,N_int,output_determinants,mrcc_state)
call davidson_diag_mrcc_HS2(psi_det,CI_eigenvectors_dressed,&
size(CI_eigenvectors_dressed,1), &
CI_electronic_energy_dressed,N_det,N_states,N_states_diag,N_int, &
output_determinants,mrcc_state)
2016-09-23 10:01:26 +02:00
call u_0_S2_u_0(CI_eigenvectors_s2_dressed,CI_eigenvectors_dressed,N_det,psi_det,N_int,&
2016-09-16 23:45:03 +02:00
N_states_diag,size(CI_eigenvectors_dressed,1))
2016-08-04 15:17:01 +02:00
2015-07-03 14:42:55 +02:00
else if (diag_algorithm == "Lapack") then
allocate (eigenvectors(size(H_matrix_dressed,1),N_det))
allocate (eigenvalues(N_det))
call lapack_diag(eigenvalues,eigenvectors, &
H_matrix_dressed,size(H_matrix_dressed,1),N_det)
CI_electronic_energy_dressed(:) = 0.d0
if (s2_eig) then
2016-09-16 23:45:03 +02:00
i_state = 0
allocate (s2_eigvalues(N_det))
allocate(index_good_state_array(N_det),good_state_array(N_det))
good_state_array = .False.
2016-09-23 10:01:26 +02:00
call u_0_S2_u_0(s2_eigvalues,eigenvectors,N_det,psi_det,N_int,&
2016-09-16 23:45:03 +02:00
N_det,size(eigenvectors,1))
2015-07-03 14:42:55 +02:00
do j=1,N_det
2016-09-16 23:45:03 +02:00
! Select at least n_states states with S^2 values closed to "expected_s2"
2016-09-17 01:54:44 +02:00
if(dabs(s2_eigvalues(j)-expected_s2).le.0.5d0)then
2015-07-03 14:42:55 +02:00
i_state += 1
2016-09-16 23:45:03 +02:00
index_good_state_array(i_state) = j
good_state_array(j) = .True.
2015-07-03 14:42:55 +02:00
endif
2016-09-16 23:45:03 +02:00
if (i_state==N_states) then
2015-07-03 14:42:55 +02:00
exit
endif
enddo
2016-09-16 23:45:03 +02:00
if (i_state /= 0) then
! Fill the first "i_state" states that have a correct S^2 value
do j = 1, i_state
do i=1,N_det
CI_eigenvectors_dressed(i,j) = eigenvectors(i,index_good_state_array(j))
enddo
CI_electronic_energy_dressed(j) = eigenvalues(index_good_state_array(j))
CI_eigenvectors_s2_dressed(j) = s2_eigvalues(index_good_state_array(j))
enddo
i_other_state = 0
do j = 1, N_det
if(good_state_array(j))cycle
i_other_state +=1
if(i_state+i_other_state.gt.n_states_diag)then
exit
endif
do i=1,N_det
CI_eigenvectors_dressed(i,i_state+i_other_state) = eigenvectors(i,j)
enddo
CI_electronic_energy_dressed(i_state+i_other_state) = eigenvalues(j)
CI_eigenvectors_s2_dressed(i_state+i_other_state) = s2_eigvalues(i_state+i_other_state)
enddo
else
print*,''
print*,'!!!!!!!! WARNING !!!!!!!!!'
print*,' Within the ',N_det,'determinants selected'
print*,' and the ',N_states_diag,'states requested'
print*,' We did not find any state with S^2 values close to ',expected_s2
print*,' We will then set the first N_states eigenvectors of the H matrix'
print*,' as the CI_eigenvectors_dressed'
print*,' You should consider more states and maybe ask for s2_eig to be .True. or just enlarge the CI space'
print*,''
do j=1,min(N_states_diag,N_det)
do i=1,N_det
CI_eigenvectors_dressed(i,j) = eigenvectors(i,j)
enddo
CI_electronic_energy_dressed(j) = eigenvalues(j)
CI_eigenvectors_s2_dressed(j) = s2_eigvalues(j)
enddo
endif
deallocate(index_good_state_array,good_state_array)
deallocate(s2_eigvalues)
2015-07-03 14:42:55 +02:00
else
2016-09-23 10:01:26 +02:00
call u_0_S2_u_0(CI_eigenvectors_s2_dressed,eigenvectors,N_det,psi_det,N_int,&
2016-09-16 23:45:03 +02:00
min(N_det,N_states_diag),size(eigenvectors,1))
! Select the "N_states_diag" states of lowest energy
do j=1,min(N_det,N_states_diag)
2015-07-03 14:42:55 +02:00
do i=1,N_det
2016-09-16 23:45:03 +02:00
CI_eigenvectors_dressed(i,j) = eigenvectors(i,j)
2015-07-03 14:42:55 +02:00
enddo
2016-09-16 23:45:03 +02:00
CI_electronic_energy_dressed(j) = eigenvalues(j)
2015-07-03 14:42:55 +02:00
enddo
endif
deallocate(eigenvectors,eigenvalues)
endif
2016-09-16 23:45:03 +02:00
2015-04-03 14:26:14 +02:00
END_PROVIDER
BEGIN_PROVIDER [ double precision, CI_energy_dressed, (N_states_diag) ]
implicit none
BEGIN_DOC
! N_states lowest eigenvalues of the dressed CI matrix
END_DOC
integer :: j
character*(8) :: st
2015-04-20 10:12:08 +02:00
call write_time(output_determinants)
do j=1,min(N_det,N_states)
2016-09-15 16:11:34 +02:00
write(st,'(I4)') j
2015-04-03 14:26:14 +02:00
CI_energy_dressed(j) = CI_electronic_energy_dressed(j) + nuclear_repulsion
2016-09-15 22:01:46 +02:00
call write_double(output_determinants,CI_energy_dressed(j),'Energy of state '//trim(st))
call write_double(output_determinants,CI_eigenvectors_s2_dressed(j),'S^2 of state '//trim(st))
2015-04-03 14:26:14 +02:00
enddo
END_PROVIDER
2016-03-30 21:33:38 +02:00
subroutine diagonalize_CI_dressed(lambda)
2015-04-09 21:46:37 +02:00
implicit none
BEGIN_DOC
! Replace the coefficients of the CI states by the coefficients of the
! eigenstates of the CI matrix
END_DOC
2016-03-30 21:33:38 +02:00
double precision, intent(in) :: lambda
2015-04-09 21:46:37 +02:00
integer :: i,j
2016-09-27 10:10:39 +02:00
do j=1,N_states
2015-04-09 21:46:37 +02:00
do i=1,N_det
2016-03-30 21:33:38 +02:00
psi_coef(i,j) = lambda * CI_eigenvectors_dressed(i,j) + (1.d0 - lambda) * psi_coef(i,j)
2015-04-09 21:46:37 +02:00
enddo
2016-03-30 21:33:38 +02:00
call normalize(psi_coef(1,j), N_det)
2015-04-09 21:46:37 +02:00
enddo
SOFT_TOUCH psi_coef
end
2016-05-20 09:44:22 +02:00
logical function is_generable(det1, det2, Nint)
use bitmasks
implicit none
integer, intent(in) :: Nint
integer(bit_kind) :: det1(Nint, 2), det2(Nint, 2)
2016-05-20 11:27:39 +02:00
integer :: degree, f, exc(0:2, 2, 2), t
integer*2 :: h1, h2, p1, p2, s1, s2
2016-05-20 09:44:22 +02:00
integer, external :: searchExc
logical, external :: excEq
double precision :: phase
2016-08-02 13:45:17 +02:00
integer*2 :: tmp_array(4)
2016-05-20 09:44:22 +02:00
is_generable = .false.
call get_excitation(det1, det2, exc, degree, phase, Nint)
if(degree == -1) return
if(degree == 0) then
is_generable = .true.
return
end if
if(degree > 2) stop "?22??"
2016-05-20 11:27:39 +02:00
call decode_exc_int2(exc,degree,h1,p1,h2,p2,s1,s2)
2016-05-20 09:44:22 +02:00
if(degree == 1) then
h2 = h1
p2 = p1
s2 = s1
h1 = 0
p1 = 0
s1 = 0
end if
2016-05-20 11:27:39 +02:00
if(h1 + (s1-1)*mo_tot_num < h2 + (s2-1)*mo_tot_num) then
2016-08-02 13:45:17 +02:00
tmp_array = (/s1, h1, s2, h2/)
2016-05-20 09:44:22 +02:00
else
2016-08-02 13:45:17 +02:00
tmp_array = (/s2, h2, s1, h1/)
2016-05-20 09:44:22 +02:00
end if
2016-08-02 13:45:17 +02:00
f = searchExc(hh_exists(1,1), tmp_array, hh_shortcut(0))
2016-05-20 11:27:39 +02:00
if(p1 + (s1-1)*mo_tot_num < p2 + (s2-1)*mo_tot_num) then
2016-08-02 13:45:17 +02:00
tmp_array = (/s1, p1, s2, p2/)
2016-05-20 09:44:22 +02:00
else
2016-08-02 13:45:17 +02:00
tmp_array = (/s2, p2, s1, p1/)
2016-05-20 09:44:22 +02:00
end if
2016-09-15 22:01:46 +02:00
if (f /= -1) then
f = searchExc(pp_exists(1,hh_shortcut(f)), tmp_array, hh_shortcut(f+1)-hh_shortcut(f))
endif
2016-08-02 13:45:17 +02:00
is_generable = (f /= -1)
2016-05-20 09:44:22 +02:00
end function
integer function searchDet(dets, det, n, Nint)
implicit none
use bitmasks
integer(bit_kind),intent(in) :: dets(Nint,2,n), det(Nint,2)
integer, intent(in) :: nint, n
integer :: l, h, c
integer, external :: detCmp
logical, external :: detEq
l = 1
h = n
do while(.true.)
searchDet = (l+h)/2
2016-05-26 13:52:48 +02:00
c = detCmp(dets(1,1,searchDet), det(1,1), Nint)
if(c == 0) then
return
else if(c == 1) then
2016-05-20 09:44:22 +02:00
h = searchDet-1
else
l = searchDet+1
end if
if(l > h) then
searchDet = -1
return
end if
end do
end function
2016-06-14 14:37:46 +02:00
integer function unsortedSearchDet(dets, det, n, Nint)
implicit none
use bitmasks
integer(bit_kind),intent(in) :: dets(Nint,2,n), det(Nint,2)
integer, intent(in) :: nint, n
integer :: l, h, c
integer, external :: detCmp
logical, external :: detEq
do l=1, n
if(detEq(det, dets(1,1,l), N_int)) then
unsortedSearchDet = l
return
end if
end do
unsortedSearchDet = -1
end function
2016-05-20 09:44:22 +02:00
integer function searchExc(excs, exc, n)
implicit none
use bitmasks
integer, intent(in) :: n
2016-05-20 11:27:39 +02:00
integer*2,intent(in) :: excs(4,n), exc(4)
2016-05-20 09:44:22 +02:00
integer :: l, h, c
integer, external :: excCmp
logical, external :: excEq
l = 1
h = n
do
searchExc = (l+h)/2
2016-05-26 13:52:48 +02:00
c = excCmp(excs(1,searchExc), exc(1))
2016-05-20 09:44:22 +02:00
if(c == 0) return
if(c == 1) then
h = searchExc-1
else
l = searchExc+1
end if
if(l > h) then
searchExc = -1
return
end if
end do
end function
subroutine sort_det(key, idx, N_key, Nint)
implicit none
integer, intent(in) :: Nint, N_key
integer(8),intent(inout) :: key(Nint,2,N_key)
2016-05-26 13:52:48 +02:00
integer,intent(inout) :: idx(N_key)
2016-05-20 09:44:22 +02:00
integer(8) :: tmp(Nint, 2)
integer :: tmpidx,i,ni
do i=1,N_key
idx(i) = i
end do
do i=N_key/2,1,-1
call tamiser(key, idx, i, N_key, Nint, N_key)
end do
do i=N_key,2,-1
do ni=1,Nint
tmp(ni,1) = key(ni,1,i)
tmp(ni,2) = key(ni,2,i)
key(ni,1,i) = key(ni,1,1)
key(ni,2,i) = key(ni,2,1)
key(ni,1,1) = tmp(ni,1)
key(ni,2,1) = tmp(ni,2)
enddo
tmpidx = idx(i)
idx(i) = idx(1)
idx(1) = tmpidx
call tamiser(key, idx, 1, i-1, Nint, N_key)
end do
end subroutine
subroutine sort_exc(key, N_key)
implicit none
integer, intent(in) :: N_key
2016-05-20 11:27:39 +02:00
integer*2,intent(inout) :: key(4,N_key)
integer*2 :: tmp(4)
2016-05-20 09:44:22 +02:00
integer :: i,ni
do i=N_key/2,1,-1
call tamise_exc(key, i, N_key, N_key)
end do
do i=N_key,2,-1
do ni=1,4
tmp(ni) = key(ni,i)
key(ni,i) = key(ni,1)
key(ni,1) = tmp(ni)
enddo
call tamise_exc(key, 1, i-1, N_key)
end do
end subroutine
logical function exc_inf(exc1, exc2)
implicit none
2016-05-20 11:27:39 +02:00
integer*2,intent(in) :: exc1(4), exc2(4)
2016-05-20 09:44:22 +02:00
integer :: i
exc_inf = .false.
do i=1,4
if(exc1(i) < exc2(i)) then
exc_inf = .true.
return
else if(exc1(i) > exc2(i)) then
return
end if
end do
end function
subroutine tamise_exc(key, no, n, N_key)
use bitmasks
implicit none
BEGIN_DOC
! Uncodumented : TODO
END_DOC
integer,intent(in) :: no, n, N_key
2016-05-20 11:27:39 +02:00
integer*2,intent(inout) :: key(4, N_key)
integer :: k,j
integer*2 :: tmp(4)
logical :: exc_inf
integer :: ni
2016-05-20 09:44:22 +02:00
k = no
j = 2*k
do while(j <= n)
if(j < n) then
if (exc_inf(key(1,j), key(1,j+1))) then
j = j+1
endif
endif
if(exc_inf(key(1,k), key(1,j))) then
do ni=1,4
tmp(ni) = key(ni,k)
key(ni,k) = key(ni,j)
key(ni,j) = tmp(ni)
enddo
k = j
j = k+k
else
return
endif
enddo
end subroutine
subroutine dec_exc(exc, h1, h2, p1, p2)
implicit none
integer :: exc(0:2,2,2), s1, s2, degree
2016-05-20 11:27:39 +02:00
integer*2, intent(out) :: h1, h2, p1, p2
2016-05-20 09:44:22 +02:00
degree = exc(0,1,1) + exc(0,1,2)
h1 = 0
h2 = 0
p1 = 0
p2 = 0
if(degree == 0) return
2016-05-20 11:27:39 +02:00
call decode_exc_int2(exc, degree, h1, p1, h2, p2, s1, s2)
2016-05-20 09:44:22 +02:00
h1 += mo_tot_num * (s1-1)
p1 += mo_tot_num * (s1-1)
if(degree == 2) then
h2 += mo_tot_num * (s2-1)
p2 += mo_tot_num * (s2-1)
if(h1 > h2) then
s1 = h1
h1 = h2
h2 = s1
end if
if(p1 > p2) then
s1 = p1
p1 = p2
p2 = s1
end if
else
h2 = h1
p2 = p1
p1 = 0
h1 = 0
end if
end subroutine
2016-05-26 13:52:48 +02:00
BEGIN_PROVIDER [ integer, N_hh_exists ]
&BEGIN_PROVIDER [ integer, N_pp_exists ]
&BEGIN_PROVIDER [ integer, N_ex_exists ]
implicit none
integer :: exc(0:2, 2, 2), degree, n, on, s, l, i
integer*2 :: h1, h2, p1, p2
double precision :: phase
logical,allocatable :: hh(:,:) , pp(:,:)
allocate(hh(0:mo_tot_num*2, 0:mo_tot_num*2))
allocate(pp(0:mo_tot_num*2, 0:mo_tot_num*2))
hh = .false.
pp = .false.
N_hh_exists = 0
N_pp_exists = 0
N_ex_exists = 0
n = 0
2016-10-06 00:36:05 +02:00
!TODO Openmp
2016-05-26 13:52:48 +02:00
do i=1, N_det_ref
do l=1, N_det_non_ref
call get_excitation(psi_ref(1,1,i), psi_non_ref(1,1,l), exc, degree, phase, N_int)
if(degree == -1) cycle
call dec_exc(exc, h1, h2, p1, p2)
N_ex_exists += 1
if(.not. hh(h1,h2)) N_hh_exists = N_hh_exists + 1
if(.not. pp(p1,p2)) N_pp_exists = N_pp_exists + 1
hh(h1,h2) = .true.
pp(p1,p2) = .true.
end do
end do
N_pp_exists = min(N_ex_exists, N_pp_exists * N_hh_exists)
END_PROVIDER
2016-06-20 10:52:54 +02:00
BEGIN_PROVIDER [ integer(bit_kind), psi_non_ref_sorted, (N_int, 2, N_det_non_ref) ]
&BEGIN_PROVIDER [ integer, psi_non_ref_sorted_idx, (N_det_non_ref) ]
implicit none
psi_non_ref_sorted = psi_non_ref
call sort_det(psi_non_ref_sorted, psi_non_ref_sorted_idx, N_det_non_ref, N_int)
END_PROVIDER
2016-08-02 13:45:17 +02:00
BEGIN_PROVIDER [ double precision, dIj_unique, (hh_shortcut(hh_shortcut(0)+1)-1, N_states) ]
&BEGIN_PROVIDER [ double precision, rho_mrcc, (N_det_non_ref, N_states) ]
implicit none
logical :: ok
integer :: i, j, k, s, II, pp, ppp, hh, ind, wk, nex, a_col, at_row
integer, external :: searchDet, unsortedSearchDet
integer(bit_kind) :: myDet(N_int, 2), myMask(N_int, 2)
integer :: N, INFO, AtA_size, r1, r2
double precision , allocatable :: AtB(:), AtA_val(:), A_val(:,:), x(:), x_new(:), A_val_mwen(:)
double precision :: t, norm, cx, res
integer, allocatable :: A_ind(:,:), lref(:), AtA_ind(:), A_ind_mwen(:), col_shortcut(:), N_col(:)
double precision :: phase
integer, allocatable :: pathTo(:), active_hh_idx(:), active_pp_idx(:)
logical, allocatable :: active(:)
double precision, allocatable :: rho_mrcc_init(:,:)
integer :: nactive
nex = hh_shortcut(hh_shortcut(0)+1)-1
print *, "TI", nex, N_det_non_ref
allocate(pathTo(N_det_non_ref), active(nex))
allocate(active_pp_idx(nex), active_hh_idx(nex))
allocate(rho_mrcc_init(N_det_non_ref, N_states))
pathTo = 0
active = .false.
nactive = 0
do hh = 1, hh_shortcut(0)
do pp = hh_shortcut(hh), hh_shortcut(hh+1)-1
do II = 1, N_det_ref
call apply_hole_local(psi_ref(1,1,II), hh_exists(1, hh), myMask, ok, N_int)
if(.not. ok) cycle
call apply_particle_local(myMask, pp_exists(1, pp), myDet, ok, N_int)
if(.not. ok) cycle
ind = searchDet(psi_non_ref_sorted(1,1,1), myDet(1,1), N_det_non_ref, N_int)
if(ind == -1) cycle
ind = psi_non_ref_sorted_idx(ind)
if(pathTo(ind) == 0) then
pathTo(ind) = pp
else
active(pp) = .true.
active(pathTo(ind)) = .true.
end if
end do
end do
end do
do hh = 1, hh_shortcut(0)
do pp = hh_shortcut(hh), hh_shortcut(hh+1)-1
if(active(pp)) then
nactive = nactive + 1
active_hh_idx(nactive) = hh
active_pp_idx(nactive) = pp
end if
end do
end do
print *, nactive, "inact/", size(active)
allocate(A_ind(0:N_det_ref+1, nactive), A_val(N_det_ref+1, nactive))
allocate(AtA_ind(N_det_ref * nactive), AtA_val(N_det_ref * nactive))
allocate(x(nex), AtB(nex))
allocate(N_col(nactive), col_shortcut(nactive))
allocate(x_new(nex))
2016-10-18 23:07:03 +02:00
do s=1, N_states
A_val = 0d0
A_ind = 0
AtA_ind = 0
AtB = 0d0
AtA_val = 0d0
x = 0d0
N_col = 0
col_shortcut = 0
!$OMP PARALLEL default(none) shared(psi_non_ref, hh_exists, pp_exists, N_int, A_val, A_ind)&
!$OMP shared(s, hh_shortcut, psi_ref_coef, N_det_non_ref, psi_non_ref_sorted, psi_non_ref_sorted_idx, psi_ref, N_det_ref)&
2016-10-18 23:07:03 +02:00
!$OMP shared(active, active_hh_idx, active_pp_idx, nactive) &
!$OMP private(lref, pp, II, ok, myMask, myDet, ind, phase, wk, ppp, hh)
2016-10-18 23:07:03 +02:00
allocate(lref(N_det_non_ref))
!$OMP DO schedule(static,10)
do ppp=1,nactive
pp = active_pp_idx(ppp)
hh = active_hh_idx(ppp)
lref = 0
do II = 1, N_det_ref
call apply_hole_local(psi_ref(1,1,II), hh_exists(1, hh), myMask, ok, N_int)
if(.not. ok) cycle
call apply_particle_local(myMask, pp_exists(1, pp), myDet, ok, N_int)
if(.not. ok) cycle
ind = searchDet(psi_non_ref_sorted(1,1,1), myDet(1,1), N_det_non_ref, N_int)
if(ind /= -1) then
call get_phase(myDet(1,1), psi_ref(1,1,II), phase, N_int)
if (phase > 0.d0) then
lref(psi_non_ref_sorted_idx(ind)) = II
else
lref(psi_non_ref_sorted_idx(ind)) = -II
endif
end if
end do
wk = 0
do i=1, N_det_non_ref
if(lref(i) > 0) then
wk += 1
A_val(wk, ppp) = psi_ref_coef(lref(i), s)
A_ind(wk, ppp) = i
else if(lref(i) < 0) then
wk += 1
A_val(wk, ppp) = -psi_ref_coef(-lref(i), s)
A_ind(wk, ppp) = i
end if
end do
2016-10-18 23:07:03 +02:00
A_ind(0,ppp) = wk
end do
!$OMP END DO
deallocate(lref)
2016-10-18 23:07:03 +02:00
!$OMP END PARALLEL
print *, 'Done building A_val, A_ind'
AtA_size = 0
col_shortcut = 0
N_col = 0
2016-10-18 23:07:03 +02:00
integer :: a_coll, at_roww
!$OMP PARALLEL default(none) shared(k, psi_non_ref_coef, A_ind, A_val, x, N_det_ref, nex, N_det_non_ref)&
!$OMP private(at_row, a_col, t, i, j, r1, r2, wk, A_ind_mwen, A_val_mwen, a_coll, at_roww)&
!$OMP shared(col_shortcut, N_col, AtB, AtA_size, AtA_val, AtA_ind, s, nactive, active_pp_idx)
allocate(A_val_mwen(nex), A_ind_mwen(nex))
2016-10-18 23:07:03 +02:00
!$OMP DO schedule(dynamic, 100)
do at_roww = 1, nactive ! nex
at_row = active_pp_idx(at_roww)
wk = 0
if(mod(at_roww, 100) == 0) print *, "AtA", at_row, "/", nex
do i=1,A_ind(0,at_roww)
j = active_pp_idx(i)
AtB(at_row) = AtB(at_row) + psi_non_ref_coef(A_ind(i, at_roww), s) * A_val(i, at_roww)
end do
2016-10-18 23:07:03 +02:00
do a_coll = 1, nactive
a_col = active_pp_idx(a_coll)
t = 0d0
r1 = 1
r2 = 1
do while ((A_ind(r1, at_roww) /= 0).and.(A_ind(r2, a_coll) /= 0))
if(A_ind(r1, at_roww) > A_ind(r2, a_coll)) then
r2 = r2+1
else if(A_ind(r1, at_roww) < A_ind(r2, a_coll)) then
r1 = r1+1
else
t = t - A_val(r1, at_roww) * A_val(r2, a_coll)
r1 = r1+1
r2 = r2+1
end if
end do
if(a_col == at_row) then
t = t + 1.d0
end if
if(t /= 0.d0) then
wk += 1
A_ind_mwen(wk) = a_col
A_val_mwen(wk) = t
end if
end do
if(wk /= 0) then
!$OMP CRITICAL
col_shortcut(at_roww) = AtA_size+1
N_col(at_roww) = wk
if (AtA_size+wk > size(AtA_ind,1)) then
2016-10-18 23:07:03 +02:00
print *, AtA_size+wk , size(AtA_ind,1)
stop 'too small'
endif
do i=1,wk
2016-10-18 23:07:03 +02:00
AtA_ind(AtA_size+i) = A_ind_mwen(i)
AtA_val(AtA_size+i) = A_val_mwen(i)
enddo
AtA_size += wk
!$OMP END CRITICAL
end if
end do
!$OMP END DO NOWAIT
deallocate (A_ind_mwen, A_val_mwen)
!$OMP END PARALLEL
print *, "ATA SIZE", ata_size
x = 0d0
do a_coll = 1, nactive
a_col = active_pp_idx(a_coll)
X(a_col) = AtB(a_col)
end do
rho_mrcc_init = 0d0
allocate(lref(N_det_ref))
2016-10-18 23:07:03 +02:00
!$OMP PARALLEL DO default(shared) schedule(static, 1) &
!$OMP private(lref, hh, pp, II, myMask, myDet, ok, ind, phase)
do hh = 1, hh_shortcut(0)
2016-10-18 23:07:03 +02:00
do pp = hh_shortcut(hh), hh_shortcut(hh+1)-1
if(active(pp)) cycle
lref = 0
do II=1,N_det_ref
call apply_hole_local(psi_ref(1,1,II), hh_exists(1, hh), myMask, ok, N_int)
if(.not. ok) cycle
call apply_particle_local(myMask, pp_exists(1, pp), myDet, ok, N_int)
if(.not. ok) cycle
ind = searchDet(psi_non_ref_sorted(1,1,1), myDet(1,1), N_det_non_ref, N_int)
if(ind == -1) cycle
ind = psi_non_ref_sorted_idx(ind)
call get_phase(myDet(1,1), psi_ref(1,1,II), phase, N_int)
X(pp) += psi_ref_coef(II,s)**2
AtB(pp) += psi_non_ref_coef(ind, s) * psi_ref_coef(II, s) * phase
lref(II) = ind
if(phase < 0d0) lref(II) = -ind
end do
X(pp) = AtB(pp) / X(pp)
do II=1,N_det_ref
if(lref(II) > 0) then
rho_mrcc_init(lref(II),s) = psi_ref_coef(II,s) * X(pp)
else if(lref(II) < 0) then
rho_mrcc_init(-lref(II),s) = -psi_ref_coef(II,s) * X(pp)
end if
end do
end do
end do
!$OMP END PARALLEL DO
x_new = x
2016-10-18 23:07:03 +02:00
double precision :: factor, resold
factor = 1.d0
resold = huge(1.d0)
do k=0,100000
!$OMP PARALLEL default(shared) private(cx, i, j, a_col, a_coll)
!$OMP DO
do i=1,N_det_non_ref
rho_mrcc(i,s) = rho_mrcc_init(i,s) ! 0d0
enddo
!$OMP END DO
!$OMP DO
do a_coll = 1, nactive !: nex
a_col = active_pp_idx(a_coll)
cx = 0d0
do i=col_shortcut(a_coll), col_shortcut(a_coll) + N_col(a_coll) - 1
cx = cx + x(AtA_ind(i)) * AtA_val(i)
end do
x_new(a_col) = AtB(a_col) + cx * factor
end do
!$OMP END DO
!$OMP END PARALLEL
res = 0.d0
2016-10-18 23:07:03 +02:00
if (res < resold) then
2016-10-18 23:07:03 +02:00
do a_coll=1,nactive ! nex
a_col = active_pp_idx(a_coll)
do j=1,N_det_non_ref
i = A_ind(j,a_coll)
if (i==0) exit
rho_mrcc(i,s) = rho_mrcc(i,s) + A_val(j,a_coll) * X_new(a_col)
enddo
res = res + (X_new(a_col) - X(a_col))*(X_new(a_col) - X(a_col))
X(a_col) = X_new(a_col)
2016-10-18 23:07:03 +02:00
end do
factor = 1.d0
else
factor = -factor * 0.5d0
endif
resold = res
2016-10-18 23:07:03 +02:00
if(mod(k, 100) == 0) then
print *, "res ", k, res
end if
2016-10-18 23:07:03 +02:00
if(res < 1d-9) exit
end do
norm = 0.d0
2016-10-18 23:07:03 +02:00
do i=1,N_det_non_ref
norm = norm + rho_mrcc(i,s)*rho_mrcc(i,s)
enddo
! Norm now contains the norm of A.X
do i=1,N_det_ref
norm = norm + psi_ref_coef(i,s)*psi_ref_coef(i,s)
enddo
! Norm now contains the norm of Psi + A.X
print *, k, "res : ", res, "norm : ", sqrt(norm)
!---------------
! double precision :: e_0, overlap
! double precision, allocatable :: u_0(:)
! integer(bit_kind), allocatable :: keys_tmp(:,:,:)
! allocate (u_0(N_det), keys_tmp(N_int,2,N_det) )
! k=0
! overlap = 0.d0
! do i=1,N_det_ref
! k = k+1
! u_0(k) = psi_ref_coef(i,1)
! keys_tmp(:,:,k) = psi_ref(:,:,i)
! overlap += u_0(k)*psi_ref_coef(i,1)
! enddo
! norm = 0.d0
! do i=1,N_det_non_ref
! k = k+1
! u_0(k) = psi_non_ref_coef(i,1)
! keys_tmp(:,:,k) = psi_non_ref(:,:,i)
! overlap += u_0(k)*psi_non_ref_coef(i,1)
! enddo
!
! call u_0_H_u_0(e_0,u_0,N_det,keys_tmp,N_int,1,N_det)
! print *, 'Energy of |Psi_CASSD> : ', e_0 + nuclear_repulsion, overlap
!
! k=0
! overlap = 0.d0
! do i=1,N_det_ref
! k = k+1
! u_0(k) = psi_ref_coef(i,1)
! keys_tmp(:,:,k) = psi_ref(:,:,i)
! overlap += u_0(k)*psi_ref_coef(i,1)
! enddo
! norm = 0.d0
! do i=1,N_det_non_ref
! k = k+1
! ! f is such that f.\tilde{c_i} = c_i
! f = psi_non_ref_coef(i,1) / rho_mrcc(i,1)
!
! ! Avoid numerical instabilities
! f = min(f,2.d0)
! f = max(f,-2.d0)
!
! f = 1.d0
!
! u_0(k) = rho_mrcc(i,1)*f
! keys_tmp(:,:,k) = psi_non_ref(:,:,i)
! norm += u_0(k)**2
! overlap += u_0(k)*psi_non_ref_coef(i,1)
! enddo
!
! call u_0_H_u_0(e_0,u_0,N_det,keys_tmp,N_int,1,N_det)
! print *, 'Energy of |(1+T)Psi_0> : ', e_0 + nuclear_repulsion, overlap
!
! f = 1.d0/norm
! norm = 1.d0
! do i=1,N_det_ref
! norm = norm - psi_ref_coef(i,s)*psi_ref_coef(i,s)
! enddo
! f = dsqrt(f*norm)
! overlap = norm
! do i=1,N_det_non_ref
! u_0(k) = rho_mrcc(i,1)*f
! overlap += u_0(k)*psi_non_ref_coef(i,1)
! enddo
!
! call u_0_H_u_0(e_0,u_0,N_det,keys_tmp,N_int,1,N_det)
! print *, 'Energy of |(1+T)Psi_0> (normalized) : ', e_0 + nuclear_repulsion, overlap
!
! k=0
! overlap = 0.d0
! do i=1,N_det_ref
! k = k+1
! u_0(k) = psi_ref_coef(i,1)
! keys_tmp(:,:,k) = psi_ref(:,:,i)
! overlap += u_0(k)*psi_ref_coef(i,1)
! enddo
! norm = 0.d0
! do i=1,N_det_non_ref
! k = k+1
! ! f is such that f.\tilde{c_i} = c_i
! f = psi_non_ref_coef(i,1) / rho_mrcc(i,1)
!
! ! Avoid numerical instabilities
! f = min(f,2.d0)
! f = max(f,-2.d0)
!
! u_0(k) = rho_mrcc(i,1)*f
! keys_tmp(:,:,k) = psi_non_ref(:,:,i)
! norm += u_0(k)**2
! overlap += u_0(k)*psi_non_ref_coef(i,1)
! enddo
!
! call u_0_H_u_0(e_0,u_0,N_det,keys_tmp,N_int,1,N_det)
! print *, 'Energy of |(1+T)Psi_0> (mu_i): ', e_0 + nuclear_repulsion, overlap
!
! f = 1.d0/norm
! norm = 1.d0
! do i=1,N_det_ref
! norm = norm - psi_ref_coef(i,s)*psi_ref_coef(i,s)
! enddo
! overlap = norm
! f = dsqrt(f*norm)
! do i=1,N_det_non_ref
! u_0(k) = rho_mrcc(i,1)*f
! overlap += u_0(k)*psi_non_ref_coef(i,1)
! enddo
!
! call u_0_H_u_0(e_0,u_0,N_det,keys_tmp,N_int,1,N_det)
! print *, 'Energy of |(1+T)Psi_0> (normalized mu_i) : ', e_0 + nuclear_repulsion, overlap
!
! deallocate(u_0, keys_tmp)
!
!---------------
2016-08-02 13:45:17 +02:00
2016-08-03 00:18:53 +02:00
norm = 0.d0
2016-10-18 23:07:03 +02:00
double precision :: f
2016-08-02 13:45:17 +02:00
do i=1,N_det_non_ref
2016-08-05 17:53:20 +02:00
if (rho_mrcc(i,s) == 0.d0) then
rho_mrcc(i,s) = 1.d-32
endif
2016-10-11 22:46:12 +02:00
! f is such that f.\tilde{c_i} = c_i
2016-08-03 00:18:53 +02:00
f = psi_non_ref_coef(i,s) / rho_mrcc(i,s)
2016-08-03 00:18:53 +02:00
! Avoid numerical instabilities
2016-09-13 09:37:58 +02:00
f = min(f,2.d0)
f = max(f,-2.d0)
2016-08-05 17:53:20 +02:00
norm = norm + f*f *rho_mrcc(i,s)*rho_mrcc(i,s)
2016-08-03 00:18:53 +02:00
rho_mrcc(i,s) = f
2016-08-02 13:45:17 +02:00
enddo
2016-10-11 22:46:12 +02:00
! norm now contains the norm of |T.Psi_0>
! rho_mrcc now contains the f factors
2016-08-03 00:18:53 +02:00
f = 1.d0/norm
2016-10-11 22:46:12 +02:00
! f now contains 1/ <T.Psi_0|T.Psi_0>
2016-10-11 22:46:12 +02:00
norm = 1.d0
do i=1,N_det_ref
norm = norm - psi_ref_coef(i,s)*psi_ref_coef(i,s)
2016-08-03 00:18:53 +02:00
enddo
2016-10-11 22:46:12 +02:00
! norm now contains <Psi_SD|Psi_SD>
2016-08-05 17:53:20 +02:00
f = dsqrt(f*norm)
2016-10-11 22:46:12 +02:00
! f normalises T.Psi_0 such that (1+T)|Psi> is normalized
2016-10-11 22:46:12 +02:00
norm = norm*f
print *, 'norm of |T Psi_0> = ', dsqrt(norm)
2016-10-11 22:46:12 +02:00
do i=1,N_det_ref
norm = norm + psi_ref_coef(i,s)*psi_ref_coef(i,s)
enddo
2016-08-03 00:18:53 +02:00
do i=1,N_det_non_ref
rho_mrcc(i,s) = rho_mrcc(i,s) * f
enddo
2016-10-11 22:46:12 +02:00
! rho_mrcc now contains the product of the scaling factors and the
! normalization constant
2016-10-18 23:07:03 +02:00
dIj_unique(:size(X), s) = X(:)
end do
2016-10-18 23:07:03 +02:00
2016-06-14 14:37:46 +02:00
END_PROVIDER
2016-07-06 15:43:21 +02:00
BEGIN_PROVIDER [ double precision, dij, (N_det_ref, N_det_non_ref, N_states) ]
integer :: s,i,j
2016-07-13 09:06:06 +02:00
double precision, external :: get_dij_index
2016-07-06 15:43:21 +02:00
print *, "computing amplitudes..."
2016-10-11 22:46:12 +02:00
!$OMP PARALLEL DEFAULT(shared) PRIVATE(s,i,j)
2016-07-06 15:43:21 +02:00
do s=1, N_states
2016-10-11 22:46:12 +02:00
!$OMP DO
2016-07-06 15:43:21 +02:00
do i=1, N_det_non_ref
do j=1, N_det_ref
dij(j, i, s) = get_dij_index(j, i, s, N_int)
end do
end do
2016-10-11 22:46:12 +02:00
!$OMP END DO
2016-07-06 15:43:21 +02:00
end do
2016-10-11 22:46:12 +02:00
!$OMP END PARALLEL
2016-07-06 15:43:21 +02:00
print *, "done computing amplitudes"
END_PROVIDER
double precision function get_dij_index(II, i, s, Nint)
integer, intent(in) :: II, i, s, Nint
2016-07-06 11:28:52 +02:00
double precision, external :: get_dij
2016-08-05 17:53:20 +02:00
double precision :: HIi, phase
2016-07-06 11:28:52 +02:00
2016-07-06 15:43:21 +02:00
if(lambda_type == 0) then
2016-08-05 17:53:20 +02:00
call get_phase(psi_ref(1,1,II), psi_non_ref(1,1,i), phase, N_int)
2016-10-11 22:46:12 +02:00
get_dij_index = get_dij(psi_ref(1,1,II), psi_non_ref(1,1,i), s, Nint) * phase
get_dij_index = get_dij_index * rho_mrcc(i,s)
2016-07-06 11:28:52 +02:00
else
2016-07-06 15:43:21 +02:00
call i_h_j(psi_ref(1,1,II), psi_non_ref(1,1,i), Nint, HIi)
get_dij_index = HIi * lambda_mrcc(s, i)
2016-07-06 11:28:52 +02:00
end if
end function
2016-07-06 15:43:21 +02:00
double precision function get_dij(det1, det2, s, Nint)
2016-06-14 14:37:46 +02:00
use bitmasks
implicit none
2016-07-06 15:43:21 +02:00
integer, intent(in) :: s, Nint
2016-06-14 14:37:46 +02:00
integer(bit_kind) :: det1(Nint, 2), det2(Nint, 2)
integer :: degree, f, exc(0:2, 2, 2), t
integer*2 :: h1, h2, p1, p2, s1, s2
integer, external :: searchExc
logical, external :: excEq
double precision :: phase
2016-08-02 13:45:17 +02:00
integer*2 :: tmp_array(4)
2016-06-14 14:37:46 +02:00
get_dij = 0d0
call get_excitation(det1, det2, exc, degree, phase, Nint)
if(degree == -1) return
if(degree == 0) then
stop "get_dij"
end if
call decode_exc_int2(exc,degree,h1,p1,h2,p2,s1,s2)
if(degree == 1) then
h2 = h1
p2 = p1
s2 = s1
h1 = 0
p1 = 0
s1 = 0
end if
if(h1 + (s1-1)*mo_tot_num < h2 + (s2-1)*mo_tot_num) then
2016-08-02 13:45:17 +02:00
tmp_array = (/s1, h1, s2, h2/)
2016-06-14 14:37:46 +02:00
else
2016-08-02 13:45:17 +02:00
tmp_array = (/s2, h2, s1, h1/)
2016-06-14 14:37:46 +02:00
end if
2016-08-02 13:45:17 +02:00
f = searchExc(hh_exists(1,1), tmp_array, hh_shortcut(0))
2016-06-14 14:37:46 +02:00
if(f == -1) return
if(p1 + (s1-1)*mo_tot_num < p2 + (s2-1)*mo_tot_num) then
2016-08-02 13:45:17 +02:00
tmp_array = (/s1, p1, s2, p2/)
2016-06-14 14:37:46 +02:00
else
2016-08-02 13:45:17 +02:00
tmp_array = (/s2, p2, s1, p1/)
2016-06-14 14:37:46 +02:00
end if
2016-08-02 13:45:17 +02:00
t = searchExc(pp_exists(1,hh_shortcut(f)), tmp_array, hh_shortcut(f+1)-hh_shortcut(f))
2016-06-14 14:37:46 +02:00
if(t /= -1) then
2016-07-06 15:43:21 +02:00
get_dij = dIj_unique(t - 1 + hh_shortcut(f), s)
2016-06-14 14:37:46 +02:00
end if
end function
2016-05-26 13:52:48 +02:00
BEGIN_PROVIDER [ integer*2, hh_exists, (4, N_hh_exists) ]
&BEGIN_PROVIDER [ integer, hh_shortcut, (0:N_hh_exists + 1) ]
&BEGIN_PROVIDER [ integer*2, pp_exists, (4, N_pp_exists) ]
2016-05-20 09:44:22 +02:00
implicit none
2016-05-20 11:27:39 +02:00
integer*2,allocatable :: num(:,:)
integer :: exc(0:2, 2, 2), degree, n, on, s, l, i
integer*2 :: h1, h2, p1, p2
2016-05-20 09:44:22 +02:00
double precision :: phase
logical, external :: excEq
2016-05-26 13:52:48 +02:00
allocate(num(4, N_ex_exists+1))
2016-05-20 09:44:22 +02:00
hh_shortcut = 0
hh_exists = 0
pp_exists = 0
num = 0
n = 0
do i=1, N_det_ref
do l=1, N_det_non_ref
call get_excitation(psi_ref(1,1,i), psi_non_ref(1,1,l), exc, degree, phase, N_int)
if(degree == -1) cycle
call dec_exc(exc, h1, h2, p1, p2)
n += 1
num(:, n) = (/h1, h2, p1, p2/)
end do
end do
call sort_exc(num, n)
hh_shortcut(0) = 1
hh_shortcut(1) = 1
2016-05-20 11:27:39 +02:00
hh_exists(:,1) = (/1_2, num(1,1), 1_2, num(2,1)/)
pp_exists(:,1) = (/1_2, num(3,1), 1_2, num(4,1)/)
2016-05-20 09:44:22 +02:00
s = 1
do i=2,n
if(.not. excEq(num(1,i), num(1,s))) then
s += 1
num(:, s) = num(:, i)
2016-05-20 11:27:39 +02:00
pp_exists(:,s) = (/1_2, num(3,s), 1_2, num(4,s)/)
2016-05-20 09:44:22 +02:00
if(hh_exists(2, hh_shortcut(0)) /= num(1,s) .or. &
hh_exists(4, hh_shortcut(0)) /= num(2,s)) then
hh_shortcut(0) += 1
hh_shortcut(hh_shortcut(0)) = s
2016-05-20 11:27:39 +02:00
hh_exists(:,hh_shortcut(0)) = (/1_2, num(1,s), 1_2, num(2,s)/)
2016-05-20 09:44:22 +02:00
end if
end if
end do
hh_shortcut(hh_shortcut(0)+1) = s+1
do s=2,4,2
do i=1,hh_shortcut(0)
if(hh_exists(s, i) == 0) then
hh_exists(s-1, i) = 0
else if(hh_exists(s, i) > mo_tot_num) then
hh_exists(s, i) -= mo_tot_num
hh_exists(s-1, i) = 2
end if
end do
do i=1,hh_shortcut(hh_shortcut(0)+1)-1
if(pp_exists(s, i) == 0) then
pp_exists(s-1, i) = 0
else if(pp_exists(s, i) > mo_tot_num) then
pp_exists(s, i) -= mo_tot_num
pp_exists(s-1, i) = 2
end if
end do
end do
END_PROVIDER
logical function excEq(exc1, exc2)
implicit none
2016-05-20 11:27:39 +02:00
integer*2, intent(in) :: exc1(4), exc2(4)
2016-05-20 09:44:22 +02:00
integer :: i
excEq = .false.
do i=1, 4
if(exc1(i) /= exc2(i)) return
end do
excEq = .true.
end function
integer function excCmp(exc1, exc2)
implicit none
2016-05-20 11:27:39 +02:00
integer*2, intent(in) :: exc1(4), exc2(4)
2016-05-20 09:44:22 +02:00
integer :: i
excCmp = 0
do i=1, 4
if(exc1(i) > exc2(i)) then
excCmp = 1
return
else if(exc1(i) < exc2(i)) then
excCmp = -1
return
end if
end do
end function
subroutine apply_hole_local(det, exc, res, ok, Nint)
2016-05-20 09:44:22 +02:00
use bitmasks
implicit none
integer, intent(in) :: Nint
2016-05-20 11:27:39 +02:00
integer*2, intent(in) :: exc(4)
integer*2 :: s1, s2, h1, h2
2016-05-20 09:44:22 +02:00
integer(bit_kind),intent(in) :: det(Nint, 2)
integer(bit_kind),intent(out) :: res(Nint, 2)
logical, intent(out) :: ok
integer :: ii, pos
ok = .false.
s1 = exc(1)
h1 = exc(2)
s2 = exc(3)
h2 = exc(4)
res = det
if(h1 /= 0) then
ii = (h1-1)/bit_kind_size + 1
pos = iand(h1-1,bit_kind_size-1) ! mod 64
if(iand(det(ii, s1), ishft(1_bit_kind, pos)) == 0_8) then
return
endif
res(ii, s1) = ibclr(res(ii, s1), pos)
2016-05-20 09:44:22 +02:00
end if
ii = (h2-1)/bit_kind_size + 1
pos = iand(h2-1,bit_kind_size-1) ! mod 64
if(iand(det(ii, s2), ishft(1_bit_kind, pos)) == 0_8) then
return
endif
res(ii, s2) = ibclr(res(ii, s2), pos)
2016-05-20 09:44:22 +02:00
ok = .true.
end subroutine
subroutine apply_particle_local(det, exc, res, ok, Nint)
2016-05-20 09:44:22 +02:00
use bitmasks
implicit none
integer, intent(in) :: Nint
2016-05-20 11:27:39 +02:00
integer*2, intent(in) :: exc(4)
integer*2 :: s1, s2, p1, p2
2016-05-20 09:44:22 +02:00
integer(bit_kind),intent(in) :: det(Nint, 2)
integer(bit_kind),intent(out) :: res(Nint, 2)
logical, intent(out) :: ok
integer :: ii, pos
ok = .false.
s1 = exc(1)
p1 = exc(2)
s2 = exc(3)
p2 = exc(4)
res = det
if(p1 /= 0) then
ii = (p1-1)/bit_kind_size + 1
pos = iand(p1-1,bit_kind_size-1)
if(iand(det(ii, s1), ishft(1_bit_kind, pos)) /= 0_8) then
return
endif
res(ii, s1) = ibset(res(ii, s1), pos)
2016-05-20 09:44:22 +02:00
end if
ii = (p2-1)/bit_kind_size + 1
pos = iand(p2-1,bit_kind_size-1)
if(iand(det(ii, s2), ishft(1_bit_kind, pos)) /= 0_8) then
return
endif
res(ii, s2) = ibset(res(ii, s2), pos)
2016-05-20 09:44:22 +02:00
ok = .true.
end subroutine