-
Notifications
You must be signed in to change notification settings - Fork 1
/
xas_tdp_utils.F
3169 lines (2601 loc) · 154 KB
/
xas_tdp_utils.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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Utilities for X-ray absorption spectroscopy using TDDFPT
!> \author AB (01.2018)
! **************************************************************************************************
MODULE xas_tdp_utils
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_cfm_diag, ONLY: cp_cfm_heevd
USE cp_cfm_types, ONLY: cp_cfm_create,&
cp_cfm_get_info,&
cp_cfm_get_submatrix,&
cp_cfm_release,&
cp_cfm_type,&
cp_fm_to_cfm
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_distribution_get, dbcsr_distribution_new, &
dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_finalize, dbcsr_get_block_p, &
dbcsr_get_info, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_multiply, &
dbcsr_p_type, dbcsr_put_block, dbcsr_release, dbcsr_reserve_all_blocks, dbcsr_set, &
dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE cp_dbcsr_cholesky, ONLY: cp_dbcsr_cholesky_decompose,&
cp_dbcsr_cholesky_invert
USE cp_dbcsr_diag, ONLY: cp_dbcsr_power
USE cp_dbcsr_operations, ONLY: copy_dbcsr_to_fm,&
copy_fm_to_dbcsr,&
cp_dbcsr_sm_fm_multiply,&
dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale,&
cp_fm_scale,&
cp_fm_transpose,&
cp_fm_upper_to_full
USE cp_fm_diag, ONLY: choose_eigv_solver,&
cp_fm_geeig
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_diag,&
cp_fm_get_info,&
cp_fm_get_submatrix,&
cp_fm_release,&
cp_fm_set_element,&
cp_fm_to_fm_submat,&
cp_fm_type
USE cp_log_handling, ONLY: cp_logger_get_default_io_unit
USE input_constants, ONLY: ot_precond_full_single,&
tddfpt_singlet,&
tddfpt_spin_cons,&
tddfpt_spin_flip,&
tddfpt_triplet,&
xas_dip_len
USE kinds, ONLY: dp
USE mathlib, ONLY: get_diag
USE message_passing, ONLY: mp_para_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE physcon, ONLY: a_fine
USE preconditioner_types, ONLY: destroy_preconditioner,&
init_preconditioner,&
preconditioner_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_mo_methods, ONLY: calculate_subspace_eigenvalues
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_ot_eigensolver, ONLY: ot_eigensolver
USE xas_tdp_kernel, ONLY: kernel_coulomb_xc,&
kernel_exchange
USE xas_tdp_types, ONLY: donor_state_type,&
xas_tdp_control_type,&
xas_tdp_env_type
!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'xas_tdp_utils'
PUBLIC :: setup_xas_tdp_prob, solve_xas_tdp_prob, include_rcs_soc, &
include_os_soc, rcs_amew_soc_elements
!A helper type for SOC
TYPE dbcsr_soc_package_type
TYPE(dbcsr_type), POINTER :: dbcsr_sg => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_tp => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_sc => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_sf => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_prod => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_ovlp => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_tmp => NULL()
TYPE(dbcsr_type), POINTER :: dbcsr_work => NULL()
END TYPE dbcsr_soc_package_type
CONTAINS
! **************************************************************************************************
!> \brief Builds the matrix that defines the XAS TDDFPT generalized eigenvalue problem to be solved
!> for excitation energies omega. The problem has the form omega*G*C = M*C, where C contains
!> the response orbitals coefficients. The matrix M and the metric G are stored in the given
!> donor_state
!> \param donor_state the donor_state for which the problem is restricted
!> \param qs_env ...
!> \param xas_tdp_env ...
!> \param xas_tdp_control ...
!> \note the matrix M is symmetric and has the form | M_d M_o |
!> | M_o M_d |,
!> -In the SPIN-RESTRICTED case:
!> depending on whether we consider singlet or triplet excitation, the diagonal (M_d) and
!> off-diagonal (M_o) parts of M differ:
!> - For singlet: M_d = A + 2B + C_aa + C_ab - D
!> M_o = 2B + C_aa + C_ab - E
!> - For triplet: M_d = A + C_aa - C_ab - D
!> M_o = C_aa - C_ab - E
!> where other subroutines computes the matrices A, B, E, D and G, which are:
!> - A: the ground-state contribution: F_pq*delta_IJ - epsilon_IJ*S_pq
!> - B: the Coulomb kernel ~(pI|Jq)
!> - C: the xc kernel c_aa (double derivatibe wrt to n_alpha) and C_ab (wrt n_alpha and n_beta)
!> - D: the on-digonal exact exchange kernel ~(pq|IJ)
!> - E: the off-diagonal exact exchange kernel ~(pJ|Iq)
!> - G: the metric S_pq*delta_IJ
!> For the xc functionals, C_aa + C_ab or C_aa - C_ab are stored in the same matrix
!> In the above definitions, I,J label the donnor MOs and p,q the sgfs of the basis
!>
!> -In the SPIN-UNRESTRICTED, spin-conserving case:
!> the on- and off-diagonal elements of M are:
!> M_d = A + B + C -D
!> M_o = B + C - E
!> where the submatrices A, B, C, D and E are:
!> - A: the groun-state contribution: (F_pq*delta_IJ - epsilon_IJ*S_pq) * delta_ab
!> - B: the Coulomb kernel: (pI_a|J_b q)
!> - C: the xc kernel: (pI_a|fxc_ab|J_b q)
!> - D: the on-diagonal exact-exchange kernel: (pq|I_a J_b) delta_ab
!> - E: the off-diagonal exact-exchange kernel: (pJ_b|I_a q) delta_ab
!> - G: the metric S_pq*delta_IJ*delta_ab
!> p,q label the sgfs, I,J the donro MOs and a,b the spins
!>
!> -In both above cases, the matrix M is always projected onto the unperturbed unoccupied
!> ground state: M <= Q * M * Q^T = (1 - SP) * M * (1 - PS)
!>
!> -In the SPIN-FLIP case:
!> Only the TDA is implemented, that is, there are only on-diagonal elements:
!> M_d = A + C - D
!> where the submatrices A, C and D are:
!> - A: the ground state-contribution: (F_pq*delta_IJ - epsilon_IJ*S_pq) * delta_ab, but here,
!> the alph-alpha quadrant has the beta Fock matrix and
!> the beta-beta quadrant has the alpha Fock matrix
!> - C: the SF xc kernel: (pI_a|fxc|J_bq), fxc = 1/m * (vxc_a -vxc_b)
!> - D: the on-diagonal exact-exchange kernel: (pq|I_a J_b) delta_ab
!> To ensure that all excitation start from a given spin to the opposite, we then multiply
!> by a Q projector where we swap the alpha-alpha and beta-beta spin-quadrants
!>
!> All possibilities: TDA or full-TDDFT, singlet or triplet, xc or hybrid, etc are treated
!> in the same routine to avoid recomputing stuff
!> Under TDA, only the on-diagonal elements of M are computed
!> In the case of non-TDA, one turns the problem Hermitian
! **************************************************************************************************
SUBROUTINE setup_xas_tdp_prob(donor_state, qs_env, xas_tdp_env, xas_tdp_control)
TYPE(donor_state_type), POINTER :: donor_state
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
CHARACTER(len=*), PARAMETER :: routineN = 'setup_xas_tdp_prob'
INTEGER :: handle
INTEGER, DIMENSION(:), POINTER :: submat_blk_size
LOGICAL :: do_coul, do_hfx, do_os, do_sc, do_sf, &
do_sg, do_tda, do_tp, do_xc
REAL(dp) :: eps_filter, sx
TYPE(dbcsr_distribution_type), POINTER :: submat_dist
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: ex_ker, xc_ker
TYPE(dbcsr_type) :: matrix_a, matrix_a_sf, matrix_b, proj_Q, &
proj_Q_sf, work
TYPE(dbcsr_type), POINTER :: matrix_c_sc, matrix_c_sf, matrix_c_sg, matrix_c_tp, matrix_d, &
matrix_e_sc, sc_matrix_tdp, sf_matrix_tdp, sg_matrix_tdp, tp_matrix_tdp
NULLIFY (sg_matrix_tdp, tp_matrix_tdp, submat_dist, submat_blk_size, matrix_c_sf)
NULLIFY (matrix_c_sg, matrix_c_tp, matrix_c_sc, matrix_d, matrix_e_sc)
NULLIFY (sc_matrix_tdp, sf_matrix_tdp, ex_ker, xc_ker)
CALL timeset(routineN, handle)
! Initialization
do_os = xas_tdp_control%do_uks .OR. xas_tdp_control%do_roks
do_sc = xas_tdp_control%do_spin_cons
do_sf = xas_tdp_control%do_spin_flip
do_sg = xas_tdp_control%do_singlet
do_tp = xas_tdp_control%do_triplet
do_xc = xas_tdp_control%do_xc
do_hfx = xas_tdp_control%do_hfx
do_coul = xas_tdp_control%do_coulomb
do_tda = xas_tdp_control%tamm_dancoff
sx = xas_tdp_control%sx
eps_filter = xas_tdp_control%eps_filter
IF (do_sc) THEN
ALLOCATE (donor_state%sc_matrix_tdp)
sc_matrix_tdp => donor_state%sc_matrix_tdp
END IF
IF (do_sf) THEN
ALLOCATE (donor_state%sf_matrix_tdp)
sf_matrix_tdp => donor_state%sf_matrix_tdp
END IF
IF (do_sg) THEN
ALLOCATE (donor_state%sg_matrix_tdp)
sg_matrix_tdp => donor_state%sg_matrix_tdp
END IF
IF (do_tp) THEN
ALLOCATE (donor_state%tp_matrix_tdp)
tp_matrix_tdp => donor_state%tp_matrix_tdp
END IF
! Get the dist and block size of all matrices A, B, C, etc
CALL compute_submat_dist_and_blk_size(donor_state, do_os, qs_env)
submat_dist => donor_state%dbcsr_dist
submat_blk_size => donor_state%blk_size
! Allocate and compute all the matrices A, B, C, etc we will need
! The projector(s) on the unoccupied unperturbed ground state 1-SP and associated work matrix
IF (do_sg .OR. do_tp .OR. do_sc) THEN !spin-conserving
CALL get_q_projector(proj_Q, donor_state, do_os, xas_tdp_env)
END IF
IF (do_sf) THEN !spin-flip
CALL get_q_projector(proj_Q_sf, donor_state, do_os, xas_tdp_env, do_sf=.TRUE.)
END IF
CALL dbcsr_create(matrix=work, matrix_type=dbcsr_type_no_symmetry, dist=submat_dist, &
name="WORK", row_blk_size=submat_blk_size, col_blk_size=submat_blk_size)
! The ground state contribution(s)
IF (do_sg .OR. do_tp .OR. do_sc) THEN !spin-conserving
CALL build_gs_contribution(matrix_a, donor_state, do_os, qs_env)
END IF
IF (do_sf) THEN !spin-flip
CALL build_gs_contribution(matrix_a_sf, donor_state, do_os, qs_env, do_sf=.TRUE.)
END IF
! The Coulomb and XC kernels. Internal analysis to know which matrix to compute
CALL dbcsr_allocate_matrix_set(xc_ker, 4)
ALLOCATE (xc_ker(1)%matrix, xc_ker(2)%matrix, xc_ker(3)%matrix, xc_ker(4)%matrix)
CALL kernel_coulomb_xc(matrix_b, xc_ker, donor_state, xas_tdp_env, xas_tdp_control, qs_env)
matrix_c_sg => xc_ker(1)%matrix; matrix_c_tp => xc_ker(2)%matrix
matrix_c_sc => xc_ker(3)%matrix; matrix_c_sf => xc_ker(4)%matrix
! The exact exchange. Internal analysis to know which matrices to compute
CALL dbcsr_allocate_matrix_set(ex_ker, 2)
ALLOCATE (ex_ker(1)%matrix, ex_ker(2)%matrix)
CALL kernel_exchange(ex_ker, donor_state, xas_tdp_env, xas_tdp_control, qs_env)
matrix_d => ex_ker(1)%matrix; matrix_e_sc => ex_ker(2)%matrix
! Build the metric G, also need its inverse in case of full-TDDFT
IF (do_tda) THEN
ALLOCATE (donor_state%metric(1))
CALL build_metric(donor_state%metric, donor_state, qs_env, do_os)
ELSE
ALLOCATE (donor_state%metric(2))
CALL build_metric(donor_state%metric, donor_state, qs_env, do_os, do_inv=.TRUE.)
END IF
! Build the eigenvalue problem, depending on the case (TDA, singlet, triplet, hfx, etc ...)
IF (do_tda) THEN
IF (do_sc) THEN ! open-shell spin-conserving under TDA
! The final matrix is M = A + B + C - D
CALL dbcsr_copy(sc_matrix_tdp, matrix_a, name="OS MATRIX TDP")
IF (do_coul) CALL dbcsr_add(sc_matrix_tdp, matrix_b, 1.0_dp, 1.0_dp)
IF (do_xc) CALL dbcsr_add(sc_matrix_tdp, matrix_c_sc, 1.0_dp, 1.0_dp) !xc kernel
IF (do_hfx) CALL dbcsr_add(sc_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx) !scaled hfx
! The product with the Q projector
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, sc_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, sc_matrix_tdp, filter_eps=eps_filter)
END IF !do_sc
IF (do_sf) THEN ! open-shell spin-flip under TDA
! The final matrix is M = A + C - D
CALL dbcsr_copy(sf_matrix_tdp, matrix_a_sf, name="OS MATRIX TDP")
IF (do_xc) CALL dbcsr_add(sf_matrix_tdp, matrix_c_sf, 1.0_dp, 1.0_dp) !xc kernel
IF (do_hfx) CALL dbcsr_add(sf_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx) !scaled hfx
! Take the product with the (spin-flip) Q projector
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q_sf, sf_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q_sf, 0.0_dp, sf_matrix_tdp, filter_eps=eps_filter)
END IF !do_sf
IF (do_sg) THEN ! singlets under TDA
! The final matrix is M = A + 2B + (C_aa + C_ab) - D
CALL dbcsr_copy(sg_matrix_tdp, matrix_a, name="SINGLET MATRIX TDP")
IF (do_coul) CALL dbcsr_add(sg_matrix_tdp, matrix_b, 1.0_dp, 2.0_dp)
IF (do_xc) CALL dbcsr_add(sg_matrix_tdp, matrix_c_sg, 1.0_dp, 1.0_dp) ! xc kernel
IF (do_hfx) CALL dbcsr_add(sg_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx) ! scaled hfx
! Take the product with the Q projector:
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, sg_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, sg_matrix_tdp, filter_eps=eps_filter)
END IF !do_sg (TDA)
IF (do_tp) THEN ! triplets under TDA
! The final matrix is M = A + (C_aa - C_ab) - D
CALL dbcsr_copy(tp_matrix_tdp, matrix_a, name="TRIPLET MATRIX TDP")
IF (do_xc) CALL dbcsr_add(tp_matrix_tdp, matrix_c_tp, 1.0_dp, 1.0_dp) ! xc_kernel
IF (do_hfx) CALL dbcsr_add(tp_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx) ! scaled hfx
! Take the product with the Q projector:
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, tp_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, tp_matrix_tdp, filter_eps=eps_filter)
END IF !do_tp (TDA)
ELSE ! not TDA
! In the case of full-TDDFT, the problem is turned Hermitian with the help of auxiliary
! matrices AUX = (A-D+E)^(+-0.5) that are stored in donor_state
CALL build_aux_matrix(1.0E-8_dp, sx, matrix_a, matrix_d, matrix_e_sc, do_hfx, proj_Q, &
work, donor_state, eps_filter, qs_env)
IF (do_sc) THEN !full-TDDFT open-shell spin-conserving
! The final matrix is the sum of the on- and off-diagonal elements as in the description
! M = A + 2B + 2C - D - E
CALL dbcsr_copy(sc_matrix_tdp, matrix_a, name="OS MATRIX TDP")
IF (do_coul) CALL dbcsr_add(sc_matrix_tdp, matrix_b, 1.0_dp, 2.0_dp)
IF (do_hfx) THEN !scaled hfx
CALL dbcsr_add(sc_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx)
CALL dbcsr_add(sc_matrix_tdp, matrix_e_sc, 1.0_dp, -1.0_dp*sx)
END IF
IF (do_xc) THEN
CALL dbcsr_add(sc_matrix_tdp, matrix_c_sc, 1.0_dp, 2.0_dp)
END IF
! Take the product with the Q projector
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, sc_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, sc_matrix_tdp, filter_eps=eps_filter)
! Take the product with the inverse metric
! M <= G^-1 * M * G^-1
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%metric(2)%matrix, sc_matrix_tdp, &
0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, work, donor_state%metric(2)%matrix, 0.0_dp, &
sc_matrix_tdp, filter_eps=eps_filter)
END IF
IF (do_sg) THEN ! full-TDDFT singlets
! The final matrix is the sum of the on- and off-diagonal elements as in the description
! M = A + 4B + 2(C_aa + C_ab) - D - E
CALL dbcsr_copy(sg_matrix_tdp, matrix_a, name="SINGLET MATRIX TDP")
IF (do_coul) CALL dbcsr_add(sg_matrix_tdp, matrix_b, 1.0_dp, 4.0_dp)
IF (do_hfx) THEN !scaled hfx
CALL dbcsr_add(sg_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx)
CALL dbcsr_add(sg_matrix_tdp, matrix_e_sc, 1.0_dp, -1.0_dp*sx)
END IF
IF (do_xc) THEN !xc kernel
CALL dbcsr_add(sg_matrix_tdp, matrix_c_sg, 1.0_dp, 2.0_dp)
END IF
! Take the product with the Q projector
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, sg_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, sg_matrix_tdp, filter_eps=eps_filter)
! Take the product with the inverse metric
! M <= G^-1 * M * G^-1
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%metric(2)%matrix, sg_matrix_tdp, &
0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, work, donor_state%metric(2)%matrix, 0.0_dp, &
sg_matrix_tdp, filter_eps=eps_filter)
END IF ! singlets
IF (do_tp) THEN ! full-TDDFT triplets
! The final matrix is the sum of the on- and off-diagonal elements as in the description
! M = A + 2(C_aa - C_ab) - D - E
CALL dbcsr_copy(tp_matrix_tdp, matrix_a, name="TRIPLET MATRIX TDP")
IF (do_hfx) THEN !scaled hfx
CALL dbcsr_add(tp_matrix_tdp, matrix_d, 1.0_dp, -1.0_dp*sx)
CALL dbcsr_add(tp_matrix_tdp, matrix_e_sc, 1.0_dp, -1.0_dp*sx)
END IF
IF (do_xc) THEN
CALL dbcsr_add(tp_matrix_tdp, matrix_c_tp, 1.0_dp, 2.0_dp)
END IF
! Take the product with the Q projector
CALL dbcsr_multiply('N', 'N', 1.0_dp, proj_Q, tp_matrix_tdp, 0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'T', 1.0_dp, work, proj_Q, 0.0_dp, tp_matrix_tdp, filter_eps=eps_filter)
! Take the product with the inverse metric
! M <= G^-1 * M * G^-1
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%metric(2)%matrix, tp_matrix_tdp, &
0.0_dp, work, filter_eps=eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, work, donor_state%metric(2)%matrix, 0.0_dp, &
tp_matrix_tdp, filter_eps=eps_filter)
END IF ! triplets
END IF ! test on TDA
! Clean-up
CALL dbcsr_release(matrix_a)
CALL dbcsr_release(matrix_a_sf)
CALL dbcsr_release(matrix_b)
CALL dbcsr_release(proj_Q)
CALL dbcsr_release(proj_Q_sf)
CALL dbcsr_release(work)
CALL dbcsr_deallocate_matrix_set(ex_ker)
CALL dbcsr_deallocate_matrix_set(xc_ker)
CALL timestop(handle)
END SUBROUTINE setup_xas_tdp_prob
! **************************************************************************************************
!> \brief Solves the XAS TDP generalized eigenvalue problem omega*C = matrix_tdp*C using standard
!> full diagonalization methods. The problem is Hermitian (made that way even if not TDA)
!> \param donor_state ...
!> \param xas_tdp_control ...
!> \param xas_tdp_env ...
!> \param qs_env ...
!> \param ex_type whether we deal with singlets, triplets, spin-conserving open-shell or spin-flip
!> \note The computed eigenvalues and eigenvectors are stored in the donor_state
!> The eigenvectors are the LR-coefficients. In case of TDA, c^- is stored. In the general
!> case, the sum c^+ + c^- is stored.
!> - Spin-restricted:
!> In case both singlets and triplets are considered, this routine must be called twice. This
!> is the choice that was made because the body of the routine is exactly the same in both cases
!> Note that for singlet we solve for u = 1/sqrt(2)*(c_alpha + c_beta) = sqrt(2)*c
!> and that for triplets we solve for v = 1/sqrt(2)*(c_alpha - c_beta) = sqrt(2)*c
!> - Spin-unrestricted:
!> The problem is solved for the LR coefficients c_pIa as they are (not linear combination)
!> The routine might be called twice (once for spin-conservign, one for spin-flip)
! **************************************************************************************************
SUBROUTINE solve_xas_tdp_prob(donor_state, xas_tdp_control, xas_tdp_env, qs_env, ex_type)
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, INTENT(IN) :: ex_type
CHARACTER(len=*), PARAMETER :: routineN = 'solve_xas_tdp_prob'
INTEGER :: first_ex, handle, i, imo, ispin, nao, &
ndo_mo, nelectron, nevals, nocc, nrow, &
nspins, ot_nevals
LOGICAL :: do_os, do_range, do_sf
REAL(dp) :: ot_elb
REAL(dp), ALLOCATABLE, DIMENSION(:) :: scaling, tmp_evals
REAL(dp), DIMENSION(:), POINTER :: lr_evals
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_type), POINTER :: ex_struct, fm_struct, ot_fm_struct
TYPE(cp_fm_type) :: c_diff, c_sum, lhs_matrix, rhs_matrix, &
work
TYPE(cp_fm_type), POINTER :: lr_coeffs
TYPE(dbcsr_type) :: tmp_mat, tmp_mat2
TYPE(dbcsr_type), POINTER :: matrix_tdp
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
NULLIFY (para_env, blacs_env, fm_struct, matrix_tdp)
NULLIFY (ex_struct, lr_evals, lr_coeffs)
CPASSERT(ASSOCIATED(xas_tdp_env))
do_os = .FALSE.
do_sf = .FALSE.
IF (ex_type == tddfpt_spin_cons) THEN
matrix_tdp => donor_state%sc_matrix_tdp
do_os = .TRUE.
ELSE IF (ex_type == tddfpt_spin_flip) THEN
matrix_tdp => donor_state%sf_matrix_tdp
do_os = .TRUE.
do_sf = .TRUE.
ELSE IF (ex_type == tddfpt_singlet) THEN
matrix_tdp => donor_state%sg_matrix_tdp
ELSE IF (ex_type == tddfpt_triplet) THEN
matrix_tdp => donor_state%tp_matrix_tdp
END IF
CALL get_qs_env(qs_env=qs_env, para_env=para_env, blacs_env=blacs_env, nelectron_total=nelectron)
! Initialization
nspins = 1; IF (do_os) nspins = 2
CALL cp_fm_get_info(donor_state%gs_coeffs, nrow_global=nao)
CALL dbcsr_get_info(matrix_tdp, nfullrows_total=nrow)
ndo_mo = donor_state%ndo_mo
nocc = nelectron/2; IF (do_os) nocc = nelectron
nocc = ndo_mo*nocc
!solve by energy_range or number of states ?
do_range = .FALSE.
IF (xas_tdp_control%e_range > 0.0_dp) do_range = .TRUE.
! create the fm infrastructure
CALL cp_fm_struct_create(fm_struct, context=blacs_env, nrow_global=nrow, &
para_env=para_env, ncol_global=nrow)
CALL cp_fm_create(rhs_matrix, fm_struct)
CALL cp_fm_create(work, fm_struct)
! Test on TDA
IF (xas_tdp_control%tamm_dancoff) THEN
IF (xas_tdp_control%do_ot) THEN
!need to precompute the number of evals for OT
IF (do_range) THEN
!in case of energy range criterion, use LUMO eigenvalues as estimate
ot_elb = xas_tdp_env%lumo_evals(1)%array(1)
IF (do_os) ot_elb = MIN(ot_elb, xas_tdp_env%lumo_evals(2)%array(1))
ot_nevals = COUNT(xas_tdp_env%lumo_evals(1)%array - ot_elb .LE. xas_tdp_control%e_range)
IF (do_os) ot_nevals = ot_nevals + &
COUNT(xas_tdp_env%lumo_evals(2)%array - ot_elb .LE. xas_tdp_control%e_range)
ELSE
ot_nevals = nspins*nao - nocc/ndo_mo
IF (xas_tdp_control%n_excited > 0 .AND. xas_tdp_control%n_excited < ot_nevals) THEN
ot_nevals = xas_tdp_control%n_excited
END IF
END IF
ot_nevals = ndo_mo*ot_nevals !as in input description, multiply by multiplicity of donor state
! Organize results data
first_ex = 1
ALLOCATE (tmp_evals(ot_nevals))
CALL cp_fm_struct_create(ot_fm_struct, context=blacs_env, para_env=para_env, &
nrow_global=nrow, ncol_global=ot_nevals)
CALL cp_fm_create(c_sum, ot_fm_struct)
CALL xas_ot_solver(matrix_tdp, donor_state%metric(1)%matrix, c_sum, tmp_evals, ot_nevals, &
do_sf, donor_state, xas_tdp_env, xas_tdp_control, qs_env)
CALL cp_fm_struct_release(ot_fm_struct)
ELSE
! Organize results data
first_ex = nocc + 1 !where to find the first proper eigenvalue
ALLOCATE (tmp_evals(nrow))
CALL cp_fm_create(c_sum, fm_struct)
! Get the main matrix_tdp as an fm
CALL copy_dbcsr_to_fm(matrix_tdp, rhs_matrix)
! Get the metric as a fm
CALL cp_fm_create(lhs_matrix, fm_struct)
CALL copy_dbcsr_to_fm(donor_state%metric(1)%matrix, lhs_matrix)
!Diagonalisation (Cholesky decomposition). In TDA, c_sum = c^-
CALL cp_fm_geeig(rhs_matrix, lhs_matrix, c_sum, tmp_evals, work)
! TDA specific clean-up
CALL cp_fm_release(lhs_matrix)
END IF
ELSE ! not TDA
! Organize results data
first_ex = nocc + 1
ALLOCATE (tmp_evals(nrow))
CALL cp_fm_create(c_sum, fm_struct)
! Need to multiply the current matrix_tdp with the auxiliary matrix
! tmp_mat = (A-D+E)^0.5 * M * (A-D+E)^0.5
CALL dbcsr_create(matrix=tmp_mat, template=matrix_tdp, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_create(matrix=tmp_mat2, template=matrix_tdp, matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%matrix_aux, matrix_tdp, &
0.0_dp, tmp_mat2, filter_eps=xas_tdp_control%eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, tmp_mat2, donor_state%matrix_aux, &
0.0_dp, tmp_mat, filter_eps=xas_tdp_control%eps_filter)
! Get the matrix as a fm
CALL copy_dbcsr_to_fm(tmp_mat, rhs_matrix)
! Solve the "turned-Hermitian" eigenvalue problem
CALL choose_eigv_solver(rhs_matrix, work, tmp_evals)
! Currently, work = (A-D+E)^0.5 (c^+ - c^-) and tmp_evals = omega^2
! Put tiny almost zero eigenvalues to zero (corresponding to occupied MOs)
WHERE (tmp_evals < 1.0E-4_dp) tmp_evals = 0.0_dp
! Retrieve c_diff = (c^+ - c^-) for normalization
! (c^+ - c^-) = 1/omega^2 * M * (A-D+E)^0.5 * work
CALL cp_fm_create(c_diff, fm_struct)
CALL dbcsr_multiply('N', 'N', 1.0_dp, matrix_tdp, donor_state%matrix_aux, &
0.0_dp, tmp_mat, filter_eps=xas_tdp_control%eps_filter)
CALL cp_dbcsr_sm_fm_multiply(tmp_mat, work, c_diff, ncol=nrow)
ALLOCATE (scaling(nrow))
scaling = 0.0_dp
WHERE (ABS(tmp_evals) > 1.0E-8_dp) scaling = 1.0_dp/tmp_evals
CALL cp_fm_column_scale(c_diff, scaling)
! Normalize with the metric: c_diff * G * c_diff = +- 1
scaling = 0.0_dp
CALL get_normal_scaling(scaling, c_diff, donor_state)
CALL cp_fm_column_scale(c_diff, scaling)
! Get the actual eigenvalues
tmp_evals = SQRT(tmp_evals)
! Get c_sum = (c^+ + c^-), which appears in all transition density related expressions
! c_sum = -1/omega G^-1 * (A-D+E) * (c^+ - c^-)
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%matrix_aux, donor_state%matrix_aux, &
0.0_dp, tmp_mat2, filter_eps=xas_tdp_control%eps_filter)
CALL dbcsr_multiply('N', 'N', 1.0_dp, donor_state%metric(2)%matrix, tmp_mat2, &
0.0_dp, tmp_mat, filter_eps=xas_tdp_control%eps_filter)
CALL cp_dbcsr_sm_fm_multiply(tmp_mat, c_diff, c_sum, ncol=nrow)
WHERE (tmp_evals .NE. 0) scaling = -1.0_dp/tmp_evals
CALL cp_fm_column_scale(c_sum, scaling)
! Full TDDFT specific clean-up
CALL cp_fm_release(c_diff)
CALL dbcsr_release(tmp_mat)
CALL dbcsr_release(tmp_mat2)
DEALLOCATE (scaling)
END IF ! TDA
! Full matrix clean-up
CALL cp_fm_release(rhs_matrix)
CALL cp_fm_release(work)
! Reorganize the eigenvalues, we want a lr_evals array with the proper dimension and where the
! first element is the first eval. Need a case study on do_range/ot
IF (xas_tdp_control%do_ot) THEN
nevals = ot_nevals
ELSE IF (do_range) THEN
WHERE (tmp_evals > tmp_evals(first_ex) + xas_tdp_control%e_range) tmp_evals = 0.0_dp
nevals = MAXLOC(tmp_evals, 1) - nocc
ELSE
!Determine the number of evals to keep base on N_EXCITED
nevals = nspins*nao - nocc/ndo_mo
IF (xas_tdp_control%n_excited > 0 .AND. xas_tdp_control%n_excited < nevals) THEN
nevals = xas_tdp_control%n_excited
END IF
nevals = ndo_mo*nevals !as in input description, multiply by # of donor MOs
END IF
ALLOCATE (lr_evals(nevals))
lr_evals(:) = tmp_evals(first_ex:first_ex + nevals - 1)
! Reorganize the eigenvectors in array of cp_fm so that each ndo_mo columns corresponds to an
! excited state. Makes later calls to those easier and more efficient
! In case of open-shell, we store the coeffs in the same logic as the matrix => first block where
! the columns are the c_Ialpha and second block with columns as c_Ibeta
CALL cp_fm_struct_create(ex_struct, nrow_global=nao, ncol_global=ndo_mo*nspins*nevals, &
para_env=para_env, context=blacs_env)
ALLOCATE (lr_coeffs)
CALL cp_fm_create(lr_coeffs, ex_struct)
DO i = 1, nevals
DO ispin = 1, nspins
DO imo = 1, ndo_mo
CALL cp_fm_to_fm_submat(msource=c_sum, mtarget=lr_coeffs, &
nrow=nao, ncol=1, s_firstrow=((ispin - 1)*ndo_mo + imo - 1)*nao + 1, &
s_firstcol=first_ex + i - 1, t_firstrow=1, &
t_firstcol=(i - 1)*ndo_mo*nspins + (ispin - 1)*ndo_mo + imo)
END DO !imo
END DO !ispin
END DO !istate
IF (ex_type == tddfpt_spin_cons) THEN
donor_state%sc_coeffs => lr_coeffs
donor_state%sc_evals => lr_evals
ELSE IF (ex_type == tddfpt_spin_flip) THEN
donor_state%sf_coeffs => lr_coeffs
donor_state%sf_evals => lr_evals
ELSE IF (ex_type == tddfpt_singlet) THEN
donor_state%sg_coeffs => lr_coeffs
donor_State%sg_evals => lr_evals
ELSE IF (ex_type == tddfpt_triplet) THEN
donor_state%tp_coeffs => lr_coeffs
donor_state%tp_evals => lr_evals
END IF
! Clean-up
CALL cp_fm_release(c_sum)
CALL cp_fm_struct_release(fm_struct)
CALL cp_fm_struct_release(ex_struct)
! Perform a partial clean-up of the donor_state
CALL dbcsr_release(matrix_tdp)
CALL timestop(handle)
END SUBROUTINE solve_xas_tdp_prob
! **************************************************************************************************
!> \brief An iterative solver based on OT for the TDA generalized eigV problem lambda Sx = Hx
!> \param matrix_tdp the RHS matrix (dbcsr)
!> \param metric the LHS matrix (dbcsr)
!> \param evecs the corresponding eigenvectors (fm)
!> \param evals the corresponding eigenvalues
!> \param neig the number of wanted eigenvalues
!> \param do_sf whther spin-flip TDDFT is on
!> \param donor_state ...
!> \param xas_tdp_env ...
!> \param xas_tdp_control ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE xas_ot_solver(matrix_tdp, metric, evecs, evals, neig, do_sf, donor_state, xas_tdp_env, &
xas_tdp_control, qs_env)
TYPE(dbcsr_type), POINTER :: matrix_tdp, metric
TYPE(cp_fm_type), INTENT(IN) :: evecs
REAL(dp), DIMENSION(:) :: evals
INTEGER, INTENT(IN) :: neig
LOGICAL :: do_sf
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'xas_ot_solver'
INTEGER :: handle, max_iter, ndo_mo, nelec_spin(2), &
nocc, nrow, output_unit
LOGICAL :: do_os
REAL(dp) :: eps_iter
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_type), POINTER :: ortho_struct
TYPE(cp_fm_type) :: ortho_space
TYPE(dbcsr_type), POINTER :: ot_prec
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(preconditioner_type), POINTER :: precond
NULLIFY (para_env, blacs_env, ortho_struct, ot_prec)
CALL timeset(routineN, handle)
output_unit = cp_logger_get_default_io_unit()
IF (output_unit > 0) THEN
WRITE (output_unit, "(/,T5,A)") &
"Using OT eigensolver for diagonalization: "
END IF
do_os = xas_tdp_control%do_uks .OR. xas_tdp_control%do_roks
ndo_mo = donor_state%ndo_mo
CALL get_qs_env(qs_env, para_env=para_env, blacs_env=blacs_env, nelectron_spin=nelec_spin)
CALL cp_fm_get_info(evecs, nrow_global=nrow)
max_iter = xas_tdp_control%ot_max_iter
eps_iter = xas_tdp_control%ot_eps_iter
nocc = nelec_spin(1)/2*ndo_mo
IF (do_os) nocc = SUM(nelec_spin)*ndo_mo
! Initialize relevent matrices
ALLOCATE (ot_prec)
CALL dbcsr_create(ot_prec, template=matrix_tdp)
CALL cp_fm_struct_create(ortho_struct, context=blacs_env, para_env=para_env, &
nrow_global=nrow, ncol_global=nocc)
CALL cp_fm_create(ortho_space, ortho_struct)
CALL prep_for_ot(evecs, ortho_space, ot_prec, neig, do_sf, donor_state, xas_tdp_env, &
xas_tdp_control, qs_env)
! Prepare the preconditioner
ALLOCATE (precond)
CALL init_preconditioner(precond, para_env, blacs_env)
precond%in_use = ot_precond_full_single ! because applying this conditioner is only a mm
precond%dbcsr_matrix => ot_prec
! Actually solving the eigenvalue problem
CALL ot_eigensolver(matrix_h=matrix_tdp, matrix_s=metric, matrix_c_fm=evecs, &
eps_gradient=eps_iter, iter_max=max_iter, silent=.FALSE., &
ot_settings=xas_tdp_control%ot_settings, &
matrix_orthogonal_space_fm=ortho_space, &
preconditioner=precond)
CALL calculate_subspace_eigenvalues(evecs, matrix_tdp, evals_arg=evals)
! Clean-up
CALL cp_fm_struct_release(ortho_struct)
CALL cp_fm_release(ortho_space)
CALL dbcsr_release(ot_prec)
CALL destroy_preconditioner(precond)
DEALLOCATE (precond)
CALL timestop(handle)
END SUBROUTINE xas_ot_solver
! **************************************************************************************************
!> \brief Prepares all required matrices for the OT eigensolver (precond, ortho space and guesses)
!> \param guess the guess eigenvectors absed on LUMOs, in fm format
!> \param ortho the orthogonal space in fm format (occupied MOs)
!> \param precond the OT preconditioner in DBCSR format
!> \param neig ...
!> \param do_sf ...
!> \param donor_state ...
!> \param xas_tdp_env ...
!> \param xas_tdp_control ...
!> \param qs_env ...
!> \note Matrices are allocate before entry
! **************************************************************************************************
SUBROUTINE prep_for_ot(guess, ortho, precond, neig, do_sf, donor_state, xas_tdp_env, &
xas_tdp_control, qs_env)
TYPE(cp_fm_type), INTENT(IN) :: guess, ortho
TYPE(dbcsr_type) :: precond
INTEGER :: neig
LOGICAL :: do_sf
TYPE(donor_state_type), POINTER :: donor_state
TYPE(xas_tdp_env_type), POINTER :: xas_tdp_env
TYPE(xas_tdp_control_type), POINTER :: xas_tdp_control
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'prep_for_ot'
INTEGER :: blk, handle, i, iblk, ido_mo, ispin, jblk, maxel, minel, nao, natom, ndo_mo, &
nelec_spin(2), nhomo(2), nlumo(2), nspins, start_block, start_col, start_row
LOGICAL :: do_os, found
REAL(dp), DIMENSION(:, :), POINTER :: pblock
TYPE(cp_fm_type), POINTER :: mo_coeff
TYPE(dbcsr_iterator_type) :: iter
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
NULLIFY (mos, mo_coeff, pblock)
!REMINDER on the organization of the xas_tdp matrix. It is DBCSR format, with a super bock
!structure. First block structure is spin quadrants: upper left is alpha-alpha spin and lower
!right is beta-beta spin. Then each quadrants is divided in a ndo_mo x ndo_mo grid (1x1 for 1s,
!2s, 3x3 for 2p). Each block in this grid has the normal DBCSR structure and dist, simply
!replicated. The resulting eigenvectors follow the same logic.
CALL timeset(routineN, handle)
do_os = xas_tdp_control%do_uks .OR. xas_tdp_control%do_roks
nspins = 1; IF (do_os) nspins = 2
ndo_mo = donor_state%ndo_mo
CALL cp_fm_get_info(xas_tdp_env%lumo_evecs(1), nrow_global=nao)
CALL get_qs_env(qs_env, natom=natom, nelectron_spin=nelec_spin)
!Compute the number of guesses for each spins
IF (do_os) THEN
minel = MINLOC(nelec_spin, 1)
maxel = 3 - minel
nlumo(minel) = (neig/ndo_mo + nelec_spin(maxel) - nelec_spin(minel))/2
nlumo(maxel) = neig/ndo_mo - nlumo(minel)
ELSE
nlumo(1) = neig/ndo_mo
END IF
!Building the guess vectors based on the LUMOs. Copy LUMOs into approriate spin/do_mo
!quadrant/block. Order within a block does not matter
!Note: in spin-flip, the upper left quadrant is for beta-alpha transition, so guess are alpha LUMOs
start_row = 0
start_col = 0
DO ispin = 1, nspins
DO ido_mo = 1, ndo_mo
CALL cp_fm_to_fm_submat(msource=xas_tdp_env%lumo_evecs(ispin), mtarget=guess, &
nrow=nao, ncol=nlumo(ispin), s_firstrow=1, s_firstcol=1, &
t_firstrow=start_row + 1, t_firstcol=start_col + 1)
start_row = start_row + nao
start_col = start_col + nlumo(ispin)
END DO
END DO
!Build the orthogonal space according to the same principles, but based on occupied MOs
!Note: in spin-flip, the upper left quadrant is for beta-alpha transition, so ortho space is beta HOMOs
CALL get_qs_env(qs_env, mos=mos)
nhomo = 0
DO ispin = 1, nspins
CALL get_mo_set(mos(ispin), homo=nhomo(ispin))
END DO
start_row = 0
start_col = 0
DO i = 1, nspins
ispin = i; IF (do_sf) ispin = 3 - i
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
DO ido_mo = 1, ndo_mo
CALL cp_fm_to_fm_submat(msource=mo_coeff, mtarget=ortho, nrow=nao, ncol=nhomo(ispin), &
s_firstrow=1, s_firstcol=1, &
t_firstrow=start_row + 1, t_firstcol=start_col + 1)
start_row = start_row + nao
start_col = start_col + nhomo(ispin)
END DO
END DO
!Build the preconditioner. Copy the "canonical" pre-computed matrix into the proper spin/do_mo
!quadrants/blocks. The end matrix is purely block diagonal
DO ispin = 1, nspins
CALL dbcsr_iterator_start(iter, xas_tdp_env%ot_prec(ispin)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, row=iblk, column=jblk, blk=blk)
CALL dbcsr_get_block_p(xas_tdp_env%ot_prec(ispin)%matrix, iblk, jblk, pblock, found)
IF (found) THEN
start_block = (ispin - 1)*ndo_mo*natom
DO ido_mo = 1, ndo_mo
CALL dbcsr_put_block(precond, start_block + iblk, start_block + jblk, pblock)
start_block = start_block + natom
END DO
END IF
END DO !dbcsr iter
CALL dbcsr_iterator_stop(iter)
END DO
CALL dbcsr_finalize(precond)
CALL timestop(handle)
END SUBROUTINE prep_for_ot
! **************************************************************************************************
!> \brief Returns the scaling to apply to normalize the LR eigenvectors.
!> \param scaling the scaling array to apply
!> \param lr_coeffs the linear response coefficients as a fm
!> \param donor_state ...
!> \note The LR coeffs are normalized when c^T G c = +- 1, G is the metric, c = c^- for TDA and
!> c = c^+ - c^- for the full problem
! **************************************************************************************************
SUBROUTINE get_normal_scaling(scaling, lr_coeffs, donor_state)
REAL(dp), ALLOCATABLE, DIMENSION(:) :: scaling
TYPE(cp_fm_type), INTENT(IN) :: lr_coeffs
TYPE(donor_state_type), POINTER :: donor_state
INTEGER :: nrow, nscal, nvals
REAL(dp), ALLOCATABLE, DIMENSION(:) :: diag
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_type), POINTER :: norm_struct, work_struct
TYPE(cp_fm_type) :: fm_norm, work
TYPE(mp_para_env_type), POINTER :: para_env
NULLIFY (para_env, blacs_env, norm_struct, work_struct)
! Creating the matrix structures and initializing the work matrices
CALL cp_fm_get_info(lr_coeffs, context=blacs_env, para_env=para_env, &
matrix_struct=work_struct, ncol_global=nvals, nrow_global=nrow)
CALL cp_fm_struct_create(norm_struct, para_env=para_env, context=blacs_env, &
nrow_global=nvals, ncol_global=nvals)
CALL cp_fm_create(work, work_struct)
CALL cp_fm_create(fm_norm, norm_struct)
! Taking c^T * G * C
CALL cp_dbcsr_sm_fm_multiply(donor_state%metric(1)%matrix, lr_coeffs, work, ncol=nvals)
CALL parallel_gemm('T', 'N', nvals, nvals, nrow, 1.0_dp, lr_coeffs, work, 0.0_dp, fm_norm)
! Computing the needed scaling
ALLOCATE (diag(nvals))
CALL cp_fm_get_diag(fm_norm, diag)
WHERE (ABS(diag) > 1.0E-8_dp) diag = 1.0_dp/SQRT(ABS(diag))
nscal = SIZE(scaling)
scaling(1:nscal) = diag(1:nscal)
! Clean-up
CALL cp_fm_release(work)
CALL cp_fm_release(fm_norm)
CALL cp_fm_struct_release(norm_struct)