-
Notifications
You must be signed in to change notification settings - Fork 3
/
davidson.f90
761 lines (588 loc) · 26.1 KB
/
davidson.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
!> \namespace Davidson eigensolver
!> \author Felipe Zapata
!> The current implementation uses a general davidson algorithm, meaning
!> that it compute all the eigenvalues simultaneusly using a variable size block approach.
!> The family of Davidson algorithm only differ in the way that the correction
!> vector is computed.
!> Computed pairs of eigenvalues/eigenvectors are deflated using algorithm
!> described at: https://doi.org/10.1023/A:101919970
module davidson_dense
!> Submodule containing the implementation of the Davidson diagonalization method
!> for dense matrices
use numeric_kinds, only: dp
use lapack_wrapper, only: lapack_generalized_eigensolver, lapack_matmul, lapack_matrix_vector, &
lapack_qr, lapack_solver, lapack_sort
use array_utils, only: concatenate, diagonal, eye, generate_preconditioner, norm
implicit none
!> \private
private
!> \public
public :: generalized_eigensolver_dense
interface
module function compute_correction_generalized_dense(mtx, V, eigenvalues, eigenvectors, method, stx) &
result(correction)
!> compute the correction vector using a given `method` for the Davidson algorithm
!> See correction_methods submodule for the implementations
!> \param[in] mtx: Original matrix
!> \param[in] stx: Matrix to compute the general eigenvalue problem
!> \param[in] V: Basis of the iteration subspace
!> \param[in] eigenvalues: of the reduce problem
!> \param[in] eigenvectors: of the reduce problem
!> \param[in] method: name of the method to compute the correction
real(dp), dimension(:), intent(in) :: eigenvalues
real(dp), dimension(:, :), intent(in) :: mtx, V, eigenvectors
real(dp), dimension(:, :), intent(in), optional :: stx
character(len=*), optional, intent(in) :: method
real(dp), dimension(size(mtx, 1), size(V, 2)) :: correction
end function compute_correction_generalized_dense
end interface
contains
subroutine generalized_eigensolver_dense(mtx, eigenvalues, ritz_vectors, lowest, method, max_iters, &
tolerance, iters, max_dim_sub, stx)
!> Implementation storing in memory the initial densed matrix mtx.
!> \param[in] mtx: Matrix to diagonalize
!> \param[in, opt] Optional matrix to solve the general eigenvalue problem:
!> \f$ mtx \lambda = V stx \lambda \f$
!> \param[out] eigenvalues Computed eigenvalues
!> \param[out] ritz_vectors approximation to the eigenvectors
!> \param[in] lowest Number of lowest eigenvalues/ritz_vectors to compute
!> \param[in] method Method to compute the correction vector. Available
!> methods are,
!> DPR: Diagonal-Preconditioned-Residue
!> GJD: Generalized Jacobi Davidson
!> \param[in] max_iters: Maximum number of iterations
!> \param[in] tolerance norm-2 error of the eigenvalues
!> \param[in] method: Method to compute the correction vectors
!> \param[in, opt] max_dim_sub: maximum dimension of the subspace search
!> \param[out] iters: Number of iterations until convergence
!> \return eigenvalues and ritz_vectors of the matrix `mtx`
implicit none
! input/output variable
integer, intent(in) :: lowest
real(dp), dimension(:, :), intent(in) :: mtx
real(dp), dimension(:, :), intent(in), optional :: stx
real(dp), dimension(lowest), intent(out) :: eigenvalues
real(dp), dimension(:, :), intent(out) :: ritz_vectors
integer, intent(in) :: max_iters
integer, intent(in), optional :: max_dim_sub
real(dp), intent(in) :: tolerance
character(len=*), intent(in) :: method
integer, intent(out) :: iters
!local variables
integer :: i, j, dim_sub, max_dim
integer :: n_converged ! Number of converged eigenvalue/eigenvector pairs
! Basis of subspace of approximants
real(dp), dimension(size(mtx, 1)) :: guess, rs
real(dp), dimension(lowest):: errors
! Working arrays
real(dp), dimension(:), allocatable :: eigenvalues_sub
real(dp), dimension(:, :), allocatable :: correction, eigenvectors_sub, mtx_proj, stx_proj, V
! Diagonal matrix
real(dp), dimension(size(mtx, 1)) :: d
! generalize problem
logical :: gev
! indices of the eigenvalues/eigenvectors pair that have not converged
logical, dimension(lowest) :: has_converged
! Iteration subpsace dimension
dim_sub = lowest * 2
! Initial number of converged eigenvalue/eigenvector pairs
n_converged = 0
has_converged = .False.
! maximum dimension of the basis for the subspace
if (present(max_dim_sub)) then
max_dim = max_dim_sub
else
max_dim = lowest * 10
endif
! generalied problem
gev = present(stx)
! 1. Variables initialization
! Select the initial ortogonal subspace based on lowest elements
! of the diagonal of the matrix
d = diagonal(mtx)
V = generate_preconditioner(d, dim_sub)
! 2. Generate subpace matrix problem by projecting into V
mtx_proj = lapack_matmul('T', 'N', V, lapack_matmul('N', 'N', mtx, V))
if(gev) then
stx_proj = lapack_matmul('T', 'N', V, lapack_matmul('N', 'N', stx, V))
end if
! ! Outer loop block Davidson schema
outer_loop: do i=1, max_iters
! 3. compute the eigenvalues and their corresponding ritz_vectors
! for the projected matrix using lapack
call check_deallocate_matrix(eigenvectors_sub)
if (allocated(eigenvalues_sub)) then
deallocate(eigenvalues_sub)
end if
allocate(eigenvalues_sub(size(mtx_proj, 1)))
allocate(eigenvectors_sub(size(mtx_proj, 1), size(mtx_proj, 2)))
if (gev) then
call lapack_generalized_eigensolver(mtx_proj, eigenvalues_sub, eigenvectors_sub, stx_proj)
else
call lapack_generalized_eigensolver(mtx_proj, eigenvalues_sub, eigenvectors_sub)
end if
! 4. Check for convergence
ritz_vectors = lapack_matmul('N', 'N', V, eigenvectors_sub(:, :lowest))
do j=1,lowest
if(gev) then
guess = eigenvalues_sub(j) * lapack_matrix_vector('N',stx,ritz_vectors(:, j))
else
guess = eigenvalues_sub(j) * ritz_vectors(:, j)
end if
rs = lapack_matrix_vector('N', mtx, ritz_vectors(:, j)) - guess
errors(j) = norm(rs)
! Check which eigenvalues has converged
if (errors(j) < tolerance) then
has_converged(j) = .true.
end if
end do
! Count converged pairs of eigenvalues/eigenvectors
n_converged = n_converged + count(errors < tolerance)
if (all(has_converged)) then
iters = i
exit
end if
! 5. Add the correction vectors to the current basis
if (size(V, 2) <= max_dim) then
! append correction to the current basis
call check_deallocate_matrix(correction)
allocate(correction(size(mtx, 1), size(V, 2)))
if(gev) then
correction = compute_correction_generalized_dense(mtx, V, eigenvalues_sub, eigenvectors_sub, method, stx)
else
correction = compute_correction_generalized_dense(mtx, V, eigenvalues_sub, eigenvectors_sub, method)
end if
! 6. Increase Basis size
call concatenate(V, correction)
! 7. Orthogonalize basis
call lapack_qr(V)
else
! 6. Otherwise reduce the basis of the subspace to the current correction
V = lapack_matmul('N', 'N', V, eigenvectors_sub(:, :dim_sub))
end if
! we refresh the projected matrices
mtx_proj = lapack_matmul('T', 'N', V, lapack_matmul('N', 'N', mtx, V))
if(gev) then
stx_proj = lapack_matmul('T', 'N', V, lapack_matmul('N', 'N', stx, V))
end if
end do outer_loop
! 8. Check convergence
if (i > max_iters) then
iters = i
print *, "Warning: Algorithm did not converge!!"
end if
! Select the lowest eigenvalues and their corresponding ritz_vectors
! They are sort in increasing order
eigenvalues = eigenvalues_sub(:lowest)
! Free memory
call check_deallocate_matrix(correction)
deallocate(eigenvalues_sub, eigenvectors_sub, V, mtx_proj)
! free optional matrix
if (gev) then
call check_deallocate_matrix(stx_proj)
endif
end subroutine generalized_eigensolver_dense
subroutine check_deallocate_matrix(mtx)
!> deallocate a matrix if allocated
real(dp), dimension(:, :), allocatable, intent(inout) :: mtx
if (allocated(mtx)) then
deallocate(mtx)
end if
end subroutine check_deallocate_matrix
end module davidson_dense
module davidson_free
use numeric_kinds, only: dp
use lapack_wrapper, only: lapack_generalized_eigensolver, lapack_matmul, lapack_matrix_vector, &
lapack_qr, lapack_solver
use array_utils, only: concatenate, eye, generate_preconditioner, norm
use davidson_dense, only: generalized_eigensolver_dense
implicit none
!> \private
private
!> \public
public :: generalized_eigensolver_free, free_matmul
contains
subroutine generalized_eigensolver_free(fun_mtx_gemv, eigenvalues, ritz_vectors, lowest, method, max_iters, &
tolerance, iters, max_dim_sub, fun_stx_gemv)
!> \brief use a pair of functions fun_mtx and fun_stx to compute on the fly the matrices to solve
!> the general eigenvalue problem
!> The current implementation uses a general davidson algorithm, meaning
!> that it compute all the eigenvalues simultaneusly using a block approach.
!> The family of Davidson algorithm only differ in the way that the correction
!> vector is computed.
!> \param[in] fun_mtx_gemv: Function to apply the matrix to a buncof vectors
!> \param[in, opt] fun_stx_gemv: (optional) function to apply the pencil to a bunch of vectors.
!> \param[out] eigenvalues Computed eigenvalues
!> \param[out] ritz_vectors approximation to the eigenvectors
!> \param[in] lowest Number of lowest eigenvalues/ritz_vectors to compute
!> \param[in] method Method to compute the correction vector. Available
!> methods are,
!> DPR: Diagonal-Preconditioned-Residue
!> GJD: Generalized Jacobi Davidson
!> \param[in] max_iters: Maximum number of iterations
!> \param[in] tolerance norm-2 error of the eigenvalues
!> \param[in] method: Method to compute the correction vectors
!> \param[in, opt] max_dim_sub: maximum dimension of the subspace search
!> \param[out] iters: Number of iterations until convergence
!> \return eigenvalues and ritz_vectors of the matrix `mtx`
implicit none
! input/output variable
integer, intent(in) :: lowest
real(dp), dimension(lowest), intent(out) :: eigenvalues
real(dp), dimension(:, :), intent(out) :: ritz_vectors
integer, intent(in) :: max_iters
integer, intent(in), optional :: max_dim_sub
real(dp), intent(in) :: tolerance
character(len=*), intent(in) :: method
integer, intent(out) :: iters
! Function to compute the target matrix on the fly
interface
function fun_mtx_gemv(input_vect) result(output_vect)
!> \brief Function to compute the optional mtx on the fly
!> \param[in] i column/row to compute from mtx
!> \param vec column/row from mtx
use numeric_kinds, only: dp
real (dp), dimension(:,:), intent(in) :: input_vect
real (dp), dimension(size(input_vect,1),size(input_vect,2)) :: output_vect
end function fun_mtx_gemv
function fun_stx_gemv(input_vect) result(output_vect)
!> \brief Fucntion to compute the optional stx matrix on the fly
!> \param[in] i column/row to compute from stx
!> \param vec column/row from stx
use numeric_kinds, only: dp
real(dp), dimension(:,:), intent(in) :: input_vect
real (dp), dimension(size(input_vect,1),size(input_vect,2)) :: output_vect
end function fun_stx_gemv
end interface
!local variables
integer :: dim_mtx, dim_sub, max_dim, i, j
! ! Basis of subspace of approximants
real(dp), dimension(size(ritz_vectors, 1),1) :: guess, rs
real(dp), dimension(size(ritz_vectors, 1) ) :: diag_mtx, diag_stx, copy_d
real(dp), dimension(lowest):: errors
! ! Working arrays
real(dp), dimension(:), allocatable :: eigenvalues_sub
real(dp), dimension(:, :), allocatable :: correction, eigenvectors_sub, mtx_proj, stx_proj, V, mtxV, stxV
! Iteration subpsace dimension
dim_sub = lowest * 2
! maximum dimension of the basis for the subspace
if (present(max_dim_sub)) then
max_dim = max_dim_sub
else
max_dim = lowest * 10
endif
! dimension of the matrix
dim_mtx = size(ritz_vectors, 1)
! extract the diagonals of the matrices
diag_mtx = extract_diagonal_free(fun_mtx_gemv,dim_mtx)
diag_stx = extract_diagonal_free(fun_stx_gemv,dim_mtx)
! 1. Variables initialization
! Select the initial ortogonal subspace based on lowest elements
! of the diagonal of the matrix
copy_d = diag_mtx
V = generate_preconditioner(copy_d, dim_sub) ! Initial orthonormal basis
! Outer loop block Davidson schema
outer_loop: do i=1, max_iters
! 2. Generate subspace matrix problem by projecting into V
mtxV = fun_mtx_gemv(V)
stxV = fun_stx_gemv(V)
mtx_proj = lapack_matmul('T', 'N', V, mtxV)
stx_proj = lapack_matmul('T', 'N', V, stxV)
! 3. compute the eigenvalues and their corresponding ritz_vectors
! for the projected matrix using lapack
call check_deallocate_matrix(eigenvectors_sub)
if (allocated(eigenvalues_sub)) then
deallocate(eigenvalues_sub)
end if
allocate(eigenvalues_sub(size(mtx_proj, 1)))
allocate(eigenvectors_sub(size(mtx_proj, 1), size(mtx_proj, 2)))
call lapack_generalized_eigensolver(mtx_proj, eigenvalues_sub, eigenvectors_sub, stx_proj)
! 4. Check for convergence
ritz_vectors = lapack_matmul('N', 'N', V, eigenvectors_sub(:, :lowest))
do j=1,lowest
guess = eigenvalues_sub(j) * fun_stx_gemv(reshape(ritz_vectors(:, j),(/dim_mtx,1/) ) )
rs = fun_mtx_gemv(reshape(ritz_vectors(:, j), (/dim_mtx,1/))) - guess
errors(j) = norm(reshape(rs,(/dim_mtx/)))
end do
if (all(errors < tolerance)) then
iters = i
exit
end if
! 5. Add the correction vectors to the current basis
if (size(V, 2) <= max_dim) then
! append correction to the current basis
call check_deallocate_matrix(correction)
allocate(correction(size(ritz_vectors, 1), size(V, 2)))
correction = compute_DPR_free(mtxV, stxV, eigenvalues_sub, eigenvectors_sub, diag_mtx, diag_stx)
! 6. Increase Basis size
call concatenate(V, correction)
! 7. Orthogonalize basis
call lapack_qr(V)
else
! 6. Otherwise reduce the basis of the subspace to the current correction
V = lapack_matmul('N', 'N', V, eigenvectors_sub(:, :dim_sub))
end if
end do outer_loop
! 8. Check convergence
if (i > max_iters / dim_sub) then
print *, "Warning: Algorithm did not converge!!"
end if
! Select the lowest eigenvalues and their corresponding ritz_vectors
! They are sort in increasing order
eigenvalues = eigenvalues_sub(:lowest)
! Free memory
call check_deallocate_matrix(correction)
deallocate(eigenvalues_sub, eigenvectors_sub, V, mtx_proj, mtxV, stxV)
! free optional matrix
call check_deallocate_matrix(stx_proj)
end subroutine generalized_eigensolver_free
function compute_DPR_free(mtxV, stxV, eigenvalues, eigenvectors, diag_mtx, diag_stx) result(correction)
!> compute the correction vector using the DPR method for a matrix free diagonalization
!> See correction_methods submodule for the implementations
!> \param[in] mtxV: projection mtx * V
!> \param[in] stxV: projection stx * V
!> \param[in] V: Basis of the iteration subspace
!> \param[in] eigenvalues: of the reduce problem
!> \param[in] eigenvectors: of the reduce problem
!> \return correction matrix
real(dp), dimension(:), intent(in) :: eigenvalues
real(dp), dimension(:, :), intent(in) :: eigenvectors, mtxV, stxV
real(dp), dimension(:), intent(in) :: diag_mtx, diag_stx
! local variables
!real(dp), dimension(size(V, 1),1) :: vector
real(dp), dimension(size(mtxV, 1), size(mtxV, 2)) :: correction
real(dp), dimension(size(mtxV, 1), size(mtxV, 2)) :: proj_mtx, proj_stx
real(dp), dimension(size(mtxV, 1),size(mtxV, 1)) :: diag_eigenvalues
integer :: ii, j
integer :: m
! leading dimension of array V
m = size(mtxV,1)
! computed the projected matrices
proj_mtx = lapack_matmul('N', 'N', mtxV, eigenvectors)
proj_stx = lapack_matmul('N', 'N', stxV, eigenvectors)
do ii =1, size(mtxV,2)
diag_eigenvalues = eye(m, m, eigenvalues(ii))
correction(:, ii) = proj_mtx(:, ii) - lapack_matrix_vector('N', diag_eigenvalues, proj_stx(:, ii))
end do
do j=1, size(mtxV, 2)
do ii=1,size(correction,1)
correction(ii, j) = correction(ii, j) / (eigenvalues(j) * diag_stx(ii) - diag_mtx(ii))
end do
end do
end function compute_DPR_free
function extract_diagonal_free(fun_A_gemv,dim) result(out)
!> \brief extract the diagonal of the matrix
!> \param dim: dimension of the matrix
implicit none
integer, intent(in) :: dim
real(dp), dimension(dim) :: out
interface
function fun_A_gemv(input_vect) result(output_vect)
!> \brief Function to compute the optional mtx on the fly
!> \param[in] i column/row to compute from mtx
!> \param vec column/row from mtx
use numeric_kinds, only: dp
real (dp), dimension(:,:), intent(in) :: input_vect
real (dp), dimension(size(input_vect,1),size(input_vect,2)) :: output_vect
end function fun_A_gemv
end interface
! local variable
integer :: ii
real(dp), dimension(dim,1) :: tmp_array
do ii = 1,dim
tmp_array = 0.0_dp
tmp_array(ii,1) = 1.0_dp
tmp_array = fun_A_gemv(tmp_array)
out(ii) = tmp_array(ii,1)
end do
end function extract_diagonal_free
function free_matmul(fun, array) result (mtx)
!> \brief perform a matrix-matrix multiplication by generating a matrix on the fly using `fun`
!> \param[in] fun function to compute a matrix on the fly
!> \param[in] array matrix to multiply with fun
!> \return resulting matrix
! input/output
implicit none
real(dp), dimension(:, :), intent(in) :: array
real(dp), dimension(size(array, 1), size(array, 2)) :: mtx
interface
function fun(i, dim) result(vec)
!> \brief Fucntion to compute the matrix `mtx` on the fly
!> \param[in] i column/row to compute from `mtx`
!> \param vec column/row from mtx
use numeric_kinds, only: dp
integer, intent(in) :: i
integer, intent(in) :: dim
real(dp), dimension(dim) :: vec
end function fun
end interface
! local variables
real(dp), dimension(size(array, 1)) :: vec
integer :: dim1, dim2, i, j
! dimension of the square matrix computed on the fly
dim1 = size(array, 1)
dim2 = size(array, 2)
!$OMP PARALLEL DO &
!$OMP PRIVATE(i, j, vec)
do i = 1, dim1
vec = fun(i, dim1)
do j = 1, dim2
mtx(i, j) = dot_product(vec, array(:, j))
end do
end do
!$OMP END PARALLEL DO
end function free_matmul
subroutine check_deallocate_matrix(mtx)
!> deallocate a matrix if allocated
real(dp), dimension(:, :), allocatable, intent(inout) :: mtx
if (allocated(mtx)) then
deallocate(mtx)
end if
end subroutine check_deallocate_matrix
end module davidson_free
module davidson
use numeric_kinds, only: dp
use lapack_wrapper, only: lapack_generalized_eigensolver, lapack_matmul, lapack_matrix_vector, &
lapack_qr, lapack_solver
use array_utils, only: concatenate, eye, norm
use davidson_dense, only: generalized_eigensolver_dense
use davidson_free, only: generalized_eigensolver_free
implicit none
!> \private
private
!> \public
public :: generalized_eigensolver
interface generalized_eigensolver
!> \brief Solve a (general) eigenvalue problem using different types of Davidson algorithms.
!> \param[in] mtx: Matrix to diagonalize
!> \param[in, opt] stx: Optional matrix for the general eigenvalue problem:
!> \f$ mtx \lambda = V stx \lambda \f$
!> \param[out] eigenvalues Computed eigenvalues
!> \param[out] ritz_vectors approximation to the eigenvectors
!> \param[in] lowest Number of lowest eigenvalues/ritz_vectors to compute
!> \param[in] method Method to compute the correction vector. Available
!> methods are,
!> DPR: Diagonal-Preconditioned-Residue
!> GJD: Generalized Jacobi Davidson
!> \param[in] max_iters: Maximum number of iterations
!> \param[in] tolerance norm-2 error of the eigenvalues
!> \param[in] method: Method to compute the correction vectors
!> \param[in, opt] max_dim_sub: maximum dimension of the subspace search
!> \param[out] iters: Number of iterations until convergence
!> \return eigenvalues and ritz_vectors of the matrix `mtx`
procedure generalized_eigensolver_dense
procedure generalized_eigensolver_free
end interface generalized_eigensolver
end module davidson
submodule (davidson_dense) correction_methods_generalized_dense
!> submodule containing the implementations of different kind
!> algorithms to compute the correction vectors for the Davidson's diagonalization
implicit none
contains
module function compute_correction_generalized_dense(mtx, V, eigenvalues, eigenvectors, method, stx) &
result(correction)
!> see interface in davidson module
real(dp), dimension(:), intent(in) :: eigenvalues
real(dp), dimension(:, :), intent(in) :: mtx, V, eigenvectors
real(dp), dimension(:, :), intent(in), optional :: stx
character(len=*), optional,intent(in) :: method
logical :: gev
! local variables
character(len=10) :: opt
real(dp), dimension(size(mtx, 1), size(V, 2)) :: correction
!check optional arguments
gev = present(stx)
opt="DPR"
if (present(method)) opt=trim(method)
select case (method)
case ("DPR")
if(gev) then
correction = compute_DPR_generalized_dense(mtx, V, eigenvalues, eigenvectors, stx)
else
correction = compute_DPR_generalized_dense(mtx, V, eigenvalues, eigenvectors)
end if
case ("GJD")
if(gev) then
correction = compute_GJD_generalized_dense(mtx, V, eigenvalues, eigenvectors, stx)
else
correction = compute_GJD_generalized_dense(mtx, V, eigenvalues, eigenvectors)
end if
end select
end function compute_correction_generalized_dense
function compute_DPR_generalized_dense(mtx, V, eigenvalues, eigenvectors, stx) result(correction)
!> compute Diagonal-Preconditioned-Residue (DPR) correction
real(dp), dimension(:), intent(in) :: eigenvalues
real(dp), dimension(:, :), intent(in) :: mtx, V, eigenvectors
real(dp), dimension(:, :), intent(in), optional :: stx
real(dp), dimension(size(mtx, 1), size(V, 2)) :: correction
! local variables
integer :: ii,j, m
real(dp), dimension(size(mtx, 1), size(mtx, 2)) :: diag, arr
real(dp), dimension(size(mtx, 1)) :: vec
logical :: gev
! shape of matrix
m = size(mtx, 1)
gev = (present(stx))
do j=1, size(V, 2)
if(gev) then
diag = eigenvalues(j) * stx
else
diag = eye(m , m, eigenvalues(j))
end if
arr = mtx - diag
vec = lapack_matrix_vector('N', V, eigenvectors(:, j))
correction(:, j) = lapack_matrix_vector('N', arr, vec)
do ii=1,size(correction,1)
if (gev) then
correction(ii, j) = correction(ii, j) / (eigenvalues(j) * stx(ii,ii) - mtx(ii, ii))
else
correction(ii, j) = correction(ii, j) / (eigenvalues(j) - mtx(ii, ii))
endif
end do
end do
end function compute_DPR_generalized_dense
function compute_GJD_generalized_dense(mtx, V, eigenvalues, eigenvectors, stx) result(correction)
!> Compute the Generalized Jacobi Davidson (GJD) correction
real(dp), dimension(:), intent(in) :: eigenvalues
real(dp), dimension(:, :), intent(in) :: mtx, V, eigenvectors
real(dp), dimension(:, :), intent(in), optional :: stx
real(dp), dimension(size(mtx, 1), size(V, 2)) :: correction
! local variables
integer :: k, m
logical :: gev
real(dp), dimension(size(mtx, 1), 1) :: rs
real(dp), dimension(size(mtx, 1), size(V, 2)) :: ritz_vectors
real(dp), dimension(size(mtx, 1), size(mtx, 2)) :: arr, xs, ys
real(dp), dimension(size(mtx, 1), 1) :: brr
! Diagonal matrix
m = size(mtx, 1)
ritz_vectors = lapack_matmul('N', 'N', V, eigenvectors)
gev = present(stx)
do k=1, size(V, 2)
rs(:, 1) = ritz_vectors(:, k)
xs = eye(m, m) - lapack_matmul('N', 'T', rs, rs)
if(gev) then
ys = mtx - eigenvalues(k)*stx
else
ys = substract_from_diagonal(mtx, eigenvalues(k))
end if
arr = lapack_matmul('N', 'N', xs, lapack_matmul('N', 'N', ys, xs))
brr = -rs
call lapack_solver(arr, brr)
correction(:, k) = brr(:, 1)
end do
end function compute_GJD_generalized_dense
function substract_from_diagonal(mtx, alpha) result(arr)
!> susbstract an scalar from the diagonal of a matrix
!> \param mtx: square matrix
!> \param alpha: scalar to substract
real(dp), dimension(:, :), intent(in) :: mtx
real(dp), dimension(size(mtx, 1), size(mtx, 2)) :: arr
real(dp), intent(in) :: alpha
integer :: i
arr = mtx
do i=1,size(mtx, 1)
arr(i, i) = arr(i, i) - alpha
end do
end function substract_from_diagonal
end submodule correction_methods_generalized_dense