4
1
mirror of https://github.com/pfloos/quack synced 2024-11-07 06:33:55 +01:00
quack/src/CC/pCCD.f90

198 lines
4.9 KiB
Fortran
Raw Normal View History

2020-03-22 22:22:47 +01:00
subroutine pCCD(maxSCF,thresh,max_diis,nBas,nC,nO,nV,nR,ERI,ENuc,ERHF,eHF)
2019-10-15 23:13:00 +02:00
2020-03-21 22:50:43 +01:00
! pair CCD module
2019-10-15 23:13:00 +02:00
implicit none
! Input variables
integer,intent(in) :: maxSCF
integer,intent(in) :: max_diis
double precision,intent(in) :: thresh
2020-03-22 22:22:47 +01:00
integer,intent(in) :: nBas,nC,nO,nV,nR
2019-10-15 23:13:00 +02:00
double precision,intent(in) :: ENuc,ERHF
double precision,intent(in) :: eHF(nBas)
double precision,intent(in) :: ERI(nBas,nBas,nBas,nBas)
! Local variables
2020-03-22 15:20:42 +01:00
integer :: i,j,a,b
2019-10-15 23:13:00 +02:00
integer :: nSCF
double precision :: Conv
double precision :: ECCD,EcCCD
2020-10-08 17:19:48 +02:00
double precision,allocatable :: eO(:)
double precision,allocatable :: eV(:)
double precision,allocatable :: delta_OV(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 15:20:42 +01:00
double precision,allocatable :: OOOO(:,:)
double precision,allocatable :: OOVV(:,:)
double precision,allocatable :: OVOV(:,:)
double precision,allocatable :: OVVO(:,:)
double precision,allocatable :: VVVV(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 22:22:47 +01:00
double precision,allocatable :: y(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 15:20:42 +01:00
double precision,allocatable :: r(:,:)
double precision,allocatable :: t(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 23:01:36 +01:00
integer :: n_diis
double precision :: rcond
double precision,allocatable :: error_diis(:,:)
double precision,allocatable :: t_diis(:,:)
2020-10-08 17:19:48 +02:00
double precision,external :: trace_matrix
2020-03-22 23:01:36 +01:00
2019-10-15 23:13:00 +02:00
! Hello world
write(*,*)
write(*,*)'**************************************'
2020-03-22 15:20:42 +01:00
write(*,*)'| pair CCD calculation |'
2019-10-15 23:13:00 +02:00
write(*,*)'**************************************'
write(*,*)
! Form energy denominator
2020-10-08 17:19:48 +02:00
allocate(eO(nO-nC),eV(nV-nR),delta_OV(nO-nC,nV-nR))
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
eO(:) = eHF(nC+1:nO)
eV(:) = eHF(nO+1:nBas-nR)
2020-03-24 12:28:56 +01:00
2020-10-08 17:19:48 +02:00
call form_delta_OV(nC,nO,nV,nR,eO,eV,delta_OV)
2019-10-15 23:13:00 +02:00
2020-03-22 15:20:42 +01:00
! Create integral batches
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
allocate(OOOO(nO-nC,nO-nC),OOVV(nO-nC,nV-nR),OVOV(nO-nC,nV-nR),OVVO(nO-nC,nV-nR),VVVV(nV-nR,nV-nR))
2020-03-24 12:28:56 +01:00
2020-10-08 17:19:48 +02:00
do i=1,nO-nC
do j=1,nO-nC
OOOO(i,j) = ERI(nC+i,nC+i,nC+j,nC+j)
2020-03-22 15:20:42 +01:00
end do
end do
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
do i=1,nO-nC
2020-03-22 22:22:47 +01:00
do a=1,nV-nR
2020-10-08 17:19:48 +02:00
OOVV(i,a) = ERI(nC+i,nC+i,nO+a,nO+a)
OVOV(i,a) = ERI(nC+i,nO+a,nC+i,nO+a)
OVVO(i,a) = ERI(nC+i,nO+a,nO+a,nC+i)
2020-03-22 15:20:42 +01:00
end do
end do
2019-10-15 23:13:00 +02:00
2020-03-22 22:22:47 +01:00
do a=1,nV-nR
do b=1,nV-nR
2020-03-22 15:20:42 +01:00
VVVV(a,b) = ERI(nO+a,nO+a,nO+b,nO+b)
end do
end do
2019-10-15 23:13:00 +02:00
! MP2 guess amplitudes
2020-10-08 17:19:48 +02:00
allocate(t(nO-nC,nV-nR))
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
t(:,:) = -0.5d0*OOVV(:,:)/delta_OV(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 23:01:36 +01:00
! Memory allocation for DIIS
2020-10-08 17:19:48 +02:00
allocate(error_diis((nO-nC)*(nV-nR),max_diis),t_diis((nO-nC)*(nV-nR),max_diis))
2020-03-22 23:01:36 +01:00
2019-10-15 23:13:00 +02:00
! Initialization
2020-10-08 17:19:48 +02:00
allocate(r(nO-nC,nV-nR),y(nO-nC,nO-nC))
2019-10-15 23:13:00 +02:00
Conv = 1d0
nSCF = 0
2020-03-22 23:01:36 +01:00
n_diis = 0
t_diis(:,:) = 0d0
error_diis(:,:) = 0d0
2019-10-15 23:13:00 +02:00
!------------------------------------------------------------------------
! Main SCF loop
!------------------------------------------------------------------------
write(*,*)
write(*,*)'----------------------------------------------------'
2020-03-21 22:50:43 +01:00
write(*,*)'| pair CCD calculation |'
2019-10-15 23:13:00 +02:00
write(*,*)'----------------------------------------------------'
write(*,'(1X,A1,1X,A3,1X,A1,1X,A16,1X,A1,1X,A10,1X,A1,1X,A10,1X,A1,1X)') &
2020-03-21 22:50:43 +01:00
'|','#','|','E(pCCD)','|','Ec(pCCD)','|','Conv','|'
2019-10-15 23:13:00 +02:00
write(*,*)'----------------------------------------------------'
do while(Conv > thresh .and. nSCF < maxSCF)
2020-03-22 15:20:42 +01:00
! Increment
2019-10-15 23:13:00 +02:00
nSCF = nSCF + 1
2020-03-22 15:20:42 +01:00
! Form intermediate array
2020-10-08 17:19:48 +02:00
y(:,:) = matmul(t,transpose(OOVV))
2020-03-22 15:20:42 +01:00
2020-03-22 23:01:36 +01:00
! Compute residual
2020-03-21 22:50:43 +01:00
2020-10-08 17:19:48 +02:00
do i=1,nO-nC
2020-03-22 22:22:47 +01:00
do a=1,nV-nR
2020-10-08 17:19:48 +02:00
r(i,a) = OOVV(i,a) + 2d0*delta_OV(i,a)*t(i,a) &
2020-03-22 22:22:47 +01:00
- 2d0*(2d0*OVOV(i,a) - OVVO(i,a) - OOVV(i,a)*t(i,a))*t(i,a)
2020-10-08 17:19:48 +02:00
do j=1,nO-nC
2020-03-24 12:28:56 +01:00
r(i,a) = r(i,a) - 2d0*OOVV(j,a)*t(j,a)*t(i,a) + OOOO(j,i)*t(j,a) + y(i,j)*t(j,a)
2020-03-22 22:22:47 +01:00
end do
do b=1,nV-nR
r(i,a) = r(i,a) - 2d0*OOVV(i,b)*t(i,b)*t(i,a) + VVVV(a,b)*t(i,b)
end do
2020-03-22 15:20:42 +01:00
end do
end do
2020-03-21 22:50:43 +01:00
2020-03-22 15:20:42 +01:00
! Check convergence
2020-03-21 22:50:43 +01:00
2020-03-22 15:20:42 +01:00
Conv = maxval(abs(r(:,:)))
2019-10-15 23:13:00 +02:00
2020-03-22 15:20:42 +01:00
! Update amplitudes
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
t(:,:) = t(:,:) - 0.5d0*r(:,:)/delta_OV(:,:)
2019-10-15 23:13:00 +02:00
2020-03-22 15:20:42 +01:00
! Compute correlation energy
2019-10-15 23:13:00 +02:00
2020-10-08 17:19:48 +02:00
EcCCD = trace_matrix(nO,matmul(t,transpose(OOVV)))
2019-10-15 23:13:00 +02:00
2020-03-22 23:01:36 +01:00
! Dump results
2019-10-15 23:13:00 +02:00
ECCD = ERHF + EcCCD
2020-03-22 23:01:36 +01:00
! DIIS extrapolation
n_diis = min(n_diis+1,max_diis)
2020-10-08 17:19:48 +02:00
call DIIS_extrapolation(rcond,nO*nV,nO*nV,n_diis,error_diis,t_diis,-0.5d0*r/delta_OV,t)
2020-03-22 23:01:36 +01:00
! Reset DIIS if required
if(abs(rcond) < 1d-15) n_diis = 0
2019-10-15 23:13:00 +02:00
write(*,'(1X,A1,1X,I3,1X,A1,1X,F16.10,1X,A1,1X,F10.6,1X,A1,1X,F10.6,1X,A1,1X)') &
'|',nSCF,'|',ECCD+ENuc,'|',EcCCD,'|',Conv,'|'
enddo
write(*,*)'----------------------------------------------------'
!------------------------------------------------------------------------
! End of SCF loop
!------------------------------------------------------------------------
! Did it actually converge?
if(nSCF == maxSCF) then
write(*,*)
write(*,*)'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
write(*,*)' Convergence failed '
write(*,*)'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
write(*,*)
stop
endif
2020-03-21 22:50:43 +01:00
end subroutine pCCD