-
Notifications
You must be signed in to change notification settings - Fork 1
/
VCA_EIGENSPACE.f90
569 lines (506 loc) · 18.5 KB
/
VCA_EIGENSPACE.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
module VCA_EIGENSPACE
USE VCA_VARS_GLOBAL
USE VCA_SETUP
implicit none
private
type sparse_estate
integer :: sector !index of the sector
real(8) :: e !energy of the eigen-state
complex(8),dimension(:),allocatable :: cvec !double precision eigen-vector
logical :: itwin=.false. !twin sector label
type(sparse_estate),pointer :: twin=>null() !link to twin box
type(sparse_estate),pointer :: next=>null() !link to next box (chain)
end type sparse_estate
type sparse_espace
integer :: size
real(8) :: emax,emin
logical :: status=.false.
type(sparse_estate),pointer :: root=>null() !head/root of the list\== list itself
end type sparse_espace
interface es_insert_state
module procedure :: es_insert_state_c
end interface es_insert_state
interface es_add_state
module procedure :: es_add_state_c
end interface es_add_state
interface es_return_cvector
module procedure :: es_return_cvector_default
#ifdef _MPI
module procedure :: es_return_cvector_mpi
#endif
end interface es_return_cvector
public :: sparse_estate
public :: sparse_espace
!
public :: es_init_espace !init the espace !checked
public :: es_delete_espace !del the espace !checked
public :: es_free_espace !free the espace !checked
!public :: es_print_espace !print the espace !checked
!
public :: es_insert_state !insert a state !checked
public :: es_add_state !add a state w/ costraint !checked
public :: es_pop_state !pop a state !checked
!
public :: es_return_sector !get the sector of a state !checked
public :: es_return_energy !get the energy of a state !checked
public :: es_return_cvector !get the vector of a state !checked
public :: es_return_gs_degeneracy !get the number of degenerate GS !checked
!
type(sparse_espace),public :: state_list
!
contains !some routine to perform simple operation on the lists
!+------------------------------------------------------------------+
!PURPOSE : initialize the list of states
!+------------------------------------------------------------------+
function es_init_espace() result(space)
type(sparse_espace) :: space
allocate(space%root)
space%status=.true.
space%root%next => null()
space%size=0
space%emax=-huge(1d0)
space%emin= huge(1d0)
end function es_init_espace
!+------------------------------------------------------------------+
!PURPOSE : destroy the list of states
!+------------------------------------------------------------------+
subroutine es_delete_espace(space)
type(sparse_espace),intent(inout) :: space
type(sparse_estate),pointer :: p,c
!
if(.not.space%status)return
do
p => space%root
c => p%next
if(.not.associated(c))exit !empty list
p%next => c%next !
c%next=>null()
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
end do
deallocate(space%root)
space%status=.false.
p=>null()
c=>null()
end subroutine es_delete_espace
!+------------------------------------------------------------------+
!PURPOSE : empty the list of states
!+------------------------------------------------------------------+
subroutine es_free_espace(space)
type(sparse_espace),intent(inout) :: space
type(sparse_estate),pointer :: p,c
do
p => space%root
c => p%next
if(.not.associated(c))exit !empty list
p%next => c%next !
c%next=>null()
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
end do
space%size=0
space%emax=-huge(1.d0)
space%emin=huge(1.d0)
p=>null()
c=>null()
end subroutine es_free_espace
!+------------------------------------------------------------------+
!PURPOSE : insert a state into the list using ener,vector,sector
!+------------------------------------------------------------------+
subroutine es_add_state_c(espace,e,cvec,sector,twin,size,verbose)
type(sparse_espace),intent(inout) :: espace
real(8),intent(in) :: e
complex(8),dimension(:),intent(in) :: cvec
integer,intent(in) :: sector
integer,intent(in),optional :: size
logical,intent(in),optional :: verbose
logical,intent(in),optional :: twin
logical :: twin_
twin_=.false.;if(present(twin))twin_=twin
if(present(size))then !if present size add respecting the size constraint.
if(espace%size<size)then
call es_insert_state_c(espace,e,cvec,sector,twin_)
else
if(e < es_return_energy(espace))then
if(present(verbose).AND.(verbose.eqv..true.))print*,"found a new state:"
call es_pop_state(espace)
call es_insert_state_c(espace,e,cvec,sector,twin_)
endif
endif
else !else add normally
call es_insert_state_c(espace,e,cvec,sector,twin_)
endif
end subroutine es_add_state_c
!+------------------------------------------------------------------+
!PURPOSE : insert a state into the list using ener,vector,sector
!+------------------------------------------------------------------+
subroutine es_insert_state_c(space,e,vec,sector,twin)
type(sparse_espace),intent(inout) :: space
real(8),intent(in) :: e
complex(8),dimension(:),intent(in) :: vec
integer,intent(in) :: sector
logical :: twin
type(sparse_estate),pointer :: p,c
p => space%root
c => p%next
do !traverse the list until e < value (ordered list)
if(.not.associated(c))exit
if(e <= c%e)exit
p => c
c => c%next
end do
!
allocate(p%next) !Create a new element in the list
p%next%e = e
if(e > space%emax)space%emax=e !update the max energy (corresponds to the top entry)
if(e < space%emin)space%emin=e !update the min energy (corresponds to the first entry)
allocate(p%next%cvec(size(vec)))
p%next%cvec = vec
p%next%itwin=.false.
p%next%sector=sector
space%size = space%size+1
if(twin)then !Create a twin element in the list with same energy, no vector and twin flag T
allocate(p%next%next)
p%next%next%e = e
p%next%next%itwin=.true.
p%next%next%sector=get_twin_sector(sector)
p%next%next%twin => p%next ! wiggled arrow of the twin_wout_vector points to its twin_w_vector
p%next%twin => p%next%next ! wiggled arrow of the twin_w_vector points to its twin_wout_vector
space%size = space%size+1
endif
if(.not.associated(c))then !end of the list special case (current=>current%next)
if(.not.twin)then
p%next%next => null()
else
p%next%next%next => null()
endif
else
if(.not.twin)then
p%next%next => c !the %next of the new node come to current
else
p%next%next%next => c
endif
end if
p=>null()
c=>null()
end subroutine es_insert_state_c
!+------------------------------------------------------------------+
!PURPOSE : remove last element from the list, if +n is given remove
! the n-th element, if +e is given remove the state with state%e=e
! hint: CIRCLE = twin state (a state flagged with itwin=T bearing no vector)
! SQUARE = normal state (itwin=F bearing vector)
!+------------------------------------------------------------------+
subroutine es_pop_state(space,n)
type(sparse_espace),intent(inout) :: space
integer,optional,intent(in) :: n
integer :: i,pos
type(sparse_estate),pointer :: pp,p,c
!
pos= space%size ; if(present(n))pos=n
!
if(pos>space%size)stop "es_pop_state: pos > espace.size"
if(space%size==0)stop "es_pop_state: empty list"
pp => null()
p => null()
c => space%root
do i=1,pos
pp => p
p => c
c => c%next
if(.not.associated(c))return !empty or end of the list
end do
!c is a circle, so the prev/next are necessarily squares: remove c and p (twins)
if(c%itwin)then
pp%next => c%next
!delete C
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
!delete P
if(allocated(p%cvec))deallocate(p%cvec)
if(associated(p%twin))p%twin=>null()
deallocate(p)
p => pp
space%size=space%size-2
else
!c is a square:
!if c%next is associated:
! if it is a circle: delete c and c%next (twins)
! if it is a square: delete c
!else c%next is not associated: delete c
if(associated(c%next))then
if(c%next%itwin)then
p%next => c%next%next
!delete C
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
!delete C%NEXT
if(allocated(c%next%cvec))deallocate(c%next%cvec)
if(associated(c%next%twin))c%next%twin=>null()
deallocate(c%next)
space%size=space%size-2
else
p%next => c%next
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
space%size=space%size-1
endif
else
p%next => c%next
if(allocated(c%cvec))deallocate(c%cvec)
if(associated(c%twin))c%twin=>null()
deallocate(c)
space%size=space%size-1
endif
endif
if(space%size>0)then
! ! space%root%next => null()
! ! space%size=0
! ! space%emax=-huge(1.d0)
! ! space%emin=huge(1.d0)
! ! else
! if(pos==space%size)then !pop last term carrying e=emax, update emax
! space%emax = p%e
! elseif(pos==1)then !pop first term carrying e=emin, update emin
! space%emin = p%e
! endif
space%emax = es_return_energy(space,space%size)
space%emin = es_return_energy(space,1)
endif
pp=>null()
p =>null()
c =>null()
end subroutine es_pop_state
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
function es_return_gs_degeneracy(space,gsthreshold) result(numzero)
type(sparse_espace),intent(in) :: space
real(8),optional :: gsthreshold
real(8) :: gsthreshold_
integer :: numzero,pos
type(sparse_estate),pointer :: c
real(8) :: oldzero,enemin
gsthreshold_=1.d-9;if(present(gsthreshold))gsthreshold_=gsthreshold
if(.not.space%status) stop "es_return_gs_degeneracy: espace not allocated"
oldzero=1000.d0
numzero=0
c => space%root
pos=0
do
c => c%next
pos=pos+1
if(.not.associated(c))exit !end of the list
enemin = c%e
if (enemin < oldzero-10.d0*gsthreshold_) then
numzero=1
oldzero=enemin
elseif(abs(enemin-oldzero) <= gsthreshold_)then
numzero=numzero+1
oldzero=min(oldzero,enemin)
endif
end do
end function es_return_gs_degeneracy
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
function es_return_sector(space,n) result(sector)
type(sparse_espace),intent(in) :: space
integer,optional,intent(in) :: n
integer :: sector
type(sparse_estate),pointer :: c
integer :: i,pos
if(.not.space%status) stop "es_return_sector: espace not allocated"
pos= space%size ; if(present(n))pos=n
if(pos>space%size) stop "es_return_sector: n > espace.size"
sector=0
c => space%root
do i=1,pos
c => c%next
if(.not.associated(c))exit
end do
if(space%size==0)return
sector = c%sector
c=>null()
end function es_return_sector
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
function es_return_energy(space,n) result(egs)
type(sparse_espace),intent(in) :: space
integer,optional,intent(in) :: n
real(8) :: egs
type(sparse_estate),pointer :: c
integer :: i,pos
if(.not.space%status) stop "es_return_energy: espace not allocated"
pos= space%size ; if(present(n))pos=n
if(pos>space%size) stop "es_return_energy: n > espace.size"
c => space%root
egs=space%emax
do i=1,pos
c => c%next
if(.not.associated(c))exit
end do
if(space%size==0)return
if(.not.c%itwin)then
egs = c%e
else
egs = c%twin%e
endif
end function es_return_energy
!+------------------------------------------------------------------+
!PURPOSE :
!+------------------------------------------------------------------+
subroutine es_return_cvector_default(space,n,vector)
type(sparse_espace),intent(in) :: space
integer,optional,intent(in) :: n
complex(8),dimension(:),allocatable :: vector
type(sparse_estate),pointer :: c
integer :: i,pos
integer :: dim
integer,dimension(:),allocatable :: order
!
if(.not.space%status) stop "es_return_cvector ERRROR: espace not allocated"
pos= space%size ; if(present(n))pos=n
if(pos>space%size) stop "es_return_cvector ERRROR: n > espace.size"
if(space%size==0)stop "es_return_cvector ERRROR: espace emtpy"
!
c => space%root
do i=1,pos
c => c%next
if(.not.associated(c))exit
end do
!
dim = getdim(c%sector)
allocate(vector(dim));vector=zero
!
if(.not.c%itwin)then
vector = c%cvec
else
allocate(Order(dim))
call twin_sector_order(c%twin%sector,Order)
do i=1,dim
vector(i) = c%twin%cvec(Order(i))
enddo
deallocate(order)
endif
end subroutine es_return_cvector_default
#ifdef _MPI
subroutine es_return_cvector_mpi(MpiComm,space,n,vector)
integer :: MpiComm
type(sparse_espace),intent(in) :: space
integer,optional,intent(in) :: n
complex(8),dimension(:),allocatable :: vtmp
complex(8),dimension(:),allocatable :: vector
type(sparse_estate),pointer :: c
integer :: i,pos,Nloc,Ndim
integer :: dim,ierr
logical :: MpiMaster
integer,dimension(:),allocatable :: order
!
if(MpiComm==MPI_COMM_NULL)return
if(MpiComm==MPI_UNDEFINED)stop "es_return_cvector ERRROR: MpiComm = MPI_UNDEFINED"
!
if(.not.space%status) stop "es_return_cvector ERRROR: espace not allocated"
pos= space%size ; if(present(n))pos=n
if(pos>space%size) stop "es_return_cvector ERRROR: n > espace.size"
if(space%size==0)stop "es_return_cvector ERRROR: espace emtpy"
!
c => space%root
do i=1,pos
c => c%next
if(.not.associated(c))exit
end do
!
if(.not.c%itwin)then
Nloc = size(c%cvec)
else
Nloc = size(c%twin%cvec)
endif
!
!
!Ensure that the sum of the dimension of all vector chunks equals the sector dimension.
Dim = getdim(c%sector)
Ndim = 0
call Allreduce_MPI(MpiComm,Nloc,Ndim)
if(Dim/=Ndim)stop "es_return_cvector ERROR: Dim != Ndim from v chunks"
!
MpiMaster = get_master_MPI(MpiComm)
!
if(.not.c%itwin)then
if(MpiMaster)then
allocate(Vector(Ndim))
else
allocate(Vector(1))
endif
Vector = zero
call gather_vector_MPI(MpiComm,c%cvec,Vector)
else
!
if(MpiMaster)then
allocate(Vtmp(Ndim))
allocate(Order(Dim))
call twin_sector_order(c%twin%sector,Order)
else
allocate(Vtmp(1))
endif
Vtmp = zero
call gather_vector_MPI(MpiComm,c%twin%cvec,Vtmp)
if(MpiMaster)then
allocate(Vector(Ndim))
forall(i=1:Dim)Vector(i) = Vtmp(Order(i))
deallocate(Order)
else
allocate(Vector(1))
Vector = zero
endif
deallocate(Vtmp)
endif
if(associated(c))nullify(c)
end subroutine es_return_cvector_mpi
#endif
!+------------------------------------------------------------------+
!PURPOSE : pretty print the list of states
!+------------------------------------------------------------------+
!subroutine es_print_espace(space,unit,wvec)
! type(sparse_espace),intent(in) :: space
! type(sparse_estate),pointer :: c
! integer :: counter,i
! integer,optional :: unit
! integer :: unit_
! logical,optional :: wvec
! logical :: wvec_
! wvec_=.false.;if(present(wvec))wvec_=wvec
! unit_=6;if(present(unit))unit_=unit
! write(*,"(A,I3)")"Print sparse espace unit ->",unit_
! c => space%root%next !assume is associated,ie list exists
! counter = 0
! if(space%size>0)then
! do
! if(.not.associated(c))exit
! counter=counter+1
! write(unit_,"(A10,I5)") "Index : ",counter
! write(unit_,"(A10,I5)") "Sector : ",c%sector
! write(unit_,"(A10,3L3)") "Twin : ",c%itwin,associated(c%cvec)
!! write(unit_,"(A10,I5)") "Size : ",getdim(c%sector)!size(c%vec)
! write(unit_,"(A10,3f18.9)")"Energy : ",c%e,space%emax,space%emin
! if(wvec_)then
! write(unit_,"(A10)")"Vec : "
! do i=1,size(c%cvec)
! write(unit_,*)c%cvec(i)
! enddo
! endif
! c => c%next !traverse list
! write(unit_,*)""
! end do
! else
! write(unit_,*)"Empty space"
! return
! endif
! c=>null()
!end subroutine es_print_espace
end module VCA_EIGENSPACE