-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeq_method.F
1809 lines (1651 loc) · 77.5 KB
/
eeq_method.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 Calculation of charge equilibration method
!> \author JGH
! **************************************************************************************************
MODULE eeq_method
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind,&
get_atomic_kind_set
USE atprop_types, ONLY: atprop_type
USE cell_types, ONLY: cell_type,&
get_cell,&
pbc,&
plane_distance
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_control_types, ONLY: dft_control_type
USE cp_fm_basic_linalg, ONLY: cp_fm_invert,&
cp_fm_matvec,&
cp_fm_solve
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_set_all,&
cp_fm_type
USE cp_log_handling, ONLY: cp_logger_get_default_unit_nr
USE distribution_1d_types, ONLY: distribution_1d_type
USE distribution_2d_types, ONLY: distribution_2d_type
USE eeq_data, ONLY: get_eeq_data
USE eeq_input, ONLY: eeq_solver_type
USE ewald_environment_types, ONLY: ewald_env_create,&
ewald_env_get,&
ewald_env_release,&
ewald_env_set,&
ewald_environment_type,&
read_ewald_section_tb
USE ewald_pw_types, ONLY: ewald_pw_create,&
ewald_pw_release,&
ewald_pw_type
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type
USE kinds, ONLY: dp
USE machine, ONLY: m_walltime
USE mathconstants, ONLY: oorootpi,&
twopi
USE mathlib, ONLY: invmat
USE message_passing, ONLY: mp_para_env_type
USE molecule_types, ONLY: molecule_type
USE particle_types, ONLY: particle_type
USE physcon, ONLY: bohr
USE pw_poisson_types, ONLY: do_ewald_spme
USE qs_dispersion_cnum, ONLY: cnumber_init,&
cnumber_release,&
dcnum_type
USE qs_dispersion_types, ONLY: qs_dispersion_release,&
qs_dispersion_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_force_types, ONLY: qs_force_type
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type,&
release_neighbor_list_sets
USE qs_neighbor_lists, ONLY: atom2d_build,&
atom2d_cleanup,&
build_neighbor_lists,&
local_atoms_type,&
pair_radius_setup
USE spme, ONLY: spme_forces,&
spme_potential,&
spme_virial
USE virial_methods, ONLY: virial_pair_force
USE virial_types, ONLY: virial_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'eeq_method'
INTEGER, PARAMETER :: maxElem = 86
! covalent radii (taken from Pyykko and Atsumi, Chem. Eur. J. 15, 2009, 188-197)
! values for metals decreased by 10 %
REAL(KIND=dp), PARAMETER :: rcov(1:maxElem) = [&
& 0.32_dp, 0.46_dp, 1.20_dp, 0.94_dp, 0.77_dp, 0.75_dp, 0.71_dp, 0.63_dp, &
& 0.64_dp, 0.67_dp, 1.40_dp, 1.25_dp, 1.13_dp, 1.04_dp, 1.10_dp, 1.02_dp, &
& 0.99_dp, 0.96_dp, 1.76_dp, 1.54_dp, 1.33_dp, 1.22_dp, 1.21_dp, 1.10_dp, &
& 1.07_dp, 1.04_dp, 1.00_dp, 0.99_dp, 1.01_dp, 1.09_dp, 1.12_dp, 1.09_dp, &
& 1.15_dp, 1.10_dp, 1.14_dp, 1.17_dp, 1.89_dp, 1.67_dp, 1.47_dp, 1.39_dp, &
& 1.32_dp, 1.24_dp, 1.15_dp, 1.13_dp, 1.13_dp, 1.08_dp, 1.15_dp, 1.23_dp, &
& 1.28_dp, 1.26_dp, 1.26_dp, 1.23_dp, 1.32_dp, 1.31_dp, 2.09_dp, 1.76_dp, &
& 1.62_dp, 1.47_dp, 1.58_dp, 1.57_dp, 1.56_dp, 1.55_dp, 1.51_dp, 1.52_dp, &
& 1.51_dp, 1.50_dp, 1.49_dp, 1.49_dp, 1.48_dp, 1.53_dp, 1.46_dp, 1.37_dp, &
& 1.31_dp, 1.23_dp, 1.18_dp, 1.16_dp, 1.11_dp, 1.12_dp, 1.13_dp, 1.32_dp, &
& 1.30_dp, 1.30_dp, 1.36_dp, 1.31_dp, 1.38_dp, 1.42_dp]
PUBLIC :: eeq_solver, eeq_print, eeq_charges, eeq_forces, &
eeq_efield_energy, eeq_efield_pot, eeq_efield_force_loc, eeq_efield_force_periodic
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param iounit ...
!> \param print_level ...
! **************************************************************************************************
SUBROUTINE eeq_print(qs_env, iounit, print_level)
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, INTENT(IN) :: iounit, print_level
CHARACTER(LEN=2) :: element_symbol
INTEGER :: enshift_type, iatom, ikind, natom
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: charges
TYPE(cell_type), POINTER :: cell
TYPE(eeq_solver_type) :: eeq_sparam
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
MARK_USED(print_level)
CALL get_qs_env(qs_env, natom=natom, particle_set=particle_set)
ALLOCATE (charges(natom))
CALL get_qs_env(qs_env, cell=cell)
! enforce en shift method 1 (original/molecular)
! method 2 from paper on PBC seems not to work
enshift_type = 1
!IF (ALL(cell%perd == 0)) enshift_type = 1
CALL eeq_charges(qs_env, charges, eeq_sparam, 2, enshift_type)
IF (iounit > 0) THEN
IF (enshift_type == 1) THEN
WRITE (UNIT=iounit, FMT="(/,T2,A)") "EEQ Charges (Parametrization 2019 (Molecules))"
ELSE IF (enshift_type == 2) THEN
WRITE (UNIT=iounit, FMT="(/,T2,A)") "EEQ Charges (Parametrization 2019 (Crystals))"
ELSE
CPABORT("Unknown enshift_type")
END IF
WRITE (UNIT=iounit, FMT="(/,T2,A)") &
"# Atom Element Kind Atomic Charge"
DO iatom = 1, natom
CALL get_atomic_kind(atomic_kind=particle_set(iatom)%atomic_kind, &
element_symbol=element_symbol, &
kind_number=ikind)
WRITE (UNIT=iounit, FMT="(T4,I8,T18,A2,I10,T43,F12.4)") &
iatom, element_symbol, ikind, charges(iatom)
END DO
END IF
DEALLOCATE (charges)
END SUBROUTINE eeq_print
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param charges ...
!> \param eeq_sparam ...
!> \param eeq_model ...
!> \param enshift_type ...
! **************************************************************************************************
SUBROUTINE eeq_charges(qs_env, charges, eeq_sparam, eeq_model, enshift_type)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: charges
TYPE(eeq_solver_type), INTENT(IN) :: eeq_sparam
INTEGER, INTENT(IN) :: eeq_model, enshift_type
CHARACTER(len=*), PARAMETER :: routineN = 'eeq_charges'
INTEGER :: handle, iatom, ikind, iunit, jkind, &
natom, nkind, za, zb
INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
INTEGER, DIMENSION(3) :: periodic
LOGICAL :: do_ewald
REAL(KIND=dp) :: ala, alb, eeq_energy, esg, kappa, &
lambda, scn, sgamma, totalcharge, xi
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: chia, cnumbers, efr, gam
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: gab
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell, cell_ref
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dcnum_type), ALLOCATABLE, DIMENSION(:) :: dcnum
TYPE(dft_control_type), POINTER :: dft_control
TYPE(ewald_environment_type), POINTER :: ewald_env
TYPE(ewald_pw_type), POINTER :: ewald_pw
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), POINTER :: ewald_section, poisson_section, &
print_section
CALL timeset(routineN, handle)
iunit = cp_logger_get_default_unit_nr()
CALL get_qs_env(qs_env, &
qs_kind_set=qs_kind_set, &
atomic_kind_set=atomic_kind_set, &
particle_set=particle_set, &
para_env=para_env, &
blacs_env=blacs_env, &
cell=cell, &
dft_control=dft_control)
CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
totalcharge = dft_control%charge
CALL get_cnumbers(qs_env, cnumbers, dcnum, .FALSE.)
! gamma[a,b]
ALLOCATE (gab(nkind, nkind), gam(nkind))
gab = 0.0_dp
gam = 0.0_dp
DO ikind = 1, nkind
CALL get_qs_kind(qs_kind_set(ikind), zatom=za)
CALL get_eeq_data(za, eeq_model, eta=gam(ikind), rad=ala)
DO jkind = 1, nkind
CALL get_qs_kind(qs_kind_set(jkind), zatom=zb)
CALL get_eeq_data(zb, eeq_model, rad=alb)
!
gab(ikind, jkind) = SQRT(1._dp/(ala*ala + alb*alb))
!
END DO
END DO
! Chi[a,a]
sgamma = 8.0_dp ! see D4 for periodic systems paper
esg = 1.0_dp + EXP(sgamma)
ALLOCATE (chia(natom))
CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
DO iatom = 1, natom
ikind = kind_of(iatom)
CALL get_qs_kind(qs_kind_set(ikind), zatom=za)
CALL get_eeq_data(za, eeq_model, chi=xi, kcn=kappa)
!
IF (enshift_type == 1) THEN
scn = cnumbers(iatom)/SQRT(cnumbers(iatom) + 1.0e-14_dp)
ELSE IF (enshift_type == 2) THEN
scn = LOG(esg/(esg - cnumbers(iatom)))
ELSE
CPABORT("Unknown enshift_type")
END IF
chia(iatom) = xi - kappa*scn
!
END DO
! efield
IF (dft_control%apply_period_efield .OR. dft_control%apply_efield .OR. &
dft_control%apply_efield_field) THEN
ALLOCATE (efr(natom))
efr(1:natom) = 0.0_dp
CALL eeq_efield_pot(qs_env, efr)
chia(1:natom) = chia(1:natom) + efr(1:natom)
DEALLOCATE (efr)
END IF
CALL cnumber_release(cnumbers, dcnum, .FALSE.)
CALL get_cell(cell, periodic=periodic)
do_ewald = .NOT. ALL(periodic == 0)
IF (do_ewald) THEN
ALLOCATE (ewald_env)
CALL ewald_env_create(ewald_env, para_env)
poisson_section => section_vals_get_subs_vals(qs_env%input, "DFT%POISSON")
CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
CALL get_qs_env(qs_env, cell_ref=cell_ref)
CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
silent=.TRUE., pset="EEQ")
ALLOCATE (ewald_pw)
CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
!
CALL eeq_solver(charges, lambda, eeq_energy, &
particle_set, kind_of, cell, chia, gam, gab, &
para_env, blacs_env, dft_control, eeq_sparam, &
totalcharge=totalcharge, ewald=do_ewald, &
ewald_env=ewald_env, ewald_pw=ewald_pw, iounit=iunit)
!
CALL ewald_env_release(ewald_env)
CALL ewald_pw_release(ewald_pw)
DEALLOCATE (ewald_env, ewald_pw)
ELSE
CALL eeq_solver(charges, lambda, eeq_energy, &
particle_set, kind_of, cell, chia, gam, gab, &
para_env, blacs_env, dft_control, eeq_sparam, &
totalcharge=totalcharge, iounit=iunit)
END IF
DEALLOCATE (gab, gam, chia)
CALL timestop(handle)
END SUBROUTINE eeq_charges
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param charges ...
!> \param dcharges ...
!> \param gradient ...
!> \param stress ...
!> \param eeq_sparam ...
!> \param eeq_model ...
!> \param enshift_type ...
!> \param response_only ...
! **************************************************************************************************
SUBROUTINE eeq_forces(qs_env, charges, dcharges, gradient, stress, &
eeq_sparam, eeq_model, enshift_type, response_only)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: charges, dcharges
REAL(KIND=dp), DIMENSION(:, :), INTENT(INOUT) :: gradient
REAL(KIND=dp), DIMENSION(3, 3), INTENT(INOUT) :: stress
TYPE(eeq_solver_type), INTENT(IN) :: eeq_sparam
INTEGER, INTENT(IN) :: eeq_model, enshift_type
LOGICAL, INTENT(IN) :: response_only
CHARACTER(len=*), PARAMETER :: routineN = 'eeq_forces'
INTEGER :: handle, i, ia, iatom, ikind, iunit, &
jatom, jkind, katom, natom, nkind, za, &
zb
INTEGER, ALLOCATABLE, DIMENSION(:) :: kind_of
INTEGER, DIMENSION(3) :: periodic
LOGICAL :: do_ewald, use_virial
LOGICAL, ALLOCATABLE, DIMENSION(:) :: default_present
REAL(KIND=dp) :: ala, alb, alpha, cn, ctot, dr, dr2, drk, &
elag, esg, gam2, gama, grc, kappa, &
qlam, qq, qq1, qq2, rcut, scn, sgamma, &
subcells, totalcharge, xi
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: c_radius, cnumbers, gam, qlag
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: epforce, gab, pair_radius
REAL(KIND=dp), DIMENSION(3) :: fdik, ri, rij, rik, rj
REAL(KIND=dp), DIMENSION(3, 3) :: pvir
REAL(KIND=dp), DIMENSION(:), POINTER :: chrgx, dchia
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(atprop_type), POINTER :: atprop
TYPE(cell_type), POINTER :: cell, cell_ref
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dcnum_type), ALLOCATABLE, DIMENSION(:) :: dcnum
TYPE(dft_control_type), POINTER :: dft_control
TYPE(distribution_1d_type), POINTER :: distribution_1d, local_particles
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(ewald_environment_type), POINTER :: ewald_env
TYPE(ewald_pw_type), POINTER :: ewald_pw
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_ew
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_force_type), DIMENSION(:), POINTER :: force
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(section_vals_type), POINTER :: ewald_section, poisson_section, &
print_section
TYPE(virial_type), POINTER :: virial
CALL timeset(routineN, handle)
iunit = cp_logger_get_default_unit_nr()
CALL get_qs_env(qs_env, &
qs_kind_set=qs_kind_set, &
atomic_kind_set=atomic_kind_set, &
particle_set=particle_set, &
para_env=para_env, &
blacs_env=blacs_env, &
cell=cell, &
force=force, &
virial=virial, &
atprop=atprop, &
dft_control=dft_control)
CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
use_virial = virial%pv_availability .AND. (.NOT. virial%pv_numer)
CALL get_atomic_kind_set(atomic_kind_set=atomic_kind_set, kind_of=kind_of)
totalcharge = dft_control%charge
CALL get_cnumbers(qs_env, cnumbers, dcnum, .TRUE.)
! gamma[a,b]
ALLOCATE (gab(nkind, nkind), gam(nkind))
gab = 0.0_dp
gam = 0.0_dp
DO ikind = 1, nkind
CALL get_qs_kind(qs_kind_set(ikind), zatom=za)
CALL get_eeq_data(za, eeq_model, eta=gam(ikind), rad=ala)
DO jkind = 1, nkind
CALL get_qs_kind(qs_kind_set(jkind), zatom=zb)
CALL get_eeq_data(zb, eeq_model, rad=alb)
!
gab(ikind, jkind) = SQRT(1._dp/(ala*ala + alb*alb))
!
END DO
END DO
ALLOCATE (qlag(natom))
CALL get_cell(cell, periodic=periodic)
do_ewald = .NOT. ALL(periodic == 0)
IF (do_ewald) THEN
ALLOCATE (ewald_env)
CALL ewald_env_create(ewald_env, para_env)
poisson_section => section_vals_get_subs_vals(qs_env%input, "DFT%POISSON")
CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
CALL get_qs_env(qs_env, cell_ref=cell_ref)
CALL read_ewald_section_tb(ewald_env, ewald_section, cell_ref%hmat, &
silent=.TRUE., pset="EEQ")
ALLOCATE (ewald_pw)
CALL ewald_pw_create(ewald_pw, ewald_env, cell, cell_ref, print_section=print_section)
!
CALL eeq_solver(qlag, qlam, elag, &
particle_set, kind_of, cell, -dcharges, gam, gab, &
para_env, blacs_env, dft_control, eeq_sparam, &
ewald=do_ewald, ewald_env=ewald_env, ewald_pw=ewald_pw, iounit=iunit)
ELSE
CALL eeq_solver(qlag, qlam, elag, &
particle_set, kind_of, cell, -dcharges, gam, gab, &
para_env, blacs_env, dft_control, eeq_sparam, iounit=iunit)
END IF
sgamma = 8.0_dp ! see D4 for periodic systems paper
esg = 1.0_dp + EXP(sgamma)
ALLOCATE (chrgx(natom), dchia(natom))
DO iatom = 1, natom
ikind = kind_of(iatom)
CALL get_qs_kind(qs_kind_set(ikind), zatom=za)
CALL get_eeq_data(za, eeq_model, chi=xi, kcn=kappa)
!
IF (response_only) THEN
ctot = -0.5_dp*qlag(iatom)
ELSE
ctot = 0.5_dp*(charges(iatom) - qlag(iatom))
END IF
IF (enshift_type == 1) THEN
scn = SQRT(cnumbers(iatom)) + 1.0e-14_dp
dchia(iatom) = -ctot*kappa/scn
ELSE IF (enshift_type == 2) THEN
cn = cnumbers(iatom)
scn = 1.0_dp/(esg - cn)
dchia(iatom) = -ctot*kappa*scn
ELSE
CPABORT("Unknown enshift_type")
END IF
END DO
! Efield
IF (dft_control%apply_period_efield) THEN
CALL eeq_efield_force_periodic(qs_env, charges, qlag)
ELSE IF (dft_control%apply_efield) THEN
CALL eeq_efield_force_loc(qs_env, charges, qlag)
ELSE IF (dft_control%apply_efield_field) THEN
CPABORT("apply field")
END IF
! Forces from q*X
CALL get_qs_env(qs_env=qs_env, local_particles=local_particles)
DO ikind = 1, nkind
DO ia = 1, local_particles%n_el(ikind)
iatom = local_particles%list(ikind)%array(ia)
DO i = 1, dcnum(iatom)%neighbors
katom = dcnum(iatom)%nlist(i)
rik = dcnum(iatom)%rik(:, i)
drk = SQRT(SUM(rik(:)**2))
IF (drk > 1.e-3_dp) THEN
fdik(:) = dchia(iatom)*dcnum(iatom)%dvals(i)*rik(:)/drk
gradient(:, iatom) = gradient(:, iatom) - fdik(:)
gradient(:, katom) = gradient(:, katom) + fdik(:)
IF (use_virial) THEN
CALL virial_pair_force(stress, 1._dp, fdik, rik)
END IF
END IF
END DO
END DO
END DO
! Forces from (0.5*q+l)*dA/dR*q
IF (do_ewald) THEN
! Build the neighbor lists for the CN
CALL get_qs_env(qs_env, &
distribution_2d=distribution_2d, &
local_particles=distribution_1d, &
molecule_set=molecule_set)
subcells = 2.0_dp
CALL ewald_env_get(ewald_env, alpha=alpha, rcut=rcut)
rcut = 2.0_dp*rcut
NULLIFY (sab_ew)
ALLOCATE (c_radius(nkind), default_present(nkind), pair_radius(nkind, nkind))
c_radius(:) = rcut
default_present = .TRUE.
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, .FALSE., particle_set=particle_set)
CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
CALL build_neighbor_lists(sab_ew, particle_set, atom2d, cell, pair_radius, &
subcells=subcells, operator_type="PP", nlname="sab_ew")
DEALLOCATE (c_radius, pair_radius, default_present)
CALL atom2d_cleanup(atom2d)
!
CALL neighbor_list_iterator_create(nl_iterator, sab_ew)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, &
iatom=iatom, jatom=jatom, r=rij)
!
dr2 = SUM(rij**2)
dr = SQRT(dr2)
IF (dr > rcut .OR. dr < 1.E-6_dp) CYCLE
IF (response_only) THEN
qq = -qlag(iatom)*charges(jatom)
ELSE
qq = (0.5_dp*charges(iatom) - qlag(iatom))*charges(jatom)
END IF
gama = gab(ikind, jkind)
gam2 = gama*gama
grc = 2._dp*gama*EXP(-gam2*dr2)*oorootpi/dr - erf(gama*dr)/dr2 &
- 2._dp*alpha*EXP(-alpha**2*dr2)*oorootpi/dr + erf(alpha*dr)/dr2
IF (response_only) THEN
qq1 = -qlag(iatom)*charges(jatom)
qq2 = -qlag(jatom)*charges(iatom)
ELSE
qq1 = (0.5_dp*charges(iatom) - qlag(iatom))*charges(jatom)
qq2 = (0.5_dp*charges(jatom) - qlag(jatom))*charges(iatom)
END IF
fdik(:) = -qq1*grc*rij(:)/dr
gradient(:, iatom) = gradient(:, iatom) + fdik(:)
gradient(:, jatom) = gradient(:, jatom) - fdik(:)
IF (use_virial) THEN
CALL virial_pair_force(stress, -1._dp, fdik, rij)
END IF
fdik(:) = qq2*grc*rij(:)/dr
gradient(:, iatom) = gradient(:, iatom) - fdik(:)
gradient(:, jatom) = gradient(:, jatom) + fdik(:)
IF (use_virial) THEN
CALL virial_pair_force(stress, 1._dp, fdik, rij)
END IF
END DO
CALL neighbor_list_iterator_release(nl_iterator)
!
CALL release_neighbor_list_sets(sab_ew)
ELSE
DO ikind = 1, nkind
DO ia = 1, local_particles%n_el(ikind)
iatom = local_particles%list(ikind)%array(ia)
ri(1:3) = particle_set(iatom)%r(1:3)
DO jatom = 1, natom
IF (iatom == jatom) CYCLE
jkind = kind_of(jatom)
IF (response_only) THEN
qq = -qlag(iatom)*charges(jatom)
ELSE
qq = (0.5_dp*charges(iatom) - qlag(iatom))*charges(jatom)
END IF
rj(1:3) = particle_set(jatom)%r(1:3)
rij(1:3) = ri(1:3) - rj(1:3)
rij = pbc(rij, cell)
dr2 = SUM(rij**2)
dr = SQRT(dr2)
gama = gab(ikind, jkind)
gam2 = gama*gama
grc = 2._dp*gama*EXP(-gam2*dr2)*oorootpi/dr - erf(gama*dr)/dr2
fdik(:) = qq*grc*rij(:)/dr
gradient(:, iatom) = gradient(:, iatom) + fdik(:)
gradient(:, jatom) = gradient(:, jatom) - fdik(:)
END DO
END DO
END DO
END IF
! Forces from Ewald potential: (q+l)*A*q
IF (do_ewald) THEN
ALLOCATE (epforce(3, natom))
epforce = 0.0_dp
IF (response_only) THEN
dchia(1:natom) = qlag(1:natom)
ELSE
dchia(1:natom) = -charges(1:natom) + qlag(1:natom)
END IF
chrgx(1:natom) = charges(1:natom)
CALL spme_forces(ewald_env, ewald_pw, cell, particle_set, chrgx, &
particle_set, dchia, epforce)
dchia(1:natom) = charges(1:natom)
chrgx(1:natom) = qlag(1:natom)
CALL spme_forces(ewald_env, ewald_pw, cell, particle_set, chrgx, &
particle_set, dchia, epforce)
gradient(1:3, 1:natom) = gradient(1:3, 1:natom) + epforce(1:3, 1:natom)
DEALLOCATE (epforce)
! virial
IF (use_virial) THEN
chrgx(1:natom) = charges(1:natom) - qlag(1:natom)
CALL spme_virial(ewald_env, ewald_pw, particle_set, cell, chrgx, pvir)
stress = stress - pvir
chrgx(1:natom) = qlag(1:natom)
CALL spme_virial(ewald_env, ewald_pw, particle_set, cell, chrgx, pvir)
stress = stress + pvir
IF (response_only) THEN
chrgx(1:natom) = charges(1:natom)
CALL spme_virial(ewald_env, ewald_pw, particle_set, cell, chrgx, pvir)
stress = stress + pvir
END IF
END IF
!
CALL ewald_env_release(ewald_env)
CALL ewald_pw_release(ewald_pw)
DEALLOCATE (ewald_env, ewald_pw)
END IF
CALL cnumber_release(cnumbers, dcnum, .TRUE.)
DEALLOCATE (gab, gam, qlag, chrgx, dchia)
CALL timestop(handle)
END SUBROUTINE eeq_forces
! **************************************************************************************************
!> \brief ...
!> \param qs_env ...
!> \param cnumbers ...
!> \param dcnum ...
!> \param calculate_forces ...
! **************************************************************************************************
SUBROUTINE get_cnumbers(qs_env, cnumbers, dcnum, calculate_forces)
TYPE(qs_environment_type), POINTER :: qs_env
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: cnumbers
TYPE(dcnum_type), ALLOCATABLE, DIMENSION(:) :: dcnum
LOGICAL, INTENT(IN) :: calculate_forces
INTEGER :: ikind, natom, nkind, za
LOGICAL, ALLOCATABLE, DIMENSION(:) :: default_present
REAL(KIND=dp) :: subcells
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: c_radius
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: pair_radius
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(distribution_1d_type), POINTER :: distribution_1d
TYPE(distribution_2d_type), POINTER :: distribution_2d
TYPE(local_atoms_type), ALLOCATABLE, DIMENSION(:) :: atom2d
TYPE(molecule_type), DIMENSION(:), POINTER :: molecule_set
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_cn
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_dispersion_type), POINTER :: disp
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
CALL get_qs_env(qs_env, &
qs_kind_set=qs_kind_set, &
atomic_kind_set=atomic_kind_set, &
particle_set=particle_set, &
cell=cell, &
distribution_2d=distribution_2d, &
local_particles=distribution_1d, &
molecule_set=molecule_set)
CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
! Check for dispersion_env and sab_cn needed for cnumbers
ALLOCATE (disp)
disp%k1 = 16.0_dp
disp%k2 = 4._dp/3._dp
disp%eps_cn = 1.E-6_dp
disp%max_elem = maxElem
ALLOCATE (disp%rcov(maxElem))
disp%rcov(1:maxElem) = bohr*disp%k2*rcov(1:maxElem)
subcells = 2.0_dp
! Build the neighbor lists for the CN
NULLIFY (sab_cn)
ALLOCATE (c_radius(nkind), default_present(nkind), pair_radius(nkind, nkind))
c_radius(:) = 0.0_dp
default_present = .TRUE.
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), z=za)
c_radius(ikind) = 4._dp*rcov(za)*bohr
END DO
ALLOCATE (atom2d(nkind))
CALL atom2d_build(atom2d, distribution_1d, distribution_2d, atomic_kind_set, &
molecule_set, .FALSE., particle_set=particle_set)
CALL pair_radius_setup(default_present, default_present, c_radius, c_radius, pair_radius)
CALL build_neighbor_lists(sab_cn, particle_set, atom2d, cell, pair_radius, &
subcells=subcells, operator_type="PP", nlname="sab_cn")
disp%sab_cn => sab_cn
DEALLOCATE (c_radius, pair_radius, default_present)
CALL atom2d_cleanup(atom2d)
! Calculate coordination numbers
CALL cnumber_init(qs_env, cnumbers, dcnum, 2, calculate_forces, disp_env=disp)
CALL qs_dispersion_release(disp)
END SUBROUTINE get_cnumbers
! **************************************************************************************************
!> \brief ...
!> \param charges ...
!> \param lambda ...
!> \param eeq_energy ...
!> \param particle_set ...
!> \param kind_of ...
!> \param cell ...
!> \param chia ...
!> \param gam ...
!> \param gab ...
!> \param para_env ...
!> \param blacs_env ...
!> \param dft_control ...
!> \param eeq_sparam ...
!> \param totalcharge ...
!> \param ewald ...
!> \param ewald_env ...
!> \param ewald_pw ...
!> \param iounit ...
! **************************************************************************************************
SUBROUTINE eeq_solver(charges, lambda, eeq_energy, particle_set, kind_of, cell, &
chia, gam, gab, para_env, blacs_env, dft_control, eeq_sparam, &
totalcharge, ewald, ewald_env, ewald_pw, iounit)
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: charges
REAL(KIND=dp), INTENT(INOUT) :: lambda, eeq_energy
TYPE(particle_type), DIMENSION(:), INTENT(IN) :: particle_set
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
TYPE(cell_type), POINTER :: cell
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: chia, gam
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: gab
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(dft_control_type), POINTER :: dft_control
TYPE(eeq_solver_type), INTENT(IN) :: eeq_sparam
REAL(KIND=dp), INTENT(IN), OPTIONAL :: totalcharge
LOGICAL, INTENT(IN), OPTIONAL :: ewald
TYPE(ewald_environment_type), OPTIONAL, POINTER :: ewald_env
TYPE(ewald_pw_type), OPTIONAL, POINTER :: ewald_pw
INTEGER, INTENT(IN), OPTIONAL :: iounit
CHARACTER(len=*), PARAMETER :: routineN = 'eeq_solver'
INTEGER :: handle, ierror, iunit, natom, nkind, ns
LOGICAL :: do_direct, do_ewald, do_sparse
REAL(KIND=dp) :: alpha, deth, ftime, qtot
TYPE(cp_fm_struct_type), POINTER :: mat_struct
TYPE(cp_fm_type) :: eeq_mat
CALL timeset(routineN, handle)
do_ewald = .FALSE.
IF (PRESENT(ewald)) do_ewald = ewald
!
qtot = 0.0_dp
IF (PRESENT(totalcharge)) qtot = totalcharge
!
iunit = -1
IF (PRESENT(iounit)) iunit = iounit
! EEQ solver parameters
do_direct = eeq_sparam%direct
do_sparse = eeq_sparam%sparse
! sparse option NYA
IF (do_sparse) THEN
CALL cp_abort(__LOCATION__, "EEQ: Sparse option not yet available")
END IF
!
natom = SIZE(particle_set)
nkind = SIZE(gam)
ns = natom + 1
CALL cp_fm_struct_create(mat_struct, context=blacs_env, para_env=para_env, &
nrow_global=ns, ncol_global=ns)
CALL cp_fm_create(eeq_mat, mat_struct)
CALL cp_fm_set_all(eeq_mat, 0.0_dp, 0.0_dp)
!
IF (do_ewald) THEN
CPASSERT(PRESENT(ewald_env))
CPASSERT(PRESENT(ewald_pw))
IF (do_direct) THEN
IF (dft_control%apply_period_efield .AND. dft_control%period_efield%displacement_field) THEN
CPABORT("NYA")
ELSE
CALL fpbc_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, &
kind_of, cell, chia, gam, gab, qtot, &
ewald_env, ewald_pw, iounit)
END IF
ELSE
IF (dft_control%apply_period_efield .AND. dft_control%period_efield%displacement_field) THEN
CPABORT("NYA")
ELSE
ierror = 0
CALL pbc_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, &
kind_of, cell, chia, gam, gab, qtot, &
ewald_env, ewald_pw, eeq_sparam, ierror, iounit)
IF (ierror /= 0) THEN
! backup to non-iterative method
CALL fpbc_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, &
kind_of, cell, chia, gam, gab, qtot, &
ewald_env, ewald_pw, iounit)
END IF
END IF
END IF
IF (qtot /= 0._dp) THEN
CALL get_cell(cell=cell, deth=deth)
CALL ewald_env_get(ewald_env, alpha=alpha)
eeq_energy = eeq_energy - 0.5_dp*qtot**2/alpha**2/deth
END IF
ELSE
CALL mi_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, kind_of, &
cell, chia, gam, gab, qtot, ftime)
IF (iounit > 0) THEN
WRITE (iunit, '(A,T67,F14.3)') " EEQ| Molecular solver time[s]", ftime
END IF
END IF
CALL cp_fm_struct_release(mat_struct)
CALL cp_fm_release(eeq_mat)
CALL timestop(handle)
END SUBROUTINE eeq_solver
! **************************************************************************************************
!> \brief ...
!> \param charges ...
!> \param lambda ...
!> \param eeq_energy ...
!> \param eeq_mat ...
!> \param particle_set ...
!> \param kind_of ...
!> \param cell ...
!> \param chia ...
!> \param gam ...
!> \param gab ...
!> \param qtot ...
!> \param ftime ...
! **************************************************************************************************
SUBROUTINE mi_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, kind_of, cell, &
chia, gam, gab, qtot, ftime)
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: charges
REAL(KIND=dp), INTENT(INOUT) :: lambda, eeq_energy
TYPE(cp_fm_type) :: eeq_mat
TYPE(particle_type), DIMENSION(:), INTENT(IN) :: particle_set
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
TYPE(cell_type), POINTER :: cell
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: chia, gam
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: gab
REAL(KIND=dp), INTENT(IN) :: qtot
REAL(KIND=dp), INTENT(OUT) :: ftime
CHARACTER(len=*), PARAMETER :: routineN = 'mi_solver'
INTEGER :: handle, ia, iac, iar, ic, ikind, ir, &
jkind, natom, ncloc, ncvloc, nkind, &
nrloc, nrvloc, ns
INTEGER, DIMENSION(:), POINTER :: cind, cvind, rind, rvind
REAL(KIND=dp) :: dr, grc, te, ti, xr
REAL(KIND=dp), DIMENSION(3) :: ri, rij, rj
TYPE(cp_fm_struct_type), POINTER :: mat_struct, vec_struct
TYPE(cp_fm_type) :: rhs_vec
TYPE(mp_para_env_type), POINTER :: para_env
CALL timeset(routineN, handle)
ti = m_walltime()
natom = SIZE(particle_set)
nkind = SIZE(gam)
!
ns = natom + 1
CALL cp_fm_get_info(eeq_mat, matrix_struct=mat_struct, para_env=para_env)
CALL cp_fm_get_info(eeq_mat, nrow_local=nrloc, ncol_local=ncloc, &
row_indices=rind, col_indices=cind)
CALL cp_fm_struct_create(vec_struct, template_fmstruct=mat_struct, &
nrow_global=ns, ncol_global=1)
CALL cp_fm_create(rhs_vec, vec_struct)
CALL cp_fm_get_info(rhs_vec, nrow_local=nrvloc, ncol_local=ncvloc, &
row_indices=rvind, col_indices=cvind)
!
! set up matrix
CALL cp_fm_set_all(eeq_mat, 1.0_dp, 0.0_dp)
CALL cp_fm_set_all(rhs_vec, 0.0_dp)
DO ir = 1, nrloc
iar = rind(ir)
IF (iar > natom) CYCLE
ikind = kind_of(iar)
ri(1:3) = particle_set(iar)%r(1:3)
DO ic = 1, ncloc
iac = cind(ic)
IF (iac > natom) CYCLE
jkind = kind_of(iac)
rj(1:3) = particle_set(iac)%r(1:3)
IF (iar == iac) THEN
grc = gam(ikind) + 2.0_dp*gab(ikind, ikind)*oorootpi
ELSE
rij(1:3) = ri(1:3) - rj(1:3)
rij = pbc(rij, cell)
dr = SQRT(SUM(rij**2))
grc = erf(gab(ikind, jkind)*dr)/dr
END IF
eeq_mat%local_data(ir, ic) = grc
END DO
END DO
! set up rhs vector
DO ir = 1, nrvloc
iar = rvind(ir)
DO ic = 1, ncvloc
iac = cvind(ic)
ia = MAX(iar, iac)
IF (ia > natom) THEN
xr = qtot
ELSE
xr = -chia(ia)
END IF
rhs_vec%local_data(ir, ic) = xr
END DO
END DO
!
CALL cp_fm_solve(eeq_mat, rhs_vec)
!
charges = 0.0_dp
lambda = 0.0_dp
DO ir = 1, nrvloc
iar = rvind(ir)
DO ic = 1, ncvloc
iac = cvind(ic)
ia = MAX(iar, iac)
IF (ia <= natom) THEN
xr = rhs_vec%local_data(ir, ic)
charges(ia) = xr
ELSE
lambda = rhs_vec%local_data(ir, ic)
END IF
END DO
END DO
CALL para_env%sum(lambda)
CALL para_env%sum(charges)
!
! energy: 0.5*(q^T.X - lambda*totalcharge)
eeq_energy = 0.5*SUM(charges(1:natom)*chia(1:natom)) - 0.5_dp*lambda*qtot
CALL cp_fm_struct_release(vec_struct)
CALL cp_fm_release(rhs_vec)
te = m_walltime()
ftime = te - ti
CALL timestop(handle)
END SUBROUTINE mi_solver
! **************************************************************************************************
!> \brief ...
!> \param charges ...
!> \param lambda ...
!> \param eeq_energy ...
!> \param eeq_mat ...
!> \param particle_set ...
!> \param kind_of ...
!> \param cell ...
!> \param chia ...
!> \param gam ...
!> \param gab ...
!> \param qtot ...
!> \param ewald_env ...
!> \param ewald_pw ...
!> \param eeq_sparam ...
!> \param ierror ...
!> \param iounit ...
! **************************************************************************************************
SUBROUTINE pbc_solver(charges, lambda, eeq_energy, eeq_mat, particle_set, &
kind_of, cell, chia, gam, gab, qtot, &
ewald_env, ewald_pw, eeq_sparam, ierror, iounit)
REAL(KIND=dp), DIMENSION(:), INTENT(INOUT) :: charges
REAL(KIND=dp), INTENT(INOUT) :: lambda, eeq_energy
TYPE(cp_fm_type) :: eeq_mat
TYPE(particle_type), DIMENSION(:), INTENT(IN) :: particle_set
INTEGER, DIMENSION(:), INTENT(IN) :: kind_of
TYPE(cell_type), POINTER :: cell
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: chia, gam
REAL(KIND=dp), DIMENSION(:, :), INTENT(IN) :: gab
REAL(KIND=dp), INTENT(IN) :: qtot