-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_tddfpt2_fhxc_forces.F
1770 lines (1693 loc) · 92.5 KB
/
qs_tddfpt2_fhxc_forces.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 !
!--------------------------------------------------------------------------------------------------!
MODULE qs_tddfpt2_fhxc_forces
USE admm_methods, ONLY: admm_projection_derivative
USE admm_types, ONLY: admm_type,&
get_admm_env
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind_set
USE cell_types, ONLY: cell_type,&
pbc
USE cp_control_types, ONLY: dft_control_type,&
stda_control_type,&
tddfpt2_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_complete_redistribute, dbcsr_copy, dbcsr_create, dbcsr_filter, &
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_transposed, dbcsr_type, &
dbcsr_type_antisymmetric, dbcsr_type_no_symmetry
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,&
cp_dbcsr_sm_fm_multiply,&
dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE cp_fm_basic_linalg, ONLY: cp_fm_row_scale,&
cp_fm_schur_product
USE cp_fm_pool_types, ONLY: fm_pool_create_fm,&
fm_pool_give_back_fm
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_to_fm,&
cp_fm_type,&
cp_fm_vectorssum
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE ewald_environment_types, ONLY: ewald_env_get,&
ewald_environment_type
USE ewald_methods_tb, ONLY: tb_ewald_overlap,&
tb_spme_evaluate
USE ewald_pw_types, ONLY: ewald_pw_type
USE exstates_types, ONLY: excited_energy_type
USE hartree_local_methods, ONLY: Vh_1c_gg_integrals,&
init_coulomb_local
USE hartree_local_types, ONLY: hartree_local_create,&
hartree_local_release,&
hartree_local_type
USE hfx_derivatives, ONLY: derivatives_four_center
USE hfx_energy_potential, ONLY: integrate_four_center
USE hfx_ri, ONLY: hfx_ri_update_forces,&
hfx_ri_update_ks
USE hfx_types, ONLY: hfx_type
USE input_constants, ONLY: do_admm_aux_exch_func_none,&
tddfpt_kernel_full,&
xc_kernel_method_analytic,&
xc_kernel_method_best,&
xc_kernel_method_numeric,&
xc_none
USE input_section_types, ONLY: section_vals_get,&
section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_string_length,&
dp
USE mathconstants, ONLY: oorootpi
USE message_passing, ONLY: mp_para_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE particle_methods, ONLY: get_particle_set
USE particle_types, ONLY: particle_type
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_methods, ONLY: pw_axpy,&
pw_scale,&
pw_transfer,&
pw_zero
USE pw_poisson_methods, ONLY: pw_poisson_solve
USE pw_poisson_types, ONLY: pw_poisson_type
USE pw_pool_types, ONLY: pw_pool_type
USE pw_types, ONLY: pw_c1d_gs_type,&
pw_r3d_rs_type
USE qs_collocate_density, ONLY: calculate_rho_elec
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_force_types, ONLY: qs_force_type
USE qs_fxc, ONLY: qs_fgxc_create,&
qs_fgxc_gdiff,&
qs_fgxc_release
USE qs_gapw_densities, ONLY: prepare_gapw_den
USE qs_integrate_potential, ONLY: integrate_v_rspace
USE qs_kernel_types, ONLY: full_kernel_env_type
USE qs_kind_types, ONLY: qs_kind_type
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_neighbor_list_types, ONLY: neighbor_list_set_p_type
USE qs_oce_methods, ONLY: build_oce_matrices
USE qs_oce_types, ONLY: allocate_oce_set,&
create_oce_set,&
oce_matrix_type
USE qs_overlap, ONLY: build_overlap_matrix
USE qs_rho0_ggrid, ONLY: integrate_vhg0_rspace,&
rho0_s_grid_create
USE qs_rho0_methods, ONLY: init_rho0
USE qs_rho_atom_methods, ONLY: allocate_rho_atom_internals,&
calculate_rho_atom_coeff
USE qs_rho_atom_types, ONLY: rho_atom_type
USE qs_rho_types, ONLY: qs_rho_create,&
qs_rho_get,&
qs_rho_set,&
qs_rho_type
USE qs_tddfpt2_stda_types, ONLY: stda_env_type
USE qs_tddfpt2_stda_utils, ONLY: get_lowdin_x,&
setup_gamma
USE qs_tddfpt2_subgroups, ONLY: tddfpt_subgroup_env_type
USE qs_tddfpt2_types, ONLY: tddfpt_ground_state_mos,&
tddfpt_work_matrices
USE qs_vxc_atom, ONLY: calculate_gfxc_atom,&
gfxc_atom_diff
USE task_list_types, ONLY: task_list_type
USE util, ONLY: get_limit
USE virial_types, ONLY: virial_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_tddfpt2_fhxc_forces'
PUBLIC :: fhxc_force, stda_force
! **************************************************************************************************
CONTAINS
! **************************************************************************************************
!> \brief Calculate direct tddft forces
!> \param qs_env ...
!> \param ex_env ...
!> \param gs_mos ...
!> \param full_kernel ...
!> \param debug_forces ...
!> \par History
!> * 01.2020 screated [JGH]
! **************************************************************************************************
SUBROUTINE fhxc_force(qs_env, ex_env, gs_mos, full_kernel, debug_forces)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(excited_energy_type), POINTER :: ex_env
TYPE(tddfpt_ground_state_mos), DIMENSION(:), &
POINTER :: gs_mos
TYPE(full_kernel_env_type), INTENT(IN) :: full_kernel
LOGICAL, INTENT(IN) :: debug_forces
CHARACTER(LEN=*), PARAMETER :: routineN = 'fhxc_force'
CHARACTER(LEN=default_string_length) :: basis_type
INTEGER :: handle, iounit, ispin, mspin, myfun, &
n_rep_hf, nao, nao_aux, natom, nkind, &
norb, nspins, order
LOGICAL :: distribute_fock_matrix, do_admm, do_analytic, do_hfx, do_hfxlr, do_hfxsr, &
do_numeric, gapw, gapw_xc, hfx_treat_lsd_in_core, is_rks_triplets, s_mstruct_changed, &
use_virial
REAL(KIND=dp) :: eh1, eh1c, eps_delta, eps_fit, focc, &
fscal, fval, kval, xehartree
REAL(KIND=dp), DIMENSION(3) :: fodeb
TYPE(admm_type), POINTER :: admm_env
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cp_fm_struct_type), POINTER :: fm_struct, fm_struct_mat
TYPE(cp_fm_type) :: cvcmat, vcvec
TYPE(cp_fm_type), DIMENSION(:), POINTER :: cpmos, evect
TYPE(cp_fm_type), POINTER :: mos
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_fx, matrix_gx, matrix_hfx, &
matrix_hfx_admm, matrix_hfx_admm_asymm, matrix_hfx_asymm, matrix_hx, matrix_p, &
matrix_p_admm, matrix_px1, matrix_px1_admm, matrix_px1_admm_asymm, matrix_px1_asymm, &
matrix_s, matrix_s_aux_fit, matrix_wx1, mdum, mfx, mgx
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: mhe, mpe, mpga
TYPE(dbcsr_type), POINTER :: dbwork, dbwork_asymm
TYPE(dft_control_type), POINTER :: dft_control
TYPE(hartree_local_type), POINTER :: hartree_local
TYPE(hfx_type), DIMENSION(:, :), POINTER :: x_data
TYPE(local_rho_type), POINTER :: local_rho_set, local_rho_set_admm, local_rho_set_f, &
local_rho_set_f_admm, local_rho_set_g, local_rho_set_g_admm
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab, sab_aux_fit, sab_orb, sap_oce
TYPE(oce_matrix_type), POINTER :: oce
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(pw_c1d_gs_type) :: rhox_tot_gspace, xv_hartree_gspace
TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g_aux, rhox_g, rhox_g_aux, rhoxx_g
TYPE(pw_env_type), POINTER :: pw_env
TYPE(pw_poisson_type), POINTER :: poisson_env
TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
TYPE(pw_r3d_rs_type) :: xv_hartree_rspace
TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: fxc_rho, fxc_tau, gxc_rho, gxc_tau, &
rho_r_aux, rhox_r, rhox_r_aux, rhoxx_r
TYPE(qs_force_type), DIMENSION(:), POINTER :: force
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_rho_type), POINTER :: rho, rho_aux_fit, rhox, rhox_aux
TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom_set, rho_atom_set_f, &
rho_atom_set_g
TYPE(section_vals_type), POINTER :: hfx_section, xc_section
TYPE(task_list_type), POINTER :: task_list
TYPE(tddfpt2_control_type), POINTER :: tddfpt_control
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
IF (logger%para_env%is_source()) THEN
iounit = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
ELSE
iounit = -1
END IF
CALL get_qs_env(qs_env, dft_control=dft_control)
tddfpt_control => dft_control%tddfpt2_control
nspins = dft_control%nspins
is_rks_triplets = tddfpt_control%rks_triplets .AND. (nspins == 1)
CPASSERT(tddfpt_control%kernel == tddfpt_kernel_full)
do_hfx = tddfpt_control%do_hfx
do_hfxsr = tddfpt_control%do_hfxsr
do_hfxlr = tddfpt_control%do_hfxlr
do_admm = tddfpt_control%do_admm
gapw = dft_control%qs_control%gapw
gapw_xc = dft_control%qs_control%gapw_xc
evect => ex_env%evect
matrix_px1 => ex_env%matrix_px1
matrix_px1_admm => ex_env%matrix_px1_admm
matrix_px1_asymm => ex_env%matrix_px1_asymm
matrix_px1_admm_asymm => ex_env%matrix_px1_admm_asymm
focc = 1.0_dp
IF (nspins == 2) focc = 0.5_dp
DO ispin = 1, nspins
CALL dbcsr_set(matrix_px1(ispin)%matrix, 0.0_dp)
CALL cp_fm_get_info(evect(ispin), ncol_global=norb)
CALL cp_dbcsr_plus_fm_fm_t(matrix_px1(ispin)%matrix, &
matrix_v=evect(ispin), &
matrix_g=gs_mos(ispin)%mos_occ, &
ncol=norb, alpha=2.0_dp*focc, symmetry_mode=1)
CALL dbcsr_set(matrix_px1_asymm(ispin)%matrix, 0.0_dp)
CALL cp_fm_get_info(evect(ispin), ncol_global=norb)
CALL cp_dbcsr_plus_fm_fm_t(matrix_px1_asymm(ispin)%matrix, &
matrix_v=gs_mos(ispin)%mos_occ, &
matrix_g=evect(ispin), &
ncol=norb, alpha=2.0_dp*focc, &
symmetry_mode=-1)
END DO
!
CALL get_qs_env(qs_env, ks_env=ks_env, pw_env=pw_env, para_env=para_env)
!
NULLIFY (hartree_local, local_rho_set, local_rho_set_admm)
IF (gapw .OR. gapw_xc) THEN
IF (nspins == 2) THEN
DO ispin = 1, nspins
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 2.0_dp)
END DO
END IF
CALL get_qs_env(qs_env, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=qs_kind_set)
CALL local_rho_set_create(local_rho_set)
CALL allocate_rho_atom_internals(local_rho_set%rho_atom_set, atomic_kind_set, &
qs_kind_set, dft_control, para_env)
IF (gapw) THEN
CALL get_qs_env(qs_env, natom=natom)
CALL init_rho0(local_rho_set, qs_env, dft_control%qs_control%gapw_control, &
zcore=0.0_dp)
CALL rho0_s_grid_create(pw_env, local_rho_set%rho0_mpole)
CALL hartree_local_create(hartree_local)
CALL init_coulomb_local(hartree_local, natom)
END IF
CALL get_qs_env(qs_env=qs_env, oce=oce, sap_oce=sap_oce, sab_orb=sab)
CALL create_oce_set(oce)
CALL get_qs_env(qs_env=qs_env, nkind=nkind, particle_set=particle_set)
CALL allocate_oce_set(oce, nkind)
eps_fit = dft_control%qs_control%gapw_control%eps_fit
CALL build_oce_matrices(oce%intac, .TRUE., 1, qs_kind_set, particle_set, &
sap_oce, eps_fit)
CALL set_qs_env(qs_env, oce=oce)
mpga(1:nspins, 1:1) => matrix_px1(1:nspins)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set%rho_atom_set, &
qs_kind_set, oce, sab, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set, do_rho0=gapw)
!
CALL local_rho_set_create(local_rho_set_f)
CALL allocate_rho_atom_internals(local_rho_set_f%rho_atom_set, atomic_kind_set, &
qs_kind_set, dft_control, para_env)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set_f%rho_atom_set, &
qs_kind_set, oce, sab, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set_f, do_rho0=.FALSE.)
!
CALL local_rho_set_create(local_rho_set_g)
CALL allocate_rho_atom_internals(local_rho_set_g%rho_atom_set, atomic_kind_set, &
qs_kind_set, dft_control, para_env)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set_g%rho_atom_set, &
qs_kind_set, oce, sab, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set_g, do_rho0=.FALSE.)
IF (nspins == 2) THEN
DO ispin = 1, nspins
CALL dbcsr_scale(matrix_px1(ispin)%matrix, 0.5_dp)
END DO
END IF
END IF
!
IF (do_admm) THEN
CALL get_qs_env(qs_env, admm_env=admm_env)
nao_aux = admm_env%nao_aux_fit
nao = admm_env%nao_orb
!
DO ispin = 1, nspins
CALL copy_dbcsr_to_fm(matrix_px1(ispin)%matrix, admm_env%work_orb_orb)
CALL parallel_gemm('N', 'N', nao_aux, nao, nao, &
1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
admm_env%work_aux_orb)
CALL parallel_gemm('N', 'T', nao_aux, nao_aux, nao, &
1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
admm_env%work_aux_aux)
CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, matrix_px1_admm(ispin)%matrix, &
keep_sparsity=.TRUE.)
CALL copy_dbcsr_to_fm(matrix_px1_asymm(ispin)%matrix, admm_env%work_orb_orb)
CALL parallel_gemm('N', 'N', nao_aux, nao, nao, &
1.0_dp, admm_env%A, admm_env%work_orb_orb, 0.0_dp, &
admm_env%work_aux_orb)
CALL parallel_gemm('N', 'T', nao_aux, nao_aux, nao, &
1.0_dp, admm_env%work_aux_orb, admm_env%A, 0.0_dp, &
admm_env%work_aux_aux)
CALL copy_fm_to_dbcsr(admm_env%work_aux_aux, matrix_px1_admm_asymm(ispin)%matrix, &
keep_sparsity=.TRUE.)
END DO
!
IF (admm_env%do_gapw) THEN
IF (do_admm .AND. tddfpt_control%admm_xc_correction) THEN
IF (admm_env%aux_exch_func == do_admm_aux_exch_func_none) THEN
! nothing to do
ELSE
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
CALL local_rho_set_create(local_rho_set_admm)
CALL allocate_rho_atom_internals(local_rho_set_admm%rho_atom_set, atomic_kind_set, &
admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
mpga(1:nspins, 1:1) => matrix_px1_admm(1:nspins)
CALL get_admm_env(admm_env, sab_aux_fit=sab_aux_fit)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set_admm%rho_atom_set, &
admm_env%admm_gapw_env%admm_kind_set, &
admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set_admm, do_rho0=.FALSE., &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
!
CALL local_rho_set_create(local_rho_set_f_admm)
CALL allocate_rho_atom_internals(local_rho_set_f_admm%rho_atom_set, atomic_kind_set, &
admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set_f_admm%rho_atom_set, &
admm_env%admm_gapw_env%admm_kind_set, &
admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set_f_admm, do_rho0=.FALSE., &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
!
CALL local_rho_set_create(local_rho_set_g_admm)
CALL allocate_rho_atom_internals(local_rho_set_g_admm%rho_atom_set, atomic_kind_set, &
admm_env%admm_gapw_env%admm_kind_set, dft_control, para_env)
CALL calculate_rho_atom_coeff(qs_env, mpga, local_rho_set_g_admm%rho_atom_set, &
admm_env%admm_gapw_env%admm_kind_set, &
admm_env%admm_gapw_env%oce, sab_aux_fit, para_env)
CALL prepare_gapw_den(qs_env, local_rho_set_g_admm, do_rho0=.FALSE., &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
END IF
END IF
END IF
END IF
!
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
poisson_env=poisson_env)
ALLOCATE (rhox_r(nspins), rhox_g(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(rhox_r(ispin))
CALL auxbas_pw_pool%create_pw(rhox_g(ispin))
END DO
CALL auxbas_pw_pool%create_pw(rhox_tot_gspace)
CALL pw_zero(rhox_tot_gspace)
DO ispin = 1, nspins
IF (nspins == 2) CALL dbcsr_scale(matrix_px1(ispin)%matrix, 2.0_dp)
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_px1(ispin)%matrix, &
rho=rhox_r(ispin), rho_gspace=rhox_g(ispin), &
soft_valid=gapw)
CALL pw_axpy(rhox_g(ispin), rhox_tot_gspace)
IF (nspins == 2) CALL dbcsr_scale(matrix_px1(ispin)%matrix, 0.5_dp)
END DO
IF (gapw_xc) THEN
ALLOCATE (rhoxx_r(nspins), rhoxx_g(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(rhoxx_r(ispin))
CALL auxbas_pw_pool%create_pw(rhoxx_g(ispin))
END DO
DO ispin = 1, nspins
IF (nspins == 2) CALL dbcsr_scale(matrix_px1(ispin)%matrix, 2.0_dp)
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_px1(ispin)%matrix, &
rho=rhoxx_r(ispin), rho_gspace=rhoxx_g(ispin), &
soft_valid=gapw_xc)
IF (nspins == 2) CALL dbcsr_scale(matrix_px1(ispin)%matrix, 0.5_dp)
END DO
END IF
CALL get_qs_env(qs_env, matrix_s=matrix_s, force=force)
IF (.NOT. is_rks_triplets) THEN
CALL auxbas_pw_pool%create_pw(xv_hartree_rspace)
CALL auxbas_pw_pool%create_pw(xv_hartree_gspace)
! calculate associated hartree potential
IF (gapw) THEN
CALL pw_axpy(local_rho_set%rho0_mpole%rho0_s_gs, rhox_tot_gspace)
END IF
CALL pw_poisson_solve(poisson_env, rhox_tot_gspace, xehartree, &
xv_hartree_gspace)
CALL pw_transfer(xv_hartree_gspace, xv_hartree_rspace)
CALL pw_scale(xv_hartree_rspace, xv_hartree_rspace%pw_grid%dvol)
!
IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
NULLIFY (matrix_hx)
CALL dbcsr_allocate_matrix_set(matrix_hx, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_hx(ispin)%matrix)
CALL dbcsr_create(matrix_hx(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(matrix_hx(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(matrix_hx(ispin)%matrix, 0.0_dp)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=xv_hartree_rspace, &
pmat=matrix_px1(ispin), hmat=matrix_hx(ispin), &
gapw=gapw, calculate_forces=.TRUE.)
END DO
IF (debug_forces) THEN
fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKh[X] ", fodeb
END IF
IF (gapw) THEN
IF (debug_forces) fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1)
CALL Vh_1c_gg_integrals(qs_env, eh1c, hartree_local%ecoul_1c, local_rho_set, para_env, tddft=.TRUE., &
core_2nd=.TRUE.)
IF (nspins == 1) THEN
kval = 1.0_dp
ELSE
kval = 0.5_dp
END IF
CALL integrate_vhg0_rspace(qs_env, xv_hartree_rspace, para_env, calculate_forces=.TRUE., &
local_rho_set=local_rho_set, kforce=kval)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%g0s_Vh_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKh[X]PAWg0", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
CALL update_ks_atom(qs_env, matrix_hx, matrix_px1, forces=.TRUE., &
rho_atom_external=local_rho_set%rho_atom_set)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKh[X]PAW ", fodeb
END IF
END IF
END IF
! XC
IF (full_kernel%do_exck) THEN
CPABORT("NYA")
END IF
NULLIFY (fxc_rho, fxc_tau, gxc_rho, gxc_tau)
xc_section => full_kernel%xc_section
CALL section_vals_val_get(xc_section, "XC_FUNCTIONAL%_SECTION_PARAMETERS_", &
i_val=myfun)
IF (myfun /= xc_none) THEN
SELECT CASE (ex_env%xc_kernel_method)
CASE (xc_kernel_method_best)
do_analytic = .TRUE.
do_numeric = .TRUE.
CASE (xc_kernel_method_analytic)
do_analytic = .TRUE.
do_numeric = .FALSE.
CASE (xc_kernel_method_numeric)
do_analytic = .FALSE.
do_numeric = .TRUE.
CASE DEFAULT
CPABORT("invalid xc_kernel_method")
END SELECT
order = ex_env%diff_order
eps_delta = ex_env%eps_delta_rho
IF (gapw_xc) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, rho_xc=rho)
ELSE
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, rho=rho)
END IF
CALL qs_rho_get(rho, rho_ao=matrix_p)
NULLIFY (rhox)
ALLOCATE (rhox)
CALL qs_rho_create(rhox)
IF (gapw_xc) THEN
CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhoxx_r, rho_g=rhoxx_g, &
rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
ELSE
CALL qs_rho_set(rho_struct=rhox, rho_ao=matrix_px1, rho_r=rhox_r, rho_g=rhox_g, &
rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
END IF
IF (do_analytic .AND. .NOT. do_numeric) THEN
CPABORT("Analytic 3rd EXC derivatives not available")
ELSEIF (do_numeric) THEN
IF (do_analytic) THEN
CALL qs_fgxc_gdiff(ks_env, rho, rhox, xc_section, order, eps_delta, is_rks_triplets, &
fxc_rho, fxc_tau, gxc_rho, gxc_tau)
ELSE
CALL qs_fgxc_create(ks_env, rho, rhox, xc_section, order, is_rks_triplets, &
fxc_rho, fxc_tau, gxc_rho, gxc_tau)
END IF
ELSE
CPABORT("FHXC forces analytic/numeric")
END IF
! Well, this is a hack :-(
! When qs_rho_set() was called on rhox it assumed ownership of the passed arrays.
! However, these arrays actually belong to ex_env. Hence, we can not call qs_rho_release()
! because this would release the arrays. Instead we're simply going to deallocate rhox.
DEALLOCATE (rhox)
IF (nspins == 2) THEN
DO ispin = 1, nspins
CALL pw_scale(gxc_rho(ispin), 0.5_dp)
IF (ASSOCIATED(gxc_tau)) CALL pw_scale(gxc_tau(ispin), 0.5_dp)
END DO
END IF
IF (gapw .OR. gapw_xc) THEN
IF (do_analytic .AND. .NOT. do_numeric) THEN
CPABORT("Analytic 3rd EXC derivatives not available")
ELSEIF (do_numeric) THEN
IF (do_analytic) THEN
CALL gfxc_atom_diff(qs_env, ex_env%local_rho_set%rho_atom_set, &
local_rho_set_f%rho_atom_set, local_rho_set_g%rho_atom_set, &
qs_kind_set, xc_section, is_rks_triplets, order, eps_delta)
ELSE
CALL calculate_gfxc_atom(qs_env, ex_env%local_rho_set%rho_atom_set, &
local_rho_set_f%rho_atom_set, local_rho_set_g%rho_atom_set, &
qs_kind_set, xc_section, is_rks_triplets, order)
END IF
ELSE
CPABORT("FHXC forces analytic/numeric")
END IF
END IF
IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
NULLIFY (matrix_fx)
CALL dbcsr_allocate_matrix_set(matrix_fx, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_fx(ispin)%matrix)
CALL dbcsr_create(matrix_fx(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(matrix_fx(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(matrix_fx(ispin)%matrix, 0.0_dp)
CALL pw_scale(fxc_rho(ispin), fxc_rho(ispin)%pw_grid%dvol)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=fxc_rho(ispin), &
pmat=matrix_px1(ispin), hmat=matrix_fx(ispin), &
gapw=(gapw .OR. gapw_xc), calculate_forces=.TRUE.)
END DO
IF (debug_forces) THEN
fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKf[X] ", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
NULLIFY (matrix_gx)
CALL dbcsr_allocate_matrix_set(matrix_gx, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_gx(ispin)%matrix)
CALL dbcsr_create(matrix_gx(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(matrix_gx(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(matrix_gx(ispin)%matrix, 0.0_dp)
CALL pw_scale(gxc_rho(ispin), gxc_rho(ispin)%pw_grid%dvol)
CALL pw_scale(gxc_rho(ispin), 0.5_dp)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=gxc_rho(ispin), &
pmat=matrix_p(ispin), hmat=matrix_gx(ispin), &
gapw=(gapw .OR. gapw_xc), calculate_forces=.TRUE.)
CALL dbcsr_scale(matrix_gx(ispin)%matrix, 2.0_dp)
END DO
IF (debug_forces) THEN
fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P*dKg[X] ", fodeb
END IF
CALL qs_fgxc_release(ks_env, fxc_rho, fxc_tau, gxc_rho, gxc_tau)
IF (gapw .OR. gapw_xc) THEN
IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
CALL update_ks_atom(qs_env, matrix_fx, matrix_px1, forces=.TRUE., tddft=.TRUE., &
rho_atom_external=local_rho_set_f%rho_atom_set, &
kintegral=1.0_dp, kforce=1.0_dp)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKf[X]PAW ", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
IF (nspins == 1) THEN
CALL update_ks_atom(qs_env, matrix_gx, matrix_p, forces=.TRUE., tddft=.TRUE., &
rho_atom_external=local_rho_set_g%rho_atom_set, &
kscale=0.5_dp)
ELSE
CALL update_ks_atom(qs_env, matrix_gx, matrix_p, forces=.TRUE., &
rho_atom_external=local_rho_set_g%rho_atom_set, &
kintegral=0.5_dp, kforce=0.25_dp)
END IF
IF (debug_forces) THEN
fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKg[X]PAW ", fodeb
END IF
END IF
END IF
! ADMM XC correction Exc[rho_admm]
IF (do_admm .AND. tddfpt_control%admm_xc_correction) THEN
IF (admm_env%aux_exch_func == do_admm_aux_exch_func_none) THEN
! nothing to do
ELSE
IF (.NOT. tddfpt_control%admm_symm) THEN
CALL cp_warn(__LOCATION__, "Forces need symmetric ADMM kernel corrections")
CPABORT("ADMM KERNEL CORRECTION")
END IF
xc_section => admm_env%xc_section_aux
CALL get_admm_env(admm_env, rho_aux_fit=rho_aux_fit, matrix_s_aux_fit=matrix_s_aux_fit, &
task_list_aux_fit=task_list)
basis_type = "AUX_FIT"
IF (admm_env%do_gapw) THEN
basis_type = "AUX_FIT_SOFT"
task_list => admm_env%admm_gapw_env%task_list
END IF
!
NULLIFY (mfx, mgx)
CALL dbcsr_allocate_matrix_set(mfx, nspins)
CALL dbcsr_allocate_matrix_set(mgx, nspins)
DO ispin = 1, nspins
ALLOCATE (mfx(ispin)%matrix, mgx(ispin)%matrix)
CALL dbcsr_create(mfx(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
CALL dbcsr_copy(mfx(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_set(mfx(ispin)%matrix, 0.0_dp)
CALL dbcsr_create(mgx(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
CALL dbcsr_copy(mgx(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_set(mgx(ispin)%matrix, 0.0_dp)
END DO
! ADMM density and response density
NULLIFY (rho_g_aux, rho_r_aux, rhox_g_aux, rhox_r_aux)
CALL qs_rho_get(rho_aux_fit, rho_r=rho_r_aux, rho_g=rho_g_aux)
CALL qs_rho_get(rho_aux_fit, rho_ao=matrix_p_admm)
! rhox_aux
ALLOCATE (rhox_r_aux(nspins), rhox_g_aux(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(rhox_r_aux(ispin))
CALL auxbas_pw_pool%create_pw(rhox_g_aux(ispin))
END DO
DO ispin = 1, nspins
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_px1_admm(ispin)%matrix, &
rho=rhox_r_aux(ispin), rho_gspace=rhox_g_aux(ispin), &
basis_type=basis_type, &
task_list_external=task_list)
END DO
!
NULLIFY (rhox_aux)
ALLOCATE (rhox_aux)
CALL qs_rho_create(rhox_aux)
CALL qs_rho_set(rho_struct=rhox_aux, rho_ao=matrix_px1_admm, &
rho_r=rhox_r_aux, rho_g=rhox_g_aux, &
rho_r_valid=.TRUE., rho_g_valid=.TRUE.)
IF (do_analytic .AND. .NOT. do_numeric) THEN
CPABORT("Analytic 3rd derivatives of EXC not available")
ELSEIF (do_numeric) THEN
IF (do_analytic) THEN
CALL qs_fgxc_gdiff(ks_env, rho_aux_fit, rhox_aux, xc_section, order, eps_delta, &
is_rks_triplets, fxc_rho, fxc_tau, gxc_rho, gxc_tau)
ELSE
CALL qs_fgxc_create(ks_env, rho_aux_fit, rhox_aux, xc_section, &
order, is_rks_triplets, fxc_rho, fxc_tau, gxc_rho, gxc_tau)
END IF
ELSE
CPABORT("FHXC forces analytic/numeric")
END IF
! Well, this is a hack :-(
! When qs_rho_set() was called on rhox_aux it assumed ownership of the passed arrays.
! However, these arrays actually belong to ex_env. Hence, we can not call qs_rho_release()
! because this would release the arrays. Instead we're simply going to deallocate rhox_aux.
DEALLOCATE (rhox_aux)
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(rhox_r_aux(ispin))
CALL auxbas_pw_pool%give_back_pw(rhox_g_aux(ispin))
END DO
DEALLOCATE (rhox_r_aux, rhox_g_aux)
fscal = 1.0_dp
IF (nspins == 2) fscal = 2.0_dp
!
IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
DO ispin = 1, nspins
CALL pw_scale(fxc_rho(ispin), fxc_rho(ispin)%pw_grid%dvol)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=fxc_rho(ispin), &
hmat=mfx(ispin), &
pmat=matrix_px1_admm(ispin), &
basis_type=basis_type, &
calculate_forces=.TRUE., &
force_adm=fscal, &
task_list_external=task_list)
END DO
IF (debug_forces) THEN
fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKf[X]ADMM", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%rho_elec(1:3, 1)
DO ispin = 1, nspins
CALL pw_scale(gxc_rho(ispin), gxc_rho(ispin)%pw_grid%dvol)
CALL pw_scale(gxc_rho(ispin), 0.5_dp)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=gxc_rho(ispin), &
hmat=mgx(ispin), &
pmat=matrix_p_admm(ispin), &
basis_type=basis_type, &
calculate_forces=.TRUE., &
force_adm=fscal, &
task_list_external=task_list)
CALL dbcsr_scale(mgx(ispin)%matrix, 2.0_dp)
END DO
IF (debug_forces) THEN
fodeb(1:3) = force(1)%rho_elec(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P*dKg[X]ADMM", fodeb
END IF
CALL qs_fgxc_release(ks_env, fxc_rho, fxc_tau, gxc_rho, gxc_tau)
!
IF (admm_env%do_gapw) THEN
CALL get_admm_env(admm_env, sab_aux_fit=sab_aux_fit)
rho_atom_set => admm_env%admm_gapw_env%local_rho_set%rho_atom_set
rho_atom_set_f => local_rho_set_f_admm%rho_atom_set
rho_atom_set_g => local_rho_set_g_admm%rho_atom_set
IF (do_analytic .AND. .NOT. do_numeric) THEN
CPABORT("Analytic 3rd EXC derivatives not available")
ELSEIF (do_numeric) THEN
IF (do_analytic) THEN
CALL gfxc_atom_diff(qs_env, rho_atom_set, &
rho_atom_set_f, rho_atom_set_g, &
admm_env%admm_gapw_env%admm_kind_set, xc_section, &
is_rks_triplets, order, eps_delta)
ELSE
CALL calculate_gfxc_atom(qs_env, rho_atom_set, &
rho_atom_set_f, rho_atom_set_g, &
admm_env%admm_gapw_env%admm_kind_set, xc_section, &
is_rks_triplets, order)
END IF
ELSE
CPABORT("FHXC forces analytic/numeric")
END IF
IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
IF (nspins == 1) THEN
CALL update_ks_atom(qs_env, mfx, matrix_px1_admm, forces=.TRUE., &
rho_atom_external=rho_atom_set_f, &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
kintegral=2.0_dp, kforce=0.5_dp)
ELSE
CALL update_ks_atom(qs_env, mfx, matrix_px1_admm, forces=.TRUE., &
rho_atom_external=rho_atom_set_f, &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
kintegral=2.0_dp, kforce=1.0_dp)
END IF
IF (debug_forces) THEN
fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Px*dKf[X]ADMM-PAW ", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1)
IF (nspins == 1) THEN
CALL update_ks_atom(qs_env, mgx, matrix_p, forces=.TRUE., &
rho_atom_external=rho_atom_set_g, &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
kintegral=1.0_dp, kforce=0.5_dp)
ELSE
CALL update_ks_atom(qs_env, mgx, matrix_p, forces=.TRUE., &
rho_atom_external=rho_atom_set_g, &
kind_set_external=admm_env%admm_gapw_env%admm_kind_set, &
oce_external=admm_env%admm_gapw_env%oce, sab_external=sab_aux_fit, &
kintegral=1.0_dp, kforce=1.0_dp)
END IF
IF (debug_forces) THEN
fodeb(1:3) = force(1)%Vhxc_atom(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P*dKg[X]ADMM-PAW ", fodeb
END IF
END IF
!
! A' fx A - Forces
!
IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
fval = 2.0_dp*REAL(nspins, KIND=dp)
CALL admm_projection_derivative(qs_env, mfx, matrix_px1, fval)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P*dfXC(P)*S' ", fodeb
END IF
IF (debug_forces) fodeb(1:3) = force(1)%overlap_admm(1:3, 1)
fval = 2.0_dp*REAL(nspins, KIND=dp)
CALL admm_projection_derivative(qs_env, mgx, matrix_p, fval)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%overlap_admm(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: P*dgXC(P)*S' ", fodeb
END IF
!
! Add ADMM fx/gx to the full basis fx/gx
fscal = 1.0_dp
IF (nspins == 2) fscal = 2.0_dp
nao = admm_env%nao_orb
nao_aux = admm_env%nao_aux_fit
ALLOCATE (dbwork)
CALL dbcsr_create(dbwork, template=matrix_fx(1)%matrix)
DO ispin = 1, nspins
! fx
CALL cp_dbcsr_sm_fm_multiply(mfx(ispin)%matrix, admm_env%A, &
admm_env%work_aux_orb, nao)
CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
admm_env%work_orb_orb)
CALL dbcsr_copy(dbwork, matrix_fx(1)%matrix)
CALL dbcsr_set(dbwork, 0.0_dp)
CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork, keep_sparsity=.TRUE.)
CALL dbcsr_add(matrix_fx(ispin)%matrix, dbwork, 1.0_dp, fscal)
! gx
CALL cp_dbcsr_sm_fm_multiply(mgx(ispin)%matrix, admm_env%A, &
admm_env%work_aux_orb, nao)
CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
admm_env%work_orb_orb)
CALL dbcsr_set(dbwork, 0.0_dp)
CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork, keep_sparsity=.TRUE.)
CALL dbcsr_add(matrix_gx(ispin)%matrix, dbwork, 1.0_dp, fscal)
END DO
CALL dbcsr_release(dbwork)
DEALLOCATE (dbwork)
CALL dbcsr_deallocate_matrix_set(mfx)
CALL dbcsr_deallocate_matrix_set(mgx)
END IF
END IF
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(rhox_r(ispin))
CALL auxbas_pw_pool%give_back_pw(rhox_g(ispin))
END DO
DEALLOCATE (rhox_r, rhox_g)
CALL auxbas_pw_pool%give_back_pw(rhox_tot_gspace)
IF (gapw_xc) THEN
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(rhoxx_r(ispin))
CALL auxbas_pw_pool%give_back_pw(rhoxx_g(ispin))
END DO
DEALLOCATE (rhoxx_r, rhoxx_g)
END IF
IF (.NOT. is_rks_triplets) THEN
CALL auxbas_pw_pool%give_back_pw(xv_hartree_rspace)
CALL auxbas_pw_pool%give_back_pw(xv_hartree_gspace)
END IF
! HFX
IF (do_hfx) THEN
NULLIFY (matrix_hfx, matrix_hfx_asymm)
CALL dbcsr_allocate_matrix_set(matrix_hfx, nspins)
CALL dbcsr_allocate_matrix_set(matrix_hfx_asymm, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_hfx(ispin)%matrix)
CALL dbcsr_create(matrix_hfx(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(matrix_hfx(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(matrix_hfx(ispin)%matrix, 0.0_dp)
ALLOCATE (matrix_hfx_asymm(ispin)%matrix)
CALL dbcsr_create(matrix_hfx_asymm(ispin)%matrix, template=matrix_s(1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_complete_redistribute(matrix_hfx(ispin)%matrix, matrix_hfx_asymm(ispin)%matrix)
END DO
!
xc_section => full_kernel%xc_section
hfx_section => section_vals_get_subs_vals(xc_section, "HF")
CALL section_vals_get(hfx_section, n_repetition=n_rep_hf)
CPASSERT(n_rep_hf == 1)
CALL section_vals_val_get(hfx_section, "TREAT_LSD_IN_CORE", l_val=hfx_treat_lsd_in_core, &
i_rep_section=1)
mspin = 1
IF (hfx_treat_lsd_in_core) mspin = nspins
!
CALL get_qs_env(qs_env=qs_env, x_data=x_data, s_mstruct_changed=s_mstruct_changed)
distribute_fock_matrix = .TRUE.
!
IF (do_admm) THEN
CALL get_admm_env(qs_env%admm_env, matrix_s_aux_fit=matrix_s_aux_fit)
NULLIFY (matrix_hfx_admm, matrix_hfx_admm_asymm)
CALL dbcsr_allocate_matrix_set(matrix_hfx_admm, nspins)
CALL dbcsr_allocate_matrix_set(matrix_hfx_admm_asymm, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_hfx_admm(ispin)%matrix)
CALL dbcsr_create(matrix_hfx_admm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
CALL dbcsr_copy(matrix_hfx_admm(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_set(matrix_hfx_admm(ispin)%matrix, 0.0_dp)
ALLOCATE (matrix_hfx_admm_asymm(ispin)%matrix)
CALL dbcsr_create(matrix_hfx_admm_asymm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_complete_redistribute(matrix_hfx_admm(ispin)%matrix, matrix_hfx_admm_asymm(ispin)%matrix)
END DO
!
NULLIFY (mpe, mhe)
ALLOCATE (mpe(nspins, 1), mhe(nspins, 1))
DO ispin = 1, nspins
mhe(ispin, 1)%matrix => matrix_hfx_admm(ispin)%matrix
mpe(ispin, 1)%matrix => matrix_px1_admm(ispin)%matrix
END DO
IF (x_data(1, 1)%do_hfx_ri) THEN
eh1 = 0.0_dp
CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
geometry_did_change=s_mstruct_changed, nspins=nspins, &
hf_fraction=x_data(1, 1)%general_parameter%fraction)
ELSE
DO ispin = 1, mspin
eh1 = 0.0
CALL integrate_four_center(qs_env, x_data, mhe, eh1, mpe, hfx_section, &
para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
ispin=ispin)
END DO
END IF
!anti-symmetric density
DO ispin = 1, nspins
mhe(ispin, 1)%matrix => matrix_hfx_admm_asymm(ispin)%matrix
mpe(ispin, 1)%matrix => matrix_px1_admm_asymm(ispin)%matrix
END DO
IF (x_data(1, 1)%do_hfx_ri) THEN
eh1 = 0.0_dp
CALL hfx_ri_update_ks(qs_env, x_data(1, 1)%ri_data, mhe, eh1, rho_ao=mpe, &
geometry_did_change=s_mstruct_changed, nspins=nspins, &
hf_fraction=x_data(1, 1)%general_parameter%fraction)
ELSE
DO ispin = 1, mspin
eh1 = 0.0
CALL integrate_four_center(qs_env, x_data, mhe, eh1, mpe, hfx_section, &
para_env, s_mstruct_changed, 1, distribute_fock_matrix, &
ispin=ispin)
END DO
END IF
!
nao = admm_env%nao_orb
nao_aux = admm_env%nao_aux_fit
ALLOCATE (dbwork, dbwork_asymm)
CALL dbcsr_create(dbwork, template=matrix_hfx(1)%matrix)
CALL dbcsr_create(dbwork_asymm, template=matrix_hfx(1)%matrix, matrix_type=dbcsr_type_antisymmetric)
DO ispin = 1, nspins
CALL cp_dbcsr_sm_fm_multiply(matrix_hfx_admm(ispin)%matrix, admm_env%A, &
admm_env%work_aux_orb, nao)
CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
admm_env%work_orb_orb)
CALL dbcsr_copy(dbwork, matrix_hfx(1)%matrix)
CALL dbcsr_set(dbwork, 0.0_dp)
CALL copy_fm_to_dbcsr(admm_env%work_orb_orb, dbwork, keep_sparsity=.TRUE.)
CALL dbcsr_add(matrix_hfx(ispin)%matrix, dbwork, 1.0_dp, 1.0_dp)
!anti-symmetric case
CALL cp_dbcsr_sm_fm_multiply(matrix_hfx_admm_asymm(ispin)%matrix, admm_env%A, &
admm_env%work_aux_orb, nao)
CALL parallel_gemm('T', 'N', nao, nao, nao_aux, &
1.0_dp, admm_env%A, admm_env%work_aux_orb, 0.0_dp, &
admm_env%work_orb_orb)
CALL dbcsr_copy(dbwork_asymm, matrix_hfx_asymm(1)%matrix)