10
0
mirror of https://github.com/LCPQ/quantum_package synced 2024-11-13 17:43:55 +01:00
quantum_package/plugins/MRCC_Utils/davidson.irp.f

1139 lines
34 KiB
Fortran
Raw Normal View History

2016-09-23 14:23:03 +02:00
subroutine davidson_diag_mrcc(dets_in,u_in,energies,dim_in,sze,N_st,N_st_diag,Nint,iunit,istate)
2015-05-21 10:58:55 +02:00
use bitmasks
implicit none
BEGIN_DOC
! Davidson diagonalization.
!
! dets_in : bitmasks corresponding to determinants
!
! u_in : guess coefficients on the various states. Overwritten
! on exit
!
! dim_in : leftmost dimension of u_in
!
! sze : Number of determinants
!
! N_st : Number of eigenstates
!
! iunit : Unit number for the I/O
!
! Initial guess vectors are not necessarily orthonormal
END_DOC
2016-09-23 14:23:03 +02:00
integer, intent(in) :: dim_in, sze, N_st, Nint, iunit, istate, N_st_diag
2015-05-21 10:58:55 +02:00
integer(bit_kind), intent(in) :: dets_in(Nint,2,sze)
2016-09-27 10:10:39 +02:00
double precision, intent(inout) :: u_in(dim_in,N_st_diag)
double precision, intent(out) :: energies(N_st_diag)
2015-05-21 10:58:55 +02:00
double precision, allocatable :: H_jj(:)
double precision :: diag_h_mat_elem
integer :: i
ASSERT (N_st > 0)
2016-09-23 14:23:03 +02:00
ASSERT (N_st_diag >= N_st)
2015-05-21 10:58:55 +02:00
ASSERT (sze > 0)
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
PROVIDE mo_bielec_integrals_in_map
allocate(H_jj(sze))
2017-03-03 10:41:55 +01:00
H_jj(1) = diag_h_mat_elem(dets_in(1,1,1),Nint)
2015-05-21 10:58:55 +02:00
!$OMP PARALLEL DEFAULT(NONE) &
2015-07-13 18:00:38 +02:00
!$OMP SHARED(sze,H_jj,N_det_ref,dets_in,Nint,istate,delta_ii,idx_ref) &
2015-05-21 10:58:55 +02:00
!$OMP PRIVATE(i)
2017-03-03 10:41:55 +01:00
!$OMP DO
do i=2,sze
H_jj(i) = diag_h_mat_elem(dets_in(1,1,i),Nint)
enddo
!$OMP END DO
2017-03-03 10:41:55 +01:00
!$OMP END PARALLEL
2015-07-13 18:00:38 +02:00
do i=1,N_det_ref
H_jj(idx_ref(i)) += delta_ii(istate,i)
2015-05-21 10:58:55 +02:00
enddo
2016-09-23 14:23:03 +02:00
call davidson_diag_hjj_mrcc(dets_in,u_in,H_jj,energies,dim_in,sze,N_st,N_st_diag,Nint,iunit,istate)
2015-05-21 10:58:55 +02:00
deallocate (H_jj)
end
2016-09-23 14:23:03 +02:00
subroutine davidson_diag_hjj_mrcc(dets_in,u_in,H_jj,energies,dim_in,sze,N_st,N_st_diag,Nint,iunit,istate)
2015-05-21 10:58:55 +02:00
use bitmasks
implicit none
BEGIN_DOC
! Davidson diagonalization with specific diagonal elements of the H matrix
!
! H_jj : specific diagonal H matrix elements to diagonalize de Davidson
!
! dets_in : bitmasks corresponding to determinants
!
! u_in : guess coefficients on the various states. Overwritten
! on exit
!
! dim_in : leftmost dimension of u_in
!
! sze : Number of determinants
!
! N_st : Number of eigenstates
2016-09-27 10:10:39 +02:00
!
! N_st_diag : Number of states in which H is diagonalized
2015-05-21 10:58:55 +02:00
!
! iunit : Unit for the I/O
!
! Initial guess vectors are not necessarily orthonormal
END_DOC
2016-09-27 10:10:39 +02:00
integer, intent(in) :: dim_in, sze, N_st, N_st_diag, Nint, istate
2015-05-21 10:58:55 +02:00
integer(bit_kind), intent(in) :: dets_in(Nint,2,sze)
double precision, intent(in) :: H_jj(sze)
integer, intent(in) :: iunit
2016-09-27 10:10:39 +02:00
double precision, intent(inout) :: u_in(dim_in,N_st_diag)
double precision, intent(out) :: energies(N_st_diag)
2015-05-21 10:58:55 +02:00
2016-09-27 10:10:39 +02:00
integer :: sze_8
2015-05-21 10:58:55 +02:00
integer :: iter
integer :: i,j,k,l,m
logical :: converged
2016-09-27 10:10:39 +02:00
double precision, allocatable :: overlap(:,:)
2015-05-21 10:58:55 +02:00
double precision :: u_dot_v, u_dot_u
integer :: k_pairs, kl
2016-09-27 10:10:39 +02:00
integer :: iter2
2015-05-21 10:58:55 +02:00
double precision, allocatable :: W(:,:,:), U(:,:,:), R(:,:)
double precision, allocatable :: y(:,:,:,:), h(:,:,:,:), lambda(:)
2016-09-27 10:10:39 +02:00
double precision, allocatable :: c(:), H_small(:,:)
2015-05-21 10:58:55 +02:00
double precision :: diag_h_mat_elem
2016-09-27 10:10:39 +02:00
double precision, allocatable :: residual_norm(:)
2015-05-21 10:58:55 +02:00
character*(16384) :: write_buffer
double precision :: to_print(2,N_st)
double precision :: cpu, wall
2016-09-27 10:10:39 +02:00
include 'constants.include.F'
2015-05-21 10:58:55 +02:00
2016-09-27 10:10:39 +02:00
!DIR$ ATTRIBUTES ALIGN : $IRP_ALIGN :: U, W, R, y, h, lambda
PROVIDE nuclear_repulsion
2015-05-21 10:58:55 +02:00
call write_time(iunit)
call wall_time(wall)
call cpu_time(cpu)
write(iunit,'(A)') ''
write(iunit,'(A)') 'Davidson Diagonalization'
write(iunit,'(A)') '------------------------'
write(iunit,'(A)') ''
call write_int(iunit,N_st,'Number of states')
2016-09-27 10:10:39 +02:00
call write_int(iunit,N_st_diag,'Number of states in diagonalization')
2015-05-21 10:58:55 +02:00
call write_int(iunit,sze,'Number of determinants')
2016-08-05 23:42:54 +02:00
call write_int(iunit,istate,'Using dressing for state ')
2015-05-21 10:58:55 +02:00
write(iunit,'(A)') ''
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ ================'
enddo
write(iunit,'(A)') trim(write_buffer)
write_buffer = ' Iter'
do i=1,N_st
write_buffer = trim(write_buffer)//' Energy Residual'
enddo
write(iunit,'(A)') trim(write_buffer)
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ ================'
enddo
write(iunit,'(A)') trim(write_buffer)
2016-09-17 01:54:44 +02:00
integer, external :: align_double
sze_8 = align_double(sze)
2015-05-21 10:58:55 +02:00
allocate( &
2016-09-27 10:10:39 +02:00
W(sze_8,N_st_diag,davidson_sze_max), &
U(sze_8,N_st_diag,davidson_sze_max), &
R(sze_8,N_st_diag), &
h(N_st_diag,davidson_sze_max,N_st_diag,davidson_sze_max), &
y(N_st_diag,davidson_sze_max,N_st_diag,davidson_sze_max), &
residual_norm(N_st_diag), &
overlap(N_st_diag,N_st_diag), &
c(N_st_diag*davidson_sze_max), &
H_small(N_st_diag,N_st_diag), &
lambda(N_st_diag*davidson_sze_max))
2015-05-21 10:58:55 +02:00
ASSERT (N_st > 0)
2016-09-27 10:10:39 +02:00
ASSERT (N_st_diag >= N_st)
2015-05-21 10:58:55 +02:00
ASSERT (sze > 0)
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
! Initialization
! ==============
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
if (k > N_st) then
do i=1,sze
double precision :: r1, r2
call random_number(r1)
call random_number(r2)
u_in(i,k) = dsqrt(-2.d0*dlog(r1))*dcos(dtwo_pi*r2)
2016-09-17 01:54:44 +02:00
enddo
2016-09-27 10:10:39 +02:00
endif
2016-09-17 01:54:44 +02:00
2016-09-27 10:10:39 +02:00
! Gram-Schmidt
! ------------
call dgemv('T',sze,k-1,1.d0,u_in,size(u_in,1), &
u_in(1,k),1,0.d0,c,1)
call dgemv('N',sze,k-1,-1.d0,u_in,size(u_in,1), &
c,1,1.d0,u_in(1,k),1)
call normalize(u_in(1,k),sze)
enddo
2016-09-17 01:54:44 +02:00
2015-05-21 10:58:55 +02:00
2016-04-01 23:33:58 +02:00
converged = .False.
2015-05-21 10:58:55 +02:00
do while (.not.converged)
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
2015-05-21 10:58:55 +02:00
do i=1,sze
U(i,k,1) = u_in(i,k)
enddo
enddo
2016-09-27 10:10:39 +02:00
2015-05-21 10:58:55 +02:00
do iter=1,davidson_sze_max-1
2016-09-27 10:10:39 +02:00
! Compute |W_k> = \sum_i |i><i|H|u_k>
! -----------------------------------------
call H_u_0_mrcc_nstates(W(1,1,iter),U(1,1,iter),H_jj,sze,dets_in,Nint,istate,N_st_diag,sze_8)
2015-05-21 10:58:55 +02:00
! Compute h_kl = <u_k | W_l> = <u_k| H |u_l>
! -------------------------------------------
2016-09-27 10:10:39 +02:00
call dgemm('T','N', N_st_diag*iter, N_st_diag, sze, &
1.d0, U, size(U,1), W(1,1,iter), size(W,1), &
0.d0, h(1,1,1,iter), size(h,1)*size(h,2))
2015-05-21 10:58:55 +02:00
! Diagonalize h
! -------------
2016-09-27 10:10:39 +02:00
call lapack_diag(lambda,y,h,N_st_diag*davidson_sze_max,N_st_diag*iter)
2015-05-21 10:58:55 +02:00
! Express eigenvectors of h in the determinant basis
! --------------------------------------------------
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
2015-05-21 10:58:55 +02:00
do i=1,sze
U(i,k,iter+1) = 0.d0
W(i,k,iter+1) = 0.d0
enddo
enddo
2016-09-27 10:10:39 +02:00
!
call dgemm('N','N', sze, N_st_diag, N_st_diag*iter, &
1.d0, U, size(U,1), y, size(y,1)*size(y,2), 0.d0, U(1,1,iter+1), size(U,1))
call dgemm('N','N',sze,N_st_diag,N_st_diag*iter, &
1.d0, W, size(W,1), y, size(y,1)*size(y,2), 0.d0, W(1,1,iter+1), size(W,1))
2015-05-21 10:58:55 +02:00
! Compute residual vector
! -----------------------
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
2015-05-21 10:58:55 +02:00
do i=1,sze
R(i,k) = lambda(k) * U(i,k,iter+1) - W(i,k,iter+1)
enddo
2016-09-27 10:10:39 +02:00
if (k <= N_st) then
residual_norm(k) = u_dot_u(R(1,k),sze)
to_print(1,k) = lambda(k) + nuclear_repulsion
to_print(2,k) = residual_norm(k)
endif
2015-05-21 10:58:55 +02:00
enddo
2016-09-27 10:10:39 +02:00
write(iunit,'(X,I3,X,100(X,F16.10,X,E16.6))') iter, to_print(:,1:N_st)
2015-05-21 10:58:55 +02:00
call davidson_converged(lambda,residual_norm,wall,iter,cpu,N_st,converged)
if (converged) then
exit
endif
! Davidson step
! -------------
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
2015-05-21 10:58:55 +02:00
do i=1,sze
U(i,k,iter+1) = -1.d0/max(H_jj(i) - lambda(k),1.d-2) * R(i,k)
enddo
enddo
! Gram-Schmidt
! ------------
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
call dgemv('T',sze,N_st_diag*iter,1.d0,U,size(U,1), &
U(1,k,iter+1),1,0.d0,c,1)
call dgemv('N',sze,N_st_diag*iter,-1.d0,U,size(U,1), &
c,1,1.d0,U(1,k,iter+1),1)
2017-03-03 10:41:55 +01:00
2016-09-27 10:10:39 +02:00
call dgemv('T',sze,k-1,1.d0,U(1,1,iter+1),size(U,1), &
U(1,k,iter+1),1,0.d0,c,1)
call dgemv('N',sze,k-1,-1.d0,U(1,1,iter+1),size(U,1), &
c,1,1.d0,U(1,k,iter+1),1)
2015-05-21 10:58:55 +02:00
call normalize( U(1,k,iter+1), sze )
enddo
enddo
if (.not.converged) then
iter = davidson_sze_max-1
endif
! Re-contract to u_in
! -----------
2016-09-27 10:10:39 +02:00
do k=1,N_st_diag
2015-05-21 10:58:55 +02:00
do i=1,sze
u_in(i,k) = 0.d0
enddo
enddo
2016-09-27 10:10:39 +02:00
call dgemm('N','N', sze, N_st_diag, N_st_diag*iter, 1.d0, &
U, size(U,1), y, N_st_diag*davidson_sze_max, &
0.d0, u_in, size(u_in,1))
2015-05-21 10:58:55 +02:00
enddo
do k=1,N_st_diag
energies(k) = lambda(k)
enddo
2015-05-21 10:58:55 +02:00
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ ================'
enddo
write(iunit,'(A)') trim(write_buffer)
write(iunit,'(A)') ''
call write_time(iunit)
deallocate ( &
2016-09-27 10:10:39 +02:00
W, residual_norm, &
U, overlap, &
R, c, &
2015-05-21 10:58:55 +02:00
h, &
y, &
lambda &
)
end
2016-09-17 01:54:44 +02:00
subroutine u_0_H_u_0_mrcc_nstates(e_0,u_0,n,keys_tmp,Nint,istate,N_st,sze_8)
use bitmasks
implicit none
BEGIN_DOC
! Computes e_0 = <u_0|H|u_0>/<u_0|u_0>
!
! n : number of determinants
!
END_DOC
integer, intent(in) :: n,Nint,N_st,sze_8
double precision, intent(out) :: e_0(N_st)
2016-09-18 00:33:19 +02:00
double precision, intent(in) :: u_0(sze_8,N_st)
2016-09-17 01:54:44 +02:00
integer(bit_kind),intent(in) :: keys_tmp(Nint,2,n)
2016-08-04 15:17:01 +02:00
integer,intent(in) :: istate
2016-09-18 00:33:19 +02:00
double precision, allocatable :: v_0(:,:), H_jj(:)
2016-08-04 15:17:01 +02:00
double precision :: u_dot_u,u_dot_v,diag_H_mat_elem
integer :: i,j
2016-09-18 00:33:19 +02:00
allocate(H_jj(n), v_0(sze_8,N_st))
2016-08-04 15:17:01 +02:00
do i = 1, n
H_jj(i) = diag_H_mat_elem(keys_tmp(1,1,i),Nint)
enddo
do i=1,N_det_ref
H_jj(idx_ref(i)) += delta_ii(istate,i)
enddo
2016-09-17 01:54:44 +02:00
call H_u_0_mrcc_nstates(v_0,u_0,H_jj,n,keys_tmp,Nint,istate,N_st,sze_8)
do i=1,N_st
e_0(i) = u_dot_v(v_0(1,i),u_0(1,i),n)/u_dot_u(u_0(1,i),n)
enddo
2016-09-18 00:33:19 +02:00
deallocate(H_jj, v_0)
2016-08-04 15:17:01 +02:00
end
2016-09-17 01:54:44 +02:00
subroutine H_u_0_mrcc_nstates(v_0,u_0,H_jj,n,keys_tmp,Nint,istate_in,N_st,sze_8)
use bitmasks
implicit none
BEGIN_DOC
! Computes v_0 = H|u_0>
!
! n : number of determinants
!
! H_jj : array of <j|H|j>
END_DOC
integer, intent(in) :: n,Nint,istate_in,N_st,sze_8
double precision, intent(out) :: v_0(sze_8,N_st)
double precision, intent(in) :: u_0(sze_8,N_st)
double precision, intent(in) :: H_jj(n)
integer(bit_kind),intent(in) :: keys_tmp(Nint,2,n)
double precision :: hij
2016-09-17 01:54:44 +02:00
double precision, allocatable :: vt(:,:)
integer :: i,j,k,l, jj,ii
integer :: i0, j0
2016-09-17 01:54:44 +02:00
integer(bit_kind) :: sorted_i(Nint)
2016-09-17 01:54:44 +02:00
integer,allocatable :: shortcut(:,:), sort_idx(:,:)
integer(bit_kind), allocatable :: sorted(:,:,:), version(:,:,:)
2015-11-02 20:56:02 +01:00
2016-09-17 01:54:44 +02:00
integer :: sh, sh2, ni, exa, ext, org_i, org_j, endi, pass, istate
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
ASSERT (n>0)
2016-09-17 01:54:44 +02:00
PROVIDE ref_bitmask_energy
allocate (shortcut(0:n+1,2), sort_idx(n,2), sorted(Nint,n,2), version(Nint,n,2))
v_0 = 0.d0
2016-09-17 01:54:44 +02:00
call sort_dets_ab_v(keys_tmp, sorted(1,1,1), sort_idx(1,1), shortcut(0,1), version(1,1,1), n, Nint)
call sort_dets_ba_v(keys_tmp, sorted(1,1,2), sort_idx(1,2), shortcut(0,2), version(1,1,2), n, Nint)
2015-11-02 20:56:02 +01:00
2016-09-17 01:54:44 +02:00
!$OMP PARALLEL DEFAULT(NONE) &
!$OMP PRIVATE(i,hij,j,k,jj,vt,ii,sh,sh2,ni,exa,ext,org_i,org_j,endi,sorted_i,istate)&
!$OMP SHARED(n,H_jj,u_0,keys_tmp,Nint,v_0,sorted,shortcut,sort_idx,version,N_st,sze_8,&
!$OMP istate_in,delta_ij,N_det_ref,N_det_non_ref,idx_ref,idx_non_ref)
allocate(vt(sze_8,N_st))
Vt = 0.d0
2015-11-02 20:56:02 +01:00
2017-03-03 10:41:55 +01:00
!$OMP DO SCHEDULE(static,1)
2016-09-17 01:54:44 +02:00
do sh=1,shortcut(0,1)
do sh2=sh,shortcut(0,1)
exa = 0
do ni=1,Nint
exa = exa + popcnt(xor(version(ni,sh,1), version(ni,sh2,1)))
end do
if(exa > 2) then
cycle
end if
2016-09-17 01:54:44 +02:00
do i=shortcut(sh,1),shortcut(sh+1,1)-1
org_i = sort_idx(i,1)
if(sh==sh2) then
endi = i-1
else
endi = shortcut(sh2+1,1)-1
2015-11-02 20:56:02 +01:00
end if
2016-09-17 01:54:44 +02:00
do ni=1,Nint
sorted_i(ni) = sorted(ni,i,1)
enddo
do j=shortcut(sh2,1),endi
org_j = sort_idx(j,1)
ext = exa
do ni=1,Nint
ext = ext + popcnt(xor(sorted_i(ni), sorted(ni,j,1)))
end do
if(ext <= 4) then
call i_H_j(keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),Nint,hij)
do istate=1,N_st
vt (org_i,istate) = vt (org_i,istate) + hij*u_0(org_j,istate)
vt (org_j,istate) = vt (org_j,istate) + hij*u_0(org_i,istate)
enddo
endif
enddo
enddo
enddo
2015-11-02 20:56:02 +01:00
enddo
2017-03-03 10:41:55 +01:00
!$OMP END DO
2017-03-03 10:41:55 +01:00
!$OMP DO SCHEDULE(static,1)
2016-09-17 01:54:44 +02:00
do sh=1,shortcut(0,2)
do i=shortcut(sh,2),shortcut(sh+1,2)-1
org_i = sort_idx(i,2)
do j=shortcut(sh,2),i-1
org_j = sort_idx(j,2)
2015-11-02 20:56:02 +01:00
ext = 0
do ni=1,Nint
2016-09-17 01:54:44 +02:00
ext = ext + popcnt(xor(sorted(ni,i,2), sorted(ni,j,2)))
2015-11-02 20:56:02 +01:00
end do
if(ext == 4) then
2015-11-02 20:56:02 +01:00
call i_H_j(keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),Nint,hij)
2016-09-17 01:54:44 +02:00
do istate=1,N_st
vt (org_i,istate) = vt (org_i,istate) + hij*u_0(org_j,istate)
vt (org_j,istate) = vt (org_j,istate) + hij*u_0(org_i,istate)
enddo
2015-11-02 20:56:02 +01:00
end if
end do
end do
enddo
2017-03-03 10:41:55 +01:00
!$OMP END DO
2016-09-17 01:54:44 +02:00
!$OMP DO
do ii=1,n_det_ref
i = idx_ref(ii)
do jj = 1, n_det_non_ref
j = idx_non_ref(jj)
2016-09-17 01:54:44 +02:00
do istate=1,N_st
vt (i,istate) = vt (i,istate) + delta_ij(istate_in,jj,ii)*u_0(j,istate)
vt (j,istate) = vt (j,istate) + delta_ij(istate_in,jj,ii)*u_0(i,istate)
enddo
enddo
enddo
!$OMP END DO
2016-09-17 01:54:44 +02:00
do istate=1,N_st
do i=n,1,-1
2017-03-06 18:55:53 +01:00
!$OMP ATOMIC
2016-09-17 01:54:44 +02:00
v_0(i,istate) = v_0(i,istate) + vt(i,istate)
enddo
enddo
2016-09-17 01:54:44 +02:00
deallocate(vt)
!$OMP END PARALLEL
2016-09-17 01:54:44 +02:00
do istate=1,N_st
do i=1,n
v_0(i,istate) += H_jj(i) * u_0(i,istate)
enddo
enddo
deallocate (shortcut, sort_idx, sorted, version)
end
2016-09-30 22:29:22 +02:00
subroutine davidson_diag_mrcc_hs2(dets_in,u_in,dim_in,energies,sze,N_st,N_st_diag,Nint,iunit,istate)
use bitmasks
implicit none
BEGIN_DOC
! Davidson diagonalization.
!
! dets_in : bitmasks corresponding to determinants
!
! u_in : guess coefficients on the various states. Overwritten
! on exit
!
! dim_in : leftmost dimension of u_in
!
! sze : Number of determinants
!
! N_st : Number of eigenstates
!
! iunit : Unit number for the I/O
!
! Initial guess vectors are not necessarily orthonormal
END_DOC
integer, intent(in) :: dim_in, sze, N_st, N_st_diag, Nint, iunit, istate
integer(bit_kind), intent(in) :: dets_in(Nint,2,sze)
double precision, intent(inout) :: u_in(dim_in,N_st_diag)
double precision, intent(out) :: energies(N_st_diag)
2016-09-30 22:29:22 +02:00
double precision, allocatable :: H_jj(:), S2_jj(:)
double precision :: diag_h_mat_elem
integer :: i
ASSERT (N_st > 0)
ASSERT (sze > 0)
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
2017-03-03 10:41:55 +01:00
PROVIDE mo_bielec_integrals_in_map
2016-09-30 22:29:22 +02:00
allocate(H_jj(sze), S2_jj(sze))
2017-03-03 10:41:55 +01:00
H_jj(1) = diag_h_mat_elem(dets_in(1,1,1),Nint)
call get_s2(dets_in(1,1,1),dets_in(1,1,1),Nint,S2_jj(1))
2016-09-30 22:29:22 +02:00
!$OMP PARALLEL DEFAULT(NONE) &
!$OMP SHARED(sze,H_jj,S2_jj, dets_in,Nint,N_det_ref,delta_ii, &
!$OMP idx_ref, istate) &
!$OMP PRIVATE(i)
2017-03-03 10:41:55 +01:00
!$OMP DO
do i=2,sze
2016-09-30 22:29:22 +02:00
H_jj(i) = diag_h_mat_elem(dets_in(1,1,i),Nint)
call get_s2(dets_in(1,1,i),dets_in(1,1,i),Nint,S2_jj(i))
enddo
!$OMP END DO
2017-03-03 10:41:55 +01:00
!$OMP END PARALLEL
2016-09-30 22:29:22 +02:00
do i=1,N_det_ref
H_jj(idx_ref(i)) += delta_ii(istate,i)
enddo
call davidson_diag_hjj_sjj_mrcc(dets_in,u_in,H_jj,S2_jj,energies,dim_in,sze,N_st,N_st_diag,Nint,iunit,istate)
deallocate (H_jj,S2_jj)
end
subroutine davidson_diag_hjj_sjj_mrcc(dets_in,u_in,H_jj,S2_jj,energies,dim_in,sze,N_st,N_st_diag,Nint,iunit,istate )
use bitmasks
implicit none
BEGIN_DOC
! Davidson diagonalization with specific diagonal elements of the H matrix
!
! H_jj : specific diagonal H matrix elements to diagonalize de Davidson
!
! S2_jj : specific diagonal S^2 matrix elements
!
! dets_in : bitmasks corresponding to determinants
!
! u_in : guess coefficients on the various states. Overwritten
! on exit
!
! dim_in : leftmost dimension of u_in
!
! sze : Number of determinants
!
! N_st : Number of eigenstates
!
! N_st_diag : Number of states in which H is diagonalized. Assumed > sze
!
! iunit : Unit for the I/O
!
! Initial guess vectors are not necessarily orthonormal
END_DOC
integer, intent(in) :: dim_in, sze, N_st, N_st_diag, Nint, istate
integer(bit_kind), intent(in) :: dets_in(Nint,2,sze)
double precision, intent(in) :: H_jj(sze), S2_jj(sze)
integer, intent(in) :: iunit
double precision, intent(inout) :: u_in(dim_in,N_st_diag)
double precision, intent(out) :: energies(N_st_diag)
integer :: sze_8
integer :: iter
integer :: i,j,k,l,m
logical :: converged
double precision :: u_dot_v, u_dot_u
integer :: k_pairs, kl
integer :: iter2
2016-11-15 11:23:00 +01:00
double precision, allocatable :: W(:,:), U(:,:), S(:,:), overlap(:,:)
2016-09-30 22:29:22 +02:00
double precision, allocatable :: y(:,:), h(:,:), lambda(:), s2(:)
double precision, allocatable :: c(:), s_(:,:), s_tmp(:,:)
double precision :: diag_h_mat_elem
double precision, allocatable :: residual_norm(:)
character*(16384) :: write_buffer
double precision :: to_print(3,N_st)
double precision :: cpu, wall
2016-11-03 12:05:19 +01:00
integer :: shift, shift2, itermax
2016-09-30 22:29:22 +02:00
include 'constants.include.F'
2016-11-04 00:57:37 +01:00
!DIR$ ATTRIBUTES ALIGN : $IRP_ALIGN :: U, W, S, y, h, lambda
2016-11-18 22:08:08 +01:00
if (N_st_diag*3 > sze) then
print *, 'error in Davidson :'
print *, 'Increase n_det_max_jacobi to ', N_st_diag*3
stop -1
2016-09-30 22:29:22 +02:00
endif
PROVIDE nuclear_repulsion
call write_time(iunit)
call wall_time(wall)
call cpu_time(cpu)
write(iunit,'(A)') ''
write(iunit,'(A)') 'Davidson Diagonalization'
write(iunit,'(A)') '------------------------'
write(iunit,'(A)') ''
call write_int(iunit,N_st,'Number of states')
call write_int(iunit,N_st_diag,'Number of states in diagonalization')
call write_int(iunit,sze,'Number of determinants')
call write_int(iunit,istate,'Using dressing for state ')
write(iunit,'(A)') ''
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ =========== ==========='
enddo
write(iunit,'(A)') trim(write_buffer)
write_buffer = ' Iter'
do i=1,N_st
2016-11-18 21:30:48 +01:00
write_buffer = trim(write_buffer)//' Energy S^2 Residual '
2016-09-30 22:29:22 +02:00
enddo
write(iunit,'(A)') trim(write_buffer)
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ =========== ==========='
enddo
write(iunit,'(A)') trim(write_buffer)
integer, external :: align_double
sze_8 = align_double(sze)
2016-11-03 12:05:19 +01:00
itermax = min(davidson_sze_max, sze/N_st_diag)
2016-09-30 22:29:22 +02:00
allocate( &
2016-11-15 11:23:00 +01:00
W(sze_8,N_st_diag*itermax), &
U(sze_8,N_st_diag*itermax), &
S(sze_8,N_st_diag*itermax), &
h(N_st_diag*itermax,N_st_diag*itermax), &
y(N_st_diag*itermax,N_st_diag*itermax), &
s_(N_st_diag*itermax,N_st_diag*itermax), &
s_tmp(N_st_diag*itermax,N_st_diag*itermax), &
2016-09-30 22:29:22 +02:00
residual_norm(N_st_diag), &
2016-11-15 11:23:00 +01:00
c(N_st_diag*itermax), &
s2(N_st_diag*itermax), &
overlap(N_st_diag*itermax,N_st_diag*itermax), &
2016-11-03 12:05:19 +01:00
lambda(N_st_diag*itermax))
h = 0.d0
s_ = 0.d0
s_tmp = 0.d0
U = 0.d0
W = 0.d0
S = 0.d0
y = 0.d0
2016-09-30 22:29:22 +02:00
ASSERT (N_st > 0)
ASSERT (N_st_diag >= N_st)
ASSERT (sze > 0)
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
! Davidson iterations
! ===================
converged = .False.
2016-11-03 12:05:19 +01:00
double precision :: r1, r2
2016-11-15 18:39:44 +01:00
do k=N_st+1,N_st_diag
2016-11-18 19:17:34 +01:00
u_in(k,k) = 10.d0
2016-11-03 12:05:19 +01:00
do i=1,sze
call random_number(r1)
call random_number(r2)
r1 = dsqrt(-2.d0*dlog(r1))
r2 = dtwo_pi*r2
u_in(i,k) = r1*dcos(r2)
enddo
2016-09-30 22:29:22 +02:00
enddo
2016-11-15 18:39:44 +01:00
do k=1,N_st_diag
call normalize(u_in(1,k),sze)
2016-09-30 22:29:22 +02:00
enddo
do while (.not.converged)
do k=1,N_st_diag
do i=1,sze
U(i,k) = u_in(i,k)
enddo
enddo
do iter=1,davidson_sze_max-1
shift = N_st_diag*(iter-1)
shift2 = N_st_diag*iter
2016-11-03 12:05:19 +01:00
call ortho_qr(U,size(U,1),sze,shift2)
2016-09-30 22:29:22 +02:00
! Compute |W_k> = \sum_i |i><i|H|u_k>
! -----------------------------------------
call H_S2_u_0_mrcc_nstates(W(1,shift+1),S(1,shift+1),U(1,shift+1),H_jj,S2_jj,sze,dets_in,Nint,&
istate,N_st_diag,sze_8)
! Compute h_kl = <u_k | W_l> = <u_k| H |u_l>
! -------------------------------------------
2016-11-04 18:03:32 +01:00
call dgemm('T','N', shift2, shift2, sze, &
1.d0, U, size(U,1), W, size(W,1), &
0.d0, h, size(h,1))
2016-09-30 22:29:22 +02:00
2016-11-04 18:03:32 +01:00
call dgemm('T','N', shift2, shift2, sze, &
1.d0, U, size(U,1), S, size(S,1), &
0.d0, s_, size(s_,1))
2016-09-30 22:29:22 +02:00
2016-11-18 22:08:08 +01:00
! ! Diagonalize S^2
! ! ---------------
!
! call lapack_diag(s2,y,s_,size(s_,1),shift2)
!
2016-11-18 19:17:34 +01:00
! ! Rotate H in the basis of eigenfunctions of s2
! ! ---------------------------------------------
!
! call dgemm('N','N',shift2,shift2,shift2, &
! 1.d0, h, size(h,1), y, size(y,1), &
! 0.d0, s_tmp, size(s_tmp,1))
!
! call dgemm('T','N',shift2,shift2,shift2, &
! 1.d0, y, size(y,1), s_tmp, size(s_tmp,1), &
! 0.d0, h, size(h,1))
!
! ! Damp interaction between different spin states
! ! ------------------------------------------------
!
! do k=1,shift2
! do l=1,shift2
! if (dabs(s2(k) - s2(l)) > 1.d0) then
! h(k,l) = h(k,l)*(max(0.d0,1.d0 - dabs(s2(k) - s2(l))))
! endif
! enddo
! enddo
!
! ! Rotate back H
! ! -------------
!
! call dgemm('N','T',shift2,shift2,shift2, &
! 1.d0, h, size(h,1), y, size(y,1), &
! 0.d0, s_tmp, size(s_tmp,1))
!
! call dgemm('N','N',shift2,shift2,shift2, &
! 1.d0, y, size(y,1), s_tmp, size(s_tmp,1), &
! 0.d0, h, size(h,1))
2016-09-30 22:29:22 +02:00
! Diagonalize h
! -------------
call lapack_diag(lambda,y,h,size(h,1),shift2)
2016-11-30 11:32:40 +01:00
2016-09-30 22:29:22 +02:00
! Compute S2 for each eigenvector
! -------------------------------
call dgemm('N','N',shift2,shift2,shift2, &
1.d0, s_, size(s_,1), y, size(y,1), &
0.d0, s_tmp, size(s_tmp,1))
call dgemm('T','N',shift2,shift2,shift2, &
1.d0, y, size(y,1), s_tmp, size(s_tmp,1), &
0.d0, s_, size(s_,1))
do k=1,shift2
s2(k) = s_(k,k) + S_z2_Sz
enddo
2016-11-18 15:06:33 +01:00
if (s2_eig) then
logical :: state_ok(N_st_diag*davidson_sze_max)
do k=1,shift2
2016-11-18 22:08:08 +01:00
state_ok(k) = (dabs(s2(k)-expected_s2) < 0.6d0)
2016-11-18 15:06:33 +01:00
enddo
else
do k=1,size(state_ok)
state_ok(k) = .True.
enddo
2016-11-15 18:39:44 +01:00
endif
2016-11-18 15:06:33 +01:00
do k=1,shift2
if (.not. state_ok(k)) then
do l=k+1,shift2
if (state_ok(l)) then
call dswap(shift2, y(1,k), 1, y(1,l), 1)
call dswap(1, s2(k), 1, s2(l), 1)
call dswap(1, lambda(k), 1, lambda(l), 1)
state_ok(k) = .True.
state_ok(l) = .False.
exit
endif
enddo
endif
enddo
2016-11-15 18:39:44 +01:00
2016-11-18 22:08:08 +01:00
if (state_following) then
! Compute overlap with U_in
! -------------------------
2016-11-19 00:39:02 +01:00
integer :: order(N_st_diag)
double precision :: cmax
2016-11-18 22:08:08 +01:00
overlap = -1.d0
do k=1,shift2
do i=1,shift2
overlap(k,i) = dabs(y(k,i))
enddo
2016-11-18 21:30:48 +01:00
enddo
2016-11-18 22:08:08 +01:00
do k=1,N_st
2016-11-19 00:39:02 +01:00
cmax = -1.d0
2016-11-21 21:42:28 +01:00
do i=1,N_st
2016-11-19 00:39:02 +01:00
if (overlap(i,k) > cmax) then
cmax = overlap(i,k)
order(k) = i
endif
enddo
do i=1,shift2
overlap(order(k),i) = -1.d0
2016-11-18 22:22:46 +01:00
enddo
2016-11-18 22:08:08 +01:00
enddo
overlap = y
do k=1,N_st
l = order(k)
if (k /= l) then
y(1:shift2,k) = overlap(1:shift2,l)
endif
enddo
do k=1,N_st
overlap(k,1) = lambda(k)
overlap(k,2) = s2(k)
enddo
do k=1,N_st
l = order(k)
if (k /= l) then
lambda(k) = overlap(l,1)
s2(k) = overlap(l,2)
endif
enddo
endif
2016-09-30 22:29:22 +02:00
! Express eigenvectors of h in the determinant basis
! --------------------------------------------------
call dgemm('N','N', sze, N_st_diag, shift2, &
1.d0, U, size(U,1), y, size(y,1), 0.d0, U(1,shift2+1), size(U,1))
call dgemm('N','N', sze, N_st_diag, shift2, &
1.d0, W, size(W,1), y, size(y,1), 0.d0, W(1,shift2+1), size(W,1))
call dgemm('N','N', sze, N_st_diag, shift2, &
1.d0, S, size(S,1), y, size(y,1), 0.d0, S(1,shift2+1), size(S,1))
! Compute residual vector
! -----------------------
2016-11-03 12:05:19 +01:00
2016-09-30 22:29:22 +02:00
do k=1,N_st_diag
2016-11-30 22:49:03 +01:00
! if (state_ok(k)) then
2016-11-18 15:06:33 +01:00
do i=1,sze
U(i,shift2+k) = (lambda(k) * U(i,shift2+k) - W(i,shift2+k) ) &
* (1.d0 + s2(k) * U(i,shift2+k) - S(i,shift2+k) - S_z2_Sz &
)/max(H_jj(i) - lambda (k),1.d-2)
enddo
2016-11-30 22:49:03 +01:00
! else
! ! Randomize components with bad <S2>
! do i=1,sze-2,2
! call random_number(r1)
! call random_number(r2)
! r1 = dsqrt(-2.d0*dlog(r1))
! r2 = dtwo_pi*r2
! U(i,shift2+k) = r1*dcos(r2)
! U(i+1,shift2+k) = r1*dsin(r2)
! enddo
! do i=sze-2+1,sze
! call random_number(r1)
! call random_number(r2)
! r1 = dsqrt(-2.d0*dlog(r1))
! r2 = dtwo_pi*r2
! U(i,shift2+k) = r1*dcos(r2)
! enddo
! endif
2016-11-18 15:06:33 +01:00
2016-09-30 22:29:22 +02:00
if (k <= N_st) then
2016-11-03 12:05:19 +01:00
residual_norm(k) = u_dot_u(U(1,shift2+k),sze)
2016-09-30 22:29:22 +02:00
to_print(1,k) = lambda(k) + nuclear_repulsion
to_print(2,k) = s2(k)
to_print(3,k) = residual_norm(k)
endif
enddo
2016-11-03 12:05:19 +01:00
write(iunit,'(X,I3,X,100(X,F16.10,X,F11.6,X,E11.3))') iter, to_print(1:3,1:N_st)
2016-09-30 22:29:22 +02:00
call davidson_converged(lambda,residual_norm,wall,iter,cpu,N_st,converged)
2016-11-03 12:05:19 +01:00
do k=1,N_st
if (residual_norm(k) > 1.e8) then
print *, ''
stop 'Davidson failed'
endif
enddo
2016-09-30 22:29:22 +02:00
if (converged) then
exit
endif
enddo
! Re-contract to u_in
! -----------
2016-11-18 19:17:34 +01:00
call dgemm('N','N', sze, N_st_diag, shift2, &
1.d0, U, size(U,1), y, size(y,1), 0.d0, u_in, size(u_in,1))
2016-09-30 22:29:22 +02:00
enddo
2016-11-18 22:08:08 +01:00
do k=1,N_st_diag
energies(k) = lambda(k)
enddo
2016-09-30 22:29:22 +02:00
write_buffer = '===== '
do i=1,N_st
write_buffer = trim(write_buffer)//' ================ =========== ==========='
enddo
write(iunit,'(A)') trim(write_buffer)
write(iunit,'(A)') ''
call write_time(iunit)
deallocate ( &
W, residual_norm, &
2016-11-18 22:08:08 +01:00
U, overlap, &
2016-11-04 00:57:37 +01:00
c, S, &
2016-09-30 22:29:22 +02:00
h, &
y, s_, s_tmp, &
lambda &
)
end
subroutine H_S2_u_0_mrcc_nstates(v_0,s_0,u_0,H_jj,S2_jj,n,keys_tmp,Nint,istate_in,N_st,sze_8)
use bitmasks
implicit none
BEGIN_DOC
! Computes v_0 = H|u_0> and s_0 = S^2 |u_0>
!
! n : number of determinants
!
! H_jj : array of <j|H|j>
!
! S2_jj : array of <j|S^2|j>
END_DOC
integer, intent(in) :: N_st,n,Nint, sze_8, istate_in
double precision, intent(out) :: v_0(sze_8,N_st), s_0(sze_8,N_st)
double precision, intent(in) :: u_0(sze_8,N_st)
double precision, intent(in) :: H_jj(n), S2_jj(n)
integer(bit_kind),intent(in) :: keys_tmp(Nint,2,n)
double precision :: hij,s2
double precision, allocatable :: vt(:,:), ut(:,:), st(:,:)
integer :: i,j,k,l, jj,ii
integer :: i0, j0
integer, allocatable :: shortcut(:,:), sort_idx(:,:)
integer(bit_kind), allocatable :: sorted(:,:,:), version(:,:,:)
integer(bit_kind) :: sorted_i(Nint)
integer :: sh, sh2, ni, exa, ext, org_i, org_j, endi, istate
integer :: N_st_8
integer, external :: align_double
!DIR$ ATTRIBUTES ALIGN : $IRP_ALIGN :: vt, ut
N_st_8 = align_double(N_st)
ASSERT (Nint > 0)
ASSERT (Nint == N_int)
ASSERT (n>0)
PROVIDE ref_bitmask_energy
allocate (shortcut(0:n+1,2), sort_idx(n,2), sorted(Nint,n,2), version(Nint,n,2))
allocate(ut(N_st_8,n))
v_0 = 0.d0
s_0 = 0.d0
do i=1,n
do istate=1,N_st
ut(istate,i) = u_0(i,istate)
enddo
enddo
call sort_dets_ab_v(keys_tmp, sorted(1,1,1), sort_idx(1,1), shortcut(0,1), version(1,1,1), n, Nint)
call sort_dets_ba_v(keys_tmp, sorted(1,1,2), sort_idx(1,2), shortcut(0,2), version(1,1,2), n, Nint)
2016-11-30 18:53:13 +01:00
PROVIDE delta_ij_s2
2016-09-30 22:29:22 +02:00
!$OMP PARALLEL DEFAULT(NONE) &
!$OMP PRIVATE(i,hij,s2,j,k,jj,vt,st,ii,sh,sh2,ni,exa,ext,org_i,org_j,endi,sorted_i,istate)&
!$OMP SHARED(n,keys_tmp,ut,Nint,v_0,s_0,sorted,shortcut,sort_idx,version,N_st,N_st_8, &
2016-11-18 19:17:34 +01:00
!$OMP N_det_ref, idx_ref, N_det_non_ref, idx_non_ref, delta_ij, delta_ij_s2,istate_in)
2016-09-30 22:29:22 +02:00
allocate(vt(N_st_8,n),st(N_st_8,n))
Vt = 0.d0
St = 0.d0
2017-03-23 15:41:27 +01:00
!$OMP DO SCHEDULE(guided)
2016-09-30 22:29:22 +02:00
do sh=1,shortcut(0,1)
do sh2=sh,shortcut(0,1)
exa = 0
do ni=1,Nint
exa = exa + popcnt(xor(version(ni,sh,1), version(ni,sh2,1)))
end do
if(exa > 2) then
cycle
end if
do i=shortcut(sh,1),shortcut(sh+1,1)-1
org_i = sort_idx(i,1)
if(sh==sh2) then
endi = i-1
else
endi = shortcut(sh2+1,1)-1
end if
do ni=1,Nint
sorted_i(ni) = sorted(ni,i,1)
enddo
do j=shortcut(sh2,1),endi
org_j = sort_idx(j,1)
ext = exa
do ni=1,Nint
ext = ext + popcnt(xor(sorted_i(ni), sorted(ni,j,1)))
end do
if(ext <= 4) then
call i_h_j (keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),nint,hij)
call get_s2(keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),nint,s2)
do istate=1,n_st
vt (istate,org_i) = vt (istate,org_i) + hij*ut(istate,org_j)
vt (istate,org_j) = vt (istate,org_j) + hij*ut(istate,org_i)
st (istate,org_i) = st (istate,org_i) + s2*ut(istate,org_j)
st (istate,org_j) = st (istate,org_j) + s2*ut(istate,org_i)
enddo
endif
enddo
enddo
enddo
enddo
!$OMP END DO
2017-03-23 15:41:27 +01:00
!$OMP DO SCHEDULE(guided)
2016-09-30 22:29:22 +02:00
do sh=1,shortcut(0,2)
do i=shortcut(sh,2),shortcut(sh+1,2)-1
org_i = sort_idx(i,2)
do j=shortcut(sh,2),i-1
org_j = sort_idx(j,2)
ext = 0
do ni=1,Nint
ext = ext + popcnt(xor(sorted(ni,i,2), sorted(ni,j,2)))
end do
if(ext == 4) then
call i_h_j (keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),nint,hij)
call get_s2(keys_tmp(1,1,org_j),keys_tmp(1,1,org_i),nint,s2)
do istate=1,n_st
vt (istate,org_i) = vt (istate,org_i) + hij*ut(istate,org_j)
vt (istate,org_j) = vt (istate,org_j) + hij*ut(istate,org_i)
st (istate,org_i) = st (istate,org_i) + s2*ut(istate,org_j)
st (istate,org_j) = st (istate,org_j) + s2*ut(istate,org_i)
enddo
end if
end do
end do
enddo
!$OMP END DO
2016-09-30 22:29:22 +02:00
! --------------------------
! Begin Specific to dressing
! --------------------------
!$OMP DO
do ii=1,n_det_ref
i = idx_ref(ii)
do jj = 1, n_det_non_ref
j = idx_non_ref(jj)
do istate=1,N_st
vt (istate,i) = vt (istate,i) + delta_ij(istate_in,jj,ii)*ut(istate,j)
vt (istate,j) = vt (istate,j) + delta_ij(istate_in,jj,ii)*ut(istate,i)
2016-11-18 19:17:34 +01:00
st (istate,i) = st (istate,i) + delta_ij_s2(istate_in,jj,ii)*ut(istate,j)
st (istate,j) = st (istate,j) + delta_ij_s2(istate_in,jj,ii)*ut(istate,i)
2016-09-30 22:29:22 +02:00
enddo
enddo
enddo
!$OMP END DO
! ------------------------
! End Specific to dressing
! ------------------------
do istate=1,N_st
do i=n,1,-1
2017-03-06 18:55:53 +01:00
!$OMP ATOMIC
2016-09-30 22:29:22 +02:00
v_0(i,istate) = v_0(i,istate) + vt(istate,i)
2017-03-06 18:55:53 +01:00
!$OMP ATOMIC
2016-09-30 22:29:22 +02:00
s_0(i,istate) = s_0(i,istate) + st(istate,i)
enddo
enddo
deallocate(vt,st)
!$OMP END PARALLEL
do istate=1,N_st
do i=1,n
v_0(i,istate) = v_0(i,istate) + H_jj(i) * u_0(i,istate)
s_0(i,istate) = s_0(i,istate) + s2_jj(i)* u_0(i,istate)
enddo
enddo
deallocate (shortcut, sort_idx, sorted, version, ut)
end