-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_vxc_atom.F
2553 lines (2190 loc) · 122 KB
/
qs_vxc_atom.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief routines that build the integrals of the Vxc potential calculated
!> for the atomic density in the basis set of spherical primitives
! **************************************************************************************************
MODULE qs_vxc_atom
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind
USE basis_set_types, ONLY: get_gto_basis_set,&
gto_basis_set_type
USE cp_control_types, ONLY: dft_control_type
USE input_constants, ONLY: tddfpt_excitations,&
tddfpt_triplet,&
xc_none
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: dp
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: mp_para_env_type
USE orbital_pointers, ONLY: indso,&
nsoset
USE paw_basis_types, ONLY: get_paw_basis_info
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_grid_atom, ONLY: grid_atom_type
USE qs_harmonics_atom, ONLY: get_none0_cg_list,&
harmonics_atom_type
USE qs_kind_types, ONLY: get_qs_kind,&
has_nlcc,&
qs_kind_type
USE qs_linres_types, ONLY: nablavks_atom_type
USE qs_rho_atom_types, ONLY: get_rho_atom,&
rho_atom_coeff,&
rho_atom_type
USE util, ONLY: get_limit
USE xc_atom, ONLY: fill_rho_set,&
vxc_of_r_new,&
xc_2nd_deriv_of_r,&
xc_rho_set_atom_update
USE xc_derivative_set_types, ONLY: xc_derivative_set_type,&
xc_dset_create,&
xc_dset_release,&
xc_dset_zero_all
USE xc_derivatives, ONLY: xc_functionals_get_needs
USE xc_rho_cflags_types, ONLY: xc_rho_cflags_type
USE xc_rho_set_types, ONLY: xc_rho_set_create,&
xc_rho_set_release,&
xc_rho_set_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_vxc_atom'
PUBLIC :: calculate_vxc_atom, &
calculate_xc_2nd_deriv_atom, &
calc_rho_angular, &
calculate_gfxc_atom, &
gfxc_atom_diff, &
gaVxcgb_noGC
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param energy_only ...
!> \param exc1 the on-body ex energy contribution
!> \param gradient_atom_set ...
!> \param adiabatic_rescale_factor ...
!> \param kind_set_external provides a non-default kind_set to use
!> \param rho_atom_set_external provides a non-default atomic density set to use
!> \param xc_section_external provides an external non-default XC
! **************************************************************************************************
SUBROUTINE calculate_vxc_atom(qs_env, energy_only, exc1, gradient_atom_set, &
adiabatic_rescale_factor, kind_set_external, &
rho_atom_set_external, xc_section_external)
TYPE(qs_environment_type), POINTER :: qs_env
LOGICAL, INTENT(IN) :: energy_only
REAL(dp), INTENT(INOUT) :: exc1
TYPE(nablavks_atom_type), DIMENSION(:), OPTIONAL, &
POINTER :: gradient_atom_set
REAL(dp), INTENT(IN), OPTIONAL :: adiabatic_rescale_factor
TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
POINTER :: kind_set_external
TYPE(rho_atom_type), DIMENSION(:), OPTIONAL, &
POINTER :: rho_atom_set_external
TYPE(section_vals_type), OPTIONAL, POINTER :: xc_section_external
CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_vxc_atom'
INTEGER :: bo(2), handle, ia, iat, iatom, idir, &
ikind, ir, ispin, myfun, na, natom, &
nr, nspins, num_pe
INTEGER, DIMENSION(2, 3) :: bounds
INTEGER, DIMENSION(:), POINTER :: atom_list
LOGICAL :: donlcc, epr_xc, gradient_f, lsd, nlcc, &
paw_atom, tau_f
REAL(dp) :: density_cut, exc_h, exc_s, gradient_cut, &
my_adiabatic_rescale_factor, tau_cut
REAL(dp), DIMENSION(1, 1, 1) :: tau_d
REAL(dp), DIMENSION(1, 1, 1, 1) :: rho_d
REAL(dp), DIMENSION(:, :), POINTER :: rho_nlcc, weight
REAL(dp), DIMENSION(:, :, :), POINTER :: rho_h, rho_s, tau_h, tau_s, vtau_h, &
vtau_s, vxc_h, vxc_s
REAL(dp), DIMENSION(:, :, :, :), POINTER :: drho_h, drho_s, vxg_h, vxg_s
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dft_control_type), POINTER :: dft_control
TYPE(grid_atom_type), POINTER :: grid_atom
TYPE(gto_basis_set_type), POINTER :: basis_1c
TYPE(harmonics_atom_type), POINTER :: harmonics
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(qs_kind_type), DIMENSION(:), POINTER :: my_kind_set
TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: dr_h, dr_s, int_hh, int_ss, r_h, r_s
TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r_h_d, r_s_d
TYPE(rho_atom_type), DIMENSION(:), POINTER :: my_rho_atom_set
TYPE(rho_atom_type), POINTER :: rho_atom
TYPE(section_vals_type), POINTER :: input, my_xc_section, xc_fun_section
TYPE(xc_derivative_set_type) :: deriv_set
TYPE(xc_rho_cflags_type) :: needs
TYPE(xc_rho_set_type) :: rho_set_h, rho_set_s
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
NULLIFY (atom_list)
NULLIFY (my_kind_set)
NULLIFY (atomic_kind_set)
NULLIFY (grid_atom)
NULLIFY (harmonics)
NULLIFY (input)
NULLIFY (para_env)
NULLIFY (rho_atom)
NULLIFY (my_rho_atom_set)
NULLIFY (rho_nlcc)
epr_xc = .FALSE.
IF (PRESENT(gradient_atom_set)) THEN
epr_xc = .TRUE.
END IF
IF (PRESENT(adiabatic_rescale_factor)) THEN
my_adiabatic_rescale_factor = adiabatic_rescale_factor
ELSE
my_adiabatic_rescale_factor = 1.0_dp
END IF
CALL get_qs_env(qs_env=qs_env, &
dft_control=dft_control, &
para_env=para_env, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=my_kind_set, &
input=input, &
rho_atom_set=my_rho_atom_set)
IF (PRESENT(kind_set_external)) my_kind_set => kind_set_external
IF (PRESENT(rho_atom_set_external)) my_rho_atom_set => rho_atom_set_external
nlcc = has_nlcc(my_kind_set)
IF (epr_xc) THEN
my_xc_section => section_vals_get_subs_vals(input, &
"PROPERTIES%LINRES%EPR%PRINT%G_TENSOR%XC")
ELSE
my_xc_section => section_vals_get_subs_vals(input, "DFT%XC")
END IF
IF (PRESENT(xc_section_external)) my_xc_section => xc_section_external
xc_fun_section => section_vals_get_subs_vals(my_xc_section, "XC_FUNCTIONAL")
CALL section_vals_val_get(xc_fun_section, "_SECTION_PARAMETERS_", &
i_val=myfun)
IF (myfun == xc_none) THEN
exc1 = 0.0_dp
my_rho_atom_set(:)%exc_h = 0.0_dp
my_rho_atom_set(:)%exc_s = 0.0_dp
ELSE
CALL section_vals_val_get(my_xc_section, "DENSITY_CUTOFF", &
r_val=density_cut)
CALL section_vals_val_get(my_xc_section, "GRADIENT_CUTOFF", &
r_val=gradient_cut)
CALL section_vals_val_get(my_xc_section, "TAU_CUTOFF", &
r_val=tau_cut)
lsd = dft_control%lsd
nspins = dft_control%nspins
needs = xc_functionals_get_needs(xc_fun_section, &
lsd=lsd, &
calc_potential=.TRUE.)
! whatever the xc, if epr_xc, drho_spin is needed
IF (epr_xc) needs%drho_spin = .TRUE.
gradient_f = (needs%drho .OR. needs%drho_spin)
tau_f = (needs%tau .OR. needs%tau_spin)
! Initialize energy contribution from the one center XC terms to zero
exc1 = 0.0_dp
! Nullify some pointers for work-arrays
NULLIFY (rho_h, drho_h, rho_s, drho_s, weight)
NULLIFY (vxc_h, vxc_s, vxg_h, vxg_s)
NULLIFY (tau_h, tau_s)
NULLIFY (vtau_h, vtau_s)
! Here starts the loop over all the atoms
DO ikind = 1, SIZE(atomic_kind_set)
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
harmonics=harmonics, grid_atom=grid_atom)
CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
IF (.NOT. paw_atom) CYCLE
nr = grid_atom%nr
na = grid_atom%ng_sphere
! Prepare the structures needed to calculate and store the xc derivatives
! Array dimension: here anly one dimensional arrays are used,
! i.e. only the first column of deriv_data is read.
! The other to dimensions are set to size equal 1
bounds(1:2, 1:3) = 1
bounds(2, 1) = na
bounds(2, 2) = nr
! create a place where to put the derivatives
CALL xc_dset_create(deriv_set, local_bounds=bounds)
! create the place where to store the argument for the functionals
CALL xc_rho_set_create(rho_set_h, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
CALL xc_rho_set_create(rho_set_s, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
! allocate the required 3d arrays where to store rho and drho
CALL xc_rho_set_atom_update(rho_set_h, needs, nspins, bounds)
CALL xc_rho_set_atom_update(rho_set_s, needs, nspins, bounds)
CALL reallocate(rho_h, 1, na, 1, nr, 1, nspins)
CALL reallocate(rho_s, 1, na, 1, nr, 1, nspins)
weight => grid_atom%weight
CALL reallocate(vxc_h, 1, na, 1, nr, 1, nspins)
CALL reallocate(vxc_s, 1, na, 1, nr, 1, nspins)
!
IF (gradient_f) THEN
CALL reallocate(drho_h, 1, 4, 1, na, 1, nr, 1, nspins)
CALL reallocate(drho_s, 1, 4, 1, na, 1, nr, 1, nspins)
CALL reallocate(vxg_h, 1, 3, 1, na, 1, nr, 1, nspins)
CALL reallocate(vxg_s, 1, 3, 1, na, 1, nr, 1, nspins)
END IF
IF (tau_f) THEN
CALL reallocate(tau_h, 1, na, 1, nr, 1, nspins)
CALL reallocate(tau_s, 1, na, 1, nr, 1, nspins)
CALL reallocate(vtau_h, 1, na, 1, nr, 1, nspins)
CALL reallocate(vtau_s, 1, na, 1, nr, 1, nspins)
END IF
! NLCC: prepare rho and drho of the core charge for this KIND
donlcc = .FALSE.
IF (nlcc) THEN
NULLIFY (rho_nlcc)
rho_nlcc => my_kind_set(ikind)%nlcc_pot
IF (ASSOCIATED(rho_nlcc)) donlcc = .TRUE.
END IF
! Distribute the atoms of this kind
num_pe = para_env%num_pe
bo = get_limit(natom, para_env%num_pe, para_env%mepos)
DO iat = bo(1), bo(2)
iatom = atom_list(iat)
my_rho_atom_set(iatom)%exc_h = 0.0_dp
my_rho_atom_set(iatom)%exc_s = 0.0_dp
rho_atom => my_rho_atom_set(iatom)
rho_h = 0.0_dp
rho_s = 0.0_dp
IF (gradient_f) THEN
NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, &
rho_rad_s=r_s, drho_rad_h=dr_h, &
drho_rad_s=dr_s, rho_rad_h_d=r_h_d, &
rho_rad_s_d=r_s_d)
drho_h = 0.0_dp
drho_s = 0.0_dp
ELSE
NULLIFY (r_h, r_s)
CALL get_rho_atom(rho_atom=rho_atom, rho_rad_h=r_h, rho_rad_s=r_s)
rho_d = 0.0_dp
END IF
IF (tau_f) THEN
!compute tau on the grid all at once
CALL calc_tau_atom(tau_h, tau_s, rho_atom, my_kind_set(ikind), nspins)
ELSE
tau_d = 0.0_dp
END IF
DO ir = 1, nr
CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_f, &
ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, &
r_h_d, r_s_d, drho_h, drho_s)
IF (donlcc) THEN
CALL calc_rho_nlcc(grid_atom, nspins, gradient_f, &
ir, rho_nlcc(:, 1), rho_h, rho_s, rho_nlcc(:, 2), drho_h, drho_s)
END IF
END DO
DO ir = 1, nr
IF (tau_f) THEN
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_h, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_s, na, ir)
ELSE IF (gradient_f) THEN
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_d, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_d, na, ir)
ELSE
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, rho_d, tau_d, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, rho_d, tau_d, na, ir)
END IF
END DO
!-------------------!
! hard atom density !
!-------------------!
CALL xc_dset_zero_all(deriv_set)
CALL vxc_of_r_new(xc_fun_section, rho_set_h, deriv_set, 1, needs, weight, &
lsd, na, nr, exc_h, vxc_h, vxg_h, vtau_h, energy_only=energy_only, &
epr_xc=epr_xc, adiabatic_rescale_factor=my_adiabatic_rescale_factor)
rho_atom%exc_h = rho_atom%exc_h + exc_h
!-------------------!
! soft atom density !
!-------------------!
CALL xc_dset_zero_all(deriv_set)
CALL vxc_of_r_new(xc_fun_section, rho_set_s, deriv_set, 1, needs, weight, &
lsd, na, nr, exc_s, vxc_s, vxg_s, vtau_s, energy_only=energy_only, &
epr_xc=epr_xc, adiabatic_rescale_factor=my_adiabatic_rescale_factor)
rho_atom%exc_s = rho_atom%exc_s + exc_s
IF (epr_xc) THEN
DO ispin = 1, nspins
DO idir = 1, 3
DO ir = 1, nr
DO ia = 1, na
gradient_atom_set(iatom)%nablavks_vec_rad_h(idir, ispin)%r_coef(ir, ia) = &
gradient_atom_set(iatom)%nablavks_vec_rad_h(idir, ispin)%r_coef(ir, ia) &
+ vxg_h(idir, ia, ir, ispin)
gradient_atom_set(iatom)%nablavks_vec_rad_s(idir, ispin)%r_coef(ir, ia) = &
gradient_atom_set(iatom)%nablavks_vec_rad_s(idir, ispin)%r_coef(ir, ia) &
+ vxg_s(idir, ia, ir, ispin)
END DO ! ia
END DO ! ir
END DO ! idir
END DO ! ispin
END IF
! Add contributions to the exc energy
exc1 = exc1 + rho_atom%exc_h - rho_atom%exc_s
! Integration to get the matrix elements relative to the vxc_atom
! here the products with the primitives is done: gaVxcgb
! internal transformation to get the integral in cartesian Gaussians
IF (.NOT. energy_only) THEN
NULLIFY (int_hh, int_ss)
CALL get_rho_atom(rho_atom=rho_atom, ga_Vlocal_gb_h=int_hh, ga_Vlocal_gb_s=int_ss)
IF (gradient_f) THEN
CALL gaVxcgb_GC(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, &
grid_atom, basis_1c, harmonics, nspins)
ELSE
CALL gaVxcgb_noGC(vxc_h, vxc_s, int_hh, int_ss, &
grid_atom, basis_1c, harmonics, nspins)
END IF
IF (tau_f) THEN
CALL dgaVtaudgb(vtau_h, vtau_s, int_hh, int_ss, &
grid_atom, basis_1c, harmonics, nspins)
END IF
END IF ! energy_only
NULLIFY (r_h, r_s, dr_h, dr_s)
END DO ! iat
! Release the xc structure used to store the xc derivatives
CALL xc_dset_release(deriv_set)
CALL xc_rho_set_release(rho_set_h)
CALL xc_rho_set_release(rho_set_s)
END DO ! ikind
CALL para_env%sum(exc1)
IF (ASSOCIATED(rho_h)) DEALLOCATE (rho_h)
IF (ASSOCIATED(rho_s)) DEALLOCATE (rho_s)
IF (ASSOCIATED(vxc_h)) DEALLOCATE (vxc_h)
IF (ASSOCIATED(vxc_s)) DEALLOCATE (vxc_s)
IF (gradient_f) THEN
IF (ASSOCIATED(drho_h)) DEALLOCATE (drho_h)
IF (ASSOCIATED(drho_s)) DEALLOCATE (drho_s)
IF (ASSOCIATED(vxg_h)) DEALLOCATE (vxg_h)
IF (ASSOCIATED(vxg_s)) DEALLOCATE (vxg_s)
END IF
IF (tau_f) THEN
IF (ASSOCIATED(tau_h)) DEALLOCATE (tau_h)
IF (ASSOCIATED(tau_s)) DEALLOCATE (tau_s)
IF (ASSOCIATED(vtau_h)) DEALLOCATE (vtau_h)
IF (ASSOCIATED(vtau_s)) DEALLOCATE (vtau_s)
END IF
END IF !xc_none
CALL timestop(handle)
END SUBROUTINE calculate_vxc_atom
! **************************************************************************************************
!> \brief ...
!> \param rho_atom_set ...
!> \param rho1_atom_set ...
!> \param qs_env ...
!> \param xc_section ...
!> \param para_env ...
!> \param do_tddft Initial implementation of TDDFT. Control parameters are read directly from
!> 'DFT' input section
!> \param do_tddfpt2 New implementation of TDDFT.
!> \param do_triplet ...
!> \param kind_set_external ...
! **************************************************************************************************
SUBROUTINE calculate_xc_2nd_deriv_atom(rho_atom_set, rho1_atom_set, qs_env, xc_section, para_env, &
do_tddft, do_tddfpt2, do_triplet, kind_set_external)
TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho_atom_set, rho1_atom_set
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(section_vals_type), POINTER :: xc_section
TYPE(mp_para_env_type), INTENT(IN) :: para_env
LOGICAL, INTENT(IN), OPTIONAL :: do_tddft, do_tddfpt2, do_triplet
TYPE(qs_kind_type), DIMENSION(:), OPTIONAL, &
POINTER :: kind_set_external
CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_xc_2nd_deriv_atom'
INTEGER :: atom, excitations, handle, iatom, ikind, &
ir, na, natom, nr, nspins, res_etype
INTEGER, DIMENSION(2) :: local_loop_limit
INTEGER, DIMENSION(2, 3) :: bounds
INTEGER, DIMENSION(:), POINTER :: atom_list
LOGICAL :: gradient_functional, lsd, lsd_singlets, &
my_tddft, paw_atom, scale_rho, tau_f
REAL(KIND=dp) :: density_cut, gradient_cut, rtot, tau_cut
REAL(KIND=dp), CONTIGUOUS, DIMENSION(:, :, :), &
POINTER :: vxc_h, vxc_s
REAL(KIND=dp), DIMENSION(1, 1, 1) :: rtau
REAL(KIND=dp), DIMENSION(1, 1, 1, 1) :: rrho
REAL(KIND=dp), DIMENSION(:, :), POINTER :: weight
REAL(KIND=dp), DIMENSION(:, :, :), POINTER :: rho1_h, rho1_s, rho_h, rho_s, tau1_h, &
tau1_s, tau_h, tau_s
REAL(KIND=dp), DIMENSION(:, :, :, :), POINTER :: drho1_h, drho1_s, drho_h, drho_s, vxg_h, &
vxg_s
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(grid_atom_type), POINTER :: grid_atom
TYPE(gto_basis_set_type), POINTER :: basis_1c
TYPE(harmonics_atom_type), POINTER :: harmonics
TYPE(qs_kind_type), DIMENSION(:), POINTER :: my_kind_set, qs_kind_set
TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: dr1_h, dr1_s, dr_h, dr_s, int_hh, &
int_ss, r1_h, r1_s, r_h, r_s
TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r1_h_d, r1_s_d, r_h_d, r_s_d
TYPE(rho_atom_type), POINTER :: rho1_atom, rho_atom
TYPE(section_vals_type), POINTER :: input, xc_fun_section
TYPE(xc_derivative_set_type) :: deriv_set
TYPE(xc_rho_cflags_type) :: needs
TYPE(xc_rho_set_type) :: rho1_set_h, rho1_set_s, rho_set_h, &
rho_set_s
! -------------------------------------------------------------------------
CALL timeset(routineN, handle)
NULLIFY (qs_kind_set)
NULLIFY (rho_h, rho_s, drho_h, drho_s, weight)
NULLIFY (rho1_h, rho1_s, drho1_h, drho1_s)
NULLIFY (vxc_h, vxc_s, vxg_h, vxg_s)
NULLIFY (tau_h, tau_s, tau1_h, tau1_s)
CALL get_qs_env(qs_env=qs_env, &
input=input, &
qs_kind_set=qs_kind_set, &
atomic_kind_set=atomic_kind_set)
IF (PRESENT(kind_set_external)) THEN
my_kind_set => kind_set_external
ELSE
my_kind_set => qs_kind_set
END IF
my_tddft = .FALSE.
IF (PRESENT(do_tddft)) my_tddft = do_tddft
CALL section_vals_val_get(input, "DFT%LSD", l_val=lsd)
CALL section_vals_val_get(xc_section, "DENSITY_CUTOFF", &
r_val=density_cut)
CALL section_vals_val_get(xc_section, "GRADIENT_CUTOFF", &
r_val=gradient_cut)
CALL section_vals_val_get(xc_section, "TAU_CUTOFF", &
r_val=tau_cut)
IF (my_tddft) THEN
CALL section_vals_val_get(input, "DFT%EXCITATIONS", &
i_val=excitations)
CALL section_vals_val_get(input, "DFT%TDDFPT%LSD_SINGLETS", &
l_val=lsd_singlets)
CALL section_vals_val_get(input, "DFT%TDDFPT%RES_ETYPE", &
i_val=res_etype)
END IF
xc_fun_section => section_vals_get_subs_vals(xc_section, &
"XC_FUNCTIONAL")
IF (lsd) THEN
nspins = 2
ELSE
nspins = 1
END IF
scale_rho = .FALSE.
IF (my_tddft) THEN
IF (excitations == tddfpt_excitations) THEN
IF (nspins == 1 .AND. (lsd_singlets .OR. res_etype == tddfpt_triplet)) THEN
lsd = .TRUE.
END IF
END IF
ELSEIF (PRESENT(do_tddfpt2) .AND. PRESENT(do_triplet)) THEN
IF (nspins == 1 .AND. do_triplet) THEN
lsd = .TRUE.
scale_rho = .TRUE.
END IF
ELSEIF (PRESENT(do_triplet)) THEN
IF (nspins == 1 .AND. do_triplet) lsd = .TRUE.
END IF
needs = xc_functionals_get_needs(xc_fun_section, lsd=lsd, &
calc_potential=.TRUE.)
gradient_functional = needs%drho .OR. needs%drho_spin
tau_f = (needs%tau .OR. needs%tau_spin)
IF (tau_f) THEN
CPABORT("Tau functionals not implemented for GAPW 2nd derivatives")
ELSE
rtau = 0.0_dp
END IF
! Here starts the loop over all the atoms
DO ikind = 1, SIZE(atomic_kind_set)
NULLIFY (atom_list, harmonics, grid_atom)
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
CALL get_qs_kind(my_kind_set(ikind), paw_atom=paw_atom, &
harmonics=harmonics, grid_atom=grid_atom)
CALL get_qs_kind(my_kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
IF (.NOT. paw_atom) CYCLE
nr = grid_atom%nr
na = grid_atom%ng_sphere
! Array dimension: here anly one dimensional arrays are used,
! i.e. only the first column of deriv_data is read.
! The other to dimensions are set to size equal 1.
bounds(1:2, 1:3) = 1
bounds(2, 1) = na
bounds(2, 2) = nr
CALL xc_dset_create(deriv_set, local_bounds=bounds)
CALL xc_rho_set_create(rho_set_h, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
CALL xc_rho_set_create(rho_set_s, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
CALL xc_rho_set_create(rho1_set_h, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
CALL xc_rho_set_create(rho1_set_s, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
! allocate the required 3d arrays where to store rho and drho
IF (nspins == 1 .AND. .NOT. lsd) THEN
CALL xc_rho_set_atom_update(rho_set_h, needs, 1, bounds)
CALL xc_rho_set_atom_update(rho1_set_h, needs, 1, bounds)
CALL xc_rho_set_atom_update(rho_set_s, needs, 1, bounds)
CALL xc_rho_set_atom_update(rho1_set_s, needs, 1, bounds)
ELSE
CALL xc_rho_set_atom_update(rho_set_h, needs, 2, bounds)
CALL xc_rho_set_atom_update(rho1_set_h, needs, 2, bounds)
CALL xc_rho_set_atom_update(rho_set_s, needs, 2, bounds)
CALL xc_rho_set_atom_update(rho1_set_s, needs, 2, bounds)
END IF
ALLOCATE (rho_h(1:na, 1:nr, 1:nspins), rho1_h(1:na, 1:nr, 1:nspins), &
rho_s(1:na, 1:nr, 1:nspins), rho1_s(1:na, 1:nr, 1:nspins))
ALLOCATE (vxc_h(1:na, 1:nr, 1:nspins), vxc_s(1:na, 1:nr, 1:nspins))
vxc_h = 0.0_dp
vxc_s = 0.0_dp
weight => grid_atom%weight
IF (gradient_functional) THEN
ALLOCATE (drho_h(1:4, 1:na, 1:nr, 1:nspins), drho1_h(1:4, 1:na, 1:nr, 1:nspins), &
drho_s(1:4, 1:na, 1:nr, 1:nspins), drho1_s(1:4, 1:na, 1:nr, 1:nspins))
ALLOCATE (vxg_h(1:3, 1:na, 1:nr, 1:nspins), vxg_s(1:3, 1:na, 1:nr, 1:nspins))
ELSE
ALLOCATE (drho_h(1, 1, 1, 1), drho1_h(1, 1, 1, 1), &
drho_s(1, 1, 1, 1), drho1_s(1, 1, 1, 1))
ALLOCATE (vxg_h(1, 1, 1, 1), vxg_s(1, 1, 1, 1))
rrho = 0.0_dp
END IF
vxg_h = 0.0_dp
vxg_s = 0.0_dp
! parallelization
local_loop_limit = get_limit(natom, para_env%num_pe, para_env%mepos)
DO iatom = local_loop_limit(1), local_loop_limit(2) !1,natom
atom = atom_list(iatom)
rho_atom_set(atom)%exc_h = 0.0_dp
rho_atom_set(atom)%exc_s = 0.0_dp
rho1_atom_set(atom)%exc_h = 0.0_dp
rho1_atom_set(atom)%exc_s = 0.0_dp
rho_atom => rho_atom_set(atom)
rho1_atom => rho1_atom_set(atom)
NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
NULLIFY (r1_h, r1_s, dr1_h, dr1_s, r1_h_d, r1_s_d)
rho_h = 0.0_dp
rho_s = 0.0_dp
rho1_h = 0.0_dp
rho1_s = 0.0_dp
IF (gradient_functional) THEN
CALL get_rho_atom(rho_atom=rho_atom, &
rho_rad_h=r_h, rho_rad_s=r_s, &
drho_rad_h=dr_h, drho_rad_s=dr_s, &
rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d)
CALL get_rho_atom(rho_atom=rho1_atom, &
rho_rad_h=r1_h, rho_rad_s=r1_s, &
drho_rad_h=dr1_h, drho_rad_s=dr1_s, &
rho_rad_h_d=r1_h_d, rho_rad_s_d=r1_s_d)
drho_h = 0.0_dp; drho_s = 0.0_dp
drho1_h = 0.0_dp; drho1_s = 0.0_dp
ELSE
CALL get_rho_atom(rho_atom=rho_atom, &
rho_rad_h=r_h, rho_rad_s=r_s)
CALL get_rho_atom(rho_atom=rho1_atom, &
rho_rad_h=r1_h, rho_rad_s=r1_s)
END IF
rtot = 0.0_dp
DO ir = 1, nr
CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_functional, &
ir, r_h, r_s, rho_h, rho_s, dr_h, dr_s, r_h_d, r_s_d, &
drho_h, drho_s)
CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_functional, &
ir, r1_h, r1_s, rho1_h, rho1_s, dr1_h, dr1_s, r1_h_d, r1_s_d, &
drho1_h, drho1_s)
END DO
IF (scale_rho) THEN
rho_h = 2.0_dp*rho_h
rho_s = 2.0_dp*rho_s
IF (gradient_functional) THEN
drho_h = 2.0_dp*drho_h
drho_s = 2.0_dp*drho_s
END IF
END IF
DO ir = 1, nr
IF (tau_f) THEN
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, tau_h, na, ir)
CALL fill_rho_set(rho1_set_h, lsd, nspins, needs, rho1_h, drho1_h, tau1_h, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, tau_s, na, ir)
CALL fill_rho_set(rho1_set_s, lsd, nspins, needs, rho1_s, drho1_s, tau1_s, na, ir)
ELSE IF (gradient_functional) THEN
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, drho_h, rtau, na, ir)
CALL fill_rho_set(rho1_set_h, lsd, nspins, needs, rho1_h, drho1_h, rtau, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, drho_s, rtau, na, ir)
CALL fill_rho_set(rho1_set_s, lsd, nspins, needs, rho1_s, drho1_s, rtau, na, ir)
ELSE
CALL fill_rho_set(rho_set_h, lsd, nspins, needs, rho_h, rrho, rtau, na, ir)
CALL fill_rho_set(rho1_set_h, lsd, nspins, needs, rho1_h, rrho, rtau, na, ir)
CALL fill_rho_set(rho_set_s, lsd, nspins, needs, rho_s, rrho, rtau, na, ir)
CALL fill_rho_set(rho1_set_s, lsd, nspins, needs, rho1_s, rrho, rtau, na, ir)
END IF
END DO
CALL xc_2nd_deriv_of_r(xc_section=xc_section, &
rho_set=rho_set_h, rho1_set=rho1_set_h, &
deriv_set=deriv_set, &
w=weight, vxc=vxc_h, vxg=vxg_h, do_triplet=do_triplet)
CALL xc_2nd_deriv_of_r(xc_section=xc_section, &
rho_set=rho_set_s, rho1_set=rho1_set_s, &
deriv_set=deriv_set, &
w=weight, vxc=vxc_s, vxg=vxg_s, do_triplet=do_triplet)
CALL get_rho_atom(rho_atom=rho1_atom, ga_Vlocal_gb_h=int_hh, ga_Vlocal_gb_s=int_ss)
IF (gradient_functional) THEN
CALL gaVxcgb_GC(vxc_h, vxc_s, vxg_h, vxg_s, int_hh, int_ss, &
grid_atom, basis_1c, harmonics, nspins)
ELSE
CALL gaVxcgb_noGC(vxc_h, vxc_s, int_hh, int_ss, &
grid_atom, basis_1c, harmonics, nspins)
END IF
NULLIFY (r_h, r_s, dr_h, dr_s)
END DO
! some cleanup
NULLIFY (weight)
DEALLOCATE (rho_h, rho_s, rho1_h, rho1_s, vxc_h, vxc_s)
DEALLOCATE (drho_h, drho_s, vxg_h, vxg_s)
DEALLOCATE (drho1_h, drho1_s)
CALL xc_dset_release(deriv_set)
CALL xc_rho_set_release(rho_set_h)
CALL xc_rho_set_release(rho1_set_h)
CALL xc_rho_set_release(rho_set_s)
CALL xc_rho_set_release(rho1_set_s)
END DO
CALL timestop(handle)
END SUBROUTINE calculate_xc_2nd_deriv_atom
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param rho0_atom_set ...
!> \param rho1_atom_set ...
!> \param rho2_atom_set ...
!> \param kind_set ...
!> \param xc_section ...
!> \param is_triplet ...
!> \param accuracy ...
! **************************************************************************************************
SUBROUTINE calculate_gfxc_atom(qs_env, rho0_atom_set, rho1_atom_set, rho2_atom_set, &
kind_set, xc_section, is_triplet, accuracy)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(rho_atom_type), DIMENSION(:), POINTER :: rho0_atom_set, rho1_atom_set, &
rho2_atom_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: kind_set
TYPE(section_vals_type), OPTIONAL, POINTER :: xc_section
LOGICAL, INTENT(IN) :: is_triplet
INTEGER, INTENT(IN) :: accuracy
CHARACTER(LEN=*), PARAMETER :: routineN = 'calculate_gfxc_atom'
REAL(KIND=dp), PARAMETER :: epsrho = 5.e-4_dp
INTEGER :: bo(2), handle, iat, iatom, ikind, ir, &
istep, mspins, myfun, na, natom, nf, &
nr, ns, nspins, nstep, num_pe
INTEGER, DIMENSION(2, 3) :: bounds
INTEGER, DIMENSION(:), POINTER :: atom_list
LOGICAL :: donlcc, gradient_f, lsd, nlcc, paw_atom, &
tau_f
REAL(dp) :: beta, density_cut, exc_h, exc_s, &
gradient_cut, oeps1, oeps2, tau_cut
REAL(dp), DIMENSION(1, 1, 1) :: tau_d
REAL(dp), DIMENSION(1, 1, 1, 1) :: rho_d
REAL(dp), DIMENSION(:, :), POINTER :: rho_nlcc, weight
REAL(dp), DIMENSION(:, :, :), POINTER :: rho0_h, rho0_s, rho1_h, rho1_s, rho_h, &
rho_s, tau0_h, tau0_s, tau1_h, tau1_s, &
tau_h, tau_s, vtau_h, vtau_s, vxc_h, &
vxc_s
REAL(dp), DIMENSION(:, :, :, :), POINTER :: drho0_h, drho0_s, drho1_h, drho1_s, &
drho_h, drho_s, vxg_h, vxg_s
REAL(KIND=dp), DIMENSION(-4:4) :: ak, bl
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dft_control_type), POINTER :: dft_control
TYPE(grid_atom_type), POINTER :: grid_atom
TYPE(gto_basis_set_type), POINTER :: basis_1c
TYPE(harmonics_atom_type), POINTER :: harmonics
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(rho_atom_coeff), DIMENSION(:), POINTER :: dr_h, dr_s, fint_hh, fint_ss, int_hh, &
int_ss, r_h, r_s
TYPE(rho_atom_coeff), DIMENSION(:, :), POINTER :: r_h_d, r_s_d
TYPE(rho_atom_type), POINTER :: rho0_atom, rho1_atom, rho2_atom
TYPE(section_vals_type), POINTER :: xc_fun_section
TYPE(xc_derivative_set_type) :: deriv_set
TYPE(xc_rho_cflags_type) :: needs
TYPE(xc_rho_set_type) :: rho_set_h, rho_set_s
CALL timeset(routineN, handle)
ak = 0.0_dp
bl = 0.0_dp
SELECT CASE (accuracy)
CASE (:4)
nstep = 2
ak(-2:2) = (/1.0_dp, -8.0_dp, 0.0_dp, 8.0_dp, -1.0_dp/)/12.0_dp
bl(-2:2) = (/-1.0_dp, 16.0_dp, -30.0_dp, 16.0_dp, -1.0_dp/)/12.0_dp
CASE (5:7)
nstep = 3
ak(-3:3) = (/-1.0_dp, 9.0_dp, -45.0_dp, 0.0_dp, 45.0_dp, -9.0_dp, 1.0_dp/)/60.0_dp
bl(-3:3) = (/2.0_dp, -27.0_dp, 270.0_dp, -490.0_dp, 270.0_dp, -27.0_dp, 2.0_dp/)/180.0_dp
CASE (8:)
nstep = 4
ak(-4:4) = (/1.0_dp, -32.0_dp/3.0_dp, 56.0_dp, -224.0_dp, 0.0_dp, &
224.0_dp, -56.0_dp, 32.0_dp/3.0_dp, -1.0_dp/)/280.0_dp
bl(-4:4) = (/-1.0_dp, 128.0_dp/9.0_dp, -112.0_dp, 896.0_dp, -14350.0_dp/9.0_dp, &
896.0_dp, -112.0_dp, 128.0_dp/9.0_dp, -1.0_dp/)/560.0_dp
END SELECT
oeps1 = 1.0_dp/epsrho
oeps2 = 1.0_dp/(epsrho**2)
CALL get_qs_env(qs_env=qs_env, &
dft_control=dft_control, &
para_env=para_env, &
atomic_kind_set=atomic_kind_set)
xc_fun_section => section_vals_get_subs_vals(xc_section, "XC_FUNCTIONAL")
CALL section_vals_val_get(xc_fun_section, "_SECTION_PARAMETERS_", i_val=myfun)
IF (myfun == xc_none) THEN
! no action needed?
ELSE
CALL section_vals_val_get(xc_section, "DENSITY_CUTOFF", r_val=density_cut)
CALL section_vals_val_get(xc_section, "GRADIENT_CUTOFF", r_val=gradient_cut)
CALL section_vals_val_get(xc_section, "TAU_CUTOFF", r_val=tau_cut)
nlcc = has_nlcc(kind_set)
lsd = dft_control%lsd
nspins = dft_control%nspins
mspins = nspins
IF (is_triplet) THEN
CPASSERT(nspins == 1)
lsd = .TRUE.
mspins = 2
END IF
needs = xc_functionals_get_needs(xc_fun_section, lsd=lsd, calc_potential=.TRUE.)
gradient_f = (needs%drho .OR. needs%drho_spin)
tau_f = (needs%tau .OR. needs%tau_spin)
! Here starts the loop over all the atoms
DO ikind = 1, SIZE(atomic_kind_set)
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=atom_list, natom=natom)
CALL get_qs_kind(kind_set(ikind), paw_atom=paw_atom, &
harmonics=harmonics, grid_atom=grid_atom)
CALL get_qs_kind(kind_set(ikind), basis_set=basis_1c, basis_type="GAPW_1C")
IF (.NOT. paw_atom) CYCLE
nr = grid_atom%nr
na = grid_atom%ng_sphere
! Prepare the structures needed to calculate and store the xc derivatives
! Array dimension: here anly one dimensional arrays are used,
! i.e. only the first column of deriv_data is read.
! The other to dimensions are set to size equal 1
bounds(1:2, 1:3) = 1
bounds(2, 1) = na
bounds(2, 2) = nr
! create a place where to put the derivatives
CALL xc_dset_create(deriv_set, local_bounds=bounds)
! create the place where to store the argument for the functionals
CALL xc_rho_set_create(rho_set_h, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
CALL xc_rho_set_create(rho_set_s, bounds, rho_cutoff=density_cut, &
drho_cutoff=gradient_cut, tau_cutoff=tau_cut)
! allocate the required 3d arrays where to store rho and drho
CALL xc_rho_set_atom_update(rho_set_h, needs, mspins, bounds)
CALL xc_rho_set_atom_update(rho_set_s, needs, mspins, bounds)
weight => grid_atom%weight
ALLOCATE (rho_h(na, nr, mspins), rho_s(na, nr, mspins), &
rho0_h(na, nr, nspins), rho0_s(na, nr, nspins), &
rho1_h(na, nr, nspins), rho1_s(na, nr, nspins))
ALLOCATE (vxc_h(na, nr, mspins), vxc_s(na, nr, mspins))
IF (gradient_f) THEN
ALLOCATE (drho_h(4, na, nr, mspins), drho_s(4, na, nr, mspins), &
drho0_h(4, na, nr, nspins), drho0_s(4, na, nr, nspins), &
drho1_h(4, na, nr, nspins), drho1_s(4, na, nr, nspins))
ALLOCATE (vxg_h(3, na, nr, mspins), vxg_s(3, na, nr, mspins))
END IF
IF (tau_f) THEN
ALLOCATE (tau_h(na, nr, mspins), tau_s(na, nr, mspins), &
tau0_h(na, nr, nspins), tau0_s(na, nr, nspins), &
tau1_h(na, nr, nspins), tau1_s(na, nr, nspins))
ALLOCATE (vtau_h(na, nr, mspins), vtau_s(na, nr, mspins))
END IF
!
! NLCC: prepare rho and drho of the core charge for this KIND
donlcc = .FALSE.
IF (nlcc) THEN
NULLIFY (rho_nlcc)
rho_nlcc => kind_set(ikind)%nlcc_pot
IF (ASSOCIATED(rho_nlcc)) donlcc = .TRUE.
END IF
! Distribute the atoms of this kind
num_pe = para_env%num_pe
bo = get_limit(natom, num_pe, para_env%mepos)
DO iat = bo(1), bo(2)
iatom = atom_list(iat)
!
NULLIFY (int_hh, int_ss)
rho0_atom => rho0_atom_set(iatom)
CALL get_rho_atom(rho_atom=rho0_atom, ga_Vlocal_gb_h=int_hh, ga_Vlocal_gb_s=int_ss)
ALLOCATE (fint_ss(nspins), fint_hh(nspins))
DO ns = 1, nspins
nf = SIZE(int_ss(ns)%r_coef, 1)
ALLOCATE (fint_ss(ns)%r_coef(nf, nf))
nf = SIZE(int_hh(ns)%r_coef, 1)
ALLOCATE (fint_hh(ns)%r_coef(nf, nf))
END DO
! RHO0
rho0_h = 0.0_dp
rho0_s = 0.0_dp
rho0_atom => rho0_atom_set(iatom)
IF (gradient_f) THEN
NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
CALL get_rho_atom(rho_atom=rho0_atom, rho_rad_h=r_h, rho_rad_s=r_s, drho_rad_h=dr_h, &
drho_rad_s=dr_s, rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d)
drho0_h = 0.0_dp
drho0_s = 0.0_dp
ELSE
NULLIFY (r_h, r_s)
CALL get_rho_atom(rho_atom=rho0_atom, rho_rad_h=r_h, rho_rad_s=r_s)
rho_d = 0.0_dp
END IF
DO ir = 1, nr
CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_f, &
ir, r_h, r_s, rho0_h, rho0_s, dr_h, dr_s, &
r_h_d, r_s_d, drho0_h, drho0_s)
IF (donlcc) THEN
CALL calc_rho_nlcc(grid_atom, nspins, gradient_f, &
ir, rho_nlcc(:, 1), rho0_h, rho0_s, rho_nlcc(:, 2), drho0_h, drho0_s)
END IF
END DO
IF (tau_f) THEN
!compute tau on the grid all at once
CALL calc_tau_atom(tau0_h, tau0_s, rho0_atom, kind_set(ikind), nspins)
ELSE
tau_d = 0.0_dp
END IF
! RHO1
rho1_h = 0.0_dp
rho1_s = 0.0_dp
rho1_atom => rho1_atom_set(iatom)
IF (gradient_f) THEN
NULLIFY (r_h, r_s, dr_h, dr_s, r_h_d, r_s_d)
CALL get_rho_atom(rho_atom=rho1_atom, rho_rad_h=r_h, rho_rad_s=r_s, drho_rad_h=dr_h, &
drho_rad_s=dr_s, rho_rad_h_d=r_h_d, rho_rad_s_d=r_s_d)
drho1_h = 0.0_dp
drho1_s = 0.0_dp
ELSE
NULLIFY (r_h, r_s)
CALL get_rho_atom(rho_atom=rho1_atom, rho_rad_h=r_h, rho_rad_s=r_s)
END IF
DO ir = 1, nr
CALL calc_rho_angular(grid_atom, harmonics, nspins, gradient_f, &
ir, r_h, r_s, rho1_h, rho1_s, dr_h, dr_s, &
r_h_d, r_s_d, drho1_h, drho1_s)
END DO
IF (tau_f) THEN
!compute tau on the grid all at once
CALL calc_tau_atom(tau1_h, tau1_s, rho1_atom, kind_set(ikind), nspins)
END IF
! RHO2
rho2_atom => rho2_atom_set(iatom)
DO istep = -nstep, nstep
beta = REAL(istep, KIND=dp)*epsrho
IF (is_triplet) THEN
rho_h(:, :, 1) = rho0_h(:, :, 1) + beta*rho1_h(:, :, 1)
rho_h(:, :, 2) = rho0_h(:, :, 1)
rho_h = 0.5_dp*rho_h
rho_s(:, :, 1) = rho0_s(:, :, 1) + beta*rho1_s(:, :, 1)
rho_s(:, :, 2) = rho0_s(:, :, 1)
rho_s = 0.5_dp*rho_s
IF (gradient_f) THEN
drho_h(:, :, :, 1) = drho0_h(:, :, :, 1) + beta*drho1_h(:, :, :, 1)
drho_h(:, :, :, 2) = drho0_h(:, :, :, 1)
drho_h = 0.5_dp*drho_h