LCOV - code coverage report
Current view: top level - src - nnp_environment.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 79.4 % 486 386
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 5 5

            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  Methods dealing with 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
      15              : 
      16              :    USE atomic_kind_types,               ONLY: atomic_kind_type
      17              :    USE bibliography,                    ONLY: Behler2007,&
      18              :                                               Behler2011,&
      19              :                                               Schran2020a,&
      20              :                                               Schran2020b,&
      21              :                                               cite_reference
      22              :    USE cell_types,                      ONLY: cell_type
      23              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      24              :                                               cp_logger_get_default_unit_nr,&
      25              :                                               cp_logger_type
      26              :    USE cp_parser_methods,               ONLY: parser_read_line,&
      27              :                                               parser_search_string
      28              :    USE cp_parser_types,                 ONLY: cp_parser_type,&
      29              :                                               parser_create,&
      30              :                                               parser_release,&
      31              :                                               parser_reset
      32              :    USE cp_subsys_methods,               ONLY: cp_subsys_create
      33              :    USE cp_subsys_types,                 ONLY: cp_subsys_type
      34              :    USE distribution_1d_types,           ONLY: distribution_1d_release,&
      35              :                                               distribution_1d_type
      36              :    USE distribution_methods,            ONLY: distribute_molecules_1d
      37              :    USE input_constants,                 ONLY: nnp_gpu_prec_double,&
      38              :                                               nnp_gpu_prec_mixed,&
      39              :                                               nnp_use_gpu_auto,&
      40              :                                               nnp_use_gpu_off,&
      41              :                                               nnp_use_gpu_on
      42              :    USE input_section_types,             ONLY: section_vals_get,&
      43              :                                               section_vals_get_subs_vals,&
      44              :                                               section_vals_type,&
      45              :                                               section_vals_val_get
      46              :    USE kinds,                           ONLY: default_path_length,&
      47              :                                               default_string_length,&
      48              :                                               dp
      49              :    USE message_passing,                 ONLY: mp_para_env_type
      50              :    USE molecule_kind_types,             ONLY: molecule_kind_type,&
      51              :                                               write_molecule_kind_set
      52              :    USE molecule_types,                  ONLY: molecule_type
      53              :    USE nnp_acsf,                        ONLY: nnp_init_acsf_groups,&
      54              :                                               nnp_sort_acsf,&
      55              :                                               nnp_sort_ele,&
      56              :                                               nnp_write_acsf
      57              :    USE nnp_environment_types,           ONLY: &
      58              :         nnp_actfnct_cos, nnp_actfnct_exp, nnp_actfnct_gaus, nnp_actfnct_invsig, nnp_actfnct_lin, &
      59              :         nnp_actfnct_quad, nnp_actfnct_sig, nnp_actfnct_softplus, nnp_actfnct_tanh, nnp_env_set, &
      60              :         nnp_type
      61              :    USE nnp_gpu,                         ONLY: nnp_gpu_available,&
      62              :                                               nnp_gpu_init_device,&
      63              :                                               nnp_gpu_model_capacity_ok,&
      64              :                                               nnp_gpu_upload_descriptor_statics,&
      65              :                                               nnp_gpu_upload_network
      66              :    USE nnp_model,                       ONLY: nnp_write_arc
      67              :    USE particle_methods,                ONLY: write_fist_particle_coordinates,&
      68              :                                               write_particle_distances,&
      69              :                                               write_structure_data
      70              :    USE particle_types,                  ONLY: particle_type
      71              :    USE periodic_table,                  ONLY: get_ptable_info
      72              : #include "./base/base_uses.f90"
      73              : 
      74              :    IMPLICIT NONE
      75              : 
      76              :    PRIVATE
      77              : 
      78              :    LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .FALSE.
      79              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'nnp_environment'
      80              : 
      81              :    PUBLIC :: nnp_init
      82              :    PUBLIC :: nnp_init_model
      83              : 
      84              : CONTAINS
      85              : 
      86              : ! **************************************************************************************************
      87              : !> \brief Read and initialize all the information for neural network potentials
      88              : !> \param nnp_env ...
      89              : !> \param root_section ...
      90              : !> \param para_env ...
      91              : !> \param force_env_section ...
      92              : !> \param subsys_section ...
      93              : !> \param use_motion_section ...
      94              : !> \date   2020-10-10
      95              : !> \author Christoph Schran (christoph.schran@rub.de)
      96              : ! **************************************************************************************************
      97           58 :    SUBROUTINE nnp_init(nnp_env, root_section, para_env, force_env_section, subsys_section, &
      98              :                        use_motion_section)
      99              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp_env
     100              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: root_section
     101              :       TYPE(mp_para_env_type), INTENT(IN), POINTER        :: para_env
     102              :       TYPE(section_vals_type), INTENT(INOUT), POINTER    :: force_env_section, subsys_section
     103              :       LOGICAL, INTENT(IN)                                :: use_motion_section
     104              : 
     105              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'nnp_init'
     106              : 
     107              :       INTEGER                                            :: handle
     108              :       LOGICAL                                            :: explicit
     109              :       TYPE(cp_subsys_type), POINTER                      :: subsys
     110              :       TYPE(section_vals_type), POINTER                   :: nnp_section
     111              : 
     112           29 :       CALL timeset(routineN, handle)
     113           29 :       CALL cite_reference(Behler2007)
     114           29 :       CALL cite_reference(Behler2011)
     115           29 :       CALL cite_reference(Schran2020a)
     116           29 :       CALL cite_reference(Schran2020b)
     117              : 
     118           29 :       CPASSERT(ASSOCIATED(nnp_env))
     119              : 
     120           29 :       NULLIFY (nnp_section, subsys)
     121              : 
     122           29 :       IF (.NOT. ASSOCIATED(subsys_section)) THEN
     123            0 :          subsys_section => section_vals_get_subs_vals(force_env_section, "SUBSYS")
     124              :       END IF
     125           29 :       nnp_section => section_vals_get_subs_vals(force_env_section, "NNP")
     126           29 :       CALL section_vals_get(nnp_section, explicit=explicit)
     127           29 :       IF (.NOT. explicit) THEN
     128            0 :          CPWARN("NNP section not explicitly stated. Using default file names.")
     129              :       END IF
     130              : 
     131              :       CALL nnp_env_set(nnp_env=nnp_env, nnp_input=nnp_section, &
     132           29 :                        force_env_input=force_env_section)
     133              : 
     134              :       CALL cp_subsys_create(subsys, para_env, root_section, &
     135              :                             force_env_section=force_env_section, subsys_section=subsys_section, &
     136           29 :                             use_motion_section=use_motion_section)
     137              : 
     138              :       CALL nnp_init_subsys(nnp_env=nnp_env, subsys=subsys, &
     139           29 :                            subsys_section=subsys_section)
     140              : 
     141           29 :       CALL timestop(handle)
     142              : 
     143           29 :    END SUBROUTINE nnp_init
     144              : 
     145              : ! **************************************************************************************************
     146              : !> \brief Read and initialize all the information for neural network potentials
     147              : !> \param nnp_env ...
     148              : !> \param subsys ...
     149              : !> \param subsys_section ...
     150              : !> \date   2020-10-10
     151              : !> \author Christoph Schran (christoph.schran@rub.de)
     152              : ! **************************************************************************************************
     153           29 :    SUBROUTINE nnp_init_subsys(nnp_env, subsys, subsys_section)
     154              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp_env
     155              :       TYPE(cp_subsys_type), INTENT(IN), POINTER          :: subsys
     156              :       TYPE(section_vals_type), INTENT(IN), POINTER       :: subsys_section
     157              : 
     158              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'nnp_init_subsys'
     159              : 
     160              :       INTEGER                                            :: handle, natom
     161           29 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     162              :       TYPE(cell_type), POINTER                           :: my_cell, my_cell_ref
     163              :       TYPE(distribution_1d_type), POINTER                :: local_molecules, local_particles
     164           29 :       TYPE(molecule_kind_type), DIMENSION(:), POINTER    :: molecule_kind_set
     165           29 :       TYPE(molecule_type), DIMENSION(:), POINTER         :: molecule_set
     166           29 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     167              : 
     168           29 :       CALL timeset(routineN, handle)
     169              : 
     170              :       NULLIFY (atomic_kind_set, molecule_kind_set, my_cell, my_cell_ref, &
     171           29 :                particle_set, molecule_set, local_molecules, local_particles)
     172              : 
     173           29 :       particle_set => subsys%particles%els
     174           29 :       atomic_kind_set => subsys%atomic_kinds%els
     175           29 :       molecule_kind_set => subsys%molecule_kinds%els
     176           29 :       molecule_set => subsys%molecules%els
     177           29 :       my_cell => subsys%cell
     178           29 :       my_cell_ref => subsys%cell_ref
     179              : 
     180              :       !Print the molecule kind set
     181           29 :       CALL write_molecule_kind_set(molecule_kind_set, subsys_section)
     182              : 
     183              :       !Set up cell
     184              :       CALL nnp_env_set(nnp_env=nnp_env, subsys=subsys, &
     185              :                        cell=my_cell, cell_ref=my_cell_ref, &
     186           29 :                        use_ref_cell=subsys%use_ref_cell)
     187              : 
     188              :       !Print the atomic coordinates
     189           29 :       CALL write_fist_particle_coordinates(particle_set, subsys_section)
     190              :       CALL write_particle_distances(particle_set, cell=my_cell, &
     191           29 :                                     subsys_section=subsys_section)
     192              :       CALL write_structure_data(particle_set, cell=my_cell, &
     193           29 :                                 input_section=subsys_section)
     194              : 
     195              :       !Distribute molecules and atoms using the new data structures
     196              :       CALL distribute_molecules_1d(atomic_kind_set=atomic_kind_set, &
     197              :                                    particle_set=particle_set, &
     198              :                                    local_particles=local_particles, &
     199              :                                    molecule_kind_set=molecule_kind_set, &
     200              :                                    molecule_set=molecule_set, &
     201              :                                    local_molecules=local_molecules, &
     202           29 :                                    force_env_section=nnp_env%force_env_input)
     203              : 
     204           29 :       natom = SIZE(particle_set)
     205              : 
     206           87 :       ALLOCATE (nnp_env%nnp_forces(3, natom))
     207              : 
     208        25333 :       nnp_env%nnp_forces(:, :) = 0.0_dp
     209              : 
     210           29 :       nnp_env%nnp_potential_energy = 0.0_dp
     211              : 
     212              :       ! Set up arrays for calculation:
     213           29 :       nnp_env%num_atoms = natom
     214           87 :       ALLOCATE (nnp_env%ele_ind(natom))
     215           58 :       ALLOCATE (nnp_env%nuc_atoms(natom))
     216           58 :       ALLOCATE (nnp_env%coord(3, natom))
     217           87 :       ALLOCATE (nnp_env%atoms(natom))
     218           58 :       ALLOCATE (nnp_env%sort(natom))
     219           58 :       ALLOCATE (nnp_env%sort_inv(natom))
     220              : 
     221              :       CALL nnp_env_set(nnp_env=nnp_env, &
     222              :                        local_molecules=local_molecules, &
     223           29 :                        local_particles=local_particles)
     224              : 
     225           29 :       CALL distribution_1d_release(local_particles)
     226           29 :       CALL distribution_1d_release(local_molecules)
     227              : 
     228           29 :       CALL nnp_init_model(nnp_env=nnp_env, printtag="NNP")
     229              : 
     230           29 :       CALL timestop(handle)
     231              : 
     232           29 :    END SUBROUTINE nnp_init_subsys
     233              : 
     234              : ! **************************************************************************************************
     235              : !> \brief Initialize the Neural Network Potential
     236              : !> \param nnp_env ...
     237              : !> \param printtag ...
     238              : !> \param gpu_capable .FALSE. for callers whose force evaluation has no device
     239              : !>                    path, such as the path-integral helium-solute coupling.
     240              : !>                    The GPU keywords are then not consulted at all and the
     241              : !>                    environment stays on the CPU, so it can neither claim the
     242              : !>                    per-process device nor make a &FORCE_EVAL NNP lose it.
     243              : !>                    Defaults to .TRUE.
     244              : !> \date   2020-10-10
     245              : !> \author Christoph Schran (christoph.schran@rub.de)
     246              : ! **************************************************************************************************
     247           30 :    SUBROUTINE nnp_init_model(nnp_env, printtag, gpu_capable)
     248              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp_env
     249              :       CHARACTER(LEN=*), INTENT(IN)                       :: printtag
     250              :       LOGICAL, INTENT(IN), OPTIONAL                      :: gpu_capable
     251              : 
     252              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'nnp_init_model'
     253              :       INTEGER, PARAMETER                                 :: def_str_len = 256
     254              : 
     255           30 :       CHARACTER(len=1), ALLOCATABLE, DIMENSION(:)        :: cactfnct
     256              :       CHARACTER(len=2)                                   :: ele
     257              :       CHARACTER(len=def_str_len)                         :: dummy, line
     258              :       CHARACTER(len=default_path_length)                 :: file_name
     259              :       INTEGER                                            :: handle, i, io, j, k, nele, nuc_ele, &
     260              :                                                             symfnct_type, unit_nr
     261              :       LOGICAL                                            :: atom_e_found, explicit, first, found, &
     262              :                                                             my_gpu_capable
     263              :       REAL(KIND=dp)                                      :: energy
     264              :       REAL(KIND=dp), DIMENSION(7)                        :: test_array
     265           30 :       REAL(KIND=dp), DIMENSION(:), POINTER               :: work
     266              :       TYPE(cp_logger_type), POINTER                      :: logger
     267              :       TYPE(cp_parser_type)                               :: parser
     268              :       TYPE(section_vals_type), POINTER                   :: bias_section, model_section
     269              : 
     270           30 :       CALL timeset(routineN, handle)
     271              : 
     272           30 :       NULLIFY (logger)
     273              : 
     274           30 :       logger => cp_get_default_logger()
     275              : 
     276           30 :       unit_nr = cp_logger_get_default_unit_nr(logger)
     277           30 :       IF (unit_nr > 0) THEN
     278           30 :          WRITE (unit_nr, *) ""
     279           30 :          WRITE (unit_nr, *) TRIM(printtag)//"| Neural Network Potential Force Environment"
     280              :       END IF
     281              : 
     282           30 :       model_section => section_vals_get_subs_vals(nnp_env%nnp_input, "MODEL")
     283           30 :       CALL section_vals_get(model_section, n_repetition=nnp_env%n_committee)
     284          120 :       ALLOCATE (nnp_env%atomic_energy(nnp_env%num_atoms, nnp_env%n_committee))
     285           90 :       ALLOCATE (nnp_env%committee_energy(nnp_env%n_committee))
     286          120 :       ALLOCATE (nnp_env%myforce(3, nnp_env%num_atoms, nnp_env%n_committee))
     287          120 :       ALLOCATE (nnp_env%committee_forces(3, nnp_env%num_atoms, nnp_env%n_committee))
     288           90 :       ALLOCATE (nnp_env%committee_stress(3, 3, nnp_env%n_committee))
     289              : 
     290           30 :       CALL section_vals_val_get(nnp_env%nnp_input, "NNP_INPUT_FILE_NAME", c_val=file_name)
     291           30 :       CALL section_vals_val_get(nnp_env%nnp_input, "RAD_SPLINE_N", i_val=nnp_env%rad_spline_n)
     292           30 :       IF (nnp_env%rad_spline_n < 2) THEN
     293            0 :          CPABORT("NNP| RAD_SPLINE_N must be >= 2.")
     294              :       END IF
     295           30 :       CALL section_vals_val_get(nnp_env%nnp_input, "VERLET_SKIN", r_val=nnp_env%verlet_skin)
     296              : 
     297           30 :       my_gpu_capable = .TRUE.
     298           30 :       IF (PRESENT(gpu_capable)) my_gpu_capable = gpu_capable
     299              : 
     300           30 :       CALL parser_create(parser, file_name, para_env=logger%para_env)
     301              : 
     302              :       ! read number of elements and cut_type and check for scale and center
     303           30 :       nnp_env%scale_acsf = .FALSE.
     304           30 :       nnp_env%scale_sigma_acsf = .FALSE.
     305              :       ! Defaults for scale min and max:
     306           30 :       nnp_env%scmin = 0.0_dp
     307           30 :       nnp_env%scmax = 1.0_dp
     308           30 :       nnp_env%center_acsf = .FALSE.
     309           30 :       nnp_env%normnodes = .FALSE.
     310           30 :       nnp_env%n_hlayer = 0
     311              : 
     312           30 :       IF (unit_nr > 0) THEN
     313           30 :          WRITE (unit_nr, *) TRIM(printtag)//"| Reading NNP input from file: ", TRIM(file_name)
     314              :       END IF
     315              : 
     316              :       CALL parser_search_string(parser, "number_of_elements", .TRUE., found, line, &
     317           30 :                                 search_from_begin_of_file=.TRUE.)
     318           30 :       IF (found) THEN
     319           30 :          READ (line, *) dummy, nnp_env%n_ele
     320              :       ELSE
     321              :          CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     322            0 :                        "| number of elements missing in NNP_INPUT_FILE")
     323              :       END IF
     324              : 
     325              :       CALL parser_search_string(parser, "scale_symmetry_functions_sigma", .TRUE., found, &
     326           30 :                                 search_from_begin_of_file=.TRUE.)
     327           30 :       nnp_env%scale_sigma_acsf = found
     328              : 
     329              :       CALL parser_search_string(parser, "scale_symmetry_functions", .TRUE., found, &
     330           30 :                                 search_from_begin_of_file=.TRUE.)
     331           30 :       nnp_env%scale_acsf = found
     332              : 
     333              :       ! parser_search_string matches substrings, so a search for
     334              :       ! "scale_symmetry_functions" also fires on "..._sigma". Re-search from the
     335              :       ! current parser position to confirm the bare keyword appears on its own line.
     336           30 :       CALL parser_search_string(parser, "scale_symmetry_functions", .TRUE., found)
     337           30 :       IF (found .AND. nnp_env%scale_sigma_acsf) THEN
     338            0 :          CPWARN("Ignoring sigma ACSF scaling; both keywords were set.")
     339            0 :          nnp_env%scale_sigma_acsf = .FALSE.
     340           30 :       ELSE IF (.NOT. found .AND. nnp_env%scale_sigma_acsf) THEN
     341            0 :          nnp_env%scale_acsf = .FALSE.
     342              :       END IF
     343              : 
     344              :       CALL parser_search_string(parser, "scale_min_short_atomic", .TRUE., found, line, &
     345           30 :                                 search_from_begin_of_file=.TRUE.)
     346           30 :       IF (found) READ (line, *) dummy, nnp_env%scmin
     347              : 
     348              :       CALL parser_search_string(parser, "scale_max_short_atomic", .TRUE., found, line, &
     349           30 :                                 search_from_begin_of_file=.TRUE.)
     350           30 :       IF (found) READ (line, *) dummy, nnp_env%scmax
     351              : 
     352              :       CALL parser_search_string(parser, "center_symmetry_functions", .TRUE., found, &
     353           30 :                                 search_from_begin_of_file=.TRUE.)
     354           30 :       nnp_env%center_acsf = found
     355              :       ! n2p2 overwrites sigma scaling, if centering is requested:
     356           30 :       IF (nnp_env%scale_sigma_acsf .AND. nnp_env%center_acsf) THEN
     357            0 :          nnp_env%scale_sigma_acsf = .FALSE.
     358              :       END IF
     359              :       ! Print warning if centering and scaling is requested:
     360           30 :       IF (nnp_env%center_acsf .AND. nnp_env%scale_acsf) THEN
     361           30 :          IF ((ABS(nnp_env%scmin) > EPSILON(0.0_dp)*1.0E+4_dp) .OR. (ABS(nnp_env%scmax - 1.0_dp) > EPSILON(0.0_dp)*1.0E+4_dp)) THEN
     362              :             CALL cp_warn(__LOCATION__, &
     363              :                          "Centering and scaling of symmetry functions requested while scale_min_short_atomic != 0 and/or "// &
     364              :                          "scale_max_short_atomic != 1. Make sure that scaling and centering of symmetry functions in CP2K "// &
     365              :                          "is consistent with your training code. "// &
     366            0 :                          "In CP2K: G* = (G - ave(G)) / (max(G) - min(G)) * (Smax - Smin) + Smin")
     367              :          END IF
     368              :       END IF
     369              : 
     370              :       CALL parser_search_string(parser, "normalize_nodes", .TRUE., found, &
     371           30 :                                 search_from_begin_of_file=.TRUE.)
     372           30 :       nnp_env%normnodes = found
     373              : 
     374              :       CALL parser_search_string(parser, "cutoff_type", .TRUE., found, line, &
     375           30 :                                 search_from_begin_of_file=.TRUE.)
     376           30 :       IF (found) THEN
     377           30 :          READ (line, *) dummy, nnp_env%cut_type
     378              :       ELSE
     379              :          CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     380            0 :                        "| no cutoff type specified in NNP_INPUT_FILE")
     381              :       END IF
     382              : 
     383              :       CALL parser_search_string(parser, "global_hidden_layers_short", .TRUE., found, line, &
     384           30 :                                 search_from_begin_of_file=.TRUE.)
     385           30 :       IF (found) THEN
     386           30 :          READ (line, *) dummy, nnp_env%n_hlayer
     387              :       ELSE
     388              :          CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     389            0 :                        "| number of hidden layers missing in NNP_INPUT_FILE")
     390              :       END IF
     391           30 :       nnp_env%n_layer = nnp_env%n_hlayer + 2
     392              : 
     393           30 :       nele = nnp_env%n_ele
     394          159 :       ALLOCATE (nnp_env%rad(nele))
     395          159 :       ALLOCATE (nnp_env%ang(nele))
     396           90 :       ALLOCATE (nnp_env%n_rad(nele))
     397           60 :       ALLOCATE (nnp_env%n_ang(nele))
     398           90 :       ALLOCATE (nnp_env%actfnct(nnp_env%n_hlayer + 1))
     399           90 :       ALLOCATE (cactfnct(nnp_env%n_hlayer + 1))
     400           60 :       ALLOCATE (nnp_env%ele(nele))
     401           60 :       ALLOCATE (nnp_env%nuc_ele(nele))
     402          159 :       ALLOCATE (nnp_env%arc(nele))
     403           99 :       DO i = 1, nele
     404          483 :          ALLOCATE (nnp_env%arc(i)%layer(nnp_env%n_layer))
     405          237 :          ALLOCATE (nnp_env%arc(i)%n_nodes(nnp_env%n_layer))
     406              :       END DO
     407           90 :       ALLOCATE (nnp_env%n_hnodes(nnp_env%n_hlayer))
     408           90 :       ALLOCATE (nnp_env%atom_energies(nele))
     409           99 :       nnp_env%atom_energies = 0.0_dp
     410              : 
     411              :       ! read elements, broadcast and sort
     412           30 :       CALL parser_reset(parser)
     413              :       DO
     414           60 :          CALL parser_search_string(parser, "elements", .TRUE., found, line)
     415           60 :          IF (found) THEN
     416           60 :             READ (line, *) dummy
     417           60 :             IF (TRIM(ADJUSTL(dummy)) == "elements") THEN
     418           99 :                READ (line, *) dummy, nnp_env%ele(:)
     419           30 :                CALL nnp_sort_ele(nnp_env%ele, nnp_env%nuc_ele)
     420           30 :                EXIT
     421              :             END IF
     422              :          ELSE
     423              :             CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     424            0 :                           "| elements not specified in NNP_INPUT_FILE")
     425              :          END IF
     426              :       END DO
     427              : 
     428              :       CALL parser_search_string(parser, "remove_atom_energies", .TRUE., atom_e_found, &
     429           30 :                                 search_from_begin_of_file=.TRUE.)
     430              : 
     431           30 :       IF (atom_e_found) THEN
     432            0 :          CALL parser_reset(parser)
     433            0 :          i = 0
     434              :          DO
     435            0 :             CALL parser_search_string(parser, "atom_energy", .TRUE., found, line)
     436            0 :             IF (found) THEN
     437            0 :                READ (line, *) dummy, ele, energy
     438            0 :                DO j = 1, nele
     439            0 :                   IF (nnp_env%ele(j) == TRIM(ele)) THEN
     440            0 :                      i = i + 1
     441            0 :                      nnp_env%atom_energies(j) = energy
     442              :                   END IF
     443              :                END DO
     444            0 :                IF (i == nele) EXIT
     445              :             ELSE
     446              :                CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     447            0 :                              "| atom energies are not specified")
     448              :             END IF
     449              :          END DO
     450              :       END IF
     451              : 
     452              :       CALL parser_search_string(parser, "global_nodes_short", .TRUE., found, line, &
     453           30 :                                 search_from_begin_of_file=.TRUE.)
     454           30 :       IF (found) THEN
     455           90 :          READ (line, *) dummy, nnp_env%n_hnodes(:)
     456              :       ELSE
     457              :          CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     458            0 :                        "NNP| global_nodes_short not specified in NNP_INPUT_FILE")
     459              :       END IF
     460              : 
     461              :       CALL parser_search_string(parser, "global_activation_short", .TRUE., found, line, &
     462           30 :                                 search_from_begin_of_file=.TRUE.)
     463           30 :       IF (found) THEN
     464           30 :          READ (line, *) dummy, cactfnct(:)
     465              :       ELSE
     466              :          CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     467            0 :                        "| global_activation_short not specified in NNP_INPUT_FILE")
     468              :       END IF
     469              : 
     470          120 :       DO i = 1, nnp_env%n_hlayer + 1
     471           30 :          SELECT CASE (cactfnct(i))
     472              :          CASE ("t")
     473           60 :             nnp_env%actfnct(i) = nnp_actfnct_tanh
     474              :          CASE ("g")
     475            0 :             nnp_env%actfnct(i) = nnp_actfnct_gaus
     476              :          CASE ("l")
     477           30 :             nnp_env%actfnct(i) = nnp_actfnct_lin
     478              :          CASE ("c")
     479            0 :             nnp_env%actfnct(i) = nnp_actfnct_cos
     480              :          CASE ("s")
     481            0 :             nnp_env%actfnct(i) = nnp_actfnct_sig
     482              :          CASE ("S")
     483            0 :             nnp_env%actfnct(i) = nnp_actfnct_invsig
     484              :          CASE ("e")
     485            0 :             nnp_env%actfnct(i) = nnp_actfnct_exp
     486              :          CASE ("p")
     487            0 :             nnp_env%actfnct(i) = nnp_actfnct_softplus
     488              :          CASE ("h")
     489            0 :             nnp_env%actfnct(i) = nnp_actfnct_quad
     490              :          CASE DEFAULT
     491              :             CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     492           90 :                           "| Activation function unkown")
     493              :          END SELECT
     494              :       END DO
     495              : 
     496              :       ! determine n_rad and n_ang
     497           99 :       DO i = 1, nele
     498           69 :          nnp_env%n_rad(i) = 0
     499           99 :          nnp_env%n_ang(i) = 0
     500              :       END DO
     501              : 
     502              :       ! count symfunctions
     503           30 :       CALL parser_reset(parser)
     504           30 :       first = .TRUE.
     505              :       DO
     506         2793 :          CALL parser_search_string(parser, "symfunction_short", .TRUE., found, line)
     507         2793 :          IF (found) THEN
     508         2763 :             READ (line, *) dummy, ele, symfnct_type
     509        10907 :             DO i = 1, nele
     510        10907 :                IF (TRIM(ele) == nnp_env%ele(i)) THEN
     511         2763 :                   IF (symfnct_type == 2) THEN
     512         1478 :                      nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
     513         1285 :                   ELSE IF (symfnct_type == 3) THEN
     514         1285 :                      nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
     515              :                   ELSE
     516              :                      CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     517            0 :                                    "| Symmetry function type not supported")
     518              :                   END IF
     519              :                END IF
     520              :             END DO
     521              :             first = .FALSE.
     522              :          ELSE
     523           30 :             IF (first) CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     524            0 :                                      "| no symfunction_short specified in NNP_INPUT_FILE")
     525              :             ! no additional symfnct found
     526              :             EXIT
     527              :          END IF
     528              :       END DO
     529              : 
     530           99 :       DO i = 1, nele
     531          207 :          ALLOCATE (nnp_env%rad(i)%y(nnp_env%n_rad(i)))
     532          207 :          ALLOCATE (nnp_env%rad(i)%funccut(nnp_env%n_rad(i)))
     533          207 :          ALLOCATE (nnp_env%rad(i)%eta(nnp_env%n_rad(i)))
     534          207 :          ALLOCATE (nnp_env%rad(i)%rs(nnp_env%n_rad(i)))
     535          207 :          ALLOCATE (nnp_env%rad(i)%loc_min(nnp_env%n_rad(i)))
     536          207 :          ALLOCATE (nnp_env%rad(i)%loc_max(nnp_env%n_rad(i)))
     537          207 :          ALLOCATE (nnp_env%rad(i)%loc_av(nnp_env%n_rad(i)))
     538          207 :          ALLOCATE (nnp_env%rad(i)%sigma(nnp_env%n_rad(i)))
     539          138 :          ALLOCATE (nnp_env%rad(i)%ele(nnp_env%n_rad(i)))
     540          207 :          ALLOCATE (nnp_env%rad(i)%nuc_ele(nnp_env%n_rad(i)))
     541         1547 :          nnp_env%rad(i)%funccut = 0.0_dp
     542         1547 :          nnp_env%rad(i)%eta = 0.0_dp
     543         1547 :          nnp_env%rad(i)%rs = 0.0_dp
     544         1547 :          nnp_env%rad(i)%ele = 'X'
     545         1547 :          nnp_env%rad(i)%nuc_ele = 0
     546              : 
     547          207 :          ALLOCATE (nnp_env%ang(i)%y(nnp_env%n_ang(i)))
     548          207 :          ALLOCATE (nnp_env%ang(i)%funccut(nnp_env%n_ang(i)))
     549          207 :          ALLOCATE (nnp_env%ang(i)%eta(nnp_env%n_ang(i)))
     550          207 :          ALLOCATE (nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
     551          207 :          ALLOCATE (nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)))
     552          207 :          ALLOCATE (nnp_env%ang(i)%lam(nnp_env%n_ang(i)))
     553          207 :          ALLOCATE (nnp_env%ang(i)%loc_min(nnp_env%n_ang(i)))
     554          207 :          ALLOCATE (nnp_env%ang(i)%loc_max(nnp_env%n_ang(i)))
     555          207 :          ALLOCATE (nnp_env%ang(i)%loc_av(nnp_env%n_ang(i)))
     556          207 :          ALLOCATE (nnp_env%ang(i)%sigma(nnp_env%n_ang(i)))
     557          138 :          ALLOCATE (nnp_env%ang(i)%ele1(nnp_env%n_ang(i)))
     558          138 :          ALLOCATE (nnp_env%ang(i)%ele2(nnp_env%n_ang(i)))
     559          207 :          ALLOCATE (nnp_env%ang(i)%nuc_ele1(nnp_env%n_ang(i)))
     560          207 :          ALLOCATE (nnp_env%ang(i)%nuc_ele2(nnp_env%n_ang(i)))
     561         1354 :          nnp_env%ang(i)%funccut = 0.0_dp
     562         1354 :          nnp_env%ang(i)%eta = 0.0_dp
     563         1354 :          nnp_env%ang(i)%zeta = 0.0_dp
     564         1354 :          nnp_env%ang(i)%prefzeta = 1.0_dp
     565         1354 :          nnp_env%ang(i)%lam = 0.0_dp
     566         1354 :          nnp_env%ang(i)%ele1 = 'X'
     567         1354 :          nnp_env%ang(i)%ele2 = 'X'
     568         1354 :          nnp_env%ang(i)%nuc_ele1 = 0
     569         1354 :          nnp_env%ang(i)%nuc_ele2 = 0
     570              : 
     571              :          ! set number of nodes
     572           69 :          nnp_env%arc(i)%n_nodes(1) = nnp_env%n_rad(i) + nnp_env%n_ang(i)
     573          207 :          nnp_env%arc(i)%n_nodes(2:nnp_env%n_layer - 1) = nnp_env%n_hnodes
     574           69 :          nnp_env%arc(i)%n_nodes(nnp_env%n_layer) = 1
     575          375 :          DO j = 1, nnp_env%n_layer
     576          828 :             ALLOCATE (nnp_env%arc(i)%layer(j)%node(nnp_env%arc(i)%n_nodes(j)))
     577          828 :             ALLOCATE (nnp_env%arc(i)%layer(j)%node_grad(nnp_env%arc(i)%n_nodes(j)))
     578         1173 :             ALLOCATE (nnp_env%arc(i)%layer(j)%tmp_der(nnp_env%arc(i)%n_nodes(1), nnp_env%arc(i)%n_nodes(j)))
     579              :          END DO
     580              :       END DO
     581              : 
     582              :       ! read, bcast and sort symfnct parameters
     583           99 :       DO i = 1, nele
     584           69 :          nnp_env%n_rad(i) = 0
     585           99 :          nnp_env%n_ang(i) = 0
     586              :       END DO
     587           30 :       CALL parser_reset(parser)
     588           30 :       first = .TRUE.
     589           30 :       nnp_env%max_cut = 0.0_dp
     590              :       DO
     591         2793 :          CALL parser_search_string(parser, "symfunction_short", .TRUE., found, line)
     592         2793 :          IF (found) THEN
     593         2763 :             READ (line, *) dummy, ele, symfnct_type
     594        10907 :             DO i = 1, nele
     595        10907 :                IF (TRIM(ele) == nnp_env%ele(i)) THEN
     596         2763 :                   IF (symfnct_type == 2) THEN
     597         1478 :                      nnp_env%n_rad(i) = nnp_env%n_rad(i) + 1
     598         1478 :                      READ (line, *) dummy, ele, symfnct_type, &
     599         1478 :                         nnp_env%rad(i)%ele(nnp_env%n_rad(i)), &
     600         1478 :                         nnp_env%rad(i)%eta(nnp_env%n_rad(i)), &
     601         1478 :                         nnp_env%rad(i)%rs(nnp_env%n_rad(i)), &
     602         2956 :                         nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
     603         1478 :                      IF (nnp_env%max_cut < nnp_env%rad(i)%funccut(nnp_env%n_rad(i))) THEN
     604           31 :                         nnp_env%max_cut = nnp_env%rad(i)%funccut(nnp_env%n_rad(i))
     605              :                      END IF
     606         1285 :                   ELSE IF (symfnct_type == 3) THEN
     607         1285 :                      nnp_env%n_ang(i) = nnp_env%n_ang(i) + 1
     608         1285 :                      READ (line, *) dummy, ele, symfnct_type, &
     609         1285 :                         nnp_env%ang(i)%ele1(nnp_env%n_ang(i)), &
     610         1285 :                         nnp_env%ang(i)%ele2(nnp_env%n_ang(i)), &
     611         1285 :                         nnp_env%ang(i)%eta(nnp_env%n_ang(i)), &
     612         1285 :                         nnp_env%ang(i)%lam(nnp_env%n_ang(i)), &
     613         1285 :                         nnp_env%ang(i)%zeta(nnp_env%n_ang(i)), &
     614         2570 :                         nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
     615              :                      nnp_env%ang(i)%prefzeta(nnp_env%n_ang(i)) = &
     616         1285 :                         2.0_dp**(1.0_dp - nnp_env%ang(i)%zeta(nnp_env%n_ang(i)))
     617         1285 :                      IF (nnp_env%max_cut < nnp_env%ang(i)%funccut(nnp_env%n_ang(i))) THEN
     618            0 :                         nnp_env%max_cut = nnp_env%ang(i)%funccut(nnp_env%n_ang(i))
     619              :                      END IF
     620              :                   ELSE
     621              :                      CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     622            0 :                                    "| Symmetry function type not supported")
     623              :                   END IF
     624              :                END IF
     625              :             END DO
     626              :             first = .FALSE.
     627              :          ELSE
     628           30 :             IF (first) CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     629            0 :                                      "| no symfunction_short specified in NNP_INPUT_FILE")
     630              :             ! no additional symfnct found
     631              :             EXIT
     632              :          END IF
     633              :       END DO
     634              : 
     635           99 :       DO i = 1, nele
     636         1547 :          DO j = 1, nnp_env%n_rad(i)
     637         1547 :             CALL get_ptable_info(nnp_env%rad(i)%ele(j), number=nnp_env%rad(i)%nuc_ele(j))
     638              :          END DO
     639         1384 :          DO j = 1, nnp_env%n_ang(i)
     640         1285 :             CALL get_ptable_info(nnp_env%ang(i)%ele1(j), number=nnp_env%ang(i)%nuc_ele1(j))
     641         1285 :             CALL get_ptable_info(nnp_env%ang(i)%ele2(j), number=nnp_env%ang(i)%nuc_ele2(j))
     642              :             ! sort ele1 and ele2
     643         1354 :             IF (nnp_env%ang(i)%nuc_ele1(j) > nnp_env%ang(i)%nuc_ele2(j)) THEN
     644          349 :                ele = nnp_env%ang(i)%ele1(j)
     645          349 :                nnp_env%ang(i)%ele1(j) = nnp_env%ang(i)%ele2(j)
     646          349 :                nnp_env%ang(i)%ele2(j) = ele
     647          349 :                nuc_ele = nnp_env%ang(i)%nuc_ele1(j)
     648          349 :                nnp_env%ang(i)%nuc_ele1(j) = nnp_env%ang(i)%nuc_ele2(j)
     649          349 :                nnp_env%ang(i)%nuc_ele2(j) = nuc_ele
     650              :             END IF
     651              :          END DO
     652              :       END DO
     653              :       ! Done with input.nn file
     654           30 :       CALL parser_release(parser)
     655              : 
     656              :       ! sort symmetry functions and output information
     657           30 :       CALL nnp_sort_acsf(nnp_env)
     658           30 :       CALL nnp_write_acsf(nnp_env, logger%para_env, printtag)
     659           30 :       CALL nnp_write_arc(nnp_env, logger%para_env, printtag)
     660              : 
     661              :       ! read scaling information from file
     662           30 :       IF (nnp_env%scale_acsf .OR. nnp_env%center_acsf .OR. nnp_env%scale_sigma_acsf) THEN
     663           30 :          IF (unit_nr > 0) THEN
     664           30 :             WRITE (unit_nr, *) TRIM(printtag)//"| Reading scaling information from file: ", TRIM(file_name)
     665              :          END IF
     666              :          CALL section_vals_val_get(nnp_env%nnp_input, "SCALE_FILE_NAME", &
     667           30 :                                    c_val=file_name)
     668           30 :          CALL parser_create(parser, file_name, para_env=logger%para_env)
     669              : 
     670              :          ! Get number of elements in scaling file
     671           30 :          CALL parser_read_line(parser, 1)
     672           30 :          k = 0
     673          239 :          DO WHILE (k < 7)
     674          210 :             READ (parser%input_line, *, IOSTAT=io) test_array(1:k)
     675          210 :             IF (io == -1) EXIT
     676          210 :             k = k + 1
     677              :          END DO
     678           30 :          k = k - 1
     679              : 
     680           30 :          IF (k == 5 .AND. nnp_env%scale_sigma_acsf) THEN
     681            0 :             CPABORT("Sigma scaling requested, but scaling.data does not contain sigma.")
     682              :          END IF
     683              : 
     684           30 :          CALL parser_reset(parser)
     685           99 :          DO i = 1, nnp_env%n_ele
     686         1547 :             DO j = 1, nnp_env%n_rad(i)
     687         1478 :                CALL parser_read_line(parser, 1)
     688         1547 :                IF (nnp_env%scale_sigma_acsf) THEN
     689            0 :                   READ (parser%input_line, *) dummy, dummy, &
     690            0 :                      nnp_env%rad(i)%loc_min(j), &
     691            0 :                      nnp_env%rad(i)%loc_max(j), &
     692            0 :                      nnp_env%rad(i)%loc_av(j), &
     693            0 :                      nnp_env%rad(i)%sigma(j)
     694              :                ELSE
     695         1478 :                   READ (parser%input_line, *) dummy, dummy, &
     696         1478 :                      nnp_env%rad(i)%loc_min(j), &
     697         1478 :                      nnp_env%rad(i)%loc_max(j), &
     698         2956 :                      nnp_env%rad(i)%loc_av(j)
     699              :                END IF
     700              :             END DO
     701         1384 :             DO j = 1, nnp_env%n_ang(i)
     702         1285 :                CALL parser_read_line(parser, 1)
     703         1354 :                IF (nnp_env%scale_sigma_acsf) THEN
     704            0 :                   READ (parser%input_line, *) dummy, dummy, &
     705            0 :                      nnp_env%ang(i)%loc_min(j), &
     706            0 :                      nnp_env%ang(i)%loc_max(j), &
     707            0 :                      nnp_env%ang(i)%loc_av(j), &
     708            0 :                      nnp_env%ang(i)%sigma(j)
     709              :                ELSE
     710         1285 :                   READ (parser%input_line, *) dummy, dummy, &
     711         1285 :                      nnp_env%ang(i)%loc_min(j), &
     712         1285 :                      nnp_env%ang(i)%loc_max(j), &
     713         2570 :                      nnp_env%ang(i)%loc_av(j)
     714              :                END IF
     715              :             END DO
     716              :          END DO
     717           30 :          CALL parser_release(parser)
     718              : 
     719              :          ! Reject degenerate scaling.data at load time. The (loc_max - loc_min)
     720              :          ! and sigma denominators are used unguarded inside nnp_scale_acsf;
     721              :          ! a zero range or zero sigma there produces silent NaN forces.
     722           30 :          IF (nnp_env%scale_acsf) THEN
     723           99 :             DO i = 1, nnp_env%n_ele
     724         1547 :                DO j = 1, nnp_env%n_rad(i)
     725         1547 :                   IF (nnp_env%rad(i)%loc_max(j) <= nnp_env%rad(i)%loc_min(j)) THEN
     726              :                      WRITE (line, '(A,I0,A,I0,A,2(1X,ES13.6))') &
     727            0 :                         "scaling.data: radial sf range non-positive for element ", i, &
     728            0 :                         " sf ", j, " (loc_min, loc_max) =", &
     729            0 :                         nnp_env%rad(i)%loc_min(j), nnp_env%rad(i)%loc_max(j)
     730            0 :                      CPABORT(TRIM(line))
     731              :                   END IF
     732              :                END DO
     733         1384 :                DO j = 1, nnp_env%n_ang(i)
     734         1354 :                   IF (nnp_env%ang(i)%loc_max(j) <= nnp_env%ang(i)%loc_min(j)) THEN
     735              :                      WRITE (line, '(A,I0,A,I0,A,2(1X,ES13.6))') &
     736            0 :                         "scaling.data: angular sf range non-positive for element ", i, &
     737            0 :                         " sf ", j, " (loc_min, loc_max) =", &
     738            0 :                         nnp_env%ang(i)%loc_min(j), nnp_env%ang(i)%loc_max(j)
     739            0 :                      CPABORT(TRIM(line))
     740              :                   END IF
     741              :                END DO
     742              :             END DO
     743              :          END IF
     744           30 :          IF (nnp_env%scale_sigma_acsf) THEN
     745            0 :             DO i = 1, nnp_env%n_ele
     746            0 :                DO j = 1, nnp_env%n_rad(i)
     747            0 :                   IF (nnp_env%rad(i)%sigma(j) <= 0.0_dp) THEN
     748              :                      WRITE (line, '(A,I0,A,I0,A,1X,ES13.6)') &
     749            0 :                         "scaling.data: radial sf sigma non-positive for element ", i, &
     750            0 :                         " sf ", j, " sigma =", nnp_env%rad(i)%sigma(j)
     751            0 :                      CPABORT(TRIM(line))
     752              :                   END IF
     753              :                END DO
     754            0 :                DO j = 1, nnp_env%n_ang(i)
     755            0 :                   IF (nnp_env%ang(i)%sigma(j) <= 0.0_dp) THEN
     756              :                      WRITE (line, '(A,I0,A,I0,A,1X,ES13.6)') &
     757            0 :                         "scaling.data: angular sf sigma non-positive for element ", i, &
     758            0 :                         " sf ", j, " sigma =", nnp_env%ang(i)%sigma(j)
     759            0 :                      CPABORT(TRIM(line))
     760              :                   END IF
     761              :                END DO
     762              :             END DO
     763              :          END IF
     764              :       END IF
     765              : 
     766           30 :       CALL nnp_init_acsf_groups(nnp_env)
     767              : 
     768              :       ! Allocate per-layer weight tables, then load each committee member
     769              :       ! from disk. Allocation lives at the call site because n_committee /
     770              :       ! n_layer / n_nodes are owned here; the loader only fills them.
     771           99 :       DO i = 1, nnp_env%n_ele
     772          306 :          DO j = 2, nnp_env%n_layer
     773            0 :             ALLOCATE (nnp_env%arc(i)%layer(j)%weights(nnp_env%arc(i)%n_nodes(j - 1), &
     774         1035 :                                                       nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
     775          897 :             ALLOCATE (nnp_env%arc(i)%layer(j)%bweights(nnp_env%arc(i)%n_nodes(j), nnp_env%n_committee))
     776              :          END DO
     777              :       END DO
     778           30 :       CALL nnp_read_committee_weights(nnp_env, model_section, printtag)
     779              : 
     780              :       ! Resolve USE_GPU only now that the model definition is fully read: the
     781              :       ! resolution sizes the model against the compiled-in device-table
     782              :       ! capacities, which needs the group tables populated above.
     783           30 :       IF (my_gpu_capable) THEN
     784           29 :          CALL nnp_resolve_gpu(nnp_env, printtag, unit_nr, logger)
     785              :       ELSE
     786              :          ! This caller has no device force path, and its section carries no GPU
     787              :          ! keywords to read.
     788            1 :          nnp_env%use_gpu = nnp_use_gpu_off
     789            1 :          nnp_env%gpu_precision = nnp_gpu_prec_double
     790              :       END IF
     791              : 
     792              :       ! Upload the model-static descriptor tables (per-element scaling
     793              :       ! parameters and per-(group, element) symmetry-function maps) to the
     794              :       ! device once the model definition is fully read; the per-step descriptor
     795              :       ! kernels read them from device memory.
     796           30 :       IF (nnp_env%use_gpu == nnp_use_gpu_on) THEN
     797            0 :          CALL nnp_gpu_upload_descriptor_statics(nnp_env)
     798            0 :          CALL nnp_gpu_upload_network(nnp_env)
     799              :       END IF
     800              : 
     801           30 :       nnp_env%expol = 0
     802              : 
     803              :       ! Bias the standard deviation of committee disagreement
     804           30 :       NULLIFY (bias_section)
     805           30 :       explicit = .FALSE.
     806              :       !HELIUM NNP does not currently define a bias term
     807           30 :       bias_section => section_vals_get_subs_vals(nnp_env%nnp_input, "BIAS", can_return_null=.TRUE.)
     808           30 :       IF (ASSOCIATED(bias_section)) CALL section_vals_get(bias_section, explicit=explicit)
     809           30 :       nnp_env%bias = .FALSE.
     810           30 :       IF (explicit) THEN
     811            4 :          IF (nnp_env%n_committee > 1) THEN
     812            4 :             IF (unit_nr > 0) THEN
     813            4 :                WRITE (unit_nr, *) "NNP| Biasing of committee disagreement enabled"
     814              :             END IF
     815            4 :             nnp_env%bias = .TRUE.
     816           12 :             ALLOCATE (nnp_env%bias_forces(3, nnp_env%num_atoms))
     817           12 :             ALLOCATE (nnp_env%bias_e_avrg(nnp_env%n_committee))
     818            4 :             CALL section_vals_val_get(bias_section, "SIGMA_0", r_val=nnp_env%bias_sigma0)
     819            4 :             CALL section_vals_val_get(bias_section, "K_B", r_val=nnp_env%bias_kb)
     820           36 :             nnp_env%bias_e_avrg(:) = 0.0_dp
     821            4 :             CALL section_vals_val_get(bias_section, "ALIGN_NNP_ENERGIES", explicit=explicit)
     822            4 :             nnp_env%bias_align = explicit
     823            4 :             IF (explicit) THEN
     824            4 :                NULLIFY (work)
     825            4 :                CALL section_vals_val_get(bias_section, "ALIGN_NNP_ENERGIES", r_vals=work)
     826            4 :                IF (SIZE(work) /= nnp_env%n_committee) THEN
     827            0 :                   CPABORT("ALIGN_NNP_ENERGIES size mismatch wrt committee size.")
     828              :                END IF
     829           68 :                nnp_env%bias_e_avrg(:) = work
     830            4 :                IF (unit_nr > 0) THEN
     831            4 :                   WRITE (unit_nr, *) TRIM(printtag)//"| Biasing is aligned by shifting the energy prediction of the C-NNP members"
     832              :                END IF
     833              :             END IF
     834              :          ELSE
     835            0 :             CPWARN("NNP committee size is 1, BIAS section is ignored.")
     836              :          END IF
     837              :       END IF
     838              : 
     839           30 :       IF (unit_nr > 0) THEN
     840           30 :          WRITE (unit_nr, *) TRIM(printtag)//"| NNP force environment initialized"
     841              :       END IF
     842              : 
     843           30 :       CALL timestop(handle)
     844              : 
     845          150 :    END SUBROUTINE nnp_init_model
     846              : 
     847              : ! **************************************************************************************************
     848              : !> \brief Resolve &NNP / USE_GPU and GPU_PRECISION once, before any force code
     849              : !>        runs, and claim the device when the GPU path is selected.
     850              : !>
     851              : !> AUTO probes for an offload device and otherwise falls back to the CPU path;
     852              : !> builds without NNP GPU support always report no device. The resolved value is
     853              : !> either nnp_use_gpu_on or nnp_use_gpu_off, so the force evaluation never has
     854              : !> to interpret AUTO.
     855              : !>
     856              : !> \param nnp_env  NNP environment; use_gpu and gpu_precision are set here.
     857              : !> \param printtag Log-line prefix.
     858              : !> \param unit_nr  Output unit, or a non-positive value when there is none.
     859              : !> \param logger   Default logger, for its parallel environment.
     860              : !> \author Dhruv Sharma (ds2173@cam.ac.uk)
     861              : ! **************************************************************************************************
     862           29 :    SUBROUTINE nnp_resolve_gpu(nnp_env, printtag, unit_nr, logger)
     863              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp_env
     864              :       CHARACTER(LEN=*), INTENT(IN)                       :: printtag
     865              :       INTEGER, INTENT(IN)                                :: unit_nr
     866              :       TYPE(cp_logger_type), POINTER                      :: logger
     867              : 
     868              :       INTEGER, PARAMETER :: gpu_auto_min_atoms_per_rank = 1000
     869              : 
     870              :       CHARACTER(LEN=2*default_string_length)             :: cap_reason
     871              :       INTEGER                                            :: n_gpu_ranks, use_gpu_input
     872              :       LOGICAL                                            :: gpu_accepted, gpu_everywhere
     873              : 
     874           29 :       CALL section_vals_val_get(nnp_env%nnp_input, "USE_GPU", i_val=use_gpu_input)
     875           29 :       CALL section_vals_val_get(nnp_env%nnp_input, "VALIDATE", l_val=nnp_env%validate_gpu)
     876              : 
     877              :       ! The outcome has to be the same on every rank. A rank-dependent answer
     878              :       ! would either hang (some ranks abort below while the rest go on into the
     879              :       ! collectives) or run a silent mix of CPU and GPU arithmetic, so count the
     880              :       ! ranks that see a device and decide from the total.
     881           29 :       n_gpu_ranks = MERGE(1, 0, nnp_gpu_available())
     882           29 :       CALL logger%para_env%sum(n_gpu_ranks)
     883           29 :       gpu_everywhere = (n_gpu_ranks == logger%para_env%num_pe)
     884              : 
     885            0 :       SELECT CASE (use_gpu_input)
     886              :       CASE (nnp_use_gpu_off)
     887            0 :          nnp_env%use_gpu = nnp_use_gpu_off
     888              :       CASE (nnp_use_gpu_on)
     889            0 :          IF (n_gpu_ranks == 0) THEN
     890              :             CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     891              :                           "| USE_GPU ON requested, but this build has no NNP GPU "// &
     892            0 :                           "support or no device is available on this node.")
     893            0 :          ELSE IF (.NOT. gpu_everywhere) THEN
     894              :             CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     895              :                           "| USE_GPU ON requested, but only some of the MPI ranks see "// &
     896            0 :                           "a device. Give every rank one, or set USE_GPU OFF.")
     897              :          END IF
     898            0 :          nnp_env%use_gpu = nnp_use_gpu_on
     899              :       CASE (nnp_use_gpu_auto)
     900              :          ! Below roughly gpu_auto_min_atoms_per_rank rank-local atoms the kernel
     901              :          ! launch and transfer overhead outweighs the device throughput, so AUTO
     902              :          ! keeps such systems on the CPU path rather than making an existing
     903              :          ! input slower, and its trajectory irreproducible, the moment it is run
     904              :          ! on an accelerated build. USE_GPU ON overrides the threshold, and so
     905              :          ! does VALIDATE: a validation run exists to exercise the GPU path, so
     906              :          ! it engages the device whenever the build and the machine support it,
     907              :          ! regardless of the system size.
     908           29 :          IF (gpu_everywhere .AND. &
     909              :              (nnp_env%validate_gpu .OR. &
     910            0 :               nnp_env%num_atoms >= gpu_auto_min_atoms_per_rank*logger%para_env%num_pe)) THEN
     911            0 :             nnp_env%use_gpu = nnp_use_gpu_on
     912              :          ELSE
     913           29 :             nnp_env%use_gpu = nnp_use_gpu_off
     914              :          END IF
     915              :       CASE DEFAULT
     916           29 :          CPABORT("Unknown USE_GPU value.")
     917              :       END SELECT
     918              : 
     919              :       CALL section_vals_val_get(nnp_env%nnp_input, "GPU_PRECISION", &
     920           29 :                                 i_val=nnp_env%gpu_precision)
     921              : 
     922              :       ! A model larger than the compiled-in device-table capacities cannot take
     923              :       ! the GPU path. Sized here, before the device is claimed and before the
     924              :       ! banner prints, so an oversized model falls back cleanly under AUTO and
     925              :       ! aborts with the limit named under ON.
     926           29 :       IF (nnp_env%use_gpu == nnp_use_gpu_on) THEN
     927            0 :          IF (.NOT. nnp_gpu_model_capacity_ok(nnp_env, cap_reason)) THEN
     928            0 :             nnp_env%use_gpu = nnp_use_gpu_off
     929            0 :             IF (use_gpu_input == nnp_use_gpu_on) THEN
     930              :                CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     931              :                              "| USE_GPU ON requested, but "//TRIM(cap_reason)// &
     932              :                              ". Set USE_GPU OFF, or raise the limit in "// &
     933            0 :                              "src/nnp/gpu/nnp_gpu_state.h and rebuild.")
     934              :             END IF
     935            0 :             IF (unit_nr > 0) THEN
     936              :                WRITE (unit_nr, *) TRIM(printtag)//"| GPU offload: declined, "// &
     937            0 :                   TRIM(cap_reason)//"; using the CPU path"
     938              :             END IF
     939              :          END IF
     940              :       END IF
     941              : 
     942           29 :       IF (nnp_env%use_gpu /= nnp_use_gpu_on) RETURN
     943              : 
     944              :       ! Claim the device and push the precision selector; any failure aborts
     945              :       ! here rather than mid-MD. The claim is declined
     946              :       ! when another NNP environment in this process already holds the device,
     947              :       ! which is a hard error only if the user asked for the GPU explicitly.
     948            0 :       CALL nnp_gpu_init_device(nnp_env%gpu_precision, gpu_accepted)
     949            0 :       IF (.NOT. gpu_accepted) THEN
     950            0 :          nnp_env%use_gpu = nnp_use_gpu_off
     951            0 :          IF (use_gpu_input == nnp_use_gpu_on) THEN
     952              :             CALL cp_abort(__LOCATION__, TRIM(printtag)// &
     953              :                           "| USE_GPU ON requested, but another NNP environment in this "// &
     954              :                           "process already uses the GPU, and only one may. Set USE_GPU "// &
     955            0 :                           "OFF on this section.")
     956              :          END IF
     957            0 :          IF (unit_nr > 0) THEN
     958              :             WRITE (unit_nr, *) TRIM(printtag)//"| GPU offload: declined, another NNP "// &
     959            0 :                "environment holds the device; using the CPU path"
     960              :          END IF
     961            0 :       ELSE IF (unit_nr > 0) THEN
     962            0 :          IF (nnp_env%gpu_precision == nnp_gpu_prec_mixed) THEN
     963            0 :             WRITE (unit_nr, *) TRIM(printtag)//"| GPU offload: enabled (MIXED precision)"
     964              :          ELSE
     965            0 :             WRITE (unit_nr, *) TRIM(printtag)//"| GPU offload: enabled (DOUBLE precision)"
     966              :          END IF
     967            0 :          IF (nnp_env%validate_gpu) THEN
     968            0 :             WRITE (unit_nr, *) TRIM(printtag)//"| GPU validation: CPU reference runs in shadow mode"
     969              :          END IF
     970              :       END IF
     971              : 
     972              :    END SUBROUTINE nnp_resolve_gpu
     973              : 
     974              : ! **************************************************************************************************
     975              : !> \brief Read committee weights from disk into pre-allocated arc layer tables.
     976              : !>
     977              : !> One file per (committee member, element), named <base>.<nuc>.data. The file
     978              : !> is a flat list of doubles; the order on disk is layer-major over (i->j weights
     979              : !> then biases) so a single counter walks every value into its slot.
     980              : !>
     981              : !> \param nnp_env       NNP environment with arc()%layer()%weights/bweights already allocated.
     982              : !> \param model_section &MODEL section block; supplies the per-member WEIGHTS base path.
     983              : !> \param printtag      Log-line prefix.
     984              : ! **************************************************************************************************
     985           30 :    SUBROUTINE nnp_read_committee_weights(nnp_env, model_section, printtag)
     986              :       TYPE(nnp_type), INTENT(INOUT), POINTER             :: nnp_env
     987              :       TYPE(section_vals_type), POINTER                   :: model_section
     988              :       CHARACTER(LEN=*), INTENT(IN)                       :: printtag
     989              : 
     990              :       CHARACTER(len=default_path_length)                 :: base_name, file_name
     991              :       INTEGER                                            :: i, i_com, iweight, j, k, l, n_weight, &
     992              :                                                             unit_nr
     993              :       LOGICAL                                            :: at_end
     994           30 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:)           :: weights
     995              :       TYPE(cp_logger_type), POINTER                      :: logger
     996              :       TYPE(cp_parser_type)                               :: parser
     997              : 
     998           30 :       NULLIFY (logger)
     999           60 :       logger => cp_get_default_logger()
    1000           30 :       unit_nr = cp_logger_get_default_unit_nr(logger)
    1001              : 
    1002          221 :       DO i_com = 1, nnp_env%n_committee
    1003          191 :          CALL section_vals_val_get(model_section, "WEIGHTS", c_val=base_name, i_rep_section=i_com)
    1004          191 :          IF (unit_nr > 0) THEN
    1005          191 :             WRITE (unit_nr, *) TRIM(printtag)//"| Initializing weights for model: ", i_com
    1006              :          END IF
    1007          612 :          DO i = 1, nnp_env%n_ele
    1008          391 :             WRITE (file_name, '(A,I0.3,A)') TRIM(base_name)//".", nnp_env%nuc_ele(i), ".data"
    1009          391 :             IF (unit_nr > 0) THEN
    1010          391 :                WRITE (unit_nr, *) TRIM(printtag)//"| Reading weights from file: ", TRIM(file_name)
    1011              :             END IF
    1012          391 :             CALL parser_create(parser, file_name, para_env=logger%para_env)
    1013          391 :             n_weight = 0
    1014       430301 :             DO WHILE (.TRUE.)
    1015       430692 :                CALL parser_read_line(parser, 1, at_end)
    1016       430692 :                IF (at_end) EXIT
    1017       430301 :                n_weight = n_weight + 1
    1018              :             END DO
    1019              : 
    1020         1173 :             ALLOCATE (weights(n_weight))
    1021              : 
    1022          391 :             CALL parser_reset(parser)
    1023       430692 :             DO j = 1, n_weight
    1024       430301 :                CALL parser_read_line(parser, 1)
    1025       430692 :                READ (parser%input_line, *) weights(j)
    1026              :             END DO
    1027          391 :             CALL parser_release(parser)
    1028              : 
    1029              :             ! sort weights into corresponding arrays
    1030          391 :             iweight = 0
    1031         1564 :             DO j = 2, nnp_env%n_layer
    1032        28943 :                DO k = 1, nnp_env%arc(i)%n_nodes(j - 1)
    1033       443023 :                   DO l = 1, nnp_env%arc(i)%n_nodes(j)
    1034       414080 :                      iweight = iweight + 1
    1035       441850 :                      nnp_env%arc(i)%layer(j)%weights(k, l, i_com) = weights(iweight)
    1036              :                   END DO
    1037              :                END DO
    1038              : 
    1039        17785 :                DO k = 1, nnp_env%arc(i)%n_nodes(j)
    1040        16221 :                   iweight = iweight + 1
    1041        17394 :                   nnp_env%arc(i)%layer(j)%bweights(k, i_com) = weights(iweight)
    1042              :                END DO
    1043              :             END DO
    1044              : 
    1045          973 :             DEALLOCATE (weights)
    1046              :          END DO
    1047              :       END DO
    1048              : 
    1049          120 :    END SUBROUTINE nnp_read_committee_weights
    1050              : 
    1051              : END MODULE nnp_environment
        

Generated by: LCOV version 2.0-1