-
Notifications
You must be signed in to change notification settings - Fork 0
/
almo_scf_qs.F
1605 lines (1350 loc) · 73.1 KB
/
almo_scf_qs.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-2023 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Interface between ALMO SCF and QS
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
MODULE almo_scf_qs
USE almo_scf_types, ONLY: almo_mat_dim_aobasis,&
almo_mat_dim_occ,&
almo_mat_dim_virt,&
almo_mat_dim_virt_disc,&
almo_mat_dim_virt_full,&
almo_scf_env_type
USE atomic_kind_types, ONLY: get_atomic_kind
USE cell_types, ONLY: cell_type,&
pbc
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_cp2k_link, ONLY: cp_dbcsr_alloc_block_from_nbl
USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set
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_release,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE cp_units, ONLY: cp_unit_to_cp2k
USE dbcsr_api, ONLY: &
dbcsr_complete_redistribute, dbcsr_copy, dbcsr_copy_into_existing, dbcsr_create, &
dbcsr_desymmetrize, dbcsr_distribution_get, dbcsr_distribution_new, &
dbcsr_distribution_release, dbcsr_distribution_type, dbcsr_filter, dbcsr_finalize, &
dbcsr_get_block_p, dbcsr_get_info, dbcsr_get_num_blocks, dbcsr_get_stored_coordinates, &
dbcsr_multiply, dbcsr_nblkcols_total, dbcsr_nblkrows_total, dbcsr_p_type, dbcsr_release, &
dbcsr_reserve_block2d, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, dbcsr_work_create
USE input_constants, ONLY: almo_constraint_ao_overlap,&
almo_constraint_block_diagonal,&
almo_constraint_distance,&
almo_domain_layout_molecular,&
almo_mat_distr_atomic,&
almo_mat_distr_molecular,&
do_bondparm_covalent,&
do_bondparm_vdw
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_comm_type
USE molecule_types, ONLY: get_molecule_set_info,&
molecule_type
USE particle_types, ONLY: particle_type
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_ks_methods, ONLY: qs_ks_update_qs_env
USE qs_ks_types, ONLY: qs_ks_did_change,&
qs_ks_env_type,&
set_ks_env
USE qs_mo_types, ONLY: allocate_mo_set,&
deallocate_mo_set,&
init_mo_set,&
mo_set_type
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type
USE qs_rho_methods, ONLY: qs_rho_update_rho
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE qs_scf_types, ONLY: qs_scf_env_type,&
scf_env_create
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'almo_scf_qs'
PUBLIC :: matrix_almo_create, &
almo_scf_construct_quencher, &
calculate_w_matrix_almo, &
init_almo_ks_matrix_via_qs, &
almo_scf_update_ks_energy, &
construct_qs_mos, &
matrix_qs_to_almo, &
almo_dm_to_almo_ks, &
almo_dm_to_qs_env
CONTAINS
! **************************************************************************************************
!> \brief create the ALMO matrix templates
!> \param matrix_new ...
!> \param matrix_qs ...
!> \param almo_scf_env ...
!> \param name_new ...
!> \param size_keys ...
!> \param symmetry_new ...
!> \param spin_key ...
!> \param init_domains ...
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE matrix_almo_create(matrix_new, matrix_qs, almo_scf_env, &
name_new, size_keys, symmetry_new, &
spin_key, init_domains)
TYPE(dbcsr_type) :: matrix_new, matrix_qs
TYPE(almo_scf_env_type), INTENT(IN) :: almo_scf_env
CHARACTER(len=*), INTENT(IN) :: name_new
INTEGER, DIMENSION(2), INTENT(IN) :: size_keys
CHARACTER, INTENT(IN) :: symmetry_new
INTEGER, INTENT(IN) :: spin_key
LOGICAL, INTENT(IN) :: init_domains
CHARACTER(len=*), PARAMETER :: routineN = 'matrix_almo_create'
INTEGER :: dimen, handle, hold, iatom, iblock_col, &
iblock_row, imol, mynode, natoms, &
nblkrows_tot, nlength, nmols, row
INTEGER, DIMENSION(:), POINTER :: blk_distr, blk_sizes, block_sizes_new, col_distr_new, &
col_sizes_new, distr_new_array, row_distr_new, row_sizes_new
LOGICAL :: active, one_dim_is_mo, tr
REAL(KIND=dp), DIMENSION(:, :), POINTER :: p_new_block
TYPE(dbcsr_distribution_type) :: dist_new, dist_qs
! dimension size: AO, MO, etc
! almo_mat_dim_aobasis - no. of AOs,
! almo_mat_dim_occ - no. of occupied MOs
! almo_mat_dim_domains - no. of domains
! symmetry type: dbcsr_type_no_symmetry, dbcsr_type_symmetric,
! dbcsr_type_antisymmetric, dbcsr_type_hermitian, dbcsr_type_antihermitian
! (see dbcsr_lib/dbcsr_types.F for other values)
! spin_key: either 1 or 2 (0 is allowed for matrics in the AO basis)
! TYPE(dbcsr_iterator_type) :: iter
! REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: allones
!-----------------------------------------------------------------------
CALL timeset(routineN, handle)
! RZK-warning The structure of the matrices can be optimized:
! 1. Diagonal matrices must be distributed evenly over the processes.
! This can be achieved by distributing cpus: 012012-rows and 001122-cols
! block_diagonal_flag is introduced but not used
! 2. Multiplication of diagonally dominant matrices will be faster
! if the diagonal blocks are local to the same processes.
! 3. Systems of molecules of drastically different sizes might need
! better distribution.
! obtain distribution from the qs matrix - it might be useful
! to get the structure of the AO dimensions
CALL dbcsr_get_info(matrix_qs, distribution=dist_qs)
natoms = almo_scf_env%natoms
nmols = almo_scf_env%nmolecules
DO dimen = 1, 2 ! 1 - row, 2 - column dimension
! distribution pattern is the same for all matrix types (ao, occ, virt)
IF (dimen == 1) THEN !rows
CALL dbcsr_distribution_get(dist_qs, row_dist=blk_distr)
ELSE !columns
CALL dbcsr_distribution_get(dist_qs, col_dist=blk_distr)
END IF
IF (size_keys(dimen) == almo_mat_dim_aobasis) THEN ! this dimension is AO
! structure of an AO dimension can be copied from matrix_qs
CALL dbcsr_get_info(matrix_qs, row_blk_size=blk_sizes)
! atomic clustering of AOs
IF (almo_scf_env%mat_distr_aos == almo_mat_distr_atomic) THEN
ALLOCATE (block_sizes_new(natoms), distr_new_array(natoms))
block_sizes_new(:) = blk_sizes(:)
distr_new_array(:) = blk_distr(:)
! molecular clustering of AOs
ELSE IF (almo_scf_env%mat_distr_aos == almo_mat_distr_molecular) THEN
ALLOCATE (block_sizes_new(nmols), distr_new_array(nmols))
block_sizes_new(:) = 0
DO iatom = 1, natoms
block_sizes_new(almo_scf_env%domain_index_of_atom(iatom)) = &
block_sizes_new(almo_scf_env%domain_index_of_atom(iatom)) + &
blk_sizes(iatom)
END DO
DO imol = 1, nmols
distr_new_array(imol) = &
blk_distr(almo_scf_env%first_atom_of_domain(imol))
END DO
ELSE
CPABORT("Illegal distribution")
END IF
ELSE ! this dimension is not AO
IF (size_keys(dimen) == almo_mat_dim_occ .OR. &
size_keys(dimen) == almo_mat_dim_virt .OR. &
size_keys(dimen) == almo_mat_dim_virt_disc .OR. &
size_keys(dimen) == almo_mat_dim_virt_full) THEN ! this dim is MO
! atomic clustering of MOs
IF (almo_scf_env%mat_distr_mos == almo_mat_distr_atomic) THEN
nlength = natoms
ALLOCATE (block_sizes_new(nlength))
block_sizes_new(:) = 0
IF (size_keys(dimen) == almo_mat_dim_occ) THEN
! currently distributing atomic distr of mos is not allowed
! RZK-warning define nocc_of_atom and nvirt_atom to implement it
!block_sizes_new(:)=almo_scf_env%nocc_of_atom(:,spin_key)
ELSE IF (size_keys(dimen) == almo_mat_dim_virt) THEN
!block_sizes_new(:)=almo_scf_env%nvirt_of_atom(:,spin_key)
END IF
! molecular clustering of MOs
ELSE IF (almo_scf_env%mat_distr_mos == almo_mat_distr_molecular) THEN
nlength = nmols
ALLOCATE (block_sizes_new(nlength))
IF (size_keys(dimen) == almo_mat_dim_occ) THEN
block_sizes_new(:) = almo_scf_env%nocc_of_domain(:, spin_key)
! Handle zero-electron fragments by adding one-orbital that
! must remain zero at all times
WHERE (block_sizes_new == 0) block_sizes_new = 1
ELSE IF (size_keys(dimen) == almo_mat_dim_virt_disc) THEN
block_sizes_new(:) = almo_scf_env%nvirt_disc_of_domain(:, spin_key)
ELSE IF (size_keys(dimen) == almo_mat_dim_virt_full) THEN
block_sizes_new(:) = almo_scf_env%nvirt_full_of_domain(:, spin_key)
ELSE IF (size_keys(dimen) == almo_mat_dim_virt) THEN
block_sizes_new(:) = almo_scf_env%nvirt_of_domain(:, spin_key)
END IF
ELSE
CPABORT("Illegal distribution")
END IF
ELSE
CPABORT("Illegal dimension")
END IF ! end choosing dim size (occ, virt)
! distribution for MOs is copied from AOs
ALLOCATE (distr_new_array(nlength))
! atomic clustering
IF (almo_scf_env%mat_distr_mos == almo_mat_distr_atomic) THEN
distr_new_array(:) = blk_distr(:)
! molecular clustering
ELSE IF (almo_scf_env%mat_distr_mos == almo_mat_distr_molecular) THEN
DO imol = 1, nmols
distr_new_array(imol) = &
blk_distr(almo_scf_env%first_atom_of_domain(imol))
END DO
END IF
END IF ! end choosing dimension size (AOs vs .NOT.AOs)
! create final arrays
IF (dimen == 1) THEN !rows
row_sizes_new => block_sizes_new
row_distr_new => distr_new_array
ELSE !columns
col_sizes_new => block_sizes_new
col_distr_new => distr_new_array
END IF
END DO ! both rows and columns are done
! Create the distribution
CALL dbcsr_distribution_new(dist_new, template=dist_qs, &
row_dist=row_distr_new, col_dist=col_distr_new, &
reuse_arrays=.TRUE.)
! Create the matrix
CALL dbcsr_create(matrix_new, name_new, &
dist_new, symmetry_new, &
row_sizes_new, col_sizes_new, reuse_arrays=.TRUE.)
CALL dbcsr_distribution_release(dist_new)
! fill out reqired blocks with 1.0_dp to tell the dbcsr library
! which blocks to keep
IF (init_domains) THEN
CALL dbcsr_distribution_get(dist_new, mynode=mynode)
CALL dbcsr_work_create(matrix_new, work_mutable=.TRUE.)
! startQQQ - this part of the code scales quadratically
! therefore it is replaced with a less general but linear scaling algorithm below
! the quadratic algorithm is kept to be re-written later
!QQQnblkrows_tot = dbcsr_nblkrows_total(matrix_new)
!QQQnblkcols_tot = dbcsr_nblkcols_total(matrix_new)
!QQQDO row = 1, nblkrows_tot
!QQQ DO col = 1, nblkcols_tot
!QQQ tr = .FALSE.
!QQQ iblock_row = row
!QQQ iblock_col = col
!QQQ CALL dbcsr_get_stored_coordinates(matrix_new, iblock_row, iblock_col, tr, hold)
!QQQ IF(hold.EQ.mynode) THEN
!QQQ
!QQQ ! RZK-warning replace with a function which says if this
!QQQ ! distribution block is active or not
!QQQ ! Translate indeces of distribution blocks to domain blocks
!QQQ if (size_keys(1)==almo_mat_dim_aobasis) then
!QQQ domain_row=almo_scf_env%domain_index_of_ao_block(iblock_row)
!QQQ else if (size_keys(2)==almo_mat_dim_occ .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt_disc .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt_full) then
!QQQ domain_row=almo_scf_env%domain_index_of_mo_block(iblock_row)
!QQQ else
!QQQ CPErrorMessage(cp_failure_level,routineP,"Illegal dimension")
!QQQ CPPrecondition(.FALSE.,cp_failure_level,routineP,failure)
!QQQ endif
!QQQ if (size_keys(2)==almo_mat_dim_aobasis) then
!QQQ domain_col=almo_scf_env%domain_index_of_ao_block(iblock_col)
!QQQ else if (size_keys(2)==almo_mat_dim_occ .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt_disc .OR. &
!QQQ size_keys(2)==almo_mat_dim_virt_full) then
!QQQ domain_col=almo_scf_env%domain_index_of_mo_block(iblock_col)
!QQQ else
!QQQ CPErrorMessage(cp_failure_level,routineP,"Illegal dimension")
!QQQ CPPrecondition(.FALSE.,cp_failure_level,routineP,failure)
!QQQ endif
!QQQ ! Finds if we need this block
!QQQ ! only the block-diagonal constraint is implemented here
!QQQ active=.false.
!QQQ if (domain_row==domain_col) active=.true.
!QQQ IF (active) THEN
!QQQ NULLIFY (p_new_block)
!QQQ CALL dbcsr_reserve_block2d(matrix_new, iblock_row, iblock_col, p_new_block)
!QQQ CPPostcondition(ASSOCIATED(p_new_block),cp_failure_level,routineP,failure)
!QQQ p_new_block(:,:) = 1.0_dp
!QQQ ENDIF
!QQQ ENDIF ! mynode
!QQQ ENDDO
!QQQENDDO
!QQQtake care of zero-electron fragments
! endQQQ - end of the quadratic part
! start linear-scaling replacement:
! works only for molecular blocks AND molecular distributions
nblkrows_tot = dbcsr_nblkrows_total(matrix_new)
DO row = 1, nblkrows_tot
tr = .FALSE.
iblock_row = row
iblock_col = row
CALL dbcsr_get_stored_coordinates(matrix_new, iblock_row, iblock_col, hold)
IF (hold .EQ. mynode) THEN
active = .TRUE.
one_dim_is_mo = .FALSE.
DO dimen = 1, 2 ! 1 - row, 2 - column dimension
IF (size_keys(dimen) == almo_mat_dim_occ) one_dim_is_mo = .TRUE.
END DO
IF (one_dim_is_mo) THEN
IF (almo_scf_env%nocc_of_domain(row, spin_key) == 0) active = .FALSE.
END IF
one_dim_is_mo = .FALSE.
DO dimen = 1, 2
IF (size_keys(dimen) == almo_mat_dim_virt) one_dim_is_mo = .TRUE.
END DO
IF (one_dim_is_mo) THEN
IF (almo_scf_env%nvirt_of_domain(row, spin_key) == 0) active = .FALSE.
END IF
one_dim_is_mo = .FALSE.
DO dimen = 1, 2
IF (size_keys(dimen) == almo_mat_dim_virt_disc) one_dim_is_mo = .TRUE.
END DO
IF (one_dim_is_mo) THEN
IF (almo_scf_env%nvirt_disc_of_domain(row, spin_key) == 0) active = .FALSE.
END IF
one_dim_is_mo = .FALSE.
DO dimen = 1, 2
IF (size_keys(dimen) == almo_mat_dim_virt_full) one_dim_is_mo = .TRUE.
END DO
IF (one_dim_is_mo) THEN
IF (almo_scf_env%nvirt_full_of_domain(row, spin_key) == 0) active = .FALSE.
END IF
IF (active) THEN
NULLIFY (p_new_block)
CALL dbcsr_reserve_block2d(matrix_new, iblock_row, iblock_col, p_new_block)
CPASSERT(ASSOCIATED(p_new_block))
p_new_block(:, :) = 1.0_dp
END IF
END IF ! mynode
END DO
! end lnear-scaling replacement
END IF ! init_domains
CALL dbcsr_finalize(matrix_new)
CALL timestop(handle)
END SUBROUTINE matrix_almo_create
! **************************************************************************************************
!> \brief convert between two types of matrices: QS style to ALMO style
!> \param matrix_qs ...
!> \param matrix_almo ...
!> \param mat_distr_aos ...
!> \param keep_sparsity ...
!> \par History
!> 2011.06 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE matrix_qs_to_almo(matrix_qs, matrix_almo, mat_distr_aos, keep_sparsity)
TYPE(dbcsr_type) :: matrix_qs, matrix_almo
INTEGER :: mat_distr_aos
LOGICAL, INTENT(IN) :: keep_sparsity
CHARACTER(len=*), PARAMETER :: routineN = 'matrix_qs_to_almo'
INTEGER :: handle
TYPE(dbcsr_type) :: matrix_qs_nosym
CALL timeset(routineN, handle)
!RZK-warning if it's not a N(AO)xN(AO) matrix then stop
SELECT CASE (mat_distr_aos)
CASE (almo_mat_distr_atomic)
! automatic data_type conversion
CALL dbcsr_copy(matrix_almo, matrix_qs, &
keep_sparsity=keep_sparsity)
CASE (almo_mat_distr_molecular)
! desymmetrize the qs matrix
CALL dbcsr_create(matrix_qs_nosym, template=matrix_qs, &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_desymmetrize(matrix_qs, matrix_qs_nosym)
! perform the magic complete_redistribute
! before calling complete_redistribute set all blocks to zero
! otherwise the non-zero elements of the redistributed matrix,
! which are in zero-blocks of the original matrix, will remain
! in the final redistributed matrix. this is a bug in
! complete_redistribute. RZK-warning it should be later corrected by calling
! dbcsr_set to 0.0 from within complete_redistribute
CALL dbcsr_set(matrix_almo, 0.0_dp)
CALL dbcsr_complete_redistribute(matrix_qs_nosym, matrix_almo, &
keep_sparsity=keep_sparsity);
CALL dbcsr_release(matrix_qs_nosym)
CASE DEFAULT
CPABORT("")
END SELECT
CALL timestop(handle)
END SUBROUTINE matrix_qs_to_almo
! **************************************************************************************************
!> \brief convert between two types of matrices: ALMO style to QS style
!> \param matrix_almo ...
!> \param matrix_qs ...
!> \param mat_distr_aos ...
!> \par History
!> 2011.06 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE matrix_almo_to_qs(matrix_almo, matrix_qs, mat_distr_aos)
TYPE(dbcsr_type) :: matrix_almo, matrix_qs
INTEGER, INTENT(IN) :: mat_distr_aos
CHARACTER(len=*), PARAMETER :: routineN = 'matrix_almo_to_qs'
INTEGER :: handle
CALL timeset(routineN, handle)
! RZK-warning if it's not a N(AO)xN(AO) matrix then stop
SELECT CASE (mat_distr_aos)
CASE (almo_mat_distr_atomic)
CALL dbcsr_copy_into_existing(matrix_qs, matrix_almo)
CASE (almo_mat_distr_molecular)
CALL dbcsr_set(matrix_qs, 0.0_dp)
CALL dbcsr_complete_redistribute(matrix_almo, matrix_qs, keep_sparsity=.TRUE.)
CASE DEFAULT
CPABORT("")
END SELECT
CALL timestop(handle)
END SUBROUTINE matrix_almo_to_qs
! **************************************************************************************************
!> \brief Initialization of the QS and ALMO KS matrix
!> \param qs_env ...
!> \param matrix_ks ...
!> \param mat_distr_aos ...
!> \param eps_filter ...
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE init_almo_ks_matrix_via_qs(qs_env, matrix_ks, mat_distr_aos, eps_filter)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_type), DIMENSION(:) :: matrix_ks
INTEGER :: mat_distr_aos
REAL(KIND=dp) :: eps_filter
CHARACTER(len=*), PARAMETER :: routineN = 'init_almo_ks_matrix_via_qs'
INTEGER :: handle, ispin, nspin
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_qs_ks, matrix_qs_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(qs_ks_env_type), POINTER :: ks_env
CALL timeset(routineN, handle)
NULLIFY (sab_orb)
! get basic quantities from the qs_env
CALL get_qs_env(qs_env, &
dft_control=dft_control, &
matrix_s=matrix_qs_s, &
matrix_ks=matrix_qs_ks, &
ks_env=ks_env, &
sab_orb=sab_orb)
nspin = dft_control%nspins
! create matrix_ks in the QS env if necessary
IF (.NOT. ASSOCIATED(matrix_qs_ks)) THEN
CALL dbcsr_allocate_matrix_set(matrix_qs_ks, nspin)
DO ispin = 1, nspin
ALLOCATE (matrix_qs_ks(ispin)%matrix)
CALL dbcsr_create(matrix_qs_ks(ispin)%matrix, &
template=matrix_qs_s(1)%matrix)
CALL cp_dbcsr_alloc_block_from_nbl(matrix_qs_ks(ispin)%matrix, sab_orb)
CALL dbcsr_set(matrix_qs_ks(ispin)%matrix, 0.0_dp)
END DO
CALL set_ks_env(ks_env, matrix_ks=matrix_qs_ks)
END IF
! copy to ALMO
DO ispin = 1, nspin
CALL matrix_qs_to_almo(matrix_qs_ks(ispin)%matrix, &
matrix_ks(ispin), mat_distr_aos, .FALSE.)
CALL dbcsr_filter(matrix_ks(ispin), eps_filter)
END DO
CALL timestop(handle)
END SUBROUTINE init_almo_ks_matrix_via_qs
! **************************************************************************************************
!> \brief Create MOs in the QS env to be able to return ALMOs to QS
!> \param qs_env ...
!> \param almo_scf_env ...
!> \par History
!> 2016.12 created [Yifei Shi]
!> \author Yifei Shi
! **************************************************************************************************
SUBROUTINE construct_qs_mos(qs_env, almo_scf_env)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(len=*), PARAMETER :: routineN = 'construct_qs_mos'
INTEGER :: handle, ispin, ncol_fm, nrow_fm
TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
TYPE(cp_fm_type) :: mo_fm_copy
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(qs_scf_env_type), POINTER :: scf_env
CALL timeset(routineN, handle)
! create and init scf_env (this is necessary to return MOs to qs)
NULLIFY (mos, fm_struct_tmp, scf_env)
ALLOCATE (scf_env)
CALL scf_env_create(scf_env)
!CALL qs_scf_env_initialize(qs_env, scf_env)
CALL set_qs_env(qs_env, scf_env=scf_env)
CALL get_qs_env(qs_env, dft_control=dft_control, mos=mos)
CALL dbcsr_get_info(almo_scf_env%matrix_t(1), nfullrows_total=nrow_fm, nfullcols_total=ncol_fm)
! allocate and init mo_set
DO ispin = 1, almo_scf_env%nspins
! Currently only fm version of mo_set is usable.
! First transform the matrix_t to fm version
! Empty the containers to prevent memory leaks
CALL deallocate_mo_set(mos(ispin))
CALL allocate_mo_set(mo_set=mos(ispin), &
nao=nrow_fm, &
nmo=ncol_fm, &
nelectron=almo_scf_env%nelectrons_total, &
n_el_f=REAL(almo_scf_env%nelectrons_total, dp), &
maxocc=2.0_dp, &
flexible_electron_count=dft_control%relax_multiplicity)
CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=nrow_fm, ncol_global=ncol_fm, &
context=almo_scf_env%blacs_env, &
para_env=almo_scf_env%para_env)
CALL cp_fm_create(mo_fm_copy, fm_struct_tmp, name="t_orthogonal_converted_to_fm")
CALL cp_fm_struct_release(fm_struct_tmp)
!CALL copy_dbcsr_to_fm(almo_scf_env%matrix_t(ispin), mo_fm_copy)
CALL init_mo_set(mos(ispin), fm_ref=mo_fm_copy, name='fm_mo')
CALL cp_fm_release(mo_fm_copy)
END DO
CALL timestop(handle)
END SUBROUTINE construct_qs_mos
! **************************************************************************************************
!> \brief return density matrix to the qs_env
!> \param qs_env ...
!> \param matrix_p ...
!> \param mat_distr_aos ...
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_dm_to_qs_env(qs_env, matrix_p, mat_distr_aos)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_type), DIMENSION(:) :: matrix_p
INTEGER, INTENT(IN) :: mat_distr_aos
CHARACTER(len=*), PARAMETER :: routineN = 'almo_dm_to_qs_env'
INTEGER :: handle, ispin, nspins
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
TYPE(qs_rho_type), POINTER :: rho
CALL timeset(routineN, handle)
NULLIFY (rho, rho_ao)
nspins = SIZE(matrix_p)
CALL get_qs_env(qs_env, rho=rho)
CALL qs_rho_get(rho, rho_ao=rho_ao)
! set the new density matrix
DO ispin = 1, nspins
CALL matrix_almo_to_qs(matrix_p(ispin), &
rho_ao(ispin)%matrix, &
mat_distr_aos)
END DO
CALL qs_rho_update_rho(rho, qs_env=qs_env)
CALL qs_ks_did_change(qs_env%ks_env, rho_changed=.TRUE.)
CALL timestop(handle)
END SUBROUTINE almo_dm_to_qs_env
! **************************************************************************************************
!> \brief uses the ALMO density matrix
!> to compute KS matrix (inside QS environment) and the new energy
!> \param qs_env ...
!> \param matrix_p ...
!> \param energy_total ...
!> \param mat_distr_aos ...
!> \param smear ...
!> \param kTS_sum ...
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_dm_to_qs_ks(qs_env, matrix_p, energy_total, mat_distr_aos, smear, kTS_sum)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_type), DIMENSION(:) :: matrix_p
REAL(KIND=dp) :: energy_total
INTEGER, INTENT(IN) :: mat_distr_aos
LOGICAL, INTENT(IN), OPTIONAL :: smear
REAL(KIND=dp), INTENT(IN), OPTIONAL :: kTS_sum
CHARACTER(len=*), PARAMETER :: routineN = 'almo_dm_to_qs_ks'
INTEGER :: handle
LOGICAL :: smearing
REAL(KIND=dp) :: entropic_term
TYPE(qs_energy_type), POINTER :: energy
CALL timeset(routineN, handle)
IF (PRESENT(smear)) THEN
smearing = smear
ELSE
smearing = .FALSE.
END IF
IF (PRESENT(kTS_sum)) THEN
entropic_term = kTS_sum
ELSE
entropic_term = 0.0_dp
END IF
NULLIFY (energy)
CALL get_qs_env(qs_env, energy=energy)
CALL almo_dm_to_qs_env(qs_env, matrix_p, mat_distr_aos)
CALL qs_ks_update_qs_env(qs_env, calculate_forces=.FALSE., just_energy=.FALSE., &
print_active=.TRUE.)
!! Add electronic entropy contribution if smearing is requested
!! Previous QS entropy is replaced by the sum of the entropy for each spin
IF (smearing) THEN
energy%total = energy%total - energy%kTS + entropic_term
END IF
energy_total = energy%total
CALL timestop(handle)
END SUBROUTINE almo_dm_to_qs_ks
! **************************************************************************************************
!> \brief uses the ALMO density matrix
!> to compute ALMO KS matrix and the new energy
!> \param qs_env ...
!> \param matrix_p ...
!> \param matrix_ks ...
!> \param energy_total ...
!> \param eps_filter ...
!> \param mat_distr_aos ...
!> \param smear ...
!> \param kTS_sum ...
!> \par History
!> 2011.05 created [Rustam Z Khaliullin]
!> 2018.09 smearing support [Ruben Staub]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_dm_to_almo_ks(qs_env, matrix_p, matrix_ks, energy_total, eps_filter, &
mat_distr_aos, smear, kTS_sum)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_type), DIMENSION(:) :: matrix_p, matrix_ks
REAL(KIND=dp) :: energy_total, eps_filter
INTEGER, INTENT(IN) :: mat_distr_aos
LOGICAL, INTENT(IN), OPTIONAL :: smear
REAL(KIND=dp), INTENT(IN), OPTIONAL :: kTS_sum
CHARACTER(len=*), PARAMETER :: routineN = 'almo_dm_to_almo_ks'
INTEGER :: handle, ispin, nspins
LOGICAL :: smearing
REAL(KIND=dp) :: entropic_term
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_qs_ks
CALL timeset(routineN, handle)
IF (PRESENT(smear)) THEN
smearing = smear
ELSE
smearing = .FALSE.
END IF
IF (PRESENT(kTS_sum)) THEN
entropic_term = kTS_sum
ELSE
entropic_term = 0.0_dp
END IF
! update KS matrix in the QS env
CALL almo_dm_to_qs_ks(qs_env, matrix_p, energy_total, mat_distr_aos, &
smear=smearing, &
kTS_sum=entropic_term)
nspins = SIZE(matrix_ks)
! get KS matrix from the QS env and convert to the ALMO format
CALL get_qs_env(qs_env, matrix_ks=matrix_qs_ks)
DO ispin = 1, nspins
CALL matrix_qs_to_almo(matrix_qs_ks(ispin)%matrix, &
matrix_ks(ispin), &
mat_distr_aos, .FALSE.)
CALL dbcsr_filter(matrix_ks(ispin), eps_filter)
END DO
CALL timestop(handle)
END SUBROUTINE almo_dm_to_almo_ks
! **************************************************************************************************
!> \brief update qs_env total energy
!> \param qs_env ...
!> \param energy ...
!> \param energy_singles_corr ...
!> \par History
!> 2013.03 created [Rustam Z Khaliullin]
!> \author Rustam Z Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_update_ks_energy(qs_env, energy, energy_singles_corr)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(KIND=dp), INTENT(IN), OPTIONAL :: energy, energy_singles_corr
TYPE(qs_energy_type), POINTER :: qs_energy
CALL get_qs_env(qs_env, energy=qs_energy)
IF (PRESENT(energy_singles_corr)) THEN
qs_energy%singles_corr = energy_singles_corr
ELSE
qs_energy%singles_corr = 0.0_dp
END IF
IF (PRESENT(energy)) THEN
qs_energy%total = energy
END IF
qs_energy%total = qs_energy%total + qs_energy%singles_corr
END SUBROUTINE almo_scf_update_ks_energy
! **************************************************************************************************
!> \brief Creates the matrix that imposes absolute locality on MOs
!> \param qs_env ...
!> \param almo_scf_env ...
!> \par History
!> 2011.11 created [Rustam Z. Khaliullin]
!> \author Rustam Z. Khaliullin
! **************************************************************************************************
SUBROUTINE almo_scf_construct_quencher(qs_env, almo_scf_env)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(almo_scf_env_type), INTENT(INOUT) :: almo_scf_env
CHARACTER(len=*), PARAMETER :: routineN = 'almo_scf_construct_quencher'
CHARACTER :: sym
INTEGER :: col, contact_atom_1, contact_atom_2, domain_col, domain_map_local_entries, &
domain_row, global_entries, global_list_length, grid1, GroupID, handle, hold, iatom, &
iatom2, iblock_col, iblock_row, idomain, idomain2, ientry, igrid, ineig, ineighbor, &
iNode, inode2, ipair, ispin, jatom, jatom2, jdomain2, local_list_length, &
max_domain_neighbors, max_neig, mynode, nblkcols_tot, nblkrows_tot, nblks, ndomains, &
neig_temp, nnode2, nNodes, row, unit_nr
INTEGER, ALLOCATABLE, DIMENSION(:) :: current_number_neighbors, domain_entries_cpu, &
domain_map_global, domain_map_local, first_atom_of_molecule, global_list, &
last_atom_of_molecule, list_length_cpu, list_offset_cpu, local_list, offset_for_cpu
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: domain_grid, domain_neighbor_list, &
domain_neighbor_list_excessive
LOGICAL :: already_listed, block_active, &
delayed_increment, found, &
max_neig_fails, tr
REAL(KIND=dp) :: contact1_radius, contact2_radius, &
distance, distance_squared, overlap, &
r0, r1, s0, s1, trial_distance_squared
REAL(KIND=dp), DIMENSION(3) :: rab
REAL(KIND=dp), DIMENSION(:, :), POINTER :: p_new_block
TYPE(cell_type), POINTER :: cell
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_distribution_type) :: dist
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
TYPE(dbcsr_type) :: matrix_s_sym
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(mp_comm_type) :: group
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator, nl_iterator2
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_almo
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
CALL timeset(routineN, handle)
! get a useful output_unit
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
unit_nr = -1
END IF
ndomains = almo_scf_env%ndomains
CALL get_qs_env(qs_env=qs_env, &
particle_set=particle_set, &
molecule_set=molecule_set, &
cell=cell, &
matrix_s=matrix_s, &
sab_almo=sab_almo)
! if we are dealing with molecules get info about them
IF (almo_scf_env%domain_layout_mos == almo_domain_layout_molecular .OR. &
almo_scf_env%domain_layout_aos == almo_domain_layout_molecular) THEN
ALLOCATE (first_atom_of_molecule(almo_scf_env%nmolecules))
ALLOCATE (last_atom_of_molecule(almo_scf_env%nmolecules))
CALL get_molecule_set_info(molecule_set, &
mol_to_first_atom=first_atom_of_molecule, &
mol_to_last_atom=last_atom_of_molecule)
END IF
! create a symmetrized copy of the ao overlap
CALL dbcsr_create(matrix_s_sym, &
template=almo_scf_env%matrix_s(1), &
matrix_type=dbcsr_type_no_symmetry)
CALL dbcsr_get_info(almo_scf_env%matrix_s(1), &
matrix_type=sym)
IF (sym .EQ. dbcsr_type_no_symmetry) THEN
CALL dbcsr_copy(matrix_s_sym, almo_scf_env%matrix_s(1))
ELSE
CALL dbcsr_desymmetrize(almo_scf_env%matrix_s(1), &
matrix_s_sym)
END IF
ALLOCATE (almo_scf_env%quench_t(almo_scf_env%nspins))
ALLOCATE (almo_scf_env%domain_map(almo_scf_env%nspins))
!DO ispin=1,almo_scf_env%nspins
ispin = 1
! create the sparsity template for the occupied orbitals
CALL matrix_almo_create(matrix_new=almo_scf_env%quench_t(ispin), &
matrix_qs=matrix_s(1)%matrix, &
almo_scf_env=almo_scf_env, &
name_new="T_QUENCHER", &
size_keys=(/almo_mat_dim_aobasis, almo_mat_dim_occ/), &
symmetry_new=dbcsr_type_no_symmetry, &
spin_key=ispin, &
init_domains=.FALSE.)
! initialize distance quencher
CALL dbcsr_work_create(almo_scf_env%quench_t(ispin), &
work_mutable=.TRUE.)
nblkrows_tot = dbcsr_nblkrows_total(almo_scf_env%quench_t(ispin))
nblkcols_tot = dbcsr_nblkcols_total(almo_scf_env%quench_t(ispin))
CALL dbcsr_get_info(almo_scf_env%quench_t(ispin), distribution=dist)
CALL dbcsr_distribution_get(dist, numnodes=nNodes, group=GroupID, mynode=mynode)
CALL group%set_handle(groupid)
! create global atom neighbor list from the local lists
! first, calculate number of local pairs
local_list_length = 0
CALL neighbor_list_iterator_create(nl_iterator, sab_almo)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
! nnode - total number of neighbors for iatom
! inode - current neighbor count
CALL get_iterator_info(nl_iterator, &
iatom=iatom2, jatom=jatom2, inode=inode2, nnode=nnode2)
!WRITE(*,*) "GET INFO: ",iatom2, jatom2, inode2, nnode2
IF (inode2 == 1) THEN
local_list_length = local_list_length + nnode2
END IF
END DO
CALL neighbor_list_iterator_release(nl_iterator)
! second, extract the local list to an array
ALLOCATE (local_list(2*local_list_length))
local_list(:) = 0
local_list_length = 0
CALL neighbor_list_iterator_create(nl_iterator2, sab_almo)
DO WHILE (neighbor_list_iterate(nl_iterator2) == 0)
CALL get_iterator_info(nl_iterator2, &
iatom=iatom2, jatom=jatom2)
local_list(2*local_list_length + 1) = iatom2
local_list(2*local_list_length + 2) = jatom2
local_list_length = local_list_length + 1
END DO ! end loop over pairs of atoms
CALL neighbor_list_iterator_release(nl_iterator2)
! third, communicate local length to the other nodes
ALLOCATE (list_length_cpu(nNodes), list_offset_cpu(nNodes))
CALL group%allgather(2*local_list_length, list_length_cpu)
! fourth, create a global list
list_offset_cpu(1) = 0
DO iNode = 2, nNodes
list_offset_cpu(iNode) = list_offset_cpu(iNode - 1) + &
list_length_cpu(iNode - 1)
END DO
global_list_length = list_offset_cpu(nNodes) + list_length_cpu(nNodes)
! fifth, communicate all list data
ALLOCATE (global_list(global_list_length))
CALL group%allgatherv(local_list, global_list, &
list_length_cpu, list_offset_cpu)
DEALLOCATE (list_length_cpu, list_offset_cpu)
DEALLOCATE (local_list)
! calculate maximum number of atoms surrounding the domain
ALLOCATE (current_number_neighbors(almo_scf_env%ndomains))