-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_resp.F
1791 lines (1586 loc) · 85.1 KB
/
qs_resp.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 provides a resp fit for gas phase systems
!> \par History
!> created
!> Dorothea Golze [06.2012] (1) extension to periodic systems
!> (2) re-structured the code
!> \author Joost VandeVondele (02.2007)
! **************************************************************************************************
MODULE qs_resp
USE atomic_charges, ONLY: print_atomic_charges
USE atomic_kind_types, ONLY: atomic_kind_type,&
get_atomic_kind
USE bibliography, ONLY: Campana2009,&
Golze2015,&
Rappe1992,&
cite_reference
USE cell_types, ONLY: cell_type,&
get_cell,&
pbc,&
use_perd_none,&
use_perd_xyz
USE cp_control_types, ONLY: dft_control_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_type
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_generate_filename,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE cp_realspace_grid_cube, ONLY: cp_pw_to_cube
USE cp_units, ONLY: cp_unit_from_cp2k,&
cp_unit_to_cp2k
USE input_constants, ONLY: do_resp_minus_x_dir,&
do_resp_minus_y_dir,&
do_resp_minus_z_dir,&
do_resp_x_dir,&
do_resp_y_dir,&
do_resp_z_dir,&
use_cambridge_vdw_radii,&
use_uff_vdw_radii
USE input_section_types, ONLY: section_get_ivals,&
section_get_lval,&
section_vals_get,&
section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kahan_sum, ONLY: accurate_sum
USE kinds, ONLY: default_path_length,&
default_string_length,&
dp
USE machine, ONLY: m_flush
USE mathconstants, ONLY: pi
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: mp_para_env_type,&
mp_request_type
USE particle_list_types, ONLY: particle_list_type
USE particle_types, ONLY: particle_type
USE periodic_table, ONLY: get_ptable_info
USE pw_env_types, ONLY: pw_env_get,&
pw_env_type
USE pw_methods, ONLY: pw_copy,&
pw_scale,&
pw_transfer,&
pw_zero
USE pw_poisson_methods, ONLY: pw_poisson_solve
USE pw_poisson_types, ONLY: pw_poisson_type
USE pw_pool_types, ONLY: pw_pool_type
USE pw_types, ONLY: pw_c1d_gs_type,&
pw_r3d_rs_type
USE qs_collocate_density, ONLY: calculate_rho_resp_all,&
calculate_rho_resp_single
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type,&
set_qs_env
USE qs_kind_types, ONLY: qs_kind_type
USE qs_subsys_types, ONLY: qs_subsys_get,&
qs_subsys_type
USE uff_vdw_radii_table, ONLY: get_uff_vdw_radius
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
! *** Global parameters ***
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_resp'
PUBLIC :: resp_fit
TYPE resp_type
LOGICAL :: equal_charges = .FALSE., itc = .FALSE., &
molecular_sys = .FALSE., rheavies = .FALSE., &
use_repeat_method = .FALSE.
INTEGER :: nres = -1, ncons = -1, &
nrest_sec = -1, ncons_sec = -1, &
npoints = -1, stride(3) = -1, my_fit = -1, &
npoints_proc = -1, &
auto_vdw_radii_table = -1
INTEGER, DIMENSION(:), POINTER :: atom_surf_list => NULL()
INTEGER, DIMENSION(:, :), POINTER :: fitpoints => NULL()
REAL(KIND=dp) :: rheavies_strength = -1.0_dp, &
length = -1.0_dp, eta = -1.0_dp, &
sum_vhartree = -1.0_dp, offset = -1.0_dp
REAL(KIND=dp), DIMENSION(3) :: box_hi = -1.0_dp, box_low = -1.0_dp
REAL(KIND=dp), DIMENSION(:), POINTER :: rmin_kind => NULL(), &
rmax_kind => NULL()
REAL(KIND=dp), DIMENSION(:), POINTER :: range_surf => NULL()
REAL(KIND=dp), DIMENSION(:), POINTER :: rhs => NULL()
REAL(KIND=dp), DIMENSION(:), POINTER :: sum_vpot => NULL()
REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix => NULL()
END TYPE resp_type
TYPE resp_p_type
TYPE(resp_type), POINTER :: p_resp => NULL()
END TYPE resp_p_type
CONTAINS
! **************************************************************************************************
!> \brief performs resp fit and generates RESP charges
!> \param qs_env the qs environment
! **************************************************************************************************
SUBROUTINE resp_fit(qs_env)
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'resp_fit'
INTEGER :: handle, info, my_per, natom, nvar, &
output_unit
INTEGER, ALLOCATABLE, DIMENSION(:) :: ipiv
LOGICAL :: has_resp
REAL(KIND=dp), DIMENSION(:), POINTER :: rhs_to_save
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(cp_logger_type), POINTER :: logger
TYPE(dft_control_type), POINTER :: dft_control
TYPE(particle_list_type), POINTER :: particles
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_subsys_type), POINTER :: subsys
TYPE(resp_p_type), DIMENSION(:), POINTER :: rep_sys
TYPE(resp_type), POINTER :: resp_env
TYPE(section_vals_type), POINTER :: cons_section, input, poisson_section, &
resp_section, rest_section
CALL timeset(routineN, handle)
NULLIFY (logger, atomic_kind_set, cell, subsys, particles, particle_set, input, &
resp_section, cons_section, rest_section, poisson_section, resp_env, rep_sys)
CPASSERT(ASSOCIATED(qs_env))
CALL get_qs_env(qs_env, atomic_kind_set=atomic_kind_set, input=input, &
subsys=subsys, particle_set=particle_set, cell=cell)
resp_section => section_vals_get_subs_vals(input, "PROPERTIES%RESP")
CALL section_vals_get(resp_section, explicit=has_resp)
IF (has_resp) THEN
logger => cp_get_default_logger()
poisson_section => section_vals_get_subs_vals(input, "DFT%POISSON")
CALL section_vals_val_get(poisson_section, "PERIODIC", i_val=my_per)
CALL create_resp_type(resp_env, rep_sys)
!initialize the RESP fitting, get all the keywords
CALL init_resp(resp_env, rep_sys, subsys, atomic_kind_set, &
cell, resp_section, cons_section, rest_section)
!print info
CALL print_resp_parameter_info(qs_env, resp_env, rep_sys, my_per)
CALL qs_subsys_get(subsys, particles=particles)
natom = particles%n_els
nvar = natom + resp_env%ncons
CALL resp_allocate(resp_env, natom, nvar)
ALLOCATE (ipiv(nvar))
ipiv = 0
! calculate the matrix and the vector rhs
SELECT CASE (my_per)
CASE (use_perd_none)
CALL calc_resp_matrix_nonper(qs_env, resp_env, atomic_kind_set, particles, cell, &
resp_env%matrix, resp_env%rhs, natom)
CASE (use_perd_xyz)
CALL cite_reference(Golze2015)
IF (resp_env%use_repeat_method) CALL cite_reference(Campana2009)
CALL calc_resp_matrix_periodic(qs_env, resp_env, rep_sys, particles, cell, natom)
CASE DEFAULT
CALL cp_abort(__LOCATION__, &
"RESP charges only implemented for nonperiodic systems"// &
" or XYZ periodicity!")
END SELECT
output_unit = cp_print_key_unit_nr(logger, resp_section, "PRINT%PROGRAM_RUN_INFO", &
extension=".resp")
IF (output_unit > 0) THEN
WRITE (output_unit, '(T3,A,T69,I12)') "Number of fitting points "// &
"found: ", resp_env%npoints
WRITE (output_unit, '()')
END IF
!adding restraints and constraints
CALL add_restraints_and_constraints(qs_env, resp_env, rest_section, &
subsys, natom, cons_section, particle_set)
!solve system for the values of the charges and the lagrangian multipliers
CALL DGETRF(nvar, nvar, resp_env%matrix, nvar, ipiv, info)
CPASSERT(info == 0)
CALL DGETRS('N', nvar, 1, resp_env%matrix, nvar, ipiv, resp_env%rhs, nvar, info)
CPASSERT(info == 0)
IF (resp_env%use_repeat_method) resp_env%offset = resp_env%rhs(natom + 1)
CALL print_resp_charges(qs_env, resp_env, output_unit, natom)
CALL print_fitting_points(qs_env, resp_env)
CALL print_pot_from_resp_charges(qs_env, resp_env, particles, natom, output_unit)
! In case of density functional embedding we need to save the charges to qs_env
NULLIFY (dft_control)
CALL get_qs_env(qs_env, dft_control=dft_control)
IF (dft_control%qs_control%ref_embed_subsys) THEN
ALLOCATE (rhs_to_save(SIZE(resp_env%rhs)))
rhs_to_save = resp_env%rhs
CALL set_qs_env(qs_env, rhs=rhs_to_save)
END IF
DEALLOCATE (ipiv)
CALL resp_dealloc(resp_env, rep_sys)
CALL cp_print_key_finished_output(output_unit, logger, resp_section, &
"PRINT%PROGRAM_RUN_INFO")
END IF
CALL timestop(handle)
END SUBROUTINE resp_fit
! **************************************************************************************************
!> \brief creates the resp_type structure
!> \param resp_env the resp environment
!> \param rep_sys structure for repeating input sections defining fit points
! **************************************************************************************************
SUBROUTINE create_resp_type(resp_env, rep_sys)
TYPE(resp_type), POINTER :: resp_env
TYPE(resp_p_type), DIMENSION(:), POINTER :: rep_sys
IF (ASSOCIATED(resp_env)) CALL resp_dealloc(resp_env, rep_sys)
ALLOCATE (resp_env)
NULLIFY (resp_env%matrix, &
resp_env%fitpoints, &
resp_env%rmin_kind, &
resp_env%rmax_kind, &
resp_env%rhs, &
resp_env%sum_vpot)
resp_env%equal_charges = .FALSE.
resp_env%itc = .FALSE.
resp_env%molecular_sys = .FALSE.
resp_env%rheavies = .FALSE.
resp_env%use_repeat_method = .FALSE.
resp_env%box_hi = 0.0_dp
resp_env%box_low = 0.0_dp
resp_env%ncons = 0
resp_env%ncons_sec = 0
resp_env%nres = 0
resp_env%nrest_sec = 0
resp_env%npoints = 0
resp_env%npoints_proc = 0
resp_env%auto_vdw_radii_table = use_cambridge_vdw_radii
END SUBROUTINE create_resp_type
! **************************************************************************************************
!> \brief allocates the resp
!> \param resp_env the resp environment
!> \param natom ...
!> \param nvar ...
! **************************************************************************************************
SUBROUTINE resp_allocate(resp_env, natom, nvar)
TYPE(resp_type), POINTER :: resp_env
INTEGER, INTENT(IN) :: natom, nvar
IF (.NOT. ASSOCIATED(resp_env%matrix)) THEN
ALLOCATE (resp_env%matrix(nvar, nvar))
END IF
IF (.NOT. ASSOCIATED(resp_env%rhs)) THEN
ALLOCATE (resp_env%rhs(nvar))
END IF
IF (.NOT. ASSOCIATED(resp_env%sum_vpot)) THEN
ALLOCATE (resp_env%sum_vpot(natom))
END IF
resp_env%matrix = 0.0_dp
resp_env%rhs = 0.0_dp
resp_env%sum_vpot = 0.0_dp
END SUBROUTINE resp_allocate
! **************************************************************************************************
!> \brief deallocates the resp_type structure
!> \param resp_env the resp environment
!> \param rep_sys structure for repeating input sections defining fit points
! **************************************************************************************************
SUBROUTINE resp_dealloc(resp_env, rep_sys)
TYPE(resp_type), POINTER :: resp_env
TYPE(resp_p_type), DIMENSION(:), POINTER :: rep_sys
INTEGER :: i
IF (ASSOCIATED(resp_env)) THEN
IF (ASSOCIATED(resp_env%matrix)) THEN
DEALLOCATE (resp_env%matrix)
END IF
IF (ASSOCIATED(resp_env%rhs)) THEN
DEALLOCATE (resp_env%rhs)
END IF
IF (ASSOCIATED(resp_env%sum_vpot)) THEN
DEALLOCATE (resp_env%sum_vpot)
END IF
IF (ASSOCIATED(resp_env%fitpoints)) THEN
DEALLOCATE (resp_env%fitpoints)
END IF
IF (ASSOCIATED(resp_env%rmin_kind)) THEN
DEALLOCATE (resp_env%rmin_kind)
END IF
IF (ASSOCIATED(resp_env%rmax_kind)) THEN
DEALLOCATE (resp_env%rmax_kind)
END IF
DEALLOCATE (resp_env)
END IF
IF (ASSOCIATED(rep_sys)) THEN
DO i = 1, SIZE(rep_sys)
DEALLOCATE (rep_sys(i)%p_resp%atom_surf_list)
DEALLOCATE (rep_sys(i)%p_resp)
END DO
DEALLOCATE (rep_sys)
END IF
END SUBROUTINE resp_dealloc
! **************************************************************************************************
!> \brief initializes the resp fit. Getting the parameters
!> \param resp_env the resp environment
!> \param rep_sys structure for repeating input sections defining fit points
!> \param subsys ...
!> \param atomic_kind_set ...
!> \param cell parameters related to the simulation cell
!> \param resp_section resp section
!> \param cons_section constraints section, part of resp section
!> \param rest_section restraints section, part of resp section
! **************************************************************************************************
SUBROUTINE init_resp(resp_env, rep_sys, subsys, atomic_kind_set, &
cell, resp_section, cons_section, rest_section)
TYPE(resp_type), POINTER :: resp_env
TYPE(resp_p_type), DIMENSION(:), POINTER :: rep_sys
TYPE(qs_subsys_type), POINTER :: subsys
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(cell_type), POINTER :: cell
TYPE(section_vals_type), POINTER :: resp_section, cons_section, rest_section
CHARACTER(len=*), PARAMETER :: routineN = 'init_resp'
INTEGER :: handle, i, nrep
INTEGER, DIMENSION(:), POINTER :: atom_list_cons, my_stride
LOGICAL :: explicit
TYPE(section_vals_type), POINTER :: slab_section, sphere_section
CALL timeset(routineN, handle)
NULLIFY (atom_list_cons, my_stride, sphere_section, slab_section)
! get the subsections
sphere_section => section_vals_get_subs_vals(resp_section, "SPHERE_SAMPLING")
slab_section => section_vals_get_subs_vals(resp_section, "SLAB_SAMPLING")
cons_section => section_vals_get_subs_vals(resp_section, "CONSTRAINT")
rest_section => section_vals_get_subs_vals(resp_section, "RESTRAINT")
! get the general keywords
CALL section_vals_val_get(resp_section, "INTEGER_TOTAL_CHARGE", &
l_val=resp_env%itc)
IF (resp_env%itc) resp_env%ncons = resp_env%ncons + 1
CALL section_vals_val_get(resp_section, "RESTRAIN_HEAVIES_TO_ZERO", &
l_val=resp_env%rheavies)
IF (resp_env%rheavies) THEN
CALL section_vals_val_get(resp_section, "RESTRAIN_HEAVIES_STRENGTH", &
r_val=resp_env%rheavies_strength)
END IF
CALL section_vals_val_get(resp_section, "STRIDE", i_vals=my_stride)
IF (SIZE(my_stride) /= 1 .AND. SIZE(my_stride) /= 3) &
CALL cp_abort(__LOCATION__, "STRIDE keyword can accept only 1 (the same for X,Y,Z) "// &
"or 3 values. Correct your input file.")
IF (SIZE(my_stride) == 1) THEN
DO i = 1, 3
resp_env%stride(i) = my_stride(1)
END DO
ELSE
resp_env%stride = my_stride(1:3)
END IF
CALL section_vals_val_get(resp_section, "WIDTH", r_val=resp_env%eta)
! get if the user wants to use REPEAT method
CALL section_vals_val_get(resp_section, "USE_REPEAT_METHOD", &
l_val=resp_env%use_repeat_method)
IF (resp_env%use_repeat_method) THEN
resp_env%ncons = resp_env%ncons + 1
! restrain heavies should be off
resp_env%rheavies = .FALSE.
END IF
! get and set the parameters for molecular (non-surface) systems
! this must come after the repeat settings being set
CALL get_parameter_molecular_sys(resp_env, sphere_section, cell, &
atomic_kind_set)
! get the parameter for periodic/surface systems
CALL section_vals_get(slab_section, explicit=explicit, n_repetition=nrep)
IF (explicit) THEN
IF (resp_env%molecular_sys) THEN
CALL cp_abort(__LOCATION__, &
"You can only use either SPHERE_SAMPLING or SLAB_SAMPLING, but "// &
"not both.")
END IF
ALLOCATE (rep_sys(nrep))
DO i = 1, nrep
ALLOCATE (rep_sys(i)%p_resp)
NULLIFY (rep_sys(i)%p_resp%range_surf, rep_sys(i)%p_resp%atom_surf_list)
CALL section_vals_val_get(slab_section, "RANGE", r_vals=rep_sys(i)%p_resp%range_surf, &
i_rep_section=i)
CALL section_vals_val_get(slab_section, "LENGTH", r_val=rep_sys(i)%p_resp%length, &
i_rep_section=i)
CALL section_vals_val_get(slab_section, "SURF_DIRECTION", &
i_rep_section=i, i_val=rep_sys(i)%p_resp%my_fit)
IF (ANY(rep_sys(i)%p_resp%range_surf < 0.0_dp)) THEN
CPABORT("Numbers in RANGE in SLAB_SAMPLING cannot be negative.")
END IF
IF (rep_sys(i)%p_resp%length <= EPSILON(0.0_dp)) THEN
CPABORT("Parameter LENGTH in SLAB_SAMPLING has to be larger than zero.")
END IF
!list of atoms specifying the surface
CALL build_atom_list(slab_section, subsys, rep_sys(i)%p_resp%atom_surf_list, rep=i)
END DO
END IF
! get the parameters for the constraint and restraint sections
CALL section_vals_get(cons_section, explicit=explicit)
IF (explicit) THEN
CALL section_vals_get(cons_section, n_repetition=resp_env%ncons_sec)
DO i = 1, resp_env%ncons_sec
CALL section_vals_val_get(cons_section, "EQUAL_CHARGES", &
l_val=resp_env%equal_charges, explicit=explicit)
IF (.NOT. explicit) CYCLE
CALL build_atom_list(cons_section, subsys, atom_list_cons, i)
!instead of using EQUAL_CHARGES the constraint sections could be repeated
resp_env%ncons = resp_env%ncons + SIZE(atom_list_cons) - 2
DEALLOCATE (atom_list_cons)
END DO
END IF
CALL section_vals_get(rest_section, explicit=explicit)
IF (explicit) THEN
CALL section_vals_get(rest_section, n_repetition=resp_env%nrest_sec)
END IF
resp_env%ncons = resp_env%ncons + resp_env%ncons_sec
resp_env%nres = resp_env%nres + resp_env%nrest_sec
CALL timestop(handle)
END SUBROUTINE init_resp
! **************************************************************************************************
!> \brief getting the parameters for nonperiodic/non-surface systems
!> \param resp_env the resp environment
!> \param sphere_section input section setting parameters for sampling
!> fitting in spheres around the atom
!> \param cell parameters related to the simulation cell
!> \param atomic_kind_set ...
! **************************************************************************************************
SUBROUTINE get_parameter_molecular_sys(resp_env, sphere_section, cell, &
atomic_kind_set)
TYPE(resp_type), POINTER :: resp_env
TYPE(section_vals_type), POINTER :: sphere_section
TYPE(cell_type), POINTER :: cell
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
CHARACTER(LEN=2) :: symbol
CHARACTER(LEN=default_string_length) :: missing_rmax, missing_rmin
CHARACTER(LEN=default_string_length), &
DIMENSION(:), POINTER :: tmpstringlist
INTEGER :: ikind, j, kind_number, n_rmax_missing, &
n_rmin_missing, nkind, nrep_rmax, &
nrep_rmin, z
LOGICAL :: explicit, has_rmax, has_rmin
LOGICAL, ALLOCATABLE, DIMENSION(:) :: rmax_is_set, rmin_is_set
REAL(KIND=dp) :: auto_rmax_scale, auto_rmin_scale, rmax, &
rmin
REAL(KIND=dp), DIMENSION(3, 3) :: hmat
TYPE(atomic_kind_type), POINTER :: atomic_kind
nrep_rmin = 0
nrep_rmax = 0
nkind = SIZE(atomic_kind_set)
has_rmin = .FALSE.
has_rmax = .FALSE.
CALL section_vals_get(sphere_section, explicit=explicit)
IF (explicit) THEN
resp_env%molecular_sys = .TRUE.
CALL section_vals_val_get(sphere_section, "AUTO_VDW_RADII_TABLE", &
i_val=resp_env%auto_vdw_radii_table)
CALL section_vals_val_get(sphere_section, "AUTO_RMIN_SCALE", r_val=auto_rmin_scale)
CALL section_vals_val_get(sphere_section, "AUTO_RMAX_SCALE", r_val=auto_rmax_scale)
CALL section_vals_val_get(sphere_section, "RMIN", explicit=has_rmin, r_val=rmin)
CALL section_vals_val_get(sphere_section, "RMAX", explicit=has_rmax, r_val=rmax)
CALL section_vals_val_get(sphere_section, "RMIN_KIND", n_rep_val=nrep_rmin)
CALL section_vals_val_get(sphere_section, "RMAX_KIND", n_rep_val=nrep_rmax)
ALLOCATE (resp_env%rmin_kind(nkind))
ALLOCATE (resp_env%rmax_kind(nkind))
resp_env%rmin_kind = 0.0_dp
resp_env%rmax_kind = 0.0_dp
ALLOCATE (rmin_is_set(nkind))
ALLOCATE (rmax_is_set(nkind))
rmin_is_set = .FALSE.
rmax_is_set = .FALSE.
! define rmin_kind and rmax_kind to predefined vdW radii
DO ikind = 1, nkind
atomic_kind => atomic_kind_set(ikind)
CALL get_atomic_kind(atomic_kind, &
element_symbol=symbol, &
kind_number=kind_number, &
z=z)
SELECT CASE (resp_env%auto_vdw_radii_table)
CASE (use_cambridge_vdw_radii)
CALL get_ptable_info(symbol, vdw_radius=resp_env%rmin_kind(kind_number))
rmin_is_set(kind_number) = .TRUE.
CASE (use_uff_vdw_radii)
CALL cite_reference(Rappe1992)
CALL get_uff_vdw_radius(z, radius=resp_env%rmin_kind(kind_number), &
found=rmin_is_set(kind_number))
CASE DEFAULT
CALL get_ptable_info(symbol, vdw_radius=resp_env%rmin_kind(kind_number))
rmin_is_set(kind_number) = .TRUE.
END SELECT
IF (rmin_is_set(kind_number)) THEN
resp_env%rmin_kind(kind_number) = cp_unit_to_cp2k(resp_env%rmin_kind(kind_number), &
"angstrom")
resp_env%rmin_kind(kind_number) = resp_env%rmin_kind(kind_number)*auto_rmin_scale
! set RMAX_KIND accourding by scaling RMIN_KIND
resp_env%rmax_kind(kind_number) = &
MAX(resp_env%rmin_kind(kind_number), &
resp_env%rmin_kind(kind_number)*auto_rmax_scale)
rmax_is_set(kind_number) = .TRUE.
END IF
END DO
! if RMIN or RMAX are present, overwrite the rmin_kind(:) and
! rmax_kind(:) to those values
IF (has_rmin) THEN
resp_env%rmin_kind = rmin
rmin_is_set = .TRUE.
END IF
IF (has_rmax) THEN
resp_env%rmax_kind = rmax
rmax_is_set = .TRUE.
END IF
! if RMIN_KIND's or RMAX_KIND's are present, overwrite the
! rmin_kinds(:) or rmax_kind(:) to those values
DO j = 1, nrep_rmin
CALL section_vals_val_get(sphere_section, "RMIN_KIND", i_rep_val=j, &
c_vals=tmpstringlist)
DO ikind = 1, nkind
atomic_kind => atomic_kind_set(ikind)
CALL get_atomic_kind(atomic_kind, element_symbol=symbol, kind_number=kind_number)
IF (TRIM(tmpstringlist(2)) == TRIM(symbol)) THEN
READ (tmpstringlist(1), *) resp_env%rmin_kind(kind_number)
resp_env%rmin_kind(kind_number) = &
cp_unit_to_cp2k(resp_env%rmin_kind(kind_number), &
"angstrom")
rmin_is_set(kind_number) = .TRUE.
END IF
END DO
END DO
DO j = 1, nrep_rmax
CALL section_vals_val_get(sphere_section, "RMAX_KIND", i_rep_val=j, &
c_vals=tmpstringlist)
DO ikind = 1, nkind
atomic_kind => atomic_kind_set(ikind)
CALL get_atomic_kind(atomic_kind, element_symbol=symbol, kind_number=kind_number)
IF (TRIM(tmpstringlist(2)) == TRIM(symbol)) THEN
READ (tmpstringlist(1), *) resp_env%rmax_kind(kind_number)
resp_env%rmax_kind(kind_number) = cp_unit_to_cp2k(resp_env%rmax_kind(kind_number), &
"angstrom")
rmax_is_set(kind_number) = .TRUE.
END IF
END DO
END DO
! check if rmin and rmax are set for each kind
n_rmin_missing = 0
n_rmax_missing = 0
missing_rmin = ""
missing_rmax = ""
DO ikind = 1, nkind
atomic_kind => atomic_kind_set(ikind)
CALL get_atomic_kind(atomic_kind, &
element_symbol=symbol, &
kind_number=kind_number)
IF (.NOT. rmin_is_set(kind_number)) THEN
n_rmin_missing = n_rmin_missing + 1
missing_rmin = TRIM(missing_rmin)//" "//TRIM(symbol)//","
END IF
IF (.NOT. rmax_is_set(kind_number)) THEN
n_rmax_missing = n_rmax_missing + 1
missing_rmax = TRIM(missing_rmax)//" "//TRIM(symbol)//","
END IF
END DO
IF (n_rmin_missing > 0) THEN
CALL cp_warn(__LOCATION__, &
"RMIN for the following elements are missing: "// &
TRIM(missing_rmin)// &
" please set these values manually using "// &
"RMIN_KIND in SPHERE_SAMPLING section")
END IF
IF (n_rmax_missing > 0) THEN
CALL cp_warn(__LOCATION__, &
"RMAX for the following elements are missing: "// &
TRIM(missing_rmax)// &
" please set these values manually using "// &
"RMAX_KIND in SPHERE_SAMPLING section")
END IF
IF (n_rmin_missing > 0 .OR. &
n_rmax_missing > 0) THEN
CPABORT("Insufficient data for RMIN or RMAX")
END IF
CALL get_cell(cell=cell, h=hmat)
resp_env%box_hi = (/hmat(1, 1), hmat(2, 2), hmat(3, 3)/)
resp_env%box_low = 0.0_dp
CALL section_vals_val_get(sphere_section, "X_HI", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "X_HI", &
r_val=resp_env%box_hi(1))
CALL section_vals_val_get(sphere_section, "X_LOW", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "X_LOW", &
r_val=resp_env%box_low(1))
CALL section_vals_val_get(sphere_section, "Y_HI", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "Y_HI", &
r_val=resp_env%box_hi(2))
CALL section_vals_val_get(sphere_section, "Y_LOW", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "Y_LOW", &
r_val=resp_env%box_low(2))
CALL section_vals_val_get(sphere_section, "Z_HI", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "Z_HI", &
r_val=resp_env%box_hi(3))
CALL section_vals_val_get(sphere_section, "Z_LOW", explicit=explicit)
IF (explicit) CALL section_vals_val_get(sphere_section, "Z_LOW", &
r_val=resp_env%box_low(3))
DEALLOCATE (rmin_is_set)
DEALLOCATE (rmax_is_set)
END IF
END SUBROUTINE get_parameter_molecular_sys
! **************************************************************************************************
!> \brief building atom lists for different sections of RESP
!> \param section input section
!> \param subsys ...
!> \param atom_list list of atoms for restraints, constraints and fit point
!> sampling for slab-like systems
!> \param rep input section can be repeated, this param defines for which
!> repetition of the input section the atom_list is built
! **************************************************************************************************
SUBROUTINE build_atom_list(section, subsys, atom_list, rep)
TYPE(section_vals_type), POINTER :: section
TYPE(qs_subsys_type), POINTER :: subsys
INTEGER, DIMENSION(:), POINTER :: atom_list
INTEGER, INTENT(IN), OPTIONAL :: rep
CHARACTER(len=*), PARAMETER :: routineN = 'build_atom_list'
INTEGER :: atom_a, atom_b, handle, i, irep, j, &
max_index, n_var, num_atom
INTEGER, DIMENSION(:), POINTER :: indexes
LOGICAL :: index_in_range
CALL timeset(routineN, handle)
NULLIFY (indexes)
irep = 1
IF (PRESENT(rep)) irep = rep
CALL section_vals_val_get(section, "ATOM_LIST", i_rep_section=irep, &
n_rep_val=n_var)
num_atom = 0
DO i = 1, n_var
CALL section_vals_val_get(section, "ATOM_LIST", i_rep_section=irep, &
i_rep_val=i, i_vals=indexes)
num_atom = num_atom + SIZE(indexes)
END DO
ALLOCATE (atom_list(num_atom))
atom_list = 0
num_atom = 1
DO i = 1, n_var
CALL section_vals_val_get(section, "ATOM_LIST", i_rep_section=irep, &
i_rep_val=i, i_vals=indexes)
atom_list(num_atom:num_atom + SIZE(indexes) - 1) = indexes(:)
num_atom = num_atom + SIZE(indexes)
END DO
!check atom list
num_atom = num_atom - 1
CALL qs_subsys_get(subsys, nparticle=max_index)
CPASSERT(SIZE(atom_list) /= 0)
index_in_range = (MAXVAL(atom_list) <= max_index) &
.AND. (MINVAL(atom_list) > 0)
CPASSERT(index_in_range)
DO i = 1, num_atom
DO j = i + 1, num_atom
atom_a = atom_list(i)
atom_b = atom_list(j)
IF (atom_a == atom_b) &
CPABORT("There are atoms doubled in atom list for RESP.")
END DO
END DO
CALL timestop(handle)
END SUBROUTINE build_atom_list
! **************************************************************************************************
!> \brief build matrix and vector for nonperiodic RESP fitting
!> \param qs_env the qs environment
!> \param resp_env the resp environment
!> \param atomic_kind_set ...
!> \param particles ...
!> \param cell parameters related to the simulation cell
!> \param matrix coefficient matrix of the linear system of equations
!> \param rhs vector of the linear system of equations
!> \param natom number of atoms
! **************************************************************************************************
SUBROUTINE calc_resp_matrix_nonper(qs_env, resp_env, atomic_kind_set, particles, &
cell, matrix, rhs, natom)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(resp_type), POINTER :: resp_env
TYPE(atomic_kind_type), DIMENSION(:), POINTER :: atomic_kind_set
TYPE(particle_list_type), POINTER :: particles
TYPE(cell_type), POINTER :: cell
REAL(KIND=dp), DIMENSION(:, :), POINTER :: matrix
REAL(KIND=dp), DIMENSION(:), POINTER :: rhs
INTEGER, INTENT(IN) :: natom
CHARACTER(len=*), PARAMETER :: routineN = 'calc_resp_matrix_nonper'
INTEGER :: bo(2, 3), gbo(2, 3), handle, i, ikind, &
jx, jy, jz, k, kind_number, l, m, &
nkind, now, np(3), p
LOGICAL, ALLOCATABLE, DIMENSION(:, :) :: not_in_range
REAL(KIND=dp) :: delta, dh(3, 3), dvol, r(3), rmax, rmin, &
vec(3), vec_pbc(3), vj
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: dist
REAL(KIND=dp), DIMENSION(3, 3) :: hmat, hmat_inv
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(pw_r3d_rs_type), POINTER :: v_hartree_pw
CALL timeset(routineN, handle)
NULLIFY (particle_set, v_hartree_pw)
delta = 1.0E-13_dp
CALL get_cell(cell=cell, h=hmat, h_inv=hmat_inv)
IF (.NOT. cell%orthorhombic) THEN
CALL cp_abort(__LOCATION__, &
"Nonperiodic solution for RESP charges only"// &
" implemented for orthorhombic cells!")
END IF
IF (.NOT. resp_env%molecular_sys) THEN
CALL cp_abort(__LOCATION__, &
"Nonperiodic solution for RESP charges (i.e. nonperiodic"// &
" Poisson solver) can only be used with section SPHERE_SAMPLING")
END IF
IF (resp_env%use_repeat_method) THEN
CALL cp_abort(__LOCATION__, &
"REPEAT method only reasonable for periodic RESP fitting")
END IF
CALL get_qs_env(qs_env, particle_set=particle_set, v_hartree_rspace=v_hartree_pw)
bo = v_hartree_pw%pw_grid%bounds_local
gbo = v_hartree_pw%pw_grid%bounds
np = v_hartree_pw%pw_grid%npts
dh = v_hartree_pw%pw_grid%dh
dvol = v_hartree_pw%pw_grid%dvol
nkind = SIZE(atomic_kind_set)
ALLOCATE (dist(natom))
ALLOCATE (not_in_range(natom, 2))
! store fitting points to calculate the RMS and RRMS later
IF (.NOT. ASSOCIATED(resp_env%fitpoints)) THEN
now = 1000
ALLOCATE (resp_env%fitpoints(3, now))
ELSE
now = SIZE(resp_env%fitpoints, 2)
END IF
DO jz = bo(1, 3), bo(2, 3)
DO jy = bo(1, 2), bo(2, 2)
DO jx = bo(1, 1), bo(2, 1)
IF (.NOT. (MODULO(jz, resp_env%stride(3)) == 0)) CYCLE
IF (.NOT. (MODULO(jy, resp_env%stride(2)) == 0)) CYCLE
IF (.NOT. (MODULO(jx, resp_env%stride(1)) == 0)) CYCLE
!bounds bo reach from -np/2 to np/2. shift of np/2 so that r(1,1,1)=(0,0,0)
l = jx - gbo(1, 1)
k = jy - gbo(1, 2)
p = jz - gbo(1, 3)
r(3) = p*dh(3, 3) + k*dh(3, 2) + l*dh(3, 1)
r(2) = p*dh(2, 3) + k*dh(2, 2) + l*dh(2, 1)
r(1) = p*dh(1, 3) + k*dh(1, 2) + l*dh(1, 1)
IF (r(3) < resp_env%box_low(3) .OR. r(3) > resp_env%box_hi(3)) CYCLE
IF (r(2) < resp_env%box_low(2) .OR. r(2) > resp_env%box_hi(2)) CYCLE
IF (r(1) < resp_env%box_low(1) .OR. r(1) > resp_env%box_hi(1)) CYCLE
! compute distance from the grid point to all atoms
not_in_range = .FALSE.
DO i = 1, natom
vec = r - particles%els(i)%r
vec_pbc(1) = vec(1) - hmat(1, 1)*ANINT(hmat_inv(1, 1)*vec(1))
vec_pbc(2) = vec(2) - hmat(2, 2)*ANINT(hmat_inv(2, 2)*vec(2))
vec_pbc(3) = vec(3) - hmat(3, 3)*ANINT(hmat_inv(3, 3)*vec(3))
dist(i) = SQRT(SUM(vec_pbc**2))
CALL get_atomic_kind(atomic_kind=particle_set(i)%atomic_kind, &
kind_number=kind_number)
DO ikind = 1, nkind
IF (ikind == kind_number) THEN
rmin = resp_env%rmin_kind(ikind)
rmax = resp_env%rmax_kind(ikind)
EXIT
END IF
END DO
IF (dist(i) < rmin + delta) not_in_range(i, 1) = .TRUE.
IF (dist(i) > rmax - delta) not_in_range(i, 2) = .TRUE.
END DO
! check if the point is sufficiently close and far. if OK, we can use
! the point for fitting, add/subtract 1.0E-13 to get rid of rounding errors when shifting atoms
IF (ANY(not_in_range(:, 1)) .OR. ALL(not_in_range(:, 2))) CYCLE
resp_env%npoints_proc = resp_env%npoints_proc + 1
IF (resp_env%npoints_proc > now) THEN
now = 2*now
CALL reallocate(resp_env%fitpoints, 1, 3, 1, now)
END IF
resp_env%fitpoints(1, resp_env%npoints_proc) = jx
resp_env%fitpoints(2, resp_env%npoints_proc) = jy
resp_env%fitpoints(3, resp_env%npoints_proc) = jz
! correct for the fact that v_hartree is scaled by dvol, and has the opposite sign
IF (qs_env%qmmm) THEN
! If it's a QM/MM run let's remove the contribution of the MM potential out of the Hartree pot
vj = -v_hartree_pw%array(jx, jy, jz)/dvol + qs_env%ks_qmmm_env%v_qmmm_rspace%array(jx, jy, jz)
ELSE
vj = -v_hartree_pw%array(jx, jy, jz)/dvol
END IF
dist(:) = 1.0_dp/dist(:)
DO i = 1, natom
DO m = 1, natom
matrix(m, i) = matrix(m, i) + 2.0_dp*dist(i)*dist(m)
END DO
rhs(i) = rhs(i) + 2.0_dp*vj*dist(i)
END DO
END DO
END DO
END DO
resp_env%npoints = resp_env%npoints_proc
CALL v_hartree_pw%pw_grid%para%group%sum(resp_env%npoints)
CALL v_hartree_pw%pw_grid%para%group%sum(matrix)
CALL v_hartree_pw%pw_grid%para%group%sum(rhs)
!weighted sum
matrix = matrix/resp_env%npoints
rhs = rhs/resp_env%npoints
DEALLOCATE (dist)
DEALLOCATE (not_in_range)
CALL timestop(handle)
END SUBROUTINE calc_resp_matrix_nonper
! **************************************************************************************************
!> \brief build matrix and vector for periodic RESP fitting
!> \param qs_env the qs environment
!> \param resp_env the resp environment
!> \param rep_sys structure for repeating input sections defining fit points
!> \param particles ...
!> \param cell parameters related to the simulation cell
!> \param natom number of atoms
! **************************************************************************************************
SUBROUTINE calc_resp_matrix_periodic(qs_env, resp_env, rep_sys, particles, cell, &
natom)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(resp_type), POINTER :: resp_env
TYPE(resp_p_type), DIMENSION(:), POINTER :: rep_sys
TYPE(particle_list_type), POINTER :: particles
TYPE(cell_type), POINTER :: cell
INTEGER, INTENT(IN) :: natom
CHARACTER(len=*), PARAMETER :: routineN = 'calc_resp_matrix_periodic'
INTEGER :: handle, i, ip, j, jx, jy, jz
INTEGER, DIMENSION(3) :: periodic
REAL(KIND=dp) :: normalize_factor
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: vpot
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(pw_c1d_gs_type) :: rho_ga, va_gspace
TYPE(pw_env_type), POINTER :: pw_env
TYPE(pw_poisson_type), POINTER :: poisson_env
TYPE(pw_pool_type), POINTER :: auxbas_pw_pool
TYPE(pw_r3d_rs_type) :: va_rspace
CALL timeset(routineN, handle)
NULLIFY (pw_env, para_env, auxbas_pw_pool, poisson_env)
CALL get_cell(cell=cell, periodic=periodic)
IF (.NOT. ALL(periodic /= 0)) THEN
CALL cp_abort(__LOCATION__, &
"Periodic solution for RESP (with periodic Poisson solver)"// &
" can only be obtained with a cell that has XYZ periodicity")
END IF
CALL get_qs_env(qs_env, pw_env=pw_env, para_env=para_env)
CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool, &
poisson_env=poisson_env)
CALL auxbas_pw_pool%create_pw(rho_ga)
CALL auxbas_pw_pool%create_pw(va_gspace)
CALL auxbas_pw_pool%create_pw(va_rspace)
!get fitting points and store them in resp_env%fitpoints
CALL get_fitting_points(qs_env, resp_env, rep_sys, particles=particles, &
cell=cell)
ALLOCATE (vpot(resp_env%npoints_proc, natom))
normalize_factor = SQRT((resp_env%eta/pi)**3)
DO i = 1, natom
!collocate gaussian for each atom
CALL pw_zero(rho_ga)
CALL calculate_rho_resp_single(rho_ga, qs_env, resp_env%eta, i)
!calculate potential va and store the part needed for fitting in vpot
CALL pw_zero(va_gspace)
CALL pw_poisson_solve(poisson_env, rho_ga, vhartree=va_gspace)
CALL pw_zero(va_rspace)
CALL pw_transfer(va_gspace, va_rspace)
CALL pw_scale(va_rspace, normalize_factor)
DO ip = 1, resp_env%npoints_proc
jx = resp_env%fitpoints(1, ip)
jy = resp_env%fitpoints(2, ip)
jz = resp_env%fitpoints(3, ip)
vpot(ip, i) = va_rspace%array(jx, jy, jz)
END DO
END DO
CALL va_gspace%release()
CALL va_rspace%release()
CALL rho_ga%release()
DO i = 1, natom
DO j = 1, natom
! calculate matrix
resp_env%matrix(i, j) = resp_env%matrix(i, j) + 2.0_dp*SUM(vpot(:, i)*vpot(:, j))
END DO
! calculate vector resp_env%rhs
CALL calculate_rhs(qs_env, resp_env, resp_env%rhs(i), vpot(:, i))
END DO
CALL para_env%sum(resp_env%matrix)
CALL para_env%sum(resp_env%rhs)
!weighted sum
resp_env%matrix = resp_env%matrix/resp_env%npoints
resp_env%rhs = resp_env%rhs/resp_env%npoints
! REPEAT stuff
IF (resp_env%use_repeat_method) THEN
! sum over selected points of single Gaussian potential vpot
DO i = 1, natom
resp_env%sum_vpot(i) = 2.0_dp*accurate_sum(vpot(:, i))/resp_env%npoints
END DO
CALL para_env%sum(resp_env%sum_vpot)
CALL para_env%sum(resp_env%sum_vhartree)
resp_env%sum_vhartree = 2.0_dp*resp_env%sum_vhartree/resp_env%npoints
END IF