-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmmm_tb_methods.F
1418 lines (1308 loc) · 68.5 KB
/
qmmm_tb_methods.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief TB methods used with QMMM
!> \author JGH
! **************************************************************************************************
MODULE qmmm_tb_methods
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind
USE cell_types, ONLY: cell_type,&
pbc
USE cp_control_types, ONLY: dft_control_type,&
dftb_control_type,&
xtb_control_type
USE cp_dbcsr_api, ONLY: &
dbcsr_add, dbcsr_copy, dbcsr_get_block_p, dbcsr_iterator_blocks_left, &
dbcsr_iterator_next_block, dbcsr_iterator_start, dbcsr_iterator_stop, dbcsr_iterator_type, &
dbcsr_p_type, dbcsr_set
USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set,&
dbcsr_deallocate_matrix_set
USE ewald_environment_types, ONLY: ewald_env_create,&
ewald_env_get,&
ewald_env_release,&
ewald_env_set,&
ewald_environment_type,&
read_ewald_section
USE ewald_pw_types, ONLY: ewald_pw_create,&
ewald_pw_release,&
ewald_pw_type
USE input_constants, ONLY: do_fist_pol_none
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type
USE kinds, ONLY: dp
USE message_passing, ONLY: mp_para_env_type
USE mulliken, ONLY: mulliken_charges
USE particle_types, ONLY: allocate_particle_set,&
deallocate_particle_set,&
particle_type
USE pw_poisson_types, ONLY: do_ewald_ewald,&
do_ewald_none,&
do_ewald_pme,&
do_ewald_spme
USE qmmm_types_low, ONLY: qmmm_env_qm_type,&
qmmm_pot_p_type,&
qmmm_pot_type
USE qmmm_util, ONLY: spherical_cutoff_factor
USE qs_dftb_coulomb, ONLY: gamma_rab_sr
USE qs_dftb_matrices, ONLY: build_dftb_overlap
USE qs_dftb_types, ONLY: qs_dftb_atom_type
USE qs_dftb_utils, ONLY: get_dftb_atom_param
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: get_qs_kind,&
qs_kind_type
USE qs_ks_qmmm_types, ONLY: qs_ks_qmmm_env_type
USE qs_ks_types, ONLY: qs_ks_env_type
USE qs_neighbor_list_types, ONLY: neighbor_list_set_p_type
USE qs_neighbor_lists, ONLY: build_qs_neighbor_lists
USE qs_overlap, ONLY: build_overlap_matrix
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE spme, ONLY: spme_forces,&
spme_potential
USE xtb_types, ONLY: get_xtb_atom_param,&
xtb_atom_type
#include "./base/base_uses.f90"
IMPLICIT NONE
! small real number
REAL(dp), PARAMETER :: rtiny = 1.e-10_dp
! eta(0) for mm atoms and non-scc qm atoms
REAL(dp), PARAMETER :: eta_mm = 0.47_dp
! step size for qmmm finite difference
REAL(dp), PARAMETER :: ddrmm = 0.0001_dp
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qmmm_tb_methods'
PUBLIC :: build_tb_qmmm_matrix, build_tb_qmmm_matrix_zero, &
build_tb_qmmm_matrix_pc, deriv_tb_qmmm_matrix, &
deriv_tb_qmmm_matrix_pc
CONTAINS
! **************************************************************************************************
!> \brief Constructs the 1-el DFTB hamiltonian
!> \param qs_env ...
!> \param qmmm_env ...
!> \param particles_mm ...
!> \param mm_cell ...
!> \param para_env ...
!> \author JGH 10.2014 [created]
! **************************************************************************************************
SUBROUTINE build_tb_qmmm_matrix(qs_env, qmmm_env, particles_mm, mm_cell, para_env)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(qmmm_env_qm_type), POINTER :: qmmm_env
TYPE(particle_type), DIMENSION(:), POINTER :: particles_mm
TYPE(cell_type), POINTER :: mm_cell
TYPE(mp_para_env_type), POINTER :: para_env
CHARACTER(len=*), PARAMETER :: routineN = 'build_tb_qmmm_matrix'
INTEGER :: blk, handle, i, iatom, ikind, jatom, &
natom, natorb, nkind
INTEGER, DIMENSION(:), POINTER :: list
LOGICAL :: defined, do_dftb, do_xtb, found
REAL(KIND=dp) :: pc_ener, zeff
REAL(KIND=dp), DIMENSION(0:3) :: eta_a
REAL(KIND=dp), DIMENSION(:), POINTER :: qpot
REAL(KIND=dp), DIMENSION(:, :), POINTER :: hblock, sblock
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_h, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(dftb_control_type), POINTER :: dftb_control
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_nl
TYPE(particle_type), DIMENSION(:), POINTER :: particles_qm
TYPE(qs_dftb_atom_type), POINTER :: dftb_kind
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_ks_qmmm_env_type), POINTER :: ks_qmmm_env_loc
TYPE(qs_rho_type), POINTER :: rho
TYPE(xtb_atom_type), POINTER :: xtb_kind
TYPE(xtb_control_type), POINTER :: xtb_control
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env=qs_env, &
dft_control=dft_control, &
atomic_kind_set=atomic_kind_set, &
particle_set=particles_qm, &
qs_kind_set=qs_kind_set, &
rho=rho, &
natom=natom)
dftb_control => dft_control%qs_control%dftb_control
xtb_control => dft_control%qs_control%xtb_control
IF (dft_control%qs_control%dftb) THEN
do_dftb = .TRUE.
do_xtb = .FALSE.
ELSEIF (dft_control%qs_control%xtb) THEN
do_dftb = .FALSE.
do_xtb = .TRUE.
ELSE
CPABORT("TB method unknown")
END IF
CALL build_qs_neighbor_lists(qs_env, para_env, force_env_section=qs_env%input)
NULLIFY (matrix_s)
IF (do_dftb) THEN
CALL build_dftb_overlap(qs_env, 0, matrix_s)
ELSEIF (do_xtb) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, sab_orb=sab_nl)
CALL build_overlap_matrix(ks_env, matrix_s, basis_type_a='ORB', basis_type_b='ORB', sab_nl=sab_nl)
END IF
ALLOCATE (qpot(natom))
qpot = 0.0_dp
pc_ener = 0.0_dp
nkind = SIZE(atomic_kind_set)
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
IF (do_dftb) THEN
NULLIFY (dftb_kind)
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, zeff=zeff, &
defined=defined, eta=eta_a, natorb=natorb)
! use mm charge smearing for non-scc cases
IF (.NOT. dftb_control%self_consistent) eta_a(0) = eta_mm
IF (.NOT. defined .OR. natorb < 1) CYCLE
ELSEIF (do_xtb) THEN
NULLIFY (xtb_kind)
CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
eta_a(0) = eta_mm
END IF
DO i = 1, SIZE(list)
iatom = list(i)
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, particles_qm)
! Possibly added charges
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, &
particles_qm)
END IF
pc_ener = pc_ener + qpot(iatom)*zeff
END DO
END DO
! Allocate the core Hamiltonian matrix
CALL get_qs_env(qs_env=qs_env, ks_qmmm_env=ks_qmmm_env_loc)
matrix_h => ks_qmmm_env_loc%matrix_h
CALL dbcsr_allocate_matrix_set(matrix_h, 1)
ALLOCATE (matrix_h(1)%matrix)
CALL dbcsr_copy(matrix_h(1)%matrix, matrix_s(1)%matrix, &
name="QMMM HAMILTONIAN MATRIX")
CALL dbcsr_set(matrix_h(1)%matrix, 0.0_dp)
CALL dbcsr_iterator_start(iter, matrix_s(1)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sblock, blk)
NULLIFY (hblock)
CALL dbcsr_get_block_p(matrix=matrix_h(1)%matrix, &
row=iatom, col=jatom, block=hblock, found=found)
CPASSERT(found)
hblock = hblock - 0.5_dp*sblock*(qpot(iatom) + qpot(jatom))
END DO
CALL dbcsr_iterator_stop(iter)
ks_qmmm_env_loc%matrix_h => matrix_h
ks_qmmm_env_loc%pc_ener = pc_ener
DEALLOCATE (qpot)
CALL dbcsr_deallocate_matrix_set(matrix_s)
CALL timestop(handle)
END SUBROUTINE build_tb_qmmm_matrix
! **************************************************************************************************
!> \brief Constructs an empty 1-el DFTB hamiltonian
!> \param qs_env ...
!> \param para_env ...
!> \author JGH 10.2014 [created]
! **************************************************************************************************
SUBROUTINE build_tb_qmmm_matrix_zero(qs_env, para_env)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(mp_para_env_type), POINTER :: para_env
CHARACTER(len=*), PARAMETER :: routineN = 'build_tb_qmmm_matrix_zero'
INTEGER :: handle
LOGICAL :: do_dftb, do_xtb
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_h, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_nl
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_ks_qmmm_env_type), POINTER :: ks_qmmm_env_loc
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env=qs_env, dft_control=dft_control)
IF (dft_control%qs_control%dftb) THEN
do_dftb = .TRUE.
do_xtb = .FALSE.
ELSEIF (dft_control%qs_control%xtb) THEN
do_dftb = .FALSE.
do_xtb = .TRUE.
ELSE
CPABORT("TB method unknown")
END IF
CALL build_qs_neighbor_lists(qs_env, para_env, force_env_section=qs_env%input)
NULLIFY (matrix_s)
IF (do_dftb) THEN
CALL build_dftb_overlap(qs_env, 0, matrix_s)
ELSEIF (do_xtb) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, sab_orb=sab_nl)
CALL build_overlap_matrix(ks_env, matrix_s, basis_type_a='ORB', basis_type_b='ORB', sab_nl=sab_nl)
END IF
! Allocate the core Hamiltonian matrix
CALL get_qs_env(qs_env=qs_env, ks_qmmm_env=ks_qmmm_env_loc)
matrix_h => ks_qmmm_env_loc%matrix_h
CALL dbcsr_allocate_matrix_set(matrix_h, 1)
ALLOCATE (matrix_h(1)%matrix)
CALL dbcsr_copy(matrix_h(1)%matrix, matrix_s(1)%matrix, &
name="QMMM HAMILTONIAN MATRIX")
CALL dbcsr_set(matrix_h(1)%matrix, 0.0_dp)
ks_qmmm_env_loc%matrix_h => matrix_h
ks_qmmm_env_loc%pc_ener = 0.0_dp
CALL dbcsr_deallocate_matrix_set(matrix_s)
CALL timestop(handle)
END SUBROUTINE build_tb_qmmm_matrix_zero
! **************************************************************************************************
!> \brief Constructs the 1-el DFTB hamiltonian
!> \param qs_env ...
!> \param qmmm_env ...
!> \param particles_mm ...
!> \param mm_cell ...
!> \param para_env ...
!> \author JGH 10.2014 [created]
! **************************************************************************************************
SUBROUTINE build_tb_qmmm_matrix_pc(qs_env, qmmm_env, particles_mm, mm_cell, para_env)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(qmmm_env_qm_type), POINTER :: qmmm_env
TYPE(particle_type), DIMENSION(:), POINTER :: particles_mm
TYPE(cell_type), POINTER :: mm_cell
TYPE(mp_para_env_type), POINTER :: para_env
CHARACTER(len=*), PARAMETER :: routineN = 'build_tb_qmmm_matrix_pc'
INTEGER :: blk, do_ipol, ewald_type, handle, i, &
iatom, ikind, imm, imp, indmm, ipot, &
jatom, natom, natorb, nkind, nmm
INTEGER, DIMENSION(:), POINTER :: list
LOGICAL :: defined, do_dftb, do_multipoles, do_xtb, &
found
REAL(KIND=dp) :: alpha, pc_ener, zeff
REAL(KIND=dp), DIMENSION(0:3) :: eta_a
REAL(KIND=dp), DIMENSION(2) :: rcutoff
REAL(KIND=dp), DIMENSION(:), POINTER :: charges_mm, qpot
REAL(KIND=dp), DIMENSION(:, :), POINTER :: hblock, sblock
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_h, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(dftb_control_type), POINTER :: dftb_control
TYPE(ewald_environment_type), POINTER :: ewald_env
TYPE(ewald_pw_type), POINTER :: ewald_pw
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_nl
TYPE(particle_type), DIMENSION(:), POINTER :: atoms_mm, particles_qm
TYPE(qmmm_pot_type), POINTER :: Pot
TYPE(qs_dftb_atom_type), POINTER :: dftb_kind
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_ks_qmmm_env_type), POINTER :: ks_qmmm_env_loc
TYPE(qs_rho_type), POINTER :: rho
TYPE(section_vals_type), POINTER :: ewald_section, poisson_section, &
print_section
TYPE(xtb_atom_type), POINTER :: xtb_kind
TYPE(xtb_control_type), POINTER :: xtb_control
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env=qs_env, &
dft_control=dft_control, &
atomic_kind_set=atomic_kind_set, &
particle_set=particles_qm, &
qs_kind_set=qs_kind_set, &
rho=rho, &
natom=natom)
dftb_control => dft_control%qs_control%dftb_control
xtb_control => dft_control%qs_control%xtb_control
IF (dft_control%qs_control%dftb) THEN
do_dftb = .TRUE.
do_xtb = .FALSE.
ELSEIF (dft_control%qs_control%xtb) THEN
do_dftb = .FALSE.
do_xtb = .TRUE.
ELSE
CPABORT("TB method unknown")
END IF
CALL build_qs_neighbor_lists(qs_env, para_env, force_env_section=qs_env%input)
NULLIFY (matrix_s)
IF (do_dftb) THEN
CALL build_dftb_overlap(qs_env, 0, matrix_s)
ELSEIF (do_xtb) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, sab_orb=sab_nl)
CALL build_overlap_matrix(ks_env, matrix_s, basis_type_a='ORB', basis_type_b='ORB', sab_nl=sab_nl)
END IF
ALLOCATE (qpot(natom))
qpot = 0.0_dp
pc_ener = 0.0_dp
! Create Ewald environments
poisson_section => section_vals_get_subs_vals(qs_env%input, "MM%POISSON")
ALLOCATE (ewald_env)
CALL ewald_env_create(ewald_env, para_env)
CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
CALL read_ewald_section(ewald_env, ewald_section)
print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
ALLOCATE (ewald_pw)
CALL ewald_pw_create(ewald_pw, ewald_env, mm_cell, mm_cell, print_section=print_section)
CALL ewald_env_get(ewald_env, ewald_type=ewald_type, do_multipoles=do_multipoles, do_ipol=do_ipol)
IF (do_multipoles) CPABORT("No multipole force fields allowed in TB QM/MM")
IF (do_ipol /= do_fist_pol_none) CPABORT("No polarizable force fields allowed in TB QM/MM")
SELECT CASE (ewald_type)
CASE (do_ewald_pme)
CPABORT("PME Ewald type not implemented for TB/QMMM")
CASE (do_ewald_ewald, do_ewald_spme)
DO ipot = 1, SIZE(qmmm_env%Potentials)
Pot => qmmm_env%Potentials(ipot)%Pot
nmm = SIZE(Pot%mm_atom_index)
! get a 'clean' mm particle set
NULLIFY (atoms_mm)
CALL allocate_particle_set(atoms_mm, nmm)
ALLOCATE (charges_mm(nmm))
DO Imp = 1, nmm
Imm = Pot%mm_atom_index(Imp)
IndMM = qmmm_env%mm_atom_index(Imm)
atoms_mm(Imp)%r = particles_mm(IndMM)%r
atoms_mm(Imp)%atomic_kind => particles_mm(IndMM)%atomic_kind
charges_mm(Imp) = qmmm_env%mm_atom_chrg(Imm)
END DO
IF (ewald_type == do_ewald_ewald) THEN
CPABORT("Ewald not implemented for TB/QMMM")
ELSE IF (ewald_type == do_ewald_spme) THEN
! spme electrostatic potential
CALL spme_potential(ewald_env, ewald_pw, mm_cell, atoms_mm, charges_mm, particles_qm, qpot)
END IF
CALL deallocate_particle_set(atoms_mm)
DEALLOCATE (charges_mm)
END DO
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
DO ipot = 1, SIZE(qmmm_env%added_charges%Potentials)
Pot => qmmm_env%added_charges%Potentials(ipot)%Pot
nmm = SIZE(Pot%mm_atom_index)
! get a 'clean' mm particle set
NULLIFY (atoms_mm)
CALL allocate_particle_set(atoms_mm, nmm)
ALLOCATE (charges_mm(nmm))
DO Imp = 1, nmm
Imm = Pot%mm_atom_index(Imp)
IndMM = qmmm_env%added_charges%mm_atom_index(Imm)
atoms_mm(Imp)%r = qmmm_env%added_charges%added_particles(IndMM)%r
atoms_mm(Imp)%atomic_kind => qmmm_env%added_charges%added_particles(IndMM)%atomic_kind
charges_mm(Imp) = qmmm_env%added_charges%mm_atom_chrg(Imm)
END DO
IF (ewald_type == do_ewald_ewald) THEN
CPABORT("Ewald not implemented for TB/QMMM")
ELSE IF (ewald_type == do_ewald_spme) THEN
! spme electrostatic potential
CALL spme_potential(ewald_env, ewald_pw, mm_cell, atoms_mm, charges_mm, particles_qm, qpot)
END IF
CALL deallocate_particle_set(atoms_mm)
DEALLOCATE (charges_mm)
END DO
END IF
CALL para_env%sum(qpot)
! Add Ewald and TB short range corrections
! This is effectively using a minimum image convention!
! Set rcutoff to values compatible with alpha Ewald
CALL ewald_env_get(ewald_env, rcut=rcutoff(1), alpha=alpha)
rcutoff(2) = 0.025_dp*rcutoff(1)
rcutoff(1) = 2.0_dp*rcutoff(1)
nkind = SIZE(atomic_kind_set)
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
IF (do_dftb) THEN
NULLIFY (dftb_kind)
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, zeff=zeff, &
defined=defined, eta=eta_a, natorb=natorb)
! use mm charge smearing for non-scc cases
IF (.NOT. dftb_control%self_consistent) eta_a(0) = eta_mm
IF (.NOT. defined .OR. natorb < 1) CYCLE
ELSEIF (do_xtb) THEN
NULLIFY (xtb_kind)
CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
eta_a(0) = eta_mm
END IF
DO i = 1, SIZE(list)
iatom = list(i)
CALL build_mm_pot(qpot(iatom), 1, eta_a(0), qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, mm_cell, iatom, rcutoff, &
particles_qm)
CALL build_mm_pot(qpot(iatom), 2, alpha, qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, mm_cell, iatom, rcutoff, &
particles_qm)
! Possibly added charges
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
CALL build_mm_pot(qpot(iatom), 1, eta_a(0), qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, rcutoff, &
particles_qm)
CALL build_mm_pot(qpot(iatom), 2, alpha, qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, rcutoff, &
particles_qm)
END IF
pc_ener = pc_ener + qpot(iatom)*zeff
END DO
END DO
CASE (do_ewald_none)
! Simply summing up charges with 1/R (DFTB corrected)
nkind = SIZE(atomic_kind_set)
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
IF (do_dftb) THEN
NULLIFY (dftb_kind)
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, zeff=zeff, &
defined=defined, eta=eta_a, natorb=natorb)
! use mm charge smearing for non-scc cases
IF (.NOT. dftb_control%self_consistent) eta_a(0) = eta_mm
IF (.NOT. defined .OR. natorb < 1) CYCLE
ELSEIF (do_xtb) THEN
NULLIFY (xtb_kind)
CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
eta_a(0) = eta_mm
END IF
DO i = 1, SIZE(list)
iatom = list(i)
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, particles_qm)
! Possibly added charges
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, &
particles_qm)
END IF
pc_ener = pc_ener + qpot(iatom)*zeff
END DO
END DO
CASE DEFAULT
CPABORT("Unknown Ewald type!")
END SELECT
! Allocate the core Hamiltonian matrix
CALL get_qs_env(qs_env=qs_env, ks_qmmm_env=ks_qmmm_env_loc)
matrix_h => ks_qmmm_env_loc%matrix_h
CALL dbcsr_allocate_matrix_set(matrix_h, 1)
ALLOCATE (matrix_h(1)%matrix)
CALL dbcsr_copy(matrix_h(1)%matrix, matrix_s(1)%matrix, &
name="QMMM HAMILTONIAN MATRIX")
CALL dbcsr_set(matrix_h(1)%matrix, 0.0_dp)
CALL dbcsr_iterator_start(iter, matrix_s(1)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sblock, blk)
NULLIFY (hblock)
CALL dbcsr_get_block_p(matrix=matrix_h(1)%matrix, &
row=iatom, col=jatom, block=hblock, found=found)
CPASSERT(found)
hblock = hblock - 0.5_dp*sblock*(qpot(iatom) + qpot(jatom))
END DO
CALL dbcsr_iterator_stop(iter)
ks_qmmm_env_loc%matrix_h => matrix_h
ks_qmmm_env_loc%pc_ener = pc_ener
DEALLOCATE (qpot)
! Release Ewald environment
CALL ewald_env_release(ewald_env)
DEALLOCATE (ewald_env)
CALL ewald_pw_release(ewald_pw)
DEALLOCATE (ewald_pw)
CALL dbcsr_deallocate_matrix_set(matrix_s)
CALL timestop(handle)
END SUBROUTINE build_tb_qmmm_matrix_pc
! **************************************************************************************************
!> \brief Constructs the derivative w.r.t. 1-el DFTB hamiltonian QMMM terms
!> \param qs_env ...
!> \param qmmm_env ...
!> \param particles_mm ...
!> \param mm_cell ...
!> \param para_env ...
!> \param calc_force ...
!> \param Forces ...
!> \param Forces_added_charges ...
!> \author JGH 10.2014 [created]
! **************************************************************************************************
SUBROUTINE deriv_tb_qmmm_matrix(qs_env, qmmm_env, particles_mm, mm_cell, para_env, &
calc_force, Forces, Forces_added_charges)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(qmmm_env_qm_type), POINTER :: qmmm_env
TYPE(particle_type), DIMENSION(:), POINTER :: particles_mm
TYPE(cell_type), POINTER :: mm_cell
TYPE(mp_para_env_type), POINTER :: para_env
LOGICAL, INTENT(in), OPTIONAL :: calc_force
REAL(KIND=dp), DIMENSION(:, :), POINTER :: Forces, Forces_added_charges
CHARACTER(len=*), PARAMETER :: routineN = 'deriv_tb_qmmm_matrix'
INTEGER :: atom_a, blk, handle, i, iatom, ikind, &
iqm, jatom, natom, natorb, nkind, &
nspins, number_qm_atoms
INTEGER, DIMENSION(:), POINTER :: list
LOGICAL :: defined, do_dftb, do_xtb, found
REAL(KIND=dp) :: fi, gmij, zeff
REAL(KIND=dp), DIMENSION(0:3) :: eta_a
REAL(KIND=dp), DIMENSION(:), POINTER :: mcharge, qpot
REAL(KIND=dp), DIMENSION(:, :), POINTER :: charges, dsblock, Forces_QM, pblock, &
sblock
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_p, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(dftb_control_type), POINTER :: dftb_control
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_nl
TYPE(particle_type), DIMENSION(:), POINTER :: particles_qm
TYPE(qs_dftb_atom_type), POINTER :: dftb_kind
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_ks_qmmm_env_type), POINTER :: ks_qmmm_env_loc
TYPE(qs_rho_type), POINTER :: rho
TYPE(xtb_atom_type), POINTER :: xtb_kind
TYPE(xtb_control_type), POINTER :: xtb_control
CALL timeset(routineN, handle)
IF (calc_force) THEN
NULLIFY (rho, atomic_kind_set, qs_kind_set, particles_qm)
CALL get_qs_env(qs_env=qs_env, &
rho=rho, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=qs_kind_set, &
ks_qmmm_env=ks_qmmm_env_loc, &
dft_control=dft_control, &
particle_set=particles_qm, &
natom=number_qm_atoms)
dftb_control => dft_control%qs_control%dftb_control
xtb_control => dft_control%qs_control%xtb_control
IF (dft_control%qs_control%dftb) THEN
do_dftb = .TRUE.
do_xtb = .FALSE.
ELSEIF (dft_control%qs_control%xtb) THEN
do_dftb = .FALSE.
do_xtb = .TRUE.
ELSE
CPABORT("TB method unknown")
END IF
NULLIFY (matrix_s)
IF (do_dftb) THEN
CALL build_dftb_overlap(qs_env, 1, matrix_s)
ELSEIF (do_xtb) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, sab_orb=sab_nl)
CALL build_overlap_matrix(ks_env, matrix_s, nderivative=1, &
basis_type_a='ORB', basis_type_b='ORB', sab_nl=sab_nl)
END IF
CALL qs_rho_get(rho, rho_ao=matrix_p)
nspins = dft_control%nspins
nkind = SIZE(atomic_kind_set)
! Mulliken charges
ALLOCATE (charges(number_qm_atoms, nspins))
!
CALL mulliken_charges(matrix_p, matrix_s(1)%matrix, para_env, charges)
!
ALLOCATE (mcharge(number_qm_atoms))
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom)
IF (do_dftb) THEN
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, zeff=zeff)
ELSEIF (do_xtb) THEN
CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
END IF
DO iatom = 1, natom
atom_a = atomic_kind_set(ikind)%atom_list(iatom)
mcharge(atom_a) = zeff - SUM(charges(atom_a, 1:nspins))
END DO
END DO
DEALLOCATE (charges)
ALLOCATE (qpot(number_qm_atoms))
qpot = 0.0_dp
ALLOCATE (Forces_QM(3, number_qm_atoms))
Forces_QM = 0.0_dp
! calculate potential and forces from classical charges
iqm = 0
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
IF (do_dftb) THEN
NULLIFY (dftb_kind)
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, &
defined=defined, eta=eta_a, natorb=natorb)
! use mm charge smearing for non-scc cases
IF (.NOT. dftb_control%self_consistent) eta_a(0) = eta_mm
IF (.NOT. defined .OR. natorb < 1) CYCLE
ELSEIF (do_xtb) THEN
eta_a(0) = eta_mm
END IF
DO i = 1, SIZE(list)
iatom = list(i)
iqm = iqm + 1
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, particles_qm)
CALL build_mm_dpot(mcharge(iatom), 0, eta_a(0), qmmm_env%Potentials, particles_mm, &
qmmm_env%mm_atom_chrg, qmmm_env%mm_atom_index, &
mm_cell, iatom, Forces, Forces_QM(:, iqm), &
qmmm_env%spherical_cutoff, particles_qm)
! Possibly added charges
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
CALL build_mm_pot(qpot(iatom), 0, eta_a(0), qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, &
qmmm_env%spherical_cutoff, &
particles_qm)
CALL build_mm_dpot(mcharge(iatom), 0, eta_a(0), qmmm_env%added_charges%potentials, &
qmmm_env%added_charges%added_particles, &
qmmm_env%added_charges%mm_atom_chrg, &
qmmm_env%added_charges%mm_atom_index, mm_cell, iatom, &
Forces_added_charges, &
Forces_QM(:, iqm), qmmm_env%spherical_cutoff, particles_qm)
END IF
END DO
END DO
! Transfer QM gradients to the QM particles..
iqm = 0
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
IF (do_dftb) THEN
NULLIFY (dftb_kind)
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, defined=defined, natorb=natorb)
IF (.NOT. defined .OR. natorb < 1) CYCLE
ELSEIF (do_xtb) THEN
! use all kinds
END IF
DO i = 1, SIZE(list)
iqm = iqm + 1
iatom = qmmm_env%qm_atom_index(list(i))
particles_mm(iatom)%f(:) = particles_mm(iatom)%f(:) + Forces_QM(:, iqm)
END DO
END DO
! derivatives from qm charges
Forces_QM = 0.0_dp
IF (SIZE(matrix_p) == 2) THEN
CALL dbcsr_add(matrix_p(1)%matrix, matrix_p(2)%matrix, &
alpha_scalar=1.0_dp, beta_scalar=1.0_dp)
END IF
!
CALL dbcsr_iterator_start(iter, matrix_s(1)%matrix)
DO WHILE (dbcsr_iterator_blocks_left(iter))
CALL dbcsr_iterator_next_block(iter, iatom, jatom, sblock, blk)
!
IF (iatom == jatom) CYCLE
!
gmij = -0.5_dp*(qpot(iatom) + qpot(jatom))
NULLIFY (pblock)
CALL dbcsr_get_block_p(matrix=matrix_p(1)%matrix, &
row=iatom, col=jatom, block=pblock, found=found)
CPASSERT(found)
DO i = 1, 3
NULLIFY (dsblock)
CALL dbcsr_get_block_p(matrix=matrix_s(1 + i)%matrix, &
row=iatom, col=jatom, block=dsblock, found=found)
CPASSERT(found)
fi = -2.0_dp*gmij*SUM(pblock*dsblock)
Forces_QM(i, iatom) = Forces_QM(i, iatom) + fi
Forces_QM(i, jatom) = Forces_QM(i, jatom) - fi
END DO
END DO
CALL dbcsr_iterator_stop(iter)
!
IF (SIZE(matrix_p) == 2) THEN
CALL dbcsr_add(matrix_p(1)%matrix, matrix_p(2)%matrix, &
alpha_scalar=1.0_dp, beta_scalar=-1.0_dp)
END IF
!
! Transfer QM gradients to the QM particles..
CALL para_env%sum(Forces_QM)
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), atom_list=list)
DO i = 1, SIZE(list)
iqm = list(i)
iatom = qmmm_env%qm_atom_index(iqm)
particles_mm(iatom)%f(:) = particles_mm(iatom)%f(:) + Forces_QM(:, iqm)
END DO
END DO
!
DEALLOCATE (mcharge)
!
! MM forces will be handled directly from the QMMM module in the same way
! as for GPW/GAPW methods
DEALLOCATE (Forces_QM)
DEALLOCATE (qpot)
CALL dbcsr_deallocate_matrix_set(matrix_s)
END IF
CALL timestop(handle)
END SUBROUTINE deriv_tb_qmmm_matrix
! **************************************************************************************************
!> \brief Constructs the derivative w.r.t. 1-el DFTB hamiltonian QMMM terms
!> \param qs_env ...
!> \param qmmm_env ...
!> \param particles_mm ...
!> \param mm_cell ...
!> \param para_env ...
!> \param calc_force ...
!> \param Forces ...
!> \param Forces_added_charges ...
!> \author JGH 10.2014 [created]
! **************************************************************************************************
SUBROUTINE deriv_tb_qmmm_matrix_pc(qs_env, qmmm_env, particles_mm, mm_cell, para_env, &
calc_force, Forces, Forces_added_charges)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(qmmm_env_qm_type), POINTER :: qmmm_env
TYPE(particle_type), DIMENSION(:), POINTER :: particles_mm
TYPE(cell_type), POINTER :: mm_cell
TYPE(mp_para_env_type), POINTER :: para_env
LOGICAL, INTENT(in), OPTIONAL :: calc_force
REAL(KIND=dp), DIMENSION(:, :), POINTER :: Forces, Forces_added_charges
CHARACTER(len=*), PARAMETER :: routineN = 'deriv_tb_qmmm_matrix_pc'
INTEGER :: atom_a, blk, do_ipol, ewald_type, handle, i, iatom, ikind, imm, imp, indmm, ipot, &
iqm, jatom, natom, natorb, nkind, nmm, nspins, number_qm_atoms
INTEGER, DIMENSION(:), POINTER :: list
LOGICAL :: defined, do_dftb, do_multipoles, do_xtb, &
found
REAL(KIND=dp) :: alpha, fi, gmij, zeff
REAL(KIND=dp), DIMENSION(0:3) :: eta_a
REAL(KIND=dp), DIMENSION(2) :: rcutoff
REAL(KIND=dp), DIMENSION(:), POINTER :: charges_mm, mcharge, qpot
REAL(KIND=dp), DIMENSION(:, :), POINTER :: charges, dsblock, Forces_MM, Forces_QM, &
pblock, sblock
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(dbcsr_iterator_type) :: iter
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_p, matrix_s
TYPE(dft_control_type), POINTER :: dft_control
TYPE(dftb_control_type), POINTER :: dftb_control
TYPE(ewald_environment_type), POINTER :: ewald_env
TYPE(ewald_pw_type), POINTER :: ewald_pw
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_nl
TYPE(particle_type), DIMENSION(:), POINTER :: atoms_mm, particles_qm
TYPE(qmmm_pot_type), POINTER :: Pot
TYPE(qs_dftb_atom_type), POINTER :: dftb_kind
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_ks_env_type), POINTER :: ks_env
TYPE(qs_ks_qmmm_env_type), POINTER :: ks_qmmm_env_loc
TYPE(qs_rho_type), POINTER :: rho
TYPE(section_vals_type), POINTER :: ewald_section, poisson_section, &
print_section
TYPE(xtb_atom_type), POINTER :: xtb_kind
TYPE(xtb_control_type), POINTER :: xtb_control
CALL timeset(routineN, handle)
IF (calc_force) THEN
NULLIFY (rho, atomic_kind_set, qs_kind_set, particles_qm)
CALL get_qs_env(qs_env=qs_env, &
rho=rho, &
atomic_kind_set=atomic_kind_set, &
qs_kind_set=qs_kind_set, &
ks_qmmm_env=ks_qmmm_env_loc, &
dft_control=dft_control, &
particle_set=particles_qm, &
natom=number_qm_atoms)
dftb_control => dft_control%qs_control%dftb_control
xtb_control => dft_control%qs_control%xtb_control
IF (dft_control%qs_control%dftb) THEN
do_dftb = .TRUE.
do_xtb = .FALSE.
ELSEIF (dft_control%qs_control%xtb) THEN
do_dftb = .FALSE.
do_xtb = .TRUE.
ELSE
CPABORT("TB method unknown")
END IF
NULLIFY (matrix_s)
IF (do_dftb) THEN
CALL build_dftb_overlap(qs_env, 1, matrix_s)
ELSEIF (do_xtb) THEN
CALL get_qs_env(qs_env=qs_env, ks_env=ks_env, sab_orb=sab_nl)
CALL build_overlap_matrix(ks_env, matrix_s, nderivative=1, &
basis_type_a='ORB', basis_type_b='ORB', sab_nl=sab_nl)
END IF
CALL qs_rho_get(rho, rho_ao=matrix_p)
nspins = dft_control%nspins
nkind = SIZE(atomic_kind_set)
! Mulliken charges
ALLOCATE (charges(number_qm_atoms, nspins))
!
CALL mulliken_charges(matrix_p, matrix_s(1)%matrix, para_env, charges)
!
ALLOCATE (mcharge(number_qm_atoms))
DO ikind = 1, nkind
CALL get_atomic_kind(atomic_kind_set(ikind), natom=natom)
IF (do_dftb) THEN
CALL get_qs_kind(qs_kind_set(ikind), dftb_parameter=dftb_kind)
CALL get_dftb_atom_param(dftb_kind, zeff=zeff)
ELSEIF (do_xtb) THEN
CALL get_qs_kind(qs_kind_set(ikind), xtb_parameter=xtb_kind)
CALL get_xtb_atom_param(xtb_kind, zeff=zeff)
END IF
DO iatom = 1, natom
atom_a = atomic_kind_set(ikind)%atom_list(iatom)
mcharge(atom_a) = zeff - SUM(charges(atom_a, 1:nspins))
END DO
END DO
DEALLOCATE (charges)
ALLOCATE (qpot(number_qm_atoms))
qpot = 0.0_dp
ALLOCATE (Forces_QM(3, number_qm_atoms))
Forces_QM = 0.0_dp
! Create Ewald environments
poisson_section => section_vals_get_subs_vals(qs_env%input, "MM%POISSON")
ALLOCATE (ewald_env)
CALL ewald_env_create(ewald_env, para_env)
CALL ewald_env_set(ewald_env, poisson_section=poisson_section)
ewald_section => section_vals_get_subs_vals(poisson_section, "EWALD")
CALL read_ewald_section(ewald_env, ewald_section)
print_section => section_vals_get_subs_vals(qs_env%input, "PRINT%GRID_INFORMATION")
ALLOCATE (ewald_pw)
CALL ewald_pw_create(ewald_pw, ewald_env, mm_cell, mm_cell, print_section=print_section)
CALL ewald_env_get(ewald_env, ewald_type=ewald_type, do_multipoles=do_multipoles, do_ipol=do_ipol)
IF (do_multipoles) CPABORT("No multipole force fields allowed in DFTB QM/MM")
IF (do_ipol /= do_fist_pol_none) CPABORT("No polarizable force fields allowed in DFTB QM/MM")
SELECT CASE (ewald_type)
CASE (do_ewald_pme)
CPABORT("PME Ewald type not implemented for DFTB/QMMM")
CASE (do_ewald_ewald, do_ewald_spme)
DO ipot = 1, SIZE(qmmm_env%Potentials)
Pot => qmmm_env%Potentials(ipot)%Pot
nmm = SIZE(Pot%mm_atom_index)
! get a 'clean' mm particle set
NULLIFY (atoms_mm)
CALL allocate_particle_set(atoms_mm, nmm)
ALLOCATE (charges_mm(nmm))
DO Imp = 1, nmm
Imm = Pot%mm_atom_index(Imp)
IndMM = qmmm_env%mm_atom_index(Imm)
atoms_mm(Imp)%r = particles_mm(IndMM)%r
atoms_mm(Imp)%atomic_kind => particles_mm(IndMM)%atomic_kind
charges_mm(Imp) = qmmm_env%mm_atom_chrg(Imm)
END DO
! force array for mm atoms
ALLOCATE (Forces_MM(3, nmm))
Forces_MM = 0.0_dp
IF (ewald_type == do_ewald_ewald) THEN
CPABORT("Ewald not implemented for DFTB/QMMM")
ELSE IF (ewald_type == do_ewald_spme) THEN
! spme electrostatic potential
CALL spme_potential(ewald_env, ewald_pw, mm_cell, atoms_mm, charges_mm, &
particles_qm, qpot)
! forces QM
CALL spme_forces(ewald_env, ewald_pw, mm_cell, atoms_mm, charges_mm, &
particles_qm, mcharge, Forces_QM)
! forces MM
CALL spme_forces(ewald_env, ewald_pw, mm_cell, particles_qm, mcharge, &
atoms_mm, charges_mm, Forces_MM)
END IF
CALL deallocate_particle_set(atoms_mm)
DEALLOCATE (charges_mm)
! transfer MM forces
CALL para_env%sum(Forces_MM)
DO Imp = 1, nmm
Imm = Pot%mm_atom_index(Imp)
Forces(:, Imm) = Forces(:, Imm) - Forces_MM(:, Imp)
END DO
DEALLOCATE (Forces_MM)
END DO
IF (qmmm_env%move_mm_charges .OR. qmmm_env%add_mm_charges) THEN
DO ipot = 1, SIZE(qmmm_env%added_charges%Potentials)
Pot => qmmm_env%added_charges%Potentials(ipot)%Pot
nmm = SIZE(Pot%mm_atom_index)
! get a 'clean' mm particle set