-
Notifications
You must be signed in to change notification settings - Fork 1
/
nnp_acsf.F
1414 lines (1270 loc) · 56.5 KB
/
nnp_acsf.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 Functionality for atom centered symmetry functions
!> for neural network potentials
!> \author Christoph Schran ([email protected])
!> \date 2020-10-10
! **************************************************************************************************
MODULE nnp_acsf
USE cell_types, ONLY: cell_type,&
pbc
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_unit_nr,&
cp_logger_type
USE kinds, ONLY: default_string_length,&
dp
USE mathconstants, ONLY: pi
USE message_passing, ONLY: mp_para_env_type
USE nnp_environment_types, ONLY: nnp_acsf_ang_type,&
nnp_acsf_rad_type,&
nnp_cut_cos,&
nnp_cut_tanh,&
nnp_env_get,&
nnp_neighbor_type,&
nnp_type
USE periodic_table, ONLY: get_ptable_info
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .TRUE.
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'nnp_acsf'
! Public subroutines ***
PUBLIC :: nnp_calc_acsf, &
nnp_init_acsf_groups, &
nnp_sort_acsf, &
nnp_sort_ele, &
nnp_write_acsf
CONTAINS
! **************************************************************************************************
!> \brief Calculate atom centered symmetry functions for given atom i
!> \param nnp ...
!> \param i ...
!> \param dsymdxyz ...
!> \param stress ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_calc_acsf(nnp, i, dsymdxyz, stress)
TYPE(nnp_type), INTENT(INOUT), POINTER :: nnp
INTEGER, INTENT(IN) :: i
REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT), &
OPTIONAL :: dsymdxyz, stress
CHARACTER(len=*), PARAMETER :: routineN = 'nnp_calc_acsf'
INTEGER :: handle, handle_sf, ind, j, jj, k, kk, l, &
m, off, s, sf
REAL(KIND=dp) :: r1, r2, r3
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: symtmp
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :) :: forcetmp
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :) :: force3tmp
REAL(KIND=dp), DIMENSION(3) :: rvect1, rvect2, rvect3
TYPE(nnp_neighbor_type) :: neighbor
CALL timeset(routineN, handle)
!determine index of atom type
ind = nnp%ele_ind(i)
! compute neighbors of atom i
CALL nnp_neighbor_create(nnp, ind, nnp%num_atoms, neighbor)
CALL nnp_compute_neighbors(nnp, neighbor, i)
! Reset y:
nnp%rad(ind)%y = 0.0_dp
nnp%ang(ind)%y = 0.0_dp
!calc forces
IF (PRESENT(dsymdxyz)) THEN
!loop over radial sym fnct grps
CALL timeset('nnp_acsf_radial', handle_sf)
DO s = 1, nnp%rad(ind)%n_symfgrp
ALLOCATE (symtmp(nnp%rad(ind)%symfgrp(s)%n_symf))
ALLOCATE (forcetmp(3, nnp%rad(ind)%symfgrp(s)%n_symf))
!loop over associated neighbors
DO j = 1, neighbor%n_rad(s)
rvect1 = neighbor%dist_rad(1:3, j, s)
r1 = neighbor%dist_rad(4, j, s)
CALL nnp_calc_rad(nnp, ind, s, rvect1, r1, symtmp, forcetmp)
jj = neighbor%ind_rad(j, s)
DO sf = 1, nnp%rad(ind)%symfgrp(s)%n_symf
m = nnp%rad(ind)%symfgrp(s)%symf(sf)
! update forces into dsymdxyz
DO l = 1, 3
dsymdxyz(l, m, i) = dsymdxyz(l, m, i) + forcetmp(l, sf)
dsymdxyz(l, m, jj) = dsymdxyz(l, m, jj) - forcetmp(l, sf)
END DO
IF (PRESENT(stress)) THEN
DO l = 1, 3
stress(:, l, m) = stress(:, l, m) + rvect1(:)*forcetmp(l, sf)
END DO
END IF
nnp%rad(ind)%y(m) = nnp%rad(ind)%y(m) + symtmp(sf)
END DO
END DO
DEALLOCATE (symtmp)
DEALLOCATE (forcetmp)
END DO
CALL timestop(handle_sf)
!loop over angular sym fnct grps
CALL timeset('nnp_acsf_angular', handle_sf)
off = nnp%n_rad(ind)
DO s = 1, nnp%ang(ind)%n_symfgrp
ALLOCATE (symtmp(nnp%ang(ind)%symfgrp(s)%n_symf))
ALLOCATE (force3tmp(3, 3, nnp%ang(ind)%symfgrp(s)%n_symf))
!loop over associated neighbors
IF (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2)) THEN
DO j = 1, neighbor%n_ang1(s)
rvect1 = neighbor%dist_ang1(1:3, j, s)
r1 = neighbor%dist_ang1(4, j, s)
DO k = j + 1, neighbor%n_ang1(s)
rvect2 = neighbor%dist_ang1(1:3, k, s)
r2 = neighbor%dist_ang1(4, k, s)
rvect3 = rvect2 - rvect1
r3 = NORM2(rvect3(:))
IF (r3 < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
jj = neighbor%ind_ang1(j, s)
kk = neighbor%ind_ang1(k, s)
CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, &
r1, r2, r3, symtmp, force3tmp)
DO sf = 1, nnp%ang(ind)%symfgrp(s)%n_symf
m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
! update forces into dsymdxy
DO l = 1, 3
dsymdxyz(l, m, i) = dsymdxyz(l, m, i) &
+ force3tmp(l, 1, sf)
dsymdxyz(l, m, jj) = dsymdxyz(l, m, jj) &
+ force3tmp(l, 2, sf)
dsymdxyz(l, m, kk) = dsymdxyz(l, m, kk) &
+ force3tmp(l, 3, sf)
END DO
IF (PRESENT(stress)) THEN
DO l = 1, 3
stress(:, l, m) = stress(:, l, m) - rvect1(:)*force3tmp(l, 2, sf)
stress(:, l, m) = stress(:, l, m) - rvect2(:)*force3tmp(l, 3, sf)
END DO
END IF
nnp%ang(ind)%y(m - off) = nnp%ang(ind)%y(m - off) + symtmp(sf)
END DO
END IF
END DO
END DO
ELSE
DO j = 1, neighbor%n_ang1(s)
rvect1 = neighbor%dist_ang1(1:3, j, s)
r1 = neighbor%dist_ang1(4, j, s)
DO k = 1, neighbor%n_ang2(s)
rvect2 = neighbor%dist_ang2(1:3, k, s)
r2 = neighbor%dist_ang2(4, k, s)
rvect3 = rvect2 - rvect1
r3 = NORM2(rvect3(:))
IF (r3 < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
jj = neighbor%ind_ang1(j, s)
kk = neighbor%ind_ang1(k, s)
CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, &
r1, r2, r3, symtmp, force3tmp)
!loop over associated sym fncts
DO sf = 1, nnp%ang(ind)%symfgrp(s)%n_symf
m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
jj = neighbor%ind_ang1(j, s)
kk = neighbor%ind_ang2(k, s)
! update forces into dsymdxy
DO l = 1, 3
dsymdxyz(l, m, i) = dsymdxyz(l, m, i) &
+ force3tmp(l, 1, sf)
dsymdxyz(l, m, jj) = dsymdxyz(l, m, jj) &
+ force3tmp(l, 2, sf)
dsymdxyz(l, m, kk) = dsymdxyz(l, m, kk) &
+ force3tmp(l, 3, sf)
END DO
IF (PRESENT(stress)) THEN
DO l = 1, 3
stress(:, l, m) = stress(:, l, m) - rvect1(:)*force3tmp(l, 2, sf)
stress(:, l, m) = stress(:, l, m) - rvect2(:)*force3tmp(l, 3, sf)
END DO
END IF
nnp%ang(ind)%y(m - off) = nnp%ang(ind)%y(m - off) + symtmp(sf)
END DO
END IF
END DO
END DO
END IF
DEALLOCATE (symtmp)
DEALLOCATE (force3tmp)
END DO
CALL timestop(handle_sf)
!no forces
ELSE
!loop over radial sym fnct grps
CALL timeset('nnp_acsf_radial', handle_sf)
DO s = 1, nnp%rad(ind)%n_symfgrp
ALLOCATE (symtmp(nnp%rad(ind)%symfgrp(s)%n_symf))
!loop over associated neighbors
DO j = 1, neighbor%n_rad(s)
rvect1 = neighbor%dist_rad(1:3, j, s)
r1 = neighbor%dist_rad(4, j, s)
CALL nnp_calc_rad(nnp, ind, s, rvect1, r1, symtmp)
DO sf = 1, nnp%rad(ind)%symfgrp(s)%n_symf
m = nnp%rad(ind)%symfgrp(s)%symf(sf)
nnp%rad(ind)%y(m) = nnp%rad(ind)%y(m) + symtmp(sf)
END DO
END DO
DEALLOCATE (symtmp)
END DO
CALL timestop(handle_sf)
!loop over angular sym fnct grps
CALL timeset('nnp_acsf_angular', handle_sf)
off = nnp%n_rad(ind)
DO s = 1, nnp%ang(ind)%n_symfgrp
ALLOCATE (symtmp(nnp%ang(ind)%symfgrp(s)%n_symf))
!loop over associated neighbors
IF (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2)) THEN
DO j = 1, neighbor%n_ang1(s)
rvect1 = neighbor%dist_ang1(1:3, j, s)
r1 = neighbor%dist_ang1(4, j, s)
DO k = j + 1, neighbor%n_ang1(s)
rvect2 = neighbor%dist_ang1(1:3, k, s)
r2 = neighbor%dist_ang1(4, k, s)
rvect3 = rvect2 - rvect1
r3 = NORM2(rvect3(:))
IF (r3 < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, symtmp)
DO sf = 1, nnp%ang(ind)%symfgrp(s)%n_symf
m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
nnp%ang(ind)%y(m - off) = nnp%ang(ind)%y(m - off) + symtmp(sf)
END DO
END IF
END DO
END DO
ELSE
DO j = 1, neighbor%n_ang1(s)
rvect1 = neighbor%dist_ang1(1:3, j, s)
r1 = neighbor%dist_ang1(4, j, s)
DO k = 1, neighbor%n_ang2(s)
rvect2 = neighbor%dist_ang2(1:3, k, s)
r2 = neighbor%dist_ang2(4, k, s)
rvect3 = rvect2 - rvect1
r3 = NORM2(rvect3(:))
IF (r3 < nnp%ang(ind)%symfgrp(s)%cutoff) THEN
CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, symtmp)
!loop over associated sym fncts
DO sf = 1, nnp%ang(ind)%symfgrp(s)%n_symf
m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
nnp%ang(ind)%y(m - off) = nnp%ang(ind)%y(m - off) + symtmp(sf)
END DO
END IF
END DO
END DO
END IF
DEALLOCATE (symtmp)
END DO
CALL timestop(handle_sf)
END IF
!check extrapolation
CALL nnp_check_extrapolation(nnp, ind)
IF (PRESENT(dsymdxyz)) THEN
IF (PRESENT(stress)) THEN
CALL nnp_scale_acsf(nnp, ind, dsymdxyz, stress)
ELSE
CALL nnp_scale_acsf(nnp, ind, dsymdxyz)
END IF
ELSE
CALL nnp_scale_acsf(nnp, ind)
END IF
CALL nnp_neighbor_release(neighbor)
CALL timestop(handle)
END SUBROUTINE nnp_calc_acsf
! **************************************************************************************************
!> \brief Check if the nnp is extrapolating
!> \param nnp ...
!> \param ind ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_check_extrapolation(nnp, ind)
TYPE(nnp_type), INTENT(INOUT) :: nnp
INTEGER, INTENT(IN) :: ind
REAL(KIND=dp), PARAMETER :: threshold = 0.0001_dp
INTEGER :: j
LOGICAL :: extrapolate
extrapolate = nnp%output_expol
DO j = 1, nnp%n_rad(ind)
IF (nnp%rad(ind)%y(j) - &
nnp%rad(ind)%loc_max(j) > threshold) THEN
extrapolate = .TRUE.
ELSE IF (-nnp%rad(ind)%y(j) + &
nnp%rad(ind)%loc_min(j) > threshold) THEN
extrapolate = .TRUE.
END IF
END DO
DO j = 1, nnp%n_ang(ind)
IF (nnp%ang(ind)%y(j) - &
nnp%ang(ind)%loc_max(j) > threshold) THEN
extrapolate = .TRUE.
ELSE IF (-nnp%ang(ind)%y(j) + &
nnp%ang(ind)%loc_min(j) > threshold) THEN
extrapolate = .TRUE.
END IF
END DO
nnp%output_expol = extrapolate
END SUBROUTINE nnp_check_extrapolation
! **************************************************************************************************
!> \brief Scale and center symetry functions (and gradients)
!> \param nnp ...
!> \param ind ...
!> \param dsymdxyz ...
!> \param stress ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_scale_acsf(nnp, ind, dsymdxyz, stress)
TYPE(nnp_type), INTENT(INOUT) :: nnp
INTEGER, INTENT(IN) :: ind
REAL(KIND=dp), DIMENSION(:, :, :), INTENT(OUT), &
OPTIONAL :: dsymdxyz, stress
INTEGER :: j, k, off
IF (nnp%center_acsf) THEN
DO j = 1, nnp%n_rad(ind)
nnp%arc(ind)%layer(1)%node(j) = &
(nnp%rad(ind)%y(j) - nnp%rad(ind)%loc_av(j))
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
nnp%arc(ind)%layer(1)%node(j + off) = &
(nnp%ang(ind)%y(j) - nnp%ang(ind)%loc_av(j))
END DO
IF (nnp%scale_acsf) THEN
DO j = 1, nnp%n_rad(ind)
nnp%arc(ind)%layer(1)%node(j) = &
nnp%arc(ind)%layer(1)%node(j)/ &
(nnp%rad(ind)%loc_max(j) - nnp%rad(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
nnp%arc(ind)%layer(1)%node(j + off) = &
nnp%arc(ind)%layer(1)%node(j + off)/ &
(nnp%ang(ind)%loc_max(j) - nnp%ang(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
END IF
ELSE IF (nnp%scale_acsf) THEN
DO j = 1, nnp%n_rad(ind)
nnp%arc(ind)%layer(1)%node(j) = &
(nnp%rad(ind)%y(j) - nnp%rad(ind)%loc_min(j))/ &
(nnp%rad(ind)%loc_max(j) - nnp%rad(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
nnp%arc(ind)%layer(1)%node(j + off) = &
(nnp%ang(ind)%y(j) - nnp%ang(ind)%loc_min(j))/ &
(nnp%ang(ind)%loc_max(j) - nnp%ang(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
ELSE IF (nnp%scale_sigma_acsf) THEN
DO j = 1, nnp%n_rad(ind)
nnp%arc(ind)%layer(1)%node(j) = &
(nnp%rad(ind)%y(j) - nnp%rad(ind)%loc_av(j))/ &
nnp%rad(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
nnp%arc(ind)%layer(1)%node(j + off) = &
(nnp%ang(ind)%y(j) - nnp%ang(ind)%loc_av(j))/ &
nnp%ang(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin) + nnp%scmin
END DO
ELSE
DO j = 1, nnp%n_rad(ind)
nnp%arc(ind)%layer(1)%node(j) = nnp%rad(ind)%y(j)
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
nnp%arc(ind)%layer(1)%node(j + off) = nnp%ang(ind)%y(j)
END DO
END IF
IF (PRESENT(dsymdxyz)) THEN
IF (nnp%scale_acsf) THEN
DO k = 1, nnp%num_atoms
DO j = 1, nnp%n_rad(ind)
dsymdxyz(:, j, k) = dsymdxyz(:, j, k)/ &
(nnp%rad(ind)%loc_max(j) - &
nnp%rad(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin)
END DO
END DO
off = nnp%n_rad(ind)
DO k = 1, nnp%num_atoms
DO j = 1, nnp%n_ang(ind)
dsymdxyz(:, j + off, k) = dsymdxyz(:, j + off, k)/ &
(nnp%ang(ind)%loc_max(j) - &
nnp%ang(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin)
END DO
END DO
ELSE IF (nnp%scale_sigma_acsf) THEN
DO k = 1, nnp%num_atoms
DO j = 1, nnp%n_rad(ind)
dsymdxyz(:, j, k) = dsymdxyz(:, j, k)/ &
nnp%rad(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin)
END DO
END DO
off = nnp%n_rad(ind)
DO k = 1, nnp%num_atoms
DO j = 1, nnp%n_ang(ind)
dsymdxyz(:, j + off, k) = dsymdxyz(:, j + off, k)/ &
nnp%ang(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin)
END DO
END DO
END IF
END IF
IF (PRESENT(stress)) THEN
IF (nnp%scale_acsf) THEN
DO j = 1, nnp%n_rad(ind)
stress(:, :, j) = stress(:, :, j)/ &
(nnp%rad(ind)%loc_max(j) - &
nnp%rad(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin)
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
stress(:, :, j + off) = stress(:, :, j + off)/ &
(nnp%ang(ind)%loc_max(j) - &
nnp%ang(ind)%loc_min(j))* &
(nnp%scmax - nnp%scmin)
END DO
ELSE IF (nnp%scale_sigma_acsf) THEN
DO j = 1, nnp%n_rad(ind)
stress(:, :, j) = stress(:, :, j)/ &
nnp%rad(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin)
END DO
off = nnp%n_rad(ind)
DO j = 1, nnp%n_ang(ind)
stress(:, :, j + off) = stress(:, :, j + off)/ &
nnp%ang(ind)%sigma(j)* &
(nnp%scmax - nnp%scmin)
END DO
END IF
END IF
END SUBROUTINE nnp_scale_acsf
! **************************************************************************************************
!> \brief Calculate radial symmetry function and gradient (optinal)
!> for given displacment vecotr rvect of atom i and j
!> \param nnp ...
!> \param ind ...
!> \param s ...
!> \param rvect ...
!> \param r ...
!> \param sym ...
!> \param force ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_calc_rad(nnp, ind, s, rvect, r, sym, force)
TYPE(nnp_type), INTENT(IN) :: nnp
INTEGER, INTENT(IN) :: ind, s
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rvect
REAL(KIND=dp), INTENT(IN) :: r
REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: sym
REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT), &
OPTIONAL :: force
INTEGER :: k, sf
REAL(KIND=dp) :: dfcutdr, dsymdr, eta, fcut, funccut, rs, &
tmp
REAL(KIND=dp), DIMENSION(3) :: drdx
!init
drdx = 0.0_dp
fcut = 0.0_dp
dfcutdr = 0.0_dp
!Calculate cutoff function and partial derivative
funccut = nnp%rad(ind)%symfgrp(s)%cutoff !cutoff
SELECT CASE (nnp%cut_type)
CASE (nnp_cut_cos)
tmp = pi*r/funccut
fcut = 0.5_dp*(COS(tmp) + 1.0_dp)
IF (PRESENT(force)) THEN
dfcutdr = 0.5_dp*(-SIN(tmp))*(pi/funccut)
END IF
CASE (nnp_cut_tanh)
tmp = TANH(1.0_dp - r/funccut)
fcut = tmp**3
IF (PRESENT(force)) THEN
dfcutdr = (-3.0_dp/funccut)*(tmp**2 - tmp**4)
END IF
CASE DEFAULT
CPABORT("NNP| Cutoff function unknown")
END SELECT
IF (PRESENT(force)) drdx(:) = rvect(:)/r
!loop over sym fncts of sym fnct group s
DO sf = 1, nnp%rad(ind)%symfgrp(s)%n_symf
k = nnp%rad(ind)%symfgrp(s)%symf(sf) !symf indice
eta = nnp%rad(ind)%eta(k) !eta
rs = nnp%rad(ind)%rs(k) !rshift
! Calculate radial symmetry function
sym(sf) = EXP(-eta*(r - rs)**2)
! Calculate partial derivatives of symmetry function and distance
! and combine them to obtain force
IF (PRESENT(force)) THEN
dsymdr = sym(sf)*(-2.0_dp*eta*(r - rs))
force(:, sf) = fcut*dsymdr*drdx(:) + sym(sf)*dfcutdr*drdx(:)
END IF
! combine radial symmetry function and cutoff function
sym(sf) = sym(sf)*fcut
END DO
END SUBROUTINE nnp_calc_rad
! **************************************************************************************************
!> \brief Calculate angular symmetry function and gradient (optinal)
!> for given displacment vectors rvect1,2,3 of atom i,j and k
!> \param nnp ...
!> \param ind ...
!> \param s ...
!> \param rvect1 ...
!> \param rvect2 ...
!> \param rvect3 ...
!> \param r1 ...
!> \param r2 ...
!> \param r3 ...
!> \param sym ...
!> \param force ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, sym, force)
TYPE(nnp_type), INTENT(IN) :: nnp
INTEGER, INTENT(IN) :: ind, s
REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rvect1, rvect2, rvect3
REAL(KIND=dp), INTENT(IN) :: r1, r2, r3
REAL(KIND=dp), DIMENSION(:), INTENT(OUT) :: sym
REAL(KIND=dp), DIMENSION(:, :, :), INTENT(OUT), &
OPTIONAL :: force
INTEGER :: i, m, sf
REAL(KIND=dp) :: angular, costheta, dfcutdr1, dfcutdr2, dfcutdr3, dsymdr1, dsymdr2, dsymdr3, &
eta, f, fcut, fcut1, fcut2, fcut3, ftot, g, lam, pref, prefzeta, rsqr1, rsqr2, rsqr3, &
symtmp, tmp, tmp1, tmp2, tmp3, tmpzeta, zeta
REAL(KIND=dp), DIMENSION(3) :: dangulardx1, dangulardx2, dangulardx3, dcosthetadx1, &
dcosthetadx2, dcosthetadx3, dfdx1, dfdx2, dfdx3, dgdx1, dgdx2, dgdx3, dr1dx, dr2dx, dr3dx
rsqr1 = r1**2
rsqr2 = r2**2
rsqr3 = r3**2
!init
dangulardx1 = 0.0_dp
dangulardx2 = 0.0_dp
dangulardx3 = 0.0_dp
dr1dx = 0.0_dp
dr2dx = 0.0_dp
dr3dx = 0.0_dp
ftot = 0.0_dp
dfcutdr1 = 0.0_dp
dfcutdr2 = 0.0_dp
dfcutdr3 = 0.0_dp
! Calculate cos(theta)
! use law of cosine for theta
! cos(ang(r1,r2)) = (r3**2 - r1**2 - r2**2)/(-2*r1*r2)
! | f | g |
f = (rsqr3 - rsqr1 - rsqr2)
g = -2.0_dp*r1*r2
costheta = f/g
! Calculate cutoff function and partial derivatives
fcut = nnp%ang(ind)%symfgrp(s)%cutoff !cutoff
SELECT CASE (nnp%cut_type)
CASE (nnp_cut_cos)
tmp1 = pi*r1/fcut
tmp2 = pi*r2/fcut
tmp3 = pi*r3/fcut
fcut1 = 0.5_dp*(COS(tmp1) + 1.0_dp)
fcut2 = 0.5_dp*(COS(tmp2) + 1.0_dp)
fcut3 = 0.5_dp*(COS(tmp3) + 1.0_dp)
ftot = fcut1*fcut2*fcut3
IF (PRESENT(force)) THEN
pref = 0.5_dp*(pi/fcut)
dfcutdr1 = pref*(-SIN(tmp1))*fcut2*fcut3
dfcutdr2 = pref*(-SIN(tmp2))*fcut1*fcut3
dfcutdr3 = pref*(-SIN(tmp3))*fcut1*fcut2
END IF
CASE (nnp_cut_tanh)
tmp1 = TANH(1.0_dp - r1/fcut)
tmp2 = TANH(1.0_dp - r2/fcut)
tmp3 = TANH(1.0_dp - r3/fcut)
fcut1 = tmp1**3
fcut2 = tmp2**3
fcut3 = tmp3**3
ftot = fcut1*fcut2*fcut3
IF (PRESENT(force)) THEN
pref = -3.0_dp/fcut
dfcutdr1 = pref*(tmp1**2 - tmp1**4)*fcut2*fcut3
dfcutdr2 = pref*(tmp2**2 - tmp2**4)*fcut1*fcut3
dfcutdr3 = pref*(tmp3**2 - tmp3**4)*fcut1*fcut2
END IF
CASE DEFAULT
CPABORT("NNP| Cutoff function unknown")
END SELECT
IF (PRESENT(force)) THEN
dr1dx(:) = rvect1(:)/r1
dr2dx(:) = rvect2(:)/r2
dr3dx(:) = rvect3(:)/r3
END IF
!loop over associated sym fncts
DO sf = 1, nnp%ang(ind)%symfgrp(s)%n_symf
m = nnp%ang(ind)%symfgrp(s)%symf(sf)
lam = nnp%ang(ind)%lam(m) !lambda
zeta = nnp%ang(ind)%zeta(m) !zeta
prefzeta = nnp%ang(ind)%prefzeta(m) ! 2**(1-zeta)
eta = nnp%ang(ind)%eta(m) !eta
tmp = (1.0_dp + lam*costheta)
IF (tmp <= 0.0_dp) THEN
sym(sf) = 0.0_dp
IF (PRESENT(force)) force(:, :, sf) = 0.0_dp
CYCLE
END IF
! Calculate symmetry function
! Calculate angular symmetry function
! ang = (1+lam*cos(theta_ijk))**zeta
i = NINT(zeta)
IF (1.0_dp*i == zeta) THEN
tmpzeta = tmp**(i - 1) ! integer power is a LOT faster
ELSE
tmpzeta = tmp**(zeta - 1.0_dp)
END IF
angular = tmpzeta*tmp
! exponential part:
! exp(-eta*(r1**2+r2**2+r3**2))
symtmp = EXP(-eta*(rsqr1 + rsqr2 + rsqr3))
! Partial derivatives (f/g)' = (f'g - fg')/g^2
IF (PRESENT(force)) THEN
pref = zeta*(tmpzeta)/g**2
DO i = 1, 3
dfdx1(i) = -2.0_dp*lam*(rvect1(i) + rvect2(i))
dfdx2(i) = 2.0_dp*lam*(rvect3(i) + rvect1(i))
dfdx3(i) = 2.0_dp*lam*(rvect2(i) - rvect3(i))
tmp1 = 2.0_dp*r2*dr1dx(i)
tmp2 = 2.0_dp*r1*dr2dx(i)
dgdx1(i) = -(tmp1 + tmp2)
dgdx2(i) = tmp1
dgdx3(i) = tmp2
dcosthetadx1(i) = dfdx1(i)*g - lam*f*dgdx1(i)
dcosthetadx2(i) = dfdx2(i)*g - lam*f*dgdx2(i)
dcosthetadx3(i) = dfdx3(i)*g - lam*f*dgdx3(i)
dangulardx1(i) = pref*dcosthetadx1(i)
dangulardx2(i) = pref*dcosthetadx2(i)
dangulardx3(i) = pref*dcosthetadx3(i)
END DO
! Calculate partial derivatives of exponential part and distance
! and combine partial derivatives to obtain force
pref = prefzeta
tmp = -2.0_dp*symtmp*eta
dsymdr1 = tmp*r1
dsymdr2 = tmp*r2
dsymdr3 = tmp*r3
! G(r1,r2,r3) = pref*angular(r1,r2,r3)*exp(r1,r2,r3)*fcut(r1,r2,r3)
! dG/dx1 = (dangular/dx1* exp * fcut +
! angular * dexp/dx1* fcut +
! angular * exp * dfcut/dx1)*pref
! dr1/dx1 = -dr1/dx2
tmp = pref*symtmp*ftot
tmp1 = pref*angular*(ftot*dsymdr1 + dfcutdr1*symtmp)
tmp2 = pref*angular*(ftot*dsymdr2 + dfcutdr2*symtmp)
tmp3 = pref*angular*(ftot*dsymdr3 + dfcutdr3*symtmp)
DO i = 1, 3
force(i, 1, sf) = tmp*dangulardx1(i) + tmp1*dr1dx(i) + tmp2*dr2dx(i)
force(i, 2, sf) = tmp*dangulardx2(i) - tmp1*dr1dx(i) + tmp3*dr3dx(i)
force(i, 3, sf) = tmp*dangulardx3(i) - tmp2*dr2dx(i) - tmp3*dr3dx(i)
END DO
END IF
! Don't forget prefactor: 2**(1-ang%zeta)
pref = prefzeta
! combine angular and exponential part with cutoff function
sym(sf) = pref*angular*symtmp*ftot
END DO
END SUBROUTINE nnp_calc_ang
! **************************************************************************************************
!> \brief Sort element array according to atomic number
!> \param ele ...
!> \param nuc_ele ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_sort_ele(ele, nuc_ele)
CHARACTER(len=2), DIMENSION(:), INTENT(INOUT) :: ele
INTEGER, DIMENSION(:), INTENT(INOUT) :: nuc_ele
CHARACTER(len=2) :: tmp_ele
INTEGER :: i, j, loc, minimum, tmp_nuc_ele
! Determine atomic number
DO i = 1, SIZE(ele)
CALL get_ptable_info(ele(i), number=nuc_ele(i))
END DO
! Sort both arrays
DO i = 1, SIZE(ele) - 1
minimum = nuc_ele(i)
loc = i
DO j = i + 1, SIZE(ele)
IF (nuc_ele(j) .LT. minimum) THEN
loc = j
minimum = nuc_ele(j)
END IF
END DO
tmp_nuc_ele = nuc_ele(i)
nuc_ele(i) = nuc_ele(loc)
nuc_ele(loc) = tmp_nuc_ele
tmp_ele = ele(i)
ele(i) = ele(loc)
ele(loc) = tmp_ele
END DO
END SUBROUTINE nnp_sort_ele
! **************************************************************************************************
!> \brief Sort symmetry functions according to different criteria
!> \param nnp ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_sort_acsf(nnp)
TYPE(nnp_type), INTENT(INOUT) :: nnp
INTEGER :: i, j, k, loc
! First sort is according to symmetry function type
! This is done manually, since data structures are separate
! Note: Bubble sort is OK here, since small sort + special
DO i = 1, nnp%n_ele
! sort by cutoff
! rad
DO j = 1, nnp%n_rad(i) - 1
loc = j
DO k = j + 1, nnp%n_rad(i)
IF (nnp%rad(i)%funccut(loc) .GT. nnp%rad(i)%funccut(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swaprad(nnp%rad(i), j, loc)
END DO
! sort by eta
! rad
DO j = 1, nnp%n_rad(i) - 1
loc = j
DO k = j + 1, nnp%n_rad(i)
IF (nnp%rad(i)%funccut(loc) .EQ. nnp%rad(i)%funccut(k) .AND. &
nnp%rad(i)%eta(loc) .GT. nnp%rad(i)%eta(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swaprad(nnp%rad(i), j, loc)
END DO
! sort by rshift
! rad
DO j = 1, nnp%n_rad(i) - 1
loc = j
DO k = j + 1, nnp%n_rad(i)
IF (nnp%rad(i)%funccut(loc) .EQ. nnp%rad(i)%funccut(k) .AND. &
nnp%rad(i)%eta(loc) .EQ. nnp%rad(i)%eta(k) .AND. &
nnp%rad(i)%rs(loc) .GT. nnp%rad(i)%rs(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swaprad(nnp%rad(i), j, loc)
END DO
! sort by ele
! rad
DO j = 1, nnp%n_rad(i) - 1
loc = j
DO k = j + 1, nnp%n_rad(i)
IF (nnp%rad(i)%funccut(loc) .EQ. nnp%rad(i)%funccut(k) .AND. &
nnp%rad(i)%eta(loc) .EQ. nnp%rad(i)%eta(k) .AND. &
nnp%rad(i)%rs(loc) .EQ. nnp%rad(i)%rs(k) .AND. &
nnp%rad(i)%nuc_ele(loc) .GT. nnp%rad(i)%nuc_ele(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swaprad(nnp%rad(i), j, loc)
END DO
! ang
! sort by cutoff
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .GT. nnp%ang(i)%funccut(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
! sort by eta
! ang
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .EQ. nnp%ang(i)%funccut(k) .AND. &
nnp%ang(i)%eta(loc) .GT. nnp%ang(i)%eta(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
! sort by zeta
! ang
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .EQ. nnp%ang(i)%funccut(k) .AND. &
nnp%ang(i)%eta(loc) .EQ. nnp%ang(i)%eta(k) .AND. &
nnp%ang(i)%zeta(loc) .GT. nnp%ang(i)%zeta(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
! sort by lambda
! ang
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .EQ. nnp%ang(i)%funccut(k) .AND. &
nnp%ang(i)%eta(loc) .EQ. nnp%ang(i)%eta(k) .AND. &
nnp%ang(i)%zeta(loc) .EQ. nnp%ang(i)%zeta(k) .AND. &
nnp%ang(i)%lam(loc) .GT. nnp%ang(i)%lam(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
! sort by ele
! ang, ele1
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .EQ. nnp%ang(i)%funccut(k) .AND. &
nnp%ang(i)%eta(loc) .EQ. nnp%ang(i)%eta(k) .AND. &
nnp%ang(i)%zeta(loc) .EQ. nnp%ang(i)%zeta(k) .AND. &
nnp%ang(i)%lam(loc) .EQ. nnp%ang(i)%lam(k) .AND. &
nnp%ang(i)%nuc_ele1(loc) .GT. nnp%ang(i)%nuc_ele1(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
! ang, ele2
DO j = 1, nnp%n_ang(i) - 1
loc = j
DO k = j + 1, nnp%n_ang(i)
IF (nnp%ang(i)%funccut(loc) .EQ. nnp%ang(i)%funccut(k) .AND. &
nnp%ang(i)%eta(loc) .EQ. nnp%ang(i)%eta(k) .AND. &
nnp%ang(i)%zeta(loc) .EQ. nnp%ang(i)%zeta(k) .AND. &
nnp%ang(i)%lam(loc) .EQ. nnp%ang(i)%lam(k) .AND. &
nnp%ang(i)%nuc_ele1(loc) .EQ. nnp%ang(i)%nuc_ele1(k) .AND. &
nnp%ang(i)%nuc_ele2(loc) .GT. nnp%ang(i)%nuc_ele2(k)) THEN
loc = k
END IF
END DO
! swap symfnct
CALL nnp_swapang(nnp%ang(i), j, loc)
END DO
END DO
END SUBROUTINE nnp_sort_acsf
! **************************************************************************************************
!> \brief Swap two radial symmetry functions
!> \param rad ...
!> \param i ...
!> \param j ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])
! **************************************************************************************************
SUBROUTINE nnp_swaprad(rad, i, j)
TYPE(nnp_acsf_rad_type), INTENT(INOUT) :: rad
INTEGER, INTENT(IN) :: i, j
CHARACTER(len=2) :: tmpc
INTEGER :: tmpi
REAL(KIND=dp) :: tmpr
tmpr = rad%funccut(i)
rad%funccut(i) = rad%funccut(j)
rad%funccut(j) = tmpr
tmpr = rad%eta(i)
rad%eta(i) = rad%eta(j)
rad%eta(j) = tmpr
tmpr = rad%rs(i)
rad%rs(i) = rad%rs(j)
rad%rs(j) = tmpr
tmpc = rad%ele(i)
rad%ele(i) = rad%ele(j)
rad%ele(j) = tmpc
tmpi = rad%nuc_ele(i)
rad%nuc_ele(i) = rad%nuc_ele(j)
rad%nuc_ele(j) = tmpi
END SUBROUTINE nnp_swaprad
! **************************************************************************************************
!> \brief Swap two angular symmetry functions
!> \param ang ...
!> \param i ...
!> \param j ...
!> \date 2020-10-10
!> \author Christoph Schran ([email protected])