-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleColExp_test.f90
274 lines (235 loc) · 7.25 KB
/
SingleColExp_test.f90
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
! MIT License
!
! Copyright (c) 2020-2021 Florian Goth
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
! OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
! THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
! FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! DEALINGS IN THE SOFTWARE.
program singlecolexptest
use MvG_mod
use Exponentials_mod
implicit none
integer :: ndim, i, j, k, n, myl
integer :: nredges, dn, IERR, incx, seed
real(kind=kind(0.D0)) :: hop, r
complex (kind=kind(0.d0)), ALLOCATABLE, DIMENSION(:,:) :: A !< the full matrix A
complex (kind=kind(0.d0)), ALLOCATABLE, DIMENSION(:,:) :: U, Identity, M1,M2, M3, ref !< A temporary matrix
complex (kind=kind(0.d0)), ALLOCATABLE, DIMENSION(:) :: vec, lwork, rwork, res, res2 !< the vector that we will test on
real(kind=kind(0.D0)), allocatable, dimension(:) :: energ
type(GraphData) :: gd
type(EulerExp) :: ee
real(kind=kind(0.D0)) :: dznrm2, zlange
integer, allocatable, dimension(:) :: seedarr
complex(kind=kind(0.D0)) :: alpha, beta
! First create some test matrix
!
! initialize A with some data
hop = 0.05
! corresponds to chain with next-nearest neighbour hopping and OBC
ndim = 6
allocate(A(ndim, ndim))
A=0
do I = 1, ndim-1, 1
A(I,I+1) = hop
A(i+1, i) = hop
if (i+2 <= ndim) then
A(I,I+2) = hop
A(i+2, i) = hop
endif
enddo
allocate(U(ndim, ndim), vec(ndim), energ(ndim), M1(ndim, ndim), M2(ndim, ndim), M3(ndim,ndim), Identity(ndim, ndim))
Allocate(ref(ndim,ndim))
! convert to the internal GraphData structure
!
gd = mat2verts(A)
! perform the actual color decomposition
!
call MvG_decomp(gd%verts)
! Output some useful information
!
! Determine the number of used colors and the number of edges
gd%usedcolors = 0
gd%nredges = 0
do i = 1, gd%ndim
gd%deltag = max(gd%deltag, gd%verts(i)%degree)
do k = 1, gd%verts(i)%degree
if (gd%verts(i)%nbrs(k) > i) gd%nredges = gd%nredges + 1
if (gd%verts(i)%nbrs(k) > gd%ndim) then
write(*,*) "invalid nbr!!!"
STOP
endif
gd%usedcolors = max(gd%usedcolors, gd%verts(i)%cols(k))
enddo
enddo
write (*,*) "Nr edges: ", gd%nredges
if (gd%usedcolors == gd%deltag) then
write(*,*) "Maximum Degree", gd%deltag, ". Found", gd%usedcolors," Families -> optimal decomposition"
else
write(*,*) "Maximum Degree", gd%deltag, ". Found", gd%usedcolors," Families"
endif
! create an Exponential from the color information and the weights of the graph
!
ee = createEulerExponentialfromGraphData(gd)
! Now follows some testing and the comparison to straight-forward exponentiation via lapack
!
! Let's retrieve the entries from the first color
U = 0
do i = 1, ee%singleexps(1)%nrofentries
! U(ee%singleexps(1)%x(i), ee%singleexps(1)%y(i)) = acosh(ee%singleexps(1)%c(i))
! U(ee%singleexps(1)%y(i), ee%singleexps(1)%x(i)) = acosh(ee%singleexps(1)%c(i))
enddo
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
do i=1,ndim
write (*,*) (dble(U(i,j)), j=1,ndim)
enddo
! Test 1 - check that inversion brings us close to the identity...
call ee%singleexps(1)%lmult(Identity)
call ee%singleexps(1)%lmultinv(Identity)
write (*,*) "========"
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
! Test 2 - the same for rmult:
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
call ee%singleexps(1)%rmult(Identity)
call ee%singleexps(1)%rmultinv(Identity)
write (*,*) "========"
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
! Test 3 - compare to output of lapack routines
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
dn = 3*ndim
allocate(lwork(dn), rwork(dn))
M1 = U
call zheev('V', 'U', ndim, M1, ndim, energ, lwork, dn, rwork, IERR)
energ = exp(energ)
! apply to Identity
alpha = 1.D0
beta = 0.D0
call ZGEMM('C', 'N', ndim, ndim, ndim, alpha, M1, ndim, Identity, ndim, beta, M2, ndim)
do i = 1, ndim
do j = 1, ndim
M2(i, j) = M2(i, j) * energ(i)
enddo
enddo
call ZGEMM('N', 'N', ndim, ndim, ndim, alpha, M1, ndim, M2, ndim, beta, Identity, ndim)
write (*,*) "========"
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
! Test 4 - test the Euler type approximations
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
call ee%lmult_T(Identity)
call ee%lmult(Identity)
write (*,*) "======== Euler lmult ===="
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
! and from the right
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
call ee%rmult(Identity)
call ee%rmult_T(Identity)
write (*,*) "======== Euler rmult ===="
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
M3 = Identity
! compare with lapack
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
M1 = 2*A
call zheev('V', 'U', ndim, M1, ndim, energ, lwork, dn, rwork, IERR)
energ = exp(energ)
! apply to Identity
alpha = 1.D0
beta = 0.D0
call ZGEMM('C', 'N', ndim, ndim, ndim, alpha, M1, ndim, Identity, ndim, beta, M2, ndim)
do i = 1, ndim
do j = 1, ndim
M2(i, j) = M2(i, j) * energ(i)
enddo
enddo
call ZGEMM('N', 'N', ndim, ndim, ndim, alpha, M1, ndim, M2, ndim, beta, ref, ndim)
write (*,*) "======== ref ======"
do i=1,ndim
write (*,*) (dble(ref(i,j)), j=1,ndim)
enddo
M3 = M3 - Ref
write (*,*) zlange('F', ndim, ndim, M3, ndim, lwork)
! test adjoint over two
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
call ee%singleexps(1)%adjoint_over_two(Identity)
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
Identity=0
do i = 1, ndim
Identity(i,i) = 1
enddo
write (*,*) ee%nrofcols
!call ee%singleexps(1)%lmult(Identity)
!call ee%singleexps(1)%rmultinv(Identity)
do k = 1,4
! call ee%adjoint_over_two(Identity)
! call ee%adjointaction(Identity)
call ee%singleexps(1)%adjoint_over_two(Identity)
! do i=1,ndim
! write (*,*) (dble(Identity(i,j)), j=1,ndim)
! enddo
call ee%singleexps(2)%adjoint_over_two(Identity)
! do i=1,ndim
! write (*,*) (dble(Identity(i,j)), j=1,ndim)
! enddo
call ee%singleexps(3)%adjoint_over_two(Identity)
! do i=1,ndim
! write (*,*) (dble(Identity(i,j)), j=1,ndim)
! enddo
call ee%singleexps(4)%adjoint_over_two(Identity)
! do i=1,ndim
! write (*,*) (dble(Identity(i,j)), j=1,ndim)
! enddo
enddo
do i=1,ndim
write (*,*) (dble(Identity(i,j)), j=1,ndim)
enddo
do i = 1, gd%ndim
call gd%verts(i)%destruct()
enddo
call ee%dealloc()
deallocate(U, vec, energ, M1, M2, M3, gd%verts, gd%elems)
end program singlecolexptest