-
Notifications
You must be signed in to change notification settings - Fork 0
/
admm_methods.F
2498 lines (2048 loc) · 120 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-2021 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
USE atomic_kind_types, ONLY: atomic_kind_type
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_type
USE bibliography, ONLY: Merlot2014,&
cite_reference
USE cell_types, ONLY: cell_type
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
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_types, ONLY: cp_fm_get_info,&
cp_fm_p_type,&
cp_fm_set_all,&
cp_fm_set_element,&
cp_fm_to_fm,&
cp_fm_type
USE cp_gemm_interface, ONLY: cp_gemm
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_type
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE cp_para_types, ONLY: cp_para_env_type
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_scale, &
dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry, dbcsr_type_symmetric
USE distribution_1d_types, ONLY: distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE input_constants, ONLY: do_admm_exch_scaling_merlot,&
do_admm_exch_scaling_none,&
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 molecule_types, ONLY: molecule_type
USE particle_types, ONLY: particle_type
USE paw_proj_set_types, ONLY: get_paw_proj_set,&
paw_proj_set_type
USE pw_types, ONLY: pw_p_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_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_ks_atom, ONLY: update_ks_atom
USE qs_ks_types, ONLY: get_ks_env,&
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_p_type,&
mo_set_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type,&
release_neighbor_list_sets
USE qs_neighbor_lists, ONLY: atom2d_build,&
atom2d_cleanup,&
build_neighbor_lists,&
local_atoms_type,&
pair_radius_setup
USE qs_oce_methods, ONLY: build_oce_matrices
USE qs_oce_types, ONLY: allocate_oce_set,&
create_oce_set
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_vxc, ONLY: qs_vxc_create
USE qs_vxc_atom, ONLY: calculate_vxc_atom
USE task_list_methods, ONLY: generate_qs_task_list
USE task_list_types, ONLY: allocate_task_list,&
deallocate_task_list,&
task_list_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: admm_mo_calc_rho_aux, &
admm_mo_merge_ks_matrix, &
admm_mo_merge_derivs, &
calc_mixed_overlap_force, &
calc_aux_mo_derivs_none, &
scale_dm, &
admm_fit_mo_coeffs, &
admm_update_ks_atom, &
admm_aux_reponse_density
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(cp_para_env_type), POINTER :: para_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_p_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_aux_fit
TYPE(pw_p_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, sab_aux_fit)
CALL get_qs_env(qs_env, &
ks_env=ks_env, &
admm_env=admm_env, &
dft_control=dft_control, &
mos_aux_fit=mos_aux_fit, &
mos=mos, &
matrix_s_aux_fit=matrix_s_aux_fit, &
matrix_s_aux_fit_vs_orb=matrix_s_aux_fit_vs_orb, &
matrix_s=matrix_s, &
para_env=para_env, &
s_mstruct_changed=s_mstruct_changed, &
rho=rho, &
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)%mo_set%use_mo_coeff_b) THEN
CALL copy_dbcsr_to_fm(mos(ispin)%mo_set%mo_coeff_b, mos(ispin)%mo_set%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)
! update the GAPW internals if structure has changed
IF (s_mstruct_changed .AND. gapw) CALL update_admm_gapw(qs_env)
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)%mo_set, &
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)%mo_set, &
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"
CALL get_ks_env(ks_env, task_list_aux_fit=task_list)
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 get_qs_env(qs_env, sab_aux_fit=sab_aux_fit)
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=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 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, ispin
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(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_aux_fit
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 (sab_aux_fit, admm_env, dft_control)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, rho_aux_fit=rho_aux_fit, matrix_ks_aux_fit=matrix_ks_aux_fit, &
matrix_ks_aux_fit_dft=matrix_ks_aux_fit_dft, sab_aux_fit=sab_aux_fit, &
matrix_ks_aux_fit_hfx=matrix_ks_aux_fit_hfx, admm_env=admm_env, &
dft_control=dft_control)
CALL qs_rho_get(rho_aux_fit, rho_ao=rho_ao_aux)
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=sab_aux_fit)
!Following the logic of sum_up_and_integrate to recover the pure DFT exchange contribution
DO ispin = 1, dft_control%nspins
CALL dbcsr_add(matrix_ks_aux_fit_dft(ispin)%matrix, matrix_ks_aux_fit(ispin)%matrix, 0.0_dp, -1.0_dp)
CALL dbcsr_add(matrix_ks_aux_fit_dft(ispin)%matrix, matrix_ks_aux_fit_hfx(ispin)%matrix, 1.0_dp, 1.0_dp)
END DO
CALL timestop(handle)
END SUBROUTINE admm_update_ks_atom
! **************************************************************************************************
!> \brief Update the admm_gapw_env internals to the current qs_env (i.e. atomic positions)
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE update_admm_gapw(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'update_admm_gapw'
INTEGER :: handle, ikind, nkind
LOGICAL :: paw_atom
LOGICAL, ALLOCATABLE, DIMENSION(:) :: aux_present, oce_present
REAL(dp) :: subcells
REAL(dp), ALLOCATABLE, DIMENSION(:) :: aux_radius, oce_radius
REAL(dp), ALLOCATABLE, DIMENSION(:, :) :: pair_radius
TYPE(admm_gapw_type), POINTER :: admm_gapw_env
TYPE(admm_type), POINTER :: admm_env
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(dft_control_type), POINTER :: dft_control
TYPE(distribution_1d_type), POINTER :: distribution_1d
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(gto_basis_set_type), POINTER :: aux_fit_basis
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_aux_fit, sap_oce
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(paw_proj_set_type), POINTER :: paw_proj
TYPE(qs_kind_type), DIMENSION(:), POINTER :: admm_kind_set, qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
NULLIFY (ks_env, sab_aux_fit, qs_kind_set, admm_kind_set, aux_fit_basis, cell, distribution_1d)
NULLIFY (distribution_2d, paw_proj, particle_set, molecule_set, admm_env, admm_gapw_env)
NULLIFY (dft_control, atomic_kind_set, sap_oce)
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, ks_env=ks_env, qs_kind_set=qs_kind_set, admm_env=admm_env, &
dft_control=dft_control)
admm_gapw_env => admm_env%admm_gapw_env
admm_kind_set => admm_gapw_env%admm_kind_set
nkind = SIZE(qs_kind_set)
!Update the task lisft for the AUX_FIT_SOFT basis
CALL get_ks_env(ks_env, sab_aux_fit=sab_aux_fit)
IF (ASSOCIATED(admm_gapw_env%task_list)) CALL deallocate_task_list(admm_gapw_env%task_list)
CALL allocate_task_list(admm_gapw_env%task_list)
!note: we set soft_valid to .FALSE. want to use AUX_FIT_SOFT and not the normal ORB SOFT basis
CALL generate_qs_task_list(ks_env, admm_gapw_env%task_list, reorder_rs_grid_ranks=.FALSE., &
soft_valid=.FALSE., basis_type="AUX_FIT_SOFT", &
skip_load_balance_distributed=dft_control%qs_control%skip_load_balance_distributed, &
sab_orb_external=sab_aux_fit)
!Update the precomputed oce integrals
!a sap_oce neighbor list is required => build it here
ALLOCATE (aux_present(nkind), oce_present(nkind))
aux_present = .FALSE.; oce_present = .FALSE.
ALLOCATE (aux_radius(nkind), oce_radius(nkind))
aux_radius = 0.0_dp; oce_radius = 0.0_dp
DO ikind = 1, nkind
CALL get_qs_kind(qs_kind_set(ikind), basis_set=aux_fit_basis, basis_type="AUX_FIT")
IF (ASSOCIATED(aux_fit_basis)) THEN
aux_present(ikind) = .TRUE.
CALL get_gto_basis_set(aux_fit_basis, kind_radius=aux_radius(ikind))
END IF
!note: get oce info from admm_kind_set
CALL get_qs_kind(admm_kind_set(ikind), paw_atom=paw_atom, paw_proj_set=paw_proj)
IF (paw_atom) THEN
oce_present(ikind) = .TRUE.
CALL get_paw_proj_set(paw_proj, rcprj=oce_radius(ikind))
END IF
END DO
ALLOCATE (pair_radius(nkind, nkind))
pair_radius = 0.0_dp
CALL pair_radius_setup(aux_present, oce_present, aux_radius, oce_radius, pair_radius)
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, cell=cell, &
distribution_2d=distribution_2d, local_particles=distribution_1d, &
particle_set=particle_set, molecule_set=molecule_set)
CALL section_vals_val_get(qs_env%input, "DFT%SUBCELLS", r_val=subcells)
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, .FALSE., particle_set)
CALL build_neighbor_lists(sap_oce, particle_set, atom2d, cell, pair_radius, &
subcells=subcells, operator_type="ABBA", nlname="AUX_PAW-PRJ")
CALL atom2d_cleanup(atom2d)
!actually compute the oce matrices
CALL create_oce_set(admm_gapw_env%oce)
CALL allocate_oce_set(admm_gapw_env%oce, nkind)
!always compute the derivative, cheap anyways
CALL build_oce_matrices(admm_gapw_env%oce%intac, calculate_forces=.TRUE., nder=1, &
qs_kind_set=admm_kind_set, particle_set=particle_set, &
sap_oce=sap_oce, eps_fit=dft_control%qs_control%gapw_control%eps_fit)
CALL release_neighbor_list_sets(sap_oce)
CALL timestop(handle)
END SUBROUTINE update_admm_gapw
! **************************************************************************************************
!> \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
CALL timeset(routineN, handle)
NULLIFY (admm_env)
CALL get_qs_env(qs_env, admm_env=admm_env)
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)
CALL merge_ks_matrix_none(qs_env)
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), POINTER :: mo_set
TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_aux_fit
TYPE(cp_fm_p_type), DIMENSION(:), POINTER :: 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_p_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
LOGICAL, INTENT(IN) :: geometry_did_change
CHARACTER(LEN=*), PARAMETER :: routineN = 'admm_fit_mo_coeffs'
INTEGER :: handle
CALL timeset(routineN, handle)
CALL fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_mixed, &
mos, geometry_did_change, &
blocked=admm_env%block_fit)
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 ...
!> \param admm_env ...
!> \param matrix_s_aux_fit ...
!> \param matrix_s_mixed ...
!> \param mos ...
!> \param geometry_did_change ...
!> \param blocked ...
! **************************************************************************************************
SUBROUTINE fit_mo_coeffs(admm_env, matrix_s_aux_fit, matrix_s_mixed, &
mos, geometry_did_change, blocked)
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s_aux_fit, matrix_s_mixed
TYPE(mo_set_p_type), DIMENSION(:), POINTER :: mos
LOGICAL, INTENT(IN) :: geometry_did_change, blocked
CHARACTER(LEN=*), PARAMETER :: routineN = 'fit_mo_coeffs'
INTEGER :: blk, handle, iatom, jatom, nao_aux_fit, &
nao_orb, nspins
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
nspins = SIZE(mos)
! *** This part only depends on overlap matrices ==> needs only to be calculated if the geometry changed
IF (geometry_did_change) THEN
IF (.NOT. blocked) 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 (blocked) THEN
CALL cp_fm_set_all(admm_env%A, 0.0_dp, 1.0_dp)
ELSE
CALL cp_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 cp_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
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_p_type), DIMENSION(:), POINTER :: 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
nao_orb = admm_env%nao_orb
nspins = SIZE(mos)
! *** Calculate the mo_coeffs for the fitting basis
DO ispin = 1, nspins
nmo = admm_env%nmo(ispin)
IF (nmo == 0) CYCLE
!! Lambda = C^(T)*B*C
CALL get_mo_set(mos(ispin)%mo_set, mo_coeff=mo_coeff)
CALL get_mo_set(mos_aux_fit(ispin)%mo_set, mo_coeff=mo_coeff_aux_fit)
CALL cp_gemm('N', 'N', nao_orb, nmo, nao_orb, &
1.0_dp, admm_env%B, mo_coeff, 0.0_dp, &
admm_env%work_orb_nmo(ispin)%matrix)
CALL cp_gemm('T', 'N', nmo, nmo, nao_orb, &
1.0_dp, mo_coeff, admm_env%work_orb_nmo(ispin)%matrix, 0.0_dp, &
admm_env%lambda(ispin)%matrix)
CALL cp_fm_to_fm(admm_env%lambda(ispin)%matrix, admm_env%work_nmo_nmo1(ispin)%matrix)
CALL cp_fm_cholesky_decompose(admm_env%work_nmo_nmo1(ispin)%matrix)
CALL cp_fm_cholesky_invert(admm_env%work_nmo_nmo1(ispin)%matrix)
!! Symmetrize the guy
CALL cp_fm_upper_to_full(admm_env%work_nmo_nmo1(ispin)%matrix, admm_env%lambda_inv(ispin)%matrix)
CALL cp_fm_to_fm(admm_env%work_nmo_nmo1(ispin)%matrix, admm_env%lambda_inv(ispin)%matrix)
!! ** C_hat = AC
CALL cp_gemm('N', 'N', nao_aux_fit, nmo, nao_orb, &
1.0_dp, admm_env%A, mo_coeff, 0.0_dp, &
admm_env%C_hat(ispin)%matrix)
CALL cp_fm_to_fm(admm_env%C_hat(ispin)%matrix, mo_coeff_aux_fit)
END DO
CALL timestop(handle)
END SUBROUTINE purify_mo_cholesky
! **************************************************************************************************
!> \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_diag(admm_env, mos, mos_aux_fit)
TYPE(admm_type), POINTER :: admm_env
TYPE(mo_set_p_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
CHARACTER(LEN=*), PARAMETER :: routineN = 'purify_mo_diag'
INTEGER :: handle, i, ispin, nao_aux_fit, nao_orb, &
nmo, nspins
REAL(dp), ALLOCATABLE, DIMENSION(:) :: eig_work
TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_aux_fit
CALL timeset(routineN, handle)
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
nspins = SIZE(mos)
! *** Calculate the mo_coeffs for the fitting basis
DO ispin = 1, nspins
nmo = admm_env%nmo(ispin)
IF (nmo == 0) CYCLE
!! Lambda = C^(T)*B*C
CALL get_mo_set(mos(ispin)%mo_set, mo_coeff=mo_coeff)
CALL get_mo_set(mos_aux_fit(ispin)%mo_set, mo_coeff=mo_coeff_aux_fit)
CALL cp_gemm('N', 'N', nao_orb, nmo, nao_orb, &
1.0_dp, admm_env%B, mo_coeff, 0.0_dp, &
admm_env%work_orb_nmo(ispin)%matrix)
CALL cp_gemm('T', 'N', nmo, nmo, nao_orb, &
1.0_dp, mo_coeff, admm_env%work_orb_nmo(ispin)%matrix, 0.0_dp, &
admm_env%lambda(ispin)%matrix)
CALL cp_fm_to_fm(admm_env%lambda(ispin)%matrix, admm_env%work_nmo_nmo1(ispin)%matrix)
CALL cp_fm_syevd(admm_env%work_nmo_nmo1(ispin)%matrix, admm_env%R(ispin)%matrix, &
admm_env%eigvals_lambda(ispin)%eigvals%data)
ALLOCATE (eig_work(nmo))
DO i = 1, nmo
eig_work(i) = 1.0_dp/SQRT(admm_env%eigvals_lambda(ispin)%eigvals%data(i))
END DO
CALL cp_fm_to_fm(admm_env%R(ispin)%matrix, admm_env%work_nmo_nmo1(ispin)%matrix)
CALL cp_fm_column_scale(admm_env%work_nmo_nmo1(ispin)%matrix, eig_work)
CALL cp_gemm('N', 'T', nmo, nmo, nmo, &
1.0_dp, admm_env%work_nmo_nmo1(ispin)%matrix, admm_env%R(ispin)%matrix, 0.0_dp, &
admm_env%lambda_inv_sqrt(ispin)%matrix)
CALL cp_gemm('N', 'N', nao_orb, nmo, nmo, &
1.0_dp, mo_coeff, admm_env%lambda_inv_sqrt(ispin)%matrix, 0.0_dp, &
admm_env%work_orb_nmo(ispin)%matrix)
CALL cp_gemm('N', 'N', nao_aux_fit, nmo, nao_orb, &
1.0_dp, admm_env%A, admm_env%work_orb_nmo(ispin)%matrix, 0.0_dp, &
mo_coeff_aux_fit)
CALL cp_fm_to_fm(mo_coeff_aux_fit, admm_env%C_hat(ispin)%matrix)
CALL cp_fm_set_all(admm_env%lambda_inv(ispin)%matrix, 0.0_dp, 1.0_dp)
DEALLOCATE (eig_work)
END DO
CALL timestop(handle)
END SUBROUTINE purify_mo_diag
! **************************************************************************************************
!> \brief ...
!> \param admm_env ...
!> \param mos ...
!> \param mos_aux_fit ...
! **************************************************************************************************
SUBROUTINE purify_mo_none(admm_env, mos, mos_aux_fit)
TYPE(admm_type), POINTER :: admm_env
TYPE(mo_set_p_type), DIMENSION(:), POINTER :: mos, mos_aux_fit
CHARACTER(LEN=*), PARAMETER :: routineN = 'purify_mo_none'
INTEGER :: handle, ispin, nao_aux_fit, nao_orb, &
nmo, nmo_mos, nspins
REAL(KIND=dp), DIMENSION(:), POINTER :: occ_num, occ_num_aux
TYPE(cp_fm_type), POINTER :: mo_coeff, mo_coeff_aux_fit
CALL timeset(routineN, handle)
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
nspins = SIZE(mos)
DO ispin = 1, nspins
nmo = admm_env%nmo(ispin)
CALL get_mo_set(mos(ispin)%mo_set, mo_coeff=mo_coeff, occupation_numbers=occ_num, nmo=nmo_mos)
CALL get_mo_set(mos_aux_fit(ispin)%mo_set, mo_coeff=mo_coeff_aux_fit, &
occupation_numbers=occ_num_aux)
CALL cp_gemm('N', 'N', nao_aux_fit, nmo, nao_orb, &
1.0_dp, admm_env%A, mo_coeff, 0.0_dp, &
mo_coeff_aux_fit)
CALL cp_fm_to_fm(mo_coeff_aux_fit, admm_env%C_hat(ispin)%matrix)
occ_num_aux(1:nmo) = occ_num(1:nmo)
! XXXX should only be done first time XXXX
CALL cp_fm_set_all(admm_env%lambda(ispin)%matrix, 0.0_dp, 1.0_dp)
CALL cp_fm_set_all(admm_env%lambda_inv(ispin)%matrix, 0.0_dp, 1.0_dp)
CALL cp_fm_set_all(admm_env%lambda_inv_sqrt(ispin)%matrix, 0.0_dp, 1.0_dp)
END DO
CALL timestop(handle)
END SUBROUTINE purify_mo_none
! **************************************************************************************************
!> \brief ...
!> \param admm_env ...
!> \param mo_set ...
!> \param density_matrix ...
!> \param ispin ...
!> \param blocked ...
! **************************************************************************************************
SUBROUTINE purify_dm_cauchy(admm_env, mo_set, density_matrix, ispin, blocked)
TYPE(admm_type), POINTER :: admm_env
TYPE(mo_set_type), POINTER :: mo_set
TYPE(dbcsr_type), POINTER :: density_matrix
INTEGER :: ispin
LOGICAL, INTENT(IN) :: blocked
CHARACTER(len=*), PARAMETER :: routineN = 'purify_dm_cauchy'
INTEGER :: handle, i, nao_aux_fit, nao_orb, nmo, &
nspins
REAL(KIND=dp) :: pole
TYPE(cp_fm_type), POINTER :: mo_coeff_aux_fit
CALL timeset(routineN, handle)
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
nmo = admm_env%nmo(ispin)
nspins = SIZE(admm_env%P_to_be_purified)
CALL get_mo_set(mo_set=mo_set, mo_coeff=mo_coeff_aux_fit)
!! * For the time beeing, get the P to be purified from the mo_coeffs
!! * This needs to be replaced with the a block modified P
IF (.NOT. blocked) THEN
CALL cp_gemm('N', 'T', nao_aux_fit, nao_aux_fit, nmo, &
1.0_dp, mo_coeff_aux_fit, mo_coeff_aux_fit, 0.0_dp, &
admm_env%P_to_be_purified(ispin)%matrix)
END IF
CALL cp_fm_to_fm(admm_env%S, admm_env%work_aux_aux)
CALL cp_fm_to_fm(admm_env%P_to_be_purified(ispin)%matrix, admm_env%work_aux_aux2)
CALL cp_fm_cholesky_decompose(admm_env%work_aux_aux)
CALL cp_fm_cholesky_reduce(admm_env%work_aux_aux2, admm_env%work_aux_aux, itype=3)
CALL cp_fm_syevd(admm_env%work_aux_aux2, admm_env%R_purify(ispin)%matrix, &
admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data)
CALL cp_fm_cholesky_restore(admm_env%R_purify(ispin)%matrix, nao_aux_fit, admm_env%work_aux_aux, &
admm_env%work_aux_aux3, op="MULTIPLY", pos="LEFT", transa="T")
CALL cp_fm_to_fm(admm_env%work_aux_aux3, admm_env%R_purify(ispin)%matrix)
! *** Construct Matrix M for Hadamard Product
CALL cp_fm_set_all(admm_env%M_purify(ispin)%matrix, 0.0_dp)
pole = 0.0_dp
DO i = 1, nao_aux_fit
pole = Heaviside(admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(i) - 0.5_dp)
CALL cp_fm_set_element(admm_env%M_purify(ispin)%matrix, i, i, pole)
END DO
CALL cp_fm_upper_to_full(admm_env%M_purify(ispin)%matrix, admm_env%work_aux_aux)
CALL copy_dbcsr_to_fm(density_matrix, admm_env%work_aux_aux3)
CALL cp_fm_upper_to_full(admm_env%work_aux_aux3, admm_env%work_aux_aux)
! ** S^(-1)*R
CALL cp_gemm('N', 'N', nao_aux_fit, nao_aux_fit, nao_aux_fit, &
1.0_dp, admm_env%S_inv, admm_env%R_purify(ispin)%matrix, 0.0_dp, &
admm_env%work_aux_aux)
! ** S^(-1)*R*M
CALL cp_gemm('N', 'N', nao_aux_fit, nao_aux_fit, nao_aux_fit, &
1.0_dp, admm_env%work_aux_aux, admm_env%M_purify(ispin)%matrix, 0.0_dp, &
admm_env%work_aux_aux2)
! ** S^(-1)*R*M*R^T*S^(-1)
CALL cp_gemm('N', 'T', nao_aux_fit, nao_aux_fit, nao_aux_fit, &
1.0_dp, admm_env%work_aux_aux2, admm_env%work_aux_aux, 0.0_dp, &
admm_env%work_aux_aux3)
CALL copy_fm_to_dbcsr(admm_env%work_aux_aux3, density_matrix, keep_sparsity=.TRUE.)
IF (nspins == 1) THEN
CALL dbcsr_scale(density_matrix, 2.0_dp)
END IF
CALL timestop(handle)
END SUBROUTINE purify_dm_cauchy
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
! **************************************************************************************************
SUBROUTINE merge_ks_matrix_cauchy(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'merge_ks_matrix_cauchy'
INTEGER :: blk, handle, i, iatom, ispin, j, jatom, &
nao_aux_fit, nao_orb, nmo
REAL(dp) :: eig_diff, pole, tmp
REAL(dp), DIMENSION(:, :), POINTER :: sparse_block
TYPE(admm_type), POINTER :: admm_env
TYPE(cp_fm_type), POINTER :: mo_coeff
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_ks_aux_fit
TYPE(dbcsr_type), POINTER :: matrix_k_tilde
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mo_set_p_type), DIMENSION(:), POINTER :: mos
CALL timeset(routineN, handle)
NULLIFY (admm_env, dft_control, matrix_ks, matrix_ks_aux_fit, mos, mo_coeff)
CALL get_qs_env(qs_env, &
admm_env=admm_env, &
dft_control=dft_control, &
matrix_ks=matrix_ks, &
matrix_ks_aux_fit=matrix_ks_aux_fit, &
mos=mos)
DO ispin = 1, dft_control%nspins
nao_aux_fit = admm_env%nao_aux_fit
nao_orb = admm_env%nao_orb
nmo = admm_env%nmo(ispin)
CALL get_mo_set(mo_set=mos(ispin)%mo_set, mo_coeff=mo_coeff)
IF (.NOT. admm_env%block_dm) THEN
!** Get P from mo_coeffs, otherwise we have troubles with occupation numbers ...
CALL cp_gemm('N', 'T', nao_orb, nao_orb, nmo, &
1.0_dp, mo_coeff, mo_coeff, 0.0_dp, &
admm_env%work_orb_orb)
!! A*P
CALL cp_gemm('N', 'N', nao_aux_fit, nao_orb, nao_orb, &
1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
admm_env%work_aux_orb2)
!! A*P*A^T
CALL cp_gemm('N', 'T', nao_aux_fit, nao_aux_fit, nao_orb, &
1.0_dp, admm_env%work_aux_orb2, admm_env%A, 0.0_dp, &
admm_env%P_to_be_purified(ispin)%matrix)
END IF
CALL cp_fm_to_fm(admm_env%S, admm_env%work_aux_aux)
CALL cp_fm_to_fm(admm_env%P_to_be_purified(ispin)%matrix, admm_env%work_aux_aux2)
CALL cp_fm_cholesky_decompose(admm_env%work_aux_aux)
CALL cp_fm_cholesky_reduce(admm_env%work_aux_aux2, admm_env%work_aux_aux, itype=3)
CALL cp_fm_syevd(admm_env%work_aux_aux2, admm_env%R_purify(ispin)%matrix, &
admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data)
CALL cp_fm_cholesky_restore(admm_env%R_purify(ispin)%matrix, nao_aux_fit, admm_env%work_aux_aux, &
admm_env%work_aux_aux3, op="MULTIPLY", pos="LEFT", transa="T")
CALL cp_fm_to_fm(admm_env%work_aux_aux3, admm_env%R_purify(ispin)%matrix)
! *** Construct Matrix M for Hadamard Product
pole = 0.0_dp
DO i = 1, nao_aux_fit
DO j = i, nao_aux_fit
eig_diff = (admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(i) - &
admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(j))
! *** two eigenvalues could be the degenerated. In that case use 2nd order formula for the poles
IF (ABS(eig_diff) == 0.0_dp) THEN
pole = delta(admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(i) - 0.5_dp)
CALL cp_fm_set_element(admm_env%M_purify(ispin)%matrix, i, j, pole)
ELSE
pole = 1.0_dp/(admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(i) - &
admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(j))
tmp = Heaviside(admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(i) - 0.5_dp)
tmp = tmp - Heaviside(admm_env%eigvals_P_to_be_purified(ispin)%eigvals%data(j) - 0.5_dp)
pole = tmp*pole