LCOV - code coverage report
Current view: top level - src - nnp_environment_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 82.7 % 248 205
Test Date: 2026-11-24 01:22:08 Functions: 17.6 % 34 6

            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  Data types for neural network potentials
      10              : !> \author Christoph Schran (christoph.schran@rub.de)
      11              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
      12              : !> \date   2020-10-10
      13              : ! **************************************************************************************************
      14              : MODULE nnp_environment_types
      15              :    USE atomic_kind_list_types,          ONLY: atomic_kind_list_create,&
      16              :                                               atomic_kind_list_release,&
      17              :                                               atomic_kind_list_type
      18              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      19              :    USE cell_types,                      ONLY: cell_release,&
      20              :                                               cell_retain,&
      21              :                                               cell_type
      22              :    USE cp_subsys_types,                 ONLY: cp_subsys_get,&
      23              :                                               cp_subsys_release,&
      24              :                                               cp_subsys_set,&
      25              :                                               cp_subsys_type
      26              :    USE distribution_1d_types,           ONLY: distribution_1d_type
      27              :    USE input_constants,                 ONLY: nnp_gpu_prec_double,&
      28              :                                               nnp_use_gpu_off,&
      29              :                                               nnp_use_gpu_on
      30              :    USE input_section_types,             ONLY: section_vals_type
      31              :    USE kinds,                           ONLY: default_string_length,&
      32              :                                               dp
      33              :    USE molecule_kind_list_types,        ONLY: molecule_kind_list_create,&
      34              :                                               molecule_kind_list_release,&
      35              :                                               molecule_kind_list_type
      36              :    USE molecule_kind_types,             ONLY: molecule_kind_type
      37              :    USE molecule_list_types,             ONLY: molecule_list_create,&
      38              :                                               molecule_list_release,&
      39              :                                               molecule_list_type
      40              :    USE molecule_types,                  ONLY: molecule_type
      41              :    USE particle_list_types,             ONLY: particle_list_create,&
      42              :                                               particle_list_release,&
      43              :                                               particle_list_type
      44              :    USE particle_types,                  ONLY: particle_type
      45              :    USE virial_types,                    ONLY: virial_type
      46              : #include "./base/base_uses.f90"
      47              : 
      48              :    IMPLICIT NONE
      49              : 
      50              :    PRIVATE
      51              : 
      52              :    LOGICAL, PRIVATE, PARAMETER :: debug_this_module = .FALSE.
      53              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'nnp_environment_types'
      54              : 
      55              :    !> derived data types
      56              :    PUBLIC :: nnp_type
      57              :    PUBLIC :: nnp_arc_type
      58              :    PUBLIC :: nnp_neighbor_type
      59              :    PUBLIC :: nnp_neigh_grp_type
      60              :    PUBLIC :: nnp_symfgrp_type
      61              :    PUBLIC :: nnp_acsf_rad_type
      62              :    PUBLIC :: nnp_acsf_ang_type
      63              :    PUBLIC :: nnp_cell_list_cache_type
      64              :    PUBLIC :: nnp_neighbor_pair_map_type
      65              :    PUBLIC :: nnp_dGdr_grp_type
      66              :    PUBLIC :: nnp_neighbor_workspace_type
      67              :    PUBLIC :: nnp_neighbor_interface_state_type
      68              :    PUBLIC :: nnp_gpu_host_state_type
      69              : 
      70              :    ! Public subroutines ***
      71              :    PUBLIC :: nnp_env_release, &
      72              :              nnp_env_set, &
      73              :              nnp_env_get, &
      74              :              nnp_cell_list_cache_release, &
      75              :              nnp_neighbor_interface_state_release
      76              : 
      77              :    INTEGER, PARAMETER, PUBLIC :: &
      78              :       nnp_cut_cos = 1, &
      79              :       nnp_cut_tanh = 2
      80              : 
      81              :    INTEGER, PARAMETER, PUBLIC :: &
      82              :       nnp_actfnct_tanh = 1, &
      83              :       nnp_actfnct_gaus = 2, &
      84              :       nnp_actfnct_lin = 3, &
      85              :       nnp_actfnct_cos = 4, &
      86              :       nnp_actfnct_sig = 5, &
      87              :       nnp_actfnct_invsig = 6, &
      88              :       nnp_actfnct_exp = 7, &
      89              :       nnp_actfnct_softplus = 8, &
      90              :       nnp_actfnct_quad = 9
      91              : 
      92              : ! **************************************************************************************************
      93              : !> \brief Main data type collecting all relevant data for neural network potentials
      94              : !> \author Christoph Schran (christoph.schran@rub.de)
      95              : !> \date   2020-10-10
      96              : ! **************************************************************************************************
      97              :    TYPE nnp_type
      98              :       TYPE(nnp_acsf_rad_type), DIMENSION(:), POINTER      :: rad => NULL() ! DIM(n_ele)
      99              :       TYPE(nnp_acsf_ang_type), DIMENSION(:), POINTER      :: ang => NULL() ! DIM(n_ele)
     100              :       INTEGER, DIMENSION(:), ALLOCATABLE                  :: n_rad ! # radial symfnct for this element
     101              :       INTEGER, DIMENSION(:), ALLOCATABLE                  :: n_ang ! # angular symfnct for this element
     102              :       INTEGER                                             :: n_ele = -1 ! # elements
     103              :       CHARACTER(len=2), ALLOCATABLE, DIMENSION(:)         :: ele ! elements(n_ele)
     104              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: nuc_ele ! elements(n_ele)
     105              :       LOGICAL                                             :: scale_acsf = .FALSE.
     106              :       LOGICAL                                             :: scale_sigma_acsf = .FALSE.
     107              :       LOGICAL                                             :: center_acsf = .FALSE.
     108              :       LOGICAL                                             :: normnodes = .FALSE.
     109              :       INTEGER                                             :: n_radgrp = -1
     110              :       INTEGER                                             :: n_anggrp = -1
     111              :       INTEGER                                             :: cut_type = -1 ! cutofftype
     112              :       REAL(KIND=dp)                                       :: eshortmin = -1.0_dp
     113              :       REAL(KIND=dp)                                       :: eshortmax = -1.0_dp
     114              :       REAL(KIND=dp)                                       :: scmax = -1.0_dp !scale
     115              :       REAL(KIND=dp)                                       :: scmin = -1.0_dp !scale
     116              :       REAL(KIND=dp)                                       :: max_cut = -1.0_dp !largest cutoff
     117              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: atom_energies !DIM(n_ele)
     118              :       TYPE(nnp_arc_type), POINTER, DIMENSION(:)           :: arc => NULL() ! DIM(n_ele)
     119              :       INTEGER                                             :: n_committee = -1
     120              :       INTEGER                                             :: n_hlayer = -1
     121              :       INTEGER                                             :: n_layer = -1
     122              :       INTEGER                                             :: rad_spline_n = 8192 ! knots/radial group (RAD_SPLINE_N)
     123              :       REAL(KIND=dp)                                       :: verlet_skin = -1.0_dp ! cell-list skin, bohr (VERLET_SKIN; <0 = auto)
     124              :       ! GPU offload controls. use_gpu holds the resolved USE_GPU value (AUTO is
     125              :       ! resolved to ON/OFF once at init, when the device is probed), so the
     126              :       ! force evaluation only ever tests use_gpu == nnp_use_gpu_on.
     127              :       INTEGER                                             :: use_gpu = nnp_use_gpu_off
     128              :       INTEGER                                             :: gpu_precision = nnp_gpu_prec_double
     129              :       ! VALIDATE: run the CPU reference in shadow mode against the GPU path on
     130              :       ! every force evaluation and abort when they disagree.
     131              :       LOGICAL                                             :: validate_gpu = .FALSE.
     132              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_hnodes
     133              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: actfnct
     134              :       INTEGER                                             :: expol = -1 ! extrapolation counter
     135              :       LOGICAL                                             :: output_expol = .FALSE. ! output extrapolation
     136              :       ! structures for calculation
     137              :       INTEGER                                             :: num_atoms = -1
     138              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: atomic_energy
     139              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: committee_energy
     140              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: ele_ind, nuc_atoms, sort, sort_inv
     141              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: coord
     142              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)      :: myforce
     143              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)      :: committee_forces, committee_stress
     144              :       CHARACTER(len=default_string_length), &
     145              :          ALLOCATABLE, DIMENSION(:)                        :: atoms
     146              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE         :: nnp_forces
     147              :       REAL(KIND=dp)                                       :: nnp_potential_energy = -1.0_dp
     148              :       TYPE(cp_subsys_type), POINTER                       :: subsys => NULL()
     149              :       TYPE(section_vals_type), POINTER                    :: nnp_input => NULL()
     150              :       TYPE(section_vals_type), POINTER                    :: force_env_input => NULL()
     151              :       TYPE(cell_type), POINTER                            :: cell => NULL()
     152              :       TYPE(cell_type), POINTER                            :: cell_ref => NULL()
     153              :       LOGICAL                                             :: use_ref_cell = .FALSE.
     154              :       ! bias
     155              :       LOGICAL                                             :: bias = .FALSE.
     156              :       LOGICAL                                             :: bias_align = .FALSE.
     157              :       REAL(KIND=dp)                                       :: bias_energy = -1.0_dp
     158              :       REAL(KIND=dp)                                       :: bias_kb = -1.0_dp
     159              :       REAL(KIND=dp)                                       :: bias_sigma0 = -1.0_dp
     160              :       REAL(KIND=dp)                                       :: bias_sigma = -1.0_dp
     161              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE         :: bias_forces
     162              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE            :: bias_e_avrg
     163              :       ! Per-nnp persistent neighbour-finder state, owned by nnp_type so committee
     164              :       ! and MIX force_evals carry independent caches whose lifetime tracks
     165              :       ! nnp_env_release. Allocated by nnp_prepare_neighbor_cache, freed in
     166              :       ! nnp_env_release; operated on by nnp_cell_list and nnp_neighbor_interface.
     167              :       TYPE(nnp_cell_list_cache_type), ALLOCATABLE         :: cell_list_cache
     168              :       TYPE(nnp_neighbor_interface_state_type), ALLOCATABLE :: neighbor_interface_state
     169              :       ! Host-side GPU walk bookkeeping, allocated on first GPU use.
     170              :       TYPE(nnp_gpu_host_state_type), ALLOCATABLE          :: gpu_host_state
     171              :    END TYPE nnp_type
     172              : 
     173              : ! **************************************************************************************************
     174              : !> \brief Symmetry functions group type
     175              : !> \param n_symf - # of associated sym fncts
     176              : !> \param symf   - indices of associated sym fncts       DIM(nsymf)
     177              : !> \param ele    - elements indices          rad:DIM(2), ang:DIM(3)
     178              : !> \param cutoff - associated cutoff value
     179              : !> \author Christoph Schran (christoph.schran@rub.de)
     180              : !> \date   2020-10-10
     181              : ! **************************************************************************************************
     182              :    TYPE nnp_symfgrp_type
     183              :       INTEGER                                             :: n_symf = -1
     184              :       INTEGER, DIMENSION(:), ALLOCATABLE                  :: symf
     185              :       INTEGER, DIMENSION(:), ALLOCATABLE                  :: ele_ind
     186              :       CHARACTER(LEN=2), DIMENSION(:), ALLOCATABLE         :: ele
     187              :       REAL(KIND=dp)                                       :: cutoff = -1.0_dp
     188              :       ! Packed per-member parameters: contiguous arrays sized n_symf so the inner
     189              :       ! SF loop streams memory instead of indexing ang(ind)%{eta,zeta,lam,prefzeta}
     190              :       ! through symf(sf). pack_izeta/pack_use_int_zeta skip NINT in the inner loop.
     191              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE            :: pack_eta
     192              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE            :: pack_zeta
     193              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE            :: pack_lam
     194              :       REAL(KIND=dp), DIMENSION(:), ALLOCATABLE            :: pack_prefzeta
     195              :       INTEGER, DIMENSION(:), ALLOCATABLE                  :: pack_izeta
     196              :       LOGICAL, DIMENSION(:), ALLOCATABLE                  :: pack_use_int_zeta
     197              :       ! Group-shared Hermite cubic spline tables (radial groups only). All SFs in
     198              :       ! a group share grp%cutoff, hence one uniform grid (spline_n nodes, spacing
     199              :       ! spline_dx). y(r) and y'(r) are packed sf-first so nnp_calc_rad streams them
     200              :       ! after computing the interpolation parameters once per call.
     201              :       LOGICAL                                             :: spline_built = .FALSE.
     202              :       INTEGER                                             :: spline_n = 0
     203              :       REAL(KIND=dp)                                       :: spline_dx = 1.0_dp
     204              :       REAL(KIND=dp)                                       :: spline_dx_inv = 1.0_dp
     205              :       REAL(KIND=dp)                                       :: spline_x_max = 0.0_dp
     206              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE         :: spline_y    ! (n_symf, n_grid)
     207              :       REAL(KIND=dp), DIMENSION(:, :), ALLOCATABLE         :: spline_dy   ! (n_symf, n_grid)
     208              :    END TYPE nnp_symfgrp_type
     209              : 
     210              : ! **************************************************************************************************
     211              : !> \brief Set of radial symmetry function type
     212              : !> \param y       - acsf value                                     - DIM(n_rad)
     213              : !> \param funccut - distance cutoff                           bohr - DIM(n_rad)
     214              : !> \param eta     - eta parameter of radial sym fncts      bohr^-2 - DIM(n_rad)
     215              : !> \param rs      - r shift parameter of radial sym fncts     bohr - DIM(n_rad)
     216              : !> \param loc_min - minimum of the sym fnct                          DIM(n_rad)
     217              : !> \param loc_max - maximum of the sym fnct                          DIM(n_rad)
     218              : !> \param loc_av  - average of the sym fnct                          DIM(n_rad)
     219              : !> \param sigma   - SD of the sym fnc                                DIM(n_rad)
     220              : !> \param ele     - element associated to the sym fnct               DIM(n_rad)
     221              : !> \param nuc_ele - associated atomic number                         DIM(n_rad)
     222              : !> \author Christoph Schran (christoph.schran@rub.de)
     223              : !> \date   2020-10-10
     224              : ! **************************************************************************************************
     225              :    TYPE nnp_acsf_rad_type
     226              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: y
     227              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: funccut
     228              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: eta
     229              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: rs
     230              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_min
     231              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_max
     232              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_av
     233              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: sigma
     234              :       CHARACTER(len=2), ALLOCATABLE, DIMENSION(:)         :: ele
     235              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: nuc_ele
     236              :       INTEGER                                             :: n_symfgrp = -1
     237              :       TYPE(nnp_symfgrp_type), DIMENSION(:), ALLOCATABLE   :: symfgrp
     238              :    END TYPE nnp_acsf_rad_type
     239              : 
     240              : ! **************************************************************************************************
     241              : !> \brief Set of angular symmetry function type
     242              : !> \param y         - acsf value                                  - DIM(n_ang)
     243              : !> \param funccut   - distance cutoff                        bohr - DIM(n_ang)
     244              : !> \param eta       - eta  param. of angular sym fncts    bohr^-2 - DIM(n_ang)
     245              : !> \param zeta      - zeta param. of angular sym fncts              DIM(n_ang)
     246              : !> \param lam       - lambda  param. of angular sym fncts           DIM(n_ang)
     247              : !> \param loc_min   - minimum of the sym fnct                       DIM(n_ang)
     248              : !> \param loc_max   - maximum of the sym fnct                       DIM(n_ang)
     249              : !> \param loc_av    - average of the sym fnct                       DIM(n_ang)
     250              : !> \param sigma     - SD of the sym fnc                             DIM(n_ang)
     251              : !> \param ele1,ele2 - elements associated to the sym fnct           DIM(n_ang)
     252              : !> \param nuc_ele2, nuc_ele2 - associated atomic numbers            DIM(n_ang)
     253              : !> \author Christoph Schran (christoph.schran@rub.de)
     254              : !> \date   2020-10-10
     255              : ! **************************************************************************************************
     256              :    TYPE nnp_acsf_ang_type
     257              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: y
     258              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: funccut
     259              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: eta
     260              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: zeta
     261              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: prefzeta
     262              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: lam
     263              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_min
     264              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_max
     265              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: loc_av
     266              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: sigma
     267              :       CHARACTER(len=2), ALLOCATABLE, DIMENSION(:)         :: ele1
     268              :       CHARACTER(len=2), ALLOCATABLE, DIMENSION(:)         :: ele2
     269              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: nuc_ele1
     270              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: nuc_ele2
     271              :       INTEGER                                             :: n_symfgrp = -1
     272              :       TYPE(nnp_symfgrp_type), DIMENSION(:), ALLOCATABLE   :: symfgrp
     273              :    END TYPE nnp_acsf_ang_type
     274              : 
     275              : ! **************************************************************************************************
     276              : !> \brief Per-SF-group dense neighbour container. cap is the allocated slab size;
     277              : !>        the live count is nnp_neighbor_type%n_rad/n_ang1/n_ang2 for the matching
     278              : !>        group. ind(j) is the j-th neighbour's atom index; dist(1:3, j) is its
     279              : !>        displacement and dist(4, j) its norm. Sized lazily by nnp_neigh_grp_grow.
     280              : ! **************************************************************************************************
     281              :    TYPE nnp_neigh_grp_type
     282              :       INTEGER                                             :: cap = 0
     283              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: ind   ! (cap)
     284              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: dist  ! (4, cap)
     285              :    END TYPE nnp_neigh_grp_type
     286              : 
     287              : ! **************************************************************************************************
     288              : !> \brief Contains neighbors list of an atom (per-group dense layout).
     289              : !> \param n_rad/n_ang1/n_ang2 - live neighbor counts per symfgrp
     290              : !> \param rad/ang1/ang2       - per-group dense (ind, dist) containers
     291              : !> \author Christoph Schran (christoph.schran@rub.de)
     292              : !> \date   2020-10-10
     293              : ! **************************************************************************************************
     294              :    TYPE nnp_neighbor_type
     295              :       INTEGER, DIMENSION(3)                                :: pbc_copies = -1
     296              :       INTEGER, DIMENSION(:), ALLOCATABLE                   :: n_rad
     297              :       INTEGER, DIMENSION(:), ALLOCATABLE                   :: n_ang1
     298              :       INTEGER, DIMENSION(:), ALLOCATABLE                   :: n_ang2
     299              :       TYPE(nnp_neigh_grp_type), ALLOCATABLE, DIMENSION(:)  :: rad
     300              :       TYPE(nnp_neigh_grp_type), ALLOCATABLE, DIMENSION(:)  :: ang1
     301              :       TYPE(nnp_neigh_grp_type), ALLOCATABLE, DIMENSION(:)  :: ang2
     302              :    END TYPE nnp_neighbor_type
     303              : 
     304              : ! **************************************************************************************************
     305              : !> \brief Linked-cell + Verlet-skin cache for the NNP descriptor neighbour walk.
     306              : !>        Persisted across MD steps so the image pool, bin geometry, and
     307              : !>        reference positions survive between force evaluations and the
     308              : !>        head/next chain can be reused as long as no atom has drifted more
     309              : !>        than skin/2. Operated on by nnp_cell_list.F.
     310              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     311              : ! **************************************************************************************************
     312              :    TYPE nnp_cell_list_cache_type
     313              :       LOGICAL                                             :: initialized = .FALSE.
     314              :       ! Counts the full cell-list rebuilds (the steps that regenerate the image
     315              :       ! translations). The GPU path compares it against the generation it last
     316              :       ! uploaded, so it re-uploads the image table exactly when the translations
     317              :       ! have moved on, whichever steps happened to reach the device in between.
     318              :       INTEGER                                             :: rebuild_count = 0
     319              :       INTEGER                                             :: num_atoms = -1
     320              :       INTEGER                                             :: n_images = 0
     321              :       INTEGER                                             :: n_cells = 1
     322              :       INTEGER, DIMENSION(3)                               :: exact_pbc_copies = 0
     323              :       INTEGER, DIMENSION(3)                               :: list_pbc_copies = 0
     324              :       INTEGER, DIMENSION(3)                               :: image_copies = 0
     325              :       INTEGER, DIMENSION(3)                               :: nbin = 1
     326              :       INTEGER, DIMENSION(3)                               :: bin_span = 0
     327              :       INTEGER, DIMENSION(3)                               :: perd = 0
     328              :       LOGICAL                                             :: orthorhombic = .FALSE.
     329              :       REAL(KIND=dp)                                       :: exact_cutoff = -1.0_dp
     330              :       REAL(KIND=dp)                                       :: list_cutoff = -1.0_dp
     331              :       REAL(KIND=dp)                                       :: verlet_skin = 0.0_dp
     332              :       REAL(KIND=dp), DIMENSION(3)                         :: lower = 0.0_dp
     333              :       REAL(KIND=dp), DIMENSION(3)                         :: upper = 0.0_dp
     334              :       REAL(KIND=dp), DIMENSION(3)                         :: bin_width = 1.0_dp
     335              :       REAL(KIND=dp), DIMENSION(3, 3)                      :: hmat = 0.0_dp
     336              :       REAL(KIND=dp), DIMENSION(3, 3)                      :: h_inv = 0.0_dp
     337              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: coord_primary
     338              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: coord_scaled
     339              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: ref_coord_primary
     340              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: image_atom
     341              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: head
     342              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: next
     343              :       INTEGER, ALLOCATABLE, DIMENSION(:, :)               :: image_shift
     344              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: image_translation
     345              :    END TYPE nnp_cell_list_cache_type
     346              : 
     347              : ! **************************************************************************************************
     348              : !> \brief Species-pair routing table. For one (central element, neighbour element)
     349              : !>        pair, lists which radial / angular symmetry-function groups need that
     350              : !>        neighbour and the worst-case relevant cutoff.
     351              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     352              : ! **************************************************************************************************
     353              :    TYPE nnp_neighbor_pair_map_type
     354              :       INTEGER                                             :: n_rad = 0
     355              :       INTEGER                                             :: n_ang1 = 0
     356              :       INTEGER                                             :: n_ang2 = 0
     357              :       REAL(KIND=dp)                                       :: max_relevant_cutoff = 0.0_dp
     358              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: rad_groups
     359              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: ang1_groups
     360              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: ang2_groups
     361              :    END TYPE nnp_neighbor_pair_map_type
     362              : 
     363              : ! **************************************************************************************************
     364              : !> \brief Per-(element, SF-group) dG_k/dr buffer, sized to the group's n_symf (no
     365              : !>        max_*_symf padding) and the observed peak neighbour count for the group.
     366              : !>        Grown lazily (1.5x) so it settles at the true peak.
     367              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     368              : ! **************************************************************************************************
     369              :    TYPE nnp_dGdr_grp_type
     370              :       INTEGER                                             :: cap = 0
     371              :       INTEGER                                             :: n_symf = 0
     372              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)      :: data    ! (3, n_symf, cap)
     373              :    END TYPE nnp_dGdr_grp_type
     374              : 
     375              : ! **************************************************************************************************
     376              : !> \brief Reusable per-element scratch / persistent caches for the ACSF
     377              : !>        descriptor and force assembly. See nnp_neighbor_interface for the
     378              : !>        routines that populate and grow these buffers.
     379              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     380              : ! **************************************************************************************************
     381              :    TYPE nnp_neighbor_workspace_type
     382              :       INTEGER                                             :: max_rad_symf = 0
     383              :       INTEGER                                             :: max_ang_symf = 0
     384              :       INTEGER                                             :: n_input_nodes = 0
     385              :       ! High-water mark of the per-element angular-cache slab, tracked so the 1D
     386              :       ! cutoff caches settle at the true per-element peak.
     387              :       INTEGER                                             :: cache_cap = 0
     388              :       ! There is a bug with some older compilers preventing requiring an explicit
     389              :       ! initialization of allocatable components (see mp2_types.F).
     390              : #if defined(FTN_NO_DEFAULT_INIT)
     391              :       TYPE(nnp_neighbor_type)                             :: neighbor = nnp_neighbor_type( &
     392              :                                                              pbc_copies=-1, n_rad=NULL(), &
     393              :                                                              n_ang1=NULL(), n_ang2=NULL(), &
     394              :                                                              rad=NULL(), ang1=NULL(), ang2=NULL())
     395              : #else
     396              :       TYPE(nnp_neighbor_type)                             :: neighbor = nnp_neighbor_type()
     397              : #endif
     398              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: radial_sym
     399              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: radial_force
     400              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: angular_sym
     401              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)      :: angular_force
     402              :       ! Per-element persistent cutoff caches reused across this element's central
     403              :       ! atoms. Sized lazily by nnp_workspace_grow_caches to MAX(n_ang1)/MAX(n_ang2).
     404              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: fc_cache1
     405              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: dfc_cache1
     406              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: fc_cache2
     407              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: dfc_cache2
     408              :       ! Per-neighbour dG_k/dr storage, replacing the global
     409              :       ! dsymdxyz(3, n_input_nodes, num_atoms) slab. The per-group buffers are
     410              :       ! dense in (3, n_symf_for_group, n_neighbors_in_group), which shrinks the
     411              :       ! working set and keeps each group walk a contiguous stride.
     412              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: self_dGdr
     413              :       TYPE(nnp_dGdr_grp_type), ALLOCATABLE, DIMENSION(:)  :: dGdr_rad
     414              :       TYPE(nnp_dGdr_grp_type), ALLOCATABLE, DIMENSION(:)  :: dGdr_ang_jj
     415              :       TYPE(nnp_dGdr_grp_type), ALLOCATABLE, DIMENSION(:)  :: dGdr_ang_kk
     416              :    END TYPE nnp_neighbor_workspace_type
     417              : 
     418              : ! **************************************************************************************************
     419              : !> \brief Persistent neighbour-interface state. Owned by nnp_type so that the
     420              : !>        per-nnp pair-routing tables and per-element workspaces survive
     421              : !>        across MD steps without leaking between independent &NNP
     422              : !>        force_evals. Operated on by nnp_neighbor_interface.F.
     423              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     424              : ! **************************************************************************************************
     425              :    TYPE nnp_neighbor_interface_state_type
     426              :       LOGICAL                                             :: initialized = .FALSE.
     427              :       INTEGER                                             :: n_ele = 0
     428              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_rad
     429              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_ang
     430              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_radgrp
     431              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_anggrp
     432              :       TYPE(nnp_neighbor_pair_map_type), ALLOCATABLE, &
     433              :          DIMENSION(:, :)                                  :: pair_map
     434              :       ! (element, thread): one workspace column per OpenMP thread
     435              :       TYPE(nnp_neighbor_workspace_type), ALLOCATABLE, &
     436              :          DIMENSION(:, :)                                  :: workspace
     437              :    END TYPE nnp_neighbor_interface_state_type
     438              : 
     439              : ! **************************************************************************************************
     440              : !> \brief Host-side bookkeeping for the GPU cell-list walk. Holds the
     441              : !>        per-element neighbour-list widths (grown on overflow and reused across
     442              : !>        MD steps) and the per-(atom-slot, group, element) neighbour counts
     443              : !>        read back from the device after each walk. Owned by nnp_type so it is
     444              : !>        allocated on first GPU use and freed with the environment.
     445              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     446              : ! **************************************************************************************************
     447              :    TYPE nnp_gpu_host_state_type
     448              :       LOGICAL                                             :: walk_initialised = .FALSE.
     449              :       INTEGER                                             :: max_n_neigh_rad = 0
     450              :       INTEGER                                             :: max_n_neigh_ang1 = 0
     451              :       INTEGER                                             :: max_n_neigh_ang2 = 0
     452              :       INTEGER                                             :: n_atoms_in_e_max = 0
     453              :       ! Value of the cell-list cache's rebuild_count at the last image-table
     454              :       ! upload; -1 until the first one. Never equal to a real generation, so the
     455              :       ! first walk always uploads.
     456              :       INTEGER                                             :: image_table_generation = -1
     457              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_atoms_in_e
     458              :       INTEGER, ALLOCATABLE, DIMENSION(:, :, :)            :: n_rad_pa
     459              :       INTEGER, ALLOCATABLE, DIMENSION(:, :, :)            :: n_ang1_pa
     460              :       INTEGER, ALLOCATABLE, DIMENSION(:, :, :)            :: n_ang2_pa
     461              :    END TYPE nnp_gpu_host_state_type
     462              : 
     463              : ! **************************************************************************************************
     464              : !> \brief Data type for artificial neural networks
     465              : !> \author Christoph Schran (christoph.schran@rub.de)
     466              : !> \date   2020-10-10
     467              : ! **************************************************************************************************
     468              :    TYPE nnp_arc_type
     469              :       TYPE(nnp_arc_layer_type), POINTER, DIMENSION(:)     :: layer => NULL() ! DIM(n_layer)
     470              :       INTEGER, ALLOCATABLE, DIMENSION(:)                  :: n_nodes
     471              :    END TYPE nnp_arc_type
     472              : 
     473              : ! **************************************************************************************************
     474              : !> \brief Data type for individual layer
     475              : !> \author Christoph Schran (christoph.schran@rub.de)
     476              : !> \date   2020-10-10
     477              : ! **************************************************************************************************
     478              :    TYPE nnp_arc_layer_type
     479              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :, :)      :: weights ! node weights
     480              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: bweights ! bias weights
     481              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: node ! DIM(n_nodes)
     482              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)            :: node_grad ! DIM(n_nodes)
     483              :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)         :: tmp_der ! DIM(n_sym,n_nodes)
     484              :    END TYPE nnp_arc_layer_type
     485              : 
     486              : CONTAINS
     487              : 
     488              : ! **************************************************************************************************
     489              : !> \brief Release data structure that holds all the information for neural
     490              : !>        network potentials
     491              : !> \param nnp_env ...
     492              : !> \date   2020-10-10
     493              : !> \author Christoph Schran (christoph.schran@rub.de)
     494              : ! **************************************************************************************************
     495           30 :    SUBROUTINE nnp_env_release(nnp_env)
     496              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp_env
     497              : 
     498              :       INTEGER                                            :: i, j
     499              : 
     500              : #if (defined(__OFFLOAD) || defined(__OFFLOAD_CUDA) || defined(__OFFLOAD_HIP)) && !defined(__NO_OFFLOAD_NNP)
     501              :       INTERFACE
     502              :          SUBROUTINE nnp_gpu_release_env_c() &
     503              :             BIND(C, NAME="nnp_gpu_release_env")
     504              :          END SUBROUTINE nnp_gpu_release_env_c
     505              :       END INTERFACE
     506              : 
     507              :       ! Device-side caches are keyed by host pointers into this environment
     508              :       ! (spline tables, packed angular parameters). Invalidate them before
     509              :       ! the host arrays are deallocated: a later environment could otherwise
     510              :       ! receive recycled heap addresses and silently hit stale device copies.
     511              :       ! This also drops this environment from the device state's live count, so
     512              :       ! a later environment may take the GPU in turn, and releases the state
     513              :       ! outright once the last one goes; the call is therefore made exactly for
     514              :       ! the environments that registered, the GPU-enabled ones.
     515              :       ! The interface is inlined here (instead of USE nnp_gpu) because
     516              :       ! nnp_gpu itself uses this module.
     517              :       IF (nnp_env%use_gpu == nnp_use_gpu_on) CALL nnp_gpu_release_env_c()
     518              : #endif
     519              : 
     520           30 :       IF (ASSOCIATED(nnp_env%rad)) THEN
     521           99 :          DO i = 1, nnp_env%n_ele
     522          240 :             DO j = 1, nnp_env%rad(i)%n_symfgrp
     523          171 :                IF (ALLOCATED(nnp_env%rad(i)%symfgrp(j)%symf)) THEN
     524              :                   DEALLOCATE (nnp_env%rad(i)%symfgrp(j)%symf, &
     525            0 :                               nnp_env%rad(i)%symfgrp(j)%ele, &
     526          171 :                               nnp_env%rad(i)%symfgrp(j)%ele_ind)
     527              :                END IF
     528          171 :                IF (ALLOCATED(nnp_env%rad(i)%symfgrp(j)%spline_y)) THEN
     529          171 :                   DEALLOCATE (nnp_env%rad(i)%symfgrp(j)%spline_y)
     530              :                END IF
     531          171 :                IF (ALLOCATED(nnp_env%rad(i)%symfgrp(j)%spline_dy)) THEN
     532          171 :                   DEALLOCATE (nnp_env%rad(i)%symfgrp(j)%spline_dy)
     533              :                END IF
     534          240 :                nnp_env%rad(i)%symfgrp(j)%spline_built = .FALSE.
     535              :             END DO
     536            0 :             DEALLOCATE (nnp_env%rad(i)%y, &
     537            0 :                         nnp_env%rad(i)%funccut, &
     538            0 :                         nnp_env%rad(i)%eta, &
     539            0 :                         nnp_env%rad(i)%rs, &
     540            0 :                         nnp_env%rad(i)%loc_min, &
     541            0 :                         nnp_env%rad(i)%loc_max, &
     542            0 :                         nnp_env%rad(i)%loc_av, &
     543            0 :                         nnp_env%rad(i)%sigma, &
     544            0 :                         nnp_env%rad(i)%ele, &
     545            0 :                         nnp_env%rad(i)%nuc_ele, &
     546          270 :                         nnp_env%rad(i)%symfgrp)
     547              :          END DO
     548           30 :          DEALLOCATE (nnp_env%rad)
     549              :       END IF
     550              : 
     551           30 :       IF (ASSOCIATED(nnp_env%ang)) THEN
     552           99 :          DO i = 1, nnp_env%n_ele
     553          360 :             DO j = 1, nnp_env%ang(i)%n_symfgrp
     554          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%symf)) THEN
     555              :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%symf, &
     556            0 :                               nnp_env%ang(i)%symfgrp(j)%ele, &
     557          291 :                               nnp_env%ang(i)%symfgrp(j)%ele_ind)
     558              :                END IF
     559          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_eta)) THEN
     560          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_eta)
     561              :                END IF
     562          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_zeta)) THEN
     563          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_zeta)
     564              :                END IF
     565          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_lam)) THEN
     566          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_lam)
     567              :                END IF
     568          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_prefzeta)) THEN
     569          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_prefzeta)
     570              :                END IF
     571          291 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_izeta)) THEN
     572          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_izeta)
     573              :                END IF
     574          360 :                IF (ALLOCATED(nnp_env%ang(i)%symfgrp(j)%pack_use_int_zeta)) THEN
     575          291 :                   DEALLOCATE (nnp_env%ang(i)%symfgrp(j)%pack_use_int_zeta)
     576              :                END IF
     577              :             END DO
     578            0 :             DEALLOCATE (nnp_env%ang(i)%y, &
     579            0 :                         nnp_env%ang(i)%funccut, &
     580            0 :                         nnp_env%ang(i)%eta, &
     581            0 :                         nnp_env%ang(i)%zeta, &
     582            0 :                         nnp_env%ang(i)%prefzeta, &
     583            0 :                         nnp_env%ang(i)%lam, &
     584            0 :                         nnp_env%ang(i)%loc_min, &
     585            0 :                         nnp_env%ang(i)%loc_max, &
     586            0 :                         nnp_env%ang(i)%loc_av, &
     587            0 :                         nnp_env%ang(i)%sigma, &
     588            0 :                         nnp_env%ang(i)%ele1, &
     589            0 :                         nnp_env%ang(i)%ele2, &
     590            0 :                         nnp_env%ang(i)%nuc_ele1, &
     591            0 :                         nnp_env%ang(i)%nuc_ele2, &
     592          390 :                         nnp_env%ang(i)%symfgrp)
     593              :          END DO
     594           30 :          DEALLOCATE (nnp_env%ang)
     595              :       END IF
     596              : 
     597           30 :       IF (ASSOCIATED(nnp_env%arc)) THEN
     598           99 :          DO i = 1, nnp_env%n_ele
     599           99 :             IF (ASSOCIATED(nnp_env%arc(i)%layer)) THEN
     600          345 :                DO j = 1, nnp_env%n_layer
     601          276 :                   IF (ALLOCATED(nnp_env%arc(i)%layer(j)%node)) THEN
     602          276 :                      DEALLOCATE (nnp_env%arc(i)%layer(j)%node)
     603              :                   END IF
     604          276 :                   IF (ALLOCATED(nnp_env%arc(i)%layer(j)%node_grad)) THEN
     605          276 :                      DEALLOCATE (nnp_env%arc(i)%layer(j)%node_grad)
     606              :                   END IF
     607          276 :                   IF (ALLOCATED(nnp_env%arc(i)%layer(j)%weights)) THEN
     608          207 :                      DEALLOCATE (nnp_env%arc(i)%layer(j)%weights)
     609              :                   END IF
     610          276 :                   IF (ALLOCATED(nnp_env%arc(i)%layer(j)%bweights)) THEN
     611          207 :                      DEALLOCATE (nnp_env%arc(i)%layer(j)%bweights)
     612              :                   END IF
     613          345 :                   IF (ALLOCATED(nnp_env%arc(i)%layer(j)%tmp_der)) THEN
     614          276 :                      DEALLOCATE (nnp_env%arc(i)%layer(j)%tmp_der)
     615              :                   END IF
     616              :                END DO
     617            0 :                DEALLOCATE (nnp_env%arc(i)%layer, &
     618           69 :                            nnp_env%arc(i)%n_nodes)
     619              :             END IF
     620              :          END DO
     621           30 :          DEALLOCATE (nnp_env%arc)
     622              :       END IF
     623              : 
     624           30 :       IF (ALLOCATED(nnp_env%ele)) DEALLOCATE (nnp_env%ele)
     625           30 :       IF (ALLOCATED(nnp_env%nuc_ele)) DEALLOCATE (nnp_env%nuc_ele)
     626           30 :       IF (ALLOCATED(nnp_env%n_hnodes)) DEALLOCATE (nnp_env%n_hnodes)
     627           30 :       IF (ALLOCATED(nnp_env%actfnct)) DEALLOCATE (nnp_env%actfnct)
     628           30 :       IF (ALLOCATED(nnp_env%nnp_forces)) DEALLOCATE (nnp_env%nnp_forces)
     629           30 :       IF (ALLOCATED(nnp_env%atomic_energy)) DEALLOCATE (nnp_env%atomic_energy)
     630           30 :       IF (ALLOCATED(nnp_env%committee_energy)) DEALLOCATE (nnp_env%committee_energy)
     631           30 :       IF (ALLOCATED(nnp_env%ele_ind)) DEALLOCATE (nnp_env%ele_ind)
     632           30 :       IF (ALLOCATED(nnp_env%nuc_atoms)) DEALLOCATE (nnp_env%nuc_atoms)
     633           30 :       IF (ALLOCATED(nnp_env%sort)) DEALLOCATE (nnp_env%sort)
     634           30 :       IF (ALLOCATED(nnp_env%sort_inv)) DEALLOCATE (nnp_env%sort_inv)
     635           30 :       IF (ALLOCATED(nnp_env%coord)) DEALLOCATE (nnp_env%coord)
     636           30 :       IF (ALLOCATED(nnp_env%myforce)) DEALLOCATE (nnp_env%myforce)
     637           30 :       IF (ALLOCATED(nnp_env%committee_forces)) DEALLOCATE (nnp_env%committee_forces)
     638           30 :       IF (ALLOCATED(nnp_env%committee_stress)) DEALLOCATE (nnp_env%committee_stress)
     639           30 :       IF (ALLOCATED(nnp_env%atoms)) DEALLOCATE (nnp_env%atoms)
     640              : 
     641           30 :       IF (ALLOCATED(nnp_env%cell_list_cache)) THEN
     642           30 :          CALL nnp_cell_list_cache_release(nnp_env%cell_list_cache)
     643           30 :          DEALLOCATE (nnp_env%cell_list_cache)
     644              :       END IF
     645           30 :       IF (ALLOCATED(nnp_env%neighbor_interface_state)) THEN
     646           30 :          CALL nnp_neighbor_interface_state_release(nnp_env%neighbor_interface_state)
     647           30 :          DEALLOCATE (nnp_env%neighbor_interface_state)
     648              :       END IF
     649              :       ! Allocatable array components are freed recursively with the parent.
     650           30 :       IF (ALLOCATED(nnp_env%gpu_host_state)) DEALLOCATE (nnp_env%gpu_host_state)
     651              : 
     652           30 :       IF (ASSOCIATED(nnp_env%subsys)) THEN
     653           29 :          CALL cp_subsys_release(nnp_env%subsys)
     654              :       END IF
     655           30 :       IF (ASSOCIATED(nnp_env%cell)) THEN
     656           30 :          CALL cell_release(nnp_env%cell)
     657              :       END IF
     658           30 :       IF (ASSOCIATED(nnp_env%cell_ref)) THEN
     659           29 :          CALL cell_release(nnp_env%cell_ref)
     660              :       END IF
     661              : 
     662           30 :    END SUBROUTINE nnp_env_release
     663              : 
     664              : ! **************************************************************************************************
     665              : !> \brief Free the allocatable parts of a cell-list cache. Co-located with the
     666              : !>        type definition to avoid creating a USE-cycle through nnp_cell_list.
     667              : !> \param cache cell-list cache whose allocatable image-pool and bin arrays are freed
     668              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     669              : ! **************************************************************************************************
     670           30 :    SUBROUTINE nnp_cell_list_cache_release(cache)
     671              :       TYPE(nnp_cell_list_cache_type), INTENT(INOUT)      :: cache
     672              : 
     673           30 :       IF (ALLOCATED(cache%coord_primary)) DEALLOCATE (cache%coord_primary)
     674           30 :       IF (ALLOCATED(cache%coord_scaled)) DEALLOCATE (cache%coord_scaled)
     675           30 :       IF (ALLOCATED(cache%ref_coord_primary)) DEALLOCATE (cache%ref_coord_primary)
     676           30 :       IF (ALLOCATED(cache%image_atom)) DEALLOCATE (cache%image_atom)
     677           30 :       IF (ALLOCATED(cache%head)) DEALLOCATE (cache%head)
     678           30 :       IF (ALLOCATED(cache%next)) DEALLOCATE (cache%next)
     679           30 :       IF (ALLOCATED(cache%image_shift)) DEALLOCATE (cache%image_shift)
     680           30 :       IF (ALLOCATED(cache%image_translation)) DEALLOCATE (cache%image_translation)
     681           30 :       cache%initialized = .FALSE.
     682           30 :       cache%num_atoms = -1
     683           30 :       cache%n_images = 0
     684           30 :    END SUBROUTINE nnp_cell_list_cache_release
     685              : 
     686              : ! **************************************************************************************************
     687              : !> \brief Free the allocatable parts of a neighbour-interface state. Co-located
     688              : !>        with the type definition to avoid creating a USE-cycle through
     689              : !>        nnp_neighbor_interface.
     690              : !> \param state neighbour-interface state whose pair maps and per-element workspaces are freed
     691              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     692              : ! **************************************************************************************************
     693           60 :    SUBROUTINE nnp_neighbor_interface_state_release(state)
     694              :       TYPE(nnp_neighbor_interface_state_type), &
     695              :          INTENT(INOUT)                                   :: state
     696              : 
     697              :       INTEGER                                            :: i, j
     698              : 
     699           60 :       IF (ALLOCATED(state%pair_map)) THEN
     700           99 :          DO i = 1, SIZE(state%pair_map, 1)
     701          272 :             DO j = 1, SIZE(state%pair_map, 2)
     702          173 :                IF (ALLOCATED(state%pair_map(i, j)%rad_groups)) DEALLOCATE (state%pair_map(i, j)%rad_groups)
     703          173 :                IF (ALLOCATED(state%pair_map(i, j)%ang1_groups)) DEALLOCATE (state%pair_map(i, j)%ang1_groups)
     704          242 :                IF (ALLOCATED(state%pair_map(i, j)%ang2_groups)) DEALLOCATE (state%pair_map(i, j)%ang2_groups)
     705              :             END DO
     706              :          END DO
     707          203 :          DEALLOCATE (state%pair_map)
     708              :       END IF
     709              : 
     710           60 :       IF (ALLOCATED(state%workspace)) THEN
     711           60 :          DO j = 1, SIZE(state%workspace, 2)
     712          129 :             DO i = 1, SIZE(state%workspace, 1)
     713           99 :                CALL nnp_workspace_release(state%workspace(i, j))
     714              :             END DO
     715              :          END DO
     716           99 :          DEALLOCATE (state%workspace)
     717              :       END IF
     718              : 
     719           60 :       IF (ALLOCATED(state%n_rad)) DEALLOCATE (state%n_rad)
     720           60 :       IF (ALLOCATED(state%n_ang)) DEALLOCATE (state%n_ang)
     721           60 :       IF (ALLOCATED(state%n_radgrp)) DEALLOCATE (state%n_radgrp)
     722           60 :       IF (ALLOCATED(state%n_anggrp)) DEALLOCATE (state%n_anggrp)
     723              : 
     724           60 :       state%initialized = .FALSE.
     725           60 :       state%n_ele = 0
     726           60 :    END SUBROUTINE nnp_neighbor_interface_state_release
     727              : 
     728              : ! **************************************************************************************************
     729              : !> \brief Free the allocatable parts of one per-element workspace.
     730              : !> \param workspace per-element workspace whose scratch, cutoff caches and dGdr buffers are freed
     731              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     732              : ! **************************************************************************************************
     733           69 :    SUBROUTINE nnp_workspace_release(workspace)
     734              :       TYPE(nnp_neighbor_workspace_type), INTENT(INOUT)   :: workspace
     735              : 
     736              :       INTEGER                                            :: s
     737              : 
     738           69 :       IF (ALLOCATED(workspace%neighbor%rad)) THEN
     739          240 :          DO s = 1, SIZE(workspace%neighbor%rad)
     740          171 :             IF (ALLOCATED(workspace%neighbor%rad(s)%ind)) DEALLOCATE (workspace%neighbor%rad(s)%ind)
     741          240 :             IF (ALLOCATED(workspace%neighbor%rad(s)%dist)) DEALLOCATE (workspace%neighbor%rad(s)%dist)
     742              :          END DO
     743          240 :          DEALLOCATE (workspace%neighbor%rad)
     744              :       END IF
     745           69 :       IF (ALLOCATED(workspace%neighbor%ang1)) THEN
     746          360 :          DO s = 1, SIZE(workspace%neighbor%ang1)
     747          291 :             IF (ALLOCATED(workspace%neighbor%ang1(s)%ind)) DEALLOCATE (workspace%neighbor%ang1(s)%ind)
     748          360 :             IF (ALLOCATED(workspace%neighbor%ang1(s)%dist)) DEALLOCATE (workspace%neighbor%ang1(s)%dist)
     749              :          END DO
     750          360 :          DEALLOCATE (workspace%neighbor%ang1)
     751              :       END IF
     752           69 :       IF (ALLOCATED(workspace%neighbor%ang2)) THEN
     753          360 :          DO s = 1, SIZE(workspace%neighbor%ang2)
     754          291 :             IF (ALLOCATED(workspace%neighbor%ang2(s)%ind)) DEALLOCATE (workspace%neighbor%ang2(s)%ind)
     755          360 :             IF (ALLOCATED(workspace%neighbor%ang2(s)%dist)) DEALLOCATE (workspace%neighbor%ang2(s)%dist)
     756              :          END DO
     757          360 :          DEALLOCATE (workspace%neighbor%ang2)
     758              :       END IF
     759           69 :       IF (ALLOCATED(workspace%neighbor%n_rad)) DEALLOCATE (workspace%neighbor%n_rad)
     760           69 :       IF (ALLOCATED(workspace%neighbor%n_ang1)) DEALLOCATE (workspace%neighbor%n_ang1)
     761           69 :       IF (ALLOCATED(workspace%neighbor%n_ang2)) DEALLOCATE (workspace%neighbor%n_ang2)
     762          276 :       workspace%neighbor%pbc_copies = -1
     763              : 
     764           69 :       IF (ALLOCATED(workspace%radial_sym)) DEALLOCATE (workspace%radial_sym)
     765           69 :       IF (ALLOCATED(workspace%radial_force)) DEALLOCATE (workspace%radial_force)
     766           69 :       IF (ALLOCATED(workspace%angular_sym)) DEALLOCATE (workspace%angular_sym)
     767           69 :       IF (ALLOCATED(workspace%angular_force)) DEALLOCATE (workspace%angular_force)
     768           69 :       IF (ALLOCATED(workspace%fc_cache1)) DEALLOCATE (workspace%fc_cache1)
     769           69 :       IF (ALLOCATED(workspace%dfc_cache1)) DEALLOCATE (workspace%dfc_cache1)
     770           69 :       IF (ALLOCATED(workspace%fc_cache2)) DEALLOCATE (workspace%fc_cache2)
     771           69 :       IF (ALLOCATED(workspace%dfc_cache2)) DEALLOCATE (workspace%dfc_cache2)
     772           69 :       IF (ALLOCATED(workspace%self_dGdr)) DEALLOCATE (workspace%self_dGdr)
     773              : 
     774           69 :       IF (ALLOCATED(workspace%dGdr_rad)) THEN
     775          240 :          DO s = 1, SIZE(workspace%dGdr_rad)
     776          240 :             IF (ALLOCATED(workspace%dGdr_rad(s)%data)) DEALLOCATE (workspace%dGdr_rad(s)%data)
     777              :          END DO
     778          240 :          DEALLOCATE (workspace%dGdr_rad)
     779              :       END IF
     780           69 :       IF (ALLOCATED(workspace%dGdr_ang_jj)) THEN
     781          360 :          DO s = 1, SIZE(workspace%dGdr_ang_jj)
     782          360 :             IF (ALLOCATED(workspace%dGdr_ang_jj(s)%data)) DEALLOCATE (workspace%dGdr_ang_jj(s)%data)
     783              :          END DO
     784          360 :          DEALLOCATE (workspace%dGdr_ang_jj)
     785              :       END IF
     786           69 :       IF (ALLOCATED(workspace%dGdr_ang_kk)) THEN
     787          360 :          DO s = 1, SIZE(workspace%dGdr_ang_kk)
     788          360 :             IF (ALLOCATED(workspace%dGdr_ang_kk(s)%data)) DEALLOCATE (workspace%dGdr_ang_kk(s)%data)
     789              :          END DO
     790          360 :          DEALLOCATE (workspace%dGdr_ang_kk)
     791              :       END IF
     792              : 
     793           69 :       workspace%max_rad_symf = 0
     794           69 :       workspace%max_ang_symf = 0
     795           69 :       workspace%n_input_nodes = 0
     796           69 :       workspace%cache_cap = 0
     797           69 :    END SUBROUTINE nnp_workspace_release
     798              : 
     799              : ! **************************************************************************************************
     800              : !> \brief Returns various attributes of the nnp environment
     801              : !> \param nnp_env ...
     802              : !> \param nnp_forces ...
     803              : !> \param subsys the particles, molecules,... of this environment
     804              : !> \param atomic_kind_set The set of all atomic kinds involved
     805              : !> \param particle_set The set of all particles
     806              : !> \param local_particles All particles on this particular node
     807              : !> \param molecule_kind_set The set of all different molecule kinds involved
     808              : !> \param molecule_set The set of all molecules
     809              : !> \param local_molecules All molecules on this particular node
     810              : !> \param nnp_input ...
     811              : !> \param force_env_input Pointer to the force_env input section
     812              : !> \param cell The simulation cell
     813              : !> \param cell_ref The reference simulation cell
     814              : !> \param use_ref_cell Logical which indicates if reference
     815              : !>                      simulation cell is used
     816              : !> \param nnp_potential_energy ...
     817              : !> \param virial Dummy virial pointer
     818              : !> \date   2020-10-10
     819              : !> \author Christoph Schran (christoph.schran@rub.de)
     820              : !> \note
     821              : !>      For possible missing arguments see the attributes of
     822              : !>      nnp_type
     823              : ! **************************************************************************************************
     824        65244 :    SUBROUTINE nnp_env_get(nnp_env, nnp_forces, subsys, &
     825              :                           atomic_kind_set, particle_set, local_particles, &
     826              :                           molecule_kind_set, molecule_set, local_molecules, &
     827              :                           nnp_input, force_env_input, cell, cell_ref, &
     828              :                           use_ref_cell, nnp_potential_energy, virial)
     829              : 
     830              :       TYPE(nnp_type), INTENT(IN)                         :: nnp_env
     831              :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: nnp_forces
     832              :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: subsys
     833              :       TYPE(atomic_kind_type), DIMENSION(:), OPTIONAL, &
     834              :          POINTER                                         :: atomic_kind_set
     835              :       TYPE(particle_type), DIMENSION(:), OPTIONAL, &
     836              :          POINTER                                         :: particle_set
     837              :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_particles
     838              :       TYPE(molecule_kind_type), DIMENSION(:), OPTIONAL, &
     839              :          POINTER                                         :: molecule_kind_set
     840              :       TYPE(molecule_type), DIMENSION(:), OPTIONAL, &
     841              :          POINTER                                         :: molecule_set
     842              :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_molecules
     843              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: nnp_input, force_env_input
     844              :       TYPE(cell_type), OPTIONAL, POINTER                 :: cell, cell_ref
     845              :       LOGICAL, INTENT(OUT), OPTIONAL                     :: use_ref_cell
     846              :       REAL(KIND=dp), INTENT(OUT), OPTIONAL               :: nnp_potential_energy
     847              :       TYPE(virial_type), OPTIONAL, POINTER               :: virial
     848              : 
     849              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     850              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     851              :       TYPE(molecule_list_type), POINTER                  :: molecules
     852              :       TYPE(particle_list_type), POINTER                  :: particles
     853              : 
     854        65244 :       NULLIFY (atomic_kinds, particles, molecules, molecule_kinds)
     855              : 
     856        65244 :       IF (PRESENT(nnp_potential_energy)) THEN
     857          832 :          nnp_potential_energy = nnp_env%nnp_potential_energy
     858              :       END IF
     859        65244 :       IF (PRESENT(nnp_forces)) nnp_forces = nnp_env%nnp_forces
     860              : 
     861              :       ! note cell will be overwritten if subsys is associated
     862              :       ! helium_env uses nnp without subsys
     863        65244 :       IF (PRESENT(cell)) cell => nnp_env%cell
     864              : 
     865        65244 :       IF (PRESENT(subsys)) subsys => nnp_env%subsys
     866        65244 :       IF (ASSOCIATED(nnp_env%subsys)) THEN
     867              :          CALL cp_subsys_get(nnp_env%subsys, &
     868              :                             atomic_kinds=atomic_kinds, &
     869              :                             particles=particles, &
     870              :                             molecule_kinds=molecule_kinds, &
     871              :                             molecules=molecules, &
     872              :                             local_molecules=local_molecules, &
     873              :                             local_particles=local_particles, &
     874              :                             virial=virial, &
     875        10072 :                             cell=cell)
     876              :       END IF
     877        65244 :       IF (PRESENT(atomic_kind_set)) atomic_kind_set => atomic_kinds%els
     878        65244 :       IF (PRESENT(particle_set)) particle_set => particles%els
     879        65244 :       IF (PRESENT(molecule_kind_set)) molecule_kind_set => molecule_kinds%els
     880        65244 :       IF (PRESENT(molecule_set)) molecule_set => molecules%els
     881              : 
     882        65244 :       IF (PRESENT(nnp_input)) nnp_input => nnp_env%nnp_input
     883        65244 :       IF (PRESENT(force_env_input)) force_env_input => nnp_env%force_env_input
     884        65244 :       IF (PRESENT(cell_ref)) cell_ref => nnp_env%cell_ref
     885        65244 :       IF (PRESENT(use_ref_cell)) use_ref_cell = nnp_env%use_ref_cell
     886              : 
     887        65244 :    END SUBROUTINE nnp_env_get
     888              : 
     889              : ! **************************************************************************************************
     890              : !> \brief Sets various attributes of the nnp environment
     891              : !> \param nnp_env ...
     892              : !> \param nnp_forces ...
     893              : !> \param subsys the particles, molecules,... of this environment
     894              : !> \param atomic_kind_set The set of all atomic kinds involved
     895              : !> \param particle_set The set of all particles
     896              : !> \param local_particles All particles on this particular node
     897              : !> \param molecule_kind_set The set of all different molecule kinds involved
     898              : !> \param molecule_set The set of all molecules
     899              : !> \param local_molecules All molecules on this particular node
     900              : !> \param nnp_input ...
     901              : !> \param force_env_input Pointer to the force_env input section
     902              : !> \param cell ...
     903              : !> \param cell_ref The reference simulation cell
     904              : !> \param use_ref_cell Logical which indicates if reference
     905              : !>                      simulation cell is used
     906              : !> \param nnp_potential_energy ...
     907              : !> \date   2020-10-10
     908              : !> \author Christoph Schran (christoph.schran@rub.de)
     909              : !> \note
     910              : !>   For possible missing arguments see the attributes of nnp_type
     911              : ! **************************************************************************************************
     912           89 :    SUBROUTINE nnp_env_set(nnp_env, nnp_forces, subsys, &
     913              :                           atomic_kind_set, particle_set, local_particles, &
     914              :                           molecule_kind_set, molecule_set, local_molecules, &
     915              :                           nnp_input, force_env_input, cell, cell_ref, &
     916              :                           use_ref_cell, nnp_potential_energy)
     917              : 
     918              :       TYPE(nnp_type), INTENT(INOUT)                      :: nnp_env
     919              :       REAL(KIND=dp), DIMENSION(:, :), OPTIONAL, POINTER  :: nnp_forces
     920              :       TYPE(cp_subsys_type), OPTIONAL, POINTER            :: subsys
     921              :       TYPE(atomic_kind_type), DIMENSION(:), OPTIONAL, &
     922              :          POINTER                                         :: atomic_kind_set
     923              :       TYPE(particle_type), DIMENSION(:), OPTIONAL, &
     924              :          POINTER                                         :: particle_set
     925              :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_particles
     926              :       TYPE(molecule_kind_type), DIMENSION(:), OPTIONAL, &
     927              :          POINTER                                         :: molecule_kind_set
     928              :       TYPE(molecule_type), DIMENSION(:), OPTIONAL, &
     929              :          POINTER                                         :: molecule_set
     930              :       TYPE(distribution_1d_type), OPTIONAL, POINTER      :: local_molecules
     931              :       TYPE(section_vals_type), OPTIONAL, POINTER         :: nnp_input, force_env_input
     932              :       TYPE(cell_type), OPTIONAL, POINTER                 :: cell, cell_ref
     933              :       LOGICAL, INTENT(IN), OPTIONAL                      :: use_ref_cell
     934              :       REAL(KIND=dp), INTENT(IN), OPTIONAL                :: nnp_potential_energy
     935              : 
     936              :       TYPE(atomic_kind_list_type), POINTER               :: atomic_kinds
     937              :       TYPE(molecule_kind_list_type), POINTER             :: molecule_kinds
     938              :       TYPE(molecule_list_type), POINTER                  :: molecules
     939              :       TYPE(particle_list_type), POINTER                  :: particles
     940              : 
     941           89 :       IF (PRESENT(nnp_potential_energy)) THEN
     942            0 :          nnp_env%nnp_potential_energy = nnp_potential_energy
     943              :       END IF
     944           89 :       IF (PRESENT(nnp_forces)) nnp_env%nnp_forces(:, :) = nnp_forces
     945              : 
     946           89 :       IF (PRESENT(subsys)) THEN
     947           29 :          IF (ASSOCIATED(nnp_env%subsys)) THEN
     948            0 :          IF (.NOT. ASSOCIATED(nnp_env%subsys, subsys)) THEN
     949            0 :             CALL cp_subsys_release(nnp_env%subsys)
     950              :          END IF
     951              :          END IF
     952           29 :          nnp_env%subsys => subsys
     953              :       END IF
     954           89 :       IF (PRESENT(cell)) THEN
     955           30 :          IF (ASSOCIATED(cell)) THEN
     956           30 :             CALL cell_retain(cell)
     957           30 :             CALL cell_release(nnp_env%cell)
     958           30 :             nnp_env%cell => cell
     959              :          END IF
     960           30 :          IF (ASSOCIATED(nnp_env%subsys)) THEN
     961           29 :             CALL cp_subsys_set(nnp_env%subsys, cell=cell)
     962              :          END IF
     963              :       END IF
     964           89 :       IF (PRESENT(atomic_kind_set)) THEN
     965            0 :          CALL atomic_kind_list_create(atomic_kinds, els_ptr=atomic_kind_set)
     966            0 :          CALL cp_subsys_set(nnp_env%subsys, atomic_kinds=atomic_kinds)
     967            0 :          CALL atomic_kind_list_release(atomic_kinds)
     968              :       END IF
     969           89 :       IF (PRESENT(particle_set)) THEN
     970            0 :          CALL particle_list_create(particles, els_ptr=particle_set)
     971            0 :          CALL cp_subsys_set(nnp_env%subsys, particles=particles)
     972            0 :          CALL particle_list_release(particles)
     973              :       END IF
     974           89 :       IF (PRESENT(molecule_kind_set)) THEN
     975            0 :          CALL molecule_kind_list_create(molecule_kinds, els_ptr=molecule_kind_set)
     976            0 :          CALL cp_subsys_set(nnp_env%subsys, molecule_kinds=molecule_kinds)
     977            0 :          CALL molecule_kind_list_release(molecule_kinds)
     978              :       END IF
     979           89 :       IF (PRESENT(molecule_set)) THEN
     980            0 :          CALL molecule_list_create(molecules, els_ptr=molecule_set)
     981            0 :          CALL cp_subsys_set(nnp_env%subsys, molecules=molecules)
     982            0 :          CALL molecule_list_release(molecules)
     983              :       END IF
     984           89 :       IF (PRESENT(local_particles)) THEN
     985           29 :          CALL cp_subsys_set(nnp_env%subsys, local_particles=local_particles)
     986              :       END IF
     987           89 :       IF (PRESENT(local_molecules)) THEN
     988           29 :          CALL cp_subsys_set(nnp_env%subsys, local_molecules=local_molecules)
     989              :       END IF
     990              : 
     991           89 :       IF (PRESENT(nnp_input)) nnp_env%nnp_input => nnp_input
     992           89 :       IF (PRESENT(force_env_input)) THEN
     993           29 :          nnp_env%force_env_input => force_env_input
     994              :       END IF
     995           89 :       IF (PRESENT(cell_ref)) THEN
     996           29 :          CALL cell_retain(cell_ref)
     997           29 :          CALL cell_release(nnp_env%cell_ref)
     998           29 :          nnp_env%cell_ref => cell_ref
     999              :       END IF
    1000           89 :       IF (PRESENT(use_ref_cell)) nnp_env%use_ref_cell = use_ref_cell
    1001           89 :    END SUBROUTINE nnp_env_set
    1002              : 
    1003            0 : END MODULE nnp_environment_types
        

Generated by: LCOV version 2.0-1