LCOV - code coverage report
Current view: top level - src - nnp_acsf.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 90.9 % 816 742
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 15 15

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief  Functionality for atom centered symmetry functions
      10              : !>         for neural network potentials
      11              : !> \author Christoph Schran (christoph.schran@rub.de)
      12              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
      13              : !> \author Claudio Malvino (claudiormal@gmail.com)
      14              : !> \date   2020-10-10
      15              : ! **************************************************************************************************
      16              : MODULE nnp_acsf
      17              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      18              :                                               cp_logger_get_default_unit_nr,&
      19              :                                               cp_logger_type
      20              :    USE kinds,                           ONLY: default_string_length,&
      21              :                                               dp
      22              :    USE mathconstants,                   ONLY: pi
      23              :    USE message_passing,                 ONLY: mp_para_env_type
      24              :    USE nnp_cell_list,                   ONLY: nnp_compute_neighbors_cell_list,&
      25              :                                               nnp_prepare_cell_list_cache
      26              :    USE nnp_environment_types,           ONLY: nnp_acsf_ang_type,&
      27              :                                               nnp_acsf_rad_type,&
      28              :                                               nnp_arc_type,&
      29              :                                               nnp_cut_cos,&
      30              :                                               nnp_cut_tanh,&
      31              :                                               nnp_symfgrp_type,&
      32              :                                               nnp_type
      33              :    USE nnp_neighbor_interface,          ONLY: nnp_grp_grow_dGdr,&
      34              :                                               nnp_neighbor_interface_prepare,&
      35              :                                               nnp_neighbor_interface_reset_neighbor,&
      36              :                                               nnp_workspace_grow_caches
      37              :    USE periodic_table,                  ONLY: get_ptable_info
      38              : 
      39              : !$ USE omp_lib, ONLY: omp_get_thread_num
      40              : #include "./base/base_uses.f90"
      41              : 
      42              :    IMPLICIT NONE
      43              : 
      44              :    PRIVATE
      45              : 
      46              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
      47              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'nnp_acsf'
      48              : 
      49              :    ! Cutoff-equality tolerance for grouping symmetry functions in
      50              :    ! nnp_init_acsf_groups: SFs whose cutoffs agree to within this absolute
      51              :    ! tolerance share a group (and a spline grid).
      52              :    REAL(KIND=dp), PARAMETER, PRIVATE                     :: cutoff_eq_tol = 1.0e-5_dp
      53              : 
      54              :    ! Public subroutines ***
      55              :    PUBLIC :: nnp_calc_acsf, &
      56              :              nnp_prepare_neighbor_cache, &
      57              :              nnp_init_acsf_groups, &
      58              :              nnp_sort_acsf, &
      59              :              nnp_sort_ele, &
      60              :              nnp_write_acsf, &
      61              :              nnp_build_radial_splines
      62              : 
      63              : CONTAINS
      64              : 
      65              : ! **************************************************************************************************
      66              : !> \brief Calculate atom centered symmetry functions for given atom i
      67              : !>
      68              : !> Per-atom symmetry-function gradients live in the per-element neighbour
      69              : !> workspace as sparse per-group arrays: self_dGdr (atom i), dGdr_rad (radial
      70              : !> group s), and dGdr_ang_jj / dGdr_ang_kk (angular group s, j- and k-side),
      71              : !> each addressed by the neighbour's slot in workspace(ind)%neighbor. No global
      72              : !> (3, n_sf, num_atoms) slab is needed: only atoms in atom i's neighbour lists
      73              : !> get derivatives.
      74              : !>
      75              : !> \param nnp          NNP environment with persistent neighbour caches populated by
      76              : !>                     nnp_prepare_neighbor_cache (must be called once before the per-atom loop).
      77              : !> \param i            central-atom index in the global atom ordering; selects
      78              : !>                     nnp%ele_ind(i) for per-element scratch routing.
      79              : !> \param calc_forces  if .TRUE., populate the per-element dGdr workspaces for caller-side
      80              : !>                     force assembly via nnp_scatter_dgdr_to_forces.
      81              : !> \param arc          caller's copy of the networks; its input layer is filled for atom i
      82              : !> \param stress       optional per-input-node stress accumulator (only valid when calc_forces).
      83              : !> \date   2020-10-10
      84              : !> \author Christoph Schran (christoph.schran@rub.de)
      85              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
      86              : !> \author Claudio Malvino (claudiormal@gmail.com)
      87              : ! **************************************************************************************************
      88       263313 :    SUBROUTINE nnp_calc_acsf(nnp, i, calc_forces, arc, stress)
      89              : 
      90              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp
      91              :       INTEGER, INTENT(IN)                                :: i
      92              :       LOGICAL, INTENT(IN)                                :: calc_forces
      93              :       TYPE(nnp_arc_type), INTENT(INOUT)                  :: arc
      94              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT), &
      95              :          OPTIONAL                                        :: stress
      96              : 
      97              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'nnp_calc_acsf'
      98              : 
      99              :       INTEGER                                            :: handle, handle_nlist, handle_sf, ii, &
     100              :                                                             ind, izeta_il, j, k, l, m, n_ang1_s, &
     101              :                                                             n_ang2_s, n_input_nodes, n_symf_s, &
     102              :                                                             off, peak, s, sf, tid
     103              :       LOGICAL                                            :: do_forces, homo_grp
     104              :       REAL(KIND=dp) :: angular_il, arg_il, costheta_il, cutoff_s, cutoff_sqr, dfcut3_il, &
     105              :          dfcutdr1_il, dfcutdr2_il, dfcutdr3_il, dgdx_t1, dgdx_t2, dsymdr1_il, dsymdr2_il, &
     106              :          dsymdr3_il, eta_il, f_il, fcut3_il, ftot_il, g_il, inv_g2_il, lam_il, pref_il, &
     107              :          pref_lam_il, prefzeta_il, r1, r1_inv, r2, r2_inv, r2sum_il, r3, r3_inv, r3_sqr, rsqr1, &
     108              :          rsqr2, rsqr3, sym_il, symtmp_il, tanh_il, tmp1_il, tmp2_il, tmp3_il, tmp_il, tmpzeta_il, &
     109              :          zeta_il
     110              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: ang_y, rad_y
     111              :       REAL(KIND=dp), DIMENSION(3)                        :: dcosbase1_il, dcosbase2_il, &
     112              :                                                             dcosbase3_il, dr1dx_il, dr2dx_il, &
     113              :                                                             dr3dx_il, f_jj_il, f_kk_il, rvect1, &
     114              :                                                             rvect2, rvect3
     115              : 
     116              : ! Inlined angular kernel variables (eliminates nnp_calc_ang call overhead
     117              : ! and intermediate angular_force3tmp array on the serial force path).
     118              : 
     119       263313 :       CALL timeset(routineN, handle)
     120              : 
     121              :       !determine index of atom type
     122       263313 :       ind = nnp%ele_ind(i)
     123       263313 :       do_forces = calc_forces
     124              : 
     125              :       ! 1-based thread id selecting this thread's workspace column
     126       263313 :       tid = 1
     127       263313 : !$    tid = omp_get_thread_num() + 1
     128              : 
     129       789939 :       ALLOCATE (rad_y(nnp%n_rad(ind)))
     130       789939 :       ALLOCATE (ang_y(nnp%n_ang(ind)))
     131              : 
     132              :       ! Lazy one-shot build of the per-radial-group spline tables. They depend
     133              :       ! only on eta/rs/cutoff/cut_type, fixed once nnp_init_acsf_groups and
     134              :       ! nnp_sort_acsf have run; %spline_built is per-nnp so re-init is safe.
     135              :       ! Guard the indexed access in case this element has no radial groups.
     136       263313 :       IF (nnp%rad(ind)%n_symfgrp > 0) THEN
     137       263313 :          IF (.NOT. nnp%rad(ind)%symfgrp(1)%spline_built) CALL nnp_build_radial_splines(nnp)
     138              :       END IF
     139              : 
     140              :       ! Persistent per-element workspace bindings. The dGdr_* slabs grow lazily
     141              :       ! via nnp_grp_grow_dGdr; the fc_cache*/dfc_cache* buffers are bound in an
     142              :       ! inner ASSOCIATE after nnp_workspace_grow_caches has sized them, so the
     143              :       ! alias stays valid.
     144              :       ASSOCIATE (workspace => nnp%neighbor_interface_state%workspace(ind, tid), &
     145              :                  neighbor => nnp%neighbor_interface_state%workspace(ind, tid)%neighbor, &
     146              :                  radial_symtmp => nnp%neighbor_interface_state%workspace(ind, tid)%radial_sym, &
     147              :                  radial_forcetmp => nnp%neighbor_interface_state%workspace(ind, tid)%radial_force, &
     148              :                  angular_symtmp => nnp%neighbor_interface_state%workspace(ind, tid)%angular_sym, &
     149              :                  angular_force3tmp => nnp%neighbor_interface_state%workspace(ind, tid)%angular_force, &
     150              :                  self_dGdr => nnp%neighbor_interface_state%workspace(ind, tid)%self_dGdr)
     151              : 
     152       263313 :          n_input_nodes = nnp%neighbor_interface_state%workspace(ind, tid)%n_input_nodes
     153      3990065 :          IF (do_forces) self_dGdr(:, 1:n_input_nodes) = 0.0_dp
     154              : 
     155              :          ! Walk the linked-cell candidates directly. The cell-list/neighbour cache
     156              :          ! is prepared once per force evaluation by nnp_prepare_neighbor_cache, not
     157              :          ! here, so the per-atom walk stays O(neighbours).
     158       263313 :          CALL timeset('nnp_acsf_neighbor_fill', handle_nlist)
     159      1053252 :          neighbor%pbc_copies = nnp%cell_list_cache%exact_pbc_copies
     160       263313 :          CALL nnp_neighbor_interface_reset_neighbor(nnp, ind)
     161       263313 :          CALL nnp_compute_neighbors_cell_list(nnp, neighbor, i)
     162       263313 :          CALL timestop(handle_nlist)
     163              : 
     164              :          ! Reset y:
     165       263313 :          rad_y = 0.0_dp
     166       263313 :          ang_y = 0.0_dp
     167              : 
     168              :          ! Grow the per-element 1D angular cutoff caches to this atom's peak
     169              :          ! per-group neighbour count, then bind them in an inner ASSOCIATE.
     170              :          ! MAXVAL of a zero-size array is compiler-defined, so guard each axis.
     171       263313 :          peak = 0
     172       815678 :          IF (SIZE(neighbor%n_ang1) > 0) peak = MAX(peak, MAXVAL(neighbor%n_ang1))
     173       815678 :          IF (SIZE(neighbor%n_ang2) > 0) peak = MAX(peak, MAXVAL(neighbor%n_ang2))
     174       263313 :          IF (peak > 0) CALL nnp_workspace_grow_caches(workspace, peak)
     175              : 
     176              :          ASSOCIATE (fc_cache1 => workspace%fc_cache1, &
     177              :                     dfc_cache1 => workspace%dfc_cache1, &
     178              :                     fc_cache2 => workspace%fc_cache2, &
     179       297554 :                     dfc_cache2 => workspace%dfc_cache2)
     180              : 
     181              :             !calc forces
     182       263313 :             IF (do_forces) THEN
     183              :                !loop over radial sym fnct grps
     184        34241 :                CALL timeset('nnp_acsf_radial', handle_sf)
     185       108931 :                DO s = 1, nnp%rad(ind)%n_symfgrp
     186        74690 :                   n_symf_s = nnp%rad(ind)%symfgrp(s)%n_symf
     187              :                   ! Per-group dense buffer: (3, n_symf_s, cap_s). Grown lazily.
     188        74690 :                   CALL nnp_grp_grow_dGdr(workspace%dGdr_rad(s), neighbor%n_rad(s))
     189        34241 :                   ASSOCIATE (rad_buf => workspace%dGdr_rad(s)%data)
     190              :                      !loop over associated neighbors
     191      3042260 :                      DO j = 1, neighbor%n_rad(s)
     192     11870280 :                         rvect1 = neighbor%rad(s)%dist(1:3, j)
     193      2967570 :                         r1 = neighbor%rad(s)%dist(4, j)
     194              :                         CALL nnp_calc_rad(nnp, ind, s, rvect1, r1, &
     195              :                                           radial_symtmp(1:n_symf_s), &
     196      2967570 :                                           radial_forcetmp(:, 1:n_symf_s))
     197              :                         ! Per-group dense write: rad_buf(:, sf, j) holds dG_m/dr_j.
     198     27073112 :                         DO sf = 1, n_symf_s
     199     24030852 :                            m = nnp%rad(ind)%symfgrp(s)%symf(sf)
     200     24030852 :                            self_dGdr(1, m) = self_dGdr(1, m) + radial_forcetmp(1, sf)
     201     24030852 :                            self_dGdr(2, m) = self_dGdr(2, m) + radial_forcetmp(2, sf)
     202     24030852 :                            self_dGdr(3, m) = self_dGdr(3, m) + radial_forcetmp(3, sf)
     203     24030852 :                            rad_buf(1, sf, j) = -radial_forcetmp(1, sf)
     204     24030852 :                            rad_buf(2, sf, j) = -radial_forcetmp(2, sf)
     205     24030852 :                            rad_buf(3, sf, j) = -radial_forcetmp(3, sf)
     206     24030852 :                            IF (PRESENT(stress)) THEN
     207     16330432 :                               DO l = 1, 3
     208     53073904 :                                  stress(:, l, m) = stress(:, l, m) + rvect1(:)*radial_forcetmp(l, sf)
     209              :                               END DO
     210              :                            END IF
     211     26998422 :                            rad_y(m) = rad_y(m) + radial_symtmp(sf)
     212              :                         END DO
     213              :                      END DO
     214              :                   END ASSOCIATE
     215              :                END DO
     216        34241 :                CALL timestop(handle_sf)
     217              : 
     218              :                !loop over angular sym fnct grps
     219        34241 :                CALL timeset('nnp_acsf_angular', handle_sf)
     220        34241 :                off = nnp%n_rad(ind)
     221              : 
     222              :                ! The angular loop runs serially within each atom: parallelism
     223              :                ! lives in the atom loop of nnp_calc_energy_force, which keeps the
     224              :                ! floating-point order identical at every thread count.
     225       123534 :                DO s = 1, nnp%ang(ind)%n_symfgrp
     226        89293 :                   cutoff_s = nnp%ang(ind)%symfgrp(s)%cutoff
     227        89293 :                   cutoff_sqr = cutoff_s*cutoff_s
     228        89293 :                   n_symf_s = nnp%ang(ind)%symfgrp(s)%n_symf
     229        89293 :                   n_ang1_s = neighbor%n_ang1(s)
     230        89293 :                   homo_grp = (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2))
     231              : 
     232              :                   ! Grow per-group dense buffers. jj is always indexed in ang1.
     233              :                   ! kk is indexed in ang1 for homo groups and in ang2 for hetero.
     234        89293 :                   CALL nnp_grp_grow_dGdr(workspace%dGdr_ang_jj(s), n_ang1_s)
     235        89293 :                   IF (homo_grp) THEN
     236        44332 :                      CALL nnp_grp_grow_dGdr(workspace%dGdr_ang_kk(s), n_ang1_s)
     237        44332 :                      n_ang2_s = 0
     238              :                   ELSE
     239        44961 :                      n_ang2_s = neighbor%n_ang2(s)
     240        44961 :                      CALL nnp_grp_grow_dGdr(workspace%dGdr_ang_kk(s), n_ang2_s)
     241              :                   END IF
     242              : 
     243              :                   ! Per-group reset. Triplets accumulate into the same (sf, j) slot
     244              :                   ! across multiple k partners, so we MUST zero before the triplet loop.
     245     88198444 :                   IF (n_ang1_s > 0) workspace%dGdr_ang_jj(s)%data(:, 1:n_symf_s, 1:n_ang1_s) = 0.0_dp
     246        89293 :                   IF (homo_grp) THEN
     247     34258242 :                      IF (n_ang1_s > 0) workspace%dGdr_ang_kk(s)%data(:, 1:n_symf_s, 1:n_ang1_s) = 0.0_dp
     248              :                   ELSE
     249     24390638 :                      IF (n_ang2_s > 0) workspace%dGdr_ang_kk(s)%data(:, 1:n_symf_s, 1:n_ang2_s) = 0.0_dp
     250              :                   END IF
     251              : 
     252              :                   ! Precompute cutoff values and derivatives for ang1 neighbors
     253              :                   CALL nnp_fill_fc_dfc_cache(neighbor%ang1(s)%dist, n_ang1_s, &
     254        89293 :                                              nnp%cut_type, cutoff_s, fc_cache1, dfc_cache1)
     255              : 
     256              :                   ! Inlined angular kernel: compute + scatter fused. The (j,k)
     257              :                   ! geometry is computed once, then the SF loop scatters sym
     258              :                   ! values and forces directly into self_dGdr / jj_buf / kk_buf,
     259              :                   ! keeping per-triple scalars in registers. Inlined equivalent
     260              :                   ! of nnp_calc_ang; the OMP path calls it directly.
     261       123534 :                   IF (homo_grp) THEN
     262              :                      ASSOCIATE (jj_buf => workspace%dGdr_ang_jj(s)%data, &
     263              :                                 kk_buf => workspace%dGdr_ang_kk(s)%data, &
     264              :                                 grp_il => nnp%ang(ind)%symfgrp(s))
     265      1762466 :                      DO j = 1, n_ang1_s
     266      6872536 :                         rvect1 = neighbor%ang1(s)%dist(1:3, j)
     267      1718134 :                         r1 = neighbor%ang1(s)%dist(4, j)
     268     44372306 :                         DO k = j + 1, n_ang1_s
     269    170439360 :                            rvect2 = neighbor%ang1(s)%dist(1:3, k)
     270     42609840 :                            r2 = neighbor%ang1(s)%dist(4, k)
     271     42609840 :                            rvect3(1) = rvect2(1) - rvect1(1)
     272     42609840 :                            rvect3(2) = rvect2(2) - rvect1(2)
     273     42609840 :                            rvect3(3) = rvect2(3) - rvect1(3)
     274     42609840 :                            r3_sqr = rvect3(1)*rvect3(1) + rvect3(2)*rvect3(2) + rvect3(3)*rvect3(3)
     275     44327974 :                            IF (r3_sqr < cutoff_sqr) THEN
     276     19607612 :                               r3 = SQRT(r3_sqr)
     277              : 
     278              :                               ! -- per-triple geometry (once) --
     279     19607612 :                               rsqr1 = r1*r1; rsqr2 = r2*r2; rsqr3 = r3*r3
     280     19607612 :                               r2sum_il = rsqr1 + rsqr2 + rsqr3
     281     19607612 :                               f_il = rsqr3 - rsqr1 - rsqr2
     282     19607612 :                               g_il = -2.0_dp*r1*r2
     283     19607612 :                               costheta_il = f_il/g_il
     284              : 
     285     19609408 :                               SELECT CASE (nnp%cut_type)
     286              :                               CASE (nnp_cut_cos)
     287         1796 :                                  arg_il = pi*r3/cutoff_s
     288         1796 :                                  fcut3_il = 0.5_dp*(COS(arg_il) + 1.0_dp)
     289         1796 :                                  dfcut3_il = -0.5_dp*SIN(arg_il)*(pi/cutoff_s)
     290              :                               CASE (nnp_cut_tanh)
     291     19605816 :                                  tanh_il = TANH(1.0_dp - r3/cutoff_s)
     292     19605816 :                                  fcut3_il = tanh_il**3
     293     19605816 :                                  dfcut3_il = (-3.0_dp/cutoff_s)*(tanh_il**2 - tanh_il**4)
     294              :                               CASE DEFAULT
     295     19607612 :                                  CPABORT("NNP| Cutoff function unknown")
     296              :                               END SELECT
     297              : 
     298     19607612 :                               ftot_il = fc_cache1(j)*fc_cache1(k)*fcut3_il
     299     19607612 :                               dfcutdr1_il = dfc_cache1(j)*fc_cache1(k)*fcut3_il
     300     19607612 :                               dfcutdr2_il = fc_cache1(j)*dfc_cache1(k)*fcut3_il
     301     19607612 :                               dfcutdr3_il = fc_cache1(j)*fc_cache1(k)*dfcut3_il
     302              : 
     303     19607612 :                               r1_inv = 1.0_dp/r1; r2_inv = 1.0_dp/r2; r3_inv = 1.0_dp/r3
     304     78430448 :                               dr1dx_il(:) = rvect1(:)*r1_inv
     305     78430448 :                               dr2dx_il(:) = rvect2(:)*r2_inv
     306     78430448 :                               dr3dx_il(:) = rvect3(:)*r3_inv
     307              : 
     308     19607612 :                               inv_g2_il = 1.0_dp/(g_il*g_il)
     309     78430448 :                               DO ii = 1, 3
     310     58822836 :                                  dgdx_t1 = 2.0_dp*r2*dr1dx_il(ii)
     311     58822836 :                                  dgdx_t2 = 2.0_dp*r1*dr2dx_il(ii)
     312              :                                  dcosbase1_il(ii) = -2.0_dp*(rvect1(ii) + rvect2(ii))*g_il &
     313     58822836 :                                                     - f_il*(-(dgdx_t1 + dgdx_t2))
     314              :                                  dcosbase2_il(ii) = 2.0_dp*(rvect3(ii) + rvect1(ii))*g_il &
     315     58822836 :                                                     - f_il*dgdx_t1
     316              :                                  dcosbase3_il(ii) = 2.0_dp*(rvect2(ii) - rvect3(ii))*g_il &
     317     78430448 :                                                     - f_il*dgdx_t2
     318              :                               END DO
     319              : 
     320              :                               ! -- fused SF loop: compute + direct scatter --
     321    118783864 :                               DO sf = 1, n_symf_s
     322     99176252 :                                  m = off + grp_il%symf(sf)
     323     99176252 :                                  lam_il = grp_il%pack_lam(sf)
     324     99176252 :                                  zeta_il = grp_il%pack_zeta(sf)
     325     99176252 :                                  eta_il = grp_il%pack_eta(sf)
     326     99176252 :                                  prefzeta_il = grp_il%pack_prefzeta(sf)
     327              : 
     328     99176252 :                                  tmp_il = 1.0_dp + lam_il*costheta_il
     329     99176252 :                                  IF (tmp_il <= 0.0_dp) THEN
     330              :                                     tmpzeta_il = 0.0_dp
     331              :                                     angular_il = 0.0_dp
     332              :                                  ELSE
     333     99176252 :                                     IF (grp_il%pack_use_int_zeta(sf)) THEN
     334     99176252 :                                        izeta_il = grp_il%pack_izeta(sf)
     335     99176252 :                                        tmpzeta_il = tmp_il**(izeta_il - 1)
     336              :                                     ELSE
     337            0 :                                        tmpzeta_il = tmp_il**(zeta_il - 1.0_dp)
     338              :                                     END IF
     339     99176252 :                                     angular_il = tmpzeta_il*tmp_il
     340              :                                  END IF
     341              : 
     342     99176252 :                                  symtmp_il = EXP(-eta_il*r2sum_il)
     343     99176252 :                                  sym_il = prefzeta_il*angular_il*symtmp_il*ftot_il
     344     99176252 :                                  ang_y(m - off) = ang_y(m - off) + sym_il
     345              : 
     346     99176252 :                                  pref_lam_il = zeta_il*tmpzeta_il*lam_il*inv_g2_il
     347     99176252 :                                  tmp_il = -2.0_dp*symtmp_il*eta_il
     348     99176252 :                                  dsymdr1_il = tmp_il*r1
     349     99176252 :                                  dsymdr2_il = tmp_il*r2
     350     99176252 :                                  dsymdr3_il = tmp_il*r3
     351              : 
     352     99176252 :                                  pref_il = prefzeta_il*symtmp_il*ftot_il
     353     99176252 :                                  tmp1_il = prefzeta_il*angular_il*(ftot_il*dsymdr1_il + dfcutdr1_il*symtmp_il)
     354     99176252 :                                  tmp2_il = prefzeta_il*angular_il*(ftot_il*dsymdr2_il + dfcutdr2_il*symtmp_il)
     355     99176252 :                                  tmp3_il = prefzeta_il*angular_il*(ftot_il*dsymdr3_il + dfcutdr3_il*symtmp_il)
     356              : 
     357    396705008 :                                  DO ii = 1, 3
     358              :                                     f_jj_il(ii) = pref_il*pref_lam_il*dcosbase2_il(ii) &
     359    297528756 :                                                   - tmp1_il*dr1dx_il(ii) + tmp3_il*dr3dx_il(ii)
     360              :                                     f_kk_il(ii) = pref_il*pref_lam_il*dcosbase3_il(ii) &
     361    297528756 :                                                   - tmp2_il*dr2dx_il(ii) - tmp3_il*dr3dx_il(ii)
     362              :                                     self_dGdr(ii, m) = self_dGdr(ii, m) &
     363              :                                                        + pref_il*pref_lam_il*dcosbase1_il(ii) &
     364    297528756 :                                                        + tmp1_il*dr1dx_il(ii) + tmp2_il*dr2dx_il(ii)
     365    297528756 :                                     jj_buf(ii, sf, j) = jj_buf(ii, sf, j) + f_jj_il(ii)
     366    396705008 :                                     kk_buf(ii, sf, k) = kk_buf(ii, sf, k) + f_kk_il(ii)
     367              :                                  END DO
     368    118783864 :                                  IF (PRESENT(stress)) THEN
     369     66410048 :                                     DO l = 1, 3
     370              :                                        stress(:, l, m) = stress(:, l, m) &
     371    215832656 :                                                          - rvect1(:)*f_jj_il(l) - rvect2(:)*f_kk_il(l)
     372              :                                     END DO
     373              :                                  END IF
     374              :                               END DO
     375              : 
     376              :                            END IF
     377              :                         END DO
     378              :                      END DO
     379              :                      END ASSOCIATE
     380              :                   ELSE
     381              :                      ! Precompute cutoff values for ang2 neighbors (different elements)
     382              :                      CALL nnp_fill_fc_dfc_cache(neighbor%ang2(s)%dist, n_ang2_s, &
     383        44961 :                                                 nnp%cut_type, cutoff_s, fc_cache2, dfc_cache2)
     384              : 
     385              :                      ASSOCIATE (jj_buf => workspace%dGdr_ang_jj(s)%data, &
     386              :                                 kk_buf => workspace%dGdr_ang_kk(s)%data, &
     387              :                                 grp_il => nnp%ang(ind)%symfgrp(s))
     388      2339338 :                      DO j = 1, n_ang1_s
     389      9177508 :                         rvect1 = neighbor%ang1(s)%dist(1:3, j)
     390      2294377 :                         r1 = neighbor%ang1(s)%dist(4, j)
     391     72272630 :                         DO k = 1, n_ang2_s
     392    279733168 :                            rvect2 = neighbor%ang2(s)%dist(1:3, k)
     393     69933292 :                            r2 = neighbor%ang2(s)%dist(4, k)
     394     69933292 :                            rvect3(1) = rvect2(1) - rvect1(1)
     395     69933292 :                            rvect3(2) = rvect2(2) - rvect1(2)
     396     69933292 :                            rvect3(3) = rvect2(3) - rvect1(3)
     397     69933292 :                            r3_sqr = rvect3(1)*rvect3(1) + rvect3(2)*rvect3(2) + rvect3(3)*rvect3(3)
     398     72227669 :                            IF (r3_sqr < cutoff_sqr) THEN
     399     32517162 :                               r3 = SQRT(r3_sqr)
     400              : 
     401              :                               ! -- per-triple geometry (once) --
     402     32517162 :                               rsqr1 = r1*r1; rsqr2 = r2*r2; rsqr3 = r3*r3
     403     32517162 :                               r2sum_il = rsqr1 + rsqr2 + rsqr3
     404     32517162 :                               f_il = rsqr3 - rsqr1 - rsqr2
     405     32517162 :                               g_il = -2.0_dp*r1*r2
     406     32517162 :                               costheta_il = f_il/g_il
     407              : 
     408     32521608 :                               SELECT CASE (nnp%cut_type)
     409              :                               CASE (nnp_cut_cos)
     410         4446 :                                  arg_il = pi*r3/cutoff_s
     411         4446 :                                  fcut3_il = 0.5_dp*(COS(arg_il) + 1.0_dp)
     412         4446 :                                  dfcut3_il = -0.5_dp*SIN(arg_il)*(pi/cutoff_s)
     413              :                               CASE (nnp_cut_tanh)
     414     32512716 :                                  tanh_il = TANH(1.0_dp - r3/cutoff_s)
     415     32512716 :                                  fcut3_il = tanh_il**3
     416     32512716 :                                  dfcut3_il = (-3.0_dp/cutoff_s)*(tanh_il**2 - tanh_il**4)
     417              :                               CASE DEFAULT
     418     32517162 :                                  CPABORT("NNP| Cutoff function unknown")
     419              :                               END SELECT
     420              : 
     421     32517162 :                               ftot_il = fc_cache1(j)*fc_cache2(k)*fcut3_il
     422     32517162 :                               dfcutdr1_il = dfc_cache1(j)*fc_cache2(k)*fcut3_il
     423     32517162 :                               dfcutdr2_il = fc_cache1(j)*dfc_cache2(k)*fcut3_il
     424     32517162 :                               dfcutdr3_il = fc_cache1(j)*fc_cache2(k)*dfcut3_il
     425              : 
     426     32517162 :                               r1_inv = 1.0_dp/r1; r2_inv = 1.0_dp/r2; r3_inv = 1.0_dp/r3
     427    130068648 :                               dr1dx_il(:) = rvect1(:)*r1_inv
     428    130068648 :                               dr2dx_il(:) = rvect2(:)*r2_inv
     429    130068648 :                               dr3dx_il(:) = rvect3(:)*r3_inv
     430              : 
     431     32517162 :                               inv_g2_il = 1.0_dp/(g_il*g_il)
     432    130068648 :                               DO ii = 1, 3
     433     97551486 :                                  dgdx_t1 = 2.0_dp*r2*dr1dx_il(ii)
     434     97551486 :                                  dgdx_t2 = 2.0_dp*r1*dr2dx_il(ii)
     435              :                                  dcosbase1_il(ii) = -2.0_dp*(rvect1(ii) + rvect2(ii))*g_il &
     436     97551486 :                                                     - f_il*(-(dgdx_t1 + dgdx_t2))
     437              :                                  dcosbase2_il(ii) = 2.0_dp*(rvect3(ii) + rvect1(ii))*g_il &
     438     97551486 :                                                     - f_il*dgdx_t1
     439              :                                  dcosbase3_il(ii) = 2.0_dp*(rvect2(ii) - rvect3(ii))*g_il &
     440    130068648 :                                                     - f_il*dgdx_t2
     441              :                               END DO
     442              : 
     443              :                               ! -- fused SF loop: compute + direct scatter --
     444    224824983 :                               DO sf = 1, n_symf_s
     445    192307821 :                                  m = off + grp_il%symf(sf)
     446    192307821 :                                  lam_il = grp_il%pack_lam(sf)
     447    192307821 :                                  zeta_il = grp_il%pack_zeta(sf)
     448    192307821 :                                  eta_il = grp_il%pack_eta(sf)
     449    192307821 :                                  prefzeta_il = grp_il%pack_prefzeta(sf)
     450              : 
     451    192307821 :                                  tmp_il = 1.0_dp + lam_il*costheta_il
     452    192307821 :                                  IF (tmp_il <= 0.0_dp) THEN
     453              :                                     tmpzeta_il = 0.0_dp
     454              :                                     angular_il = 0.0_dp
     455              :                                  ELSE
     456    192307821 :                                     IF (grp_il%pack_use_int_zeta(sf)) THEN
     457    192307821 :                                        izeta_il = grp_il%pack_izeta(sf)
     458    192307821 :                                        tmpzeta_il = tmp_il**(izeta_il - 1)
     459              :                                     ELSE
     460            0 :                                        tmpzeta_il = tmp_il**(zeta_il - 1.0_dp)
     461              :                                     END IF
     462    192307821 :                                     angular_il = tmpzeta_il*tmp_il
     463              :                                  END IF
     464              : 
     465    192307821 :                                  symtmp_il = EXP(-eta_il*r2sum_il)
     466    192307821 :                                  sym_il = prefzeta_il*angular_il*symtmp_il*ftot_il
     467    192307821 :                                  ang_y(m - off) = ang_y(m - off) + sym_il
     468              : 
     469    192307821 :                                  pref_lam_il = zeta_il*tmpzeta_il*lam_il*inv_g2_il
     470    192307821 :                                  tmp_il = -2.0_dp*symtmp_il*eta_il
     471    192307821 :                                  dsymdr1_il = tmp_il*r1
     472    192307821 :                                  dsymdr2_il = tmp_il*r2
     473    192307821 :                                  dsymdr3_il = tmp_il*r3
     474              : 
     475    192307821 :                                  pref_il = prefzeta_il*symtmp_il*ftot_il
     476    192307821 :                                  tmp1_il = prefzeta_il*angular_il*(ftot_il*dsymdr1_il + dfcutdr1_il*symtmp_il)
     477    192307821 :                                  tmp2_il = prefzeta_il*angular_il*(ftot_il*dsymdr2_il + dfcutdr2_il*symtmp_il)
     478    192307821 :                                  tmp3_il = prefzeta_il*angular_il*(ftot_il*dsymdr3_il + dfcutdr3_il*symtmp_il)
     479              : 
     480    769231284 :                                  DO ii = 1, 3
     481              :                                     f_jj_il(ii) = pref_il*pref_lam_il*dcosbase2_il(ii) &
     482    576923463 :                                                   - tmp1_il*dr1dx_il(ii) + tmp3_il*dr3dx_il(ii)
     483              :                                     f_kk_il(ii) = pref_il*pref_lam_il*dcosbase3_il(ii) &
     484    576923463 :                                                   - tmp2_il*dr2dx_il(ii) - tmp3_il*dr3dx_il(ii)
     485              :                                     self_dGdr(ii, m) = self_dGdr(ii, m) &
     486              :                                                        + pref_il*pref_lam_il*dcosbase1_il(ii) &
     487    576923463 :                                                        + tmp1_il*dr1dx_il(ii) + tmp2_il*dr2dx_il(ii)
     488    576923463 :                                     jj_buf(ii, sf, j) = jj_buf(ii, sf, j) + f_jj_il(ii)
     489    769231284 :                                     kk_buf(ii, sf, k) = kk_buf(ii, sf, k) + f_kk_il(ii)
     490              :                                  END DO
     491    224824983 :                                  IF (PRESENT(stress)) THEN
     492    135046240 :                                     DO l = 1, 3
     493              :                                        stress(:, l, m) = stress(:, l, m) &
     494    438900280 :                                                          - rvect1(:)*f_jj_il(l) - rvect2(:)*f_kk_il(l)
     495              :                                     END DO
     496              :                                  END IF
     497              :                               END DO
     498              : 
     499              :                            END IF
     500              :                         END DO
     501              :                      END DO
     502              :                      END ASSOCIATE
     503              :                   END IF
     504              :                END DO
     505        34241 :                CALL timestop(handle_sf)
     506              :             ELSE
     507              :                !loop over radial sym fnct grps
     508       229072 :                CALL timeset('nnp_acsf_radial', handle_sf)
     509       794360 :                DO s = 1, nnp%rad(ind)%n_symfgrp
     510              :                   !loop over associated neighbors
     511      2724876 :                   DO j = 1, neighbor%n_rad(s)
     512      7722064 :                      rvect1 = neighbor%rad(s)%dist(1:3, j)
     513      1930516 :                      r1 = neighbor%rad(s)%dist(4, j)
     514      1930516 :                      CALL nnp_calc_rad(nnp, ind, s, rvect1, r1, radial_symtmp(1:nnp%rad(ind)%symfgrp(s)%n_symf))
     515     17187322 :                      DO sf = 1, nnp%rad(ind)%symfgrp(s)%n_symf
     516     14691518 :                         m = nnp%rad(ind)%symfgrp(s)%symf(sf)
     517     16622034 :                         rad_y(m) = rad_y(m) + radial_symtmp(sf)
     518              :                      END DO
     519              :                   END DO
     520              :                END DO
     521       229072 :                CALL timestop(handle_sf)
     522              : 
     523              :                !loop over angular sym fnct grps
     524       229072 :                CALL timeset('nnp_acsf_angular', handle_sf)
     525       229072 :                off = nnp%n_rad(ind)
     526       692144 :                DO s = 1, nnp%ang(ind)%n_symfgrp
     527       463072 :                   cutoff_s = nnp%ang(ind)%symfgrp(s)%cutoff
     528       463072 :                   cutoff_sqr = cutoff_s*cutoff_s
     529       463072 :                   n_symf_s = nnp%ang(ind)%symfgrp(s)%n_symf
     530       463072 :                   n_ang1_s = neighbor%n_ang1(s)
     531              : 
     532              :                   ! Precompute cutoff values for ang1 neighbors (no derivatives needed)
     533              :                   CALL nnp_fill_fc_cache(neighbor%ang1(s)%dist, n_ang1_s, &
     534       463072 :                                          nnp%cut_type, cutoff_s, fc_cache1)
     535              : 
     536       692144 :                   IF (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2)) THEN
     537      1121116 :                      DO j = 1, n_ang1_s
     538      3977040 :                         rvect1 = neighbor%ang1(s)%dist(1:3, j)
     539       994260 :                         r1 = neighbor%ang1(s)%dist(4, j)
     540     22659329 :                         DO k = j + 1, n_ang1_s
     541     86152852 :                            rvect2 = neighbor%ang1(s)%dist(1:3, k)
     542     21538213 :                            r2 = neighbor%ang1(s)%dist(4, k)
     543     21538213 :                            rvect3(1) = rvect2(1) - rvect1(1)
     544     21538213 :                            rvect3(2) = rvect2(2) - rvect1(2)
     545     21538213 :                            rvect3(3) = rvect2(3) - rvect1(3)
     546     21538213 :                            r3_sqr = rvect3(1)*rvect3(1) + rvect3(2)*rvect3(2) + rvect3(3)*rvect3(3)
     547     22532473 :                            IF (r3_sqr < cutoff_sqr) THEN
     548      9944571 :                               r3 = SQRT(r3_sqr)
     549              :                               CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, &
     550              :                                                 fc_cache1(j), 0.0_dp, fc_cache1(k), 0.0_dp, &
     551      9944571 :                                                 angular_symtmp(1:n_symf_s))
     552     61315369 :                               DO sf = 1, n_symf_s
     553     51370798 :                                  m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
     554     61315369 :                                  ang_y(m - off) = ang_y(m - off) + angular_symtmp(sf)
     555              :                               END DO
     556              :                            END IF
     557              :                         END DO
     558              :                      END DO
     559              :                   ELSE
     560              :                      ! Precompute cutoff values for ang2 neighbors
     561       336216 :                      n_ang2_s = neighbor%n_ang2(s)
     562              :                      CALL nnp_fill_fc_cache(neighbor%ang2(s)%dist, n_ang2_s, &
     563       336216 :                                             nnp%cut_type, cutoff_s, fc_cache2)
     564              : 
     565      1719216 :                      DO j = 1, n_ang1_s
     566      5532000 :                         rvect1 = neighbor%ang1(s)%dist(1:3, j)
     567      1383000 :                         r1 = neighbor%ang1(s)%dist(4, j)
     568     39030025 :                         DO k = 1, n_ang2_s
     569    149243236 :                            rvect2 = neighbor%ang2(s)%dist(1:3, k)
     570     37310809 :                            r2 = neighbor%ang2(s)%dist(4, k)
     571     37310809 :                            rvect3(1) = rvect2(1) - rvect1(1)
     572     37310809 :                            rvect3(2) = rvect2(2) - rvect1(2)
     573     37310809 :                            rvect3(3) = rvect2(3) - rvect1(3)
     574     37310809 :                            r3_sqr = rvect3(1)*rvect3(1) + rvect3(2)*rvect3(2) + rvect3(3)*rvect3(3)
     575     38693809 :                            IF (r3_sqr < cutoff_sqr) THEN
     576     17429498 :                               r3 = SQRT(r3_sqr)
     577              :                               CALL nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, &
     578              :                                                 fc_cache1(j), 0.0_dp, fc_cache2(k), 0.0_dp, &
     579     17429498 :                                                 angular_symtmp(1:n_symf_s))
     580    121983157 :                               DO sf = 1, n_symf_s
     581    104553659 :                                  m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
     582    121983157 :                                  ang_y(m - off) = ang_y(m - off) + angular_symtmp(sf)
     583              :                               END DO
     584              :                            END IF
     585              :                         END DO
     586              :                      END DO
     587              :                   END IF
     588              :                END DO
     589       229072 :                CALL timestop(handle_sf)
     590              :             END IF
     591              : 
     592              :          END ASSOCIATE
     593              : 
     594              :          ! fc_cache1/2 and dfc_cache1/2 are persistent workspace; nothing to deallocate here.
     595              : 
     596              :       END ASSOCIATE
     597              : 
     598              :       !check extrapolation
     599       263313 :       CALL nnp_check_extrapolation(nnp, ind, rad_y, ang_y)
     600              : 
     601       263313 :       IF (PRESENT(stress)) THEN
     602         4800 :          CALL nnp_scale_acsf(nnp, ind, do_forces, arc, rad_y, ang_y, stress)
     603              :       ELSE
     604       258513 :          CALL nnp_scale_acsf(nnp, ind, do_forces, arc, rad_y, ang_y)
     605              :       END IF
     606              : 
     607       263313 :       DEALLOCATE (rad_y, ang_y)
     608              : 
     609       263313 :       CALL timestop(handle)
     610              : 
     611       263313 :    END SUBROUTINE nnp_calc_acsf
     612              : 
     613              : ! **************************************************************************************************
     614              : !> \brief Fill the per-neighbour fc and dfc cutoff-function caches used by the
     615              : !>        force branch of the angular ACSF kernel.
     616              : !> \param dist       (4, :) neighbour array; column 4 holds the scalar distance
     617              : !> \param n          number of neighbors to process
     618              : !> \param cut_type   cutoff function selector (nnp_cut_cos / nnp_cut_tanh)
     619              : !> \param cutoff_s   per-group cutoff radius
     620              : !> \param fc_cache   output fc values, sized >= n
     621              : !> \param dfc_cache  output dfc values, sized >= n
     622              : ! **************************************************************************************************
     623       134292 :    PURE SUBROUTINE nnp_fill_fc_dfc_cache(dist, n, cut_type, cutoff_s, fc_cache, dfc_cache)
     624              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: dist
     625              :       INTEGER, INTENT(IN)                                :: n, cut_type
     626              :       REAL(KIND=dp), INTENT(IN)                          :: cutoff_s
     627              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: fc_cache, dfc_cache
     628              : 
     629              :       INTEGER                                            :: j
     630              :       REAL(KIND=dp)                                      :: arg_tmp, r_tmp, tanh_tmp
     631              : 
     632      5136718 :       DO j = 1, n
     633      5002464 :          r_tmp = dist(4, j)
     634       134254 :          SELECT CASE (cut_type)
     635              :          CASE (nnp_cut_cos)
     636        17790 :             arg_tmp = pi*r_tmp/cutoff_s
     637        17790 :             fc_cache(j) = 0.5_dp*(COS(arg_tmp) + 1.0_dp)
     638        17790 :             dfc_cache(j) = -0.5_dp*SIN(arg_tmp)*(pi/cutoff_s)
     639              :          CASE (nnp_cut_tanh)
     640      4984674 :             tanh_tmp = TANH(1.0_dp - r_tmp/cutoff_s)
     641      4984674 :             fc_cache(j) = tanh_tmp**3
     642      5002464 :             dfc_cache(j) = (-3.0_dp/cutoff_s)*(tanh_tmp**2 - tanh_tmp**4)
     643              :          END SELECT
     644              :       END DO
     645              : 
     646       134254 :    END SUBROUTINE nnp_fill_fc_dfc_cache
     647              : 
     648              : ! **************************************************************************************************
     649              : !> \brief Fill the per-neighbour fc cutoff-function cache for the sym-only
     650              : !>        (no-forces) branch of the angular ACSF kernel. Derivatives are not needed.
     651              : !> \param dist       (4, :) neighbour array; column 4 holds the scalar distance
     652              : !> \param n          number of neighbors to process
     653              : !> \param cut_type   cutoff function selector (nnp_cut_cos / nnp_cut_tanh)
     654              : !> \param cutoff_s   per-group cutoff radius
     655              : !> \param fc_cache   output fc values, sized >= n
     656              : ! **************************************************************************************************
     657       799288 :    PURE SUBROUTINE nnp_fill_fc_cache(dist, n, cut_type, cutoff_s, fc_cache)
     658              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(IN)         :: dist
     659              :       INTEGER, INTENT(IN)                                :: n, cut_type
     660              :       REAL(KIND=dp), INTENT(IN)                          :: cutoff_s
     661              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: fc_cache
     662              : 
     663              :       INTEGER                                            :: j
     664              :       REAL(KIND=dp)                                      :: r_tmp, tanh_tmp
     665              : 
     666      3831208 :       DO j = 1, n
     667      3031920 :          r_tmp = dist(4, j)
     668       799288 :          SELECT CASE (cut_type)
     669              :          CASE (nnp_cut_cos)
     670       584048 :             fc_cache(j) = 0.5_dp*(COS(pi*r_tmp/cutoff_s) + 1.0_dp)
     671              :          CASE (nnp_cut_tanh)
     672      2447872 :             tanh_tmp = TANH(1.0_dp - r_tmp/cutoff_s)
     673      3031920 :             fc_cache(j) = tanh_tmp**3
     674              :          END SELECT
     675              :       END DO
     676              : 
     677       799288 :    END SUBROUTINE nnp_fill_fc_cache
     678              : 
     679              : ! **************************************************************************************************
     680              : !> \brief Prepare or update the linked-cell / Verlet cache for the current
     681              : !>        geometry. Lazily allocates nnp%cell_list_cache and
     682              : !>        nnp%neighbor_interface_state, then delegates to
     683              : !>        nnp_prepare_cell_list_cache and nnp_neighbor_interface_prepare. Call
     684              : !>        once per force eval before the per-atom loop; cheap on re-entry when
     685              : !>        nothing has changed.
     686              : !> \param nnp NNP environment whose persistent caches are to be (re)built.
     687              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     688              : ! **************************************************************************************************
     689        55599 :    SUBROUTINE nnp_prepare_neighbor_cache(nnp)
     690              : 
     691              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp
     692              : 
     693              :       INTEGER                                            :: ind
     694              : 
     695        57459 :       IF (.NOT. ALLOCATED(nnp%cell_list_cache)) ALLOCATE (nnp%cell_list_cache)
     696        55599 :       IF (.NOT. ALLOCATED(nnp%neighbor_interface_state)) ALLOCATE (nnp%neighbor_interface_state)
     697        55599 :       CALL nnp_prepare_cell_list_cache(nnp)
     698        55599 :       CALL nnp_neighbor_interface_prepare(nnp)
     699              : 
     700              :       ! Build the radial spline tables here, serially, before the (possibly
     701              :       ! atom-level-threaded) per-atom loop. The lazy first-touch build inside
     702              :       ! nnp_calc_acsf would otherwise race across threads on the first step.
     703       221932 :       DO ind = 1, nnp%n_ele
     704       221932 :          IF (nnp%rad(ind)%n_symfgrp > 0) THEN
     705       166363 :             IF (.NOT. nnp%rad(ind)%symfgrp(1)%spline_built) THEN
     706           30 :                CALL nnp_build_radial_splines(nnp)
     707           30 :                EXIT
     708              :             END IF
     709              :          END IF
     710              :       END DO
     711              : 
     712        55599 :    END SUBROUTINE nnp_prepare_neighbor_cache
     713              : 
     714              : ! **************************************************************************************************
     715              : !> \brief Check if the nnp is extrapolating
     716              : !> \param nnp ...
     717              : !> \param ind ...
     718              : !> \param rad_y radial SF values of the current atom
     719              : !> \param ang_y angular SF values of the current atom
     720              : !> \date   2020-10-10
     721              : !> \author Christoph Schran (christoph.schran@rub.de)
     722              : ! **************************************************************************************************
     723       263313 :    SUBROUTINE nnp_check_extrapolation(nnp, ind, rad_y, ang_y)
     724              : 
     725              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp
     726              :       INTEGER, INTENT(IN)                                :: ind
     727              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: rad_y, ang_y
     728              : 
     729              :       REAL(KIND=dp), PARAMETER                           :: threshold = 0.0001_dp
     730              : 
     731              :       INTEGER                                            :: j
     732              :       LOGICAL                                            :: extrapolate
     733              : 
     734       263313 :       extrapolate = .FALSE.
     735              : 
     736      4015869 :       DO j = 1, nnp%n_rad(ind)
     737      4015869 :          IF (rad_y(j) - nnp%rad(ind)%loc_max(j) > threshold) THEN
     738              :             extrapolate = .TRUE.
     739      3752556 :          ELSE IF (-rad_y(j) + nnp%rad(ind)%loc_min(j) > threshold) THEN
     740          164 :             extrapolate = .TRUE.
     741              :          END IF
     742              :       END DO
     743      2306569 :       DO j = 1, nnp%n_ang(ind)
     744      2306569 :          IF (ang_y(j) - nnp%ang(ind)%loc_max(j) > threshold) THEN
     745              :             extrapolate = .TRUE.
     746      2043256 :          ELSE IF (-ang_y(j) + nnp%ang(ind)%loc_min(j) > threshold) THEN
     747           30 :             extrapolate = .TRUE.
     748              :          END IF
     749              :       END DO
     750              : 
     751              :       ! Threads only ever set the shared flag to .TRUE. here (it is reset
     752              :       ! serially in nnp_calc_energy_force), so concurrent writes are benign.
     753       263313 :       IF (extrapolate) nnp%output_expol = .TRUE.
     754              : 
     755       263313 :    END SUBROUTINE nnp_check_extrapolation
     756              : 
     757              : ! **************************************************************************************************
     758              : !> \brief Scale and center symmetry functions (and gradients)
     759              : !> \param nnp ...
     760              : !> \param ind ...
     761              : !> \param do_forces ...
     762              : !> \param arc network whose input layer receives the scaled SF values
     763              : !> \param rad_y radial SF values of the current atom
     764              : !> \param ang_y angular SF values of the current atom
     765              : !> \param stress ...
     766              : !> \date   2020-10-10
     767              : !> \author Christoph Schran (christoph.schran@rub.de)
     768              : ! **************************************************************************************************
     769       263313 :    SUBROUTINE nnp_scale_acsf(nnp, ind, do_forces, arc, rad_y, ang_y, stress)
     770              : 
     771              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp
     772              :       INTEGER, INTENT(IN)                                :: ind
     773              :       LOGICAL, INTENT(IN)                                :: do_forces
     774              :       TYPE(nnp_arc_type), INTENT(INOUT)                  :: arc
     775              :       REAL(KIND=dp), DIMENSION(:), INTENT(IN)            :: rad_y, ang_y
     776              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(INOUT), &
     777              :          OPTIONAL                                        :: stress
     778              : 
     779              :       INTEGER                                            :: j, k, m, n_ang1_s, n_ang2_s, n_symf_s, &
     780              :                                                             off, s, sf, tid
     781              :       LOGICAL                                            :: homo_grp
     782              :       REAL(KIND=dp)                                      :: scale
     783              : 
     784       263313 :       tid = 1
     785       263313 : !$    tid = omp_get_thread_num() + 1
     786              : 
     787              : ! INOUT (not OUT): stress is per-input-node and accumulates across central atoms.
     788              : 
     789       263313 :       IF (nnp%center_acsf) THEN
     790      4015869 :          DO j = 1, nnp%n_rad(ind)
     791      4015869 :             arc%layer(1)%node(j) = rad_y(j) - nnp%rad(ind)%loc_av(j)
     792              :          END DO
     793       263313 :          off = nnp%n_rad(ind)
     794      2306569 :          DO j = 1, nnp%n_ang(ind)
     795      2306569 :             arc%layer(1)%node(j + off) = ang_y(j) - nnp%ang(ind)%loc_av(j)
     796              :          END DO
     797              : 
     798       263313 :          IF (nnp%scale_acsf) THEN
     799      4015869 :             DO j = 1, nnp%n_rad(ind)
     800              :                arc%layer(1)%node(j) = arc%layer(1)%node(j)/ &
     801      4015869 :                                       (nnp%rad(ind)%loc_max(j) - nnp%rad(ind)%loc_min(j))*(nnp%scmax - nnp%scmin) + nnp%scmin
     802              :             END DO
     803       263313 :             off = nnp%n_rad(ind)
     804      2306569 :             DO j = 1, nnp%n_ang(ind)
     805              :                arc%layer(1)%node(j + off) = arc%layer(1)%node(j + off)/ &
     806      2306569 :                                             (nnp%ang(ind)%loc_max(j) - nnp%ang(ind)%loc_min(j))*(nnp%scmax - nnp%scmin) + nnp%scmin
     807              :             END DO
     808              :          END IF
     809            0 :       ELSE IF (nnp%scale_acsf) THEN
     810            0 :          DO j = 1, nnp%n_rad(ind)
     811              :             arc%layer(1)%node(j) = (rad_y(j) - nnp%rad(ind)%loc_min(j))/ &
     812              :                                    (nnp%rad(ind)%loc_max(j) - nnp%rad(ind)%loc_min(j))* &
     813            0 :                                    (nnp%scmax - nnp%scmin) + nnp%scmin
     814              :          END DO
     815            0 :          off = nnp%n_rad(ind)
     816            0 :          DO j = 1, nnp%n_ang(ind)
     817              :             arc%layer(1)%node(j + off) = (ang_y(j) - nnp%ang(ind)%loc_min(j))/ &
     818              :                                          (nnp%ang(ind)%loc_max(j) - nnp%ang(ind)%loc_min(j))* &
     819            0 :                                          (nnp%scmax - nnp%scmin) + nnp%scmin
     820              :          END DO
     821            0 :       ELSE IF (nnp%scale_sigma_acsf) THEN
     822            0 :          DO j = 1, nnp%n_rad(ind)
     823              :             arc%layer(1)%node(j) = (rad_y(j) - nnp%rad(ind)%loc_av(j))/ &
     824            0 :                                    nnp%rad(ind)%sigma(j)*(nnp%scmax - nnp%scmin) + nnp%scmin
     825              :          END DO
     826            0 :          off = nnp%n_rad(ind)
     827            0 :          DO j = 1, nnp%n_ang(ind)
     828              :             arc%layer(1)%node(j + off) = (ang_y(j) - nnp%ang(ind)%loc_av(j))/ &
     829            0 :                                          nnp%ang(ind)%sigma(j)*(nnp%scmax - nnp%scmin) + nnp%scmin
     830              :          END DO
     831              :       ELSE
     832            0 :          DO j = 1, nnp%n_rad(ind)
     833            0 :             arc%layer(1)%node(j) = rad_y(j)
     834              :          END DO
     835            0 :          off = nnp%n_rad(ind)
     836            0 :          DO j = 1, nnp%n_ang(ind)
     837            0 :             arc%layer(1)%node(j + off) = ang_y(j)
     838              :          END DO
     839              :       END IF
     840              : 
     841       263313 :       IF (do_forces .AND. (nnp%scale_acsf .OR. nnp%scale_sigma_acsf)) THEN
     842              :          ! Scale the per-neighbor dGdr slabs in workspace state. Only the
     843              :          ! actually-populated entries in each slab need touching: for each
     844              :          ! group s the valid ranges are sf=1..n_symf_s and j=1..n_rad(s)
     845              :          ! (radial) or j=1..n_ang1_s, k=1..n_ang2_s (angular). The self
     846              :          ! contribution lives in self_dGdr(:, m).
     847              :          ASSOCIATE (workspace => nnp%neighbor_interface_state%workspace(ind, tid), &
     848              :                     neighbor => nnp%neighbor_interface_state%workspace(ind, tid)%neighbor, &
     849              :                     self_dGdr => nnp%neighbor_interface_state%workspace(ind, tid)%self_dGdr)
     850              : 
     851              :             ! Radial groups
     852       108931 :             DO s = 1, nnp%rad(ind)%n_symfgrp
     853        74690 :                n_symf_s = nnp%rad(ind)%symfgrp(s)%n_symf
     854        34241 :                ASSOCIATE (rad_buf => workspace%dGdr_rad(s)%data)
     855       644242 :                DO sf = 1, n_symf_s
     856       569552 :                   m = nnp%rad(ind)%symfgrp(s)%symf(sf)
     857       569552 :                   IF (nnp%scale_acsf) THEN
     858              :                      scale = (nnp%scmax - nnp%scmin)/ &
     859       569552 :                              (nnp%rad(ind)%loc_max(m) - nnp%rad(ind)%loc_min(m))
     860              :                   ELSE
     861            0 :                      scale = (nnp%scmax - nnp%scmin)/nnp%rad(ind)%sigma(m)
     862              :                   END IF
     863       569552 :                   self_dGdr(1, m) = self_dGdr(1, m)*scale
     864       569552 :                   self_dGdr(2, m) = self_dGdr(2, m)*scale
     865       569552 :                   self_dGdr(3, m) = self_dGdr(3, m)*scale
     866     24675094 :                   DO j = 1, neighbor%n_rad(s)
     867     24030852 :                      rad_buf(1, sf, j) = rad_buf(1, sf, j)*scale
     868     24030852 :                      rad_buf(2, sf, j) = rad_buf(2, sf, j)*scale
     869     24600404 :                      rad_buf(3, sf, j) = rad_buf(3, sf, j)*scale
     870              :                   END DO
     871              :                END DO
     872              :                END ASSOCIATE
     873              :             END DO
     874              : 
     875              :             ! Angular groups
     876        34241 :             off = nnp%n_rad(ind)
     877       157775 :             DO s = 1, nnp%ang(ind)%n_symfgrp
     878        89293 :                n_symf_s = nnp%ang(ind)%symfgrp(s)%n_symf
     879        89293 :                n_ang1_s = neighbor%n_ang1(s)
     880        89293 :                homo_grp = (nnp%ang(ind)%symfgrp(s)%ele(1) == nnp%ang(ind)%symfgrp(s)%ele(2))
     881        89293 :                IF (homo_grp) THEN
     882              :                   ! kk slab is also indexed in ind_ang1 in the homo case
     883              :                   n_ang2_s = n_ang1_s
     884              :                ELSE
     885        44961 :                   n_ang2_s = neighbor%n_ang2(s)
     886              :                END IF
     887              :                ASSOCIATE (jj_buf => workspace%dGdr_ang_jj(s)%data, &
     888        34241 :                           kk_buf => workspace%dGdr_ang_kk(s)%data)
     889       508697 :                DO sf = 1, n_symf_s
     890       419404 :                   m = off + nnp%ang(ind)%symfgrp(s)%symf(sf)
     891       419404 :                   IF (nnp%scale_acsf) THEN
     892              :                      scale = (nnp%scmax - nnp%scmin)/ &
     893       419404 :                              (nnp%ang(ind)%loc_max(m - off) - nnp%ang(ind)%loc_min(m - off))
     894              :                   ELSE
     895            0 :                      scale = (nnp%scmax - nnp%scmin)/nnp%ang(ind)%sigma(m - off)
     896              :                   END IF
     897       419404 :                   self_dGdr(1, m) = self_dGdr(1, m)*scale
     898       419404 :                   self_dGdr(2, m) = self_dGdr(2, m)*scale
     899       419404 :                   self_dGdr(3, m) = self_dGdr(3, m)*scale
     900     21443564 :                   DO j = 1, n_ang1_s
     901     21024160 :                      jj_buf(1, sf, j) = jj_buf(1, sf, j)*scale
     902     21024160 :                      jj_buf(2, sf, j) = jj_buf(2, sf, j)*scale
     903     21443564 :                      jj_buf(3, sf, j) = jj_buf(3, sf, j)*scale
     904              :                   END DO
     905     14471572 :                   DO k = 1, n_ang2_s
     906     13962875 :                      kk_buf(1, sf, k) = kk_buf(1, sf, k)*scale
     907     13962875 :                      kk_buf(2, sf, k) = kk_buf(2, sf, k)*scale
     908     14382279 :                      kk_buf(3, sf, k) = kk_buf(3, sf, k)*scale
     909              :                   END DO
     910              :                END DO
     911              :                END ASSOCIATE
     912              :             END DO
     913              : 
     914              :          END ASSOCIATE
     915              :       END IF
     916              : 
     917       263313 :       IF (PRESENT(stress)) THEN
     918         4800 :          IF (nnp%scale_acsf) THEN
     919        81600 :             DO j = 1, nnp%n_rad(ind)
     920              :                stress(:, :, j) = stress(:, :, j)/(nnp%rad(ind)%loc_max(j) - nnp%rad(ind)%loc_min(j))* &
     921      1003200 :                                  (nnp%scmax - nnp%scmin)
     922              :             END DO
     923         4800 :             off = nnp%n_rad(ind)
     924        62400 :             DO j = 1, nnp%n_ang(ind)
     925              :                stress(:, :, j + off) = stress(:, :, j + off)/ &
     926              :                                        (nnp%ang(ind)%loc_max(j) - nnp%ang(ind)%loc_min(j))* &
     927       753600 :                                        (nnp%scmax - nnp%scmin)
     928              :             END DO
     929            0 :          ELSE IF (nnp%scale_sigma_acsf) THEN
     930            0 :             DO j = 1, nnp%n_rad(ind)
     931            0 :                stress(:, :, j) = stress(:, :, j)/nnp%rad(ind)%sigma(j)*(nnp%scmax - nnp%scmin)
     932              :             END DO
     933            0 :             off = nnp%n_rad(ind)
     934            0 :             DO j = 1, nnp%n_ang(ind)
     935            0 :                stress(:, :, j + off) = stress(:, :, j + off)/nnp%ang(ind)%sigma(j)*(nnp%scmax - nnp%scmin)
     936              :             END DO
     937              :          END IF
     938              :       END IF
     939              : 
     940       263313 :    END SUBROUTINE nnp_scale_acsf
     941              : 
     942              : ! **************************************************************************************************
     943              : !> \brief Calculate radial symmetry function and gradient (optional)
     944              : !> \param nnp ...
     945              : !> \param ind ...
     946              : !> \param s ...
     947              : !> \param rvect ...
     948              : !> \param r ...
     949              : !> \param sym ...
     950              : !> \param force ...
     951              : !> \date   2020-10-10
     952              : !> \author Christoph Schran (christoph.schran@rub.de)
     953              : ! **************************************************************************************************
     954      4898086 :    SUBROUTINE nnp_calc_rad(nnp, ind, s, rvect, r, sym, force)
     955              : 
     956              :       TYPE(nnp_type), INTENT(IN), TARGET                 :: nnp
     957              :       INTEGER, INTENT(IN)                                :: ind, s
     958              :       REAL(KIND=dp), DIMENSION(3), INTENT(IN)            :: rvect
     959              :       REAL(KIND=dp), INTENT(IN)                          :: r
     960              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: sym
     961              :       REAL(KIND=dp), DIMENSION(:, :), INTENT(OUT), &
     962              :          OPTIONAL                                        :: force
     963              : 
     964              :       INTEGER                                            :: i, n_symf, sf
     965              :       REAL(KIND=dp)                                      :: dh00, dh01, dh10, dh11, drdx_x, drdx_y, &
     966              :                                                             drdx_z, dsymdr_full, dyi, dyi1, h00, &
     967              :                                                             h01, h10, h10_dx, h11, h11_dx, r_inv, &
     968              :                                                             t, t2, t3, yi, yi1
     969              :       TYPE(nnp_symfgrp_type), POINTER                    :: grp
     970              : 
     971      4898086 :       grp => nnp%rad(ind)%symfgrp(s)
     972      4898086 :       n_symf = grp%n_symf
     973              : 
     974              :       ! Group-shared Hermite cubic spline. All SFs in this radial group share
     975              :       ! grp%cutoff, hence the same uniform grid, so the interpolation parameters
     976              :       ! (i, t, h00..h11, dh00..dh11) are computed once outside the SF loop; the
     977              :       ! inner loop then streams 4 contiguous reads per SF from spline_y/spline_dy.
     978              :       !
     979              :       ! Out-of-range clamping: for r past spline_x_max (= grp%cutoff) every SF
     980              :       ! sees the boundary value of its tabulated y(r), which the build routine
     981              :       ! pinned to 0 since fcut(cutoff) = 0, so the clamp returns sym = 0.
     982      4898086 :       IF (r >= grp%spline_x_max) THEN
     983            0 :          DO sf = 1, n_symf
     984            0 :             sym(sf) = 0.0_dp
     985              :          END DO
     986            0 :          IF (PRESENT(force)) THEN
     987            0 :             DO sf = 1, n_symf
     988            0 :                force(1, sf) = 0.0_dp
     989            0 :                force(2, sf) = 0.0_dp
     990            0 :                force(3, sf) = 0.0_dp
     991              :             END DO
     992              :          END IF
     993            0 :          RETURN
     994              :       END IF
     995              : 
     996      4898086 :       i = INT(r*grp%spline_dx_inv) + 1
     997      4898086 :       IF (i < 1) i = 1
     998      4898086 :       IF (i > grp%spline_n - 1) i = grp%spline_n - 1
     999              : 
    1000      4898086 :       t = (r - REAL(i - 1, KIND=dp)*grp%spline_dx)*grp%spline_dx_inv
    1001      4898086 :       t2 = t*t
    1002      4898086 :       t3 = t2*t
    1003              : 
    1004      4898086 :       h00 = 2.0_dp*t3 - 3.0_dp*t2 + 1.0_dp
    1005      4898086 :       h10 = t3 - 2.0_dp*t2 + t
    1006      4898086 :       h01 = -2.0_dp*t3 + 3.0_dp*t2
    1007      4898086 :       h11 = t3 - t2
    1008      4898086 :       h10_dx = h10*grp%spline_dx
    1009      4898086 :       h11_dx = h11*grp%spline_dx
    1010              : 
    1011      4898086 :       IF (PRESENT(force)) THEN
    1012      2967570 :          dh00 = 6.0_dp*(t2 - t)
    1013      2967570 :          dh10 = 3.0_dp*t2 - 4.0_dp*t + 1.0_dp
    1014      2967570 :          dh01 = -dh00
    1015      2967570 :          dh11 = 3.0_dp*t2 - 2.0_dp*t
    1016              : 
    1017      2967570 :          r_inv = 1.0_dp/r
    1018      2967570 :          drdx_x = rvect(1)*r_inv
    1019      2967570 :          drdx_y = rvect(2)*r_inv
    1020      2967570 :          drdx_z = rvect(3)*r_inv
    1021              : 
    1022              :          ! Vectorizable inner loop: contiguous reads on sf, no branches,
    1023              :          ! no function calls, no transcendentals.
    1024              :          ASSOCIATE (spy_i => grp%spline_y(:, i), spy_i1 => grp%spline_y(:, i + 1), &
    1025              :                     spdy_i => grp%spline_dy(:, i), spdy_i1 => grp%spline_dy(:, i + 1))
    1026      2967570 :             !$OMP SIMD PRIVATE(yi, yi1, dyi, dyi1, dsymdr_full)
    1027              :             DO sf = 1, n_symf
    1028     24030852 :                yi = spy_i(sf)
    1029     24030852 :                yi1 = spy_i1(sf)
    1030     24030852 :                dyi = spdy_i(sf)
    1031     24030852 :                dyi1 = spdy_i1(sf)
    1032     24030852 :                sym(sf) = h00*yi + h10_dx*dyi + h01*yi1 + h11_dx*dyi1
    1033     24030852 :                dsymdr_full = (dh00*yi + dh01*yi1)*grp%spline_dx_inv + dh10*dyi + dh11*dyi1
    1034     24030852 :                force(1, sf) = dsymdr_full*drdx_x
    1035     24030852 :                force(2, sf) = dsymdr_full*drdx_y
    1036     24030852 :                force(3, sf) = dsymdr_full*drdx_z
    1037              :             END DO
    1038              :          END ASSOCIATE
    1039              :       ELSE
    1040              :          ASSOCIATE (spy_i => grp%spline_y(:, i), spy_i1 => grp%spline_y(:, i + 1), &
    1041              :                     spdy_i => grp%spline_dy(:, i), spdy_i1 => grp%spline_dy(:, i + 1))
    1042      1930516 :             !$OMP SIMD
    1043              :             DO sf = 1, n_symf
    1044              :                sym(sf) = h00*spy_i(sf) + h10_dx*spdy_i(sf) + &
    1045     14691518 :                          h01*spy_i1(sf) + h11_dx*spdy_i1(sf)
    1046              :             END DO
    1047              :          END ASSOCIATE
    1048              :       END IF
    1049              : 
    1050              :    END SUBROUTINE nnp_calc_rad
    1051              : 
    1052              : ! **************************************************************************************************
    1053              : !> \brief Build sf-first (n_symf, n_grid) Hermite cubic spline tables for radial SFs.
    1054              : !>        Uses a custom sf-first layout (not splines_methods) so nnp_calc_rad can
    1055              : !>        stream contiguous SF values under !$OMP SIMD without indirect addressing.
    1056              : !> \param nnp ...
    1057              : ! **************************************************************************************************
    1058           30 :    SUBROUTINE nnp_build_radial_splines(nnp)
    1059              : 
    1060              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp
    1061              : 
    1062              :       CHARACTER(len=*), PARAMETER :: routineN = 'nnp_build_radial_splines'
    1063              : 
    1064              :       INTEGER                                            :: handle, ind, k, n_symf, p, s, sf
    1065              :       REAL(KIND=dp)                                      :: arg, cutoff, dfcutdr, dr, eta, exp_term, &
    1066              :                                                             fcut, r, rs, tanh_tmp
    1067              : 
    1068           30 :       CALL timeset(routineN, handle)
    1069              : 
    1070           99 :       DO ind = 1, nnp%n_ele
    1071          270 :          DO s = 1, nnp%rad(ind)%n_symfgrp
    1072           69 :             ASSOCIATE (grp => nnp%rad(ind)%symfgrp(s))
    1073          171 :                n_symf = grp%n_symf
    1074          171 :                cutoff = grp%cutoff
    1075          171 :                dr = cutoff/REAL(nnp%rad_spline_n - 1, KIND=dp)
    1076              : 
    1077            0 :                IF (ALLOCATED(grp%spline_y)) DEALLOCATE (grp%spline_y)
    1078          171 :                IF (ALLOCATED(grp%spline_dy)) DEALLOCATE (grp%spline_dy)
    1079          684 :                ALLOCATE (grp%spline_y(MAX(1, n_symf), nnp%rad_spline_n))
    1080          684 :                ALLOCATE (grp%spline_dy(MAX(1, n_symf), nnp%rad_spline_n))
    1081          171 :                grp%spline_n = nnp%rad_spline_n
    1082          171 :                grp%spline_dx = dr
    1083          171 :                grp%spline_dx_inv = 1.0_dp/dr
    1084          171 :                grp%spline_x_max = cutoff
    1085              : 
    1086              :                ! Fill the table SF-by-SF. The grid loop is the inner one
    1087              :                ! during build only; runtime nnp_calc_rad reads sf-first.
    1088         1649 :                DO sf = 1, n_symf
    1089         1478 :                   k = grp%symf(sf)
    1090         1478 :                   eta = nnp%rad(ind)%eta(k)
    1091         1478 :                   rs = nnp%rad(ind)%rs(k)
    1092              : 
    1093     12109425 :                   DO p = 1, nnp%rad_spline_n
    1094     12107776 :                      r = REAL(p - 1, KIND=dp)*dr
    1095              : 
    1096     12419072 :                      SELECT CASE (nnp%cut_type)
    1097              :                      CASE (nnp_cut_cos)
    1098       311296 :                         arg = pi*r/cutoff
    1099       311296 :                         fcut = 0.5_dp*(COS(arg) + 1.0_dp)
    1100       311296 :                         dfcutdr = -0.5_dp*SIN(arg)*(pi/cutoff)
    1101              :                      CASE (nnp_cut_tanh)
    1102     11796480 :                         tanh_tmp = TANH(1.0_dp - r/cutoff)
    1103     11796480 :                         fcut = tanh_tmp**3
    1104     11796480 :                         dfcutdr = (-3.0_dp/cutoff)*(tanh_tmp**2 - tanh_tmp**4)
    1105              :                      CASE DEFAULT
    1106     12107776 :                         CPABORT("NNP| Cutoff function unknown")
    1107              :                      END SELECT
    1108              : 
    1109     12107776 :                      exp_term = EXP(-eta*(r - rs)**2)
    1110              : 
    1111     12107776 :                      grp%spline_y(sf, p) = exp_term*fcut
    1112              :                      grp%spline_dy(sf, p) = exp_term*(-2.0_dp*eta*(r - rs))*fcut + &
    1113     12109254 :                                             exp_term*dfcutdr
    1114              :                   END DO
    1115              :                END DO
    1116              : 
    1117              :                ! Pin the boundary so the runtime out-of-range branch can
    1118              :                ! return zeros without re-checking each SF.
    1119         1649 :                DO sf = 1, n_symf
    1120         1478 :                   grp%spline_y(sf, nnp%rad_spline_n) = 0.0_dp
    1121         1649 :                   grp%spline_dy(sf, nnp%rad_spline_n) = 0.0_dp
    1122              :                END DO
    1123              : 
    1124          342 :                grp%spline_built = .TRUE.
    1125              :             END ASSOCIATE
    1126              :          END DO
    1127              :       END DO
    1128              : 
    1129           30 :       CALL timestop(handle)
    1130              : 
    1131           30 :    END SUBROUTINE nnp_build_radial_splines
    1132              : 
    1133              : ! **************************************************************************************************
    1134              : !> \brief Calculate angular symmetry function and gradient (optional)
    1135              : !>
    1136              : !> Vectorized SF-batched form. The original eta-sorted exp-skip loop has
    1137              : !> been replaced by a sequence of SF passes:
    1138              : !>   1. Branchless angular base: clamps tmp at the cusp (tmp <= 0), then
    1139              : !>      computes tmpzeta via integer or real pow per SF. Stays scalar
    1140              : !>      because the int/real switch and integer pow cannot SIMD.
    1141              : !>   2. Vectorized EXP: symtmp_arr(sf) = EXP(-eta(sf)*r2sum). Marked
    1142              : !>      !$OMP SIMD so the compiler maps it to libmvec/SVML vector EXP.
    1143              : !>      Vector libm hides EXP latency across the SF loop without needing
    1144              : !>      an eta-dedup pre-pass.
    1145              : !>   3. SIMD sym scatter: sym(sf) = prefzeta*angular*symtmp*ftot.
    1146              : !>   4. Force scatter (only when forces requested). The clamp in pass 1
    1147              : !>      forces tmpzeta_arr(sf) = 0 at the cusp, which makes both angular
    1148              : !>      AND pref_lam vanish naturally -- no per-iter branch.
    1149              : !>
    1150              : !> Caller passes precomputed cutoff values for the j/k legs; the j-k leg
    1151              : !> is computed inline. Geometry-only derivative bases are hoisted outside
    1152              : !> the SF loops as before.
    1153              : !> \param nnp ...
    1154              : !> \param ind ...
    1155              : !> \param s ...
    1156              : !> \param rvect1 ...
    1157              : !> \param rvect2 ...
    1158              : !> \param rvect3 ...
    1159              : !> \param r1 ...
    1160              : !> \param r2 ...
    1161              : !> \param r3 ...
    1162              : !> \param fcut_j ...
    1163              : !> \param dfcut_j ...
    1164              : !> \param fcut_k ...
    1165              : !> \param dfcut_k ...
    1166              : !> \param sym ...
    1167              : !> \param force ...
    1168              : !> \date   2020-10-10
    1169              : !> \author Christoph Schran (christoph.schran@rub.de)
    1170              : !> \note   This kernel is also inlined into nnp_calc_acsf on the serial force
    1171              : !>         path; any change to the computation or force scatter here must be
    1172              : !>         mirrored at that inlining site (see the comment there).
    1173              : ! **************************************************************************************************
    1174     27374069 :    SUBROUTINE nnp_calc_ang(nnp, ind, s, rvect1, rvect2, rvect3, r1, r2, r3, &
    1175     27374069 :                            fcut_j, dfcut_j, fcut_k, dfcut_k, sym, force)
    1176              : 
    1177              :       TYPE(nnp_type), INTENT(IN), TARGET                 :: nnp
    1178              :       INTEGER, INTENT(IN)                                :: ind, s
    1179              :       REAL(KIND=dp), DIMENSION(3), INTENT(IN)            :: rvect1, rvect2, rvect3
    1180              :       REAL(KIND=dp), INTENT(IN)                          :: r1, r2, r3, fcut_j, dfcut_j, fcut_k, &
    1181              :                                                             dfcut_k
    1182              :       REAL(KIND=dp), DIMENSION(:), INTENT(OUT)           :: sym
    1183              :       REAL(KIND=dp), DIMENSION(:, :, :), INTENT(OUT), &
    1184              :          OPTIONAL                                        :: force
    1185              : 
    1186              :       INTEGER                                            :: ii, izeta, n_symf, sf
    1187              :       LOGICAL                                            :: do_forces
    1188              :       REAL(KIND=dp) :: angular, arg_tmp, costheta, dfcut3, dfcutdr1, dfcutdr2, dfcutdr3, dsymdr1, &
    1189              :          dsymdr2, dsymdr3, eta, f, fcut3, fcut_rc, ftot, g, inv_g2, lam, pref_lam, prefzeta, &
    1190              :          r2sum, rsqr1, rsqr2, rsqr3, symtmp, tanh_tmp, tmp, tmp1, tmp2, tmp3, tmpzeta, zeta
    1191              :       REAL(KIND=dp), DIMENSION(3)                        :: dcosbase1, dcosbase2, dcosbase3, dgdx1, &
    1192              :                                                             dgdx2, dgdx3, dr1dx, dr2dx, dr3dx
    1193              :       TYPE(nnp_symfgrp_type), POINTER                    :: grp
    1194              :       REAL(KIND=dp), &
    1195     54748138 :          DIMENSION(nnp%ang(ind)%symfgrp(s)%n_symf)       :: angular_arr, symtmp_arr, tmpzeta_arr
    1196              : 
    1197              : ! Per-SF scratch (automatic arrays sized to the current group). For
    1198              : ! typical n2p2 ACSF n_symf is 8-32, so this is ~256-1024 bytes on
    1199              : ! stack per call. Holds the staged outputs of pass 1 + pass 2 so
    1200              : ! pass 3 streams them contiguously.
    1201              : 
    1202     27374069 :       do_forces = PRESENT(force)
    1203     27374069 :       grp => nnp%ang(ind)%symfgrp(s)
    1204     27374069 :       n_symf = grp%n_symf
    1205     27374069 :       fcut_rc = grp%cutoff
    1206              : 
    1207     27374069 :       rsqr1 = r1*r1
    1208     27374069 :       rsqr2 = r2*r2
    1209     27374069 :       rsqr3 = r3*r3
    1210     27374069 :       r2sum = rsqr1 + rsqr2 + rsqr3
    1211              : 
    1212     27374069 :       f = rsqr3 - rsqr1 - rsqr2
    1213     27374069 :       g = -2.0_dp*r1*r2
    1214     27374069 :       costheta = f/g
    1215              : 
    1216              :       ! Compute fcut3 for r3 (j-k distance -- cannot be precomputed by caller)
    1217     27573631 :       SELECT CASE (nnp%cut_type)
    1218              :       CASE (nnp_cut_cos)
    1219       199562 :          arg_tmp = pi*r3/fcut_rc
    1220       199562 :          fcut3 = 0.5_dp*(COS(arg_tmp) + 1.0_dp)
    1221       199562 :          IF (do_forces) dfcut3 = -0.5_dp*SIN(arg_tmp)*(pi/fcut_rc)
    1222              :       CASE (nnp_cut_tanh)
    1223     27174507 :          tanh_tmp = TANH(1.0_dp - r3/fcut_rc)
    1224     27174507 :          fcut3 = tanh_tmp**3
    1225     27174507 :          IF (do_forces) dfcut3 = (-3.0_dp/fcut_rc)*(tanh_tmp**2 - tanh_tmp**4)
    1226              :       CASE DEFAULT
    1227     27374069 :          CPABORT("NNP| Cutoff function unknown")
    1228              :       END SELECT
    1229              : 
    1230              :       ! Use precomputed fcut values for j and k neighbors
    1231     27374069 :       ftot = fcut_j*fcut_k*fcut3
    1232              : 
    1233     27374069 :       IF (do_forces) THEN
    1234              :          ! Combined cutoff derivatives (product rule)
    1235            0 :          dfcutdr1 = dfcut_j*fcut_k*fcut3
    1236            0 :          dfcutdr2 = fcut_j*dfcut_k*fcut3
    1237            0 :          dfcutdr3 = fcut_j*fcut_k*dfcut3
    1238              : 
    1239            0 :          dr1dx(:) = rvect1(:)/r1
    1240            0 :          dr2dx(:) = rvect2(:)/r2
    1241            0 :          dr3dx(:) = rvect3(:)/r3
    1242              : 
    1243              :          ! Hoist geometry-only parts of costheta derivatives outside SF loop.
    1244              :          ! Full angular derivative: dangulardx = (zeta*tmpzeta*lam/g^2) * dcosbase
    1245              :          ! where dcosbase factors out lam from dfdx and the f*dgdx term.
    1246              :          ! inv_g2 = 1/g^2 is hoisted: one divide per triplet, one mul per SF.
    1247            0 :          inv_g2 = 1.0_dp/(g*g)
    1248            0 :          DO ii = 1, 3
    1249            0 :             tmp1 = 2.0_dp*r2*dr1dx(ii)
    1250            0 :             tmp2 = 2.0_dp*r1*dr2dx(ii)
    1251            0 :             dgdx1(ii) = -(tmp1 + tmp2)
    1252              :             dgdx2(ii) = tmp1
    1253              :             dgdx3(ii) = tmp2
    1254              : 
    1255            0 :             dcosbase1(ii) = -2.0_dp*(rvect1(ii) + rvect2(ii))*g - f*dgdx1(ii)
    1256            0 :             dcosbase2(ii) = 2.0_dp*(rvect3(ii) + rvect1(ii))*g - f*dgdx2(ii)
    1257            0 :             dcosbase3(ii) = 2.0_dp*(rvect2(ii) - rvect3(ii))*g - f*dgdx3(ii)
    1258              :          END DO
    1259              :       ELSE
    1260              :          inv_g2 = 0.0_dp
    1261              :       END IF
    1262              : 
    1263              :       ! Pass 1: branchless cusp clamp -- zero tmpzeta propagates to sym and pref_lam.
    1264    183298526 :       DO sf = 1, n_symf
    1265    155924457 :          tmp = 1.0_dp + grp%pack_lam(sf)*costheta
    1266    183298526 :          IF (tmp <= 0.0_dp) THEN
    1267            0 :             tmpzeta_arr(sf) = 0.0_dp
    1268            0 :             angular_arr(sf) = 0.0_dp
    1269              :          ELSE
    1270    155924457 :             IF (grp%pack_use_int_zeta(sf)) THEN
    1271    155924457 :                izeta = grp%pack_izeta(sf)
    1272    155924457 :                tmpzeta_arr(sf) = tmp**(izeta - 1)
    1273              :             ELSE
    1274            0 :                tmpzeta_arr(sf) = tmp**(grp%pack_zeta(sf) - 1.0_dp)
    1275              :             END IF
    1276    155924457 :             angular_arr(sf) = tmpzeta_arr(sf)*tmp
    1277              :          END IF
    1278              :       END DO
    1279              : 
    1280              :       ! ---- Pass 2: vectorized EXP via libmvec/SVML ----
    1281              :       ! Single SIMD loop computing symtmp_arr(sf) = EXP(-eta(sf)*r2sum).
    1282              :       ! With -fopenmp-simd this maps to vector libm (e.g. _ZGVdN4v_exp on
    1283              :       ! AVX2, _ZGVeN8v_exp on AVX-512), hiding EXP latency across 4-8 SFs
    1284              :       ! per iter -- ~10 cycles/element vs ~30 for scalar EXP, which beats
    1285              :       ! any eta-dedup pre-pass.
    1286    183298526 :       !$OMP SIMD
    1287              :       DO sf = 1, n_symf
    1288    155924457 :          symtmp_arr(sf) = EXP(-grp%pack_eta(sf)*r2sum)
    1289              :       END DO
    1290              : 
    1291              :       ! ---- Pass 3: sym scatter (vectorizable) ----
    1292     27374069 :       !$OMP SIMD
    1293              :       DO sf = 1, n_symf
    1294    155924457 :          sym(sf) = grp%pack_prefzeta(sf)*angular_arr(sf)*symtmp_arr(sf)*ftot
    1295              :       END DO
    1296              : 
    1297              :       ! ---- Pass 4: force scatter (only when do_forces) ----
    1298     27374069 :       IF (do_forces) THEN
    1299            0 :          DO sf = 1, n_symf
    1300            0 :             symtmp = symtmp_arr(sf)
    1301            0 :             angular = angular_arr(sf)
    1302            0 :             tmpzeta = tmpzeta_arr(sf)
    1303            0 :             eta = grp%pack_eta(sf)
    1304            0 :             lam = grp%pack_lam(sf)
    1305            0 :             zeta = grp%pack_zeta(sf)
    1306            0 :             prefzeta = grp%pack_prefzeta(sf)
    1307              : 
    1308              :             ! pref_lam carries tmpzeta, so it vanishes at the cusp without
    1309              :             ! an explicit branch (tmpzeta_arr was clamped to 0 in pass 1).
    1310            0 :             pref_lam = zeta*tmpzeta*lam*inv_g2
    1311              : 
    1312            0 :             tmp = -2.0_dp*symtmp*eta
    1313            0 :             dsymdr1 = tmp*r1
    1314            0 :             dsymdr2 = tmp*r2
    1315            0 :             dsymdr3 = tmp*r3
    1316              : 
    1317            0 :             tmp = prefzeta*symtmp*ftot
    1318            0 :             tmp1 = prefzeta*angular*(ftot*dsymdr1 + dfcutdr1*symtmp)
    1319            0 :             tmp2 = prefzeta*angular*(ftot*dsymdr2 + dfcutdr2*symtmp)
    1320            0 :             tmp3 = prefzeta*angular*(ftot*dsymdr3 + dfcutdr3*symtmp)
    1321            0 :             DO ii = 1, 3
    1322            0 :                force(ii, 1, sf) = tmp*pref_lam*dcosbase1(ii) + tmp1*dr1dx(ii) + tmp2*dr2dx(ii)
    1323            0 :                force(ii, 2, sf) = tmp*pref_lam*dcosbase2(ii) - tmp1*dr1dx(ii) + tmp3*dr3dx(ii)
    1324            0 :                force(ii, 3, sf) = tmp*pref_lam*dcosbase3(ii) - tmp2*dr2dx(ii) - tmp3*dr3dx(ii)
    1325              :             END DO
    1326              :          END DO
    1327              :       END IF
    1328              : 
    1329     27374069 :    END SUBROUTINE nnp_calc_ang
    1330              : 
    1331              : ! **************************************************************************************************
    1332              : !> \brief Sort an (ele, nuc_ele) pair of arrays in ascending order of atomic number.
    1333              : !>        Used to canonicalise element ordering inside the NNP environment so the
    1334              : !>        same model file produces the same per-element index layout regardless
    1335              : !>        of input ordering.
    1336              : !> \param ele      element-symbol array, sorted in place to match nuc_ele.
    1337              : !> \param nuc_ele  per-element atomic number, sorted in place.
    1338              : !> \author Christoph Schran (christoph.schran@rub.de)
    1339              : ! **************************************************************************************************
    1340           30 :    SUBROUTINE nnp_sort_ele(ele, nuc_ele)
    1341              :       CHARACTER(len=2), DIMENSION(:), INTENT(INOUT)      :: ele
    1342              :       INTEGER, DIMENSION(:), INTENT(INOUT)               :: nuc_ele
    1343              : 
    1344              :       CHARACTER(len=2)                                   :: tmp_ele
    1345              :       INTEGER                                            :: i, j, loc, minimum, tmp_nuc_ele
    1346              : 
    1347           99 :       DO i = 1, SIZE(ele)
    1348           99 :          CALL get_ptable_info(ele(i), number=nuc_ele(i))
    1349              :       END DO
    1350              : 
    1351           69 :       DO i = 1, SIZE(ele) - 1
    1352           39 :          minimum = nuc_ele(i)
    1353           39 :          loc = i
    1354           91 :          DO j = i + 1, SIZE(ele)
    1355           91 :             IF (nuc_ele(j) < minimum) THEN
    1356           31 :                loc = j
    1357           31 :                minimum = nuc_ele(j)
    1358              :             END IF
    1359              :          END DO
    1360           39 :          tmp_nuc_ele = nuc_ele(i)
    1361           39 :          nuc_ele(i) = nuc_ele(loc)
    1362           39 :          nuc_ele(loc) = tmp_nuc_ele
    1363              : 
    1364           39 :          tmp_ele = ele(i)
    1365           39 :          ele(i) = ele(loc)
    1366           69 :          ele(loc) = tmp_ele
    1367              :       END DO
    1368              : 
    1369           30 :    END SUBROUTINE nnp_sort_ele
    1370              : 
    1371              : ! **************************************************************************************************
    1372              : !> \brief Sort radial and angular symmetry functions in canonical order.
    1373              : !>        Radial SFs are sorted by eta (ascending) then rcut; angular SFs by
    1374              : !>        eta, lambda, zeta. This is the order downstream code in nnp_init_acsf_groups
    1375              : !>        relies on for run-length-style group packing.
    1376              : !> \param nnp NNP environment whose rad/ang SF arrays will be reordered in place.
    1377              : !> \author Christoph Schran (christoph.schran@rub.de)
    1378              : ! **************************************************************************************************
    1379           30 :    SUBROUTINE nnp_sort_acsf(nnp)
    1380              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp
    1381              : 
    1382              :       INTEGER                                            :: i, j, k, loc
    1383              : 
    1384           99 :       DO i = 1, nnp%n_ele
    1385         1478 :          DO j = 1, nnp%n_rad(i) - 1
    1386         1409 :             loc = j
    1387        20125 :             DO k = j + 1, nnp%n_rad(i)
    1388        20125 :                IF (nnp%rad(i)%funccut(loc) > nnp%rad(i)%funccut(k)) THEN
    1389            6 :                   loc = k
    1390              :                END IF
    1391              :             END DO
    1392         1478 :             CALL nnp_swaprad(nnp%rad(i), j, loc)
    1393              :          END DO
    1394              : 
    1395         1478 :          DO j = 1, nnp%n_rad(i) - 1
    1396         1409 :             loc = j
    1397        20125 :             DO k = j + 1, nnp%n_rad(i)
    1398        18716 :                IF (nnp%rad(i)%funccut(loc) == nnp%rad(i)%funccut(k) .AND. &
    1399         1409 :                    nnp%rad(i)%eta(loc) > nnp%rad(i)%eta(k)) THEN
    1400          858 :                   loc = k
    1401              :                END IF
    1402              :             END DO
    1403         1478 :             CALL nnp_swaprad(nnp%rad(i), j, loc)
    1404              :          END DO
    1405              : 
    1406         1478 :          DO j = 1, nnp%n_rad(i) - 1
    1407         1409 :             loc = j
    1408        20125 :             DO k = j + 1, nnp%n_rad(i)
    1409              :                IF (nnp%rad(i)%funccut(loc) == nnp%rad(i)%funccut(k) .AND. &
    1410        18716 :                    nnp%rad(i)%eta(loc) == nnp%rad(i)%eta(k) .AND. &
    1411         1409 :                    nnp%rad(i)%rs(loc) > nnp%rad(i)%rs(k)) THEN
    1412         1044 :                   loc = k
    1413              :                END IF
    1414              :             END DO
    1415         1478 :             CALL nnp_swaprad(nnp%rad(i), j, loc)
    1416              :          END DO
    1417              : 
    1418         1478 :          DO j = 1, nnp%n_rad(i) - 1
    1419         1409 :             loc = j
    1420        20125 :             DO k = j + 1, nnp%n_rad(i)
    1421              :                IF (nnp%rad(i)%funccut(loc) == nnp%rad(i)%funccut(k) .AND. &
    1422              :                    nnp%rad(i)%eta(loc) == nnp%rad(i)%eta(k) .AND. &
    1423        18716 :                    nnp%rad(i)%rs(loc) == nnp%rad(i)%rs(k) .AND. &
    1424         1409 :                    nnp%rad(i)%nuc_ele(loc) > nnp%rad(i)%nuc_ele(k)) THEN
    1425          208 :                   loc = k
    1426              :                END IF
    1427              :             END DO
    1428         1478 :             CALL nnp_swaprad(nnp%rad(i), j, loc)
    1429              :          END DO
    1430              : 
    1431         1285 :          DO j = 1, nnp%n_ang(i) - 1
    1432         1216 :             loc = j
    1433        17403 :             DO k = j + 1, nnp%n_ang(i)
    1434        17403 :                IF (nnp%ang(i)%funccut(loc) > nnp%ang(i)%funccut(k)) THEN
    1435            3 :                   loc = k
    1436              :                END IF
    1437              :             END DO
    1438         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1439              :          END DO
    1440              : 
    1441         1285 :          DO j = 1, nnp%n_ang(i) - 1
    1442         1216 :             loc = j
    1443        17403 :             DO k = j + 1, nnp%n_ang(i)
    1444        16187 :                IF (nnp%ang(i)%funccut(loc) == nnp%ang(i)%funccut(k) .AND. &
    1445         1216 :                    nnp%ang(i)%eta(loc) > nnp%ang(i)%eta(k)) THEN
    1446          700 :                   loc = k
    1447              :                END IF
    1448              :             END DO
    1449         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1450              :          END DO
    1451              : 
    1452         1285 :          DO j = 1, nnp%n_ang(i) - 1
    1453         1216 :             loc = j
    1454        17403 :             DO k = j + 1, nnp%n_ang(i)
    1455              :                IF (nnp%ang(i)%funccut(loc) == nnp%ang(i)%funccut(k) .AND. &
    1456        16187 :                    nnp%ang(i)%eta(loc) == nnp%ang(i)%eta(k) .AND. &
    1457         1216 :                    nnp%ang(i)%zeta(loc) > nnp%ang(i)%zeta(k)) THEN
    1458          295 :                   loc = k
    1459              :                END IF
    1460              :             END DO
    1461         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1462              :          END DO
    1463              : 
    1464         1285 :          DO j = 1, nnp%n_ang(i) - 1
    1465         1216 :             loc = j
    1466        17403 :             DO k = j + 1, nnp%n_ang(i)
    1467              :                IF (nnp%ang(i)%funccut(loc) == nnp%ang(i)%funccut(k) .AND. &
    1468              :                    nnp%ang(i)%eta(loc) == nnp%ang(i)%eta(k) .AND. &
    1469        16187 :                    nnp%ang(i)%zeta(loc) == nnp%ang(i)%zeta(k) .AND. &
    1470         1216 :                    nnp%ang(i)%lam(loc) > nnp%ang(i)%lam(k)) THEN
    1471          546 :                   loc = k
    1472              :                END IF
    1473              :             END DO
    1474         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1475              :          END DO
    1476              : 
    1477         1285 :          DO j = 1, nnp%n_ang(i) - 1
    1478         1216 :             loc = j
    1479        17403 :             DO k = j + 1, nnp%n_ang(i)
    1480              :                IF (nnp%ang(i)%funccut(loc) == nnp%ang(i)%funccut(k) .AND. &
    1481              :                    nnp%ang(i)%eta(loc) == nnp%ang(i)%eta(k) .AND. &
    1482              :                    nnp%ang(i)%zeta(loc) == nnp%ang(i)%zeta(k) .AND. &
    1483        16187 :                    nnp%ang(i)%lam(loc) == nnp%ang(i)%lam(k) .AND. &
    1484         1216 :                    nnp%ang(i)%nuc_ele1(loc) > nnp%ang(i)%nuc_ele1(k)) THEN
    1485          443 :                   loc = k
    1486              :                END IF
    1487              :             END DO
    1488         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1489              :          END DO
    1490              : 
    1491         1315 :          DO j = 1, nnp%n_ang(i) - 1
    1492         1216 :             loc = j
    1493        17403 :             DO k = j + 1, nnp%n_ang(i)
    1494              :                IF (nnp%ang(i)%funccut(loc) == nnp%ang(i)%funccut(k) .AND. &
    1495              :                    nnp%ang(i)%eta(loc) == nnp%ang(i)%eta(k) .AND. &
    1496              :                    nnp%ang(i)%zeta(loc) == nnp%ang(i)%zeta(k) .AND. &
    1497              :                    nnp%ang(i)%lam(loc) == nnp%ang(i)%lam(k) .AND. &
    1498        16187 :                    nnp%ang(i)%nuc_ele1(loc) == nnp%ang(i)%nuc_ele1(k) .AND. &
    1499         1216 :                    nnp%ang(i)%nuc_ele2(loc) > nnp%ang(i)%nuc_ele2(k)) THEN
    1500          339 :                   loc = k
    1501              :                END IF
    1502              :             END DO
    1503         1285 :             CALL nnp_swapang(nnp%ang(i), j, loc)
    1504              :          END DO
    1505              :       END DO
    1506              : 
    1507           30 :    END SUBROUTINE nnp_sort_acsf
    1508              : 
    1509              : ! **************************************************************************************************
    1510              : !> \brief Swap two radial symmetry functions
    1511              : !> \param rad ...
    1512              : !> \param i ...
    1513              : !> \param j ...
    1514              : !> \date   2020-10-10
    1515              : !> \author Christoph Schran (christoph.schran@rub.de)
    1516              : ! **************************************************************************************************
    1517         5636 :    SUBROUTINE nnp_swaprad(rad, i, j)
    1518              :       TYPE(nnp_acsf_rad_type), INTENT(INOUT)             :: rad
    1519              :       INTEGER, INTENT(IN)                                :: i, j
    1520              : 
    1521              :       CHARACTER(len=2)                                   :: tmpc
    1522              :       INTEGER                                            :: tmpi
    1523              :       REAL(KIND=dp)                                      :: tmpr
    1524              : 
    1525         5636 :       tmpr = rad%funccut(i)
    1526         5636 :       rad%funccut(i) = rad%funccut(j)
    1527         5636 :       rad%funccut(j) = tmpr
    1528              : 
    1529         5636 :       tmpr = rad%eta(i)
    1530         5636 :       rad%eta(i) = rad%eta(j)
    1531         5636 :       rad%eta(j) = tmpr
    1532              : 
    1533         5636 :       tmpr = rad%rs(i)
    1534         5636 :       rad%rs(i) = rad%rs(j)
    1535         5636 :       rad%rs(j) = tmpr
    1536              : 
    1537         5636 :       tmpc = rad%ele(i)
    1538         5636 :       rad%ele(i) = rad%ele(j)
    1539         5636 :       rad%ele(j) = tmpc
    1540              : 
    1541         5636 :       tmpi = rad%nuc_ele(i)
    1542         5636 :       rad%nuc_ele(i) = rad%nuc_ele(j)
    1543         5636 :       rad%nuc_ele(j) = tmpi
    1544              : 
    1545         5636 :    END SUBROUTINE nnp_swaprad
    1546              : 
    1547              : ! **************************************************************************************************
    1548              : !> \brief Swap two angular symmetry functions
    1549              : !> \param ang ...
    1550              : !> \param i ...
    1551              : !> \param j ...
    1552              : !> \date   2020-10-10
    1553              : !> \author Christoph Schran (christoph.schran@rub.de)
    1554              : ! **************************************************************************************************
    1555         7296 :    SUBROUTINE nnp_swapang(ang, i, j)
    1556              :       TYPE(nnp_acsf_ang_type), INTENT(INOUT)             :: ang
    1557              :       INTEGER, INTENT(IN)                                :: i, j
    1558              : 
    1559              :       CHARACTER(len=2)                                   :: tmpc
    1560              :       INTEGER                                            :: tmpi
    1561              :       REAL(KIND=dp)                                      :: tmpr
    1562              : 
    1563         7296 :       tmpr = ang%funccut(i)
    1564         7296 :       ang%funccut(i) = ang%funccut(j)
    1565         7296 :       ang%funccut(j) = tmpr
    1566              : 
    1567         7296 :       tmpr = ang%eta(i)
    1568         7296 :       ang%eta(i) = ang%eta(j)
    1569         7296 :       ang%eta(j) = tmpr
    1570              : 
    1571         7296 :       tmpr = ang%zeta(i)
    1572         7296 :       ang%zeta(i) = ang%zeta(j)
    1573         7296 :       ang%zeta(j) = tmpr
    1574              : 
    1575         7296 :       tmpr = ang%prefzeta(i)
    1576         7296 :       ang%prefzeta(i) = ang%prefzeta(j)
    1577         7296 :       ang%prefzeta(j) = tmpr
    1578              : 
    1579         7296 :       tmpr = ang%lam(i)
    1580         7296 :       ang%lam(i) = ang%lam(j)
    1581         7296 :       ang%lam(j) = tmpr
    1582              : 
    1583         7296 :       tmpc = ang%ele1(i)
    1584         7296 :       ang%ele1(i) = ang%ele1(j)
    1585         7296 :       ang%ele1(j) = tmpc
    1586              : 
    1587         7296 :       tmpi = ang%nuc_ele1(i)
    1588         7296 :       ang%nuc_ele1(i) = ang%nuc_ele1(j)
    1589         7296 :       ang%nuc_ele1(j) = tmpi
    1590              : 
    1591         7296 :       tmpc = ang%ele2(i)
    1592         7296 :       ang%ele2(i) = ang%ele2(j)
    1593         7296 :       ang%ele2(j) = tmpc
    1594              : 
    1595         7296 :       tmpi = ang%nuc_ele2(i)
    1596         7296 :       ang%nuc_ele2(i) = ang%nuc_ele2(j)
    1597         7296 :       ang%nuc_ele2(j) = tmpi
    1598              : 
    1599         7296 :    END SUBROUTINE nnp_swapang
    1600              : 
    1601              : ! **************************************************************************************************
    1602              : !> \brief Pack symmetry functions into groups that share input parameters.
    1603              : !>        Builds nnp%rad(i)%symfgrp(:) and nnp%ang(i)%symfgrp(:) so that all
    1604              : !>        radial / angular SFs with identical (eta, rcut [, lambda, zeta])
    1605              : !>        live in one group; downstream the descriptor pass evaluates each
    1606              : !>        group's shared cutoff/exponent once and applies it across the
    1607              : !>        per-element neighbour slab.
    1608              : !> \param nnp NNP environment with rad/ang SF metadata already populated by nnp_init_model.
    1609              : !> \author Christoph Schran (christoph.schran@rub.de)
    1610              : ! **************************************************************************************************
    1611           30 :    SUBROUTINE nnp_init_acsf_groups(nnp)
    1612              : 
    1613              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp
    1614              : 
    1615              :       INTEGER                                            :: ang, i, izeta_tmp, j, k, m, n_symf, rad, &
    1616              :                                                             s, sf
    1617              :       REAL(KIND=dp)                                      :: eta_tmp, funccut, zeta_tmp
    1618              : 
    1619           99 :       DO i = 1, nnp%n_ele
    1620           69 :          nnp%rad(i)%n_symfgrp = 0
    1621           69 :          nnp%ang(i)%n_symfgrp = 0
    1622          242 :          DO j = 1, nnp%n_ele
    1623          173 :             funccut = -1.0_dp
    1624         4516 :             DO s = 1, nnp%n_rad(i)
    1625         4447 :                IF (nnp%rad(i)%ele(s) == nnp%ele(j)) THEN
    1626         1478 :                   IF (ABS(nnp%rad(i)%funccut(s) - funccut) > cutoff_eq_tol) THEN
    1627          171 :                      nnp%rad(i)%n_symfgrp = nnp%rad(i)%n_symfgrp + 1
    1628          171 :                      funccut = nnp%rad(i)%funccut(s)
    1629              :                   END IF
    1630              :                END IF
    1631              :             END DO
    1632              :          END DO
    1633          272 :          DO j = 1, nnp%n_ele
    1634          570 :             DO k = j, nnp%n_ele
    1635          328 :                funccut = -1.0_dp
    1636         8896 :                DO s = 1, nnp%n_ang(i)
    1637              :                   IF ((nnp%ang(i)%ele1(s) == nnp%ele(j) .AND. &
    1638         8395 :                        nnp%ang(i)%ele2(s) == nnp%ele(k)) .OR. &
    1639              :                       (nnp%ang(i)%ele1(s) == nnp%ele(k) .AND. &
    1640          328 :                        nnp%ang(i)%ele2(s) == nnp%ele(j))) THEN
    1641         1285 :                      IF (ABS(nnp%ang(i)%funccut(s) - funccut) > cutoff_eq_tol) THEN
    1642          291 :                         nnp%ang(i)%n_symfgrp = nnp%ang(i)%n_symfgrp + 1
    1643          291 :                         funccut = nnp%ang(i)%funccut(s)
    1644              :                      END IF
    1645              :                   END IF
    1646              :                END DO
    1647              :             END DO
    1648              :          END DO
    1649              :       END DO
    1650              : 
    1651           99 :       DO i = 1, nnp%n_ele
    1652          378 :          ALLOCATE (nnp%rad(i)%symfgrp(nnp%rad(i)%n_symfgrp))
    1653          498 :          ALLOCATE (nnp%ang(i)%symfgrp(nnp%ang(i)%n_symfgrp))
    1654          240 :          DO j = 1, nnp%rad(i)%n_symfgrp
    1655          240 :             nnp%rad(i)%symfgrp(j)%n_symf = 0
    1656              :          END DO
    1657          390 :          DO j = 1, nnp%ang(i)%n_symfgrp
    1658          360 :             nnp%ang(i)%symfgrp(j)%n_symf = 0
    1659              :          END DO
    1660              :       END DO
    1661              : 
    1662           99 :       DO i = 1, nnp%n_ele
    1663              :          rad = 0
    1664          242 :          ang = 0
    1665          242 :          DO j = 1, nnp%n_ele
    1666          173 :             funccut = -1.0_dp
    1667         4516 :             DO s = 1, nnp%n_rad(i)
    1668         4447 :                IF (nnp%rad(i)%ele(s) == nnp%ele(j)) THEN
    1669         1478 :                   IF (ABS(nnp%rad(i)%funccut(s) - funccut) > cutoff_eq_tol) THEN
    1670          171 :                      rad = rad + 1
    1671          171 :                      funccut = nnp%rad(i)%funccut(s)
    1672          171 :                      nnp%rad(i)%symfgrp(rad)%cutoff = funccut
    1673          171 :                      ALLOCATE (nnp%rad(i)%symfgrp(rad)%ele(1))
    1674          171 :                      ALLOCATE (nnp%rad(i)%symfgrp(rad)%ele_ind(1))
    1675          171 :                      nnp%rad(i)%symfgrp(rad)%ele(1) = nnp%ele(j)
    1676          171 :                      nnp%rad(i)%symfgrp(rad)%ele_ind(1) = j
    1677              :                   END IF
    1678         1478 :                   nnp%rad(i)%symfgrp(rad)%n_symf = nnp%rad(i)%symfgrp(rad)%n_symf + 1
    1679              :                END IF
    1680              :             END DO
    1681              :          END DO
    1682          272 :          DO j = 1, nnp%n_ele
    1683          570 :             DO k = j, nnp%n_ele
    1684          328 :                funccut = -1.0_dp
    1685         8896 :                DO s = 1, nnp%n_ang(i)
    1686              :                   IF ((nnp%ang(i)%ele1(s) == nnp%ele(j) .AND. &
    1687         8395 :                        nnp%ang(i)%ele2(s) == nnp%ele(k)) .OR. &
    1688              :                       (nnp%ang(i)%ele1(s) == nnp%ele(k) .AND. &
    1689          328 :                        nnp%ang(i)%ele2(s) == nnp%ele(j))) THEN
    1690         1285 :                      IF (ABS(nnp%ang(i)%funccut(s) - funccut) > cutoff_eq_tol) THEN
    1691          291 :                         ang = ang + 1
    1692          291 :                         funccut = nnp%ang(i)%funccut(s)
    1693          291 :                         nnp%ang(i)%symfgrp(ang)%cutoff = funccut
    1694          291 :                         ALLOCATE (nnp%ang(i)%symfgrp(ang)%ele(2))
    1695          291 :                         ALLOCATE (nnp%ang(i)%symfgrp(ang)%ele_ind(2))
    1696          291 :                         nnp%ang(i)%symfgrp(ang)%ele(1) = nnp%ele(j)
    1697          291 :                         nnp%ang(i)%symfgrp(ang)%ele(2) = nnp%ele(k)
    1698          291 :                         nnp%ang(i)%symfgrp(ang)%ele_ind(1) = j
    1699          291 :                         nnp%ang(i)%symfgrp(ang)%ele_ind(2) = k
    1700              :                      END IF
    1701         1285 :                      nnp%ang(i)%symfgrp(ang)%n_symf = nnp%ang(i)%symfgrp(ang)%n_symf + 1
    1702              :                   END IF
    1703              :                END DO
    1704              :             END DO
    1705              :          END DO
    1706              :       END DO
    1707              : 
    1708           99 :       DO i = 1, nnp%n_ele
    1709          240 :          DO j = 1, nnp%rad(i)%n_symfgrp
    1710          513 :             ALLOCATE (nnp%rad(i)%symfgrp(j)%symf(nnp%rad(i)%symfgrp(j)%n_symf))
    1711          171 :             rad = 0
    1712         4493 :             DO s = 1, nnp%n_rad(i)
    1713         4424 :                IF (nnp%rad(i)%ele(s) == nnp%rad(i)%symfgrp(j)%ele(1)) THEN
    1714         1478 :                   IF (ABS(nnp%rad(i)%funccut(s) - nnp%rad(i)%symfgrp(j)%cutoff) <= cutoff_eq_tol) THEN
    1715         1478 :                      rad = rad + 1
    1716         1478 :                      nnp%rad(i)%symfgrp(j)%symf(rad) = s
    1717              :                   END IF
    1718              :                END IF
    1719              :             END DO
    1720              :          END DO
    1721          390 :          DO j = 1, nnp%ang(i)%n_symfgrp
    1722          873 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%symf(nnp%ang(i)%symfgrp(j)%n_symf))
    1723          291 :             ang = 0
    1724         8400 :             DO s = 1, nnp%n_ang(i)
    1725              :                IF ((nnp%ang(i)%ele1(s) == nnp%ang(i)%symfgrp(j)%ele(1) .AND. &
    1726         8040 :                     nnp%ang(i)%ele2(s) == nnp%ang(i)%symfgrp(j)%ele(2)) .OR. &
    1727              :                    (nnp%ang(i)%ele1(s) == nnp%ang(i)%symfgrp(j)%ele(2) .AND. &
    1728          291 :                     nnp%ang(i)%ele2(s) == nnp%ang(i)%symfgrp(j)%ele(1))) THEN
    1729         1285 :                   IF (ABS(nnp%ang(i)%funccut(s) - nnp%ang(i)%symfgrp(j)%cutoff) <= cutoff_eq_tol) THEN
    1730         1285 :                      ang = ang + 1
    1731         1285 :                      nnp%ang(i)%symfgrp(j)%symf(ang) = s
    1732              :                   END IF
    1733              :                END IF
    1734              :             END DO
    1735              :          END DO
    1736              :       END DO
    1737              : 
    1738              :       ! Populate packed parameter arrays for the angular SF group inner loop
    1739              :       ! so it reads contiguous memory rather than chasing
    1740              :       ! ang(i)%{eta,zeta,lam,prefzeta}(symf(sf)) on every iteration.
    1741              :       ! pack_use_int_zeta and pack_izeta let the inner loop skip NINT and
    1742              :       ! dispatch a tight integer power when zeta is integral. The radial
    1743              :       ! group does not need packed parameters since nnp_calc_rad reads
    1744              :       ! pretabulated splines.
    1745           99 :       DO i = 1, nnp%n_ele
    1746          390 :          DO j = 1, nnp%ang(i)%n_symfgrp
    1747          291 :             n_symf = nnp%ang(i)%symfgrp(j)%n_symf
    1748          873 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_eta(n_symf))
    1749          582 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_zeta(n_symf))
    1750          582 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_lam(n_symf))
    1751          582 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_prefzeta(n_symf))
    1752          873 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_izeta(n_symf))
    1753          582 :             ALLOCATE (nnp%ang(i)%symfgrp(j)%pack_use_int_zeta(n_symf))
    1754         1645 :             DO sf = 1, n_symf
    1755         1285 :                m = nnp%ang(i)%symfgrp(j)%symf(sf)
    1756         1285 :                eta_tmp = nnp%ang(i)%eta(m)
    1757         1285 :                zeta_tmp = nnp%ang(i)%zeta(m)
    1758         1285 :                nnp%ang(i)%symfgrp(j)%pack_eta(sf) = eta_tmp
    1759         1285 :                nnp%ang(i)%symfgrp(j)%pack_zeta(sf) = zeta_tmp
    1760         1285 :                nnp%ang(i)%symfgrp(j)%pack_lam(sf) = nnp%ang(i)%lam(m)
    1761         1285 :                nnp%ang(i)%symfgrp(j)%pack_prefzeta(sf) = nnp%ang(i)%prefzeta(m)
    1762         1285 :                izeta_tmp = NINT(zeta_tmp)
    1763         1285 :                nnp%ang(i)%symfgrp(j)%pack_izeta(sf) = izeta_tmp
    1764              :                nnp%ang(i)%symfgrp(j)%pack_use_int_zeta(sf) = &
    1765         1576 :                   (REAL(izeta_tmp, dp) == zeta_tmp)
    1766              :             END DO
    1767              :          END DO
    1768              :       END DO
    1769              : 
    1770           30 :    END SUBROUTINE nnp_init_acsf_groups
    1771              : 
    1772              : ! **************************************************************************************************
    1773              : !> \brief Print a summary of the active symmetry-function set on the source rank.
    1774              : !>        Emits one line per element listing the per-element n_rad / n_ang counts
    1775              : !>        and the per-group breakdown; used at NNP init time and after sort/group
    1776              : !>        passes for traceability.
    1777              : !> \param nnp       NNP environment whose SF metadata is to be printed.
    1778              : !> \param para_env  parallel environment; only the source rank emits output.
    1779              : !> \param printtag  log-line prefix (typically "NNP" or "HELIUM_NNP").
    1780              : !> \author Christoph Schran (christoph.schran@rub.de)
    1781              : ! **************************************************************************************************
    1782           30 :    SUBROUTINE nnp_write_acsf(nnp, para_env, printtag)
    1783              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp
    1784              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1785              :       CHARACTER(LEN=*), INTENT(IN)                       :: printtag
    1786              : 
    1787              :       CHARACTER(len=default_string_length)               :: my_label
    1788              :       INTEGER                                            :: i, j, unit_nr
    1789              :       TYPE(cp_logger_type), POINTER                      :: logger
    1790              : 
    1791           30 :       NULLIFY (logger)
    1792           30 :       logger => cp_get_default_logger()
    1793              : 
    1794           30 :       my_label = TRIM(printtag)//"| "
    1795           30 :       IF (para_env%is_source()) THEN
    1796           16 :          unit_nr = cp_logger_get_default_unit_nr(logger)
    1797           16 :          WRITE (unit_nr, '(1X,A,1X,10(I2,1X))') TRIM(my_label)//" Activation functions:", nnp%actfnct(:)
    1798           53 :          DO i = 1, nnp%n_ele
    1799              :             WRITE (unit_nr, *) TRIM(my_label)//" short range atomic symmetry functions element "// &
    1800           37 :                nnp%ele(i)//":"
    1801          811 :             DO j = 1, nnp%n_rad(i)
    1802          774 :                WRITE (unit_nr, '(1X,A,1X,I3,1X,A2,1X,I2,1X,A2,11X,3(F6.3,1X))') TRIM(my_label), j, nnp%ele(i), 2, &
    1803          774 :                   nnp%rad(i)%ele(j), nnp%rad(i)%eta(j), &
    1804         1585 :                   nnp%rad(i)%rs(j), nnp%rad(i)%funccut(j)
    1805              :             END DO
    1806          718 :             DO j = 1, nnp%n_ang(i)
    1807              :                WRITE (unit_nr, '(1X,A,1X,I3,1X,A2,1X,I2,2(1X,A2),1X,4(F6.3,1X))') &
    1808          665 :                   TRIM(my_label), j, nnp%ele(i), 3, &
    1809          665 :                   nnp%ang(i)%ele1(j), nnp%ang(i)%ele2(j), &
    1810          665 :                   nnp%ang(i)%eta(j), nnp%ang(i)%lam(j), &
    1811         1367 :                   nnp%ang(i)%zeta(j), nnp%ang(i)%funccut(j)
    1812              :             END DO
    1813              :          END DO
    1814              :       END IF
    1815              : 
    1816           30 :    END SUBROUTINE nnp_write_acsf
    1817              : 
    1818              : END MODULE nnp_acsf
        

Generated by: LCOV version 2.0-1