-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsrpapack.f
executable file
·356 lines (310 loc) · 9.94 KB
/
srpapack.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
! This file is part of std2.
!
! Copyright (C) 2013-2025 Stefan Grimme and Marc de Wergifosse
!
! std2 is free software: you can redistribute it and/or modify it under
! the terms of the GNU Lesser General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! std2 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 Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License
! along with std2. If not, see <https://www.gnu.org/licenses/>.
!
!! ------------------------------------------------------------------------
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c sRPA routine c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c amb: A - B (packed form) c
c apb: A + B (packed form) c
c ambsqr: (A - B)**0.5 (packed form c
c omsq: omega**2 - eigenvalue belonging to Z c
c xpy: X + Y, (later xpy = x) c
c xmy: X - Y, (later xmy = y) c
c n: number of configurations in A and B matrices c
c nroots: number of roots c
c ggavec : print z vector c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccc
subroutine srpapack(n,thr,ambsqr,apb,eci,xpy,xmy,nroots,ggavec)
use commonlogicals
implicit none
integer ierror,n,nroots,k,i,j,ij,m,nro,lin
integer lwork,liwork,il,iu,info,nfound
c matrices
real*4 ambsqr(n*(n+1)/2),xpy(n,nroots)
real*4 apb(n*(n+1)/2),xmy(n,nroots)
real*4 eci(n)
real*4 summe,x,y,omsqi,vl,vu
real*8 thr
integer, allocatable ::iwork(:),isuppz(:)
real*4, allocatable ::u(:,:),v(:,:),w(:,:)
real*4, allocatable ::z(:,:)
real*4, allocatable ::work(:)
real*4, allocatable ::e (:)
logical ggavec
! allocate(ambsqr(n*(n+1)/2),
allocate(e(n),u(n,n),v(n,n),w(n,n),stat=ierror)
if(ierror.ne.0) stop 'allocation error (rpasolve)'
call sblow(n,ambsqr,U) ! blow up sqrt(A - B) from vector to matrix
call sblow(n,apb ,V) ! blow up A + B from vector to matrix
! call prmat4(6,U ,n,n,'(A-B)^0.5') ! for debugging: print ambsqr
! call prmat4(6,V ,n,n,' A+B ') ! for debugging: print apb
c form product: w = (a+b)*(a-b)^0.5
write(*,*) ' calculating (A+B)*(A-B)^0.5 ...'
call ssymm('l','l',n,n,1.e0,V,n,U,n,0.e0,W,n)
! call prmat4(6,W,n,n,'(a+b)*(a-b)^0.5') ! for debugging: print w
c form product: v = (a-b)^0.5 * w
call ssymm('l','l',n,n,1.e0,U,n,W,n,0.e0,V,n)
! call prmat4(6,V,n,n,'M') ! for debugging: print M matrix (V)
c get rid of matrices which are not needed anymore
deallocate(u,w,stat=ierror)
c set variables for RPA diagonalization
lwork =26*n
liwork=10*n
vl=0
vu=thr**2.0 ! set nroot threshold to Ethr^2
allocate(z(n,n),work(lwork)
. ,iwork(liwork),isuppz(n),stat=ierror)
if(ierror.ne.0) stop 'allocation error (rpasolve)'
write(*,*)'calculate eigenvalues of (A-B)^0.5*(A+B)*(A-B)^0.5 ...'
call ssyevr('V','V','U',n,v,n,vl,vu,il,iu,1.e-6,
. nfound,e,z,n,isuppz,
. work,lwork,iwork,liwork,info)
nroots=nfound
if(info.ne.0.or.nroots.lt.1) stop 'RPA diag failed'
deallocate(v,work,iwork,isuppz,stat=ierror)
if(ierror.ne.0) stop 'deallocation after RPA diag failed'
! testing print Z
! call prmat4(6,z,n,n,'Z')
if((TPA .eqv. .false.) .and. (FULL2PA .eqv. .false.))then
ij=0
do i=1,n
ij=ij+i
ambsqr(ij)=ambsqr(ij)*0.5
apb(ij)=apb(ij)*0.5
enddo
endif
do nro=1,nroots
eci(nro)=sqrt(e(nro))
c (A-B)^0.5 * Z = X+Y ! results from conversion to Hermitian eigenvalue problem
do m=1,n
xpy(m,nro)=0.0
enddo
k=0
do i=1,n
do j=1,i
k=k+1
xpy(i,nro)=xpy(i,nro)+ambsqr(k)*z(j,nro)/sqrt(eci(nro)) ! dividing by sqrt(eci) yields correct norm
xpy(j,nro)=xpy(j,nro)+ambsqr(k)*z(i,nro)/sqrt(eci(nro))
enddo
enddo
c (A+B)*|X+Y> = e * (X-Y) ! first row of TD-DFT equation
do m=1,n
xmy(m,nro)=0.0
enddo
k=0
do i=1,n
do j=1,i
k=k+1
xmy(i,nro)=xmy(i,nro)+apb(k)*xpy(j,nro)
xmy(j,nro)=xmy(j,nro)+apb(k)*xpy(i,nro)
enddo
enddo
c write(*,'(''x+y'',10f8.4)')(xpy(i,nro),i=1,n)
c write(*,'(''x-y'',10f8.4)')(xmy(i,nro),i=1,n)
summe=0.0
do i=1,n
xmy(i,nro)=xmy(i,nro)/eci(nro)
x=(xmy(i,nro)+xpy(i,nro))*0.5
y=xpy(i,nro)-x
xpy(i,nro)=x !xpy is now x
xmy(i,nro)=y !xmy is now y
! summe=summe+xpy(i,nro)**2-xmy(i,nro)**2
enddo
c write(*,'(''x '',10f8.4)')(xpy(i,nro),i=1,n)
c write(*,'(''y '',10f8.4)')(xmy(i,nro),i=1,n)
c write(*,*) 'norm',summe
c write(*,*) 'e ',eci(nro)
c write(*,'(''x+y'',10f8.4)')(xpy(i,nro),i=1,n)
! norm the vectors
! summe=1.0/sqrt(summe)
! do i=1,n
! xpy(i,nro)=xpy(i,nro)*summe
! xmy(i,nro)=xmy(i,nro)*summe
! enddo
c write(38)xpy
c write(38)xmy
enddo
c close(36)
write(*,*)' rpa vectors ok'
! call prmat4(6,xpy,n,nro,'X')
! call prmat4(6,xmy,n,nro,'Y')
! internal check for orthonormality
! nro=nroots
! z=0.0
! call sgemm('T','n',n,nro,n,1.d0,xpy,n,xpy,n,0.d0,z,n)
! call sgemm('T','N',n,nro,n,-1.0d0,xmy,n,xmy,n,1.0d0,z,n)
! do i=1,min(12,nro)
! write(6,'(12f10.6)') (z(j,i),j=1,min(12,nro))
! enddo
! write(6,*)
! do i=max(1,nro-11),nro
! write(6,'(12f10.6)') (z(j,i),j=max(1,nro-11),nro)
! enddo
if (ggavec) then
call printvectda(ggavec,n,nroots,z,e)
endif
deallocate(z,stat=ierror)
c deallocate(ambsqr,w5,z,stat=ierror)
return
end
c----------------------------------------------------------------------
c subroutine to take the power of a matrix (used here for (A-B)**0.5)
subroutine smatpow(n,a)
use omp_lib
implicit none
integer n,i,ierror,m,j,k,info
real*4 a(n*(n+1)/2)
real*4 summe
real*4, allocatable ::c(:)
real*4, allocatable ::e(:)
real*4, allocatable ::w(:)
c working variables for sspevd diagonalization routine
integer lwork,liwork,lin
integer, allocatable ::iwork(:)
lwork =1 + 6*n + n**2
liwork=3 + 5*n
allocate(iwork(liwork),stat=ierror)
if(ierror.ne.0) stop 'allocation error (iwork in matpow)'
c allocate(c(n*n),w(n*5),e(n),stat=ierror)
allocate(c(n*n),w(lwork),e(n),stat=ierror)
if(ierror.ne.0) stop 'allocation error (matpow)'
c call shqrii(a,n,n,w,e,c) ! old routine - not used
c call sspev('V','U',n,a,e,c,n,w,info) ! alternative LAPACK routine (not used)
c used LAPACK routine using divide-and-conquer algorithm (slightly faster than sspev)
call sspevd('V','U',n,a,e,c,n,w,lwork,iwork,liwork,info)
if(e(1).lt.0) stop 'matrix power impossible'
c take square root of diagonal elements
do i=1,n
e(i)=sqrt(e(i))
enddo
a=0.0e0
c transform back from diagonal to non-diagonal form
m=0
!$omp parallel private(i,j,m,k,summe)
!$omp do
do i=1,n
do j=1,i
summe=0.0
m=lin(i,j)
do k=1,n
summe=summe+c(i+(k-1)*n)*e(k)*c(j+(k-1)*n)
enddo
a(m)=summe
enddo
enddo
!$omp end do
!$omp end parallel
c deallocate(c,w,e,stat=ierror)
deallocate(c,w,e,iwork,stat=ierror)
return
end
subroutine sblow(n,a,b)
c blow up symmetric matrix to full size
implicit none
real*4 a(n*(n+1)/2),b(n,n)
integer ij,i,n,j,lin
ij=0
do i=1,n
do j=1,i-1
ij=ij+1
b(j,i)=a(ij)
b(i,j)=a(ij)
enddo
ij=ij+1
b(i,i)=a(ij)
enddo
return
end
subroutine sblow_fast(n,a,b)
c blow up symmetric matrix to full size
implicit none
real*4 a(n*(n+1)/2),b(n,n)
integer ij,i,n,j,lin
!$omp parallel private(i,j,ij)
!$omp do
do i=1,n
do j=1,i-1
ij=lin(i,j)
b(j,i)=a(ij)
b(i,j)=a(ij)
enddo
ij=lin(i,i)
b(i,i)=a(ij)
enddo
!$omp end do
!$omp end parallel
return
end
subroutine sUnblow_fast(n,a,b)
c blow up symmetric matrix to full size
implicit none
real*4 b(n*(n+1)/2),a(n,n)
integer ij,i,n,j,lin
!$omp parallel private(i,j,ij)
!$omp do
do i=1,n
do j=1,i-1
ij=lin(i,j)
b(ij)=a(j,i)
enddo
ij=lin(i,i)
b(ij)=a(i,i)
enddo
!$omp end do
!$omp end parallel
return
end
subroutine dblow_fast(n,a,b)
c blow up symmetric matrix to full size
implicit none
real*8 a(n*(n+1)/2),b(n,n)
integer ij,i,n,j,lin
!$omp parallel private(i,j,ij)
!$omp do
do i=1,n
do j=1,i-1
ij=lin(i,j)
b(j,i)=a(ij)
b(i,j)=a(ij)
enddo
ij=lin(i,i)
b(i,i)=a(ij)
enddo
!$omp end do
!$omp end parallel
return
end
subroutine dUnblow_fast(n,a,b)
c blow up symmetric matrix to full size
implicit none
real*8 b(n*(n+1)/2),a(n,n)
integer ij,i,n,j,lin
!$omp parallel private(i,j,ij)
!$omp do
do i=1,n
do j=1,i-1
ij=lin(i,j)
b(ij)=a(j,i)
enddo
ij=lin(i,i)
b(ij)=a(i,i)
enddo
!$omp end do
!$omp end parallel
return
end