-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_tddfpt2_forces.F
1335 lines (1225 loc) · 64.6 KB
/
qs_tddfpt2_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_forces
USE admm_types, ONLY: admm_type,&
get_admm_env
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind,&
get_atomic_kind_set
USE cp_control_types, ONLY: dft_control_type,&
tddfpt2_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_complete_redistribute, dbcsr_copy, dbcsr_create, dbcsr_dot, dbcsr_p_type, &
dbcsr_release, dbcsr_scale, dbcsr_set, dbcsr_type, dbcsr_type_antisymmetric
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_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_copy_general,&
cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE exstates_types, ONLY: excited_energy_type,&
exstate_potential_release
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_energy_potential, ONLY: integrate_four_center
USE hfx_ri, ONLY: hfx_ri_update_ks
USE hfx_types, ONLY: hfx_type
USE input_constants, ONLY: do_admm_aux_exch_func_none,&
oe_shift,&
tddfpt_kernel_full,&
tddfpt_kernel_none,&
tddfpt_kernel_stda
USE input_section_types, ONLY: section_get_lval,&
section_vals_get,&
section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_string_length,&
dp
USE message_passing, ONLY: mp_para_env_type
USE mulliken, ONLY: ao_charges
USE parallel_gemm_api, ONLY: parallel_gemm
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_density_matrices, ONLY: calculate_wx_matrix,&
calculate_xwx_matrix
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_force_types, ONLY: allocate_qs_force,&
deallocate_qs_force,&
qs_force_type,&
sum_qs_force,&
total_qs_force,&
zero_qs_force
USE qs_fxc, ONLY: qs_fxc_analytic,&
qs_fxc_fdiff
USE qs_gapw_densities, ONLY: prepare_gapw_den
USE qs_integrate_potential, ONLY: integrate_v_rspace
USE qs_kernel_types, ONLY: kernel_env_type
USE qs_kind_types, ONLY: get_qs_kind,&
get_qs_kind_set,&
qs_kind_type
USE qs_ks_atom, ONLY: update_ks_atom
USE qs_ks_reference, ONLY: ks_ref_potential,&
ks_ref_potential_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_oce_types, ONLY: 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_rho0_types, ONLY: get_rho0_mpole
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_fhxc_forces, ONLY: fhxc_force,&
stda_force
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_xc_2nd_deriv_atom
USE task_list_types, ONLY: task_list_type
USE xtb_ehess, ONLY: xtb_coulomb_hessian
USE xtb_types, ONLY: get_xtb_atom_param,&
xtb_atom_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_tddfpt2_forces'
PUBLIC :: tddfpt_forces_main
! **************************************************************************************************
CONTAINS
! **************************************************************************************************
!> \brief Perform TDDFPT gradient calculation.
!> \param qs_env Quickstep environment
!> \param gs_mos ...
!> \param ex_env ...
!> \param kernel_env ...
!> \param sub_env ...
!> \param work_matrices ...
!> \par History
!> * 10.2022 created JHU
! **************************************************************************************************
SUBROUTINE tddfpt_forces_main(qs_env, gs_mos, ex_env, kernel_env, sub_env, work_matrices)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(tddfpt_ground_state_mos), DIMENSION(:), &
POINTER :: gs_mos
TYPE(excited_energy_type), POINTER :: ex_env
TYPE(kernel_env_type) :: kernel_env
TYPE(tddfpt_subgroup_env_type) :: sub_env
TYPE(tddfpt_work_matrices) :: work_matrices
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_forces_main'
INTEGER :: handle, ispin, nspins
TYPE(admm_type), POINTER :: admm_env
TYPE(cp_fm_struct_type), POINTER :: matrix_struct
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_pe_asymm, matrix_pe_symm, &
matrix_s, matrix_s_aux_fit
TYPE(dft_control_type), POINTER :: dft_control
TYPE(tddfpt2_control_type), POINTER :: tddfpt_control
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, dft_control=dft_control)
nspins = dft_control%nspins
tddfpt_control => dft_control%tddfpt2_control
! rhs of linres equation
IF (ASSOCIATED(ex_env%cpmos)) THEN
DO ispin = 1, SIZE(ex_env%cpmos)
CALL cp_fm_release(ex_env%cpmos(ispin))
END DO
DEALLOCATE (ex_env%cpmos)
END IF
ALLOCATE (ex_env%cpmos(nspins))
DO ispin = 1, nspins
CALL cp_fm_get_info(matrix=ex_env%evect(ispin), matrix_struct=matrix_struct)
CALL cp_fm_create(ex_env%cpmos(ispin), matrix_struct)
CALL cp_fm_set_all(ex_env%cpmos(ispin), 0.0_dp)
END DO
CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s)
NULLIFY (matrix_pe_asymm, matrix_pe_symm)
CALL dbcsr_allocate_matrix_set(ex_env%matrix_pe, nspins)
CALL dbcsr_allocate_matrix_set(matrix_pe_symm, nspins)
CALL dbcsr_allocate_matrix_set(matrix_pe_asymm, nspins)
DO ispin = 1, nspins
ALLOCATE (ex_env%matrix_pe(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_pe(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(ex_env%matrix_pe(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(ex_env%matrix_pe(ispin)%matrix, 0.0_dp)
ALLOCATE (matrix_pe_symm(ispin)%matrix)
CALL dbcsr_create(matrix_pe_symm(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(matrix_pe_symm(ispin)%matrix, ex_env%matrix_pe(ispin)%matrix)
ALLOCATE (matrix_pe_asymm(ispin)%matrix)
CALL dbcsr_create(matrix_pe_asymm(ispin)%matrix, template=matrix_s(1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_complete_redistribute(ex_env%matrix_pe(ispin)%matrix, matrix_pe_asymm(ispin)%matrix)
CALL tddfpt_resvec1(ex_env%evect(ispin), gs_mos(ispin)%mos_occ, &
matrix_s(1)%matrix, ex_env%matrix_pe(ispin)%matrix)
END DO
!
! ground state ADMM!
IF (dft_control%do_admm) THEN
CALL get_qs_env(qs_env, admm_env=admm_env)
CALL get_admm_env(admm_env, matrix_s_aux_fit=matrix_s_aux_fit)
CALL dbcsr_allocate_matrix_set(ex_env%matrix_pe_admm, nspins)
DO ispin = 1, nspins
ALLOCATE (ex_env%matrix_pe_admm(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_pe_admm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
CALL dbcsr_copy(ex_env%matrix_pe_admm(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_set(ex_env%matrix_pe_admm(ispin)%matrix, 0.0_dp)
CALL tddfpt_resvec1_admm(ex_env%matrix_pe(ispin)%matrix, &
admm_env, ex_env%matrix_pe_admm(ispin)%matrix)
END DO
END IF
!
CALL dbcsr_allocate_matrix_set(ex_env%matrix_hz, nspins)
DO ispin = 1, nspins
ALLOCATE (ex_env%matrix_hz(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_hz(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(ex_env%matrix_hz(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(ex_env%matrix_hz(ispin)%matrix, 0.0_dp)
END DO
IF (dft_control%qs_control%xtb) THEN
CALL tddfpt_resvec2_xtb(qs_env, ex_env%matrix_pe, gs_mos, ex_env%matrix_hz, ex_env%cpmos)
ELSE
CALL tddfpt_resvec2(qs_env, ex_env%matrix_pe, ex_env%matrix_pe_admm, &
gs_mos, ex_env%matrix_hz, ex_env%cpmos)
END IF
!
CALL dbcsr_allocate_matrix_set(ex_env%matrix_px1, nspins)
CALL dbcsr_allocate_matrix_set(ex_env%matrix_px1_asymm, nspins)
DO ispin = 1, nspins
ALLOCATE (ex_env%matrix_px1(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_px1(ispin)%matrix, template=matrix_s(1)%matrix)
CALL dbcsr_copy(ex_env%matrix_px1(ispin)%matrix, matrix_s(1)%matrix)
CALL dbcsr_set(ex_env%matrix_px1(ispin)%matrix, 0.0_dp)
ALLOCATE (ex_env%matrix_px1_asymm(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_px1_asymm(ispin)%matrix, template=matrix_s(1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_complete_redistribute(ex_env%matrix_px1(ispin)%matrix, ex_env%matrix_px1_asymm(ispin)%matrix)
END DO
! Kernel ADMM
IF (tddfpt_control%do_admm) THEN
CALL get_qs_env(qs_env, admm_env=admm_env)
CALL get_admm_env(admm_env, matrix_s_aux_fit=matrix_s_aux_fit)
CALL dbcsr_allocate_matrix_set(ex_env%matrix_px1_admm, nspins)
CALL dbcsr_allocate_matrix_set(ex_env%matrix_px1_admm_asymm, nspins)
DO ispin = 1, nspins
ALLOCATE (ex_env%matrix_px1_admm(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_px1_admm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix)
CALL dbcsr_copy(ex_env%matrix_px1_admm(ispin)%matrix, matrix_s_aux_fit(1)%matrix)
CALL dbcsr_set(ex_env%matrix_px1_admm(ispin)%matrix, 0.0_dp)
ALLOCATE (ex_env%matrix_px1_admm_asymm(ispin)%matrix)
CALL dbcsr_create(ex_env%matrix_px1_admm_asymm(ispin)%matrix, template=matrix_s_aux_fit(1)%matrix, &
matrix_type=dbcsr_type_antisymmetric)
CALL dbcsr_complete_redistribute(ex_env%matrix_px1_admm(ispin)%matrix, &
ex_env%matrix_px1_admm_asymm(ispin)%matrix)
END DO
END IF
! TDA forces
CALL tddfpt_forces(qs_env, ex_env, gs_mos, kernel_env, sub_env, work_matrices)
! Rotate res vector cpmos into original frame of occupied orbitals
CALL tddfpt_resvec3(qs_env, ex_env%cpmos, work_matrices)
CALL dbcsr_deallocate_matrix_set(matrix_pe_symm)
CALL dbcsr_deallocate_matrix_set(matrix_pe_asymm)
CALL timestop(handle)
END SUBROUTINE tddfpt_forces_main
! **************************************************************************************************
!> \brief Calculate direct tddft forces
!> \param qs_env ...
!> \param ex_env ...
!> \param gs_mos ...
!> \param kernel_env ...
!> \param sub_env ...
!> \param work_matrices ...
!> \par History
!> * 01.2020 screated [JGH]
! **************************************************************************************************
SUBROUTINE tddfpt_forces(qs_env, ex_env, gs_mos, kernel_env, sub_env, work_matrices)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(excited_energy_type), POINTER :: ex_env
TYPE(tddfpt_ground_state_mos), DIMENSION(:), &
POINTER :: gs_mos
TYPE(kernel_env_type), INTENT(IN) :: kernel_env
TYPE(tddfpt_subgroup_env_type) :: sub_env
TYPE(tddfpt_work_matrices) :: work_matrices
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_forces'
INTEGER :: handle
INTEGER, ALLOCATABLE, DIMENSION(:) :: natom_of_kind
LOGICAL :: debug_forces
REAL(KIND=dp) :: ehartree, exc
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dft_control_type), POINTER :: dft_control
TYPE(qs_force_type), DIMENSION(:), POINTER :: ks_force, td_force
CALL timeset(routineN, handle)
! for extended debug output
debug_forces = ex_env%debug_forces
! prepare force array
CALL get_qs_env(qs_env, dft_control=dft_control, force=ks_force, &
atomic_kind_set=atomic_kind_set)
CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, natom_of_kind=natom_of_kind)
NULLIFY (td_force)
CALL allocate_qs_force(td_force, natom_of_kind)
DEALLOCATE (natom_of_kind)
CALL zero_qs_force(td_force)
CALL set_qs_env(qs_env, force=td_force)
!
IF (dft_control%qs_control%xtb) THEN
CALL tddfpt_force_direct(qs_env, ex_env, gs_mos, kernel_env, sub_env, &
work_matrices, debug_forces)
ELSE
!
CALL exstate_potential_release(ex_env)
CALL ks_ref_potential(qs_env, ex_env%vh_rspace, ex_env%vxc_rspace, &
ex_env%vtau_rspace, ex_env%vadmm_rspace, ehartree, exc)
CALL ks_ref_potential_atom(qs_env, ex_env%local_rho_set, ex_env%local_rho_set_admm, &
ex_env%vh_rspace)
CALL tddfpt_force_direct(qs_env, ex_env, gs_mos, kernel_env, sub_env, &
work_matrices, debug_forces)
END IF
!
! add TD and KS forces
CALL get_qs_env(qs_env, force=td_force)
CALL sum_qs_force(ks_force, td_force)
CALL set_qs_env(qs_env, force=ks_force)
CALL deallocate_qs_force(td_force)
!
CALL timestop(handle)
END SUBROUTINE tddfpt_forces
! **************************************************************************************************
!> \brief Calculate direct tddft forces
!> \param qs_env ...
!> \param ex_env ...
!> \param gs_mos ...
!> \param kernel_env ...
!> \param sub_env ...
!> \param work_matrices ...
!> \param debug_forces ...
!> \par History
!> * 01.2020 screated [JGH]
! **************************************************************************************************
SUBROUTINE tddfpt_force_direct(qs_env, ex_env, gs_mos, kernel_env, sub_env, work_matrices, &
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(kernel_env_type), INTENT(IN) :: kernel_env
TYPE(tddfpt_subgroup_env_type) :: sub_env
TYPE(tddfpt_work_matrices) :: work_matrices
LOGICAL :: debug_forces
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_force_direct'
INTEGER :: handle, iounit, ispin, natom, norb, &
nspins
REAL(KIND=dp) :: evalue
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: ftot1, ftot2
REAL(KIND=dp), DIMENSION(3) :: fodeb
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cp_fm_type), DIMENSION(:), POINTER :: evect
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_ks, matrix_s, matrix_wx1, &
matrix_wz, scrm
TYPE(dft_control_type), POINTER :: dft_control
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(qs_force_type), DIMENSION(:), POINTER :: force
TYPE(qs_ks_env_type), POINTER :: ks_env
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
evect => ex_env%evect
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, para_env=para_env, &
sab_orb=sab_orb, dft_control=dft_control, force=force)
NULLIFY (tddfpt_control)
tddfpt_control => dft_control%tddfpt2_control
nspins = dft_control%nspins
IF (debug_forces) THEN
CALL get_qs_env(qs_env, natom=natom, atomic_kind_set=atomic_kind_set)
ALLOCATE (ftot1(3, natom))
CALL total_qs_force(ftot1, force, atomic_kind_set)
END IF
CALL tddfpt_kernel_force(qs_env, ex_env, gs_mos, kernel_env, sub_env, work_matrices, debug_forces)
! Overlap matrix
matrix_wx1 => ex_env%matrix_wx1
CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, matrix_ks=matrix_ks)
NULLIFY (matrix_wz)
CALL dbcsr_allocate_matrix_set(matrix_wz, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_wz(ispin)%matrix)
CALL dbcsr_create(matrix=matrix_wz(ispin)%matrix, template=matrix_s(1)%matrix)
CALL cp_dbcsr_alloc_block_from_nbl(matrix_wz(ispin)%matrix, sab_orb)
CALL dbcsr_set(matrix_wz(ispin)%matrix, 0.0_dp)
CALL cp_fm_get_info(gs_mos(ispin)%mos_occ, ncol_global=norb)
CALL cp_dbcsr_plus_fm_fm_t(matrix_wz(ispin)%matrix, matrix_v=evect(ispin), ncol=norb)
evalue = ex_env%evalue
IF (tddfpt_control%oe_corr == oe_shift) THEN
evalue = ex_env%evalue - tddfpt_control%ev_shift
END IF
CALL dbcsr_scale(matrix_wz(ispin)%matrix, evalue)
CALL calculate_wx_matrix(gs_mos(ispin)%mos_occ, evect(ispin), matrix_ks(ispin)%matrix, &
matrix_wz(ispin)%matrix)
END DO
IF (nspins == 2) THEN
CALL dbcsr_add(matrix_wz(1)%matrix, matrix_wz(2)%matrix, &
alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
END IF
NULLIFY (scrm)
IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
CALL build_overlap_matrix(ks_env, matrix_s=scrm, &
matrix_name="OVERLAP MATRIX", &
basis_type_a="ORB", basis_type_b="ORB", &
sab_nl=sab_orb, calculate_forces=.TRUE., &
matrix_p=matrix_wz(1)%matrix)
CALL dbcsr_deallocate_matrix_set(scrm)
CALL dbcsr_deallocate_matrix_set(matrix_wz)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: Wx*dS ", fodeb
END IF
! Overlap matrix
CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, matrix_ks=matrix_ks)
NULLIFY (matrix_wz)
CALL dbcsr_allocate_matrix_set(matrix_wz, nspins)
DO ispin = 1, nspins
ALLOCATE (matrix_wz(ispin)%matrix)
CALL dbcsr_create(matrix=matrix_wz(ispin)%matrix, template=matrix_s(1)%matrix)
CALL cp_dbcsr_alloc_block_from_nbl(matrix_wz(ispin)%matrix, sab_orb)
CALL dbcsr_set(matrix_wz(ispin)%matrix, 0.0_dp)
CALL cp_fm_get_info(gs_mos(ispin)%mos_occ, ncol_global=norb)
evalue = ex_env%evalue
IF (tddfpt_control%oe_corr == oe_shift) THEN
evalue = ex_env%evalue - tddfpt_control%ev_shift
END IF
CALL calculate_xwx_matrix(gs_mos(ispin)%mos_occ, evect(ispin), matrix_s(1)%matrix, &
matrix_ks(ispin)%matrix, matrix_wz(ispin)%matrix, evalue)
END DO
IF (nspins == 2) THEN
CALL dbcsr_add(matrix_wz(1)%matrix, matrix_wz(2)%matrix, &
alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
END IF
NULLIFY (scrm)
IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
CALL build_overlap_matrix(ks_env, matrix_s=scrm, &
matrix_name="OVERLAP MATRIX", &
basis_type_a="ORB", basis_type_b="ORB", &
sab_nl=sab_orb, calculate_forces=.TRUE., &
matrix_p=matrix_wz(1)%matrix)
CALL dbcsr_deallocate_matrix_set(scrm)
CALL dbcsr_deallocate_matrix_set(matrix_wz)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: xWx*dS ", fodeb
END IF
! Overlap matrix
IF (ASSOCIATED(matrix_wx1)) THEN
IF (nspins == 2) THEN
CALL dbcsr_add(matrix_wx1(1)%matrix, matrix_wx1(2)%matrix, &
alpha_scalar=0.5_dp, beta_scalar=0.5_dp)
END IF
NULLIFY (scrm)
IF (debug_forces) fodeb(1:3) = force(1)%overlap(1:3, 1)
CALL build_overlap_matrix(ks_env, matrix_s=scrm, &
matrix_name="OVERLAP MATRIX", &
basis_type_a="ORB", basis_type_b="ORB", &
sab_nl=sab_orb, calculate_forces=.TRUE., &
matrix_p=matrix_wx1(1)%matrix)
CALL dbcsr_deallocate_matrix_set(scrm)
IF (debug_forces) THEN
fodeb(1:3) = force(1)%overlap(1:3, 1) - fodeb(1:3)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T33,3F16.8)") "DEBUG:: WK*dS ", fodeb
END IF
END IF
IF (debug_forces) THEN
ALLOCATE (ftot2(3, natom))
CALL total_qs_force(ftot2, force, atomic_kind_set)
fodeb(1:3) = ftot2(1:3, 1) - ftot1(1:3, 1)
CALL para_env%sum(fodeb)
IF (iounit > 0) WRITE (iounit, "(T3,A,T30,3F16.8)") "DEBUG:: Excitation Force", fodeb
DEALLOCATE (ftot1, ftot2)
END IF
CALL timestop(handle)
END SUBROUTINE tddfpt_force_direct
! **************************************************************************************************
!> \brief ...
!> \param evect ...
!> \param mos_occ ...
!> \param matrix_s ...
!> \param matrix_pe ...
! **************************************************************************************************
SUBROUTINE tddfpt_resvec1(evect, mos_occ, matrix_s, matrix_pe)
TYPE(cp_fm_type), INTENT(IN) :: evect, mos_occ
TYPE(dbcsr_type), POINTER :: matrix_s, matrix_pe
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_resvec1'
INTEGER :: handle, iounit, nao, norb
REAL(KIND=dp) :: tmp
TYPE(cp_fm_struct_type), POINTER :: fmstruct, fmstruct2
TYPE(cp_fm_type) :: cxmat, xxmat
TYPE(cp_logger_type), POINTER :: logger
CALL timeset(routineN, handle)
! X*X^T
CALL cp_fm_get_info(mos_occ, nrow_global=nao, ncol_global=norb)
CALL cp_dbcsr_plus_fm_fm_t(matrix_pe, matrix_v=evect, ncol=norb)
! X^T*S*X
CALL cp_fm_get_info(evect, matrix_struct=fmstruct)
NULLIFY (fmstruct2)
CALL cp_fm_struct_create(fmstruct=fmstruct2, template_fmstruct=fmstruct, &
nrow_global=norb, ncol_global=norb)
CALL cp_fm_create(xxmat, matrix_struct=fmstruct2)
CALL cp_fm_struct_release(fmstruct2)
CALL cp_fm_create(cxmat, matrix_struct=fmstruct)
CALL cp_dbcsr_sm_fm_multiply(matrix_s, evect, cxmat, norb, alpha=1.0_dp, beta=0.0_dp)
CALL parallel_gemm('T', 'N', norb, norb, nao, 1.0_dp, cxmat, evect, 0.0_dp, xxmat)
CALL parallel_gemm('N', 'N', nao, norb, norb, 1.0_dp, mos_occ, xxmat, 0.0_dp, cxmat)
CALL cp_fm_release(xxmat)
! C*C^T*XX
CALL cp_dbcsr_plus_fm_fm_t(matrix_pe, matrix_v=mos_occ, matrix_g=cxmat, &
ncol=norb, alpha=-1.0_dp, symmetry_mode=1)
CALL cp_fm_release(cxmat)
!
! Test for Tr(Pe*S)=0
CALL dbcsr_dot(matrix_pe, matrix_s, tmp)
IF (ABS(tmp) > 1.e-08_dp) THEN
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
CPWARN("Electron count of excitation density matrix is non-zero.")
IF (iounit > 0) THEN
WRITE (iounit, "(T2,A,T61,G20.10)") "Measured electron count is ", tmp
WRITE (iounit, "(T2,A,/)") REPEAT("*", 79)
END IF
END IF
!
CALL timestop(handle)
END SUBROUTINE tddfpt_resvec1
! **************************************************************************************************
!> \brief PA = A * P * A(T)
!> \param matrix_pe ...
!> \param admm_env ...
!> \param matrix_pe_admm ...
! **************************************************************************************************
SUBROUTINE tddfpt_resvec1_admm(matrix_pe, admm_env, matrix_pe_admm)
TYPE(dbcsr_type), POINTER :: matrix_pe
TYPE(admm_type), POINTER :: admm_env
TYPE(dbcsr_type), POINTER :: matrix_pe_admm
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_resvec1_admm'
INTEGER :: handle, nao, nao_aux
CALL timeset(routineN, handle)
!
nao_aux = admm_env%nao_aux_fit
nao = admm_env%nao_orb
!
CALL copy_dbcsr_to_fm(matrix_pe, 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_pe_admm, keep_sparsity=.TRUE.)
!
CALL timestop(handle)
END SUBROUTINE tddfpt_resvec1_admm
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param matrix_pe ...
!> \param matrix_pe_admm ...
!> \param gs_mos ...
!> \param matrix_hz ...
!> \param cpmos ...
! **************************************************************************************************
SUBROUTINE tddfpt_resvec2(qs_env, matrix_pe, matrix_pe_admm, gs_mos, matrix_hz, cpmos)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_pe, matrix_pe_admm
TYPE(tddfpt_ground_state_mos), DIMENSION(:), &
POINTER :: gs_mos
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_hz
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: cpmos
CHARACTER(LEN=*), PARAMETER :: routineN = 'tddfpt_resvec2'
CHARACTER(LEN=default_string_length) :: basis_type
INTEGER :: handle, iounit, ispin, mspin, n_rep_hf, &
nao, nao_aux, natom, norb, nspins
LOGICAL :: deriv2_analytic, distribute_fock_matrix, &
do_hfx, gapw, gapw_xc, &
hfx_treat_lsd_in_core, &
s_mstruct_changed
REAL(KIND=dp) :: eh1, focc, rhotot, thartree
REAL(KIND=dp), DIMENSION(2) :: total_rho
REAL(KIND=dp), DIMENSION(:), POINTER :: Qlm_tot
TYPE(admm_type), POINTER :: admm_env
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cp_fm_type), POINTER :: mos
TYPE(cp_logger_type), POINTER :: logger
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: msaux
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: mhz, mpe
TYPE(dbcsr_type), POINTER :: dbwork
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
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab, sab_aux_fit
TYPE(oce_matrix_type), POINTER :: oce
TYPE(pw_c1d_gs_type) :: rho_tot_gspace, v_hartree_gspace
TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER :: rho_g, rho_g_aux, rhoz_g_aux, trho_g, &
trho_xc_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) :: v_hartree_rspace
TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER :: rho_r, rho_r_aux, rhoz_r_aux, tau_r, &
trho_r, trho_xc_r, v_xc, v_xc_tau
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, rho_xc, rhoz_aux, trho
TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho1_atom_set, rho_atom_set
TYPE(section_vals_type), POINTER :: hfx_section, input, xc_section
TYPE(task_list_type), POINTER :: task_list
CALL timeset(routineN, handle)
NULLIFY (pw_env)
CALL get_qs_env(qs_env=qs_env, pw_env=pw_env, ks_env=ks_env, &
dft_control=dft_control, para_env=para_env)
CPASSERT(ASSOCIATED(pw_env))
nspins = dft_control%nspins
gapw = dft_control%qs_control%gapw
gapw_xc = dft_control%qs_control%gapw_xc
CPASSERT(.NOT. dft_control%tddfpt2_control%do_exck)
CPASSERT(.NOT. dft_control%tddfpt2_control%do_hfxsr)
CPASSERT(.NOT. dft_control%tddfpt2_control%do_hfxlr)
NULLIFY (auxbas_pw_pool, poisson_env)
! gets the tmp grids
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
poisson_env=poisson_env)
CALL auxbas_pw_pool%create_pw(v_hartree_gspace)
CALL auxbas_pw_pool%create_pw(rho_tot_gspace)
CALL auxbas_pw_pool%create_pw(v_hartree_rspace)
ALLOCATE (trho_r(nspins), trho_g(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(trho_r(ispin))
CALL auxbas_pw_pool%create_pw(trho_g(ispin))
END DO
IF (gapw_xc) THEN
ALLOCATE (trho_xc_r(nspins), trho_xc_g(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(trho_xc_r(ispin))
CALL auxbas_pw_pool%create_pw(trho_xc_g(ispin))
END DO
END IF
! GAPW/GAPW_XC initializations
NULLIFY (hartree_local, local_rho_set)
IF (gapw) THEN
CALL get_qs_env(qs_env, &
atomic_kind_set=atomic_kind_set, &
natom=natom, &
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)
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)
ELSEIF (gapw_xc) THEN
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)
END IF
total_rho = 0.0_dp
CALL pw_zero(rho_tot_gspace)
DO ispin = 1, nspins
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_pe(ispin)%matrix, &
rho=trho_r(ispin), &
rho_gspace=trho_g(ispin), &
soft_valid=gapw, &
total_rho=total_rho(ispin))
CALL pw_axpy(trho_g(ispin), rho_tot_gspace)
IF (gapw_xc) THEN
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=matrix_pe(ispin)%matrix, &
rho=trho_xc_r(ispin), &
rho_gspace=trho_xc_g(ispin), &
soft_valid=gapw_xc, &
total_rho=rhotot)
END IF
END DO
! GAPW o GAPW_XC require the calculation of hard and soft local densities
IF (gapw .OR. gapw_xc) THEN
CALL get_qs_env(qs_env=qs_env, oce=oce, sab_orb=sab)
CALL calculate_rho_atom_coeff(qs_env, matrix_pe, 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)
END IF
rhotot = SUM(total_rho)
IF (gapw) THEN
CALL get_rho0_mpole(local_rho_set%rho0_mpole, Qlm_tot=Qlm_tot)
rhotot = rhotot + local_rho_set%rho0_mpole%total_rho0_h
CALL pw_axpy(local_rho_set%rho0_mpole%rho0_s_gs, rho_tot_gspace)
END IF
IF (ABS(rhotot) > 1.e-05_dp) THEN
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
CPWARN("Real space electron count of excitation density is non-zero.")
IF (iounit > 0) THEN
WRITE (iounit, "(T2,A,T61,G20.10)") "Measured electron count is ", rhotot
WRITE (iounit, "(T2,A,/)") REPEAT("*", 79)
END IF
END IF
! calculate associated hartree potential
CALL pw_poisson_solve(poisson_env, rho_tot_gspace, thartree, &
v_hartree_gspace)
CALL pw_transfer(v_hartree_gspace, v_hartree_rspace)
CALL pw_scale(v_hartree_rspace, v_hartree_rspace%pw_grid%dvol)
IF (gapw) THEN
CALL Vh_1c_gg_integrals(qs_env, thartree, hartree_local%ecoul_1c, &
local_rho_set, para_env, tddft=.TRUE.)
CALL integrate_vhg0_rspace(qs_env, v_hartree_rspace, para_env, &
calculate_forces=.FALSE., &
local_rho_set=local_rho_set)
END IF
! Fxc*drho term
CALL get_qs_env(qs_env, rho=rho)
CALL qs_rho_get(rho, rho_r=rho_r, rho_g=rho_g)
!
CALL get_qs_env(qs_env, input=input)
IF (dft_control%do_admm) THEN
CALL get_qs_env(qs_env, admm_env=admm_env)
xc_section => admm_env%xc_section_primary
ELSE
xc_section => section_vals_get_subs_vals(input, "DFT%XC")
END IF
!
deriv2_analytic = section_get_lval(xc_section, "2ND_DERIV_ANALYTICAL")
IF (deriv2_analytic) THEN
NULLIFY (v_xc, v_xc_tau, tau_r)
IF (gapw_xc) THEN
CALL get_qs_env(qs_env=qs_env, rho_xc=rho_xc)
CALL qs_fxc_analytic(rho_xc, trho_xc_r, tau_r, xc_section, auxbas_pw_pool, .FALSE., v_xc, v_xc_tau)
ELSE
CALL qs_fxc_analytic(rho, trho_r, tau_r, xc_section, auxbas_pw_pool, .FALSE., v_xc, v_xc_tau)
END IF
IF (gapw .OR. gapw_xc) THEN
CALL get_qs_env(qs_env, rho_atom_set=rho_atom_set)
rho1_atom_set => local_rho_set%rho_atom_set
CALL calculate_xc_2nd_deriv_atom(rho_atom_set, rho1_atom_set, qs_env, xc_section, para_env, &
do_tddft=.TRUE., do_triplet=.FALSE.)
END IF
ELSE
CPABORT("NYA 00006")
NULLIFY (v_xc, trho)
ALLOCATE (trho)
CALL qs_rho_create(trho)
CALL qs_rho_set(trho, rho_r=trho_r, rho_g=trho_g)
CALL qs_fxc_fdiff(ks_env, rho, trho, xc_section, 6, .FALSE., v_xc, v_xc_tau)
DEALLOCATE (trho)
END IF
DO ispin = 1, nspins
CALL dbcsr_set(matrix_hz(ispin)%matrix, 0.0_dp)
CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
END DO
IF (gapw_xc) THEN
DO ispin = 1, nspins
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_hartree_rspace, &
hmat=matrix_hz(ispin), &
calculate_forces=.FALSE.)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_xc(ispin), &
hmat=matrix_hz(ispin), &
gapw=gapw_xc, calculate_forces=.FALSE.)
END DO
ELSE
! vtot = v_xc(ispin) + v_hartree
DO ispin = 1, nspins
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_xc(ispin), &
hmat=matrix_hz(ispin), &
gapw=gapw, calculate_forces=.FALSE.)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_hartree_rspace, &
hmat=matrix_hz(ispin), &
gapw=gapw, calculate_forces=.FALSE.)
END DO
END IF
IF (gapw .OR. gapw_xc) THEN
mhz(1:nspins, 1:1) => matrix_hz(1:nspins)
mpe(1:nspins, 1:1) => matrix_pe(1:nspins)
CALL update_ks_atom(qs_env, mhz, mpe, forces=.FALSE., &
rho_atom_external=local_rho_set%rho_atom_set)
END IF
CALL auxbas_pw_pool%give_back_pw(v_hartree_gspace)
CALL auxbas_pw_pool%give_back_pw(v_hartree_rspace)
CALL auxbas_pw_pool%give_back_pw(rho_tot_gspace)
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(trho_r(ispin))
CALL auxbas_pw_pool%give_back_pw(trho_g(ispin))
CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
END DO
DEALLOCATE (trho_r, trho_g, v_xc)
IF (gapw_xc) THEN
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(trho_xc_r(ispin))
CALL auxbas_pw_pool%give_back_pw(trho_xc_g(ispin))
END DO
DEALLOCATE (trho_xc_r, trho_xc_g)
END IF
IF (ASSOCIATED(v_xc_tau)) THEN
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(v_xc_tau(ispin))
END DO
DEALLOCATE (v_xc_tau)
END IF
IF (dft_control%do_admm) THEN
IF (qs_env%admm_env%aux_exch_func == do_admm_aux_exch_func_none) THEN
! nothing to do
ELSE
! add ADMM xc_section_aux terms: f_x[rhoz_ADMM]
CALL get_qs_env(qs_env, admm_env=admm_env)
CALL get_admm_env(admm_env, rho_aux_fit=rho_aux_fit, matrix_s_aux_fit=msaux, &
task_list_aux_fit=task_list)
basis_type = "AUX_FIT"
!
NULLIFY (mpe, mhz)
ALLOCATE (mpe(nspins, 1))
CALL dbcsr_allocate_matrix_set(mhz, nspins, 1)
DO ispin = 1, nspins
ALLOCATE (mhz(ispin, 1)%matrix)
CALL dbcsr_create(mhz(ispin, 1)%matrix, template=msaux(1)%matrix)
CALL dbcsr_copy(mhz(ispin, 1)%matrix, msaux(1)%matrix)
CALL dbcsr_set(mhz(ispin, 1)%matrix, 0.0_dp)
mpe(ispin, 1)%matrix => matrix_pe_admm(ispin)%matrix
END DO
!
! GAPW/GAPW_XC initializations
NULLIFY (local_rho_set_admm)
IF (admm_env%do_gapw) THEN
basis_type = "AUX_FIT_SOFT"
task_list => admm_env%admm_gapw_env%task_list
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set)
CALL get_admm_env(admm_env, sab_aux_fit=sab_aux_fit)
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)
CALL calculate_rho_atom_coeff(qs_env, matrix_pe_admm, &
rho_atom_set=local_rho_set_admm%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=local_rho_set_admm, &
do_rho0=.FALSE., kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
END IF
!
xc_section => admm_env%xc_section_aux
!
NULLIFY (rho_g_aux, rho_r_aux, rhoz_g_aux, rhoz_r_aux)
CALL qs_rho_get(rho_aux_fit, rho_r=rho_r_aux, rho_g=rho_g_aux)
! rhoz_aux
ALLOCATE (rhoz_r_aux(nspins), rhoz_g_aux(nspins))
DO ispin = 1, nspins
CALL auxbas_pw_pool%create_pw(rhoz_r_aux(ispin))
CALL auxbas_pw_pool%create_pw(rhoz_g_aux(ispin))
END DO
DO ispin = 1, nspins
CALL calculate_rho_elec(ks_env=ks_env, matrix_p=mpe(ispin, 1)%matrix, &
rho=rhoz_r_aux(ispin), rho_gspace=rhoz_g_aux(ispin), &
basis_type=basis_type, &
task_list_external=task_list)
END DO
!
NULLIFY (v_xc)
deriv2_analytic = section_get_lval(xc_section, "2ND_DERIV_ANALYTICAL")
IF (deriv2_analytic) THEN
NULLIFY (tau_r)
CALL qs_fxc_analytic(rho_aux_fit, rhoz_r_aux, tau_r, xc_section, auxbas_pw_pool, .FALSE., v_xc, v_xc_tau)
ELSE
CPABORT("NYA 00007")
NULLIFY (rhoz_aux)
ALLOCATE (rhoz_aux)
CALL qs_rho_create(rhoz_aux)
CALL qs_rho_set(rhoz_aux, rho_r=rhoz_r_aux, rho_g=rhoz_g_aux)
CALL qs_fxc_fdiff(ks_env, rho_aux_fit, rhoz_aux, xc_section, 6, .FALSE., v_xc, v_xc_tau)
DEALLOCATE (rhoz_aux)
END IF
!
DO ispin = 1, nspins
CALL pw_scale(v_xc(ispin), v_xc(ispin)%pw_grid%dvol)
CALL integrate_v_rspace(qs_env=qs_env, v_rspace=v_xc(ispin), &
hmat=mhz(ispin, 1), basis_type=basis_type, &
calculate_forces=.FALSE., &
task_list_external=task_list)
END DO
DO ispin = 1, nspins
CALL auxbas_pw_pool%give_back_pw(v_xc(ispin))
CALL auxbas_pw_pool%give_back_pw(rhoz_r_aux(ispin))
CALL auxbas_pw_pool%give_back_pw(rhoz_g_aux(ispin))
END DO
DEALLOCATE (v_xc, rhoz_r_aux, rhoz_g_aux)
!
IF (admm_env%do_gapw) THEN
rho_atom_set => admm_env%admm_gapw_env%local_rho_set%rho_atom_set
rho1_atom_set => local_rho_set_admm%rho_atom_set
CALL calculate_xc_2nd_deriv_atom(rho_atom_set, rho1_atom_set, qs_env, xc_section, &
para_env, kind_set_external=admm_env%admm_gapw_env%admm_kind_set)
CALL update_ks_atom(qs_env, mhz(:, 1), matrix_pe_admm, forces=.FALSE., tddft=.FALSE., &
rho_atom_external=rho1_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)
END IF
!
nao = admm_env%nao_orb
nao_aux = admm_env%nao_aux_fit
ALLOCATE (dbwork)
CALL dbcsr_create(dbwork, template=matrix_hz(1)%matrix)