forked from cp2k/cp2k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admm_methods.F
4173 lines (3487 loc) · 200 KB
/
admm_methods.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 Contains ADMM methods which require molecular orbitals
!> \par History
!> 04.2008 created [Manuel Guidon]
!> 12.2019 Made GAPW compatible [A. Bussy]
!> \author Manuel Guidon
! **************************************************************************************************
MODULE admm_methods
USE admm_types, ONLY: admm_gapw_type,&
admm_type,&
get_admm_env
USE atomic_kind_types, ONLY: atomic_kind_type
USE bibliography, ONLY: Merlot2014,&
cite_reference
USE cp_cfm_basic_linalg, ONLY: cp_cfm_cholesky_decompose,&
cp_cfm_cholesky_invert,&
cp_cfm_scale,&
cp_cfm_scale_and_add,&
cp_cfm_scale_and_add_fm,&
cp_cfm_transpose
USE cp_cfm_types, ONLY: cp_cfm_create,&
cp_cfm_get_info,&
cp_cfm_release,&
cp_cfm_to_fm,&
cp_cfm_type,&
cp_fm_to_cfm
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: copy_dbcsr_to_fm,&
copy_fm_to_dbcsr,&
cp_dbcsr_plus_fm_fm_t,&
dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE cp_dbcsr_output, ONLY: cp_dbcsr_write_sparse_matrix
USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale,&
cp_fm_scale,&
cp_fm_scale_and_add,&
cp_fm_schur_product,&
cp_fm_upper_to_full
USE cp_fm_cholesky, ONLY: cp_fm_cholesky_decompose,&
cp_fm_cholesky_invert,&
cp_fm_cholesky_reduce,&
cp_fm_cholesky_restore
USE cp_fm_diag, ONLY: cp_fm_syevd
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: &
copy_info_type, cp_fm_cleanup_copy_general, cp_fm_create, cp_fm_finish_copy_general, &
cp_fm_get_info, cp_fm_release, cp_fm_set_all, cp_fm_set_element, cp_fm_start_copy_general, &
cp_fm_to_fm, cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE dbcsr_api, ONLY: &
dbcsr_add, dbcsr_copy, dbcsr_create, dbcsr_deallocate_matrix, dbcsr_desymmetrize, &
dbcsr_dot, dbcsr_get_block_p, dbcsr_iterator_blocks_left, dbcsr_iterator_next_block, &
dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, dbcsr_p_type, &
dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric, &
dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE input_constants, ONLY: do_admm_purify_cauchy,&
do_admm_purify_cauchy_subspace,&
do_admm_purify_mo_diag,&
do_admm_purify_mo_no_diag,&
do_admm_purify_none
USE input_section_types, ONLY: section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_string_length,&
dp
USE kpoint_methods, ONLY: kpoint_density_matrices,&
kpoint_density_transform,&
rskp_transform
USE kpoint_types, ONLY: get_kpoint_env,&
get_kpoint_info,&
kpoint_env_type,&
kpoint_type
USE mathconstants, ONLY: gaussi,&
z_one,&
z_zero
USE message_passing, ONLY: mp_para_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE pw_types, ONLY: pw_type
USE qs_collocate_density, ONLY: calculate_rho_elec
USE qs_energy_types, ONLY: qs_energy_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_force_types, ONLY: add_qs_force,&
qs_force_type
USE qs_gapw_densities, ONLY: prepare_gapw_den
USE qs_ks_atom, ONLY: update_ks_atom
USE qs_ks_types, ONLY: qs_ks_env_type
USE qs_local_rho_types, ONLY: local_rho_set_create,&
local_rho_set_release,&
local_rho_type
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
USE qs_overlap, ONLY: build_overlap_force
USE qs_rho_atom_methods, ONLY: allocate_rho_atom_internals,&
calculate_rho_atom_coeff
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_set,&
qs_rho_type
USE qs_scf_types, ONLY: qs_scf_env_type
USE qs_vxc, ONLY: qs_vxc_create
USE qs_vxc_atom, ONLY: calculate_vxc_atom
USE task_list_types, ONLY: task_list_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: admm_mo_calc_rho_aux, &
admm_mo_calc_rho_aux_kp, &
admm_mo_merge_ks_matrix, &
admm_mo_merge_derivs, &
admm_aux_response_density, &
calc_mixed_overlap_force, &
scale_dm, &
admm_fit_mo_coeffs, &
admm_update_ks_atom, &
calc_admm_mo_derivatives, &
calc_admm_ovlp_forces, &
calc_admm_ovlp_forces_kp, &
admm_projection_derivative, &
kpoint_calc_admm_matrices
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'admm_methods'
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE admm_mo_calc_rho_aux(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'admm_mo_calc_rho_aux'
CHARACTER(LEN=default_string_length) :: basis_type
INTEGER :: handle, ispin
LOGICAL :: gapw, s_mstruct_changed
REAL(KIND=dp), DIMENSION(:), POINTER :: tot_rho_r_aux
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s, matrix_s_aux_fit, &
matrix_s_aux_fit_vs_orb, rho_ao, &
rho_ao_aux
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(pw_type), DIMENSION(:), POINTER :: rho_g_aux, rho_r_aux
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_rho_type), POINTER :: rho, rho_aux_fit
TYPE(task_list_type), POINTER :: task_list
CALL timeset(routineN, handle)
NULLIFY (ks_env, admm_env, mos, mos_aux_fit, matrix_s_aux_fit, &
matrix_s_aux_fit_vs_orb, matrix_s, rho, rho_aux_fit, para_env)
NULLIFY (rho_g_aux, rho_r_aux, rho_ao, rho_ao_aux, tot_rho_r_aux, task_list)
CALL get_qs_env(qs_env, &
ks_env=ks_env, &
admm_env=admm_env, &
dft_control=dft_control, &
mos=mos, &
matrix_s=matrix_s, &
para_env=para_env, &
s_mstruct_changed=s_mstruct_changed, &
rho=rho)
CALL get_admm_env(admm_env, mos_aux_fit=mos_aux_fit, matrix_s_aux_fit=matrix_s_aux_fit, &
matrix_s_aux_fit_vs_orb=matrix_s_aux_fit_vs_orb, rho_aux_fit=rho_aux_fit)
CALL qs_rho_get(rho, rho_ao=rho_ao)
CALL qs_rho_get(rho_aux_fit, &
rho_ao=rho_ao_aux, &
rho_g=rho_g_aux, &
rho_r=rho_r_aux, &
tot_rho_r=tot_rho_r_aux)
gapw = admm_env%do_gapw
! convert mos from full to dbcsr matrices
DO ispin = 1, dft_control%nspins
IF (mos(ispin)%use_mo_coeff_b) THEN
CALL copy_dbcsr_to_fm(mos(ispin)%mo_coeff_b, mos(ispin)%mo_coeff)
END IF
END DO
! fit mo coeffcients
CALL admm_fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_aux_fit_vs_orb, &
mos, mos_aux_fit, s_mstruct_changed)
DO ispin = 1, dft_control%nspins
IF (admm_env%block_dm) THEN
CALL blockify_density_matrix(admm_env, &
density_matrix=rho_ao(ispin)%matrix, &
density_matrix_aux=rho_ao_aux(ispin)%matrix, &
ispin=ispin, &
nspins=dft_control%nspins)
ELSE
! Here, the auxiliary DM gets calculated and is written into rho_aux_fit%...
CALL calculate_dm_mo_no_diag(admm_env, &
mo_set=mos(ispin), &
overlap_matrix=matrix_s_aux_fit(1)%matrix, &
density_matrix=rho_ao_aux(ispin)%matrix, &
overlap_matrix_large=matrix_s(1)%matrix, &
density_matrix_large=rho_ao(ispin)%matrix, &
ispin=ispin)
END IF
IF (admm_env%purification_method == do_admm_purify_cauchy) &
CALL purify_dm_cauchy(admm_env, &
mo_set=mos_aux_fit(ispin), &
density_matrix=rho_ao_aux(ispin)%matrix, &
ispin=ispin, &
blocked=admm_env%block_dm)
!GPW is the default, PW density is computed using the AUX_FIT basis and task_list
!If GAPW, the we use the AUX_FIT_SOFT basis and task list
basis_type = "AUX_FIT"
task_list => admm_env%task_list_aux_fit
IF (gapw) THEN
basis_type = "AUX_FIT_SOFT"
task_list => admm_env%admm_gapw_env%task_list
END IF
CALL calculate_rho_elec(ks_env=ks_env, &
matrix_p=rho_ao_aux(ispin)%matrix, &
rho=rho_r_aux(ispin), &
rho_gspace=rho_g_aux(ispin), &
total_rho=tot_rho_r_aux(ispin), &
soft_valid=.FALSE., &
basis_type=basis_type, &
task_list_external=task_list)
END DO
!If GAPW, also need to prepare the atomic densities
IF (gapw) THEN
CALL calculate_rho_atom_coeff(qs_env, rho_ao_aux, &
rho_atom_set=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
qs_kind_set=admm_env%admm_gapw_env%admm_kind_set, &
oce=admm_env%admm_gapw_env%oce, sab=admm_env%sab_aux_fit, para_env=para_env)
CALL prepare_gapw_den(qs_env, local_rho_set=admm_env%admm_gapw_env%local_rho_set, &
do_rho0=.FALSE., kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
END IF
IF (dft_control%nspins == 1) THEN
admm_env%gsi(3) = admm_env%gsi(1)
ELSE
admm_env%gsi(3) = (admm_env%gsi(1) + admm_env%gsi(2))/2.0_dp
END IF
CALL qs_rho_set(rho_aux_fit, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
CALL timestop(handle)
END SUBROUTINE admm_mo_calc_rho_aux
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE admm_mo_calc_rho_aux_kp(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'admm_mo_calc_rho_aux_kp'
CHARACTER(LEN=default_string_length) :: basis_type
INTEGER :: handle, i, igroup, ik, ikp, img, indx, &
ispin, kplocal, nao_aux_fit, nao_orb, &
natom, nkp, nkp_groups, nmo, nspins
INTEGER, DIMENSION(2) :: kp_range
INTEGER, DIMENSION(:, :), POINTER :: kp_dist
INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
LOGICAL :: gapw, my_kpgrp, pmat_from_rs, &
use_real_wfn
REAL(dp) :: maxval_mos, nelec_aux(2), nelec_orb(2), &
tmp
REAL(KIND=dp), DIMENSION(:), POINTER :: occ_num, occ_num_aux, tot_rho_r_aux
REAL(KIND=dp), DIMENSION(:, :), POINTER :: xkp
TYPE(admm_type), POINTER :: admm_env
TYPE(copy_info_type), ALLOCATABLE, DIMENSION(:, :) :: info
TYPE(cp_cfm_type) :: cA, cmo_coeff, cmo_coeff_aux_fit, &
cpmatrix, cwork_aux_aux, cwork_aux_orb
TYPE(cp_fm_struct_type), POINTER :: mo_struct, mo_struct_aux_fit, &
struct_aux_aux, struct_aux_orb, &
struct_orb_orb
TYPE(cp_fm_type) :: fmdummy, work_aux_orb, work_orb_orb, &
work_orb_orb2
TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_aux_fit
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: rho_ao
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s, matrix_s_aux_fit, rho_ao_aux, &
rho_ao_orb
TYPE(dbcsr_type) :: pmatrix_tmp
TYPE(dbcsr_type), ALLOCATABLE, DIMENSION(:) :: pmatrix
TYPE(dft_control_type), POINTER :: dft_control
TYPE(kpoint_env_type), POINTER :: kp
TYPE(kpoint_type), POINTER :: kpoints
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
TYPE(mo_set_type), DIMENSION(:, :), POINTER :: mos_aux_fit_kp, mos_kp
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_aux_fit, sab_kp
TYPE(pw_type), DIMENSION(:), POINTER :: rho_g_aux, rho_r_aux
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_rho_type), POINTER :: rho_aux_fit, rho_orb
TYPE(qs_scf_env_type), POINTER :: scf_env
TYPE(task_list_type), POINTER :: task_list
CALL timeset(routineN, handle)
NULLIFY (ks_env, admm_env, mos, mos_aux_fit, matrix_s, rho_orb, &
matrix_s_aux_fit, rho_aux_fit, rho_ao_orb, &
para_env, rho_g_aux, rho_r_aux, rho_ao_aux, tot_rho_r_aux, &
kpoints, sab_aux_fit, sab_kp, kp, &
struct_orb_orb, struct_aux_orb, struct_aux_aux, mo_struct, mo_struct_aux_fit)
CALL get_qs_env(qs_env, &
ks_env=ks_env, &
admm_env=admm_env, &
dft_control=dft_control, &
kpoints=kpoints, &
natom=natom, &
scf_env=scf_env, &
matrix_s_kp=matrix_s, &
rho=rho_orb)
CALL get_admm_env(admm_env, &
rho_aux_fit=rho_aux_fit, &
matrix_s_aux_fit_kp=matrix_s_aux_fit, &
sab_aux_fit=sab_aux_fit)
gapw = admm_env%do_gapw
CALL qs_rho_get(rho_aux_fit, &
rho_ao_kp=rho_ao_aux, &
rho_g=rho_g_aux, &
rho_r=rho_r_aux, &
tot_rho_r=tot_rho_r_aux)
CALL qs_rho_get(rho_orb, rho_ao_kp=rho_ao_orb)
CALL get_kpoint_info(kpoints, nkp=nkp, xkp=xkp, use_real_wfn=use_real_wfn, kp_range=kp_range, &
nkp_groups=nkp_groups, kp_dist=kp_dist, &
cell_to_index=cell_to_index, sab_nl=sab_kp)
! the temporary DBCSR matrices for the rskp_transform we have to manually allocate
! index 1 => real, index 2 => imaginary
ALLOCATE (pmatrix(2))
CALL dbcsr_create(pmatrix(1), template=matrix_s(1, 1)%matrix, &
matrix_type=dbcsr_type_symmetric)
CALL dbcsr_create(pmatrix(2), template=matrix_s(1, 1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_create(pmatrix_tmp, template=matrix_s(1, 1)%matrix, &
matrix_type=dbcsr_type_no_symmetry)
CALL cp_dbcsr_alloc_block_from_nbl(pmatrix(1), sab_kp)
CALL cp_dbcsr_alloc_block_from_nbl(pmatrix(2), sab_kp)
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
nspins = dft_control%nspins
!Create fm and cfm work matrices, for each KP subgroup
CALL cp_fm_struct_create(struct_orb_orb, context=kpoints%blacs_env, para_env=kpoints%para_env_kp, &
nrow_global=nao_orb, ncol_global=nao_orb)
CALL cp_fm_create(work_orb_orb, struct_orb_orb)
CALL cp_fm_create(work_orb_orb2, struct_orb_orb)
CALL cp_fm_struct_create(struct_aux_aux, context=kpoints%blacs_env, para_env=kpoints%para_env_kp, &
nrow_global=nao_aux_fit, ncol_global=nao_aux_fit)
CALL cp_fm_struct_create(struct_aux_orb, context=kpoints%blacs_env, para_env=kpoints%para_env_kp, &
nrow_global=nao_aux_fit, ncol_global=nao_orb)
CALL cp_fm_create(work_aux_orb, struct_orb_orb)
IF (.NOT. use_real_wfn) THEN
CALL cp_cfm_create(cpmatrix, struct_orb_orb)
CALL cp_cfm_create(cwork_aux_aux, struct_aux_aux)
CALL cp_cfm_create(cA, struct_aux_orb)
CALL cp_cfm_create(cwork_aux_orb, struct_aux_orb)
CALL get_kpoint_env(kpoints%kp_env(1)%kpoint_env, mos=mos_kp)
mos => mos_kp(1, :)
CALL get_mo_set(mos(1), mo_coeff=mo_coeff)
CALL cp_fm_get_info(mo_coeff, matrix_struct=mo_struct)
CALL cp_cfm_create(cmo_coeff, mo_struct)
CALL get_kpoint_env(kpoints%kp_aux_env(1)%kpoint_env, mos=mos_aux_fit_kp)
mos => mos_aux_fit_kp(1, :)
CALL get_mo_set(mos(1), mo_coeff=mo_coeff_aux_fit)
CALL cp_fm_get_info(mo_coeff_aux_fit, matrix_struct=mo_struct_aux_fit)
CALL cp_cfm_create(cmo_coeff_aux_fit, mo_struct_aux_fit)
END IF
CALL cp_fm_struct_release(struct_orb_orb)
CALL cp_fm_struct_release(struct_aux_aux)
CALL cp_fm_struct_release(struct_aux_orb)
para_env => kpoints%blacs_env_all%para_env
kplocal = kp_range(2) - kp_range(1) + 1
!We querry the maximum absolute value of the KP MOs to see if they are populated at all. If not, we
!need to get the KP Pmat from the RS ones (happens at first SCF step, for example)
maxval_mos = 0.0_dp
indx = 0
DO ikp = 1, kplocal
DO ispin = 1, nspins
DO igroup = 1, nkp_groups
! number of current kpoint
ik = kp_dist(1, igroup) + ikp - 1
my_kpgrp = (ik >= kpoints%kp_range(1) .AND. ik <= kpoints%kp_range(2))
indx = indx + 1
CALL get_kpoint_env(kpoints%kp_env(ikp)%kpoint_env, mos=mos_kp)
mos => mos_kp(1, :)
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
maxval_mos = MAX(maxval_mos, MAXVAL(ABS(mo_coeff%local_data)))
IF (.NOT. use_real_wfn) THEN
mos => mos_kp(2, :)
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
maxval_mos = MAX(maxval_mos, MAXVAL(ABS(mo_coeff%local_data)))
END IF
END DO
END DO
END DO
CALL para_env%sum(maxval_mos) !I think para_env is the global one
pmat_from_rs = .FALSE.
IF (maxval_mos < EPSILON(0.0_dp)) pmat_from_rs = .TRUE.
!TODO: issue a warning when doing ADMM with ATOMIC guess. If small number of K-points => leads to bad things
ALLOCATE (info(kplocal*nspins*nkp_groups, 2))
!Start communication: only P matrix, and only if required
indx = 0
IF (pmat_from_rs) THEN
DO ikp = 1, kplocal
DO ispin = 1, nspins
DO igroup = 1, nkp_groups
! number of current kpoint
ik = kp_dist(1, igroup) + ikp - 1
my_kpgrp = (ik >= kpoints%kp_range(1) .AND. ik <= kpoints%kp_range(2))
indx = indx + 1
! FT of matrices P if required, then transfer to FM type
IF (use_real_wfn) THEN
CALL dbcsr_set(pmatrix(1), 0.0_dp)
CALL rskp_transform(rmatrix=pmatrix(1), rsmat=rho_ao_orb, ispin=ispin, &
xkp=xkp(1:3, ik), cell_to_index=cell_to_index, sab_nl=sab_kp)
CALL dbcsr_desymmetrize(pmatrix(1), pmatrix_tmp)
CALL copy_dbcsr_to_fm(pmatrix_tmp, admm_env%work_orb_orb)
ELSE
CALL dbcsr_set(pmatrix(1), 0.0_dp)
CALL dbcsr_set(pmatrix(2), 0.0_dp)
CALL rskp_transform(rmatrix=pmatrix(1), cmatrix=pmatrix(2), rsmat=rho_ao_orb, ispin=ispin, &
xkp=xkp(1:3, ik), cell_to_index=cell_to_index, sab_nl=sab_kp)
CALL dbcsr_desymmetrize(pmatrix(1), pmatrix_tmp)
CALL copy_dbcsr_to_fm(pmatrix_tmp, admm_env%work_orb_orb)
CALL dbcsr_desymmetrize(pmatrix(2), pmatrix_tmp)
CALL copy_dbcsr_to_fm(pmatrix_tmp, admm_env%work_orb_orb2)
END IF
IF (my_kpgrp) THEN
CALL cp_fm_start_copy_general(admm_env%work_orb_orb, work_orb_orb, para_env, info(indx, 1))
IF (.NOT. use_real_wfn) THEN
CALL cp_fm_start_copy_general(admm_env%work_orb_orb2, work_orb_orb2, para_env, info(indx, 2))
END IF
ELSE
CALL cp_fm_start_copy_general(admm_env%work_orb_orb, fmdummy, para_env, info(indx, 1))
IF (.NOT. use_real_wfn) THEN
CALL cp_fm_start_copy_general(admm_env%work_orb_orb2, fmdummy, para_env, info(indx, 2))
END IF
END IF !my_kpgrp
END DO
END DO
END DO
END IF !pmat_from_rs
indx = 0
DO ikp = 1, kplocal
DO ispin = 1, nspins
DO igroup = 1, nkp_groups
! number of current kpoint
ik = kp_dist(1, igroup) + ikp - 1
my_kpgrp = (ik >= kpoints%kp_range(1) .AND. ik <= kpoints%kp_range(2))
indx = indx + 1
IF (my_kpgrp .AND. pmat_from_rs) THEN
CALL cp_fm_finish_copy_general(work_orb_orb, info(indx, 1))
IF (.NOT. use_real_wfn) THEN
CALL cp_fm_finish_copy_general(work_orb_orb2, info(indx, 2))
CALL cp_fm_to_cfm(work_orb_orb, work_orb_orb2, cpmatrix)
END IF
END IF
END DO
IF (use_real_wfn) THEN
nmo = admm_env%nmo(ispin)
!! Each kpoint group has now information on a kpoint for which to calculate the MOS_aux
CALL get_kpoint_env(kpoints%kp_env(ikp)%kpoint_env, mos=mos_kp)
CALL get_kpoint_env(kpoints%kp_aux_env(ikp)%kpoint_env, mos=mos_aux_fit_kp)
mos => mos_kp(1, :)
mos_aux_fit => mos_aux_fit_kp(1, :)
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, occupation_numbers=occ_num)
CALL get_mo_set(mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit, &
occupation_numbers=occ_num_aux)
kp => kpoints%kp_aux_env(ikp)%kpoint_env
CALL parallel_gemm('N', 'N', nao_aux_fit, nmo, nao_orb, 1.0_dp, kp%amat(1, 1), &
mo_coeff, 0.0_dp, mo_coeff_aux_fit)
occ_num_aux(1:nmo) = occ_num(1:nmo)
IF (pmat_from_rs) THEN
!We project on the AUX basis: P_aux = A * P *A^T
CALL parallel_gemm('N', 'N', nao_aux_fit, nao_orb, nao_orb, 1.0_dp, kp%amat(1, 1), &
work_orb_orb, 0.0_dp, work_aux_orb)
CALL parallel_gemm('N', 'T', nao_aux_fit, nao_aux_fit, nao_orb, 1.0_dp, work_aux_orb, &
kp%amat(1, 1), 0.0_dp, kpoints%kp_aux_env(ikp)%kpoint_env%pmat(1, ispin))
END IF
ELSE !complex wfn
!construct the ORB MOs in complex format
nmo = admm_env%nmo(ispin)
CALL get_kpoint_env(kpoints%kp_env(ikp)%kpoint_env, mos=mos_kp)
mos => mos_kp(1, :) !real
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
CALL cp_cfm_scale_and_add_fm(z_zero, cmo_coeff, z_one, mo_coeff)
mos => mos_kp(2, :) !complex
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff)
CALL cp_cfm_scale_and_add_fm(z_one, cmo_coeff, gaussi, mo_coeff)
!project
kp => kpoints%kp_aux_env(ikp)%kpoint_env
CALL cp_fm_to_cfm(kp%amat(1, 1), kp%amat(2, 1), cA)
CALL parallel_gemm('N', 'N', nao_aux_fit, nmo, nao_orb, &
z_one, cA, cmo_coeff, z_zero, cmo_coeff_aux_fit)
!write result back to KP MOs
CALL get_kpoint_env(kpoints%kp_aux_env(ikp)%kpoint_env, mos=mos_aux_fit_kp)
mos_aux_fit => mos_aux_fit_kp(1, :)
CALL get_mo_set(mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit)
CALL cp_cfm_to_fm(cmo_coeff_aux_fit, mtargetr=mo_coeff_aux_fit)
mos_aux_fit => mos_aux_fit_kp(2, :)
CALL get_mo_set(mos_aux_fit(ispin), mo_coeff=mo_coeff_aux_fit)
CALL cp_cfm_to_fm(cmo_coeff_aux_fit, mtargeti=mo_coeff_aux_fit)
DO i = 1, 2
mos => mos_kp(i, :)
CALL get_mo_set(mos(ispin), occupation_numbers=occ_num)
mos_aux_fit => mos_aux_fit_kp(i, :)
CALL get_mo_set(mos_aux_fit(ispin), occupation_numbers=occ_num_aux)
occ_num_aux(:) = occ_num(:)
END DO
IF (pmat_from_rs) THEN
CALL parallel_gemm('N', 'N', nao_aux_fit, nao_orb, nao_orb, z_one, cA, &
cpmatrix, z_zero, cwork_aux_orb)
CALL parallel_gemm('N', 'C', nao_aux_fit, nao_aux_fit, nao_orb, z_one, cwork_aux_orb, &
cA, z_zero, cwork_aux_aux)
CALL cp_cfm_to_fm(cwork_aux_aux, mtargetr=kpoints%kp_aux_env(ikp)%kpoint_env%pmat(1, ispin), &
mtargeti=kpoints%kp_aux_env(ikp)%kpoint_env%pmat(2, ispin))
END IF
END IF
END DO
END DO
!Clean-up communication
IF (pmat_from_rs) THEN
indx = 0
DO ikp = 1, kplocal
DO ispin = 1, nspins
DO igroup = 1, nkp_groups
! number of current kpoint
ik = kp_dist(1, igroup) + ikp - 1
my_kpgrp = (ik >= kpoints%kp_range(1) .AND. ik <= kpoints%kp_range(2))
indx = indx + 1
CALL cp_fm_cleanup_copy_general(info(indx, 1))
IF (.NOT. use_real_wfn) CALL cp_fm_cleanup_copy_general(info(indx, 2))
END DO
END DO
END DO
END IF
DEALLOCATE (info)
CALL dbcsr_release(pmatrix(1))
CALL dbcsr_release(pmatrix(2))
CALL dbcsr_release(pmatrix_tmp)
CALL cp_fm_release(work_orb_orb)
CALL cp_fm_release(work_orb_orb2)
CALL cp_fm_release(work_aux_orb)
IF (.NOT. use_real_wfn) THEN
CALL cp_cfm_release(cpmatrix)
CALL cp_cfm_release(cwork_aux_aux)
CALL cp_cfm_release(cwork_aux_orb)
CALL cp_cfm_release(cA)
CALL cp_cfm_release(cmo_coeff)
CALL cp_cfm_release(cmo_coeff_aux_fit)
END IF
IF (.NOT. pmat_from_rs) CALL kpoint_density_matrices(kpoints, for_aux_fit=.TRUE.)
CALL kpoint_density_transform(kpoints, rho_ao_aux, .FALSE., &
matrix_s_aux_fit(1, 1)%matrix, sab_aux_fit, &
admm_env%scf_work_aux_fit, for_aux_fit=.TRUE.)
!ADMMQ, ADMMP, ADMMS
IF (admm_env%do_admmq .OR. admm_env%do_admmp .OR. admm_env%do_admms) THEN
CALL cite_reference(Merlot2014)
nelec_orb = 0.0_dp
nelec_aux = 0.0_dp
admm_env%n_large_basis = 0.0_dp
!Note: we can take the trace of the symmetric-typed matrices as P_mu^0,nu^b = P_nu^0,mu^-b
! and because of the sum over all images, all atomic blocks are accounted for
DO img = 1, dft_control%nimages
DO ispin = 1, dft_control%nspins
CALL dbcsr_dot(rho_ao_orb(ispin, img)%matrix, matrix_s(1, img)%matrix, tmp)
nelec_orb(ispin) = nelec_orb(ispin) + tmp
CALL dbcsr_dot(rho_ao_aux(ispin, img)%matrix, matrix_s_aux_fit(1, img)%matrix, tmp)
nelec_aux(ispin) = nelec_aux(ispin) + tmp
END DO
END DO
DO ispin = 1, dft_control%nspins
admm_env%n_large_basis(ispin) = nelec_orb(ispin)
admm_env%gsi(ispin) = nelec_orb(ispin)/nelec_aux(ispin)
END DO
IF (admm_env%charge_constrain) THEN
DO img = 1, dft_control%nimages
DO ispin = 1, dft_control%nspins
CALL dbcsr_scale(rho_ao_aux(ispin, img)%matrix, admm_env%gsi(ispin))
END DO
END DO
END IF
IF (dft_control%nspins == 1) THEN
admm_env%gsi(3) = admm_env%gsi(1)
ELSE
admm_env%gsi(3) = (admm_env%gsi(1) + admm_env%gsi(2))/2.0_dp
END IF
END IF
basis_type = "AUX_FIT"
task_list => admm_env%task_list_aux_fit
IF (gapw) THEN
basis_type = "AUX_FIT_SOFT"
task_list => admm_env%admm_gapw_env%task_list
END IF
DO ispin = 1, nspins
rho_ao => rho_ao_aux(ispin, :)
CALL calculate_rho_elec(ks_env=ks_env, &
matrix_p_kp=rho_ao, &
rho=rho_r_aux(ispin), &
rho_gspace=rho_g_aux(ispin), &
total_rho=tot_rho_r_aux(ispin), &
soft_valid=.FALSE., &
basis_type=basis_type, &
task_list_external=task_list)
END DO
IF (gapw) THEN
CALL calculate_rho_atom_coeff(qs_env, rho_ao_aux, &
rho_atom_set=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
qs_kind_set=admm_env%admm_gapw_env%admm_kind_set, &
oce=admm_env%admm_gapw_env%oce, &
sab=admm_env%sab_aux_fit, para_env=para_env)
CALL prepare_gapw_den(qs_env, local_rho_set=admm_env%admm_gapw_env%local_rho_set, &
do_rho0=.FALSE., kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
END IF
CALL qs_rho_set(rho_aux_fit, rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
CALL timestop(handle)
END SUBROUTINE admm_mo_calc_rho_aux_kp
! **************************************************************************************************
!> \brief Adds the GAPW exchange contribution to the aux_fit ks matrices
!> \param qs_env ...
!> \param calculate_forces ...
! **************************************************************************************************
SUBROUTINE admm_update_ks_atom(qs_env, calculate_forces)
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(IN) :: calculate_forces
CHARACTER(len=*), PARAMETER :: routineN = 'admm_update_ks_atom'
INTEGER :: handle, img, ispin
REAL(dp) :: force_fac(2)
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_aux_fit, &
matrix_ks_aux_fit_dft, &
matrix_ks_aux_fit_hfx, rho_ao_aux
TYPE(dft_control_type), POINTER :: dft_control
TYPE(qs_rho_type), POINTER :: rho_aux_fit
NULLIFY (matrix_ks_aux_fit, matrix_ks_aux_fit_dft, matrix_ks_aux_fit_hfx, rho_ao_aux, rho_aux_fit)
NULLIFY (admm_env, dft_control)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, admm_env=admm_env, dft_control=dft_control)
CALL get_admm_env(admm_env, rho_aux_fit=rho_aux_fit, matrix_ks_aux_fit_kp=matrix_ks_aux_fit, &
matrix_ks_aux_fit_dft_kp=matrix_ks_aux_fit_dft, &
matrix_ks_aux_fit_hfx_kp=matrix_ks_aux_fit_hfx)
CALL qs_rho_get(rho_aux_fit, rho_ao_kp=rho_ao_aux)
!In case of ADMMS or ADMMP, need to scale the forces stemming from DFT exchagne correction
force_fac = 1.0_dp
IF (admm_env%do_admms) THEN
DO ispin = 1, dft_control%nspins
force_fac(ispin) = admm_env%gsi(ispin)**(2.0_dp/3.0_dp)
END DO
ELSE IF (admm_env%do_admmp) THEN
DO ispin = 1, dft_control%nspins
force_fac(ispin) = admm_env%gsi(ispin)**2
END DO
END IF
CALL update_ks_atom(qs_env, matrix_ks_aux_fit, rho_ao_aux, calculate_forces, tddft=.FALSE., &
rho_atom_external=admm_env%admm_gapw_env%local_rho_set%rho_atom_set, &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
oce_external=admm_env%admm_gapw_env%oce, &
sab_external=admm_env%sab_aux_fit, fscale=force_fac)
!Following the logic of sum_up_and_integrate to recover the pure DFT exchange contribution
DO img = 1, dft_control%nimages
DO ispin = 1, dft_control%nspins
CALL dbcsr_add(matrix_ks_aux_fit_dft(ispin, img)%matrix, matrix_ks_aux_fit(ispin, img)%matrix, &
0.0_dp, -1.0_dp)
CALL dbcsr_add(matrix_ks_aux_fit_dft(ispin, img)%matrix, matrix_ks_aux_fit_hfx(ispin, img)%matrix, &
1.0_dp, 1.0_dp)
END DO
END DO
CALL timestop(handle)
END SUBROUTINE admm_update_ks_atom
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE admm_mo_merge_ks_matrix(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'admm_mo_merge_ks_matrix'
INTEGER :: handle
TYPE(admm_type), POINTER :: admm_env
TYPE(dft_control_type), POINTER :: dft_control
CALL timeset(routineN, handle)
NULLIFY (admm_env)
CALL get_qs_env(qs_env, admm_env=admm_env, dft_control=dft_control)
SELECT CASE (admm_env%purification_method)
CASE (do_admm_purify_cauchy)
CALL merge_ks_matrix_cauchy(qs_env)
CASE (do_admm_purify_cauchy_subspace)
CALL merge_ks_matrix_cauchy_subspace(qs_env)
CASE (do_admm_purify_none)
IF (dft_control%nimages > 1) THEN
CALL merge_ks_matrix_none_kp(qs_env)
ELSE
CALL merge_ks_matrix_none(qs_env)
END IF
CASE (do_admm_purify_mo_diag, do_admm_purify_mo_no_diag)
!do nothing
CASE DEFAULT
CPABORT("admm_mo_merge_ks_matrix: unknown purification method")
END SELECT
CALL timestop(handle)
END SUBROUTINE admm_mo_merge_ks_matrix
! **************************************************************************************************
!> \brief ...
!> \param ispin ...
!> \param admm_env ...
!> \param mo_set ...
!> \param mo_coeff ...
!> \param mo_coeff_aux_fit ...
!> \param mo_derivs ...
!> \param mo_derivs_aux_fit ...
!> \param matrix_ks_aux_fit ...
! **************************************************************************************************
SUBROUTINE admm_mo_merge_derivs(ispin, admm_env, mo_set, mo_coeff, mo_coeff_aux_fit, mo_derivs, &
mo_derivs_aux_fit, matrix_ks_aux_fit)
INTEGER, INTENT(IN) :: ispin
TYPE(admm_type), POINTER :: admm_env
TYPE(mo_set_type), INTENT(IN) :: mo_set
TYPE(cp_fm_type), INTENT(IN) :: mo_coeff, mo_coeff_aux_fit
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_derivs, mo_derivs_aux_fit
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks_aux_fit
CHARACTER(LEN=*), PARAMETER :: routineN = 'admm_mo_merge_derivs'
INTEGER :: handle
CALL timeset(routineN, handle)
SELECT CASE (admm_env%purification_method)
CASE (do_admm_purify_mo_diag)
CALL merge_mo_derivs_diag(ispin, admm_env, mo_set, mo_coeff, mo_coeff_aux_fit, &
mo_derivs, mo_derivs_aux_fit, matrix_ks_aux_fit)
CASE (do_admm_purify_mo_no_diag)
CALL merge_mo_derivs_no_diag(ispin, admm_env, mo_set, mo_derivs, matrix_ks_aux_fit)
CASE (do_admm_purify_none, do_admm_purify_cauchy, do_admm_purify_cauchy_subspace)
!do nothing
CASE DEFAULT
CPABORT("admm_mo_merge_derivs: unknown purification method")
END SELECT
CALL timestop(handle)
END SUBROUTINE admm_mo_merge_derivs
! **************************************************************************************************
!> \brief ...
!> \param admm_env ...
!> \param matrix_s_aux_fit ...
!> \param matrix_s_mixed ...
!> \param mos ...
!> \param mos_aux_fit ...
!> \param geometry_did_change ...
! **************************************************************************************************
SUBROUTINE admm_fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_mixed, &
mos, mos_aux_fit, geometry_did_change)
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s_aux_fit, matrix_s_mixed
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos, mos_aux_fit
LOGICAL, INTENT(IN) :: geometry_did_change
CHARACTER(LEN=*), PARAMETER :: routineN = 'admm_fit_mo_coeffs'
INTEGER :: handle
CALL timeset(routineN, handle)
IF (geometry_did_change) THEN
CALL fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_mixed)
END IF
SELECT CASE (admm_env%purification_method)
CASE (do_admm_purify_mo_no_diag, do_admm_purify_cauchy_subspace)
CALL purify_mo_cholesky(admm_env, mos, mos_aux_fit)
CASE (do_admm_purify_mo_diag)
CALL purify_mo_diag(admm_env, mos, mos_aux_fit)
CASE DEFAULT
CALL purify_mo_none(admm_env, mos, mos_aux_fit)
END SELECT
CALL timestop(handle)
END SUBROUTINE admm_fit_mo_coeffs
! **************************************************************************************************
!> \brief Calculate S^-1, Q, B full-matrices given sparse S_tilde and Q
!> \param admm_env ...
!> \param matrix_s_aux_fit ...
!> \param matrix_s_mixed ...
! **************************************************************************************************
SUBROUTINE fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_mixed)
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s_aux_fit, matrix_s_mixed
CHARACTER(LEN=*), PARAMETER :: routineN = 'fit_mo_coeffs'
INTEGER :: blk, handle, iatom, jatom, nao_aux_fit, &
nao_orb
REAL(dp), DIMENSION(:, :), POINTER :: sparse_block
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_type), POINTER :: matrix_s_tilde
CALL timeset(routineN, handle)
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
! *** This part only depends on overlap matrices ==> needs only to be calculated if the geometry changed
IF (.NOT. admm_env%block_fit) THEN
CALL copy_dbcsr_to_fm(matrix_s_aux_fit(1)%matrix, admm_env%S_inv)
ELSE
NULLIFY (matrix_s_tilde)
ALLOCATE (matrix_s_tilde)
CALL dbcsr_create(matrix_s_tilde, template=matrix_s_aux_fit(1)%matrix, &
name='MATRIX s_tilde', &
matrix_type=dbcsr_type_symmetric)
CALL dbcsr_copy(matrix_s_tilde, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_iterator_start(iter, matrix_s_tilde)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sparse_block, blk)
IF (admm_env%block_map(iatom, jatom) == 0) THEN
sparse_block = 0.0_dp
END IF
END DO
CALL dbcsr_iterator_stop(iter)
CALL copy_dbcsr_to_fm(matrix_s_tilde, admm_env%S_inv)
CALL dbcsr_deallocate_matrix(matrix_s_tilde)
END IF
CALL cp_fm_upper_to_full(admm_env%S_inv, admm_env%work_aux_aux)
CALL cp_fm_to_fm(admm_env%S_inv, admm_env%S)
CALL copy_dbcsr_to_fm(matrix_s_mixed(1)%matrix, admm_env%Q)
!! Calculate S'_inverse
CALL cp_fm_cholesky_decompose(admm_env%S_inv)
CALL cp_fm_cholesky_invert(admm_env%S_inv)
!! Symmetrize the guy
CALL cp_fm_upper_to_full(admm_env%S_inv, admm_env%work_aux_aux)
!! Calculate A=S'^(-1)*Q
IF (admm_env%block_fit) THEN
CALL cp_fm_set_all(admm_env%A, 0.0_dp, 1.0_dp)
ELSE
CALL parallel_gemm('N', 'N', nao_aux_fit, nao_orb, nao_aux_fit, &
1.0_dp, admm_env%S_inv, admm_env%Q, 0.0_dp, &
admm_env%A)
! this multiplication is apparent not need for purify_none
!! B=Q^(T)*A
CALL parallel_gemm('T', 'N', nao_orb, nao_orb, nao_aux_fit, &
1.0_dp, admm_env%Q, admm_env%A, 0.0_dp, &
admm_env%B)
END IF
CALL timestop(handle)
END SUBROUTINE fit_mo_coeffs
! **************************************************************************************************
!> \brief Calculates the MO coefficients for the auxiliary fitting basis set
!> by minimizing int (psi_i - psi_aux_i)^2 using Lagrangian Multipliers
!>
!> \param admm_env The ADMM env
!> \param mos the MO's of the orbital basis set
!> \param mos_aux_fit the MO's of the auxiliary fitting basis set
!> \par History
!> 05.2008 created [Manuel Guidon]
!> \author Manuel Guidon
! **************************************************************************************************
SUBROUTINE purify_mo_cholesky(admm_env, mos, mos_aux_fit)
TYPE(admm_type), POINTER :: admm_env
TYPE(mo_set_type), DIMENSION(:), INTENT(IN) :: mos, mos_aux_fit
CHARACTER(LEN=*), PARAMETER :: routineN = 'purify_mo_cholesky'
INTEGER :: handle, ispin, nao_aux_fit, nao_orb, &
nmo, nspins
TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_aux_fit
CALL timeset(routineN, handle)
nao_aux_fit = admm_env%nao_aux_fit